1//===- ASTReader.h - AST File Reader ----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_H
14#define LLVM_CLANG_SERIALIZATION_ASTREADER_H
15
16#include "clang/AST/Type.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/DiagnosticOptions.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/OpenCLOptions.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/StackExhaustionHandler.h"
23#include "clang/Basic/Version.h"
24#include "clang/Lex/ExternalPreprocessorSource.h"
25#include "clang/Lex/HeaderSearch.h"
26#include "clang/Lex/PreprocessingRecord.h"
27#include "clang/Lex/PreprocessorOptions.h"
28#include "clang/Sema/ExternalSemaSource.h"
29#include "clang/Sema/IdentifierResolver.h"
30#include "clang/Sema/Sema.h"
31#include "clang/Serialization/ASTBitCodes.h"
32#include "clang/Serialization/ContinuousRangeMap.h"
33#include "clang/Serialization/ModuleFile.h"
34#include "clang/Serialization/ModuleFileExtension.h"
35#include "clang/Serialization/ModuleManager.h"
36#include "clang/Serialization/SourceLocationEncoding.h"
37#include "llvm/ADT/ArrayRef.h"
38#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/DenseSet.h"
40#include "llvm/ADT/IntrusiveRefCntPtr.h"
41#include "llvm/ADT/MapVector.h"
42#include "llvm/ADT/PagedVector.h"
43#include "llvm/ADT/STLExtras.h"
44#include "llvm/ADT/SetVector.h"
45#include "llvm/ADT/SmallPtrSet.h"
46#include "llvm/ADT/SmallVector.h"
47#include "llvm/ADT/StringMap.h"
48#include "llvm/ADT/StringRef.h"
49#include "llvm/ADT/iterator.h"
50#include "llvm/ADT/iterator_range.h"
51#include "llvm/Bitstream/BitstreamReader.h"
52#include "llvm/Support/MemoryBuffer.h"
53#include "llvm/Support/SaveAndRestore.h"
54#include "llvm/Support/Timer.h"
55#include "llvm/Support/VersionTuple.h"
56#include <cassert>
57#include <cstddef>
58#include <cstdint>
59#include <ctime>
60#include <deque>
61#include <memory>
62#include <optional>
63#include <set>
64#include <string>
65#include <utility>
66#include <vector>
67
68namespace clang {
69
70class ASTConsumer;
71class ASTContext;
72class ASTDeserializationListener;
73class ASTReader;
74class ASTRecordReader;
75class CXXTemporary;
76class Decl;
77class DeclarationName;
78class DeclaratorDecl;
79class DeclContext;
80class EnumDecl;
81class Expr;
82class FieldDecl;
83class FileEntry;
84class FileManager;
85class FileSystemOptions;
86class FunctionDecl;
87class GlobalModuleIndex;
88struct HeaderFileInfo;
89class HeaderSearchOptions;
90class LangOptions;
91class MacroInfo;
92class ModuleCache;
93class NamedDecl;
94class NamespaceDecl;
95class ObjCCategoryDecl;
96class ObjCInterfaceDecl;
97class PCHContainerReader;
98class Preprocessor;
99class PreprocessorOptions;
100class Sema;
101class SourceManager;
102class Stmt;
103class SwitchCase;
104class TargetOptions;
105class Token;
106class TypedefNameDecl;
107class ValueDecl;
108class VarDecl;
109
110/// Abstract interface for callback invocations by the ASTReader.
111///
112/// While reading an AST file, the ASTReader will call the methods of the
113/// listener to pass on specific information. Some of the listener methods can
114/// return true to indicate to the ASTReader that the information (and
115/// consequently the AST file) is invalid.
116class ASTReaderListener {
117public:
118 virtual ~ASTReaderListener();
119
120 /// Receives the full Clang version information.
121 ///
122 /// \returns true to indicate that the version is invalid. Subclasses should
123 /// generally defer to this implementation.
124 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
125 return FullVersion != getClangFullRepositoryVersion();
126 }
127
128 virtual void ReadModuleName(StringRef ModuleName) {}
129 virtual void ReadModuleMapFile(StringRef ModuleMapPath) {}
130
131 /// Receives the language options.
132 ///
133 /// \returns true to indicate the options are invalid or false otherwise.
134 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
135 StringRef ModuleFilename, bool Complain,
136 bool AllowCompatibleDifferences) {
137 return false;
138 }
139
140 /// Receives the target options.
141 ///
142 /// \returns true to indicate the target options are invalid, or false
143 /// otherwise.
144 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
145 StringRef ModuleFilename, bool Complain,
146 bool AllowCompatibleDifferences) {
147 return false;
148 }
149
150 /// Receives the diagnostic options.
151 ///
152 /// \returns true to indicate the diagnostic options are invalid, or false
153 /// otherwise.
154 virtual bool ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
155 StringRef ModuleFilename, bool Complain) {
156 return false;
157 }
158
159 /// Receives the file system options.
160 ///
161 /// \returns true to indicate the file system options are invalid, or false
162 /// otherwise.
163 virtual bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
164 bool Complain) {
165 return false;
166 }
167
168 /// Receives the header search options.
169 ///
170 /// \param HSOpts The read header search options. The following fields are
171 /// missing and are reported in ReadHeaderSearchPaths():
172 /// UserEntries, SystemHeaderPrefixes, VFSOverlayFiles.
173 ///
174 /// \returns true to indicate the header search options are invalid, or false
175 /// otherwise.
176 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
177 StringRef ModuleFilename,
178 StringRef SpecificModuleCachePath,
179 bool Complain) {
180 return false;
181 }
182
183 /// Receives the header search paths.
184 ///
185 /// \param HSOpts The read header search paths. Only the following fields are
186 /// initialized: UserEntries, SystemHeaderPrefixes,
187 /// VFSOverlayFiles. The rest is reported in
188 /// ReadHeaderSearchOptions().
189 ///
190 /// \returns true to indicate the header search paths are invalid, or false
191 /// otherwise.
192 virtual bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts,
193 bool Complain) {
194 return false;
195 }
196
197 /// Receives the preprocessor options.
198 ///
199 /// \param SuggestedPredefines Can be filled in with the set of predefines
200 /// that are suggested by the preprocessor options. Typically only used when
201 /// loading a precompiled header.
202 ///
203 /// \returns true to indicate the preprocessor options are invalid, or false
204 /// otherwise.
205 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
206 StringRef ModuleFilename,
207 bool ReadMacros, bool Complain,
208 std::string &SuggestedPredefines) {
209 return false;
210 }
211
212 /// Receives __COUNTER__ value.
213 virtual void ReadCounter(const serialization::ModuleFile &M,
214 unsigned Value) {}
215
216 /// This is called for each AST file loaded.
217 virtual void visitModuleFile(StringRef Filename,
218 serialization::ModuleKind Kind) {}
219
220 /// Returns true if this \c ASTReaderListener wants to receive the
221 /// input files of the AST file via \c visitInputFile, false otherwise.
222 virtual bool needsInputFileVisitation() { return false; }
223
224 /// Returns true if this \c ASTReaderListener wants to receive the
225 /// system input files of the AST file via \c visitInputFile, false otherwise.
226 virtual bool needsSystemInputFileVisitation() { return false; }
227
228 /// if \c needsInputFileVisitation returns true, this is called for
229 /// each non-system input file of the AST File. If
230 /// \c needsSystemInputFileVisitation is true, then it is called for all
231 /// system input files as well.
232 ///
233 /// \returns true to continue receiving the next input file, false to stop.
234 virtual bool visitInputFile(StringRef Filename, bool isSystem,
235 bool isOverridden, bool isExplicitModule) {
236 return true;
237 }
238
239 /// Overloaded member function of \c visitInputFile that should
240 /// be defined when there is a distinction between
241 /// the file name and name-as-requested. For example, when deserializing input
242 /// files from precompiled AST files.
243 ///
244 /// \returns true to continue receiving the next input file, false to stop.
245 virtual bool visitInputFile(StringRef FilenameAsRequested, StringRef Filename,
246 bool isSystem, bool isOverridden,
247 bool isExplicitModule) {
248 return true;
249 }
250
251 /// Returns true if this \c ASTReaderListener wants to receive the
252 /// imports of the AST file via \c visitImport, false otherwise.
253 virtual bool needsImportVisitation() const { return false; }
254
255 /// If needsImportVisitation returns \c true, this is called for each
256 /// AST file imported by this AST file.
257 virtual void visitImport(StringRef ModuleName, StringRef Filename) {}
258
259 /// Indicates that a particular module file extension has been read.
260 virtual void readModuleFileExtension(
261 const ModuleFileExtensionMetadata &Metadata) {}
262};
263
264/// Simple wrapper class for chaining listeners.
265class ChainedASTReaderListener : public ASTReaderListener {
266 std::unique_ptr<ASTReaderListener> First;
267 std::unique_ptr<ASTReaderListener> Second;
268
269public:
270 /// Takes ownership of \p First and \p Second.
271 ChainedASTReaderListener(std::unique_ptr<ASTReaderListener> First,
272 std::unique_ptr<ASTReaderListener> Second)
273 : First(std::move(First)), Second(std::move(Second)) {}
274
275 std::unique_ptr<ASTReaderListener> takeFirst() { return std::move(First); }
276 std::unique_ptr<ASTReaderListener> takeSecond() { return std::move(Second); }
277
278 bool ReadFullVersionInformation(StringRef FullVersion) override;
279 void ReadModuleName(StringRef ModuleName) override;
280 void ReadModuleMapFile(StringRef ModuleMapPath) override;
281 bool ReadLanguageOptions(const LangOptions &LangOpts,
282 StringRef ModuleFilename, bool Complain,
283 bool AllowCompatibleDifferences) override;
284 bool ReadTargetOptions(const TargetOptions &TargetOpts,
285 StringRef ModuleFilename, bool Complain,
286 bool AllowCompatibleDifferences) override;
287 bool ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
288 StringRef ModuleFilename, bool Complain) override;
289 bool ReadFileSystemOptions(const FileSystemOptions &FSOpts,
290 bool Complain) override;
291
292 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
293 StringRef ModuleFilename,
294 StringRef SpecificModuleCachePath,
295 bool Complain) override;
296 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
297 StringRef ModuleFilename, bool ReadMacros,
298 bool Complain,
299 std::string &SuggestedPredefines) override;
300
301 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
302 bool needsInputFileVisitation() override;
303 bool needsSystemInputFileVisitation() override;
304 void visitModuleFile(StringRef Filename,
305 serialization::ModuleKind Kind) override;
306 bool visitInputFile(StringRef Filename, bool isSystem,
307 bool isOverridden, bool isExplicitModule) override;
308 void readModuleFileExtension(
309 const ModuleFileExtensionMetadata &Metadata) override;
310};
311
312/// ASTReaderListener implementation to validate the information of
313/// the PCH file against an initialized Preprocessor.
314class PCHValidator : public ASTReaderListener {
315 Preprocessor &PP;
316 ASTReader &Reader;
317
318public:
319 PCHValidator(Preprocessor &PP, ASTReader &Reader)
320 : PP(PP), Reader(Reader) {}
321
322 bool ReadLanguageOptions(const LangOptions &LangOpts,
323 StringRef ModuleFilename, bool Complain,
324 bool AllowCompatibleDifferences) override;
325 bool ReadTargetOptions(const TargetOptions &TargetOpts,
326 StringRef ModuleFilename, bool Complain,
327 bool AllowCompatibleDifferences) override;
328 bool ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
329 StringRef ModuleFilename, bool Complain) override;
330 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
331 StringRef ModuleFilename, bool ReadMacros,
332 bool Complain,
333 std::string &SuggestedPredefines) override;
334 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
335 StringRef ModuleFilename,
336 StringRef SpecificModuleCachePath,
337 bool Complain) override;
338 void ReadCounter(const serialization::ModuleFile &M, unsigned Value) override;
339};
340
341/// ASTReaderListenter implementation to set SuggestedPredefines of
342/// ASTReader which is required to use a pch file. This is the replacement
343/// of PCHValidator or SimplePCHValidator when using a pch file without
344/// validating it.
345class SimpleASTReaderListener : public ASTReaderListener {
346 Preprocessor &PP;
347
348public:
349 SimpleASTReaderListener(Preprocessor &PP) : PP(PP) {}
350
351 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
352 StringRef ModuleFilename, bool ReadMacros,
353 bool Complain,
354 std::string &SuggestedPredefines) override;
355};
356
357namespace serialization {
358
359class ReadMethodPoolVisitor;
360
361namespace reader {
362
363class ASTIdentifierLookupTrait;
364
365/// The on-disk hash table(s) used for DeclContext name lookup.
366struct DeclContextLookupTable;
367struct ModuleLocalLookupTable;
368
369/// The on-disk hash table(s) used for specialization decls.
370struct LazySpecializationInfoLookupTable;
371
372} // namespace reader
373
374} // namespace serialization
375
376struct VisibleLookupBlockOffsets {
377 uint64_t VisibleOffset = 0;
378 uint64_t ModuleLocalOffset = 0;
379 uint64_t TULocalOffset = 0;
380
381 operator bool() const {
382 return VisibleOffset || ModuleLocalOffset || TULocalOffset;
383 }
384};
385
386struct LookupBlockOffsets : VisibleLookupBlockOffsets {
387 uint64_t LexicalOffset = 0;
388
389 operator bool() const {
390 return VisibleLookupBlockOffsets::operator bool() || LexicalOffset;
391 }
392};
393
394/// Reads an AST files chain containing the contents of a translation
395/// unit.
396///
397/// The ASTReader class reads bitstreams (produced by the ASTWriter
398/// class) containing the serialized representation of a given
399/// abstract syntax tree and its supporting data structures. An
400/// instance of the ASTReader can be attached to an ASTContext object,
401/// which will provide access to the contents of the AST files.
402///
403/// The AST reader provides lazy de-serialization of declarations, as
404/// required when traversing the AST. Only those AST nodes that are
405/// actually required will be de-serialized.
406class ASTReader
407 : public ExternalPreprocessorSource,
408 public ExternalPreprocessingRecordSource,
409 public ExternalHeaderFileInfoSource,
410 public ExternalSemaSource,
411 public IdentifierInfoLookup,
412 public ExternalSLocEntrySource
413{
414public:
415 /// Types of AST files.
416 friend class ASTDeclMerger;
417 friend class ASTDeclReader;
418 friend class ASTIdentifierIterator;
419 friend class ASTRecordReader;
420 friend class ASTUnit; // ASTUnit needs to remap source locations.
421 friend class ASTWriter;
422 friend class PCHValidator;
423 friend class serialization::reader::ASTIdentifierLookupTrait;
424 friend class serialization::ReadMethodPoolVisitor;
425 friend class TypeLocReader;
426 friend class LocalDeclID;
427
428 using RecordData = SmallVector<uint64_t, 64>;
429 using RecordDataImpl = SmallVectorImpl<uint64_t>;
430
431 /// The result of reading the control block of an AST file, which
432 /// can fail for various reasons.
433 enum ASTReadResult {
434 /// The control block was read successfully. Aside from failures,
435 /// the AST file is safe to read into the current context.
436 Success,
437
438 /// The AST file itself appears corrupted.
439 Failure,
440
441 /// The AST file was missing.
442 Missing,
443
444 /// The AST file is out-of-date relative to its input files,
445 /// and needs to be regenerated.
446 OutOfDate,
447
448 /// The AST file was written by a different version of Clang.
449 VersionMismatch,
450
451 /// The AST file was written with a different language/target
452 /// configuration.
453 ConfigurationMismatch,
454
455 /// The AST file has errors.
456 HadErrors
457 };
458
459 using ModuleFile = serialization::ModuleFile;
460 using ModuleKind = serialization::ModuleKind;
461 using ModuleManager = serialization::ModuleManager;
462 using ModuleIterator = ModuleManager::ModuleIterator;
463 using ModuleConstIterator = ModuleManager::ModuleConstIterator;
464 using ModuleReverseIterator = ModuleManager::ModuleReverseIterator;
465
466private:
467 /// The receiver of some callbacks invoked by ASTReader.
468 std::unique_ptr<ASTReaderListener> Listener;
469
470 /// The receiver of deserialization events.
471 ASTDeserializationListener *DeserializationListener = nullptr;
472
473 bool OwnsDeserializationListener = false;
474
475 SourceManager &SourceMgr;
476 FileManager &FileMgr;
477 const PCHContainerReader &PCHContainerRdr;
478 DiagnosticsEngine &Diags;
479 // Sema has duplicate logic, but SemaObj can sometimes be null so ASTReader
480 // has its own version.
481 StackExhaustionHandler StackHandler;
482
483 /// The semantic analysis object that will be processing the
484 /// AST files and the translation unit that uses it.
485 Sema *SemaObj = nullptr;
486
487 /// The preprocessor that will be loading the source file.
488 Preprocessor &PP;
489
490 /// The AST context into which we'll read the AST files.
491 ASTContext *ContextObj = nullptr;
492
493 /// The AST consumer.
494 ASTConsumer *Consumer = nullptr;
495
496 /// The module manager which manages modules and their dependencies
497 ModuleManager ModuleMgr;
498
499 /// A dummy identifier resolver used to merge TU-scope declarations in
500 /// C, for the cases where we don't have a Sema object to provide a real
501 /// identifier resolver.
502 IdentifierResolver DummyIdResolver;
503
504 /// A mapping from extension block names to module file extensions.
505 llvm::StringMap<std::shared_ptr<ModuleFileExtension>> ModuleFileExtensions;
506
507 /// A timer used to track the time spent deserializing.
508 std::unique_ptr<llvm::Timer> ReadTimer;
509
510 /// The location where the module file will be considered as
511 /// imported from. For non-module AST types it should be invalid.
512 SourceLocation CurrentImportLoc;
513
514 /// The module kind that is currently deserializing.
515 std::optional<ModuleKind> CurrentDeserializingModuleKind;
516
517 /// The global module index, if loaded.
518 std::unique_ptr<GlobalModuleIndex> GlobalIndex;
519
520 /// A map of global bit offsets to the module that stores entities
521 /// at those bit offsets.
522 ContinuousRangeMap<uint64_t, ModuleFile*, 4> GlobalBitOffsetsMap;
523
524 /// A map of negated SLocEntryIDs to the modules containing them.
525 ContinuousRangeMap<unsigned, ModuleFile*, 64> GlobalSLocEntryMap;
526
527 using GlobalSLocOffsetMapType =
528 ContinuousRangeMap<SourceLocation::UIntTy, ModuleFile *, 64>;
529
530 /// A map of reversed (SourceManager::MaxLoadedOffset - SLocOffset)
531 /// SourceLocation offsets to the modules containing them.
532 GlobalSLocOffsetMapType GlobalSLocOffsetMap;
533
534 /// Types that have already been loaded from the chain.
535 ///
536 /// When the pointer at index I is non-NULL, the type with
537 /// ID = (I + 1) << FastQual::Width has already been loaded
538 llvm::PagedVector<QualType> TypesLoaded;
539
540 /// Declarations that have already been loaded from the chain.
541 ///
542 /// When the pointer at index I is non-NULL, the declaration with ID
543 /// = I + 1 has already been loaded.
544 llvm::PagedVector<Decl *> DeclsLoaded;
545
546 using FileOffset = std::pair<ModuleFile *, uint64_t>;
547 using FileOffsetsTy = SmallVector<FileOffset, 2>;
548 using DeclUpdateOffsetsMap = llvm::DenseMap<GlobalDeclID, FileOffsetsTy>;
549
550 /// Declarations that have modifications residing in a later file
551 /// in the chain.
552 DeclUpdateOffsetsMap DeclUpdateOffsets;
553
554 using DelayedNamespaceOffsetMapTy =
555 llvm::DenseMap<GlobalDeclID, LookupBlockOffsets>;
556
557 /// Mapping from global declaration IDs to the lexical and visible block
558 /// offset for delayed namespace in reduced BMI.
559 ///
560 /// We can't use the existing DeclUpdate mechanism since the DeclUpdate
561 /// may only be applied in an outer most read. However, we need to know
562 /// whether or not a DeclContext has external storage during the recursive
563 /// reading. So we need to apply the offset immediately after we read the
564 /// namespace as if it is not delayed.
565 DelayedNamespaceOffsetMapTy DelayedNamespaceOffsetMap;
566
567 /// Mapping from main decl ID to the related decls IDs.
568 ///
569 /// The key is the main decl ID, and the value is a vector of related decls
570 /// that must be loaded immediately after the main decl. This is necessary
571 /// to ensure that the definition for related decls comes from the same module
572 /// as the enclosing main decl. Without this, due to lazy deserialization,
573 /// the definition for the main decl and related decls may come from different
574 /// modules. It is used for the following cases:
575 /// - Lambda inside a template function definition: The main declaration is
576 /// the enclosing function, and the related declarations are the lambda
577 /// call operators.
578 /// - Friend function defined inside a template CXXRecord declaration: The
579 /// main declaration is the enclosing record, and the related declarations
580 /// are the friend functions.
581 llvm::DenseMap<GlobalDeclID, SmallVector<GlobalDeclID, 4>> RelatedDeclsMap;
582
583 struct PendingUpdateRecord {
584 Decl *D;
585 GlobalDeclID ID;
586
587 // Whether the declaration was just deserialized.
588 bool JustLoaded;
589
590 PendingUpdateRecord(GlobalDeclID ID, Decl *D, bool JustLoaded)
591 : D(D), ID(ID), JustLoaded(JustLoaded) {}
592 };
593
594 /// Declaration updates for already-loaded declarations that we need
595 /// to apply once we finish processing an import.
596 llvm::SmallVector<PendingUpdateRecord, 16> PendingUpdateRecords;
597
598 enum class PendingFakeDefinitionKind { NotFake, Fake, FakeLoaded };
599
600 /// The DefinitionData pointers that we faked up for class definitions
601 /// that we needed but hadn't loaded yet.
602 llvm::DenseMap<void *, PendingFakeDefinitionKind> PendingFakeDefinitionData;
603
604 /// Exception specification updates that have been loaded but not yet
605 /// propagated across the relevant redeclaration chain. The map key is the
606 /// canonical declaration (used only for deduplication) and the value is a
607 /// declaration that has an exception specification.
608 llvm::SmallMapVector<Decl *, FunctionDecl *, 4> PendingExceptionSpecUpdates;
609
610 /// Deduced return type updates that have been loaded but not yet propagated
611 /// across the relevant redeclaration chain. The map key is the canonical
612 /// declaration and the value is the deduced return type.
613 llvm::SmallMapVector<FunctionDecl *, QualType, 4> PendingDeducedTypeUpdates;
614
615 /// Functions has undededuced return type and we wish we can find the deduced
616 /// return type by iterating the redecls in other modules.
617 llvm::SmallVector<FunctionDecl *, 4> PendingUndeducedFunctionDecls;
618
619 /// Declarations that have been imported and have typedef names for
620 /// linkage purposes.
621 llvm::DenseMap<std::pair<DeclContext *, IdentifierInfo *>, NamedDecl *>
622 ImportedTypedefNamesForLinkage;
623
624 /// Mergeable declaration contexts that have anonymous declarations
625 /// within them, and those anonymous declarations.
626 llvm::DenseMap<Decl*, llvm::SmallVector<NamedDecl*, 2>>
627 AnonymousDeclarationsForMerging;
628
629 /// Map from numbering information for lambdas to the corresponding lambdas.
630 llvm::DenseMap<std::pair<const Decl *, unsigned>, NamedDecl *>
631 LambdaDeclarationsForMerging;
632
633 /// Key used to identify LifetimeExtendedTemporaryDecl for merging,
634 /// containing the lifetime-extending declaration and the mangling number.
635 using LETemporaryKey = std::pair<Decl *, unsigned>;
636
637 /// Map of already deserialiazed temporaries.
638 llvm::DenseMap<LETemporaryKey, LifetimeExtendedTemporaryDecl *>
639 LETemporaryForMerging;
640
641 struct FileDeclsInfo {
642 ModuleFile *Mod = nullptr;
643 ArrayRef<serialization::unaligned_decl_id_t> Decls;
644
645 FileDeclsInfo() = default;
646 FileDeclsInfo(ModuleFile *Mod,
647 ArrayRef<serialization::unaligned_decl_id_t> Decls)
648 : Mod(Mod), Decls(Decls) {}
649 };
650
651 /// Map from a FileID to the file-level declarations that it contains.
652 llvm::DenseMap<FileID, FileDeclsInfo> FileDeclIDs;
653
654 /// An array of lexical contents of a declaration context, as a sequence of
655 /// Decl::Kind, DeclID pairs.
656 using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>;
657
658 /// Map from a DeclContext to its lexical contents.
659 llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
660 LexicalDecls;
661
662 /// Map from the TU to its lexical contents from each module file.
663 std::vector<std::pair<ModuleFile*, LexicalContents>> TULexicalDecls;
664
665 /// Map from a DeclContext to its lookup tables.
666 llvm::DenseMap<const DeclContext *,
667 serialization::reader::DeclContextLookupTable> Lookups;
668 llvm::DenseMap<const DeclContext *,
669 serialization::reader::ModuleLocalLookupTable>
670 ModuleLocalLookups;
671 llvm::DenseMap<const DeclContext *,
672 serialization::reader::DeclContextLookupTable>
673 TULocalLookups;
674
675 using SpecLookupTableTy =
676 llvm::DenseMap<const Decl *,
677 serialization::reader::LazySpecializationInfoLookupTable>;
678 /// Map from decls to specialized decls.
679 SpecLookupTableTy SpecializationsLookups;
680 /// Split partial specialization from specialization to speed up lookups.
681 SpecLookupTableTy PartialSpecializationsLookups;
682
683 bool LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
684 const Decl *D);
685 bool LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
686 const Decl *D,
687 ArrayRef<TemplateArgument> TemplateArgs);
688
689 // Updates for visible decls can occur for other contexts than just the
690 // TU, and when we read those update records, the actual context may not
691 // be available yet, so have this pending map using the ID as a key. It
692 // will be realized when the data is actually loaded.
693 struct UpdateData {
694 ModuleFile *Mod;
695 const unsigned char *Data;
696 };
697 using DeclContextVisibleUpdates = SmallVector<UpdateData, 1>;
698
699 /// Updates to the visible declarations of declaration contexts that
700 /// haven't been loaded yet.
701 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> PendingVisibleUpdates;
702 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates>
703 PendingModuleLocalVisibleUpdates;
704 llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> TULocalUpdates;
705
706 using SpecializationsUpdate = SmallVector<UpdateData, 1>;
707 using SpecializationsUpdateMap =
708 llvm::DenseMap<GlobalDeclID, SpecializationsUpdate>;
709 SpecializationsUpdateMap PendingSpecializationsUpdates;
710 SpecializationsUpdateMap PendingPartialSpecializationsUpdates;
711
712 /// The set of C++ or Objective-C classes that have forward
713 /// declarations that have not yet been linked to their definitions.
714 llvm::SmallPtrSet<Decl *, 4> PendingDefinitions;
715
716 using PendingBodiesMap =
717 llvm::MapVector<Decl *, uint64_t,
718 llvm::SmallDenseMap<Decl *, unsigned, 4>,
719 SmallVector<std::pair<Decl *, uint64_t>, 4>>;
720
721 /// Functions or methods that have bodies that will be attached.
722 PendingBodiesMap PendingBodies;
723
724 /// Definitions for which we have added merged definitions but not yet
725 /// performed deduplication.
726 llvm::SetVector<NamedDecl *> PendingMergedDefinitionsToDeduplicate;
727
728 /// The duplicated definitions in module units which are pending to be warned.
729 /// We need to delay it to wait for the loading of definitions since we don't
730 /// want to warn for forward declarations.
731 llvm::SmallVector<std::pair<Decl *, Decl *>>
732 PendingWarningForDuplicatedDefsInModuleUnits;
733
734 /// Read the record that describes the lexical contents of a DC.
735 bool ReadLexicalDeclContextStorage(ModuleFile &M,
736 llvm::BitstreamCursor &Cursor,
737 uint64_t Offset, DeclContext *DC);
738
739 enum class VisibleDeclContextStorageKind {
740 GenerallyVisible,
741 ModuleLocalVisible,
742 TULocalVisible,
743 };
744
745 /// Read the record that describes the visible contents of a DC.
746 bool ReadVisibleDeclContextStorage(ModuleFile &M,
747 llvm::BitstreamCursor &Cursor,
748 uint64_t Offset, GlobalDeclID ID,
749 VisibleDeclContextStorageKind VisibleKind);
750
751 bool ReadSpecializations(ModuleFile &M, llvm::BitstreamCursor &Cursor,
752 uint64_t Offset, Decl *D, bool IsPartial);
753 void AddSpecializations(const Decl *D, const unsigned char *Data,
754 ModuleFile &M, bool IsPartial);
755
756 /// A vector containing identifiers that have already been
757 /// loaded.
758 ///
759 /// If the pointer at index I is non-NULL, then it refers to the
760 /// IdentifierInfo for the identifier with ID=I+1 that has already
761 /// been loaded.
762 std::vector<IdentifierInfo *> IdentifiersLoaded;
763
764 /// A vector containing macros that have already been
765 /// loaded.
766 ///
767 /// If the pointer at index I is non-NULL, then it refers to the
768 /// MacroInfo for the identifier with ID=I+1 that has already
769 /// been loaded.
770 std::vector<MacroInfo *> MacrosLoaded;
771
772 using LoadedMacroInfo =
773 std::pair<IdentifierInfo *, serialization::SubmoduleID>;
774
775 /// A set of #undef directives that we have loaded; used to
776 /// deduplicate the same #undef information coming from multiple module
777 /// files.
778 llvm::DenseSet<LoadedMacroInfo> LoadedUndefs;
779
780 using GlobalMacroMapType =
781 ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>;
782
783 /// Mapping from global macro IDs to the module in which the
784 /// macro resides along with the offset that should be added to the
785 /// global macro ID to produce a local ID.
786 GlobalMacroMapType GlobalMacroMap;
787
788 /// A vector containing submodules that have already been loaded.
789 ///
790 /// This vector is indexed by the Submodule ID (-1). NULL submodule entries
791 /// indicate that the particular submodule ID has not yet been loaded.
792 SmallVector<Module *, 2> SubmodulesLoaded;
793
794 using GlobalSubmoduleMapType =
795 ContinuousRangeMap<serialization::SubmoduleID, ModuleFile *, 4>;
796
797 /// Mapping from global submodule IDs to the module file in which the
798 /// submodule resides along with the offset that should be added to the
799 /// global submodule ID to produce a local ID.
800 GlobalSubmoduleMapType GlobalSubmoduleMap;
801
802 /// A set of hidden declarations.
803 using HiddenNames = SmallVector<Decl *, 2>;
804 using HiddenNamesMapType = llvm::DenseMap<Module *, HiddenNames>;
805
806 /// A mapping from each of the hidden submodules to the deserialized
807 /// declarations in that submodule that could be made visible.
808 HiddenNamesMapType HiddenNamesMap;
809
810 /// A module import, export, or conflict that hasn't yet been resolved.
811 struct UnresolvedModuleRef {
812 /// The file in which this module resides.
813 ModuleFile *File;
814
815 /// The module that is importing or exporting.
816 Module *Mod;
817
818 /// The kind of module reference.
819 enum { Import, Export, Conflict, Affecting } Kind;
820
821 /// The local ID of the module that is being exported.
822 unsigned ID;
823
824 /// Whether this is a wildcard export.
825 LLVM_PREFERRED_TYPE(bool)
826 unsigned IsWildcard : 1;
827
828 /// String data.
829 StringRef String;
830 };
831
832 /// The set of module imports and exports that still need to be
833 /// resolved.
834 SmallVector<UnresolvedModuleRef, 2> UnresolvedModuleRefs;
835
836 /// A vector containing selectors that have already been loaded.
837 ///
838 /// This vector is indexed by the Selector ID (-1). NULL selector
839 /// entries indicate that the particular selector ID has not yet
840 /// been loaded.
841 SmallVector<Selector, 16> SelectorsLoaded;
842
843 using GlobalSelectorMapType =
844 ContinuousRangeMap<serialization::SelectorID, ModuleFile *, 4>;
845
846 /// Mapping from global selector IDs to the module in which the
847 /// global selector ID to produce a local ID.
848 GlobalSelectorMapType GlobalSelectorMap;
849
850 /// The generation number of the last time we loaded data from the
851 /// global method pool for this selector.
852 llvm::DenseMap<Selector, unsigned> SelectorGeneration;
853
854 /// Whether a selector is out of date. We mark a selector as out of date
855 /// if we load another module after the method pool entry was pulled in.
856 llvm::DenseMap<Selector, bool> SelectorOutOfDate;
857
858 struct PendingMacroInfo {
859 ModuleFile *M;
860 /// Offset relative to ModuleFile::MacroOffsetsBase.
861 uint32_t MacroDirectivesOffset;
862
863 PendingMacroInfo(ModuleFile *M, uint32_t MacroDirectivesOffset)
864 : M(M), MacroDirectivesOffset(MacroDirectivesOffset) {}
865 };
866
867 using PendingMacroIDsMap =
868 llvm::MapVector<IdentifierInfo *, SmallVector<PendingMacroInfo, 2>>;
869
870 /// Mapping from identifiers that have a macro history to the global
871 /// IDs have not yet been deserialized to the global IDs of those macros.
872 PendingMacroIDsMap PendingMacroIDs;
873
874 using GlobalPreprocessedEntityMapType =
875 ContinuousRangeMap<unsigned, ModuleFile *, 4>;
876
877 /// Mapping from global preprocessing entity IDs to the module in
878 /// which the preprocessed entity resides along with the offset that should be
879 /// added to the global preprocessing entity ID to produce a local ID.
880 GlobalPreprocessedEntityMapType GlobalPreprocessedEntityMap;
881
882 using GlobalSkippedRangeMapType =
883 ContinuousRangeMap<unsigned, ModuleFile *, 4>;
884
885 /// Mapping from global skipped range base IDs to the module in which
886 /// the skipped ranges reside.
887 GlobalSkippedRangeMapType GlobalSkippedRangeMap;
888
889 /// \name CodeGen-relevant special data
890 /// Fields containing data that is relevant to CodeGen.
891 //@{
892
893 /// The IDs of all declarations that fulfill the criteria of
894 /// "interesting" decls.
895 ///
896 /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
897 /// in the chain. The referenced declarations are deserialized and passed to
898 /// the consumer eagerly.
899 SmallVector<GlobalDeclID, 16> EagerlyDeserializedDecls;
900
901 /// The IDs of all vtables to emit. The referenced declarations are passed
902 /// to the consumers' HandleVTable eagerly after passing
903 /// EagerlyDeserializedDecls.
904 SmallVector<GlobalDeclID, 16> VTablesToEmit;
905
906 /// The IDs of all tentative definitions stored in the chain.
907 ///
908 /// Sema keeps track of all tentative definitions in a TU because it has to
909 /// complete them and pass them on to CodeGen. Thus, tentative definitions in
910 /// the PCH chain must be eagerly deserialized.
911 SmallVector<GlobalDeclID, 16> TentativeDefinitions;
912
913 /// The IDs of all CXXRecordDecls stored in the chain whose VTables are
914 /// used.
915 ///
916 /// CodeGen has to emit VTables for these records, so they have to be eagerly
917 /// deserialized.
918 struct VTableUse {
919 GlobalDeclID ID;
920 SourceLocation::UIntTy RawLoc;
921 bool Used;
922 };
923 SmallVector<VTableUse> VTableUses;
924
925 /// A snapshot of the pending instantiations in the chain.
926 ///
927 /// This record tracks the instantiations that Sema has to perform at the
928 /// end of the TU. It consists of a pair of values for every pending
929 /// instantiation where the first value is the ID of the decl and the second
930 /// is the instantiation location.
931 struct PendingInstantiation {
932 GlobalDeclID ID;
933 SourceLocation::UIntTy RawLoc;
934 };
935 SmallVector<PendingInstantiation, 64> PendingInstantiations;
936
937 //@}
938
939 /// \name DiagnosticsEngine-relevant special data
940 /// Fields containing data that is used for generating diagnostics
941 //@{
942
943 /// A snapshot of Sema's unused file-scoped variable tracking, for
944 /// generating warnings.
945 SmallVector<GlobalDeclID, 16> UnusedFileScopedDecls;
946
947 /// A list of all the delegating constructors we've seen, to diagnose
948 /// cycles.
949 SmallVector<GlobalDeclID, 4> DelegatingCtorDecls;
950
951 /// Method selectors used in a @selector expression. Used for
952 /// implementation of -Wselector.
953 SmallVector<serialization::SelectorID, 64> ReferencedSelectorsData;
954
955 /// A snapshot of Sema's weak undeclared identifier tracking, for
956 /// generating warnings.
957 SmallVector<serialization::IdentifierID, 64> WeakUndeclaredIdentifiers;
958
959 /// The IDs of type aliases for ext_vectors that exist in the chain.
960 ///
961 /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
962 SmallVector<GlobalDeclID, 4> ExtVectorDecls;
963
964 //@}
965
966 /// \name Sema-relevant special data
967 /// Fields containing data that is used for semantic analysis
968 //@{
969
970 /// The IDs of all potentially unused typedef names in the chain.
971 ///
972 /// Sema tracks these to emit warnings.
973 SmallVector<GlobalDeclID, 16> UnusedLocalTypedefNameCandidates;
974
975 /// Our current depth in #pragma cuda force_host_device begin/end
976 /// macros.
977 unsigned ForceHostDeviceDepth = 0;
978
979 /// The IDs of the declarations Sema stores directly.
980 ///
981 /// Sema tracks a few important decls, such as namespace std, directly.
982 SmallVector<GlobalDeclID, 4> SemaDeclRefs;
983
984 /// The IDs of the types ASTContext stores directly.
985 ///
986 /// The AST context tracks a few important types, such as va_list, directly.
987 SmallVector<serialization::TypeID, 16> SpecialTypes;
988
989 /// The IDs of CUDA-specific declarations ASTContext stores directly.
990 ///
991 /// The AST context tracks a few important decls, currently cudaConfigureCall,
992 /// directly.
993 SmallVector<GlobalDeclID, 2> CUDASpecialDeclRefs;
994
995 /// The floating point pragma option settings.
996 SmallVector<uint64_t, 1> FPPragmaOptions;
997
998 /// The pragma clang optimize location (if the pragma state is "off").
999 SourceLocation OptimizeOffPragmaLocation;
1000
1001 /// The PragmaMSStructKind pragma ms_struct state if set, or -1.
1002 int PragmaMSStructState = -1;
1003
1004 /// The PragmaMSPointersToMembersKind pragma pointers_to_members state.
1005 int PragmaMSPointersToMembersState = -1;
1006 SourceLocation PointersToMembersPragmaLocation;
1007
1008 /// The pragma float_control state.
1009 std::optional<FPOptionsOverride> FpPragmaCurrentValue;
1010 SourceLocation FpPragmaCurrentLocation;
1011 struct FpPragmaStackEntry {
1012 FPOptionsOverride Value;
1013 SourceLocation Location;
1014 SourceLocation PushLocation;
1015 StringRef SlotLabel;
1016 };
1017 llvm::SmallVector<FpPragmaStackEntry, 2> FpPragmaStack;
1018 llvm::SmallVector<std::string, 2> FpPragmaStrings;
1019
1020 /// The pragma align/pack state.
1021 std::optional<Sema::AlignPackInfo> PragmaAlignPackCurrentValue;
1022 SourceLocation PragmaAlignPackCurrentLocation;
1023 struct PragmaAlignPackStackEntry {
1024 Sema::AlignPackInfo Value;
1025 SourceLocation Location;
1026 SourceLocation PushLocation;
1027 StringRef SlotLabel;
1028 };
1029 llvm::SmallVector<PragmaAlignPackStackEntry, 2> PragmaAlignPackStack;
1030 llvm::SmallVector<std::string, 2> PragmaAlignPackStrings;
1031
1032 /// The OpenCL extension settings.
1033 OpenCLOptions OpenCLExtensions;
1034
1035 /// Extensions required by an OpenCL type.
1036 llvm::DenseMap<const Type *, std::set<std::string>> OpenCLTypeExtMap;
1037
1038 /// Extensions required by an OpenCL declaration.
1039 llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap;
1040
1041 /// A list of the namespaces we've seen.
1042 SmallVector<GlobalDeclID, 4> KnownNamespaces;
1043
1044 /// A list of undefined decls with internal linkage followed by the
1045 /// SourceLocation of a matching ODR-use.
1046 struct UndefinedButUsedDecl {
1047 GlobalDeclID ID;
1048 SourceLocation::UIntTy RawLoc;
1049 };
1050 SmallVector<UndefinedButUsedDecl, 8> UndefinedButUsed;
1051
1052 /// Delete expressions to analyze at the end of translation unit.
1053 SmallVector<uint64_t, 8> DelayedDeleteExprs;
1054
1055 // A list of late parsed template function data with their module files.
1056 SmallVector<std::pair<ModuleFile *, SmallVector<uint64_t, 1>>, 4>
1057 LateParsedTemplates;
1058
1059 /// The IDs of all decls to be checked for deferred diags.
1060 ///
1061 /// Sema tracks these to emit deferred diags.
1062 llvm::SmallSetVector<GlobalDeclID, 4> DeclsToCheckForDeferredDiags;
1063
1064 /// The IDs of all decls with function effects to be checked.
1065 SmallVector<GlobalDeclID> DeclsWithEffectsToVerify;
1066
1067private:
1068 struct ImportedSubmodule {
1069 serialization::SubmoduleID ID;
1070 SourceLocation ImportLoc;
1071
1072 ImportedSubmodule(serialization::SubmoduleID ID, SourceLocation ImportLoc)
1073 : ID(ID), ImportLoc(ImportLoc) {}
1074 };
1075
1076 /// A list of modules that were imported by precompiled headers or
1077 /// any other non-module AST file and have not yet been made visible. If a
1078 /// module is made visible in the ASTReader, it will be transfered to
1079 /// \c PendingImportedModulesSema.
1080 SmallVector<ImportedSubmodule, 2> PendingImportedModules;
1081
1082 /// A list of modules that were imported by precompiled headers or
1083 /// any other non-module AST file and have not yet been made visible for Sema.
1084 SmallVector<ImportedSubmodule, 2> PendingImportedModulesSema;
1085 //@}
1086
1087 /// The system include root to be used when loading the
1088 /// precompiled header.
1089 std::string isysroot;
1090
1091 /// Whether to disable the normal validation performed on precompiled
1092 /// headers and module files when they are loaded.
1093 DisableValidationForModuleKind DisableValidationKind;
1094
1095 /// Whether to accept an AST file with compiler errors.
1096 bool AllowASTWithCompilerErrors;
1097
1098 /// Whether to accept an AST file that has a different configuration
1099 /// from the current compiler instance.
1100 bool AllowConfigurationMismatch;
1101
1102 /// Whether to validate system input files.
1103 bool ValidateSystemInputs;
1104
1105 /// Whether to force the validation of user input files.
1106 bool ForceValidateUserInputs;
1107
1108 /// Whether validate headers and module maps using hash based on contents.
1109 bool ValidateASTInputFilesContent;
1110
1111 /// Whether we are allowed to use the global module index.
1112 bool UseGlobalIndex;
1113
1114 /// Whether we have tried loading the global module index yet.
1115 bool TriedLoadingGlobalIndex = false;
1116
1117 ///Whether we are currently processing update records.
1118 bool ProcessingUpdateRecords = false;
1119
1120 using SwitchCaseMapTy = llvm::DenseMap<unsigned, SwitchCase *>;
1121
1122 /// Mapping from switch-case IDs in the chain to switch-case statements
1123 ///
1124 /// Statements usually don't have IDs, but switch cases need them, so that the
1125 /// switch statement can refer to them.
1126 SwitchCaseMapTy SwitchCaseStmts;
1127
1128 SwitchCaseMapTy *CurrSwitchCaseStmts;
1129
1130 /// The number of source location entries de-serialized from
1131 /// the PCH file.
1132 unsigned NumSLocEntriesRead = 0;
1133
1134 /// The number of source location entries in the chain.
1135 unsigned TotalNumSLocEntries = 0;
1136
1137 /// The number of statements (and expressions) de-serialized
1138 /// from the chain.
1139 unsigned NumStatementsRead = 0;
1140
1141 /// The total number of statements (and expressions) stored
1142 /// in the chain.
1143 unsigned TotalNumStatements = 0;
1144
1145 /// The number of macros de-serialized from the chain.
1146 unsigned NumMacrosRead = 0;
1147
1148 /// The total number of macros stored in the chain.
1149 unsigned TotalNumMacros = 0;
1150
1151 /// The number of lookups into identifier tables.
1152 unsigned NumIdentifierLookups = 0;
1153
1154 /// The number of lookups into identifier tables that succeed.
1155 unsigned NumIdentifierLookupHits = 0;
1156
1157 /// The number of selectors that have been read.
1158 unsigned NumSelectorsRead = 0;
1159
1160 /// The number of method pool entries that have been read.
1161 unsigned NumMethodPoolEntriesRead = 0;
1162
1163 /// The number of times we have looked up a selector in the method
1164 /// pool.
1165 unsigned NumMethodPoolLookups = 0;
1166
1167 /// The number of times we have looked up a selector in the method
1168 /// pool and found something.
1169 unsigned NumMethodPoolHits = 0;
1170
1171 /// The number of times we have looked up a selector in the method
1172 /// pool within a specific module.
1173 unsigned NumMethodPoolTableLookups = 0;
1174
1175 /// The number of times we have looked up a selector in the method
1176 /// pool within a specific module and found something.
1177 unsigned NumMethodPoolTableHits = 0;
1178
1179 /// The total number of method pool entries in the selector table.
1180 unsigned TotalNumMethodPoolEntries = 0;
1181
1182 /// Number of lexical decl contexts read/total.
1183 unsigned NumLexicalDeclContextsRead = 0, TotalLexicalDeclContexts = 0;
1184
1185 /// Number of visible decl contexts read/total.
1186 unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0;
1187
1188 /// Number of module local visible decl contexts read/total.
1189 unsigned NumModuleLocalVisibleDeclContexts = 0,
1190 TotalModuleLocalVisibleDeclContexts = 0;
1191
1192 /// Number of TU Local decl contexts read/total
1193 unsigned NumTULocalVisibleDeclContexts = 0,
1194 TotalTULocalVisibleDeclContexts = 0;
1195
1196 /// Total size of modules, in bits, currently loaded
1197 uint64_t TotalModulesSizeInBits = 0;
1198
1199 /// Number of Decl/types that are currently deserializing.
1200 unsigned NumCurrentElementsDeserializing = 0;
1201
1202 /// Set false while we are in a state where we cannot safely pass deserialized
1203 /// "interesting" decls to the consumer inside FinishedDeserializing().
1204 /// This is used as a guard to avoid recursively entering the process of
1205 /// passing decls to consumer.
1206 bool CanPassDeclsToConsumer = true;
1207
1208 /// The set of identifiers that were read while the AST reader was
1209 /// (recursively) loading declarations.
1210 ///
1211 /// The declarations on the identifier chain for these identifiers will be
1212 /// loaded once the recursive loading has completed.
1213 llvm::MapVector<IdentifierInfo *, SmallVector<GlobalDeclID, 4>>
1214 PendingIdentifierInfos;
1215
1216 /// The set of lookup results that we have faked in order to support
1217 /// merging of partially deserialized decls but that we have not yet removed.
1218 llvm::SmallMapVector<const IdentifierInfo *, SmallVector<NamedDecl *, 2>, 16>
1219 PendingFakeLookupResults;
1220
1221 /// The generation number of each identifier, which keeps track of
1222 /// the last time we loaded information about this identifier.
1223 llvm::DenseMap<const IdentifierInfo *, unsigned> IdentifierGeneration;
1224
1225 /// Contains declarations and definitions that could be
1226 /// "interesting" to the ASTConsumer, when we get that AST consumer.
1227 ///
1228 /// "Interesting" declarations are those that have data that may
1229 /// need to be emitted, such as inline function definitions or
1230 /// Objective-C protocols.
1231 std::deque<Decl *> PotentiallyInterestingDecls;
1232
1233 /// The list of deduced function types that we have not yet read, because
1234 /// they might contain a deduced return type that refers to a local type
1235 /// declared within the function.
1236 SmallVector<std::pair<FunctionDecl *, serialization::TypeID>, 16>
1237 PendingDeducedFunctionTypes;
1238
1239 /// The list of deduced variable types that we have not yet read, because
1240 /// they might contain a deduced type that refers to a local type declared
1241 /// within the variable.
1242 SmallVector<std::pair<VarDecl *, serialization::TypeID>, 16>
1243 PendingDeducedVarTypes;
1244
1245 /// The list of redeclaration chains that still need to be
1246 /// reconstructed, and the local offset to the corresponding list
1247 /// of redeclarations.
1248 SmallVector<std::pair<Decl *, uint64_t>, 16> PendingDeclChains;
1249
1250 /// The list of canonical declarations whose redeclaration chains
1251 /// need to be marked as incomplete once we're done deserializing things.
1252 SmallVector<Decl *, 16> PendingIncompleteDeclChains;
1253
1254 /// The Decl IDs for the Sema/Lexical DeclContext of a Decl that has
1255 /// been loaded but its DeclContext was not set yet.
1256 struct PendingDeclContextInfo {
1257 Decl *D;
1258 GlobalDeclID SemaDC;
1259 GlobalDeclID LexicalDC;
1260 };
1261
1262 /// The set of Decls that have been loaded but their DeclContexts are
1263 /// not set yet.
1264 ///
1265 /// The DeclContexts for these Decls will be set once recursive loading has
1266 /// been completed.
1267 std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
1268
1269 template <typename DeclTy>
1270 using DuplicateObjCDecls = std::pair<DeclTy *, DeclTy *>;
1271
1272 /// When resolving duplicate ivars from Objective-C extensions we don't error
1273 /// out immediately but check if can merge identical extensions. Not checking
1274 /// extensions for equality immediately because ivar deserialization isn't
1275 /// over yet at that point.
1276 llvm::SmallMapVector<DuplicateObjCDecls<ObjCCategoryDecl>,
1277 llvm::SmallVector<DuplicateObjCDecls<ObjCIvarDecl>, 4>,
1278 2>
1279 PendingObjCExtensionIvarRedeclarations;
1280
1281 /// Members that have been added to classes, for which the class has not yet
1282 /// been notified. CXXRecordDecl::addedMember will be called for each of
1283 /// these once recursive deserialization is complete.
1284 SmallVector<std::pair<CXXRecordDecl*, Decl*>, 4> PendingAddedClassMembers;
1285
1286 /// The set of NamedDecls that have been loaded, but are members of a
1287 /// context that has been merged into another context where the corresponding
1288 /// declaration is either missing or has not yet been loaded.
1289 ///
1290 /// We will check whether the corresponding declaration is in fact missing
1291 /// once recursing loading has been completed.
1292 llvm::SmallVector<NamedDecl *, 16> PendingOdrMergeChecks;
1293
1294 using DataPointers =
1295 std::pair<CXXRecordDecl *, struct CXXRecordDecl::DefinitionData *>;
1296 using ObjCInterfaceDataPointers =
1297 std::pair<ObjCInterfaceDecl *,
1298 struct ObjCInterfaceDecl::DefinitionData *>;
1299 using ObjCProtocolDataPointers =
1300 std::pair<ObjCProtocolDecl *, struct ObjCProtocolDecl::DefinitionData *>;
1301
1302 /// Record definitions in which we found an ODR violation.
1303 llvm::SmallDenseMap<CXXRecordDecl *, llvm::SmallVector<DataPointers, 2>, 2>
1304 PendingOdrMergeFailures;
1305
1306 /// C/ObjC record definitions in which we found an ODR violation.
1307 llvm::SmallDenseMap<RecordDecl *, llvm::SmallVector<RecordDecl *, 2>, 2>
1308 PendingRecordOdrMergeFailures;
1309
1310 /// Function definitions in which we found an ODR violation.
1311 llvm::SmallDenseMap<FunctionDecl *, llvm::SmallVector<FunctionDecl *, 2>, 2>
1312 PendingFunctionOdrMergeFailures;
1313
1314 /// Enum definitions in which we found an ODR violation.
1315 llvm::SmallDenseMap<EnumDecl *, llvm::SmallVector<EnumDecl *, 2>, 2>
1316 PendingEnumOdrMergeFailures;
1317
1318 /// ObjCInterfaceDecl in which we found an ODR violation.
1319 llvm::SmallDenseMap<ObjCInterfaceDecl *,
1320 llvm::SmallVector<ObjCInterfaceDataPointers, 2>, 2>
1321 PendingObjCInterfaceOdrMergeFailures;
1322
1323 /// ObjCProtocolDecl in which we found an ODR violation.
1324 llvm::SmallDenseMap<ObjCProtocolDecl *,
1325 llvm::SmallVector<ObjCProtocolDataPointers, 2>, 2>
1326 PendingObjCProtocolOdrMergeFailures;
1327
1328 /// DeclContexts in which we have diagnosed an ODR violation.
1329 llvm::SmallPtrSet<DeclContext*, 2> DiagnosedOdrMergeFailures;
1330
1331 /// The set of Objective-C categories that have been deserialized
1332 /// since the last time the declaration chains were linked.
1333 llvm::SmallPtrSet<ObjCCategoryDecl *, 16> CategoriesDeserialized;
1334
1335 /// The set of Objective-C class definitions that have already been
1336 /// loaded, for which we will need to check for categories whenever a new
1337 /// module is loaded.
1338 SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
1339
1340 using KeyDeclsMap = llvm::DenseMap<Decl *, SmallVector<GlobalDeclID, 2>>;
1341
1342 /// A mapping from canonical declarations to the set of global
1343 /// declaration IDs for key declaration that have been merged with that
1344 /// canonical declaration. A key declaration is a formerly-canonical
1345 /// declaration whose module did not import any other key declaration for that
1346 /// entity. These are the IDs that we use as keys when finding redecl chains.
1347 KeyDeclsMap KeyDecls;
1348
1349 /// A mapping from DeclContexts to the semantic DeclContext that we
1350 /// are treating as the definition of the entity. This is used, for instance,
1351 /// when merging implicit instantiations of class templates across modules.
1352 llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
1353
1354 /// A mapping from canonical declarations of enums to their canonical
1355 /// definitions. Only populated when using modules in C++.
1356 llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
1357
1358 /// A mapping from canonical declarations of records to their canonical
1359 /// definitions. Doesn't cover CXXRecordDecl.
1360 llvm::DenseMap<RecordDecl *, RecordDecl *> RecordDefinitions;
1361
1362 /// When reading a Stmt tree, Stmt operands are placed in this stack.
1363 SmallVector<Stmt *, 16> StmtStack;
1364
1365 /// What kind of records we are reading.
1366 enum ReadingKind {
1367 Read_None, Read_Decl, Read_Type, Read_Stmt
1368 };
1369
1370 /// What kind of records we are reading.
1371 ReadingKind ReadingKind = Read_None;
1372
1373 /// RAII object to change the reading kind.
1374 class ReadingKindTracker {
1375 ASTReader &Reader;
1376 enum ReadingKind PrevKind;
1377
1378 public:
1379 ReadingKindTracker(enum ReadingKind newKind, ASTReader &reader)
1380 : Reader(reader), PrevKind(Reader.ReadingKind) {
1381 Reader.ReadingKind = newKind;
1382 }
1383
1384 ReadingKindTracker(const ReadingKindTracker &) = delete;
1385 ReadingKindTracker &operator=(const ReadingKindTracker &) = delete;
1386 ~ReadingKindTracker() { Reader.ReadingKind = PrevKind; }
1387 };
1388
1389 /// RAII object to mark the start of processing updates.
1390 class ProcessingUpdatesRAIIObj {
1391 ASTReader &Reader;
1392 bool PrevState;
1393
1394 public:
1395 ProcessingUpdatesRAIIObj(ASTReader &reader)
1396 : Reader(reader), PrevState(Reader.ProcessingUpdateRecords) {
1397 Reader.ProcessingUpdateRecords = true;
1398 }
1399
1400 ProcessingUpdatesRAIIObj(const ProcessingUpdatesRAIIObj &) = delete;
1401 ProcessingUpdatesRAIIObj &
1402 operator=(const ProcessingUpdatesRAIIObj &) = delete;
1403 ~ProcessingUpdatesRAIIObj() { Reader.ProcessingUpdateRecords = PrevState; }
1404 };
1405
1406 /// Suggested contents of the predefines buffer, after this
1407 /// PCH file has been processed.
1408 ///
1409 /// In most cases, this string will be empty, because the predefines
1410 /// buffer computed to build the PCH file will be identical to the
1411 /// predefines buffer computed from the command line. However, when
1412 /// there are differences that the PCH reader can work around, this
1413 /// predefines buffer may contain additional definitions.
1414 std::string SuggestedPredefines;
1415
1416 llvm::DenseMap<const Decl *, bool> DefinitionSource;
1417
1418 /// Friend functions that were defined but might have had their bodies
1419 /// removed.
1420 llvm::DenseSet<const FunctionDecl *> ThisDeclarationWasADefinitionSet;
1421
1422 bool shouldDisableValidationForFile(const serialization::ModuleFile &M) const;
1423
1424 /// Reads a statement from the specified cursor.
1425 Stmt *ReadStmtFromStream(ModuleFile &F);
1426
1427 /// Retrieve the stored information about an input file.
1428 serialization::InputFileInfo getInputFileInfo(ModuleFile &F, unsigned ID);
1429
1430 /// Retrieve the file entry and 'overridden' bit for an input
1431 /// file in the given module file.
1432 serialization::InputFile getInputFile(ModuleFile &F, unsigned ID,
1433 bool Complain = true);
1434
1435 /// The buffer used as the temporary backing storage for resolved paths.
1436 SmallString<0> PathBuf;
1437
1438 /// A wrapper around StringRef that temporarily borrows the underlying buffer.
1439 class TemporarilyOwnedStringRef {
1440 StringRef String;
1441 llvm::SaveAndRestore<SmallString<0>> UnderlyingBuffer;
1442
1443 public:
1444 TemporarilyOwnedStringRef(StringRef S, SmallString<0> &UnderlyingBuffer)
1445 : String(S), UnderlyingBuffer(UnderlyingBuffer, {}) {}
1446
1447 /// Return the wrapped \c StringRef that must be outlived by \c this.
1448 const StringRef *operator->() const & { return &String; }
1449 const StringRef &operator*() const & { return String; }
1450
1451 /// Make it harder to get a \c StringRef that outlives \c this.
1452 const StringRef *operator->() && = delete;
1453 const StringRef &operator*() && = delete;
1454 };
1455
1456public:
1457 /// Get the buffer for resolving paths.
1458 SmallString<0> &getPathBuf() { return PathBuf; }
1459
1460 /// Resolve \c Path in the context of module file \c M. The return value
1461 /// must go out of scope before the next call to \c ResolveImportedPath.
1462 static TemporarilyOwnedStringRef
1463 ResolveImportedPath(SmallString<0> &Buf, StringRef Path, ModuleFile &ModF);
1464 /// Resolve \c Path in the context of the \c Prefix directory. The return
1465 /// value must go out of scope before the next call to \c ResolveImportedPath.
1466 static TemporarilyOwnedStringRef
1467 ResolveImportedPath(SmallString<0> &Buf, StringRef Path, StringRef Prefix);
1468
1469 /// Resolve \c Path in the context of module file \c M.
1470 static std::string ResolveImportedPathAndAllocate(SmallString<0> &Buf,
1471 StringRef Path,
1472 ModuleFile &ModF);
1473 /// Resolve \c Path in the context of the \c Prefix directory.
1474 static std::string ResolveImportedPathAndAllocate(SmallString<0> &Buf,
1475 StringRef Path,
1476 StringRef Prefix);
1477
1478 /// Returns the first key declaration for the given declaration. This
1479 /// is one that is formerly-canonical (or still canonical) and whose module
1480 /// did not import any other key declaration of the entity.
1481 Decl *getKeyDeclaration(Decl *D) {
1482 D = D->getCanonicalDecl();
1483 if (D->isFromASTFile())
1484 return D;
1485
1486 auto I = KeyDecls.find(Val: D);
1487 if (I == KeyDecls.end() || I->second.empty())
1488 return D;
1489 return GetExistingDecl(ID: I->second[0]);
1490 }
1491 const Decl *getKeyDeclaration(const Decl *D) {
1492 return getKeyDeclaration(D: const_cast<Decl*>(D));
1493 }
1494
1495 /// Run a callback on each imported key declaration of \p D.
1496 template <typename Fn>
1497 void forEachImportedKeyDecl(const Decl *D, Fn Visit) {
1498 D = D->getCanonicalDecl();
1499 if (D->isFromASTFile())
1500 Visit(D);
1501
1502 auto It = KeyDecls.find(Val: const_cast<Decl*>(D));
1503 if (It != KeyDecls.end())
1504 for (auto ID : It->second)
1505 Visit(GetExistingDecl(ID));
1506 }
1507
1508 /// Get the loaded lookup tables for \p Primary, if any.
1509 const serialization::reader::DeclContextLookupTable *
1510 getLoadedLookupTables(DeclContext *Primary) const;
1511
1512 const serialization::reader::ModuleLocalLookupTable *
1513 getModuleLocalLookupTables(DeclContext *Primary) const;
1514
1515 const serialization::reader::DeclContextLookupTable *
1516 getTULocalLookupTables(DeclContext *Primary) const;
1517
1518 /// Get the loaded specializations lookup tables for \p D,
1519 /// if any.
1520 serialization::reader::LazySpecializationInfoLookupTable *
1521 getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial);
1522
1523 /// If we have any unloaded specialization for \p D
1524 bool haveUnloadedSpecializations(const Decl *D) const;
1525
1526private:
1527 struct ImportedModule {
1528 ModuleFile *Mod;
1529 ModuleFile *ImportedBy;
1530 SourceLocation ImportLoc;
1531
1532 ImportedModule(ModuleFile *Mod,
1533 ModuleFile *ImportedBy,
1534 SourceLocation ImportLoc)
1535 : Mod(Mod), ImportedBy(ImportedBy), ImportLoc(ImportLoc) {}
1536 };
1537
1538 ASTReadResult ReadASTCore(StringRef FileName, ModuleKind Type,
1539 SourceLocation ImportLoc, ModuleFile *ImportedBy,
1540 SmallVectorImpl<ImportedModule> &Loaded,
1541 off_t ExpectedSize, time_t ExpectedModTime,
1542 ASTFileSignature ExpectedSignature,
1543 unsigned ClientLoadCapabilities);
1544 ASTReadResult ReadControlBlock(ModuleFile &F,
1545 SmallVectorImpl<ImportedModule> &Loaded,
1546 const ModuleFile *ImportedBy,
1547 unsigned ClientLoadCapabilities);
1548 static ASTReadResult
1549 ReadOptionsBlock(llvm::BitstreamCursor &Stream, StringRef Filename,
1550 unsigned ClientLoadCapabilities,
1551 bool AllowCompatibleConfigurationMismatch,
1552 ASTReaderListener &Listener,
1553 std::string &SuggestedPredefines);
1554
1555 /// Read the unhashed control block.
1556 ///
1557 /// This has no effect on \c F.Stream, instead creating a fresh cursor from
1558 /// \c F.Data and reading ahead.
1559 ASTReadResult readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
1560 unsigned ClientLoadCapabilities);
1561
1562 static ASTReadResult readUnhashedControlBlockImpl(
1563 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
1564 unsigned ClientLoadCapabilities,
1565 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
1566 bool ValidateDiagnosticOptions);
1567
1568 llvm::Error ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities);
1569 llvm::Error ReadExtensionBlock(ModuleFile &F);
1570 void ReadModuleOffsetMap(ModuleFile &F) const;
1571 void ParseLineTable(ModuleFile &F, const RecordData &Record);
1572 llvm::Error ReadSourceManagerBlock(ModuleFile &F);
1573 SourceLocation getImportLocation(ModuleFile *F);
1574 ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
1575 const ModuleFile *ImportedBy,
1576 unsigned ClientLoadCapabilities);
1577 llvm::Error ReadSubmoduleBlock(ModuleFile &F,
1578 unsigned ClientLoadCapabilities);
1579 static bool ParseLanguageOptions(const RecordData &Record,
1580 StringRef ModuleFilename, bool Complain,
1581 ASTReaderListener &Listener,
1582 bool AllowCompatibleDifferences);
1583 static bool ParseTargetOptions(const RecordData &Record,
1584 StringRef ModuleFilename, bool Complain,
1585 ASTReaderListener &Listener,
1586 bool AllowCompatibleDifferences);
1587 static bool ParseDiagnosticOptions(const RecordData &Record,
1588 StringRef ModuleFilename, bool Complain,
1589 ASTReaderListener &Listener);
1590 static bool ParseFileSystemOptions(const RecordData &Record, bool Complain,
1591 ASTReaderListener &Listener);
1592 static bool ParseHeaderSearchOptions(const RecordData &Record,
1593 StringRef ModuleFilename, bool Complain,
1594 ASTReaderListener &Listener);
1595 static bool ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
1596 ASTReaderListener &Listener);
1597 static bool ParsePreprocessorOptions(const RecordData &Record,
1598 StringRef ModuleFilename, bool Complain,
1599 ASTReaderListener &Listener,
1600 std::string &SuggestedPredefines);
1601
1602 struct RecordLocation {
1603 ModuleFile *F;
1604 uint64_t Offset;
1605
1606 RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {}
1607 };
1608
1609 QualType readTypeRecord(serialization::TypeID ID);
1610 RecordLocation TypeCursorForIndex(serialization::TypeID ID);
1611 void LoadedDecl(unsigned Index, Decl *D);
1612 Decl *ReadDeclRecord(GlobalDeclID ID);
1613 void markIncompleteDeclChain(Decl *D);
1614
1615 /// Returns the most recent declaration of a declaration (which must be
1616 /// of a redeclarable kind) that is either local or has already been loaded
1617 /// merged into its redecl chain.
1618 Decl *getMostRecentExistingDecl(Decl *D);
1619
1620 RecordLocation DeclCursorForID(GlobalDeclID ID, SourceLocation &Location);
1621 void loadDeclUpdateRecords(PendingUpdateRecord &Record);
1622 void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
1623 void loadObjCCategories(GlobalDeclID ID, ObjCInterfaceDecl *D,
1624 unsigned PreviousGeneration = 0);
1625
1626 RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
1627 uint64_t getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset);
1628
1629 /// Returns the first preprocessed entity ID that begins or ends after
1630 /// \arg Loc.
1631 serialization::PreprocessedEntityID
1632 findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1633
1634 /// Find the next module that contains entities and return the ID
1635 /// of the first entry.
1636 ///
1637 /// \param SLocMapI points at a chunk of a module that contains no
1638 /// preprocessed entities or the entities it contains are not the
1639 /// ones we are looking for.
1640 serialization::PreprocessedEntityID
1641 findNextPreprocessedEntity(
1642 GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1643
1644 /// Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
1645 /// preprocessed entity.
1646 std::pair<ModuleFile *, unsigned>
1647 getModulePreprocessedEntity(unsigned GlobalIndex);
1648
1649 /// Returns (begin, end) pair for the preprocessed entities of a
1650 /// particular module.
1651 llvm::iterator_range<PreprocessingRecord::iterator>
1652 getModulePreprocessedEntities(ModuleFile &Mod) const;
1653
1654 bool canRecoverFromOutOfDate(StringRef ModuleFileName,
1655 unsigned ClientLoadCapabilities);
1656
1657public:
1658 class ModuleDeclIterator
1659 : public llvm::iterator_adaptor_base<
1660 ModuleDeclIterator, const serialization::unaligned_decl_id_t *,
1661 std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1662 const Decl *, const Decl *> {
1663 ASTReader *Reader = nullptr;
1664 ModuleFile *Mod = nullptr;
1665
1666 public:
1667 ModuleDeclIterator() : iterator_adaptor_base(nullptr) {}
1668
1669 ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1670 const serialization::unaligned_decl_id_t *Pos)
1671 : iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
1672
1673 value_type operator*() const {
1674 LocalDeclID ID = LocalDeclID::get(Reader&: *Reader, MF&: *Mod, ID: *I);
1675 return Reader->GetDecl(ID: Reader->getGlobalDeclID(F&: *Mod, LocalID: ID));
1676 }
1677
1678 value_type operator->() const { return **this; }
1679
1680 bool operator==(const ModuleDeclIterator &RHS) const {
1681 assert(Reader == RHS.Reader && Mod == RHS.Mod);
1682 return I == RHS.I;
1683 }
1684 };
1685
1686 llvm::iterator_range<ModuleDeclIterator>
1687 getModuleFileLevelDecls(ModuleFile &Mod);
1688
1689private:
1690 bool isConsumerInterestedIn(Decl *D);
1691 void PassInterestingDeclsToConsumer();
1692 void PassInterestingDeclToConsumer(Decl *D);
1693 void PassVTableToConsumer(CXXRecordDecl *RD);
1694
1695 void finishPendingActions();
1696 void diagnoseOdrViolations();
1697
1698 void pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name);
1699
1700 void addPendingDeclContextInfo(Decl *D, GlobalDeclID SemaDC,
1701 GlobalDeclID LexicalDC) {
1702 assert(D);
1703 PendingDeclContextInfo Info = { .D: D, .SemaDC: SemaDC, .LexicalDC: LexicalDC };
1704 PendingDeclContextInfos.push_back(x: Info);
1705 }
1706
1707 /// Produce an error diagnostic and return true.
1708 ///
1709 /// This routine should only be used for fatal errors that have to
1710 /// do with non-routine failures (e.g., corrupted AST file).
1711 void Error(StringRef Msg) const;
1712 void Error(unsigned DiagID, StringRef Arg1 = StringRef(),
1713 StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const;
1714 void Error(llvm::Error &&Err) const;
1715
1716 /// Translate a \param GlobalDeclID to the index of DeclsLoaded array.
1717 unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const;
1718
1719 /// Translate an \param IdentifierID ID to the index of IdentifiersLoaded
1720 /// array and the corresponding module file.
1721 std::pair<ModuleFile *, unsigned>
1722 translateIdentifierIDToIndex(serialization::IdentifierID ID) const;
1723
1724 /// Translate an \param TypeID ID to the index of TypesLoaded
1725 /// array and the corresponding module file.
1726 std::pair<ModuleFile *, unsigned>
1727 translateTypeIDToIndex(serialization::TypeID ID) const;
1728
1729 /// Get a predefined Decl from ASTContext.
1730 Decl *getPredefinedDecl(PredefinedDeclIDs ID);
1731
1732public:
1733 /// Load the AST file and validate its contents against the given
1734 /// Preprocessor.
1735 ///
1736 /// \param PP the preprocessor associated with the context in which this
1737 /// precompiled header will be loaded.
1738 ///
1739 /// \param Context the AST context that this precompiled header will be
1740 /// loaded into, if any.
1741 ///
1742 /// \param PCHContainerRdr the PCHContainerOperations to use for loading and
1743 /// creating modules.
1744 ///
1745 /// \param Extensions the list of module file extensions that can be loaded
1746 /// from the AST files.
1747 ///
1748 /// \param isysroot If non-NULL, the system include path specified by the
1749 /// user. This is only used with relocatable PCH files. If non-NULL,
1750 /// a relocatable PCH file will use the default path "/".
1751 ///
1752 /// \param DisableValidationKind If set, the AST reader will suppress most
1753 /// of its regular consistency checking, allowing the use of precompiled
1754 /// headers and module files that cannot be determined to be compatible.
1755 ///
1756 /// \param AllowASTWithCompilerErrors If true, the AST reader will accept an
1757 /// AST file the was created out of an AST with compiler errors,
1758 /// otherwise it will reject it.
1759 ///
1760 /// \param AllowConfigurationMismatch If true, the AST reader will not check
1761 /// for configuration differences between the AST file and the invocation.
1762 ///
1763 /// \param ValidateSystemInputs If true, the AST reader will validate
1764 /// system input files in addition to user input files. This is only
1765 /// meaningful if \p DisableValidation is false.
1766 ///
1767 /// \param UseGlobalIndex If true, the AST reader will try to load and use
1768 /// the global module index.
1769 ///
1770 /// \param ReadTimer If non-null, a timer used to track the time spent
1771 /// deserializing.
1772 ASTReader(Preprocessor &PP, ModuleCache &ModCache, ASTContext *Context,
1773 const PCHContainerReader &PCHContainerRdr,
1774 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
1775 StringRef isysroot = "",
1776 DisableValidationForModuleKind DisableValidationKind =
1777 DisableValidationForModuleKind::None,
1778 bool AllowASTWithCompilerErrors = false,
1779 bool AllowConfigurationMismatch = false,
1780 bool ValidateSystemInputs = false,
1781 bool ForceValidateUserInputs = true,
1782 bool ValidateASTInputFilesContent = false,
1783 bool UseGlobalIndex = true,
1784 std::unique_ptr<llvm::Timer> ReadTimer = {});
1785 ASTReader(const ASTReader &) = delete;
1786 ASTReader &operator=(const ASTReader &) = delete;
1787 ~ASTReader() override;
1788
1789 SourceManager &getSourceManager() const { return SourceMgr; }
1790 FileManager &getFileManager() const { return FileMgr; }
1791 DiagnosticsEngine &getDiags() const { return Diags; }
1792
1793 /// Flags that indicate what kind of AST loading failures the client
1794 /// of the AST reader can directly handle.
1795 ///
1796 /// When a client states that it can handle a particular kind of failure,
1797 /// the AST reader will not emit errors when producing that kind of failure.
1798 enum LoadFailureCapabilities {
1799 /// The client can't handle any AST loading failures.
1800 ARR_None = 0,
1801
1802 /// The client can handle an AST file that cannot load because it
1803 /// is missing.
1804 ARR_Missing = 0x1,
1805
1806 /// The client can handle an AST file that cannot load because it
1807 /// is out-of-date relative to its input files.
1808 ARR_OutOfDate = 0x2,
1809
1810 /// The client can handle an AST file that cannot load because it
1811 /// was built with a different version of Clang.
1812 ARR_VersionMismatch = 0x4,
1813
1814 /// The client can handle an AST file that cannot load because it's
1815 /// compiled configuration doesn't match that of the context it was
1816 /// loaded into.
1817 ARR_ConfigurationMismatch = 0x8,
1818
1819 /// If a module file is marked with errors treat it as out-of-date so the
1820 /// caller can rebuild it.
1821 ARR_TreatModuleWithErrorsAsOutOfDate = 0x10
1822 };
1823
1824 /// Load the AST file designated by the given file name.
1825 ///
1826 /// \param FileName The name of the AST file to load.
1827 ///
1828 /// \param Type The kind of AST being loaded, e.g., PCH, module, main file,
1829 /// or preamble.
1830 ///
1831 /// \param ImportLoc the location where the module file will be considered as
1832 /// imported from. For non-module AST types it should be invalid.
1833 ///
1834 /// \param ClientLoadCapabilities The set of client load-failure
1835 /// capabilities, represented as a bitset of the enumerators of
1836 /// LoadFailureCapabilities.
1837 ///
1838 /// \param LoadedModuleFile The optional out-parameter refers to the new
1839 /// loaded modules. In case the module specified by FileName is already
1840 /// loaded, the module file pointer referred by NewLoadedModuleFile wouldn't
1841 /// change. Otherwise if the AST file get loaded successfully,
1842 /// NewLoadedModuleFile would refer to the address of the new loaded top level
1843 /// module. The state of NewLoadedModuleFile is unspecified if the AST file
1844 /// isn't loaded successfully.
1845 ASTReadResult ReadAST(StringRef FileName, ModuleKind Type,
1846 SourceLocation ImportLoc,
1847 unsigned ClientLoadCapabilities,
1848 ModuleFile **NewLoadedModuleFile = nullptr);
1849
1850 /// Make the entities in the given module and any of its (non-explicit)
1851 /// submodules visible to name lookup.
1852 ///
1853 /// \param Mod The module whose names should be made visible.
1854 ///
1855 /// \param NameVisibility The level of visibility to give the names in the
1856 /// module. Visibility can only be increased over time.
1857 ///
1858 /// \param ImportLoc The location at which the import occurs.
1859 void makeModuleVisible(Module *Mod,
1860 Module::NameVisibilityKind NameVisibility,
1861 SourceLocation ImportLoc);
1862
1863 /// Make the names within this set of hidden names visible.
1864 void makeNamesVisible(const HiddenNames &Names, Module *Owner);
1865
1866 /// Note that MergedDef is a redefinition of the canonical definition
1867 /// Def, so Def should be visible whenever MergedDef is.
1868 void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef);
1869
1870 /// Take the AST callbacks listener.
1871 std::unique_ptr<ASTReaderListener> takeListener() {
1872 return std::move(Listener);
1873 }
1874
1875 /// Set the AST callbacks listener.
1876 void setListener(std::unique_ptr<ASTReaderListener> Listener) {
1877 this->Listener = std::move(Listener);
1878 }
1879
1880 /// Add an AST callback listener.
1881 ///
1882 /// Takes ownership of \p L.
1883 void addListener(std::unique_ptr<ASTReaderListener> L) {
1884 if (Listener)
1885 L = std::make_unique<ChainedASTReaderListener>(args: std::move(L),
1886 args: std::move(Listener));
1887 Listener = std::move(L);
1888 }
1889
1890 /// RAII object to temporarily add an AST callback listener.
1891 class ListenerScope {
1892 ASTReader &Reader;
1893 bool Chained = false;
1894
1895 public:
1896 ListenerScope(ASTReader &Reader, std::unique_ptr<ASTReaderListener> L)
1897 : Reader(Reader) {
1898 auto Old = Reader.takeListener();
1899 if (Old) {
1900 Chained = true;
1901 L = std::make_unique<ChainedASTReaderListener>(args: std::move(L),
1902 args: std::move(Old));
1903 }
1904 Reader.setListener(std::move(L));
1905 }
1906
1907 ~ListenerScope() {
1908 auto New = Reader.takeListener();
1909 if (Chained)
1910 Reader.setListener(static_cast<ChainedASTReaderListener *>(New.get())
1911 ->takeSecond());
1912 }
1913 };
1914
1915 /// Set the AST deserialization listener.
1916 void setDeserializationListener(ASTDeserializationListener *Listener,
1917 bool TakeOwnership = false);
1918
1919 /// Get the AST deserialization listener.
1920 ASTDeserializationListener *getDeserializationListener() {
1921 return DeserializationListener;
1922 }
1923
1924 /// Determine whether this AST reader has a global index.
1925 bool hasGlobalIndex() const { return (bool)GlobalIndex; }
1926
1927 /// Return global module index.
1928 GlobalModuleIndex *getGlobalIndex() { return GlobalIndex.get(); }
1929
1930 /// Reset reader for a reload try.
1931 void resetForReload() { TriedLoadingGlobalIndex = false; }
1932
1933 /// Attempts to load the global index.
1934 ///
1935 /// \returns true if loading the global index has failed for any reason.
1936 bool loadGlobalIndex();
1937
1938 /// Determine whether we tried to load the global index, but failed,
1939 /// e.g., because it is out-of-date or does not exist.
1940 bool isGlobalIndexUnavailable() const;
1941
1942 /// Initializes the ASTContext
1943 void InitializeContext();
1944
1945 /// Update the state of Sema after loading some additional modules.
1946 void UpdateSema();
1947
1948 /// Add in-memory (virtual file) buffer.
1949 void addInMemoryBuffer(StringRef &FileName,
1950 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
1951 ModuleMgr.addInMemoryBuffer(FileName, Buffer: std::move(Buffer));
1952 }
1953
1954 /// Finalizes the AST reader's state before writing an AST file to
1955 /// disk.
1956 ///
1957 /// This operation may undo temporary state in the AST that should not be
1958 /// emitted.
1959 void finalizeForWriting();
1960
1961 /// Retrieve the module manager.
1962 ModuleManager &getModuleManager() { return ModuleMgr; }
1963 const ModuleManager &getModuleManager() const { return ModuleMgr; }
1964
1965 /// Retrieve the preprocessor.
1966 Preprocessor &getPreprocessor() const { return PP; }
1967
1968 /// Retrieve the name of the original source file name for the primary
1969 /// module file.
1970 StringRef getOriginalSourceFile() {
1971 return ModuleMgr.getPrimaryModule().OriginalSourceFileName;
1972 }
1973
1974 /// Retrieve the name of the original source file name directly from
1975 /// the AST file, without actually loading the AST file.
1976 static std::string
1977 getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr,
1978 const PCHContainerReader &PCHContainerRdr,
1979 DiagnosticsEngine &Diags);
1980
1981 /// Read the control block for the named AST file.
1982 ///
1983 /// \returns true if an error occurred, false otherwise.
1984 static bool readASTFileControlBlock(
1985 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
1986 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
1987 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
1988 unsigned ClientLoadCapabilities = ARR_ConfigurationMismatch |
1989 ARR_OutOfDate);
1990
1991 /// Determine whether the given AST file is acceptable to load into a
1992 /// translation unit with the given language and target options.
1993 static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
1994 const ModuleCache &ModCache,
1995 const PCHContainerReader &PCHContainerRdr,
1996 const LangOptions &LangOpts,
1997 const TargetOptions &TargetOpts,
1998 const PreprocessorOptions &PPOpts,
1999 StringRef ExistingModuleCachePath,
2000 bool RequireStrictOptionMatches = false);
2001
2002 /// Returns the suggested contents of the predefines buffer,
2003 /// which contains a (typically-empty) subset of the predefines
2004 /// build prior to including the precompiled header.
2005 const std::string &getSuggestedPredefines() { return SuggestedPredefines; }
2006
2007 /// Read a preallocated preprocessed entity from the external source.
2008 ///
2009 /// \returns null if an error occurred that prevented the preprocessed
2010 /// entity from being loaded.
2011 PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) override;
2012
2013 /// Returns a pair of [Begin, End) indices of preallocated
2014 /// preprocessed entities that \p Range encompasses.
2015 std::pair<unsigned, unsigned>
2016 findPreprocessedEntitiesInRange(SourceRange Range) override;
2017
2018 /// Optionally returns true or false if the preallocated preprocessed
2019 /// entity with index \p Index came from file \p FID.
2020 std::optional<bool> isPreprocessedEntityInFileID(unsigned Index,
2021 FileID FID) override;
2022
2023 /// Read a preallocated skipped range from the external source.
2024 SourceRange ReadSkippedRange(unsigned Index) override;
2025
2026 /// Read the header file information for the given file entry.
2027 HeaderFileInfo GetHeaderFileInfo(FileEntryRef FE) override;
2028
2029 void ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag);
2030
2031 /// Returns the number of source locations found in the chain.
2032 unsigned getTotalNumSLocs() const {
2033 return TotalNumSLocEntries;
2034 }
2035
2036 /// Returns the number of identifiers found in the chain.
2037 unsigned getTotalNumIdentifiers() const {
2038 return static_cast<unsigned>(IdentifiersLoaded.size());
2039 }
2040
2041 /// Returns the number of macros found in the chain.
2042 unsigned getTotalNumMacros() const {
2043 return static_cast<unsigned>(MacrosLoaded.size());
2044 }
2045
2046 /// Returns the number of types found in the chain.
2047 unsigned getTotalNumTypes() const {
2048 return static_cast<unsigned>(TypesLoaded.size());
2049 }
2050
2051 /// Returns the number of declarations found in the chain.
2052 unsigned getTotalNumDecls() const {
2053 return static_cast<unsigned>(DeclsLoaded.size());
2054 }
2055
2056 /// Returns the number of submodules known.
2057 unsigned getTotalNumSubmodules() const {
2058 return static_cast<unsigned>(SubmodulesLoaded.size());
2059 }
2060
2061 /// Returns the number of selectors found in the chain.
2062 unsigned getTotalNumSelectors() const {
2063 return static_cast<unsigned>(SelectorsLoaded.size());
2064 }
2065
2066 /// Returns the number of preprocessed entities known to the AST
2067 /// reader.
2068 unsigned getTotalNumPreprocessedEntities() const {
2069 unsigned Result = 0;
2070 for (const auto &M : ModuleMgr)
2071 Result += M.NumPreprocessedEntities;
2072 return Result;
2073 }
2074
2075 /// Resolve a type ID into a type, potentially building a new
2076 /// type.
2077 QualType GetType(serialization::TypeID ID);
2078
2079 /// Resolve a local type ID within a given AST file into a type.
2080 QualType getLocalType(ModuleFile &F, serialization::LocalTypeID LocalID);
2081
2082 /// Map a local type ID within a given AST file into a global type ID.
2083 serialization::TypeID
2084 getGlobalTypeID(ModuleFile &F, serialization::LocalTypeID LocalID) const;
2085
2086 /// Read a type from the current position in the given record, which
2087 /// was read from the given AST file.
2088 QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx) {
2089 if (Idx >= Record.size())
2090 return {};
2091
2092 return getLocalType(F, LocalID: Record[Idx++]);
2093 }
2094
2095 /// Map from a local declaration ID within a given module to a
2096 /// global declaration ID.
2097 GlobalDeclID getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const;
2098
2099 /// Returns true if global DeclID \p ID originated from module \p M.
2100 bool isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const;
2101
2102 /// Retrieve the module file that owns the given declaration, or NULL
2103 /// if the declaration is not from a module file.
2104 ModuleFile *getOwningModuleFile(const Decl *D) const;
2105 ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
2106
2107 /// Returns the source location for the decl \p ID.
2108 SourceLocation getSourceLocationForDeclID(GlobalDeclID ID);
2109
2110 /// Resolve a declaration ID into a declaration, potentially
2111 /// building a new declaration.
2112 Decl *GetDecl(GlobalDeclID ID);
2113 Decl *GetExternalDecl(GlobalDeclID ID) override;
2114
2115 /// Resolve a declaration ID into a declaration. Return 0 if it's not
2116 /// been loaded yet.
2117 Decl *GetExistingDecl(GlobalDeclID ID);
2118
2119 /// Reads a declaration with the given local ID in the given module.
2120 Decl *GetLocalDecl(ModuleFile &F, LocalDeclID LocalID) {
2121 return GetDecl(ID: getGlobalDeclID(F, LocalID));
2122 }
2123
2124 /// Reads a declaration with the given local ID in the given module.
2125 ///
2126 /// \returns The requested declaration, casted to the given return type.
2127 template <typename T> T *GetLocalDeclAs(ModuleFile &F, LocalDeclID LocalID) {
2128 return cast_or_null<T>(GetLocalDecl(F, LocalID));
2129 }
2130
2131 /// Map a global declaration ID into the declaration ID used to
2132 /// refer to this declaration within the given module fule.
2133 ///
2134 /// \returns the global ID of the given declaration as known in the given
2135 /// module file.
2136 LocalDeclID mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
2137 GlobalDeclID GlobalID);
2138
2139 /// Reads a declaration ID from the given position in a record in the
2140 /// given module.
2141 ///
2142 /// \returns The declaration ID read from the record, adjusted to a global ID.
2143 GlobalDeclID ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
2144 unsigned &Idx);
2145
2146 /// Reads a declaration from the given position in a record in the
2147 /// given module.
2148 Decl *ReadDecl(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
2149 return GetDecl(ID: ReadDeclID(F, Record: R, Idx&: I));
2150 }
2151
2152 /// Reads a declaration from the given position in a record in the
2153 /// given module.
2154 ///
2155 /// \returns The declaration read from this location, casted to the given
2156 /// result type.
2157 template <typename T>
2158 T *ReadDeclAs(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
2159 return cast_or_null<T>(GetDecl(ID: ReadDeclID(F, Record: R, Idx&: I)));
2160 }
2161
2162 /// If any redeclarations of \p D have been imported since it was
2163 /// last checked, this digs out those redeclarations and adds them to the
2164 /// redeclaration chain for \p D.
2165 void CompleteRedeclChain(const Decl *D) override;
2166
2167 CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset) override;
2168
2169 /// Resolve the offset of a statement into a statement.
2170 ///
2171 /// This operation will read a new statement from the external
2172 /// source each time it is called, and is meant to be used via a
2173 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
2174 Stmt *GetExternalDeclStmt(uint64_t Offset) override;
2175
2176 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
2177 /// specified cursor. Read the abbreviations that are at the top of the block
2178 /// and then leave the cursor pointing into the block.
2179 static llvm::Error ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
2180 unsigned BlockID,
2181 uint64_t *StartOfBlockOffset = nullptr);
2182
2183 bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
2184
2185 bool
2186 LoadExternalSpecializations(const Decl *D,
2187 ArrayRef<TemplateArgument> TemplateArgs) override;
2188
2189 /// Finds all the visible declarations with a given name.
2190 /// The current implementation of this method just loads the entire
2191 /// lookup table as unmaterialized references.
2192 bool FindExternalVisibleDeclsByName(const DeclContext *DC,
2193 DeclarationName Name,
2194 const DeclContext *OriginalDC) override;
2195
2196 /// Read all of the declarations lexically stored in a
2197 /// declaration context.
2198 ///
2199 /// \param DC The declaration context whose declarations will be
2200 /// read.
2201 ///
2202 /// \param IsKindWeWant A predicate indicating which declaration kinds
2203 /// we are interested in.
2204 ///
2205 /// \param Decls Vector that will contain the declarations loaded
2206 /// from the external source. The caller is responsible for merging
2207 /// these declarations with any declarations already stored in the
2208 /// declaration context.
2209 void
2210 FindExternalLexicalDecls(const DeclContext *DC,
2211 llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
2212 SmallVectorImpl<Decl *> &Decls) override;
2213
2214 /// Get the decls that are contained in a file in the Offset/Length
2215 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
2216 /// a range.
2217 void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2218 SmallVectorImpl<Decl *> &Decls) override;
2219
2220 /// Notify ASTReader that we started deserialization of
2221 /// a decl or type so until FinishedDeserializing is called there may be
2222 /// decls that are initializing. Must be paired with FinishedDeserializing.
2223 void StartedDeserializing() override;
2224
2225 /// Notify ASTReader that we finished the deserialization of
2226 /// a decl or type. Must be paired with StartedDeserializing.
2227 void FinishedDeserializing() override;
2228
2229 /// Function that will be invoked when we begin parsing a new
2230 /// translation unit involving this external AST source.
2231 ///
2232 /// This function will provide all of the external definitions to
2233 /// the ASTConsumer.
2234 void StartTranslationUnit(ASTConsumer *Consumer) override;
2235
2236 /// Print some statistics about AST usage.
2237 void PrintStats() override;
2238
2239 /// Dump information about the AST reader to standard error.
2240 void dump();
2241
2242 /// Return the amount of memory used by memory buffers, breaking down
2243 /// by heap-backed versus mmap'ed memory.
2244 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override;
2245
2246 /// Initialize the semantic source with the Sema instance
2247 /// being used to perform semantic analysis on the abstract syntax
2248 /// tree.
2249 void InitializeSema(Sema &S) override;
2250
2251 /// Inform the semantic consumer that Sema is no longer available.
2252 void ForgetSema() override { SemaObj = nullptr; }
2253
2254 /// Retrieve the IdentifierInfo for the named identifier.
2255 ///
2256 /// This routine builds a new IdentifierInfo for the given identifier. If any
2257 /// declarations with this name are visible from translation unit scope, their
2258 /// declarations will be deserialized and introduced into the declaration
2259 /// chain of the identifier.
2260 IdentifierInfo *get(StringRef Name) override;
2261
2262 /// Retrieve an iterator into the set of all identifiers
2263 /// in all loaded AST files.
2264 IdentifierIterator *getIdentifiers() override;
2265
2266 /// Load the contents of the global method pool for a given
2267 /// selector.
2268 void ReadMethodPool(Selector Sel) override;
2269
2270 /// Load the contents of the global method pool for a given
2271 /// selector if necessary.
2272 void updateOutOfDateSelector(Selector Sel) override;
2273
2274 /// Load the set of namespaces that are known to the external source,
2275 /// which will be used during typo correction.
2276 void ReadKnownNamespaces(
2277 SmallVectorImpl<NamespaceDecl *> &Namespaces) override;
2278
2279 void ReadUndefinedButUsed(
2280 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) override;
2281
2282 void ReadMismatchingDeleteExpressions(llvm::MapVector<
2283 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
2284 Exprs) override;
2285
2286 void ReadTentativeDefinitions(
2287 SmallVectorImpl<VarDecl *> &TentativeDefs) override;
2288
2289 void ReadUnusedFileScopedDecls(
2290 SmallVectorImpl<const DeclaratorDecl *> &Decls) override;
2291
2292 void ReadDelegatingConstructors(
2293 SmallVectorImpl<CXXConstructorDecl *> &Decls) override;
2294
2295 void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) override;
2296
2297 void ReadUnusedLocalTypedefNameCandidates(
2298 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) override;
2299
2300 void ReadDeclsToCheckForDeferredDiags(
2301 llvm::SmallSetVector<Decl *, 4> &Decls) override;
2302
2303 void ReadReferencedSelectors(
2304 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) override;
2305
2306 void ReadWeakUndeclaredIdentifiers(
2307 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) override;
2308
2309 void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
2310
2311 void ReadPendingInstantiations(
2312 SmallVectorImpl<std::pair<ValueDecl *,
2313 SourceLocation>> &Pending) override;
2314
2315 void ReadLateParsedTemplates(
2316 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
2317 &LPTMap) override;
2318
2319 void AssignedLambdaNumbering(CXXRecordDecl *Lambda) override;
2320
2321 /// Load a selector from disk, registering its ID if it exists.
2322 void LoadSelector(Selector Sel);
2323
2324 void SetIdentifierInfo(serialization::IdentifierID ID, IdentifierInfo *II);
2325 void SetGloballyVisibleDecls(IdentifierInfo *II,
2326 const SmallVectorImpl<GlobalDeclID> &DeclIDs,
2327 SmallVectorImpl<Decl *> *Decls = nullptr);
2328
2329 /// Report a diagnostic.
2330 DiagnosticBuilder Diag(unsigned DiagID) const;
2331
2332 /// Report a diagnostic.
2333 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const;
2334
2335 void runWithSufficientStackSpace(SourceLocation Loc,
2336 llvm::function_ref<void()> Fn);
2337
2338 IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID);
2339
2340 IdentifierInfo *readIdentifier(ModuleFile &M, const RecordData &Record,
2341 unsigned &Idx) {
2342 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID: Record[Idx++]));
2343 }
2344
2345 IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) override {
2346 // Note that we are loading an identifier.
2347 Deserializing AnIdentifier(this);
2348
2349 return DecodeIdentifierInfo(ID);
2350 }
2351
2352 IdentifierInfo *getLocalIdentifier(ModuleFile &M, uint64_t LocalID);
2353
2354 serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
2355 uint64_t LocalID);
2356
2357 void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
2358
2359 /// Retrieve the macro with the given ID.
2360 MacroInfo *getMacro(serialization::MacroID ID);
2361
2362 /// Retrieve the global macro ID corresponding to the given local
2363 /// ID within the given module file.
2364 serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
2365
2366 /// Read the source location entry with index ID.
2367 bool ReadSLocEntry(int ID) override;
2368 /// Get the index ID for the loaded SourceLocation offset.
2369 int getSLocEntryID(SourceLocation::UIntTy SLocOffset) override;
2370 /// Try to read the offset of the SLocEntry at the given index in the given
2371 /// module file.
2372 llvm::Expected<SourceLocation::UIntTy> readSLocOffset(ModuleFile *F,
2373 unsigned Index);
2374
2375 /// Retrieve the module import location and module name for the
2376 /// given source manager entry ID.
2377 std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override;
2378
2379 /// Retrieve the global submodule ID given a module and its local ID
2380 /// number.
2381 serialization::SubmoduleID getGlobalSubmoduleID(ModuleFile &M,
2382 unsigned LocalID) const;
2383
2384 /// Retrieve the submodule that corresponds to a global submodule ID.
2385 ///
2386 Module *getSubmodule(serialization::SubmoduleID GlobalID);
2387
2388 /// Retrieve the module that corresponds to the given module ID.
2389 ///
2390 /// Note: overrides method in ExternalASTSource
2391 Module *getModule(unsigned ID) override;
2392
2393 /// Retrieve the module file with a given local ID within the specified
2394 /// ModuleFile.
2395 ModuleFile *getLocalModuleFile(ModuleFile &M, unsigned ID) const;
2396
2397 /// Get an ID for the given module file.
2398 unsigned getModuleFileID(ModuleFile *M);
2399
2400 /// Return a descriptor for the corresponding module.
2401 std::optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override;
2402
2403 ExtKind hasExternalDefinitions(const Decl *D) override;
2404
2405 bool wasThisDeclarationADefinition(const FunctionDecl *FD) override;
2406
2407 /// Retrieve a selector from the given module with its local ID
2408 /// number.
2409 Selector getLocalSelector(ModuleFile &M, unsigned LocalID);
2410
2411 Selector DecodeSelector(serialization::SelectorID Idx);
2412
2413 Selector GetExternalSelector(serialization::SelectorID ID) override;
2414 uint32_t GetNumExternalSelectors() override;
2415
2416 Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx) {
2417 return getLocalSelector(M, LocalID: Record[Idx++]);
2418 }
2419
2420 /// Retrieve the global selector ID that corresponds to this
2421 /// the local selector ID in a given module.
2422 serialization::SelectorID getGlobalSelectorID(ModuleFile &M,
2423 unsigned LocalID) const;
2424
2425 /// Read the contents of a CXXCtorInitializer array.
2426 CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
2427
2428 /// Read a AlignPackInfo from raw form.
2429 Sema::AlignPackInfo ReadAlignPackInfo(uint32_t Raw) const {
2430 return Sema::AlignPackInfo::getFromRawEncoding(Encoding: Raw);
2431 }
2432
2433 using RawLocEncoding = SourceLocationEncoding::RawLocEncoding;
2434
2435 /// Read a source location from raw form and return it in its
2436 /// originating module file's source location space.
2437 std::pair<SourceLocation, unsigned>
2438 ReadUntranslatedSourceLocation(RawLocEncoding Raw) const {
2439 return SourceLocationEncoding::decode(Encoded: Raw);
2440 }
2441
2442 /// Read a source location from raw form.
2443 SourceLocation ReadSourceLocation(ModuleFile &MF, RawLocEncoding Raw) const {
2444 if (!MF.ModuleOffsetMap.empty())
2445 ReadModuleOffsetMap(F&: MF);
2446
2447 auto [Loc, ModuleFileIndex] = ReadUntranslatedSourceLocation(Raw);
2448 ModuleFile *OwningModuleFile =
2449 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
2450
2451 assert(!SourceMgr.isLoadedSourceLocation(Loc) &&
2452 "Run out source location space");
2453
2454 return TranslateSourceLocation(ModuleFile&: *OwningModuleFile, Loc);
2455 }
2456
2457 /// Translate a source location from another module file's source
2458 /// location space into ours.
2459 SourceLocation TranslateSourceLocation(ModuleFile &ModuleFile,
2460 SourceLocation Loc) const {
2461 if (Loc.isInvalid())
2462 return Loc;
2463
2464 // FIXME: TranslateSourceLocation is not re-enterable. It is problematic
2465 // to call TranslateSourceLocation on a translated source location.
2466 // We either need a method to know whether or not a source location is
2467 // translated or refactor the code to make it clear that
2468 // TranslateSourceLocation won't be called with translated source location.
2469
2470 return Loc.getLocWithOffset(Offset: ModuleFile.SLocEntryBaseOffset - 2);
2471 }
2472
2473 /// Read a source location.
2474 SourceLocation ReadSourceLocation(ModuleFile &ModuleFile,
2475 const RecordDataImpl &Record,
2476 unsigned &Idx) {
2477 return ReadSourceLocation(MF&: ModuleFile, Raw: Record[Idx++]);
2478 }
2479
2480 /// Read a FileID.
2481 FileID ReadFileID(ModuleFile &F, const RecordDataImpl &Record,
2482 unsigned &Idx) const {
2483 return TranslateFileID(F, FID: FileID::get(V: Record[Idx++]));
2484 }
2485
2486 /// Translate a FileID from another module file's FileID space into ours.
2487 FileID TranslateFileID(ModuleFile &F, FileID FID) const {
2488 assert(FID.ID >= 0 && "Reading non-local FileID.");
2489 if (FID.isInvalid())
2490 return FID;
2491 return FileID::get(V: F.SLocEntryBaseID + FID.ID - 1);
2492 }
2493
2494 /// Read a source range.
2495 SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record,
2496 unsigned &Idx);
2497
2498 static llvm::BitVector ReadBitVector(const RecordData &Record,
2499 const StringRef Blob);
2500
2501 // Read a string
2502 static std::string ReadString(const RecordDataImpl &Record, unsigned &Idx);
2503 static StringRef ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
2504 StringRef &Blob);
2505
2506 // Read a path
2507 std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx);
2508
2509 // Read a path
2510 std::string ReadPath(StringRef BaseDirectory, const RecordData &Record,
2511 unsigned &Idx);
2512 std::string ReadPathBlob(StringRef BaseDirectory, const RecordData &Record,
2513 unsigned &Idx, StringRef &Blob);
2514
2515 /// Read a version tuple.
2516 static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx);
2517
2518 CXXTemporary *ReadCXXTemporary(ModuleFile &F, const RecordData &Record,
2519 unsigned &Idx);
2520
2521 /// Reads a statement.
2522 Stmt *ReadStmt(ModuleFile &F);
2523
2524 /// Reads an expression.
2525 Expr *ReadExpr(ModuleFile &F);
2526
2527 /// Reads a sub-statement operand during statement reading.
2528 Stmt *ReadSubStmt() {
2529 assert(ReadingKind == Read_Stmt &&
2530 "Should be called only during statement reading!");
2531 // Subexpressions are stored from last to first, so the next Stmt we need
2532 // is at the back of the stack.
2533 assert(!StmtStack.empty() && "Read too many sub-statements!");
2534 return StmtStack.pop_back_val();
2535 }
2536
2537 /// Reads a sub-expression operand during statement reading.
2538 Expr *ReadSubExpr();
2539
2540 /// Reads a token out of a record.
2541 Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx);
2542
2543 /// Reads the macro record located at the given offset.
2544 MacroInfo *ReadMacroRecord(ModuleFile &F, uint64_t Offset);
2545
2546 /// Determine the global preprocessed entity ID that corresponds to
2547 /// the given local ID within the given module.
2548 serialization::PreprocessedEntityID
2549 getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2550
2551 /// Add a macro to deserialize its macro directive history.
2552 ///
2553 /// \param II The name of the macro.
2554 /// \param M The module file.
2555 /// \param MacroDirectivesOffset Offset of the serialized macro directive
2556 /// history.
2557 void addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2558 uint32_t MacroDirectivesOffset);
2559
2560 /// Read the set of macros defined by this external macro source.
2561 void ReadDefinedMacros() override;
2562
2563 /// Update an out-of-date identifier.
2564 void updateOutOfDateIdentifier(const IdentifierInfo &II) override;
2565
2566 /// Note that this identifier is up-to-date.
2567 void markIdentifierUpToDate(const IdentifierInfo *II);
2568
2569 /// Load all external visible decls in the given DeclContext.
2570 void completeVisibleDeclsMap(const DeclContext *DC) override;
2571
2572 /// Retrieve the AST context that this AST reader supplements.
2573 ASTContext &getContext() {
2574 assert(ContextObj && "requested AST context when not loading AST");
2575 return *ContextObj;
2576 }
2577
2578 // Contains the IDs for declarations that were requested before we have
2579 // access to a Sema object.
2580 SmallVector<GlobalDeclID, 16> PreloadedDeclIDs;
2581
2582 /// Retrieve the semantic analysis object used to analyze the
2583 /// translation unit in which the precompiled header is being
2584 /// imported.
2585 Sema *getSema() { return SemaObj; }
2586
2587 /// Get the identifier resolver used for name lookup / updates
2588 /// in the translation unit scope. We have one of these even if we don't
2589 /// have a Sema object.
2590 IdentifierResolver &getIdResolver();
2591
2592 /// Retrieve the identifier table associated with the
2593 /// preprocessor.
2594 IdentifierTable &getIdentifierTable();
2595
2596 /// Record that the given ID maps to the given switch-case
2597 /// statement.
2598 void RecordSwitchCaseID(SwitchCase *SC, unsigned ID);
2599
2600 /// Retrieve the switch-case statement with the given ID.
2601 SwitchCase *getSwitchCaseWithID(unsigned ID);
2602
2603 void ClearSwitchCaseIDs();
2604
2605 /// Cursors for comments blocks.
2606 SmallVector<std::pair<llvm::BitstreamCursor,
2607 serialization::ModuleFile *>, 8> CommentsCursors;
2608
2609 /// Loads comments ranges.
2610 void ReadComments() override;
2611
2612 /// Visit all the input file infos of the given module file.
2613 void visitInputFileInfos(
2614 serialization::ModuleFile &MF, bool IncludeSystem,
2615 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
2616 bool IsSystem)>
2617 Visitor);
2618
2619 /// Visit all the input files of the given module file.
2620 void visitInputFiles(serialization::ModuleFile &MF,
2621 bool IncludeSystem, bool Complain,
2622 llvm::function_ref<void(const serialization::InputFile &IF,
2623 bool isSystem)> Visitor);
2624
2625 /// Visit all the top-level module maps loaded when building the given module
2626 /// file.
2627 void visitTopLevelModuleMaps(serialization::ModuleFile &MF,
2628 llvm::function_ref<void(FileEntryRef)> Visitor);
2629
2630 bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; }
2631};
2632
2633/// A simple helper class to unpack an integer to bits and consuming
2634/// the bits in order.
2635class BitsUnpacker {
2636 constexpr static uint32_t BitsIndexUpbound = 32;
2637
2638public:
2639 BitsUnpacker(uint32_t V) { updateValue(V); }
2640 BitsUnpacker(const BitsUnpacker &) = delete;
2641 BitsUnpacker(BitsUnpacker &&) = delete;
2642 BitsUnpacker operator=(const BitsUnpacker &) = delete;
2643 BitsUnpacker operator=(BitsUnpacker &&) = delete;
2644 ~BitsUnpacker() = default;
2645
2646 void updateValue(uint32_t V) {
2647 Value = V;
2648 CurrentBitsIndex = 0;
2649 }
2650
2651 void advance(uint32_t BitsWidth) { CurrentBitsIndex += BitsWidth; }
2652
2653 bool getNextBit() {
2654 assert(isValid());
2655 return Value & (1 << CurrentBitsIndex++);
2656 }
2657
2658 uint32_t getNextBits(uint32_t Width) {
2659 assert(isValid());
2660 assert(Width < BitsIndexUpbound);
2661 uint32_t Ret = (Value >> CurrentBitsIndex) & ((1 << Width) - 1);
2662 CurrentBitsIndex += Width;
2663 return Ret;
2664 }
2665
2666 bool canGetNextNBits(uint32_t Width) const {
2667 return CurrentBitsIndex + Width < BitsIndexUpbound;
2668 }
2669
2670private:
2671 bool isValid() const { return CurrentBitsIndex < BitsIndexUpbound; }
2672
2673 uint32_t Value;
2674 uint32_t CurrentBitsIndex = ~0;
2675};
2676
2677inline bool shouldSkipCheckingODR(const Decl *D) {
2678 return D->getASTContext().getLangOpts().SkipODRCheckInGMF &&
2679 (D->isFromGlobalModule() || D->isFromHeaderUnit());
2680}
2681
2682/// Calculate a hash value for the primary module name of the given module.
2683/// \returns std::nullopt if M is not a C++ standard module.
2684UnsignedOrNone getPrimaryModuleHash(const Module *M);
2685
2686} // namespace clang
2687
2688#endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H
2689

source code of clang/include/clang/Serialization/ASTReader.h