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