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