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