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