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