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