1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
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 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTStructuralEquivalence.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/AbstractTypeReader.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/DeclarationName.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
31 #include "clang/AST/ExternalASTSource.h"
32 #include "clang/AST/NestedNameSpecifier.h"
33 #include "clang/AST/ODRDiagsEmitter.h"
34 #include "clang/AST/ODRHash.h"
35 #include "clang/AST/OpenMPClause.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticError.h"
46 #include "clang/Basic/DiagnosticOptions.h"
47 #include "clang/Basic/DiagnosticSema.h"
48 #include "clang/Basic/ExceptionSpecificationType.h"
49 #include "clang/Basic/FileManager.h"
50 #include "clang/Basic/FileSystemOptions.h"
51 #include "clang/Basic/IdentifierTable.h"
52 #include "clang/Basic/LLVM.h"
53 #include "clang/Basic/LangOptions.h"
54 #include "clang/Basic/Module.h"
55 #include "clang/Basic/ObjCRuntime.h"
56 #include "clang/Basic/OpenMPKinds.h"
57 #include "clang/Basic/OperatorKinds.h"
58 #include "clang/Basic/PragmaKinds.h"
59 #include "clang/Basic/Sanitizers.h"
60 #include "clang/Basic/SourceLocation.h"
61 #include "clang/Basic/SourceManager.h"
62 #include "clang/Basic/SourceManagerInternals.h"
63 #include "clang/Basic/Specifiers.h"
64 #include "clang/Basic/TargetInfo.h"
65 #include "clang/Basic/TargetOptions.h"
66 #include "clang/Basic/TokenKinds.h"
67 #include "clang/Basic/Version.h"
68 #include "clang/Lex/HeaderSearch.h"
69 #include "clang/Lex/HeaderSearchOptions.h"
70 #include "clang/Lex/MacroInfo.h"
71 #include "clang/Lex/ModuleMap.h"
72 #include "clang/Lex/PreprocessingRecord.h"
73 #include "clang/Lex/Preprocessor.h"
74 #include "clang/Lex/PreprocessorOptions.h"
75 #include "clang/Lex/Token.h"
76 #include "clang/Sema/ObjCMethodList.h"
77 #include "clang/Sema/Scope.h"
78 #include "clang/Sema/Sema.h"
79 #include "clang/Sema/Weak.h"
80 #include "clang/Serialization/ASTBitCodes.h"
81 #include "clang/Serialization/ASTDeserializationListener.h"
82 #include "clang/Serialization/ASTRecordReader.h"
83 #include "clang/Serialization/ContinuousRangeMap.h"
84 #include "clang/Serialization/GlobalModuleIndex.h"
85 #include "clang/Serialization/InMemoryModuleCache.h"
86 #include "clang/Serialization/ModuleFile.h"
87 #include "clang/Serialization/ModuleFileExtension.h"
88 #include "clang/Serialization/ModuleManager.h"
89 #include "clang/Serialization/PCHContainerOperations.h"
90 #include "clang/Serialization/SerializationDiagnostic.h"
91 #include "llvm/ADT/APFloat.h"
92 #include "llvm/ADT/APInt.h"
93 #include "llvm/ADT/APSInt.h"
94 #include "llvm/ADT/ArrayRef.h"
95 #include "llvm/ADT/DenseMap.h"
96 #include "llvm/ADT/FloatingPointMode.h"
97 #include "llvm/ADT/FoldingSet.h"
98 #include "llvm/ADT/Hashing.h"
99 #include "llvm/ADT/IntrusiveRefCntPtr.h"
100 #include "llvm/ADT/STLExtras.h"
101 #include "llvm/ADT/ScopeExit.h"
102 #include "llvm/ADT/SmallPtrSet.h"
103 #include "llvm/ADT/SmallString.h"
104 #include "llvm/ADT/SmallVector.h"
105 #include "llvm/ADT/StringExtras.h"
106 #include "llvm/ADT/StringMap.h"
107 #include "llvm/ADT/StringRef.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/LEB128.h"
119 #include "llvm/Support/MemoryBuffer.h"
120 #include "llvm/Support/Path.h"
121 #include "llvm/Support/SaveAndRestore.h"
122 #include "llvm/Support/TimeProfiler.h"
123 #include "llvm/Support/Timer.h"
124 #include "llvm/Support/VersionTuple.h"
125 #include "llvm/Support/raw_ostream.h"
126 #include "llvm/TargetParser/Triple.h"
127 #include <algorithm>
128 #include <cassert>
129 #include <cstddef>
130 #include <cstdint>
131 #include <cstdio>
132 #include <ctime>
133 #include <iterator>
134 #include <limits>
135 #include <map>
136 #include <memory>
137 #include <optional>
138 #include <string>
139 #include <system_error>
140 #include <tuple>
141 #include <utility>
142 #include <vector>
143 
144 using namespace clang;
145 using namespace clang::serialization;
146 using namespace clang::serialization::reader;
147 using llvm::BitstreamCursor;
148 
149 //===----------------------------------------------------------------------===//
150 // ChainedASTReaderListener implementation
151 //===----------------------------------------------------------------------===//
152 
153 bool
154 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155   return First->ReadFullVersionInformation(FullVersion) ||
156          Second->ReadFullVersionInformation(FullVersion);
157 }
158 
159 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160   First->ReadModuleName(ModuleName);
161   Second->ReadModuleName(ModuleName);
162 }
163 
164 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165   First->ReadModuleMapFile(ModuleMapPath);
166   Second->ReadModuleMapFile(ModuleMapPath);
167 }
168 
169 bool
170 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
171                                               bool Complain,
172                                               bool AllowCompatibleDifferences) {
173   return First->ReadLanguageOptions(LangOpts, Complain,
174                                     AllowCompatibleDifferences) ||
175          Second->ReadLanguageOptions(LangOpts, Complain,
176                                      AllowCompatibleDifferences);
177 }
178 
179 bool ChainedASTReaderListener::ReadTargetOptions(
180     const TargetOptions &TargetOpts, bool Complain,
181     bool AllowCompatibleDifferences) {
182   return First->ReadTargetOptions(TargetOpts, Complain,
183                                   AllowCompatibleDifferences) ||
184          Second->ReadTargetOptions(TargetOpts, Complain,
185                                    AllowCompatibleDifferences);
186 }
187 
188 bool ChainedASTReaderListener::ReadDiagnosticOptions(
189     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
190   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
191          Second->ReadDiagnosticOptions(DiagOpts, Complain);
192 }
193 
194 bool
195 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
196                                                 bool Complain) {
197   return First->ReadFileSystemOptions(FSOpts, Complain) ||
198          Second->ReadFileSystemOptions(FSOpts, Complain);
199 }
200 
201 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
202     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
203     bool Complain) {
204   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
205                                         Complain) ||
206          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
207                                          Complain);
208 }
209 
210 bool ChainedASTReaderListener::ReadPreprocessorOptions(
211     const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
212     std::string &SuggestedPredefines) {
213   return First->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
214                                         SuggestedPredefines) ||
215          Second->ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
216                                          SuggestedPredefines);
217 }
218 
219 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
220                                            unsigned Value) {
221   First->ReadCounter(M, Value);
222   Second->ReadCounter(M, Value);
223 }
224 
225 bool ChainedASTReaderListener::needsInputFileVisitation() {
226   return First->needsInputFileVisitation() ||
227          Second->needsInputFileVisitation();
228 }
229 
230 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
231   return First->needsSystemInputFileVisitation() ||
232   Second->needsSystemInputFileVisitation();
233 }
234 
235 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
236                                                ModuleKind Kind) {
237   First->visitModuleFile(Filename, Kind);
238   Second->visitModuleFile(Filename, Kind);
239 }
240 
241 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
242                                               bool isSystem,
243                                               bool isOverridden,
244                                               bool isExplicitModule) {
245   bool Continue = false;
246   if (First->needsInputFileVisitation() &&
247       (!isSystem || First->needsSystemInputFileVisitation()))
248     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
249                                       isExplicitModule);
250   if (Second->needsInputFileVisitation() &&
251       (!isSystem || Second->needsSystemInputFileVisitation()))
252     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
253                                        isExplicitModule);
254   return Continue;
255 }
256 
257 void ChainedASTReaderListener::readModuleFileExtension(
258        const ModuleFileExtensionMetadata &Metadata) {
259   First->readModuleFileExtension(Metadata);
260   Second->readModuleFileExtension(Metadata);
261 }
262 
263 //===----------------------------------------------------------------------===//
264 // PCH validator implementation
265 //===----------------------------------------------------------------------===//
266 
267 ASTReaderListener::~ASTReaderListener() = default;
268 
269 /// Compare the given set of language options against an existing set of
270 /// language options.
271 ///
272 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
273 /// \param AllowCompatibleDifferences If true, differences between compatible
274 ///        language options will be permitted.
275 ///
276 /// \returns true if the languagae options mis-match, false otherwise.
277 static bool checkLanguageOptions(const LangOptions &LangOpts,
278                                  const LangOptions &ExistingLangOpts,
279                                  DiagnosticsEngine *Diags,
280                                  bool AllowCompatibleDifferences = true) {
281 #define LANGOPT(Name, Bits, Default, Description)                   \
282   if (ExistingLangOpts.Name != LangOpts.Name) {                     \
283     if (Diags) {                                                    \
284       if (Bits == 1)                                                \
285         Diags->Report(diag::err_pch_langopt_mismatch)               \
286           << Description << LangOpts.Name << ExistingLangOpts.Name; \
287       else                                                          \
288         Diags->Report(diag::err_pch_langopt_value_mismatch)         \
289           << Description;                                           \
290     }                                                               \
291     return true;                                                    \
292   }
293 
294 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
295   if (ExistingLangOpts.Name != LangOpts.Name) {           \
296     if (Diags)                                            \
297       Diags->Report(diag::err_pch_langopt_value_mismatch) \
298         << Description;                                   \
299     return true;                                          \
300   }
301 
302 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
303   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
304     if (Diags)                                                 \
305       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
306         << Description;                                        \
307     return true;                                               \
308   }
309 
310 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
311   if (!AllowCompatibleDifferences)                            \
312     LANGOPT(Name, Bits, Default, Description)
313 
314 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
315   if (!AllowCompatibleDifferences)                                 \
316     ENUM_LANGOPT(Name, Bits, Default, Description)
317 
318 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
319   if (!AllowCompatibleDifferences)                                 \
320     VALUE_LANGOPT(Name, Bits, Default, Description)
321 
322 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
323 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
324 #define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
325 #include "clang/Basic/LangOptions.def"
326 
327   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
328     if (Diags)
329       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
330     return true;
331   }
332 
333   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
334     if (Diags)
335       Diags->Report(diag::err_pch_langopt_value_mismatch)
336       << "target Objective-C runtime";
337     return true;
338   }
339 
340   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
341       LangOpts.CommentOpts.BlockCommandNames) {
342     if (Diags)
343       Diags->Report(diag::err_pch_langopt_value_mismatch)
344         << "block command names";
345     return true;
346   }
347 
348   // Sanitizer feature mismatches are treated as compatible differences. If
349   // compatible differences aren't allowed, we still only want to check for
350   // mismatches of non-modular sanitizers (the only ones which can affect AST
351   // generation).
352   if (!AllowCompatibleDifferences) {
353     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
354     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
355     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
356     ExistingSanitizers.clear(ModularSanitizers);
357     ImportedSanitizers.clear(ModularSanitizers);
358     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
359       const std::string Flag = "-fsanitize=";
360       if (Diags) {
361 #define SANITIZER(NAME, ID)                                                    \
362   {                                                                            \
363     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
364     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
365     if (InExistingModule != InImportedModule)                                  \
366       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
367           << InExistingModule << (Flag + NAME);                                \
368   }
369 #include "clang/Basic/Sanitizers.def"
370       }
371       return true;
372     }
373   }
374 
375   return false;
376 }
377 
378 /// Compare the given set of target options against an existing set of
379 /// target options.
380 ///
381 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
382 ///
383 /// \returns true if the target options mis-match, false otherwise.
384 static bool checkTargetOptions(const TargetOptions &TargetOpts,
385                                const TargetOptions &ExistingTargetOpts,
386                                DiagnosticsEngine *Diags,
387                                bool AllowCompatibleDifferences = true) {
388 #define CHECK_TARGET_OPT(Field, Name)                             \
389   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
390     if (Diags)                                                    \
391       Diags->Report(diag::err_pch_targetopt_mismatch)             \
392         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
393     return true;                                                  \
394   }
395 
396   // The triple and ABI must match exactly.
397   CHECK_TARGET_OPT(Triple, "target");
398   CHECK_TARGET_OPT(ABI, "target ABI");
399 
400   // We can tolerate different CPUs in many cases, notably when one CPU
401   // supports a strict superset of another. When allowing compatible
402   // differences skip this check.
403   if (!AllowCompatibleDifferences) {
404     CHECK_TARGET_OPT(CPU, "target CPU");
405     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
406   }
407 
408 #undef CHECK_TARGET_OPT
409 
410   // Compare feature sets.
411   SmallVector<StringRef, 4> ExistingFeatures(
412                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
413                                              ExistingTargetOpts.FeaturesAsWritten.end());
414   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
415                                          TargetOpts.FeaturesAsWritten.end());
416   llvm::sort(ExistingFeatures);
417   llvm::sort(ReadFeatures);
418 
419   // We compute the set difference in both directions explicitly so that we can
420   // diagnose the differences differently.
421   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
422   std::set_difference(
423       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
424       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
425   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
426                       ExistingFeatures.begin(), ExistingFeatures.end(),
427                       std::back_inserter(UnmatchedReadFeatures));
428 
429   // If we are allowing compatible differences and the read feature set is
430   // a strict subset of the existing feature set, there is nothing to diagnose.
431   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
432     return false;
433 
434   if (Diags) {
435     for (StringRef Feature : UnmatchedReadFeatures)
436       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
437           << /* is-existing-feature */ false << Feature;
438     for (StringRef Feature : UnmatchedExistingFeatures)
439       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
440           << /* is-existing-feature */ true << Feature;
441   }
442 
443   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
444 }
445 
446 bool
447 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
448                                   bool Complain,
449                                   bool AllowCompatibleDifferences) {
450   const LangOptions &ExistingLangOpts = PP.getLangOpts();
451   return checkLanguageOptions(LangOpts, ExistingLangOpts,
452                               Complain ? &Reader.Diags : nullptr,
453                               AllowCompatibleDifferences);
454 }
455 
456 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
457                                      bool Complain,
458                                      bool AllowCompatibleDifferences) {
459   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
460   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
461                             Complain ? &Reader.Diags : nullptr,
462                             AllowCompatibleDifferences);
463 }
464 
465 namespace {
466 
467 using MacroDefinitionsMap =
468     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
469 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
470 
471 } // namespace
472 
473 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
474                                          DiagnosticsEngine &Diags,
475                                          bool Complain) {
476   using Level = DiagnosticsEngine::Level;
477 
478   // Check current mappings for new -Werror mappings, and the stored mappings
479   // for cases that were explicitly mapped to *not* be errors that are now
480   // errors because of options like -Werror.
481   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
482 
483   for (DiagnosticsEngine *MappingSource : MappingSources) {
484     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
485       diag::kind DiagID = DiagIDMappingPair.first;
486       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
487       if (CurLevel < DiagnosticsEngine::Error)
488         continue; // not significant
489       Level StoredLevel =
490           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
491       if (StoredLevel < DiagnosticsEngine::Error) {
492         if (Complain)
493           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
494               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
495         return true;
496       }
497     }
498   }
499 
500   return false;
501 }
502 
503 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
504   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
505   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
506     return true;
507   return Ext >= diag::Severity::Error;
508 }
509 
510 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
511                                     DiagnosticsEngine &Diags, bool IsSystem,
512                                     bool SystemHeaderWarningsInModule,
513                                     bool Complain) {
514   // Top-level options
515   if (IsSystem) {
516     if (Diags.getSuppressSystemWarnings())
517       return false;
518     // If -Wsystem-headers was not enabled before, and it was not explicit,
519     // be conservative
520     if (StoredDiags.getSuppressSystemWarnings() &&
521         !SystemHeaderWarningsInModule) {
522       if (Complain)
523         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
524       return true;
525     }
526   }
527 
528   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
529     if (Complain)
530       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
531     return true;
532   }
533 
534   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
535       !StoredDiags.getEnableAllWarnings()) {
536     if (Complain)
537       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
538     return true;
539   }
540 
541   if (isExtHandlingFromDiagsError(Diags) &&
542       !isExtHandlingFromDiagsError(StoredDiags)) {
543     if (Complain)
544       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
545     return true;
546   }
547 
548   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
549 }
550 
551 /// Return the top import module if it is implicit, nullptr otherwise.
552 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
553                                           Preprocessor &PP) {
554   // If the original import came from a file explicitly generated by the user,
555   // don't check the diagnostic mappings.
556   // FIXME: currently this is approximated by checking whether this is not a
557   // module import of an implicitly-loaded module file.
558   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
559   // the transitive closure of its imports, since unrelated modules cannot be
560   // imported until after this module finishes validation.
561   ModuleFile *TopImport = &*ModuleMgr.rbegin();
562   while (!TopImport->ImportedBy.empty())
563     TopImport = TopImport->ImportedBy[0];
564   if (TopImport->Kind != MK_ImplicitModule)
565     return nullptr;
566 
567   StringRef ModuleName = TopImport->ModuleName;
568   assert(!ModuleName.empty() && "diagnostic options read before module name");
569 
570   Module *M =
571       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
572   assert(M && "missing module");
573   return M;
574 }
575 
576 bool PCHValidator::ReadDiagnosticOptions(
577     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
578   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
579   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
580   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
581       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
582   // This should never fail, because we would have processed these options
583   // before writing them to an ASTFile.
584   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
585 
586   ModuleManager &ModuleMgr = Reader.getModuleManager();
587   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
588 
589   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
590   if (!TopM)
591     return false;
592 
593   Module *Importer = PP.getCurrentModule();
594 
595   DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
596   bool SystemHeaderWarningsInModule =
597       Importer && llvm::is_contained(ExistingOpts.SystemHeaderWarningsModules,
598                                      Importer->Name);
599 
600   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
601   // contains the union of their flags.
602   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
603                                  SystemHeaderWarningsInModule, Complain);
604 }
605 
606 /// Collect the macro definitions provided by the given preprocessor
607 /// options.
608 static void
609 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
610                         MacroDefinitionsMap &Macros,
611                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
612   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
613     StringRef Macro = PPOpts.Macros[I].first;
614     bool IsUndef = PPOpts.Macros[I].second;
615 
616     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
617     StringRef MacroName = MacroPair.first;
618     StringRef MacroBody = MacroPair.second;
619 
620     // For an #undef'd macro, we only care about the name.
621     if (IsUndef) {
622       if (MacroNames && !Macros.count(MacroName))
623         MacroNames->push_back(MacroName);
624 
625       Macros[MacroName] = std::make_pair("", true);
626       continue;
627     }
628 
629     // For a #define'd macro, figure out the actual definition.
630     if (MacroName.size() == Macro.size())
631       MacroBody = "1";
632     else {
633       // Note: GCC drops anything following an end-of-line character.
634       StringRef::size_type End = MacroBody.find_first_of("\n\r");
635       MacroBody = MacroBody.substr(0, End);
636     }
637 
638     if (MacroNames && !Macros.count(MacroName))
639       MacroNames->push_back(MacroName);
640     Macros[MacroName] = std::make_pair(MacroBody, false);
641   }
642 }
643 
644 enum OptionValidation {
645   OptionValidateNone,
646   OptionValidateContradictions,
647   OptionValidateStrictMatches,
648 };
649 
650 /// Check the preprocessor options deserialized from the control block
651 /// against the preprocessor options in an existing preprocessor.
652 ///
653 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
654 /// \param Validation If set to OptionValidateNone, ignore differences in
655 ///        preprocessor options. If set to OptionValidateContradictions,
656 ///        require that options passed both in the AST file and on the command
657 ///        line (-D or -U) match, but tolerate options missing in one or the
658 ///        other. If set to OptionValidateContradictions, require that there
659 ///        are no differences in the options between the two.
660 static bool checkPreprocessorOptions(
661     const PreprocessorOptions &PPOpts,
662     const PreprocessorOptions &ExistingPPOpts, bool ReadMacros,
663     DiagnosticsEngine *Diags, FileManager &FileMgr,
664     std::string &SuggestedPredefines, const LangOptions &LangOpts,
665     OptionValidation Validation = OptionValidateContradictions) {
666   if (ReadMacros) {
667     // Check macro definitions.
668     MacroDefinitionsMap ASTFileMacros;
669     collectMacroDefinitions(PPOpts, ASTFileMacros);
670     MacroDefinitionsMap ExistingMacros;
671     SmallVector<StringRef, 4> ExistingMacroNames;
672     collectMacroDefinitions(ExistingPPOpts, ExistingMacros,
673                             &ExistingMacroNames);
674 
675     // Use a line marker to enter the <command line> file, as the defines and
676     // undefines here will have come from the command line.
677     SuggestedPredefines += "# 1 \"<command line>\" 1\n";
678 
679     for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
680       // Dig out the macro definition in the existing preprocessor options.
681       StringRef MacroName = ExistingMacroNames[I];
682       std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
683 
684       // Check whether we know anything about this macro name or not.
685       llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
686           ASTFileMacros.find(MacroName);
687       if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
688         if (Validation == OptionValidateStrictMatches) {
689           // If strict matches are requested, don't tolerate any extra defines
690           // on the command line that are missing in the AST file.
691           if (Diags) {
692             Diags->Report(diag::err_pch_macro_def_undef) << MacroName << true;
693           }
694           return true;
695         }
696         // FIXME: Check whether this identifier was referenced anywhere in the
697         // AST file. If so, we should reject the AST file. Unfortunately, this
698         // information isn't in the control block. What shall we do about it?
699 
700         if (Existing.second) {
701           SuggestedPredefines += "#undef ";
702           SuggestedPredefines += MacroName.str();
703           SuggestedPredefines += '\n';
704         } else {
705           SuggestedPredefines += "#define ";
706           SuggestedPredefines += MacroName.str();
707           SuggestedPredefines += ' ';
708           SuggestedPredefines += Existing.first.str();
709           SuggestedPredefines += '\n';
710         }
711         continue;
712       }
713 
714       // If the macro was defined in one but undef'd in the other, we have a
715       // conflict.
716       if (Existing.second != Known->second.second) {
717         if (Diags) {
718           Diags->Report(diag::err_pch_macro_def_undef)
719               << MacroName << Known->second.second;
720         }
721         return true;
722       }
723 
724       // If the macro was #undef'd in both, or if the macro bodies are
725       // identical, it's fine.
726       if (Existing.second || Existing.first == Known->second.first) {
727         ASTFileMacros.erase(Known);
728         continue;
729       }
730 
731       // The macro bodies differ; complain.
732       if (Diags) {
733         Diags->Report(diag::err_pch_macro_def_conflict)
734             << MacroName << Known->second.first << Existing.first;
735       }
736       return true;
737     }
738 
739     // Leave the <command line> file and return to <built-in>.
740     SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
741 
742     if (Validation == OptionValidateStrictMatches) {
743       // If strict matches are requested, don't tolerate any extra defines in
744       // the AST file that are missing on the command line.
745       for (const auto &MacroName : ASTFileMacros.keys()) {
746         if (Diags) {
747           Diags->Report(diag::err_pch_macro_def_undef) << MacroName << false;
748         }
749         return true;
750       }
751     }
752   }
753 
754   // Check whether we're using predefines.
755   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
756       Validation != OptionValidateNone) {
757     if (Diags) {
758       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
759     }
760     return true;
761   }
762 
763   // Detailed record is important since it is used for the module cache hash.
764   if (LangOpts.Modules &&
765       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
766       Validation != OptionValidateNone) {
767     if (Diags) {
768       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
769     }
770     return true;
771   }
772 
773   // Compute the #include and #include_macros lines we need.
774   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
775     StringRef File = ExistingPPOpts.Includes[I];
776 
777     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
778         !ExistingPPOpts.PCHThroughHeader.empty()) {
779       // In case the through header is an include, we must add all the includes
780       // to the predefines so the start point can be determined.
781       SuggestedPredefines += "#include \"";
782       SuggestedPredefines += File;
783       SuggestedPredefines += "\"\n";
784       continue;
785     }
786 
787     if (File == ExistingPPOpts.ImplicitPCHInclude)
788       continue;
789 
790     if (llvm::is_contained(PPOpts.Includes, File))
791       continue;
792 
793     SuggestedPredefines += "#include \"";
794     SuggestedPredefines += File;
795     SuggestedPredefines += "\"\n";
796   }
797 
798   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
799     StringRef File = ExistingPPOpts.MacroIncludes[I];
800     if (llvm::is_contained(PPOpts.MacroIncludes, File))
801       continue;
802 
803     SuggestedPredefines += "#__include_macros \"";
804     SuggestedPredefines += File;
805     SuggestedPredefines += "\"\n##\n";
806   }
807 
808   return false;
809 }
810 
811 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
812                                            bool ReadMacros, bool Complain,
813                                            std::string &SuggestedPredefines) {
814   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
815 
816   return checkPreprocessorOptions(
817       PPOpts, ExistingPPOpts, ReadMacros, Complain ? &Reader.Diags : nullptr,
818       PP.getFileManager(), SuggestedPredefines, PP.getLangOpts());
819 }
820 
821 bool SimpleASTReaderListener::ReadPreprocessorOptions(
822     const PreprocessorOptions &PPOpts, bool ReadMacros, bool Complain,
823     std::string &SuggestedPredefines) {
824   return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(), ReadMacros,
825                                   nullptr, PP.getFileManager(),
826                                   SuggestedPredefines, PP.getLangOpts(),
827                                   OptionValidateNone);
828 }
829 
830 /// Check the header search options deserialized from the control block
831 /// against the header search options in an existing preprocessor.
832 ///
833 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
834 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
835                                      StringRef SpecificModuleCachePath,
836                                      StringRef ExistingModuleCachePath,
837                                      DiagnosticsEngine *Diags,
838                                      const LangOptions &LangOpts,
839                                      const PreprocessorOptions &PPOpts) {
840   if (LangOpts.Modules) {
841     if (SpecificModuleCachePath != ExistingModuleCachePath &&
842         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
843       if (Diags)
844         Diags->Report(diag::err_pch_modulecache_mismatch)
845           << SpecificModuleCachePath << ExistingModuleCachePath;
846       return true;
847     }
848   }
849 
850   return false;
851 }
852 
853 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
854                                            StringRef SpecificModuleCachePath,
855                                            bool Complain) {
856   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
857                                   PP.getHeaderSearchInfo().getModuleCachePath(),
858                                   Complain ? &Reader.Diags : nullptr,
859                                   PP.getLangOpts(), PP.getPreprocessorOpts());
860 }
861 
862 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
863   PP.setCounterValue(Value);
864 }
865 
866 //===----------------------------------------------------------------------===//
867 // AST reader implementation
868 //===----------------------------------------------------------------------===//
869 
870 static uint64_t readULEB(const unsigned char *&P) {
871   unsigned Length = 0;
872   const char *Error = nullptr;
873 
874   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
875   if (Error)
876     llvm::report_fatal_error(Error);
877   P += Length;
878   return Val;
879 }
880 
881 /// Read ULEB-encoded key length and data length.
882 static std::pair<unsigned, unsigned>
883 readULEBKeyDataLength(const unsigned char *&P) {
884   unsigned KeyLen = readULEB(P);
885   if ((unsigned)KeyLen != KeyLen)
886     llvm::report_fatal_error("key too large");
887 
888   unsigned DataLen = readULEB(P);
889   if ((unsigned)DataLen != DataLen)
890     llvm::report_fatal_error("data too large");
891 
892   return std::make_pair(KeyLen, DataLen);
893 }
894 
895 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
896                                            bool TakeOwnership) {
897   DeserializationListener = Listener;
898   OwnsDeserializationListener = TakeOwnership;
899 }
900 
901 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
902   return serialization::ComputeHash(Sel);
903 }
904 
905 std::pair<unsigned, unsigned>
906 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
907   return readULEBKeyDataLength(d);
908 }
909 
910 ASTSelectorLookupTrait::internal_key_type
911 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
912   using namespace llvm::support;
913 
914   SelectorTable &SelTable = Reader.getContext().Selectors;
915   unsigned N =
916       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
917   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
918       F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
919   if (N == 0)
920     return SelTable.getNullarySelector(FirstII);
921   else if (N == 1)
922     return SelTable.getUnarySelector(FirstII);
923 
924   SmallVector<IdentifierInfo *, 16> Args;
925   Args.push_back(FirstII);
926   for (unsigned I = 1; I != N; ++I)
927     Args.push_back(Reader.getLocalIdentifier(
928         F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
929 
930   return SelTable.getSelector(N, Args.data());
931 }
932 
933 ASTSelectorLookupTrait::data_type
934 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
935                                  unsigned DataLen) {
936   using namespace llvm::support;
937 
938   data_type Result;
939 
940   Result.ID = Reader.getGlobalSelectorID(
941       F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
942   unsigned FullInstanceBits =
943       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
944   unsigned FullFactoryBits =
945       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
946   Result.InstanceBits = FullInstanceBits & 0x3;
947   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
948   Result.FactoryBits = FullFactoryBits & 0x3;
949   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
950   unsigned NumInstanceMethods = FullInstanceBits >> 3;
951   unsigned NumFactoryMethods = FullFactoryBits >> 3;
952 
953   // Load instance methods
954   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
955     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
956             F,
957             endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
958       Result.Instance.push_back(Method);
959   }
960 
961   // Load factory methods
962   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
963     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
964             F,
965             endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)))
966       Result.Factory.push_back(Method);
967   }
968 
969   return Result;
970 }
971 
972 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
973   return llvm::djbHash(a);
974 }
975 
976 std::pair<unsigned, unsigned>
977 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
978   return readULEBKeyDataLength(d);
979 }
980 
981 ASTIdentifierLookupTraitBase::internal_key_type
982 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
983   assert(n >= 2 && d[n-1] == '\0');
984   return StringRef((const char*) d, n-1);
985 }
986 
987 /// Whether the given identifier is "interesting".
988 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
989                                     bool IsModule) {
990   return II.hadMacroDefinition() || II.isPoisoned() ||
991          (!IsModule && II.getObjCOrBuiltinID()) ||
992          II.hasRevertedTokenIDToIdentifier() ||
993          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
994           II.getFETokenInfo());
995 }
996 
997 static bool readBit(unsigned &Bits) {
998   bool Value = Bits & 0x1;
999   Bits >>= 1;
1000   return Value;
1001 }
1002 
1003 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1004   using namespace llvm::support;
1005 
1006   unsigned RawID =
1007       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1008   return Reader.getGlobalIdentifierID(F, RawID >> 1);
1009 }
1010 
1011 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
1012   if (!II.isFromAST()) {
1013     II.setIsFromAST();
1014     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1015     if (isInterestingIdentifier(Reader, II, IsModule))
1016       II.setChangedSinceDeserialization();
1017   }
1018 }
1019 
1020 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1021                                                    const unsigned char* d,
1022                                                    unsigned DataLen) {
1023   using namespace llvm::support;
1024 
1025   unsigned RawID =
1026       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1027   bool IsInteresting = RawID & 0x01;
1028 
1029   // Wipe out the "is interesting" bit.
1030   RawID = RawID >> 1;
1031 
1032   // Build the IdentifierInfo and link the identifier ID with it.
1033   IdentifierInfo *II = KnownII;
1034   if (!II) {
1035     II = &Reader.getIdentifierTable().getOwn(k);
1036     KnownII = II;
1037   }
1038   markIdentifierFromAST(Reader, *II);
1039   Reader.markIdentifierUpToDate(II);
1040 
1041   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
1042   if (!IsInteresting) {
1043     // For uninteresting identifiers, there's nothing else to do. Just notify
1044     // the reader that we've finished loading this identifier.
1045     Reader.SetIdentifierInfo(ID, II);
1046     return II;
1047   }
1048 
1049   unsigned ObjCOrBuiltinID =
1050       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1051   unsigned Bits =
1052       endian::readNext<uint16_t, llvm::endianness::little, unaligned>(d);
1053   bool CPlusPlusOperatorKeyword = readBit(Bits);
1054   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1055   bool Poisoned = readBit(Bits);
1056   bool ExtensionToken = readBit(Bits);
1057   bool HadMacroDefinition = readBit(Bits);
1058 
1059   assert(Bits == 0 && "Extra bits in the identifier?");
1060   DataLen -= 8;
1061 
1062   // Set or check the various bits in the IdentifierInfo structure.
1063   // Token IDs are read-only.
1064   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1065     II->revertTokenIDToIdentifier();
1066   if (!F.isModule())
1067     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1068   assert(II->isExtensionToken() == ExtensionToken &&
1069          "Incorrect extension token flag");
1070   (void)ExtensionToken;
1071   if (Poisoned)
1072     II->setIsPoisoned(true);
1073   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1074          "Incorrect C++ operator keyword flag");
1075   (void)CPlusPlusOperatorKeyword;
1076 
1077   // If this identifier is a macro, deserialize the macro
1078   // definition.
1079   if (HadMacroDefinition) {
1080     uint32_t MacroDirectivesOffset =
1081         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1082     DataLen -= 4;
1083 
1084     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1085   }
1086 
1087   Reader.SetIdentifierInfo(ID, II);
1088 
1089   // Read all of the declarations visible at global scope with this
1090   // name.
1091   if (DataLen > 0) {
1092     SmallVector<uint32_t, 4> DeclIDs;
1093     for (; DataLen > 0; DataLen -= 4)
1094       DeclIDs.push_back(Reader.getGlobalDeclID(
1095           F,
1096           endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)));
1097     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1098   }
1099 
1100   return II;
1101 }
1102 
1103 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1104     : Kind(Name.getNameKind()) {
1105   switch (Kind) {
1106   case DeclarationName::Identifier:
1107     Data = (uint64_t)Name.getAsIdentifierInfo();
1108     break;
1109   case DeclarationName::ObjCZeroArgSelector:
1110   case DeclarationName::ObjCOneArgSelector:
1111   case DeclarationName::ObjCMultiArgSelector:
1112     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1113     break;
1114   case DeclarationName::CXXOperatorName:
1115     Data = Name.getCXXOverloadedOperator();
1116     break;
1117   case DeclarationName::CXXLiteralOperatorName:
1118     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1119     break;
1120   case DeclarationName::CXXDeductionGuideName:
1121     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1122                ->getDeclName().getAsIdentifierInfo();
1123     break;
1124   case DeclarationName::CXXConstructorName:
1125   case DeclarationName::CXXDestructorName:
1126   case DeclarationName::CXXConversionFunctionName:
1127   case DeclarationName::CXXUsingDirective:
1128     Data = 0;
1129     break;
1130   }
1131 }
1132 
1133 unsigned DeclarationNameKey::getHash() const {
1134   llvm::FoldingSetNodeID ID;
1135   ID.AddInteger(Kind);
1136 
1137   switch (Kind) {
1138   case DeclarationName::Identifier:
1139   case DeclarationName::CXXLiteralOperatorName:
1140   case DeclarationName::CXXDeductionGuideName:
1141     ID.AddString(((IdentifierInfo*)Data)->getName());
1142     break;
1143   case DeclarationName::ObjCZeroArgSelector:
1144   case DeclarationName::ObjCOneArgSelector:
1145   case DeclarationName::ObjCMultiArgSelector:
1146     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1147     break;
1148   case DeclarationName::CXXOperatorName:
1149     ID.AddInteger((OverloadedOperatorKind)Data);
1150     break;
1151   case DeclarationName::CXXConstructorName:
1152   case DeclarationName::CXXDestructorName:
1153   case DeclarationName::CXXConversionFunctionName:
1154   case DeclarationName::CXXUsingDirective:
1155     break;
1156   }
1157 
1158   return ID.ComputeHash();
1159 }
1160 
1161 ModuleFile *
1162 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1163   using namespace llvm::support;
1164 
1165   uint32_t ModuleFileID =
1166       endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1167   return Reader.getLocalModuleFile(F, ModuleFileID);
1168 }
1169 
1170 std::pair<unsigned, unsigned>
1171 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1172   return readULEBKeyDataLength(d);
1173 }
1174 
1175 ASTDeclContextNameLookupTrait::internal_key_type
1176 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1177   using namespace llvm::support;
1178 
1179   auto Kind = (DeclarationName::NameKind)*d++;
1180   uint64_t Data;
1181   switch (Kind) {
1182   case DeclarationName::Identifier:
1183   case DeclarationName::CXXLiteralOperatorName:
1184   case DeclarationName::CXXDeductionGuideName:
1185     Data = (uint64_t)Reader.getLocalIdentifier(
1186         F, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
1187     break;
1188   case DeclarationName::ObjCZeroArgSelector:
1189   case DeclarationName::ObjCOneArgSelector:
1190   case DeclarationName::ObjCMultiArgSelector:
1191     Data =
1192         (uint64_t)Reader
1193             .getLocalSelector(
1194                 F,
1195                 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(
1196                     d))
1197             .getAsOpaquePtr();
1198     break;
1199   case DeclarationName::CXXOperatorName:
1200     Data = *d++; // OverloadedOperatorKind
1201     break;
1202   case DeclarationName::CXXConstructorName:
1203   case DeclarationName::CXXDestructorName:
1204   case DeclarationName::CXXConversionFunctionName:
1205   case DeclarationName::CXXUsingDirective:
1206     Data = 0;
1207     break;
1208   }
1209 
1210   return DeclarationNameKey(Kind, Data);
1211 }
1212 
1213 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1214                                                  const unsigned char *d,
1215                                                  unsigned DataLen,
1216                                                  data_type_builder &Val) {
1217   using namespace llvm::support;
1218 
1219   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1220     uint32_t LocalID =
1221         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
1222     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1223   }
1224 }
1225 
1226 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1227                                               BitstreamCursor &Cursor,
1228                                               uint64_t Offset,
1229                                               DeclContext *DC) {
1230   assert(Offset != 0);
1231 
1232   SavedStreamPosition SavedPosition(Cursor);
1233   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1234     Error(std::move(Err));
1235     return true;
1236   }
1237 
1238   RecordData Record;
1239   StringRef Blob;
1240   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1241   if (!MaybeCode) {
1242     Error(MaybeCode.takeError());
1243     return true;
1244   }
1245   unsigned Code = MaybeCode.get();
1246 
1247   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1248   if (!MaybeRecCode) {
1249     Error(MaybeRecCode.takeError());
1250     return true;
1251   }
1252   unsigned RecCode = MaybeRecCode.get();
1253   if (RecCode != DECL_CONTEXT_LEXICAL) {
1254     Error("Expected lexical block");
1255     return true;
1256   }
1257 
1258   assert(!isa<TranslationUnitDecl>(DC) &&
1259          "expected a TU_UPDATE_LEXICAL record for TU");
1260   // If we are handling a C++ class template instantiation, we can see multiple
1261   // lexical updates for the same record. It's important that we select only one
1262   // of them, so that field numbering works properly. Just pick the first one we
1263   // see.
1264   auto &Lex = LexicalDecls[DC];
1265   if (!Lex.first) {
1266     Lex = std::make_pair(
1267         &M, llvm::ArrayRef(
1268                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1269                     Blob.data()),
1270                 Blob.size() / 4));
1271   }
1272   DC->setHasExternalLexicalStorage(true);
1273   return false;
1274 }
1275 
1276 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1277                                               BitstreamCursor &Cursor,
1278                                               uint64_t Offset,
1279                                               DeclID ID) {
1280   assert(Offset != 0);
1281 
1282   SavedStreamPosition SavedPosition(Cursor);
1283   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1284     Error(std::move(Err));
1285     return true;
1286   }
1287 
1288   RecordData Record;
1289   StringRef Blob;
1290   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1291   if (!MaybeCode) {
1292     Error(MaybeCode.takeError());
1293     return true;
1294   }
1295   unsigned Code = MaybeCode.get();
1296 
1297   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1298   if (!MaybeRecCode) {
1299     Error(MaybeRecCode.takeError());
1300     return true;
1301   }
1302   unsigned RecCode = MaybeRecCode.get();
1303   if (RecCode != DECL_CONTEXT_VISIBLE) {
1304     Error("Expected visible lookup table block");
1305     return true;
1306   }
1307 
1308   // We can't safely determine the primary context yet, so delay attaching the
1309   // lookup table until we're done with recursive deserialization.
1310   auto *Data = (const unsigned char*)Blob.data();
1311   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1312   return false;
1313 }
1314 
1315 void ASTReader::Error(StringRef Msg) const {
1316   Error(diag::err_fe_pch_malformed, Msg);
1317   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1318       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1319     Diag(diag::note_module_cache_path)
1320       << PP.getHeaderSearchInfo().getModuleCachePath();
1321   }
1322 }
1323 
1324 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1325                       StringRef Arg3) const {
1326   if (Diags.isDiagnosticInFlight())
1327     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1328   else
1329     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1330 }
1331 
1332 void ASTReader::Error(llvm::Error &&Err) const {
1333   llvm::Error RemainingErr =
1334       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1335         auto Diag = E.getDiagnostic().second;
1336 
1337         // Ideally we'd just emit it, but have to handle a possible in-flight
1338         // diagnostic. Note that the location is currently ignored as well.
1339         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1340         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1341         StringRef Arg1, Arg2, Arg3;
1342         switch (NumArgs) {
1343         case 3:
1344           Arg3 = Diag.getStringArg(2);
1345           [[fallthrough]];
1346         case 2:
1347           Arg2 = Diag.getStringArg(1);
1348           [[fallthrough]];
1349         case 1:
1350           Arg1 = Diag.getStringArg(0);
1351         }
1352         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1353       });
1354   if (RemainingErr)
1355     Error(toString(std::move(RemainingErr)));
1356 }
1357 
1358 //===----------------------------------------------------------------------===//
1359 // Source Manager Deserialization
1360 //===----------------------------------------------------------------------===//
1361 
1362 /// Read the line table in the source manager block.
1363 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1364   unsigned Idx = 0;
1365   LineTableInfo &LineTable = SourceMgr.getLineTable();
1366 
1367   // Parse the file names
1368   std::map<int, int> FileIDs;
1369   FileIDs[-1] = -1; // For unspecified filenames.
1370   for (unsigned I = 0; Record[Idx]; ++I) {
1371     // Extract the file name
1372     auto Filename = ReadPath(F, Record, Idx);
1373     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1374   }
1375   ++Idx;
1376 
1377   // Parse the line entries
1378   std::vector<LineEntry> Entries;
1379   while (Idx < Record.size()) {
1380     FileID FID = ReadFileID(F, Record, Idx);
1381 
1382     // Extract the line entries
1383     unsigned NumEntries = Record[Idx++];
1384     assert(NumEntries && "no line entries for file ID");
1385     Entries.clear();
1386     Entries.reserve(NumEntries);
1387     for (unsigned I = 0; I != NumEntries; ++I) {
1388       unsigned FileOffset = Record[Idx++];
1389       unsigned LineNo = Record[Idx++];
1390       int FilenameID = FileIDs[Record[Idx++]];
1391       SrcMgr::CharacteristicKind FileKind
1392         = (SrcMgr::CharacteristicKind)Record[Idx++];
1393       unsigned IncludeOffset = Record[Idx++];
1394       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1395                                        FileKind, IncludeOffset));
1396     }
1397     LineTable.AddEntry(FID, Entries);
1398   }
1399 }
1400 
1401 /// Read a source manager block
1402 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1403   using namespace SrcMgr;
1404 
1405   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1406 
1407   // Set the source-location entry cursor to the current position in
1408   // the stream. This cursor will be used to read the contents of the
1409   // source manager block initially, and then lazily read
1410   // source-location entries as needed.
1411   SLocEntryCursor = F.Stream;
1412 
1413   // The stream itself is going to skip over the source manager block.
1414   if (llvm::Error Err = F.Stream.SkipBlock())
1415     return Err;
1416 
1417   // Enter the source manager block.
1418   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1419     return Err;
1420   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1421 
1422   RecordData Record;
1423   while (true) {
1424     Expected<llvm::BitstreamEntry> MaybeE =
1425         SLocEntryCursor.advanceSkippingSubblocks();
1426     if (!MaybeE)
1427       return MaybeE.takeError();
1428     llvm::BitstreamEntry E = MaybeE.get();
1429 
1430     switch (E.Kind) {
1431     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1432     case llvm::BitstreamEntry::Error:
1433       return llvm::createStringError(std::errc::illegal_byte_sequence,
1434                                      "malformed block record in AST file");
1435     case llvm::BitstreamEntry::EndBlock:
1436       return llvm::Error::success();
1437     case llvm::BitstreamEntry::Record:
1438       // The interesting case.
1439       break;
1440     }
1441 
1442     // Read a record.
1443     Record.clear();
1444     StringRef Blob;
1445     Expected<unsigned> MaybeRecord =
1446         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1447     if (!MaybeRecord)
1448       return MaybeRecord.takeError();
1449     switch (MaybeRecord.get()) {
1450     default:  // Default behavior: ignore.
1451       break;
1452 
1453     case SM_SLOC_FILE_ENTRY:
1454     case SM_SLOC_BUFFER_ENTRY:
1455     case SM_SLOC_EXPANSION_ENTRY:
1456       // Once we hit one of the source location entries, we're done.
1457       return llvm::Error::success();
1458     }
1459   }
1460 }
1461 
1462 llvm::Expected<SourceLocation::UIntTy>
1463 ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1464   BitstreamCursor &Cursor = F->SLocEntryCursor;
1465   SavedStreamPosition SavedPosition(Cursor);
1466   if (llvm::Error Err = Cursor.JumpToBit(F->SLocEntryOffsetsBase +
1467                                          F->SLocEntryOffsets[Index]))
1468     return std::move(Err);
1469 
1470   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1471   if (!MaybeEntry)
1472     return MaybeEntry.takeError();
1473 
1474   llvm::BitstreamEntry Entry = MaybeEntry.get();
1475   if (Entry.Kind != llvm::BitstreamEntry::Record)
1476     return llvm::createStringError(
1477         std::errc::illegal_byte_sequence,
1478         "incorrectly-formatted source location entry in AST file");
1479 
1480   RecordData Record;
1481   StringRef Blob;
1482   Expected<unsigned> MaybeSLOC = Cursor.readRecord(Entry.ID, Record, &Blob);
1483   if (!MaybeSLOC)
1484     return MaybeSLOC.takeError();
1485 
1486   switch (MaybeSLOC.get()) {
1487   default:
1488     return llvm::createStringError(
1489         std::errc::illegal_byte_sequence,
1490         "incorrectly-formatted source location entry in AST file");
1491   case SM_SLOC_FILE_ENTRY:
1492   case SM_SLOC_BUFFER_ENTRY:
1493   case SM_SLOC_EXPANSION_ENTRY:
1494     return F->SLocEntryBaseOffset + Record[0];
1495   }
1496 }
1497 
1498 int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1499   auto SLocMapI =
1500       GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - SLocOffset - 1);
1501   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1502          "Corrupted global sloc offset map");
1503   ModuleFile *F = SLocMapI->second;
1504 
1505   bool Invalid = false;
1506 
1507   auto It = llvm::upper_bound(
1508       llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,
1509       [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1510         int ID = F->SLocEntryBaseID + LocalIndex;
1511         std::size_t Index = -ID - 2;
1512         if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1513           assert(!SourceMgr.SLocEntryLoaded[Index]);
1514           auto MaybeEntryOffset = readSLocOffset(F, LocalIndex);
1515           if (!MaybeEntryOffset) {
1516             Error(MaybeEntryOffset.takeError());
1517             Invalid = true;
1518             return true;
1519           }
1520           SourceMgr.LoadedSLocEntryTable[Index] =
1521               SrcMgr::SLocEntry::getOffsetOnly(*MaybeEntryOffset);
1522           SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1523         }
1524         return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1525       });
1526 
1527   if (Invalid)
1528     return 0;
1529 
1530   // The iterator points to the first entry with start offset greater than the
1531   // offset of interest. The previous entry must contain the offset of interest.
1532   return F->SLocEntryBaseID + *std::prev(It);
1533 }
1534 
1535 bool ASTReader::ReadSLocEntry(int ID) {
1536   if (ID == 0)
1537     return false;
1538 
1539   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1540     Error("source location entry ID out-of-range for AST file");
1541     return true;
1542   }
1543 
1544   // Local helper to read the (possibly-compressed) buffer data following the
1545   // entry record.
1546   auto ReadBuffer = [this](
1547       BitstreamCursor &SLocEntryCursor,
1548       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1549     RecordData Record;
1550     StringRef Blob;
1551     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1552     if (!MaybeCode) {
1553       Error(MaybeCode.takeError());
1554       return nullptr;
1555     }
1556     unsigned Code = MaybeCode.get();
1557 
1558     Expected<unsigned> MaybeRecCode =
1559         SLocEntryCursor.readRecord(Code, Record, &Blob);
1560     if (!MaybeRecCode) {
1561       Error(MaybeRecCode.takeError());
1562       return nullptr;
1563     }
1564     unsigned RecCode = MaybeRecCode.get();
1565 
1566     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1567       // Inspect the first byte to differentiate zlib (\x78) and zstd
1568       // (little-endian 0xFD2FB528).
1569       const llvm::compression::Format F =
1570           Blob.size() > 0 && Blob.data()[0] == 0x78
1571               ? llvm::compression::Format::Zlib
1572               : llvm::compression::Format::Zstd;
1573       if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1574         Error(Reason);
1575         return nullptr;
1576       }
1577       SmallVector<uint8_t, 0> Decompressed;
1578       if (llvm::Error E = llvm::compression::decompress(
1579               F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {
1580         Error("could not decompress embedded file contents: " +
1581               llvm::toString(std::move(E)));
1582         return nullptr;
1583       }
1584       return llvm::MemoryBuffer::getMemBufferCopy(
1585           llvm::toStringRef(Decompressed), Name);
1586     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1587       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1588     } else {
1589       Error("AST record has invalid code");
1590       return nullptr;
1591     }
1592   };
1593 
1594   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1595   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1596           F->SLocEntryOffsetsBase +
1597           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1598     Error(std::move(Err));
1599     return true;
1600   }
1601 
1602   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1603   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1604 
1605   ++NumSLocEntriesRead;
1606   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1607   if (!MaybeEntry) {
1608     Error(MaybeEntry.takeError());
1609     return true;
1610   }
1611   llvm::BitstreamEntry Entry = MaybeEntry.get();
1612 
1613   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1614     Error("incorrectly-formatted source location entry in AST file");
1615     return true;
1616   }
1617 
1618   RecordData Record;
1619   StringRef Blob;
1620   Expected<unsigned> MaybeSLOC =
1621       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1622   if (!MaybeSLOC) {
1623     Error(MaybeSLOC.takeError());
1624     return true;
1625   }
1626   switch (MaybeSLOC.get()) {
1627   default:
1628     Error("incorrectly-formatted source location entry in AST file");
1629     return true;
1630 
1631   case SM_SLOC_FILE_ENTRY: {
1632     // We will detect whether a file changed and return 'Failure' for it, but
1633     // we will also try to fail gracefully by setting up the SLocEntry.
1634     unsigned InputID = Record[4];
1635     InputFile IF = getInputFile(*F, InputID);
1636     OptionalFileEntryRef File = IF.getFile();
1637     bool OverriddenBuffer = IF.isOverridden();
1638 
1639     // Note that we only check if a File was returned. If it was out-of-date
1640     // we have complained but we will continue creating a FileID to recover
1641     // gracefully.
1642     if (!File)
1643       return true;
1644 
1645     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1646     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1647       // This is the module's main file.
1648       IncludeLoc = getImportLocation(F);
1649     }
1650     SrcMgr::CharacteristicKind
1651       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1652     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1653                                         BaseOffset + Record[0]);
1654     SrcMgr::FileInfo &FileInfo =
1655           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1656     FileInfo.NumCreatedFIDs = Record[5];
1657     if (Record[3])
1658       FileInfo.setHasLineDirectives();
1659 
1660     unsigned NumFileDecls = Record[7];
1661     if (NumFileDecls && ContextObj) {
1662       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1663       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1664       FileDeclIDs[FID] =
1665           FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1666     }
1667 
1668     const SrcMgr::ContentCache &ContentCache =
1669         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1670     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1671         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1672         !ContentCache.getBufferIfLoaded()) {
1673       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1674       if (!Buffer)
1675         return true;
1676       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1677     }
1678 
1679     break;
1680   }
1681 
1682   case SM_SLOC_BUFFER_ENTRY: {
1683     const char *Name = Blob.data();
1684     unsigned Offset = Record[0];
1685     SrcMgr::CharacteristicKind
1686       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1687     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1688     if (IncludeLoc.isInvalid() && F->isModule()) {
1689       IncludeLoc = getImportLocation(F);
1690     }
1691 
1692     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1693     if (!Buffer)
1694       return true;
1695     FileID FID = SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1696                                         BaseOffset + Offset, IncludeLoc);
1697     if (Record[3]) {
1698       auto &FileInfo =
1699           const_cast<SrcMgr::FileInfo &>(SourceMgr.getSLocEntry(FID).getFile());
1700       FileInfo.setHasLineDirectives();
1701     }
1702     break;
1703   }
1704 
1705   case SM_SLOC_EXPANSION_ENTRY: {
1706     LocSeq::State Seq;
1707     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq);
1708     SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq);
1709     SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq);
1710     SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,
1711                                  Record[5], Record[4], ID,
1712                                  BaseOffset + Record[0]);
1713     break;
1714   }
1715   }
1716 
1717   return false;
1718 }
1719 
1720 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1721   if (ID == 0)
1722     return std::make_pair(SourceLocation(), "");
1723 
1724   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1725     Error("source location entry ID out-of-range for AST file");
1726     return std::make_pair(SourceLocation(), "");
1727   }
1728 
1729   // Find which module file this entry lands in.
1730   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1731   if (!M->isModule())
1732     return std::make_pair(SourceLocation(), "");
1733 
1734   // FIXME: Can we map this down to a particular submodule? That would be
1735   // ideal.
1736   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1737 }
1738 
1739 /// Find the location where the module F is imported.
1740 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1741   if (F->ImportLoc.isValid())
1742     return F->ImportLoc;
1743 
1744   // Otherwise we have a PCH. It's considered to be "imported" at the first
1745   // location of its includer.
1746   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1747     // Main file is the importer.
1748     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1749     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1750   }
1751   return F->ImportedBy[0]->FirstLoc;
1752 }
1753 
1754 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1755 /// the abbreviations that are at the top of the block and then leave the cursor
1756 /// pointing into the block.
1757 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1758                                         unsigned BlockID,
1759                                         uint64_t *StartOfBlockOffset) {
1760   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1761     return Err;
1762 
1763   if (StartOfBlockOffset)
1764     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1765 
1766   while (true) {
1767     uint64_t Offset = Cursor.GetCurrentBitNo();
1768     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1769     if (!MaybeCode)
1770       return MaybeCode.takeError();
1771     unsigned Code = MaybeCode.get();
1772 
1773     // We expect all abbrevs to be at the start of the block.
1774     if (Code != llvm::bitc::DEFINE_ABBREV) {
1775       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1776         return Err;
1777       return llvm::Error::success();
1778     }
1779     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1780       return Err;
1781   }
1782 }
1783 
1784 Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
1785                            unsigned &Idx) {
1786   Token Tok;
1787   Tok.startToken();
1788   Tok.setLocation(ReadSourceLocation(M, Record, Idx));
1789   Tok.setKind((tok::TokenKind)Record[Idx++]);
1790   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1791 
1792   if (Tok.isAnnotation()) {
1793     Tok.setAnnotationEndLoc(ReadSourceLocation(M, Record, Idx));
1794     switch (Tok.getKind()) {
1795     case tok::annot_pragma_loop_hint: {
1796       auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1797       Info->PragmaName = ReadToken(M, Record, Idx);
1798       Info->Option = ReadToken(M, Record, Idx);
1799       unsigned NumTokens = Record[Idx++];
1800       SmallVector<Token, 4> Toks;
1801       Toks.reserve(NumTokens);
1802       for (unsigned I = 0; I < NumTokens; ++I)
1803         Toks.push_back(ReadToken(M, Record, Idx));
1804       Info->Toks = llvm::ArrayRef(Toks).copy(PP.getPreprocessorAllocator());
1805       Tok.setAnnotationValue(static_cast<void *>(Info));
1806       break;
1807     }
1808     case tok::annot_pragma_pack: {
1809       auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
1810       Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
1811       auto SlotLabel = ReadString(Record, Idx);
1812       Info->SlotLabel =
1813           llvm::StringRef(SlotLabel).copy(PP.getPreprocessorAllocator());
1814       Info->Alignment = ReadToken(M, Record, Idx);
1815       Tok.setAnnotationValue(static_cast<void *>(Info));
1816       break;
1817     }
1818     // Some annotation tokens do not use the PtrData field.
1819     case tok::annot_pragma_openmp:
1820     case tok::annot_pragma_openmp_end:
1821     case tok::annot_pragma_unused:
1822     case tok::annot_pragma_openacc:
1823     case tok::annot_pragma_openacc_end:
1824       break;
1825     default:
1826       llvm_unreachable("missing deserialization code for annotation token");
1827     }
1828   } else {
1829     Tok.setLength(Record[Idx++]);
1830     if (IdentifierInfo *II = getLocalIdentifier(M, Record[Idx++]))
1831       Tok.setIdentifierInfo(II);
1832   }
1833   return Tok;
1834 }
1835 
1836 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1837   BitstreamCursor &Stream = F.MacroCursor;
1838 
1839   // Keep track of where we are in the stream, then jump back there
1840   // after reading this macro.
1841   SavedStreamPosition SavedPosition(Stream);
1842 
1843   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1844     // FIXME this drops errors on the floor.
1845     consumeError(std::move(Err));
1846     return nullptr;
1847   }
1848   RecordData Record;
1849   SmallVector<IdentifierInfo*, 16> MacroParams;
1850   MacroInfo *Macro = nullptr;
1851   llvm::MutableArrayRef<Token> MacroTokens;
1852 
1853   while (true) {
1854     // Advance to the next record, but if we get to the end of the block, don't
1855     // pop it (removing all the abbreviations from the cursor) since we want to
1856     // be able to reseek within the block and read entries.
1857     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1858     Expected<llvm::BitstreamEntry> MaybeEntry =
1859         Stream.advanceSkippingSubblocks(Flags);
1860     if (!MaybeEntry) {
1861       Error(MaybeEntry.takeError());
1862       return Macro;
1863     }
1864     llvm::BitstreamEntry Entry = MaybeEntry.get();
1865 
1866     switch (Entry.Kind) {
1867     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1868     case llvm::BitstreamEntry::Error:
1869       Error("malformed block record in AST file");
1870       return Macro;
1871     case llvm::BitstreamEntry::EndBlock:
1872       return Macro;
1873     case llvm::BitstreamEntry::Record:
1874       // The interesting case.
1875       break;
1876     }
1877 
1878     // Read a record.
1879     Record.clear();
1880     PreprocessorRecordTypes RecType;
1881     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1882       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1883     else {
1884       Error(MaybeRecType.takeError());
1885       return Macro;
1886     }
1887     switch (RecType) {
1888     case PP_MODULE_MACRO:
1889     case PP_MACRO_DIRECTIVE_HISTORY:
1890       return Macro;
1891 
1892     case PP_MACRO_OBJECT_LIKE:
1893     case PP_MACRO_FUNCTION_LIKE: {
1894       // If we already have a macro, that means that we've hit the end
1895       // of the definition of the macro we were looking for. We're
1896       // done.
1897       if (Macro)
1898         return Macro;
1899 
1900       unsigned NextIndex = 1; // Skip identifier ID.
1901       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1902       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1903       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1904       MI->setIsUsed(Record[NextIndex++]);
1905       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1906       MacroTokens = MI->allocateTokens(Record[NextIndex++],
1907                                        PP.getPreprocessorAllocator());
1908       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1909         // Decode function-like macro info.
1910         bool isC99VarArgs = Record[NextIndex++];
1911         bool isGNUVarArgs = Record[NextIndex++];
1912         bool hasCommaPasting = Record[NextIndex++];
1913         MacroParams.clear();
1914         unsigned NumArgs = Record[NextIndex++];
1915         for (unsigned i = 0; i != NumArgs; ++i)
1916           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1917 
1918         // Install function-like macro info.
1919         MI->setIsFunctionLike();
1920         if (isC99VarArgs) MI->setIsC99Varargs();
1921         if (isGNUVarArgs) MI->setIsGNUVarargs();
1922         if (hasCommaPasting) MI->setHasCommaPasting();
1923         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1924       }
1925 
1926       // Remember that we saw this macro last so that we add the tokens that
1927       // form its body to it.
1928       Macro = MI;
1929 
1930       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1931           Record[NextIndex]) {
1932         // We have a macro definition. Register the association
1933         PreprocessedEntityID
1934             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1935         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1936         PreprocessingRecord::PPEntityID PPID =
1937             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1938         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1939             PPRec.getPreprocessedEntity(PPID));
1940         if (PPDef)
1941           PPRec.RegisterMacroDefinition(Macro, PPDef);
1942       }
1943 
1944       ++NumMacrosRead;
1945       break;
1946     }
1947 
1948     case PP_TOKEN: {
1949       // If we see a TOKEN before a PP_MACRO_*, then the file is
1950       // erroneous, just pretend we didn't see this.
1951       if (!Macro) break;
1952       if (MacroTokens.empty()) {
1953         Error("unexpected number of macro tokens for a macro in AST file");
1954         return Macro;
1955       }
1956 
1957       unsigned Idx = 0;
1958       MacroTokens[0] = ReadToken(F, Record, Idx);
1959       MacroTokens = MacroTokens.drop_front();
1960       break;
1961     }
1962     }
1963   }
1964 }
1965 
1966 PreprocessedEntityID
1967 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1968                                          unsigned LocalID) const {
1969   if (!M.ModuleOffsetMap.empty())
1970     ReadModuleOffsetMap(M);
1971 
1972   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1973     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1974   assert(I != M.PreprocessedEntityRemap.end()
1975          && "Invalid index into preprocessed entity index remap");
1976 
1977   return LocalID + I->second;
1978 }
1979 
1980 const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
1981   FileManager &FileMgr = Reader.getFileManager();
1982   if (!Key.Imported) {
1983     if (auto File = FileMgr.getFile(Key.Filename))
1984       return *File;
1985     return nullptr;
1986   }
1987 
1988   std::string Resolved = std::string(Key.Filename);
1989   Reader.ResolveImportedPath(M, Resolved);
1990   if (auto File = FileMgr.getFile(Resolved))
1991     return *File;
1992   return nullptr;
1993 }
1994 
1995 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1996   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1997 }
1998 
1999 HeaderFileInfoTrait::internal_key_type
2000 HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2001   internal_key_type ikey = {ekey.getSize(),
2002                             M.HasTimestamps ? ekey.getModificationTime() : 0,
2003                             ekey.getName(), /*Imported*/ false};
2004   return ikey;
2005 }
2006 
2007 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2008   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2009     return false;
2010 
2011   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
2012     return true;
2013 
2014   // Determine whether the actual files are equivalent.
2015   const FileEntry *FEA = getFile(a);
2016   const FileEntry *FEB = getFile(b);
2017   return FEA && FEA == FEB;
2018 }
2019 
2020 std::pair<unsigned, unsigned>
2021 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2022   return readULEBKeyDataLength(d);
2023 }
2024 
2025 HeaderFileInfoTrait::internal_key_type
2026 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2027   using namespace llvm::support;
2028 
2029   internal_key_type ikey;
2030   ikey.Size =
2031       off_t(endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2032   ikey.ModTime = time_t(
2033       endian::readNext<uint64_t, llvm::endianness::little, unaligned>(d));
2034   ikey.Filename = (const char *)d;
2035   ikey.Imported = true;
2036   return ikey;
2037 }
2038 
2039 HeaderFileInfoTrait::data_type
2040 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2041                               unsigned DataLen) {
2042   using namespace llvm::support;
2043 
2044   const unsigned char *End = d + DataLen;
2045   HeaderFileInfo HFI;
2046   unsigned Flags = *d++;
2047 
2048   bool Included = (Flags >> 6) & 0x01;
2049   if (Included)
2050     if (const FileEntry *FE = getFile(key))
2051       // Not using \c Preprocessor::markIncluded(), since that would attempt to
2052       // deserialize this header file info again.
2053       Reader.getPreprocessor().getIncludedFiles().insert(FE);
2054 
2055   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2056   HFI.isImport |= (Flags >> 5) & 0x01;
2057   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2058   HFI.DirInfo = (Flags >> 1) & 0x07;
2059   HFI.IndexHeaderMapHeader = Flags & 0x01;
2060   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
2061       M, endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d));
2062   if (unsigned FrameworkOffset =
2063           endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d)) {
2064     // The framework offset is 1 greater than the actual offset,
2065     // since 0 is used as an indicator for "no framework name".
2066     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2067     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
2068   }
2069 
2070   assert((End - d) % 4 == 0 &&
2071          "Wrong data length in HeaderFileInfo deserialization");
2072   while (d != End) {
2073     uint32_t LocalSMID =
2074         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);
2075     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2076     LocalSMID >>= 3;
2077 
2078     // This header is part of a module. Associate it with the module to enable
2079     // implicit module import.
2080     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
2081     Module *Mod = Reader.getSubmodule(GlobalSMID);
2082     FileManager &FileMgr = Reader.getFileManager();
2083     ModuleMap &ModMap =
2084         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2085 
2086     std::string Filename = std::string(key.Filename);
2087     if (key.Imported)
2088       Reader.ResolveImportedPath(M, Filename);
2089     if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2090       // FIXME: NameAsWritten
2091       Module::Header H = {std::string(key.Filename), "", *FE};
2092       ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);
2093     }
2094     HFI.isModuleHeader |= ModuleMap::isModular(HeaderRole);
2095   }
2096 
2097   // This HeaderFileInfo was externally loaded.
2098   HFI.External = true;
2099   HFI.IsValid = true;
2100   return HFI;
2101 }
2102 
2103 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2104                                 uint32_t MacroDirectivesOffset) {
2105   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2106   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
2107 }
2108 
2109 void ASTReader::ReadDefinedMacros() {
2110   // Note that we are loading defined macros.
2111   Deserializing Macros(this);
2112 
2113   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
2114     BitstreamCursor &MacroCursor = I.MacroCursor;
2115 
2116     // If there was no preprocessor block, skip this file.
2117     if (MacroCursor.getBitcodeBytes().empty())
2118       continue;
2119 
2120     BitstreamCursor Cursor = MacroCursor;
2121     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
2122       Error(std::move(Err));
2123       return;
2124     }
2125 
2126     RecordData Record;
2127     while (true) {
2128       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2129       if (!MaybeE) {
2130         Error(MaybeE.takeError());
2131         return;
2132       }
2133       llvm::BitstreamEntry E = MaybeE.get();
2134 
2135       switch (E.Kind) {
2136       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2137       case llvm::BitstreamEntry::Error:
2138         Error("malformed block record in AST file");
2139         return;
2140       case llvm::BitstreamEntry::EndBlock:
2141         goto NextCursor;
2142 
2143       case llvm::BitstreamEntry::Record: {
2144         Record.clear();
2145         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2146         if (!MaybeRecord) {
2147           Error(MaybeRecord.takeError());
2148           return;
2149         }
2150         switch (MaybeRecord.get()) {
2151         default:  // Default behavior: ignore.
2152           break;
2153 
2154         case PP_MACRO_OBJECT_LIKE:
2155         case PP_MACRO_FUNCTION_LIKE: {
2156           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2157           if (II->isOutOfDate())
2158             updateOutOfDateIdentifier(*II);
2159           break;
2160         }
2161 
2162         case PP_TOKEN:
2163           // Ignore tokens.
2164           break;
2165         }
2166         break;
2167       }
2168       }
2169     }
2170     NextCursor:  ;
2171   }
2172 }
2173 
2174 namespace {
2175 
2176   /// Visitor class used to look up identifirs in an AST file.
2177   class IdentifierLookupVisitor {
2178     StringRef Name;
2179     unsigned NameHash;
2180     unsigned PriorGeneration;
2181     unsigned &NumIdentifierLookups;
2182     unsigned &NumIdentifierLookupHits;
2183     IdentifierInfo *Found = nullptr;
2184 
2185   public:
2186     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2187                             unsigned &NumIdentifierLookups,
2188                             unsigned &NumIdentifierLookupHits)
2189       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2190         PriorGeneration(PriorGeneration),
2191         NumIdentifierLookups(NumIdentifierLookups),
2192         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2193 
2194     bool operator()(ModuleFile &M) {
2195       // If we've already searched this module file, skip it now.
2196       if (M.Generation <= PriorGeneration)
2197         return true;
2198 
2199       ASTIdentifierLookupTable *IdTable
2200         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2201       if (!IdTable)
2202         return false;
2203 
2204       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2205                                      Found);
2206       ++NumIdentifierLookups;
2207       ASTIdentifierLookupTable::iterator Pos =
2208           IdTable->find_hashed(Name, NameHash, &Trait);
2209       if (Pos == IdTable->end())
2210         return false;
2211 
2212       // Dereferencing the iterator has the effect of building the
2213       // IdentifierInfo node and populating it with the various
2214       // declarations it needs.
2215       ++NumIdentifierLookupHits;
2216       Found = *Pos;
2217       return true;
2218     }
2219 
2220     // Retrieve the identifier info found within the module
2221     // files.
2222     IdentifierInfo *getIdentifierInfo() const { return Found; }
2223   };
2224 
2225 } // namespace
2226 
2227 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2228   // Note that we are loading an identifier.
2229   Deserializing AnIdentifier(this);
2230 
2231   unsigned PriorGeneration = 0;
2232   if (getContext().getLangOpts().Modules)
2233     PriorGeneration = IdentifierGeneration[&II];
2234 
2235   // If there is a global index, look there first to determine which modules
2236   // provably do not have any results for this identifier.
2237   GlobalModuleIndex::HitSet Hits;
2238   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2239   if (!loadGlobalIndex()) {
2240     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2241       HitsPtr = &Hits;
2242     }
2243   }
2244 
2245   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2246                                   NumIdentifierLookups,
2247                                   NumIdentifierLookupHits);
2248   ModuleMgr.visit(Visitor, HitsPtr);
2249   markIdentifierUpToDate(&II);
2250 }
2251 
2252 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2253   if (!II)
2254     return;
2255 
2256   II->setOutOfDate(false);
2257 
2258   // Update the generation for this identifier.
2259   if (getContext().getLangOpts().Modules)
2260     IdentifierGeneration[II] = getGeneration();
2261 }
2262 
2263 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2264                                     const PendingMacroInfo &PMInfo) {
2265   ModuleFile &M = *PMInfo.M;
2266 
2267   BitstreamCursor &Cursor = M.MacroCursor;
2268   SavedStreamPosition SavedPosition(Cursor);
2269   if (llvm::Error Err =
2270           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2271     Error(std::move(Err));
2272     return;
2273   }
2274 
2275   struct ModuleMacroRecord {
2276     SubmoduleID SubModID;
2277     MacroInfo *MI;
2278     SmallVector<SubmoduleID, 8> Overrides;
2279   };
2280   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2281 
2282   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2283   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2284   // macro histroy.
2285   RecordData Record;
2286   while (true) {
2287     Expected<llvm::BitstreamEntry> MaybeEntry =
2288         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2289     if (!MaybeEntry) {
2290       Error(MaybeEntry.takeError());
2291       return;
2292     }
2293     llvm::BitstreamEntry Entry = MaybeEntry.get();
2294 
2295     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2296       Error("malformed block record in AST file");
2297       return;
2298     }
2299 
2300     Record.clear();
2301     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2302     if (!MaybePP) {
2303       Error(MaybePP.takeError());
2304       return;
2305     }
2306     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2307     case PP_MACRO_DIRECTIVE_HISTORY:
2308       break;
2309 
2310     case PP_MODULE_MACRO: {
2311       ModuleMacros.push_back(ModuleMacroRecord());
2312       auto &Info = ModuleMacros.back();
2313       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2314       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2315       for (int I = 2, N = Record.size(); I != N; ++I)
2316         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2317       continue;
2318     }
2319 
2320     default:
2321       Error("malformed block record in AST file");
2322       return;
2323     }
2324 
2325     // We found the macro directive history; that's the last record
2326     // for this macro.
2327     break;
2328   }
2329 
2330   // Module macros are listed in reverse dependency order.
2331   {
2332     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2333     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2334     for (auto &MMR : ModuleMacros) {
2335       Overrides.clear();
2336       for (unsigned ModID : MMR.Overrides) {
2337         Module *Mod = getSubmodule(ModID);
2338         auto *Macro = PP.getModuleMacro(Mod, II);
2339         assert(Macro && "missing definition for overridden macro");
2340         Overrides.push_back(Macro);
2341       }
2342 
2343       bool Inserted = false;
2344       Module *Owner = getSubmodule(MMR.SubModID);
2345       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2346     }
2347   }
2348 
2349   // Don't read the directive history for a module; we don't have anywhere
2350   // to put it.
2351   if (M.isModule())
2352     return;
2353 
2354   // Deserialize the macro directives history in reverse source-order.
2355   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2356   unsigned Idx = 0, N = Record.size();
2357   while (Idx < N) {
2358     MacroDirective *MD = nullptr;
2359     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2360     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2361     switch (K) {
2362     case MacroDirective::MD_Define: {
2363       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2364       MD = PP.AllocateDefMacroDirective(MI, Loc);
2365       break;
2366     }
2367     case MacroDirective::MD_Undefine:
2368       MD = PP.AllocateUndefMacroDirective(Loc);
2369       break;
2370     case MacroDirective::MD_Visibility:
2371       bool isPublic = Record[Idx++];
2372       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2373       break;
2374     }
2375 
2376     if (!Latest)
2377       Latest = MD;
2378     if (Earliest)
2379       Earliest->setPrevious(MD);
2380     Earliest = MD;
2381   }
2382 
2383   if (Latest)
2384     PP.setLoadedMacroDirective(II, Earliest, Latest);
2385 }
2386 
2387 bool ASTReader::shouldDisableValidationForFile(
2388     const serialization::ModuleFile &M) const {
2389   if (DisableValidationKind == DisableValidationForModuleKind::None)
2390     return false;
2391 
2392   // If a PCH is loaded and validation is disabled for PCH then disable
2393   // validation for the PCH and the modules it loads.
2394   ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2395 
2396   switch (K) {
2397   case MK_MainFile:
2398   case MK_Preamble:
2399   case MK_PCH:
2400     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2401   case MK_ImplicitModule:
2402   case MK_ExplicitModule:
2403   case MK_PrebuiltModule:
2404     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2405   }
2406 
2407   return false;
2408 }
2409 
2410 InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2411   // If this ID is bogus, just return an empty input file.
2412   if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2413     return InputFileInfo();
2414 
2415   // If we've already loaded this input file, return it.
2416   if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2417     return F.InputFileInfosLoaded[ID - 1];
2418 
2419   // Go find this input file.
2420   BitstreamCursor &Cursor = F.InputFilesCursor;
2421   SavedStreamPosition SavedPosition(Cursor);
2422   if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2423                                          F.InputFileOffsets[ID - 1])) {
2424     // FIXME this drops errors on the floor.
2425     consumeError(std::move(Err));
2426   }
2427 
2428   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2429   if (!MaybeCode) {
2430     // FIXME this drops errors on the floor.
2431     consumeError(MaybeCode.takeError());
2432   }
2433   unsigned Code = MaybeCode.get();
2434   RecordData Record;
2435   StringRef Blob;
2436 
2437   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2438     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2439            "invalid record type for input file");
2440   else {
2441     // FIXME this drops errors on the floor.
2442     consumeError(Maybe.takeError());
2443   }
2444 
2445   assert(Record[0] == ID && "Bogus stored ID or offset");
2446   InputFileInfo R;
2447   R.StoredSize = static_cast<off_t>(Record[1]);
2448   R.StoredTime = static_cast<time_t>(Record[2]);
2449   R.Overridden = static_cast<bool>(Record[3]);
2450   R.Transient = static_cast<bool>(Record[4]);
2451   R.TopLevel = static_cast<bool>(Record[5]);
2452   R.ModuleMap = static_cast<bool>(Record[6]);
2453   std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2454     uint16_t AsRequestedLength = Record[7];
2455 
2456     std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2457     std::string Name = Blob.substr(AsRequestedLength).str();
2458 
2459     ResolveImportedPath(F, NameAsRequested);
2460     ResolveImportedPath(F, Name);
2461 
2462     if (Name.empty())
2463       Name = NameAsRequested;
2464 
2465     return std::make_pair(std::move(NameAsRequested), std::move(Name));
2466   }();
2467 
2468   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2469   if (!MaybeEntry) // FIXME this drops errors on the floor.
2470     consumeError(MaybeEntry.takeError());
2471   llvm::BitstreamEntry Entry = MaybeEntry.get();
2472   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2473          "expected record type for input file hash");
2474 
2475   Record.clear();
2476   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2477     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2478            "invalid record type for input file hash");
2479   else {
2480     // FIXME this drops errors on the floor.
2481     consumeError(Maybe.takeError());
2482   }
2483   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2484                   static_cast<uint64_t>(Record[0]);
2485 
2486   // Note that we've loaded this input file info.
2487   F.InputFileInfosLoaded[ID - 1] = R;
2488   return R;
2489 }
2490 
2491 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2492 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2493   // If this ID is bogus, just return an empty input file.
2494   if (ID == 0 || ID > F.InputFilesLoaded.size())
2495     return InputFile();
2496 
2497   // If we've already loaded this input file, return it.
2498   if (F.InputFilesLoaded[ID-1].getFile())
2499     return F.InputFilesLoaded[ID-1];
2500 
2501   if (F.InputFilesLoaded[ID-1].isNotFound())
2502     return InputFile();
2503 
2504   // Go find this input file.
2505   BitstreamCursor &Cursor = F.InputFilesCursor;
2506   SavedStreamPosition SavedPosition(Cursor);
2507   if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2508                                          F.InputFileOffsets[ID - 1])) {
2509     // FIXME this drops errors on the floor.
2510     consumeError(std::move(Err));
2511   }
2512 
2513   InputFileInfo FI = getInputFileInfo(F, ID);
2514   off_t StoredSize = FI.StoredSize;
2515   time_t StoredTime = FI.StoredTime;
2516   bool Overridden = FI.Overridden;
2517   bool Transient = FI.Transient;
2518   StringRef Filename = FI.FilenameAsRequested;
2519   uint64_t StoredContentHash = FI.ContentHash;
2520 
2521   // For standard C++ modules, we don't need to check the inputs.
2522   bool SkipChecks = F.StandardCXXModule;
2523 
2524   const HeaderSearchOptions &HSOpts =
2525       PP.getHeaderSearchInfo().getHeaderSearchOpts();
2526 
2527   // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2528   // modules.
2529   if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2530     SkipChecks = false;
2531     Overridden = false;
2532   }
2533 
2534   auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
2535 
2536   // For an overridden file, create a virtual file with the stored
2537   // size/timestamp.
2538   if ((Overridden || Transient || SkipChecks) && !File)
2539     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2540 
2541   if (!File) {
2542     if (Complain) {
2543       std::string ErrorStr = "could not find file '";
2544       ErrorStr += Filename;
2545       ErrorStr += "' referenced by AST file '";
2546       ErrorStr += F.FileName;
2547       ErrorStr += "'";
2548       Error(ErrorStr);
2549     }
2550     // Record that we didn't find the file.
2551     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2552     return InputFile();
2553   }
2554 
2555   // Check if there was a request to override the contents of the file
2556   // that was part of the precompiled header. Overriding such a file
2557   // can lead to problems when lexing using the source locations from the
2558   // PCH.
2559   SourceManager &SM = getSourceManager();
2560   // FIXME: Reject if the overrides are different.
2561   if ((!Overridden && !Transient) && !SkipChecks &&
2562       SM.isFileOverridden(*File)) {
2563     if (Complain)
2564       Error(diag::err_fe_pch_file_overridden, Filename);
2565 
2566     // After emitting the diagnostic, bypass the overriding file to recover
2567     // (this creates a separate FileEntry).
2568     File = SM.bypassFileContentsOverride(*File);
2569     if (!File) {
2570       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2571       return InputFile();
2572     }
2573   }
2574 
2575   struct Change {
2576     enum ModificationKind {
2577       Size,
2578       ModTime,
2579       Content,
2580       None,
2581     } Kind;
2582     std::optional<int64_t> Old = std::nullopt;
2583     std::optional<int64_t> New = std::nullopt;
2584   };
2585   auto HasInputContentChanged = [&](Change OriginalChange) {
2586     assert(ValidateASTInputFilesContent &&
2587            "We should only check the content of the inputs with "
2588            "ValidateASTInputFilesContent enabled.");
2589 
2590     if (StoredContentHash == static_cast<uint64_t>(llvm::hash_code(-1)))
2591       return OriginalChange;
2592 
2593     auto MemBuffOrError = FileMgr.getBufferForFile(*File);
2594     if (!MemBuffOrError) {
2595       if (!Complain)
2596         return OriginalChange;
2597       std::string ErrorStr = "could not get buffer for file '";
2598       ErrorStr += File->getName();
2599       ErrorStr += "'";
2600       Error(ErrorStr);
2601       return OriginalChange;
2602     }
2603 
2604     // FIXME: hash_value is not guaranteed to be stable!
2605     auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2606     if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2607       return Change{Change::None};
2608 
2609     return Change{Change::Content};
2610   };
2611   auto HasInputFileChanged = [&]() {
2612     if (StoredSize != File->getSize())
2613       return Change{Change::Size, StoredSize, File->getSize()};
2614     if (!shouldDisableValidationForFile(F) && StoredTime &&
2615         StoredTime != File->getModificationTime()) {
2616       Change MTimeChange = {Change::ModTime, StoredTime,
2617                             File->getModificationTime()};
2618 
2619       // In case the modification time changes but not the content,
2620       // accept the cached file as legit.
2621       if (ValidateASTInputFilesContent)
2622         return HasInputContentChanged(MTimeChange);
2623 
2624       return MTimeChange;
2625     }
2626     return Change{Change::None};
2627   };
2628 
2629   bool IsOutOfDate = false;
2630   auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();
2631   // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2632   // enabled, it is better to check the contents of the inputs. Since we can't
2633   // get correct modified time information for inputs from overriden inputs.
2634   if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2635       F.StandardCXXModule && FileChange.Kind == Change::None)
2636     FileChange = HasInputContentChanged(FileChange);
2637 
2638   // For an overridden file, there is nothing to validate.
2639   if (!Overridden && FileChange.Kind != Change::None) {
2640     if (Complain && !Diags.isDiagnosticInFlight()) {
2641       // Build a list of the PCH imports that got us here (in reverse).
2642       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2643       while (!ImportStack.back()->ImportedBy.empty())
2644         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2645 
2646       // The top-level PCH is stale.
2647       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2648       Diag(diag::err_fe_ast_file_modified)
2649           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2650           << TopLevelPCHName << FileChange.Kind
2651           << (FileChange.Old && FileChange.New)
2652           << llvm::itostr(FileChange.Old.value_or(0))
2653           << llvm::itostr(FileChange.New.value_or(0));
2654 
2655       // Print the import stack.
2656       if (ImportStack.size() > 1) {
2657         Diag(diag::note_pch_required_by)
2658           << Filename << ImportStack[0]->FileName;
2659         for (unsigned I = 1; I < ImportStack.size(); ++I)
2660           Diag(diag::note_pch_required_by)
2661             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2662       }
2663 
2664       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2665     }
2666 
2667     IsOutOfDate = true;
2668   }
2669   // FIXME: If the file is overridden and we've already opened it,
2670   // issue an error (or split it into a separate FileEntry).
2671 
2672   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2673 
2674   // Note that we've loaded this input file.
2675   F.InputFilesLoaded[ID-1] = IF;
2676   return IF;
2677 }
2678 
2679 /// If we are loading a relocatable PCH or module file, and the filename
2680 /// is not an absolute path, add the system or module root to the beginning of
2681 /// the file name.
2682 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2683   // Resolve relative to the base directory, if we have one.
2684   if (!M.BaseDirectory.empty())
2685     return ResolveImportedPath(Filename, M.BaseDirectory);
2686 }
2687 
2688 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2689   if (Filename.empty() || llvm::sys::path::is_absolute(Filename) ||
2690       Filename == "<built-in>" || Filename == "<command line>")
2691     return;
2692 
2693   SmallString<128> Buffer;
2694   llvm::sys::path::append(Buffer, Prefix, Filename);
2695   Filename.assign(Buffer.begin(), Buffer.end());
2696 }
2697 
2698 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2699   switch (ARR) {
2700   case ASTReader::Failure: return true;
2701   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2702   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2703   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2704   case ASTReader::ConfigurationMismatch:
2705     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2706   case ASTReader::HadErrors: return true;
2707   case ASTReader::Success: return false;
2708   }
2709 
2710   llvm_unreachable("unknown ASTReadResult");
2711 }
2712 
2713 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2714     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2715     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2716     std::string &SuggestedPredefines) {
2717   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2718     // FIXME this drops errors on the floor.
2719     consumeError(std::move(Err));
2720     return Failure;
2721   }
2722 
2723   // Read all of the records in the options block.
2724   RecordData Record;
2725   ASTReadResult Result = Success;
2726   while (true) {
2727     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2728     if (!MaybeEntry) {
2729       // FIXME this drops errors on the floor.
2730       consumeError(MaybeEntry.takeError());
2731       return Failure;
2732     }
2733     llvm::BitstreamEntry Entry = MaybeEntry.get();
2734 
2735     switch (Entry.Kind) {
2736     case llvm::BitstreamEntry::Error:
2737     case llvm::BitstreamEntry::SubBlock:
2738       return Failure;
2739 
2740     case llvm::BitstreamEntry::EndBlock:
2741       return Result;
2742 
2743     case llvm::BitstreamEntry::Record:
2744       // The interesting case.
2745       break;
2746     }
2747 
2748     // Read and process a record.
2749     Record.clear();
2750     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2751     if (!MaybeRecordType) {
2752       // FIXME this drops errors on the floor.
2753       consumeError(MaybeRecordType.takeError());
2754       return Failure;
2755     }
2756     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2757     case LANGUAGE_OPTIONS: {
2758       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2759       if (ParseLanguageOptions(Record, Complain, Listener,
2760                                AllowCompatibleConfigurationMismatch))
2761         Result = ConfigurationMismatch;
2762       break;
2763     }
2764 
2765     case TARGET_OPTIONS: {
2766       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2767       if (ParseTargetOptions(Record, Complain, Listener,
2768                              AllowCompatibleConfigurationMismatch))
2769         Result = ConfigurationMismatch;
2770       break;
2771     }
2772 
2773     case FILE_SYSTEM_OPTIONS: {
2774       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2775       if (!AllowCompatibleConfigurationMismatch &&
2776           ParseFileSystemOptions(Record, Complain, Listener))
2777         Result = ConfigurationMismatch;
2778       break;
2779     }
2780 
2781     case HEADER_SEARCH_OPTIONS: {
2782       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2783       if (!AllowCompatibleConfigurationMismatch &&
2784           ParseHeaderSearchOptions(Record, Complain, Listener))
2785         Result = ConfigurationMismatch;
2786       break;
2787     }
2788 
2789     case PREPROCESSOR_OPTIONS:
2790       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2791       if (!AllowCompatibleConfigurationMismatch &&
2792           ParsePreprocessorOptions(Record, Complain, Listener,
2793                                    SuggestedPredefines))
2794         Result = ConfigurationMismatch;
2795       break;
2796     }
2797   }
2798 }
2799 
2800 ASTReader::ASTReadResult
2801 ASTReader::ReadControlBlock(ModuleFile &F,
2802                             SmallVectorImpl<ImportedModule> &Loaded,
2803                             const ModuleFile *ImportedBy,
2804                             unsigned ClientLoadCapabilities) {
2805   BitstreamCursor &Stream = F.Stream;
2806 
2807   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2808     Error(std::move(Err));
2809     return Failure;
2810   }
2811 
2812   // Lambda to read the unhashed control block the first time it's called.
2813   //
2814   // For PCM files, the unhashed control block cannot be read until after the
2815   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2816   // need to look ahead before reading the IMPORTS record.  For consistency,
2817   // this block is always read somehow (see BitstreamEntry::EndBlock).
2818   bool HasReadUnhashedControlBlock = false;
2819   auto readUnhashedControlBlockOnce = [&]() {
2820     if (!HasReadUnhashedControlBlock) {
2821       HasReadUnhashedControlBlock = true;
2822       if (ASTReadResult Result =
2823               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2824         return Result;
2825     }
2826     return Success;
2827   };
2828 
2829   bool DisableValidation = shouldDisableValidationForFile(F);
2830 
2831   // Read all of the records and blocks in the control block.
2832   RecordData Record;
2833   unsigned NumInputs = 0;
2834   unsigned NumUserInputs = 0;
2835   StringRef BaseDirectoryAsWritten;
2836   while (true) {
2837     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2838     if (!MaybeEntry) {
2839       Error(MaybeEntry.takeError());
2840       return Failure;
2841     }
2842     llvm::BitstreamEntry Entry = MaybeEntry.get();
2843 
2844     switch (Entry.Kind) {
2845     case llvm::BitstreamEntry::Error:
2846       Error("malformed block record in AST file");
2847       return Failure;
2848     case llvm::BitstreamEntry::EndBlock: {
2849       // Validate the module before returning.  This call catches an AST with
2850       // no module name and no imports.
2851       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2852         return Result;
2853 
2854       // Validate input files.
2855       const HeaderSearchOptions &HSOpts =
2856           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2857 
2858       // All user input files reside at the index range [0, NumUserInputs), and
2859       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2860       // loaded module files, ignore missing inputs.
2861       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2862           F.Kind != MK_PrebuiltModule) {
2863         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2864 
2865         // If we are reading a module, we will create a verification timestamp,
2866         // so we verify all input files.  Otherwise, verify only user input
2867         // files.
2868 
2869         unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2870         if (HSOpts.ModulesValidateOncePerBuildSession &&
2871             F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
2872             F.Kind == MK_ImplicitModule)
2873           N = NumUserInputs;
2874 
2875         for (unsigned I = 0; I < N; ++I) {
2876           InputFile IF = getInputFile(F, I+1, Complain);
2877           if (!IF.getFile() || IF.isOutOfDate())
2878             return OutOfDate;
2879         }
2880       }
2881 
2882       if (Listener)
2883         Listener->visitModuleFile(F.FileName, F.Kind);
2884 
2885       if (Listener && Listener->needsInputFileVisitation()) {
2886         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2887                                                                 : NumUserInputs;
2888         for (unsigned I = 0; I < N; ++I) {
2889           bool IsSystem = I >= NumUserInputs;
2890           InputFileInfo FI = getInputFileInfo(F, I + 1);
2891           Listener->visitInputFile(
2892               FI.FilenameAsRequested, IsSystem, FI.Overridden,
2893               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
2894         }
2895       }
2896 
2897       return Success;
2898     }
2899 
2900     case llvm::BitstreamEntry::SubBlock:
2901       switch (Entry.ID) {
2902       case INPUT_FILES_BLOCK_ID:
2903         F.InputFilesCursor = Stream;
2904         if (llvm::Error Err = Stream.SkipBlock()) {
2905           Error(std::move(Err));
2906           return Failure;
2907         }
2908         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2909           Error("malformed block record in AST file");
2910           return Failure;
2911         }
2912         F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2913         continue;
2914 
2915       case OPTIONS_BLOCK_ID:
2916         // If we're reading the first module for this group, check its options
2917         // are compatible with ours. For modules it imports, no further checking
2918         // is required, because we checked them when we built it.
2919         if (Listener && !ImportedBy) {
2920           // Should we allow the configuration of the module file to differ from
2921           // the configuration of the current translation unit in a compatible
2922           // way?
2923           //
2924           // FIXME: Allow this for files explicitly specified with -include-pch.
2925           bool AllowCompatibleConfigurationMismatch =
2926               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2927 
2928           ASTReadResult Result =
2929               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2930                                AllowCompatibleConfigurationMismatch, *Listener,
2931                                SuggestedPredefines);
2932           if (Result == Failure) {
2933             Error("malformed block record in AST file");
2934             return Result;
2935           }
2936 
2937           if (DisableValidation ||
2938               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2939             Result = Success;
2940 
2941           // If we can't load the module, exit early since we likely
2942           // will rebuild the module anyway. The stream may be in the
2943           // middle of a block.
2944           if (Result != Success)
2945             return Result;
2946         } else if (llvm::Error Err = Stream.SkipBlock()) {
2947           Error(std::move(Err));
2948           return Failure;
2949         }
2950         continue;
2951 
2952       default:
2953         if (llvm::Error Err = Stream.SkipBlock()) {
2954           Error(std::move(Err));
2955           return Failure;
2956         }
2957         continue;
2958       }
2959 
2960     case llvm::BitstreamEntry::Record:
2961       // The interesting case.
2962       break;
2963     }
2964 
2965     // Read and process a record.
2966     Record.clear();
2967     StringRef Blob;
2968     Expected<unsigned> MaybeRecordType =
2969         Stream.readRecord(Entry.ID, Record, &Blob);
2970     if (!MaybeRecordType) {
2971       Error(MaybeRecordType.takeError());
2972       return Failure;
2973     }
2974     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2975     case METADATA: {
2976       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2977         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2978           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2979                                         : diag::err_pch_version_too_new);
2980         return VersionMismatch;
2981       }
2982 
2983       bool hasErrors = Record[7];
2984       if (hasErrors && !DisableValidation) {
2985         // If requested by the caller and the module hasn't already been read
2986         // or compiled, mark modules on error as out-of-date.
2987         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2988             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2989           return OutOfDate;
2990 
2991         if (!AllowASTWithCompilerErrors) {
2992           Diag(diag::err_pch_with_compiler_errors);
2993           return HadErrors;
2994         }
2995       }
2996       if (hasErrors) {
2997         Diags.ErrorOccurred = true;
2998         Diags.UncompilableErrorOccurred = true;
2999         Diags.UnrecoverableErrorOccurred = true;
3000       }
3001 
3002       F.RelocatablePCH = Record[4];
3003       // Relative paths in a relocatable PCH are relative to our sysroot.
3004       if (F.RelocatablePCH)
3005         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3006 
3007       F.StandardCXXModule = Record[5];
3008 
3009       F.HasTimestamps = Record[6];
3010 
3011       const std::string &CurBranch = getClangFullRepositoryVersion();
3012       StringRef ASTBranch = Blob;
3013       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3014         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3015           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
3016         return VersionMismatch;
3017       }
3018       break;
3019     }
3020 
3021     case IMPORTS: {
3022       // Validate the AST before processing any imports (otherwise, untangling
3023       // them can be error-prone and expensive).  A module will have a name and
3024       // will already have been validated, but this catches the PCH case.
3025       if (ASTReadResult Result = readUnhashedControlBlockOnce())
3026         return Result;
3027 
3028       // Load each of the imported PCH files.
3029       unsigned Idx = 0, N = Record.size();
3030       while (Idx < N) {
3031         // Read information about the AST file.
3032         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3033         // Whether we're importing a standard c++ module.
3034         bool IsImportingStdCXXModule = Record[Idx++];
3035         // The import location will be the local one for now; we will adjust
3036         // all import locations of module imports after the global source
3037         // location info are setup, in ReadAST.
3038         SourceLocation ImportLoc =
3039             ReadUntranslatedSourceLocation(Record[Idx++]);
3040         off_t StoredSize = (off_t)Record[Idx++];
3041         time_t StoredModTime = (time_t)Record[Idx++];
3042         auto FirstSignatureByte = Record.begin() + Idx;
3043         ASTFileSignature StoredSignature = ASTFileSignature::create(
3044             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
3045         Idx += ASTFileSignature::size;
3046 
3047         std::string ImportedName = ReadString(Record, Idx);
3048         std::string ImportedFile;
3049 
3050         // For prebuilt and explicit modules first consult the file map for
3051         // an override. Note that here we don't search prebuilt module
3052         // directories if we're not importing standard c++ module, only the
3053         // explicit name to file mappings. Also, we will still verify the
3054         // size/signature making sure it is essentially the same file but
3055         // perhaps in a different location.
3056         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3057           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3058               ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3059 
3060         if (ImportedFile.empty()) {
3061           // It is deprecated for C++20 Named modules to use the implicitly
3062           // paths.
3063           if (IsImportingStdCXXModule)
3064             Diag(clang::diag::warn_reading_std_cxx_module_by_implicit_paths)
3065                 << ImportedName;
3066 
3067           // Use BaseDirectoryAsWritten to ensure we use the same path in the
3068           // ModuleCache as when writing.
3069           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
3070         } else
3071           SkipPath(Record, Idx);
3072 
3073         // If our client can't cope with us being out of date, we can't cope with
3074         // our dependency being missing.
3075         unsigned Capabilities = ClientLoadCapabilities;
3076         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3077           Capabilities &= ~ARR_Missing;
3078 
3079         // Load the AST file.
3080         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
3081                                   Loaded, StoredSize, StoredModTime,
3082                                   StoredSignature, Capabilities);
3083 
3084         // If we diagnosed a problem, produce a backtrace.
3085         bool recompilingFinalized =
3086             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3087             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
3088         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3089           Diag(diag::note_module_file_imported_by)
3090               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3091         if (recompilingFinalized)
3092           Diag(diag::note_module_file_conflict);
3093 
3094         switch (Result) {
3095         case Failure: return Failure;
3096           // If we have to ignore the dependency, we'll have to ignore this too.
3097         case Missing:
3098         case OutOfDate: return OutOfDate;
3099         case VersionMismatch: return VersionMismatch;
3100         case ConfigurationMismatch: return ConfigurationMismatch;
3101         case HadErrors: return HadErrors;
3102         case Success: break;
3103         }
3104       }
3105       break;
3106     }
3107 
3108     case ORIGINAL_FILE:
3109       F.OriginalSourceFileID = FileID::get(Record[0]);
3110       F.ActualOriginalSourceFileName = std::string(Blob);
3111       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
3112       ResolveImportedPath(F, F.OriginalSourceFileName);
3113       break;
3114 
3115     case ORIGINAL_FILE_ID:
3116       F.OriginalSourceFileID = FileID::get(Record[0]);
3117       break;
3118 
3119     case MODULE_NAME:
3120       F.ModuleName = std::string(Blob);
3121       Diag(diag::remark_module_import)
3122           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3123           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3124       if (Listener)
3125         Listener->ReadModuleName(F.ModuleName);
3126 
3127       // Validate the AST as soon as we have a name so we can exit early on
3128       // failure.
3129       if (ASTReadResult Result = readUnhashedControlBlockOnce())
3130         return Result;
3131 
3132       break;
3133 
3134     case MODULE_DIRECTORY: {
3135       // Save the BaseDirectory as written in the PCM for computing the module
3136       // filename for the ModuleCache.
3137       BaseDirectoryAsWritten = Blob;
3138       assert(!F.ModuleName.empty() &&
3139              "MODULE_DIRECTORY found before MODULE_NAME");
3140       F.BaseDirectory = std::string(Blob);
3141       if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3142         break;
3143       // If we've already loaded a module map file covering this module, we may
3144       // have a better path for it (relative to the current build).
3145       Module *M = PP.getHeaderSearchInfo().lookupModule(
3146           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
3147           /*AllowExtraModuleMapSearch*/ true);
3148       if (M && M->Directory) {
3149         // If we're implicitly loading a module, the base directory can't
3150         // change between the build and use.
3151         // Don't emit module relocation error if we have -fno-validate-pch
3152         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3153                   DisableValidationForModuleKind::Module) &&
3154             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3155           auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(Blob);
3156           if (!BuildDir || *BuildDir != M->Directory) {
3157             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3158               Diag(diag::err_imported_module_relocated)
3159                   << F.ModuleName << Blob << M->Directory->getName();
3160             return OutOfDate;
3161           }
3162         }
3163         F.BaseDirectory = std::string(M->Directory->getName());
3164       }
3165       break;
3166     }
3167 
3168     case MODULE_MAP_FILE:
3169       if (ASTReadResult Result =
3170               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3171         return Result;
3172       break;
3173 
3174     case INPUT_FILE_OFFSETS:
3175       NumInputs = Record[0];
3176       NumUserInputs = Record[1];
3177       F.InputFileOffsets =
3178           (const llvm::support::unaligned_uint64_t *)Blob.data();
3179       F.InputFilesLoaded.resize(NumInputs);
3180       F.InputFileInfosLoaded.resize(NumInputs);
3181       F.NumUserInputFiles = NumUserInputs;
3182       break;
3183     }
3184   }
3185 }
3186 
3187 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3188                                     unsigned ClientLoadCapabilities) {
3189   BitstreamCursor &Stream = F.Stream;
3190 
3191   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
3192     return Err;
3193   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3194 
3195   // Read all of the records and blocks for the AST file.
3196   RecordData Record;
3197   while (true) {
3198     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3199     if (!MaybeEntry)
3200       return MaybeEntry.takeError();
3201     llvm::BitstreamEntry Entry = MaybeEntry.get();
3202 
3203     switch (Entry.Kind) {
3204     case llvm::BitstreamEntry::Error:
3205       return llvm::createStringError(
3206           std::errc::illegal_byte_sequence,
3207           "error at end of module block in AST file");
3208     case llvm::BitstreamEntry::EndBlock:
3209       // Outside of C++, we do not store a lookup map for the translation unit.
3210       // Instead, mark it as needing a lookup map to be built if this module
3211       // contains any declarations lexically within it (which it always does!).
3212       // This usually has no cost, since we very rarely need the lookup map for
3213       // the translation unit outside C++.
3214       if (ASTContext *Ctx = ContextObj) {
3215         DeclContext *DC = Ctx->getTranslationUnitDecl();
3216         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3217           DC->setMustBuildLookupTable();
3218       }
3219 
3220       return llvm::Error::success();
3221     case llvm::BitstreamEntry::SubBlock:
3222       switch (Entry.ID) {
3223       case DECLTYPES_BLOCK_ID:
3224         // We lazily load the decls block, but we want to set up the
3225         // DeclsCursor cursor to point into it.  Clone our current bitcode
3226         // cursor to it, enter the block and read the abbrevs in that block.
3227         // With the main cursor, we just skip over it.
3228         F.DeclsCursor = Stream;
3229         if (llvm::Error Err = Stream.SkipBlock())
3230           return Err;
3231         if (llvm::Error Err = ReadBlockAbbrevs(
3232                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3233           return Err;
3234         break;
3235 
3236       case PREPROCESSOR_BLOCK_ID:
3237         F.MacroCursor = Stream;
3238         if (!PP.getExternalSource())
3239           PP.setExternalSource(this);
3240 
3241         if (llvm::Error Err = Stream.SkipBlock())
3242           return Err;
3243         if (llvm::Error Err =
3244                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3245           return Err;
3246         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3247         break;
3248 
3249       case PREPROCESSOR_DETAIL_BLOCK_ID:
3250         F.PreprocessorDetailCursor = Stream;
3251 
3252         if (llvm::Error Err = Stream.SkipBlock()) {
3253           return Err;
3254         }
3255         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3256                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3257           return Err;
3258         F.PreprocessorDetailStartOffset
3259         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3260 
3261         if (!PP.getPreprocessingRecord())
3262           PP.createPreprocessingRecord();
3263         if (!PP.getPreprocessingRecord()->getExternalSource())
3264           PP.getPreprocessingRecord()->SetExternalSource(*this);
3265         break;
3266 
3267       case SOURCE_MANAGER_BLOCK_ID:
3268         if (llvm::Error Err = ReadSourceManagerBlock(F))
3269           return Err;
3270         break;
3271 
3272       case SUBMODULE_BLOCK_ID:
3273         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3274           return Err;
3275         break;
3276 
3277       case COMMENTS_BLOCK_ID: {
3278         BitstreamCursor C = Stream;
3279 
3280         if (llvm::Error Err = Stream.SkipBlock())
3281           return Err;
3282         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3283           return Err;
3284         CommentsCursors.push_back(std::make_pair(C, &F));
3285         break;
3286       }
3287 
3288       default:
3289         if (llvm::Error Err = Stream.SkipBlock())
3290           return Err;
3291         break;
3292       }
3293       continue;
3294 
3295     case llvm::BitstreamEntry::Record:
3296       // The interesting case.
3297       break;
3298     }
3299 
3300     // Read and process a record.
3301     Record.clear();
3302     StringRef Blob;
3303     Expected<unsigned> MaybeRecordType =
3304         Stream.readRecord(Entry.ID, Record, &Blob);
3305     if (!MaybeRecordType)
3306       return MaybeRecordType.takeError();
3307     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3308 
3309     // If we're not loading an AST context, we don't care about most records.
3310     if (!ContextObj) {
3311       switch (RecordType) {
3312       case IDENTIFIER_TABLE:
3313       case IDENTIFIER_OFFSET:
3314       case INTERESTING_IDENTIFIERS:
3315       case STATISTICS:
3316       case PP_ASSUME_NONNULL_LOC:
3317       case PP_CONDITIONAL_STACK:
3318       case PP_COUNTER_VALUE:
3319       case SOURCE_LOCATION_OFFSETS:
3320       case MODULE_OFFSET_MAP:
3321       case SOURCE_MANAGER_LINE_TABLE:
3322       case PPD_ENTITIES_OFFSETS:
3323       case HEADER_SEARCH_TABLE:
3324       case IMPORTED_MODULES:
3325       case MACRO_OFFSET:
3326         break;
3327       default:
3328         continue;
3329       }
3330     }
3331 
3332     switch (RecordType) {
3333     default:  // Default behavior: ignore.
3334       break;
3335 
3336     case TYPE_OFFSET: {
3337       if (F.LocalNumTypes != 0)
3338         return llvm::createStringError(
3339             std::errc::illegal_byte_sequence,
3340             "duplicate TYPE_OFFSET record in AST file");
3341       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3342       F.LocalNumTypes = Record[0];
3343       unsigned LocalBaseTypeIndex = Record[1];
3344       F.BaseTypeIndex = getTotalNumTypes();
3345 
3346       if (F.LocalNumTypes > 0) {
3347         // Introduce the global -> local mapping for types within this module.
3348         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3349 
3350         // Introduce the local -> global mapping for types within this module.
3351         F.TypeRemap.insertOrReplace(
3352           std::make_pair(LocalBaseTypeIndex,
3353                          F.BaseTypeIndex - LocalBaseTypeIndex));
3354 
3355         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3356       }
3357       break;
3358     }
3359 
3360     case DECL_OFFSET: {
3361       if (F.LocalNumDecls != 0)
3362         return llvm::createStringError(
3363             std::errc::illegal_byte_sequence,
3364             "duplicate DECL_OFFSET record in AST file");
3365       F.DeclOffsets = (const DeclOffset *)Blob.data();
3366       F.LocalNumDecls = Record[0];
3367       unsigned LocalBaseDeclID = Record[1];
3368       F.BaseDeclID = getTotalNumDecls();
3369 
3370       if (F.LocalNumDecls > 0) {
3371         // Introduce the global -> local mapping for declarations within this
3372         // module.
3373         GlobalDeclMap.insert(
3374           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3375 
3376         // Introduce the local -> global mapping for declarations within this
3377         // module.
3378         F.DeclRemap.insertOrReplace(
3379           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3380 
3381         // Introduce the global -> local mapping for declarations within this
3382         // module.
3383         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3384 
3385         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3386       }
3387       break;
3388     }
3389 
3390     case TU_UPDATE_LEXICAL: {
3391       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3392       LexicalContents Contents(
3393           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3394               Blob.data()),
3395           static_cast<unsigned int>(Blob.size() / 4));
3396       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3397       TU->setHasExternalLexicalStorage(true);
3398       break;
3399     }
3400 
3401     case UPDATE_VISIBLE: {
3402       unsigned Idx = 0;
3403       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3404       auto *Data = (const unsigned char*)Blob.data();
3405       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3406       // If we've already loaded the decl, perform the updates when we finish
3407       // loading this block.
3408       if (Decl *D = GetExistingDecl(ID))
3409         PendingUpdateRecords.push_back(
3410             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3411       break;
3412     }
3413 
3414     case IDENTIFIER_TABLE:
3415       F.IdentifierTableData =
3416           reinterpret_cast<const unsigned char *>(Blob.data());
3417       if (Record[0]) {
3418         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3419             F.IdentifierTableData + Record[0],
3420             F.IdentifierTableData + sizeof(uint32_t),
3421             F.IdentifierTableData,
3422             ASTIdentifierLookupTrait(*this, F));
3423 
3424         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3425       }
3426       break;
3427 
3428     case IDENTIFIER_OFFSET: {
3429       if (F.LocalNumIdentifiers != 0)
3430         return llvm::createStringError(
3431             std::errc::illegal_byte_sequence,
3432             "duplicate IDENTIFIER_OFFSET record in AST file");
3433       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3434       F.LocalNumIdentifiers = Record[0];
3435       unsigned LocalBaseIdentifierID = Record[1];
3436       F.BaseIdentifierID = getTotalNumIdentifiers();
3437 
3438       if (F.LocalNumIdentifiers > 0) {
3439         // Introduce the global -> local mapping for identifiers within this
3440         // module.
3441         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3442                                                   &F));
3443 
3444         // Introduce the local -> global mapping for identifiers within this
3445         // module.
3446         F.IdentifierRemap.insertOrReplace(
3447           std::make_pair(LocalBaseIdentifierID,
3448                          F.BaseIdentifierID - LocalBaseIdentifierID));
3449 
3450         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3451                                  + F.LocalNumIdentifiers);
3452       }
3453       break;
3454     }
3455 
3456     case INTERESTING_IDENTIFIERS:
3457       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3458       break;
3459 
3460     case EAGERLY_DESERIALIZED_DECLS:
3461       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3462       // about "interesting" decls (for instance, if we're building a module).
3463       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3464         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3465       break;
3466 
3467     case MODULAR_CODEGEN_DECLS:
3468       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3469       // them (ie: if we're not codegenerating this module).
3470       if (F.Kind == MK_MainFile ||
3471           getContext().getLangOpts().BuildingPCHWithObjectFile)
3472         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3473           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3474       break;
3475 
3476     case SPECIAL_TYPES:
3477       if (SpecialTypes.empty()) {
3478         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3479           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3480         break;
3481       }
3482 
3483       if (SpecialTypes.size() != Record.size())
3484         return llvm::createStringError(std::errc::illegal_byte_sequence,
3485                                        "invalid special-types record");
3486 
3487       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3488         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3489         if (!SpecialTypes[I])
3490           SpecialTypes[I] = ID;
3491         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3492         // merge step?
3493       }
3494       break;
3495 
3496     case STATISTICS:
3497       TotalNumStatements += Record[0];
3498       TotalNumMacros += Record[1];
3499       TotalLexicalDeclContexts += Record[2];
3500       TotalVisibleDeclContexts += Record[3];
3501       break;
3502 
3503     case UNUSED_FILESCOPED_DECLS:
3504       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3505         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3506       break;
3507 
3508     case DELEGATING_CTORS:
3509       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3510         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3511       break;
3512 
3513     case WEAK_UNDECLARED_IDENTIFIERS:
3514       if (Record.size() % 3 != 0)
3515         return llvm::createStringError(std::errc::illegal_byte_sequence,
3516                                        "invalid weak identifiers record");
3517 
3518       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3519       // files. This isn't the way to do it :)
3520       WeakUndeclaredIdentifiers.clear();
3521 
3522       // Translate the weak, undeclared identifiers into global IDs.
3523       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3524         WeakUndeclaredIdentifiers.push_back(
3525           getGlobalIdentifierID(F, Record[I++]));
3526         WeakUndeclaredIdentifiers.push_back(
3527           getGlobalIdentifierID(F, Record[I++]));
3528         WeakUndeclaredIdentifiers.push_back(
3529             ReadSourceLocation(F, Record, I).getRawEncoding());
3530       }
3531       break;
3532 
3533     case SELECTOR_OFFSETS: {
3534       F.SelectorOffsets = (const uint32_t *)Blob.data();
3535       F.LocalNumSelectors = Record[0];
3536       unsigned LocalBaseSelectorID = Record[1];
3537       F.BaseSelectorID = getTotalNumSelectors();
3538 
3539       if (F.LocalNumSelectors > 0) {
3540         // Introduce the global -> local mapping for selectors within this
3541         // module.
3542         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3543 
3544         // Introduce the local -> global mapping for selectors within this
3545         // module.
3546         F.SelectorRemap.insertOrReplace(
3547           std::make_pair(LocalBaseSelectorID,
3548                          F.BaseSelectorID - LocalBaseSelectorID));
3549 
3550         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3551       }
3552       break;
3553     }
3554 
3555     case METHOD_POOL:
3556       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3557       if (Record[0])
3558         F.SelectorLookupTable
3559           = ASTSelectorLookupTable::Create(
3560                         F.SelectorLookupTableData + Record[0],
3561                         F.SelectorLookupTableData,
3562                         ASTSelectorLookupTrait(*this, F));
3563       TotalNumMethodPoolEntries += Record[1];
3564       break;
3565 
3566     case REFERENCED_SELECTOR_POOL:
3567       if (!Record.empty()) {
3568         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3569           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3570                                                                 Record[Idx++]));
3571           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3572                                               getRawEncoding());
3573         }
3574       }
3575       break;
3576 
3577     case PP_ASSUME_NONNULL_LOC: {
3578       unsigned Idx = 0;
3579       if (!Record.empty())
3580         PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3581             ReadSourceLocation(F, Record, Idx));
3582       break;
3583     }
3584 
3585     case PP_CONDITIONAL_STACK:
3586       if (!Record.empty()) {
3587         unsigned Idx = 0, End = Record.size() - 1;
3588         bool ReachedEOFWhileSkipping = Record[Idx++];
3589         std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3590         if (ReachedEOFWhileSkipping) {
3591           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3592           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3593           bool FoundNonSkipPortion = Record[Idx++];
3594           bool FoundElse = Record[Idx++];
3595           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3596           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3597                            FoundElse, ElseLoc);
3598         }
3599         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3600         while (Idx < End) {
3601           auto Loc = ReadSourceLocation(F, Record, Idx);
3602           bool WasSkipping = Record[Idx++];
3603           bool FoundNonSkip = Record[Idx++];
3604           bool FoundElse = Record[Idx++];
3605           ConditionalStack.push_back(
3606               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3607         }
3608         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3609       }
3610       break;
3611 
3612     case PP_COUNTER_VALUE:
3613       if (!Record.empty() && Listener)
3614         Listener->ReadCounter(F, Record[0]);
3615       break;
3616 
3617     case FILE_SORTED_DECLS:
3618       F.FileSortedDecls = (const DeclID *)Blob.data();
3619       F.NumFileSortedDecls = Record[0];
3620       break;
3621 
3622     case SOURCE_LOCATION_OFFSETS: {
3623       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3624       F.LocalNumSLocEntries = Record[0];
3625       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3626       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3627       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3628           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3629                                               SLocSpaceSize);
3630       if (!F.SLocEntryBaseID) {
3631         if (!Diags.isDiagnosticInFlight()) {
3632           Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3633           SourceMgr.noteSLocAddressSpaceUsage(Diags);
3634         }
3635         return llvm::createStringError(std::errc::invalid_argument,
3636                                        "ran out of source locations");
3637       }
3638       // Make our entry in the range map. BaseID is negative and growing, so
3639       // we invert it. Because we invert it, though, we need the other end of
3640       // the range.
3641       unsigned RangeStart =
3642           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3643       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3644       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3645 
3646       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3647       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3648       GlobalSLocOffsetMap.insert(
3649           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3650                            - SLocSpaceSize,&F));
3651 
3652       // Initialize the remapping table.
3653       // Invalid stays invalid.
3654       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3655       // This module. Base was 2 when being compiled.
3656       F.SLocRemap.insertOrReplace(std::make_pair(
3657           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3658 
3659       TotalNumSLocEntries += F.LocalNumSLocEntries;
3660       break;
3661     }
3662 
3663     case MODULE_OFFSET_MAP:
3664       F.ModuleOffsetMap = Blob;
3665       break;
3666 
3667     case SOURCE_MANAGER_LINE_TABLE:
3668       ParseLineTable(F, Record);
3669       break;
3670 
3671     case EXT_VECTOR_DECLS:
3672       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3673         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3674       break;
3675 
3676     case VTABLE_USES:
3677       if (Record.size() % 3 != 0)
3678         return llvm::createStringError(std::errc::illegal_byte_sequence,
3679                                        "Invalid VTABLE_USES record");
3680 
3681       // Later tables overwrite earlier ones.
3682       // FIXME: Modules will have some trouble with this. This is clearly not
3683       // the right way to do this.
3684       VTableUses.clear();
3685 
3686       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3687         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3688         VTableUses.push_back(
3689           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3690         VTableUses.push_back(Record[Idx++]);
3691       }
3692       break;
3693 
3694     case PENDING_IMPLICIT_INSTANTIATIONS:
3695       if (PendingInstantiations.size() % 2 != 0)
3696         return llvm::createStringError(
3697             std::errc::illegal_byte_sequence,
3698             "Invalid existing PendingInstantiations");
3699 
3700       if (Record.size() % 2 != 0)
3701         return llvm::createStringError(
3702             std::errc::illegal_byte_sequence,
3703             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3704 
3705       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3706         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3707         PendingInstantiations.push_back(
3708           ReadSourceLocation(F, Record, I).getRawEncoding());
3709       }
3710       break;
3711 
3712     case SEMA_DECL_REFS:
3713       if (Record.size() != 3)
3714         return llvm::createStringError(std::errc::illegal_byte_sequence,
3715                                        "Invalid SEMA_DECL_REFS block");
3716       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3717         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3718       break;
3719 
3720     case PPD_ENTITIES_OFFSETS: {
3721       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3722       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3723       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3724 
3725       unsigned LocalBasePreprocessedEntityID = Record[0];
3726 
3727       unsigned StartingID;
3728       if (!PP.getPreprocessingRecord())
3729         PP.createPreprocessingRecord();
3730       if (!PP.getPreprocessingRecord()->getExternalSource())
3731         PP.getPreprocessingRecord()->SetExternalSource(*this);
3732       StartingID
3733         = PP.getPreprocessingRecord()
3734             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3735       F.BasePreprocessedEntityID = StartingID;
3736 
3737       if (F.NumPreprocessedEntities > 0) {
3738         // Introduce the global -> local mapping for preprocessed entities in
3739         // this module.
3740         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3741 
3742         // Introduce the local -> global mapping for preprocessed entities in
3743         // this module.
3744         F.PreprocessedEntityRemap.insertOrReplace(
3745           std::make_pair(LocalBasePreprocessedEntityID,
3746             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3747       }
3748 
3749       break;
3750     }
3751 
3752     case PPD_SKIPPED_RANGES: {
3753       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3754       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3755       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3756 
3757       if (!PP.getPreprocessingRecord())
3758         PP.createPreprocessingRecord();
3759       if (!PP.getPreprocessingRecord()->getExternalSource())
3760         PP.getPreprocessingRecord()->SetExternalSource(*this);
3761       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3762           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3763 
3764       if (F.NumPreprocessedSkippedRanges > 0)
3765         GlobalSkippedRangeMap.insert(
3766             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3767       break;
3768     }
3769 
3770     case DECL_UPDATE_OFFSETS:
3771       if (Record.size() % 2 != 0)
3772         return llvm::createStringError(
3773             std::errc::illegal_byte_sequence,
3774             "invalid DECL_UPDATE_OFFSETS block in AST file");
3775       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3776         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3777         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3778 
3779         // If we've already loaded the decl, perform the updates when we finish
3780         // loading this block.
3781         if (Decl *D = GetExistingDecl(ID))
3782           PendingUpdateRecords.push_back(
3783               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3784       }
3785       break;
3786 
3787     case OBJC_CATEGORIES_MAP:
3788       if (F.LocalNumObjCCategoriesInMap != 0)
3789         return llvm::createStringError(
3790             std::errc::illegal_byte_sequence,
3791             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3792 
3793       F.LocalNumObjCCategoriesInMap = Record[0];
3794       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3795       break;
3796 
3797     case OBJC_CATEGORIES:
3798       F.ObjCCategories.swap(Record);
3799       break;
3800 
3801     case CUDA_SPECIAL_DECL_REFS:
3802       // Later tables overwrite earlier ones.
3803       // FIXME: Modules will have trouble with this.
3804       CUDASpecialDeclRefs.clear();
3805       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3806         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3807       break;
3808 
3809     case HEADER_SEARCH_TABLE:
3810       F.HeaderFileInfoTableData = Blob.data();
3811       F.LocalNumHeaderFileInfos = Record[1];
3812       if (Record[0]) {
3813         F.HeaderFileInfoTable
3814           = HeaderFileInfoLookupTable::Create(
3815                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3816                    (const unsigned char *)F.HeaderFileInfoTableData,
3817                    HeaderFileInfoTrait(*this, F,
3818                                        &PP.getHeaderSearchInfo(),
3819                                        Blob.data() + Record[2]));
3820 
3821         PP.getHeaderSearchInfo().SetExternalSource(this);
3822         if (!PP.getHeaderSearchInfo().getExternalLookup())
3823           PP.getHeaderSearchInfo().SetExternalLookup(this);
3824       }
3825       break;
3826 
3827     case FP_PRAGMA_OPTIONS:
3828       // Later tables overwrite earlier ones.
3829       FPPragmaOptions.swap(Record);
3830       break;
3831 
3832     case OPENCL_EXTENSIONS:
3833       for (unsigned I = 0, E = Record.size(); I != E; ) {
3834         auto Name = ReadString(Record, I);
3835         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3836         OptInfo.Supported = Record[I++] != 0;
3837         OptInfo.Enabled = Record[I++] != 0;
3838         OptInfo.WithPragma = Record[I++] != 0;
3839         OptInfo.Avail = Record[I++];
3840         OptInfo.Core = Record[I++];
3841         OptInfo.Opt = Record[I++];
3842       }
3843       break;
3844 
3845     case TENTATIVE_DEFINITIONS:
3846       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3847         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3848       break;
3849 
3850     case KNOWN_NAMESPACES:
3851       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3852         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3853       break;
3854 
3855     case UNDEFINED_BUT_USED:
3856       if (UndefinedButUsed.size() % 2 != 0)
3857         return llvm::createStringError(std::errc::illegal_byte_sequence,
3858                                        "Invalid existing UndefinedButUsed");
3859 
3860       if (Record.size() % 2 != 0)
3861         return llvm::createStringError(std::errc::illegal_byte_sequence,
3862                                        "invalid undefined-but-used record");
3863       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3864         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3865         UndefinedButUsed.push_back(
3866             ReadSourceLocation(F, Record, I).getRawEncoding());
3867       }
3868       break;
3869 
3870     case DELETE_EXPRS_TO_ANALYZE:
3871       for (unsigned I = 0, N = Record.size(); I != N;) {
3872         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3873         const uint64_t Count = Record[I++];
3874         DelayedDeleteExprs.push_back(Count);
3875         for (uint64_t C = 0; C < Count; ++C) {
3876           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3877           bool IsArrayForm = Record[I++] == 1;
3878           DelayedDeleteExprs.push_back(IsArrayForm);
3879         }
3880       }
3881       break;
3882 
3883     case IMPORTED_MODULES:
3884       if (!F.isModule()) {
3885         // If we aren't loading a module (which has its own exports), make
3886         // all of the imported modules visible.
3887         // FIXME: Deal with macros-only imports.
3888         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3889           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3890           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3891           if (GlobalID) {
3892             PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3893             if (DeserializationListener)
3894               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3895           }
3896         }
3897       }
3898       break;
3899 
3900     case MACRO_OFFSET: {
3901       if (F.LocalNumMacros != 0)
3902         return llvm::createStringError(
3903             std::errc::illegal_byte_sequence,
3904             "duplicate MACRO_OFFSET record in AST file");
3905       F.MacroOffsets = (const uint32_t *)Blob.data();
3906       F.LocalNumMacros = Record[0];
3907       unsigned LocalBaseMacroID = Record[1];
3908       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3909       F.BaseMacroID = getTotalNumMacros();
3910 
3911       if (F.LocalNumMacros > 0) {
3912         // Introduce the global -> local mapping for macros within this module.
3913         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3914 
3915         // Introduce the local -> global mapping for macros within this module.
3916         F.MacroRemap.insertOrReplace(
3917           std::make_pair(LocalBaseMacroID,
3918                          F.BaseMacroID - LocalBaseMacroID));
3919 
3920         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3921       }
3922       break;
3923     }
3924 
3925     case LATE_PARSED_TEMPLATE:
3926       LateParsedTemplates.emplace_back(
3927           std::piecewise_construct, std::forward_as_tuple(&F),
3928           std::forward_as_tuple(Record.begin(), Record.end()));
3929       break;
3930 
3931     case OPTIMIZE_PRAGMA_OPTIONS:
3932       if (Record.size() != 1)
3933         return llvm::createStringError(std::errc::illegal_byte_sequence,
3934                                        "invalid pragma optimize record");
3935       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3936       break;
3937 
3938     case MSSTRUCT_PRAGMA_OPTIONS:
3939       if (Record.size() != 1)
3940         return llvm::createStringError(std::errc::illegal_byte_sequence,
3941                                        "invalid pragma ms_struct record");
3942       PragmaMSStructState = Record[0];
3943       break;
3944 
3945     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3946       if (Record.size() != 2)
3947         return llvm::createStringError(
3948             std::errc::illegal_byte_sequence,
3949             "invalid pragma pointers to members record");
3950       PragmaMSPointersToMembersState = Record[0];
3951       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3952       break;
3953 
3954     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3955       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3956         UnusedLocalTypedefNameCandidates.push_back(
3957             getGlobalDeclID(F, Record[I]));
3958       break;
3959 
3960     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3961       if (Record.size() != 1)
3962         return llvm::createStringError(std::errc::illegal_byte_sequence,
3963                                        "invalid cuda pragma options record");
3964       ForceCUDAHostDeviceDepth = Record[0];
3965       break;
3966 
3967     case ALIGN_PACK_PRAGMA_OPTIONS: {
3968       if (Record.size() < 3)
3969         return llvm::createStringError(std::errc::illegal_byte_sequence,
3970                                        "invalid pragma pack record");
3971       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3972       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3973       unsigned NumStackEntries = Record[2];
3974       unsigned Idx = 3;
3975       // Reset the stack when importing a new module.
3976       PragmaAlignPackStack.clear();
3977       for (unsigned I = 0; I < NumStackEntries; ++I) {
3978         PragmaAlignPackStackEntry Entry;
3979         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3980         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3981         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3982         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3983         Entry.SlotLabel = PragmaAlignPackStrings.back();
3984         PragmaAlignPackStack.push_back(Entry);
3985       }
3986       break;
3987     }
3988 
3989     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3990       if (Record.size() < 3)
3991         return llvm::createStringError(std::errc::illegal_byte_sequence,
3992                                        "invalid pragma float control record");
3993       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3994       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3995       unsigned NumStackEntries = Record[2];
3996       unsigned Idx = 3;
3997       // Reset the stack when importing a new module.
3998       FpPragmaStack.clear();
3999       for (unsigned I = 0; I < NumStackEntries; ++I) {
4000         FpPragmaStackEntry Entry;
4001         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
4002         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4003         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4004         FpPragmaStrings.push_back(ReadString(Record, Idx));
4005         Entry.SlotLabel = FpPragmaStrings.back();
4006         FpPragmaStack.push_back(Entry);
4007       }
4008       break;
4009     }
4010 
4011     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4012       for (unsigned I = 0, N = Record.size(); I != N; ++I)
4013         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
4014       break;
4015     }
4016   }
4017 }
4018 
4019 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4020   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4021 
4022   // Additional remapping information.
4023   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4024   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4025   F.ModuleOffsetMap = StringRef();
4026 
4027   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
4028   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
4029     F.SLocRemap.insert(std::make_pair(0U, 0));
4030     F.SLocRemap.insert(std::make_pair(2U, 1));
4031   }
4032 
4033   // Continuous range maps we may be updating in our module.
4034   using SLocRemapBuilder =
4035       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
4036                          2>::Builder;
4037   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4038   SLocRemapBuilder SLocRemap(F.SLocRemap);
4039   RemapBuilder IdentifierRemap(F.IdentifierRemap);
4040   RemapBuilder MacroRemap(F.MacroRemap);
4041   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4042   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4043   RemapBuilder SelectorRemap(F.SelectorRemap);
4044   RemapBuilder DeclRemap(F.DeclRemap);
4045   RemapBuilder TypeRemap(F.TypeRemap);
4046 
4047   while (Data < DataEnd) {
4048     // FIXME: Looking up dependency modules by filename is horrible. Let's
4049     // start fixing this with prebuilt, explicit and implicit modules and see
4050     // how it goes...
4051     using namespace llvm::support;
4052     ModuleKind Kind = static_cast<ModuleKind>(
4053         endian::readNext<uint8_t, llvm::endianness::little, unaligned>(Data));
4054     uint16_t Len =
4055         endian::readNext<uint16_t, llvm::endianness::little, unaligned>(Data);
4056     StringRef Name = StringRef((const char*)Data, Len);
4057     Data += Len;
4058     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4059                               Kind == MK_ImplicitModule
4060                           ? ModuleMgr.lookupByModuleName(Name)
4061                           : ModuleMgr.lookupByFileName(Name));
4062     if (!OM) {
4063       std::string Msg =
4064           "SourceLocation remap refers to unknown module, cannot find ";
4065       Msg.append(std::string(Name));
4066       Error(Msg);
4067       return;
4068     }
4069 
4070     SourceLocation::UIntTy SLocOffset =
4071         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4072     uint32_t IdentifierIDOffset =
4073         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4074     uint32_t MacroIDOffset =
4075         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4076     uint32_t PreprocessedEntityIDOffset =
4077         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4078     uint32_t SubmoduleIDOffset =
4079         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4080     uint32_t SelectorIDOffset =
4081         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4082     uint32_t DeclIDOffset =
4083         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4084     uint32_t TypeIndexOffset =
4085         endian::readNext<uint32_t, llvm::endianness::little, unaligned>(Data);
4086 
4087     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4088                          RemapBuilder &Remap) {
4089       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4090       if (Offset != None)
4091         Remap.insert(std::make_pair(Offset,
4092                                     static_cast<int>(BaseOffset - Offset)));
4093     };
4094 
4095     constexpr SourceLocation::UIntTy SLocNone =
4096         std::numeric_limits<SourceLocation::UIntTy>::max();
4097     if (SLocOffset != SLocNone)
4098       SLocRemap.insert(std::make_pair(
4099           SLocOffset, static_cast<SourceLocation::IntTy>(
4100                           OM->SLocEntryBaseOffset - SLocOffset)));
4101 
4102     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
4103     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4104     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4105               PreprocessedEntityRemap);
4106     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4107     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4108     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
4109     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
4110 
4111     // Global -> local mappings.
4112     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
4113   }
4114 }
4115 
4116 ASTReader::ASTReadResult
4117 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4118                                   const ModuleFile *ImportedBy,
4119                                   unsigned ClientLoadCapabilities) {
4120   unsigned Idx = 0;
4121   F.ModuleMapPath = ReadPath(F, Record, Idx);
4122 
4123   // Try to resolve ModuleName in the current header search context and
4124   // verify that it is found in the same module map file as we saved. If the
4125   // top-level AST file is a main file, skip this check because there is no
4126   // usable header search context.
4127   assert(!F.ModuleName.empty() &&
4128          "MODULE_NAME should come before MODULE_MAP_FILE");
4129   if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4130       F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4131     // An implicitly-loaded module file should have its module listed in some
4132     // module map file that we've already loaded.
4133     Module *M =
4134         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
4135     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4136     OptionalFileEntryRef ModMap =
4137         M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4138     // Don't emit module relocation error if we have -fno-validate-pch
4139     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4140               DisableValidationForModuleKind::Module) &&
4141         !ModMap) {
4142       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
4143         if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4144           // This module was defined by an imported (explicit) module.
4145           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4146                                                << ASTFE->getName();
4147         } else {
4148           // This module was built with a different module map.
4149           Diag(diag::err_imported_module_not_found)
4150               << F.ModuleName << F.FileName
4151               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4152               << !ImportedBy;
4153           // In case it was imported by a PCH, there's a chance the user is
4154           // just missing to include the search path to the directory containing
4155           // the modulemap.
4156           if (ImportedBy && ImportedBy->Kind == MK_PCH)
4157             Diag(diag::note_imported_by_pch_module_not_found)
4158                 << llvm::sys::path::parent_path(F.ModuleMapPath);
4159         }
4160       }
4161       return OutOfDate;
4162     }
4163 
4164     assert(M && M->Name == F.ModuleName && "found module with different name");
4165 
4166     // Check the primary module map file.
4167     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
4168     if (!StoredModMap || *StoredModMap != ModMap) {
4169       assert(ModMap && "found module is missing module map file");
4170       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4171              "top-level import should be verified");
4172       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4173       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4174         Diag(diag::err_imported_module_modmap_changed)
4175             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4176             << ModMap->getName() << F.ModuleMapPath << NotImported;
4177       return OutOfDate;
4178     }
4179 
4180     ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4181     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4182       // FIXME: we should use input files rather than storing names.
4183       std::string Filename = ReadPath(F, Record, Idx);
4184       auto SF = FileMgr.getOptionalFileRef(Filename, false, false);
4185       if (!SF) {
4186         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4187           Error("could not find file '" + Filename +"' referenced by AST file");
4188         return OutOfDate;
4189       }
4190       AdditionalStoredMaps.insert(*SF);
4191     }
4192 
4193     // Check any additional module map files (e.g. module.private.modulemap)
4194     // that are not in the pcm.
4195     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4196       for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4197         // Remove files that match
4198         // Note: SmallPtrSet::erase is really remove
4199         if (!AdditionalStoredMaps.erase(ModMap)) {
4200           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4201             Diag(diag::err_module_different_modmap)
4202               << F.ModuleName << /*new*/0 << ModMap.getName();
4203           return OutOfDate;
4204         }
4205       }
4206     }
4207 
4208     // Check any additional module map files that are in the pcm, but not
4209     // found in header search. Cases that match are already removed.
4210     for (FileEntryRef ModMap : AdditionalStoredMaps) {
4211       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4212         Diag(diag::err_module_different_modmap)
4213           << F.ModuleName << /*not new*/1 << ModMap.getName();
4214       return OutOfDate;
4215     }
4216   }
4217 
4218   if (Listener)
4219     Listener->ReadModuleMapFile(F.ModuleMapPath);
4220   return Success;
4221 }
4222 
4223 /// Move the given method to the back of the global list of methods.
4224 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4225   // Find the entry for this selector in the method pool.
4226   Sema::GlobalMethodPool::iterator Known
4227     = S.MethodPool.find(Method->getSelector());
4228   if (Known == S.MethodPool.end())
4229     return;
4230 
4231   // Retrieve the appropriate method list.
4232   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4233                                                     : Known->second.second;
4234   bool Found = false;
4235   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4236     if (!Found) {
4237       if (List->getMethod() == Method) {
4238         Found = true;
4239       } else {
4240         // Keep searching.
4241         continue;
4242       }
4243     }
4244 
4245     if (List->getNext())
4246       List->setMethod(List->getNext()->getMethod());
4247     else
4248       List->setMethod(Method);
4249   }
4250 }
4251 
4252 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4253   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4254   for (Decl *D : Names) {
4255     bool wasHidden = !D->isUnconditionallyVisible();
4256     D->setVisibleDespiteOwningModule();
4257 
4258     if (wasHidden && SemaObj) {
4259       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4260         moveMethodToBackOfGlobalList(*SemaObj, Method);
4261       }
4262     }
4263   }
4264 }
4265 
4266 void ASTReader::makeModuleVisible(Module *Mod,
4267                                   Module::NameVisibilityKind NameVisibility,
4268                                   SourceLocation ImportLoc) {
4269   llvm::SmallPtrSet<Module *, 4> Visited;
4270   SmallVector<Module *, 4> Stack;
4271   Stack.push_back(Mod);
4272   while (!Stack.empty()) {
4273     Mod = Stack.pop_back_val();
4274 
4275     if (NameVisibility <= Mod->NameVisibility) {
4276       // This module already has this level of visibility (or greater), so
4277       // there is nothing more to do.
4278       continue;
4279     }
4280 
4281     if (Mod->isUnimportable()) {
4282       // Modules that aren't importable cannot be made visible.
4283       continue;
4284     }
4285 
4286     // Update the module's name visibility.
4287     Mod->NameVisibility = NameVisibility;
4288 
4289     // If we've already deserialized any names from this module,
4290     // mark them as visible.
4291     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4292     if (Hidden != HiddenNamesMap.end()) {
4293       auto HiddenNames = std::move(*Hidden);
4294       HiddenNamesMap.erase(Hidden);
4295       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4296       assert(!HiddenNamesMap.contains(Mod) &&
4297              "making names visible added hidden names");
4298     }
4299 
4300     // Push any exported modules onto the stack to be marked as visible.
4301     SmallVector<Module *, 16> Exports;
4302     Mod->getExportedModules(Exports);
4303     for (SmallVectorImpl<Module *>::iterator
4304            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4305       Module *Exported = *I;
4306       if (Visited.insert(Exported).second)
4307         Stack.push_back(Exported);
4308     }
4309   }
4310 }
4311 
4312 /// We've merged the definition \p MergedDef into the existing definition
4313 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4314 /// visible.
4315 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4316                                           NamedDecl *MergedDef) {
4317   if (!Def->isUnconditionallyVisible()) {
4318     // If MergedDef is visible or becomes visible, make the definition visible.
4319     if (MergedDef->isUnconditionallyVisible())
4320       Def->setVisibleDespiteOwningModule();
4321     else {
4322       getContext().mergeDefinitionIntoModule(
4323           Def, MergedDef->getImportedOwningModule(),
4324           /*NotifyListeners*/ false);
4325       PendingMergedDefinitionsToDeduplicate.insert(Def);
4326     }
4327   }
4328 }
4329 
4330 bool ASTReader::loadGlobalIndex() {
4331   if (GlobalIndex)
4332     return false;
4333 
4334   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4335       !PP.getLangOpts().Modules)
4336     return true;
4337 
4338   // Try to load the global index.
4339   TriedLoadingGlobalIndex = true;
4340   StringRef ModuleCachePath
4341     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4342   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4343       GlobalModuleIndex::readIndex(ModuleCachePath);
4344   if (llvm::Error Err = std::move(Result.second)) {
4345     assert(!Result.first);
4346     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4347     return true;
4348   }
4349 
4350   GlobalIndex.reset(Result.first);
4351   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4352   return false;
4353 }
4354 
4355 bool ASTReader::isGlobalIndexUnavailable() const {
4356   return PP.getLangOpts().Modules && UseGlobalIndex &&
4357          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4358 }
4359 
4360 static void updateModuleTimestamp(ModuleFile &MF) {
4361   // Overwrite the timestamp file contents so that file's mtime changes.
4362   std::string TimestampFilename = MF.getTimestampFilename();
4363   std::error_code EC;
4364   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4365                           llvm::sys::fs::OF_TextWithCRLF);
4366   if (EC)
4367     return;
4368   OS << "Timestamp file\n";
4369   OS.close();
4370   OS.clear_error(); // Avoid triggering a fatal error.
4371 }
4372 
4373 /// Given a cursor at the start of an AST file, scan ahead and drop the
4374 /// cursor into the start of the given block ID, returning false on success and
4375 /// true on failure.
4376 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4377   while (true) {
4378     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4379     if (!MaybeEntry) {
4380       // FIXME this drops errors on the floor.
4381       consumeError(MaybeEntry.takeError());
4382       return true;
4383     }
4384     llvm::BitstreamEntry Entry = MaybeEntry.get();
4385 
4386     switch (Entry.Kind) {
4387     case llvm::BitstreamEntry::Error:
4388     case llvm::BitstreamEntry::EndBlock:
4389       return true;
4390 
4391     case llvm::BitstreamEntry::Record:
4392       // Ignore top-level records.
4393       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4394         break;
4395       else {
4396         // FIXME this drops errors on the floor.
4397         consumeError(Skipped.takeError());
4398         return true;
4399       }
4400 
4401     case llvm::BitstreamEntry::SubBlock:
4402       if (Entry.ID == BlockID) {
4403         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4404           // FIXME this drops the error on the floor.
4405           consumeError(std::move(Err));
4406           return true;
4407         }
4408         // Found it!
4409         return false;
4410       }
4411 
4412       if (llvm::Error Err = Cursor.SkipBlock()) {
4413         // FIXME this drops the error on the floor.
4414         consumeError(std::move(Err));
4415         return true;
4416       }
4417     }
4418   }
4419 }
4420 
4421 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4422                                             SourceLocation ImportLoc,
4423                                             unsigned ClientLoadCapabilities,
4424                                             ModuleFile **NewLoadedModuleFile) {
4425   llvm::TimeTraceScope scope("ReadAST", FileName);
4426 
4427   llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4428   llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4429       CurrentDeserializingModuleKind, Type);
4430 
4431   // Defer any pending actions until we get to the end of reading the AST file.
4432   Deserializing AnASTFile(this);
4433 
4434   // Bump the generation number.
4435   unsigned PreviousGeneration = 0;
4436   if (ContextObj)
4437     PreviousGeneration = incrementGeneration(*ContextObj);
4438 
4439   unsigned NumModules = ModuleMgr.size();
4440   SmallVector<ImportedModule, 4> Loaded;
4441   if (ASTReadResult ReadResult =
4442           ReadASTCore(FileName, Type, ImportLoc,
4443                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4444                       ClientLoadCapabilities)) {
4445     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4446 
4447     // If we find that any modules are unusable, the global index is going
4448     // to be out-of-date. Just remove it.
4449     GlobalIndex.reset();
4450     ModuleMgr.setGlobalIndex(nullptr);
4451     return ReadResult;
4452   }
4453 
4454   if (NewLoadedModuleFile && !Loaded.empty())
4455     *NewLoadedModuleFile = Loaded.back().Mod;
4456 
4457   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4458   // remove modules from this point. Various fields are updated during reading
4459   // the AST block and removing the modules would result in dangling pointers.
4460   // They are generally only incidentally dereferenced, ie. a binary search
4461   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4462   // be dereferenced but it wouldn't actually be used.
4463 
4464   // Load the AST blocks of all of the modules that we loaded. We can still
4465   // hit errors parsing the ASTs at this point.
4466   for (ImportedModule &M : Loaded) {
4467     ModuleFile &F = *M.Mod;
4468     llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4469 
4470     // Read the AST block.
4471     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4472       Error(std::move(Err));
4473       return Failure;
4474     }
4475 
4476     // The AST block should always have a definition for the main module.
4477     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4478       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4479       return Failure;
4480     }
4481 
4482     // Read the extension blocks.
4483     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4484       if (llvm::Error Err = ReadExtensionBlock(F)) {
4485         Error(std::move(Err));
4486         return Failure;
4487       }
4488     }
4489 
4490     // Once read, set the ModuleFile bit base offset and update the size in
4491     // bits of all files we've seen.
4492     F.GlobalBitOffset = TotalModulesSizeInBits;
4493     TotalModulesSizeInBits += F.SizeInBits;
4494     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4495   }
4496 
4497   // Preload source locations and interesting indentifiers.
4498   for (ImportedModule &M : Loaded) {
4499     ModuleFile &F = *M.Mod;
4500 
4501     // Map the original source file ID into the ID space of the current
4502     // compilation.
4503     if (F.OriginalSourceFileID.isValid())
4504       F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4505 
4506     for (auto Offset : F.PreloadIdentifierOffsets) {
4507       const unsigned char *Data = F.IdentifierTableData + Offset;
4508 
4509       ASTIdentifierLookupTrait Trait(*this, F);
4510       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4511       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4512 
4513       IdentifierInfo *II;
4514       if (!PP.getLangOpts().CPlusPlus) {
4515         // Identifiers present in both the module file and the importing
4516         // instance are marked out-of-date so that they can be deserialized
4517         // on next use via ASTReader::updateOutOfDateIdentifier().
4518         // Identifiers present in the module file but not in the importing
4519         // instance are ignored for now, preventing growth of the identifier
4520         // table. They will be deserialized on first use via ASTReader::get().
4521         auto It = PP.getIdentifierTable().find(Key);
4522         if (It == PP.getIdentifierTable().end())
4523           continue;
4524         II = It->second;
4525       } else {
4526         // With C++ modules, not many identifiers are considered interesting.
4527         // All identifiers in the module file can be placed into the identifier
4528         // table of the importing instance and marked as out-of-date. This makes
4529         // ASTReader::get() a no-op, and deserialization will take place on
4530         // first/next use via ASTReader::updateOutOfDateIdentifier().
4531         II = &PP.getIdentifierTable().getOwn(Key);
4532       }
4533 
4534       II->setOutOfDate(true);
4535 
4536       // Mark this identifier as being from an AST file so that we can track
4537       // whether we need to serialize it.
4538       markIdentifierFromAST(*this, *II);
4539 
4540       // Associate the ID with the identifier so that the writer can reuse it.
4541       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4542       SetIdentifierInfo(ID, II);
4543     }
4544   }
4545 
4546   // Builtins and library builtins have already been initialized. Mark all
4547   // identifiers as out-of-date, so that they are deserialized on first use.
4548   if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4549     for (auto &Id : PP.getIdentifierTable())
4550       Id.second->setOutOfDate(true);
4551 
4552   // Mark selectors as out of date.
4553   for (const auto &Sel : SelectorGeneration)
4554     SelectorOutOfDate[Sel.first] = true;
4555 
4556   // Setup the import locations and notify the module manager that we've
4557   // committed to these module files.
4558   for (ImportedModule &M : Loaded) {
4559     ModuleFile &F = *M.Mod;
4560 
4561     ModuleMgr.moduleFileAccepted(&F);
4562 
4563     // Set the import location.
4564     F.DirectImportLoc = ImportLoc;
4565     // FIXME: We assume that locations from PCH / preamble do not need
4566     // any translation.
4567     if (!M.ImportedBy)
4568       F.ImportLoc = M.ImportLoc;
4569     else
4570       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4571   }
4572 
4573   // Resolve any unresolved module exports.
4574   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4575     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4576     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4577     Module *ResolvedMod = getSubmodule(GlobalID);
4578 
4579     switch (Unresolved.Kind) {
4580     case UnresolvedModuleRef::Conflict:
4581       if (ResolvedMod) {
4582         Module::Conflict Conflict;
4583         Conflict.Other = ResolvedMod;
4584         Conflict.Message = Unresolved.String.str();
4585         Unresolved.Mod->Conflicts.push_back(Conflict);
4586       }
4587       continue;
4588 
4589     case UnresolvedModuleRef::Import:
4590       if (ResolvedMod)
4591         Unresolved.Mod->Imports.insert(ResolvedMod);
4592       continue;
4593 
4594     case UnresolvedModuleRef::Affecting:
4595       if (ResolvedMod)
4596         Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4597       continue;
4598 
4599     case UnresolvedModuleRef::Export:
4600       if (ResolvedMod || Unresolved.IsWildcard)
4601         Unresolved.Mod->Exports.push_back(
4602           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4603       continue;
4604     }
4605   }
4606   UnresolvedModuleRefs.clear();
4607 
4608   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4609   // Might be unnecessary as use declarations are only used to build the
4610   // module itself.
4611 
4612   if (ContextObj)
4613     InitializeContext();
4614 
4615   if (SemaObj)
4616     UpdateSema();
4617 
4618   if (DeserializationListener)
4619     DeserializationListener->ReaderInitialized(this);
4620 
4621   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4622   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4623     // If this AST file is a precompiled preamble, then set the
4624     // preamble file ID of the source manager to the file source file
4625     // from which the preamble was built.
4626     if (Type == MK_Preamble) {
4627       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4628     } else if (Type == MK_MainFile) {
4629       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4630     }
4631   }
4632 
4633   // For any Objective-C class definitions we have already loaded, make sure
4634   // that we load any additional categories.
4635   if (ContextObj) {
4636     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4637       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4638                          ObjCClassesLoaded[I],
4639                          PreviousGeneration);
4640     }
4641   }
4642 
4643   HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4644   if (HSOpts.ModulesValidateOncePerBuildSession) {
4645     // Now we are certain that the module and all modules it depends on are
4646     // up-to-date. For implicitly-built module files, ensure the corresponding
4647     // timestamp files are up-to-date in this build session.
4648     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4649       ImportedModule &M = Loaded[I];
4650       if (M.Mod->Kind == MK_ImplicitModule &&
4651           M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4652         updateModuleTimestamp(*M.Mod);
4653     }
4654   }
4655 
4656   return Success;
4657 }
4658 
4659 static ASTFileSignature readASTFileSignature(StringRef PCH);
4660 
4661 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4662 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4663   // FIXME checking magic headers is done in other places such as
4664   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4665   // always done the same. Unify it all with a helper.
4666   if (!Stream.canSkipToPos(4))
4667     return llvm::createStringError(std::errc::illegal_byte_sequence,
4668                                    "file too small to contain AST file magic");
4669   for (unsigned C : {'C', 'P', 'C', 'H'})
4670     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4671       if (Res.get() != C)
4672         return llvm::createStringError(
4673             std::errc::illegal_byte_sequence,
4674             "file doesn't start with AST file magic");
4675     } else
4676       return Res.takeError();
4677   return llvm::Error::success();
4678 }
4679 
4680 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4681   switch (Kind) {
4682   case MK_PCH:
4683     return 0; // PCH
4684   case MK_ImplicitModule:
4685   case MK_ExplicitModule:
4686   case MK_PrebuiltModule:
4687     return 1; // module
4688   case MK_MainFile:
4689   case MK_Preamble:
4690     return 2; // main source file
4691   }
4692   llvm_unreachable("unknown module kind");
4693 }
4694 
4695 ASTReader::ASTReadResult
4696 ASTReader::ReadASTCore(StringRef FileName,
4697                        ModuleKind Type,
4698                        SourceLocation ImportLoc,
4699                        ModuleFile *ImportedBy,
4700                        SmallVectorImpl<ImportedModule> &Loaded,
4701                        off_t ExpectedSize, time_t ExpectedModTime,
4702                        ASTFileSignature ExpectedSignature,
4703                        unsigned ClientLoadCapabilities) {
4704   ModuleFile *M;
4705   std::string ErrorStr;
4706   ModuleManager::AddModuleResult AddResult
4707     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4708                           getGeneration(), ExpectedSize, ExpectedModTime,
4709                           ExpectedSignature, readASTFileSignature,
4710                           M, ErrorStr);
4711 
4712   switch (AddResult) {
4713   case ModuleManager::AlreadyLoaded:
4714     Diag(diag::remark_module_import)
4715         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4716         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4717     return Success;
4718 
4719   case ModuleManager::NewlyLoaded:
4720     // Load module file below.
4721     break;
4722 
4723   case ModuleManager::Missing:
4724     // The module file was missing; if the client can handle that, return
4725     // it.
4726     if (ClientLoadCapabilities & ARR_Missing)
4727       return Missing;
4728 
4729     // Otherwise, return an error.
4730     Diag(diag::err_ast_file_not_found)
4731         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4732         << ErrorStr;
4733     return Failure;
4734 
4735   case ModuleManager::OutOfDate:
4736     // We couldn't load the module file because it is out-of-date. If the
4737     // client can handle out-of-date, return it.
4738     if (ClientLoadCapabilities & ARR_OutOfDate)
4739       return OutOfDate;
4740 
4741     // Otherwise, return an error.
4742     Diag(diag::err_ast_file_out_of_date)
4743         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4744         << ErrorStr;
4745     return Failure;
4746   }
4747 
4748   assert(M && "Missing module file");
4749 
4750   bool ShouldFinalizePCM = false;
4751   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4752     auto &MC = getModuleManager().getModuleCache();
4753     if (ShouldFinalizePCM)
4754       MC.finalizePCM(FileName);
4755     else
4756       MC.tryToDropPCM(FileName);
4757   });
4758   ModuleFile &F = *M;
4759   BitstreamCursor &Stream = F.Stream;
4760   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4761   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4762 
4763   // Sniff for the signature.
4764   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4765     Diag(diag::err_ast_file_invalid)
4766         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4767     return Failure;
4768   }
4769 
4770   // This is used for compatibility with older PCH formats.
4771   bool HaveReadControlBlock = false;
4772   while (true) {
4773     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4774     if (!MaybeEntry) {
4775       Error(MaybeEntry.takeError());
4776       return Failure;
4777     }
4778     llvm::BitstreamEntry Entry = MaybeEntry.get();
4779 
4780     switch (Entry.Kind) {
4781     case llvm::BitstreamEntry::Error:
4782     case llvm::BitstreamEntry::Record:
4783     case llvm::BitstreamEntry::EndBlock:
4784       Error("invalid record at top-level of AST file");
4785       return Failure;
4786 
4787     case llvm::BitstreamEntry::SubBlock:
4788       break;
4789     }
4790 
4791     switch (Entry.ID) {
4792     case CONTROL_BLOCK_ID:
4793       HaveReadControlBlock = true;
4794       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4795       case Success:
4796         // Check that we didn't try to load a non-module AST file as a module.
4797         //
4798         // FIXME: Should we also perform the converse check? Loading a module as
4799         // a PCH file sort of works, but it's a bit wonky.
4800         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4801              Type == MK_PrebuiltModule) &&
4802             F.ModuleName.empty()) {
4803           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4804           if (Result != OutOfDate ||
4805               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4806             Diag(diag::err_module_file_not_module) << FileName;
4807           return Result;
4808         }
4809         break;
4810 
4811       case Failure: return Failure;
4812       case Missing: return Missing;
4813       case OutOfDate: return OutOfDate;
4814       case VersionMismatch: return VersionMismatch;
4815       case ConfigurationMismatch: return ConfigurationMismatch;
4816       case HadErrors: return HadErrors;
4817       }
4818       break;
4819 
4820     case AST_BLOCK_ID:
4821       if (!HaveReadControlBlock) {
4822         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4823           Diag(diag::err_pch_version_too_old);
4824         return VersionMismatch;
4825       }
4826 
4827       // Record that we've loaded this module.
4828       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4829       ShouldFinalizePCM = true;
4830       return Success;
4831 
4832     default:
4833       if (llvm::Error Err = Stream.SkipBlock()) {
4834         Error(std::move(Err));
4835         return Failure;
4836       }
4837       break;
4838     }
4839   }
4840 
4841   llvm_unreachable("unexpected break; expected return");
4842 }
4843 
4844 ASTReader::ASTReadResult
4845 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4846                                     unsigned ClientLoadCapabilities) {
4847   const HeaderSearchOptions &HSOpts =
4848       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4849   bool AllowCompatibleConfigurationMismatch =
4850       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4851   bool DisableValidation = shouldDisableValidationForFile(F);
4852 
4853   ASTReadResult Result = readUnhashedControlBlockImpl(
4854       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4855       Listener.get(),
4856       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4857 
4858   // If F was directly imported by another module, it's implicitly validated by
4859   // the importing module.
4860   if (DisableValidation || WasImportedBy ||
4861       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4862     return Success;
4863 
4864   if (Result == Failure) {
4865     Error("malformed block record in AST file");
4866     return Failure;
4867   }
4868 
4869   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4870     // If this module has already been finalized in the ModuleCache, we're stuck
4871     // with it; we can only load a single version of each module.
4872     //
4873     // This can happen when a module is imported in two contexts: in one, as a
4874     // user module; in another, as a system module (due to an import from
4875     // another module marked with the [system] flag).  It usually indicates a
4876     // bug in the module map: this module should also be marked with [system].
4877     //
4878     // If -Wno-system-headers (the default), and the first import is as a
4879     // system module, then validation will fail during the as-user import,
4880     // since -Werror flags won't have been validated.  However, it's reasonable
4881     // to treat this consistently as a system module.
4882     //
4883     // If -Wsystem-headers, the PCM on disk was built with
4884     // -Wno-system-headers, and the first import is as a user module, then
4885     // validation will fail during the as-system import since the PCM on disk
4886     // doesn't guarantee that -Werror was respected.  However, the -Werror
4887     // flags were checked during the initial as-user import.
4888     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4889       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4890       return Success;
4891     }
4892   }
4893 
4894   return Result;
4895 }
4896 
4897 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4898     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4899     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4900     bool ValidateDiagnosticOptions) {
4901   // Initialize a stream.
4902   BitstreamCursor Stream(StreamData);
4903 
4904   // Sniff for the signature.
4905   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4906     // FIXME this drops the error on the floor.
4907     consumeError(std::move(Err));
4908     return Failure;
4909   }
4910 
4911   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4912   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4913     return Failure;
4914 
4915   // Read all of the records in the options block.
4916   RecordData Record;
4917   ASTReadResult Result = Success;
4918   while (true) {
4919     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4920     if (!MaybeEntry) {
4921       // FIXME this drops the error on the floor.
4922       consumeError(MaybeEntry.takeError());
4923       return Failure;
4924     }
4925     llvm::BitstreamEntry Entry = MaybeEntry.get();
4926 
4927     switch (Entry.Kind) {
4928     case llvm::BitstreamEntry::Error:
4929     case llvm::BitstreamEntry::SubBlock:
4930       return Failure;
4931 
4932     case llvm::BitstreamEntry::EndBlock:
4933       return Result;
4934 
4935     case llvm::BitstreamEntry::Record:
4936       // The interesting case.
4937       break;
4938     }
4939 
4940     // Read and process a record.
4941     Record.clear();
4942     StringRef Blob;
4943     Expected<unsigned> MaybeRecordType =
4944         Stream.readRecord(Entry.ID, Record, &Blob);
4945     if (!MaybeRecordType) {
4946       // FIXME this drops the error.
4947       return Failure;
4948     }
4949     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4950     case SIGNATURE:
4951       if (F) {
4952         F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
4953         assert(F->Signature != ASTFileSignature::createDummy() &&
4954                "Dummy AST file signature not backpatched in ASTWriter.");
4955       }
4956       break;
4957     case AST_BLOCK_HASH:
4958       if (F) {
4959         F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());
4960         assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
4961                "Dummy AST block hash not backpatched in ASTWriter.");
4962       }
4963       break;
4964     case DIAGNOSTIC_OPTIONS: {
4965       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4966       if (Listener && ValidateDiagnosticOptions &&
4967           !AllowCompatibleConfigurationMismatch &&
4968           ParseDiagnosticOptions(Record, Complain, *Listener))
4969         Result = OutOfDate; // Don't return early.  Read the signature.
4970       break;
4971     }
4972     case HEADER_SEARCH_PATHS: {
4973       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
4974       if (!AllowCompatibleConfigurationMismatch &&
4975           ParseHeaderSearchPaths(Record, Complain, *Listener))
4976         Result = ConfigurationMismatch;
4977       break;
4978     }
4979     case DIAG_PRAGMA_MAPPINGS:
4980       if (!F)
4981         break;
4982       if (F->PragmaDiagMappings.empty())
4983         F->PragmaDiagMappings.swap(Record);
4984       else
4985         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4986                                      Record.begin(), Record.end());
4987       break;
4988     case HEADER_SEARCH_ENTRY_USAGE:
4989       if (!F)
4990         break;
4991       unsigned Count = Record[0];
4992       const char *Byte = Blob.data();
4993       F->SearchPathUsage = llvm::BitVector(Count, false);
4994       for (unsigned I = 0; I < Count; ++Byte)
4995         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4996           if (*Byte & (1 << Bit))
4997             F->SearchPathUsage[I] = true;
4998       break;
4999     }
5000   }
5001 }
5002 
5003 /// Parse a record and blob containing module file extension metadata.
5004 static bool parseModuleFileExtensionMetadata(
5005               const SmallVectorImpl<uint64_t> &Record,
5006               StringRef Blob,
5007               ModuleFileExtensionMetadata &Metadata) {
5008   if (Record.size() < 4) return true;
5009 
5010   Metadata.MajorVersion = Record[0];
5011   Metadata.MinorVersion = Record[1];
5012 
5013   unsigned BlockNameLen = Record[2];
5014   unsigned UserInfoLen = Record[3];
5015 
5016   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5017 
5018   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5019   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5020                                   Blob.data() + BlockNameLen + UserInfoLen);
5021   return false;
5022 }
5023 
5024 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5025   BitstreamCursor &Stream = F.Stream;
5026 
5027   RecordData Record;
5028   while (true) {
5029     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5030     if (!MaybeEntry)
5031       return MaybeEntry.takeError();
5032     llvm::BitstreamEntry Entry = MaybeEntry.get();
5033 
5034     switch (Entry.Kind) {
5035     case llvm::BitstreamEntry::SubBlock:
5036       if (llvm::Error Err = Stream.SkipBlock())
5037         return Err;
5038       continue;
5039     case llvm::BitstreamEntry::EndBlock:
5040       return llvm::Error::success();
5041     case llvm::BitstreamEntry::Error:
5042       return llvm::createStringError(std::errc::illegal_byte_sequence,
5043                                      "malformed block record in AST file");
5044     case llvm::BitstreamEntry::Record:
5045       break;
5046     }
5047 
5048     Record.clear();
5049     StringRef Blob;
5050     Expected<unsigned> MaybeRecCode =
5051         Stream.readRecord(Entry.ID, Record, &Blob);
5052     if (!MaybeRecCode)
5053       return MaybeRecCode.takeError();
5054     switch (MaybeRecCode.get()) {
5055     case EXTENSION_METADATA: {
5056       ModuleFileExtensionMetadata Metadata;
5057       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5058         return llvm::createStringError(
5059             std::errc::illegal_byte_sequence,
5060             "malformed EXTENSION_METADATA in AST file");
5061 
5062       // Find a module file extension with this block name.
5063       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
5064       if (Known == ModuleFileExtensions.end()) break;
5065 
5066       // Form a reader.
5067       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
5068                                                              F, Stream)) {
5069         F.ExtensionReaders.push_back(std::move(Reader));
5070       }
5071 
5072       break;
5073     }
5074     }
5075   }
5076 
5077   return llvm::Error::success();
5078 }
5079 
5080 void ASTReader::InitializeContext() {
5081   assert(ContextObj && "no context to initialize");
5082   ASTContext &Context = *ContextObj;
5083 
5084   // If there's a listener, notify them that we "read" the translation unit.
5085   if (DeserializationListener)
5086     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
5087                                       Context.getTranslationUnitDecl());
5088 
5089   // FIXME: Find a better way to deal with collisions between these
5090   // built-in types. Right now, we just ignore the problem.
5091 
5092   // Load the special types.
5093   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5094     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5095       if (!Context.CFConstantStringTypeDecl)
5096         Context.setCFConstantStringType(GetType(String));
5097     }
5098 
5099     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5100       QualType FileType = GetType(File);
5101       if (FileType.isNull()) {
5102         Error("FILE type is NULL");
5103         return;
5104       }
5105 
5106       if (!Context.FILEDecl) {
5107         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5108           Context.setFILEDecl(Typedef->getDecl());
5109         else {
5110           const TagType *Tag = FileType->getAs<TagType>();
5111           if (!Tag) {
5112             Error("Invalid FILE type in AST file");
5113             return;
5114           }
5115           Context.setFILEDecl(Tag->getDecl());
5116         }
5117       }
5118     }
5119 
5120     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5121       QualType Jmp_bufType = GetType(Jmp_buf);
5122       if (Jmp_bufType.isNull()) {
5123         Error("jmp_buf type is NULL");
5124         return;
5125       }
5126 
5127       if (!Context.jmp_bufDecl) {
5128         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5129           Context.setjmp_bufDecl(Typedef->getDecl());
5130         else {
5131           const TagType *Tag = Jmp_bufType->getAs<TagType>();
5132           if (!Tag) {
5133             Error("Invalid jmp_buf type in AST file");
5134             return;
5135           }
5136           Context.setjmp_bufDecl(Tag->getDecl());
5137         }
5138       }
5139     }
5140 
5141     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5142       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
5143       if (Sigjmp_bufType.isNull()) {
5144         Error("sigjmp_buf type is NULL");
5145         return;
5146       }
5147 
5148       if (!Context.sigjmp_bufDecl) {
5149         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5150           Context.setsigjmp_bufDecl(Typedef->getDecl());
5151         else {
5152           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5153           assert(Tag && "Invalid sigjmp_buf type in AST file");
5154           Context.setsigjmp_bufDecl(Tag->getDecl());
5155         }
5156       }
5157     }
5158 
5159     if (unsigned ObjCIdRedef
5160           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5161       if (Context.ObjCIdRedefinitionType.isNull())
5162         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
5163     }
5164 
5165     if (unsigned ObjCClassRedef
5166           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5167       if (Context.ObjCClassRedefinitionType.isNull())
5168         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
5169     }
5170 
5171     if (unsigned ObjCSelRedef
5172           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5173       if (Context.ObjCSelRedefinitionType.isNull())
5174         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
5175     }
5176 
5177     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5178       QualType Ucontext_tType = GetType(Ucontext_t);
5179       if (Ucontext_tType.isNull()) {
5180         Error("ucontext_t type is NULL");
5181         return;
5182       }
5183 
5184       if (!Context.ucontext_tDecl) {
5185         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5186           Context.setucontext_tDecl(Typedef->getDecl());
5187         else {
5188           const TagType *Tag = Ucontext_tType->getAs<TagType>();
5189           assert(Tag && "Invalid ucontext_t type in AST file");
5190           Context.setucontext_tDecl(Tag->getDecl());
5191         }
5192       }
5193     }
5194   }
5195 
5196   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5197 
5198   // If there were any CUDA special declarations, deserialize them.
5199   if (!CUDASpecialDeclRefs.empty()) {
5200     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5201     Context.setcudaConfigureCallDecl(
5202                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5203   }
5204 
5205   // Re-export any modules that were imported by a non-module AST file.
5206   // FIXME: This does not make macro-only imports visible again.
5207   for (auto &Import : PendingImportedModules) {
5208     if (Module *Imported = getSubmodule(Import.ID)) {
5209       makeModuleVisible(Imported, Module::AllVisible,
5210                         /*ImportLoc=*/Import.ImportLoc);
5211       if (Import.ImportLoc.isValid())
5212         PP.makeModuleVisible(Imported, Import.ImportLoc);
5213       // This updates visibility for Preprocessor only. For Sema, which can be
5214       // nullptr here, we do the same later, in UpdateSema().
5215     }
5216   }
5217 
5218   // Hand off these modules to Sema.
5219   PendingImportedModulesSema.append(PendingImportedModules);
5220   PendingImportedModules.clear();
5221 }
5222 
5223 void ASTReader::finalizeForWriting() {
5224   // Nothing to do for now.
5225 }
5226 
5227 /// Reads and return the signature record from \p PCH's control block, or
5228 /// else returns 0.
5229 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5230   BitstreamCursor Stream(PCH);
5231   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5232     // FIXME this drops the error on the floor.
5233     consumeError(std::move(Err));
5234     return ASTFileSignature();
5235   }
5236 
5237   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5238   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5239     return ASTFileSignature();
5240 
5241   // Scan for SIGNATURE inside the diagnostic options block.
5242   ASTReader::RecordData Record;
5243   while (true) {
5244     Expected<llvm::BitstreamEntry> MaybeEntry =
5245         Stream.advanceSkippingSubblocks();
5246     if (!MaybeEntry) {
5247       // FIXME this drops the error on the floor.
5248       consumeError(MaybeEntry.takeError());
5249       return ASTFileSignature();
5250     }
5251     llvm::BitstreamEntry Entry = MaybeEntry.get();
5252 
5253     if (Entry.Kind != llvm::BitstreamEntry::Record)
5254       return ASTFileSignature();
5255 
5256     Record.clear();
5257     StringRef Blob;
5258     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5259     if (!MaybeRecord) {
5260       // FIXME this drops the error on the floor.
5261       consumeError(MaybeRecord.takeError());
5262       return ASTFileSignature();
5263     }
5264     if (SIGNATURE == MaybeRecord.get()) {
5265       auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5266       assert(Signature != ASTFileSignature::createDummy() &&
5267              "Dummy AST file signature not backpatched in ASTWriter.");
5268       return Signature;
5269     }
5270   }
5271 }
5272 
5273 /// Retrieve the name of the original source file name
5274 /// directly from the AST file, without actually loading the AST
5275 /// file.
5276 std::string ASTReader::getOriginalSourceFile(
5277     const std::string &ASTFileName, FileManager &FileMgr,
5278     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5279   // Open the AST file.
5280   auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5281                                          /*RequiresNullTerminator=*/false);
5282   if (!Buffer) {
5283     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5284         << ASTFileName << Buffer.getError().message();
5285     return std::string();
5286   }
5287 
5288   // Initialize the stream
5289   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5290 
5291   // Sniff for the signature.
5292   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5293     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5294     return std::string();
5295   }
5296 
5297   // Scan for the CONTROL_BLOCK_ID block.
5298   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5299     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5300     return std::string();
5301   }
5302 
5303   // Scan for ORIGINAL_FILE inside the control block.
5304   RecordData Record;
5305   while (true) {
5306     Expected<llvm::BitstreamEntry> MaybeEntry =
5307         Stream.advanceSkippingSubblocks();
5308     if (!MaybeEntry) {
5309       // FIXME this drops errors on the floor.
5310       consumeError(MaybeEntry.takeError());
5311       return std::string();
5312     }
5313     llvm::BitstreamEntry Entry = MaybeEntry.get();
5314 
5315     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5316       return std::string();
5317 
5318     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5319       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5320       return std::string();
5321     }
5322 
5323     Record.clear();
5324     StringRef Blob;
5325     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5326     if (!MaybeRecord) {
5327       // FIXME this drops the errors on the floor.
5328       consumeError(MaybeRecord.takeError());
5329       return std::string();
5330     }
5331     if (ORIGINAL_FILE == MaybeRecord.get())
5332       return Blob.str();
5333   }
5334 }
5335 
5336 namespace {
5337 
5338   class SimplePCHValidator : public ASTReaderListener {
5339     const LangOptions &ExistingLangOpts;
5340     const TargetOptions &ExistingTargetOpts;
5341     const PreprocessorOptions &ExistingPPOpts;
5342     std::string ExistingModuleCachePath;
5343     FileManager &FileMgr;
5344     bool StrictOptionMatches;
5345 
5346   public:
5347     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5348                        const TargetOptions &ExistingTargetOpts,
5349                        const PreprocessorOptions &ExistingPPOpts,
5350                        StringRef ExistingModuleCachePath, FileManager &FileMgr,
5351                        bool StrictOptionMatches)
5352         : ExistingLangOpts(ExistingLangOpts),
5353           ExistingTargetOpts(ExistingTargetOpts),
5354           ExistingPPOpts(ExistingPPOpts),
5355           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5356           StrictOptionMatches(StrictOptionMatches) {}
5357 
5358     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5359                              bool AllowCompatibleDifferences) override {
5360       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5361                                   AllowCompatibleDifferences);
5362     }
5363 
5364     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5365                            bool AllowCompatibleDifferences) override {
5366       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5367                                 AllowCompatibleDifferences);
5368     }
5369 
5370     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5371                                  StringRef SpecificModuleCachePath,
5372                                  bool Complain) override {
5373       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5374                                       ExistingModuleCachePath, nullptr,
5375                                       ExistingLangOpts, ExistingPPOpts);
5376     }
5377 
5378     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5379                                  bool ReadMacros, bool Complain,
5380                                  std::string &SuggestedPredefines) override {
5381       return checkPreprocessorOptions(
5382           PPOpts, ExistingPPOpts, ReadMacros, /*Diags=*/nullptr, FileMgr,
5383           SuggestedPredefines, ExistingLangOpts,
5384           StrictOptionMatches ? OptionValidateStrictMatches
5385                               : OptionValidateContradictions);
5386     }
5387   };
5388 
5389 } // namespace
5390 
5391 bool ASTReader::readASTFileControlBlock(
5392     StringRef Filename, FileManager &FileMgr,
5393     const InMemoryModuleCache &ModuleCache,
5394     const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5395     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5396   // Open the AST file.
5397   std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5398   llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
5399   if (!Buffer) {
5400     // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5401     // read again later, but we do not have the context here to determine if it
5402     // is safe to change the result of InMemoryModuleCache::getPCMState().
5403 
5404     // FIXME: This allows use of the VFS; we do not allow use of the
5405     // VFS when actually loading a module.
5406     auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5407     if (!BufferOrErr)
5408       return true;
5409     OwnedBuffer = std::move(*BufferOrErr);
5410     Buffer = OwnedBuffer.get();
5411   }
5412 
5413   // Initialize the stream
5414   StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
5415   BitstreamCursor Stream(Bytes);
5416 
5417   // Sniff for the signature.
5418   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5419     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5420     return true;
5421   }
5422 
5423   // Scan for the CONTROL_BLOCK_ID block.
5424   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5425     return true;
5426 
5427   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5428   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5429   bool NeedsImports = Listener.needsImportVisitation();
5430   BitstreamCursor InputFilesCursor;
5431   uint64_t InputFilesOffsetBase = 0;
5432 
5433   RecordData Record;
5434   std::string ModuleDir;
5435   bool DoneWithControlBlock = false;
5436   while (!DoneWithControlBlock) {
5437     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5438     if (!MaybeEntry) {
5439       // FIXME this drops the error on the floor.
5440       consumeError(MaybeEntry.takeError());
5441       return true;
5442     }
5443     llvm::BitstreamEntry Entry = MaybeEntry.get();
5444 
5445     switch (Entry.Kind) {
5446     case llvm::BitstreamEntry::SubBlock: {
5447       switch (Entry.ID) {
5448       case OPTIONS_BLOCK_ID: {
5449         std::string IgnoredSuggestedPredefines;
5450         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5451                              /*AllowCompatibleConfigurationMismatch*/ false,
5452                              Listener, IgnoredSuggestedPredefines) != Success)
5453           return true;
5454         break;
5455       }
5456 
5457       case INPUT_FILES_BLOCK_ID:
5458         InputFilesCursor = Stream;
5459         if (llvm::Error Err = Stream.SkipBlock()) {
5460           // FIXME this drops the error on the floor.
5461           consumeError(std::move(Err));
5462           return true;
5463         }
5464         if (NeedsInputFiles &&
5465             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5466           return true;
5467         InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5468         break;
5469 
5470       default:
5471         if (llvm::Error Err = Stream.SkipBlock()) {
5472           // FIXME this drops the error on the floor.
5473           consumeError(std::move(Err));
5474           return true;
5475         }
5476         break;
5477       }
5478 
5479       continue;
5480     }
5481 
5482     case llvm::BitstreamEntry::EndBlock:
5483       DoneWithControlBlock = true;
5484       break;
5485 
5486     case llvm::BitstreamEntry::Error:
5487       return true;
5488 
5489     case llvm::BitstreamEntry::Record:
5490       break;
5491     }
5492 
5493     if (DoneWithControlBlock) break;
5494 
5495     Record.clear();
5496     StringRef Blob;
5497     Expected<unsigned> MaybeRecCode =
5498         Stream.readRecord(Entry.ID, Record, &Blob);
5499     if (!MaybeRecCode) {
5500       // FIXME this drops the error.
5501       return Failure;
5502     }
5503     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5504     case METADATA:
5505       if (Record[0] != VERSION_MAJOR)
5506         return true;
5507       if (Listener.ReadFullVersionInformation(Blob))
5508         return true;
5509       break;
5510     case MODULE_NAME:
5511       Listener.ReadModuleName(Blob);
5512       break;
5513     case MODULE_DIRECTORY:
5514       ModuleDir = std::string(Blob);
5515       break;
5516     case MODULE_MAP_FILE: {
5517       unsigned Idx = 0;
5518       auto Path = ReadString(Record, Idx);
5519       ResolveImportedPath(Path, ModuleDir);
5520       Listener.ReadModuleMapFile(Path);
5521       break;
5522     }
5523     case INPUT_FILE_OFFSETS: {
5524       if (!NeedsInputFiles)
5525         break;
5526 
5527       unsigned NumInputFiles = Record[0];
5528       unsigned NumUserFiles = Record[1];
5529       const llvm::support::unaligned_uint64_t *InputFileOffs =
5530           (const llvm::support::unaligned_uint64_t *)Blob.data();
5531       for (unsigned I = 0; I != NumInputFiles; ++I) {
5532         // Go find this input file.
5533         bool isSystemFile = I >= NumUserFiles;
5534 
5535         if (isSystemFile && !NeedsSystemInputFiles)
5536           break; // the rest are system input files
5537 
5538         BitstreamCursor &Cursor = InputFilesCursor;
5539         SavedStreamPosition SavedPosition(Cursor);
5540         if (llvm::Error Err =
5541                 Cursor.JumpToBit(InputFilesOffsetBase + InputFileOffs[I])) {
5542           // FIXME this drops errors on the floor.
5543           consumeError(std::move(Err));
5544         }
5545 
5546         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5547         if (!MaybeCode) {
5548           // FIXME this drops errors on the floor.
5549           consumeError(MaybeCode.takeError());
5550         }
5551         unsigned Code = MaybeCode.get();
5552 
5553         RecordData Record;
5554         StringRef Blob;
5555         bool shouldContinue = false;
5556         Expected<unsigned> MaybeRecordType =
5557             Cursor.readRecord(Code, Record, &Blob);
5558         if (!MaybeRecordType) {
5559           // FIXME this drops errors on the floor.
5560           consumeError(MaybeRecordType.takeError());
5561         }
5562         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5563         case INPUT_FILE_HASH:
5564           break;
5565         case INPUT_FILE:
5566           bool Overridden = static_cast<bool>(Record[3]);
5567           std::string Filename = std::string(Blob);
5568           ResolveImportedPath(Filename, ModuleDir);
5569           shouldContinue = Listener.visitInputFile(
5570               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5571           break;
5572         }
5573         if (!shouldContinue)
5574           break;
5575       }
5576       break;
5577     }
5578 
5579     case IMPORTS: {
5580       if (!NeedsImports)
5581         break;
5582 
5583       unsigned Idx = 0, N = Record.size();
5584       while (Idx < N) {
5585         // Read information about the AST file.
5586 
5587         // Kind, StandardCXXModule, ImportLoc, Size, ModTime, Signature
5588         Idx += 1 + 1 + 1 + 1 + 1 + ASTFileSignature::size;
5589         std::string ModuleName = ReadString(Record, Idx);
5590         std::string Filename = ReadString(Record, Idx);
5591         ResolveImportedPath(Filename, ModuleDir);
5592         Listener.visitImport(ModuleName, Filename);
5593       }
5594       break;
5595     }
5596 
5597     default:
5598       // No other validation to perform.
5599       break;
5600     }
5601   }
5602 
5603   // Look for module file extension blocks, if requested.
5604   if (FindModuleFileExtensions) {
5605     BitstreamCursor SavedStream = Stream;
5606     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5607       bool DoneWithExtensionBlock = false;
5608       while (!DoneWithExtensionBlock) {
5609         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5610         if (!MaybeEntry) {
5611           // FIXME this drops the error.
5612           return true;
5613         }
5614         llvm::BitstreamEntry Entry = MaybeEntry.get();
5615 
5616         switch (Entry.Kind) {
5617         case llvm::BitstreamEntry::SubBlock:
5618           if (llvm::Error Err = Stream.SkipBlock()) {
5619             // FIXME this drops the error on the floor.
5620             consumeError(std::move(Err));
5621             return true;
5622           }
5623           continue;
5624 
5625         case llvm::BitstreamEntry::EndBlock:
5626           DoneWithExtensionBlock = true;
5627           continue;
5628 
5629         case llvm::BitstreamEntry::Error:
5630           return true;
5631 
5632         case llvm::BitstreamEntry::Record:
5633           break;
5634         }
5635 
5636        Record.clear();
5637        StringRef Blob;
5638        Expected<unsigned> MaybeRecCode =
5639            Stream.readRecord(Entry.ID, Record, &Blob);
5640        if (!MaybeRecCode) {
5641          // FIXME this drops the error.
5642          return true;
5643        }
5644        switch (MaybeRecCode.get()) {
5645        case EXTENSION_METADATA: {
5646          ModuleFileExtensionMetadata Metadata;
5647          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5648            return true;
5649 
5650          Listener.readModuleFileExtension(Metadata);
5651          break;
5652        }
5653        }
5654       }
5655     }
5656     Stream = SavedStream;
5657   }
5658 
5659   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5660   if (readUnhashedControlBlockImpl(
5661           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5662           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5663           ValidateDiagnosticOptions) != Success)
5664     return true;
5665 
5666   return false;
5667 }
5668 
5669 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5670                                     const InMemoryModuleCache &ModuleCache,
5671                                     const PCHContainerReader &PCHContainerRdr,
5672                                     const LangOptions &LangOpts,
5673                                     const TargetOptions &TargetOpts,
5674                                     const PreprocessorOptions &PPOpts,
5675                                     StringRef ExistingModuleCachePath,
5676                                     bool RequireStrictOptionMatches) {
5677   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5678                                ExistingModuleCachePath, FileMgr,
5679                                RequireStrictOptionMatches);
5680   return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5681                                   PCHContainerRdr,
5682                                   /*FindModuleFileExtensions=*/false, validator,
5683                                   /*ValidateDiagnosticOptions=*/true);
5684 }
5685 
5686 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5687                                           unsigned ClientLoadCapabilities) {
5688   // Enter the submodule block.
5689   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5690     return Err;
5691 
5692   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5693   bool First = true;
5694   Module *CurrentModule = nullptr;
5695   RecordData Record;
5696   while (true) {
5697     Expected<llvm::BitstreamEntry> MaybeEntry =
5698         F.Stream.advanceSkippingSubblocks();
5699     if (!MaybeEntry)
5700       return MaybeEntry.takeError();
5701     llvm::BitstreamEntry Entry = MaybeEntry.get();
5702 
5703     switch (Entry.Kind) {
5704     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5705     case llvm::BitstreamEntry::Error:
5706       return llvm::createStringError(std::errc::illegal_byte_sequence,
5707                                      "malformed block record in AST file");
5708     case llvm::BitstreamEntry::EndBlock:
5709       return llvm::Error::success();
5710     case llvm::BitstreamEntry::Record:
5711       // The interesting case.
5712       break;
5713     }
5714 
5715     // Read a record.
5716     StringRef Blob;
5717     Record.clear();
5718     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5719     if (!MaybeKind)
5720       return MaybeKind.takeError();
5721     unsigned Kind = MaybeKind.get();
5722 
5723     if ((Kind == SUBMODULE_METADATA) != First)
5724       return llvm::createStringError(
5725           std::errc::illegal_byte_sequence,
5726           "submodule metadata record should be at beginning of block");
5727     First = false;
5728 
5729     // Submodule information is only valid if we have a current module.
5730     // FIXME: Should we error on these cases?
5731     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5732         Kind != SUBMODULE_DEFINITION)
5733       continue;
5734 
5735     switch (Kind) {
5736     default:  // Default behavior: ignore.
5737       break;
5738 
5739     case SUBMODULE_DEFINITION: {
5740       if (Record.size() < 13)
5741         return llvm::createStringError(std::errc::illegal_byte_sequence,
5742                                        "malformed module definition");
5743 
5744       StringRef Name = Blob;
5745       unsigned Idx = 0;
5746       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5747       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5748       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5749       SourceLocation DefinitionLoc = ReadSourceLocation(F, Record[Idx++]);
5750       bool IsFramework = Record[Idx++];
5751       bool IsExplicit = Record[Idx++];
5752       bool IsSystem = Record[Idx++];
5753       bool IsExternC = Record[Idx++];
5754       bool InferSubmodules = Record[Idx++];
5755       bool InferExplicitSubmodules = Record[Idx++];
5756       bool InferExportWildcard = Record[Idx++];
5757       bool ConfigMacrosExhaustive = Record[Idx++];
5758       bool ModuleMapIsPrivate = Record[Idx++];
5759       bool NamedModuleHasInit = Record[Idx++];
5760 
5761       Module *ParentModule = nullptr;
5762       if (Parent)
5763         ParentModule = getSubmodule(Parent);
5764 
5765       // Retrieve this (sub)module from the module map, creating it if
5766       // necessary.
5767       CurrentModule =
5768           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5769               .first;
5770 
5771       // FIXME: Call ModMap.setInferredModuleAllowedBy()
5772 
5773       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5774       if (GlobalIndex >= SubmodulesLoaded.size() ||
5775           SubmodulesLoaded[GlobalIndex])
5776         return llvm::createStringError(std::errc::invalid_argument,
5777                                        "too many submodules");
5778 
5779       if (!ParentModule) {
5780         if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
5781           // Don't emit module relocation error if we have -fno-validate-pch
5782           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5783                     DisableValidationForModuleKind::Module) &&
5784               CurFile != F.File) {
5785             auto ConflictError =
5786                 PartialDiagnostic(diag::err_module_file_conflict,
5787                                   ContextObj->DiagAllocator)
5788                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5789                 << F.File.getName();
5790             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5791           }
5792         }
5793 
5794         F.DidReadTopLevelSubmodule = true;
5795         CurrentModule->setASTFile(F.File);
5796         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5797       }
5798 
5799       CurrentModule->Kind = Kind;
5800       CurrentModule->DefinitionLoc = DefinitionLoc;
5801       CurrentModule->Signature = F.Signature;
5802       CurrentModule->IsFromModuleFile = true;
5803       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5804       CurrentModule->IsExternC = IsExternC;
5805       CurrentModule->InferSubmodules = InferSubmodules;
5806       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5807       CurrentModule->InferExportWildcard = InferExportWildcard;
5808       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5809       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5810       CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
5811       if (DeserializationListener)
5812         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5813 
5814       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5815 
5816       // Clear out data that will be replaced by what is in the module file.
5817       CurrentModule->LinkLibraries.clear();
5818       CurrentModule->ConfigMacros.clear();
5819       CurrentModule->UnresolvedConflicts.clear();
5820       CurrentModule->Conflicts.clear();
5821 
5822       // The module is available unless it's missing a requirement; relevant
5823       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5824       // Missing headers that were present when the module was built do not
5825       // make it unavailable -- if we got this far, this must be an explicitly
5826       // imported module file.
5827       CurrentModule->Requirements.clear();
5828       CurrentModule->MissingHeaders.clear();
5829       CurrentModule->IsUnimportable =
5830           ParentModule && ParentModule->IsUnimportable;
5831       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5832       break;
5833     }
5834 
5835     case SUBMODULE_UMBRELLA_HEADER: {
5836       // FIXME: This doesn't work for framework modules as `Filename` is the
5837       //        name as written in the module file and does not include
5838       //        `Headers/`, so this path will never exist.
5839       std::string Filename = std::string(Blob);
5840       ResolveImportedPath(F, Filename);
5841       if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) {
5842         if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
5843           // FIXME: NameAsWritten
5844           ModMap.setUmbrellaHeaderAsWritten(CurrentModule, *Umbrella, Blob, "");
5845         }
5846         // Note that it's too late at this point to return out of date if the
5847         // name from the PCM doesn't match up with the one in the module map,
5848         // but also quite unlikely since we will have already checked the
5849         // modification time and size of the module map file itself.
5850       }
5851       break;
5852     }
5853 
5854     case SUBMODULE_HEADER:
5855     case SUBMODULE_EXCLUDED_HEADER:
5856     case SUBMODULE_PRIVATE_HEADER:
5857       // We lazily associate headers with their modules via the HeaderInfo table.
5858       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5859       // of complete filenames or remove it entirely.
5860       break;
5861 
5862     case SUBMODULE_TEXTUAL_HEADER:
5863     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5864       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5865       // them here.
5866       break;
5867 
5868     case SUBMODULE_TOPHEADER: {
5869       std::string HeaderName(Blob);
5870       ResolveImportedPath(F, HeaderName);
5871       CurrentModule->addTopHeaderFilename(HeaderName);
5872       break;
5873     }
5874 
5875     case SUBMODULE_UMBRELLA_DIR: {
5876       // See comments in SUBMODULE_UMBRELLA_HEADER
5877       std::string Dirname = std::string(Blob);
5878       ResolveImportedPath(F, Dirname);
5879       if (auto Umbrella =
5880               PP.getFileManager().getOptionalDirectoryRef(Dirname)) {
5881         if (!CurrentModule->getUmbrellaDirAsWritten()) {
5882           // FIXME: NameAsWritten
5883           ModMap.setUmbrellaDirAsWritten(CurrentModule, *Umbrella, Blob, "");
5884         }
5885       }
5886       break;
5887     }
5888 
5889     case SUBMODULE_METADATA: {
5890       F.BaseSubmoduleID = getTotalNumSubmodules();
5891       F.LocalNumSubmodules = Record[0];
5892       unsigned LocalBaseSubmoduleID = Record[1];
5893       if (F.LocalNumSubmodules > 0) {
5894         // Introduce the global -> local mapping for submodules within this
5895         // module.
5896         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5897 
5898         // Introduce the local -> global mapping for submodules within this
5899         // module.
5900         F.SubmoduleRemap.insertOrReplace(
5901           std::make_pair(LocalBaseSubmoduleID,
5902                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5903 
5904         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5905       }
5906       break;
5907     }
5908 
5909     case SUBMODULE_IMPORTS:
5910       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5911         UnresolvedModuleRef Unresolved;
5912         Unresolved.File = &F;
5913         Unresolved.Mod = CurrentModule;
5914         Unresolved.ID = Record[Idx];
5915         Unresolved.Kind = UnresolvedModuleRef::Import;
5916         Unresolved.IsWildcard = false;
5917         UnresolvedModuleRefs.push_back(Unresolved);
5918       }
5919       break;
5920 
5921     case SUBMODULE_AFFECTING_MODULES:
5922       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5923         UnresolvedModuleRef Unresolved;
5924         Unresolved.File = &F;
5925         Unresolved.Mod = CurrentModule;
5926         Unresolved.ID = Record[Idx];
5927         Unresolved.Kind = UnresolvedModuleRef::Affecting;
5928         Unresolved.IsWildcard = false;
5929         UnresolvedModuleRefs.push_back(Unresolved);
5930       }
5931       break;
5932 
5933     case SUBMODULE_EXPORTS:
5934       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5935         UnresolvedModuleRef Unresolved;
5936         Unresolved.File = &F;
5937         Unresolved.Mod = CurrentModule;
5938         Unresolved.ID = Record[Idx];
5939         Unresolved.Kind = UnresolvedModuleRef::Export;
5940         Unresolved.IsWildcard = Record[Idx + 1];
5941         UnresolvedModuleRefs.push_back(Unresolved);
5942       }
5943 
5944       // Once we've loaded the set of exports, there's no reason to keep
5945       // the parsed, unresolved exports around.
5946       CurrentModule->UnresolvedExports.clear();
5947       break;
5948 
5949     case SUBMODULE_REQUIRES:
5950       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5951                                     PP.getTargetInfo());
5952       break;
5953 
5954     case SUBMODULE_LINK_LIBRARY:
5955       ModMap.resolveLinkAsDependencies(CurrentModule);
5956       CurrentModule->LinkLibraries.push_back(
5957           Module::LinkLibrary(std::string(Blob), Record[0]));
5958       break;
5959 
5960     case SUBMODULE_CONFIG_MACRO:
5961       CurrentModule->ConfigMacros.push_back(Blob.str());
5962       break;
5963 
5964     case SUBMODULE_CONFLICT: {
5965       UnresolvedModuleRef Unresolved;
5966       Unresolved.File = &F;
5967       Unresolved.Mod = CurrentModule;
5968       Unresolved.ID = Record[0];
5969       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5970       Unresolved.IsWildcard = false;
5971       Unresolved.String = Blob;
5972       UnresolvedModuleRefs.push_back(Unresolved);
5973       break;
5974     }
5975 
5976     case SUBMODULE_INITIALIZERS: {
5977       if (!ContextObj)
5978         break;
5979       SmallVector<uint32_t, 16> Inits;
5980       for (auto &ID : Record)
5981         Inits.push_back(getGlobalDeclID(F, ID));
5982       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5983       break;
5984     }
5985 
5986     case SUBMODULE_EXPORT_AS:
5987       CurrentModule->ExportAsModule = Blob.str();
5988       ModMap.addLinkAsDependency(CurrentModule);
5989       break;
5990     }
5991   }
5992 }
5993 
5994 /// Parse the record that corresponds to a LangOptions data
5995 /// structure.
5996 ///
5997 /// This routine parses the language options from the AST file and then gives
5998 /// them to the AST listener if one is set.
5999 ///
6000 /// \returns true if the listener deems the file unacceptable, false otherwise.
6001 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6002                                      bool Complain,
6003                                      ASTReaderListener &Listener,
6004                                      bool AllowCompatibleDifferences) {
6005   LangOptions LangOpts;
6006   unsigned Idx = 0;
6007 #define LANGOPT(Name, Bits, Default, Description) \
6008   LangOpts.Name = Record[Idx++];
6009 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
6010   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6011 #include "clang/Basic/LangOptions.def"
6012 #define SANITIZER(NAME, ID)                                                    \
6013   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6014 #include "clang/Basic/Sanitizers.def"
6015 
6016   for (unsigned N = Record[Idx++]; N; --N)
6017     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
6018 
6019   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6020   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6021   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6022 
6023   LangOpts.CurrentModule = ReadString(Record, Idx);
6024 
6025   // Comment options.
6026   for (unsigned N = Record[Idx++]; N; --N) {
6027     LangOpts.CommentOpts.BlockCommandNames.push_back(
6028       ReadString(Record, Idx));
6029   }
6030   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6031 
6032   // OpenMP offloading options.
6033   for (unsigned N = Record[Idx++]; N; --N) {
6034     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
6035   }
6036 
6037   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6038 
6039   return Listener.ReadLanguageOptions(LangOpts, Complain,
6040                                       AllowCompatibleDifferences);
6041 }
6042 
6043 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
6044                                    ASTReaderListener &Listener,
6045                                    bool AllowCompatibleDifferences) {
6046   unsigned Idx = 0;
6047   TargetOptions TargetOpts;
6048   TargetOpts.Triple = ReadString(Record, Idx);
6049   TargetOpts.CPU = ReadString(Record, Idx);
6050   TargetOpts.TuneCPU = ReadString(Record, Idx);
6051   TargetOpts.ABI = ReadString(Record, Idx);
6052   for (unsigned N = Record[Idx++]; N; --N) {
6053     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
6054   }
6055   for (unsigned N = Record[Idx++]; N; --N) {
6056     TargetOpts.Features.push_back(ReadString(Record, Idx));
6057   }
6058 
6059   return Listener.ReadTargetOptions(TargetOpts, Complain,
6060                                     AllowCompatibleDifferences);
6061 }
6062 
6063 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
6064                                        ASTReaderListener &Listener) {
6065   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
6066   unsigned Idx = 0;
6067 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
6068 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6069   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
6070 #include "clang/Basic/DiagnosticOptions.def"
6071 
6072   for (unsigned N = Record[Idx++]; N; --N)
6073     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
6074   for (unsigned N = Record[Idx++]; N; --N)
6075     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
6076 
6077   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
6078 }
6079 
6080 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6081                                        ASTReaderListener &Listener) {
6082   FileSystemOptions FSOpts;
6083   unsigned Idx = 0;
6084   FSOpts.WorkingDir = ReadString(Record, Idx);
6085   return Listener.ReadFileSystemOptions(FSOpts, Complain);
6086 }
6087 
6088 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6089                                          bool Complain,
6090                                          ASTReaderListener &Listener) {
6091   HeaderSearchOptions HSOpts;
6092   unsigned Idx = 0;
6093   HSOpts.Sysroot = ReadString(Record, Idx);
6094 
6095   HSOpts.ResourceDir = ReadString(Record, Idx);
6096   HSOpts.ModuleCachePath = ReadString(Record, Idx);
6097   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6098   HSOpts.DisableModuleHash = Record[Idx++];
6099   HSOpts.ImplicitModuleMaps = Record[Idx++];
6100   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6101   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6102   HSOpts.UseBuiltinIncludes = Record[Idx++];
6103   HSOpts.UseStandardSystemIncludes = Record[Idx++];
6104   HSOpts.UseStandardCXXIncludes = Record[Idx++];
6105   HSOpts.UseLibcxx = Record[Idx++];
6106   std::string SpecificModuleCachePath = ReadString(Record, Idx);
6107 
6108   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
6109                                           Complain);
6110 }
6111 
6112 bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6113                                        ASTReaderListener &Listener) {
6114   HeaderSearchOptions HSOpts;
6115   unsigned Idx = 0;
6116 
6117   // Include entries.
6118   for (unsigned N = Record[Idx++]; N; --N) {
6119     std::string Path = ReadString(Record, Idx);
6120     frontend::IncludeDirGroup Group
6121       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6122     bool IsFramework = Record[Idx++];
6123     bool IgnoreSysRoot = Record[Idx++];
6124     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
6125                                     IgnoreSysRoot);
6126   }
6127 
6128   // System header prefixes.
6129   for (unsigned N = Record[Idx++]; N; --N) {
6130     std::string Prefix = ReadString(Record, Idx);
6131     bool IsSystemHeader = Record[Idx++];
6132     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
6133   }
6134 
6135   // VFS overlay files.
6136   for (unsigned N = Record[Idx++]; N; --N) {
6137     std::string VFSOverlayFile = ReadString(Record, Idx);
6138     HSOpts.VFSOverlayFiles.emplace_back(std::move(VFSOverlayFile));
6139   }
6140 
6141   return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6142 }
6143 
6144 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6145                                          bool Complain,
6146                                          ASTReaderListener &Listener,
6147                                          std::string &SuggestedPredefines) {
6148   PreprocessorOptions PPOpts;
6149   unsigned Idx = 0;
6150 
6151   // Macro definitions/undefs
6152   bool ReadMacros = Record[Idx++];
6153   if (ReadMacros) {
6154     for (unsigned N = Record[Idx++]; N; --N) {
6155       std::string Macro = ReadString(Record, Idx);
6156       bool IsUndef = Record[Idx++];
6157       PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
6158     }
6159   }
6160 
6161   // Includes
6162   for (unsigned N = Record[Idx++]; N; --N) {
6163     PPOpts.Includes.push_back(ReadString(Record, Idx));
6164   }
6165 
6166   // Macro Includes
6167   for (unsigned N = Record[Idx++]; N; --N) {
6168     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
6169   }
6170 
6171   PPOpts.UsePredefines = Record[Idx++];
6172   PPOpts.DetailedRecord = Record[Idx++];
6173   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6174   PPOpts.ObjCXXARCStandardLibrary =
6175     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6176   SuggestedPredefines.clear();
6177   return Listener.ReadPreprocessorOptions(PPOpts, ReadMacros, Complain,
6178                                           SuggestedPredefines);
6179 }
6180 
6181 std::pair<ModuleFile *, unsigned>
6182 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6183   GlobalPreprocessedEntityMapType::iterator
6184   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
6185   assert(I != GlobalPreprocessedEntityMap.end() &&
6186          "Corrupted global preprocessed entity map");
6187   ModuleFile *M = I->second;
6188   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6189   return std::make_pair(M, LocalIndex);
6190 }
6191 
6192 llvm::iterator_range<PreprocessingRecord::iterator>
6193 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6194   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6195     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
6196                                              Mod.NumPreprocessedEntities);
6197 
6198   return llvm::make_range(PreprocessingRecord::iterator(),
6199                           PreprocessingRecord::iterator());
6200 }
6201 
6202 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6203                                         unsigned int ClientLoadCapabilities) {
6204   return ClientLoadCapabilities & ARR_OutOfDate &&
6205          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
6206 }
6207 
6208 llvm::iterator_range<ASTReader::ModuleDeclIterator>
6209 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6210   return llvm::make_range(
6211       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6212       ModuleDeclIterator(this, &Mod,
6213                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6214 }
6215 
6216 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6217   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
6218   assert(I != GlobalSkippedRangeMap.end() &&
6219     "Corrupted global skipped range map");
6220   ModuleFile *M = I->second;
6221   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6222   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6223   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6224   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
6225                     TranslateSourceLocation(*M, RawRange.getEnd()));
6226   assert(Range.isValid());
6227   return Range;
6228 }
6229 
6230 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6231   PreprocessedEntityID PPID = Index+1;
6232   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6233   ModuleFile &M = *PPInfo.first;
6234   unsigned LocalIndex = PPInfo.second;
6235   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6236 
6237   if (!PP.getPreprocessingRecord()) {
6238     Error("no preprocessing record");
6239     return nullptr;
6240   }
6241 
6242   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6243   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6244           M.MacroOffsetsBase + PPOffs.BitOffset)) {
6245     Error(std::move(Err));
6246     return nullptr;
6247   }
6248 
6249   Expected<llvm::BitstreamEntry> MaybeEntry =
6250       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
6251   if (!MaybeEntry) {
6252     Error(MaybeEntry.takeError());
6253     return nullptr;
6254   }
6255   llvm::BitstreamEntry Entry = MaybeEntry.get();
6256 
6257   if (Entry.Kind != llvm::BitstreamEntry::Record)
6258     return nullptr;
6259 
6260   // Read the record.
6261   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
6262                     TranslateSourceLocation(M, PPOffs.getEnd()));
6263   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6264   StringRef Blob;
6265   RecordData Record;
6266   Expected<unsigned> MaybeRecType =
6267       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
6268   if (!MaybeRecType) {
6269     Error(MaybeRecType.takeError());
6270     return nullptr;
6271   }
6272   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6273   case PPD_MACRO_EXPANSION: {
6274     bool isBuiltin = Record[0];
6275     IdentifierInfo *Name = nullptr;
6276     MacroDefinitionRecord *Def = nullptr;
6277     if (isBuiltin)
6278       Name = getLocalIdentifier(M, Record[1]);
6279     else {
6280       PreprocessedEntityID GlobalID =
6281           getGlobalPreprocessedEntityID(M, Record[1]);
6282       Def = cast<MacroDefinitionRecord>(
6283           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6284     }
6285 
6286     MacroExpansion *ME;
6287     if (isBuiltin)
6288       ME = new (PPRec) MacroExpansion(Name, Range);
6289     else
6290       ME = new (PPRec) MacroExpansion(Def, Range);
6291 
6292     return ME;
6293   }
6294 
6295   case PPD_MACRO_DEFINITION: {
6296     // Decode the identifier info and then check again; if the macro is
6297     // still defined and associated with the identifier,
6298     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6299     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6300 
6301     if (DeserializationListener)
6302       DeserializationListener->MacroDefinitionRead(PPID, MD);
6303 
6304     return MD;
6305   }
6306 
6307   case PPD_INCLUSION_DIRECTIVE: {
6308     const char *FullFileNameStart = Blob.data() + Record[0];
6309     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6310     OptionalFileEntryRef File;
6311     if (!FullFileName.empty())
6312       File = PP.getFileManager().getOptionalFileRef(FullFileName);
6313 
6314     // FIXME: Stable encoding
6315     InclusionDirective::InclusionKind Kind
6316       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6317     InclusionDirective *ID
6318       = new (PPRec) InclusionDirective(PPRec, Kind,
6319                                        StringRef(Blob.data(), Record[0]),
6320                                        Record[1], Record[3],
6321                                        File,
6322                                        Range);
6323     return ID;
6324   }
6325   }
6326 
6327   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6328 }
6329 
6330 /// Find the next module that contains entities and return the ID
6331 /// of the first entry.
6332 ///
6333 /// \param SLocMapI points at a chunk of a module that contains no
6334 /// preprocessed entities or the entities it contains are not the ones we are
6335 /// looking for.
6336 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6337                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6338   ++SLocMapI;
6339   for (GlobalSLocOffsetMapType::const_iterator
6340          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6341     ModuleFile &M = *SLocMapI->second;
6342     if (M.NumPreprocessedEntities)
6343       return M.BasePreprocessedEntityID;
6344   }
6345 
6346   return getTotalNumPreprocessedEntities();
6347 }
6348 
6349 namespace {
6350 
6351 struct PPEntityComp {
6352   const ASTReader &Reader;
6353   ModuleFile &M;
6354 
6355   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6356 
6357   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6358     SourceLocation LHS = getLoc(L);
6359     SourceLocation RHS = getLoc(R);
6360     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6361   }
6362 
6363   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6364     SourceLocation LHS = getLoc(L);
6365     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6366   }
6367 
6368   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6369     SourceLocation RHS = getLoc(R);
6370     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6371   }
6372 
6373   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6374     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6375   }
6376 };
6377 
6378 } // namespace
6379 
6380 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6381                                                        bool EndsAfter) const {
6382   if (SourceMgr.isLocalSourceLocation(Loc))
6383     return getTotalNumPreprocessedEntities();
6384 
6385   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6386       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6387   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6388          "Corrupted global sloc offset map");
6389 
6390   if (SLocMapI->second->NumPreprocessedEntities == 0)
6391     return findNextPreprocessedEntity(SLocMapI);
6392 
6393   ModuleFile &M = *SLocMapI->second;
6394 
6395   using pp_iterator = const PPEntityOffset *;
6396 
6397   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6398   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6399 
6400   size_t Count = M.NumPreprocessedEntities;
6401   size_t Half;
6402   pp_iterator First = pp_begin;
6403   pp_iterator PPI;
6404 
6405   if (EndsAfter) {
6406     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6407                            PPEntityComp(*this, M));
6408   } else {
6409     // Do a binary search manually instead of using std::lower_bound because
6410     // The end locations of entities may be unordered (when a macro expansion
6411     // is inside another macro argument), but for this case it is not important
6412     // whether we get the first macro expansion or its containing macro.
6413     while (Count > 0) {
6414       Half = Count / 2;
6415       PPI = First;
6416       std::advance(PPI, Half);
6417       if (SourceMgr.isBeforeInTranslationUnit(
6418               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6419         First = PPI;
6420         ++First;
6421         Count = Count - Half - 1;
6422       } else
6423         Count = Half;
6424     }
6425   }
6426 
6427   if (PPI == pp_end)
6428     return findNextPreprocessedEntity(SLocMapI);
6429 
6430   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6431 }
6432 
6433 /// Returns a pair of [Begin, End) indices of preallocated
6434 /// preprocessed entities that \arg Range encompasses.
6435 std::pair<unsigned, unsigned>
6436     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6437   if (Range.isInvalid())
6438     return std::make_pair(0,0);
6439   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6440 
6441   PreprocessedEntityID BeginID =
6442       findPreprocessedEntity(Range.getBegin(), false);
6443   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6444   return std::make_pair(BeginID, EndID);
6445 }
6446 
6447 /// Optionally returns true or false if the preallocated preprocessed
6448 /// entity with index \arg Index came from file \arg FID.
6449 std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6450                                                             FileID FID) {
6451   if (FID.isInvalid())
6452     return false;
6453 
6454   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6455   ModuleFile &M = *PPInfo.first;
6456   unsigned LocalIndex = PPInfo.second;
6457   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6458 
6459   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6460   if (Loc.isInvalid())
6461     return false;
6462 
6463   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6464     return true;
6465   else
6466     return false;
6467 }
6468 
6469 namespace {
6470 
6471   /// Visitor used to search for information about a header file.
6472   class HeaderFileInfoVisitor {
6473   FileEntryRef FE;
6474     std::optional<HeaderFileInfo> HFI;
6475 
6476   public:
6477     explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6478 
6479     bool operator()(ModuleFile &M) {
6480       HeaderFileInfoLookupTable *Table
6481         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6482       if (!Table)
6483         return false;
6484 
6485       // Look in the on-disk hash table for an entry for this file name.
6486       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6487       if (Pos == Table->end())
6488         return false;
6489 
6490       HFI = *Pos;
6491       return true;
6492     }
6493 
6494     std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6495   };
6496 
6497 } // namespace
6498 
6499 HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6500   HeaderFileInfoVisitor Visitor(FE);
6501   ModuleMgr.visit(Visitor);
6502   if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6503       return *HFI;
6504 
6505   return HeaderFileInfo();
6506 }
6507 
6508 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6509   using DiagState = DiagnosticsEngine::DiagState;
6510   SmallVector<DiagState *, 32> DiagStates;
6511 
6512   for (ModuleFile &F : ModuleMgr) {
6513     unsigned Idx = 0;
6514     auto &Record = F.PragmaDiagMappings;
6515     if (Record.empty())
6516       continue;
6517 
6518     DiagStates.clear();
6519 
6520     auto ReadDiagState = [&](const DiagState &BasedOn,
6521                              bool IncludeNonPragmaStates) {
6522       unsigned BackrefID = Record[Idx++];
6523       if (BackrefID != 0)
6524         return DiagStates[BackrefID - 1];
6525 
6526       // A new DiagState was created here.
6527       Diag.DiagStates.push_back(BasedOn);
6528       DiagState *NewState = &Diag.DiagStates.back();
6529       DiagStates.push_back(NewState);
6530       unsigned Size = Record[Idx++];
6531       assert(Idx + Size * 2 <= Record.size() &&
6532              "Invalid data, not enough diag/map pairs");
6533       while (Size--) {
6534         unsigned DiagID = Record[Idx++];
6535         DiagnosticMapping NewMapping =
6536             DiagnosticMapping::deserialize(Record[Idx++]);
6537         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6538           continue;
6539 
6540         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6541 
6542         // If this mapping was specified as a warning but the severity was
6543         // upgraded due to diagnostic settings, simulate the current diagnostic
6544         // settings (and use a warning).
6545         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6546           NewMapping.setSeverity(diag::Severity::Warning);
6547           NewMapping.setUpgradedFromWarning(false);
6548         }
6549 
6550         Mapping = NewMapping;
6551       }
6552       return NewState;
6553     };
6554 
6555     // Read the first state.
6556     DiagState *FirstState;
6557     if (F.Kind == MK_ImplicitModule) {
6558       // Implicitly-built modules are reused with different diagnostic
6559       // settings.  Use the initial diagnostic state from Diag to simulate this
6560       // compilation's diagnostic settings.
6561       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6562       DiagStates.push_back(FirstState);
6563 
6564       // Skip the initial diagnostic state from the serialized module.
6565       assert(Record[1] == 0 &&
6566              "Invalid data, unexpected backref in initial state");
6567       Idx = 3 + Record[2] * 2;
6568       assert(Idx < Record.size() &&
6569              "Invalid data, not enough state change pairs in initial state");
6570     } else if (F.isModule()) {
6571       // For an explicit module, preserve the flags from the module build
6572       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6573       // -Wblah flags.
6574       unsigned Flags = Record[Idx++];
6575       DiagState Initial;
6576       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6577       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6578       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6579       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6580       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6581       Initial.ExtBehavior = (diag::Severity)Flags;
6582       FirstState = ReadDiagState(Initial, true);
6583 
6584       assert(F.OriginalSourceFileID.isValid());
6585 
6586       // Set up the root buffer of the module to start with the initial
6587       // diagnostic state of the module itself, to cover files that contain no
6588       // explicit transitions (for which we did not serialize anything).
6589       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6590           .StateTransitions.push_back({FirstState, 0});
6591     } else {
6592       // For prefix ASTs, start with whatever the user configured on the
6593       // command line.
6594       Idx++; // Skip flags.
6595       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6596     }
6597 
6598     // Read the state transitions.
6599     unsigned NumLocations = Record[Idx++];
6600     while (NumLocations--) {
6601       assert(Idx < Record.size() &&
6602              "Invalid data, missing pragma diagnostic states");
6603       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6604       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6605       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6606       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6607       unsigned Transitions = Record[Idx++];
6608 
6609       // Note that we don't need to set up Parent/ParentOffset here, because
6610       // we won't be changing the diagnostic state within imported FileIDs
6611       // (other than perhaps appending to the main source file, which has no
6612       // parent).
6613       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6614       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6615       for (unsigned I = 0; I != Transitions; ++I) {
6616         unsigned Offset = Record[Idx++];
6617         auto *State = ReadDiagState(*FirstState, false);
6618         F.StateTransitions.push_back({State, Offset});
6619       }
6620     }
6621 
6622     // Read the final state.
6623     assert(Idx < Record.size() &&
6624            "Invalid data, missing final pragma diagnostic state");
6625     SourceLocation CurStateLoc = ReadSourceLocation(F, Record[Idx++]);
6626     auto *CurState = ReadDiagState(*FirstState, false);
6627 
6628     if (!F.isModule()) {
6629       Diag.DiagStatesByLoc.CurDiagState = CurState;
6630       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6631 
6632       // Preserve the property that the imaginary root file describes the
6633       // current state.
6634       FileID NullFile;
6635       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6636       if (T.empty())
6637         T.push_back({CurState, 0});
6638       else
6639         T[0].State = CurState;
6640     }
6641 
6642     // Don't try to read these mappings again.
6643     Record.clear();
6644   }
6645 }
6646 
6647 /// Get the correct cursor and offset for loading a type.
6648 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6649   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6650   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6651   ModuleFile *M = I->second;
6652   return RecordLocation(
6653       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6654              M->DeclsBlockStartOffset);
6655 }
6656 
6657 static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6658   switch (code) {
6659 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6660   case TYPE_##CODE_ID: return Type::CLASS_ID;
6661 #include "clang/Serialization/TypeBitCodes.def"
6662   default:
6663     return std::nullopt;
6664   }
6665 }
6666 
6667 /// Read and return the type with the given index..
6668 ///
6669 /// The index is the type ID, shifted and minus the number of predefs. This
6670 /// routine actually reads the record corresponding to the type at the given
6671 /// location. It is a helper routine for GetType, which deals with reading type
6672 /// IDs.
6673 QualType ASTReader::readTypeRecord(unsigned Index) {
6674   assert(ContextObj && "reading type with no AST context");
6675   ASTContext &Context = *ContextObj;
6676   RecordLocation Loc = TypeCursorForIndex(Index);
6677   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6678 
6679   // Keep track of where we are in the stream, then jump back there
6680   // after reading this type.
6681   SavedStreamPosition SavedPosition(DeclsCursor);
6682 
6683   ReadingKindTracker ReadingKind(Read_Type, *this);
6684 
6685   // Note that we are loading a type record.
6686   Deserializing AType(this);
6687 
6688   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6689     Error(std::move(Err));
6690     return QualType();
6691   }
6692   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6693   if (!RawCode) {
6694     Error(RawCode.takeError());
6695     return QualType();
6696   }
6697 
6698   ASTRecordReader Record(*this, *Loc.F);
6699   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6700   if (!Code) {
6701     Error(Code.takeError());
6702     return QualType();
6703   }
6704   if (Code.get() == TYPE_EXT_QUAL) {
6705     QualType baseType = Record.readQualType();
6706     Qualifiers quals = Record.readQualifiers();
6707     return Context.getQualifiedType(baseType, quals);
6708   }
6709 
6710   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6711   if (!maybeClass) {
6712     Error("Unexpected code for type");
6713     return QualType();
6714   }
6715 
6716   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6717   return TypeReader.read(*maybeClass);
6718 }
6719 
6720 namespace clang {
6721 
6722 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6723   using LocSeq = SourceLocationSequence;
6724 
6725   ASTRecordReader &Reader;
6726   LocSeq *Seq;
6727 
6728   SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); }
6729   SourceRange readSourceRange() { return Reader.readSourceRange(Seq); }
6730 
6731   TypeSourceInfo *GetTypeSourceInfo() {
6732     return Reader.readTypeSourceInfo();
6733   }
6734 
6735   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6736     return Reader.readNestedNameSpecifierLoc();
6737   }
6738 
6739   Attr *ReadAttr() {
6740     return Reader.readAttr();
6741   }
6742 
6743 public:
6744   TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq)
6745       : Reader(Reader), Seq(Seq) {}
6746 
6747   // We want compile-time assurance that we've enumerated all of
6748   // these, so unfortunately we have to declare them first, then
6749   // define them out-of-line.
6750 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6751 #define TYPELOC(CLASS, PARENT) \
6752   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6753 #include "clang/AST/TypeLocNodes.def"
6754 
6755   void VisitFunctionTypeLoc(FunctionTypeLoc);
6756   void VisitArrayTypeLoc(ArrayTypeLoc);
6757 };
6758 
6759 } // namespace clang
6760 
6761 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6762   // nothing to do
6763 }
6764 
6765 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6766   TL.setBuiltinLoc(readSourceLocation());
6767   if (TL.needsExtraLocalData()) {
6768     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6769     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6770     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6771     TL.setModeAttr(Reader.readInt());
6772   }
6773 }
6774 
6775 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6776   TL.setNameLoc(readSourceLocation());
6777 }
6778 
6779 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6780   TL.setStarLoc(readSourceLocation());
6781 }
6782 
6783 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6784   // nothing to do
6785 }
6786 
6787 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6788   // nothing to do
6789 }
6790 
6791 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6792   TL.setExpansionLoc(readSourceLocation());
6793 }
6794 
6795 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6796   TL.setCaretLoc(readSourceLocation());
6797 }
6798 
6799 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6800   TL.setAmpLoc(readSourceLocation());
6801 }
6802 
6803 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6804   TL.setAmpAmpLoc(readSourceLocation());
6805 }
6806 
6807 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6808   TL.setStarLoc(readSourceLocation());
6809   TL.setClassTInfo(GetTypeSourceInfo());
6810 }
6811 
6812 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6813   TL.setLBracketLoc(readSourceLocation());
6814   TL.setRBracketLoc(readSourceLocation());
6815   if (Reader.readBool())
6816     TL.setSizeExpr(Reader.readExpr());
6817   else
6818     TL.setSizeExpr(nullptr);
6819 }
6820 
6821 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6822   VisitArrayTypeLoc(TL);
6823 }
6824 
6825 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6826   VisitArrayTypeLoc(TL);
6827 }
6828 
6829 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6830   VisitArrayTypeLoc(TL);
6831 }
6832 
6833 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6834                                             DependentSizedArrayTypeLoc TL) {
6835   VisitArrayTypeLoc(TL);
6836 }
6837 
6838 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6839     DependentAddressSpaceTypeLoc TL) {
6840 
6841     TL.setAttrNameLoc(readSourceLocation());
6842     TL.setAttrOperandParensRange(readSourceRange());
6843     TL.setAttrExprOperand(Reader.readExpr());
6844 }
6845 
6846 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6847                                         DependentSizedExtVectorTypeLoc TL) {
6848   TL.setNameLoc(readSourceLocation());
6849 }
6850 
6851 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6852   TL.setNameLoc(readSourceLocation());
6853 }
6854 
6855 void TypeLocReader::VisitDependentVectorTypeLoc(
6856     DependentVectorTypeLoc TL) {
6857   TL.setNameLoc(readSourceLocation());
6858 }
6859 
6860 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6861   TL.setNameLoc(readSourceLocation());
6862 }
6863 
6864 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6865   TL.setAttrNameLoc(readSourceLocation());
6866   TL.setAttrOperandParensRange(readSourceRange());
6867   TL.setAttrRowOperand(Reader.readExpr());
6868   TL.setAttrColumnOperand(Reader.readExpr());
6869 }
6870 
6871 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6872     DependentSizedMatrixTypeLoc TL) {
6873   TL.setAttrNameLoc(readSourceLocation());
6874   TL.setAttrOperandParensRange(readSourceRange());
6875   TL.setAttrRowOperand(Reader.readExpr());
6876   TL.setAttrColumnOperand(Reader.readExpr());
6877 }
6878 
6879 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6880   TL.setLocalRangeBegin(readSourceLocation());
6881   TL.setLParenLoc(readSourceLocation());
6882   TL.setRParenLoc(readSourceLocation());
6883   TL.setExceptionSpecRange(readSourceRange());
6884   TL.setLocalRangeEnd(readSourceLocation());
6885   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6886     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6887   }
6888 }
6889 
6890 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6891   VisitFunctionTypeLoc(TL);
6892 }
6893 
6894 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6895   VisitFunctionTypeLoc(TL);
6896 }
6897 
6898 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6899   TL.setNameLoc(readSourceLocation());
6900 }
6901 
6902 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6903   TL.setNameLoc(readSourceLocation());
6904 }
6905 
6906 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6907   TL.setNameLoc(readSourceLocation());
6908 }
6909 
6910 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6911   TL.setTypeofLoc(readSourceLocation());
6912   TL.setLParenLoc(readSourceLocation());
6913   TL.setRParenLoc(readSourceLocation());
6914 }
6915 
6916 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6917   TL.setTypeofLoc(readSourceLocation());
6918   TL.setLParenLoc(readSourceLocation());
6919   TL.setRParenLoc(readSourceLocation());
6920   TL.setUnmodifiedTInfo(GetTypeSourceInfo());
6921 }
6922 
6923 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6924   TL.setDecltypeLoc(readSourceLocation());
6925   TL.setRParenLoc(readSourceLocation());
6926 }
6927 
6928 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6929   TL.setKWLoc(readSourceLocation());
6930   TL.setLParenLoc(readSourceLocation());
6931   TL.setRParenLoc(readSourceLocation());
6932   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6933 }
6934 
6935 ConceptReference *ASTRecordReader::readConceptReference() {
6936   auto NNS = readNestedNameSpecifierLoc();
6937   auto TemplateKWLoc = readSourceLocation();
6938   auto ConceptNameLoc = readDeclarationNameInfo();
6939   auto FoundDecl = readDeclAs<NamedDecl>();
6940   auto NamedConcept = readDeclAs<ConceptDecl>();
6941   auto *CR = ConceptReference::Create(
6942       getContext(), NNS, TemplateKWLoc, ConceptNameLoc, FoundDecl, NamedConcept,
6943       (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
6944   return CR;
6945 }
6946 
6947 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6948   TL.setNameLoc(readSourceLocation());
6949   if (Reader.readBool())
6950     TL.setConceptReference(Reader.readConceptReference());
6951   if (Reader.readBool())
6952     TL.setRParenLoc(readSourceLocation());
6953 }
6954 
6955 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6956     DeducedTemplateSpecializationTypeLoc TL) {
6957   TL.setTemplateNameLoc(readSourceLocation());
6958 }
6959 
6960 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6961   TL.setNameLoc(readSourceLocation());
6962 }
6963 
6964 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6965   TL.setNameLoc(readSourceLocation());
6966 }
6967 
6968 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6969   TL.setAttr(ReadAttr());
6970 }
6971 
6972 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6973   // Nothing to do.
6974 }
6975 
6976 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6977   TL.setNameLoc(readSourceLocation());
6978 }
6979 
6980 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6981                                             SubstTemplateTypeParmTypeLoc TL) {
6982   TL.setNameLoc(readSourceLocation());
6983 }
6984 
6985 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6986                                           SubstTemplateTypeParmPackTypeLoc TL) {
6987   TL.setNameLoc(readSourceLocation());
6988 }
6989 
6990 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6991                                            TemplateSpecializationTypeLoc TL) {
6992   TL.setTemplateKeywordLoc(readSourceLocation());
6993   TL.setTemplateNameLoc(readSourceLocation());
6994   TL.setLAngleLoc(readSourceLocation());
6995   TL.setRAngleLoc(readSourceLocation());
6996   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6997     TL.setArgLocInfo(i,
6998                      Reader.readTemplateArgumentLocInfo(
6999                          TL.getTypePtr()->template_arguments()[i].getKind()));
7000 }
7001 
7002 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7003   TL.setLParenLoc(readSourceLocation());
7004   TL.setRParenLoc(readSourceLocation());
7005 }
7006 
7007 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7008   TL.setElaboratedKeywordLoc(readSourceLocation());
7009   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7010 }
7011 
7012 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7013   TL.setNameLoc(readSourceLocation());
7014 }
7015 
7016 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7017   TL.setElaboratedKeywordLoc(readSourceLocation());
7018   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7019   TL.setNameLoc(readSourceLocation());
7020 }
7021 
7022 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7023        DependentTemplateSpecializationTypeLoc TL) {
7024   TL.setElaboratedKeywordLoc(readSourceLocation());
7025   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7026   TL.setTemplateKeywordLoc(readSourceLocation());
7027   TL.setTemplateNameLoc(readSourceLocation());
7028   TL.setLAngleLoc(readSourceLocation());
7029   TL.setRAngleLoc(readSourceLocation());
7030   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7031     TL.setArgLocInfo(I,
7032                      Reader.readTemplateArgumentLocInfo(
7033                          TL.getTypePtr()->template_arguments()[I].getKind()));
7034 }
7035 
7036 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7037   TL.setEllipsisLoc(readSourceLocation());
7038 }
7039 
7040 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7041   TL.setNameLoc(readSourceLocation());
7042   TL.setNameEndLoc(readSourceLocation());
7043 }
7044 
7045 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7046   if (TL.getNumProtocols()) {
7047     TL.setProtocolLAngleLoc(readSourceLocation());
7048     TL.setProtocolRAngleLoc(readSourceLocation());
7049   }
7050   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7051     TL.setProtocolLoc(i, readSourceLocation());
7052 }
7053 
7054 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7055   TL.setHasBaseTypeAsWritten(Reader.readBool());
7056   TL.setTypeArgsLAngleLoc(readSourceLocation());
7057   TL.setTypeArgsRAngleLoc(readSourceLocation());
7058   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7059     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7060   TL.setProtocolLAngleLoc(readSourceLocation());
7061   TL.setProtocolRAngleLoc(readSourceLocation());
7062   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7063     TL.setProtocolLoc(i, readSourceLocation());
7064 }
7065 
7066 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7067   TL.setStarLoc(readSourceLocation());
7068 }
7069 
7070 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7071   TL.setKWLoc(readSourceLocation());
7072   TL.setLParenLoc(readSourceLocation());
7073   TL.setRParenLoc(readSourceLocation());
7074 }
7075 
7076 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7077   TL.setKWLoc(readSourceLocation());
7078 }
7079 
7080 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7081   TL.setNameLoc(readSourceLocation());
7082 }
7083 void TypeLocReader::VisitDependentBitIntTypeLoc(
7084     clang::DependentBitIntTypeLoc TL) {
7085   TL.setNameLoc(readSourceLocation());
7086 }
7087 
7088 void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) {
7089   LocSeq::State Seq(ParentSeq);
7090   TypeLocReader TLR(*this, Seq);
7091   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7092     TLR.Visit(TL);
7093 }
7094 
7095 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7096   QualType InfoTy = readType();
7097   if (InfoTy.isNull())
7098     return nullptr;
7099 
7100   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7101   readTypeLoc(TInfo->getTypeLoc());
7102   return TInfo;
7103 }
7104 
7105 QualType ASTReader::GetType(TypeID ID) {
7106   assert(ContextObj && "reading type with no AST context");
7107   ASTContext &Context = *ContextObj;
7108 
7109   unsigned FastQuals = ID & Qualifiers::FastMask;
7110   unsigned Index = ID >> Qualifiers::FastWidth;
7111 
7112   if (Index < NUM_PREDEF_TYPE_IDS) {
7113     QualType T;
7114     switch ((PredefinedTypeIDs)Index) {
7115     case PREDEF_TYPE_LAST_ID:
7116       // We should never use this one.
7117       llvm_unreachable("Invalid predefined type");
7118       break;
7119     case PREDEF_TYPE_NULL_ID:
7120       return QualType();
7121     case PREDEF_TYPE_VOID_ID:
7122       T = Context.VoidTy;
7123       break;
7124     case PREDEF_TYPE_BOOL_ID:
7125       T = Context.BoolTy;
7126       break;
7127     case PREDEF_TYPE_CHAR_U_ID:
7128     case PREDEF_TYPE_CHAR_S_ID:
7129       // FIXME: Check that the signedness of CharTy is correct!
7130       T = Context.CharTy;
7131       break;
7132     case PREDEF_TYPE_UCHAR_ID:
7133       T = Context.UnsignedCharTy;
7134       break;
7135     case PREDEF_TYPE_USHORT_ID:
7136       T = Context.UnsignedShortTy;
7137       break;
7138     case PREDEF_TYPE_UINT_ID:
7139       T = Context.UnsignedIntTy;
7140       break;
7141     case PREDEF_TYPE_ULONG_ID:
7142       T = Context.UnsignedLongTy;
7143       break;
7144     case PREDEF_TYPE_ULONGLONG_ID:
7145       T = Context.UnsignedLongLongTy;
7146       break;
7147     case PREDEF_TYPE_UINT128_ID:
7148       T = Context.UnsignedInt128Ty;
7149       break;
7150     case PREDEF_TYPE_SCHAR_ID:
7151       T = Context.SignedCharTy;
7152       break;
7153     case PREDEF_TYPE_WCHAR_ID:
7154       T = Context.WCharTy;
7155       break;
7156     case PREDEF_TYPE_SHORT_ID:
7157       T = Context.ShortTy;
7158       break;
7159     case PREDEF_TYPE_INT_ID:
7160       T = Context.IntTy;
7161       break;
7162     case PREDEF_TYPE_LONG_ID:
7163       T = Context.LongTy;
7164       break;
7165     case PREDEF_TYPE_LONGLONG_ID:
7166       T = Context.LongLongTy;
7167       break;
7168     case PREDEF_TYPE_INT128_ID:
7169       T = Context.Int128Ty;
7170       break;
7171     case PREDEF_TYPE_BFLOAT16_ID:
7172       T = Context.BFloat16Ty;
7173       break;
7174     case PREDEF_TYPE_HALF_ID:
7175       T = Context.HalfTy;
7176       break;
7177     case PREDEF_TYPE_FLOAT_ID:
7178       T = Context.FloatTy;
7179       break;
7180     case PREDEF_TYPE_DOUBLE_ID:
7181       T = Context.DoubleTy;
7182       break;
7183     case PREDEF_TYPE_LONGDOUBLE_ID:
7184       T = Context.LongDoubleTy;
7185       break;
7186     case PREDEF_TYPE_SHORT_ACCUM_ID:
7187       T = Context.ShortAccumTy;
7188       break;
7189     case PREDEF_TYPE_ACCUM_ID:
7190       T = Context.AccumTy;
7191       break;
7192     case PREDEF_TYPE_LONG_ACCUM_ID:
7193       T = Context.LongAccumTy;
7194       break;
7195     case PREDEF_TYPE_USHORT_ACCUM_ID:
7196       T = Context.UnsignedShortAccumTy;
7197       break;
7198     case PREDEF_TYPE_UACCUM_ID:
7199       T = Context.UnsignedAccumTy;
7200       break;
7201     case PREDEF_TYPE_ULONG_ACCUM_ID:
7202       T = Context.UnsignedLongAccumTy;
7203       break;
7204     case PREDEF_TYPE_SHORT_FRACT_ID:
7205       T = Context.ShortFractTy;
7206       break;
7207     case PREDEF_TYPE_FRACT_ID:
7208       T = Context.FractTy;
7209       break;
7210     case PREDEF_TYPE_LONG_FRACT_ID:
7211       T = Context.LongFractTy;
7212       break;
7213     case PREDEF_TYPE_USHORT_FRACT_ID:
7214       T = Context.UnsignedShortFractTy;
7215       break;
7216     case PREDEF_TYPE_UFRACT_ID:
7217       T = Context.UnsignedFractTy;
7218       break;
7219     case PREDEF_TYPE_ULONG_FRACT_ID:
7220       T = Context.UnsignedLongFractTy;
7221       break;
7222     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7223       T = Context.SatShortAccumTy;
7224       break;
7225     case PREDEF_TYPE_SAT_ACCUM_ID:
7226       T = Context.SatAccumTy;
7227       break;
7228     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7229       T = Context.SatLongAccumTy;
7230       break;
7231     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7232       T = Context.SatUnsignedShortAccumTy;
7233       break;
7234     case PREDEF_TYPE_SAT_UACCUM_ID:
7235       T = Context.SatUnsignedAccumTy;
7236       break;
7237     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7238       T = Context.SatUnsignedLongAccumTy;
7239       break;
7240     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7241       T = Context.SatShortFractTy;
7242       break;
7243     case PREDEF_TYPE_SAT_FRACT_ID:
7244       T = Context.SatFractTy;
7245       break;
7246     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7247       T = Context.SatLongFractTy;
7248       break;
7249     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7250       T = Context.SatUnsignedShortFractTy;
7251       break;
7252     case PREDEF_TYPE_SAT_UFRACT_ID:
7253       T = Context.SatUnsignedFractTy;
7254       break;
7255     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7256       T = Context.SatUnsignedLongFractTy;
7257       break;
7258     case PREDEF_TYPE_FLOAT16_ID:
7259       T = Context.Float16Ty;
7260       break;
7261     case PREDEF_TYPE_FLOAT128_ID:
7262       T = Context.Float128Ty;
7263       break;
7264     case PREDEF_TYPE_IBM128_ID:
7265       T = Context.Ibm128Ty;
7266       break;
7267     case PREDEF_TYPE_OVERLOAD_ID:
7268       T = Context.OverloadTy;
7269       break;
7270     case PREDEF_TYPE_BOUND_MEMBER:
7271       T = Context.BoundMemberTy;
7272       break;
7273     case PREDEF_TYPE_PSEUDO_OBJECT:
7274       T = Context.PseudoObjectTy;
7275       break;
7276     case PREDEF_TYPE_DEPENDENT_ID:
7277       T = Context.DependentTy;
7278       break;
7279     case PREDEF_TYPE_UNKNOWN_ANY:
7280       T = Context.UnknownAnyTy;
7281       break;
7282     case PREDEF_TYPE_NULLPTR_ID:
7283       T = Context.NullPtrTy;
7284       break;
7285     case PREDEF_TYPE_CHAR8_ID:
7286       T = Context.Char8Ty;
7287       break;
7288     case PREDEF_TYPE_CHAR16_ID:
7289       T = Context.Char16Ty;
7290       break;
7291     case PREDEF_TYPE_CHAR32_ID:
7292       T = Context.Char32Ty;
7293       break;
7294     case PREDEF_TYPE_OBJC_ID:
7295       T = Context.ObjCBuiltinIdTy;
7296       break;
7297     case PREDEF_TYPE_OBJC_CLASS:
7298       T = Context.ObjCBuiltinClassTy;
7299       break;
7300     case PREDEF_TYPE_OBJC_SEL:
7301       T = Context.ObjCBuiltinSelTy;
7302       break;
7303 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7304     case PREDEF_TYPE_##Id##_ID: \
7305       T = Context.SingletonId; \
7306       break;
7307 #include "clang/Basic/OpenCLImageTypes.def"
7308 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7309     case PREDEF_TYPE_##Id##_ID: \
7310       T = Context.Id##Ty; \
7311       break;
7312 #include "clang/Basic/OpenCLExtensionTypes.def"
7313     case PREDEF_TYPE_SAMPLER_ID:
7314       T = Context.OCLSamplerTy;
7315       break;
7316     case PREDEF_TYPE_EVENT_ID:
7317       T = Context.OCLEventTy;
7318       break;
7319     case PREDEF_TYPE_CLK_EVENT_ID:
7320       T = Context.OCLClkEventTy;
7321       break;
7322     case PREDEF_TYPE_QUEUE_ID:
7323       T = Context.OCLQueueTy;
7324       break;
7325     case PREDEF_TYPE_RESERVE_ID_ID:
7326       T = Context.OCLReserveIDTy;
7327       break;
7328     case PREDEF_TYPE_AUTO_DEDUCT:
7329       T = Context.getAutoDeductType();
7330       break;
7331     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7332       T = Context.getAutoRRefDeductType();
7333       break;
7334     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7335       T = Context.ARCUnbridgedCastTy;
7336       break;
7337     case PREDEF_TYPE_BUILTIN_FN:
7338       T = Context.BuiltinFnTy;
7339       break;
7340     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7341       T = Context.IncompleteMatrixIdxTy;
7342       break;
7343     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7344       T = Context.OMPArraySectionTy;
7345       break;
7346     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7347       T = Context.OMPArraySectionTy;
7348       break;
7349     case PREDEF_TYPE_OMP_ITERATOR:
7350       T = Context.OMPIteratorTy;
7351       break;
7352 #define SVE_TYPE(Name, Id, SingletonId) \
7353     case PREDEF_TYPE_##Id##_ID: \
7354       T = Context.SingletonId; \
7355       break;
7356 #include "clang/Basic/AArch64SVEACLETypes.def"
7357 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7358     case PREDEF_TYPE_##Id##_ID: \
7359       T = Context.Id##Ty; \
7360       break;
7361 #include "clang/Basic/PPCTypes.def"
7362 #define RVV_TYPE(Name, Id, SingletonId) \
7363     case PREDEF_TYPE_##Id##_ID: \
7364       T = Context.SingletonId; \
7365       break;
7366 #include "clang/Basic/RISCVVTypes.def"
7367 #define WASM_TYPE(Name, Id, SingletonId)                                       \
7368   case PREDEF_TYPE_##Id##_ID:                                                  \
7369     T = Context.SingletonId;                                                   \
7370     break;
7371 #include "clang/Basic/WebAssemblyReferenceTypes.def"
7372     }
7373 
7374     assert(!T.isNull() && "Unknown predefined type");
7375     return T.withFastQualifiers(FastQuals);
7376   }
7377 
7378   Index -= NUM_PREDEF_TYPE_IDS;
7379   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7380   if (TypesLoaded[Index].isNull()) {
7381     TypesLoaded[Index] = readTypeRecord(Index);
7382     if (TypesLoaded[Index].isNull())
7383       return QualType();
7384 
7385     TypesLoaded[Index]->setFromAST();
7386     if (DeserializationListener)
7387       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7388                                         TypesLoaded[Index]);
7389   }
7390 
7391   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7392 }
7393 
7394 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7395   return GetType(getGlobalTypeID(F, LocalID));
7396 }
7397 
7398 serialization::TypeID
7399 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7400   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7401   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7402 
7403   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7404     return LocalID;
7405 
7406   if (!F.ModuleOffsetMap.empty())
7407     ReadModuleOffsetMap(F);
7408 
7409   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7410     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7411   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7412 
7413   unsigned GlobalIndex = LocalIndex + I->second;
7414   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7415 }
7416 
7417 TemplateArgumentLocInfo
7418 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7419   switch (Kind) {
7420   case TemplateArgument::Expression:
7421     return readExpr();
7422   case TemplateArgument::Type:
7423     return readTypeSourceInfo();
7424   case TemplateArgument::Template: {
7425     NestedNameSpecifierLoc QualifierLoc =
7426       readNestedNameSpecifierLoc();
7427     SourceLocation TemplateNameLoc = readSourceLocation();
7428     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7429                                    TemplateNameLoc, SourceLocation());
7430   }
7431   case TemplateArgument::TemplateExpansion: {
7432     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7433     SourceLocation TemplateNameLoc = readSourceLocation();
7434     SourceLocation EllipsisLoc = readSourceLocation();
7435     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7436                                    TemplateNameLoc, EllipsisLoc);
7437   }
7438   case TemplateArgument::Null:
7439   case TemplateArgument::Integral:
7440   case TemplateArgument::Declaration:
7441   case TemplateArgument::NullPtr:
7442   case TemplateArgument::Pack:
7443     // FIXME: Is this right?
7444     return TemplateArgumentLocInfo();
7445   }
7446   llvm_unreachable("unexpected template argument loc");
7447 }
7448 
7449 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7450   TemplateArgument Arg = readTemplateArgument();
7451 
7452   if (Arg.getKind() == TemplateArgument::Expression) {
7453     if (readBool()) // bool InfoHasSameExpr.
7454       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7455   }
7456   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7457 }
7458 
7459 void ASTRecordReader::readTemplateArgumentListInfo(
7460     TemplateArgumentListInfo &Result) {
7461   Result.setLAngleLoc(readSourceLocation());
7462   Result.setRAngleLoc(readSourceLocation());
7463   unsigned NumArgsAsWritten = readInt();
7464   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7465     Result.addArgument(readTemplateArgumentLoc());
7466 }
7467 
7468 const ASTTemplateArgumentListInfo *
7469 ASTRecordReader::readASTTemplateArgumentListInfo() {
7470   TemplateArgumentListInfo Result;
7471   readTemplateArgumentListInfo(Result);
7472   return ASTTemplateArgumentListInfo::Create(getContext(), Result);
7473 }
7474 
7475 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7476   return GetDecl(ID);
7477 }
7478 
7479 void ASTReader::CompleteRedeclChain(const Decl *D) {
7480   if (NumCurrentElementsDeserializing) {
7481     // We arrange to not care about the complete redeclaration chain while we're
7482     // deserializing. Just remember that the AST has marked this one as complete
7483     // but that it's not actually complete yet, so we know we still need to
7484     // complete it later.
7485     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7486     return;
7487   }
7488 
7489   if (!D->getDeclContext()) {
7490     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7491     return;
7492   }
7493 
7494   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7495 
7496   // If this is a named declaration, complete it by looking it up
7497   // within its context.
7498   //
7499   // FIXME: Merging a function definition should merge
7500   // all mergeable entities within it.
7501   if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(DC)) {
7502     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7503       if (!getContext().getLangOpts().CPlusPlus &&
7504           isa<TranslationUnitDecl>(DC)) {
7505         // Outside of C++, we don't have a lookup table for the TU, so update
7506         // the identifier instead. (For C++ modules, we don't store decls
7507         // in the serialized identifier table, so we do the lookup in the TU.)
7508         auto *II = Name.getAsIdentifierInfo();
7509         assert(II && "non-identifier name in C?");
7510         if (II->isOutOfDate())
7511           updateOutOfDateIdentifier(*II);
7512       } else
7513         DC->lookup(Name);
7514     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7515       // Find all declarations of this kind from the relevant context.
7516       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7517         auto *DC = cast<DeclContext>(DCDecl);
7518         SmallVector<Decl*, 8> Decls;
7519         FindExternalLexicalDecls(
7520             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7521       }
7522     }
7523   }
7524 
7525   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7526     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7527   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7528     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7529   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7530     if (auto *Template = FD->getPrimaryTemplate())
7531       Template->LoadLazySpecializations();
7532   }
7533 }
7534 
7535 CXXCtorInitializer **
7536 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7537   RecordLocation Loc = getLocalBitOffset(Offset);
7538   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7539   SavedStreamPosition SavedPosition(Cursor);
7540   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7541     Error(std::move(Err));
7542     return nullptr;
7543   }
7544   ReadingKindTracker ReadingKind(Read_Decl, *this);
7545   Deserializing D(this);
7546 
7547   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7548   if (!MaybeCode) {
7549     Error(MaybeCode.takeError());
7550     return nullptr;
7551   }
7552   unsigned Code = MaybeCode.get();
7553 
7554   ASTRecordReader Record(*this, *Loc.F);
7555   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7556   if (!MaybeRecCode) {
7557     Error(MaybeRecCode.takeError());
7558     return nullptr;
7559   }
7560   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7561     Error("malformed AST file: missing C++ ctor initializers");
7562     return nullptr;
7563   }
7564 
7565   return Record.readCXXCtorInitializers();
7566 }
7567 
7568 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7569   assert(ContextObj && "reading base specifiers with no AST context");
7570   ASTContext &Context = *ContextObj;
7571 
7572   RecordLocation Loc = getLocalBitOffset(Offset);
7573   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7574   SavedStreamPosition SavedPosition(Cursor);
7575   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7576     Error(std::move(Err));
7577     return nullptr;
7578   }
7579   ReadingKindTracker ReadingKind(Read_Decl, *this);
7580   Deserializing D(this);
7581 
7582   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7583   if (!MaybeCode) {
7584     Error(MaybeCode.takeError());
7585     return nullptr;
7586   }
7587   unsigned Code = MaybeCode.get();
7588 
7589   ASTRecordReader Record(*this, *Loc.F);
7590   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7591   if (!MaybeRecCode) {
7592     Error(MaybeCode.takeError());
7593     return nullptr;
7594   }
7595   unsigned RecCode = MaybeRecCode.get();
7596 
7597   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7598     Error("malformed AST file: missing C++ base specifiers");
7599     return nullptr;
7600   }
7601 
7602   unsigned NumBases = Record.readInt();
7603   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7604   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7605   for (unsigned I = 0; I != NumBases; ++I)
7606     Bases[I] = Record.readCXXBaseSpecifier();
7607   return Bases;
7608 }
7609 
7610 serialization::DeclID
7611 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7612   if (LocalID < NUM_PREDEF_DECL_IDS)
7613     return LocalID;
7614 
7615   if (!F.ModuleOffsetMap.empty())
7616     ReadModuleOffsetMap(F);
7617 
7618   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7619     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7620   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7621 
7622   return LocalID + I->second;
7623 }
7624 
7625 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7626                                    ModuleFile &M) const {
7627   // Predefined decls aren't from any module.
7628   if (ID < NUM_PREDEF_DECL_IDS)
7629     return false;
7630 
7631   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7632          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7633 }
7634 
7635 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7636   if (!D->isFromASTFile())
7637     return nullptr;
7638   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7639   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7640   return I->second;
7641 }
7642 
7643 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7644   if (ID < NUM_PREDEF_DECL_IDS)
7645     return SourceLocation();
7646 
7647   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7648 
7649   if (Index > DeclsLoaded.size()) {
7650     Error("declaration ID out-of-range for AST file");
7651     return SourceLocation();
7652   }
7653 
7654   if (Decl *D = DeclsLoaded[Index])
7655     return D->getLocation();
7656 
7657   SourceLocation Loc;
7658   DeclCursorForID(ID, Loc);
7659   return Loc;
7660 }
7661 
7662 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7663   switch (ID) {
7664   case PREDEF_DECL_NULL_ID:
7665     return nullptr;
7666 
7667   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7668     return Context.getTranslationUnitDecl();
7669 
7670   case PREDEF_DECL_OBJC_ID_ID:
7671     return Context.getObjCIdDecl();
7672 
7673   case PREDEF_DECL_OBJC_SEL_ID:
7674     return Context.getObjCSelDecl();
7675 
7676   case PREDEF_DECL_OBJC_CLASS_ID:
7677     return Context.getObjCClassDecl();
7678 
7679   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7680     return Context.getObjCProtocolDecl();
7681 
7682   case PREDEF_DECL_INT_128_ID:
7683     return Context.getInt128Decl();
7684 
7685   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7686     return Context.getUInt128Decl();
7687 
7688   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7689     return Context.getObjCInstanceTypeDecl();
7690 
7691   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7692     return Context.getBuiltinVaListDecl();
7693 
7694   case PREDEF_DECL_VA_LIST_TAG:
7695     return Context.getVaListTagDecl();
7696 
7697   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7698     return Context.getBuiltinMSVaListDecl();
7699 
7700   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7701     return Context.getMSGuidTagDecl();
7702 
7703   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7704     return Context.getExternCContextDecl();
7705 
7706   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7707     return Context.getMakeIntegerSeqDecl();
7708 
7709   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7710     return Context.getCFConstantStringDecl();
7711 
7712   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7713     return Context.getCFConstantStringTagDecl();
7714 
7715   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7716     return Context.getTypePackElementDecl();
7717   }
7718   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7719 }
7720 
7721 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7722   assert(ContextObj && "reading decl with no AST context");
7723   if (ID < NUM_PREDEF_DECL_IDS) {
7724     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7725     if (D) {
7726       // Track that we have merged the declaration with ID \p ID into the
7727       // pre-existing predefined declaration \p D.
7728       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7729       if (Merged.empty())
7730         Merged.push_back(ID);
7731     }
7732     return D;
7733   }
7734 
7735   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7736 
7737   if (Index >= DeclsLoaded.size()) {
7738     assert(0 && "declaration ID out-of-range for AST file");
7739     Error("declaration ID out-of-range for AST file");
7740     return nullptr;
7741   }
7742 
7743   return DeclsLoaded[Index];
7744 }
7745 
7746 Decl *ASTReader::GetDecl(DeclID ID) {
7747   if (ID < NUM_PREDEF_DECL_IDS)
7748     return GetExistingDecl(ID);
7749 
7750   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7751 
7752   if (Index >= DeclsLoaded.size()) {
7753     assert(0 && "declaration ID out-of-range for AST file");
7754     Error("declaration ID out-of-range for AST file");
7755     return nullptr;
7756   }
7757 
7758   if (!DeclsLoaded[Index]) {
7759     ReadDeclRecord(ID);
7760     if (DeserializationListener)
7761       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7762   }
7763 
7764   return DeclsLoaded[Index];
7765 }
7766 
7767 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7768                                                   DeclID GlobalID) {
7769   if (GlobalID < NUM_PREDEF_DECL_IDS)
7770     return GlobalID;
7771 
7772   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7773   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7774   ModuleFile *Owner = I->second;
7775 
7776   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7777     = M.GlobalToLocalDeclIDs.find(Owner);
7778   if (Pos == M.GlobalToLocalDeclIDs.end())
7779     return 0;
7780 
7781   return GlobalID - Owner->BaseDeclID + Pos->second;
7782 }
7783 
7784 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7785                                             const RecordData &Record,
7786                                             unsigned &Idx) {
7787   if (Idx >= Record.size()) {
7788     Error("Corrupted AST file");
7789     return 0;
7790   }
7791 
7792   return getGlobalDeclID(F, Record[Idx++]);
7793 }
7794 
7795 /// Resolve the offset of a statement into a statement.
7796 ///
7797 /// This operation will read a new statement from the external
7798 /// source each time it is called, and is meant to be used via a
7799 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7800 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7801   // Switch case IDs are per Decl.
7802   ClearSwitchCaseIDs();
7803 
7804   // Offset here is a global offset across the entire chain.
7805   RecordLocation Loc = getLocalBitOffset(Offset);
7806   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7807     Error(std::move(Err));
7808     return nullptr;
7809   }
7810   assert(NumCurrentElementsDeserializing == 0 &&
7811          "should not be called while already deserializing");
7812   Deserializing D(this);
7813   return ReadStmtFromStream(*Loc.F);
7814 }
7815 
7816 void ASTReader::FindExternalLexicalDecls(
7817     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7818     SmallVectorImpl<Decl *> &Decls) {
7819   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7820 
7821   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7822     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7823     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7824       auto K = (Decl::Kind)+LexicalDecls[I];
7825       if (!IsKindWeWant(K))
7826         continue;
7827 
7828       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7829 
7830       // Don't add predefined declarations to the lexical context more
7831       // than once.
7832       if (ID < NUM_PREDEF_DECL_IDS) {
7833         if (PredefsVisited[ID])
7834           continue;
7835 
7836         PredefsVisited[ID] = true;
7837       }
7838 
7839       if (Decl *D = GetLocalDecl(*M, ID)) {
7840         assert(D->getKind() == K && "wrong kind for lexical decl");
7841         if (!DC->isDeclInLexicalTraversal(D))
7842           Decls.push_back(D);
7843       }
7844     }
7845   };
7846 
7847   if (isa<TranslationUnitDecl>(DC)) {
7848     for (const auto &Lexical : TULexicalDecls)
7849       Visit(Lexical.first, Lexical.second);
7850   } else {
7851     auto I = LexicalDecls.find(DC);
7852     if (I != LexicalDecls.end())
7853       Visit(I->second.first, I->second.second);
7854   }
7855 
7856   ++NumLexicalDeclContextsRead;
7857 }
7858 
7859 namespace {
7860 
7861 class DeclIDComp {
7862   ASTReader &Reader;
7863   ModuleFile &Mod;
7864 
7865 public:
7866   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7867 
7868   bool operator()(LocalDeclID L, LocalDeclID R) const {
7869     SourceLocation LHS = getLocation(L);
7870     SourceLocation RHS = getLocation(R);
7871     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7872   }
7873 
7874   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7875     SourceLocation RHS = getLocation(R);
7876     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7877   }
7878 
7879   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7880     SourceLocation LHS = getLocation(L);
7881     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7882   }
7883 
7884   SourceLocation getLocation(LocalDeclID ID) const {
7885     return Reader.getSourceManager().getFileLoc(
7886             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7887   }
7888 };
7889 
7890 } // namespace
7891 
7892 void ASTReader::FindFileRegionDecls(FileID File,
7893                                     unsigned Offset, unsigned Length,
7894                                     SmallVectorImpl<Decl *> &Decls) {
7895   SourceManager &SM = getSourceManager();
7896 
7897   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7898   if (I == FileDeclIDs.end())
7899     return;
7900 
7901   FileDeclsInfo &DInfo = I->second;
7902   if (DInfo.Decls.empty())
7903     return;
7904 
7905   SourceLocation
7906     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7907   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7908 
7909   DeclIDComp DIDComp(*this, *DInfo.Mod);
7910   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7911       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7912   if (BeginIt != DInfo.Decls.begin())
7913     --BeginIt;
7914 
7915   // If we are pointing at a top-level decl inside an objc container, we need
7916   // to backtrack until we find it otherwise we will fail to report that the
7917   // region overlaps with an objc container.
7918   while (BeginIt != DInfo.Decls.begin() &&
7919          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7920              ->isTopLevelDeclInObjCContainer())
7921     --BeginIt;
7922 
7923   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7924       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7925   if (EndIt != DInfo.Decls.end())
7926     ++EndIt;
7927 
7928   for (ArrayRef<serialization::LocalDeclID>::iterator
7929          DIt = BeginIt; DIt != EndIt; ++DIt)
7930     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7931 }
7932 
7933 bool
7934 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7935                                           DeclarationName Name) {
7936   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7937          "DeclContext has no visible decls in storage");
7938   if (!Name)
7939     return false;
7940 
7941   auto It = Lookups.find(DC);
7942   if (It == Lookups.end())
7943     return false;
7944 
7945   Deserializing LookupResults(this);
7946 
7947   // Load the list of declarations.
7948   SmallVector<NamedDecl *, 64> Decls;
7949   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7950   for (DeclID ID : It->second.Table.find(Name)) {
7951     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7952     if (ND->getDeclName() == Name && Found.insert(ND).second)
7953       Decls.push_back(ND);
7954   }
7955 
7956   ++NumVisibleDeclContextsRead;
7957   SetExternalVisibleDeclsForName(DC, Name, Decls);
7958   return !Decls.empty();
7959 }
7960 
7961 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7962   if (!DC->hasExternalVisibleStorage())
7963     return;
7964 
7965   auto It = Lookups.find(DC);
7966   assert(It != Lookups.end() &&
7967          "have external visible storage but no lookup tables");
7968 
7969   DeclsMap Decls;
7970 
7971   for (DeclID ID : It->second.Table.findAll()) {
7972     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7973     Decls[ND->getDeclName()].push_back(ND);
7974   }
7975 
7976   ++NumVisibleDeclContextsRead;
7977 
7978   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7979     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7980   }
7981   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7982 }
7983 
7984 const serialization::reader::DeclContextLookupTable *
7985 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7986   auto I = Lookups.find(Primary);
7987   return I == Lookups.end() ? nullptr : &I->second;
7988 }
7989 
7990 /// Under non-PCH compilation the consumer receives the objc methods
7991 /// before receiving the implementation, and codegen depends on this.
7992 /// We simulate this by deserializing and passing to consumer the methods of the
7993 /// implementation before passing the deserialized implementation decl.
7994 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7995                                        ASTConsumer *Consumer) {
7996   assert(ImplD && Consumer);
7997 
7998   for (auto *I : ImplD->methods())
7999     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8000 
8001   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8002 }
8003 
8004 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8005   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8006     PassObjCImplDeclToConsumer(ImplD, Consumer);
8007   else
8008     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8009 }
8010 
8011 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8012   this->Consumer = Consumer;
8013 
8014   if (Consumer)
8015     PassInterestingDeclsToConsumer();
8016 
8017   if (DeserializationListener)
8018     DeserializationListener->ReaderInitialized(this);
8019 }
8020 
8021 void ASTReader::PrintStats() {
8022   std::fprintf(stderr, "*** AST File Statistics:\n");
8023 
8024   unsigned NumTypesLoaded =
8025       TypesLoaded.size() - llvm::count(TypesLoaded.materialized(), QualType());
8026   unsigned NumDeclsLoaded =
8027       DeclsLoaded.size() -
8028       llvm::count(DeclsLoaded.materialized(), (Decl *)nullptr);
8029   unsigned NumIdentifiersLoaded =
8030       IdentifiersLoaded.size() -
8031       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
8032   unsigned NumMacrosLoaded =
8033       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
8034   unsigned NumSelectorsLoaded =
8035       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
8036 
8037   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8038     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8039                  NumSLocEntriesRead, TotalNumSLocEntries,
8040                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8041   if (!TypesLoaded.empty())
8042     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8043                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8044                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8045   if (!DeclsLoaded.empty())
8046     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8047                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8048                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8049   if (!IdentifiersLoaded.empty())
8050     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8051                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8052                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8053   if (!MacrosLoaded.empty())
8054     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8055                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8056                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8057   if (!SelectorsLoaded.empty())
8058     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8059                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8060                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8061   if (TotalNumStatements)
8062     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8063                  NumStatementsRead, TotalNumStatements,
8064                  ((float)NumStatementsRead/TotalNumStatements * 100));
8065   if (TotalNumMacros)
8066     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8067                  NumMacrosRead, TotalNumMacros,
8068                  ((float)NumMacrosRead/TotalNumMacros * 100));
8069   if (TotalLexicalDeclContexts)
8070     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8071                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8072                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8073                   * 100));
8074   if (TotalVisibleDeclContexts)
8075     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8076                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8077                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8078                   * 100));
8079   if (TotalNumMethodPoolEntries)
8080     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8081                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8082                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8083                   * 100));
8084   if (NumMethodPoolLookups)
8085     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8086                  NumMethodPoolHits, NumMethodPoolLookups,
8087                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8088   if (NumMethodPoolTableLookups)
8089     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8090                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8091                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8092                   * 100.0));
8093   if (NumIdentifierLookupHits)
8094     std::fprintf(stderr,
8095                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8096                  NumIdentifierLookupHits, NumIdentifierLookups,
8097                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8098 
8099   if (GlobalIndex) {
8100     std::fprintf(stderr, "\n");
8101     GlobalIndex->printStats();
8102   }
8103 
8104   std::fprintf(stderr, "\n");
8105   dump();
8106   std::fprintf(stderr, "\n");
8107 }
8108 
8109 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8110 LLVM_DUMP_METHOD static void
8111 dumpModuleIDMap(StringRef Name,
8112                 const ContinuousRangeMap<Key, ModuleFile *,
8113                                          InitialCapacity> &Map) {
8114   if (Map.begin() == Map.end())
8115     return;
8116 
8117   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8118 
8119   llvm::errs() << Name << ":\n";
8120   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8121        I != IEnd; ++I) {
8122     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8123       << "\n";
8124   }
8125 }
8126 
8127 LLVM_DUMP_METHOD void ASTReader::dump() {
8128   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8129   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8130   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8131   dumpModuleIDMap("Global type map", GlobalTypeMap);
8132   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8133   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8134   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8135   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8136   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8137   dumpModuleIDMap("Global preprocessed entity map",
8138                   GlobalPreprocessedEntityMap);
8139 
8140   llvm::errs() << "\n*** PCH/Modules Loaded:";
8141   for (ModuleFile &M : ModuleMgr)
8142     M.dump();
8143 }
8144 
8145 /// Return the amount of memory used by memory buffers, breaking down
8146 /// by heap-backed versus mmap'ed memory.
8147 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8148   for (ModuleFile &I : ModuleMgr) {
8149     if (llvm::MemoryBuffer *buf = I.Buffer) {
8150       size_t bytes = buf->getBufferSize();
8151       switch (buf->getBufferKind()) {
8152         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8153           sizes.malloc_bytes += bytes;
8154           break;
8155         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8156           sizes.mmap_bytes += bytes;
8157           break;
8158       }
8159     }
8160   }
8161 }
8162 
8163 void ASTReader::InitializeSema(Sema &S) {
8164   SemaObj = &S;
8165   S.addExternalSource(this);
8166 
8167   // Makes sure any declarations that were deserialized "too early"
8168   // still get added to the identifier's declaration chains.
8169   for (uint64_t ID : PreloadedDeclIDs) {
8170     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8171     pushExternalDeclIntoScope(D, D->getDeclName());
8172   }
8173   PreloadedDeclIDs.clear();
8174 
8175   // FIXME: What happens if these are changed by a module import?
8176   if (!FPPragmaOptions.empty()) {
8177     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8178     FPOptionsOverride NewOverrides =
8179         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
8180     SemaObj->CurFPFeatures =
8181         NewOverrides.applyOverrides(SemaObj->getLangOpts());
8182   }
8183 
8184   SemaObj->OpenCLFeatures = OpenCLExtensions;
8185 
8186   UpdateSema();
8187 }
8188 
8189 void ASTReader::UpdateSema() {
8190   assert(SemaObj && "no Sema to update");
8191 
8192   // Load the offsets of the declarations that Sema references.
8193   // They will be lazily deserialized when needed.
8194   if (!SemaDeclRefs.empty()) {
8195     assert(SemaDeclRefs.size() % 3 == 0);
8196     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8197       if (!SemaObj->StdNamespace)
8198         SemaObj->StdNamespace = SemaDeclRefs[I];
8199       if (!SemaObj->StdBadAlloc)
8200         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8201       if (!SemaObj->StdAlignValT)
8202         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8203     }
8204     SemaDeclRefs.clear();
8205   }
8206 
8207   // Update the state of pragmas. Use the same API as if we had encountered the
8208   // pragma in the source.
8209   if(OptimizeOffPragmaLocation.isValid())
8210     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8211   if (PragmaMSStructState != -1)
8212     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8213   if (PointersToMembersPragmaLocation.isValid()) {
8214     SemaObj->ActOnPragmaMSPointersToMembers(
8215         (LangOptions::PragmaMSPointersToMembersKind)
8216             PragmaMSPointersToMembersState,
8217         PointersToMembersPragmaLocation);
8218   }
8219   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8220 
8221   if (PragmaAlignPackCurrentValue) {
8222     // The bottom of the stack might have a default value. It must be adjusted
8223     // to the current value to ensure that the packing state is preserved after
8224     // popping entries that were included/imported from a PCH/module.
8225     bool DropFirst = false;
8226     if (!PragmaAlignPackStack.empty() &&
8227         PragmaAlignPackStack.front().Location.isInvalid()) {
8228       assert(PragmaAlignPackStack.front().Value ==
8229                  SemaObj->AlignPackStack.DefaultValue &&
8230              "Expected a default alignment value");
8231       SemaObj->AlignPackStack.Stack.emplace_back(
8232           PragmaAlignPackStack.front().SlotLabel,
8233           SemaObj->AlignPackStack.CurrentValue,
8234           SemaObj->AlignPackStack.CurrentPragmaLocation,
8235           PragmaAlignPackStack.front().PushLocation);
8236       DropFirst = true;
8237     }
8238     for (const auto &Entry :
8239          llvm::ArrayRef(PragmaAlignPackStack).drop_front(DropFirst ? 1 : 0)) {
8240       SemaObj->AlignPackStack.Stack.emplace_back(
8241           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8242     }
8243     if (PragmaAlignPackCurrentLocation.isInvalid()) {
8244       assert(*PragmaAlignPackCurrentValue ==
8245                  SemaObj->AlignPackStack.DefaultValue &&
8246              "Expected a default align and pack value");
8247       // Keep the current values.
8248     } else {
8249       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8250       SemaObj->AlignPackStack.CurrentPragmaLocation =
8251           PragmaAlignPackCurrentLocation;
8252     }
8253   }
8254   if (FpPragmaCurrentValue) {
8255     // The bottom of the stack might have a default value. It must be adjusted
8256     // to the current value to ensure that fp-pragma state is preserved after
8257     // popping entries that were included/imported from a PCH/module.
8258     bool DropFirst = false;
8259     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8260       assert(FpPragmaStack.front().Value ==
8261                  SemaObj->FpPragmaStack.DefaultValue &&
8262              "Expected a default pragma float_control value");
8263       SemaObj->FpPragmaStack.Stack.emplace_back(
8264           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
8265           SemaObj->FpPragmaStack.CurrentPragmaLocation,
8266           FpPragmaStack.front().PushLocation);
8267       DropFirst = true;
8268     }
8269     for (const auto &Entry :
8270          llvm::ArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
8271       SemaObj->FpPragmaStack.Stack.emplace_back(
8272           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
8273     if (FpPragmaCurrentLocation.isInvalid()) {
8274       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8275              "Expected a default pragma float_control value");
8276       // Keep the current values.
8277     } else {
8278       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8279       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8280     }
8281   }
8282 
8283   // For non-modular AST files, restore visiblity of modules.
8284   for (auto &Import : PendingImportedModulesSema) {
8285     if (Import.ImportLoc.isInvalid())
8286       continue;
8287     if (Module *Imported = getSubmodule(Import.ID)) {
8288       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8289     }
8290   }
8291   PendingImportedModulesSema.clear();
8292 }
8293 
8294 IdentifierInfo *ASTReader::get(StringRef Name) {
8295   // Note that we are loading an identifier.
8296   Deserializing AnIdentifier(this);
8297 
8298   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8299                                   NumIdentifierLookups,
8300                                   NumIdentifierLookupHits);
8301 
8302   // We don't need to do identifier table lookups in C++ modules (we preload
8303   // all interesting declarations, and don't need to use the scope for name
8304   // lookups). Perform the lookup in PCH files, though, since we don't build
8305   // a complete initial identifier table if we're carrying on from a PCH.
8306   if (PP.getLangOpts().CPlusPlus) {
8307     for (auto *F : ModuleMgr.pch_modules())
8308       if (Visitor(*F))
8309         break;
8310   } else {
8311     // If there is a global index, look there first to determine which modules
8312     // provably do not have any results for this identifier.
8313     GlobalModuleIndex::HitSet Hits;
8314     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8315     if (!loadGlobalIndex()) {
8316       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8317         HitsPtr = &Hits;
8318       }
8319     }
8320 
8321     ModuleMgr.visit(Visitor, HitsPtr);
8322   }
8323 
8324   IdentifierInfo *II = Visitor.getIdentifierInfo();
8325   markIdentifierUpToDate(II);
8326   return II;
8327 }
8328 
8329 namespace clang {
8330 
8331   /// An identifier-lookup iterator that enumerates all of the
8332   /// identifiers stored within a set of AST files.
8333   class ASTIdentifierIterator : public IdentifierIterator {
8334     /// The AST reader whose identifiers are being enumerated.
8335     const ASTReader &Reader;
8336 
8337     /// The current index into the chain of AST files stored in
8338     /// the AST reader.
8339     unsigned Index;
8340 
8341     /// The current position within the identifier lookup table
8342     /// of the current AST file.
8343     ASTIdentifierLookupTable::key_iterator Current;
8344 
8345     /// The end position within the identifier lookup table of
8346     /// the current AST file.
8347     ASTIdentifierLookupTable::key_iterator End;
8348 
8349     /// Whether to skip any modules in the ASTReader.
8350     bool SkipModules;
8351 
8352   public:
8353     explicit ASTIdentifierIterator(const ASTReader &Reader,
8354                                    bool SkipModules = false);
8355 
8356     StringRef Next() override;
8357   };
8358 
8359 } // namespace clang
8360 
8361 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8362                                              bool SkipModules)
8363     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8364 }
8365 
8366 StringRef ASTIdentifierIterator::Next() {
8367   while (Current == End) {
8368     // If we have exhausted all of our AST files, we're done.
8369     if (Index == 0)
8370       return StringRef();
8371 
8372     --Index;
8373     ModuleFile &F = Reader.ModuleMgr[Index];
8374     if (SkipModules && F.isModule())
8375       continue;
8376 
8377     ASTIdentifierLookupTable *IdTable =
8378         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8379     Current = IdTable->key_begin();
8380     End = IdTable->key_end();
8381   }
8382 
8383   // We have any identifiers remaining in the current AST file; return
8384   // the next one.
8385   StringRef Result = *Current;
8386   ++Current;
8387   return Result;
8388 }
8389 
8390 namespace {
8391 
8392 /// A utility for appending two IdentifierIterators.
8393 class ChainedIdentifierIterator : public IdentifierIterator {
8394   std::unique_ptr<IdentifierIterator> Current;
8395   std::unique_ptr<IdentifierIterator> Queued;
8396 
8397 public:
8398   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8399                             std::unique_ptr<IdentifierIterator> Second)
8400       : Current(std::move(First)), Queued(std::move(Second)) {}
8401 
8402   StringRef Next() override {
8403     if (!Current)
8404       return StringRef();
8405 
8406     StringRef result = Current->Next();
8407     if (!result.empty())
8408       return result;
8409 
8410     // Try the queued iterator, which may itself be empty.
8411     Current.reset();
8412     std::swap(Current, Queued);
8413     return Next();
8414   }
8415 };
8416 
8417 } // namespace
8418 
8419 IdentifierIterator *ASTReader::getIdentifiers() {
8420   if (!loadGlobalIndex()) {
8421     std::unique_ptr<IdentifierIterator> ReaderIter(
8422         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8423     std::unique_ptr<IdentifierIterator> ModulesIter(
8424         GlobalIndex->createIdentifierIterator());
8425     return new ChainedIdentifierIterator(std::move(ReaderIter),
8426                                          std::move(ModulesIter));
8427   }
8428 
8429   return new ASTIdentifierIterator(*this);
8430 }
8431 
8432 namespace clang {
8433 namespace serialization {
8434 
8435   class ReadMethodPoolVisitor {
8436     ASTReader &Reader;
8437     Selector Sel;
8438     unsigned PriorGeneration;
8439     unsigned InstanceBits = 0;
8440     unsigned FactoryBits = 0;
8441     bool InstanceHasMoreThanOneDecl = false;
8442     bool FactoryHasMoreThanOneDecl = false;
8443     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8444     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8445 
8446   public:
8447     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8448                           unsigned PriorGeneration)
8449         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8450 
8451     bool operator()(ModuleFile &M) {
8452       if (!M.SelectorLookupTable)
8453         return false;
8454 
8455       // If we've already searched this module file, skip it now.
8456       if (M.Generation <= PriorGeneration)
8457         return true;
8458 
8459       ++Reader.NumMethodPoolTableLookups;
8460       ASTSelectorLookupTable *PoolTable
8461         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8462       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8463       if (Pos == PoolTable->end())
8464         return false;
8465 
8466       ++Reader.NumMethodPoolTableHits;
8467       ++Reader.NumSelectorsRead;
8468       // FIXME: Not quite happy with the statistics here. We probably should
8469       // disable this tracking when called via LoadSelector.
8470       // Also, should entries without methods count as misses?
8471       ++Reader.NumMethodPoolEntriesRead;
8472       ASTSelectorLookupTrait::data_type Data = *Pos;
8473       if (Reader.DeserializationListener)
8474         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8475 
8476       // Append methods in the reverse order, so that later we can process them
8477       // in the order they appear in the source code by iterating through
8478       // the vector in the reverse order.
8479       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8480       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8481       InstanceBits = Data.InstanceBits;
8482       FactoryBits = Data.FactoryBits;
8483       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8484       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8485       return false;
8486     }
8487 
8488     /// Retrieve the instance methods found by this visitor.
8489     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8490       return InstanceMethods;
8491     }
8492 
8493     /// Retrieve the instance methods found by this visitor.
8494     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8495       return FactoryMethods;
8496     }
8497 
8498     unsigned getInstanceBits() const { return InstanceBits; }
8499     unsigned getFactoryBits() const { return FactoryBits; }
8500 
8501     bool instanceHasMoreThanOneDecl() const {
8502       return InstanceHasMoreThanOneDecl;
8503     }
8504 
8505     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8506   };
8507 
8508 } // namespace serialization
8509 } // namespace clang
8510 
8511 /// Add the given set of methods to the method list.
8512 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8513                              ObjCMethodList &List) {
8514   for (ObjCMethodDecl *M : llvm::reverse(Methods))
8515     S.addMethodToGlobalList(&List, M);
8516 }
8517 
8518 void ASTReader::ReadMethodPool(Selector Sel) {
8519   // Get the selector generation and update it to the current generation.
8520   unsigned &Generation = SelectorGeneration[Sel];
8521   unsigned PriorGeneration = Generation;
8522   Generation = getGeneration();
8523   SelectorOutOfDate[Sel] = false;
8524 
8525   // Search for methods defined with this selector.
8526   ++NumMethodPoolLookups;
8527   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8528   ModuleMgr.visit(Visitor);
8529 
8530   if (Visitor.getInstanceMethods().empty() &&
8531       Visitor.getFactoryMethods().empty())
8532     return;
8533 
8534   ++NumMethodPoolHits;
8535 
8536   if (!getSema())
8537     return;
8538 
8539   Sema &S = *getSema();
8540   Sema::GlobalMethodPool::iterator Pos =
8541       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8542           .first;
8543 
8544   Pos->second.first.setBits(Visitor.getInstanceBits());
8545   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8546   Pos->second.second.setBits(Visitor.getFactoryBits());
8547   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8548 
8549   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8550   // when building a module we keep every method individually and may need to
8551   // update hasMoreThanOneDecl as we add the methods.
8552   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8553   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8554 }
8555 
8556 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8557   if (SelectorOutOfDate[Sel])
8558     ReadMethodPool(Sel);
8559 }
8560 
8561 void ASTReader::ReadKnownNamespaces(
8562                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8563   Namespaces.clear();
8564 
8565   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8566     if (NamespaceDecl *Namespace
8567                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8568       Namespaces.push_back(Namespace);
8569   }
8570 }
8571 
8572 void ASTReader::ReadUndefinedButUsed(
8573     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8574   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8575     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8576     SourceLocation Loc =
8577         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8578     Undefined.insert(std::make_pair(D, Loc));
8579   }
8580 }
8581 
8582 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8583     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8584                                                      Exprs) {
8585   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8586     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8587     uint64_t Count = DelayedDeleteExprs[Idx++];
8588     for (uint64_t C = 0; C < Count; ++C) {
8589       SourceLocation DeleteLoc =
8590           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8591       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8592       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8593     }
8594   }
8595 }
8596 
8597 void ASTReader::ReadTentativeDefinitions(
8598                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8599   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8600     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8601     if (Var)
8602       TentativeDefs.push_back(Var);
8603   }
8604   TentativeDefinitions.clear();
8605 }
8606 
8607 void ASTReader::ReadUnusedFileScopedDecls(
8608                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8609   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8610     DeclaratorDecl *D
8611       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8612     if (D)
8613       Decls.push_back(D);
8614   }
8615   UnusedFileScopedDecls.clear();
8616 }
8617 
8618 void ASTReader::ReadDelegatingConstructors(
8619                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8620   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8621     CXXConstructorDecl *D
8622       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8623     if (D)
8624       Decls.push_back(D);
8625   }
8626   DelegatingCtorDecls.clear();
8627 }
8628 
8629 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8630   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8631     TypedefNameDecl *D
8632       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8633     if (D)
8634       Decls.push_back(D);
8635   }
8636   ExtVectorDecls.clear();
8637 }
8638 
8639 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8640     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8641   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8642        ++I) {
8643     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8644         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8645     if (D)
8646       Decls.insert(D);
8647   }
8648   UnusedLocalTypedefNameCandidates.clear();
8649 }
8650 
8651 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8652     llvm::SmallSetVector<Decl *, 4> &Decls) {
8653   for (auto I : DeclsToCheckForDeferredDiags) {
8654     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8655     if (D)
8656       Decls.insert(D);
8657   }
8658   DeclsToCheckForDeferredDiags.clear();
8659 }
8660 
8661 void ASTReader::ReadReferencedSelectors(
8662        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8663   if (ReferencedSelectorsData.empty())
8664     return;
8665 
8666   // If there are @selector references added them to its pool. This is for
8667   // implementation of -Wselector.
8668   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8669   unsigned I = 0;
8670   while (I < DataSize) {
8671     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8672     SourceLocation SelLoc
8673       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8674     Sels.push_back(std::make_pair(Sel, SelLoc));
8675   }
8676   ReferencedSelectorsData.clear();
8677 }
8678 
8679 void ASTReader::ReadWeakUndeclaredIdentifiers(
8680        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8681   if (WeakUndeclaredIdentifiers.empty())
8682     return;
8683 
8684   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8685     IdentifierInfo *WeakId
8686       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8687     IdentifierInfo *AliasId
8688       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8689     SourceLocation Loc =
8690         SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8691     WeakInfo WI(AliasId, Loc);
8692     WeakIDs.push_back(std::make_pair(WeakId, WI));
8693   }
8694   WeakUndeclaredIdentifiers.clear();
8695 }
8696 
8697 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8698   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8699     ExternalVTableUse VT;
8700     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8701     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8702     VT.DefinitionRequired = VTableUses[Idx++];
8703     VTables.push_back(VT);
8704   }
8705 
8706   VTableUses.clear();
8707 }
8708 
8709 void ASTReader::ReadPendingInstantiations(
8710        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8711   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8712     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8713     SourceLocation Loc
8714       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8715 
8716     Pending.push_back(std::make_pair(D, Loc));
8717   }
8718   PendingInstantiations.clear();
8719 }
8720 
8721 void ASTReader::ReadLateParsedTemplates(
8722     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8723         &LPTMap) {
8724   for (auto &LPT : LateParsedTemplates) {
8725     ModuleFile *FMod = LPT.first;
8726     RecordDataImpl &LateParsed = LPT.second;
8727     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8728          /* In loop */) {
8729       FunctionDecl *FD =
8730           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8731 
8732       auto LT = std::make_unique<LateParsedTemplate>();
8733       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8734       LT->FPO = FPOptions::getFromOpaqueInt(LateParsed[Idx++]);
8735 
8736       ModuleFile *F = getOwningModuleFile(LT->D);
8737       assert(F && "No module");
8738 
8739       unsigned TokN = LateParsed[Idx++];
8740       LT->Toks.reserve(TokN);
8741       for (unsigned T = 0; T < TokN; ++T)
8742         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8743 
8744       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8745     }
8746   }
8747 
8748   LateParsedTemplates.clear();
8749 }
8750 
8751 void ASTReader::AssignedLambdaNumbering(const CXXRecordDecl *Lambda) {
8752   if (Lambda->getLambdaContextDecl()) {
8753     // Keep track of this lambda so it can be merged with another lambda that
8754     // is loaded later.
8755     LambdaDeclarationsForMerging.insert(
8756         {{Lambda->getLambdaContextDecl()->getCanonicalDecl(),
8757           Lambda->getLambdaIndexInContext()},
8758          const_cast<CXXRecordDecl *>(Lambda)});
8759   }
8760 }
8761 
8762 void ASTReader::LoadSelector(Selector Sel) {
8763   // It would be complicated to avoid reading the methods anyway. So don't.
8764   ReadMethodPool(Sel);
8765 }
8766 
8767 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8768   assert(ID && "Non-zero identifier ID required");
8769   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8770   IdentifiersLoaded[ID - 1] = II;
8771   if (DeserializationListener)
8772     DeserializationListener->IdentifierRead(ID, II);
8773 }
8774 
8775 /// Set the globally-visible declarations associated with the given
8776 /// identifier.
8777 ///
8778 /// If the AST reader is currently in a state where the given declaration IDs
8779 /// cannot safely be resolved, they are queued until it is safe to resolve
8780 /// them.
8781 ///
8782 /// \param II an IdentifierInfo that refers to one or more globally-visible
8783 /// declarations.
8784 ///
8785 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8786 /// visible at global scope.
8787 ///
8788 /// \param Decls if non-null, this vector will be populated with the set of
8789 /// deserialized declarations. These declarations will not be pushed into
8790 /// scope.
8791 void
8792 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8793                               const SmallVectorImpl<uint32_t> &DeclIDs,
8794                                    SmallVectorImpl<Decl *> *Decls) {
8795   if (NumCurrentElementsDeserializing && !Decls) {
8796     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8797     return;
8798   }
8799 
8800   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8801     if (!SemaObj) {
8802       // Queue this declaration so that it will be added to the
8803       // translation unit scope and identifier's declaration chain
8804       // once a Sema object is known.
8805       PreloadedDeclIDs.push_back(DeclIDs[I]);
8806       continue;
8807     }
8808 
8809     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8810 
8811     // If we're simply supposed to record the declarations, do so now.
8812     if (Decls) {
8813       Decls->push_back(D);
8814       continue;
8815     }
8816 
8817     // Introduce this declaration into the translation-unit scope
8818     // and add it to the declaration chain for this identifier, so
8819     // that (unqualified) name lookup will find it.
8820     pushExternalDeclIntoScope(D, II);
8821   }
8822 }
8823 
8824 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8825   if (ID == 0)
8826     return nullptr;
8827 
8828   if (IdentifiersLoaded.empty()) {
8829     Error("no identifier table in AST file");
8830     return nullptr;
8831   }
8832 
8833   ID -= 1;
8834   if (!IdentifiersLoaded[ID]) {
8835     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8836     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8837     ModuleFile *M = I->second;
8838     unsigned Index = ID - M->BaseIdentifierID;
8839     const unsigned char *Data =
8840         M->IdentifierTableData + M->IdentifierOffsets[Index];
8841 
8842     ASTIdentifierLookupTrait Trait(*this, *M);
8843     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8844     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8845     auto &II = PP.getIdentifierTable().get(Key);
8846     IdentifiersLoaded[ID] = &II;
8847     markIdentifierFromAST(*this,  II);
8848     if (DeserializationListener)
8849       DeserializationListener->IdentifierRead(ID + 1, &II);
8850   }
8851 
8852   return IdentifiersLoaded[ID];
8853 }
8854 
8855 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8856   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8857 }
8858 
8859 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8860   if (LocalID < NUM_PREDEF_IDENT_IDS)
8861     return LocalID;
8862 
8863   if (!M.ModuleOffsetMap.empty())
8864     ReadModuleOffsetMap(M);
8865 
8866   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8867     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8868   assert(I != M.IdentifierRemap.end()
8869          && "Invalid index into identifier index remap");
8870 
8871   return LocalID + I->second;
8872 }
8873 
8874 MacroInfo *ASTReader::getMacro(MacroID ID) {
8875   if (ID == 0)
8876     return nullptr;
8877 
8878   if (MacrosLoaded.empty()) {
8879     Error("no macro table in AST file");
8880     return nullptr;
8881   }
8882 
8883   ID -= NUM_PREDEF_MACRO_IDS;
8884   if (!MacrosLoaded[ID]) {
8885     GlobalMacroMapType::iterator I
8886       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8887     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8888     ModuleFile *M = I->second;
8889     unsigned Index = ID - M->BaseMacroID;
8890     MacrosLoaded[ID] =
8891         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8892 
8893     if (DeserializationListener)
8894       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8895                                          MacrosLoaded[ID]);
8896   }
8897 
8898   return MacrosLoaded[ID];
8899 }
8900 
8901 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8902   if (LocalID < NUM_PREDEF_MACRO_IDS)
8903     return LocalID;
8904 
8905   if (!M.ModuleOffsetMap.empty())
8906     ReadModuleOffsetMap(M);
8907 
8908   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8909     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8910   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8911 
8912   return LocalID + I->second;
8913 }
8914 
8915 serialization::SubmoduleID
8916 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8917   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8918     return LocalID;
8919 
8920   if (!M.ModuleOffsetMap.empty())
8921     ReadModuleOffsetMap(M);
8922 
8923   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8924     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8925   assert(I != M.SubmoduleRemap.end()
8926          && "Invalid index into submodule index remap");
8927 
8928   return LocalID + I->second;
8929 }
8930 
8931 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8932   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8933     assert(GlobalID == 0 && "Unhandled global submodule ID");
8934     return nullptr;
8935   }
8936 
8937   if (GlobalID > SubmodulesLoaded.size()) {
8938     Error("submodule ID out of range in AST file");
8939     return nullptr;
8940   }
8941 
8942   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8943 }
8944 
8945 Module *ASTReader::getModule(unsigned ID) {
8946   return getSubmodule(ID);
8947 }
8948 
8949 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) {
8950   if (ID & 1) {
8951     // It's a module, look it up by submodule ID.
8952     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(M, ID >> 1));
8953     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8954   } else {
8955     // It's a prefix (preamble, PCH, ...). Look it up by index.
8956     unsigned IndexFromEnd = ID >> 1;
8957     assert(IndexFromEnd && "got reference to unknown module file");
8958     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8959   }
8960 }
8961 
8962 unsigned ASTReader::getModuleFileID(ModuleFile *M) {
8963   if (!M)
8964     return 1;
8965 
8966   // For a file representing a module, use the submodule ID of the top-level
8967   // module as the file ID. For any other kind of file, the number of such
8968   // files loaded beforehand will be the same on reload.
8969   // FIXME: Is this true even if we have an explicit module file and a PCH?
8970   if (M->isModule())
8971     return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8972 
8973   auto PCHModules = getModuleManager().pch_modules();
8974   auto I = llvm::find(PCHModules, M);
8975   assert(I != PCHModules.end() && "emitting reference to unknown file");
8976   return (I - PCHModules.end()) << 1;
8977 }
8978 
8979 std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
8980   if (Module *M = getSubmodule(ID))
8981     return ASTSourceDescriptor(*M);
8982 
8983   // If there is only a single PCH, return it instead.
8984   // Chained PCH are not supported.
8985   const auto &PCHChain = ModuleMgr.pch_modules();
8986   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8987     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8988     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8989     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8990     return ASTSourceDescriptor(ModuleName,
8991                                llvm::sys::path::parent_path(MF.FileName),
8992                                FileName, MF.Signature);
8993   }
8994   return std::nullopt;
8995 }
8996 
8997 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8998   auto I = DefinitionSource.find(FD);
8999   if (I == DefinitionSource.end())
9000     return EK_ReplyHazy;
9001   return I->second ? EK_Never : EK_Always;
9002 }
9003 
9004 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9005   return DecodeSelector(getGlobalSelectorID(M, LocalID));
9006 }
9007 
9008 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9009   if (ID == 0)
9010     return Selector();
9011 
9012   if (ID > SelectorsLoaded.size()) {
9013     Error("selector ID out of range in AST file");
9014     return Selector();
9015   }
9016 
9017   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9018     // Load this selector from the selector table.
9019     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9020     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9021     ModuleFile &M = *I->second;
9022     ASTSelectorLookupTrait Trait(*this, M);
9023     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9024     SelectorsLoaded[ID - 1] =
9025       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9026     if (DeserializationListener)
9027       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9028   }
9029 
9030   return SelectorsLoaded[ID - 1];
9031 }
9032 
9033 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9034   return DecodeSelector(ID);
9035 }
9036 
9037 uint32_t ASTReader::GetNumExternalSelectors() {
9038   // ID 0 (the null selector) is considered an external selector.
9039   return getTotalNumSelectors() + 1;
9040 }
9041 
9042 serialization::SelectorID
9043 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9044   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9045     return LocalID;
9046 
9047   if (!M.ModuleOffsetMap.empty())
9048     ReadModuleOffsetMap(M);
9049 
9050   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9051     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9052   assert(I != M.SelectorRemap.end()
9053          && "Invalid index into selector index remap");
9054 
9055   return LocalID + I->second;
9056 }
9057 
9058 DeclarationNameLoc
9059 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9060   switch (Name.getNameKind()) {
9061   case DeclarationName::CXXConstructorName:
9062   case DeclarationName::CXXDestructorName:
9063   case DeclarationName::CXXConversionFunctionName:
9064     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
9065 
9066   case DeclarationName::CXXOperatorName:
9067     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
9068 
9069   case DeclarationName::CXXLiteralOperatorName:
9070     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9071         readSourceLocation());
9072 
9073   case DeclarationName::Identifier:
9074   case DeclarationName::ObjCZeroArgSelector:
9075   case DeclarationName::ObjCOneArgSelector:
9076   case DeclarationName::ObjCMultiArgSelector:
9077   case DeclarationName::CXXUsingDirective:
9078   case DeclarationName::CXXDeductionGuideName:
9079     break;
9080   }
9081   return DeclarationNameLoc();
9082 }
9083 
9084 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9085   DeclarationNameInfo NameInfo;
9086   NameInfo.setName(readDeclarationName());
9087   NameInfo.setLoc(readSourceLocation());
9088   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
9089   return NameInfo;
9090 }
9091 
9092 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9093   Info.QualifierLoc = readNestedNameSpecifierLoc();
9094   unsigned NumTPLists = readInt();
9095   Info.NumTemplParamLists = NumTPLists;
9096   if (NumTPLists) {
9097     Info.TemplParamLists =
9098         new (getContext()) TemplateParameterList *[NumTPLists];
9099     for (unsigned i = 0; i != NumTPLists; ++i)
9100       Info.TemplParamLists[i] = readTemplateParameterList();
9101   }
9102 }
9103 
9104 TemplateParameterList *
9105 ASTRecordReader::readTemplateParameterList() {
9106   SourceLocation TemplateLoc = readSourceLocation();
9107   SourceLocation LAngleLoc = readSourceLocation();
9108   SourceLocation RAngleLoc = readSourceLocation();
9109 
9110   unsigned NumParams = readInt();
9111   SmallVector<NamedDecl *, 16> Params;
9112   Params.reserve(NumParams);
9113   while (NumParams--)
9114     Params.push_back(readDeclAs<NamedDecl>());
9115 
9116   bool HasRequiresClause = readBool();
9117   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9118 
9119   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9120       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9121   return TemplateParams;
9122 }
9123 
9124 void ASTRecordReader::readTemplateArgumentList(
9125                         SmallVectorImpl<TemplateArgument> &TemplArgs,
9126                         bool Canonicalize) {
9127   unsigned NumTemplateArgs = readInt();
9128   TemplArgs.reserve(NumTemplateArgs);
9129   while (NumTemplateArgs--)
9130     TemplArgs.push_back(readTemplateArgument(Canonicalize));
9131 }
9132 
9133 /// Read a UnresolvedSet structure.
9134 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9135   unsigned NumDecls = readInt();
9136   Set.reserve(getContext(), NumDecls);
9137   while (NumDecls--) {
9138     DeclID ID = readDeclID();
9139     AccessSpecifier AS = (AccessSpecifier) readInt();
9140     Set.addLazyDecl(getContext(), ID, AS);
9141   }
9142 }
9143 
9144 CXXBaseSpecifier
9145 ASTRecordReader::readCXXBaseSpecifier() {
9146   bool isVirtual = readBool();
9147   bool isBaseOfClass = readBool();
9148   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9149   bool inheritConstructors = readBool();
9150   TypeSourceInfo *TInfo = readTypeSourceInfo();
9151   SourceRange Range = readSourceRange();
9152   SourceLocation EllipsisLoc = readSourceLocation();
9153   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9154                           EllipsisLoc);
9155   Result.setInheritConstructors(inheritConstructors);
9156   return Result;
9157 }
9158 
9159 CXXCtorInitializer **
9160 ASTRecordReader::readCXXCtorInitializers() {
9161   ASTContext &Context = getContext();
9162   unsigned NumInitializers = readInt();
9163   assert(NumInitializers && "wrote ctor initializers but have no inits");
9164   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9165   for (unsigned i = 0; i != NumInitializers; ++i) {
9166     TypeSourceInfo *TInfo = nullptr;
9167     bool IsBaseVirtual = false;
9168     FieldDecl *Member = nullptr;
9169     IndirectFieldDecl *IndirectMember = nullptr;
9170 
9171     CtorInitializerType Type = (CtorInitializerType) readInt();
9172     switch (Type) {
9173     case CTOR_INITIALIZER_BASE:
9174       TInfo = readTypeSourceInfo();
9175       IsBaseVirtual = readBool();
9176       break;
9177 
9178     case CTOR_INITIALIZER_DELEGATING:
9179       TInfo = readTypeSourceInfo();
9180       break;
9181 
9182      case CTOR_INITIALIZER_MEMBER:
9183       Member = readDeclAs<FieldDecl>();
9184       break;
9185 
9186      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9187       IndirectMember = readDeclAs<IndirectFieldDecl>();
9188       break;
9189     }
9190 
9191     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9192     Expr *Init = readExpr();
9193     SourceLocation LParenLoc = readSourceLocation();
9194     SourceLocation RParenLoc = readSourceLocation();
9195 
9196     CXXCtorInitializer *BOMInit;
9197     if (Type == CTOR_INITIALIZER_BASE)
9198       BOMInit = new (Context)
9199           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9200                              RParenLoc, MemberOrEllipsisLoc);
9201     else if (Type == CTOR_INITIALIZER_DELEGATING)
9202       BOMInit = new (Context)
9203           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9204     else if (Member)
9205       BOMInit = new (Context)
9206           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9207                              Init, RParenLoc);
9208     else
9209       BOMInit = new (Context)
9210           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9211                              LParenLoc, Init, RParenLoc);
9212 
9213     if (/*IsWritten*/readBool()) {
9214       unsigned SourceOrder = readInt();
9215       BOMInit->setSourceOrder(SourceOrder);
9216     }
9217 
9218     CtorInitializers[i] = BOMInit;
9219   }
9220 
9221   return CtorInitializers;
9222 }
9223 
9224 NestedNameSpecifierLoc
9225 ASTRecordReader::readNestedNameSpecifierLoc() {
9226   ASTContext &Context = getContext();
9227   unsigned N = readInt();
9228   NestedNameSpecifierLocBuilder Builder;
9229   for (unsigned I = 0; I != N; ++I) {
9230     auto Kind = readNestedNameSpecifierKind();
9231     switch (Kind) {
9232     case NestedNameSpecifier::Identifier: {
9233       IdentifierInfo *II = readIdentifier();
9234       SourceRange Range = readSourceRange();
9235       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9236       break;
9237     }
9238 
9239     case NestedNameSpecifier::Namespace: {
9240       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9241       SourceRange Range = readSourceRange();
9242       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9243       break;
9244     }
9245 
9246     case NestedNameSpecifier::NamespaceAlias: {
9247       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
9248       SourceRange Range = readSourceRange();
9249       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9250       break;
9251     }
9252 
9253     case NestedNameSpecifier::TypeSpec:
9254     case NestedNameSpecifier::TypeSpecWithTemplate: {
9255       bool Template = readBool();
9256       TypeSourceInfo *T = readTypeSourceInfo();
9257       if (!T)
9258         return NestedNameSpecifierLoc();
9259       SourceLocation ColonColonLoc = readSourceLocation();
9260 
9261       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9262       Builder.Extend(Context,
9263                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9264                      T->getTypeLoc(), ColonColonLoc);
9265       break;
9266     }
9267 
9268     case NestedNameSpecifier::Global: {
9269       SourceLocation ColonColonLoc = readSourceLocation();
9270       Builder.MakeGlobal(Context, ColonColonLoc);
9271       break;
9272     }
9273 
9274     case NestedNameSpecifier::Super: {
9275       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
9276       SourceRange Range = readSourceRange();
9277       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9278       break;
9279     }
9280     }
9281   }
9282 
9283   return Builder.getWithLocInContext(Context);
9284 }
9285 
9286 SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9287                                        unsigned &Idx, LocSeq *Seq) {
9288   SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq);
9289   SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq);
9290   return SourceRange(beg, end);
9291 }
9292 
9293 /// Read a floating-point value
9294 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9295   return llvm::APFloat(Sem, readAPInt());
9296 }
9297 
9298 // Read a string
9299 std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9300   unsigned Len = Record[Idx++];
9301   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9302   Idx += Len;
9303   return Result;
9304 }
9305 
9306 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9307                                 unsigned &Idx) {
9308   std::string Filename = ReadString(Record, Idx);
9309   ResolveImportedPath(F, Filename);
9310   return Filename;
9311 }
9312 
9313 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9314                                 const RecordData &Record, unsigned &Idx) {
9315   std::string Filename = ReadString(Record, Idx);
9316   if (!BaseDirectory.empty())
9317     ResolveImportedPath(Filename, BaseDirectory);
9318   return Filename;
9319 }
9320 
9321 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9322                                          unsigned &Idx) {
9323   unsigned Major = Record[Idx++];
9324   unsigned Minor = Record[Idx++];
9325   unsigned Subminor = Record[Idx++];
9326   if (Minor == 0)
9327     return VersionTuple(Major);
9328   if (Subminor == 0)
9329     return VersionTuple(Major, Minor - 1);
9330   return VersionTuple(Major, Minor - 1, Subminor - 1);
9331 }
9332 
9333 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9334                                           const RecordData &Record,
9335                                           unsigned &Idx) {
9336   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9337   return CXXTemporary::Create(getContext(), Decl);
9338 }
9339 
9340 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9341   return Diag(CurrentImportLoc, DiagID);
9342 }
9343 
9344 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9345   return Diags.Report(Loc, DiagID);
9346 }
9347 
9348 /// Retrieve the identifier table associated with the
9349 /// preprocessor.
9350 IdentifierTable &ASTReader::getIdentifierTable() {
9351   return PP.getIdentifierTable();
9352 }
9353 
9354 /// Record that the given ID maps to the given switch-case
9355 /// statement.
9356 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9357   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9358          "Already have a SwitchCase with this ID");
9359   (*CurrSwitchCaseStmts)[ID] = SC;
9360 }
9361 
9362 /// Retrieve the switch-case statement with the given ID.
9363 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9364   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9365   return (*CurrSwitchCaseStmts)[ID];
9366 }
9367 
9368 void ASTReader::ClearSwitchCaseIDs() {
9369   CurrSwitchCaseStmts->clear();
9370 }
9371 
9372 void ASTReader::ReadComments() {
9373   ASTContext &Context = getContext();
9374   std::vector<RawComment *> Comments;
9375   for (SmallVectorImpl<std::pair<BitstreamCursor,
9376                                  serialization::ModuleFile *>>::iterator
9377        I = CommentsCursors.begin(),
9378        E = CommentsCursors.end();
9379        I != E; ++I) {
9380     Comments.clear();
9381     BitstreamCursor &Cursor = I->first;
9382     serialization::ModuleFile &F = *I->second;
9383     SavedStreamPosition SavedPosition(Cursor);
9384 
9385     RecordData Record;
9386     while (true) {
9387       Expected<llvm::BitstreamEntry> MaybeEntry =
9388           Cursor.advanceSkippingSubblocks(
9389               BitstreamCursor::AF_DontPopBlockAtEnd);
9390       if (!MaybeEntry) {
9391         Error(MaybeEntry.takeError());
9392         return;
9393       }
9394       llvm::BitstreamEntry Entry = MaybeEntry.get();
9395 
9396       switch (Entry.Kind) {
9397       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9398       case llvm::BitstreamEntry::Error:
9399         Error("malformed block record in AST file");
9400         return;
9401       case llvm::BitstreamEntry::EndBlock:
9402         goto NextCursor;
9403       case llvm::BitstreamEntry::Record:
9404         // The interesting case.
9405         break;
9406       }
9407 
9408       // Read a record.
9409       Record.clear();
9410       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9411       if (!MaybeComment) {
9412         Error(MaybeComment.takeError());
9413         return;
9414       }
9415       switch ((CommentRecordTypes)MaybeComment.get()) {
9416       case COMMENTS_RAW_COMMENT: {
9417         unsigned Idx = 0;
9418         SourceRange SR = ReadSourceRange(F, Record, Idx);
9419         RawComment::CommentKind Kind =
9420             (RawComment::CommentKind) Record[Idx++];
9421         bool IsTrailingComment = Record[Idx++];
9422         bool IsAlmostTrailingComment = Record[Idx++];
9423         Comments.push_back(new (Context) RawComment(
9424             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9425         break;
9426       }
9427       }
9428     }
9429   NextCursor:
9430     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9431         FileToOffsetToComment;
9432     for (RawComment *C : Comments) {
9433       SourceLocation CommentLoc = C->getBeginLoc();
9434       if (CommentLoc.isValid()) {
9435         std::pair<FileID, unsigned> Loc =
9436             SourceMgr.getDecomposedLoc(CommentLoc);
9437         if (Loc.first.isValid())
9438           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9439       }
9440     }
9441   }
9442 }
9443 
9444 void ASTReader::visitInputFileInfos(
9445     serialization::ModuleFile &MF, bool IncludeSystem,
9446     llvm::function_ref<void(const serialization::InputFileInfo &IFI,
9447                             bool IsSystem)>
9448         Visitor) {
9449   unsigned NumUserInputs = MF.NumUserInputFiles;
9450   unsigned NumInputs = MF.InputFilesLoaded.size();
9451   assert(NumUserInputs <= NumInputs);
9452   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9453   for (unsigned I = 0; I < N; ++I) {
9454     bool IsSystem = I >= NumUserInputs;
9455     InputFileInfo IFI = getInputFileInfo(MF, I+1);
9456     Visitor(IFI, IsSystem);
9457   }
9458 }
9459 
9460 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9461                                 bool IncludeSystem, bool Complain,
9462                     llvm::function_ref<void(const serialization::InputFile &IF,
9463                                             bool isSystem)> Visitor) {
9464   unsigned NumUserInputs = MF.NumUserInputFiles;
9465   unsigned NumInputs = MF.InputFilesLoaded.size();
9466   assert(NumUserInputs <= NumInputs);
9467   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9468   for (unsigned I = 0; I < N; ++I) {
9469     bool IsSystem = I >= NumUserInputs;
9470     InputFile IF = getInputFile(MF, I+1, Complain);
9471     Visitor(IF, IsSystem);
9472   }
9473 }
9474 
9475 void ASTReader::visitTopLevelModuleMaps(
9476     serialization::ModuleFile &MF,
9477     llvm::function_ref<void(FileEntryRef FE)> Visitor) {
9478   unsigned NumInputs = MF.InputFilesLoaded.size();
9479   for (unsigned I = 0; I < NumInputs; ++I) {
9480     InputFileInfo IFI = getInputFileInfo(MF, I + 1);
9481     if (IFI.TopLevel && IFI.ModuleMap)
9482       if (auto FE = getInputFile(MF, I + 1).getFile())
9483         Visitor(*FE);
9484   }
9485 }
9486 
9487 void ASTReader::finishPendingActions() {
9488   while (
9489       !PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
9490       !PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
9491       !PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
9492       !PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
9493       !PendingObjCExtensionIvarRedeclarations.empty()) {
9494     // If any identifiers with corresponding top-level declarations have
9495     // been loaded, load those declarations now.
9496     using TopLevelDeclsMap =
9497         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9498     TopLevelDeclsMap TopLevelDecls;
9499 
9500     while (!PendingIdentifierInfos.empty()) {
9501       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9502       SmallVector<uint32_t, 4> DeclIDs =
9503           std::move(PendingIdentifierInfos.back().second);
9504       PendingIdentifierInfos.pop_back();
9505 
9506       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9507     }
9508 
9509     // Load each function type that we deferred loading because it was a
9510     // deduced type that might refer to a local type declared within itself.
9511     for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
9512       auto *FD = PendingDeducedFunctionTypes[I].first;
9513       FD->setType(GetType(PendingDeducedFunctionTypes[I].second));
9514 
9515       // If we gave a function a deduced return type, remember that we need to
9516       // propagate that along the redeclaration chain.
9517       auto *DT = FD->getReturnType()->getContainedDeducedType();
9518       if (DT && DT->isDeduced())
9519         PendingDeducedTypeUpdates.insert(
9520             {FD->getCanonicalDecl(), FD->getReturnType()});
9521     }
9522     PendingDeducedFunctionTypes.clear();
9523 
9524     // Load each variable type that we deferred loading because it was a
9525     // deduced type that might refer to a local type declared within itself.
9526     for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
9527       auto *VD = PendingDeducedVarTypes[I].first;
9528       VD->setType(GetType(PendingDeducedVarTypes[I].second));
9529     }
9530     PendingDeducedVarTypes.clear();
9531 
9532     // For each decl chain that we wanted to complete while deserializing, mark
9533     // it as "still needs to be completed".
9534     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9535       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9536     }
9537     PendingIncompleteDeclChains.clear();
9538 
9539     // Load pending declaration chains.
9540     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9541       loadPendingDeclChain(PendingDeclChains[I].first,
9542                            PendingDeclChains[I].second);
9543     PendingDeclChains.clear();
9544 
9545     // Make the most recent of the top-level declarations visible.
9546     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9547            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9548       IdentifierInfo *II = TLD->first;
9549       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9550         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9551       }
9552     }
9553 
9554     // Load any pending macro definitions.
9555     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9556       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9557       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9558       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9559       // Initialize the macro history from chained-PCHs ahead of module imports.
9560       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9561            ++IDIdx) {
9562         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9563         if (!Info.M->isModule())
9564           resolvePendingMacro(II, Info);
9565       }
9566       // Handle module imports.
9567       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9568            ++IDIdx) {
9569         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9570         if (Info.M->isModule())
9571           resolvePendingMacro(II, Info);
9572       }
9573     }
9574     PendingMacroIDs.clear();
9575 
9576     // Wire up the DeclContexts for Decls that we delayed setting until
9577     // recursive loading is completed.
9578     while (!PendingDeclContextInfos.empty()) {
9579       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9580       PendingDeclContextInfos.pop_front();
9581       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9582       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9583       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9584     }
9585 
9586     // Perform any pending declaration updates.
9587     while (!PendingUpdateRecords.empty()) {
9588       auto Update = PendingUpdateRecords.pop_back_val();
9589       ReadingKindTracker ReadingKind(Read_Decl, *this);
9590       loadDeclUpdateRecords(Update);
9591     }
9592 
9593     while (!PendingObjCExtensionIvarRedeclarations.empty()) {
9594       auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
9595       auto DuplicateIvars =
9596           PendingObjCExtensionIvarRedeclarations.back().second;
9597       llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
9598       StructuralEquivalenceContext Ctx(
9599           ExtensionsPair.first->getASTContext(),
9600           ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
9601           StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
9602           /*Complain =*/false,
9603           /*ErrorOnTagTypeMismatch =*/true);
9604       if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {
9605         // Merge redeclared ivars with their predecessors.
9606         for (auto IvarPair : DuplicateIvars) {
9607           ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
9608           // Change semantic DeclContext but keep the lexical one.
9609           Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(),
9610                                     Ivar->getLexicalDeclContext(),
9611                                     getContext());
9612           getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());
9613         }
9614         // Invalidate duplicate extension and the cached ivar list.
9615         ExtensionsPair.first->setInvalidDecl();
9616         ExtensionsPair.second->getClassInterface()
9617             ->getDefinition()
9618             ->setIvarList(nullptr);
9619       } else {
9620         for (auto IvarPair : DuplicateIvars) {
9621           Diag(IvarPair.first->getLocation(),
9622                diag::err_duplicate_ivar_declaration)
9623               << IvarPair.first->getIdentifier();
9624           Diag(IvarPair.second->getLocation(), diag::note_previous_definition);
9625         }
9626       }
9627       PendingObjCExtensionIvarRedeclarations.pop_back();
9628     }
9629   }
9630 
9631   // At this point, all update records for loaded decls are in place, so any
9632   // fake class definitions should have become real.
9633   assert(PendingFakeDefinitionData.empty() &&
9634          "faked up a class definition but never saw the real one");
9635 
9636   // If we deserialized any C++ or Objective-C class definitions, any
9637   // Objective-C protocol definitions, or any redeclarable templates, make sure
9638   // that all redeclarations point to the definitions. Note that this can only
9639   // happen now, after the redeclaration chains have been fully wired.
9640   for (Decl *D : PendingDefinitions) {
9641     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9642       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9643         // Make sure that the TagType points at the definition.
9644         const_cast<TagType*>(TagT)->decl = TD;
9645       }
9646 
9647       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9648         for (auto *R = getMostRecentExistingDecl(RD); R;
9649              R = R->getPreviousDecl()) {
9650           assert((R == D) ==
9651                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9652                  "declaration thinks it's the definition but it isn't");
9653           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9654         }
9655       }
9656 
9657       continue;
9658     }
9659 
9660     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9661       // Make sure that the ObjCInterfaceType points at the definition.
9662       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9663         ->Decl = ID;
9664 
9665       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9666         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9667 
9668       continue;
9669     }
9670 
9671     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9672       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9673         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9674 
9675       continue;
9676     }
9677 
9678     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9679     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9680       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9681   }
9682   PendingDefinitions.clear();
9683 
9684   // Load the bodies of any functions or methods we've encountered. We do
9685   // this now (delayed) so that we can be sure that the declaration chains
9686   // have been fully wired up (hasBody relies on this).
9687   // FIXME: We shouldn't require complete redeclaration chains here.
9688   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9689                                PBEnd = PendingBodies.end();
9690        PB != PBEnd; ++PB) {
9691     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9692       // For a function defined inline within a class template, force the
9693       // canonical definition to be the one inside the canonical definition of
9694       // the template. This ensures that we instantiate from a correct view
9695       // of the template.
9696       //
9697       // Sadly we can't do this more generally: we can't be sure that all
9698       // copies of an arbitrary class definition will have the same members
9699       // defined (eg, some member functions may not be instantiated, and some
9700       // special members may or may not have been implicitly defined).
9701       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9702         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9703           continue;
9704 
9705       // FIXME: Check for =delete/=default?
9706       const FunctionDecl *Defn = nullptr;
9707       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9708         FD->setLazyBody(PB->second);
9709       } else {
9710         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9711         mergeDefinitionVisibility(NonConstDefn, FD);
9712 
9713         if (!FD->isLateTemplateParsed() &&
9714             !NonConstDefn->isLateTemplateParsed() &&
9715             FD->getODRHash() != NonConstDefn->getODRHash()) {
9716           if (!isa<CXXMethodDecl>(FD)) {
9717             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9718           } else if (FD->getLexicalParent()->isFileContext() &&
9719                      NonConstDefn->getLexicalParent()->isFileContext()) {
9720             // Only diagnose out-of-line method definitions.  If they are
9721             // in class definitions, then an error will be generated when
9722             // processing the class bodies.
9723             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9724           }
9725         }
9726       }
9727       continue;
9728     }
9729 
9730     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9731     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9732       MD->setLazyBody(PB->second);
9733   }
9734   PendingBodies.clear();
9735 
9736   // Inform any classes that had members added that they now have more members.
9737   for (auto [RD, MD] : PendingAddedClassMembers) {
9738     RD->addedMember(MD);
9739   }
9740   PendingAddedClassMembers.clear();
9741 
9742   // Do some cleanup.
9743   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9744     getContext().deduplicateMergedDefinitonsFor(ND);
9745   PendingMergedDefinitionsToDeduplicate.clear();
9746 }
9747 
9748 void ASTReader::diagnoseOdrViolations() {
9749   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9750       PendingRecordOdrMergeFailures.empty() &&
9751       PendingFunctionOdrMergeFailures.empty() &&
9752       PendingEnumOdrMergeFailures.empty() &&
9753       PendingObjCInterfaceOdrMergeFailures.empty() &&
9754       PendingObjCProtocolOdrMergeFailures.empty())
9755     return;
9756 
9757   // Trigger the import of the full definition of each class that had any
9758   // odr-merging problems, so we can produce better diagnostics for them.
9759   // These updates may in turn find and diagnose some ODR failures, so take
9760   // ownership of the set first.
9761   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9762   PendingOdrMergeFailures.clear();
9763   for (auto &Merge : OdrMergeFailures) {
9764     Merge.first->buildLookup();
9765     Merge.first->decls_begin();
9766     Merge.first->bases_begin();
9767     Merge.first->vbases_begin();
9768     for (auto &RecordPair : Merge.second) {
9769       auto *RD = RecordPair.first;
9770       RD->decls_begin();
9771       RD->bases_begin();
9772       RD->vbases_begin();
9773     }
9774   }
9775 
9776   // Trigger the import of the full definition of each record in C/ObjC.
9777   auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
9778   PendingRecordOdrMergeFailures.clear();
9779   for (auto &Merge : RecordOdrMergeFailures) {
9780     Merge.first->decls_begin();
9781     for (auto &D : Merge.second)
9782       D->decls_begin();
9783   }
9784 
9785   // Trigger the import of the full interface definition.
9786   auto ObjCInterfaceOdrMergeFailures =
9787       std::move(PendingObjCInterfaceOdrMergeFailures);
9788   PendingObjCInterfaceOdrMergeFailures.clear();
9789   for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
9790     Merge.first->decls_begin();
9791     for (auto &InterfacePair : Merge.second)
9792       InterfacePair.first->decls_begin();
9793   }
9794 
9795   // Trigger the import of functions.
9796   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9797   PendingFunctionOdrMergeFailures.clear();
9798   for (auto &Merge : FunctionOdrMergeFailures) {
9799     Merge.first->buildLookup();
9800     Merge.first->decls_begin();
9801     Merge.first->getBody();
9802     for (auto &FD : Merge.second) {
9803       FD->buildLookup();
9804       FD->decls_begin();
9805       FD->getBody();
9806     }
9807   }
9808 
9809   // Trigger the import of enums.
9810   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9811   PendingEnumOdrMergeFailures.clear();
9812   for (auto &Merge : EnumOdrMergeFailures) {
9813     Merge.first->decls_begin();
9814     for (auto &Enum : Merge.second) {
9815       Enum->decls_begin();
9816     }
9817   }
9818 
9819   // Trigger the import of the full protocol definition.
9820   auto ObjCProtocolOdrMergeFailures =
9821       std::move(PendingObjCProtocolOdrMergeFailures);
9822   PendingObjCProtocolOdrMergeFailures.clear();
9823   for (auto &Merge : ObjCProtocolOdrMergeFailures) {
9824     Merge.first->decls_begin();
9825     for (auto &ProtocolPair : Merge.second)
9826       ProtocolPair.first->decls_begin();
9827   }
9828 
9829   // For each declaration from a merged context, check that the canonical
9830   // definition of that context also contains a declaration of the same
9831   // entity.
9832   //
9833   // Caution: this loop does things that might invalidate iterators into
9834   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9835   while (!PendingOdrMergeChecks.empty()) {
9836     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9837 
9838     // FIXME: Skip over implicit declarations for now. This matters for things
9839     // like implicitly-declared special member functions. This isn't entirely
9840     // correct; we can end up with multiple unmerged declarations of the same
9841     // implicit entity.
9842     if (D->isImplicit())
9843       continue;
9844 
9845     DeclContext *CanonDef = D->getDeclContext();
9846 
9847     bool Found = false;
9848     const Decl *DCanon = D->getCanonicalDecl();
9849 
9850     for (auto *RI : D->redecls()) {
9851       if (RI->getLexicalDeclContext() == CanonDef) {
9852         Found = true;
9853         break;
9854       }
9855     }
9856     if (Found)
9857       continue;
9858 
9859     // Quick check failed, time to do the slow thing. Note, we can't just
9860     // look up the name of D in CanonDef here, because the member that is
9861     // in CanonDef might not be found by name lookup (it might have been
9862     // replaced by a more recent declaration in the lookup table), and we
9863     // can't necessarily find it in the redeclaration chain because it might
9864     // be merely mergeable, not redeclarable.
9865     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9866     for (auto *CanonMember : CanonDef->decls()) {
9867       if (CanonMember->getCanonicalDecl() == DCanon) {
9868         // This can happen if the declaration is merely mergeable and not
9869         // actually redeclarable (we looked for redeclarations earlier).
9870         //
9871         // FIXME: We should be able to detect this more efficiently, without
9872         // pulling in all of the members of CanonDef.
9873         Found = true;
9874         break;
9875       }
9876       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9877         if (ND->getDeclName() == D->getDeclName())
9878           Candidates.push_back(ND);
9879     }
9880 
9881     if (!Found) {
9882       // The AST doesn't like TagDecls becoming invalid after they've been
9883       // completed. We only really need to mark FieldDecls as invalid here.
9884       if (!isa<TagDecl>(D))
9885         D->setInvalidDecl();
9886 
9887       // Ensure we don't accidentally recursively enter deserialization while
9888       // we're producing our diagnostic.
9889       Deserializing RecursionGuard(this);
9890 
9891       std::string CanonDefModule =
9892           ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
9893               cast<Decl>(CanonDef));
9894       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9895         << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
9896         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9897 
9898       if (Candidates.empty())
9899         Diag(cast<Decl>(CanonDef)->getLocation(),
9900              diag::note_module_odr_violation_no_possible_decls) << D;
9901       else {
9902         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9903           Diag(Candidates[I]->getLocation(),
9904                diag::note_module_odr_violation_possible_decl)
9905             << Candidates[I];
9906       }
9907 
9908       DiagnosedOdrMergeFailures.insert(CanonDef);
9909     }
9910   }
9911 
9912   if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
9913       FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
9914       ObjCInterfaceOdrMergeFailures.empty() &&
9915       ObjCProtocolOdrMergeFailures.empty())
9916     return;
9917 
9918   ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
9919                                getPreprocessor().getLangOpts());
9920 
9921   // Issue any pending ODR-failure diagnostics.
9922   for (auto &Merge : OdrMergeFailures) {
9923     // If we've already pointed out a specific problem with this class, don't
9924     // bother issuing a general "something's different" diagnostic.
9925     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9926       continue;
9927 
9928     bool Diagnosed = false;
9929     CXXRecordDecl *FirstRecord = Merge.first;
9930     for (auto &RecordPair : Merge.second) {
9931       if (DiagsEmitter.diagnoseMismatch(FirstRecord, RecordPair.first,
9932                                         RecordPair.second)) {
9933         Diagnosed = true;
9934         break;
9935       }
9936     }
9937 
9938     if (!Diagnosed) {
9939       // All definitions are updates to the same declaration. This happens if a
9940       // module instantiates the declaration of a class template specialization
9941       // and two or more other modules instantiate its definition.
9942       //
9943       // FIXME: Indicate which modules had instantiations of this definition.
9944       // FIXME: How can this even happen?
9945       Diag(Merge.first->getLocation(),
9946            diag::err_module_odr_violation_different_instantiations)
9947           << Merge.first;
9948     }
9949   }
9950 
9951   // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
9952   // that in C++ this is done as a part of CXXRecordDecl ODR checking.
9953   for (auto &Merge : RecordOdrMergeFailures) {
9954     // If we've already pointed out a specific problem with this class, don't
9955     // bother issuing a general "something's different" diagnostic.
9956     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9957       continue;
9958 
9959     RecordDecl *FirstRecord = Merge.first;
9960     bool Diagnosed = false;
9961     for (auto *SecondRecord : Merge.second) {
9962       if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
9963         Diagnosed = true;
9964         break;
9965       }
9966     }
9967     (void)Diagnosed;
9968     assert(Diagnosed && "Unable to emit ODR diagnostic.");
9969   }
9970 
9971   // Issue ODR failures diagnostics for functions.
9972   for (auto &Merge : FunctionOdrMergeFailures) {
9973     FunctionDecl *FirstFunction = Merge.first;
9974     bool Diagnosed = false;
9975     for (auto &SecondFunction : Merge.second) {
9976       if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
9977         Diagnosed = true;
9978         break;
9979       }
9980     }
9981     (void)Diagnosed;
9982     assert(Diagnosed && "Unable to emit ODR diagnostic.");
9983   }
9984 
9985   // Issue ODR failures diagnostics for enums.
9986   for (auto &Merge : EnumOdrMergeFailures) {
9987     // If we've already pointed out a specific problem with this enum, don't
9988     // bother issuing a general "something's different" diagnostic.
9989     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9990       continue;
9991 
9992     EnumDecl *FirstEnum = Merge.first;
9993     bool Diagnosed = false;
9994     for (auto &SecondEnum : Merge.second) {
9995       if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
9996         Diagnosed = true;
9997         break;
9998       }
9999     }
10000     (void)Diagnosed;
10001     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10002   }
10003 
10004   for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10005     // If we've already pointed out a specific problem with this interface,
10006     // don't bother issuing a general "something's different" diagnostic.
10007     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10008       continue;
10009 
10010     bool Diagnosed = false;
10011     ObjCInterfaceDecl *FirstID = Merge.first;
10012     for (auto &InterfacePair : Merge.second) {
10013       if (DiagsEmitter.diagnoseMismatch(FirstID, InterfacePair.first,
10014                                         InterfacePair.second)) {
10015         Diagnosed = true;
10016         break;
10017       }
10018     }
10019     (void)Diagnosed;
10020     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10021   }
10022 
10023   for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10024     // If we've already pointed out a specific problem with this protocol,
10025     // don't bother issuing a general "something's different" diagnostic.
10026     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10027       continue;
10028 
10029     ObjCProtocolDecl *FirstProtocol = Merge.first;
10030     bool Diagnosed = false;
10031     for (auto &ProtocolPair : Merge.second) {
10032       if (DiagsEmitter.diagnoseMismatch(FirstProtocol, ProtocolPair.first,
10033                                         ProtocolPair.second)) {
10034         Diagnosed = true;
10035         break;
10036       }
10037     }
10038     (void)Diagnosed;
10039     assert(Diagnosed && "Unable to emit ODR diagnostic.");
10040   }
10041 }
10042 
10043 void ASTReader::StartedDeserializing() {
10044   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10045     ReadTimer->startTimer();
10046 }
10047 
10048 void ASTReader::FinishedDeserializing() {
10049   assert(NumCurrentElementsDeserializing &&
10050          "FinishedDeserializing not paired with StartedDeserializing");
10051   if (NumCurrentElementsDeserializing == 1) {
10052     // We decrease NumCurrentElementsDeserializing only after pending actions
10053     // are finished, to avoid recursively re-calling finishPendingActions().
10054     finishPendingActions();
10055   }
10056   --NumCurrentElementsDeserializing;
10057 
10058   if (NumCurrentElementsDeserializing == 0) {
10059     // Propagate exception specification and deduced type updates along
10060     // redeclaration chains.
10061     //
10062     // We do this now rather than in finishPendingActions because we want to
10063     // be able to walk the complete redeclaration chains of the updated decls.
10064     while (!PendingExceptionSpecUpdates.empty() ||
10065            !PendingDeducedTypeUpdates.empty()) {
10066       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10067       PendingExceptionSpecUpdates.clear();
10068       for (auto Update : ESUpdates) {
10069         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10070         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10071         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10072         if (auto *Listener = getContext().getASTMutationListener())
10073           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
10074         for (auto *Redecl : Update.second->redecls())
10075           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10076       }
10077 
10078       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10079       PendingDeducedTypeUpdates.clear();
10080       for (auto Update : DTUpdates) {
10081         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10082         // FIXME: If the return type is already deduced, check that it matches.
10083         getContext().adjustDeducedFunctionResultType(Update.first,
10084                                                      Update.second);
10085       }
10086     }
10087 
10088     if (ReadTimer)
10089       ReadTimer->stopTimer();
10090 
10091     diagnoseOdrViolations();
10092 
10093     // We are not in recursive loading, so it's safe to pass the "interesting"
10094     // decls to the consumer.
10095     if (Consumer)
10096       PassInterestingDeclsToConsumer();
10097   }
10098 }
10099 
10100 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10101   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10102     // Remove any fake results before adding any real ones.
10103     auto It = PendingFakeLookupResults.find(II);
10104     if (It != PendingFakeLookupResults.end()) {
10105       for (auto *ND : It->second)
10106         SemaObj->IdResolver.RemoveDecl(ND);
10107       // FIXME: this works around module+PCH performance issue.
10108       // Rather than erase the result from the map, which is O(n), just clear
10109       // the vector of NamedDecls.
10110       It->second.clear();
10111     }
10112   }
10113 
10114   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10115     SemaObj->TUScope->AddDecl(D);
10116   } else if (SemaObj->TUScope) {
10117     // Adding the decl to IdResolver may have failed because it was already in
10118     // (even though it was not added in scope). If it is already in, make sure
10119     // it gets in the scope as well.
10120     if (llvm::is_contained(SemaObj->IdResolver.decls(Name), D))
10121       SemaObj->TUScope->AddDecl(D);
10122   }
10123 }
10124 
10125 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10126                      ASTContext *Context,
10127                      const PCHContainerReader &PCHContainerRdr,
10128                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10129                      StringRef isysroot,
10130                      DisableValidationForModuleKind DisableValidationKind,
10131                      bool AllowASTWithCompilerErrors,
10132                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10133                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10134                      std::unique_ptr<llvm::Timer> ReadTimer)
10135     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
10136                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
10137                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
10138       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
10139       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
10140       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
10141                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
10142       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
10143       DisableValidationKind(DisableValidationKind),
10144       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
10145       AllowConfigurationMismatch(AllowConfigurationMismatch),
10146       ValidateSystemInputs(ValidateSystemInputs),
10147       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
10148       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10149   SourceMgr.setExternalSLocEntrySource(this);
10150 
10151   for (const auto &Ext : Extensions) {
10152     auto BlockName = Ext->getExtensionMetadata().BlockName;
10153     auto Known = ModuleFileExtensions.find(BlockName);
10154     if (Known != ModuleFileExtensions.end()) {
10155       Diags.Report(diag::warn_duplicate_module_file_extension)
10156         << BlockName;
10157       continue;
10158     }
10159 
10160     ModuleFileExtensions.insert({BlockName, Ext});
10161   }
10162 }
10163 
10164 ASTReader::~ASTReader() {
10165   if (OwnsDeserializationListener)
10166     delete DeserializationListener;
10167 }
10168 
10169 IdentifierResolver &ASTReader::getIdResolver() {
10170   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
10171 }
10172 
10173 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
10174                                                unsigned AbbrevID) {
10175   Idx = 0;
10176   Record.clear();
10177   return Cursor.readRecord(AbbrevID, Record);
10178 }
10179 //===----------------------------------------------------------------------===//
10180 //// OMPClauseReader implementation
10181 ////===----------------------------------------------------------------------===//
10182 
10183 // This has to be in namespace clang because it's friended by all
10184 // of the OMP clauses.
10185 namespace clang {
10186 
10187 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
10188   ASTRecordReader &Record;
10189   ASTContext &Context;
10190 
10191 public:
10192   OMPClauseReader(ASTRecordReader &Record)
10193       : Record(Record), Context(Record.getContext()) {}
10194 #define GEN_CLANG_CLAUSE_CLASS
10195 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
10196 #include "llvm/Frontend/OpenMP/OMP.inc"
10197   OMPClause *readClause();
10198   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
10199   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
10200 };
10201 
10202 } // end namespace clang
10203 
10204 OMPClause *ASTRecordReader::readOMPClause() {
10205   return OMPClauseReader(*this).readClause();
10206 }
10207 
10208 OMPClause *OMPClauseReader::readClause() {
10209   OMPClause *C = nullptr;
10210   switch (llvm::omp::Clause(Record.readInt())) {
10211   case llvm::omp::OMPC_if:
10212     C = new (Context) OMPIfClause();
10213     break;
10214   case llvm::omp::OMPC_final:
10215     C = new (Context) OMPFinalClause();
10216     break;
10217   case llvm::omp::OMPC_num_threads:
10218     C = new (Context) OMPNumThreadsClause();
10219     break;
10220   case llvm::omp::OMPC_safelen:
10221     C = new (Context) OMPSafelenClause();
10222     break;
10223   case llvm::omp::OMPC_simdlen:
10224     C = new (Context) OMPSimdlenClause();
10225     break;
10226   case llvm::omp::OMPC_sizes: {
10227     unsigned NumSizes = Record.readInt();
10228     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
10229     break;
10230   }
10231   case llvm::omp::OMPC_full:
10232     C = OMPFullClause::CreateEmpty(Context);
10233     break;
10234   case llvm::omp::OMPC_partial:
10235     C = OMPPartialClause::CreateEmpty(Context);
10236     break;
10237   case llvm::omp::OMPC_allocator:
10238     C = new (Context) OMPAllocatorClause();
10239     break;
10240   case llvm::omp::OMPC_collapse:
10241     C = new (Context) OMPCollapseClause();
10242     break;
10243   case llvm::omp::OMPC_default:
10244     C = new (Context) OMPDefaultClause();
10245     break;
10246   case llvm::omp::OMPC_proc_bind:
10247     C = new (Context) OMPProcBindClause();
10248     break;
10249   case llvm::omp::OMPC_schedule:
10250     C = new (Context) OMPScheduleClause();
10251     break;
10252   case llvm::omp::OMPC_ordered:
10253     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
10254     break;
10255   case llvm::omp::OMPC_nowait:
10256     C = new (Context) OMPNowaitClause();
10257     break;
10258   case llvm::omp::OMPC_untied:
10259     C = new (Context) OMPUntiedClause();
10260     break;
10261   case llvm::omp::OMPC_mergeable:
10262     C = new (Context) OMPMergeableClause();
10263     break;
10264   case llvm::omp::OMPC_read:
10265     C = new (Context) OMPReadClause();
10266     break;
10267   case llvm::omp::OMPC_write:
10268     C = new (Context) OMPWriteClause();
10269     break;
10270   case llvm::omp::OMPC_update:
10271     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
10272     break;
10273   case llvm::omp::OMPC_capture:
10274     C = new (Context) OMPCaptureClause();
10275     break;
10276   case llvm::omp::OMPC_compare:
10277     C = new (Context) OMPCompareClause();
10278     break;
10279   case llvm::omp::OMPC_fail:
10280     C = new (Context) OMPFailClause();
10281     break;
10282   case llvm::omp::OMPC_seq_cst:
10283     C = new (Context) OMPSeqCstClause();
10284     break;
10285   case llvm::omp::OMPC_acq_rel:
10286     C = new (Context) OMPAcqRelClause();
10287     break;
10288   case llvm::omp::OMPC_acquire:
10289     C = new (Context) OMPAcquireClause();
10290     break;
10291   case llvm::omp::OMPC_release:
10292     C = new (Context) OMPReleaseClause();
10293     break;
10294   case llvm::omp::OMPC_relaxed:
10295     C = new (Context) OMPRelaxedClause();
10296     break;
10297   case llvm::omp::OMPC_threads:
10298     C = new (Context) OMPThreadsClause();
10299     break;
10300   case llvm::omp::OMPC_simd:
10301     C = new (Context) OMPSIMDClause();
10302     break;
10303   case llvm::omp::OMPC_nogroup:
10304     C = new (Context) OMPNogroupClause();
10305     break;
10306   case llvm::omp::OMPC_unified_address:
10307     C = new (Context) OMPUnifiedAddressClause();
10308     break;
10309   case llvm::omp::OMPC_unified_shared_memory:
10310     C = new (Context) OMPUnifiedSharedMemoryClause();
10311     break;
10312   case llvm::omp::OMPC_reverse_offload:
10313     C = new (Context) OMPReverseOffloadClause();
10314     break;
10315   case llvm::omp::OMPC_dynamic_allocators:
10316     C = new (Context) OMPDynamicAllocatorsClause();
10317     break;
10318   case llvm::omp::OMPC_atomic_default_mem_order:
10319     C = new (Context) OMPAtomicDefaultMemOrderClause();
10320     break;
10321   case llvm::omp::OMPC_at:
10322     C = new (Context) OMPAtClause();
10323     break;
10324   case llvm::omp::OMPC_severity:
10325     C = new (Context) OMPSeverityClause();
10326     break;
10327   case llvm::omp::OMPC_message:
10328     C = new (Context) OMPMessageClause();
10329     break;
10330   case llvm::omp::OMPC_private:
10331     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
10332     break;
10333   case llvm::omp::OMPC_firstprivate:
10334     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
10335     break;
10336   case llvm::omp::OMPC_lastprivate:
10337     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
10338     break;
10339   case llvm::omp::OMPC_shared:
10340     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
10341     break;
10342   case llvm::omp::OMPC_reduction: {
10343     unsigned N = Record.readInt();
10344     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
10345     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
10346     break;
10347   }
10348   case llvm::omp::OMPC_task_reduction:
10349     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
10350     break;
10351   case llvm::omp::OMPC_in_reduction:
10352     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
10353     break;
10354   case llvm::omp::OMPC_linear:
10355     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
10356     break;
10357   case llvm::omp::OMPC_aligned:
10358     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
10359     break;
10360   case llvm::omp::OMPC_copyin:
10361     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
10362     break;
10363   case llvm::omp::OMPC_copyprivate:
10364     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
10365     break;
10366   case llvm::omp::OMPC_flush:
10367     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
10368     break;
10369   case llvm::omp::OMPC_depobj:
10370     C = OMPDepobjClause::CreateEmpty(Context);
10371     break;
10372   case llvm::omp::OMPC_depend: {
10373     unsigned NumVars = Record.readInt();
10374     unsigned NumLoops = Record.readInt();
10375     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
10376     break;
10377   }
10378   case llvm::omp::OMPC_device:
10379     C = new (Context) OMPDeviceClause();
10380     break;
10381   case llvm::omp::OMPC_map: {
10382     OMPMappableExprListSizeTy Sizes;
10383     Sizes.NumVars = Record.readInt();
10384     Sizes.NumUniqueDeclarations = Record.readInt();
10385     Sizes.NumComponentLists = Record.readInt();
10386     Sizes.NumComponents = Record.readInt();
10387     C = OMPMapClause::CreateEmpty(Context, Sizes);
10388     break;
10389   }
10390   case llvm::omp::OMPC_num_teams:
10391     C = new (Context) OMPNumTeamsClause();
10392     break;
10393   case llvm::omp::OMPC_thread_limit:
10394     C = new (Context) OMPThreadLimitClause();
10395     break;
10396   case llvm::omp::OMPC_priority:
10397     C = new (Context) OMPPriorityClause();
10398     break;
10399   case llvm::omp::OMPC_grainsize:
10400     C = new (Context) OMPGrainsizeClause();
10401     break;
10402   case llvm::omp::OMPC_num_tasks:
10403     C = new (Context) OMPNumTasksClause();
10404     break;
10405   case llvm::omp::OMPC_hint:
10406     C = new (Context) OMPHintClause();
10407     break;
10408   case llvm::omp::OMPC_dist_schedule:
10409     C = new (Context) OMPDistScheduleClause();
10410     break;
10411   case llvm::omp::OMPC_defaultmap:
10412     C = new (Context) OMPDefaultmapClause();
10413     break;
10414   case llvm::omp::OMPC_to: {
10415     OMPMappableExprListSizeTy Sizes;
10416     Sizes.NumVars = Record.readInt();
10417     Sizes.NumUniqueDeclarations = Record.readInt();
10418     Sizes.NumComponentLists = Record.readInt();
10419     Sizes.NumComponents = Record.readInt();
10420     C = OMPToClause::CreateEmpty(Context, Sizes);
10421     break;
10422   }
10423   case llvm::omp::OMPC_from: {
10424     OMPMappableExprListSizeTy Sizes;
10425     Sizes.NumVars = Record.readInt();
10426     Sizes.NumUniqueDeclarations = Record.readInt();
10427     Sizes.NumComponentLists = Record.readInt();
10428     Sizes.NumComponents = Record.readInt();
10429     C = OMPFromClause::CreateEmpty(Context, Sizes);
10430     break;
10431   }
10432   case llvm::omp::OMPC_use_device_ptr: {
10433     OMPMappableExprListSizeTy Sizes;
10434     Sizes.NumVars = Record.readInt();
10435     Sizes.NumUniqueDeclarations = Record.readInt();
10436     Sizes.NumComponentLists = Record.readInt();
10437     Sizes.NumComponents = Record.readInt();
10438     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
10439     break;
10440   }
10441   case llvm::omp::OMPC_use_device_addr: {
10442     OMPMappableExprListSizeTy Sizes;
10443     Sizes.NumVars = Record.readInt();
10444     Sizes.NumUniqueDeclarations = Record.readInt();
10445     Sizes.NumComponentLists = Record.readInt();
10446     Sizes.NumComponents = Record.readInt();
10447     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
10448     break;
10449   }
10450   case llvm::omp::OMPC_is_device_ptr: {
10451     OMPMappableExprListSizeTy Sizes;
10452     Sizes.NumVars = Record.readInt();
10453     Sizes.NumUniqueDeclarations = Record.readInt();
10454     Sizes.NumComponentLists = Record.readInt();
10455     Sizes.NumComponents = Record.readInt();
10456     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
10457     break;
10458   }
10459   case llvm::omp::OMPC_has_device_addr: {
10460     OMPMappableExprListSizeTy Sizes;
10461     Sizes.NumVars = Record.readInt();
10462     Sizes.NumUniqueDeclarations = Record.readInt();
10463     Sizes.NumComponentLists = Record.readInt();
10464     Sizes.NumComponents = Record.readInt();
10465     C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);
10466     break;
10467   }
10468   case llvm::omp::OMPC_allocate:
10469     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
10470     break;
10471   case llvm::omp::OMPC_nontemporal:
10472     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
10473     break;
10474   case llvm::omp::OMPC_inclusive:
10475     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
10476     break;
10477   case llvm::omp::OMPC_exclusive:
10478     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
10479     break;
10480   case llvm::omp::OMPC_order:
10481     C = new (Context) OMPOrderClause();
10482     break;
10483   case llvm::omp::OMPC_init:
10484     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
10485     break;
10486   case llvm::omp::OMPC_use:
10487     C = new (Context) OMPUseClause();
10488     break;
10489   case llvm::omp::OMPC_destroy:
10490     C = new (Context) OMPDestroyClause();
10491     break;
10492   case llvm::omp::OMPC_novariants:
10493     C = new (Context) OMPNovariantsClause();
10494     break;
10495   case llvm::omp::OMPC_nocontext:
10496     C = new (Context) OMPNocontextClause();
10497     break;
10498   case llvm::omp::OMPC_detach:
10499     C = new (Context) OMPDetachClause();
10500     break;
10501   case llvm::omp::OMPC_uses_allocators:
10502     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
10503     break;
10504   case llvm::omp::OMPC_affinity:
10505     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
10506     break;
10507   case llvm::omp::OMPC_filter:
10508     C = new (Context) OMPFilterClause();
10509     break;
10510   case llvm::omp::OMPC_bind:
10511     C = OMPBindClause::CreateEmpty(Context);
10512     break;
10513   case llvm::omp::OMPC_align:
10514     C = new (Context) OMPAlignClause();
10515     break;
10516   case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
10517     C = new (Context) OMPXDynCGroupMemClause();
10518     break;
10519   case llvm::omp::OMPC_doacross: {
10520     unsigned NumVars = Record.readInt();
10521     unsigned NumLoops = Record.readInt();
10522     C = OMPDoacrossClause::CreateEmpty(Context, NumVars, NumLoops);
10523     break;
10524   }
10525   case llvm::omp::OMPC_ompx_attribute:
10526     C = new (Context) OMPXAttributeClause();
10527     break;
10528   case llvm::omp::OMPC_ompx_bare:
10529     C = new (Context) OMPXBareClause();
10530     break;
10531 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
10532   case llvm::omp::Enum:                                                        \
10533     break;
10534 #include "llvm/Frontend/OpenMP/OMPKinds.def"
10535   default:
10536     break;
10537   }
10538   assert(C && "Unknown OMPClause type");
10539 
10540   Visit(C);
10541   C->setLocStart(Record.readSourceLocation());
10542   C->setLocEnd(Record.readSourceLocation());
10543 
10544   return C;
10545 }
10546 
10547 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
10548   C->setPreInitStmt(Record.readSubStmt(),
10549                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
10550 }
10551 
10552 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
10553   VisitOMPClauseWithPreInit(C);
10554   C->setPostUpdateExpr(Record.readSubExpr());
10555 }
10556 
10557 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
10558   VisitOMPClauseWithPreInit(C);
10559   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
10560   C->setNameModifierLoc(Record.readSourceLocation());
10561   C->setColonLoc(Record.readSourceLocation());
10562   C->setCondition(Record.readSubExpr());
10563   C->setLParenLoc(Record.readSourceLocation());
10564 }
10565 
10566 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
10567   VisitOMPClauseWithPreInit(C);
10568   C->setCondition(Record.readSubExpr());
10569   C->setLParenLoc(Record.readSourceLocation());
10570 }
10571 
10572 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
10573   VisitOMPClauseWithPreInit(C);
10574   C->setNumThreads(Record.readSubExpr());
10575   C->setLParenLoc(Record.readSourceLocation());
10576 }
10577 
10578 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
10579   C->setSafelen(Record.readSubExpr());
10580   C->setLParenLoc(Record.readSourceLocation());
10581 }
10582 
10583 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
10584   C->setSimdlen(Record.readSubExpr());
10585   C->setLParenLoc(Record.readSourceLocation());
10586 }
10587 
10588 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
10589   for (Expr *&E : C->getSizesRefs())
10590     E = Record.readSubExpr();
10591   C->setLParenLoc(Record.readSourceLocation());
10592 }
10593 
10594 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
10595 
10596 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
10597   C->setFactor(Record.readSubExpr());
10598   C->setLParenLoc(Record.readSourceLocation());
10599 }
10600 
10601 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
10602   C->setAllocator(Record.readExpr());
10603   C->setLParenLoc(Record.readSourceLocation());
10604 }
10605 
10606 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
10607   C->setNumForLoops(Record.readSubExpr());
10608   C->setLParenLoc(Record.readSourceLocation());
10609 }
10610 
10611 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
10612   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
10613   C->setLParenLoc(Record.readSourceLocation());
10614   C->setDefaultKindKwLoc(Record.readSourceLocation());
10615 }
10616 
10617 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
10618   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
10619   C->setLParenLoc(Record.readSourceLocation());
10620   C->setProcBindKindKwLoc(Record.readSourceLocation());
10621 }
10622 
10623 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
10624   VisitOMPClauseWithPreInit(C);
10625   C->setScheduleKind(
10626        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
10627   C->setFirstScheduleModifier(
10628       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10629   C->setSecondScheduleModifier(
10630       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
10631   C->setChunkSize(Record.readSubExpr());
10632   C->setLParenLoc(Record.readSourceLocation());
10633   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
10634   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
10635   C->setScheduleKindLoc(Record.readSourceLocation());
10636   C->setCommaLoc(Record.readSourceLocation());
10637 }
10638 
10639 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
10640   C->setNumForLoops(Record.readSubExpr());
10641   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10642     C->setLoopNumIterations(I, Record.readSubExpr());
10643   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
10644     C->setLoopCounter(I, Record.readSubExpr());
10645   C->setLParenLoc(Record.readSourceLocation());
10646 }
10647 
10648 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
10649   C->setEventHandler(Record.readSubExpr());
10650   C->setLParenLoc(Record.readSourceLocation());
10651 }
10652 
10653 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
10654 
10655 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
10656 
10657 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
10658 
10659 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
10660 
10661 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
10662 
10663 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
10664   if (C->isExtended()) {
10665     C->setLParenLoc(Record.readSourceLocation());
10666     C->setArgumentLoc(Record.readSourceLocation());
10667     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
10668   }
10669 }
10670 
10671 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
10672 
10673 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
10674 
10675 // Read the parameter of fail clause. This will have been saved when
10676 // OMPClauseWriter is called.
10677 void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
10678   C->setLParenLoc(Record.readSourceLocation());
10679   SourceLocation FailParameterLoc = Record.readSourceLocation();
10680   C->setFailParameterLoc(FailParameterLoc);
10681   OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
10682   C->setFailParameter(CKind);
10683 }
10684 
10685 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
10686 
10687 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
10688 
10689 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
10690 
10691 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
10692 
10693 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
10694 
10695 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
10696 
10697 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
10698 
10699 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
10700 
10701 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
10702   unsigned NumVars = C->varlist_size();
10703   SmallVector<Expr *, 16> Vars;
10704   Vars.reserve(NumVars);
10705   for (unsigned I = 0; I != NumVars; ++I)
10706     Vars.push_back(Record.readSubExpr());
10707   C->setVarRefs(Vars);
10708   C->setIsTarget(Record.readBool());
10709   C->setIsTargetSync(Record.readBool());
10710   C->setLParenLoc(Record.readSourceLocation());
10711   C->setVarLoc(Record.readSourceLocation());
10712 }
10713 
10714 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
10715   C->setInteropVar(Record.readSubExpr());
10716   C->setLParenLoc(Record.readSourceLocation());
10717   C->setVarLoc(Record.readSourceLocation());
10718 }
10719 
10720 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
10721   C->setInteropVar(Record.readSubExpr());
10722   C->setLParenLoc(Record.readSourceLocation());
10723   C->setVarLoc(Record.readSourceLocation());
10724 }
10725 
10726 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
10727   VisitOMPClauseWithPreInit(C);
10728   C->setCondition(Record.readSubExpr());
10729   C->setLParenLoc(Record.readSourceLocation());
10730 }
10731 
10732 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
10733   VisitOMPClauseWithPreInit(C);
10734   C->setCondition(Record.readSubExpr());
10735   C->setLParenLoc(Record.readSourceLocation());
10736 }
10737 
10738 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
10739 
10740 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
10741     OMPUnifiedSharedMemoryClause *) {}
10742 
10743 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
10744 
10745 void
10746 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
10747 }
10748 
10749 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
10750     OMPAtomicDefaultMemOrderClause *C) {
10751   C->setAtomicDefaultMemOrderKind(
10752       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
10753   C->setLParenLoc(Record.readSourceLocation());
10754   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
10755 }
10756 
10757 void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
10758   C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
10759   C->setLParenLoc(Record.readSourceLocation());
10760   C->setAtKindKwLoc(Record.readSourceLocation());
10761 }
10762 
10763 void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
10764   C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
10765   C->setLParenLoc(Record.readSourceLocation());
10766   C->setSeverityKindKwLoc(Record.readSourceLocation());
10767 }
10768 
10769 void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
10770   C->setMessageString(Record.readSubExpr());
10771   C->setLParenLoc(Record.readSourceLocation());
10772 }
10773 
10774 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
10775   C->setLParenLoc(Record.readSourceLocation());
10776   unsigned NumVars = C->varlist_size();
10777   SmallVector<Expr *, 16> Vars;
10778   Vars.reserve(NumVars);
10779   for (unsigned i = 0; i != NumVars; ++i)
10780     Vars.push_back(Record.readSubExpr());
10781   C->setVarRefs(Vars);
10782   Vars.clear();
10783   for (unsigned i = 0; i != NumVars; ++i)
10784     Vars.push_back(Record.readSubExpr());
10785   C->setPrivateCopies(Vars);
10786 }
10787 
10788 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
10789   VisitOMPClauseWithPreInit(C);
10790   C->setLParenLoc(Record.readSourceLocation());
10791   unsigned NumVars = C->varlist_size();
10792   SmallVector<Expr *, 16> Vars;
10793   Vars.reserve(NumVars);
10794   for (unsigned i = 0; i != NumVars; ++i)
10795     Vars.push_back(Record.readSubExpr());
10796   C->setVarRefs(Vars);
10797   Vars.clear();
10798   for (unsigned i = 0; i != NumVars; ++i)
10799     Vars.push_back(Record.readSubExpr());
10800   C->setPrivateCopies(Vars);
10801   Vars.clear();
10802   for (unsigned i = 0; i != NumVars; ++i)
10803     Vars.push_back(Record.readSubExpr());
10804   C->setInits(Vars);
10805 }
10806 
10807 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
10808   VisitOMPClauseWithPostUpdate(C);
10809   C->setLParenLoc(Record.readSourceLocation());
10810   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
10811   C->setKindLoc(Record.readSourceLocation());
10812   C->setColonLoc(Record.readSourceLocation());
10813   unsigned NumVars = C->varlist_size();
10814   SmallVector<Expr *, 16> Vars;
10815   Vars.reserve(NumVars);
10816   for (unsigned i = 0; i != NumVars; ++i)
10817     Vars.push_back(Record.readSubExpr());
10818   C->setVarRefs(Vars);
10819   Vars.clear();
10820   for (unsigned i = 0; i != NumVars; ++i)
10821     Vars.push_back(Record.readSubExpr());
10822   C->setPrivateCopies(Vars);
10823   Vars.clear();
10824   for (unsigned i = 0; i != NumVars; ++i)
10825     Vars.push_back(Record.readSubExpr());
10826   C->setSourceExprs(Vars);
10827   Vars.clear();
10828   for (unsigned i = 0; i != NumVars; ++i)
10829     Vars.push_back(Record.readSubExpr());
10830   C->setDestinationExprs(Vars);
10831   Vars.clear();
10832   for (unsigned i = 0; i != NumVars; ++i)
10833     Vars.push_back(Record.readSubExpr());
10834   C->setAssignmentOps(Vars);
10835 }
10836 
10837 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
10838   C->setLParenLoc(Record.readSourceLocation());
10839   unsigned NumVars = C->varlist_size();
10840   SmallVector<Expr *, 16> Vars;
10841   Vars.reserve(NumVars);
10842   for (unsigned i = 0; i != NumVars; ++i)
10843     Vars.push_back(Record.readSubExpr());
10844   C->setVarRefs(Vars);
10845 }
10846 
10847 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
10848   VisitOMPClauseWithPostUpdate(C);
10849   C->setLParenLoc(Record.readSourceLocation());
10850   C->setModifierLoc(Record.readSourceLocation());
10851   C->setColonLoc(Record.readSourceLocation());
10852   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10853   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10854   C->setQualifierLoc(NNSL);
10855   C->setNameInfo(DNI);
10856 
10857   unsigned NumVars = C->varlist_size();
10858   SmallVector<Expr *, 16> Vars;
10859   Vars.reserve(NumVars);
10860   for (unsigned i = 0; i != NumVars; ++i)
10861     Vars.push_back(Record.readSubExpr());
10862   C->setVarRefs(Vars);
10863   Vars.clear();
10864   for (unsigned i = 0; i != NumVars; ++i)
10865     Vars.push_back(Record.readSubExpr());
10866   C->setPrivates(Vars);
10867   Vars.clear();
10868   for (unsigned i = 0; i != NumVars; ++i)
10869     Vars.push_back(Record.readSubExpr());
10870   C->setLHSExprs(Vars);
10871   Vars.clear();
10872   for (unsigned i = 0; i != NumVars; ++i)
10873     Vars.push_back(Record.readSubExpr());
10874   C->setRHSExprs(Vars);
10875   Vars.clear();
10876   for (unsigned i = 0; i != NumVars; ++i)
10877     Vars.push_back(Record.readSubExpr());
10878   C->setReductionOps(Vars);
10879   if (C->getModifier() == OMPC_REDUCTION_inscan) {
10880     Vars.clear();
10881     for (unsigned i = 0; i != NumVars; ++i)
10882       Vars.push_back(Record.readSubExpr());
10883     C->setInscanCopyOps(Vars);
10884     Vars.clear();
10885     for (unsigned i = 0; i != NumVars; ++i)
10886       Vars.push_back(Record.readSubExpr());
10887     C->setInscanCopyArrayTemps(Vars);
10888     Vars.clear();
10889     for (unsigned i = 0; i != NumVars; ++i)
10890       Vars.push_back(Record.readSubExpr());
10891     C->setInscanCopyArrayElems(Vars);
10892   }
10893 }
10894 
10895 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
10896   VisitOMPClauseWithPostUpdate(C);
10897   C->setLParenLoc(Record.readSourceLocation());
10898   C->setColonLoc(Record.readSourceLocation());
10899   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10900   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10901   C->setQualifierLoc(NNSL);
10902   C->setNameInfo(DNI);
10903 
10904   unsigned NumVars = C->varlist_size();
10905   SmallVector<Expr *, 16> Vars;
10906   Vars.reserve(NumVars);
10907   for (unsigned I = 0; I != NumVars; ++I)
10908     Vars.push_back(Record.readSubExpr());
10909   C->setVarRefs(Vars);
10910   Vars.clear();
10911   for (unsigned I = 0; I != NumVars; ++I)
10912     Vars.push_back(Record.readSubExpr());
10913   C->setPrivates(Vars);
10914   Vars.clear();
10915   for (unsigned I = 0; I != NumVars; ++I)
10916     Vars.push_back(Record.readSubExpr());
10917   C->setLHSExprs(Vars);
10918   Vars.clear();
10919   for (unsigned I = 0; I != NumVars; ++I)
10920     Vars.push_back(Record.readSubExpr());
10921   C->setRHSExprs(Vars);
10922   Vars.clear();
10923   for (unsigned I = 0; I != NumVars; ++I)
10924     Vars.push_back(Record.readSubExpr());
10925   C->setReductionOps(Vars);
10926 }
10927 
10928 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
10929   VisitOMPClauseWithPostUpdate(C);
10930   C->setLParenLoc(Record.readSourceLocation());
10931   C->setColonLoc(Record.readSourceLocation());
10932   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
10933   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
10934   C->setQualifierLoc(NNSL);
10935   C->setNameInfo(DNI);
10936 
10937   unsigned NumVars = C->varlist_size();
10938   SmallVector<Expr *, 16> Vars;
10939   Vars.reserve(NumVars);
10940   for (unsigned I = 0; I != NumVars; ++I)
10941     Vars.push_back(Record.readSubExpr());
10942   C->setVarRefs(Vars);
10943   Vars.clear();
10944   for (unsigned I = 0; I != NumVars; ++I)
10945     Vars.push_back(Record.readSubExpr());
10946   C->setPrivates(Vars);
10947   Vars.clear();
10948   for (unsigned I = 0; I != NumVars; ++I)
10949     Vars.push_back(Record.readSubExpr());
10950   C->setLHSExprs(Vars);
10951   Vars.clear();
10952   for (unsigned I = 0; I != NumVars; ++I)
10953     Vars.push_back(Record.readSubExpr());
10954   C->setRHSExprs(Vars);
10955   Vars.clear();
10956   for (unsigned I = 0; I != NumVars; ++I)
10957     Vars.push_back(Record.readSubExpr());
10958   C->setReductionOps(Vars);
10959   Vars.clear();
10960   for (unsigned I = 0; I != NumVars; ++I)
10961     Vars.push_back(Record.readSubExpr());
10962   C->setTaskgroupDescriptors(Vars);
10963 }
10964 
10965 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
10966   VisitOMPClauseWithPostUpdate(C);
10967   C->setLParenLoc(Record.readSourceLocation());
10968   C->setColonLoc(Record.readSourceLocation());
10969   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
10970   C->setModifierLoc(Record.readSourceLocation());
10971   unsigned NumVars = C->varlist_size();
10972   SmallVector<Expr *, 16> Vars;
10973   Vars.reserve(NumVars);
10974   for (unsigned i = 0; i != NumVars; ++i)
10975     Vars.push_back(Record.readSubExpr());
10976   C->setVarRefs(Vars);
10977   Vars.clear();
10978   for (unsigned i = 0; i != NumVars; ++i)
10979     Vars.push_back(Record.readSubExpr());
10980   C->setPrivates(Vars);
10981   Vars.clear();
10982   for (unsigned i = 0; i != NumVars; ++i)
10983     Vars.push_back(Record.readSubExpr());
10984   C->setInits(Vars);
10985   Vars.clear();
10986   for (unsigned i = 0; i != NumVars; ++i)
10987     Vars.push_back(Record.readSubExpr());
10988   C->setUpdates(Vars);
10989   Vars.clear();
10990   for (unsigned i = 0; i != NumVars; ++i)
10991     Vars.push_back(Record.readSubExpr());
10992   C->setFinals(Vars);
10993   C->setStep(Record.readSubExpr());
10994   C->setCalcStep(Record.readSubExpr());
10995   Vars.clear();
10996   for (unsigned I = 0; I != NumVars + 1; ++I)
10997     Vars.push_back(Record.readSubExpr());
10998   C->setUsedExprs(Vars);
10999 }
11000 
11001 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
11002   C->setLParenLoc(Record.readSourceLocation());
11003   C->setColonLoc(Record.readSourceLocation());
11004   unsigned NumVars = C->varlist_size();
11005   SmallVector<Expr *, 16> Vars;
11006   Vars.reserve(NumVars);
11007   for (unsigned i = 0; i != NumVars; ++i)
11008     Vars.push_back(Record.readSubExpr());
11009   C->setVarRefs(Vars);
11010   C->setAlignment(Record.readSubExpr());
11011 }
11012 
11013 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
11014   C->setLParenLoc(Record.readSourceLocation());
11015   unsigned NumVars = C->varlist_size();
11016   SmallVector<Expr *, 16> Exprs;
11017   Exprs.reserve(NumVars);
11018   for (unsigned i = 0; i != NumVars; ++i)
11019     Exprs.push_back(Record.readSubExpr());
11020   C->setVarRefs(Exprs);
11021   Exprs.clear();
11022   for (unsigned i = 0; i != NumVars; ++i)
11023     Exprs.push_back(Record.readSubExpr());
11024   C->setSourceExprs(Exprs);
11025   Exprs.clear();
11026   for (unsigned i = 0; i != NumVars; ++i)
11027     Exprs.push_back(Record.readSubExpr());
11028   C->setDestinationExprs(Exprs);
11029   Exprs.clear();
11030   for (unsigned i = 0; i != NumVars; ++i)
11031     Exprs.push_back(Record.readSubExpr());
11032   C->setAssignmentOps(Exprs);
11033 }
11034 
11035 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11036   C->setLParenLoc(Record.readSourceLocation());
11037   unsigned NumVars = C->varlist_size();
11038   SmallVector<Expr *, 16> Exprs;
11039   Exprs.reserve(NumVars);
11040   for (unsigned i = 0; i != NumVars; ++i)
11041     Exprs.push_back(Record.readSubExpr());
11042   C->setVarRefs(Exprs);
11043   Exprs.clear();
11044   for (unsigned i = 0; i != NumVars; ++i)
11045     Exprs.push_back(Record.readSubExpr());
11046   C->setSourceExprs(Exprs);
11047   Exprs.clear();
11048   for (unsigned i = 0; i != NumVars; ++i)
11049     Exprs.push_back(Record.readSubExpr());
11050   C->setDestinationExprs(Exprs);
11051   Exprs.clear();
11052   for (unsigned i = 0; i != NumVars; ++i)
11053     Exprs.push_back(Record.readSubExpr());
11054   C->setAssignmentOps(Exprs);
11055 }
11056 
11057 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
11058   C->setLParenLoc(Record.readSourceLocation());
11059   unsigned NumVars = C->varlist_size();
11060   SmallVector<Expr *, 16> Vars;
11061   Vars.reserve(NumVars);
11062   for (unsigned i = 0; i != NumVars; ++i)
11063     Vars.push_back(Record.readSubExpr());
11064   C->setVarRefs(Vars);
11065 }
11066 
11067 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
11068   C->setDepobj(Record.readSubExpr());
11069   C->setLParenLoc(Record.readSourceLocation());
11070 }
11071 
11072 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
11073   C->setLParenLoc(Record.readSourceLocation());
11074   C->setModifier(Record.readSubExpr());
11075   C->setDependencyKind(
11076       static_cast<OpenMPDependClauseKind>(Record.readInt()));
11077   C->setDependencyLoc(Record.readSourceLocation());
11078   C->setColonLoc(Record.readSourceLocation());
11079   C->setOmpAllMemoryLoc(Record.readSourceLocation());
11080   unsigned NumVars = C->varlist_size();
11081   SmallVector<Expr *, 16> Vars;
11082   Vars.reserve(NumVars);
11083   for (unsigned I = 0; I != NumVars; ++I)
11084     Vars.push_back(Record.readSubExpr());
11085   C->setVarRefs(Vars);
11086   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11087     C->setLoopData(I, Record.readSubExpr());
11088 }
11089 
11090 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
11091   VisitOMPClauseWithPreInit(C);
11092   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
11093   C->setDevice(Record.readSubExpr());
11094   C->setModifierLoc(Record.readSourceLocation());
11095   C->setLParenLoc(Record.readSourceLocation());
11096 }
11097 
11098 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
11099   C->setLParenLoc(Record.readSourceLocation());
11100   bool HasIteratorModifier = false;
11101   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
11102     C->setMapTypeModifier(
11103         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
11104     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
11105     if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
11106       HasIteratorModifier = true;
11107   }
11108   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11109   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11110   C->setMapType(
11111      static_cast<OpenMPMapClauseKind>(Record.readInt()));
11112   C->setMapLoc(Record.readSourceLocation());
11113   C->setColonLoc(Record.readSourceLocation());
11114   auto NumVars = C->varlist_size();
11115   auto UniqueDecls = C->getUniqueDeclarationsNum();
11116   auto TotalLists = C->getTotalComponentListNum();
11117   auto TotalComponents = C->getTotalComponentsNum();
11118 
11119   SmallVector<Expr *, 16> Vars;
11120   Vars.reserve(NumVars);
11121   for (unsigned i = 0; i != NumVars; ++i)
11122     Vars.push_back(Record.readExpr());
11123   C->setVarRefs(Vars);
11124 
11125   SmallVector<Expr *, 16> UDMappers;
11126   UDMappers.reserve(NumVars);
11127   for (unsigned I = 0; I < NumVars; ++I)
11128     UDMappers.push_back(Record.readExpr());
11129   C->setUDMapperRefs(UDMappers);
11130 
11131   if (HasIteratorModifier)
11132     C->setIteratorModifier(Record.readExpr());
11133 
11134   SmallVector<ValueDecl *, 16> Decls;
11135   Decls.reserve(UniqueDecls);
11136   for (unsigned i = 0; i < UniqueDecls; ++i)
11137     Decls.push_back(Record.readDeclAs<ValueDecl>());
11138   C->setUniqueDecls(Decls);
11139 
11140   SmallVector<unsigned, 16> ListsPerDecl;
11141   ListsPerDecl.reserve(UniqueDecls);
11142   for (unsigned i = 0; i < UniqueDecls; ++i)
11143     ListsPerDecl.push_back(Record.readInt());
11144   C->setDeclNumLists(ListsPerDecl);
11145 
11146   SmallVector<unsigned, 32> ListSizes;
11147   ListSizes.reserve(TotalLists);
11148   for (unsigned i = 0; i < TotalLists; ++i)
11149     ListSizes.push_back(Record.readInt());
11150   C->setComponentListSizes(ListSizes);
11151 
11152   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11153   Components.reserve(TotalComponents);
11154   for (unsigned i = 0; i < TotalComponents; ++i) {
11155     Expr *AssociatedExprPr = Record.readExpr();
11156     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11157     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11158                             /*IsNonContiguous=*/false);
11159   }
11160   C->setComponents(Components, ListSizes);
11161 }
11162 
11163 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
11164   C->setLParenLoc(Record.readSourceLocation());
11165   C->setColonLoc(Record.readSourceLocation());
11166   C->setAllocator(Record.readSubExpr());
11167   unsigned NumVars = C->varlist_size();
11168   SmallVector<Expr *, 16> Vars;
11169   Vars.reserve(NumVars);
11170   for (unsigned i = 0; i != NumVars; ++i)
11171     Vars.push_back(Record.readSubExpr());
11172   C->setVarRefs(Vars);
11173 }
11174 
11175 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
11176   VisitOMPClauseWithPreInit(C);
11177   C->setNumTeams(Record.readSubExpr());
11178   C->setLParenLoc(Record.readSourceLocation());
11179 }
11180 
11181 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
11182   VisitOMPClauseWithPreInit(C);
11183   C->setThreadLimit(Record.readSubExpr());
11184   C->setLParenLoc(Record.readSourceLocation());
11185 }
11186 
11187 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
11188   VisitOMPClauseWithPreInit(C);
11189   C->setPriority(Record.readSubExpr());
11190   C->setLParenLoc(Record.readSourceLocation());
11191 }
11192 
11193 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
11194   VisitOMPClauseWithPreInit(C);
11195   C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
11196   C->setGrainsize(Record.readSubExpr());
11197   C->setModifierLoc(Record.readSourceLocation());
11198   C->setLParenLoc(Record.readSourceLocation());
11199 }
11200 
11201 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
11202   VisitOMPClauseWithPreInit(C);
11203   C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
11204   C->setNumTasks(Record.readSubExpr());
11205   C->setModifierLoc(Record.readSourceLocation());
11206   C->setLParenLoc(Record.readSourceLocation());
11207 }
11208 
11209 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
11210   C->setHint(Record.readSubExpr());
11211   C->setLParenLoc(Record.readSourceLocation());
11212 }
11213 
11214 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
11215   VisitOMPClauseWithPreInit(C);
11216   C->setDistScheduleKind(
11217       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
11218   C->setChunkSize(Record.readSubExpr());
11219   C->setLParenLoc(Record.readSourceLocation());
11220   C->setDistScheduleKindLoc(Record.readSourceLocation());
11221   C->setCommaLoc(Record.readSourceLocation());
11222 }
11223 
11224 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
11225   C->setDefaultmapKind(
11226        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
11227   C->setDefaultmapModifier(
11228       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
11229   C->setLParenLoc(Record.readSourceLocation());
11230   C->setDefaultmapModifierLoc(Record.readSourceLocation());
11231   C->setDefaultmapKindLoc(Record.readSourceLocation());
11232 }
11233 
11234 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
11235   C->setLParenLoc(Record.readSourceLocation());
11236   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11237     C->setMotionModifier(
11238         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11239     C->setMotionModifierLoc(I, Record.readSourceLocation());
11240   }
11241   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11242   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11243   C->setColonLoc(Record.readSourceLocation());
11244   auto NumVars = C->varlist_size();
11245   auto UniqueDecls = C->getUniqueDeclarationsNum();
11246   auto TotalLists = C->getTotalComponentListNum();
11247   auto TotalComponents = C->getTotalComponentsNum();
11248 
11249   SmallVector<Expr *, 16> Vars;
11250   Vars.reserve(NumVars);
11251   for (unsigned i = 0; i != NumVars; ++i)
11252     Vars.push_back(Record.readSubExpr());
11253   C->setVarRefs(Vars);
11254 
11255   SmallVector<Expr *, 16> UDMappers;
11256   UDMappers.reserve(NumVars);
11257   for (unsigned I = 0; I < NumVars; ++I)
11258     UDMappers.push_back(Record.readSubExpr());
11259   C->setUDMapperRefs(UDMappers);
11260 
11261   SmallVector<ValueDecl *, 16> Decls;
11262   Decls.reserve(UniqueDecls);
11263   for (unsigned i = 0; i < UniqueDecls; ++i)
11264     Decls.push_back(Record.readDeclAs<ValueDecl>());
11265   C->setUniqueDecls(Decls);
11266 
11267   SmallVector<unsigned, 16> ListsPerDecl;
11268   ListsPerDecl.reserve(UniqueDecls);
11269   for (unsigned i = 0; i < UniqueDecls; ++i)
11270     ListsPerDecl.push_back(Record.readInt());
11271   C->setDeclNumLists(ListsPerDecl);
11272 
11273   SmallVector<unsigned, 32> ListSizes;
11274   ListSizes.reserve(TotalLists);
11275   for (unsigned i = 0; i < TotalLists; ++i)
11276     ListSizes.push_back(Record.readInt());
11277   C->setComponentListSizes(ListSizes);
11278 
11279   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11280   Components.reserve(TotalComponents);
11281   for (unsigned i = 0; i < TotalComponents; ++i) {
11282     Expr *AssociatedExprPr = Record.readSubExpr();
11283     bool IsNonContiguous = Record.readBool();
11284     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11285     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11286   }
11287   C->setComponents(Components, ListSizes);
11288 }
11289 
11290 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
11291   C->setLParenLoc(Record.readSourceLocation());
11292   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
11293     C->setMotionModifier(
11294         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
11295     C->setMotionModifierLoc(I, Record.readSourceLocation());
11296   }
11297   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
11298   C->setMapperIdInfo(Record.readDeclarationNameInfo());
11299   C->setColonLoc(Record.readSourceLocation());
11300   auto NumVars = C->varlist_size();
11301   auto UniqueDecls = C->getUniqueDeclarationsNum();
11302   auto TotalLists = C->getTotalComponentListNum();
11303   auto TotalComponents = C->getTotalComponentsNum();
11304 
11305   SmallVector<Expr *, 16> Vars;
11306   Vars.reserve(NumVars);
11307   for (unsigned i = 0; i != NumVars; ++i)
11308     Vars.push_back(Record.readSubExpr());
11309   C->setVarRefs(Vars);
11310 
11311   SmallVector<Expr *, 16> UDMappers;
11312   UDMappers.reserve(NumVars);
11313   for (unsigned I = 0; I < NumVars; ++I)
11314     UDMappers.push_back(Record.readSubExpr());
11315   C->setUDMapperRefs(UDMappers);
11316 
11317   SmallVector<ValueDecl *, 16> Decls;
11318   Decls.reserve(UniqueDecls);
11319   for (unsigned i = 0; i < UniqueDecls; ++i)
11320     Decls.push_back(Record.readDeclAs<ValueDecl>());
11321   C->setUniqueDecls(Decls);
11322 
11323   SmallVector<unsigned, 16> ListsPerDecl;
11324   ListsPerDecl.reserve(UniqueDecls);
11325   for (unsigned i = 0; i < UniqueDecls; ++i)
11326     ListsPerDecl.push_back(Record.readInt());
11327   C->setDeclNumLists(ListsPerDecl);
11328 
11329   SmallVector<unsigned, 32> ListSizes;
11330   ListSizes.reserve(TotalLists);
11331   for (unsigned i = 0; i < TotalLists; ++i)
11332     ListSizes.push_back(Record.readInt());
11333   C->setComponentListSizes(ListSizes);
11334 
11335   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11336   Components.reserve(TotalComponents);
11337   for (unsigned i = 0; i < TotalComponents; ++i) {
11338     Expr *AssociatedExprPr = Record.readSubExpr();
11339     bool IsNonContiguous = Record.readBool();
11340     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11341     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
11342   }
11343   C->setComponents(Components, ListSizes);
11344 }
11345 
11346 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
11347   C->setLParenLoc(Record.readSourceLocation());
11348   auto NumVars = C->varlist_size();
11349   auto UniqueDecls = C->getUniqueDeclarationsNum();
11350   auto TotalLists = C->getTotalComponentListNum();
11351   auto TotalComponents = C->getTotalComponentsNum();
11352 
11353   SmallVector<Expr *, 16> Vars;
11354   Vars.reserve(NumVars);
11355   for (unsigned i = 0; i != NumVars; ++i)
11356     Vars.push_back(Record.readSubExpr());
11357   C->setVarRefs(Vars);
11358   Vars.clear();
11359   for (unsigned i = 0; i != NumVars; ++i)
11360     Vars.push_back(Record.readSubExpr());
11361   C->setPrivateCopies(Vars);
11362   Vars.clear();
11363   for (unsigned i = 0; i != NumVars; ++i)
11364     Vars.push_back(Record.readSubExpr());
11365   C->setInits(Vars);
11366 
11367   SmallVector<ValueDecl *, 16> Decls;
11368   Decls.reserve(UniqueDecls);
11369   for (unsigned i = 0; i < UniqueDecls; ++i)
11370     Decls.push_back(Record.readDeclAs<ValueDecl>());
11371   C->setUniqueDecls(Decls);
11372 
11373   SmallVector<unsigned, 16> ListsPerDecl;
11374   ListsPerDecl.reserve(UniqueDecls);
11375   for (unsigned i = 0; i < UniqueDecls; ++i)
11376     ListsPerDecl.push_back(Record.readInt());
11377   C->setDeclNumLists(ListsPerDecl);
11378 
11379   SmallVector<unsigned, 32> ListSizes;
11380   ListSizes.reserve(TotalLists);
11381   for (unsigned i = 0; i < TotalLists; ++i)
11382     ListSizes.push_back(Record.readInt());
11383   C->setComponentListSizes(ListSizes);
11384 
11385   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11386   Components.reserve(TotalComponents);
11387   for (unsigned i = 0; i < TotalComponents; ++i) {
11388     auto *AssociatedExprPr = Record.readSubExpr();
11389     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11390     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
11391                             /*IsNonContiguous=*/false);
11392   }
11393   C->setComponents(Components, ListSizes);
11394 }
11395 
11396 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
11397   C->setLParenLoc(Record.readSourceLocation());
11398   auto NumVars = C->varlist_size();
11399   auto UniqueDecls = C->getUniqueDeclarationsNum();
11400   auto TotalLists = C->getTotalComponentListNum();
11401   auto TotalComponents = C->getTotalComponentsNum();
11402 
11403   SmallVector<Expr *, 16> Vars;
11404   Vars.reserve(NumVars);
11405   for (unsigned i = 0; i != NumVars; ++i)
11406     Vars.push_back(Record.readSubExpr());
11407   C->setVarRefs(Vars);
11408 
11409   SmallVector<ValueDecl *, 16> Decls;
11410   Decls.reserve(UniqueDecls);
11411   for (unsigned i = 0; i < UniqueDecls; ++i)
11412     Decls.push_back(Record.readDeclAs<ValueDecl>());
11413   C->setUniqueDecls(Decls);
11414 
11415   SmallVector<unsigned, 16> ListsPerDecl;
11416   ListsPerDecl.reserve(UniqueDecls);
11417   for (unsigned i = 0; i < UniqueDecls; ++i)
11418     ListsPerDecl.push_back(Record.readInt());
11419   C->setDeclNumLists(ListsPerDecl);
11420 
11421   SmallVector<unsigned, 32> ListSizes;
11422   ListSizes.reserve(TotalLists);
11423   for (unsigned i = 0; i < TotalLists; ++i)
11424     ListSizes.push_back(Record.readInt());
11425   C->setComponentListSizes(ListSizes);
11426 
11427   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11428   Components.reserve(TotalComponents);
11429   for (unsigned i = 0; i < TotalComponents; ++i) {
11430     Expr *AssociatedExpr = Record.readSubExpr();
11431     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11432     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11433                             /*IsNonContiguous*/ false);
11434   }
11435   C->setComponents(Components, ListSizes);
11436 }
11437 
11438 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
11439   C->setLParenLoc(Record.readSourceLocation());
11440   auto NumVars = C->varlist_size();
11441   auto UniqueDecls = C->getUniqueDeclarationsNum();
11442   auto TotalLists = C->getTotalComponentListNum();
11443   auto TotalComponents = C->getTotalComponentsNum();
11444 
11445   SmallVector<Expr *, 16> Vars;
11446   Vars.reserve(NumVars);
11447   for (unsigned i = 0; i != NumVars; ++i)
11448     Vars.push_back(Record.readSubExpr());
11449   C->setVarRefs(Vars);
11450   Vars.clear();
11451 
11452   SmallVector<ValueDecl *, 16> Decls;
11453   Decls.reserve(UniqueDecls);
11454   for (unsigned i = 0; i < UniqueDecls; ++i)
11455     Decls.push_back(Record.readDeclAs<ValueDecl>());
11456   C->setUniqueDecls(Decls);
11457 
11458   SmallVector<unsigned, 16> ListsPerDecl;
11459   ListsPerDecl.reserve(UniqueDecls);
11460   for (unsigned i = 0; i < UniqueDecls; ++i)
11461     ListsPerDecl.push_back(Record.readInt());
11462   C->setDeclNumLists(ListsPerDecl);
11463 
11464   SmallVector<unsigned, 32> ListSizes;
11465   ListSizes.reserve(TotalLists);
11466   for (unsigned i = 0; i < TotalLists; ++i)
11467     ListSizes.push_back(Record.readInt());
11468   C->setComponentListSizes(ListSizes);
11469 
11470   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11471   Components.reserve(TotalComponents);
11472   for (unsigned i = 0; i < TotalComponents; ++i) {
11473     Expr *AssociatedExpr = Record.readSubExpr();
11474     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11475     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11476                             /*IsNonContiguous=*/false);
11477   }
11478   C->setComponents(Components, ListSizes);
11479 }
11480 
11481 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
11482   C->setLParenLoc(Record.readSourceLocation());
11483   auto NumVars = C->varlist_size();
11484   auto UniqueDecls = C->getUniqueDeclarationsNum();
11485   auto TotalLists = C->getTotalComponentListNum();
11486   auto TotalComponents = C->getTotalComponentsNum();
11487 
11488   SmallVector<Expr *, 16> Vars;
11489   Vars.reserve(NumVars);
11490   for (unsigned I = 0; I != NumVars; ++I)
11491     Vars.push_back(Record.readSubExpr());
11492   C->setVarRefs(Vars);
11493   Vars.clear();
11494 
11495   SmallVector<ValueDecl *, 16> Decls;
11496   Decls.reserve(UniqueDecls);
11497   for (unsigned I = 0; I < UniqueDecls; ++I)
11498     Decls.push_back(Record.readDeclAs<ValueDecl>());
11499   C->setUniqueDecls(Decls);
11500 
11501   SmallVector<unsigned, 16> ListsPerDecl;
11502   ListsPerDecl.reserve(UniqueDecls);
11503   for (unsigned I = 0; I < UniqueDecls; ++I)
11504     ListsPerDecl.push_back(Record.readInt());
11505   C->setDeclNumLists(ListsPerDecl);
11506 
11507   SmallVector<unsigned, 32> ListSizes;
11508   ListSizes.reserve(TotalLists);
11509   for (unsigned i = 0; i < TotalLists; ++i)
11510     ListSizes.push_back(Record.readInt());
11511   C->setComponentListSizes(ListSizes);
11512 
11513   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
11514   Components.reserve(TotalComponents);
11515   for (unsigned I = 0; I < TotalComponents; ++I) {
11516     Expr *AssociatedExpr = Record.readSubExpr();
11517     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
11518     Components.emplace_back(AssociatedExpr, AssociatedDecl,
11519                             /*IsNonContiguous=*/false);
11520   }
11521   C->setComponents(Components, ListSizes);
11522 }
11523 
11524 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
11525   C->setLParenLoc(Record.readSourceLocation());
11526   unsigned NumVars = C->varlist_size();
11527   SmallVector<Expr *, 16> Vars;
11528   Vars.reserve(NumVars);
11529   for (unsigned i = 0; i != NumVars; ++i)
11530     Vars.push_back(Record.readSubExpr());
11531   C->setVarRefs(Vars);
11532   Vars.clear();
11533   Vars.reserve(NumVars);
11534   for (unsigned i = 0; i != NumVars; ++i)
11535     Vars.push_back(Record.readSubExpr());
11536   C->setPrivateRefs(Vars);
11537 }
11538 
11539 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
11540   C->setLParenLoc(Record.readSourceLocation());
11541   unsigned NumVars = C->varlist_size();
11542   SmallVector<Expr *, 16> Vars;
11543   Vars.reserve(NumVars);
11544   for (unsigned i = 0; i != NumVars; ++i)
11545     Vars.push_back(Record.readSubExpr());
11546   C->setVarRefs(Vars);
11547 }
11548 
11549 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
11550   C->setLParenLoc(Record.readSourceLocation());
11551   unsigned NumVars = C->varlist_size();
11552   SmallVector<Expr *, 16> Vars;
11553   Vars.reserve(NumVars);
11554   for (unsigned i = 0; i != NumVars; ++i)
11555     Vars.push_back(Record.readSubExpr());
11556   C->setVarRefs(Vars);
11557 }
11558 
11559 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
11560   C->setLParenLoc(Record.readSourceLocation());
11561   unsigned NumOfAllocators = C->getNumberOfAllocators();
11562   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
11563   Data.reserve(NumOfAllocators);
11564   for (unsigned I = 0; I != NumOfAllocators; ++I) {
11565     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
11566     D.Allocator = Record.readSubExpr();
11567     D.AllocatorTraits = Record.readSubExpr();
11568     D.LParenLoc = Record.readSourceLocation();
11569     D.RParenLoc = Record.readSourceLocation();
11570   }
11571   C->setAllocatorsData(Data);
11572 }
11573 
11574 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
11575   C->setLParenLoc(Record.readSourceLocation());
11576   C->setModifier(Record.readSubExpr());
11577   C->setColonLoc(Record.readSourceLocation());
11578   unsigned NumOfLocators = C->varlist_size();
11579   SmallVector<Expr *, 4> Locators;
11580   Locators.reserve(NumOfLocators);
11581   for (unsigned I = 0; I != NumOfLocators; ++I)
11582     Locators.push_back(Record.readSubExpr());
11583   C->setVarRefs(Locators);
11584 }
11585 
11586 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
11587   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
11588   C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
11589   C->setLParenLoc(Record.readSourceLocation());
11590   C->setKindKwLoc(Record.readSourceLocation());
11591   C->setModifierKwLoc(Record.readSourceLocation());
11592 }
11593 
11594 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
11595   VisitOMPClauseWithPreInit(C);
11596   C->setThreadID(Record.readSubExpr());
11597   C->setLParenLoc(Record.readSourceLocation());
11598 }
11599 
11600 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
11601   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
11602   C->setLParenLoc(Record.readSourceLocation());
11603   C->setBindKindLoc(Record.readSourceLocation());
11604 }
11605 
11606 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
11607   C->setAlignment(Record.readExpr());
11608   C->setLParenLoc(Record.readSourceLocation());
11609 }
11610 
11611 void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
11612   VisitOMPClauseWithPreInit(C);
11613   C->setSize(Record.readSubExpr());
11614   C->setLParenLoc(Record.readSourceLocation());
11615 }
11616 
11617 void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
11618   C->setLParenLoc(Record.readSourceLocation());
11619   C->setDependenceType(
11620       static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
11621   C->setDependenceLoc(Record.readSourceLocation());
11622   C->setColonLoc(Record.readSourceLocation());
11623   unsigned NumVars = C->varlist_size();
11624   SmallVector<Expr *, 16> Vars;
11625   Vars.reserve(NumVars);
11626   for (unsigned I = 0; I != NumVars; ++I)
11627     Vars.push_back(Record.readSubExpr());
11628   C->setVarRefs(Vars);
11629   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
11630     C->setLoopData(I, Record.readSubExpr());
11631 }
11632 
11633 void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
11634   AttrVec Attrs;
11635   Record.readAttributes(Attrs);
11636   C->setAttrs(Attrs);
11637   C->setLocStart(Record.readSourceLocation());
11638   C->setLParenLoc(Record.readSourceLocation());
11639   C->setLocEnd(Record.readSourceLocation());
11640 }
11641 
11642 void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
11643 
11644 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
11645   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
11646   TI.Sets.resize(readUInt32());
11647   for (auto &Set : TI.Sets) {
11648     Set.Kind = readEnum<llvm::omp::TraitSet>();
11649     Set.Selectors.resize(readUInt32());
11650     for (auto &Selector : Set.Selectors) {
11651       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
11652       Selector.ScoreOrCondition = nullptr;
11653       if (readBool())
11654         Selector.ScoreOrCondition = readExprRef();
11655       Selector.Properties.resize(readUInt32());
11656       for (auto &Property : Selector.Properties)
11657         Property.Kind = readEnum<llvm::omp::TraitProperty>();
11658     }
11659   }
11660   return &TI;
11661 }
11662 
11663 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
11664   if (!Data)
11665     return;
11666   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
11667     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
11668     skipInts(3);
11669   }
11670   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
11671   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
11672     Clauses[I] = readOMPClause();
11673   Data->setClauses(Clauses);
11674   if (Data->hasAssociatedStmt())
11675     Data->setAssociatedStmt(readStmt());
11676   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
11677     Data->getChildren()[I] = readStmt();
11678 }
11679