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