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