1 //===--- ASTReader.h - AST File Reader --------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ASTReader class, which reads AST files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTREADER_H
16
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Basic/FileSystemOptions.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Basic/Version.h"
26 #include "clang/Lex/ExternalPreprocessorSource.h"
27 #include "clang/Lex/HeaderSearch.h"
28 #include "clang/Lex/PreprocessingRecord.h"
29 #include "clang/Sema/ExternalSemaSource.h"
30 #include "clang/Serialization/ASTBitCodes.h"
31 #include "clang/Serialization/ContinuousRangeMap.h"
32 #include "clang/Serialization/Module.h"
33 #include "clang/Serialization/ModuleManager.h"
34 #include "llvm/ADT/APFloat.h"
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/ADT/APSInt.h"
37 #include "llvm/ADT/MapVector.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/SmallSet.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/ADT/TinyPtrVector.h"
43 #include "llvm/Bitcode/BitstreamReader.h"
44 #include "llvm/Support/DataTypes.h"
45 #include <deque>
46 #include <map>
47 #include <memory>
48 #include <string>
49 #include <utility>
50 #include <vector>
51
52 namespace llvm {
53 class MemoryBuffer;
54 }
55
56 namespace clang {
57
58 class AddrLabelExpr;
59 class ASTConsumer;
60 class ASTContext;
61 class ASTIdentifierIterator;
62 class ASTUnit; // FIXME: Layering violation and egregious hack.
63 class Attr;
64 class Decl;
65 class DeclContext;
66 class DefMacroDirective;
67 class DiagnosticOptions;
68 class NestedNameSpecifier;
69 class CXXBaseSpecifier;
70 class CXXConstructorDecl;
71 class CXXCtorInitializer;
72 class GlobalModuleIndex;
73 class GotoStmt;
74 class MacroDefinition;
75 class MacroDirective;
76 class NamedDecl;
77 class OpaqueValueExpr;
78 class Preprocessor;
79 class PreprocessorOptions;
80 class Sema;
81 class SwitchCase;
82 class ASTDeserializationListener;
83 class ASTWriter;
84 class ASTReader;
85 class ASTDeclReader;
86 class ASTStmtReader;
87 class TypeLocReader;
88 struct HeaderFileInfo;
89 class VersionTuple;
90 class TargetOptions;
91 class LazyASTUnresolvedSet;
92
93 /// \brief Abstract interface for callback invocations by the ASTReader.
94 ///
95 /// While reading an AST file, the ASTReader will call the methods of the
96 /// listener to pass on specific information. Some of the listener methods can
97 /// return true to indicate to the ASTReader that the information (and
98 /// consequently the AST file) is invalid.
99 class ASTReaderListener {
100 public:
101 virtual ~ASTReaderListener();
102
103 /// \brief Receives the full Clang version information.
104 ///
105 /// \returns true to indicate that the version is invalid. Subclasses should
106 /// generally defer to this implementation.
ReadFullVersionInformation(StringRef FullVersion)107 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
108 return FullVersion != getClangFullRepositoryVersion();
109 }
110
ReadModuleName(StringRef ModuleName)111 virtual void ReadModuleName(StringRef ModuleName) {}
ReadModuleMapFile(StringRef ModuleMapPath)112 virtual void ReadModuleMapFile(StringRef ModuleMapPath) {}
113
114 /// \brief Receives the language options.
115 ///
116 /// \returns true to indicate the options are invalid or false otherwise.
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)117 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
118 bool Complain,
119 bool AllowCompatibleDifferences) {
120 return false;
121 }
122
123 /// \brief Receives the target options.
124 ///
125 /// \returns true to indicate the target options are invalid, or false
126 /// otherwise.
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain)127 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
128 bool Complain) {
129 return false;
130 }
131
132 /// \brief Receives the diagnostic options.
133 ///
134 /// \returns true to indicate the diagnostic options are invalid, or false
135 /// otherwise.
136 virtual bool
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)137 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
138 bool Complain) {
139 return false;
140 }
141
142 /// \brief Receives the file system options.
143 ///
144 /// \returns true to indicate the file system options are invalid, or false
145 /// otherwise.
ReadFileSystemOptions(const FileSystemOptions & FSOpts,bool Complain)146 virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
147 bool Complain) {
148 return false;
149 }
150
151 /// \brief Receives the header search options.
152 ///
153 /// \returns true to indicate the header search options are invalid, or false
154 /// otherwise.
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,bool Complain)155 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
156 bool Complain) {
157 return false;
158 }
159
160 /// \brief Receives the preprocessor options.
161 ///
162 /// \param SuggestedPredefines Can be filled in with the set of predefines
163 /// that are suggested by the preprocessor options. Typically only used when
164 /// loading a precompiled header.
165 ///
166 /// \returns true to indicate the preprocessor options are invalid, or false
167 /// otherwise.
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)168 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
169 bool Complain,
170 std::string &SuggestedPredefines) {
171 return false;
172 }
173
174 /// \brief Receives __COUNTER__ value.
ReadCounter(const serialization::ModuleFile & M,unsigned Value)175 virtual void ReadCounter(const serialization::ModuleFile &M,
176 unsigned Value) {}
177
178 /// This is called for each AST file loaded.
visitModuleFile(StringRef Filename)179 virtual void visitModuleFile(StringRef Filename) {}
180
181 /// \brief Returns true if this \c ASTReaderListener wants to receive the
182 /// input files of the AST file via \c visitInputFile, false otherwise.
needsInputFileVisitation()183 virtual bool needsInputFileVisitation() { return false; }
184 /// \brief Returns true if this \c ASTReaderListener wants to receive the
185 /// system input files of the AST file via \c visitInputFile, false otherwise.
needsSystemInputFileVisitation()186 virtual bool needsSystemInputFileVisitation() { return false; }
187 /// \brief if \c needsInputFileVisitation returns true, this is called for
188 /// each non-system input file of the AST File. If
189 /// \c needsSystemInputFileVisitation is true, then it is called for all
190 /// system input files as well.
191 ///
192 /// \returns true to continue receiving the next input file, false to stop.
visitInputFile(StringRef Filename,bool isSystem,bool isOverridden)193 virtual bool visitInputFile(StringRef Filename, bool isSystem,
194 bool isOverridden) {
195 return true;
196 }
197
198 /// \brief Returns true if this \c ASTReaderListener wants to receive the
199 /// imports of the AST file via \c visitImport, false otherwise.
needsImportVisitation()200 virtual bool needsImportVisitation() const { return false; }
201 /// \brief If needsImportVisitation returns \c true, this is called for each
202 /// AST file imported by this AST file.
visitImport(StringRef Filename)203 virtual void visitImport(StringRef Filename) {}
204 };
205
206 /// \brief Simple wrapper class for chaining listeners.
207 class ChainedASTReaderListener : public ASTReaderListener {
208 std::unique_ptr<ASTReaderListener> First;
209 std::unique_ptr<ASTReaderListener> Second;
210
211 public:
212 /// Takes ownership of \p First and \p Second.
ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First,std::unique_ptr<ASTReaderListener> Second)213 ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First,
214 std::unique_ptr<ASTReaderListener> Second)
215 : First(std::move(First)), Second(std::move(Second)) {}
216
takeFirst()217 std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); }
takeSecond()218 std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); }
219
220 bool ReadFullVersionInformation(StringRef FullVersion) override;
221 void ReadModuleName(StringRef ModuleName) override;
222 void ReadModuleMapFile(StringRef ModuleMapPath) override;
223 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
224 bool AllowCompatibleDifferences) override;
225 bool ReadTargetOptions(const TargetOptions &TargetOpts,
226 bool Complain) override;
227 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
228 bool Complain) override;
229 bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
230 bool Complain) override;
231
232 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
233 bool Complain) override;
234 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
235 bool Complain,
236 std::string &SuggestedPredefines) override;
237
238 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
239 bool needsInputFileVisitation() override;
240 bool needsSystemInputFileVisitation() override;
241 void visitModuleFile(StringRef Filename) override;
242 bool visitInputFile(StringRef Filename, bool isSystem,
243 bool isOverridden) override;
244 };
245
246 /// \brief ASTReaderListener implementation to validate the information of
247 /// the PCH file against an initialized Preprocessor.
248 class PCHValidator : public ASTReaderListener {
249 Preprocessor &PP;
250 ASTReader &Reader;
251
252 public:
PCHValidator(Preprocessor & PP,ASTReader & Reader)253 PCHValidator(Preprocessor &PP, ASTReader &Reader)
254 : PP(PP), Reader(Reader) {}
255
256 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
257 bool AllowCompatibleDifferences) override;
258 bool ReadTargetOptions(const TargetOptions &TargetOpts,
259 bool Complain) override;
260 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
261 bool Complain) override;
262 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
263 std::string &SuggestedPredefines) override;
264 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
265
266 private:
267 void Error(const char *Msg);
268 };
269
270 namespace serialization {
271
272 class ReadMethodPoolVisitor;
273
274 namespace reader {
275 class ASTIdentifierLookupTrait;
276 /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
277 typedef llvm::OnDiskIterableChainedHashTable<ASTDeclContextNameLookupTrait>
278 ASTDeclContextNameLookupTable;
279 }
280
281 } // end namespace serialization
282
283 /// \brief Reads an AST files chain containing the contents of a translation
284 /// unit.
285 ///
286 /// The ASTReader class reads bitstreams (produced by the ASTWriter
287 /// class) containing the serialized representation of a given
288 /// abstract syntax tree and its supporting data structures. An
289 /// instance of the ASTReader can be attached to an ASTContext object,
290 /// which will provide access to the contents of the AST files.
291 ///
292 /// The AST reader provides lazy de-serialization of declarations, as
293 /// required when traversing the AST. Only those AST nodes that are
294 /// actually required will be de-serialized.
295 class ASTReader
296 : public ExternalPreprocessorSource,
297 public ExternalPreprocessingRecordSource,
298 public ExternalHeaderFileInfoSource,
299 public ExternalSemaSource,
300 public IdentifierInfoLookup,
301 public ExternalIdentifierLookup,
302 public ExternalSLocEntrySource
303 {
304 public:
305 typedef SmallVector<uint64_t, 64> RecordData;
306 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
307
308 /// \brief The result of reading the control block of an AST file, which
309 /// can fail for various reasons.
310 enum ASTReadResult {
311 /// \brief The control block was read successfully. Aside from failures,
312 /// the AST file is safe to read into the current context.
313 Success,
314 /// \brief The AST file itself appears corrupted.
315 Failure,
316 /// \brief The AST file was missing.
317 Missing,
318 /// \brief The AST file is out-of-date relative to its input files,
319 /// and needs to be regenerated.
320 OutOfDate,
321 /// \brief The AST file was written by a different version of Clang.
322 VersionMismatch,
323 /// \brief The AST file was writtten with a different language/target
324 /// configuration.
325 ConfigurationMismatch,
326 /// \brief The AST file has errors.
327 HadErrors
328 };
329
330 /// \brief Types of AST files.
331 friend class PCHValidator;
332 friend class ASTDeclReader;
333 friend class ASTStmtReader;
334 friend class ASTIdentifierIterator;
335 friend class serialization::reader::ASTIdentifierLookupTrait;
336 friend class TypeLocReader;
337 friend class ASTWriter;
338 friend class ASTUnit; // ASTUnit needs to remap source locations.
339 friend class serialization::ReadMethodPoolVisitor;
340
341 typedef serialization::ModuleFile ModuleFile;
342 typedef serialization::ModuleKind ModuleKind;
343 typedef serialization::ModuleManager ModuleManager;
344
345 typedef ModuleManager::ModuleIterator ModuleIterator;
346 typedef ModuleManager::ModuleConstIterator ModuleConstIterator;
347 typedef ModuleManager::ModuleReverseIterator ModuleReverseIterator;
348
349 private:
350 /// \brief The receiver of some callbacks invoked by ASTReader.
351 std::unique_ptr<ASTReaderListener> Listener;
352
353 /// \brief The receiver of deserialization events.
354 ASTDeserializationListener *DeserializationListener;
355 bool OwnsDeserializationListener;
356
357 SourceManager &SourceMgr;
358 FileManager &FileMgr;
359 DiagnosticsEngine &Diags;
360
361 /// \brief The semantic analysis object that will be processing the
362 /// AST files and the translation unit that uses it.
363 Sema *SemaObj;
364
365 /// \brief The preprocessor that will be loading the source file.
366 Preprocessor &PP;
367
368 /// \brief The AST context into which we'll read the AST files.
369 ASTContext &Context;
370
371 /// \brief The AST consumer.
372 ASTConsumer *Consumer;
373
374 /// \brief The module manager which manages modules and their dependencies
375 ModuleManager ModuleMgr;
376
377 /// \brief The location where the module file will be considered as
378 /// imported from. For non-module AST types it should be invalid.
379 SourceLocation CurrentImportLoc;
380
381 /// \brief The global module index, if loaded.
382 std::unique_ptr<GlobalModuleIndex> GlobalIndex;
383
384 /// \brief A map of global bit offsets to the module that stores entities
385 /// at those bit offsets.
386 ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
387
388 /// \brief A map of negated SLocEntryIDs to the modules containing them.
389 ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
390
391 typedef ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocOffsetMapType;
392
393 /// \brief A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
394 /// SourceLocation offsets to the modules containing them.
395 GlobalSLocOffsetMapType GlobalSLocOffsetMap;
396
397 /// \brief Types that have already been loaded from the chain.
398 ///
399 /// When the pointer at index I is non-NULL, the type with
400 /// ID = (I + 1) << FastQual::Width has already been loaded
401 std::vector<QualType> TypesLoaded;
402
403 typedef ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>
404 GlobalTypeMapType;
405
406 /// \brief Mapping from global type IDs to the module in which the
407 /// type resides along with the offset that should be added to the
408 /// global type ID to produce a local ID.
409 GlobalTypeMapType GlobalTypeMap;
410
411 /// \brief Declarations that have already been loaded from the chain.
412 ///
413 /// When the pointer at index I is non-NULL, the declaration with ID
414 /// = I + 1 has already been loaded.
415 std::vector<Decl *> DeclsLoaded;
416
417 typedef ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>
418 GlobalDeclMapType;
419
420 /// \brief Mapping from global declaration IDs to the module in which the
421 /// declaration resides.
422 GlobalDeclMapType GlobalDeclMap;
423
424 typedef std::pair<ModuleFile *, uint64_t> FileOffset;
425 typedef SmallVector<FileOffset, 2> FileOffsetsTy;
426 typedef llvm::DenseMap<serialization::DeclID, FileOffsetsTy>
427 DeclUpdateOffsetsMap;
428
429 /// \brief Declarations that have modifications residing in a later file
430 /// in the chain.
431 DeclUpdateOffsetsMap DeclUpdateOffsets;
432
433 /// \brief Declaration updates for already-loaded declarations that we need
434 /// to apply once we finish processing an import.
435 llvm::SmallVector<std::pair<serialization::GlobalDeclID, Decl*>, 16>
436 PendingUpdateRecords;
437
438 struct ReplacedDeclInfo {
439 ModuleFile *Mod;
440 uint64_t Offset;
441 unsigned RawLoc;
442
ReplacedDeclInfoReplacedDeclInfo443 ReplacedDeclInfo() : Mod(nullptr), Offset(0), RawLoc(0) {}
ReplacedDeclInfoReplacedDeclInfo444 ReplacedDeclInfo(ModuleFile *Mod, uint64_t Offset, unsigned RawLoc)
445 : Mod(Mod), Offset(Offset), RawLoc(RawLoc) {}
446 };
447
448 typedef llvm::DenseMap<serialization::DeclID, ReplacedDeclInfo>
449 DeclReplacementMap;
450 /// \brief Declarations that have been replaced in a later file in the chain.
451 DeclReplacementMap ReplacedDecls;
452
453 /// \brief Declarations that have been imported and have typedef names for
454 /// linkage purposes.
455 llvm::DenseMap<std::pair<DeclContext*, IdentifierInfo*>, NamedDecl*>
456 ImportedTypedefNamesForLinkage;
457
458 /// \brief Mergeable declaration contexts that have anonymous declarations
459 /// within them, and those anonymous declarations.
460 llvm::DenseMap<DeclContext*, llvm::SmallVector<NamedDecl*, 2>>
461 AnonymousDeclarationsForMerging;
462
463 struct FileDeclsInfo {
464 ModuleFile *Mod;
465 ArrayRef<serialization::LocalDeclID> Decls;
466
FileDeclsInfoFileDeclsInfo467 FileDeclsInfo() : Mod(nullptr) {}
FileDeclsInfoFileDeclsInfo468 FileDeclsInfo(ModuleFile *Mod, ArrayRef<serialization::LocalDeclID> Decls)
469 : Mod(Mod), Decls(Decls) {}
470 };
471
472 /// \brief Map from a FileID to the file-level declarations that it contains.
473 llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
474
475 // Updates for visible decls can occur for other contexts than just the
476 // TU, and when we read those update records, the actual context will not
477 // be available yet (unless it's the TU), so have this pending map using the
478 // ID as a key. It will be realized when the context is actually loaded.
479 typedef
480 SmallVector<std::pair<serialization::reader::ASTDeclContextNameLookupTable *,
481 ModuleFile*>, 1> DeclContextVisibleUpdates;
482 typedef llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
483 DeclContextVisibleUpdatesPending;
484
485 /// \brief Updates to the visible declarations of declaration contexts that
486 /// haven't been loaded yet.
487 DeclContextVisibleUpdatesPending PendingVisibleUpdates;
488
489 /// \brief The set of C++ or Objective-C classes that have forward
490 /// declarations that have not yet been linked to their definitions.
491 llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
492
493 typedef llvm::MapVector<Decl *, uint64_t,
494 llvm::SmallDenseMap<Decl *, unsigned, 4>,
495 SmallVector<std::pair<Decl *, uint64_t>, 4> >
496 PendingBodiesMap;
497
498 /// \brief Functions or methods that have bodies that will be attached.
499 PendingBodiesMap PendingBodies;
500
501 /// \brief Read the records that describe the contents of declcontexts.
502 bool ReadDeclContextStorage(ModuleFile &M,
503 llvm::BitstreamCursor &Cursor,
504 const std::pair<uint64_t, uint64_t> &Offsets,
505 serialization::DeclContextInfo &Info);
506
507 /// \brief A vector containing identifiers that have already been
508 /// loaded.
509 ///
510 /// If the pointer at index I is non-NULL, then it refers to the
511 /// IdentifierInfo for the identifier with ID=I+1 that has already
512 /// been loaded.
513 std::vector<IdentifierInfo *> IdentifiersLoaded;
514
515 typedef ContinuousRangeMap<serialization::IdentID, ModuleFile *, 4>
516 GlobalIdentifierMapType;
517
518 /// \brief Mapping from global identifier IDs to the module in which the
519 /// identifier resides along with the offset that should be added to the
520 /// global identifier ID to produce a local ID.
521 GlobalIdentifierMapType GlobalIdentifierMap;
522
523 /// \brief A vector containing macros that have already been
524 /// loaded.
525 ///
526 /// If the pointer at index I is non-NULL, then it refers to the
527 /// MacroInfo for the identifier with ID=I+1 that has already
528 /// been loaded.
529 std::vector<MacroInfo *> MacrosLoaded;
530
531 typedef ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>
532 GlobalMacroMapType;
533
534 /// \brief Mapping from global macro IDs to the module in which the
535 /// macro resides along with the offset that should be added to the
536 /// global macro ID to produce a local ID.
537 GlobalMacroMapType GlobalMacroMap;
538
539 /// \brief A vector containing submodules that have already been loaded.
540 ///
541 /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
542 /// indicate that the particular submodule ID has not yet been loaded.
543 SmallVector<Module *, 2> SubmodulesLoaded;
544
545 typedef ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>
546 GlobalSubmoduleMapType;
547
548 /// \brief Mapping from global submodule IDs to the module file in which the
549 /// submodule resides along with the offset that should be added to the
550 /// global submodule ID to produce a local ID.
551 GlobalSubmoduleMapType GlobalSubmoduleMap;
552
553 /// \brief Information on a macro definition or undefinition that is visible
554 /// at the end of a submodule.
555 struct ModuleMacroInfo;
556
557 /// \brief An entity that has been hidden.
558 class HiddenName {
559 public:
560 enum NameKind {
561 Declaration,
562 Macro
563 } Kind;
564
565 private:
566 union {
567 Decl *D;
568 ModuleMacroInfo *MMI;
569 };
570
571 IdentifierInfo *Id;
572
573 public:
HiddenName(Decl * D)574 HiddenName(Decl *D) : Kind(Declaration), D(D), Id() { }
575
HiddenName(IdentifierInfo * II,ModuleMacroInfo * MMI)576 HiddenName(IdentifierInfo *II, ModuleMacroInfo *MMI)
577 : Kind(Macro), MMI(MMI), Id(II) { }
578
getKind()579 NameKind getKind() const { return Kind; }
580
getDecl()581 Decl *getDecl() const {
582 assert(getKind() == Declaration && "Hidden name is not a declaration");
583 return D;
584 }
585
getMacro()586 std::pair<IdentifierInfo *, ModuleMacroInfo *> getMacro() const {
587 assert(getKind() == Macro && "Hidden name is not a macro!");
588 return std::make_pair(Id, MMI);
589 }
590 };
591
592 typedef llvm::SmallDenseMap<IdentifierInfo*,
593 ModuleMacroInfo*> HiddenMacrosMap;
594
595 /// \brief A set of hidden declarations.
596 struct HiddenNames {
597 SmallVector<Decl*, 2> HiddenDecls;
598 HiddenMacrosMap HiddenMacros;
599 };
600
601 typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
602
603 /// \brief A mapping from each of the hidden submodules to the deserialized
604 /// declarations in that submodule that could be made visible.
605 HiddenNamesMapType HiddenNamesMap;
606
607
608 /// \brief A module import, export, or conflict that hasn't yet been resolved.
609 struct UnresolvedModuleRef {
610 /// \brief The file in which this module resides.
611 ModuleFile *File;
612
613 /// \brief The module that is importing or exporting.
614 Module *Mod;
615
616 /// \brief The kind of module reference.
617 enum { Import, Export, Conflict } Kind;
618
619 /// \brief The local ID of the module that is being exported.
620 unsigned ID;
621
622 /// \brief Whether this is a wildcard export.
623 unsigned IsWildcard : 1;
624
625 /// \brief String data.
626 StringRef String;
627 };
628
629 /// \brief The set of module imports and exports that still need to be
630 /// resolved.
631 SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
632
633 /// \brief A vector containing selectors that have already been loaded.
634 ///
635 /// This vector is indexed by the Selector ID (-1). NULL selector
636 /// entries indicate that the particular selector ID has not yet
637 /// been loaded.
638 SmallVector<Selector, 16> SelectorsLoaded;
639
640 typedef ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>
641 GlobalSelectorMapType;
642
643 /// \brief Mapping from global selector IDs to the module in which the
644
645 /// global selector ID to produce a local ID.
646 GlobalSelectorMapType GlobalSelectorMap;
647
648 /// \brief The generation number of the last time we loaded data from the
649 /// global method pool for this selector.
650 llvm::DenseMap<Selector, unsigned> SelectorGeneration;
651
652 struct PendingMacroInfo {
653 ModuleFile *M;
654
655 struct ModuleMacroDataTy {
656 uint32_t MacID;
657 serialization::SubmoduleID *Overrides;
658 };
659 struct PCHMacroDataTy {
660 uint64_t MacroDirectivesOffset;
661 };
662
663 union {
664 ModuleMacroDataTy ModuleMacroData;
665 PCHMacroDataTy PCHMacroData;
666 };
667
PendingMacroInfoPendingMacroInfo668 PendingMacroInfo(ModuleFile *M,
669 uint32_t MacID,
670 serialization::SubmoduleID *Overrides) : M(M) {
671 ModuleMacroData.MacID = MacID;
672 ModuleMacroData.Overrides = Overrides;
673 }
674
PendingMacroInfoPendingMacroInfo675 PendingMacroInfo(ModuleFile *M, uint64_t MacroDirectivesOffset) : M(M) {
676 PCHMacroData.MacroDirectivesOffset = MacroDirectivesOffset;
677 }
678 };
679
680 typedef llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2> >
681 PendingMacroIDsMap;
682
683 /// \brief Mapping from identifiers that have a macro history to the global
684 /// IDs have not yet been deserialized to the global IDs of those macros.
685 PendingMacroIDsMap PendingMacroIDs;
686
687 typedef ContinuousRangeMap<unsigned, ModuleFile *, 4>
688 GlobalPreprocessedEntityMapType;
689
690 /// \brief Mapping from global preprocessing entity IDs to the module in
691 /// which the preprocessed entity resides along with the offset that should be
692 /// added to the global preprocessing entitiy ID to produce a local ID.
693 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
694
695 /// \name CodeGen-relevant special data
696 /// \brief Fields containing data that is relevant to CodeGen.
697 //@{
698
699 /// \brief The IDs of all declarations that fulfill the criteria of
700 /// "interesting" decls.
701 ///
702 /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
703 /// in the chain. The referenced declarations are deserialized and passed to
704 /// the consumer eagerly.
705 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
706
707 /// \brief The IDs of all tentative definitions stored in the chain.
708 ///
709 /// Sema keeps track of all tentative definitions in a TU because it has to
710 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
711 /// the PCH chain must be eagerly deserialized.
712 SmallVector<uint64_t, 16> TentativeDefinitions;
713
714 /// \brief The IDs of all CXXRecordDecls stored in the chain whose VTables are
715 /// used.
716 ///
717 /// CodeGen has to emit VTables for these records, so they have to be eagerly
718 /// deserialized.
719 SmallVector<uint64_t, 64> VTableUses;
720
721 /// \brief A snapshot of the pending instantiations in the chain.
722 ///
723 /// This record tracks the instantiations that Sema has to perform at the
724 /// end of the TU. It consists of a pair of values for every pending
725 /// instantiation where the first value is the ID of the decl and the second
726 /// is the instantiation location.
727 SmallVector<uint64_t, 64> PendingInstantiations;
728
729 //@}
730
731 /// \name DiagnosticsEngine-relevant special data
732 /// \brief Fields containing data that is used for generating diagnostics
733 //@{
734
735 /// \brief A snapshot of Sema's unused file-scoped variable tracking, for
736 /// generating warnings.
737 SmallVector<uint64_t, 16> UnusedFileScopedDecls;
738
739 /// \brief A list of all the delegating constructors we've seen, to diagnose
740 /// cycles.
741 SmallVector<uint64_t, 4> DelegatingCtorDecls;
742
743 /// \brief Method selectors used in a @selector expression. Used for
744 /// implementation of -Wselector.
745 SmallVector<uint64_t, 64> ReferencedSelectorsData;
746
747 /// \brief A snapshot of Sema's weak undeclared identifier tracking, for
748 /// generating warnings.
749 SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;
750
751 /// \brief The IDs of type aliases for ext_vectors that exist in the chain.
752 ///
753 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
754 SmallVector<uint64_t, 4> ExtVectorDecls;
755
756 //@}
757
758 /// \name Sema-relevant special data
759 /// \brief Fields containing data that is used for semantic analysis
760 //@{
761
762 /// \brief The IDs of all locally scoped extern "C" decls in the chain.
763 ///
764 /// Sema tracks these to validate that the types are consistent across all
765 /// local extern "C" declarations.
766 SmallVector<uint64_t, 16> LocallyScopedExternCDecls;
767
768 /// \brief The IDs of all dynamic class declarations in the chain.
769 ///
770 /// Sema tracks these because it checks for the key functions being defined
771 /// at the end of the TU, in which case it directs CodeGen to emit the VTable.
772 SmallVector<uint64_t, 16> DynamicClasses;
773
774 /// \brief The IDs of all potentially unused typedef names in the chain.
775 ///
776 /// Sema tracks these to emit warnings.
777 SmallVector<uint64_t, 16> UnusedLocalTypedefNameCandidates;
778
779 /// \brief The IDs of the declarations Sema stores directly.
780 ///
781 /// Sema tracks a few important decls, such as namespace std, directly.
782 SmallVector<uint64_t, 4> SemaDeclRefs;
783
784 /// \brief The IDs of the types ASTContext stores directly.
785 ///
786 /// The AST context tracks a few important types, such as va_list, directly.
787 SmallVector<uint64_t, 16> SpecialTypes;
788
789 /// \brief The IDs of CUDA-specific declarations ASTContext stores directly.
790 ///
791 /// The AST context tracks a few important decls, currently cudaConfigureCall,
792 /// directly.
793 SmallVector<uint64_t, 2> CUDASpecialDeclRefs;
794
795 /// \brief The floating point pragma option settings.
796 SmallVector<uint64_t, 1> FPPragmaOptions;
797
798 /// \brief The pragma clang optimize location (if the pragma state is "off").
799 SourceLocation OptimizeOffPragmaLocation;
800
801 /// \brief The OpenCL extension settings.
802 SmallVector<uint64_t, 1> OpenCLExtensions;
803
804 /// \brief A list of the namespaces we've seen.
805 SmallVector<uint64_t, 4> KnownNamespaces;
806
807 /// \brief A list of undefined decls with internal linkage followed by the
808 /// SourceLocation of a matching ODR-use.
809 SmallVector<uint64_t, 8> UndefinedButUsed;
810
811 // \brief A list of late parsed template function data.
812 SmallVector<uint64_t, 1> LateParsedTemplates;
813
814 struct ImportedSubmodule {
815 serialization::SubmoduleID ID;
816 SourceLocation ImportLoc;
817
ImportedSubmoduleImportedSubmodule818 ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc)
819 : ID(ID), ImportLoc(ImportLoc) {}
820 };
821
822 /// \brief A list of modules that were imported by precompiled headers or
823 /// any other non-module AST file.
824 SmallVector<ImportedSubmodule, 2> ImportedModules;
825 //@}
826
827 /// \brief The directory that the PCH we are reading is stored in.
828 std::string CurrentDir;
829
830 /// \brief The system include root to be used when loading the
831 /// precompiled header.
832 std::string isysroot;
833
834 /// \brief Whether to disable the normal validation performed on precompiled
835 /// headers when they are loaded.
836 bool DisableValidation;
837
838 /// \brief Whether to accept an AST file with compiler errors.
839 bool AllowASTWithCompilerErrors;
840
841 /// \brief Whether to accept an AST file that has a different configuration
842 /// from the current compiler instance.
843 bool AllowConfigurationMismatch;
844
845 /// \brief Whether validate system input files.
846 bool ValidateSystemInputs;
847
848 /// \brief Whether we are allowed to use the global module index.
849 bool UseGlobalIndex;
850
851 /// \brief Whether we have tried loading the global module index yet.
852 bool TriedLoadingGlobalIndex;
853
854 typedef llvm::DenseMap<unsigned, SwitchCase *> SwitchCaseMapTy;
855 /// \brief Mapping from switch-case IDs in the chain to switch-case statements
856 ///
857 /// Statements usually don't have IDs, but switch cases need them, so that the
858 /// switch statement can refer to them.
859 SwitchCaseMapTy SwitchCaseStmts;
860
861 SwitchCaseMapTy *CurrSwitchCaseStmts;
862
863 /// \brief The number of source location entries de-serialized from
864 /// the PCH file.
865 unsigned NumSLocEntriesRead;
866
867 /// \brief The number of source location entries in the chain.
868 unsigned TotalNumSLocEntries;
869
870 /// \brief The number of statements (and expressions) de-serialized
871 /// from the chain.
872 unsigned NumStatementsRead;
873
874 /// \brief The total number of statements (and expressions) stored
875 /// in the chain.
876 unsigned TotalNumStatements;
877
878 /// \brief The number of macros de-serialized from the chain.
879 unsigned NumMacrosRead;
880
881 /// \brief The total number of macros stored in the chain.
882 unsigned TotalNumMacros;
883
884 /// \brief The number of lookups into identifier tables.
885 unsigned NumIdentifierLookups;
886
887 /// \brief The number of lookups into identifier tables that succeed.
888 unsigned NumIdentifierLookupHits;
889
890 /// \brief The number of selectors that have been read.
891 unsigned NumSelectorsRead;
892
893 /// \brief The number of method pool entries that have been read.
894 unsigned NumMethodPoolEntriesRead;
895
896 /// \brief The number of times we have looked up a selector in the method
897 /// pool.
898 unsigned NumMethodPoolLookups;
899
900 /// \brief The number of times we have looked up a selector in the method
901 /// pool and found something.
902 unsigned NumMethodPoolHits;
903
904 /// \brief The number of times we have looked up a selector in the method
905 /// pool within a specific module.
906 unsigned NumMethodPoolTableLookups;
907
908 /// \brief The number of times we have looked up a selector in the method
909 /// pool within a specific module and found something.
910 unsigned NumMethodPoolTableHits;
911
912 /// \brief The total number of method pool entries in the selector table.
913 unsigned TotalNumMethodPoolEntries;
914
915 /// Number of lexical decl contexts read/total.
916 unsigned NumLexicalDeclContextsRead, TotalLexicalDeclContexts;
917
918 /// Number of visible decl contexts read/total.
919 unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
920
921 /// Total size of modules, in bits, currently loaded
922 uint64_t TotalModulesSizeInBits;
923
924 /// \brief Number of Decl/types that are currently deserializing.
925 unsigned NumCurrentElementsDeserializing;
926
927 /// \brief Set true while we are in the process of passing deserialized
928 /// "interesting" decls to consumer inside FinishedDeserializing().
929 /// This is used as a guard to avoid recursively repeating the process of
930 /// passing decls to consumer.
931 bool PassingDeclsToConsumer;
932
933 /// Number of CXX base specifiers currently loaded
934 unsigned NumCXXBaseSpecifiersLoaded;
935
936 /// \brief The set of identifiers that were read while the AST reader was
937 /// (recursively) loading declarations.
938 ///
939 /// The declarations on the identifier chain for these identifiers will be
940 /// loaded once the recursive loading has completed.
941 llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4> >
942 PendingIdentifierInfos;
943
944 /// \brief The generation number of each identifier, which keeps track of
945 /// the last time we loaded information about this identifier.
946 llvm::DenseMap<IdentifierInfo *, unsigned> IdentifierGeneration;
947
948 /// \brief Contains declarations and definitions that will be
949 /// "interesting" to the ASTConsumer, when we get that AST consumer.
950 ///
951 /// "Interesting" declarations are those that have data that may
952 /// need to be emitted, such as inline function definitions or
953 /// Objective-C protocols.
954 std::deque<Decl *> InterestingDecls;
955
956 /// \brief The set of redeclarable declarations that have been deserialized
957 /// since the last time the declaration chains were linked.
958 llvm::SmallPtrSet<Decl *, 16> RedeclsDeserialized;
959
960 /// \brief The list of redeclaration chains that still need to be
961 /// reconstructed.
962 ///
963 /// Each element is the global declaration ID of the first declaration in
964 /// the chain. Elements in this vector should be unique; use
965 /// PendingDeclChainsKnown to ensure uniqueness.
966 SmallVector<serialization::DeclID, 16> PendingDeclChains;
967
968 /// \brief Keeps track of the elements added to PendingDeclChains.
969 llvm::SmallSet<serialization::DeclID, 16> PendingDeclChainsKnown;
970
971 /// \brief The list of canonical declarations whose redeclaration chains
972 /// need to be marked as incomplete once we're done deserializing things.
973 SmallVector<Decl *, 16> PendingIncompleteDeclChains;
974
975 /// \brief The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
976 /// been loaded but its DeclContext was not set yet.
977 struct PendingDeclContextInfo {
978 Decl *D;
979 serialization::GlobalDeclID SemaDC;
980 serialization::GlobalDeclID LexicalDC;
981 };
982
983 /// \brief The set of Decls that have been loaded but their DeclContexts are
984 /// not set yet.
985 ///
986 /// The DeclContexts for these Decls will be set once recursive loading has
987 /// been completed.
988 std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
989
990 /// \brief The set of NamedDecls that have been loaded, but are members of a
991 /// context that has been merged into another context where the corresponding
992 /// declaration is either missing or has not yet been loaded.
993 ///
994 /// We will check whether the corresponding declaration is in fact missing
995 /// once recursing loading has been completed.
996 llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
997
998 /// \brief Record definitions in which we found an ODR violation.
999 llvm::SmallDenseMap<CXXRecordDecl *, llvm::TinyPtrVector<CXXRecordDecl *>, 2>
1000 PendingOdrMergeFailures;
1001
1002 /// \brief DeclContexts in which we have diagnosed an ODR violation.
1003 llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures;
1004
1005 /// \brief The set of Objective-C categories that have been deserialized
1006 /// since the last time the declaration chains were linked.
1007 llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
1008
1009 /// \brief The set of Objective-C class definitions that have already been
1010 /// loaded, for which we will need to check for categories whenever a new
1011 /// module is loaded.
1012 SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
1013
1014 /// \brief A mapping from a primary context for a declaration chain to the
1015 /// other declarations of that entity that also have name lookup tables.
1016 /// Used when we merge together two class definitions that have different
1017 /// sets of declared special member functions.
1018 llvm::DenseMap<const DeclContext*, SmallVector<const DeclContext*, 2>>
1019 MergedLookups;
1020
1021 typedef llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2> >
1022 MergedDeclsMap;
1023
1024 /// \brief A mapping from canonical declarations to the set of additional
1025 /// (global, previously-canonical) declaration IDs that have been merged with
1026 /// that canonical declaration.
1027 MergedDeclsMap MergedDecls;
1028
1029 typedef llvm::DenseMap<serialization::GlobalDeclID,
1030 SmallVector<serialization::DeclID, 2> >
1031 StoredMergedDeclsMap;
1032
1033 /// \brief A mapping from canonical declaration IDs to the set of additional
1034 /// declaration IDs that have been merged with that canonical declaration.
1035 ///
1036 /// This is the deserialized representation of the entries in MergedDecls.
1037 /// When we query entries in MergedDecls, they will be augmented with entries
1038 /// from StoredMergedDecls.
1039 StoredMergedDeclsMap StoredMergedDecls;
1040
1041 /// \brief Combine the stored merged declarations for the given canonical
1042 /// declaration into the set of merged declarations.
1043 ///
1044 /// \returns An iterator into MergedDecls that corresponds to the position of
1045 /// the given canonical declaration.
1046 MergedDeclsMap::iterator
1047 combineStoredMergedDecls(Decl *Canon, serialization::GlobalDeclID CanonID);
1048
1049 /// \brief A mapping from DeclContexts to the semantic DeclContext that we
1050 /// are treating as the definition of the entity. This is used, for instance,
1051 /// when merging implicit instantiations of class templates across modules.
1052 llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
1053
1054 /// \brief A mapping from canonical declarations of enums to their canonical
1055 /// definitions. Only populated when using modules in C++.
1056 llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
1057
1058 /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
1059 SmallVector<Stmt *, 16> StmtStack;
1060
1061 /// \brief What kind of records we are reading.
1062 enum ReadingKind {
1063 Read_None, Read_Decl, Read_Type, Read_Stmt
1064 };
1065
1066 /// \brief What kind of records we are reading.
1067 ReadingKind ReadingKind;
1068
1069 /// \brief RAII object to change the reading kind.
1070 class ReadingKindTracker {
1071 ASTReader &Reader;
1072 enum ReadingKind PrevKind;
1073
1074 ReadingKindTracker(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
1075 void operator=(const ReadingKindTracker &) LLVM_DELETED_FUNCTION;
1076
1077 public:
ReadingKindTracker(enum ReadingKind newKind,ASTReader & reader)1078 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
1079 : Reader(reader), PrevKind(Reader.ReadingKind) {
1080 Reader.ReadingKind = newKind;
1081 }
1082
~ReadingKindTracker()1083 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
1084 };
1085
1086 /// \brief Suggested contents of the predefines buffer, after this
1087 /// PCH file has been processed.
1088 ///
1089 /// In most cases, this string will be empty, because the predefines
1090 /// buffer computed to build the PCH file will be identical to the
1091 /// predefines buffer computed from the command line. However, when
1092 /// there are differences that the PCH reader can work around, this
1093 /// predefines buffer may contain additional definitions.
1094 std::string SuggestedPredefines;
1095
1096 /// \brief Reads a statement from the specified cursor.
1097 Stmt *ReadStmtFromStream(ModuleFile &F);
1098
1099 struct InputFileInfo {
1100 std::string Filename;
1101 off_t StoredSize;
1102 time_t StoredTime;
1103 bool Overridden;
1104 };
1105
1106 /// \brief Reads the stored information about an input file.
1107 InputFileInfo readInputFileInfo(ModuleFile &F, unsigned ID);
1108 /// \brief A convenience method to read the filename from an input file.
1109 std::string getInputFileName(ModuleFile &F, unsigned ID);
1110
1111 /// \brief Retrieve the file entry and 'overridden' bit for an input
1112 /// file in the given module file.
1113 serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
1114 bool Complain = true);
1115
1116 public:
1117 void ResolveImportedPath(ModuleFile &M, std::string &Filename);
1118 static void ResolveImportedPath(std::string &Filename, StringRef Prefix);
1119
1120 private:
1121 struct ImportedModule {
1122 ModuleFile *Mod;
1123 ModuleFile *ImportedBy;
1124 SourceLocation ImportLoc;
1125
ImportedModuleImportedModule1126 ImportedModule(ModuleFile *Mod,
1127 ModuleFile *ImportedBy,
1128 SourceLocation ImportLoc)
1129 : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) { }
1130 };
1131
1132 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
1133 SourceLocation ImportLoc, ModuleFile *ImportedBy,
1134 SmallVectorImpl<ImportedModule> &Loaded,
1135 off_t ExpectedSize, time_t ExpectedModTime,
1136 serialization::ASTFileSignature ExpectedSignature,
1137 unsigned ClientLoadCapabilities);
1138 ASTReadResult ReadControlBlock(ModuleFile &F,
1139 SmallVectorImpl<ImportedModule> &Loaded,
1140 const ModuleFile *ImportedBy,
1141 unsigned ClientLoadCapabilities);
1142 ASTReadResult ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities);
1143 bool ParseLineTable(ModuleFile &F, const RecordData &Record);
1144 bool ReadSourceManagerBlock(ModuleFile &F);
1145 llvm::BitstreamCursor &SLocCursorForID(int ID);
1146 SourceLocation getImportLocation(ModuleFile *F);
1147 ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
1148 const ModuleFile *ImportedBy,
1149 unsigned ClientLoadCapabilities);
1150 ASTReadResult ReadSubmoduleBlock(ModuleFile &F,
1151 unsigned ClientLoadCapabilities);
1152 static bool ParseLanguageOptions(const RecordData &Record, bool Complain,
1153 ASTReaderListener &Listener,
1154 bool AllowCompatibleDifferences);
1155 static bool ParseTargetOptions(const RecordData &Record, bool Complain,
1156 ASTReaderListener &Listener);
1157 static bool ParseDiagnosticOptions(const RecordData &Record, bool Complain,
1158 ASTReaderListener &Listener);
1159 static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1160 ASTReaderListener &Listener);
1161 static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain,
1162 ASTReaderListener &Listener);
1163 static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain,
1164 ASTReaderListener &Listener,
1165 std::string &SuggestedPredefines);
1166
1167 struct RecordLocation {
RecordLocationRecordLocation1168 RecordLocation(ModuleFile *M, uint64_t O)
1169 : F(M), Offset(O) {}
1170 ModuleFile *F;
1171 uint64_t Offset;
1172 };
1173
1174 QualType readTypeRecord(unsigned Index);
1175 void readExceptionSpec(ModuleFile &ModuleFile,
1176 SmallVectorImpl<QualType> &ExceptionStorage,
1177 FunctionProtoType::ExceptionSpecInfo &ESI,
1178 const RecordData &Record, unsigned &Index);
1179 RecordLocation TypeCursorForIndex(unsigned Index);
1180 void LoadedDecl(unsigned Index, Decl *D);
1181 Decl *ReadDeclRecord(serialization::DeclID ID);
1182 void markIncompleteDeclChain(Decl *Canon);
1183 RecordLocation DeclCursorForID(serialization::DeclID ID,
1184 unsigned &RawLocation);
1185 void loadDeclUpdateRecords(serialization::DeclID ID, Decl *D);
1186 void loadPendingDeclChain(serialization::GlobalDeclID ID);
1187 void loadObjCCategories(serialization::GlobalDeclID ID, ObjCInterfaceDecl *D,
1188 unsigned PreviousGeneration = 0);
1189
1190 RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1191 uint64_t getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset);
1192
1193 /// \brief Returns the first preprocessed entity ID that begins or ends after
1194 /// \arg Loc.
1195 serialization::PreprocessedEntityID
1196 findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1197
1198 /// \brief Find the next module that contains entities and return the ID
1199 /// of the first entry.
1200 ///
1201 /// \param SLocMapI points at a chunk of a module that contains no
1202 /// preprocessed entities or the entities it contains are not the
1203 /// ones we are looking for.
1204 serialization::PreprocessedEntityID
1205 findNextPreprocessedEntity(
1206 GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1207
1208 /// \brief Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1209 /// preprocessed entity.
1210 std::pair<ModuleFile *, unsigned>
1211 getModulePreprocessedEntity(unsigned GlobalIndex);
1212
1213 /// \brief Returns (begin, end) pair for the preprocessed entities of a
1214 /// particular module.
1215 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
1216 getModulePreprocessedEntities(ModuleFile &Mod) const;
1217
1218 class ModuleDeclIterator {
1219 ASTReader *Reader;
1220 ModuleFile *Mod;
1221 const serialization::LocalDeclID *Pos;
1222
1223 public:
1224 typedef const Decl *value_type;
1225 typedef value_type& reference;
1226 typedef value_type* pointer;
1227
ModuleDeclIterator()1228 ModuleDeclIterator() : Reader(nullptr), Mod(nullptr), Pos(nullptr) { }
1229
ModuleDeclIterator(ASTReader * Reader,ModuleFile * Mod,const serialization::LocalDeclID * Pos)1230 ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1231 const serialization::LocalDeclID *Pos)
1232 : Reader(Reader), Mod(Mod), Pos(Pos) { }
1233
1234 value_type operator*() const {
1235 return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *Pos));
1236 }
1237
1238 ModuleDeclIterator &operator++() {
1239 ++Pos;
1240 return *this;
1241 }
1242
1243 ModuleDeclIterator operator++(int) {
1244 ModuleDeclIterator Prev(*this);
1245 ++Pos;
1246 return Prev;
1247 }
1248
1249 ModuleDeclIterator &operator--() {
1250 --Pos;
1251 return *this;
1252 }
1253
1254 ModuleDeclIterator operator--(int) {
1255 ModuleDeclIterator Prev(*this);
1256 --Pos;
1257 return Prev;
1258 }
1259
1260 friend bool operator==(const ModuleDeclIterator &LHS,
1261 const ModuleDeclIterator &RHS) {
1262 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1263 return LHS.Pos == RHS.Pos;
1264 }
1265
1266 friend bool operator!=(const ModuleDeclIterator &LHS,
1267 const ModuleDeclIterator &RHS) {
1268 assert(LHS.Reader == RHS.Reader && LHS.Mod == RHS.Mod);
1269 return LHS.Pos != RHS.Pos;
1270 }
1271 };
1272
1273 std::pair<ModuleDeclIterator, ModuleDeclIterator>
1274 getModuleFileLevelDecls(ModuleFile &Mod);
1275
1276 void PassInterestingDeclsToConsumer();
1277 void PassInterestingDeclToConsumer(Decl *D);
1278
1279 void finishPendingActions();
1280 void diagnoseOdrViolations();
1281
1282 void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1283
addPendingDeclContextInfo(Decl * D,serialization::GlobalDeclID SemaDC,serialization::GlobalDeclID LexicalDC)1284 void addPendingDeclContextInfo(Decl *D,
1285 serialization::GlobalDeclID SemaDC,
1286 serialization::GlobalDeclID LexicalDC) {
1287 assert(D);
1288 PendingDeclContextInfo Info = { D, SemaDC, LexicalDC };
1289 PendingDeclContextInfos.push_back(Info);
1290 }
1291
1292 /// \brief Produce an error diagnostic and return true.
1293 ///
1294 /// This routine should only be used for fatal errors that have to
1295 /// do with non-routine failures (e.g., corrupted AST file).
1296 void Error(StringRef Msg);
1297 void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1298 StringRef Arg2 = StringRef());
1299
1300 ASTReader(const ASTReader &) LLVM_DELETED_FUNCTION;
1301 void operator=(const ASTReader &) LLVM_DELETED_FUNCTION;
1302 public:
1303 /// \brief Load the AST file and validate its contents against the given
1304 /// Preprocessor.
1305 ///
1306 /// \param PP the preprocessor associated with the context in which this
1307 /// precompiled header will be loaded.
1308 ///
1309 /// \param Context the AST context that this precompiled header will be
1310 /// loaded into.
1311 ///
1312 /// \param isysroot If non-NULL, the system include path specified by the
1313 /// user. This is only used with relocatable PCH files. If non-NULL,
1314 /// a relocatable PCH file will use the default path "/".
1315 ///
1316 /// \param DisableValidation If true, the AST reader will suppress most
1317 /// of its regular consistency checking, allowing the use of precompiled
1318 /// headers that cannot be determined to be compatible.
1319 ///
1320 /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1321 /// AST file the was created out of an AST with compiler errors,
1322 /// otherwise it will reject it.
1323 ///
1324 /// \param AllowConfigurationMismatch If true, the AST reader will not check
1325 /// for configuration differences between the AST file and the invocation.
1326 ///
1327 /// \param ValidateSystemInputs If true, the AST reader will validate
1328 /// system input files in addition to user input files. This is only
1329 /// meaningful if \p DisableValidation is false.
1330 ///
1331 /// \param UseGlobalIndex If true, the AST reader will try to load and use
1332 /// the global module index.
1333 ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot = "",
1334 bool DisableValidation = false,
1335 bool AllowASTWithCompilerErrors = false,
1336 bool AllowConfigurationMismatch = false,
1337 bool ValidateSystemInputs = false,
1338 bool UseGlobalIndex = true);
1339
1340 ~ASTReader();
1341
getSourceManager()1342 SourceManager &getSourceManager() const { return SourceMgr; }
getFileManager()1343 FileManager &getFileManager() const { return FileMgr; }
1344
1345 /// \brief Flags that indicate what kind of AST loading failures the client
1346 /// of the AST reader can directly handle.
1347 ///
1348 /// When a client states that it can handle a particular kind of failure,
1349 /// the AST reader will not emit errors when producing that kind of failure.
1350 enum LoadFailureCapabilities {
1351 /// \brief The client can't handle any AST loading failures.
1352 ARR_None = 0,
1353 /// \brief The client can handle an AST file that cannot load because it
1354 /// is missing.
1355 ARR_Missing = 0x1,
1356 /// \brief The client can handle an AST file that cannot load because it
1357 /// is out-of-date relative to its input files.
1358 ARR_OutOfDate = 0x2,
1359 /// \brief The client can handle an AST file that cannot load because it
1360 /// was built with a different version of Clang.
1361 ARR_VersionMismatch = 0x4,
1362 /// \brief The client can handle an AST file that cannot load because it's
1363 /// compiled configuration doesn't match that of the context it was
1364 /// loaded into.
1365 ARR_ConfigurationMismatch = 0x8
1366 };
1367
1368 /// \brief Load the AST file designated by the given file name.
1369 ///
1370 /// \param FileName The name of the AST file to load.
1371 ///
1372 /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1373 /// or preamble.
1374 ///
1375 /// \param ImportLoc the location where the module file will be considered as
1376 /// imported from. For non-module AST types it should be invalid.
1377 ///
1378 /// \param ClientLoadCapabilities The set of client load-failure
1379 /// capabilities, represented as a bitset of the enumerators of
1380 /// LoadFailureCapabilities.
1381 ASTReadResult ReadAST(const std::string &FileName, ModuleKind Type,
1382 SourceLocation ImportLoc,
1383 unsigned ClientLoadCapabilities);
1384
1385 /// \brief Make the entities in the given module and any of its (non-explicit)
1386 /// submodules visible to name lookup.
1387 ///
1388 /// \param Mod The module whose names should be made visible.
1389 ///
1390 /// \param NameVisibility The level of visibility to give the names in the
1391 /// module. Visibility can only be increased over time.
1392 ///
1393 /// \param ImportLoc The location at which the import occurs.
1394 ///
1395 /// \param Complain Whether to complain about conflicting module imports.
1396 void makeModuleVisible(Module *Mod,
1397 Module::NameVisibilityKind NameVisibility,
1398 SourceLocation ImportLoc,
1399 bool Complain);
1400
1401 /// \brief Make the names within this set of hidden names visible.
1402 void makeNamesVisible(const HiddenNames &Names, Module *Owner,
1403 bool FromFinalization);
1404
1405 /// \brief Take the AST callbacks listener.
takeListener()1406 std::unique_ptr<ASTReaderListener> takeListener() {
1407 return std::move(Listener);
1408 }
1409
1410 /// \brief Set the AST callbacks listener.
setListener(std::unique_ptr<ASTReaderListener> Listener)1411 void setListener(std::unique_ptr<ASTReaderListener> Listener) {
1412 this->Listener = std::move(Listener);
1413 }
1414
1415 /// \brief Add an AST callback listener.
1416 ///
1417 /// Takes ownership of \p L.
addListener(std::unique_ptr<ASTReaderListener> L)1418 void addListener(std::unique_ptr<ASTReaderListener> L) {
1419 if (Listener)
1420 L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1421 std::move(Listener));
1422 Listener = std::move(L);
1423 }
1424
1425 /// RAII object to temporarily add an AST callback listener.
1426 class ListenerScope {
1427 ASTReader &Reader;
1428 bool Chained;
1429
1430 public:
ListenerScope(ASTReader & Reader,std::unique_ptr<ASTReaderListener> L)1431 ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L)
1432 : Reader(Reader), Chained(false) {
1433 auto Old = Reader.takeListener();
1434 if (Old) {
1435 Chained = true;
1436 L = llvm::make_unique<ChainedASTReaderListener>(std::move(L),
1437 std::move(Old));
1438 }
1439 Reader.setListener(std::move(L));
1440 }
~ListenerScope()1441 ~ListenerScope() {
1442 auto New = Reader.takeListener();
1443 if (Chained)
1444 Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get())
1445 ->takeSecond());
1446 }
1447 };
1448
1449 /// \brief Set the AST deserialization listener.
1450 void setDeserializationListener(ASTDeserializationListener *Listener,
1451 bool TakeOwnership = false);
1452
1453 /// \brief Determine whether this AST reader has a global index.
hasGlobalIndex()1454 bool hasGlobalIndex() const { return (bool)GlobalIndex; }
1455
1456 /// \brief Return global module index.
getGlobalIndex()1457 GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); }
1458
1459 /// \brief Reset reader for a reload try.
resetForReload()1460 void resetForReload() { TriedLoadingGlobalIndex = false; }
1461
1462 /// \brief Attempts to load the global index.
1463 ///
1464 /// \returns true if loading the global index has failed for any reason.
1465 bool loadGlobalIndex();
1466
1467 /// \brief Determine whether we tried to load the global index, but failed,
1468 /// e.g., because it is out-of-date or does not exist.
1469 bool isGlobalIndexUnavailable() const;
1470
1471 /// \brief Initializes the ASTContext
1472 void InitializeContext();
1473
1474 /// \brief Update the state of Sema after loading some additional modules.
1475 void UpdateSema();
1476
1477 /// \brief Add in-memory (virtual file) buffer.
addInMemoryBuffer(StringRef & FileName,std::unique_ptr<llvm::MemoryBuffer> Buffer)1478 void addInMemoryBuffer(StringRef &FileName,
1479 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
1480 ModuleMgr.addInMemoryBuffer(FileName, std::move(Buffer));
1481 }
1482
1483 /// \brief Finalizes the AST reader's state before writing an AST file to
1484 /// disk.
1485 ///
1486 /// This operation may undo temporary state in the AST that should not be
1487 /// emitted.
1488 void finalizeForWriting();
1489
1490 /// \brief Retrieve the module manager.
getModuleManager()1491 ModuleManager &getModuleManager() { return ModuleMgr; }
1492
1493 /// \brief Retrieve the preprocessor.
getPreprocessor()1494 Preprocessor &getPreprocessor() const { return PP; }
1495
1496 /// \brief Retrieve the name of the original source file name for the primary
1497 /// module file.
getOriginalSourceFile()1498 StringRef getOriginalSourceFile() {
1499 return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1500 }
1501
1502 /// \brief Retrieve the name of the original source file name directly from
1503 /// the AST file, without actually loading the AST file.
1504 static std::string getOriginalSourceFile(const std::string &ASTFileName,
1505 FileManager &FileMgr,
1506 DiagnosticsEngine &Diags);
1507
1508 /// \brief Read the control block for the named AST file.
1509 ///
1510 /// \returns true if an error occurred, false otherwise.
1511 static bool readASTFileControlBlock(StringRef Filename,
1512 FileManager &FileMgr,
1513 ASTReaderListener &Listener);
1514
1515 /// \brief Determine whether the given AST file is acceptable to load into a
1516 /// translation unit with the given language and target options.
1517 static bool isAcceptableASTFile(StringRef Filename,
1518 FileManager &FileMgr,
1519 const LangOptions &LangOpts,
1520 const TargetOptions &TargetOpts,
1521 const PreprocessorOptions &PPOpts);
1522
1523 /// \brief Returns the suggested contents of the predefines buffer,
1524 /// which contains a (typically-empty) subset of the predefines
1525 /// build prior to including the precompiled header.
getSuggestedPredefines()1526 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
1527
1528 /// \brief Read a preallocated preprocessed entity from the external source.
1529 ///
1530 /// \returns null if an error occurred that prevented the preprocessed
1531 /// entity from being loaded.
1532 PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override;
1533
1534 /// \brief Returns a pair of [Begin, End) indices of preallocated
1535 /// preprocessed entities that \p Range encompasses.
1536 std::pair<unsigned, unsigned>
1537 findPreprocessedEntitiesInRange(SourceRange Range) override;
1538
1539 /// \brief Optionally returns true or false if the preallocated preprocessed
1540 /// entity with index \p Index came from file \p FID.
1541 Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
1542 FileID FID) override;
1543
1544 /// \brief Read the header file information for the given file entry.
1545 HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) override;
1546
1547 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
1548
1549 /// \brief Returns the number of source locations found in the chain.
getTotalNumSLocs()1550 unsigned getTotalNumSLocs() const {
1551 return TotalNumSLocEntries;
1552 }
1553
1554 /// \brief Returns the number of identifiers found in the chain.
getTotalNumIdentifiers()1555 unsigned getTotalNumIdentifiers() const {
1556 return static_cast<unsigned>(IdentifiersLoaded.size());
1557 }
1558
1559 /// \brief Returns the number of macros found in the chain.
getTotalNumMacros()1560 unsigned getTotalNumMacros() const {
1561 return static_cast<unsigned>(MacrosLoaded.size());
1562 }
1563
1564 /// \brief Returns the number of types found in the chain.
getTotalNumTypes()1565 unsigned getTotalNumTypes() const {
1566 return static_cast<unsigned>(TypesLoaded.size());
1567 }
1568
1569 /// \brief Returns the number of declarations found in the chain.
getTotalNumDecls()1570 unsigned getTotalNumDecls() const {
1571 return static_cast<unsigned>(DeclsLoaded.size());
1572 }
1573
1574 /// \brief Returns the number of submodules known.
getTotalNumSubmodules()1575 unsigned getTotalNumSubmodules() const {
1576 return static_cast<unsigned>(SubmodulesLoaded.size());
1577 }
1578
1579 /// \brief Returns the number of selectors found in the chain.
getTotalNumSelectors()1580 unsigned getTotalNumSelectors() const {
1581 return static_cast<unsigned>(SelectorsLoaded.size());
1582 }
1583
1584 /// \brief Returns the number of preprocessed entities known to the AST
1585 /// reader.
getTotalNumPreprocessedEntities()1586 unsigned getTotalNumPreprocessedEntities() const {
1587 unsigned Result = 0;
1588 for (ModuleConstIterator I = ModuleMgr.begin(),
1589 E = ModuleMgr.end(); I != E; ++I) {
1590 Result += (*I)->NumPreprocessedEntities;
1591 }
1592
1593 return Result;
1594 }
1595
1596 /// \brief Returns the number of C++ base specifiers found in the chain.
getTotalNumCXXBaseSpecifiers()1597 unsigned getTotalNumCXXBaseSpecifiers() const {
1598 return NumCXXBaseSpecifiersLoaded;
1599 }
1600
1601 /// \brief Reads a TemplateArgumentLocInfo appropriate for the
1602 /// given TemplateArgument kind.
1603 TemplateArgumentLocInfo
1604 GetTemplateArgumentLocInfo(ModuleFile &F, TemplateArgument::ArgKind Kind,
1605 const RecordData &Record, unsigned &Idx);
1606
1607 /// \brief Reads a TemplateArgumentLoc.
1608 TemplateArgumentLoc
1609 ReadTemplateArgumentLoc(ModuleFile &F,
1610 const RecordData &Record, unsigned &Idx);
1611
1612 const ASTTemplateArgumentListInfo*
1613 ReadASTTemplateArgumentListInfo(ModuleFile &F,
1614 const RecordData &Record, unsigned &Index);
1615
1616 /// \brief Reads a declarator info from the given record.
1617 TypeSourceInfo *GetTypeSourceInfo(ModuleFile &F,
1618 const RecordData &Record, unsigned &Idx);
1619
1620 /// \brief Resolve a type ID into a type, potentially building a new
1621 /// type.
1622 QualType GetType(serialization::TypeID ID);
1623
1624 /// \brief Resolve a local type ID within a given AST file into a type.
1625 QualType getLocalType(ModuleFile &F, unsigned LocalID);
1626
1627 /// \brief Map a local type ID within a given AST file into a global type ID.
1628 serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1629
1630 /// \brief Read a type from the current position in the given record, which
1631 /// was read from the given AST file.
readType(ModuleFile & F,const RecordData & Record,unsigned & Idx)1632 QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
1633 if (Idx >= Record.size())
1634 return QualType();
1635
1636 return getLocalType(F, Record[Idx++]);
1637 }
1638
1639 /// \brief Map from a local declaration ID within a given module to a
1640 /// global declaration ID.
1641 serialization::DeclID getGlobalDeclID(ModuleFile &F,
1642 serialization::LocalDeclID LocalID) const;
1643
1644 /// \brief Returns true if global DeclID \p ID originated from module \p M.
1645 bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
1646
1647 /// \brief Retrieve the module file that owns the given declaration, or NULL
1648 /// if the declaration is not from a module file.
1649 ModuleFile *getOwningModuleFile(const Decl *D);
1650
1651 /// \brief Get the best name we know for the module that owns the given
1652 /// declaration, or an empty string if the declaration is not from a module.
1653 std::string getOwningModuleNameForDiagnostic(const Decl *D);
1654
1655 /// \brief Returns the source location for the decl \p ID.
1656 SourceLocation getSourceLocationForDeclID(serialization::GlobalDeclID ID);
1657
1658 /// \brief Resolve a declaration ID into a declaration, potentially
1659 /// building a new declaration.
1660 Decl *GetDecl(serialization::DeclID ID);
1661 Decl *GetExternalDecl(uint32_t ID) override;
1662
1663 /// \brief Resolve a declaration ID into a declaration. Return 0 if it's not
1664 /// been loaded yet.
1665 Decl *GetExistingDecl(serialization::DeclID ID);
1666
1667 /// \brief Reads a declaration with the given local ID in the given module.
GetLocalDecl(ModuleFile & F,uint32_t LocalID)1668 Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1669 return GetDecl(getGlobalDeclID(F, LocalID));
1670 }
1671
1672 /// \brief Reads a declaration with the given local ID in the given module.
1673 ///
1674 /// \returns The requested declaration, casted to the given return type.
1675 template<typename T>
GetLocalDeclAs(ModuleFile & F,uint32_t LocalID)1676 T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1677 return cast_or_null<T>(GetLocalDecl(F, LocalID));
1678 }
1679
1680 /// \brief Map a global declaration ID into the declaration ID used to
1681 /// refer to this declaration within the given module fule.
1682 ///
1683 /// \returns the global ID of the given declaration as known in the given
1684 /// module file.
1685 serialization::DeclID
1686 mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1687 serialization::DeclID GlobalID);
1688
1689 /// \brief Reads a declaration ID from the given position in a record in the
1690 /// given module.
1691 ///
1692 /// \returns The declaration ID read from the record, adjusted to a global ID.
1693 serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1694 unsigned &Idx);
1695
1696 /// \brief Reads a declaration from the given position in a record in the
1697 /// given module.
ReadDecl(ModuleFile & F,const RecordData & R,unsigned & I)1698 Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1699 return GetDecl(ReadDeclID(F, R, I));
1700 }
1701
1702 /// \brief Reads a declaration from the given position in a record in the
1703 /// given module.
1704 ///
1705 /// \returns The declaration read from this location, casted to the given
1706 /// result type.
1707 template<typename T>
ReadDeclAs(ModuleFile & F,const RecordData & R,unsigned & I)1708 T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1709 return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
1710 }
1711
1712 /// \brief If any redeclarations of \p D have been imported since it was
1713 /// last checked, this digs out those redeclarations and adds them to the
1714 /// redeclaration chain for \p D.
1715 void CompleteRedeclChain(const Decl *D) override;
1716
1717 /// \brief Read a CXXBaseSpecifiers ID form the given record and
1718 /// return its global bit offset.
1719 uint64_t readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
1720 unsigned &Idx);
1721
1722 CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
1723
1724 /// \brief Resolve the offset of a statement into a statement.
1725 ///
1726 /// This operation will read a new statement from the external
1727 /// source each time it is called, and is meant to be used via a
1728 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
1729 Stmt *GetExternalDeclStmt(uint64_t Offset) override;
1730
1731 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1732 /// specified cursor. Read the abbreviations that are at the top of the block
1733 /// and then leave the cursor pointing into the block.
1734 bool ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, unsigned BlockID);
1735
1736 /// \brief Finds all the visible declarations with a given name.
1737 /// The current implementation of this method just loads the entire
1738 /// lookup table as unmaterialized references.
1739 bool FindExternalVisibleDeclsByName(const DeclContext *DC,
1740 DeclarationName Name) override;
1741
1742 /// \brief Read all of the declarations lexically stored in a
1743 /// declaration context.
1744 ///
1745 /// \param DC The declaration context whose declarations will be
1746 /// read.
1747 ///
1748 /// \param Decls Vector that will contain the declarations loaded
1749 /// from the external source. The caller is responsible for merging
1750 /// these declarations with any declarations already stored in the
1751 /// declaration context.
1752 ///
1753 /// \returns true if there was an error while reading the
1754 /// declarations for this declaration context.
1755 ExternalLoadResult FindExternalLexicalDecls(const DeclContext *DC,
1756 bool (*isKindWeWant)(Decl::Kind),
1757 SmallVectorImpl<Decl*> &Decls) override;
1758
1759 /// \brief Get the decls that are contained in a file in the Offset/Length
1760 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
1761 /// a range.
1762 void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
1763 SmallVectorImpl<Decl *> &Decls) override;
1764
1765 /// \brief Notify ASTReader that we started deserialization of
1766 /// a decl or type so until FinishedDeserializing is called there may be
1767 /// decls that are initializing. Must be paired with FinishedDeserializing.
StartedDeserializing()1768 void StartedDeserializing() override { ++NumCurrentElementsDeserializing; }
1769
1770 /// \brief Notify ASTReader that we finished the deserialization of
1771 /// a decl or type. Must be paired with StartedDeserializing.
1772 void FinishedDeserializing() override;
1773
1774 /// \brief Function that will be invoked when we begin parsing a new
1775 /// translation unit involving this external AST source.
1776 ///
1777 /// This function will provide all of the external definitions to
1778 /// the ASTConsumer.
1779 void StartTranslationUnit(ASTConsumer *Consumer) override;
1780
1781 /// \brief Print some statistics about AST usage.
1782 void PrintStats() override;
1783
1784 /// \brief Dump information about the AST reader to standard error.
1785 void dump();
1786
1787 /// Return the amount of memory used by memory buffers, breaking down
1788 /// by heap-backed versus mmap'ed memory.
1789 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
1790
1791 /// \brief Initialize the semantic source with the Sema instance
1792 /// being used to perform semantic analysis on the abstract syntax
1793 /// tree.
1794 void InitializeSema(Sema &S) override;
1795
1796 /// \brief Inform the semantic consumer that Sema is no longer available.
ForgetSema()1797 void ForgetSema() override { SemaObj = nullptr; }
1798
1799 /// \brief Retrieve the IdentifierInfo for the named identifier.
1800 ///
1801 /// This routine builds a new IdentifierInfo for the given identifier. If any
1802 /// declarations with this name are visible from translation unit scope, their
1803 /// declarations will be deserialized and introduced into the declaration
1804 /// chain of the identifier.
1805 virtual IdentifierInfo *get(const char *NameStart, const char *NameEnd);
get(StringRef Name)1806 IdentifierInfo *get(StringRef Name) override {
1807 return get(Name.begin(), Name.end());
1808 }
1809
1810 /// \brief Retrieve an iterator into the set of all identifiers
1811 /// in all loaded AST files.
1812 IdentifierIterator *getIdentifiers() override;
1813
1814 /// \brief Load the contents of the global method pool for a given
1815 /// selector.
1816 void ReadMethodPool(Selector Sel) override;
1817
1818 /// \brief Load the set of namespaces that are known to the external source,
1819 /// which will be used during typo correction.
1820 void ReadKnownNamespaces(
1821 SmallVectorImpl<NamespaceDecl *> &Namespaces) override;
1822
1823 void ReadUndefinedButUsed(
1824 llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) override;
1825
1826 void ReadTentativeDefinitions(
1827 SmallVectorImpl<VarDecl *> &TentativeDefs) override;
1828
1829 void ReadUnusedFileScopedDecls(
1830 SmallVectorImpl<const DeclaratorDecl *> &Decls) override;
1831
1832 void ReadDelegatingConstructors(
1833 SmallVectorImpl<CXXConstructorDecl *> &Decls) override;
1834
1835 void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override;
1836
1837 void ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) override;
1838
1839 void ReadUnusedLocalTypedefNameCandidates(
1840 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
1841
1842 void ReadLocallyScopedExternCDecls(
1843 SmallVectorImpl<NamedDecl *> &Decls) override;
1844
1845 void ReadReferencedSelectors(
1846 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) override;
1847
1848 void ReadWeakUndeclaredIdentifiers(
1849 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI) override;
1850
1851 void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
1852
1853 void ReadPendingInstantiations(
1854 SmallVectorImpl<std::pair<ValueDecl *,
1855 SourceLocation> > &Pending) override;
1856
1857 void ReadLateParsedTemplates(
1858 llvm::DenseMap<const FunctionDecl *,
1859 LateParsedTemplate *> &LPTMap) override;
1860
1861 /// \brief Load a selector from disk, registering its ID if it exists.
1862 void LoadSelector(Selector Sel);
1863
1864 void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
1865 void SetGloballyVisibleDecls(IdentifierInfo *II,
1866 const SmallVectorImpl<uint32_t> &DeclIDs,
1867 SmallVectorImpl<Decl *> *Decls = nullptr);
1868
1869 /// \brief Report a diagnostic.
1870 DiagnosticBuilder Diag(unsigned DiagID);
1871
1872 /// \brief Report a diagnostic.
1873 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
1874
1875 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
1876
GetIdentifierInfo(ModuleFile & M,const RecordData & Record,unsigned & Idx)1877 IdentifierInfo *GetIdentifierInfo(ModuleFile &M, const RecordData &Record,
1878 unsigned &Idx) {
1879 return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++]));
1880 }
1881
GetIdentifier(serialization::IdentifierID ID)1882 IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override {
1883 // Note that we are loading an identifier.
1884 Deserializing AnIdentifier(this);
1885
1886 return DecodeIdentifierInfo(ID);
1887 }
1888
1889 IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
1890
1891 serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
1892 unsigned LocalID);
1893
1894 ModuleMacroInfo *getModuleMacro(const PendingMacroInfo &PMInfo);
1895
1896 void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
1897
1898 void installPCHMacroDirectives(IdentifierInfo *II,
1899 ModuleFile &M, uint64_t Offset);
1900
1901 void installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1902 Module *Owner);
1903
1904 typedef llvm::TinyPtrVector<DefMacroDirective *> AmbiguousMacros;
1905 llvm::DenseMap<IdentifierInfo*, AmbiguousMacros> AmbiguousMacroDefs;
1906
1907 void
1908 removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
1909 AmbiguousMacros &Ambig,
1910 ArrayRef<serialization::SubmoduleID> Overrides);
1911
1912 AmbiguousMacros *
1913 removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
1914 ArrayRef<serialization::SubmoduleID> Overrides);
1915
1916 /// \brief Retrieve the macro with the given ID.
1917 MacroInfo *getMacro(serialization::MacroID ID);
1918
1919 /// \brief Retrieve the global macro ID corresponding to the given local
1920 /// ID within the given module file.
1921 serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
1922
1923 /// \brief Read the source location entry with index ID.
1924 bool ReadSLocEntry(int ID) override;
1925
1926 /// \brief Retrieve the module import location and module name for the
1927 /// given source manager entry ID.
1928 std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override;
1929
1930 /// \brief Retrieve the global submodule ID given a module and its local ID
1931 /// number.
1932 serialization::SubmoduleID
1933 getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID);
1934
1935 /// \brief Retrieve the submodule that corresponds to a global submodule ID.
1936 ///
1937 Module *getSubmodule(serialization::SubmoduleID GlobalID);
1938
1939 /// \brief Retrieve the module that corresponds to the given module ID.
1940 ///
1941 /// Note: overrides method in ExternalASTSource
1942 Module *getModule(unsigned ID) override;
1943
1944 /// \brief Retrieve a selector from the given module with its local ID
1945 /// number.
1946 Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
1947
1948 Selector DecodeSelector(serialization::SelectorID Idx);
1949
1950 Selector GetExternalSelector(serialization::SelectorID ID) override;
1951 uint32_t GetNumExternalSelectors() override;
1952
ReadSelector(ModuleFile & M,const RecordData & Record,unsigned & Idx)1953 Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
1954 return getLocalSelector(M, Record[Idx++]);
1955 }
1956
1957 /// \brief Retrieve the global selector ID that corresponds to this
1958 /// the local selector ID in a given module.
1959 serialization::SelectorID getGlobalSelectorID(ModuleFile &F,
1960 unsigned LocalID) const;
1961
1962 /// \brief Read a declaration name.
1963 DeclarationName ReadDeclarationName(ModuleFile &F,
1964 const RecordData &Record, unsigned &Idx);
1965 void ReadDeclarationNameLoc(ModuleFile &F,
1966 DeclarationNameLoc &DNLoc, DeclarationName Name,
1967 const RecordData &Record, unsigned &Idx);
1968 void ReadDeclarationNameInfo(ModuleFile &F, DeclarationNameInfo &NameInfo,
1969 const RecordData &Record, unsigned &Idx);
1970
1971 void ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
1972 const RecordData &Record, unsigned &Idx);
1973
1974 NestedNameSpecifier *ReadNestedNameSpecifier(ModuleFile &F,
1975 const RecordData &Record,
1976 unsigned &Idx);
1977
1978 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc(ModuleFile &F,
1979 const RecordData &Record,
1980 unsigned &Idx);
1981
1982 /// \brief Read a template name.
1983 TemplateName ReadTemplateName(ModuleFile &F, const RecordData &Record,
1984 unsigned &Idx);
1985
1986 /// \brief Read a template argument.
1987 TemplateArgument ReadTemplateArgument(ModuleFile &F,
1988 const RecordData &Record,unsigned &Idx);
1989
1990 /// \brief Read a template parameter list.
1991 TemplateParameterList *ReadTemplateParameterList(ModuleFile &F,
1992 const RecordData &Record,
1993 unsigned &Idx);
1994
1995 /// \brief Read a template argument array.
1996 void
1997 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
1998 ModuleFile &F, const RecordData &Record,
1999 unsigned &Idx);
2000
2001 /// \brief Read a UnresolvedSet structure.
2002 void ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
2003 const RecordData &Record, unsigned &Idx);
2004
2005 /// \brief Read a C++ base specifier.
2006 CXXBaseSpecifier ReadCXXBaseSpecifier(ModuleFile &F,
2007 const RecordData &Record,unsigned &Idx);
2008
2009 /// \brief Read a CXXCtorInitializer array.
2010 std::pair<CXXCtorInitializer **, unsigned>
2011 ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
2012 unsigned &Idx);
2013
2014 /// \brief Read a source location from raw form.
ReadSourceLocation(ModuleFile & ModuleFile,unsigned Raw)2015 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, unsigned Raw) const {
2016 SourceLocation Loc = SourceLocation::getFromRawEncoding(Raw);
2017 assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() &&
2018 "Cannot find offset to remap.");
2019 int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second;
2020 return Loc.getLocWithOffset(Remap);
2021 }
2022
2023 /// \brief Read a source location.
ReadSourceLocation(ModuleFile & ModuleFile,const RecordDataImpl & Record,unsigned & Idx)2024 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2025 const RecordDataImpl &Record,
2026 unsigned &Idx) {
2027 return ReadSourceLocation(ModuleFile, Record[Idx++]);
2028 }
2029
2030 /// \brief Read a source range.
2031 SourceRange ReadSourceRange(ModuleFile &F,
2032 const RecordData &Record, unsigned &Idx);
2033
2034 /// \brief Read an integral value
2035 llvm::APInt ReadAPInt(const RecordData &Record, unsigned &Idx);
2036
2037 /// \brief Read a signed integral value
2038 llvm::APSInt ReadAPSInt(const RecordData &Record, unsigned &Idx);
2039
2040 /// \brief Read a floating-point value
2041 llvm::APFloat ReadAPFloat(const RecordData &Record,
2042 const llvm::fltSemantics &Sem, unsigned &Idx);
2043
2044 // \brief Read a string
2045 static std::string ReadString(const RecordData &Record, unsigned &Idx);
2046
2047 // \brief Read a path
2048 std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx);
2049
2050 /// \brief Read a version tuple.
2051 static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
2052
2053 CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
2054 unsigned &Idx);
2055
2056 /// \brief Reads attributes from the current stream position.
2057 void ReadAttributes(ModuleFile &F, AttrVec &Attrs,
2058 const RecordData &Record, unsigned &Idx);
2059
2060 /// \brief Reads a statement.
2061 Stmt *ReadStmt(ModuleFile &F);
2062
2063 /// \brief Reads an expression.
2064 Expr *ReadExpr(ModuleFile &F);
2065
2066 /// \brief Reads a sub-statement operand during statement reading.
ReadSubStmt()2067 Stmt *ReadSubStmt() {
2068 assert(ReadingKind == Read_Stmt &&
2069 "Should be called only during statement reading!");
2070 // Subexpressions are stored from last to first, so the next Stmt we need
2071 // is at the back of the stack.
2072 assert(!StmtStack.empty() && "Read too many sub-statements!");
2073 return StmtStack.pop_back_val();
2074 }
2075
2076 /// \brief Reads a sub-expression operand during statement reading.
2077 Expr *ReadSubExpr();
2078
2079 /// \brief Reads a token out of a record.
2080 Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
2081
2082 /// \brief Reads the macro record located at the given offset.
2083 MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
2084
2085 /// \brief Determine the global preprocessed entity ID that corresponds to
2086 /// the given local ID within the given module.
2087 serialization::PreprocessedEntityID
2088 getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2089
2090 /// \brief Add a macro to resolve imported from a module.
2091 ///
2092 /// \param II The name of the macro.
2093 /// \param M The module file.
2094 /// \param GMacID The global macro ID that is associated with this identifier.
2095 void addPendingMacroFromModule(IdentifierInfo *II,
2096 ModuleFile *M,
2097 serialization::GlobalMacroID GMacID,
2098 ArrayRef<serialization::SubmoduleID>);
2099
2100 /// \brief Add a macro to deserialize its macro directive history from a PCH.
2101 ///
2102 /// \param II The name of the macro.
2103 /// \param M The module file.
2104 /// \param MacroDirectivesOffset Offset of the serialized macro directive
2105 /// history.
2106 void addPendingMacroFromPCH(IdentifierInfo *II,
2107 ModuleFile *M, uint64_t MacroDirectivesOffset);
2108
2109 /// \brief Read the set of macros defined by this external macro source.
2110 void ReadDefinedMacros() override;
2111
2112 /// \brief Update an out-of-date identifier.
2113 void updateOutOfDateIdentifier(IdentifierInfo &II) override;
2114
2115 /// \brief Note that this identifier is up-to-date.
2116 void markIdentifierUpToDate(IdentifierInfo *II);
2117
2118 /// \brief Load all external visible decls in the given DeclContext.
2119 void completeVisibleDeclsMap(const DeclContext *DC) override;
2120
2121 /// \brief Retrieve the AST context that this AST reader supplements.
getContext()2122 ASTContext &getContext() { return Context; }
2123
2124 // \brief Contains the IDs for declarations that were requested before we have
2125 // access to a Sema object.
2126 SmallVector<uint64_t, 16> PreloadedDeclIDs;
2127
2128 /// \brief Retrieve the semantic analysis object used to analyze the
2129 /// translation unit in which the precompiled header is being
2130 /// imported.
getSema()2131 Sema *getSema() { return SemaObj; }
2132
2133 /// \brief Retrieve the identifier table associated with the
2134 /// preprocessor.
2135 IdentifierTable &getIdentifierTable();
2136
2137 /// \brief Record that the given ID maps to the given switch-case
2138 /// statement.
2139 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
2140
2141 /// \brief Retrieve the switch-case statement with the given ID.
2142 SwitchCase *getSwitchCaseWithID(unsigned ID);
2143
2144 void ClearSwitchCaseIDs();
2145
2146 /// \brief Cursors for comments blocks.
2147 SmallVector<std::pair<llvm::BitstreamCursor,
2148 serialization::ModuleFile *>, 8> CommentsCursors;
2149
2150 //RIDErief Loads comments ranges.
2151 void ReadComments() override;
2152
2153 /// Return all input files for the given module file.
2154 void getInputFiles(ModuleFile &F,
2155 SmallVectorImpl<serialization::InputFile> &Files);
2156 };
2157
2158 /// \brief Helper class that saves the current stream position and
2159 /// then restores it when destroyed.
2160 struct SavedStreamPosition {
SavedStreamPositionSavedStreamPosition2161 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
2162 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
2163
~SavedStreamPositionSavedStreamPosition2164 ~SavedStreamPosition() {
2165 Cursor.JumpToBit(Offset);
2166 }
2167
2168 private:
2169 llvm::BitstreamCursor &Cursor;
2170 uint64_t Offset;
2171 };
2172
Error(const char * Msg)2173 inline void PCHValidator::Error(const char *Msg) {
2174 Reader.Error(Msg);
2175 }
2176
2177 } // end namespace clang
2178
2179 #endif
2180