clang API Documentation

ASTConsumer.h
Go to the documentation of this file.
00001 //===--- ASTConsumer.h - Abstract interface for reading ASTs ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the ASTConsumer class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_AST_ASTCONSUMER_H
00015 #define LLVM_CLANG_AST_ASTCONSUMER_H
00016 
00017 #include "llvm/ADT/StringRef.h"
00018 
00019 namespace clang {
00020   class ASTContext;
00021   class CXXMethodDecl;
00022   class CXXRecordDecl;
00023   class Decl;
00024   class DeclGroupRef;
00025   class ASTMutationListener;
00026   class ASTDeserializationListener; // layering violation because void* is ugly
00027   class SemaConsumer; // layering violation required for safe SemaConsumer
00028   class TagDecl;
00029   class VarDecl;
00030   class FunctionDecl;
00031   class ImportDecl;
00032 
00033 /// ASTConsumer - This is an abstract interface that should be implemented by
00034 /// clients that read ASTs.  This abstraction layer allows the client to be
00035 /// independent of the AST producer (e.g. parser vs AST dump file reader, etc).
00036 class ASTConsumer {
00037   /// \brief Whether this AST consumer also requires information about
00038   /// semantic analysis.
00039   bool SemaConsumer;
00040 
00041   friend class SemaConsumer;
00042 
00043 public:
00044   ASTConsumer() : SemaConsumer(false) { }
00045 
00046   virtual ~ASTConsumer() {}
00047 
00048   /// Initialize - This is called to initialize the consumer, providing the
00049   /// ASTContext.
00050   virtual void Initialize(ASTContext &Context) {}
00051 
00052   /// HandleTopLevelDecl - Handle the specified top-level declaration.  This is
00053   /// called by the parser to process every top-level Decl*.
00054   ///
00055   /// \returns true to continue parsing, or false to abort parsing.
00056   virtual bool HandleTopLevelDecl(DeclGroupRef D);
00057 
00058   /// \brief This callback is invoked each time an inline method definition is
00059   /// completed.
00060   virtual void HandleInlineMethodDefinition(CXXMethodDecl *D) {}
00061 
00062   /// HandleInterestingDecl - Handle the specified interesting declaration. This
00063   /// is called by the AST reader when deserializing things that might interest
00064   /// the consumer. The default implementation forwards to HandleTopLevelDecl.
00065   virtual void HandleInterestingDecl(DeclGroupRef D);
00066 
00067   /// HandleTranslationUnit - This method is called when the ASTs for entire
00068   /// translation unit have been parsed.
00069   virtual void HandleTranslationUnit(ASTContext &Ctx) {}
00070 
00071   /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
00072   /// (e.g. struct, union, enum, class) is completed.  This allows the client to
00073   /// hack on the type, which can occur at any point in the file (because these
00074   /// can be defined in declspecs).
00075   virtual void HandleTagDeclDefinition(TagDecl *D) {}
00076 
00077   /// \brief This callback is invoked the first time each TagDecl is required to
00078   /// be complete.
00079   virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) {}
00080 
00081   /// \brief Invoked when a function is implicitly instantiated.
00082   /// Note that at this point point it does not have a body, its body is
00083   /// instantiated at the end of the translation unit and passed to
00084   /// HandleTopLevelDecl.
00085   virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {}
00086 
00087   /// \brief Handle the specified top-level declaration that occurred inside
00088   /// and ObjC container.
00089   /// The default implementation ignored them.
00090   virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D);
00091 
00092   /// \brief Handle an ImportDecl that was implicitly created due to an
00093   /// inclusion directive.
00094   /// The default implementation passes it to HandleTopLevelDecl.
00095   virtual void HandleImplicitImportDecl(ImportDecl *D);
00096 
00097   /// \brief Handle a pragma that appends to Linker Options.  Currently this
00098   /// only exists to support Microsoft's #pragma comment(linker, "/foo").
00099   virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {}
00100 
00101   /// \brief Handle a pragma that emits a mismatch identifier and value to the
00102   /// object file for the linker to work with.  Currently, this only exists to
00103   /// support Microsoft's #pragma detect_mismatch.
00104   virtual void HandleDetectMismatch(llvm::StringRef Name,
00105                                     llvm::StringRef Value) {}
00106 
00107   /// \brief Handle a dependent library created by a pragma in the source.
00108   /// Currently this only exists to support Microsoft's
00109   /// #pragma comment(lib, "/foo").
00110   virtual void HandleDependentLibrary(llvm::StringRef Lib) {}
00111 
00112   /// CompleteTentativeDefinition - Callback invoked at the end of a translation
00113   /// unit to notify the consumer that the given tentative definition should be
00114   /// completed.
00115   ///
00116   /// The variable declaration itself will be a tentative
00117   /// definition. If it had an incomplete array type, its type will
00118   /// have already been changed to an array of size 1. However, the
00119   /// declaration remains a tentative definition and has not been
00120   /// modified by the introduction of an implicit zero initializer.
00121   virtual void CompleteTentativeDefinition(VarDecl *D) {}
00122 
00123   /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this
00124   // variable has been instantiated.
00125   virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D) {}
00126 
00127   /// \brief Callback involved at the end of a translation unit to
00128   /// notify the consumer that a vtable for the given C++ class is
00129   /// required.
00130   ///
00131   /// \param RD The class whose vtable was used.
00132   ///
00133   /// \param DefinitionRequired Whether a definition of this vtable is
00134   /// required in this translation unit; otherwise, it is only needed if
00135   /// it was actually used.
00136   virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {}
00137 
00138   /// \brief If the consumer is interested in entities getting modified after
00139   /// their initial creation, it should return a pointer to
00140   /// an ASTMutationListener here.
00141   virtual ASTMutationListener *GetASTMutationListener() { return nullptr; }
00142 
00143   /// \brief If the consumer is interested in entities being deserialized from
00144   /// AST files, it should return a pointer to a ASTDeserializationListener here
00145   virtual ASTDeserializationListener *GetASTDeserializationListener() {
00146     return nullptr;
00147   }
00148 
00149   /// PrintStats - If desired, print any statistics.
00150   virtual void PrintStats() {}
00151 
00152   /// \brief This callback is called for each function if the Parser was
00153   /// initialized with \c SkipFunctionBodies set to \c true.
00154   ///
00155   /// \return \c true if the function's body should be skipped. The function
00156   /// body may be parsed anyway if it is needed (for instance, if it contains
00157   /// the code completion point or is constexpr).
00158   virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
00159 };
00160 
00161 } // end namespace clang.
00162 
00163 #endif