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