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