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