ASTReader.h revision cd9e339014b5f38e626735893010930605e9c232
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/Sema/ExternalSemaSource.h" 19#include "clang/AST/DeclarationName.h" 20#include "clang/AST/DeclObjC.h" 21#include "clang/AST/TemplateBase.h" 22#include "clang/Lex/ExternalPreprocessorSource.h" 23#include "clang/Lex/PreprocessingRecord.h" 24#include "clang/Basic/Diagnostic.h" 25#include "clang/Basic/IdentifierTable.h" 26#include "clang/Basic/SourceManager.h" 27#include "llvm/ADT/APFloat.h" 28#include "llvm/ADT/APInt.h" 29#include "llvm/ADT/APSInt.h" 30#include "llvm/ADT/OwningPtr.h" 31#include "llvm/ADT/SmallVector.h" 32#include "llvm/ADT/StringRef.h" 33#include "llvm/Bitcode/BitstreamReader.h" 34#include "llvm/System/DataTypes.h" 35#include <deque> 36#include <map> 37#include <string> 38#include <utility> 39#include <vector> 40 41namespace llvm { 42 class MemoryBuffer; 43} 44 45namespace clang { 46 47class AddrLabelExpr; 48class ASTConsumer; 49class ASTContext; 50class Attr; 51class Decl; 52class DeclContext; 53class NestedNameSpecifier; 54class CXXBaseSpecifier; 55class CXXBaseOrMemberInitializer; 56class GotoStmt; 57class LabelStmt; 58class MacroDefinition; 59class NamedDecl; 60class ASTDeserializationListener; 61class Preprocessor; 62class Sema; 63class SwitchCase; 64class ASTReader; 65class ASTDeclReader; 66struct HeaderFileInfo; 67 68struct PCHPredefinesBlock { 69 /// \brief The file ID for this predefines buffer in a PCH file. 70 FileID BufferID; 71 72 /// \brief This predefines buffer in a PCH file. 73 llvm::StringRef Data; 74}; 75typedef llvm::SmallVector<PCHPredefinesBlock, 2> PCHPredefinesBlocks; 76 77/// \brief Abstract interface for callback invocations by the ASTReader. 78/// 79/// While reading an AST file, the ASTReader will call the methods of the 80/// listener to pass on specific information. Some of the listener methods can 81/// return true to indicate to the ASTReader that the information (and 82/// consequently the AST file) is invalid. 83class ASTReaderListener { 84public: 85 virtual ~ASTReaderListener(); 86 87 /// \brief Receives the language options. 88 /// 89 /// \returns true to indicate the options are invalid or false otherwise. 90 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { 91 return false; 92 } 93 94 /// \brief Receives the target triple. 95 /// 96 /// \returns true to indicate the target triple is invalid or false otherwise. 97 virtual bool ReadTargetTriple(llvm::StringRef Triple) { 98 return false; 99 } 100 101 /// \brief Receives the contents of the predefines buffer. 102 /// 103 /// \param Buffers Information about the predefines buffers. 104 /// 105 /// \param OriginalFileName The original file name for the AST file, which 106 /// will appear as an entry in the predefines buffer. 107 /// 108 /// \param SuggestedPredefines If necessary, additional definitions are added 109 /// here. 110 /// 111 /// \returns true to indicate the predefines are invalid or false otherwise. 112 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 113 llvm::StringRef OriginalFileName, 114 std::string &SuggestedPredefines) { 115 return false; 116 } 117 118 /// \brief Receives a HeaderFileInfo entry. 119 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {} 120 121 /// \brief Receives __COUNTER__ value. 122 virtual void ReadCounter(unsigned Value) {} 123}; 124 125/// \brief ASTReaderListener implementation to validate the information of 126/// the PCH file against an initialized Preprocessor. 127class PCHValidator : public ASTReaderListener { 128 Preprocessor &PP; 129 ASTReader &Reader; 130 131 unsigned NumHeaderInfos; 132 133public: 134 PCHValidator(Preprocessor &PP, ASTReader &Reader) 135 : PP(PP), Reader(Reader), NumHeaderInfos(0) {} 136 137 virtual bool ReadLanguageOptions(const LangOptions &LangOpts); 138 virtual bool ReadTargetTriple(llvm::StringRef Triple); 139 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 140 llvm::StringRef OriginalFileName, 141 std::string &SuggestedPredefines); 142 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID); 143 virtual void ReadCounter(unsigned Value); 144 145private: 146 void Error(const char *Msg); 147}; 148 149/// \brief Reads an AST files chain containing the contents of a translation 150/// unit. 151/// 152/// The ASTReader class reads bitstreams (produced by the ASTWriter 153/// class) containing the serialized representation of a given 154/// abstract syntax tree and its supporting data structures. An 155/// instance of the ASTReader can be attached to an ASTContext object, 156/// which will provide access to the contents of the AST files. 157/// 158/// The AST reader provides lazy de-serialization of declarations, as 159/// required when traversing the AST. Only those AST nodes that are 160/// actually required will be de-serialized. 161class ASTReader 162 : public ExternalPreprocessorSource, 163 public ExternalPreprocessingRecordSource, 164 public ExternalSemaSource, 165 public IdentifierInfoLookup, 166 public ExternalIdentifierLookup, 167 public ExternalSLocEntrySource { 168public: 169 enum ASTReadResult { Success, Failure, IgnorePCH }; 170 friend class PCHValidator; 171 friend class ASTDeclReader; 172private: 173 /// \brief The receiver of some callbacks invoked by ASTReader. 174 llvm::OwningPtr<ASTReaderListener> Listener; 175 176 /// \brief The receiver of deserialization events. 177 ASTDeserializationListener *DeserializationListener; 178 179 SourceManager &SourceMgr; 180 FileManager &FileMgr; 181 Diagnostic &Diags; 182 183 /// \brief The semantic analysis object that will be processing the 184 /// AST files and the translation unit that uses it. 185 Sema *SemaObj; 186 187 /// \brief The preprocessor that will be loading the source file. 188 Preprocessor *PP; 189 190 /// \brief The AST context into which we'll read the AST files. 191 ASTContext *Context; 192 193 /// \brief The AST consumer. 194 ASTConsumer *Consumer; 195 196 /// \brief Information that is needed for every file in the chain. 197 struct PerFileData { 198 PerFileData(); 199 ~PerFileData(); 200 201 /// \brief The AST stat cache installed for this file, if any. 202 /// 203 /// The dynamic type of this stat cache is always ASTStatCache 204 void *StatCache; 205 206 /// \brief The bitstream reader from which we'll read the AST file. 207 llvm::BitstreamReader StreamFile; 208 llvm::BitstreamCursor Stream; 209 210 /// \brief The size of this file, in bits. 211 uint64_t SizeInBits; 212 213 /// \brief The cursor to the start of the preprocessor block, which stores 214 /// all of the macro definitions. 215 llvm::BitstreamCursor MacroCursor; 216 217 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It 218 /// has read all the abbreviations at the start of the block and is ready to 219 /// jump around with these in context. 220 llvm::BitstreamCursor DeclsCursor; 221 222 /// \brief The file name of the AST file. 223 std::string FileName; 224 225 /// \brief The memory buffer that stores the data associated with 226 /// this AST file. 227 llvm::OwningPtr<llvm::MemoryBuffer> Buffer; 228 229 /// \brief Cursor used to read source location entries. 230 llvm::BitstreamCursor SLocEntryCursor; 231 232 /// \brief The number of source location entries in this AST file. 233 unsigned LocalNumSLocEntries; 234 235 /// \brief Offsets for all of the source location entries in the 236 /// AST file. 237 const uint32_t *SLocOffsets; 238 239 /// \brief The number of types in this AST file. 240 unsigned LocalNumTypes; 241 242 /// \brief Offset of each type within the bitstream, indexed by the 243 /// type ID, or the representation of a Type*. 244 const uint32_t *TypeOffsets; 245 246 /// \brief The number of declarations in this AST file. 247 unsigned LocalNumDecls; 248 249 /// \brief Offset of each declaration within the bitstream, indexed 250 /// by the declaration ID (-1). 251 const uint32_t *DeclOffsets; 252 253 /// \brief The number of identifiers in this AST file. 254 unsigned LocalNumIdentifiers; 255 256 /// \brief Offsets into the identifier table data. 257 /// 258 /// This array is indexed by the identifier ID (-1), and provides 259 /// the offset into IdentifierTableData where the string data is 260 /// stored. 261 const uint32_t *IdentifierOffsets; 262 263 /// \brief Actual data for the on-disk hash table. 264 /// 265 // This pointer points into a memory buffer, where the on-disk hash 266 // table for identifiers actually lives. 267 const char *IdentifierTableData; 268 269 /// \brief A pointer to an on-disk hash table of opaque type 270 /// IdentifierHashTable. 271 void *IdentifierLookupTable; 272 273 /// \brief The number of macro definitions in this file. 274 unsigned LocalNumMacroDefinitions; 275 276 /// \brief Offsets of all of the macro definitions in the preprocessing 277 /// record in the AST file. 278 const uint32_t *MacroDefinitionOffsets; 279 280 /// \brief The number of preallocated preprocessing entities in the 281 /// preprocessing record. 282 unsigned NumPreallocatedPreprocessingEntities; 283 284 /// \brief A pointer to an on-disk hash table of opaque type 285 /// ASTSelectorLookupTable. 286 /// 287 /// This hash table provides the IDs of all selectors, and the associated 288 /// instance and factory methods. 289 void *SelectorLookupTable; 290 291 /// \brief A pointer to the character data that comprises the selector table 292 /// 293 /// The SelectorOffsets table refers into this memory. 294 const unsigned char *SelectorLookupTableData; 295 296 /// \brief Offsets into the method pool lookup table's data array 297 /// where each selector resides. 298 const uint32_t *SelectorOffsets; 299 300 /// \brief The number of selectors new to this file. 301 /// 302 /// This is the number of entries in SelectorOffsets. 303 unsigned LocalNumSelectors; 304 }; 305 306 /// \brief The chain of AST files. The first entry is the one named by the 307 /// user, the last one is the one that doesn't depend on anything further. 308 /// That is, the entry I was created with -include-pch I+1. 309 llvm::SmallVector<PerFileData*, 2> Chain; 310 311 /// \brief Types that have already been loaded from the chain. 312 /// 313 /// When the pointer at index I is non-NULL, the type with 314 /// ID = (I + 1) << FastQual::Width has already been loaded 315 std::vector<QualType> TypesLoaded; 316 317 /// \brief Map that provides the ID numbers of each type within the 318 /// output stream, plus those deserialized from a chained PCH. 319 /// 320 /// The ID numbers of types are consecutive (in order of discovery) 321 /// and start at 1. 0 is reserved for NULL. When types are actually 322 /// stored in the stream, the ID number is shifted by 2 bits to 323 /// allow for the const/volatile qualifiers. 324 /// 325 /// Keys in the map never have const/volatile qualifiers. 326 serialization::TypeIdxMap TypeIdxs; 327 328 /// \brief Declarations that have already been loaded from the chain. 329 /// 330 /// When the pointer at index I is non-NULL, the declaration with ID 331 /// = I + 1 has already been loaded. 332 std::vector<Decl *> DeclsLoaded; 333 334 typedef llvm::DenseMap<serialization::DeclID, 335 std::pair<PerFileData *, uint64_t> > 336 DeclReplacementMap; 337 /// \brief Declarations that have been replaced in a later file in the chain. 338 DeclReplacementMap ReplacedDecls; 339 340 /// \brief Information about the contents of a DeclContext. 341 struct DeclContextInfo { 342 void *NameLookupTableData; // a ASTDeclContextNameLookupTable. 343 const serialization::DeclID *LexicalDecls; 344 unsigned NumLexicalDecls; 345 }; 346 // In a full chain, there could be multiple updates to every decl context, 347 // so this is a vector. However, typically a chain is only two elements long, 348 // with only one file containing updates, so there will be only one update 349 // per decl context. 350 typedef llvm::SmallVector<DeclContextInfo, 1> DeclContextInfos; 351 typedef llvm::DenseMap<const DeclContext *, DeclContextInfos> 352 DeclContextOffsetsMap; 353 // Updates for visible decls can occur for other contexts than just the 354 // TU, and when we read those update records, the actual context will not 355 // be available yet (unless it's the TU), so have this pending map using the 356 // ID as a key. It will be realized when the context is actually loaded. 357 typedef llvm::SmallVector<void *, 1> DeclContextVisibleUpdates; 358 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates> 359 DeclContextVisibleUpdatesPending; 360 361 /// \brief Offsets of the lexical and visible declarations for each 362 /// DeclContext. 363 DeclContextOffsetsMap DeclContextOffsets; 364 365 /// \brief Updates to the visible declarations of declaration contexts that 366 /// haven't been loaded yet. 367 DeclContextVisibleUpdatesPending PendingVisibleUpdates; 368 369 typedef llvm::DenseMap<serialization::DeclID, serialization::DeclID> 370 FirstLatestDeclIDMap; 371 /// \brief Map of first declarations from a chained PCH that point to the 372 /// most recent declarations in another AST file. 373 FirstLatestDeclIDMap FirstLatestDeclIDs; 374 375 typedef llvm::SmallVector<serialization::DeclID, 4> 376 AdditionalTemplateSpecializations; 377 typedef llvm::DenseMap<serialization::DeclID, 378 AdditionalTemplateSpecializations> 379 AdditionalTemplateSpecializationsMap; 380 381 /// \brief Additional specializations (including partial) of templates that 382 /// were introduced after the template was serialized. 383 AdditionalTemplateSpecializationsMap AdditionalTemplateSpecializationsPending; 384 385 /// \brief Read the records that describe the contents of declcontexts. 386 bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor, 387 const std::pair<uint64_t, uint64_t> &Offsets, 388 DeclContextInfo &Info); 389 390 /// \brief A vector containing identifiers that have already been 391 /// loaded. 392 /// 393 /// If the pointer at index I is non-NULL, then it refers to the 394 /// IdentifierInfo for the identifier with ID=I+1 that has already 395 /// been loaded. 396 std::vector<IdentifierInfo *> IdentifiersLoaded; 397 398 /// \brief A vector containing selectors that have already been loaded. 399 /// 400 /// This vector is indexed by the Selector ID (-1). NULL selector 401 /// entries indicate that the particular selector ID has not yet 402 /// been loaded. 403 llvm::SmallVector<Selector, 16> SelectorsLoaded; 404 405 /// \brief Method selectors used in a @selector expression. Used for 406 /// implementation of -Wselector. 407 llvm::SmallVector<long long unsigned int,64u> ReferencedSelectorsData; 408 409 /// \brief The macro definitions we have already loaded. 410 llvm::SmallVector<MacroDefinition *, 16> MacroDefinitionsLoaded; 411 412 /// \brief The set of external definitions stored in the the chain. 413 llvm::SmallVector<uint64_t, 16> ExternalDefinitions; 414 415 /// \brief The set of tentative definitions stored in the the chain. 416 llvm::SmallVector<uint64_t, 16> TentativeDefinitions; 417 418 /// \brief The set of unused file scoped decls stored in the the chain. 419 llvm::SmallVector<uint64_t, 16> UnusedFileScopedDecls; 420 421 /// \brief The set of weak undeclared identifiers stored in the chain. 422 llvm::SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers; 423 424 /// \brief The set of locally-scoped external declarations stored in the chain 425 llvm::SmallVector<uint64_t, 16> LocallyScopedExternalDecls; 426 427 /// \brief The set of ext_vector type declarations stored in the the chain. 428 llvm::SmallVector<uint64_t, 4> ExtVectorDecls; 429 430 /// \brief The set of VTable uses of CXXRecordDecls stored in the chain. 431 llvm::SmallVector<uint64_t, 64> VTableUses; 432 433 /// \brief The set of dynamic CXXRecord declarations stored in the chain. 434 llvm::SmallVector<uint64_t, 16> DynamicClasses; 435 436 /// \brief The set of pending implicit instantiations stored in the chain. 437 llvm::SmallVector<uint64_t, 64> PendingImplicitInstantiations; 438 439 /// \brief The set of Sema declaration references stored in the chain. 440 llvm::SmallVector<uint64_t, 4> SemaDeclRefs; 441 442 /// \brief The original file name that was used to build the primary AST file, 443 /// which may have been modified for relocatable-pch support. 444 std::string OriginalFileName; 445 446 /// \brief The actual original file name that was used to build the primary 447 /// AST file. 448 std::string ActualOriginalFileName; 449 450 /// \brief Whether this precompiled header is a relocatable PCH file. 451 bool RelocatablePCH; 452 453 /// \brief The system include root to be used when loading the 454 /// precompiled header. 455 const char *isysroot; 456 457 /// \brief Whether to disable the normal validation performed on precompiled 458 /// headers when they are loaded. 459 bool DisableValidation; 460 461 /// \brief Mapping from switch-case IDs in the chain to switch-case statements 462 std::map<unsigned, SwitchCase *> SwitchCaseStmts; 463 464 /// \brief Mapping from label statement IDs in the chain to label statements. 465 std::map<unsigned, LabelStmt *> LabelStmts; 466 467 /// \brief Mapping from label IDs to the set of "goto" statements 468 /// that point to that label before the label itself has been 469 /// de-serialized. 470 std::multimap<unsigned, GotoStmt *> UnresolvedGotoStmts; 471 472 /// \brief Mapping from label IDs to the set of address label 473 /// expressions that point to that label before the label itself has 474 /// been de-serialized. 475 std::multimap<unsigned, AddrLabelExpr *> UnresolvedAddrLabelExprs; 476 477 /// \brief The number of stat() calls that hit/missed the stat 478 /// cache. 479 unsigned NumStatHits, NumStatMisses; 480 481 /// \brief The number of source location entries de-serialized from 482 /// the PCH file. 483 unsigned NumSLocEntriesRead; 484 485 /// \brief The number of source location entries in the chain. 486 unsigned TotalNumSLocEntries; 487 488 /// \brief The number of statements (and expressions) de-serialized 489 /// from the chain. 490 unsigned NumStatementsRead; 491 492 /// \brief The total number of statements (and expressions) stored 493 /// in the chain. 494 unsigned TotalNumStatements; 495 496 /// \brief The number of macros de-serialized from the chain. 497 unsigned NumMacrosRead; 498 499 /// \brief The total number of macros stored in the chain. 500 unsigned TotalNumMacros; 501 502 /// \brief The number of selectors that have been read. 503 unsigned NumSelectorsRead; 504 505 /// \brief The number of method pool entries that have been read. 506 unsigned NumMethodPoolEntriesRead; 507 508 /// \brief The number of times we have looked up a selector in the method 509 /// pool and not found anything interesting. 510 unsigned NumMethodPoolMisses; 511 512 /// \brief The total number of method pool entries in the selector table. 513 unsigned TotalNumMethodPoolEntries; 514 515 /// Number of lexical decl contexts read/total. 516 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts; 517 518 /// Number of visible decl contexts read/total. 519 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts; 520 521 /// \brief Number of Decl/types that are currently deserializing. 522 unsigned NumCurrentElementsDeserializing; 523 524 /// \brief An IdentifierInfo that has been loaded but whose top-level 525 /// declarations of the same name have not (yet) been loaded. 526 struct PendingIdentifierInfo { 527 IdentifierInfo *II; 528 llvm::SmallVector<uint32_t, 4> DeclIDs; 529 }; 530 531 /// \brief The set of identifiers that were read while the AST reader was 532 /// (recursively) loading declarations. 533 /// 534 /// The declarations on the identifier chain for these identifiers will be 535 /// loaded once the recursive loading has completed. 536 std::deque<PendingIdentifierInfo> PendingIdentifierInfos; 537 538 /// \brief FIXME: document! 539 llvm::SmallVector<uint64_t, 16> SpecialTypes; 540 541 /// \brief Contains declarations and definitions that will be 542 /// "interesting" to the ASTConsumer, when we get that AST consumer. 543 /// 544 /// "Interesting" declarations are those that have data that may 545 /// need to be emitted, such as inline function definitions or 546 /// Objective-C protocols. 547 std::deque<Decl *> InterestingDecls; 548 549 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack. 550 llvm::SmallVector<Stmt *, 16> StmtStack; 551 552 /// \brief What kind of records we are reading. 553 enum ReadingKind { 554 Read_Decl, Read_Type, Read_Stmt 555 }; 556 557 /// \brief What kind of records we are reading. 558 ReadingKind ReadingKind; 559 560 /// \brief RAII object to change the reading kind. 561 class ReadingKindTracker { 562 ASTReader &Reader; 563 enum ReadingKind PrevKind; 564 565 ReadingKindTracker(const ReadingKindTracker&); // do not implement 566 ReadingKindTracker &operator=(const ReadingKindTracker&);// do not implement 567 568 public: 569 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader) 570 : Reader(reader), PrevKind(Reader.ReadingKind) { 571 Reader.ReadingKind = newKind; 572 } 573 574 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; } 575 }; 576 577 /// \brief All predefines buffers in the chain, to be treated as if 578 /// concatenated. 579 PCHPredefinesBlocks PCHPredefinesBuffers; 580 581 /// \brief Suggested contents of the predefines buffer, after this 582 /// PCH file has been processed. 583 /// 584 /// In most cases, this string will be empty, because the predefines 585 /// buffer computed to build the PCH file will be identical to the 586 /// predefines buffer computed from the command line. However, when 587 /// there are differences that the PCH reader can work around, this 588 /// predefines buffer may contain additional definitions. 589 std::string SuggestedPredefines; 590 591 /// \brief Reads a statement from the specified cursor. 592 Stmt *ReadStmtFromStream(llvm::BitstreamCursor &Cursor); 593 594 void MaybeAddSystemRootToFilename(std::string &Filename); 595 596 ASTReadResult ReadASTCore(llvm::StringRef FileName); 597 ASTReadResult ReadASTBlock(PerFileData &F); 598 bool CheckPredefinesBuffers(); 599 bool ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record); 600 ASTReadResult ReadSourceManagerBlock(PerFileData &F); 601 ASTReadResult ReadSLocEntryRecord(unsigned ID); 602 llvm::BitstreamCursor &SLocCursorForID(unsigned ID); 603 bool ParseLanguageOptions(const llvm::SmallVectorImpl<uint64_t> &Record); 604 605 typedef std::pair<llvm::BitstreamCursor *, uint64_t> RecordLocation; 606 607 QualType ReadTypeRecord(unsigned Index); 608 RecordLocation TypeCursorForIndex(unsigned Index); 609 void LoadedDecl(unsigned Index, Decl *D); 610 Decl *ReadDeclRecord(unsigned Index, serialization::DeclID ID); 611 RecordLocation DeclCursorForIndex(unsigned Index, serialization::DeclID ID); 612 613 void PassInterestingDeclsToConsumer(); 614 615 /// \brief Produce an error diagnostic and return true. 616 /// 617 /// This routine should only be used for fatal errors that have to 618 /// do with non-routine failures (e.g., corrupted AST file). 619 void Error(const char *Msg); 620 621 ASTReader(const ASTReader&); // do not implement 622 ASTReader &operator=(const ASTReader &); // do not implement 623public: 624 typedef llvm::SmallVector<uint64_t, 64> RecordData; 625 626 /// \brief Load the AST file and validate its contents against the given 627 /// Preprocessor. 628 /// 629 /// \param PP the preprocessor associated with the context in which this 630 /// precompiled header will be loaded. 631 /// 632 /// \param Context the AST context that this precompiled header will be 633 /// loaded into. 634 /// 635 /// \param isysroot If non-NULL, the system include path specified by the 636 /// user. This is only used with relocatable PCH files. If non-NULL, 637 /// a relocatable PCH file will use the default path "/". 638 /// 639 /// \param DisableValidation If true, the AST reader will suppress most 640 /// of its regular consistency checking, allowing the use of precompiled 641 /// headers that cannot be determined to be compatible. 642 ASTReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0, 643 bool DisableValidation = false); 644 645 /// \brief Load the AST file without using any pre-initialized Preprocessor. 646 /// 647 /// The necessary information to initialize a Preprocessor later can be 648 /// obtained by setting a ASTReaderListener. 649 /// 650 /// \param SourceMgr the source manager into which the AST file will be loaded 651 /// 652 /// \param FileMgr the file manager into which the AST file will be loaded. 653 /// 654 /// \param Diags the diagnostics system to use for reporting errors and 655 /// warnings relevant to loading the AST file. 656 /// 657 /// \param isysroot If non-NULL, the system include path specified by the 658 /// user. This is only used with relocatable PCH files. If non-NULL, 659 /// a relocatable PCH file will use the default path "/". 660 /// 661 /// \param DisableValidation If true, the AST reader will suppress most 662 /// of its regular consistency checking, allowing the use of precompiled 663 /// headers that cannot be determined to be compatible. 664 ASTReader(SourceManager &SourceMgr, FileManager &FileMgr, 665 Diagnostic &Diags, const char *isysroot = 0, 666 bool DisableValidation = false); 667 ~ASTReader(); 668 669 /// \brief Load the precompiled header designated by the given file 670 /// name. 671 ASTReadResult ReadAST(const std::string &FileName); 672 673 /// \brief Set the AST callbacks listener. 674 void setListener(ASTReaderListener *listener) { 675 Listener.reset(listener); 676 } 677 678 /// \brief Set the AST deserialization listener. 679 void setDeserializationListener(ASTDeserializationListener *Listener); 680 681 /// \brief Set the Preprocessor to use. 682 void setPreprocessor(Preprocessor &pp); 683 684 /// \brief Sets and initializes the given Context. 685 void InitializeContext(ASTContext &Context); 686 687 /// \brief Retrieve the name of the named (primary) AST file 688 const std::string &getFileName() const { return Chain[0]->FileName; } 689 690 /// \brief Retrieve the name of the original source file name 691 const std::string &getOriginalSourceFile() { return OriginalFileName; } 692 693 /// \brief Retrieve the name of the original source file name directly from 694 /// the AST file, without actually loading the AST file. 695 static std::string getOriginalSourceFile(const std::string &ASTFileName, 696 Diagnostic &Diags); 697 698 /// \brief Returns the suggested contents of the predefines buffer, 699 /// which contains a (typically-empty) subset of the predefines 700 /// build prior to including the precompiled header. 701 const std::string &getSuggestedPredefines() { return SuggestedPredefines; } 702 703 /// \brief Read preprocessed entities into the 704 virtual void ReadPreprocessedEntities(); 705 706 /// \brief Returns the number of source locations found in the chain. 707 unsigned getTotalNumSLocs() const { 708 return TotalNumSLocEntries; 709 } 710 711 /// \brief Returns the number of identifiers found in the chain. 712 unsigned getTotalNumIdentifiers() const { 713 return static_cast<unsigned>(IdentifiersLoaded.size()); 714 } 715 716 /// \brief Returns the number of types found in the chain. 717 unsigned getTotalNumTypes() const { 718 return static_cast<unsigned>(TypesLoaded.size()); 719 } 720 721 /// \brief Returns the number of declarations found in the chain. 722 unsigned getTotalNumDecls() const { 723 return static_cast<unsigned>(DeclsLoaded.size()); 724 } 725 726 /// \brief Returns the number of selectors found in the chain. 727 unsigned getTotalNumSelectors() const { 728 return static_cast<unsigned>(SelectorsLoaded.size()); 729 } 730 731 /// \brief Reads a TemplateArgumentLocInfo appropriate for the 732 /// given TemplateArgument kind. 733 TemplateArgumentLocInfo 734 GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 735 llvm::BitstreamCursor &DeclsCursor, 736 const RecordData &Record, unsigned &Idx); 737 738 /// \brief Reads a TemplateArgumentLoc. 739 TemplateArgumentLoc 740 ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor, 741 const RecordData &Record, unsigned &Idx); 742 743 /// \brief Reads a declarator info from the given record. 744 TypeSourceInfo *GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor, 745 const RecordData &Record, unsigned &Idx); 746 747 /// \brief Resolve and return the translation unit declaration. 748 TranslationUnitDecl *GetTranslationUnitDecl(); 749 750 /// \brief Resolve a type ID into a type, potentially building a new 751 /// type. 752 QualType GetType(serialization::TypeID ID); 753 754 /// \brief Returns the type ID associated with the given type. 755 /// If the type didn't come from the AST file the ID that is returned is 756 /// marked as "doesn't exist in AST". 757 serialization::TypeID GetTypeID(QualType T) const; 758 759 /// \brief Returns the type index associated with the given type. 760 /// If the type didn't come from the AST file the index that is returned is 761 /// marked as "doesn't exist in AST". 762 serialization::TypeIdx GetTypeIdx(QualType T) const; 763 764 /// \brief Resolve a declaration ID into a declaration, potentially 765 /// building a new declaration. 766 Decl *GetDecl(serialization::DeclID ID); 767 virtual Decl *GetExternalDecl(uint32_t ID); 768 769 /// \brief Resolve the offset of a statement into a statement. 770 /// 771 /// This operation will read a new statement from the external 772 /// source each time it is called, and is meant to be used via a 773 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 774 virtual Stmt *GetExternalDeclStmt(uint64_t Offset); 775 776 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 777 /// specified cursor. Read the abbreviations that are at the top of the block 778 /// and then leave the cursor pointing into the block. 779 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID); 780 781 /// \brief Finds all the visible declarations with a given name. 782 /// The current implementation of this method just loads the entire 783 /// lookup table as unmaterialized references. 784 virtual DeclContext::lookup_result 785 FindExternalVisibleDeclsByName(const DeclContext *DC, 786 DeclarationName Name); 787 788 virtual void MaterializeVisibleDecls(const DeclContext *DC); 789 790 /// \brief Read all of the declarations lexically stored in a 791 /// declaration context. 792 /// 793 /// \param DC The declaration context whose declarations will be 794 /// read. 795 /// 796 /// \param Decls Vector that will contain the declarations loaded 797 /// from the external source. The caller is responsible for merging 798 /// these declarations with any declarations already stored in the 799 /// declaration context. 800 /// 801 /// \returns true if there was an error while reading the 802 /// declarations for this declaration context. 803 virtual bool FindExternalLexicalDecls(const DeclContext *DC, 804 llvm::SmallVectorImpl<Decl*> &Decls); 805 806 /// \brief Notify ASTReader that we started deserialization of 807 /// a decl or type so until FinishedDeserializing is called there may be 808 /// decls that are initializing. Must be paired with FinishedDeserializing. 809 virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; } 810 811 /// \brief Notify ASTReader that we finished the deserialization of 812 /// a decl or type. Must be paired with StartedDeserializing. 813 virtual void FinishedDeserializing(); 814 815 /// \brief Function that will be invoked when we begin parsing a new 816 /// translation unit involving this external AST source. 817 /// 818 /// This function will provide all of the external definitions to 819 /// the ASTConsumer. 820 virtual void StartTranslationUnit(ASTConsumer *Consumer); 821 822 /// \brief Print some statistics about AST usage. 823 virtual void PrintStats(); 824 825 /// \brief Initialize the semantic source with the Sema instance 826 /// being used to perform semantic analysis on the abstract syntax 827 /// tree. 828 virtual void InitializeSema(Sema &S); 829 830 /// \brief Inform the semantic consumer that Sema is no longer available. 831 virtual void ForgetSema() { SemaObj = 0; } 832 833 /// \brief Retrieve the IdentifierInfo for the named identifier. 834 /// 835 /// This routine builds a new IdentifierInfo for the given identifier. If any 836 /// declarations with this name are visible from translation unit scope, their 837 /// declarations will be deserialized and introduced into the declaration 838 /// chain of the identifier. 839 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd); 840 IdentifierInfo *get(llvm::StringRef Name) { 841 return get(Name.begin(), Name.end()); 842 } 843 844 /// \brief Load the contents of the global method pool for a given 845 /// selector. 846 /// 847 /// \returns a pair of Objective-C methods lists containing the 848 /// instance and factory methods, respectively, with this selector. 849 virtual std::pair<ObjCMethodList, ObjCMethodList> 850 ReadMethodPool(Selector Sel); 851 852 /// \brief Load a selector from disk, registering its ID if it exists. 853 void LoadSelector(Selector Sel); 854 855 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II); 856 void SetGloballyVisibleDecls(IdentifierInfo *II, 857 const llvm::SmallVectorImpl<uint32_t> &DeclIDs, 858 bool Nonrecursive = false); 859 860 /// \brief Report a diagnostic. 861 DiagnosticBuilder Diag(unsigned DiagID); 862 863 /// \brief Report a diagnostic. 864 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); 865 866 IdentifierInfo *DecodeIdentifierInfo(unsigned Idx); 867 868 IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) { 869 return DecodeIdentifierInfo(Record[Idx++]); 870 } 871 872 virtual IdentifierInfo *GetIdentifier(unsigned ID) { 873 return DecodeIdentifierInfo(ID); 874 } 875 876 /// \brief Read the source location entry with index ID. 877 virtual void ReadSLocEntry(unsigned ID); 878 879 Selector DecodeSelector(unsigned Idx); 880 881 virtual Selector GetExternalSelector(uint32_t ID); 882 uint32_t GetNumExternalSelectors(); 883 884 Selector GetSelector(const RecordData &Record, unsigned &Idx) { 885 return DecodeSelector(Record[Idx++]); 886 } 887 888 /// \brief Read a declaration name. 889 DeclarationName ReadDeclarationName(const RecordData &Record, unsigned &Idx); 890 891 NestedNameSpecifier *ReadNestedNameSpecifier(const RecordData &Record, 892 unsigned &Idx); 893 894 /// \brief Read a template name. 895 TemplateName ReadTemplateName(const RecordData &Record, unsigned &Idx); 896 897 /// \brief Read a template argument. 898 TemplateArgument ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor, 899 const RecordData &Record,unsigned &Idx); 900 901 /// \brief Read a template parameter list. 902 TemplateParameterList *ReadTemplateParameterList(const RecordData &Record, 903 unsigned &Idx); 904 905 /// \brief Read a template argument array. 906 void 907 ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, 908 llvm::BitstreamCursor &DeclsCursor, 909 const RecordData &Record, unsigned &Idx); 910 911 /// \brief Read a UnresolvedSet structure. 912 void ReadUnresolvedSet(UnresolvedSetImpl &Set, 913 const RecordData &Record, unsigned &Idx); 914 915 /// \brief Read a C++ base specifier. 916 CXXBaseSpecifier ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor, 917 const RecordData &Record,unsigned &Idx); 918 919 /// \brief Read a CXXBaseOrMemberInitializer array. 920 std::pair<CXXBaseOrMemberInitializer **, unsigned> 921 ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &DeclsCursor, 922 const RecordData &Record, unsigned &Idx); 923 924 /// \brief Read a source location. 925 SourceLocation ReadSourceLocation(const RecordData &Record, unsigned& Idx) { 926 return SourceLocation::getFromRawEncoding(Record[Idx++]); 927 } 928 929 /// \brief Read a source range. 930 SourceRange ReadSourceRange(const RecordData &Record, unsigned& Idx); 931 932 /// \brief Read an integral value 933 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx); 934 935 /// \brief Read a signed integral value 936 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx); 937 938 /// \brief Read a floating-point value 939 llvm::APFloat ReadAPFloat(const RecordData &Record, unsigned &Idx); 940 941 // \brief Read a string 942 std::string ReadString(const RecordData &Record, unsigned &Idx); 943 944 CXXTemporary *ReadCXXTemporary(const RecordData &Record, unsigned &Idx); 945 946 /// \brief Reads attributes from the current stream position. 947 void ReadAttributes(llvm::BitstreamCursor &DeclsCursor, AttrVec &Attrs); 948 949 /// \brief Reads a statement. 950 Stmt *ReadStmt(llvm::BitstreamCursor &Cursor); 951 952 /// \brief Reads an expression. 953 Expr *ReadExpr(llvm::BitstreamCursor &Cursor); 954 955 /// \brief Reads a sub-statement operand during statement reading. 956 Stmt *ReadSubStmt() { 957 assert(ReadingKind == Read_Stmt && 958 "Should be called only during statement reading!"); 959 // Subexpressions are stored from last to first, so the next Stmt we need 960 // is at the back of the stack. 961 assert(!StmtStack.empty() && "Read too many sub statements!"); 962 return StmtStack.pop_back_val(); 963 } 964 965 /// \brief Reads a sub-expression operand during statement reading. 966 Expr *ReadSubExpr(); 967 968 /// \brief Reads the macro record located at the given offset. 969 void ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset); 970 971 /// \brief Read the set of macros defined by this external macro source. 972 virtual void ReadDefinedMacros(); 973 974 /// \brief Retrieve the macro definition with the given ID. 975 MacroDefinition *getMacroDefinition(serialization::IdentID ID); 976 977 /// \brief Retrieve the AST context that this AST reader supplements. 978 ASTContext *getContext() { return Context; } 979 980 // \brief Contains declarations that were loaded before we have 981 // access to a Sema object. 982 llvm::SmallVector<NamedDecl *, 16> PreloadedDecls; 983 984 /// \brief Retrieve the semantic analysis object used to analyze the 985 /// translation unit in which the precompiled header is being 986 /// imported. 987 Sema *getSema() { return SemaObj; } 988 989 /// \brief Retrieve the identifier table associated with the 990 /// preprocessor. 991 IdentifierTable &getIdentifierTable(); 992 993 /// \brief Record that the given ID maps to the given switch-case 994 /// statement. 995 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID); 996 997 /// \brief Retrieve the switch-case statement with the given ID. 998 SwitchCase *getSwitchCaseWithID(unsigned ID); 999 1000 /// \brief Record that the given label statement has been 1001 /// deserialized and has the given ID. 1002 void RecordLabelStmt(LabelStmt *S, unsigned ID); 1003 1004 /// \brief Set the label of the given statement to the label 1005 /// identified by ID. 1006 /// 1007 /// Depending on the order in which the label and other statements 1008 /// referencing that label occur, this operation may complete 1009 /// immediately (updating the statement) or it may queue the 1010 /// statement to be back-patched later. 1011 void SetLabelOf(GotoStmt *S, unsigned ID); 1012 1013 /// \brief Set the label of the given expression to the label 1014 /// identified by ID. 1015 /// 1016 /// Depending on the order in which the label and other statements 1017 /// referencing that label occur, this operation may complete 1018 /// immediately (updating the statement) or it may queue the 1019 /// statement to be back-patched later. 1020 void SetLabelOf(AddrLabelExpr *S, unsigned ID); 1021}; 1022 1023/// \brief Helper class that saves the current stream position and 1024/// then restores it when destroyed. 1025struct SavedStreamPosition { 1026 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor) 1027 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { } 1028 1029 ~SavedStreamPosition() { 1030 Cursor.JumpToBit(Offset); 1031 } 1032 1033private: 1034 llvm::BitstreamCursor &Cursor; 1035 uint64_t Offset; 1036}; 1037 1038inline void PCHValidator::Error(const char *Msg) { 1039 Reader.Error(Msg); 1040} 1041 1042} // end namespace clang 1043 1044#endif 1045