ASTConsumer.h revision d1bac8d46740eb00085ec816af0829fd75fb4d5c
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
17namespace clang {
18  class ASTContext;
19  class CXXRecordDecl;
20  class Decl;
21  class DeclGroupRef;
22  class HandleTagDeclDefinition;
23  class PPMutationListener;
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 Invoked when a function is implicitly instantiated.
75  /// Note that at this point point it does not have a body, its body is
76  /// instantiated at the end of the translation unit and passed to
77  /// HandleTopLevelDecl.
78  virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {}
79
80  /// \brief Handle the specified top-level declaration that occurred inside
81  /// and ObjC container.
82  /// The default implementation ignored them.
83  virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D);
84
85  /// \brief Handle an ImportDecl that was implicitly created due to an
86  /// inclusion directive.
87  /// The default implementation passes it to HandleTopLevelDecl.
88  virtual void HandleImplicitImportDecl(ImportDecl *D);
89
90  /// CompleteTentativeDefinition - Callback invoked at the end of a translation
91  /// unit to notify the consumer that the given tentative definition should be
92  /// completed.
93  ///
94  /// The variable declaration itself will be a tentative
95  /// definition. If it had an incomplete array type, its type will
96  /// have already been changed to an array of size 1. However, the
97  /// declaration remains a tentative definition and has not been
98  /// modified by the introduction of an implicit zero initializer.
99  virtual void CompleteTentativeDefinition(VarDecl *D) {}
100
101  /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this
102  // variable has been instantiated.
103  virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D) {}
104
105  /// \brief Callback involved at the end of a translation unit to
106  /// notify the consumer that a vtable for the given C++ class is
107  /// required.
108  ///
109  /// \param RD The class whose vtable was used.
110  ///
111  /// \param DefinitionRequired Whether a definition of this vtable is
112  /// required in this translation unit; otherwise, it is only needed if
113  /// it was actually used.
114  virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {}
115
116  /// \brief If the consumer is interested in preprocessor entities getting
117  /// modified after their initial creation, it should return a pointer to
118  /// a PPMutationListener here.
119  virtual PPMutationListener *GetPPMutationListener() { return 0; }
120
121  /// \brief If the consumer is interested in entities getting modified after
122  /// their initial creation, it should return a pointer to
123  /// an ASTMutationListener here.
124  virtual ASTMutationListener *GetASTMutationListener() { return 0; }
125
126  /// \brief If the consumer is interested in entities being deserialized from
127  /// AST files, it should return a pointer to a ASTDeserializationListener here
128  virtual ASTDeserializationListener *GetASTDeserializationListener() {
129    return 0;
130  }
131
132  /// PrintStats - If desired, print any statistics.
133  virtual void PrintStats() {}
134
135  /// \brief This callback is called for each function if the Parser was
136  /// initialized with \c SkipFunctionBodies set to \c true.
137  ///
138  /// \return \c true if the function's body should be skipped. The function
139  /// body may be parsed anyway if it is needed (for instance, if it contains
140  /// the code completion point or is constexpr).
141  virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
142};
143
144} // end namespace clang.
145
146#endif
147