ASTConsumer.h revision 3190ca922d3743137e15fe0c525c04b177b9983b
1//===--- ASTConsumer.h - Abstract interface for reading ASTs ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the ASTConsumer class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_ASTCONSUMER_H
15#define LLVM_CLANG_AST_ASTCONSUMER_H
16
17#include "llvm/ADT/StringRef.h"
18
19namespace clang {
20  class ASTContext;
21  class CXXRecordDecl;
22  class Decl;
23  class DeclGroupRef;
24  class HandleTagDeclDefinition;
25  class ASTMutationListener;
26  class ASTDeserializationListener; // layering violation because void* is ugly
27  class SemaConsumer; // layering violation required for safe SemaConsumer
28  class TagDecl;
29  class VarDecl;
30  class FunctionDecl;
31  class ImportDecl;
32
33/// ASTConsumer - This is an abstract interface that should be implemented by
34/// clients that read ASTs.  This abstraction layer allows the client to be
35/// independent of the AST producer (e.g. parser vs AST dump file reader, etc).
36class ASTConsumer {
37  /// \brief Whether this AST consumer also requires information about
38  /// semantic analysis.
39  bool SemaConsumer;
40
41  friend class SemaConsumer;
42
43public:
44  ASTConsumer() : SemaConsumer(false) { }
45
46  virtual ~ASTConsumer() {}
47
48  /// Initialize - This is called to initialize the consumer, providing the
49  /// ASTContext.
50  virtual void Initialize(ASTContext &Context) {}
51
52  /// HandleTopLevelDecl - Handle the specified top-level declaration.  This is
53  /// called by the parser to process every top-level Decl*. Note that D can be
54  /// the head of a chain of Decls (e.g. for `int a, b` the chain will have two
55  /// elements). Use Decl::getNextDeclarator() to walk the chain.
56  ///
57  /// \returns true to continue parsing, or false to abort parsing.
58  virtual bool HandleTopLevelDecl(DeclGroupRef D);
59
60  /// HandleInterestingDecl - Handle the specified interesting declaration. This
61  /// is called by the AST reader when deserializing things that might interest
62  /// the consumer. The default implementation forwards to HandleTopLevelDecl.
63  virtual void HandleInterestingDecl(DeclGroupRef D);
64
65  /// HandleTranslationUnit - This method is called when the ASTs for entire
66  /// translation unit have been parsed.
67  virtual void HandleTranslationUnit(ASTContext &Ctx) {}
68
69  /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
70  /// (e.g. struct, union, enum, class) is completed.  This allows the client to
71  /// hack on the type, which can occur at any point in the file (because these
72  /// can be defined in declspecs).
73  virtual void HandleTagDeclDefinition(TagDecl *D) {}
74
75  /// \brief Invoked when a function is implicitly instantiated.
76  /// Note that at this point point it does not have a body, its body is
77  /// instantiated at the end of the translation unit and passed to
78  /// HandleTopLevelDecl.
79  virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {}
80
81  /// \brief Handle the specified top-level declaration that occurred inside
82  /// and ObjC container.
83  /// The default implementation ignored them.
84  virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D);
85
86  /// \brief Handle an ImportDecl that was implicitly created due to an
87  /// inclusion directive.
88  /// The default implementation passes it to HandleTopLevelDecl.
89  virtual void HandleImplicitImportDecl(ImportDecl *D);
90
91  /// \brief Handle a pragma that appends to Linker Options.  Currently this
92  /// only exists to support Microsoft's #pragma comment(linker, "/foo").
93  virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {}
94
95  /// \brief Handle a dependent library created by a pragma in the source.
96  /// Currently this only exists to support Microsoft's
97  /// #pragma comment(lib, "/foo").
98  virtual void HandleDependentLibrary(llvm::StringRef Lib) {}
99
100  /// CompleteTentativeDefinition - Callback invoked at the end of a translation
101  /// unit to notify the consumer that the given tentative definition should be
102  /// completed.
103  ///
104  /// The variable declaration itself will be a tentative
105  /// definition. If it had an incomplete array type, its type will
106  /// have already been changed to an array of size 1. However, the
107  /// declaration remains a tentative definition and has not been
108  /// modified by the introduction of an implicit zero initializer.
109  virtual void CompleteTentativeDefinition(VarDecl *D) {}
110
111  /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this
112  // variable has been instantiated.
113  virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D) {}
114
115  /// \brief Callback involved at the end of a translation unit to
116  /// notify the consumer that a vtable for the given C++ class is
117  /// required.
118  ///
119  /// \param RD The class whose vtable was used.
120  ///
121  /// \param DefinitionRequired Whether a definition of this vtable is
122  /// required in this translation unit; otherwise, it is only needed if
123  /// it was actually used.
124  virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {}
125
126  /// \brief If the consumer is interested in entities getting modified after
127  /// their initial creation, it should return a pointer to
128  /// an ASTMutationListener here.
129  virtual ASTMutationListener *GetASTMutationListener() { return 0; }
130
131  /// \brief If the consumer is interested in entities being deserialized from
132  /// AST files, it should return a pointer to a ASTDeserializationListener here
133  virtual ASTDeserializationListener *GetASTDeserializationListener() {
134    return 0;
135  }
136
137  /// PrintStats - If desired, print any statistics.
138  virtual void PrintStats() {}
139
140  /// \brief This callback is called for each function if the Parser was
141  /// initialized with \c SkipFunctionBodies set to \c true.
142  ///
143  /// \return \c true if the function's body should be skipped. The function
144  /// body may be parsed anyway if it is needed (for instance, if it contains
145  /// the code completion point or is constexpr).
146  virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
147};
148
149} // end namespace clang.
150
151#endif
152