Home | History | Annotate | Download | only in Serialization
      1 //===--- ASTReader.h - AST File Reader --------------------------*- 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 ASTReader class, which reads AST files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_FRONTEND_AST_READER_H
     15 #define LLVM_CLANG_FRONTEND_AST_READER_H
     16 
     17 #include "clang/Serialization/ASTBitCodes.h"
     18 #include "clang/Serialization/ContinuousRangeMap.h"
     19 #include "clang/Sema/ExternalSemaSource.h"
     20 #include "clang/AST/DeclarationName.h"
     21 #include "clang/AST/DeclObjC.h"
     22 #include "clang/AST/TemplateBase.h"
     23 #include "clang/Lex/ExternalPreprocessorSource.h"
     24 #include "clang/Lex/HeaderSearch.h"
     25 #include "clang/Lex/PreprocessingRecord.h"
     26 #include "clang/Basic/Diagnostic.h"
     27 #include "clang/Basic/IdentifierTable.h"
     28 #include "clang/Basic/SourceManager.h"
     29 #include "llvm/ADT/APFloat.h"
     30 #include "llvm/ADT/APInt.h"
     31 #include "llvm/ADT/APSInt.h"
     32 #include "llvm/ADT/OwningPtr.h"
     33 #include "llvm/ADT/SmallVector.h"
     34 #include "llvm/ADT/StringRef.h"
     35 #include "llvm/Bitcode/BitstreamReader.h"
     36 #include "llvm/Support/DataTypes.h"
     37 #include <deque>
     38 #include <map>
     39 #include <string>
     40 #include <utility>
     41 #include <vector>
     42 
     43 namespace llvm {
     44   class MemoryBuffer;
     45 }
     46 
     47 namespace clang {
     48 
     49 class AddrLabelExpr;
     50 class ASTConsumer;
     51 class ASTContext;
     52 class ASTIdentifierIterator;
     53 class ASTUnit; // FIXME: Layering violation and egregious hack.
     54 class Attr;
     55 class Decl;
     56 class DeclContext;
     57 class NestedNameSpecifier;
     58 class CXXBaseSpecifier;
     59 class CXXConstructorDecl;
     60 class CXXCtorInitializer;
     61 class GotoStmt;
     62 class MacroDefinition;
     63 class NamedDecl;
     64 class OpaqueValueExpr;
     65 class Preprocessor;
     66 class Sema;
     67 class SwitchCase;
     68 class ASTDeserializationListener;
     69 class ASTWriter;
     70 class ASTReader;
     71 class ASTDeclReader;
     72 class ASTStmtReader;
     73 class ASTIdentifierLookupTrait;
     74 class TypeLocReader;
     75 struct HeaderFileInfo;
     76 class VersionTuple;
     77 
     78 struct PCHPredefinesBlock {
     79   /// \brief The file ID for this predefines buffer in a PCH file.
     80   FileID BufferID;
     81 
     82   /// \brief This predefines buffer in a PCH file.
     83   llvm::StringRef Data;
     84 };
     85 typedef llvm::SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks;
     86 
     87 /// \brief Abstract interface for callback invocations by the ASTReader.
     88 ///
     89 /// While reading an AST file, the ASTReader will call the methods of the
     90 /// listener to pass on specific information. Some of the listener methods can
     91 /// return true to indicate to the ASTReader that the information (and
     92 /// consequently the AST file) is invalid.
     93 class ASTReaderListener {
     94 public:
     95   virtual ~ASTReaderListener();
     96 
     97   /// \brief Receives the language options.
     98   ///
     99   /// \returns true to indicate the options are invalid or false otherwise.
    100   virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
    101     return false;
    102   }
    103 
    104   /// \brief Receives the target triple.
    105   ///
    106   /// \returns true to indicate the target triple is invalid or false otherwise.
    107   virtual bool ReadTargetTriple(llvm::StringRef Triple) {
    108     return false;
    109   }
    110 
    111   /// \brief Receives the contents of the predefines buffer.
    112   ///
    113   /// \param Buffers Information about the predefines buffers.
    114   ///
    115   /// \param OriginalFileName The original file name for the AST file, which
    116   /// will appear as an entry in the predefines buffer.
    117   ///
    118   /// \param SuggestedPredefines If necessary, additional definitions are added
    119   /// here.
    120   ///
    121   /// \returns true to indicate the predefines are invalid or false otherwise.
    122   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
    123                                     llvm::StringRef OriginalFileName,
    124                                     std::string &SuggestedPredefines,
    125                                     FileManager &FileMgr) {
    126     return false;
    127   }
    128 
    129   /// \brief Receives a HeaderFileInfo entry.
    130   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {}
    131 
    132   /// \brief Receives __COUNTER__ value.
    133   virtual void ReadCounter(unsigned Value) {}
    134 };
    135 
    136 /// \brief ASTReaderListener implementation to validate the information of
    137 /// the PCH file against an initialized Preprocessor.
    138 class PCHValidator : public ASTReaderListener {
    139   Preprocessor &PP;
    140   ASTReader &Reader;
    141 
    142   unsigned NumHeaderInfos;
    143 
    144 public:
    145   PCHValidator(Preprocessor &PP, ASTReader &Reader)
    146     : PP(PP), Reader(Reader), NumHeaderInfos(0) {}
    147 
    148   virtual bool ReadLanguageOptions(const LangOptions &LangOpts);
    149   virtual bool ReadTargetTriple(llvm::StringRef Triple);
    150   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
    151                                     llvm::StringRef OriginalFileName,
    152                                     std::string &SuggestedPredefines,
    153                                     FileManager &FileMgr);
    154   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
    155   virtual void ReadCounter(unsigned Value);
    156 
    157 private:
    158   void Error(const char *Msg);
    159 };
    160 
    161 /// \brief Reads an AST files chain containing the contents of a translation
    162 /// unit.
    163 ///
    164 /// The ASTReader class reads bitstreams (produced by the ASTWriter
    165 /// class) containing the serialized representation of a given
    166 /// abstract syntax tree and its supporting data structures. An
    167 /// instance of the ASTReader can be attached to an ASTContext object,
    168 /// which will provide access to the contents of the AST files.
    169 ///
    170 /// The AST reader provides lazy de-serialization of declarations, as
    171 /// required when traversing the AST. Only those AST nodes that are
    172 /// actually required will be de-serialized.
    173 class ASTReader
    174   : public ExternalPreprocessorSource,
    175     public ExternalPreprocessingRecordSource,
    176     public ExternalHeaderFileInfoSource,
    177     public ExternalSemaSource,
    178     public IdentifierInfoLookup,
    179     public ExternalIdentifierLookup,
    180     public ExternalSLocEntrySource
    181 {
    182 public:
    183   enum ASTReadResult { Success, Failure, IgnorePCH };
    184   /// \brief Types of AST files.
    185   enum ASTFileType {
    186     Module,   ///< File is a module proper.
    187     PCH,      ///< File is a PCH file treated as such.
    188     Preamble, ///< File is a PCH file treated as the preamble.
    189     MainFile  ///< File is a PCH file treated as the actual main file.
    190   };
    191   friend class PCHValidator;
    192   friend class ASTDeclReader;
    193   friend class ASTStmtReader;
    194   friend class ASTIdentifierIterator;
    195   friend class ASTIdentifierLookupTrait;
    196   friend class TypeLocReader;
    197   friend class ASTWriter;
    198   friend class ASTUnit; // ASTUnit needs to remap source locations.
    199 private:
    200   /// \brief The receiver of some callbacks invoked by ASTReader.
    201   llvm::OwningPtr<ASTReaderListener> Listener;
    202 
    203   /// \brief The receiver of deserialization events.
    204   ASTDeserializationListener *DeserializationListener;
    205 
    206   SourceManager &SourceMgr;
    207   FileManager &FileMgr;
    208   Diagnostic &Diags;
    209 
    210   /// \brief The semantic analysis object that will be processing the
    211   /// AST files and the translation unit that uses it.
    212   Sema *SemaObj;
    213 
    214   /// \brief The preprocessor that will be loading the source file.
    215   Preprocessor *PP;
    216 
    217   /// \brief The AST context into which we'll read the AST files.
    218   ASTContext *Context;
    219 
    220   /// \brief The AST consumer.
    221   ASTConsumer *Consumer;
    222 
    223   /// \brief AST buffers for chained PCHs created and stored in memory.
    224   /// First (not depending on another) PCH in chain is in front.
    225   std::vector<llvm::MemoryBuffer *> ASTBuffers;
    226 
    227   /// \brief Information that is needed for every module.
    228   struct PerFileData {
    229     PerFileData(ASTFileType Ty);
    230     ~PerFileData();
    231 
    232     // === General information ===
    233 
    234     /// \brief The type of this AST file.
    235     ASTFileType Type;
    236 
    237     /// \brief The file name of the AST file.
    238     std::string FileName;
    239 
    240     /// \brief The memory buffer that stores the data associated with
    241     /// this AST file.
    242     llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
    243 
    244     /// \brief The size of this file, in bits.
    245     uint64_t SizeInBits;
    246 
    247     /// \brief The bitstream reader from which we'll read the AST file.
    248     llvm::BitstreamReader StreamFile;
    249 
    250     /// \brief The main bitstream cursor for the main block.
    251     llvm::BitstreamCursor Stream;
    252 
    253     /// \brief The source location where this module was first imported.
    254     SourceLocation ImportLoc;
    255 
    256     /// \brief The first source location in this module.
    257     SourceLocation FirstLoc;
    258 
    259     // === Source Locations ===
    260 
    261     /// \brief Cursor used to read source location entries.
    262     llvm::BitstreamCursor SLocEntryCursor;
    263 
    264     /// \brief The number of source location entries in this AST file.
    265     unsigned LocalNumSLocEntries;
    266 
    267     /// \brief The base ID in the source manager's view of this module.
    268     int SLocEntryBaseID;
    269 
    270     /// \brief The base offset in the source manager's view of this module.
    271     unsigned SLocEntryBaseOffset;
    272 
    273     /// \brief Offsets for all of the source location entries in the
    274     /// AST file.
    275     const uint32_t *SLocEntryOffsets;
    276 
    277     /// \brief The number of source location file entries in this AST file.
    278     unsigned LocalNumSLocFileEntries;
    279 
    280     /// \brief Offsets for all of the source location file entries in the
    281     /// AST file.
    282     const uint32_t *SLocFileOffsets;
    283 
    284     /// \brief Remapping table for source locations in this module.
    285     ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
    286 
    287     // === Identifiers ===
    288 
    289     /// \brief The number of identifiers in this AST file.
    290     unsigned LocalNumIdentifiers;
    291 
    292     /// \brief Offsets into the identifier table data.
    293     ///
    294     /// This array is indexed by the identifier ID (-1), and provides
    295     /// the offset into IdentifierTableData where the string data is
    296     /// stored.
    297     const uint32_t *IdentifierOffsets;
    298 
    299     /// \brief Actual data for the on-disk hash table of identifiers.
    300     ///
    301     /// This pointer points into a memory buffer, where the on-disk hash
    302     /// table for identifiers actually lives.
    303     const char *IdentifierTableData;
    304 
    305     /// \brief A pointer to an on-disk hash table of opaque type
    306     /// IdentifierHashTable.
    307     void *IdentifierLookupTable;
    308 
    309     // === Macros ===
    310 
    311     /// \brief The cursor to the start of the preprocessor block, which stores
    312     /// all of the macro definitions.
    313     llvm::BitstreamCursor MacroCursor;
    314 
    315     /// \brief The offset of the start of the set of defined macros.
    316     uint64_t MacroStartOffset;
    317 
    318     // === Detailed PreprocessingRecord ===
    319 
    320     /// \brief The cursor to the start of the (optional) detailed preprocessing
    321     /// record block.
    322     llvm::BitstreamCursor PreprocessorDetailCursor;
    323 
    324     /// \brief The offset of the start of the preprocessor detail cursor.
    325     uint64_t PreprocessorDetailStartOffset;
    326 
    327     /// \brief The number of macro definitions in this file.
    328     unsigned LocalNumMacroDefinitions;
    329 
    330     /// \brief Offsets of all of the macro definitions in the preprocessing
    331     /// record in the AST file.
    332     const uint32_t *MacroDefinitionOffsets;
    333 
    334     // === Header search information ===
    335 
    336     /// \brief The number of local HeaderFileInfo structures.
    337     unsigned LocalNumHeaderFileInfos;
    338 
    339     /// \brief Actual data for the on-disk hash table of header file
    340     /// information.
    341     ///
    342     /// This pointer points into a memory buffer, where the on-disk hash
    343     /// table for header file information actually lives.
    344     const char *HeaderFileInfoTableData;
    345 
    346     /// \brief The on-disk hash table that contains information about each of
    347     /// the header files.
    348     void *HeaderFileInfoTable;
    349 
    350     // === Selectors ===
    351 
    352     /// \brief The number of selectors new to this file.
    353     ///
    354     /// This is the number of entries in SelectorOffsets.
    355     unsigned LocalNumSelectors;
    356 
    357     /// \brief Offsets into the selector lookup table's data array
    358     /// where each selector resides.
    359     const uint32_t *SelectorOffsets;
    360 
    361     /// \brief A pointer to the character data that comprises the selector table
    362     ///
    363     /// The SelectorOffsets table refers into this memory.
    364     const unsigned char *SelectorLookupTableData;
    365 
    366     /// \brief A pointer to an on-disk hash table of opaque type
    367     /// ASTSelectorLookupTable.
    368     ///
    369     /// This hash table provides the IDs of all selectors, and the associated
    370     /// instance and factory methods.
    371     void *SelectorLookupTable;
    372 
    373     /// \brief Method selectors used in a @selector expression. Used for
    374     /// implementation of -Wselector.
    375     llvm::SmallVector<uint64_t, 64> ReferencedSelectorsData;
    376 
    377     // === Declarations ===
    378 
    379     /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
    380     /// has read all the abbreviations at the start of the block and is ready to
    381     /// jump around with these in context.
    382     llvm::BitstreamCursor DeclsCursor;
    383 
    384     /// \brief The number of declarations in this AST file.
    385     unsigned LocalNumDecls;
    386 
    387     /// \brief Offset of each declaration within the bitstream, indexed
    388     /// by the declaration ID (-1).
    389     const uint32_t *DeclOffsets;
    390 
    391     /// \brief A snapshot of the pending instantiations in the chain.
    392     ///
    393     /// This record tracks the instantiations that Sema has to perform at the
    394     /// end of the TU. It consists of a pair of values for every pending
    395     /// instantiation where the first value is the ID of the decl and the second
    396     /// is the instantiation location.
    397     llvm::SmallVector<uint64_t, 64> PendingInstantiations;
    398 
    399     /// \brief The number of C++ base specifier sets in this AST file.
    400     unsigned LocalNumCXXBaseSpecifiers;
    401 
    402     /// \brief Offset of each C++ base specifier set within the bitstream,
    403     /// indexed by the C++ base specifier set ID (-1).
    404     const uint32_t *CXXBaseSpecifiersOffsets;
    405 
    406     // === Types ===
    407 
    408     /// \brief The number of types in this AST file.
    409     unsigned LocalNumTypes;
    410 
    411     /// \brief Offset of each type within the bitstream, indexed by the
    412     /// type ID, or the representation of a Type*.
    413     const uint32_t *TypeOffsets;
    414 
    415     // === Miscellaneous ===
    416 
    417     /// \brief Diagnostic IDs and their mappings that the user changed.
    418     llvm::SmallVector<uint64_t, 8> PragmaDiagMappings;
    419 
    420     /// \brief The AST stat cache installed for this file, if any.
    421     ///
    422     /// The dynamic type of this stat cache is always ASTStatCache
    423     void *StatCache;
    424 
    425     /// \brief The number of preallocated preprocessing entities in the
    426     /// preprocessing record.
    427     unsigned NumPreallocatedPreprocessingEntities;
    428 
    429     /// \brief The next module in source order.
    430     PerFileData *NextInSource;
    431 
    432     /// \brief All the modules that loaded this one. Can contain NULL for
    433     /// directly loaded modules.
    434     llvm::SmallVector<PerFileData *, 1> Loaders;
    435   };
    436 
    437   /// \brief All loaded modules, indexed by name.
    438   llvm::StringMap<PerFileData*> Modules;
    439 
    440   /// \brief The first module in source order.
    441   PerFileData *FirstInSource;
    442 
    443   /// \brief The chain of AST files. The first entry is the one named by the
    444   /// user, the last one is the one that doesn't depend on anything further.
    445   /// That is, the entry I was created with -include-pch I+1.
    446   llvm::SmallVector<PerFileData*, 2> Chain;
    447 
    448   /// \brief SLocEntries that we're going to preload.
    449   llvm::SmallVector<int, 64> PreloadSLocEntries;
    450 
    451   /// \brief A map of negated SLocEntryIDs to the modules containing them.
    452   ContinuousRangeMap<unsigned, PerFileData*, 64> GlobalSLocEntryMap;
    453 
    454   /// \brief Types that have already been loaded from the chain.
    455   ///
    456   /// When the pointer at index I is non-NULL, the type with
    457   /// ID = (I + 1) << FastQual::Width has already been loaded
    458   std::vector<QualType> TypesLoaded;
    459 
    460   /// \brief Map that provides the ID numbers of each type within the
    461   /// output stream, plus those deserialized from a chained PCH.
    462   ///
    463   /// The ID numbers of types are consecutive (in order of discovery)
    464   /// and start at 1. 0 is reserved for NULL. When types are actually
    465   /// stored in the stream, the ID number is shifted by 2 bits to
    466   /// allow for the const/volatile qualifiers.
    467   ///
    468   /// Keys in the map never have const/volatile qualifiers.
    469   serialization::TypeIdxMap TypeIdxs;
    470 
    471   /// \brief Declarations that have already been loaded from the chain.
    472   ///
    473   /// When the pointer at index I is non-NULL, the declaration with ID
    474   /// = I + 1 has already been loaded.
    475   std::vector<Decl *> DeclsLoaded;
    476 
    477   typedef ContinuousRangeMap<serialization::DeclID,
    478                              std::pair<PerFileData *, int32_t>, 4>
    479     GlobalDeclMapType;
    480 
    481   /// \brief Mapping from global declaration IDs to the module in which the
    482   /// declaration resides along with the offset that should be added to the
    483   /// global declaration ID to produce a local ID.
    484   GlobalDeclMapType GlobalDeclMap;
    485 
    486   typedef std::pair<PerFileData *, uint64_t> FileOffset;
    487   typedef llvm::SmallVector<FileOffset, 2> FileOffsetsTy;
    488   typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
    489       DeclUpdateOffsetsMap;
    490 
    491   /// \brief Declarations that have modifications residing in a later file
    492   /// in the chain.
    493   DeclUpdateOffsetsMap DeclUpdateOffsets;
    494 
    495   typedef llvm::DenseMap<serialization::DeclID,
    496                          std::pair<PerFileData *, uint64_t> >
    497       DeclReplacementMap;
    498   /// \brief Declarations that have been replaced in a later file in the chain.
    499   DeclReplacementMap ReplacedDecls;
    500 
    501   /// \brief Information about the contents of a DeclContext.
    502   struct DeclContextInfo {
    503     void *NameLookupTableData; // a ASTDeclContextNameLookupTable.
    504     const serialization::KindDeclIDPair *LexicalDecls;
    505     unsigned NumLexicalDecls;
    506   };
    507   // In a full chain, there could be multiple updates to every decl context,
    508   // so this is a vector. However, typically a chain is only two elements long,
    509   // with only one file containing updates, so there will be only one update
    510   // per decl context.
    511   typedef llvm::SmallVector<DeclContextInfo, 1> DeclContextInfos;
    512   typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
    513       DeclContextOffsetsMap;
    514   // Updates for visible decls can occur for other contexts than just the
    515   // TU, and when we read those update records, the actual context will not
    516   // be available yet (unless it's the TU), so have this pending map using the
    517   // ID as a key. It will be realized when the context is actually loaded.
    518   typedef llvm::SmallVector<void *, 1> DeclContextVisibleUpdates;
    519   typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
    520       DeclContextVisibleUpdatesPending;
    521 
    522   /// \brief Offsets of the lexical and visible declarations for each
    523   /// DeclContext.
    524   DeclContextOffsetsMap DeclContextOffsets;
    525 
    526   /// \brief Updates to the visible declarations of declaration contexts that
    527   /// haven't been loaded yet.
    528   DeclContextVisibleUpdatesPending PendingVisibleUpdates;
    529 
    530   typedef llvm::SmallVector<CXXRecordDecl *, 4> ForwardRefs;
    531   typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs>
    532       PendingForwardRefsMap;
    533   /// \brief Forward references that have a definition but the definition decl
    534   /// is still initializing. When the definition gets read it will update
    535   /// the DefinitionData pointer of all pending references.
    536   PendingForwardRefsMap PendingForwardRefs;
    537 
    538   typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID>
    539       FirstLatestDeclIDMap;
    540   /// \brief Map of first declarations from a chained PCH that point to the
    541   /// most recent declarations in another AST file.
    542   FirstLatestDeclIDMap FirstLatestDeclIDs;
    543 
    544   /// \brief Read the records that describe the contents of declcontexts.
    545   bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
    546                               const std::pair<uint64_t, uint64_t> &Offsets,
    547                               DeclContextInfo &Info);
    548 
    549   /// \brief A vector containing identifiers that have already been
    550   /// loaded.
    551   ///
    552   /// If the pointer at index I is non-NULL, then it refers to the
    553   /// IdentifierInfo for the identifier with ID=I+1 that has already
    554   /// been loaded.
    555   std::vector<IdentifierInfo *> IdentifiersLoaded;
    556 
    557   typedef ContinuousRangeMap<serialization::IdentID,
    558                              std::pair<PerFileData *, int32_t>, 4>
    559     GlobalIdentifierMapType;
    560 
    561   /// \brief Mapping from global identifer IDs to the module in which the
    562   /// identifier resides along with the offset that should be added to the
    563   /// global identifier ID to produce a local ID.
    564   GlobalIdentifierMapType GlobalIdentifierMap;
    565 
    566   /// \brief A vector containing selectors that have already been loaded.
    567   ///
    568   /// This vector is indexed by the Selector ID (-1). NULL selector
    569   /// entries indicate that the particular selector ID has not yet
    570   /// been loaded.
    571   llvm::SmallVector<Selector, 16> SelectorsLoaded;
    572 
    573   typedef ContinuousRangeMap<serialization::SelectorID,
    574                              std::pair<PerFileData *, int32_t>, 4>
    575     GlobalSelectorMapType;
    576 
    577   /// \brief Mapping from global selector IDs to the module in which the
    578   /// selector resides along with the offset that should be added to the
    579   /// global selector ID to produce a local ID.
    580   GlobalSelectorMapType GlobalSelectorMap;
    581 
    582   /// \brief The macro definitions we have already loaded.
    583   llvm::SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded;
    584 
    585   typedef ContinuousRangeMap<serialization::MacroID,
    586                              std::pair<PerFileData *, int32_t>, 4>
    587     GlobalMacroDefinitionMapType;
    588 
    589   /// \brief Mapping from global macro definition IDs to the module in which the
    590   /// selector resides along with the offset that should be added to the
    591   /// global selector ID to produce a local ID.
    592   GlobalMacroDefinitionMapType GlobalMacroDefinitionMap;
    593 
    594   /// \brief Mapping from identifiers that represent macros whose definitions
    595   /// have not yet been deserialized to the global offset where the macro
    596   /// record resides.
    597   llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets;
    598 
    599   /// \name CodeGen-relevant special data
    600   /// \brief Fields containing data that is relevant to CodeGen.
    601   //@{
    602 
    603   /// \brief The IDs of all declarations that fulfill the criteria of
    604   /// "interesting" decls.
    605   ///
    606   /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the
    607   /// chain. The referenced declarations are deserialized and passed to the
    608   /// consumer eagerly.
    609   llvm::SmallVector<uint64_t, 16> ExternalDefinitions;
    610 
    611   /// \brief The IDs of all tentative definitions stored in the the chain.
    612   ///
    613   /// Sema keeps track of all tentative definitions in a TU because it has to
    614   /// complete them and pass them on to CodeGen. Thus, tentative definitions in
    615   /// the PCH chain must be eagerly deserialized.
    616   llvm::SmallVector<uint64_t, 16> TentativeDefinitions;
    617 
    618   /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
    619   /// used.
    620   ///
    621   /// CodeGen has to emit VTables for these records, so they have to be eagerly
    622   /// deserialized.
    623   llvm::SmallVector<uint64_t, 64> VTableUses;
    624 
    625   //@}
    626 
    627   /// \name Diagnostic-relevant special data
    628   /// \brief Fields containing data that is used for generating diagnostics
    629   //@{
    630 
    631   /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
    632   /// generating warnings.
    633   llvm::SmallVector<uint64_t, 16> UnusedFileScopedDecls;
    634 
    635   /// \brief A list of all the delegating constructors we've seen, to diagnose
    636   /// cycles.
    637   llvm::SmallVector<uint64_t, 4> DelegatingCtorDecls;
    638 
    639   /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
    640   /// generating warnings.
    641   llvm::SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
    642 
    643   /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
    644   ///
    645   /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
    646   llvm::SmallVector<uint64_t, 4> ExtVectorDecls;
    647 
    648   //@}
    649 
    650   /// \name Sema-relevant special data
    651   /// \brief Fields containing data that is used for semantic analysis
    652   //@{
    653 
    654   /// \brief The IDs of all locally scoped external decls in the chain.
    655   ///
    656   /// Sema tracks these to validate that the types are consistent across all
    657   /// local external declarations.
    658   llvm::SmallVector<uint64_t, 16> LocallyScopedExternalDecls;
    659 
    660   /// \brief The IDs of all dynamic class declarations in the chain.
    661   ///
    662   /// Sema tracks these because it checks for the key functions being defined
    663   /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
    664   llvm::SmallVector<uint64_t, 16> DynamicClasses;
    665 
    666   /// \brief The IDs of the declarations Sema stores directly.
    667   ///
    668   /// Sema tracks a few important decls, such as namespace std, directly.
    669   llvm::SmallVector<uint64_t, 4> SemaDeclRefs;
    670 
    671   /// \brief The IDs of the types ASTContext stores directly.
    672   ///
    673   /// The AST context tracks a few important types, such as va_list, directly.
    674   llvm::SmallVector<uint64_t, 16> SpecialTypes;
    675 
    676   /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
    677   ///
    678   /// The AST context tracks a few important decls, currently cudaConfigureCall,
    679   /// directly.
    680   llvm::SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
    681 
    682   /// \brief The floating point pragma option settings.
    683   llvm::SmallVector<uint64_t, 1> FPPragmaOptions;
    684 
    685   /// \brief The OpenCL extension settings.
    686   llvm::SmallVector<uint64_t, 1> OpenCLExtensions;
    687 
    688   /// \brief A list of the namespaces we've seen.
    689   llvm::SmallVector<uint64_t, 4> KnownNamespaces;
    690 
    691   //@}
    692 
    693   /// \brief The original file name that was used to build the primary AST file,
    694   /// which may have been modified for relocatable-pch support.
    695   std::string OriginalFileName;
    696 
    697   /// \brief The actual original file name that was used to build the primary
    698   /// AST file.
    699   std::string ActualOriginalFileName;
    700 
    701   /// \brief The file ID for the original file that was used to build the
    702   /// primary AST file.
    703   FileID OriginalFileID;
    704 
    705   /// \brief The directory that the PCH was originally created in. Used to
    706   /// allow resolving headers even after headers+PCH was moved to a new path.
    707   std::string OriginalDir;
    708 
    709   /// \brief The directory that the PCH we are reading is stored in.
    710   std::string CurrentDir;
    711 
    712   /// \brief Whether this precompiled header is a relocatable PCH file.
    713   bool RelocatablePCH;
    714 
    715   /// \brief The system include root to be used when loading the
    716   /// precompiled header.
    717   const char *isysroot;
    718 
    719   /// \brief Whether to disable the normal validation performed on precompiled
    720   /// headers when they are loaded.
    721   bool DisableValidation;
    722 
    723   /// \brief Whether to disable the use of stat caches in AST files.
    724   bool DisableStatCache;
    725 
    726   /// \brief Mapping from switch-case IDs in the chain to switch-case statements
    727   ///
    728   /// Statements usually don't have IDs, but switch cases need them, so that the
    729   /// switch statement can refer to them.
    730   std::map<unsigned, SwitchCase *> SwitchCaseStmts;
    731 
    732   /// \brief Mapping from opaque value IDs to OpaqueValueExprs.
    733   std::map<unsigned, OpaqueValueExpr*> OpaqueValueExprs;
    734 
    735   /// \brief The number of stat() calls that hit/missed the stat
    736   /// cache.
    737   unsigned NumStatHits, NumStatMisses;
    738 
    739   /// \brief The number of source location entries de-serialized from
    740   /// the PCH file.
    741   unsigned NumSLocEntriesRead;
    742 
    743   /// \brief The number of source location entries in the chain.
    744   unsigned TotalNumSLocEntries;
    745 
    746   /// \brief The number of statements (and expressions) de-serialized
    747   /// from the chain.
    748   unsigned NumStatementsRead;
    749 
    750   /// \brief The total number of statements (and expressions) stored
    751   /// in the chain.
    752   unsigned TotalNumStatements;
    753 
    754   /// \brief The number of macros de-serialized from the chain.
    755   unsigned NumMacrosRead;
    756 
    757   /// \brief The total number of macros stored in the chain.
    758   unsigned TotalNumMacros;
    759 
    760   /// \brief The number of selectors that have been read.
    761   unsigned NumSelectorsRead;
    762 
    763   /// \brief The number of method pool entries that have been read.
    764   unsigned NumMethodPoolEntriesRead;
    765 
    766   /// \brief The number of times we have looked up a selector in the method
    767   /// pool and not found anything interesting.
    768   unsigned NumMethodPoolMisses;
    769 
    770   /// \brief The total number of method pool entries in the selector table.
    771   unsigned TotalNumMethodPoolEntries;
    772 
    773   /// Number of lexical decl contexts read/total.
    774   unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
    775 
    776   /// Number of visible decl contexts read/total.
    777   unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
    778 
    779   /// \brief Number of Decl/types that are currently deserializing.
    780   unsigned NumCurrentElementsDeserializing;
    781 
    782   /// \brief An IdentifierInfo that has been loaded but whose top-level
    783   /// declarations of the same name have not (yet) been loaded.
    784   struct PendingIdentifierInfo {
    785     IdentifierInfo *II;
    786     llvm::SmallVector<uint32_t, 4> DeclIDs;
    787   };
    788 
    789   /// \brief The set of identifiers that were read while the AST reader was
    790   /// (recursively) loading declarations.
    791   ///
    792   /// The declarations on the identifier chain for these identifiers will be
    793   /// loaded once the recursive loading has completed.
    794   std::deque<PendingIdentifierInfo> PendingIdentifierInfos;
    795 
    796   /// \brief Contains declarations and definitions that will be
    797   /// "interesting" to the ASTConsumer, when we get that AST consumer.
    798   ///
    799   /// "Interesting" declarations are those that have data that may
    800   /// need to be emitted, such as inline function definitions or
    801   /// Objective-C protocols.
    802   std::deque<Decl *> InterestingDecls;
    803 
    804   /// \brief We delay loading of the previous declaration chain to avoid
    805   /// deeply nested calls when there are many redeclarations.
    806   std::deque<std::pair<Decl *, serialization::DeclID> > PendingPreviousDecls;
    807 
    808   /// \brief Ready to load the previous declaration of the given Decl.
    809   void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID);
    810 
    811   /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
    812   llvm::SmallVector<Stmt *, 16> StmtStack;
    813 
    814   /// \brief What kind of records we are reading.
    815   enum ReadingKind {
    816     Read_Decl, Read_Type, Read_Stmt
    817   };
    818 
    819   /// \brief What kind of records we are reading.
    820   ReadingKind ReadingKind;
    821 
    822   /// \brief RAII object to change the reading kind.
    823   class ReadingKindTracker {
    824     ASTReader &Reader;
    825     enum ReadingKind PrevKind;
    826 
    827     ReadingKindTracker(const ReadingKindTracker&); // do not implement
    828     ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement
    829 
    830   public:
    831     ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
    832       : Reader(reader), PrevKind(Reader.ReadingKind) {
    833       Reader.ReadingKind = newKind;
    834     }
    835 
    836     ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
    837   };
    838 
    839   /// \brief All predefines buffers in the chain, to be treated as if
    840   /// concatenated.
    841   PCHPredefinesBlocks PCHPredefinesBuffers;
    842 
    843   /// \brief Suggested contents of the predefines buffer, after this
    844   /// PCH file has been processed.
    845   ///
    846   /// In most cases, this string will be empty, because the predefines
    847   /// buffer computed to build the PCH file will be identical to the
    848   /// predefines buffer computed from the command line. However, when
    849   /// there are differences that the PCH reader can work around, this
    850   /// predefines buffer may contain additional definitions.
    851   std::string SuggestedPredefines;
    852 
    853   /// \brief Reads a statement from the specified cursor.
    854   Stmt *ReadStmtFromStream(PerFileData &F);
    855 
    856   /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take
    857   /// into account all the necessary relocations.
    858   const FileEntry *getFileEntry(llvm::StringRef filename);
    859 
    860   void MaybeAddSystemRootToFilename(std::string &Filename);
    861 
    862   ASTReadResult ReadASTCore(llvm::StringRef FileName, ASTFileType Type);
    863   ASTReadResult ReadASTBlock(PerFileData &F);
    864   bool CheckPredefinesBuffers();
    865   bool ParseLineTable(PerFileData &F, llvm::SmallVectorImpl<uint64_t> &Record);
    866   ASTReadResult ReadSourceManagerBlock(PerFileData &F);
    867   ASTReadResult ReadSLocEntryRecord(int ID);
    868   llvm::BitstreamCursor &SLocCursorForID(int ID);
    869   SourceLocation getImportLocation(PerFileData *F);
    870   bool ParseLanguageOptions(const llvm::SmallVectorImpl<uint64_t> &Record);
    871 
    872   struct RecordLocation {
    873     RecordLocation(PerFileData *M, uint64_t O)
    874       : F(M), Offset(O) {}
    875     PerFileData *F;
    876     uint64_t Offset;
    877   };
    878 
    879   QualType ReadTypeRecord(unsigned Index);
    880   RecordLocation TypeCursorForIndex(unsigned Index);
    881   void LoadedDecl(unsigned Index, Decl *D);
    882   Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID);
    883   RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID);
    884 
    885   void PassInterestingDeclsToConsumer();
    886 
    887   /// \brief Produce an error diagnostic and return true.
    888   ///
    889   /// This routine should only be used for fatal errors that have to
    890   /// do with non-routine failures (e.g., corrupted AST file).
    891   void Error(llvm::StringRef Msg);
    892   void Error(unsigned DiagID, llvm::StringRef Arg1 = llvm::StringRef(),
    893              llvm::StringRef Arg2 = llvm::StringRef());
    894 
    895   ASTReader(const ASTReader&); // do not implement
    896   ASTReader &operator=(const ASTReader &); // do not implement
    897 public:
    898   typedef llvm::SmallVector<uint64_t, 64> RecordData;
    899 
    900   /// \brief Load the AST file and validate its contents against the given
    901   /// Preprocessor.
    902   ///
    903   /// \param PP the preprocessor associated with the context in which this
    904   /// precompiled header will be loaded.
    905   ///
    906   /// \param Context the AST context that this precompiled header will be
    907   /// loaded into.
    908   ///
    909   /// \param isysroot If non-NULL, the system include path specified by the
    910   /// user. This is only used with relocatable PCH files. If non-NULL,
    911   /// a relocatable PCH file will use the default path "/".
    912   ///
    913   /// \param DisableValidation If true, the AST reader will suppress most
    914   /// of its regular consistency checking, allowing the use of precompiled
    915   /// headers that cannot be determined to be compatible.
    916   ///
    917   /// \param DisableStatCache If true, the AST reader will ignore the
    918   /// stat cache in the AST files. This performance pessimization can
    919   /// help when an AST file is being used in cases where the
    920   /// underlying files in the file system may have changed, but
    921   /// parsing should still continue.
    922   ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
    923             bool DisableValidation = false, bool DisableStatCache = false);
    924 
    925   /// \brief Load the AST file without using any pre-initialized Preprocessor.
    926   ///
    927   /// The necessary information to initialize a Preprocessor later can be
    928   /// obtained by setting a ASTReaderListener.
    929   ///
    930   /// \param SourceMgr the source manager into which the AST file will be loaded
    931   ///
    932   /// \param FileMgr the file manager into which the AST file will be loaded.
    933   ///
    934   /// \param Diags the diagnostics system to use for reporting errors and
    935   /// warnings relevant to loading the AST file.
    936   ///
    937   /// \param isysroot If non-NULL, the system include path specified by the
    938   /// user. This is only used with relocatable PCH files. If non-NULL,
    939   /// a relocatable PCH file will use the default path "/".
    940   ///
    941   /// \param DisableValidation If true, the AST reader will suppress most
    942   /// of its regular consistency checking, allowing the use of precompiled
    943   /// headers that cannot be determined to be compatible.
    944   ///
    945   /// \param DisableStatCache If true, the AST reader will ignore the
    946   /// stat cache in the AST files. This performance pessimization can
    947   /// help when an AST file is being used in cases where the
    948   /// underlying files in the file system may have changed, but
    949   /// parsing should still continue.
    950   ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
    951             Diagnostic &Diags, const char *isysroot = 0,
    952             bool DisableValidation = false, bool DisableStatCache = false);
    953   ~ASTReader();
    954 
    955   /// \brief Load the precompiled header designated by the given file
    956   /// name.
    957   ASTReadResult ReadAST(const std::string &FileName, ASTFileType Type);
    958 
    959   /// \brief Checks that no file that is stored in PCH is out-of-sync with
    960   /// the actual file in the file system.
    961   ASTReadResult validateFileEntries();
    962 
    963   /// \brief Set the AST callbacks listener.
    964   void setListener(ASTReaderListener *listener) {
    965     Listener.reset(listener);
    966   }
    967 
    968   /// \brief Set the AST deserialization listener.
    969   void setDeserializationListener(ASTDeserializationListener *Listener);
    970 
    971   /// \brief Set the Preprocessor to use.
    972   void setPreprocessor(Preprocessor &pp);
    973 
    974   /// \brief Sets and initializes the given Context.
    975   void InitializeContext(ASTContext &Context);
    976 
    977   /// \brief Set AST buffers for chained PCHs created and stored in memory.
    978   /// First (not depending on another) PCH in chain is first in array.
    979   void setASTMemoryBuffers(llvm::MemoryBuffer **bufs, unsigned numBufs) {
    980     ASTBuffers.clear();
    981     ASTBuffers.insert(ASTBuffers.begin(), bufs, bufs + numBufs);
    982   }
    983 
    984   /// \brief Retrieve the name of the named (primary) AST file
    985   const std::string &getFileName() const { return Chain[0]->FileName; }
    986 
    987   /// \brief Retrieve the name of the original source file name
    988   const std::string &getOriginalSourceFile() { return OriginalFileName; }
    989 
    990   /// \brief Retrieve the name of the original source file name directly from
    991   /// the AST file, without actually loading the AST file.
    992   static std::string getOriginalSourceFile(const std::string &ASTFileName,
    993                                            FileManager &FileMgr,
    994                                            Diagnostic &Diags);
    995 
    996   /// \brief Returns the suggested contents of the predefines buffer,
    997   /// which contains a (typically-empty) subset of the predefines
    998   /// build prior to including the precompiled header.
    999   const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
   1000 
   1001   /// \brief Read preprocessed entities into the preprocessing record.
   1002   virtual void ReadPreprocessedEntities();
   1003 
   1004   /// \brief Read the preprocessed entity at the given offset.
   1005   virtual PreprocessedEntity *ReadPreprocessedEntityAtOffset(uint64_t Offset);
   1006 
   1007   /// \brief Read the header file information for the given file entry.
   1008   virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE);
   1009 
   1010   void ReadPragmaDiagnosticMappings(Diagnostic &Diag);
   1011 
   1012   /// \brief Returns the number of source locations found in the chain.
   1013   unsigned getTotalNumSLocs() const {
   1014     return TotalNumSLocEntries;
   1015   }
   1016 
   1017   /// \brief Returns the number of identifiers found in the chain.
   1018   unsigned getTotalNumIdentifiers() const {
   1019     return static_cast<unsigned>(IdentifiersLoaded.size());
   1020   }
   1021 
   1022   /// \brief Returns the number of types found in the chain.
   1023   unsigned getTotalNumTypes() const {
   1024     return static_cast<unsigned>(TypesLoaded.size());
   1025   }
   1026 
   1027   /// \brief Returns the number of declarations found in the chain.
   1028   unsigned getTotalNumDecls() const {
   1029     return static_cast<unsigned>(DeclsLoaded.size());
   1030   }
   1031 
   1032   /// \brief Returns the number of selectors found in the chain.
   1033   unsigned getTotalNumSelectors() const {
   1034     return static_cast<unsigned>(SelectorsLoaded.size());
   1035   }
   1036 
   1037   /// \brief Returns the number of macro definitions found in the chain.
   1038   unsigned getTotalNumMacroDefinitions() const {
   1039     return static_cast<unsigned>(MacroDefinitionsLoaded.size());
   1040   }
   1041 
   1042   /// \brief Returns the number of C++ base specifiers found in the chain.
   1043   unsigned getTotalNumCXXBaseSpecifiers() const;
   1044 
   1045   /// \brief Reads a TemplateArgumentLocInfo appropriate for the
   1046   /// given TemplateArgument kind.
   1047   TemplateArgumentLocInfo
   1048   GetTemplateArgumentLocInfo(PerFileData &F, TemplateArgument::ArgKind Kind,
   1049                              const RecordData &Record, unsigned &Idx);
   1050 
   1051   /// \brief Reads a TemplateArgumentLoc.
   1052   TemplateArgumentLoc
   1053   ReadTemplateArgumentLoc(PerFileData &F,
   1054                           const RecordData &Record, unsigned &Idx);
   1055 
   1056   /// \brief Reads a declarator info from the given record.
   1057   TypeSourceInfo *GetTypeSourceInfo(PerFileData &F,
   1058                                     const RecordData &Record, unsigned &Idx);
   1059 
   1060   /// \brief Resolve and return the translation unit declaration.
   1061   TranslationUnitDecl *GetTranslationUnitDecl();
   1062 
   1063   /// \brief Resolve a type ID into a type, potentially building a new
   1064   /// type.
   1065   QualType GetType(serialization::TypeID ID);
   1066 
   1067   /// \brief Returns the type ID associated with the given type.
   1068   /// If the type didn't come from the AST file the ID that is returned is
   1069   /// marked as "doesn't exist in AST".
   1070   serialization::TypeID GetTypeID(QualType T) const;
   1071 
   1072   /// \brief Returns the type index associated with the given type.
   1073   /// If the type didn't come from the AST file the index that is returned is
   1074   /// marked as "doesn't exist in AST".
   1075   serialization::TypeIdx GetTypeIdx(QualType T) const;
   1076 
   1077   /// \brief Resolve a declaration ID into a declaration, potentially
   1078   /// building a new declaration.
   1079   Decl *GetDecl(serialization::DeclID ID);
   1080   virtual Decl *GetExternalDecl(uint32_t ID);
   1081 
   1082   /// \brief Resolve a CXXBaseSpecifiers ID into an offset into the chain
   1083   /// of loaded AST files.
   1084   uint64_t GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID);
   1085 
   1086   virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
   1087 
   1088   /// \brief Resolve the offset of a statement into a statement.
   1089   ///
   1090   /// This operation will read a new statement from the external
   1091   /// source each time it is called, and is meant to be used via a
   1092   /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
   1093   virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
   1094 
   1095   /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
   1096   /// specified cursor.  Read the abbreviations that are at the top of the block
   1097   /// and then leave the cursor pointing into the block.
   1098   bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
   1099 
   1100   /// \brief Finds all the visible declarations with a given name.
   1101   /// The current implementation of this method just loads the entire
   1102   /// lookup table as unmaterialized references.
   1103   virtual DeclContext::lookup_result
   1104   FindExternalVisibleDeclsByName(const DeclContext *DC,
   1105                                  DeclarationName Name);
   1106 
   1107   virtual void MaterializeVisibleDecls(const DeclContext *DC);
   1108 
   1109   /// \brief Read all of the declarations lexically stored in a
   1110   /// declaration context.
   1111   ///
   1112   /// \param DC The declaration context whose declarations will be
   1113   /// read.
   1114   ///
   1115   /// \param Decls Vector that will contain the declarations loaded
   1116   /// from the external source. The caller is responsible for merging
   1117   /// these declarations with any declarations already stored in the
   1118   /// declaration context.
   1119   ///
   1120   /// \returns true if there was an error while reading the
   1121   /// declarations for this declaration context.
   1122   virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
   1123                                         bool (*isKindWeWant)(Decl::Kind),
   1124                                         llvm::SmallVectorImpl<Decl*> &Decls);
   1125 
   1126   /// \brief Notify ASTReader that we started deserialization of
   1127   /// a decl or type so until FinishedDeserializing is called there may be
   1128   /// decls that are initializing. Must be paired with FinishedDeserializing.
   1129   virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
   1130 
   1131   /// \brief Notify ASTReader that we finished the deserialization of
   1132   /// a decl or type. Must be paired with StartedDeserializing.
   1133   virtual void FinishedDeserializing();
   1134 
   1135   /// \brief Function that will be invoked when we begin parsing a new
   1136   /// translation unit involving this external AST source.
   1137   ///
   1138   /// This function will provide all of the external definitions to
   1139   /// the ASTConsumer.
   1140   virtual void StartTranslationUnit(ASTConsumer *Consumer);
   1141 
   1142   /// \brief Print some statistics about AST usage.
   1143   virtual void PrintStats();
   1144 
   1145   /// Return the amount of memory used by memory buffers, breaking down
   1146   /// by heap-backed versus mmap'ed memory.
   1147   virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
   1148 
   1149   /// \brief Initialize the semantic source with the Sema instance
   1150   /// being used to perform semantic analysis on the abstract syntax
   1151   /// tree.
   1152   virtual void InitializeSema(Sema &S);
   1153 
   1154   /// \brief Inform the semantic consumer that Sema is no longer available.
   1155   virtual void ForgetSema() { SemaObj = 0; }
   1156 
   1157   /// \brief Retrieve the IdentifierInfo for the named identifier.
   1158   ///
   1159   /// This routine builds a new IdentifierInfo for the given identifier. If any
   1160   /// declarations with this name are visible from translation unit scope, their
   1161   /// declarations will be deserialized and introduced into the declaration
   1162   /// chain of the identifier.
   1163   virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
   1164   IdentifierInfo *get(llvm::StringRef Name) {
   1165     return get(Name.begin(), Name.end());
   1166   }
   1167 
   1168   /// \brief Retrieve an iterator into the set of all identifiers
   1169   /// in all loaded AST files.
   1170   virtual IdentifierIterator *getIdentifiers() const;
   1171 
   1172   /// \brief Load the contents of the global method pool for a given
   1173   /// selector.
   1174   ///
   1175   /// \returns a pair of Objective-C methods lists containing the
   1176   /// instance and factory methods, respectively, with this selector.
   1177   virtual std::pair<ObjCMethodList, ObjCMethodList>
   1178     ReadMethodPool(Selector Sel);
   1179 
   1180   /// \brief Load the set of namespaces that are known to the external source,
   1181   /// which will be used during typo correction.
   1182   virtual void ReadKnownNamespaces(
   1183                            llvm::SmallVectorImpl<NamespaceDecl *> &Namespaces);
   1184 
   1185   /// \brief Load a selector from disk, registering its ID if it exists.
   1186   void LoadSelector(Selector Sel);
   1187 
   1188   void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
   1189   void SetGloballyVisibleDecls(IdentifierInfo *II,
   1190                                const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
   1191                                bool Nonrecursive = false);
   1192 
   1193   /// \brief Report a diagnostic.
   1194   DiagnosticBuilder Diag(unsigned DiagID);
   1195 
   1196   /// \brief Report a diagnostic.
   1197   DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
   1198 
   1199   IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
   1200 
   1201   IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
   1202     return DecodeIdentifierInfo(Record[Idx++]);
   1203   }
   1204 
   1205   virtual IdentifierInfo *GetIdentifier(unsigned ID) {
   1206     return DecodeIdentifierInfo(ID);
   1207   }
   1208 
   1209   /// \brief Read the source location entry with index ID.
   1210   virtual bool ReadSLocEntry(int ID);
   1211 
   1212   Selector DecodeSelector(unsigned Idx);
   1213 
   1214   virtual Selector GetExternalSelector(uint32_t ID);
   1215   uint32_t GetNumExternalSelectors();
   1216 
   1217   Selector GetSelector(const RecordData &Record, unsigned &Idx) {
   1218     return DecodeSelector(Record[Idx++]);
   1219   }
   1220 
   1221   /// \brief Read a declaration name.
   1222   DeclarationName ReadDeclarationName(const RecordData &Record, unsigned &Idx);
   1223   void ReadDeclarationNameLoc(PerFileData &F,
   1224                               DeclarationNameLoc &DNLoc, DeclarationName Name,
   1225                               const RecordData &Record, unsigned &Idx);
   1226   void ReadDeclarationNameInfo(PerFileData &F, DeclarationNameInfo &NameInfo,
   1227                                const RecordData &Record, unsigned &Idx);
   1228 
   1229   void ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
   1230                          const RecordData &Record, unsigned &Idx);
   1231 
   1232   NestedNameSpecifier *ReadNestedNameSpecifier(const RecordData &Record,
   1233                                                unsigned &Idx);
   1234 
   1235   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(PerFileData &F,
   1236                                                     const RecordData &Record,
   1237                                                     unsigned &Idx);
   1238 
   1239   /// \brief Read a template name.
   1240   TemplateName ReadTemplateName(PerFileData &F, const RecordData &Record,
   1241                                 unsigned &Idx);
   1242 
   1243   /// \brief Read a template argument.
   1244   TemplateArgument ReadTemplateArgument(PerFileData &F,
   1245                                         const RecordData &Record,unsigned &Idx);
   1246 
   1247   /// \brief Read a template parameter list.
   1248   TemplateParameterList *ReadTemplateParameterList(PerFileData &F,
   1249                                                    const RecordData &Record,
   1250                                                    unsigned &Idx);
   1251 
   1252   /// \brief Read a template argument array.
   1253   void
   1254   ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
   1255                            PerFileData &F, const RecordData &Record,
   1256                            unsigned &Idx);
   1257 
   1258   /// \brief Read a UnresolvedSet structure.
   1259   void ReadUnresolvedSet(UnresolvedSetImpl &Set,
   1260                          const RecordData &Record, unsigned &Idx);
   1261 
   1262   /// \brief Read a C++ base specifier.
   1263   CXXBaseSpecifier ReadCXXBaseSpecifier(PerFileData &F,
   1264                                         const RecordData &Record,unsigned &Idx);
   1265 
   1266   /// \brief Read a CXXCtorInitializer array.
   1267   std::pair<CXXCtorInitializer **, unsigned>
   1268   ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
   1269                           unsigned &Idx);
   1270 
   1271   /// \brief Read a source location from raw form.
   1272   SourceLocation ReadSourceLocation(PerFileData &Module, unsigned Raw) {
   1273     unsigned Flag = Raw & (1U << 31);
   1274     unsigned Offset = Raw & ~(1U << 31);
   1275     assert(Module.SLocRemap.find(Offset) != Module.SLocRemap.end() &&
   1276            "Cannot find offset to remap.");
   1277     int Remap = Module.SLocRemap.find(Offset)->second;
   1278     Offset += Remap;
   1279     assert((Offset & (1U << 31)) == 0 &&
   1280            "Bad offset in reading source location");
   1281     return SourceLocation::getFromRawEncoding(Offset | Flag);
   1282   }
   1283 
   1284   /// \brief Read a source location.
   1285   SourceLocation ReadSourceLocation(PerFileData &Module,
   1286                                     const RecordData &Record, unsigned& Idx) {
   1287     return ReadSourceLocation(Module, Record[Idx++]);
   1288   }
   1289 
   1290   /// \brief Read a source range.
   1291   SourceRange ReadSourceRange(PerFileData &F,
   1292                               const RecordData &Record, unsigned& Idx);
   1293 
   1294   /// \brief Read an integral value
   1295   llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
   1296 
   1297   /// \brief Read a signed integral value
   1298   llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
   1299 
   1300   /// \brief Read a floating-point value
   1301   llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx);
   1302 
   1303   // \brief Read a string
   1304   std::string ReadString(const RecordData &Record, unsigned &Idx);
   1305 
   1306   /// \brief Read a version tuple.
   1307   VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
   1308 
   1309   CXXTemporary *ReadCXXTemporary(const RecordData &Record, unsigned &Idx);
   1310 
   1311   /// \brief Reads attributes from the current stream position.
   1312   void ReadAttributes(PerFileData &F, AttrVec &Attrs,
   1313                       const RecordData &Record, unsigned &Idx);
   1314 
   1315   /// \brief Reads a statement.
   1316   Stmt *ReadStmt(PerFileData &F);
   1317 
   1318   /// \brief Reads an expression.
   1319   Expr *ReadExpr(PerFileData &F);
   1320 
   1321   /// \brief Reads a sub-statement operand during statement reading.
   1322   Stmt *ReadSubStmt() {
   1323     assert(ReadingKind == Read_Stmt &&
   1324            "Should be called only during statement reading!");
   1325     // Subexpressions are stored from last to first, so the next Stmt we need
   1326     // is at the back of the stack.
   1327     assert(!StmtStack.empty() && "Read too many sub statements!");
   1328     return StmtStack.pop_back_val();
   1329   }
   1330 
   1331   /// \brief Reads a sub-expression operand during statement reading.
   1332   Expr *ReadSubExpr();
   1333 
   1334   /// \brief Reads the macro record located at the given offset.
   1335   PreprocessedEntity *ReadMacroRecord(PerFileData &F, uint64_t Offset);
   1336 
   1337   /// \brief Reads the preprocessed entity located at the current stream
   1338   /// position.
   1339   PreprocessedEntity *LoadPreprocessedEntity(PerFileData &F);
   1340 
   1341   /// \brief Note that the identifier is a macro whose record will be loaded
   1342   /// from the given AST file at the given (file-local) offset.
   1343   void SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
   1344                             uint64_t Offset);
   1345 
   1346   /// \brief Read the set of macros defined by this external macro source.
   1347   virtual void ReadDefinedMacros();
   1348 
   1349   /// \brief Read the macro definition for this identifier.
   1350   virtual void LoadMacroDefinition(IdentifierInfo *II);
   1351 
   1352   /// \brief Read the macro definition corresponding to this iterator
   1353   /// into the unread macro record offsets table.
   1354   void LoadMacroDefinition(
   1355                      llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos);
   1356 
   1357   /// \brief Retrieve the macro definition with the given ID.
   1358   MacroDefinition *getMacroDefinition(serialization::MacroID ID);
   1359 
   1360   /// \brief Retrieve the AST context that this AST reader supplements.
   1361   ASTContext *getContext() { return Context; }
   1362 
   1363   // \brief Contains declarations that were loaded before we have
   1364   // access to a Sema object.
   1365   llvm::SmallVector<NamedDecl *, 16> PreloadedDecls;
   1366 
   1367   /// \brief Retrieve the semantic analysis object used to analyze the
   1368   /// translation unit in which the precompiled header is being
   1369   /// imported.
   1370   Sema *getSema() { return SemaObj; }
   1371 
   1372   /// \brief Retrieve the identifier table associated with the
   1373   /// preprocessor.
   1374   IdentifierTable &getIdentifierTable();
   1375 
   1376   /// \brief Record that the given ID maps to the given switch-case
   1377   /// statement.
   1378   void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
   1379 
   1380   /// \brief Retrieve the switch-case statement with the given ID.
   1381   SwitchCase *getSwitchCaseWithID(unsigned ID);
   1382 
   1383   void ClearSwitchCaseIDs();
   1384 };
   1385 
   1386 /// \brief Helper class that saves the current stream position and
   1387 /// then restores it when destroyed.
   1388 struct SavedStreamPosition {
   1389   explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
   1390   : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
   1391 
   1392   ~SavedStreamPosition() {
   1393     Cursor.JumpToBit(Offset);
   1394   }
   1395 
   1396 private:
   1397   llvm::BitstreamCursor &Cursor;
   1398   uint64_t Offset;
   1399 };
   1400 
   1401 inline void PCHValidator::Error(const char *Msg) {
   1402   Reader.Error(Msg);
   1403 }
   1404 
   1405 } // end namespace clang
   1406 
   1407 #endif
   1408