ASTReader.h revision d10a381d92a60d4f4c126c4e81045c8ad4636a0b
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 577class ReadMethodPoolVisitor; 578 579} // end namespace serialization 580 581/// \brief Reads an AST files chain containing the contents of a translation 582/// unit. 583/// 584/// The ASTReader class reads bitstreams (produced by the ASTWriter 585/// class) containing the serialized representation of a given 586/// abstract syntax tree and its supporting data structures. An 587/// instance of the ASTReader can be attached to an ASTContext object, 588/// which will provide access to the contents of the AST files. 589/// 590/// The AST reader provides lazy de-serialization of declarations, as 591/// required when traversing the AST. Only those AST nodes that are 592/// actually required will be de-serialized. 593class ASTReader 594 : public ExternalPreprocessorSource, 595 public ExternalPreprocessingRecordSource, 596 public ExternalHeaderFileInfoSource, 597 public ExternalSemaSource, 598 public IdentifierInfoLookup, 599 public ExternalIdentifierLookup, 600 public ExternalSLocEntrySource 601{ 602public: 603 enum ASTReadResult { Success, Failure, IgnorePCH }; 604 /// \brief Types of AST files. 605 friend class PCHValidator; 606 friend class ASTDeclReader; 607 friend class ASTStmtReader; 608 friend class ASTIdentifierIterator; 609 friend class ASTIdentifierLookupTrait; 610 friend class TypeLocReader; 611 friend class ASTWriter; 612 friend class ASTUnit; // ASTUnit needs to remap source locations. 613 friend class serialization::ReadMethodPoolVisitor; 614 615 typedef serialization::Module Module; 616 typedef serialization::ModuleKind ModuleKind; 617 typedef serialization::ModuleManager ModuleManager; 618 619 typedef ModuleManager::ModuleIterator ModuleIterator; 620 typedef ModuleManager::ModuleConstIterator ModuleConstIterator; 621 typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator; 622 623private: 624 /// \brief The receiver of some callbacks invoked by ASTReader. 625 llvm::OwningPtr<ASTReaderListener> Listener; 626 627 /// \brief The receiver of deserialization events. 628 ASTDeserializationListener *DeserializationListener; 629 630 SourceManager &SourceMgr; 631 FileManager &FileMgr; 632 Diagnostic &Diags; 633 634 /// \brief The semantic analysis object that will be processing the 635 /// AST files and the translation unit that uses it. 636 Sema *SemaObj; 637 638 /// \brief The preprocessor that will be loading the source file. 639 Preprocessor *PP; 640 641 /// \brief The AST context into which we'll read the AST files. 642 ASTContext *Context; 643 644 /// \brief The AST consumer. 645 ASTConsumer *Consumer; 646 647 /// \brief The module manager which manages modules and their dependencies 648 ModuleManager ModuleMgr; 649 650 /// \brief A map of global bit offsets to the module that stores entities 651 /// at those bit offsets. 652 ContinuousRangeMap<uint64_t, Module*, 4> GlobalBitOffsetsMap; 653 654 /// \brief SLocEntries that we're going to preload. 655 SmallVector<int, 64> PreloadSLocEntries; 656 657 /// \brief A map of negated SLocEntryIDs to the modules containing them. 658 ContinuousRangeMap<unsigned, Module*, 64> GlobalSLocEntryMap; 659 660 /// \brief Types that have already been loaded from the chain. 661 /// 662 /// When the pointer at index I is non-NULL, the type with 663 /// ID = (I + 1) << FastQual::Width has already been loaded 664 std::vector<QualType> TypesLoaded; 665 666 typedef ContinuousRangeMap<serialization::TypeID, Module *, 4> 667 GlobalTypeMapType; 668 669 /// \brief Mapping from global type IDs to the module in which the 670 /// type resides along with the offset that should be added to the 671 /// global type ID to produce a local ID. 672 GlobalTypeMapType GlobalTypeMap; 673 674 /// \brief Declarations that have already been loaded from the chain. 675 /// 676 /// When the pointer at index I is non-NULL, the declaration with ID 677 /// = I + 1 has already been loaded. 678 std::vector<Decl *> DeclsLoaded; 679 680 typedef ContinuousRangeMap<serialization::DeclID, Module *, 4> 681 GlobalDeclMapType; 682 683 /// \brief Mapping from global declaration IDs to the module in which the 684 /// declaration resides. 685 GlobalDeclMapType GlobalDeclMap; 686 687 typedef std::pair<Module *, uint64_t> FileOffset; 688 typedef SmallVector<FileOffset, 2> FileOffsetsTy; 689 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy> 690 DeclUpdateOffsetsMap; 691 692 /// \brief Declarations that have modifications residing in a later file 693 /// in the chain. 694 DeclUpdateOffsetsMap DeclUpdateOffsets; 695 696 typedef llvm::DenseMap<serialization::DeclID, 697 std::pair<Module *, uint64_t> > 698 DeclReplacementMap; 699 /// \brief Declarations that have been replaced in a later file in the chain. 700 DeclReplacementMap ReplacedDecls; 701 702 // Updates for visible decls can occur for other contexts than just the 703 // TU, and when we read those update records, the actual context will not 704 // be available yet (unless it's the TU), so have this pending map using the 705 // ID as a key. It will be realized when the context is actually loaded. 706 typedef SmallVector<std::pair<void *, Module*>, 1> DeclContextVisibleUpdates; 707 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates> 708 DeclContextVisibleUpdatesPending; 709 710 /// \brief Updates to the visible declarations of declaration contexts that 711 /// haven't been loaded yet. 712 DeclContextVisibleUpdatesPending PendingVisibleUpdates; 713 714 typedef SmallVector<CXXRecordDecl *, 4> ForwardRefs; 715 typedef llvm::DenseMap<const CXXRecordDecl *, ForwardRefs> 716 PendingForwardRefsMap; 717 /// \brief Forward references that have a definition but the definition decl 718 /// is still initializing. When the definition gets read it will update 719 /// the DefinitionData pointer of all pending references. 720 PendingForwardRefsMap PendingForwardRefs; 721 722 typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID> 723 FirstLatestDeclIDMap; 724 /// \brief Map of first declarations from a chained PCH that point to the 725 /// most recent declarations in another AST file. 726 FirstLatestDeclIDMap FirstLatestDeclIDs; 727 728 /// \brief Read the records that describe the contents of declcontexts. 729 bool ReadDeclContextStorage(Module &M, 730 llvm::BitstreamCursor &Cursor, 731 const std::pair<uint64_t, uint64_t> &Offsets, 732 serialization::DeclContextInfo &Info); 733 734 /// \brief A vector containing identifiers that have already been 735 /// loaded. 736 /// 737 /// If the pointer at index I is non-NULL, then it refers to the 738 /// IdentifierInfo for the identifier with ID=I+1 that has already 739 /// been loaded. 740 std::vector<IdentifierInfo *> IdentifiersLoaded; 741 742 typedef ContinuousRangeMap<serialization::IdentID, Module *, 4> 743 GlobalIdentifierMapType; 744 745 /// \brief Mapping from global identifer IDs to the module in which the 746 /// identifier resides along with the offset that should be added to the 747 /// global identifier ID to produce a local ID. 748 GlobalIdentifierMapType GlobalIdentifierMap; 749 750 /// \brief A vector containing selectors that have already been loaded. 751 /// 752 /// This vector is indexed by the Selector ID (-1). NULL selector 753 /// entries indicate that the particular selector ID has not yet 754 /// been loaded. 755 SmallVector<Selector, 16> SelectorsLoaded; 756 757 typedef ContinuousRangeMap<serialization::SelectorID, Module *, 4> 758 GlobalSelectorMapType; 759 760 /// \brief Mapping from global selector IDs to the module in which the 761 /// selector resides along with the offset that should be added to the 762 /// global selector ID to produce a local ID. 763 GlobalSelectorMapType GlobalSelectorMap; 764 765 /// \brief The macro definitions we have already loaded. 766 SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded; 767 768 typedef ContinuousRangeMap<serialization::MacroID, Module *, 4> 769 GlobalMacroDefinitionMapType; 770 771 /// \brief Mapping from global macro definition IDs to the module in which the 772 /// selector resides along with the offset that should be added to the 773 /// global selector ID to produce a local ID. 774 GlobalMacroDefinitionMapType GlobalMacroDefinitionMap; 775 776 /// \brief Mapping from identifiers that represent macros whose definitions 777 /// have not yet been deserialized to the global offset where the macro 778 /// record resides. 779 llvm::DenseMap<IdentifierInfo *, uint64_t> UnreadMacroRecordOffsets; 780 781 typedef ContinuousRangeMap<unsigned, Module *, 4> 782 GlobalPreprocessedEntityMapType; 783 784 /// \brief Mapping from global preprocessing entity IDs to the module in 785 /// which the preprocessed entity resides along with the offset that should be 786 /// added to the global preprocessing entitiy ID to produce a local ID. 787 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap; 788 789 /// \name CodeGen-relevant special data 790 /// \brief Fields containing data that is relevant to CodeGen. 791 //@{ 792 793 /// \brief The IDs of all declarations that fulfill the criteria of 794 /// "interesting" decls. 795 /// 796 /// This contains the data loaded from all EXTERNAL_DEFINITIONS blocks in the 797 /// chain. The referenced declarations are deserialized and passed to the 798 /// consumer eagerly. 799 SmallVector<uint64_t, 16> ExternalDefinitions; 800 801 /// \brief The IDs of all tentative definitions stored in the the chain. 802 /// 803 /// Sema keeps track of all tentative definitions in a TU because it has to 804 /// complete them and pass them on to CodeGen. Thus, tentative definitions in 805 /// the PCH chain must be eagerly deserialized. 806 SmallVector<uint64_t, 16> TentativeDefinitions; 807 808 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are 809 /// used. 810 /// 811 /// CodeGen has to emit VTables for these records, so they have to be eagerly 812 /// deserialized. 813 SmallVector<uint64_t, 64> VTableUses; 814 815 /// \brief A snapshot of the pending instantiations in the chain. 816 /// 817 /// This record tracks the instantiations that Sema has to perform at the 818 /// end of the TU. It consists of a pair of values for every pending 819 /// instantiation where the first value is the ID of the decl and the second 820 /// is the instantiation location. 821 SmallVector<uint64_t, 64> PendingInstantiations; 822 823 //@} 824 825 /// \name Diagnostic-relevant special data 826 /// \brief Fields containing data that is used for generating diagnostics 827 //@{ 828 829 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for 830 /// generating warnings. 831 SmallVector<uint64_t, 16> UnusedFileScopedDecls; 832 833 /// \brief A list of all the delegating constructors we've seen, to diagnose 834 /// cycles. 835 SmallVector<uint64_t, 4> DelegatingCtorDecls; 836 837 /// \brief Method selectors used in a @selector expression. Used for 838 /// implementation of -Wselector. 839 SmallVector<uint64_t, 64> ReferencedSelectorsData; 840 841 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for 842 /// generating warnings. 843 SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers; 844 845 /// \brief The IDs of type aliases for ext_vectors that exist in the chain. 846 /// 847 /// Used by Sema for finding sugared names for ext_vectors in diagnostics. 848 SmallVector<uint64_t, 4> ExtVectorDecls; 849 850 //@} 851 852 /// \name Sema-relevant special data 853 /// \brief Fields containing data that is used for semantic analysis 854 //@{ 855 856 /// \brief The IDs of all locally scoped external decls in the chain. 857 /// 858 /// Sema tracks these to validate that the types are consistent across all 859 /// local external declarations. 860 SmallVector<uint64_t, 16> LocallyScopedExternalDecls; 861 862 /// \brief The IDs of all dynamic class declarations in the chain. 863 /// 864 /// Sema tracks these because it checks for the key functions being defined 865 /// at the end of the TU, in which case it directs CodeGen to emit the VTable. 866 SmallVector<uint64_t, 16> DynamicClasses; 867 868 /// \brief The IDs of the declarations Sema stores directly. 869 /// 870 /// Sema tracks a few important decls, such as namespace std, directly. 871 SmallVector<uint64_t, 4> SemaDeclRefs; 872 873 /// \brief The IDs of the types ASTContext stores directly. 874 /// 875 /// The AST context tracks a few important types, such as va_list, directly. 876 SmallVector<uint64_t, 16> SpecialTypes; 877 878 /// \brief The IDs of CUDA-specific declarations ASTContext stores directly. 879 /// 880 /// The AST context tracks a few important decls, currently cudaConfigureCall, 881 /// directly. 882 SmallVector<uint64_t, 2> CUDASpecialDeclRefs; 883 884 /// \brief The floating point pragma option settings. 885 SmallVector<uint64_t, 1> FPPragmaOptions; 886 887 /// \brief The OpenCL extension settings. 888 SmallVector<uint64_t, 1> OpenCLExtensions; 889 890 /// \brief A list of the namespaces we've seen. 891 SmallVector<uint64_t, 4> KnownNamespaces; 892 893 //@} 894 895 /// \brief The original file name that was used to build the primary AST file, 896 /// which may have been modified for relocatable-pch support. 897 std::string OriginalFileName; 898 899 /// \brief The actual original file name that was used to build the primary 900 /// AST file. 901 std::string ActualOriginalFileName; 902 903 /// \brief The file ID for the original file that was used to build the 904 /// primary AST file. 905 FileID OriginalFileID; 906 907 /// \brief The directory that the PCH was originally created in. Used to 908 /// allow resolving headers even after headers+PCH was moved to a new path. 909 std::string OriginalDir; 910 911 /// \brief The directory that the PCH we are reading is stored in. 912 std::string CurrentDir; 913 914 /// \brief Whether this precompiled header is a relocatable PCH file. 915 bool RelocatablePCH; 916 917 /// \brief The system include root to be used when loading the 918 /// precompiled header. 919 std::string isysroot; 920 921 /// \brief Whether to disable the normal validation performed on precompiled 922 /// headers when they are loaded. 923 bool DisableValidation; 924 925 /// \brief Whether to disable the use of stat caches in AST files. 926 bool DisableStatCache; 927 928 /// \brief Mapping from switch-case IDs in the chain to switch-case statements 929 /// 930 /// Statements usually don't have IDs, but switch cases need them, so that the 931 /// switch statement can refer to them. 932 std::map<unsigned, SwitchCase *> SwitchCaseStmts; 933 934 /// \brief Mapping from opaque value IDs to OpaqueValueExprs. 935 std::map<unsigned, OpaqueValueExpr*> OpaqueValueExprs; 936 937 /// \brief The number of stat() calls that hit/missed the stat 938 /// cache. 939 unsigned NumStatHits, NumStatMisses; 940 941 /// \brief The number of source location entries de-serialized from 942 /// the PCH file. 943 unsigned NumSLocEntriesRead; 944 945 /// \brief The number of source location entries in the chain. 946 unsigned TotalNumSLocEntries; 947 948 /// \brief The number of statements (and expressions) de-serialized 949 /// from the chain. 950 unsigned NumStatementsRead; 951 952 /// \brief The total number of statements (and expressions) stored 953 /// in the chain. 954 unsigned TotalNumStatements; 955 956 /// \brief The number of macros de-serialized from the chain. 957 unsigned NumMacrosRead; 958 959 /// \brief The total number of macros stored in the chain. 960 unsigned TotalNumMacros; 961 962 /// \brief The number of selectors that have been read. 963 unsigned NumSelectorsRead; 964 965 /// \brief The number of method pool entries that have been read. 966 unsigned NumMethodPoolEntriesRead; 967 968 /// \brief The number of times we have looked up a selector in the method 969 /// pool and not found anything interesting. 970 unsigned NumMethodPoolMisses; 971 972 /// \brief The total number of method pool entries in the selector table. 973 unsigned TotalNumMethodPoolEntries; 974 975 /// Number of lexical decl contexts read/total. 976 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts; 977 978 /// Number of visible decl contexts read/total. 979 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts; 980 981 /// Total size of modules, in bits, currently loaded 982 uint64_t TotalModulesSizeInBits; 983 984 /// \brief Number of Decl/types that are currently deserializing. 985 unsigned NumCurrentElementsDeserializing; 986 987 /// Number of CXX base specifiers currently loaded 988 unsigned NumCXXBaseSpecifiersLoaded; 989 990 /// \brief An IdentifierInfo that has been loaded but whose top-level 991 /// declarations of the same name have not (yet) been loaded. 992 struct PendingIdentifierInfo { 993 IdentifierInfo *II; 994 SmallVector<uint32_t, 4> DeclIDs; 995 }; 996 997 /// \brief The set of identifiers that were read while the AST reader was 998 /// (recursively) loading declarations. 999 /// 1000 /// The declarations on the identifier chain for these identifiers will be 1001 /// loaded once the recursive loading has completed. 1002 std::deque<PendingIdentifierInfo> PendingIdentifierInfos; 1003 1004 /// \brief Contains declarations and definitions that will be 1005 /// "interesting" to the ASTConsumer, when we get that AST consumer. 1006 /// 1007 /// "Interesting" declarations are those that have data that may 1008 /// need to be emitted, such as inline function definitions or 1009 /// Objective-C protocols. 1010 std::deque<Decl *> InterestingDecls; 1011 1012 /// \brief We delay loading of the previous declaration chain to avoid 1013 /// deeply nested calls when there are many redeclarations. 1014 std::deque<std::pair<Decl *, serialization::DeclID> > PendingPreviousDecls; 1015 1016 /// \brief Ready to load the previous declaration of the given Decl. 1017 void loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID); 1018 1019 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack. 1020 SmallVector<Stmt *, 16> StmtStack; 1021 1022 /// \brief What kind of records we are reading. 1023 enum ReadingKind { 1024 Read_Decl, Read_Type, Read_Stmt 1025 }; 1026 1027 /// \brief What kind of records we are reading. 1028 ReadingKind ReadingKind; 1029 1030 /// \brief RAII object to change the reading kind. 1031 class ReadingKindTracker { 1032 ASTReader &Reader; 1033 enum ReadingKind PrevKind; 1034 1035 ReadingKindTracker(const ReadingKindTracker&); // do not implement 1036 ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement 1037 1038 public: 1039 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader) 1040 : Reader(reader), PrevKind(Reader.ReadingKind) { 1041 Reader.ReadingKind = newKind; 1042 } 1043 1044 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; } 1045 }; 1046 1047 /// \brief All predefines buffers in the chain, to be treated as if 1048 /// concatenated. 1049 PCHPredefinesBlocks PCHPredefinesBuffers; 1050 1051 /// \brief Suggested contents of the predefines buffer, after this 1052 /// PCH file has been processed. 1053 /// 1054 /// In most cases, this string will be empty, because the predefines 1055 /// buffer computed to build the PCH file will be identical to the 1056 /// predefines buffer computed from the command line. However, when 1057 /// there are differences that the PCH reader can work around, this 1058 /// predefines buffer may contain additional definitions. 1059 std::string SuggestedPredefines; 1060 1061 /// \brief Reads a statement from the specified cursor. 1062 Stmt *ReadStmtFromStream(Module &F); 1063 1064 /// \brief Get a FileEntry out of stored-in-PCH filename, making sure we take 1065 /// into account all the necessary relocations. 1066 const FileEntry *getFileEntry(StringRef filename); 1067 1068 void MaybeAddSystemRootToFilename(std::string &Filename); 1069 1070 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type, 1071 Module *ImportedBy); 1072 ASTReadResult ReadASTBlock(Module &F); 1073 bool CheckPredefinesBuffers(); 1074 bool ParseLineTable(Module &F, SmallVectorImpl<uint64_t> &Record); 1075 ASTReadResult ReadSourceManagerBlock(Module &F); 1076 ASTReadResult ReadSLocEntryRecord(int ID); 1077 llvm::BitstreamCursor &SLocCursorForID(int ID); 1078 SourceLocation getImportLocation(Module *F); 1079 bool ParseLanguageOptions(const SmallVectorImpl<uint64_t> &Record); 1080 1081 struct RecordLocation { 1082 RecordLocation(Module *M, uint64_t O) 1083 : F(M), Offset(O) {} 1084 Module *F; 1085 uint64_t Offset; 1086 }; 1087 1088 QualType readTypeRecord(unsigned Index); 1089 RecordLocation TypeCursorForIndex(unsigned Index); 1090 void LoadedDecl(unsigned Index, Decl *D); 1091 Decl *ReadDeclRecord(serialization::DeclID ID); 1092 RecordLocation DeclCursorForID(serialization::DeclID ID); 1093 void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D); 1094 1095 RecordLocation getLocalBitOffset(uint64_t GlobalOffset); 1096 uint64_t getGlobalBitOffset(Module &M, uint32_t LocalOffset); 1097 1098 void PassInterestingDeclsToConsumer(); 1099 1100 /// \brief Produce an error diagnostic and return true. 1101 /// 1102 /// This routine should only be used for fatal errors that have to 1103 /// do with non-routine failures (e.g., corrupted AST file). 1104 void Error(StringRef Msg); 1105 void Error(unsigned DiagID, StringRef Arg1 = StringRef(), 1106 StringRef Arg2 = StringRef()); 1107 1108 ASTReader(const ASTReader&); // do not implement 1109 ASTReader &operator=(const ASTReader &); // do not implement 1110public: 1111 typedef SmallVector<uint64_t, 64> RecordData; 1112 1113 /// \brief Load the AST file and validate its contents against the given 1114 /// Preprocessor. 1115 /// 1116 /// \param PP the preprocessor associated with the context in which this 1117 /// precompiled header will be loaded. 1118 /// 1119 /// \param Context the AST context that this precompiled header will be 1120 /// loaded into. 1121 /// 1122 /// \param isysroot If non-NULL, the system include path specified by the 1123 /// user. This is only used with relocatable PCH files. If non-NULL, 1124 /// a relocatable PCH file will use the default path "/". 1125 /// 1126 /// \param DisableValidation If true, the AST reader will suppress most 1127 /// of its regular consistency checking, allowing the use of precompiled 1128 /// headers that cannot be determined to be compatible. 1129 /// 1130 /// \param DisableStatCache If true, the AST reader will ignore the 1131 /// stat cache in the AST files. This performance pessimization can 1132 /// help when an AST file is being used in cases where the 1133 /// underlying files in the file system may have changed, but 1134 /// parsing should still continue. 1135 ASTReader(Preprocessor &PP, ASTContext *Context, StringRef isysroot = "", 1136 bool DisableValidation = false, bool DisableStatCache = false); 1137 1138 /// \brief Load the AST file without using any pre-initialized Preprocessor. 1139 /// 1140 /// The necessary information to initialize a Preprocessor later can be 1141 /// obtained by setting a ASTReaderListener. 1142 /// 1143 /// \param SourceMgr the source manager into which the AST file will be loaded 1144 /// 1145 /// \param FileMgr the file manager into which the AST file will be loaded. 1146 /// 1147 /// \param Diags the diagnostics system to use for reporting errors and 1148 /// warnings relevant to loading the AST file. 1149 /// 1150 /// \param isysroot If non-NULL, the system include path specified by the 1151 /// user. This is only used with relocatable PCH files. If non-NULL, 1152 /// a relocatable PCH file will use the default path "/". 1153 /// 1154 /// \param DisableValidation If true, the AST reader will suppress most 1155 /// of its regular consistency checking, allowing the use of precompiled 1156 /// headers that cannot be determined to be compatible. 1157 /// 1158 /// \param DisableStatCache If true, the AST reader will ignore the 1159 /// stat cache in the AST files. This performance pessimization can 1160 /// help when an AST file is being used in cases where the 1161 /// underlying files in the file system may have changed, but 1162 /// parsing should still continue. 1163 ASTReader(SourceManager &SourceMgr, FileManager &FileMgr, 1164 Diagnostic &Diags, StringRef isysroot = "", 1165 bool DisableValidation = false, bool DisableStatCache = false); 1166 ~ASTReader(); 1167 1168 /// \brief Load the AST file designated by the given file name. 1169 ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type); 1170 1171 /// \brief Checks that no file that is stored in PCH is out-of-sync with 1172 /// the actual file in the file system. 1173 ASTReadResult validateFileEntries(); 1174 1175 /// \brief Set the AST callbacks listener. 1176 void setListener(ASTReaderListener *listener) { 1177 Listener.reset(listener); 1178 } 1179 1180 /// \brief Set the AST deserialization listener. 1181 void setDeserializationListener(ASTDeserializationListener *Listener); 1182 1183 /// \brief Set the Preprocessor to use. 1184 void setPreprocessor(Preprocessor &pp); 1185 1186 /// \brief Sets and initializes the given Context. 1187 void InitializeContext(ASTContext &Context); 1188 1189 /// \brief Add in-memory (virtual file) buffer. 1190 void addInMemoryBuffer(StringRef &FileName, llvm::MemoryBuffer *Buffer) { 1191 ModuleMgr.addInMemoryBuffer(FileName, Buffer); 1192 } 1193 1194 /// \brief Retrieve the module manager. 1195 ModuleManager &getModuleManager() { return ModuleMgr; } 1196 1197 /// \brief Retrieve the preprocessor. 1198 Preprocessor &getPreprocessor() const { 1199 assert(PP && "ASTReader does not have a preprocessor"); 1200 return *PP; 1201 } 1202 1203 /// \brief Retrieve the name of the original source file name 1204 const std::string &getOriginalSourceFile() { return OriginalFileName; } 1205 1206 /// \brief Retrieve the name of the original source file name directly from 1207 /// the AST file, without actually loading the AST file. 1208 static std::string getOriginalSourceFile(const std::string &ASTFileName, 1209 FileManager &FileMgr, 1210 Diagnostic &Diags); 1211 1212 /// \brief Returns the suggested contents of the predefines buffer, 1213 /// which contains a (typically-empty) subset of the predefines 1214 /// build prior to including the precompiled header. 1215 const std::string &getSuggestedPredefines() { return SuggestedPredefines; } 1216 1217 /// \brief Read preprocessed entities into the preprocessing record. 1218 virtual void ReadPreprocessedEntities(); 1219 1220 /// \brief Read the preprocessed entity at the given offset. 1221 virtual PreprocessedEntity *ReadPreprocessedEntityAtOffset(uint64_t Offset); 1222 1223 /// \brief Read the header file information for the given file entry. 1224 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE); 1225 1226 void ReadPragmaDiagnosticMappings(Diagnostic &Diag); 1227 1228 /// \brief Returns the number of source locations found in the chain. 1229 unsigned getTotalNumSLocs() const { 1230 return TotalNumSLocEntries; 1231 } 1232 1233 /// \brief Returns the number of identifiers found in the chain. 1234 unsigned getTotalNumIdentifiers() const { 1235 return static_cast<unsigned>(IdentifiersLoaded.size()); 1236 } 1237 1238 /// \brief Returns the number of types found in the chain. 1239 unsigned getTotalNumTypes() const { 1240 return static_cast<unsigned>(TypesLoaded.size()); 1241 } 1242 1243 /// \brief Returns the number of declarations found in the chain. 1244 unsigned getTotalNumDecls() const { 1245 return static_cast<unsigned>(DeclsLoaded.size()); 1246 } 1247 1248 /// \brief Returns the number of selectors found in the chain. 1249 unsigned getTotalNumSelectors() const { 1250 return static_cast<unsigned>(SelectorsLoaded.size()); 1251 } 1252 1253 /// \brief Returns the number of preprocessed entities known to the AST 1254 /// reader. 1255 unsigned getTotalNumPreprocessedEntities() const { 1256 unsigned Result = 0; 1257 for (ModuleConstIterator I = ModuleMgr.begin(), 1258 E = ModuleMgr.end(); I != E; ++I) { 1259 Result += (*I)->NumPreallocatedPreprocessingEntities; 1260 } 1261 1262 return Result; 1263 } 1264 1265 /// \brief Returns the number of macro definitions found in the chain. 1266 unsigned getTotalNumMacroDefinitions() const { 1267 return static_cast<unsigned>(MacroDefinitionsLoaded.size()); 1268 } 1269 1270 /// \brief Returns the number of C++ base specifiers found in the chain. 1271 unsigned getTotalNumCXXBaseSpecifiers() const { 1272 return NumCXXBaseSpecifiersLoaded; 1273 } 1274 1275 /// \brief Reads a TemplateArgumentLocInfo appropriate for the 1276 /// given TemplateArgument kind. 1277 TemplateArgumentLocInfo 1278 GetTemplateArgumentLocInfo(Module &F, TemplateArgument::ArgKind Kind, 1279 const RecordData &Record, unsigned &Idx); 1280 1281 /// \brief Reads a TemplateArgumentLoc. 1282 TemplateArgumentLoc 1283 ReadTemplateArgumentLoc(Module &F, 1284 const RecordData &Record, unsigned &Idx); 1285 1286 /// \brief Reads a declarator info from the given record. 1287 TypeSourceInfo *GetTypeSourceInfo(Module &F, 1288 const RecordData &Record, unsigned &Idx); 1289 1290 /// \brief Resolve a type ID into a type, potentially building a new 1291 /// type. 1292 QualType GetType(serialization::TypeID ID); 1293 1294 /// \brief Resolve a local type ID within a given AST file into a type. 1295 QualType getLocalType(Module &F, unsigned LocalID); 1296 1297 /// \brief Map a local type ID within a given AST file into a global type ID. 1298 serialization::TypeID getGlobalTypeID(Module &F, unsigned LocalID) const; 1299 1300 /// \brief Read a type from the current position in the given record, which 1301 /// was read from the given AST file. 1302 QualType readType(Module &F, const RecordData &Record, unsigned &Idx) { 1303 if (Idx >= Record.size()) 1304 return QualType(); 1305 1306 return getLocalType(F, Record[Idx++]); 1307 } 1308 1309 /// \brief Map from a local declaration ID within a given module to a 1310 /// global declaration ID. 1311 serialization::DeclID getGlobalDeclID(Module &F, unsigned LocalID) const; 1312 1313 /// \brief Resolve a declaration ID into a declaration, potentially 1314 /// building a new declaration. 1315 Decl *GetDecl(serialization::DeclID ID); 1316 virtual Decl *GetExternalDecl(uint32_t ID); 1317 1318 /// \brief Reads a declaration with the given local ID in the given module. 1319 Decl *GetLocalDecl(Module &F, uint32_t LocalID) { 1320 return GetDecl(getGlobalDeclID(F, LocalID)); 1321 } 1322 1323 /// \brief Reads a declaration with the given local ID in the given module. 1324 /// 1325 /// \returns The requested declaration, casted to the given return type. 1326 template<typename T> 1327 T *GetLocalDeclAs(Module &F, uint32_t LocalID) { 1328 return cast_or_null<T>(GetLocalDecl(F, LocalID)); 1329 } 1330 1331 /// \brief Reads a declaration ID from the given position in a record in the 1332 /// given module. 1333 /// 1334 /// \returns The declaration ID read from the record, adjusted to a global ID. 1335 serialization::DeclID ReadDeclID(Module &F, const RecordData &Record, 1336 unsigned &Idx); 1337 1338 /// \brief Reads a declaration from the given position in a record in the 1339 /// given module. 1340 Decl *ReadDecl(Module &F, const RecordData &R, unsigned &I) { 1341 return GetDecl(ReadDeclID(F, R, I)); 1342 } 1343 1344 /// \brief Reads a declaration from the given position in a record in the 1345 /// given module. 1346 /// 1347 /// \returns The declaration read from this location, casted to the given 1348 /// result type. 1349 template<typename T> 1350 T *ReadDeclAs(Module &F, const RecordData &R, unsigned &I) { 1351 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I))); 1352 } 1353 1354 /// \brief Read a CXXBaseSpecifiers ID form the given record and 1355 /// return its global bit offset. 1356 uint64_t readCXXBaseSpecifiers(Module &M, const RecordData &Record, 1357 unsigned &Idx); 1358 1359 virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset); 1360 1361 /// \brief Resolve the offset of a statement into a statement. 1362 /// 1363 /// This operation will read a new statement from the external 1364 /// source each time it is called, and is meant to be used via a 1365 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 1366 virtual Stmt *GetExternalDeclStmt(uint64_t Offset); 1367 1368 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1369 /// specified cursor. Read the abbreviations that are at the top of the block 1370 /// and then leave the cursor pointing into the block. 1371 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID); 1372 1373 /// \brief Finds all the visible declarations with a given name. 1374 /// The current implementation of this method just loads the entire 1375 /// lookup table as unmaterialized references. 1376 virtual DeclContext::lookup_result 1377 FindExternalVisibleDeclsByName(const DeclContext *DC, 1378 DeclarationName Name); 1379 1380 /// \brief Read all of the declarations lexically stored in a 1381 /// declaration context. 1382 /// 1383 /// \param DC The declaration context whose declarations will be 1384 /// read. 1385 /// 1386 /// \param Decls Vector that will contain the declarations loaded 1387 /// from the external source. The caller is responsible for merging 1388 /// these declarations with any declarations already stored in the 1389 /// declaration context. 1390 /// 1391 /// \returns true if there was an error while reading the 1392 /// declarations for this declaration context. 1393 virtual ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC, 1394 bool (*isKindWeWant)(Decl::Kind), 1395 SmallVectorImpl<Decl*> &Decls); 1396 1397 /// \brief Notify ASTReader that we started deserialization of 1398 /// a decl or type so until FinishedDeserializing is called there may be 1399 /// decls that are initializing. Must be paired with FinishedDeserializing. 1400 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; } 1401 1402 /// \brief Notify ASTReader that we finished the deserialization of 1403 /// a decl or type. Must be paired with StartedDeserializing. 1404 virtual void FinishedDeserializing(); 1405 1406 /// \brief Function that will be invoked when we begin parsing a new 1407 /// translation unit involving this external AST source. 1408 /// 1409 /// This function will provide all of the external definitions to 1410 /// the ASTConsumer. 1411 virtual void StartTranslationUnit(ASTConsumer *Consumer); 1412 1413 /// \brief Print some statistics about AST usage. 1414 virtual void PrintStats(); 1415 1416 /// \brief Dump information about the AST reader to standard error. 1417 void dump(); 1418 1419 /// Return the amount of memory used by memory buffers, breaking down 1420 /// by heap-backed versus mmap'ed memory. 1421 virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const; 1422 1423 /// \brief Initialize the semantic source with the Sema instance 1424 /// being used to perform semantic analysis on the abstract syntax 1425 /// tree. 1426 virtual void InitializeSema(Sema &S); 1427 1428 /// \brief Inform the semantic consumer that Sema is no longer available. 1429 virtual void ForgetSema() { SemaObj = 0; } 1430 1431 /// \brief Retrieve the IdentifierInfo for the named identifier. 1432 /// 1433 /// This routine builds a new IdentifierInfo for the given identifier. If any 1434 /// declarations with this name are visible from translation unit scope, their 1435 /// declarations will be deserialized and introduced into the declaration 1436 /// chain of the identifier. 1437 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd); 1438 IdentifierInfo *get(StringRef Name) { 1439 return get(Name.begin(), Name.end()); 1440 } 1441 1442 /// \brief Retrieve an iterator into the set of all identifiers 1443 /// in all loaded AST files. 1444 virtual IdentifierIterator *getIdentifiers() const; 1445 1446 /// \brief Load the contents of the global method pool for a given 1447 /// selector. 1448 /// 1449 /// \returns a pair of Objective-C methods lists containing the 1450 /// instance and factory methods, respectively, with this selector. 1451 virtual std::pair<ObjCMethodList, ObjCMethodList> 1452 ReadMethodPool(Selector Sel); 1453 1454 /// \brief Load the set of namespaces that are known to the external source, 1455 /// which will be used during typo correction. 1456 virtual void ReadKnownNamespaces( 1457 SmallVectorImpl<NamespaceDecl *> &Namespaces); 1458 1459 virtual void ReadTentativeDefinitions( 1460 SmallVectorImpl<VarDecl *> &TentativeDefs); 1461 1462 virtual void ReadUnusedFileScopedDecls( 1463 SmallVectorImpl<const DeclaratorDecl *> &Decls); 1464 1465 virtual void ReadDelegatingConstructors( 1466 SmallVectorImpl<CXXConstructorDecl *> &Decls); 1467 1468 virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls); 1469 1470 virtual void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls); 1471 1472 virtual void ReadLocallyScopedExternalDecls( 1473 SmallVectorImpl<NamedDecl *> &Decls); 1474 1475 virtual void ReadReferencedSelectors( 1476 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels); 1477 1478 virtual void ReadWeakUndeclaredIdentifiers( 1479 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI); 1480 1481 virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables); 1482 1483 virtual void ReadPendingInstantiations( 1484 SmallVectorImpl<std::pair<ValueDecl *, 1485 SourceLocation> > &Pending); 1486 1487 /// \brief Load a selector from disk, registering its ID if it exists. 1488 void LoadSelector(Selector Sel); 1489 1490 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II); 1491 void SetGloballyVisibleDecls(IdentifierInfo *II, 1492 const SmallVectorImpl<uint32_t> &DeclIDs, 1493 bool Nonrecursive = false); 1494 1495 /// \brief Report a diagnostic. 1496 DiagnosticBuilder Diag(unsigned DiagID); 1497 1498 /// \brief Report a diagnostic. 1499 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); 1500 1501 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID); 1502 1503 IdentifierInfo *GetIdentifierInfo(Module &M, const RecordData &Record, 1504 unsigned &Idx) { 1505 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++])); 1506 } 1507 1508 virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) { 1509 return DecodeIdentifierInfo(ID); 1510 } 1511 1512 IdentifierInfo *getLocalIdentifier(Module &M, unsigned LocalID); 1513 1514 serialization::IdentifierID getGlobalIdentifierID(Module &M, 1515 unsigned LocalID); 1516 1517 /// \brief Read the source location entry with index ID. 1518 virtual bool ReadSLocEntry(int ID); 1519 1520 /// \brief Retrieve a selector from the given module with its local ID 1521 /// number. 1522 Selector getLocalSelector(Module &M, unsigned LocalID); 1523 1524 Selector DecodeSelector(serialization::SelectorID Idx); 1525 1526 virtual Selector GetExternalSelector(serialization::SelectorID ID); 1527 uint32_t GetNumExternalSelectors(); 1528 1529 Selector ReadSelector(Module &M, const RecordData &Record, unsigned &Idx) { 1530 return getLocalSelector(M, Record[Idx++]); 1531 } 1532 1533 /// \brief Retrieve the global selector ID that corresponds to this 1534 /// the local selector ID in a given module. 1535 serialization::SelectorID getGlobalSelectorID(Module &F, 1536 unsigned LocalID) const; 1537 1538 /// \brief Read a declaration name. 1539 DeclarationName ReadDeclarationName(Module &F, 1540 const RecordData &Record, unsigned &Idx); 1541 void ReadDeclarationNameLoc(Module &F, 1542 DeclarationNameLoc &DNLoc, DeclarationName Name, 1543 const RecordData &Record, unsigned &Idx); 1544 void ReadDeclarationNameInfo(Module &F, DeclarationNameInfo &NameInfo, 1545 const RecordData &Record, unsigned &Idx); 1546 1547 void ReadQualifierInfo(Module &F, QualifierInfo &Info, 1548 const RecordData &Record, unsigned &Idx); 1549 1550 NestedNameSpecifier *ReadNestedNameSpecifier(Module &F, 1551 const RecordData &Record, 1552 unsigned &Idx); 1553 1554 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(Module &F, 1555 const RecordData &Record, 1556 unsigned &Idx); 1557 1558 /// \brief Read a template name. 1559 TemplateName ReadTemplateName(Module &F, const RecordData &Record, 1560 unsigned &Idx); 1561 1562 /// \brief Read a template argument. 1563 TemplateArgument ReadTemplateArgument(Module &F, 1564 const RecordData &Record,unsigned &Idx); 1565 1566 /// \brief Read a template parameter list. 1567 TemplateParameterList *ReadTemplateParameterList(Module &F, 1568 const RecordData &Record, 1569 unsigned &Idx); 1570 1571 /// \brief Read a template argument array. 1572 void 1573 ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs, 1574 Module &F, const RecordData &Record, 1575 unsigned &Idx); 1576 1577 /// \brief Read a UnresolvedSet structure. 1578 void ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set, 1579 const RecordData &Record, unsigned &Idx); 1580 1581 /// \brief Read a C++ base specifier. 1582 CXXBaseSpecifier ReadCXXBaseSpecifier(Module &F, 1583 const RecordData &Record,unsigned &Idx); 1584 1585 /// \brief Read a CXXCtorInitializer array. 1586 std::pair<CXXCtorInitializer **, unsigned> 1587 ReadCXXCtorInitializers(Module &F, const RecordData &Record, 1588 unsigned &Idx); 1589 1590 /// \brief Read a source location from raw form. 1591 SourceLocation ReadSourceLocation(Module &Module, unsigned Raw) { 1592 unsigned Flag = Raw & (1U << 31); 1593 unsigned Offset = Raw & ~(1U << 31); 1594 assert(Module.SLocRemap.find(Offset) != Module.SLocRemap.end() && 1595 "Cannot find offset to remap."); 1596 int Remap = Module.SLocRemap.find(Offset)->second; 1597 Offset += Remap; 1598 assert((Offset & (1U << 31)) == 0 && 1599 "Bad offset in reading source location"); 1600 return SourceLocation::getFromRawEncoding(Offset | Flag); 1601 } 1602 1603 /// \brief Read a source location. 1604 SourceLocation ReadSourceLocation(Module &Module, 1605 const RecordData &Record, unsigned& Idx) { 1606 return ReadSourceLocation(Module, Record[Idx++]); 1607 } 1608 1609 /// \brief Read a source range. 1610 SourceRange ReadSourceRange(Module &F, 1611 const RecordData &Record, unsigned& Idx); 1612 1613 /// \brief Read an integral value 1614 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx); 1615 1616 /// \brief Read a signed integral value 1617 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx); 1618 1619 /// \brief Read a floating-point value 1620 llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx); 1621 1622 // \brief Read a string 1623 std::string ReadString(const RecordData &Record, unsigned &Idx); 1624 1625 /// \brief Read a version tuple. 1626 VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx); 1627 1628 CXXTemporary *ReadCXXTemporary(Module &F, const RecordData &Record, 1629 unsigned &Idx); 1630 1631 /// \brief Reads attributes from the current stream position. 1632 void ReadAttributes(Module &F, AttrVec &Attrs, 1633 const RecordData &Record, unsigned &Idx); 1634 1635 /// \brief Reads a statement. 1636 Stmt *ReadStmt(Module &F); 1637 1638 /// \brief Reads an expression. 1639 Expr *ReadExpr(Module &F); 1640 1641 /// \brief Reads a sub-statement operand during statement reading. 1642 Stmt *ReadSubStmt() { 1643 assert(ReadingKind == Read_Stmt && 1644 "Should be called only during statement reading!"); 1645 // Subexpressions are stored from last to first, so the next Stmt we need 1646 // is at the back of the stack. 1647 assert(!StmtStack.empty() && "Read too many sub statements!"); 1648 return StmtStack.pop_back_val(); 1649 } 1650 1651 /// \brief Reads a sub-expression operand during statement reading. 1652 Expr *ReadSubExpr(); 1653 1654 /// \brief Reads the macro record located at the given offset. 1655 void ReadMacroRecord(Module &F, uint64_t Offset); 1656 1657 /// \brief Reads the preprocessed entity located at the current stream 1658 /// position. 1659 PreprocessedEntity *LoadPreprocessedEntity(Module &F); 1660 1661 /// \brief Determine the global preprocessed entity ID that corresponds to 1662 /// the given local ID within the given module. 1663 serialization::PreprocessedEntityID 1664 getGlobalPreprocessedEntityID(Module &M, unsigned LocalID); 1665 1666 /// \brief Note that the identifier is a macro whose record will be loaded 1667 /// from the given AST file at the given (file-local) offset. 1668 void SetIdentifierIsMacro(IdentifierInfo *II, Module &F, 1669 uint64_t Offset); 1670 1671 /// \brief Read the set of macros defined by this external macro source. 1672 virtual void ReadDefinedMacros(); 1673 1674 /// \brief Read the macro definition for this identifier. 1675 virtual void LoadMacroDefinition(IdentifierInfo *II); 1676 1677 /// \brief Read the macro definition corresponding to this iterator 1678 /// into the unread macro record offsets table. 1679 void LoadMacroDefinition( 1680 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos); 1681 1682 /// \brief Retrieve the macro definition with the given ID. 1683 MacroDefinition *getMacroDefinition(serialization::MacroID ID); 1684 1685 /// \brief Retrieve the global macro definition ID that corresponds to the 1686 /// local macro definition ID within a given module. 1687 serialization::MacroID getGlobalMacroDefinitionID(Module &M, 1688 unsigned LocalID); 1689 1690 /// \brief Deserialize a macro definition that is local to the given 1691 /// module. 1692 MacroDefinition *getLocalMacroDefinition(Module &M, unsigned LocalID) { 1693 return getMacroDefinition(getGlobalMacroDefinitionID(M, LocalID)); 1694 } 1695 1696 /// \brief Retrieve the AST context that this AST reader supplements. 1697 ASTContext *getContext() { return Context; } 1698 1699 // \brief Contains declarations that were loaded before we have 1700 // access to a Sema object. 1701 SmallVector<NamedDecl *, 16> PreloadedDecls; 1702 1703 /// \brief Retrieve the semantic analysis object used to analyze the 1704 /// translation unit in which the precompiled header is being 1705 /// imported. 1706 Sema *getSema() { return SemaObj; } 1707 1708 /// \brief Retrieve the identifier table associated with the 1709 /// preprocessor. 1710 IdentifierTable &getIdentifierTable(); 1711 1712 /// \brief Record that the given ID maps to the given switch-case 1713 /// statement. 1714 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID); 1715 1716 /// \brief Retrieve the switch-case statement with the given ID. 1717 SwitchCase *getSwitchCaseWithID(unsigned ID); 1718 1719 void ClearSwitchCaseIDs(); 1720}; 1721 1722/// \brief Helper class that saves the current stream position and 1723/// then restores it when destroyed. 1724struct SavedStreamPosition { 1725 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor) 1726 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { } 1727 1728 ~SavedStreamPosition() { 1729 Cursor.JumpToBit(Offset); 1730 } 1731 1732private: 1733 llvm::BitstreamCursor &Cursor; 1734 uint64_t Offset; 1735}; 1736 1737inline void PCHValidator::Error(const char *Msg) { 1738 Reader.Error(Msg); 1739} 1740 1741} // end namespace clang 1742 1743#endif 1744