1//===--- ASTConsumer.h - Abstract interface for reading ASTs ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file defines the ASTConsumer class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_ASTCONSUMER_H
14#define LLVM_CLANG_AST_ASTCONSUMER_H
15
16namespace clang {
17  class ASTContext;
18  class CXXMethodDecl;
19  class CXXRecordDecl;
20  class Decl;
21  class DeclGroupRef;
22  class ASTMutationListener;
23  class ASTDeserializationListener; // layering violation because void* is ugly
24  class SemaConsumer; // layering violation required for safe SemaConsumer
25  class TagDecl;
26  class VarDecl;
27  class FunctionDecl;
28  class ImportDecl;
29
30/// ASTConsumer - This is an abstract interface that should be implemented by
31/// clients that read ASTs.  This abstraction layer allows the client to be
32/// independent of the AST producer (e.g. parser vs AST dump file reader, etc).
33class ASTConsumer {
34  /// Whether this AST consumer also requires information about
35  /// semantic analysis.
36  bool SemaConsumer = false;
37
38  friend class SemaConsumer;
39
40public:
41  ASTConsumer() = default;
42
43  virtual ~ASTConsumer() {}
44
45  /// Initialize - This is called to initialize the consumer, providing the
46  /// ASTContext.
47  virtual void Initialize(ASTContext &Context) {}
48
49  /// HandleTopLevelDecl - Handle the specified top-level declaration.  This is
50  /// called by the parser to process every top-level Decl*.
51  ///
52  /// \returns true to continue parsing, or false to abort parsing.
53  virtual bool HandleTopLevelDecl(DeclGroupRef D);
54
55  /// This callback is invoked each time an inline (method or friend)
56  /// function definition in a class is completed.
57  virtual void HandleInlineFunctionDefinition(FunctionDecl *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  /// This callback is invoked the first time each TagDecl is required to
75  /// be complete.
76  virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) {}
77
78  /// Invoked when a function is implicitly instantiated.
79  /// Note that at this 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  /// 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  /// 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  /// CompleteTentativeDefinition - Callback invoked at the end of a translation
95  /// unit to notify the consumer that the given tentative definition should be
96  /// completed.
97  ///
98  /// The variable declaration itself will be a tentative
99  /// definition. If it had an incomplete array type, its type will
100  /// have already been changed to an array of size 1. However, the
101  /// declaration remains a tentative definition and has not been
102  /// modified by the introduction of an implicit zero initializer.
103  virtual void CompleteTentativeDefinition(VarDecl *D) {}
104
105  /// CompleteExternalDeclaration - Callback invoked at the end of a translation
106  /// unit to notify the consumer that the given external declaration should be
107  /// completed.
108  virtual void CompleteExternalDeclaration(VarDecl *D) {}
109
110  /// Callback invoked when an MSInheritanceAttr has been attached to a
111  /// CXXRecordDecl.
112  virtual void AssignInheritanceModel(CXXRecordDecl *RD) {}
113
114  /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this
115  // variable has been instantiated.
116  virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D) {}
117
118  /// Callback involved at the end of a translation unit to
119  /// notify the consumer that a vtable for the given C++ class is
120  /// required.
121  ///
122  /// \param RD The class whose vtable was used.
123  virtual void HandleVTable(CXXRecordDecl *RD) {}
124
125  /// If the consumer is interested in entities getting modified after
126  /// their initial creation, it should return a pointer to
127  /// an ASTMutationListener here.
128  virtual ASTMutationListener *GetASTMutationListener() { return nullptr; }
129
130  /// If the consumer is interested in entities being deserialized from
131  /// AST files, it should return a pointer to a ASTDeserializationListener here
132  virtual ASTDeserializationListener *GetASTDeserializationListener() {
133    return nullptr;
134  }
135
136  /// PrintStats - If desired, print any statistics.
137  virtual void PrintStats() {}
138
139  /// This callback is called for each function if the Parser was
140  /// initialized with \c SkipFunctionBodies set to \c true.
141  ///
142  /// \return \c true if the function's body should be skipped. The function
143  /// body may be parsed anyway if it is needed (for instance, if it contains
144  /// the code completion point or is constexpr).
145  virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
146};
147
148} // end namespace clang.
149
150#endif
151