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

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