1556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner//===--- ASTConsumer.h - Abstract interface for reading ASTs ----*- C++ -*-===// 2556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// 3556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// The LLVM Compiler Infrastructure 4556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 7556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// 8556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner//===----------------------------------------------------------------------===// 9556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// 10556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// This file defines the ASTConsumer class. 11556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner// 12556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner//===----------------------------------------------------------------------===// 13556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner 14556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner#ifndef LLVM_CLANG_AST_ASTCONSUMER_H 15556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner#define LLVM_CLANG_AST_ASTCONSUMER_H 16556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner 173190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner#include "llvm/ADT/StringRef.h" 183190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner 19556beb71b8820ed5243e385ffcc91433a494c170Chris Lattnernamespace clang { 20556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner class ASTContext; 216fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor class CXXRecordDecl; 22d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith class Decl; 23682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner class DeclGroupRef; 242ae34ed8caf90ca4044feca07811561053c08666Chris Lattner class HandleTagDeclDefinition; 257b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis class ASTMutationListener; 26571db7f0cb31789737be92fce1c1b738e6dbe795Sebastian Redl class ASTDeserializationListener; // layering violation because void* is ugly 27e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor class SemaConsumer; // layering violation required for safe SemaConsumer 28ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl class TagDecl; 29b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor class VarDecl; 306d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis class FunctionDecl; 31d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis class ImportDecl; 32e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor 33556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner/// ASTConsumer - This is an abstract interface that should be implemented by 34556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner/// clients that read ASTs. This abstraction layer allows the client to be 35556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner/// independent of the AST producer (e.g. parser vs AST dump file reader, etc). 36556beb71b8820ed5243e385ffcc91433a494c170Chris Lattnerclass ASTConsumer { 37e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor /// \brief Whether this AST consumer also requires information about 38e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor /// semantic analysis. 39e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor bool SemaConsumer; 40e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor 41e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor friend class SemaConsumer; 42e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor 43556beb71b8820ed5243e385ffcc91433a494c170Chris Lattnerpublic: 44e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor ASTConsumer() : SemaConsumer(false) { } 45e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor 46682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner virtual ~ASTConsumer() {} 471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner /// Initialize - This is called to initialize the consumer, providing the 493926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// ASTContext. 5095041a2029a069386ee67439f6d0fb524a9d184fTed Kremenek virtual void Initialize(ASTContext &Context) {} 511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 522ff3e691ecc63a5d82d4023b46b5b47ce14ace53Ted Kremenek /// HandleTopLevelDecl - Handle the specified top-level declaration. This is 533926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// called by the parser to process every top-level Decl*. Note that D can be 543926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// the head of a chain of Decls (e.g. for `int a, b` the chain will have two 553926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// elements). Use Decl::getNextDeclarator() to walk the chain. 5688c2596edc8eb475e20f6033de1ea01669695a0cArgyrios Kyrtzidis /// 5788c2596edc8eb475e20f6033de1ea01669695a0cArgyrios Kyrtzidis /// \returns true to continue parsing, or false to abort parsing. 5888c2596edc8eb475e20f6033de1ea01669695a0cArgyrios Kyrtzidis virtual bool HandleTopLevelDecl(DeclGroupRef D); 591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6027372b4f1f402e95dd479ecf40c39ca71c15619fSebastian Redl /// HandleInterestingDecl - Handle the specified interesting declaration. This 613c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl /// is called by the AST reader when deserializing things that might interest 6227372b4f1f402e95dd479ecf40c39ca71c15619fSebastian Redl /// the consumer. The default implementation forwards to HandleTopLevelDecl. 6327372b4f1f402e95dd479ecf40c39ca71c15619fSebastian Redl virtual void HandleInterestingDecl(DeclGroupRef D); 6427372b4f1f402e95dd479ecf40c39ca71c15619fSebastian Redl 65c87190da60bca25fe3bc1fe6831916f0076b72b8Ted Kremenek /// HandleTranslationUnit - This method is called when the ASTs for entire 663926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// translation unit have been parsed. 671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump virtual void HandleTranslationUnit(ASTContext &Ctx) {} 681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 692ae34ed8caf90ca4044feca07811561053c08666Chris Lattner /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 702ae34ed8caf90ca4044feca07811561053c08666Chris Lattner /// (e.g. struct, union, enum, class) is completed. This allows the client to 712ae34ed8caf90ca4044feca07811561053c08666Chris Lattner /// hack on the type, which can occur at any point in the file (because these 722ae34ed8caf90ca4044feca07811561053c08666Chris Lattner /// can be defined in declspecs). 732ae34ed8caf90ca4044feca07811561053c08666Chris Lattner virtual void HandleTagDeclDefinition(TagDecl *D) {} 741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 75658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie /// \brief This callback is invoked the first time each TagDecl is required to 76658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie /// be complete. 77658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) {} 78658cd2c287b1a0b419f51cd18e5a48d4560d1c56David Blaikie 796d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis /// \brief Invoked when a function is implicitly instantiated. 806d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis /// Note that at this point point it does not have a body, its body is 816d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis /// instantiated at the end of the translation unit and passed to 826d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis /// HandleTopLevelDecl. 836d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {} 846d968363877388f0a0268711d59367907b465ae1Argyrios Kyrtzidis 85b4a686df4de21ec4eeca69211b21f7fe716abeaeArgyrios Kyrtzidis /// \brief Handle the specified top-level declaration that occurred inside 86b4a686df4de21ec4eeca69211b21f7fe716abeaeArgyrios Kyrtzidis /// and ObjC container. 87b4a686df4de21ec4eeca69211b21f7fe716abeaeArgyrios Kyrtzidis /// The default implementation ignored them. 88b4a686df4de21ec4eeca69211b21f7fe716abeaeArgyrios Kyrtzidis virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D); 89b4a686df4de21ec4eeca69211b21f7fe716abeaeArgyrios Kyrtzidis 90d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis /// \brief Handle an ImportDecl that was implicitly created due to an 91d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis /// inclusion directive. 92d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis /// The default implementation passes it to HandleTopLevelDecl. 93d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis virtual void HandleImplicitImportDecl(ImportDecl *D); 94d3d981627c375b187f33cc92a034a77ac329ec47Argyrios Kyrtzidis 953190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner /// \brief Handle a pragma that appends to Linker Options. Currently this 963190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner /// only exists to support Microsoft's #pragma comment(linker, "/foo"). 973190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {} 983190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner 99a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman /// \brief Handle a pragma that emits a mismatch identifier and value to the 100a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman /// object file for the linker to work with. Currently, this only exists to 101a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman /// support Microsoft's #pragma detect_mismatch. 102a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman virtual void HandleDetectMismatch(llvm::StringRef Name, 103a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman llvm::StringRef Value) {} 104a7ff62f9443efa3b13a28a1e566d4625b15b8553Aaron Ballman 1053190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner /// \brief Handle a dependent library created by a pragma in the source. 1063190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner /// Currently this only exists to support Microsoft's 1073190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner /// #pragma comment(lib, "/foo"). 1083190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner virtual void HandleDependentLibrary(llvm::StringRef Lib) {} 1093190ca922d3743137e15fe0c525c04b177b9983bReid Kleckner 1103926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// CompleteTentativeDefinition - Callback invoked at the end of a translation 1113926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// unit to notify the consumer that the given tentative definition should be 1123926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar /// completed. 113b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// 114b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// The variable declaration itself will be a tentative 115b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// definition. If it had an incomplete array type, its type will 116b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// have already been changed to an array of size 1. However, the 117b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// declaration remains a tentative definition and has not been 118b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor /// modified by the introduction of an implicit zero initializer. 119b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor virtual void CompleteTentativeDefinition(VarDecl *D) {} 120b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 121025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola /// HandleCXXStaticMemberVarInstantiation - Tell the consumer that this 122025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola // variable has been instantiated. 123025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *D) {} 124234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola 1256fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// \brief Callback involved at the end of a translation unit to 1266fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// notify the consumer that a vtable for the given C++ class is 1276fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// required. 1286fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// 1296fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// \param RD The class whose vtable was used. 1306fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// 1316fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// \param DefinitionRequired Whether a definition of this vtable is 1326fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// required in this translation unit; otherwise, it is only needed if 1336fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor /// it was actually used. 1346fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {} 1356fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor 1367b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis /// \brief If the consumer is interested in entities getting modified after 1377b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis /// their initial creation, it should return a pointer to 13858a2cd8c0d52e710cbcc57a67eac7b51b0b831c4Sebastian Redl /// an ASTMutationListener here. 1397b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis virtual ASTMutationListener *GetASTMutationListener() { return 0; } 1407b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis 141ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl /// \brief If the consumer is interested in entities being deserialized from 142571db7f0cb31789737be92fce1c1b738e6dbe795Sebastian Redl /// AST files, it should return a pointer to a ASTDeserializationListener here 143ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie virtual ASTDeserializationListener *GetASTDeserializationListener() { 144ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie return 0; 145ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie } 146ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl 147556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner /// PrintStats - If desired, print any statistics. 1483926a1fe92f622f194d7d99560d0b17b2af65a61Daniel Dunbar virtual void PrintStats() {} 149d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith 150d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// \brief This callback is called for each function if the Parser was 151d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// initialized with \c SkipFunctionBodies set to \c true. 152d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// 153d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// \return \c true if the function's body should be skipped. The function 154d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// body may be parsed anyway if it is needed (for instance, if it contains 155d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith /// the code completion point or is constexpr). 156d1bac8d46740eb00085ec816af0829fd75fb4d5cRichard Smith virtual bool shouldSkipFunctionBody(Decl *D) { return true; } 157556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner}; 158556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner 159556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner} // end namespace clang. 160556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner 161556beb71b8820ed5243e385ffcc91433a494c170Chris Lattner#endif 162