ASTReader.h revision 353358
1327952Sdim//===- ASTReader.h - AST File Reader ----------------------------*- C++ -*-===// 2212795Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6212795Sdim// 7212795Sdim//===----------------------------------------------------------------------===// 8212795Sdim// 9212795Sdim// This file defines the ASTReader class, which reads AST files. 10212795Sdim// 11212795Sdim//===----------------------------------------------------------------------===// 12212795Sdim 13280031Sdim#ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H 14280031Sdim#define LLVM_CLANG_SERIALIZATION_ASTREADER_H 15212795Sdim 16341825Sdim#include "clang/AST/DeclCXX.h" 17249423Sdim#include "clang/AST/DeclObjC.h" 18212795Sdim#include "clang/AST/DeclarationName.h" 19341825Sdim#include "clang/AST/NestedNameSpecifier.h" 20344779Sdim#include "clang/AST/OpenMPClause.h" 21212795Sdim#include "clang/AST/TemplateBase.h" 22327952Sdim#include "clang/AST/TemplateName.h" 23327952Sdim#include "clang/AST/Type.h" 24212795Sdim#include "clang/Basic/Diagnostic.h" 25327952Sdim#include "clang/Basic/DiagnosticOptions.h" 26212795Sdim#include "clang/Basic/IdentifierTable.h" 27327952Sdim#include "clang/Basic/Module.h" 28327952Sdim#include "clang/Basic/OpenCLOptions.h" 29327952Sdim#include "clang/Basic/SourceLocation.h" 30249423Sdim#include "clang/Basic/Version.h" 31249423Sdim#include "clang/Lex/ExternalPreprocessorSource.h" 32249423Sdim#include "clang/Lex/HeaderSearch.h" 33249423Sdim#include "clang/Lex/PreprocessingRecord.h" 34327952Sdim#include "clang/Lex/Token.h" 35249423Sdim#include "clang/Sema/ExternalSemaSource.h" 36309124Sdim#include "clang/Sema/IdentifierResolver.h" 37249423Sdim#include "clang/Serialization/ASTBitCodes.h" 38249423Sdim#include "clang/Serialization/ContinuousRangeMap.h" 39249423Sdim#include "clang/Serialization/Module.h" 40296417Sdim#include "clang/Serialization/ModuleFileExtension.h" 41249423Sdim#include "clang/Serialization/ModuleManager.h" 42327952Sdim#include "llvm/ADT/APFloat.h" 43327952Sdim#include "llvm/ADT/APInt.h" 44327952Sdim#include "llvm/ADT/APSInt.h" 45327952Sdim#include "llvm/ADT/ArrayRef.h" 46327952Sdim#include "llvm/ADT/DenseMap.h" 47327952Sdim#include "llvm/ADT/DenseSet.h" 48327952Sdim#include "llvm/ADT/IntrusiveRefCntPtr.h" 49243830Sdim#include "llvm/ADT/MapVector.h" 50327952Sdim#include "llvm/ADT/Optional.h" 51327952Sdim#include "llvm/ADT/STLExtras.h" 52327952Sdim#include "llvm/ADT/SetVector.h" 53234353Sdim#include "llvm/ADT/SmallPtrSet.h" 54212795Sdim#include "llvm/ADT/SmallVector.h" 55296417Sdim#include "llvm/ADT/StringMap.h" 56212795Sdim#include "llvm/ADT/StringRef.h" 57327952Sdim#include "llvm/ADT/iterator.h" 58327952Sdim#include "llvm/ADT/iterator_range.h" 59353358Sdim#include "llvm/Bitstream/BitstreamReader.h" 60327952Sdim#include "llvm/Support/Casting.h" 61327952Sdim#include "llvm/Support/Endian.h" 62327952Sdim#include "llvm/Support/MemoryBuffer.h" 63288943Sdim#include "llvm/Support/Timer.h" 64341825Sdim#include "llvm/Support/VersionTuple.h" 65327952Sdim#include <cassert> 66327952Sdim#include <cstddef> 67327952Sdim#include <cstdint> 68327952Sdim#include <ctime> 69212795Sdim#include <deque> 70276479Sdim#include <memory> 71327952Sdim#include <set> 72212795Sdim#include <string> 73212795Sdim#include <utility> 74212795Sdim#include <vector> 75212795Sdim 76212795Sdimnamespace clang { 77212795Sdim 78212795Sdimclass ASTConsumer; 79212795Sdimclass ASTContext; 80327952Sdimclass ASTDeserializationListener; 81327952Sdimclass ASTReader; 82327952Sdimclass ASTRecordReader; 83327952Sdimclass CXXTemporary; 84327952Sdimclass Decl; 85327952Sdimclass DeclaratorDecl; 86327952Sdimclass DeclContext; 87327952Sdimclass EnumDecl; 88327952Sdimclass Expr; 89327952Sdimclass FieldDecl; 90327952Sdimclass FileEntry; 91327952Sdimclass FileManager; 92327952Sdimclass FileSystemOptions; 93327952Sdimclass FunctionDecl; 94249423Sdimclass GlobalModuleIndex; 95327952Sdimstruct HeaderFileInfo; 96327952Sdimclass HeaderSearchOptions; 97327952Sdimclass LangOptions; 98327952Sdimclass LazyASTUnresolvedSet; 99327952Sdimclass MacroInfo; 100353358Sdimclass InMemoryModuleCache; 101212795Sdimclass NamedDecl; 102327952Sdimclass NamespaceDecl; 103327952Sdimclass ObjCCategoryDecl; 104327952Sdimclass ObjCInterfaceDecl; 105327952Sdimclass PCHContainerReader; 106212795Sdimclass Preprocessor; 107243830Sdimclass PreprocessorOptions; 108327952Sdimstruct QualifierInfo; 109212795Sdimclass Sema; 110327952Sdimclass SourceManager; 111327952Sdimclass Stmt; 112212795Sdimclass SwitchCase; 113243830Sdimclass TargetOptions; 114327952Sdimclass TemplateParameterList; 115327952Sdimclass TypedefNameDecl; 116327952Sdimclass TypeSourceInfo; 117327952Sdimclass ValueDecl; 118327952Sdimclass VarDecl; 119212795Sdim 120341825Sdim/// Abstract interface for callback invocations by the ASTReader. 121212795Sdim/// 122212795Sdim/// While reading an AST file, the ASTReader will call the methods of the 123212795Sdim/// listener to pass on specific information. Some of the listener methods can 124212795Sdim/// return true to indicate to the ASTReader that the information (and 125212795Sdim/// consequently the AST file) is invalid. 126212795Sdimclass ASTReaderListener { 127212795Sdimpublic: 128212795Sdim virtual ~ASTReaderListener(); 129212795Sdim 130341825Sdim /// Receives the full Clang version information. 131249423Sdim /// 132249423Sdim /// \returns true to indicate that the version is invalid. Subclasses should 133249423Sdim /// generally defer to this implementation. 134249423Sdim virtual bool ReadFullVersionInformation(StringRef FullVersion) { 135249423Sdim return FullVersion != getClangFullRepositoryVersion(); 136249423Sdim } 137249423Sdim 138276479Sdim virtual void ReadModuleName(StringRef ModuleName) {} 139276479Sdim virtual void ReadModuleMapFile(StringRef ModuleMapPath) {} 140276479Sdim 141341825Sdim /// Receives the language options. 142212795Sdim /// 143212795Sdim /// \returns true to indicate the options are invalid or false otherwise. 144243830Sdim virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 145280031Sdim bool Complain, 146280031Sdim bool AllowCompatibleDifferences) { 147212795Sdim return false; 148212795Sdim } 149212795Sdim 150341825Sdim /// Receives the target options. 151212795Sdim /// 152243830Sdim /// \returns true to indicate the target options are invalid, or false 153243830Sdim /// otherwise. 154288943Sdim virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 155288943Sdim bool AllowCompatibleDifferences) { 156212795Sdim return false; 157212795Sdim } 158212795Sdim 159341825Sdim /// Receives the diagnostic options. 160212795Sdim /// 161243830Sdim /// \returns true to indicate the diagnostic options are invalid, or false 162243830Sdim /// otherwise. 163276479Sdim virtual bool 164276479Sdim ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 165276479Sdim bool Complain) { 166243830Sdim return false; 167243830Sdim } 168243830Sdim 169341825Sdim /// Receives the file system options. 170212795Sdim /// 171243830Sdim /// \returns true to indicate the file system options are invalid, or false 172243830Sdim /// otherwise. 173243830Sdim virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts, 174243830Sdim bool Complain) { 175243830Sdim return false; 176243830Sdim } 177243830Sdim 178341825Sdim /// Receives the header search options. 179212795Sdim /// 180243830Sdim /// \returns true to indicate the header search options are invalid, or false 181243830Sdim /// otherwise. 182243830Sdim virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 183288943Sdim StringRef SpecificModuleCachePath, 184243830Sdim bool Complain) { 185243830Sdim return false; 186243830Sdim } 187243830Sdim 188341825Sdim /// Receives the preprocessor options. 189212795Sdim /// 190243830Sdim /// \param SuggestedPredefines Can be filled in with the set of predefines 191243830Sdim /// that are suggested by the preprocessor options. Typically only used when 192243830Sdim /// loading a precompiled header. 193243830Sdim /// 194243830Sdim /// \returns true to indicate the preprocessor options are invalid, or false 195243830Sdim /// otherwise. 196243830Sdim virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 197243830Sdim bool Complain, 198243830Sdim std::string &SuggestedPredefines) { 199212795Sdim return false; 200212795Sdim } 201212795Sdim 202341825Sdim /// Receives __COUNTER__ value. 203243830Sdim virtual void ReadCounter(const serialization::ModuleFile &M, 204243830Sdim unsigned Value) {} 205251662Sdim 206276479Sdim /// This is called for each AST file loaded. 207296417Sdim virtual void visitModuleFile(StringRef Filename, 208296417Sdim serialization::ModuleKind Kind) {} 209276479Sdim 210341825Sdim /// Returns true if this \c ASTReaderListener wants to receive the 211251662Sdim /// input files of the AST file via \c visitInputFile, false otherwise. 212251662Sdim virtual bool needsInputFileVisitation() { return false; } 213327952Sdim 214341825Sdim /// Returns true if this \c ASTReaderListener wants to receive the 215276479Sdim /// system input files of the AST file via \c visitInputFile, false otherwise. 216276479Sdim virtual bool needsSystemInputFileVisitation() { return false; } 217327952Sdim 218341825Sdim /// if \c needsInputFileVisitation returns true, this is called for 219276479Sdim /// each non-system input file of the AST File. If 220276479Sdim /// \c needsSystemInputFileVisitation is true, then it is called for all 221276479Sdim /// system input files as well. 222251662Sdim /// 223251662Sdim /// \returns true to continue receiving the next input file, false to stop. 224276479Sdim virtual bool visitInputFile(StringRef Filename, bool isSystem, 225296417Sdim bool isOverridden, bool isExplicitModule) { 226276479Sdim return true; 227276479Sdim } 228280031Sdim 229341825Sdim /// Returns true if this \c ASTReaderListener wants to receive the 230280031Sdim /// imports of the AST file via \c visitImport, false otherwise. 231280031Sdim virtual bool needsImportVisitation() const { return false; } 232327952Sdim 233341825Sdim /// If needsImportVisitation returns \c true, this is called for each 234280031Sdim /// AST file imported by this AST file. 235344779Sdim virtual void visitImport(StringRef ModuleName, StringRef Filename) {} 236296417Sdim 237296417Sdim /// Indicates that a particular module file extension has been read. 238296417Sdim virtual void readModuleFileExtension( 239296417Sdim const ModuleFileExtensionMetadata &Metadata) {} 240212795Sdim}; 241212795Sdim 242341825Sdim/// Simple wrapper class for chaining listeners. 243276479Sdimclass ChainedASTReaderListener : public ASTReaderListener { 244276479Sdim std::unique_ptr<ASTReaderListener> First; 245276479Sdim std::unique_ptr<ASTReaderListener> Second; 246276479Sdim 247276479Sdimpublic: 248276479Sdim /// Takes ownership of \p First and \p Second. 249280031Sdim ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First, 250280031Sdim std::unique_ptr<ASTReaderListener> Second) 251280031Sdim : First(std::move(First)), Second(std::move(Second)) {} 252276479Sdim 253280031Sdim std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); } 254280031Sdim std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); } 255280031Sdim 256276479Sdim bool ReadFullVersionInformation(StringRef FullVersion) override; 257276479Sdim void ReadModuleName(StringRef ModuleName) override; 258276479Sdim void ReadModuleMapFile(StringRef ModuleMapPath) override; 259280031Sdim bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 260280031Sdim bool AllowCompatibleDifferences) override; 261288943Sdim bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 262288943Sdim bool AllowCompatibleDifferences) override; 263276479Sdim bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 264276479Sdim bool Complain) override; 265276479Sdim bool ReadFileSystemOptions(const FileSystemOptions &FSOpts, 266276479Sdim bool Complain) override; 267276479Sdim 268276479Sdim bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 269288943Sdim StringRef SpecificModuleCachePath, 270276479Sdim bool Complain) override; 271276479Sdim bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 272276479Sdim bool Complain, 273276479Sdim std::string &SuggestedPredefines) override; 274276479Sdim 275276479Sdim void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override; 276276479Sdim bool needsInputFileVisitation() override; 277276479Sdim bool needsSystemInputFileVisitation() override; 278296417Sdim void visitModuleFile(StringRef Filename, 279296417Sdim serialization::ModuleKind Kind) override; 280276479Sdim bool visitInputFile(StringRef Filename, bool isSystem, 281296417Sdim bool isOverridden, bool isExplicitModule) override; 282296417Sdim void readModuleFileExtension( 283296417Sdim const ModuleFileExtensionMetadata &Metadata) override; 284276479Sdim}; 285276479Sdim 286341825Sdim/// ASTReaderListener implementation to validate the information of 287212795Sdim/// the PCH file against an initialized Preprocessor. 288212795Sdimclass PCHValidator : public ASTReaderListener { 289212795Sdim Preprocessor &PP; 290212795Sdim ASTReader &Reader; 291212795Sdim 292212795Sdimpublic: 293212795Sdim PCHValidator(Preprocessor &PP, ASTReader &Reader) 294341825Sdim : PP(PP), Reader(Reader) {} 295212795Sdim 296280031Sdim bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 297280031Sdim bool AllowCompatibleDifferences) override; 298288943Sdim bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 299288943Sdim bool AllowCompatibleDifferences) override; 300276479Sdim bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 301276479Sdim bool Complain) override; 302276479Sdim bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain, 303276479Sdim std::string &SuggestedPredefines) override; 304288943Sdim bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 305288943Sdim StringRef SpecificModuleCachePath, 306288943Sdim bool Complain) override; 307276479Sdim void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override; 308212795Sdim 309212795Sdimprivate: 310212795Sdim void Error(const char *Msg); 311212795Sdim}; 312212795Sdim 313341825Sdim/// ASTReaderListenter implementation to set SuggestedPredefines of 314314564Sdim/// ASTReader which is required to use a pch file. This is the replacement 315314564Sdim/// of PCHValidator or SimplePCHValidator when using a pch file without 316314564Sdim/// validating it. 317314564Sdimclass SimpleASTReaderListener : public ASTReaderListener { 318314564Sdim Preprocessor &PP; 319314564Sdim 320314564Sdimpublic: 321341825Sdim SimpleASTReaderListener(Preprocessor &PP) : PP(PP) {} 322314564Sdim 323314564Sdim bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain, 324314564Sdim std::string &SuggestedPredefines) override; 325314564Sdim}; 326314564Sdim 327234353Sdimnamespace serialization { 328226633Sdim 329226633Sdimclass ReadMethodPoolVisitor; 330234353Sdim 331226633Sdimnamespace reader { 332234353Sdim 333327952Sdimclass ASTIdentifierLookupTrait; 334234353Sdim 335341825Sdim/// The on-disk hash table(s) used for DeclContext name lookup. 336327952Sdimstruct DeclContextLookupTable; 337327952Sdim 338327952Sdim} // namespace reader 339327952Sdim 340327952Sdim} // namespace serialization 341327952Sdim 342341825Sdim/// Reads an AST files chain containing the contents of a translation 343212795Sdim/// unit. 344212795Sdim/// 345212795Sdim/// The ASTReader class reads bitstreams (produced by the ASTWriter 346212795Sdim/// class) containing the serialized representation of a given 347212795Sdim/// abstract syntax tree and its supporting data structures. An 348212795Sdim/// instance of the ASTReader can be attached to an ASTContext object, 349212795Sdim/// which will provide access to the contents of the AST files. 350212795Sdim/// 351212795Sdim/// The AST reader provides lazy de-serialization of declarations, as 352212795Sdim/// required when traversing the AST. Only those AST nodes that are 353212795Sdim/// actually required will be de-serialized. 354212795Sdimclass ASTReader 355212795Sdim : public ExternalPreprocessorSource, 356212795Sdim public ExternalPreprocessingRecordSource, 357218893Sdim public ExternalHeaderFileInfoSource, 358212795Sdim public ExternalSemaSource, 359212795Sdim public IdentifierInfoLookup, 360234353Sdim public ExternalSLocEntrySource 361218893Sdim{ 362212795Sdimpublic: 363341825Sdim /// Types of AST files. 364327952Sdim friend class ASTDeclReader; 365327952Sdim friend class ASTIdentifierIterator; 366327952Sdim friend class ASTRecordReader; 367327952Sdim friend class ASTStmtReader; 368327952Sdim friend class ASTUnit; // ASTUnit needs to remap source locations. 369327952Sdim friend class ASTWriter; 370327952Sdim friend class PCHValidator; 371327952Sdim friend class serialization::reader::ASTIdentifierLookupTrait; 372327952Sdim friend class serialization::ReadMethodPoolVisitor; 373327952Sdim friend class TypeLocReader; 374239462Sdim 375327952Sdim using RecordData = SmallVector<uint64_t, 64>; 376327952Sdim using RecordDataImpl = SmallVectorImpl<uint64_t>; 377327952Sdim 378341825Sdim /// The result of reading the control block of an AST file, which 379243830Sdim /// can fail for various reasons. 380243830Sdim enum ASTReadResult { 381341825Sdim /// The control block was read successfully. Aside from failures, 382243830Sdim /// the AST file is safe to read into the current context. 383243830Sdim Success, 384327952Sdim 385341825Sdim /// The AST file itself appears corrupted. 386243830Sdim Failure, 387327952Sdim 388341825Sdim /// The AST file was missing. 389249423Sdim Missing, 390327952Sdim 391341825Sdim /// The AST file is out-of-date relative to its input files, 392243830Sdim /// and needs to be regenerated. 393243830Sdim OutOfDate, 394327952Sdim 395341825Sdim /// The AST file was written by a different version of Clang. 396243830Sdim VersionMismatch, 397327952Sdim 398341825Sdim /// The AST file was writtten with a different language/target 399243830Sdim /// configuration. 400243830Sdim ConfigurationMismatch, 401327952Sdim 402341825Sdim /// The AST file has errors. 403243830Sdim HadErrors 404243830Sdim }; 405314564Sdim 406327952Sdim using ModuleFile = serialization::ModuleFile; 407327952Sdim using ModuleKind = serialization::ModuleKind; 408327952Sdim using ModuleManager = serialization::ModuleManager; 409327952Sdim using ModuleIterator = ModuleManager::ModuleIterator; 410327952Sdim using ModuleConstIterator = ModuleManager::ModuleConstIterator; 411327952Sdim using ModuleReverseIterator = ModuleManager::ModuleReverseIterator; 412234353Sdim 413212795Sdimprivate: 414341825Sdim /// The receiver of some callbacks invoked by ASTReader. 415276479Sdim std::unique_ptr<ASTReaderListener> Listener; 416212795Sdim 417341825Sdim /// The receiver of deserialization events. 418314564Sdim ASTDeserializationListener *DeserializationListener = nullptr; 419327952Sdim 420314564Sdim bool OwnsDeserializationListener = false; 421212795Sdim 422212795Sdim SourceManager &SourceMgr; 423212795Sdim FileManager &FileMgr; 424288943Sdim const PCHContainerReader &PCHContainerRdr; 425226633Sdim DiagnosticsEngine &Diags; 426234353Sdim 427341825Sdim /// The semantic analysis object that will be processing the 428212795Sdim /// AST files and the translation unit that uses it. 429314564Sdim Sema *SemaObj = nullptr; 430212795Sdim 431341825Sdim /// The preprocessor that will be loading the source file. 432226633Sdim Preprocessor &PP; 433212795Sdim 434341825Sdim /// The AST context into which we'll read the AST files. 435321369Sdim ASTContext *ContextObj = nullptr; 436234353Sdim 437341825Sdim /// The AST consumer. 438314564Sdim ASTConsumer *Consumer = nullptr; 439212795Sdim 440341825Sdim /// The module manager which manages modules and their dependencies 441226633Sdim ModuleManager ModuleMgr; 442221345Sdim 443341825Sdim /// A dummy identifier resolver used to merge TU-scope declarations in 444309124Sdim /// C, for the cases where we don't have a Sema object to provide a real 445309124Sdim /// identifier resolver. 446309124Sdim IdentifierResolver DummyIdResolver; 447309124Sdim 448296417Sdim /// A mapping from extension block names to module file extensions. 449314564Sdim llvm::StringMap<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions; 450296417Sdim 451341825Sdim /// A timer used to track the time spent deserializing. 452288943Sdim std::unique_ptr<llvm::Timer> ReadTimer; 453288943Sdim 454341825Sdim /// The location where the module file will be considered as 455261991Sdim /// imported from. For non-module AST types it should be invalid. 456261991Sdim SourceLocation CurrentImportLoc; 457261991Sdim 458341825Sdim /// The global module index, if loaded. 459276479Sdim std::unique_ptr<GlobalModuleIndex> GlobalIndex; 460249423Sdim 461341825Sdim /// A map of global bit offsets to the module that stores entities 462226633Sdim /// at those bit offsets. 463234353Sdim ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap; 464212795Sdim 465341825Sdim /// A map of negated SLocEntryIDs to the modules containing them. 466234353Sdim ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap; 467212795Sdim 468327952Sdim using GlobalSLocOffsetMapType = 469327952Sdim ContinuousRangeMap<unsigned, ModuleFile *, 64>; 470234353Sdim 471341825Sdim /// A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset) 472226633Sdim /// SourceLocation offsets to the modules containing them. 473226633Sdim GlobalSLocOffsetMapType GlobalSLocOffsetMap; 474234353Sdim 475341825Sdim /// Types that have already been loaded from the chain. 476212795Sdim /// 477212795Sdim /// When the pointer at index I is non-NULL, the type with 478212795Sdim /// ID = (I + 1) << FastQual::Width has already been loaded 479212795Sdim std::vector<QualType> TypesLoaded; 480212795Sdim 481327952Sdim using GlobalTypeMapType = 482327952Sdim ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>; 483212795Sdim 484341825Sdim /// Mapping from global type IDs to the module in which the 485226633Sdim /// type resides along with the offset that should be added to the 486226633Sdim /// global type ID to produce a local ID. 487226633Sdim GlobalTypeMapType GlobalTypeMap; 488226633Sdim 489341825Sdim /// Declarations that have already been loaded from the chain. 490212795Sdim /// 491212795Sdim /// When the pointer at index I is non-NULL, the declaration with ID 492212795Sdim /// = I + 1 has already been loaded. 493212795Sdim std::vector<Decl *> DeclsLoaded; 494212795Sdim 495327952Sdim using GlobalDeclMapType = 496327952Sdim ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>; 497234353Sdim 498341825Sdim /// Mapping from global declaration IDs to the module in which the 499226633Sdim /// declaration resides. 500226633Sdim GlobalDeclMapType GlobalDeclMap; 501234353Sdim 502327952Sdim using FileOffset = std::pair<ModuleFile *, uint64_t>; 503327952Sdim using FileOffsetsTy = SmallVector<FileOffset, 2>; 504327952Sdim using DeclUpdateOffsetsMap = 505327952Sdim llvm::DenseMap<serialization::DeclID, FileOffsetsTy>; 506234353Sdim 507341825Sdim /// Declarations that have modifications residing in a later file 508218893Sdim /// in the chain. 509218893Sdim DeclUpdateOffsetsMap DeclUpdateOffsets; 510218893Sdim 511321369Sdim struct PendingUpdateRecord { 512321369Sdim Decl *D; 513321369Sdim serialization::GlobalDeclID ID; 514327952Sdim 515321369Sdim // Whether the declaration was just deserialized. 516321369Sdim bool JustLoaded; 517327952Sdim 518321369Sdim PendingUpdateRecord(serialization::GlobalDeclID ID, Decl *D, 519321369Sdim bool JustLoaded) 520321369Sdim : D(D), ID(ID), JustLoaded(JustLoaded) {} 521321369Sdim }; 522327952Sdim 523341825Sdim /// Declaration updates for already-loaded declarations that we need 524276479Sdim /// to apply once we finish processing an import. 525321369Sdim llvm::SmallVector<PendingUpdateRecord, 16> PendingUpdateRecords; 526276479Sdim 527288943Sdim enum class PendingFakeDefinitionKind { NotFake, Fake, FakeLoaded }; 528288943Sdim 529341825Sdim /// The DefinitionData pointers that we faked up for class definitions 530288943Sdim /// that we needed but hadn't loaded yet. 531288943Sdim llvm::DenseMap<void *, PendingFakeDefinitionKind> PendingFakeDefinitionData; 532288943Sdim 533341825Sdim /// Exception specification updates that have been loaded but not yet 534288943Sdim /// propagated across the relevant redeclaration chain. The map key is the 535288943Sdim /// canonical declaration (used only for deduplication) and the value is a 536288943Sdim /// declaration that has an exception specification. 537288943Sdim llvm::SmallMapVector<Decl *, FunctionDecl *, 4> PendingExceptionSpecUpdates; 538288943Sdim 539344779Sdim /// Deduced return type updates that have been loaded but not yet propagated 540344779Sdim /// across the relevant redeclaration chain. The map key is the canonical 541344779Sdim /// declaration and the value is the deduced return type. 542344779Sdim llvm::SmallMapVector<FunctionDecl *, QualType, 4> PendingDeducedTypeUpdates; 543344779Sdim 544341825Sdim /// Declarations that have been imported and have typedef names for 545280031Sdim /// linkage purposes. 546327952Sdim llvm::DenseMap<std::pair<DeclContext *, IdentifierInfo *>, NamedDecl *> 547280031Sdim ImportedTypedefNamesForLinkage; 548280031Sdim 549341825Sdim /// Mergeable declaration contexts that have anonymous declarations 550280031Sdim /// within them, and those anonymous declarations. 551341825Sdim llvm::DenseMap<Decl*, llvm::SmallVector<NamedDecl*, 2>> 552280031Sdim AnonymousDeclarationsForMerging; 553280031Sdim 554234353Sdim struct FileDeclsInfo { 555327952Sdim ModuleFile *Mod = nullptr; 556234353Sdim ArrayRef<serialization::LocalDeclID> Decls; 557234353Sdim 558327952Sdim FileDeclsInfo() = default; 559234353Sdim FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls) 560341825Sdim : Mod(Mod), Decls(Decls) {} 561234353Sdim }; 562234353Sdim 563341825Sdim /// Map from a FileID to the file-level declarations that it contains. 564234353Sdim llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs; 565234353Sdim 566341825Sdim /// An array of lexical contents of a declaration context, as a sequence of 567296417Sdim /// Decl::Kind, DeclID pairs. 568327952Sdim using LexicalContents = ArrayRef<llvm::support::unaligned_uint32_t>; 569296417Sdim 570341825Sdim /// Map from a DeclContext to its lexical contents. 571296417Sdim llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>> 572296417Sdim LexicalDecls; 573296417Sdim 574341825Sdim /// Map from the TU to its lexical contents from each module file. 575296417Sdim std::vector<std::pair<ModuleFile*, LexicalContents>> TULexicalDecls; 576296417Sdim 577341825Sdim /// Map from a DeclContext to its lookup tables. 578296417Sdim llvm::DenseMap<const DeclContext *, 579296417Sdim serialization::reader::DeclContextLookupTable> Lookups; 580296417Sdim 581212795Sdim // Updates for visible decls can occur for other contexts than just the 582296417Sdim // TU, and when we read those update records, the actual context may not 583296417Sdim // be available yet, so have this pending map using the ID as a key. It 584296417Sdim // will be realized when the context is actually loaded. 585296417Sdim struct PendingVisibleUpdate { 586296417Sdim ModuleFile *Mod; 587296417Sdim const unsigned char *Data; 588296417Sdim }; 589327952Sdim using DeclContextVisibleUpdates = SmallVector<PendingVisibleUpdate, 1>; 590212795Sdim 591341825Sdim /// Updates to the visible declarations of declaration contexts that 592212795Sdim /// haven't been loaded yet. 593296417Sdim llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates> 594296417Sdim PendingVisibleUpdates; 595296417Sdim 596341825Sdim /// The set of C++ or Objective-C classes that have forward 597234353Sdim /// declarations that have not yet been linked to their definitions. 598234353Sdim llvm::SmallPtrSet<Decl *, 4> PendingDefinitions; 599243830Sdim 600327952Sdim using PendingBodiesMap = 601327952Sdim llvm::MapVector<Decl *, uint64_t, 602327952Sdim llvm::SmallDenseMap<Decl *, unsigned, 4>, 603327952Sdim SmallVector<std::pair<Decl *, uint64_t>, 4>>; 604243830Sdim 605341825Sdim /// Functions or methods that have bodies that will be attached. 606243830Sdim PendingBodiesMap PendingBodies; 607243830Sdim 608341825Sdim /// Definitions for which we have added merged definitions but not yet 609288943Sdim /// performed deduplication. 610327952Sdim llvm::SetVector<NamedDecl *> PendingMergedDefinitionsToDeduplicate; 611288943Sdim 612341825Sdim /// Read the record that describes the lexical contents of a DC. 613296417Sdim bool ReadLexicalDeclContextStorage(ModuleFile &M, 614296417Sdim llvm::BitstreamCursor &Cursor, 615296417Sdim uint64_t Offset, DeclContext *DC); 616327952Sdim 617341825Sdim /// Read the record that describes the visible contents of a DC. 618296417Sdim bool ReadVisibleDeclContextStorage(ModuleFile &M, 619296417Sdim llvm::BitstreamCursor &Cursor, 620296417Sdim uint64_t Offset, serialization::DeclID ID); 621212795Sdim 622341825Sdim /// A vector containing identifiers that have already been 623212795Sdim /// loaded. 624212795Sdim /// 625212795Sdim /// If the pointer at index I is non-NULL, then it refers to the 626212795Sdim /// IdentifierInfo for the identifier with ID=I+1 that has already 627212795Sdim /// been loaded. 628212795Sdim std::vector<IdentifierInfo *> IdentifiersLoaded; 629212795Sdim 630327952Sdim using GlobalIdentifierMapType = 631327952Sdim ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>; 632234353Sdim 633341825Sdim /// Mapping from global identifier IDs to the module in which the 634226633Sdim /// identifier resides along with the offset that should be added to the 635226633Sdim /// global identifier ID to produce a local ID. 636226633Sdim GlobalIdentifierMapType GlobalIdentifierMap; 637226633Sdim 638341825Sdim /// A vector containing macros that have already been 639243830Sdim /// loaded. 640243830Sdim /// 641243830Sdim /// If the pointer at index I is non-NULL, then it refers to the 642243830Sdim /// MacroInfo for the identifier with ID=I+1 that has already 643243830Sdim /// been loaded. 644243830Sdim std::vector<MacroInfo *> MacrosLoaded; 645243830Sdim 646327952Sdim using LoadedMacroInfo = 647327952Sdim std::pair<IdentifierInfo *, serialization::SubmoduleID>; 648288943Sdim 649341825Sdim /// A set of #undef directives that we have loaded; used to 650288943Sdim /// deduplicate the same #undef information coming from multiple module 651288943Sdim /// files. 652288943Sdim llvm::DenseSet<LoadedMacroInfo> LoadedUndefs; 653288943Sdim 654327952Sdim using GlobalMacroMapType = 655327952Sdim ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>; 656243830Sdim 657341825Sdim /// Mapping from global macro IDs to the module in which the 658243830Sdim /// macro resides along with the offset that should be added to the 659243830Sdim /// global macro ID to produce a local ID. 660243830Sdim GlobalMacroMapType GlobalMacroMap; 661243830Sdim 662341825Sdim /// A vector containing submodules that have already been loaded. 663234353Sdim /// 664234353Sdim /// This vector is indexed by the Submodule ID (-1). NULL submodule entries 665234353Sdim /// indicate that the particular submodule ID has not yet been loaded. 666234353Sdim SmallVector<Module *, 2> SubmodulesLoaded; 667341825Sdim 668327952Sdim using GlobalSubmoduleMapType = 669327952Sdim ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>; 670341825Sdim 671341825Sdim /// Mapping from global submodule IDs to the module file in which the 672234353Sdim /// submodule resides along with the offset that should be added to the 673234353Sdim /// global submodule ID to produce a local ID. 674234353Sdim GlobalSubmoduleMapType GlobalSubmoduleMap; 675234353Sdim 676341825Sdim /// A set of hidden declarations. 677327952Sdim using HiddenNames = SmallVector<Decl *, 2>; 678327952Sdim using HiddenNamesMapType = llvm::DenseMap<Module *, HiddenNames>; 679234353Sdim 680341825Sdim /// A mapping from each of the hidden submodules to the deserialized 681234353Sdim /// declarations in that submodule that could be made visible. 682234353Sdim HiddenNamesMapType HiddenNamesMap; 683341825Sdim 684341825Sdim /// A module import, export, or conflict that hasn't yet been resolved. 685249423Sdim struct UnresolvedModuleRef { 686341825Sdim /// The file in which this module resides. 687234353Sdim ModuleFile *File; 688341825Sdim 689341825Sdim /// The module that is importing or exporting. 690234353Sdim Module *Mod; 691249423Sdim 692341825Sdim /// The kind of module reference. 693249423Sdim enum { Import, Export, Conflict } Kind; 694249423Sdim 695341825Sdim /// The local ID of the module that is being exported. 696234353Sdim unsigned ID; 697249423Sdim 698341825Sdim /// Whether this is a wildcard export. 699234353Sdim unsigned IsWildcard : 1; 700249423Sdim 701341825Sdim /// String data. 702249423Sdim StringRef String; 703234353Sdim }; 704341825Sdim 705341825Sdim /// The set of module imports and exports that still need to be 706234353Sdim /// resolved. 707249423Sdim SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs; 708341825Sdim 709341825Sdim /// A vector containing selectors that have already been loaded. 710212795Sdim /// 711212795Sdim /// This vector is indexed by the Selector ID (-1). NULL selector 712212795Sdim /// entries indicate that the particular selector ID has not yet 713212795Sdim /// been loaded. 714226633Sdim SmallVector<Selector, 16> SelectorsLoaded; 715212795Sdim 716327952Sdim using GlobalSelectorMapType = 717327952Sdim ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>; 718234353Sdim 719341825Sdim /// Mapping from global selector IDs to the module in which the 720226633Sdim /// global selector ID to produce a local ID. 721226633Sdim GlobalSelectorMapType GlobalSelectorMap; 722212795Sdim 723341825Sdim /// The generation number of the last time we loaded data from the 724234353Sdim /// global method pool for this selector. 725234353Sdim llvm::DenseMap<Selector, unsigned> SelectorGeneration; 726234353Sdim 727309124Sdim /// Whether a selector is out of date. We mark a selector as out of date 728309124Sdim /// if we load another module after the method pool entry was pulled in. 729309124Sdim llvm::DenseMap<Selector, bool> SelectorOutOfDate; 730309124Sdim 731249423Sdim struct PendingMacroInfo { 732249423Sdim ModuleFile *M; 733288943Sdim uint64_t MacroDirectivesOffset; 734249423Sdim 735288943Sdim PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset) 736288943Sdim : M(M), MacroDirectivesOffset(MacroDirectivesOffset) {} 737249423Sdim }; 738249423Sdim 739327952Sdim using PendingMacroIDsMap = 740327952Sdim llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2>>; 741226633Sdim 742341825Sdim /// Mapping from identifiers that have a macro history to the global 743243830Sdim /// IDs have not yet been deserialized to the global IDs of those macros. 744243830Sdim PendingMacroIDsMap PendingMacroIDs; 745243830Sdim 746327952Sdim using GlobalPreprocessedEntityMapType = 747327952Sdim ContinuousRangeMap<unsigned, ModuleFile *, 4>; 748234353Sdim 749341825Sdim /// Mapping from global preprocessing entity IDs to the module in 750226633Sdim /// which the preprocessed entity resides along with the offset that should be 751321369Sdim /// added to the global preprocessing entity ID to produce a local ID. 752226633Sdim GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap; 753234353Sdim 754341825Sdim using GlobalSkippedRangeMapType = 755341825Sdim ContinuousRangeMap<unsigned, ModuleFile *, 4>; 756341825Sdim 757341825Sdim /// Mapping from global skipped range base IDs to the module in which 758341825Sdim /// the skipped ranges reside. 759341825Sdim GlobalSkippedRangeMapType GlobalSkippedRangeMap; 760341825Sdim 761212795Sdim /// \name CodeGen-relevant special data 762341825Sdim /// Fields containing data that is relevant to CodeGen. 763212795Sdim //@{ 764212795Sdim 765341825Sdim /// The IDs of all declarations that fulfill the criteria of 766212795Sdim /// "interesting" decls. 767212795Sdim /// 768276479Sdim /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks 769276479Sdim /// in the chain. The referenced declarations are deserialized and passed to 770276479Sdim /// the consumer eagerly. 771276479Sdim SmallVector<uint64_t, 16> EagerlyDeserializedDecls; 772212795Sdim 773341825Sdim /// The IDs of all tentative definitions stored in the chain. 774212795Sdim /// 775212795Sdim /// Sema keeps track of all tentative definitions in a TU because it has to 776212795Sdim /// complete them and pass them on to CodeGen. Thus, tentative definitions in 777212795Sdim /// the PCH chain must be eagerly deserialized. 778226633Sdim SmallVector<uint64_t, 16> TentativeDefinitions; 779212795Sdim 780341825Sdim /// The IDs of all CXXRecordDecls stored in the chain whose VTables are 781212795Sdim /// used. 782212795Sdim /// 783212795Sdim /// CodeGen has to emit VTables for these records, so they have to be eagerly 784212795Sdim /// deserialized. 785226633Sdim SmallVector<uint64_t, 64> VTableUses; 786212795Sdim 787341825Sdim /// A snapshot of the pending instantiations in the chain. 788226633Sdim /// 789226633Sdim /// This record tracks the instantiations that Sema has to perform at the 790226633Sdim /// end of the TU. It consists of a pair of values for every pending 791226633Sdim /// instantiation where the first value is the ID of the decl and the second 792226633Sdim /// is the instantiation location. 793226633Sdim SmallVector<uint64_t, 64> PendingInstantiations; 794226633Sdim 795212795Sdim //@} 796212795Sdim 797226633Sdim /// \name DiagnosticsEngine-relevant special data 798341825Sdim /// Fields containing data that is used for generating diagnostics 799212795Sdim //@{ 800212795Sdim 801341825Sdim /// A snapshot of Sema's unused file-scoped variable tracking, for 802212795Sdim /// generating warnings. 803226633Sdim SmallVector<uint64_t, 16> UnusedFileScopedDecls; 804212795Sdim 805341825Sdim /// A list of all the delegating constructors we've seen, to diagnose 806223017Sdim /// cycles. 807226633Sdim SmallVector<uint64_t, 4> DelegatingCtorDecls; 808234353Sdim 809341825Sdim /// Method selectors used in a @selector expression. Used for 810226633Sdim /// implementation of -Wselector. 811226633Sdim SmallVector<uint64_t, 64> ReferencedSelectorsData; 812223017Sdim 813341825Sdim /// A snapshot of Sema's weak undeclared identifier tracking, for 814212795Sdim /// generating warnings. 815226633Sdim SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers; 816212795Sdim 817341825Sdim /// The IDs of type aliases for ext_vectors that exist in the chain. 818212795Sdim /// 819212795Sdim /// Used by Sema for finding sugared names for ext_vectors in diagnostics. 820226633Sdim SmallVector<uint64_t, 4> ExtVectorDecls; 821212795Sdim 822212795Sdim //@} 823212795Sdim 824212795Sdim /// \name Sema-relevant special data 825341825Sdim /// Fields containing data that is used for semantic analysis 826212795Sdim //@{ 827212795Sdim 828341825Sdim /// The IDs of all potentially unused typedef names in the chain. 829280031Sdim /// 830280031Sdim /// Sema tracks these to emit warnings. 831280031Sdim SmallVector<uint64_t, 16> UnusedLocalTypedefNameCandidates; 832280031Sdim 833341825Sdim /// Our current depth in #pragma cuda force_host_device begin/end 834314564Sdim /// macros. 835314564Sdim unsigned ForceCUDAHostDeviceDepth = 0; 836314564Sdim 837341825Sdim /// The IDs of the declarations Sema stores directly. 838212795Sdim /// 839212795Sdim /// Sema tracks a few important decls, such as namespace std, directly. 840226633Sdim SmallVector<uint64_t, 4> SemaDeclRefs; 841212795Sdim 842341825Sdim /// The IDs of the types ASTContext stores directly. 843212795Sdim /// 844212795Sdim /// The AST context tracks a few important types, such as va_list, directly. 845226633Sdim SmallVector<uint64_t, 16> SpecialTypes; 846212795Sdim 847341825Sdim /// The IDs of CUDA-specific declarations ASTContext stores directly. 848218893Sdim /// 849218893Sdim /// The AST context tracks a few important decls, currently cudaConfigureCall, 850218893Sdim /// directly. 851226633Sdim SmallVector<uint64_t, 2> CUDASpecialDeclRefs; 852218893Sdim 853341825Sdim /// The floating point pragma option settings. 854226633Sdim SmallVector<uint64_t, 1> FPPragmaOptions; 855218893Sdim 856341825Sdim /// The pragma clang optimize location (if the pragma state is "off"). 857276479Sdim SourceLocation OptimizeOffPragmaLocation; 858276479Sdim 859341825Sdim /// The PragmaMSStructKind pragma ms_struct state if set, or -1. 860314564Sdim int PragmaMSStructState = -1; 861309124Sdim 862341825Sdim /// The PragmaMSPointersToMembersKind pragma pointers_to_members state. 863314564Sdim int PragmaMSPointersToMembersState = -1; 864309124Sdim SourceLocation PointersToMembersPragmaLocation; 865309124Sdim 866341825Sdim /// The pragma pack state. 867321369Sdim Optional<unsigned> PragmaPackCurrentValue; 868321369Sdim SourceLocation PragmaPackCurrentLocation; 869321369Sdim struct PragmaPackStackEntry { 870321369Sdim unsigned Value; 871321369Sdim SourceLocation Location; 872327952Sdim SourceLocation PushLocation; 873321369Sdim StringRef SlotLabel; 874321369Sdim }; 875321369Sdim llvm::SmallVector<PragmaPackStackEntry, 2> PragmaPackStack; 876321369Sdim llvm::SmallVector<std::string, 2> PragmaPackStrings; 877321369Sdim 878341825Sdim /// The OpenCL extension settings. 879314564Sdim OpenCLOptions OpenCLExtensions; 880218893Sdim 881341825Sdim /// Extensions required by an OpenCL type. 882314564Sdim llvm::DenseMap<const Type *, std::set<std::string>> OpenCLTypeExtMap; 883314564Sdim 884341825Sdim /// Extensions required by an OpenCL declaration. 885314564Sdim llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap; 886314564Sdim 887341825Sdim /// A list of the namespaces we've seen. 888226633Sdim SmallVector<uint64_t, 4> KnownNamespaces; 889224145Sdim 890341825Sdim /// A list of undefined decls with internal linkage followed by the 891249423Sdim /// SourceLocation of a matching ODR-use. 892249423Sdim SmallVector<uint64_t, 8> UndefinedButUsed; 893249423Sdim 894341825Sdim /// Delete expressions to analyze at the end of translation unit. 895288943Sdim SmallVector<uint64_t, 8> DelayedDeleteExprs; 896288943Sdim 897341825Sdim // A list of late parsed template function data. 898261991Sdim SmallVector<uint64_t, 1> LateParsedTemplates; 899261991Sdim 900314564Sdimpublic: 901276479Sdim struct ImportedSubmodule { 902276479Sdim serialization::SubmoduleID ID; 903276479Sdim SourceLocation ImportLoc; 904276479Sdim 905276479Sdim ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc) 906341825Sdim : ID(ID), ImportLoc(ImportLoc) {} 907276479Sdim }; 908276479Sdim 909314564Sdimprivate: 910341825Sdim /// A list of modules that were imported by precompiled headers or 911234353Sdim /// any other non-module AST file. 912276479Sdim SmallVector<ImportedSubmodule, 2> ImportedModules; 913212795Sdim //@} 914212795Sdim 915341825Sdim /// The system include root to be used when loading the 916212795Sdim /// precompiled header. 917226633Sdim std::string isysroot; 918212795Sdim 919341825Sdim /// Whether to disable the normal validation performed on precompiled 920212795Sdim /// headers when they are loaded. 921212795Sdim bool DisableValidation; 922234353Sdim 923341825Sdim /// Whether to accept an AST file with compiler errors. 924234353Sdim bool AllowASTWithCompilerErrors; 925234353Sdim 926341825Sdim /// Whether to accept an AST file that has a different configuration 927276479Sdim /// from the current compiler instance. 928276479Sdim bool AllowConfigurationMismatch; 929276479Sdim 930341825Sdim /// Whether validate system input files. 931276479Sdim bool ValidateSystemInputs; 932276479Sdim 933341825Sdim /// Whether we are allowed to use the global module index. 934249423Sdim bool UseGlobalIndex; 935249423Sdim 936341825Sdim /// Whether we have tried loading the global module index yet. 937314564Sdim bool TriedLoadingGlobalIndex = false; 938249423Sdim 939341825Sdim ///Whether we are currently processing update records. 940314564Sdim bool ProcessingUpdateRecords = false; 941309124Sdim 942327952Sdim using SwitchCaseMapTy = llvm::DenseMap<unsigned, SwitchCase *>; 943327952Sdim 944341825Sdim /// Mapping from switch-case IDs in the chain to switch-case statements 945212795Sdim /// 946212795Sdim /// Statements usually don't have IDs, but switch cases need them, so that the 947212795Sdim /// switch statement can refer to them. 948239462Sdim SwitchCaseMapTy SwitchCaseStmts; 949212795Sdim 950239462Sdim SwitchCaseMapTy *CurrSwitchCaseStmts; 951239462Sdim 952341825Sdim /// The number of source location entries de-serialized from 953212795Sdim /// the PCH file. 954314564Sdim unsigned NumSLocEntriesRead = 0; 955212795Sdim 956341825Sdim /// The number of source location entries in the chain. 957314564Sdim unsigned TotalNumSLocEntries = 0; 958212795Sdim 959341825Sdim /// The number of statements (and expressions) de-serialized 960212795Sdim /// from the chain. 961314564Sdim unsigned NumStatementsRead = 0; 962212795Sdim 963341825Sdim /// The total number of statements (and expressions) stored 964212795Sdim /// in the chain. 965314564Sdim unsigned TotalNumStatements = 0; 966212795Sdim 967341825Sdim /// The number of macros de-serialized from the chain. 968314564Sdim unsigned NumMacrosRead = 0; 969212795Sdim 970341825Sdim /// The total number of macros stored in the chain. 971314564Sdim unsigned TotalNumMacros = 0; 972212795Sdim 973341825Sdim /// The number of lookups into identifier tables. 974314564Sdim unsigned NumIdentifierLookups = 0; 975249423Sdim 976341825Sdim /// The number of lookups into identifier tables that succeed. 977314564Sdim unsigned NumIdentifierLookupHits = 0; 978249423Sdim 979341825Sdim /// The number of selectors that have been read. 980314564Sdim unsigned NumSelectorsRead = 0; 981212795Sdim 982341825Sdim /// The number of method pool entries that have been read. 983314564Sdim unsigned NumMethodPoolEntriesRead = 0; 984212795Sdim 985341825Sdim /// The number of times we have looked up a selector in the method 986249423Sdim /// pool. 987314564Sdim unsigned NumMethodPoolLookups = 0; 988212795Sdim 989341825Sdim /// The number of times we have looked up a selector in the method 990249423Sdim /// pool and found something. 991314564Sdim unsigned NumMethodPoolHits = 0; 992249423Sdim 993341825Sdim /// The number of times we have looked up a selector in the method 994249423Sdim /// pool within a specific module. 995314564Sdim unsigned NumMethodPoolTableLookups = 0; 996249423Sdim 997341825Sdim /// The number of times we have looked up a selector in the method 998249423Sdim /// pool within a specific module and found something. 999314564Sdim unsigned NumMethodPoolTableHits = 0; 1000249423Sdim 1001341825Sdim /// The total number of method pool entries in the selector table. 1002314564Sdim unsigned TotalNumMethodPoolEntries = 0; 1003212795Sdim 1004212795Sdim /// Number of lexical decl contexts read/total. 1005314564Sdim unsigned NumLexicalDeclContextsRead = 0, TotalLexicalDeclContexts = 0; 1006212795Sdim 1007212795Sdim /// Number of visible decl contexts read/total. 1008314564Sdim unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0; 1009234353Sdim 1010226633Sdim /// Total size of modules, in bits, currently loaded 1011314564Sdim uint64_t TotalModulesSizeInBits = 0; 1012226633Sdim 1013341825Sdim /// Number of Decl/types that are currently deserializing. 1014314564Sdim unsigned NumCurrentElementsDeserializing = 0; 1015212795Sdim 1016341825Sdim /// Set true while we are in the process of passing deserialized 1017234353Sdim /// "interesting" decls to consumer inside FinishedDeserializing(). 1018234353Sdim /// This is used as a guard to avoid recursively repeating the process of 1019234353Sdim /// passing decls to consumer. 1020314564Sdim bool PassingDeclsToConsumer = false; 1021234353Sdim 1022341825Sdim /// The set of identifiers that were read while the AST reader was 1023212795Sdim /// (recursively) loading declarations. 1024212795Sdim /// 1025212795Sdim /// The declarations on the identifier chain for these identifiers will be 1026212795Sdim /// loaded once the recursive loading has completed. 1027327952Sdim llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4>> 1028249423Sdim PendingIdentifierInfos; 1029212795Sdim 1030341825Sdim /// The set of lookup results that we have faked in order to support 1031288943Sdim /// merging of partially deserialized decls but that we have not yet removed. 1032288943Sdim llvm::SmallMapVector<IdentifierInfo *, SmallVector<NamedDecl*, 2>, 16> 1033288943Sdim PendingFakeLookupResults; 1034288943Sdim 1035341825Sdim /// The generation number of each identifier, which keeps track of 1036234353Sdim /// the last time we loaded information about this identifier. 1037234353Sdim llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration; 1038321369Sdim 1039321369Sdim class InterestingDecl { 1040321369Sdim Decl *D; 1041321369Sdim bool DeclHasPendingBody; 1042321369Sdim 1043321369Sdim public: 1044321369Sdim InterestingDecl(Decl *D, bool HasBody) 1045321369Sdim : D(D), DeclHasPendingBody(HasBody) {} 1046327952Sdim 1047321369Sdim Decl *getDecl() { return D; } 1048327952Sdim 1049321369Sdim /// Whether the declaration has a pending body. 1050321369Sdim bool hasPendingBody() { return DeclHasPendingBody; } 1051321369Sdim }; 1052321369Sdim 1053341825Sdim /// Contains declarations and definitions that could be 1054212795Sdim /// "interesting" to the ASTConsumer, when we get that AST consumer. 1055212795Sdim /// 1056212795Sdim /// "Interesting" declarations are those that have data that may 1057212795Sdim /// need to be emitted, such as inline function definitions or 1058212795Sdim /// Objective-C protocols. 1059321369Sdim std::deque<InterestingDecl> PotentiallyInterestingDecls; 1060212795Sdim 1061344779Sdim /// The list of deduced function types that we have not yet read, because 1062344779Sdim /// they might contain a deduced return type that refers to a local type 1063344779Sdim /// declared within the function. 1064344779Sdim SmallVector<std::pair<FunctionDecl *, serialization::TypeID>, 16> 1065344779Sdim PendingFunctionTypes; 1066344779Sdim 1067341825Sdim /// The list of redeclaration chains that still need to be 1068296417Sdim /// reconstructed, and the local offset to the corresponding list 1069296417Sdim /// of redeclarations. 1070296417Sdim SmallVector<std::pair<Decl *, uint64_t>, 16> PendingDeclChains; 1071218893Sdim 1072341825Sdim /// The list of canonical declarations whose redeclaration chains 1073276479Sdim /// need to be marked as incomplete once we're done deserializing things. 1074276479Sdim SmallVector<Decl *, 16> PendingIncompleteDeclChains; 1075276479Sdim 1076341825Sdim /// The Decl IDs for the Sema/Lexical DeclContext of a Decl that has 1077249423Sdim /// been loaded but its DeclContext was not set yet. 1078249423Sdim struct PendingDeclContextInfo { 1079249423Sdim Decl *D; 1080249423Sdim serialization::GlobalDeclID SemaDC; 1081249423Sdim serialization::GlobalDeclID LexicalDC; 1082249423Sdim }; 1083249423Sdim 1084341825Sdim /// The set of Decls that have been loaded but their DeclContexts are 1085249423Sdim /// not set yet. 1086249423Sdim /// 1087249423Sdim /// The DeclContexts for these Decls will be set once recursive loading has 1088249423Sdim /// been completed. 1089249423Sdim std::deque<PendingDeclContextInfo> PendingDeclContextInfos; 1090249423Sdim 1091341825Sdim /// The set of NamedDecls that have been loaded, but are members of a 1092261991Sdim /// context that has been merged into another context where the corresponding 1093261991Sdim /// declaration is either missing or has not yet been loaded. 1094261991Sdim /// 1095261991Sdim /// We will check whether the corresponding declaration is in fact missing 1096261991Sdim /// once recursing loading has been completed. 1097261991Sdim llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks; 1098261991Sdim 1099327952Sdim using DataPointers = 1100327952Sdim std::pair<CXXRecordDecl *, struct CXXRecordDecl::DefinitionData *>; 1101327952Sdim 1102341825Sdim /// Record definitions in which we found an ODR violation. 1103327952Sdim llvm::SmallDenseMap<CXXRecordDecl *, llvm::SmallVector<DataPointers, 2>, 2> 1104276479Sdim PendingOdrMergeFailures; 1105276479Sdim 1106341825Sdim /// Function definitions in which we found an ODR violation. 1107327952Sdim llvm::SmallDenseMap<FunctionDecl *, llvm::SmallVector<FunctionDecl *, 2>, 2> 1108327952Sdim PendingFunctionOdrMergeFailures; 1109327952Sdim 1110341825Sdim /// Enum definitions in which we found an ODR violation. 1111341825Sdim llvm::SmallDenseMap<EnumDecl *, llvm::SmallVector<EnumDecl *, 2>, 2> 1112341825Sdim PendingEnumOdrMergeFailures; 1113341825Sdim 1114341825Sdim /// DeclContexts in which we have diagnosed an ODR violation. 1115276479Sdim llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures; 1116276479Sdim 1117341825Sdim /// The set of Objective-C categories that have been deserialized 1118234353Sdim /// since the last time the declaration chains were linked. 1119234353Sdim llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized; 1120234353Sdim 1121341825Sdim /// The set of Objective-C class definitions that have already been 1122234353Sdim /// loaded, for which we will need to check for categories whenever a new 1123234353Sdim /// module is loaded. 1124249423Sdim SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded; 1125276479Sdim 1126327952Sdim using KeyDeclsMap = 1127327952Sdim llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2>>; 1128341825Sdim 1129341825Sdim /// A mapping from canonical declarations to the set of global 1130288943Sdim /// declaration IDs for key declaration that have been merged with that 1131288943Sdim /// canonical declaration. A key declaration is a formerly-canonical 1132288943Sdim /// declaration whose module did not import any other key declaration for that 1133288943Sdim /// entity. These are the IDs that we use as keys when finding redecl chains. 1134288943Sdim KeyDeclsMap KeyDecls; 1135341825Sdim 1136341825Sdim /// A mapping from DeclContexts to the semantic DeclContext that we 1137261991Sdim /// are treating as the definition of the entity. This is used, for instance, 1138261991Sdim /// when merging implicit instantiations of class templates across modules. 1139261991Sdim llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts; 1140261991Sdim 1141341825Sdim /// A mapping from canonical declarations of enums to their canonical 1142261991Sdim /// definitions. Only populated when using modules in C++. 1143261991Sdim llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions; 1144261991Sdim 1145341825Sdim /// When reading a Stmt tree, Stmt operands are placed in this stack. 1146226633Sdim SmallVector<Stmt *, 16> StmtStack; 1147212795Sdim 1148341825Sdim /// What kind of records we are reading. 1149212795Sdim enum ReadingKind { 1150261991Sdim Read_None, Read_Decl, Read_Type, Read_Stmt 1151212795Sdim }; 1152212795Sdim 1153341825Sdim /// What kind of records we are reading. 1154314564Sdim ReadingKind ReadingKind = Read_None; 1155212795Sdim 1156341825Sdim /// RAII object to change the reading kind. 1157212795Sdim class ReadingKindTracker { 1158212795Sdim ASTReader &Reader; 1159212795Sdim enum ReadingKind PrevKind; 1160212795Sdim 1161212795Sdim public: 1162212795Sdim ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader) 1163341825Sdim : Reader(reader), PrevKind(Reader.ReadingKind) { 1164212795Sdim Reader.ReadingKind = newKind; 1165212795Sdim } 1166212795Sdim 1167327952Sdim ReadingKindTracker(const ReadingKindTracker &) = delete; 1168327952Sdim ReadingKindTracker &operator=(const ReadingKindTracker &) = delete; 1169212795Sdim ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; } 1170212795Sdim }; 1171212795Sdim 1172341825Sdim /// RAII object to mark the start of processing updates. 1173309124Sdim class ProcessingUpdatesRAIIObj { 1174309124Sdim ASTReader &Reader; 1175309124Sdim bool PrevState; 1176309124Sdim 1177309124Sdim public: 1178309124Sdim ProcessingUpdatesRAIIObj(ASTReader &reader) 1179341825Sdim : Reader(reader), PrevState(Reader.ProcessingUpdateRecords) { 1180309124Sdim Reader.ProcessingUpdateRecords = true; 1181309124Sdim } 1182309124Sdim 1183327952Sdim ProcessingUpdatesRAIIObj(const ProcessingUpdatesRAIIObj &) = delete; 1184327952Sdim ProcessingUpdatesRAIIObj & 1185327952Sdim operator=(const ProcessingUpdatesRAIIObj &) = delete; 1186309124Sdim ~ProcessingUpdatesRAIIObj() { Reader.ProcessingUpdateRecords = PrevState; } 1187309124Sdim }; 1188309124Sdim 1189341825Sdim /// Suggested contents of the predefines buffer, after this 1190212795Sdim /// PCH file has been processed. 1191212795Sdim /// 1192212795Sdim /// In most cases, this string will be empty, because the predefines 1193212795Sdim /// buffer computed to build the PCH file will be identical to the 1194212795Sdim /// predefines buffer computed from the command line. However, when 1195212795Sdim /// there are differences that the PCH reader can work around, this 1196212795Sdim /// predefines buffer may contain additional definitions. 1197212795Sdim std::string SuggestedPredefines; 1198212795Sdim 1199327952Sdim llvm::DenseMap<const Decl *, bool> DefinitionSource; 1200321369Sdim 1201341825Sdim /// Reads a statement from the specified cursor. 1202234353Sdim Stmt *ReadStmtFromStream(ModuleFile &F); 1203212795Sdim 1204276479Sdim struct InputFileInfo { 1205276479Sdim std::string Filename; 1206276479Sdim off_t StoredSize; 1207276479Sdim time_t StoredTime; 1208276479Sdim bool Overridden; 1209296417Sdim bool Transient; 1210321369Sdim bool TopLevelModuleMap; 1211276479Sdim }; 1212276479Sdim 1213341825Sdim /// Reads the stored information about an input file. 1214276479Sdim InputFileInfo readInputFileInfo(ModuleFile &F, unsigned ID); 1215276479Sdim 1216341825Sdim /// Retrieve the file entry and 'overridden' bit for an input 1217243830Sdim /// file in the given module file. 1218249423Sdim serialization::InputFile getInputFile(ModuleFile &F, unsigned ID, 1219249423Sdim bool Complain = true); 1220243830Sdim 1221280031Sdimpublic: 1222280031Sdim void ResolveImportedPath(ModuleFile &M, std::string &Filename); 1223280031Sdim static void ResolveImportedPath(std::string &Filename, StringRef Prefix); 1224223017Sdim 1225341825Sdim /// Returns the first key declaration for the given declaration. This 1226288943Sdim /// is one that is formerly-canonical (or still canonical) and whose module 1227288943Sdim /// did not import any other key declaration of the entity. 1228288943Sdim Decl *getKeyDeclaration(Decl *D) { 1229288943Sdim D = D->getCanonicalDecl(); 1230288943Sdim if (D->isFromASTFile()) 1231288943Sdim return D; 1232288943Sdim 1233288943Sdim auto I = KeyDecls.find(D); 1234288943Sdim if (I == KeyDecls.end() || I->second.empty()) 1235288943Sdim return D; 1236288943Sdim return GetExistingDecl(I->second[0]); 1237288943Sdim } 1238288943Sdim const Decl *getKeyDeclaration(const Decl *D) { 1239288943Sdim return getKeyDeclaration(const_cast<Decl*>(D)); 1240288943Sdim } 1241288943Sdim 1242341825Sdim /// Run a callback on each imported key declaration of \p D. 1243288943Sdim template <typename Fn> 1244288943Sdim void forEachImportedKeyDecl(const Decl *D, Fn Visit) { 1245288943Sdim D = D->getCanonicalDecl(); 1246288943Sdim if (D->isFromASTFile()) 1247288943Sdim Visit(D); 1248288943Sdim 1249288943Sdim auto It = KeyDecls.find(const_cast<Decl*>(D)); 1250288943Sdim if (It != KeyDecls.end()) 1251288943Sdim for (auto ID : It->second) 1252288943Sdim Visit(GetExistingDecl(ID)); 1253288943Sdim } 1254288943Sdim 1255341825Sdim /// Get the loaded lookup tables for \p Primary, if any. 1256296417Sdim const serialization::reader::DeclContextLookupTable * 1257296417Sdim getLoadedLookupTables(DeclContext *Primary) const; 1258296417Sdim 1259280031Sdimprivate: 1260249423Sdim struct ImportedModule { 1261249423Sdim ModuleFile *Mod; 1262249423Sdim ModuleFile *ImportedBy; 1263249423Sdim SourceLocation ImportLoc; 1264249423Sdim 1265249423Sdim ImportedModule(ModuleFile *Mod, 1266249423Sdim ModuleFile *ImportedBy, 1267249423Sdim SourceLocation ImportLoc) 1268341825Sdim : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) {} 1269249423Sdim }; 1270249423Sdim 1271226633Sdim ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type, 1272249423Sdim SourceLocation ImportLoc, ModuleFile *ImportedBy, 1273249423Sdim SmallVectorImpl<ImportedModule> &Loaded, 1274249423Sdim off_t ExpectedSize, time_t ExpectedModTime, 1275321369Sdim ASTFileSignature ExpectedSignature, 1276243830Sdim unsigned ClientLoadCapabilities); 1277243830Sdim ASTReadResult ReadControlBlock(ModuleFile &F, 1278249423Sdim SmallVectorImpl<ImportedModule> &Loaded, 1279276479Sdim const ModuleFile *ImportedBy, 1280243830Sdim unsigned ClientLoadCapabilities); 1281296417Sdim static ASTReadResult ReadOptionsBlock( 1282296417Sdim llvm::BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 1283296417Sdim bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 1284321369Sdim std::string &SuggestedPredefines); 1285321369Sdim 1286321369Sdim /// Read the unhashed control block. 1287321369Sdim /// 1288321369Sdim /// This has no effect on \c F.Stream, instead creating a fresh cursor from 1289321369Sdim /// \c F.Data and reading ahead. 1290321369Sdim ASTReadResult readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 1291321369Sdim unsigned ClientLoadCapabilities); 1292321369Sdim 1293321369Sdim static ASTReadResult 1294321369Sdim readUnhashedControlBlockImpl(ModuleFile *F, llvm::StringRef StreamData, 1295321369Sdim unsigned ClientLoadCapabilities, 1296321369Sdim bool AllowCompatibleConfigurationMismatch, 1297321369Sdim ASTReaderListener *Listener, 1298321369Sdim bool ValidateDiagnosticOptions); 1299321369Sdim 1300276479Sdim ASTReadResult ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities); 1301296417Sdim ASTReadResult ReadExtensionBlock(ModuleFile &F); 1302321369Sdim void ReadModuleOffsetMap(ModuleFile &F) const; 1303280031Sdim bool ParseLineTable(ModuleFile &F, const RecordData &Record); 1304243830Sdim bool ReadSourceManagerBlock(ModuleFile &F); 1305226633Sdim llvm::BitstreamCursor &SLocCursorForID(int ID); 1306234353Sdim SourceLocation getImportLocation(ModuleFile *F); 1307280031Sdim ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 1308280031Sdim const ModuleFile *ImportedBy, 1309280031Sdim unsigned ClientLoadCapabilities); 1310276479Sdim ASTReadResult ReadSubmoduleBlock(ModuleFile &F, 1311276479Sdim unsigned ClientLoadCapabilities); 1312243830Sdim static bool ParseLanguageOptions(const RecordData &Record, bool Complain, 1313280031Sdim ASTReaderListener &Listener, 1314280031Sdim bool AllowCompatibleDifferences); 1315243830Sdim static bool ParseTargetOptions(const RecordData &Record, bool Complain, 1316288943Sdim ASTReaderListener &Listener, 1317288943Sdim bool AllowCompatibleDifferences); 1318243830Sdim static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain, 1319243830Sdim ASTReaderListener &Listener); 1320243830Sdim static bool ParseFileSystemOptions(const RecordData &Record, bool Complain, 1321243830Sdim ASTReaderListener &Listener); 1322243830Sdim static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain, 1323243830Sdim ASTReaderListener &Listener); 1324243830Sdim static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain, 1325243830Sdim ASTReaderListener &Listener, 1326243830Sdim std::string &SuggestedPredefines); 1327243830Sdim 1328218893Sdim struct RecordLocation { 1329327952Sdim ModuleFile *F; 1330327952Sdim uint64_t Offset; 1331327952Sdim 1332341825Sdim RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {} 1333218893Sdim }; 1334212795Sdim 1335226633Sdim QualType readTypeRecord(unsigned Index); 1336276479Sdim void readExceptionSpec(ModuleFile &ModuleFile, 1337276479Sdim SmallVectorImpl<QualType> &ExceptionStorage, 1338280031Sdim FunctionProtoType::ExceptionSpecInfo &ESI, 1339276479Sdim const RecordData &Record, unsigned &Index); 1340212795Sdim RecordLocation TypeCursorForIndex(unsigned Index); 1341212795Sdim void LoadedDecl(unsigned Index, Decl *D); 1342226633Sdim Decl *ReadDeclRecord(serialization::DeclID ID); 1343276479Sdim void markIncompleteDeclChain(Decl *Canon); 1344288943Sdim 1345341825Sdim /// Returns the most recent declaration of a declaration (which must be 1346288943Sdim /// of a redeclarable kind) that is either local or has already been loaded 1347288943Sdim /// merged into its redecl chain. 1348288943Sdim Decl *getMostRecentExistingDecl(Decl *D); 1349288943Sdim 1350234353Sdim RecordLocation DeclCursorForID(serialization::DeclID ID, 1351309124Sdim SourceLocation &Location); 1352321369Sdim void loadDeclUpdateRecords(PendingUpdateRecord &Record); 1353296417Sdim void loadPendingDeclChain(Decl *D, uint64_t LocalOffset); 1354234353Sdim void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D, 1355234353Sdim unsigned PreviousGeneration = 0); 1356234353Sdim 1357226633Sdim RecordLocation getLocalBitOffset(uint64_t GlobalOffset); 1358234353Sdim uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset); 1359212795Sdim 1360341825Sdim /// Returns the first preprocessed entity ID that begins or ends after 1361276479Sdim /// \arg Loc. 1362226633Sdim serialization::PreprocessedEntityID 1363276479Sdim findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const; 1364226633Sdim 1365341825Sdim /// Find the next module that contains entities and return the ID 1366226633Sdim /// of the first entry. 1367243830Sdim /// 1368243830Sdim /// \param SLocMapI points at a chunk of a module that contains no 1369239462Sdim /// preprocessed entities or the entities it contains are not the 1370239462Sdim /// ones we are looking for. 1371226633Sdim serialization::PreprocessedEntityID 1372226633Sdim findNextPreprocessedEntity( 1373226633Sdim GlobalSLocOffsetMapType::const_iterator SLocMapI) const; 1374226633Sdim 1375341825Sdim /// Returns (ModuleFile, Local index) pair for \p GlobalIndex of a 1376234353Sdim /// preprocessed entity. 1377234353Sdim std::pair<ModuleFile *, unsigned> 1378234353Sdim getModulePreprocessedEntity(unsigned GlobalIndex); 1379234353Sdim 1380341825Sdim /// Returns (begin, end) pair for the preprocessed entities of a 1381243830Sdim /// particular module. 1382288943Sdim llvm::iterator_range<PreprocessingRecord::iterator> 1383288943Sdim getModulePreprocessedEntities(ModuleFile &Mod) const; 1384243830Sdim 1385321369Sdimpublic: 1386288943Sdim class ModuleDeclIterator 1387288943Sdim : public llvm::iterator_adaptor_base< 1388288943Sdim ModuleDeclIterator, const serialization::LocalDeclID *, 1389288943Sdim std::random_access_iterator_tag, const Decl *, ptrdiff_t, 1390288943Sdim const Decl *, const Decl *> { 1391327952Sdim ASTReader *Reader = nullptr; 1392327952Sdim ModuleFile *Mod = nullptr; 1393243830Sdim 1394243830Sdim public: 1395327952Sdim ModuleDeclIterator() : iterator_adaptor_base(nullptr) {} 1396243830Sdim 1397243830Sdim ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod, 1398243830Sdim const serialization::LocalDeclID *Pos) 1399288943Sdim : iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {} 1400243830Sdim 1401243830Sdim value_type operator*() const { 1402288943Sdim return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I)); 1403243830Sdim } 1404327952Sdim 1405288943Sdim value_type operator->() const { return **this; } 1406243830Sdim 1407288943Sdim bool operator==(const ModuleDeclIterator &RHS) const { 1408288943Sdim assert(Reader == RHS.Reader && Mod == RHS.Mod); 1409288943Sdim return I == RHS.I; 1410243830Sdim } 1411243830Sdim }; 1412243830Sdim 1413288943Sdim llvm::iterator_range<ModuleDeclIterator> 1414288943Sdim getModuleFileLevelDecls(ModuleFile &Mod); 1415243830Sdim 1416321369Sdimprivate: 1417212795Sdim void PassInterestingDeclsToConsumer(); 1418234353Sdim void PassInterestingDeclToConsumer(Decl *D); 1419212795Sdim 1420234353Sdim void finishPendingActions(); 1421280031Sdim void diagnoseOdrViolations(); 1422234353Sdim 1423251662Sdim void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name); 1424251662Sdim 1425249423Sdim void addPendingDeclContextInfo(Decl *D, 1426249423Sdim serialization::GlobalDeclID SemaDC, 1427249423Sdim serialization::GlobalDeclID LexicalDC) { 1428249423Sdim assert(D); 1429249423Sdim PendingDeclContextInfo Info = { D, SemaDC, LexicalDC }; 1430249423Sdim PendingDeclContextInfos.push_back(Info); 1431249423Sdim } 1432249423Sdim 1433341825Sdim /// Produce an error diagnostic and return true. 1434212795Sdim /// 1435212795Sdim /// This routine should only be used for fatal errors that have to 1436212795Sdim /// do with non-routine failures (e.g., corrupted AST file). 1437321369Sdim void Error(StringRef Msg) const; 1438226633Sdim void Error(unsigned DiagID, StringRef Arg1 = StringRef(), 1439321369Sdim StringRef Arg2 = StringRef()) const; 1440353358Sdim void Error(llvm::Error &&Err) const; 1441212795Sdim 1442212795Sdimpublic: 1443341825Sdim /// Load the AST file and validate its contents against the given 1444212795Sdim /// Preprocessor. 1445212795Sdim /// 1446212795Sdim /// \param PP the preprocessor associated with the context in which this 1447212795Sdim /// precompiled header will be loaded. 1448212795Sdim /// 1449212795Sdim /// \param Context the AST context that this precompiled header will be 1450321369Sdim /// loaded into, if any. 1451212795Sdim /// 1452296417Sdim /// \param PCHContainerRdr the PCHContainerOperations to use for loading and 1453288943Sdim /// creating modules. 1454288943Sdim /// 1455296417Sdim /// \param Extensions the list of module file extensions that can be loaded 1456296417Sdim /// from the AST files. 1457296417Sdim /// 1458212795Sdim /// \param isysroot If non-NULL, the system include path specified by the 1459212795Sdim /// user. This is only used with relocatable PCH files. If non-NULL, 1460212795Sdim /// a relocatable PCH file will use the default path "/". 1461212795Sdim /// 1462212795Sdim /// \param DisableValidation If true, the AST reader will suppress most 1463212795Sdim /// of its regular consistency checking, allowing the use of precompiled 1464212795Sdim /// headers that cannot be determined to be compatible. 1465218893Sdim /// 1466234353Sdim /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an 1467234353Sdim /// AST file the was created out of an AST with compiler errors, 1468234353Sdim /// otherwise it will reject it. 1469249423Sdim /// 1470276479Sdim /// \param AllowConfigurationMismatch If true, the AST reader will not check 1471276479Sdim /// for configuration differences between the AST file and the invocation. 1472276479Sdim /// 1473276479Sdim /// \param ValidateSystemInputs If true, the AST reader will validate 1474276479Sdim /// system input files in addition to user input files. This is only 1475276479Sdim /// meaningful if \p DisableValidation is false. 1476276479Sdim /// 1477249423Sdim /// \param UseGlobalIndex If true, the AST reader will try to load and use 1478249423Sdim /// the global module index. 1479288943Sdim /// 1480288943Sdim /// \param ReadTimer If non-null, a timer used to track the time spent 1481288943Sdim /// deserializing. 1482353358Sdim ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 1483353358Sdim ASTContext *Context, const PCHContainerReader &PCHContainerRdr, 1484314564Sdim ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 1485288943Sdim StringRef isysroot = "", bool DisableValidation = false, 1486249423Sdim bool AllowASTWithCompilerErrors = false, 1487276479Sdim bool AllowConfigurationMismatch = false, 1488288943Sdim bool ValidateSystemInputs = false, bool UseGlobalIndex = true, 1489288943Sdim std::unique_ptr<llvm::Timer> ReadTimer = {}); 1490327952Sdim ASTReader(const ASTReader &) = delete; 1491327952Sdim ASTReader &operator=(const ASTReader &) = delete; 1492288943Sdim ~ASTReader() override; 1493212795Sdim 1494226633Sdim SourceManager &getSourceManager() const { return SourceMgr; } 1495249423Sdim FileManager &getFileManager() const { return FileMgr; } 1496296417Sdim DiagnosticsEngine &getDiags() const { return Diags; } 1497234353Sdim 1498341825Sdim /// Flags that indicate what kind of AST loading failures the client 1499243830Sdim /// of the AST reader can directly handle. 1500243830Sdim /// 1501243830Sdim /// When a client states that it can handle a particular kind of failure, 1502243830Sdim /// the AST reader will not emit errors when producing that kind of failure. 1503243830Sdim enum LoadFailureCapabilities { 1504341825Sdim /// The client can't handle any AST loading failures. 1505243830Sdim ARR_None = 0, 1506327952Sdim 1507341825Sdim /// The client can handle an AST file that cannot load because it 1508249423Sdim /// is missing. 1509249423Sdim ARR_Missing = 0x1, 1510327952Sdim 1511341825Sdim /// The client can handle an AST file that cannot load because it 1512243830Sdim /// is out-of-date relative to its input files. 1513249423Sdim ARR_OutOfDate = 0x2, 1514327952Sdim 1515341825Sdim /// The client can handle an AST file that cannot load because it 1516243830Sdim /// was built with a different version of Clang. 1517249423Sdim ARR_VersionMismatch = 0x4, 1518327952Sdim 1519341825Sdim /// The client can handle an AST file that cannot load because it's 1520243830Sdim /// compiled configuration doesn't match that of the context it was 1521243830Sdim /// loaded into. 1522249423Sdim ARR_ConfigurationMismatch = 0x8 1523243830Sdim }; 1524243830Sdim 1525341825Sdim /// Load the AST file designated by the given file name. 1526243830Sdim /// 1527243830Sdim /// \param FileName The name of the AST file to load. 1528243830Sdim /// 1529243830Sdim /// \param Type The kind of AST being loaded, e.g., PCH, module, main file, 1530243830Sdim /// or preamble. 1531243830Sdim /// 1532249423Sdim /// \param ImportLoc the location where the module file will be considered as 1533249423Sdim /// imported from. For non-module AST types it should be invalid. 1534249423Sdim /// 1535243830Sdim /// \param ClientLoadCapabilities The set of client load-failure 1536243830Sdim /// capabilities, represented as a bitset of the enumerators of 1537243830Sdim /// LoadFailureCapabilities. 1538314564Sdim /// 1539314564Sdim /// \param Imported optional out-parameter to append the list of modules 1540314564Sdim /// that were imported by precompiled headers or any other non-module AST file 1541309124Sdim ASTReadResult ReadAST(StringRef FileName, ModuleKind Type, 1542249423Sdim SourceLocation ImportLoc, 1543314564Sdim unsigned ClientLoadCapabilities, 1544314564Sdim SmallVectorImpl<ImportedSubmodule> *Imported = nullptr); 1545212795Sdim 1546341825Sdim /// Make the entities in the given module and any of its (non-explicit) 1547234353Sdim /// submodules visible to name lookup. 1548234353Sdim /// 1549234353Sdim /// \param Mod The module whose names should be made visible. 1550234353Sdim /// 1551239462Sdim /// \param NameVisibility The level of visibility to give the names in the 1552239462Sdim /// module. Visibility can only be increased over time. 1553249423Sdim /// 1554249423Sdim /// \param ImportLoc The location at which the import occurs. 1555276479Sdim void makeModuleVisible(Module *Mod, 1556249423Sdim Module::NameVisibilityKind NameVisibility, 1557288943Sdim SourceLocation ImportLoc); 1558276479Sdim 1559341825Sdim /// Make the names within this set of hidden names visible. 1560288943Sdim void makeNamesVisible(const HiddenNames &Names, Module *Owner); 1561276479Sdim 1562341825Sdim /// Note that MergedDef is a redefinition of the canonical definition 1563314564Sdim /// Def, so Def should be visible whenever MergedDef is. 1564314564Sdim void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef); 1565314564Sdim 1566341825Sdim /// Take the AST callbacks listener. 1567280031Sdim std::unique_ptr<ASTReaderListener> takeListener() { 1568280031Sdim return std::move(Listener); 1569280031Sdim } 1570280031Sdim 1571341825Sdim /// Set the AST callbacks listener. 1572280031Sdim void setListener(std::unique_ptr<ASTReaderListener> Listener) { 1573280031Sdim this->Listener = std::move(Listener); 1574212795Sdim } 1575212795Sdim 1576341825Sdim /// Add an AST callback listener. 1577276479Sdim /// 1578276479Sdim /// Takes ownership of \p L. 1579280031Sdim void addListener(std::unique_ptr<ASTReaderListener> L) { 1580276479Sdim if (Listener) 1581280031Sdim L = llvm::make_unique<ChainedASTReaderListener>(std::move(L), 1582280031Sdim std::move(Listener)); 1583280031Sdim Listener = std::move(L); 1584276479Sdim } 1585276479Sdim 1586280031Sdim /// RAII object to temporarily add an AST callback listener. 1587280031Sdim class ListenerScope { 1588280031Sdim ASTReader &Reader; 1589327952Sdim bool Chained = false; 1590280031Sdim 1591280031Sdim public: 1592280031Sdim ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L) 1593327952Sdim : Reader(Reader) { 1594280031Sdim auto Old = Reader.takeListener(); 1595280031Sdim if (Old) { 1596280031Sdim Chained = true; 1597280031Sdim L = llvm::make_unique<ChainedASTReaderListener>(std::move(L), 1598280031Sdim std::move(Old)); 1599280031Sdim } 1600280031Sdim Reader.setListener(std::move(L)); 1601280031Sdim } 1602327952Sdim 1603280031Sdim ~ListenerScope() { 1604280031Sdim auto New = Reader.takeListener(); 1605280031Sdim if (Chained) 1606280031Sdim Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get()) 1607280031Sdim ->takeSecond()); 1608280031Sdim } 1609280031Sdim }; 1610280031Sdim 1611341825Sdim /// Set the AST deserialization listener. 1612276479Sdim void setDeserializationListener(ASTDeserializationListener *Listener, 1613276479Sdim bool TakeOwnership = false); 1614212795Sdim 1615341825Sdim /// Get the AST deserialization listener. 1616341825Sdim ASTDeserializationListener *getDeserializationListener() { 1617341825Sdim return DeserializationListener; 1618341825Sdim } 1619341825Sdim 1620341825Sdim /// Determine whether this AST reader has a global index. 1621276479Sdim bool hasGlobalIndex() const { return (bool)GlobalIndex; } 1622249423Sdim 1623341825Sdim /// Return global module index. 1624276479Sdim GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); } 1625276479Sdim 1626341825Sdim /// Reset reader for a reload try. 1627276479Sdim void resetForReload() { TriedLoadingGlobalIndex = false; } 1628276479Sdim 1629341825Sdim /// Attempts to load the global index. 1630249423Sdim /// 1631249423Sdim /// \returns true if loading the global index has failed for any reason. 1632249423Sdim bool loadGlobalIndex(); 1633249423Sdim 1634341825Sdim /// Determine whether we tried to load the global index, but failed, 1635249423Sdim /// e.g., because it is out-of-date or does not exist. 1636249423Sdim bool isGlobalIndexUnavailable() const; 1637341825Sdim 1638341825Sdim /// Initializes the ASTContext 1639226633Sdim void InitializeContext(); 1640212795Sdim 1641341825Sdim /// Update the state of Sema after loading some additional modules. 1642261991Sdim void UpdateSema(); 1643261991Sdim 1644341825Sdim /// Add in-memory (virtual file) buffer. 1645280031Sdim void addInMemoryBuffer(StringRef &FileName, 1646280031Sdim std::unique_ptr<llvm::MemoryBuffer> Buffer) { 1647280031Sdim ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer)); 1648221345Sdim } 1649221345Sdim 1650341825Sdim /// Finalizes the AST reader's state before writing an AST file to 1651234353Sdim /// disk. 1652234353Sdim /// 1653234353Sdim /// This operation may undo temporary state in the AST that should not be 1654234353Sdim /// emitted. 1655234353Sdim void finalizeForWriting(); 1656234353Sdim 1657341825Sdim /// Retrieve the module manager. 1658226633Sdim ModuleManager &getModuleManager() { return ModuleMgr; } 1659212795Sdim 1660341825Sdim /// Retrieve the preprocessor. 1661226633Sdim Preprocessor &getPreprocessor() const { return PP; } 1662234353Sdim 1663341825Sdim /// Retrieve the name of the original source file name for the primary 1664243830Sdim /// module file. 1665243830Sdim StringRef getOriginalSourceFile() { 1666341825Sdim return ModuleMgr.getPrimaryModule().OriginalSourceFileName; 1667243830Sdim } 1668212795Sdim 1669341825Sdim /// Retrieve the name of the original source file name directly from 1670212795Sdim /// the AST file, without actually loading the AST file. 1671288943Sdim static std::string 1672288943Sdim getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr, 1673288943Sdim const PCHContainerReader &PCHContainerRdr, 1674288943Sdim DiagnosticsEngine &Diags); 1675212795Sdim 1676341825Sdim /// Read the control block for the named AST file. 1677243830Sdim /// 1678243830Sdim /// \returns true if an error occurred, false otherwise. 1679288943Sdim static bool 1680288943Sdim readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, 1681288943Sdim const PCHContainerReader &PCHContainerRdr, 1682296417Sdim bool FindModuleFileExtensions, 1683314564Sdim ASTReaderListener &Listener, 1684314564Sdim bool ValidateDiagnosticOptions); 1685243830Sdim 1686341825Sdim /// Determine whether the given AST file is acceptable to load into a 1687243830Sdim /// translation unit with the given language and target options. 1688288943Sdim static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 1689288943Sdim const PCHContainerReader &PCHContainerRdr, 1690243830Sdim const LangOptions &LangOpts, 1691243830Sdim const TargetOptions &TargetOpts, 1692288943Sdim const PreprocessorOptions &PPOpts, 1693321369Sdim StringRef ExistingModuleCachePath); 1694243830Sdim 1695341825Sdim /// Returns the suggested contents of the predefines buffer, 1696212795Sdim /// which contains a (typically-empty) subset of the predefines 1697212795Sdim /// build prior to including the precompiled header. 1698212795Sdim const std::string &getSuggestedPredefines() { return SuggestedPredefines; } 1699212795Sdim 1700341825Sdim /// Read a preallocated preprocessed entity from the external source. 1701226633Sdim /// 1702226633Sdim /// \returns null if an error occurred that prevented the preprocessed 1703226633Sdim /// entity from being loaded. 1704276479Sdim PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override; 1705218893Sdim 1706341825Sdim /// Returns a pair of [Begin, End) indices of preallocated 1707243830Sdim /// preprocessed entities that \p Range encompasses. 1708276479Sdim std::pair<unsigned, unsigned> 1709276479Sdim findPreprocessedEntitiesInRange(SourceRange Range) override; 1710226633Sdim 1711341825Sdim /// Optionally returns true or false if the preallocated preprocessed 1712243830Sdim /// entity with index \p Index came from file \p FID. 1713276479Sdim Optional<bool> isPreprocessedEntityInFileID(unsigned Index, 1714276479Sdim FileID FID) override; 1715234353Sdim 1716341825Sdim /// Read a preallocated skipped range from the external source. 1717341825Sdim SourceRange ReadSkippedRange(unsigned Index) override; 1718341825Sdim 1719341825Sdim /// Read the header file information for the given file entry. 1720276479Sdim HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) override; 1721218893Sdim 1722226633Sdim void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag); 1723218893Sdim 1724341825Sdim /// Returns the number of source locations found in the chain. 1725212795Sdim unsigned getTotalNumSLocs() const { 1726212795Sdim return TotalNumSLocEntries; 1727212795Sdim } 1728212795Sdim 1729341825Sdim /// Returns the number of identifiers found in the chain. 1730212795Sdim unsigned getTotalNumIdentifiers() const { 1731212795Sdim return static_cast<unsigned>(IdentifiersLoaded.size()); 1732212795Sdim } 1733212795Sdim 1734341825Sdim /// Returns the number of macros found in the chain. 1735243830Sdim unsigned getTotalNumMacros() const { 1736243830Sdim return static_cast<unsigned>(MacrosLoaded.size()); 1737243830Sdim } 1738243830Sdim 1739341825Sdim /// Returns the number of types found in the chain. 1740212795Sdim unsigned getTotalNumTypes() const { 1741212795Sdim return static_cast<unsigned>(TypesLoaded.size()); 1742212795Sdim } 1743212795Sdim 1744341825Sdim /// Returns the number of declarations found in the chain. 1745212795Sdim unsigned getTotalNumDecls() const { 1746212795Sdim return static_cast<unsigned>(DeclsLoaded.size()); 1747212795Sdim } 1748212795Sdim 1749341825Sdim /// Returns the number of submodules known. 1750234353Sdim unsigned getTotalNumSubmodules() const { 1751234353Sdim return static_cast<unsigned>(SubmodulesLoaded.size()); 1752234353Sdim } 1753341825Sdim 1754341825Sdim /// Returns the number of selectors found in the chain. 1755212795Sdim unsigned getTotalNumSelectors() const { 1756212795Sdim return static_cast<unsigned>(SelectorsLoaded.size()); 1757212795Sdim } 1758212795Sdim 1759341825Sdim /// Returns the number of preprocessed entities known to the AST 1760226633Sdim /// reader. 1761226633Sdim unsigned getTotalNumPreprocessedEntities() const { 1762226633Sdim unsigned Result = 0; 1763321369Sdim for (const auto &M : ModuleMgr) 1764321369Sdim Result += M.NumPreprocessedEntities; 1765226633Sdim return Result; 1766218893Sdim } 1767234353Sdim 1768341825Sdim /// Reads a TemplateArgumentLocInfo appropriate for the 1769212795Sdim /// given TemplateArgument kind. 1770212795Sdim TemplateArgumentLocInfo 1771234353Sdim GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind, 1772212795Sdim const RecordData &Record, unsigned &Idx); 1773212795Sdim 1774341825Sdim /// Reads a TemplateArgumentLoc. 1775212795Sdim TemplateArgumentLoc 1776234353Sdim ReadTemplateArgumentLoc(ModuleFile &F, 1777212795Sdim const RecordData &Record, unsigned &Idx); 1778212795Sdim 1779261991Sdim const ASTTemplateArgumentListInfo* 1780261991Sdim ReadASTTemplateArgumentListInfo(ModuleFile &F, 1781261991Sdim const RecordData &Record, unsigned &Index); 1782261991Sdim 1783341825Sdim /// Reads a declarator info from the given record. 1784234353Sdim TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F, 1785212795Sdim const RecordData &Record, unsigned &Idx); 1786212795Sdim 1787341825Sdim /// Raad the type locations for the given TInfo. 1788341825Sdim void ReadTypeLoc(ModuleFile &F, const RecordData &Record, unsigned &Idx, 1789341825Sdim TypeLoc TL); 1790341825Sdim 1791341825Sdim /// Resolve a type ID into a type, potentially building a new 1792212795Sdim /// type. 1793212795Sdim QualType GetType(serialization::TypeID ID); 1794212795Sdim 1795341825Sdim /// Resolve a local type ID within a given AST file into a type. 1796234353Sdim QualType getLocalType(ModuleFile &F, unsigned LocalID); 1797234353Sdim 1798341825Sdim /// Map a local type ID within a given AST file into a global type ID. 1799234353Sdim serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const; 1800234353Sdim 1801341825Sdim /// Read a type from the current position in the given record, which 1802226633Sdim /// was read from the given AST file. 1803234353Sdim QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) { 1804226633Sdim if (Idx >= Record.size()) 1805341825Sdim return {}; 1806234353Sdim 1807226633Sdim return getLocalType(F, Record[Idx++]); 1808226633Sdim } 1809234353Sdim 1810341825Sdim /// Map from a local declaration ID within a given module to a 1811226633Sdim /// global declaration ID. 1812243830Sdim serialization::DeclID getGlobalDeclID(ModuleFile &F, 1813243830Sdim serialization::LocalDeclID LocalID) const; 1814212795Sdim 1815341825Sdim /// Returns true if global DeclID \p ID originated from module \p M. 1816234353Sdim bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const; 1817234353Sdim 1818341825Sdim /// Retrieve the module file that owns the given declaration, or NULL 1819234353Sdim /// if the declaration is not from a module file. 1820249423Sdim ModuleFile *getOwningModuleFile(const Decl *D); 1821276479Sdim 1822341825Sdim /// Get the best name we know for the module that owns the given 1823276479Sdim /// declaration, or an empty string if the declaration is not from a module. 1824276479Sdim std::string getOwningModuleNameForDiagnostic(const Decl *D); 1825276479Sdim 1826341825Sdim /// Returns the source location for the decl \p ID. 1827234353Sdim SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID); 1828234353Sdim 1829341825Sdim /// Resolve a declaration ID into a declaration, potentially 1830212795Sdim /// building a new declaration. 1831212795Sdim Decl *GetDecl(serialization::DeclID ID); 1832276479Sdim Decl *GetExternalDecl(uint32_t ID) override; 1833212795Sdim 1834341825Sdim /// Resolve a declaration ID into a declaration. Return 0 if it's not 1835276479Sdim /// been loaded yet. 1836276479Sdim Decl *GetExistingDecl(serialization::DeclID ID); 1837276479Sdim 1838341825Sdim /// Reads a declaration with the given local ID in the given module. 1839234353Sdim Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) { 1840226633Sdim return GetDecl(getGlobalDeclID(F, LocalID)); 1841226633Sdim } 1842226633Sdim 1843341825Sdim /// Reads a declaration with the given local ID in the given module. 1844226633Sdim /// 1845226633Sdim /// \returns The requested declaration, casted to the given return type. 1846226633Sdim template<typename T> 1847234353Sdim T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) { 1848226633Sdim return cast_or_null<T>(GetLocalDecl(F, LocalID)); 1849226633Sdim } 1850226633Sdim 1851341825Sdim /// Map a global declaration ID into the declaration ID used to 1852234353Sdim /// refer to this declaration within the given module fule. 1853234353Sdim /// 1854234353Sdim /// \returns the global ID of the given declaration as known in the given 1855234353Sdim /// module file. 1856341825Sdim serialization::DeclID 1857234353Sdim mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 1858234353Sdim serialization::DeclID GlobalID); 1859341825Sdim 1860341825Sdim /// Reads a declaration ID from the given position in a record in the 1861226633Sdim /// given module. 1862226633Sdim /// 1863226633Sdim /// \returns The declaration ID read from the record, adjusted to a global ID. 1864234353Sdim serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record, 1865226633Sdim unsigned &Idx); 1866234353Sdim 1867341825Sdim /// Reads a declaration from the given position in a record in the 1868226633Sdim /// given module. 1869234353Sdim Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) { 1870226633Sdim return GetDecl(ReadDeclID(F, R, I)); 1871226633Sdim } 1872234353Sdim 1873341825Sdim /// Reads a declaration from the given position in a record in the 1874226633Sdim /// given module. 1875226633Sdim /// 1876226633Sdim /// \returns The declaration read from this location, casted to the given 1877226633Sdim /// result type. 1878226633Sdim template<typename T> 1879234353Sdim T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) { 1880226633Sdim return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I))); 1881226633Sdim } 1882226633Sdim 1883341825Sdim /// If any redeclarations of \p D have been imported since it was 1884276479Sdim /// last checked, this digs out those redeclarations and adds them to the 1885276479Sdim /// redeclaration chain for \p D. 1886276479Sdim void CompleteRedeclChain(const Decl *D) override; 1887276479Sdim 1888276479Sdim CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override; 1889234353Sdim 1890341825Sdim /// Resolve the offset of a statement into a statement. 1891212795Sdim /// 1892212795Sdim /// This operation will read a new statement from the external 1893212795Sdim /// source each time it is called, and is meant to be used via a 1894212795Sdim /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 1895276479Sdim Stmt *GetExternalDeclStmt(uint64_t Offset) override; 1896212795Sdim 1897212795Sdim /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1898212795Sdim /// specified cursor. Read the abbreviations that are at the top of the block 1899212795Sdim /// and then leave the cursor pointing into the block. 1900296417Sdim static bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID); 1901212795Sdim 1902341825Sdim /// Finds all the visible declarations with a given name. 1903212795Sdim /// The current implementation of this method just loads the entire 1904212795Sdim /// lookup table as unmaterialized references. 1905276479Sdim bool FindExternalVisibleDeclsByName(const DeclContext *DC, 1906276479Sdim DeclarationName Name) override; 1907212795Sdim 1908341825Sdim /// Read all of the declarations lexically stored in a 1909212795Sdim /// declaration context. 1910212795Sdim /// 1911212795Sdim /// \param DC The declaration context whose declarations will be 1912212795Sdim /// read. 1913212795Sdim /// 1914296417Sdim /// \param IsKindWeWant A predicate indicating which declaration kinds 1915296417Sdim /// we are interested in. 1916296417Sdim /// 1917212795Sdim /// \param Decls Vector that will contain the declarations loaded 1918212795Sdim /// from the external source. The caller is responsible for merging 1919212795Sdim /// these declarations with any declarations already stored in the 1920212795Sdim /// declaration context. 1921296417Sdim void 1922296417Sdim FindExternalLexicalDecls(const DeclContext *DC, 1923296417Sdim llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 1924296417Sdim SmallVectorImpl<Decl *> &Decls) override; 1925212795Sdim 1926341825Sdim /// Get the decls that are contained in a file in the Offset/Length 1927243830Sdim /// range. \p Length can be 0 to indicate a point at \p Offset instead of 1928234353Sdim /// a range. 1929276479Sdim void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length, 1930276479Sdim SmallVectorImpl<Decl *> &Decls) override; 1931234353Sdim 1932341825Sdim /// Notify ASTReader that we started deserialization of 1933212795Sdim /// a decl or type so until FinishedDeserializing is called there may be 1934212795Sdim /// decls that are initializing. Must be paired with FinishedDeserializing. 1935288943Sdim void StartedDeserializing() override; 1936212795Sdim 1937341825Sdim /// Notify ASTReader that we finished the deserialization of 1938212795Sdim /// a decl or type. Must be paired with StartedDeserializing. 1939276479Sdim void FinishedDeserializing() override; 1940212795Sdim 1941341825Sdim /// Function that will be invoked when we begin parsing a new 1942212795Sdim /// translation unit involving this external AST source. 1943212795Sdim /// 1944212795Sdim /// This function will provide all of the external definitions to 1945212795Sdim /// the ASTConsumer. 1946276479Sdim void StartTranslationUnit(ASTConsumer *Consumer) override; 1947212795Sdim 1948341825Sdim /// Print some statistics about AST usage. 1949276479Sdim void PrintStats() override; 1950212795Sdim 1951341825Sdim /// Dump information about the AST reader to standard error. 1952226633Sdim void dump(); 1953234353Sdim 1954221345Sdim /// Return the amount of memory used by memory buffers, breaking down 1955221345Sdim /// by heap-backed versus mmap'ed memory. 1956276479Sdim void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override; 1957221345Sdim 1958341825Sdim /// Initialize the semantic source with the Sema instance 1959212795Sdim /// being used to perform semantic analysis on the abstract syntax 1960212795Sdim /// tree. 1961276479Sdim void InitializeSema(Sema &S) override; 1962212795Sdim 1963341825Sdim /// Inform the semantic consumer that Sema is no longer available. 1964276479Sdim void ForgetSema() override { SemaObj = nullptr; } 1965212795Sdim 1966341825Sdim /// Retrieve the IdentifierInfo for the named identifier. 1967212795Sdim /// 1968212795Sdim /// This routine builds a new IdentifierInfo for the given identifier. If any 1969212795Sdim /// declarations with this name are visible from translation unit scope, their 1970212795Sdim /// declarations will be deserialized and introduced into the declaration 1971212795Sdim /// chain of the identifier. 1972296417Sdim IdentifierInfo *get(StringRef Name) override; 1973212795Sdim 1974341825Sdim /// Retrieve an iterator into the set of all identifiers 1975218893Sdim /// in all loaded AST files. 1976276479Sdim IdentifierIterator *getIdentifiers() override; 1977218893Sdim 1978341825Sdim /// Load the contents of the global method pool for a given 1979212795Sdim /// selector. 1980276479Sdim void ReadMethodPool(Selector Sel) override; 1981212795Sdim 1982309124Sdim /// Load the contents of the global method pool for a given 1983309124Sdim /// selector if necessary. 1984309124Sdim void updateOutOfDateSelector(Selector Sel) override; 1985309124Sdim 1986341825Sdim /// Load the set of namespaces that are known to the external source, 1987224145Sdim /// which will be used during typo correction. 1988276479Sdim void ReadKnownNamespaces( 1989276479Sdim SmallVectorImpl<NamespaceDecl *> &Namespaces) override; 1990224145Sdim 1991276479Sdim void ReadUndefinedButUsed( 1992309124Sdim llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override; 1993249423Sdim 1994288943Sdim void ReadMismatchingDeleteExpressions(llvm::MapVector< 1995288943Sdim FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 1996288943Sdim Exprs) override; 1997288943Sdim 1998276479Sdim void ReadTentativeDefinitions( 1999276479Sdim SmallVectorImpl<VarDecl *> &TentativeDefs) override; 2000226633Sdim 2001276479Sdim void ReadUnusedFileScopedDecls( 2002276479Sdim SmallVectorImpl<const DeclaratorDecl *> &Decls) override; 2003226633Sdim 2004276479Sdim void ReadDelegatingConstructors( 2005276479Sdim SmallVectorImpl<CXXConstructorDecl *> &Decls) override; 2006226633Sdim 2007276479Sdim void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override; 2008226633Sdim 2009280031Sdim void ReadUnusedLocalTypedefNameCandidates( 2010280031Sdim llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override; 2011280031Sdim 2012276479Sdim void ReadReferencedSelectors( 2013327952Sdim SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) override; 2014226633Sdim 2015276479Sdim void ReadWeakUndeclaredIdentifiers( 2016327952Sdim SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WI) override; 2017226633Sdim 2018276479Sdim void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override; 2019226633Sdim 2020276479Sdim void ReadPendingInstantiations( 2021327952Sdim SmallVectorImpl<std::pair<ValueDecl *, 2022327952Sdim SourceLocation>> &Pending) override; 2023226633Sdim 2024276479Sdim void ReadLateParsedTemplates( 2025314564Sdim llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 2026314564Sdim &LPTMap) override; 2027261991Sdim 2028341825Sdim /// Load a selector from disk, registering its ID if it exists. 2029212795Sdim void LoadSelector(Selector Sel); 2030212795Sdim 2031212795Sdim void SetIdentifierInfo(unsigned ID, IdentifierInfo *II); 2032212795Sdim void SetGloballyVisibleDecls(IdentifierInfo *II, 2033226633Sdim const SmallVectorImpl<uint32_t> &DeclIDs, 2034276479Sdim SmallVectorImpl<Decl *> *Decls = nullptr); 2035212795Sdim 2036341825Sdim /// Report a diagnostic. 2037321369Sdim DiagnosticBuilder Diag(unsigned DiagID) const; 2038212795Sdim 2039341825Sdim /// Report a diagnostic. 2040321369Sdim DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const; 2041212795Sdim 2042226633Sdim IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID); 2043212795Sdim 2044234353Sdim IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record, 2045226633Sdim unsigned &Idx) { 2046226633Sdim return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++])); 2047212795Sdim } 2048212795Sdim 2049276479Sdim IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override { 2050243830Sdim // Note that we are loading an identifier. 2051243830Sdim Deserializing AnIdentifier(this); 2052243830Sdim 2053212795Sdim return DecodeIdentifierInfo(ID); 2054212795Sdim } 2055212795Sdim 2056234353Sdim IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID); 2057234353Sdim 2058234353Sdim serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M, 2059226633Sdim unsigned LocalID); 2060234353Sdim 2061249423Sdim void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo); 2062249423Sdim 2063341825Sdim /// Retrieve the macro with the given ID. 2064249423Sdim MacroInfo *getMacro(serialization::MacroID ID); 2065243830Sdim 2066341825Sdim /// Retrieve the global macro ID corresponding to the given local 2067243830Sdim /// ID within the given module file. 2068243830Sdim serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID); 2069243830Sdim 2070341825Sdim /// Read the source location entry with index ID. 2071276479Sdim bool ReadSLocEntry(int ID) override; 2072212795Sdim 2073341825Sdim /// Retrieve the module import location and module name for the 2074249423Sdim /// given source manager entry ID. 2075276479Sdim std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override; 2076249423Sdim 2077341825Sdim /// Retrieve the global submodule ID given a module and its local ID 2078234353Sdim /// number. 2079341825Sdim serialization::SubmoduleID 2080234353Sdim getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID); 2081314564Sdim 2082341825Sdim /// Retrieve the submodule that corresponds to a global submodule ID. 2083234353Sdim /// 2084234353Sdim Module *getSubmodule(serialization::SubmoduleID GlobalID); 2085249423Sdim 2086341825Sdim /// Retrieve the module that corresponds to the given module ID. 2087249423Sdim /// 2088249423Sdim /// Note: overrides method in ExternalASTSource 2089276479Sdim Module *getModule(unsigned ID) override; 2090249423Sdim 2091341825Sdim bool DeclIsFromPCHWithObjectFile(const Decl *D) override; 2092341825Sdim 2093341825Sdim /// Retrieve the module file with a given local ID within the specified 2094296417Sdim /// ModuleFile. 2095296417Sdim ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID); 2096296417Sdim 2097341825Sdim /// Get an ID for the given module file. 2098296417Sdim unsigned getModuleFileID(ModuleFile *M); 2099296417Sdim 2100341825Sdim /// Return a descriptor for the corresponding module. 2101288943Sdim llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override; 2102288943Sdim 2103321369Sdim ExtKind hasExternalDefinitions(const Decl *D) override; 2104321369Sdim 2105341825Sdim /// Retrieve a selector from the given module with its local ID 2106226633Sdim /// number. 2107234353Sdim Selector getLocalSelector(ModuleFile &M, unsigned LocalID); 2108212795Sdim 2109226633Sdim Selector DecodeSelector(serialization::SelectorID Idx); 2110226633Sdim 2111276479Sdim Selector GetExternalSelector(serialization::SelectorID ID) override; 2112276479Sdim uint32_t GetNumExternalSelectors() override; 2113212795Sdim 2114234353Sdim Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) { 2115226633Sdim return getLocalSelector(M, Record[Idx++]); 2116212795Sdim } 2117234353Sdim 2118341825Sdim /// Retrieve the global selector ID that corresponds to this 2119226633Sdim /// the local selector ID in a given module. 2120234353Sdim serialization::SelectorID getGlobalSelectorID(ModuleFile &F, 2121226633Sdim unsigned LocalID) const; 2122212795Sdim 2123341825Sdim /// Read a declaration name. 2124234353Sdim DeclarationName ReadDeclarationName(ModuleFile &F, 2125226633Sdim const RecordData &Record, unsigned &Idx); 2126234353Sdim void ReadDeclarationNameLoc(ModuleFile &F, 2127218893Sdim DeclarationNameLoc &DNLoc, DeclarationName Name, 2128218893Sdim const RecordData &Record, unsigned &Idx); 2129234353Sdim void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo, 2130218893Sdim const RecordData &Record, unsigned &Idx); 2131212795Sdim 2132234353Sdim void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 2133218893Sdim const RecordData &Record, unsigned &Idx); 2134218893Sdim 2135234353Sdim NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F, 2136226633Sdim const RecordData &Record, 2137212795Sdim unsigned &Idx); 2138212795Sdim 2139234353Sdim NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F, 2140219077Sdim const RecordData &Record, 2141219077Sdim unsigned &Idx); 2142219077Sdim 2143341825Sdim /// Read a template name. 2144234353Sdim TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record, 2145218893Sdim unsigned &Idx); 2146212795Sdim 2147341825Sdim /// Read a template argument. 2148296417Sdim TemplateArgument ReadTemplateArgument(ModuleFile &F, const RecordData &Record, 2149296417Sdim unsigned &Idx, 2150296417Sdim bool Canonicalize = false); 2151234353Sdim 2152341825Sdim /// Read a template parameter list. 2153234353Sdim TemplateParameterList *ReadTemplateParameterList(ModuleFile &F, 2154218893Sdim const RecordData &Record, 2155212795Sdim unsigned &Idx); 2156234353Sdim 2157341825Sdim /// Read a template argument array. 2158296417Sdim void ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 2159296417Sdim ModuleFile &F, const RecordData &Record, 2160296417Sdim unsigned &Idx, bool Canonicalize = false); 2161212795Sdim 2162341825Sdim /// Read a UnresolvedSet structure. 2163261991Sdim void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 2164212795Sdim const RecordData &Record, unsigned &Idx); 2165212795Sdim 2166341825Sdim /// Read a C++ base specifier. 2167234353Sdim CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F, 2168212795Sdim const RecordData &Record,unsigned &Idx); 2169212795Sdim 2170341825Sdim /// Read a CXXCtorInitializer array. 2171288943Sdim CXXCtorInitializer ** 2172234353Sdim ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 2173218893Sdim unsigned &Idx); 2174212795Sdim 2175341825Sdim /// Read the contents of a CXXCtorInitializer array. 2176288943Sdim CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override; 2177288943Sdim 2178341825Sdim /// Read a source location from raw form and return it in its 2179309124Sdim /// originating module file's source location space. 2180309124Sdim SourceLocation ReadUntranslatedSourceLocation(uint32_t Raw) const { 2181309124Sdim return SourceLocation::getFromRawEncoding((Raw >> 1) | (Raw << 31)); 2182309124Sdim } 2183309124Sdim 2184341825Sdim /// Read a source location from raw form. 2185309124Sdim SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const { 2186309124Sdim SourceLocation Loc = ReadUntranslatedSourceLocation(Raw); 2187309124Sdim return TranslateSourceLocation(ModuleFile, Loc); 2188309124Sdim } 2189309124Sdim 2190341825Sdim /// Translate a source location from another module file's source 2191309124Sdim /// location space into ours. 2192309124Sdim SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile, 2193309124Sdim SourceLocation Loc) const { 2194321369Sdim if (!ModuleFile.ModuleOffsetMap.empty()) 2195321369Sdim ReadModuleOffsetMap(ModuleFile); 2196309124Sdim assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != 2197309124Sdim ModuleFile.SLocRemap.end() && 2198226633Sdim "Cannot find offset to remap."); 2199234353Sdim int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second; 2200226633Sdim return Loc.getLocWithOffset(Remap); 2201218893Sdim } 2202218893Sdim 2203341825Sdim /// Read a source location. 2204234353Sdim SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, 2205261991Sdim const RecordDataImpl &Record, 2206261991Sdim unsigned &Idx) { 2207234353Sdim return ReadSourceLocation(ModuleFile, Record[Idx++]); 2208212795Sdim } 2209212795Sdim 2210341825Sdim /// Read a source range. 2211234353Sdim SourceRange ReadSourceRange(ModuleFile &F, 2212249423Sdim const RecordData &Record, unsigned &Idx); 2213212795Sdim 2214341825Sdim /// Read an integral value 2215212795Sdim llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx); 2216212795Sdim 2217341825Sdim /// Read a signed integral value 2218212795Sdim llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx); 2219212795Sdim 2220341825Sdim /// Read a floating-point value 2221249423Sdim llvm::APFloat ReadAPFloat(const RecordData &Record, 2222249423Sdim const llvm::fltSemantics &Sem, unsigned &Idx); 2223212795Sdim 2224353358Sdim /// Read an APValue 2225353358Sdim APValue ReadAPValue(const RecordData &Record, unsigned &Idx); 2226353358Sdim 2227341825Sdim // Read a string 2228243830Sdim static std::string ReadString(const RecordData &Record, unsigned &Idx); 2229212795Sdim 2230341825Sdim // Skip a string 2231327952Sdim static void SkipString(const RecordData &Record, unsigned &Idx) { 2232327952Sdim Idx += Record[Idx] + 1; 2233327952Sdim } 2234327952Sdim 2235341825Sdim // Read a path 2236280031Sdim std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx); 2237280031Sdim 2238353358Sdim // Read a path 2239353358Sdim std::string ReadPath(StringRef BaseDirectory, const RecordData &Record, 2240353358Sdim unsigned &Idx); 2241353358Sdim 2242341825Sdim // Skip a path 2243327952Sdim static void SkipPath(const RecordData &Record, unsigned &Idx) { 2244327952Sdim SkipString(Record, Idx); 2245327952Sdim } 2246327952Sdim 2247341825Sdim /// Read a version tuple. 2248243830Sdim static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx); 2249221345Sdim 2250234353Sdim CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record, 2251226633Sdim unsigned &Idx); 2252234353Sdim 2253344779Sdim /// Reads one attribute from the current stream position. 2254344779Sdim Attr *ReadAttr(ModuleFile &M, const RecordData &Record, unsigned &Idx); 2255344779Sdim 2256341825Sdim /// Reads attributes from the current stream position. 2257321369Sdim void ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs); 2258212795Sdim 2259341825Sdim /// Reads a statement. 2260234353Sdim Stmt *ReadStmt(ModuleFile &F); 2261212795Sdim 2262341825Sdim /// Reads an expression. 2263234353Sdim Expr *ReadExpr(ModuleFile &F); 2264212795Sdim 2265341825Sdim /// Reads a sub-statement operand during statement reading. 2266212795Sdim Stmt *ReadSubStmt() { 2267212795Sdim assert(ReadingKind == Read_Stmt && 2268212795Sdim "Should be called only during statement reading!"); 2269212795Sdim // Subexpressions are stored from last to first, so the next Stmt we need 2270212795Sdim // is at the back of the stack. 2271276479Sdim assert(!StmtStack.empty() && "Read too many sub-statements!"); 2272212795Sdim return StmtStack.pop_back_val(); 2273212795Sdim } 2274212795Sdim 2275341825Sdim /// Reads a sub-expression operand during statement reading. 2276212795Sdim Expr *ReadSubExpr(); 2277212795Sdim 2278341825Sdim /// Reads a token out of a record. 2279261991Sdim Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx); 2280251662Sdim 2281341825Sdim /// Reads the macro record located at the given offset. 2282249423Sdim MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset); 2283234353Sdim 2284341825Sdim /// Determine the global preprocessed entity ID that corresponds to 2285226633Sdim /// the given local ID within the given module. 2286234353Sdim serialization::PreprocessedEntityID 2287234353Sdim getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const; 2288234353Sdim 2289341825Sdim /// Add a macro to deserialize its macro directive history. 2290234353Sdim /// 2291234353Sdim /// \param II The name of the macro. 2292249423Sdim /// \param M The module file. 2293249423Sdim /// \param MacroDirectivesOffset Offset of the serialized macro directive 2294249423Sdim /// history. 2295288943Sdim void addPendingMacro(IdentifierInfo *II, ModuleFile *M, 2296288943Sdim uint64_t MacroDirectivesOffset); 2297234353Sdim 2298341825Sdim /// Read the set of macros defined by this external macro source. 2299276479Sdim void ReadDefinedMacros() override; 2300212795Sdim 2301341825Sdim /// Update an out-of-date identifier. 2302276479Sdim void updateOutOfDateIdentifier(IdentifierInfo &II) override; 2303234353Sdim 2304341825Sdim /// Note that this identifier is up-to-date. 2305234353Sdim void markIdentifierUpToDate(IdentifierInfo *II); 2306234353Sdim 2307341825Sdim /// Load all external visible decls in the given DeclContext. 2308276479Sdim void completeVisibleDeclsMap(const DeclContext *DC) override; 2309234353Sdim 2310341825Sdim /// Retrieve the AST context that this AST reader supplements. 2311321369Sdim ASTContext &getContext() { 2312321369Sdim assert(ContextObj && "requested AST context when not loading AST"); 2313321369Sdim return *ContextObj; 2314321369Sdim } 2315212795Sdim 2316341825Sdim // Contains the IDs for declarations that were requested before we have 2317212795Sdim // access to a Sema object. 2318280031Sdim SmallVector<uint64_t, 16> PreloadedDeclIDs; 2319212795Sdim 2320341825Sdim /// Retrieve the semantic analysis object used to analyze the 2321212795Sdim /// translation unit in which the precompiled header is being 2322212795Sdim /// imported. 2323212795Sdim Sema *getSema() { return SemaObj; } 2324212795Sdim 2325341825Sdim /// Get the identifier resolver used for name lookup / updates 2326309124Sdim /// in the translation unit scope. We have one of these even if we don't 2327309124Sdim /// have a Sema object. 2328309124Sdim IdentifierResolver &getIdResolver(); 2329309124Sdim 2330341825Sdim /// Retrieve the identifier table associated with the 2331212795Sdim /// preprocessor. 2332212795Sdim IdentifierTable &getIdentifierTable(); 2333212795Sdim 2334341825Sdim /// Record that the given ID maps to the given switch-case 2335212795Sdim /// statement. 2336212795Sdim void RecordSwitchCaseID(SwitchCase *SC, unsigned ID); 2337212795Sdim 2338341825Sdim /// Retrieve the switch-case statement with the given ID. 2339212795Sdim SwitchCase *getSwitchCaseWithID(unsigned ID); 2340212795Sdim 2341218893Sdim void ClearSwitchCaseIDs(); 2342239462Sdim 2343341825Sdim /// Cursors for comments blocks. 2344239462Sdim SmallVector<std::pair<llvm::BitstreamCursor, 2345239462Sdim serialization::ModuleFile *>, 8> CommentsCursors; 2346239462Sdim 2347341825Sdim /// Loads comments ranges. 2348276479Sdim void ReadComments() override; 2349309124Sdim 2350321369Sdim /// Visit all the input files of the given module file. 2351321369Sdim void visitInputFiles(serialization::ModuleFile &MF, 2352321369Sdim bool IncludeSystem, bool Complain, 2353321369Sdim llvm::function_ref<void(const serialization::InputFile &IF, 2354321369Sdim bool isSystem)> Visitor); 2355321369Sdim 2356321369Sdim /// Visit all the top-level module maps loaded when building the given module 2357321369Sdim /// file. 2358321369Sdim void visitTopLevelModuleMaps(serialization::ModuleFile &MF, 2359321369Sdim llvm::function_ref< 2360321369Sdim void(const FileEntry *)> Visitor); 2361321369Sdim 2362309124Sdim bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; } 2363212795Sdim}; 2364212795Sdim 2365341825Sdim/// An object for streaming information from a record. 2366314564Sdimclass ASTRecordReader { 2367327952Sdim using ModuleFile = serialization::ModuleFile; 2368314564Sdim 2369314564Sdim ASTReader *Reader; 2370314564Sdim ModuleFile *F; 2371314564Sdim unsigned Idx = 0; 2372314564Sdim ASTReader::RecordData Record; 2373314564Sdim 2374327952Sdim using RecordData = ASTReader::RecordData; 2375327952Sdim using RecordDataImpl = ASTReader::RecordDataImpl; 2376314564Sdim 2377314564Sdimpublic: 2378314564Sdim /// Construct an ASTRecordReader that uses the default encoding scheme. 2379327952Sdim ASTRecordReader(ASTReader &Reader, ModuleFile &F) : Reader(&Reader), F(&F) {} 2380314564Sdim 2381341825Sdim /// Reads a record with id AbbrevID from Cursor, resetting the 2382314564Sdim /// internal state. 2383353358Sdim Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor, 2384353358Sdim unsigned AbbrevID); 2385314564Sdim 2386341825Sdim /// Is this a module file for a module (rather than a PCH or similar). 2387314564Sdim bool isModule() const { return F->isModule(); } 2388314564Sdim 2389341825Sdim /// Retrieve the AST context that this AST reader supplements. 2390314564Sdim ASTContext &getContext() { return Reader->getContext(); } 2391314564Sdim 2392341825Sdim /// The current position in this record. 2393314564Sdim unsigned getIdx() const { return Idx; } 2394327952Sdim 2395341825Sdim /// The length of this record. 2396314564Sdim size_t size() const { return Record.size(); } 2397314564Sdim 2398341825Sdim /// An arbitrary index in this record. 2399314564Sdim const uint64_t &operator[](size_t N) { return Record[N]; } 2400327952Sdim 2401341825Sdim /// The last element in this record. 2402314564Sdim const uint64_t &back() const { return Record.back(); } 2403314564Sdim 2404341825Sdim /// Returns the current value in this record, and advances to the 2405314564Sdim /// next value. 2406314564Sdim const uint64_t &readInt() { return Record[Idx++]; } 2407327952Sdim 2408341825Sdim /// Returns the current value in this record, without advancing. 2409314564Sdim const uint64_t &peekInt() { return Record[Idx]; } 2410314564Sdim 2411341825Sdim /// Skips the specified number of values. 2412314564Sdim void skipInts(unsigned N) { Idx += N; } 2413314564Sdim 2414341825Sdim /// Retrieve the global submodule ID its local ID number. 2415314564Sdim serialization::SubmoduleID 2416314564Sdim getGlobalSubmoduleID(unsigned LocalID) { 2417314564Sdim return Reader->getGlobalSubmoduleID(*F, LocalID); 2418314564Sdim } 2419314564Sdim 2420341825Sdim /// Retrieve the submodule that corresponds to a global submodule ID. 2421314564Sdim Module *getSubmodule(serialization::SubmoduleID GlobalID) { 2422314564Sdim return Reader->getSubmodule(GlobalID); 2423314564Sdim } 2424314564Sdim 2425341825Sdim /// Read the record that describes the lexical contents of a DC. 2426314564Sdim bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC) { 2427314564Sdim return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset, 2428314564Sdim DC); 2429314564Sdim } 2430314564Sdim 2431341825Sdim /// Read the record that describes the visible contents of a DC. 2432314564Sdim bool readVisibleDeclContextStorage(uint64_t Offset, 2433314564Sdim serialization::DeclID ID) { 2434314564Sdim return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset, 2435314564Sdim ID); 2436314564Sdim } 2437314564Sdim 2438353358Sdim ExplicitSpecifier readExplicitSpec() { 2439353358Sdim uint64_t Kind = readInt(); 2440353358Sdim bool HasExpr = Kind & 0x1; 2441353358Sdim Kind = Kind >> 1; 2442353358Sdim return ExplicitSpecifier(HasExpr ? readExpr() : nullptr, 2443353358Sdim static_cast<ExplicitSpecKind>(Kind)); 2444353358Sdim } 2445353358Sdim 2446314564Sdim void readExceptionSpec(SmallVectorImpl<QualType> &ExceptionStorage, 2447314564Sdim FunctionProtoType::ExceptionSpecInfo &ESI) { 2448314564Sdim return Reader->readExceptionSpec(*F, ExceptionStorage, ESI, Record, Idx); 2449314564Sdim } 2450314564Sdim 2451341825Sdim /// Get the global offset corresponding to a local offset. 2452314564Sdim uint64_t getGlobalBitOffset(uint32_t LocalOffset) { 2453314564Sdim return Reader->getGlobalBitOffset(*F, LocalOffset); 2454314564Sdim } 2455314564Sdim 2456341825Sdim /// Reads a statement. 2457314564Sdim Stmt *readStmt() { return Reader->ReadStmt(*F); } 2458314564Sdim 2459341825Sdim /// Reads an expression. 2460314564Sdim Expr *readExpr() { return Reader->ReadExpr(*F); } 2461314564Sdim 2462341825Sdim /// Reads a sub-statement operand during statement reading. 2463314564Sdim Stmt *readSubStmt() { return Reader->ReadSubStmt(); } 2464314564Sdim 2465341825Sdim /// Reads a sub-expression operand during statement reading. 2466314564Sdim Expr *readSubExpr() { return Reader->ReadSubExpr(); } 2467314564Sdim 2468341825Sdim /// Reads a declaration with the given local ID in the given module. 2469321369Sdim /// 2470321369Sdim /// \returns The requested declaration, casted to the given return type. 2471321369Sdim template<typename T> 2472321369Sdim T *GetLocalDeclAs(uint32_t LocalID) { 2473321369Sdim return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID)); 2474321369Sdim } 2475321369Sdim 2476341825Sdim /// Reads a TemplateArgumentLocInfo appropriate for the 2477314564Sdim /// given TemplateArgument kind, advancing Idx. 2478314564Sdim TemplateArgumentLocInfo 2479314564Sdim getTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 2480314564Sdim return Reader->GetTemplateArgumentLocInfo(*F, Kind, Record, Idx); 2481314564Sdim } 2482314564Sdim 2483341825Sdim /// Reads a TemplateArgumentLoc, advancing Idx. 2484314564Sdim TemplateArgumentLoc 2485314564Sdim readTemplateArgumentLoc() { 2486314564Sdim return Reader->ReadTemplateArgumentLoc(*F, Record, Idx); 2487314564Sdim } 2488314564Sdim 2489314564Sdim const ASTTemplateArgumentListInfo* 2490314564Sdim readASTTemplateArgumentListInfo() { 2491314564Sdim return Reader->ReadASTTemplateArgumentListInfo(*F, Record, Idx); 2492314564Sdim } 2493314564Sdim 2494341825Sdim /// Reads a declarator info from the given record, advancing Idx. 2495314564Sdim TypeSourceInfo *getTypeSourceInfo() { 2496314564Sdim return Reader->GetTypeSourceInfo(*F, Record, Idx); 2497314564Sdim } 2498314564Sdim 2499341825Sdim /// Reads the location information for a type. 2500341825Sdim void readTypeLoc(TypeLoc TL) { 2501341825Sdim return Reader->ReadTypeLoc(*F, Record, Idx, TL); 2502341825Sdim } 2503341825Sdim 2504341825Sdim /// Map a local type ID within a given AST file to a global type ID. 2505314564Sdim serialization::TypeID getGlobalTypeID(unsigned LocalID) const { 2506314564Sdim return Reader->getGlobalTypeID(*F, LocalID); 2507314564Sdim } 2508314564Sdim 2509341825Sdim /// Read a type from the current position in the record. 2510314564Sdim QualType readType() { 2511314564Sdim return Reader->readType(*F, Record, Idx); 2512314564Sdim } 2513314564Sdim 2514341825Sdim /// Reads a declaration ID from the given position in this record. 2515314564Sdim /// 2516314564Sdim /// \returns The declaration ID read from the record, adjusted to a global ID. 2517314564Sdim serialization::DeclID readDeclID() { 2518314564Sdim return Reader->ReadDeclID(*F, Record, Idx); 2519314564Sdim } 2520314564Sdim 2521341825Sdim /// Reads a declaration from the given position in a record in the 2522314564Sdim /// given module, advancing Idx. 2523314564Sdim Decl *readDecl() { 2524314564Sdim return Reader->ReadDecl(*F, Record, Idx); 2525314564Sdim } 2526314564Sdim 2527341825Sdim /// Reads a declaration from the given position in the record, 2528314564Sdim /// advancing Idx. 2529314564Sdim /// 2530314564Sdim /// \returns The declaration read from this location, casted to the given 2531314564Sdim /// result type. 2532314564Sdim template<typename T> 2533314564Sdim T *readDeclAs() { 2534314564Sdim return Reader->ReadDeclAs<T>(*F, Record, Idx); 2535314564Sdim } 2536314564Sdim 2537314564Sdim IdentifierInfo *getIdentifierInfo() { 2538314564Sdim return Reader->GetIdentifierInfo(*F, Record, Idx); 2539314564Sdim } 2540314564Sdim 2541341825Sdim /// Read a selector from the Record, advancing Idx. 2542314564Sdim Selector readSelector() { 2543314564Sdim return Reader->ReadSelector(*F, Record, Idx); 2544314564Sdim } 2545314564Sdim 2546341825Sdim /// Read a declaration name, advancing Idx. 2547314564Sdim DeclarationName readDeclarationName() { 2548314564Sdim return Reader->ReadDeclarationName(*F, Record, Idx); 2549314564Sdim } 2550314564Sdim void readDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) { 2551314564Sdim return Reader->ReadDeclarationNameLoc(*F, DNLoc, Name, Record, Idx); 2552314564Sdim } 2553314564Sdim void readDeclarationNameInfo(DeclarationNameInfo &NameInfo) { 2554314564Sdim return Reader->ReadDeclarationNameInfo(*F, NameInfo, Record, Idx); 2555314564Sdim } 2556314564Sdim 2557314564Sdim void readQualifierInfo(QualifierInfo &Info) { 2558314564Sdim return Reader->ReadQualifierInfo(*F, Info, Record, Idx); 2559314564Sdim } 2560314564Sdim 2561314564Sdim NestedNameSpecifier *readNestedNameSpecifier() { 2562314564Sdim return Reader->ReadNestedNameSpecifier(*F, Record, Idx); 2563314564Sdim } 2564314564Sdim 2565314564Sdim NestedNameSpecifierLoc readNestedNameSpecifierLoc() { 2566314564Sdim return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx); 2567314564Sdim } 2568314564Sdim 2569341825Sdim /// Read a template name, advancing Idx. 2570314564Sdim TemplateName readTemplateName() { 2571314564Sdim return Reader->ReadTemplateName(*F, Record, Idx); 2572314564Sdim } 2573314564Sdim 2574341825Sdim /// Read a template argument, advancing Idx. 2575314564Sdim TemplateArgument readTemplateArgument(bool Canonicalize = false) { 2576314564Sdim return Reader->ReadTemplateArgument(*F, Record, Idx, Canonicalize); 2577314564Sdim } 2578314564Sdim 2579341825Sdim /// Read a template parameter list, advancing Idx. 2580314564Sdim TemplateParameterList *readTemplateParameterList() { 2581314564Sdim return Reader->ReadTemplateParameterList(*F, Record, Idx); 2582314564Sdim } 2583314564Sdim 2584341825Sdim /// Read a template argument array, advancing Idx. 2585314564Sdim void readTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 2586314564Sdim bool Canonicalize = false) { 2587314564Sdim return Reader->ReadTemplateArgumentList(TemplArgs, *F, Record, Idx, 2588314564Sdim Canonicalize); 2589314564Sdim } 2590314564Sdim 2591341825Sdim /// Read a UnresolvedSet structure, advancing Idx. 2592314564Sdim void readUnresolvedSet(LazyASTUnresolvedSet &Set) { 2593314564Sdim return Reader->ReadUnresolvedSet(*F, Set, Record, Idx); 2594314564Sdim } 2595314564Sdim 2596341825Sdim /// Read a C++ base specifier, advancing Idx. 2597314564Sdim CXXBaseSpecifier readCXXBaseSpecifier() { 2598314564Sdim return Reader->ReadCXXBaseSpecifier(*F, Record, Idx); 2599314564Sdim } 2600314564Sdim 2601341825Sdim /// Read a CXXCtorInitializer array, advancing Idx. 2602314564Sdim CXXCtorInitializer **readCXXCtorInitializers() { 2603314564Sdim return Reader->ReadCXXCtorInitializers(*F, Record, Idx); 2604314564Sdim } 2605314564Sdim 2606314564Sdim CXXTemporary *readCXXTemporary() { 2607314564Sdim return Reader->ReadCXXTemporary(*F, Record, Idx); 2608314564Sdim } 2609314564Sdim 2610341825Sdim /// Read a source location, advancing Idx. 2611314564Sdim SourceLocation readSourceLocation() { 2612314564Sdim return Reader->ReadSourceLocation(*F, Record, Idx); 2613314564Sdim } 2614314564Sdim 2615341825Sdim /// Read a source range, advancing Idx. 2616314564Sdim SourceRange readSourceRange() { 2617314564Sdim return Reader->ReadSourceRange(*F, Record, Idx); 2618314564Sdim } 2619314564Sdim 2620353358Sdim APValue readAPValue() { return Reader->ReadAPValue(Record, Idx); } 2621353358Sdim 2622341825Sdim /// Read an integral value, advancing Idx. 2623314564Sdim llvm::APInt readAPInt() { 2624314564Sdim return Reader->ReadAPInt(Record, Idx); 2625314564Sdim } 2626314564Sdim 2627341825Sdim /// Read a signed integral value, advancing Idx. 2628314564Sdim llvm::APSInt readAPSInt() { 2629314564Sdim return Reader->ReadAPSInt(Record, Idx); 2630314564Sdim } 2631314564Sdim 2632341825Sdim /// Read a floating-point value, advancing Idx. 2633314564Sdim llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem) { 2634314564Sdim return Reader->ReadAPFloat(Record, Sem,Idx); 2635314564Sdim } 2636314564Sdim 2637341825Sdim /// Read a string, advancing Idx. 2638314564Sdim std::string readString() { 2639314564Sdim return Reader->ReadString(Record, Idx); 2640314564Sdim } 2641314564Sdim 2642341825Sdim /// Read a path, advancing Idx. 2643314564Sdim std::string readPath() { 2644314564Sdim return Reader->ReadPath(*F, Record, Idx); 2645314564Sdim } 2646314564Sdim 2647341825Sdim /// Read a version tuple, advancing Idx. 2648314564Sdim VersionTuple readVersionTuple() { 2649314564Sdim return ASTReader::ReadVersionTuple(Record, Idx); 2650314564Sdim } 2651314564Sdim 2652344779Sdim /// Reads one attribute from the current stream position, advancing Idx. 2653344779Sdim Attr *readAttr() { 2654344779Sdim return Reader->ReadAttr(*F, Record, Idx); 2655344779Sdim } 2656344779Sdim 2657341825Sdim /// Reads attributes from the current stream position, advancing Idx. 2658314564Sdim void readAttributes(AttrVec &Attrs) { 2659321369Sdim return Reader->ReadAttributes(*this, Attrs); 2660314564Sdim } 2661314564Sdim 2662341825Sdim /// Reads a token out of a record, advancing Idx. 2663314564Sdim Token readToken() { 2664314564Sdim return Reader->ReadToken(*F, Record, Idx); 2665314564Sdim } 2666314564Sdim 2667314564Sdim void recordSwitchCaseID(SwitchCase *SC, unsigned ID) { 2668314564Sdim Reader->RecordSwitchCaseID(SC, ID); 2669314564Sdim } 2670314564Sdim 2671341825Sdim /// Retrieve the switch-case statement with the given ID. 2672314564Sdim SwitchCase *getSwitchCaseWithID(unsigned ID) { 2673314564Sdim return Reader->getSwitchCaseWithID(ID); 2674314564Sdim } 2675314564Sdim}; 2676314564Sdim 2677341825Sdim/// Helper class that saves the current stream position and 2678212795Sdim/// then restores it when destroyed. 2679212795Sdimstruct SavedStreamPosition { 2680212795Sdim explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor) 2681341825Sdim : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {} 2682212795Sdim 2683212795Sdim ~SavedStreamPosition() { 2684353358Sdim if (llvm::Error Err = Cursor.JumpToBit(Offset)) 2685353358Sdim llvm::report_fatal_error( 2686353358Sdim "Cursor should always be able to go back, failed: " + 2687353358Sdim toString(std::move(Err))); 2688212795Sdim } 2689212795Sdim 2690212795Sdimprivate: 2691212795Sdim llvm::BitstreamCursor &Cursor; 2692212795Sdim uint64_t Offset; 2693212795Sdim}; 2694212795Sdim 2695212795Sdiminline void PCHValidator::Error(const char *Msg) { 2696212795Sdim Reader.Error(Msg); 2697212795Sdim} 2698212795Sdim 2699344779Sdimclass OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 2700344779Sdim ASTRecordReader &Record; 2701344779Sdim ASTContext &Context; 2702344779Sdim 2703344779Sdimpublic: 2704344779Sdim OMPClauseReader(ASTRecordReader &Record) 2705344779Sdim : Record(Record), Context(Record.getContext()) {} 2706344779Sdim 2707344779Sdim#define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C); 2708344779Sdim#include "clang/Basic/OpenMPKinds.def" 2709344779Sdim OMPClause *readClause(); 2710344779Sdim void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 2711344779Sdim void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 2712344779Sdim}; 2713344779Sdim 2714327952Sdim} // namespace clang 2715212795Sdim 2716327952Sdim#endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H 2717