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 "clang/Serialization/ASTReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/CommentOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/DiagnosticOptions.h"
43 #include "clang/Basic/ExceptionSpecificationType.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/FileSystemOptions.h"
46 #include "clang/Basic/IdentifierTable.h"
47 #include "clang/Basic/LLVM.h"
48 #include "clang/Basic/LangOptions.h"
49 #include "clang/Basic/Module.h"
50 #include "clang/Basic/ObjCRuntime.h"
51 #include "clang/Basic/OperatorKinds.h"
52 #include "clang/Basic/PragmaKinds.h"
53 #include "clang/Basic/Sanitizers.h"
54 #include "clang/Basic/SourceLocation.h"
55 #include "clang/Basic/SourceManager.h"
56 #include "clang/Basic/SourceManagerInternals.h"
57 #include "clang/Basic/Specifiers.h"
58 #include "clang/Basic/TargetInfo.h"
59 #include "clang/Basic/TargetOptions.h"
60 #include "clang/Basic/TokenKinds.h"
61 #include "clang/Basic/Version.h"
62 #include "clang/Lex/HeaderSearch.h"
63 #include "clang/Lex/HeaderSearchOptions.h"
64 #include "clang/Lex/MacroInfo.h"
65 #include "clang/Lex/ModuleMap.h"
66 #include "clang/Lex/PreprocessingRecord.h"
67 #include "clang/Lex/Preprocessor.h"
68 #include "clang/Lex/PreprocessorOptions.h"
69 #include "clang/Lex/Token.h"
70 #include "clang/Sema/ObjCMethodList.h"
71 #include "clang/Sema/Scope.h"
72 #include "clang/Sema/Sema.h"
73 #include "clang/Sema/Weak.h"
74 #include "clang/Serialization/ASTBitCodes.h"
75 #include "clang/Serialization/ASTDeserializationListener.h"
76 #include "clang/Serialization/ContinuousRangeMap.h"
77 #include "clang/Serialization/GlobalModuleIndex.h"
78 #include "clang/Serialization/InMemoryModuleCache.h"
79 #include "clang/Serialization/Module.h"
80 #include "clang/Serialization/ModuleFileExtension.h"
81 #include "clang/Serialization/ModuleManager.h"
82 #include "clang/Serialization/PCHContainerOperations.h"
83 #include "clang/Serialization/SerializationDiagnostic.h"
84 #include "llvm/ADT/APFloat.h"
85 #include "llvm/ADT/APInt.h"
86 #include "llvm/ADT/APSInt.h"
87 #include "llvm/ADT/ArrayRef.h"
88 #include "llvm/ADT/DenseMap.h"
89 #include "llvm/ADT/FoldingSet.h"
90 #include "llvm/ADT/Hashing.h"
91 #include "llvm/ADT/IntrusiveRefCntPtr.h"
92 #include "llvm/ADT/None.h"
93 #include "llvm/ADT/Optional.h"
94 #include "llvm/ADT/STLExtras.h"
95 #include "llvm/ADT/ScopeExit.h"
96 #include "llvm/ADT/SmallPtrSet.h"
97 #include "llvm/ADT/SmallString.h"
98 #include "llvm/ADT/SmallVector.h"
99 #include "llvm/ADT/StringExtras.h"
100 #include "llvm/ADT/StringMap.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/ADT/iterator_range.h"
104 #include "llvm/Bitstream/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
119 #include <algorithm>
120 #include <cassert>
121 #include <cstddef>
122 #include <cstdint>
123 #include <cstdio>
124 #include <ctime>
125 #include <iterator>
126 #include <limits>
127 #include <map>
128 #include <memory>
129 #include <string>
130 #include <system_error>
131 #include <tuple>
132 #include <utility>
133 #include <vector>
134 
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
139 
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
143 
144 bool
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146   return First->ReadFullVersionInformation(FullVersion) ||
147          Second->ReadFullVersionInformation(FullVersion);
148 }
149 
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151   First->ReadModuleName(ModuleName);
152   Second->ReadModuleName(ModuleName);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156   First->ReadModuleMapFile(ModuleMapPath);
157   Second->ReadModuleMapFile(ModuleMapPath);
158 }
159 
160 bool
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
162                                               bool Complain,
163                                               bool AllowCompatibleDifferences) {
164   return First->ReadLanguageOptions(LangOpts, Complain,
165                                     AllowCompatibleDifferences) ||
166          Second->ReadLanguageOptions(LangOpts, Complain,
167                                      AllowCompatibleDifferences);
168 }
169 
170 bool ChainedASTReaderListener::ReadTargetOptions(
171     const TargetOptions &TargetOpts, bool Complain,
172     bool AllowCompatibleDifferences) {
173   return First->ReadTargetOptions(TargetOpts, Complain,
174                                   AllowCompatibleDifferences) ||
175          Second->ReadTargetOptions(TargetOpts, Complain,
176                                    AllowCompatibleDifferences);
177 }
178 
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182          Second->ReadDiagnosticOptions(DiagOpts, Complain);
183 }
184 
185 bool
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
187                                                 bool Complain) {
188   return First->ReadFileSystemOptions(FSOpts, Complain) ||
189          Second->ReadFileSystemOptions(FSOpts, Complain);
190 }
191 
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
194     bool Complain) {
195   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
196                                         Complain) ||
197          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198                                          Complain);
199 }
200 
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202     const PreprocessorOptions &PPOpts, bool Complain,
203     std::string &SuggestedPredefines) {
204   return First->ReadPreprocessorOptions(PPOpts, Complain,
205                                         SuggestedPredefines) ||
206          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
207 }
208 
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
210                                            unsigned Value) {
211   First->ReadCounter(M, Value);
212   Second->ReadCounter(M, Value);
213 }
214 
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216   return First->needsInputFileVisitation() ||
217          Second->needsInputFileVisitation();
218 }
219 
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221   return First->needsSystemInputFileVisitation() ||
222   Second->needsSystemInputFileVisitation();
223 }
224 
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
226                                                ModuleKind Kind) {
227   First->visitModuleFile(Filename, Kind);
228   Second->visitModuleFile(Filename, Kind);
229 }
230 
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
232                                               bool isSystem,
233                                               bool isOverridden,
234                                               bool isExplicitModule) {
235   bool Continue = false;
236   if (First->needsInputFileVisitation() &&
237       (!isSystem || First->needsSystemInputFileVisitation()))
238     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
239                                       isExplicitModule);
240   if (Second->needsInputFileVisitation() &&
241       (!isSystem || Second->needsSystemInputFileVisitation()))
242     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
243                                        isExplicitModule);
244   return Continue;
245 }
246 
247 void ChainedASTReaderListener::readModuleFileExtension(
248        const ModuleFileExtensionMetadata &Metadata) {
249   First->readModuleFileExtension(Metadata);
250   Second->readModuleFileExtension(Metadata);
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
256 
257 ASTReaderListener::~ASTReaderListener() = default;
258 
259 /// Compare the given set of language options against an existing set of
260 /// language options.
261 ///
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 ///        language options will be permitted.
265 ///
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268                                  const LangOptions &ExistingLangOpts,
269                                  DiagnosticsEngine *Diags,
270                                  bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description)                 \
272   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
273     if (Diags)                                                    \
274       Diags->Report(diag::err_pch_langopt_mismatch)               \
275         << Description << LangOpts.Name << ExistingLangOpts.Name; \
276     return true;                                                  \
277   }
278 
279 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
280   if (ExistingLangOpts.Name != LangOpts.Name) {           \
281     if (Diags)                                            \
282       Diags->Report(diag::err_pch_langopt_value_mismatch) \
283         << Description;                                   \
284     return true;                                          \
285   }
286 
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
288   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
289     if (Diags)                                                 \
290       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
291         << Description;                                        \
292     return true;                                               \
293   }
294 
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
296   if (!AllowCompatibleDifferences)                            \
297     LANGOPT(Name, Bits, Default, Description)
298 
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
300   if (!AllowCompatibleDifferences)                                 \
301     ENUM_LANGOPT(Name, Bits, Default, Description)
302 
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304   if (!AllowCompatibleDifferences)                                 \
305     VALUE_LANGOPT(Name, Bits, Default, Description)
306 
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
311 
312   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
313     if (Diags)
314       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
315     return true;
316   }
317 
318   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch)
321       << "target Objective-C runtime";
322     return true;
323   }
324 
325   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326       LangOpts.CommentOpts.BlockCommandNames) {
327     if (Diags)
328       Diags->Report(diag::err_pch_langopt_value_mismatch)
329         << "block command names";
330     return true;
331   }
332 
333   // Sanitizer feature mismatches are treated as compatible differences. If
334   // compatible differences aren't allowed, we still only want to check for
335   // mismatches of non-modular sanitizers (the only ones which can affect AST
336   // generation).
337   if (!AllowCompatibleDifferences) {
338     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341     ExistingSanitizers.clear(ModularSanitizers);
342     ImportedSanitizers.clear(ModularSanitizers);
343     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344       const std::string Flag = "-fsanitize=";
345       if (Diags) {
346 #define SANITIZER(NAME, ID)                                                    \
347   {                                                                            \
348     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
349     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
350     if (InExistingModule != InImportedModule)                                  \
351       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
352           << InExistingModule << (Flag + NAME);                                \
353   }
354 #include "clang/Basic/Sanitizers.def"
355       }
356       return true;
357     }
358   }
359 
360   return false;
361 }
362 
363 /// Compare the given set of target options against an existing set of
364 /// target options.
365 ///
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
367 ///
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370                                const TargetOptions &ExistingTargetOpts,
371                                DiagnosticsEngine *Diags,
372                                bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name)                             \
374   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
375     if (Diags)                                                    \
376       Diags->Report(diag::err_pch_targetopt_mismatch)             \
377         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
378     return true;                                                  \
379   }
380 
381   // The triple and ABI must match exactly.
382   CHECK_TARGET_OPT(Triple, "target");
383   CHECK_TARGET_OPT(ABI, "target ABI");
384 
385   // We can tolerate different CPUs in many cases, notably when one CPU
386   // supports a strict superset of another. When allowing compatible
387   // differences skip this check.
388   if (!AllowCompatibleDifferences)
389     CHECK_TARGET_OPT(CPU, "target CPU");
390 
391 #undef CHECK_TARGET_OPT
392 
393   // Compare feature sets.
394   SmallVector<StringRef, 4> ExistingFeatures(
395                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
396                                              ExistingTargetOpts.FeaturesAsWritten.end());
397   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398                                          TargetOpts.FeaturesAsWritten.end());
399   llvm::sort(ExistingFeatures);
400   llvm::sort(ReadFeatures);
401 
402   // We compute the set difference in both directions explicitly so that we can
403   // diagnose the differences differently.
404   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
405   std::set_difference(
406       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409                       ExistingFeatures.begin(), ExistingFeatures.end(),
410                       std::back_inserter(UnmatchedReadFeatures));
411 
412   // If we are allowing compatible differences and the read feature set is
413   // a strict subset of the existing feature set, there is nothing to diagnose.
414   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
415     return false;
416 
417   if (Diags) {
418     for (StringRef Feature : UnmatchedReadFeatures)
419       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420           << /* is-existing-feature */ false << Feature;
421     for (StringRef Feature : UnmatchedExistingFeatures)
422       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423           << /* is-existing-feature */ true << Feature;
424   }
425 
426   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
427 }
428 
429 bool
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
431                                   bool Complain,
432                                   bool AllowCompatibleDifferences) {
433   const LangOptions &ExistingLangOpts = PP.getLangOpts();
434   return checkLanguageOptions(LangOpts, ExistingLangOpts,
435                               Complain ? &Reader.Diags : nullptr,
436                               AllowCompatibleDifferences);
437 }
438 
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
440                                      bool Complain,
441                                      bool AllowCompatibleDifferences) {
442   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444                             Complain ? &Reader.Diags : nullptr,
445                             AllowCompatibleDifferences);
446 }
447 
448 namespace {
449 
450 using MacroDefinitionsMap =
451     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
453 
454 } // namespace
455 
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457                                          DiagnosticsEngine &Diags,
458                                          bool Complain) {
459   using Level = DiagnosticsEngine::Level;
460 
461   // Check current mappings for new -Werror mappings, and the stored mappings
462   // for cases that were explicitly mapped to *not* be errors that are now
463   // errors because of options like -Werror.
464   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
465 
466   for (DiagnosticsEngine *MappingSource : MappingSources) {
467     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468       diag::kind DiagID = DiagIDMappingPair.first;
469       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470       if (CurLevel < DiagnosticsEngine::Error)
471         continue; // not significant
472       Level StoredLevel =
473           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474       if (StoredLevel < DiagnosticsEngine::Error) {
475         if (Complain)
476           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
478         return true;
479       }
480     }
481   }
482 
483   return false;
484 }
485 
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
489     return true;
490   return Ext >= diag::Severity::Error;
491 }
492 
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494                                     DiagnosticsEngine &Diags,
495                                     bool IsSystem, bool Complain) {
496   // Top-level options
497   if (IsSystem) {
498     if (Diags.getSuppressSystemWarnings())
499       return false;
500     // If -Wsystem-headers was not enabled before, be conservative
501     if (StoredDiags.getSuppressSystemWarnings()) {
502       if (Complain)
503         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
504       return true;
505     }
506   }
507 
508   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
509     if (Complain)
510       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
511     return true;
512   }
513 
514   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515       !StoredDiags.getEnableAllWarnings()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
518     return true;
519   }
520 
521   if (isExtHandlingFromDiagsError(Diags) &&
522       !isExtHandlingFromDiagsError(StoredDiags)) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
525     return true;
526   }
527 
528   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
529 }
530 
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
533                                           Preprocessor &PP) {
534   // If the original import came from a file explicitly generated by the user,
535   // don't check the diagnostic mappings.
536   // FIXME: currently this is approximated by checking whether this is not a
537   // module import of an implicitly-loaded module file.
538   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539   // the transitive closure of its imports, since unrelated modules cannot be
540   // imported until after this module finishes validation.
541   ModuleFile *TopImport = &*ModuleMgr.rbegin();
542   while (!TopImport->ImportedBy.empty())
543     TopImport = TopImport->ImportedBy[0];
544   if (TopImport->Kind != MK_ImplicitModule)
545     return nullptr;
546 
547   StringRef ModuleName = TopImport->ModuleName;
548   assert(!ModuleName.empty() && "diagnostic options read before module name");
549 
550   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551   assert(M && "missing module");
552   return M;
553 }
554 
555 bool PCHValidator::ReadDiagnosticOptions(
556     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561   // This should never fail, because we would have processed these options
562   // before writing them to an ASTFile.
563   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
564 
565   ModuleManager &ModuleMgr = Reader.getModuleManager();
566   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
567 
568   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
569   if (!TopM)
570     return false;
571 
572   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573   // contains the union of their flags.
574   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
575                                  Complain);
576 }
577 
578 /// Collect the macro definitions provided by the given preprocessor
579 /// options.
580 static void
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582                         MacroDefinitionsMap &Macros,
583                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585     StringRef Macro = PPOpts.Macros[I].first;
586     bool IsUndef = PPOpts.Macros[I].second;
587 
588     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589     StringRef MacroName = MacroPair.first;
590     StringRef MacroBody = MacroPair.second;
591 
592     // For an #undef'd macro, we only care about the name.
593     if (IsUndef) {
594       if (MacroNames && !Macros.count(MacroName))
595         MacroNames->push_back(MacroName);
596 
597       Macros[MacroName] = std::make_pair("", true);
598       continue;
599     }
600 
601     // For a #define'd macro, figure out the actual definition.
602     if (MacroName.size() == Macro.size())
603       MacroBody = "1";
604     else {
605       // Note: GCC drops anything following an end-of-line character.
606       StringRef::size_type End = MacroBody.find_first_of("\n\r");
607       MacroBody = MacroBody.substr(0, End);
608     }
609 
610     if (MacroNames && !Macros.count(MacroName))
611       MacroNames->push_back(MacroName);
612     Macros[MacroName] = std::make_pair(MacroBody, false);
613   }
614 }
615 
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
618 ///
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 ///        macros defined by \p ExistingPPOpts to override those defined by
622 ///        \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624                                      const PreprocessorOptions &ExistingPPOpts,
625                                      DiagnosticsEngine *Diags,
626                                      FileManager &FileMgr,
627                                      std::string &SuggestedPredefines,
628                                      const LangOptions &LangOpts,
629                                      bool Validate = true) {
630   // Check macro definitions.
631   MacroDefinitionsMap ASTFileMacros;
632   collectMacroDefinitions(PPOpts, ASTFileMacros);
633   MacroDefinitionsMap ExistingMacros;
634   SmallVector<StringRef, 4> ExistingMacroNames;
635   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
636 
637   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638     // Dig out the macro definition in the existing preprocessor options.
639     StringRef MacroName = ExistingMacroNames[I];
640     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
641 
642     // Check whether we know anything about this macro name or not.
643     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644         ASTFileMacros.find(MacroName);
645     if (!Validate || Known == ASTFileMacros.end()) {
646       // FIXME: Check whether this identifier was referenced anywhere in the
647       // AST file. If so, we should reject the AST file. Unfortunately, this
648       // information isn't in the control block. What shall we do about it?
649 
650       if (Existing.second) {
651         SuggestedPredefines += "#undef ";
652         SuggestedPredefines += MacroName.str();
653         SuggestedPredefines += '\n';
654       } else {
655         SuggestedPredefines += "#define ";
656         SuggestedPredefines += MacroName.str();
657         SuggestedPredefines += ' ';
658         SuggestedPredefines += Existing.first.str();
659         SuggestedPredefines += '\n';
660       }
661       continue;
662     }
663 
664     // If the macro was defined in one but undef'd in the other, we have a
665     // conflict.
666     if (Existing.second != Known->second.second) {
667       if (Diags) {
668         Diags->Report(diag::err_pch_macro_def_undef)
669           << MacroName << Known->second.second;
670       }
671       return true;
672     }
673 
674     // If the macro was #undef'd in both, or if the macro bodies are identical,
675     // it's fine.
676     if (Existing.second || Existing.first == Known->second.first)
677       continue;
678 
679     // The macro bodies differ; complain.
680     if (Diags) {
681       Diags->Report(diag::err_pch_macro_def_conflict)
682         << MacroName << Known->second.first << Existing.first;
683     }
684     return true;
685   }
686 
687   // Check whether we're using predefines.
688   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
689     if (Diags) {
690       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
691     }
692     return true;
693   }
694 
695   // Detailed record is important since it is used for the module cache hash.
696   if (LangOpts.Modules &&
697       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
700     }
701     return true;
702   }
703 
704   // Compute the #include and #include_macros lines we need.
705   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706     StringRef File = ExistingPPOpts.Includes[I];
707 
708     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709         !ExistingPPOpts.PCHThroughHeader.empty()) {
710       // In case the through header is an include, we must add all the includes
711       // to the predefines so the start point can be determined.
712       SuggestedPredefines += "#include \"";
713       SuggestedPredefines += File;
714       SuggestedPredefines += "\"\n";
715       continue;
716     }
717 
718     if (File == ExistingPPOpts.ImplicitPCHInclude)
719       continue;
720 
721     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722           != PPOpts.Includes.end())
723       continue;
724 
725     SuggestedPredefines += "#include \"";
726     SuggestedPredefines += File;
727     SuggestedPredefines += "\"\n";
728   }
729 
730   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731     StringRef File = ExistingPPOpts.MacroIncludes[I];
732     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
733                   File)
734         != PPOpts.MacroIncludes.end())
735       continue;
736 
737     SuggestedPredefines += "#__include_macros \"";
738     SuggestedPredefines += File;
739     SuggestedPredefines += "\"\n##\n";
740   }
741 
742   return false;
743 }
744 
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
746                                            bool Complain,
747                                            std::string &SuggestedPredefines) {
748   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
749 
750   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751                                   Complain? &Reader.Diags : nullptr,
752                                   PP.getFileManager(),
753                                   SuggestedPredefines,
754                                   PP.getLangOpts());
755 }
756 
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758                                   const PreprocessorOptions &PPOpts,
759                                   bool Complain,
760                                   std::string &SuggestedPredefines) {
761   return checkPreprocessorOptions(PPOpts,
762                                   PP.getPreprocessorOpts(),
763                                   nullptr,
764                                   PP.getFileManager(),
765                                   SuggestedPredefines,
766                                   PP.getLangOpts(),
767                                   false);
768 }
769 
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
772 ///
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775                                      StringRef SpecificModuleCachePath,
776                                      StringRef ExistingModuleCachePath,
777                                      DiagnosticsEngine *Diags,
778                                      const LangOptions &LangOpts) {
779   if (LangOpts.Modules) {
780     if (SpecificModuleCachePath != ExistingModuleCachePath) {
781       if (Diags)
782         Diags->Report(diag::err_pch_modulecache_mismatch)
783           << SpecificModuleCachePath << ExistingModuleCachePath;
784       return true;
785     }
786   }
787 
788   return false;
789 }
790 
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792                                            StringRef SpecificModuleCachePath,
793                                            bool Complain) {
794   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795                                   PP.getHeaderSearchInfo().getModuleCachePath(),
796                                   Complain ? &Reader.Diags : nullptr,
797                                   PP.getLangOpts());
798 }
799 
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801   PP.setCounterValue(Value);
802 }
803 
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
807 
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809                                            bool TakeOwnership) {
810   DeserializationListener = Listener;
811   OwnsDeserializationListener = TakeOwnership;
812 }
813 
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815   return serialization::ComputeHash(Sel);
816 }
817 
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820   using namespace llvm::support;
821 
822   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824   return std::make_pair(KeyLen, DataLen);
825 }
826 
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829   using namespace llvm::support;
830 
831   SelectorTable &SelTable = Reader.getContext().Selectors;
832   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834       F, endian::readNext<uint32_t, little, unaligned>(d));
835   if (N == 0)
836     return SelTable.getNullarySelector(FirstII);
837   else if (N == 1)
838     return SelTable.getUnarySelector(FirstII);
839 
840   SmallVector<IdentifierInfo *, 16> Args;
841   Args.push_back(FirstII);
842   for (unsigned I = 1; I != N; ++I)
843     Args.push_back(Reader.getLocalIdentifier(
844         F, endian::readNext<uint32_t, little, unaligned>(d)));
845 
846   return SelTable.getSelector(N, Args.data());
847 }
848 
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
851                                  unsigned DataLen) {
852   using namespace llvm::support;
853 
854   data_type Result;
855 
856   Result.ID = Reader.getGlobalSelectorID(
857       F, endian::readNext<uint32_t, little, unaligned>(d));
858   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860   Result.InstanceBits = FullInstanceBits & 0x3;
861   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862   Result.FactoryBits = FullFactoryBits & 0x3;
863   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864   unsigned NumInstanceMethods = FullInstanceBits >> 3;
865   unsigned NumFactoryMethods = FullFactoryBits >> 3;
866 
867   // Load instance methods
868   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870             F, endian::readNext<uint32_t, little, unaligned>(d)))
871       Result.Instance.push_back(Method);
872   }
873 
874   // Load factory methods
875   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Factory.push_back(Method);
879   }
880 
881   return Result;
882 }
883 
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885   return llvm::djbHash(a);
886 }
887 
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890   using namespace llvm::support;
891 
892   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894   return std::make_pair(KeyLen, DataLen);
895 }
896 
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899   assert(n >= 2 && d[n-1] == '\0');
900   return StringRef((const char*) d, n-1);
901 }
902 
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
905                                     bool IsModule) {
906   return II.hadMacroDefinition() ||
907          II.isPoisoned() ||
908          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909          II.hasRevertedTokenIDToIdentifier() ||
910          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911           II.getFETokenInfo());
912 }
913 
914 static bool readBit(unsigned &Bits) {
915   bool Value = Bits & 0x1;
916   Bits >>= 1;
917   return Value;
918 }
919 
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921   using namespace llvm::support;
922 
923   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924   return Reader.getGlobalIdentifierID(F, RawID >> 1);
925 }
926 
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928   if (!II.isFromAST()) {
929     II.setIsFromAST();
930     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931     if (isInterestingIdentifier(Reader, II, IsModule))
932       II.setChangedSinceDeserialization();
933   }
934 }
935 
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937                                                    const unsigned char* d,
938                                                    unsigned DataLen) {
939   using namespace llvm::support;
940 
941   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942   bool IsInteresting = RawID & 0x01;
943 
944   // Wipe out the "is interesting" bit.
945   RawID = RawID >> 1;
946 
947   // Build the IdentifierInfo and link the identifier ID with it.
948   IdentifierInfo *II = KnownII;
949   if (!II) {
950     II = &Reader.getIdentifierTable().getOwn(k);
951     KnownII = II;
952   }
953   markIdentifierFromAST(Reader, *II);
954   Reader.markIdentifierUpToDate(II);
955 
956   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957   if (!IsInteresting) {
958     // For uninteresting identifiers, there's nothing else to do. Just notify
959     // the reader that we've finished loading this identifier.
960     Reader.SetIdentifierInfo(ID, II);
961     return II;
962   }
963 
964   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966   bool CPlusPlusOperatorKeyword = readBit(Bits);
967   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968   bool HasRevertedBuiltin = readBit(Bits);
969   bool Poisoned = readBit(Bits);
970   bool ExtensionToken = readBit(Bits);
971   bool HadMacroDefinition = readBit(Bits);
972 
973   assert(Bits == 0 && "Extra bits in the identifier?");
974   DataLen -= 8;
975 
976   // Set or check the various bits in the IdentifierInfo structure.
977   // Token IDs are read-only.
978   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979     II->revertTokenIDToIdentifier();
980   if (!F.isModule())
981     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982   else if (HasRevertedBuiltin && II->getBuiltinID()) {
983     II->revertBuiltin();
984     assert((II->hasRevertedBuiltin() ||
985             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986            "Incorrect ObjC keyword or builtin ID");
987   }
988   assert(II->isExtensionToken() == ExtensionToken &&
989          "Incorrect extension token flag");
990   (void)ExtensionToken;
991   if (Poisoned)
992     II->setIsPoisoned(true);
993   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994          "Incorrect C++ operator keyword flag");
995   (void)CPlusPlusOperatorKeyword;
996 
997   // If this identifier is a macro, deserialize the macro
998   // definition.
999   if (HadMacroDefinition) {
1000     uint32_t MacroDirectivesOffset =
1001         endian::readNext<uint32_t, little, unaligned>(d);
1002     DataLen -= 4;
1003 
1004     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1005   }
1006 
1007   Reader.SetIdentifierInfo(ID, II);
1008 
1009   // Read all of the declarations visible at global scope with this
1010   // name.
1011   if (DataLen > 0) {
1012     SmallVector<uint32_t, 4> DeclIDs;
1013     for (; DataLen > 0; DataLen -= 4)
1014       DeclIDs.push_back(Reader.getGlobalDeclID(
1015           F, endian::readNext<uint32_t, little, unaligned>(d)));
1016     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1017   }
1018 
1019   return II;
1020 }
1021 
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023     : Kind(Name.getNameKind()) {
1024   switch (Kind) {
1025   case DeclarationName::Identifier:
1026     Data = (uint64_t)Name.getAsIdentifierInfo();
1027     break;
1028   case DeclarationName::ObjCZeroArgSelector:
1029   case DeclarationName::ObjCOneArgSelector:
1030   case DeclarationName::ObjCMultiArgSelector:
1031     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1032     break;
1033   case DeclarationName::CXXOperatorName:
1034     Data = Name.getCXXOverloadedOperator();
1035     break;
1036   case DeclarationName::CXXLiteralOperatorName:
1037     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1038     break;
1039   case DeclarationName::CXXDeductionGuideName:
1040     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041                ->getDeclName().getAsIdentifierInfo();
1042     break;
1043   case DeclarationName::CXXConstructorName:
1044   case DeclarationName::CXXDestructorName:
1045   case DeclarationName::CXXConversionFunctionName:
1046   case DeclarationName::CXXUsingDirective:
1047     Data = 0;
1048     break;
1049   }
1050 }
1051 
1052 unsigned DeclarationNameKey::getHash() const {
1053   llvm::FoldingSetNodeID ID;
1054   ID.AddInteger(Kind);
1055 
1056   switch (Kind) {
1057   case DeclarationName::Identifier:
1058   case DeclarationName::CXXLiteralOperatorName:
1059   case DeclarationName::CXXDeductionGuideName:
1060     ID.AddString(((IdentifierInfo*)Data)->getName());
1061     break;
1062   case DeclarationName::ObjCZeroArgSelector:
1063   case DeclarationName::ObjCOneArgSelector:
1064   case DeclarationName::ObjCMultiArgSelector:
1065     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1066     break;
1067   case DeclarationName::CXXOperatorName:
1068     ID.AddInteger((OverloadedOperatorKind)Data);
1069     break;
1070   case DeclarationName::CXXConstructorName:
1071   case DeclarationName::CXXDestructorName:
1072   case DeclarationName::CXXConversionFunctionName:
1073   case DeclarationName::CXXUsingDirective:
1074     break;
1075   }
1076 
1077   return ID.ComputeHash();
1078 }
1079 
1080 ModuleFile *
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082   using namespace llvm::support;
1083 
1084   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085   return Reader.getLocalModuleFile(F, ModuleFileID);
1086 }
1087 
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090   using namespace llvm::support;
1091 
1092   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094   return std::make_pair(KeyLen, DataLen);
1095 }
1096 
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099   using namespace llvm::support;
1100 
1101   auto Kind = (DeclarationName::NameKind)*d++;
1102   uint64_t Data;
1103   switch (Kind) {
1104   case DeclarationName::Identifier:
1105   case DeclarationName::CXXLiteralOperatorName:
1106   case DeclarationName::CXXDeductionGuideName:
1107     Data = (uint64_t)Reader.getLocalIdentifier(
1108         F, endian::readNext<uint32_t, little, unaligned>(d));
1109     break;
1110   case DeclarationName::ObjCZeroArgSelector:
1111   case DeclarationName::ObjCOneArgSelector:
1112   case DeclarationName::ObjCMultiArgSelector:
1113     Data =
1114         (uint64_t)Reader.getLocalSelector(
1115                              F, endian::readNext<uint32_t, little, unaligned>(
1116                                     d)).getAsOpaquePtr();
1117     break;
1118   case DeclarationName::CXXOperatorName:
1119     Data = *d++; // OverloadedOperatorKind
1120     break;
1121   case DeclarationName::CXXConstructorName:
1122   case DeclarationName::CXXDestructorName:
1123   case DeclarationName::CXXConversionFunctionName:
1124   case DeclarationName::CXXUsingDirective:
1125     Data = 0;
1126     break;
1127   }
1128 
1129   return DeclarationNameKey(Kind, Data);
1130 }
1131 
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133                                                  const unsigned char *d,
1134                                                  unsigned DataLen,
1135                                                  data_type_builder &Val) {
1136   using namespace llvm::support;
1137 
1138   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1141   }
1142 }
1143 
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145                                               BitstreamCursor &Cursor,
1146                                               uint64_t Offset,
1147                                               DeclContext *DC) {
1148   assert(Offset != 0);
1149 
1150   SavedStreamPosition SavedPosition(Cursor);
1151   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1152     Error(std::move(Err));
1153     return true;
1154   }
1155 
1156   RecordData Record;
1157   StringRef Blob;
1158   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1159   if (!MaybeCode) {
1160     Error(MaybeCode.takeError());
1161     return true;
1162   }
1163   unsigned Code = MaybeCode.get();
1164 
1165   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1166   if (!MaybeRecCode) {
1167     Error(MaybeRecCode.takeError());
1168     return true;
1169   }
1170   unsigned RecCode = MaybeRecCode.get();
1171   if (RecCode != DECL_CONTEXT_LEXICAL) {
1172     Error("Expected lexical block");
1173     return true;
1174   }
1175 
1176   assert(!isa<TranslationUnitDecl>(DC) &&
1177          "expected a TU_UPDATE_LEXICAL record for TU");
1178   // If we are handling a C++ class template instantiation, we can see multiple
1179   // lexical updates for the same record. It's important that we select only one
1180   // of them, so that field numbering works properly. Just pick the first one we
1181   // see.
1182   auto &Lex = LexicalDecls[DC];
1183   if (!Lex.first) {
1184     Lex = std::make_pair(
1185         &M, llvm::makeArrayRef(
1186                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1187                     Blob.data()),
1188                 Blob.size() / 4));
1189   }
1190   DC->setHasExternalLexicalStorage(true);
1191   return false;
1192 }
1193 
1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1195                                               BitstreamCursor &Cursor,
1196                                               uint64_t Offset,
1197                                               DeclID ID) {
1198   assert(Offset != 0);
1199 
1200   SavedStreamPosition SavedPosition(Cursor);
1201   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1202     Error(std::move(Err));
1203     return true;
1204   }
1205 
1206   RecordData Record;
1207   StringRef Blob;
1208   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1209   if (!MaybeCode) {
1210     Error(MaybeCode.takeError());
1211     return true;
1212   }
1213   unsigned Code = MaybeCode.get();
1214 
1215   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1216   if (!MaybeRecCode) {
1217     Error(MaybeRecCode.takeError());
1218     return true;
1219   }
1220   unsigned RecCode = MaybeRecCode.get();
1221   if (RecCode != DECL_CONTEXT_VISIBLE) {
1222     Error("Expected visible lookup table block");
1223     return true;
1224   }
1225 
1226   // We can't safely determine the primary context yet, so delay attaching the
1227   // lookup table until we're done with recursive deserialization.
1228   auto *Data = (const unsigned char*)Blob.data();
1229   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1230   return false;
1231 }
1232 
1233 void ASTReader::Error(StringRef Msg) const {
1234   Error(diag::err_fe_pch_malformed, Msg);
1235   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1236       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1237     Diag(diag::note_module_cache_path)
1238       << PP.getHeaderSearchInfo().getModuleCachePath();
1239   }
1240 }
1241 
1242 void ASTReader::Error(unsigned DiagID,
1243                       StringRef Arg1, StringRef Arg2) const {
1244   if (Diags.isDiagnosticInFlight())
1245     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1246   else
1247     Diag(DiagID) << Arg1 << Arg2;
1248 }
1249 
1250 void ASTReader::Error(llvm::Error &&Err) const {
1251   Error(toString(std::move(Err)));
1252 }
1253 
1254 //===----------------------------------------------------------------------===//
1255 // Source Manager Deserialization
1256 //===----------------------------------------------------------------------===//
1257 
1258 /// Read the line table in the source manager block.
1259 /// \returns true if there was an error.
1260 bool ASTReader::ParseLineTable(ModuleFile &F,
1261                                const RecordData &Record) {
1262   unsigned Idx = 0;
1263   LineTableInfo &LineTable = SourceMgr.getLineTable();
1264 
1265   // Parse the file names
1266   std::map<int, int> FileIDs;
1267   FileIDs[-1] = -1; // For unspecified filenames.
1268   for (unsigned I = 0; Record[Idx]; ++I) {
1269     // Extract the file name
1270     auto Filename = ReadPath(F, Record, Idx);
1271     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1272   }
1273   ++Idx;
1274 
1275   // Parse the line entries
1276   std::vector<LineEntry> Entries;
1277   while (Idx < Record.size()) {
1278     int FID = Record[Idx++];
1279     assert(FID >= 0 && "Serialized line entries for non-local file.");
1280     // Remap FileID from 1-based old view.
1281     FID += F.SLocEntryBaseID - 1;
1282 
1283     // Extract the line entries
1284     unsigned NumEntries = Record[Idx++];
1285     assert(NumEntries && "no line entries for file ID");
1286     Entries.clear();
1287     Entries.reserve(NumEntries);
1288     for (unsigned I = 0; I != NumEntries; ++I) {
1289       unsigned FileOffset = Record[Idx++];
1290       unsigned LineNo = Record[Idx++];
1291       int FilenameID = FileIDs[Record[Idx++]];
1292       SrcMgr::CharacteristicKind FileKind
1293         = (SrcMgr::CharacteristicKind)Record[Idx++];
1294       unsigned IncludeOffset = Record[Idx++];
1295       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1296                                        FileKind, IncludeOffset));
1297     }
1298     LineTable.AddEntry(FileID::get(FID), Entries);
1299   }
1300 
1301   return false;
1302 }
1303 
1304 /// Read a source manager block
1305 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1306   using namespace SrcMgr;
1307 
1308   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1309 
1310   // Set the source-location entry cursor to the current position in
1311   // the stream. This cursor will be used to read the contents of the
1312   // source manager block initially, and then lazily read
1313   // source-location entries as needed.
1314   SLocEntryCursor = F.Stream;
1315 
1316   // The stream itself is going to skip over the source manager block.
1317   if (llvm::Error Err = F.Stream.SkipBlock()) {
1318     Error(std::move(Err));
1319     return true;
1320   }
1321 
1322   // Enter the source manager block.
1323   if (llvm::Error Err =
1324           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1325     Error(std::move(Err));
1326     return true;
1327   }
1328 
1329   RecordData Record;
1330   while (true) {
1331     Expected<llvm::BitstreamEntry> MaybeE =
1332         SLocEntryCursor.advanceSkippingSubblocks();
1333     if (!MaybeE) {
1334       Error(MaybeE.takeError());
1335       return true;
1336     }
1337     llvm::BitstreamEntry E = MaybeE.get();
1338 
1339     switch (E.Kind) {
1340     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1341     case llvm::BitstreamEntry::Error:
1342       Error("malformed block record in AST file");
1343       return true;
1344     case llvm::BitstreamEntry::EndBlock:
1345       return false;
1346     case llvm::BitstreamEntry::Record:
1347       // The interesting case.
1348       break;
1349     }
1350 
1351     // Read a record.
1352     Record.clear();
1353     StringRef Blob;
1354     Expected<unsigned> MaybeRecord =
1355         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1356     if (!MaybeRecord) {
1357       Error(MaybeRecord.takeError());
1358       return true;
1359     }
1360     switch (MaybeRecord.get()) {
1361     default:  // Default behavior: ignore.
1362       break;
1363 
1364     case SM_SLOC_FILE_ENTRY:
1365     case SM_SLOC_BUFFER_ENTRY:
1366     case SM_SLOC_EXPANSION_ENTRY:
1367       // Once we hit one of the source location entries, we're done.
1368       return false;
1369     }
1370   }
1371 }
1372 
1373 /// If a header file is not found at the path that we expect it to be
1374 /// and the PCH file was moved from its original location, try to resolve the
1375 /// file by assuming that header+PCH were moved together and the header is in
1376 /// the same place relative to the PCH.
1377 static std::string
1378 resolveFileRelativeToOriginalDir(const std::string &Filename,
1379                                  const std::string &OriginalDir,
1380                                  const std::string &CurrDir) {
1381   assert(OriginalDir != CurrDir &&
1382          "No point trying to resolve the file if the PCH dir didn't change");
1383 
1384   using namespace llvm::sys;
1385 
1386   SmallString<128> filePath(Filename);
1387   fs::make_absolute(filePath);
1388   assert(path::is_absolute(OriginalDir));
1389   SmallString<128> currPCHPath(CurrDir);
1390 
1391   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1392                        fileDirE = path::end(path::parent_path(filePath));
1393   path::const_iterator origDirI = path::begin(OriginalDir),
1394                        origDirE = path::end(OriginalDir);
1395   // Skip the common path components from filePath and OriginalDir.
1396   while (fileDirI != fileDirE && origDirI != origDirE &&
1397          *fileDirI == *origDirI) {
1398     ++fileDirI;
1399     ++origDirI;
1400   }
1401   for (; origDirI != origDirE; ++origDirI)
1402     path::append(currPCHPath, "..");
1403   path::append(currPCHPath, fileDirI, fileDirE);
1404   path::append(currPCHPath, path::filename(Filename));
1405   return currPCHPath.str();
1406 }
1407 
1408 bool ASTReader::ReadSLocEntry(int ID) {
1409   if (ID == 0)
1410     return false;
1411 
1412   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1413     Error("source location entry ID out-of-range for AST file");
1414     return true;
1415   }
1416 
1417   // Local helper to read the (possibly-compressed) buffer data following the
1418   // entry record.
1419   auto ReadBuffer = [this](
1420       BitstreamCursor &SLocEntryCursor,
1421       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1422     RecordData Record;
1423     StringRef Blob;
1424     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1425     if (!MaybeCode) {
1426       Error(MaybeCode.takeError());
1427       return nullptr;
1428     }
1429     unsigned Code = MaybeCode.get();
1430 
1431     Expected<unsigned> MaybeRecCode =
1432         SLocEntryCursor.readRecord(Code, Record, &Blob);
1433     if (!MaybeRecCode) {
1434       Error(MaybeRecCode.takeError());
1435       return nullptr;
1436     }
1437     unsigned RecCode = MaybeRecCode.get();
1438 
1439     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1440       if (!llvm::zlib::isAvailable()) {
1441         Error("zlib is not available");
1442         return nullptr;
1443       }
1444       SmallString<0> Uncompressed;
1445       if (llvm::Error E =
1446               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1447         Error("could not decompress embedded file contents: " +
1448               llvm::toString(std::move(E)));
1449         return nullptr;
1450       }
1451       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1452     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1453       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1454     } else {
1455       Error("AST record has invalid code");
1456       return nullptr;
1457     }
1458   };
1459 
1460   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1461   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1462           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1463     Error(std::move(Err));
1464     return true;
1465   }
1466 
1467   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1468   unsigned BaseOffset = F->SLocEntryBaseOffset;
1469 
1470   ++NumSLocEntriesRead;
1471   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1472   if (!MaybeEntry) {
1473     Error(MaybeEntry.takeError());
1474     return true;
1475   }
1476   llvm::BitstreamEntry Entry = MaybeEntry.get();
1477 
1478   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1479     Error("incorrectly-formatted source location entry in AST file");
1480     return true;
1481   }
1482 
1483   RecordData Record;
1484   StringRef Blob;
1485   Expected<unsigned> MaybeSLOC =
1486       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1487   if (!MaybeSLOC) {
1488     Error(MaybeSLOC.takeError());
1489     return true;
1490   }
1491   switch (MaybeSLOC.get()) {
1492   default:
1493     Error("incorrectly-formatted source location entry in AST file");
1494     return true;
1495 
1496   case SM_SLOC_FILE_ENTRY: {
1497     // We will detect whether a file changed and return 'Failure' for it, but
1498     // we will also try to fail gracefully by setting up the SLocEntry.
1499     unsigned InputID = Record[4];
1500     InputFile IF = getInputFile(*F, InputID);
1501     const FileEntry *File = IF.getFile();
1502     bool OverriddenBuffer = IF.isOverridden();
1503 
1504     // Note that we only check if a File was returned. If it was out-of-date
1505     // we have complained but we will continue creating a FileID to recover
1506     // gracefully.
1507     if (!File)
1508       return true;
1509 
1510     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1511     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1512       // This is the module's main file.
1513       IncludeLoc = getImportLocation(F);
1514     }
1515     SrcMgr::CharacteristicKind
1516       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1517     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1518                                         ID, BaseOffset + Record[0]);
1519     SrcMgr::FileInfo &FileInfo =
1520           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1521     FileInfo.NumCreatedFIDs = Record[5];
1522     if (Record[3])
1523       FileInfo.setHasLineDirectives();
1524 
1525     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1526     unsigned NumFileDecls = Record[7];
1527     if (NumFileDecls && ContextObj) {
1528       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1529       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1530                                                              NumFileDecls));
1531     }
1532 
1533     const SrcMgr::ContentCache *ContentCache
1534       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1535     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1536         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1537         !ContentCache->getRawBuffer()) {
1538       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1539       if (!Buffer)
1540         return true;
1541       SourceMgr.overrideFileContents(File, std::move(Buffer));
1542     }
1543 
1544     break;
1545   }
1546 
1547   case SM_SLOC_BUFFER_ENTRY: {
1548     const char *Name = Blob.data();
1549     unsigned Offset = Record[0];
1550     SrcMgr::CharacteristicKind
1551       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1552     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1553     if (IncludeLoc.isInvalid() && F->isModule()) {
1554       IncludeLoc = getImportLocation(F);
1555     }
1556 
1557     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1558     if (!Buffer)
1559       return true;
1560     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1561                            BaseOffset + Offset, IncludeLoc);
1562     break;
1563   }
1564 
1565   case SM_SLOC_EXPANSION_ENTRY: {
1566     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1567     SourceMgr.createExpansionLoc(SpellingLoc,
1568                                      ReadSourceLocation(*F, Record[2]),
1569                                      ReadSourceLocation(*F, Record[3]),
1570                                      Record[5],
1571                                      Record[4],
1572                                      ID,
1573                                      BaseOffset + Record[0]);
1574     break;
1575   }
1576   }
1577 
1578   return false;
1579 }
1580 
1581 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1582   if (ID == 0)
1583     return std::make_pair(SourceLocation(), "");
1584 
1585   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1586     Error("source location entry ID out-of-range for AST file");
1587     return std::make_pair(SourceLocation(), "");
1588   }
1589 
1590   // Find which module file this entry lands in.
1591   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1592   if (!M->isModule())
1593     return std::make_pair(SourceLocation(), "");
1594 
1595   // FIXME: Can we map this down to a particular submodule? That would be
1596   // ideal.
1597   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1598 }
1599 
1600 /// Find the location where the module F is imported.
1601 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1602   if (F->ImportLoc.isValid())
1603     return F->ImportLoc;
1604 
1605   // Otherwise we have a PCH. It's considered to be "imported" at the first
1606   // location of its includer.
1607   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1608     // Main file is the importer.
1609     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1610     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1611   }
1612   return F->ImportedBy[0]->FirstLoc;
1613 }
1614 
1615 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1616 /// the abbreviations that are at the top of the block and then leave the cursor
1617 /// pointing into the block.
1618 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1619   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1620     // FIXME this drops errors on the floor.
1621     consumeError(std::move(Err));
1622     return true;
1623   }
1624 
1625   while (true) {
1626     uint64_t Offset = Cursor.GetCurrentBitNo();
1627     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1628     if (!MaybeCode) {
1629       // FIXME this drops errors on the floor.
1630       consumeError(MaybeCode.takeError());
1631       return true;
1632     }
1633     unsigned Code = MaybeCode.get();
1634 
1635     // We expect all abbrevs to be at the start of the block.
1636     if (Code != llvm::bitc::DEFINE_ABBREV) {
1637       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1638         // FIXME this drops errors on the floor.
1639         consumeError(std::move(Err));
1640         return true;
1641       }
1642       return false;
1643     }
1644     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1645       // FIXME this drops errors on the floor.
1646       consumeError(std::move(Err));
1647       return true;
1648     }
1649   }
1650 }
1651 
1652 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1653                            unsigned &Idx) {
1654   Token Tok;
1655   Tok.startToken();
1656   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1657   Tok.setLength(Record[Idx++]);
1658   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1659     Tok.setIdentifierInfo(II);
1660   Tok.setKind((tok::TokenKind)Record[Idx++]);
1661   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1662   return Tok;
1663 }
1664 
1665 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1666   BitstreamCursor &Stream = F.MacroCursor;
1667 
1668   // Keep track of where we are in the stream, then jump back there
1669   // after reading this macro.
1670   SavedStreamPosition SavedPosition(Stream);
1671 
1672   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1673     // FIXME this drops errors on the floor.
1674     consumeError(std::move(Err));
1675     return nullptr;
1676   }
1677   RecordData Record;
1678   SmallVector<IdentifierInfo*, 16> MacroParams;
1679   MacroInfo *Macro = nullptr;
1680 
1681   while (true) {
1682     // Advance to the next record, but if we get to the end of the block, don't
1683     // pop it (removing all the abbreviations from the cursor) since we want to
1684     // be able to reseek within the block and read entries.
1685     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1686     Expected<llvm::BitstreamEntry> MaybeEntry =
1687         Stream.advanceSkippingSubblocks(Flags);
1688     if (!MaybeEntry) {
1689       Error(MaybeEntry.takeError());
1690       return Macro;
1691     }
1692     llvm::BitstreamEntry Entry = MaybeEntry.get();
1693 
1694     switch (Entry.Kind) {
1695     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1696     case llvm::BitstreamEntry::Error:
1697       Error("malformed block record in AST file");
1698       return Macro;
1699     case llvm::BitstreamEntry::EndBlock:
1700       return Macro;
1701     case llvm::BitstreamEntry::Record:
1702       // The interesting case.
1703       break;
1704     }
1705 
1706     // Read a record.
1707     Record.clear();
1708     PreprocessorRecordTypes RecType;
1709     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1710       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1711     else {
1712       Error(MaybeRecType.takeError());
1713       return Macro;
1714     }
1715     switch (RecType) {
1716     case PP_MODULE_MACRO:
1717     case PP_MACRO_DIRECTIVE_HISTORY:
1718       return Macro;
1719 
1720     case PP_MACRO_OBJECT_LIKE:
1721     case PP_MACRO_FUNCTION_LIKE: {
1722       // If we already have a macro, that means that we've hit the end
1723       // of the definition of the macro we were looking for. We're
1724       // done.
1725       if (Macro)
1726         return Macro;
1727 
1728       unsigned NextIndex = 1; // Skip identifier ID.
1729       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1730       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1731       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1732       MI->setIsUsed(Record[NextIndex++]);
1733       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1734 
1735       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1736         // Decode function-like macro info.
1737         bool isC99VarArgs = Record[NextIndex++];
1738         bool isGNUVarArgs = Record[NextIndex++];
1739         bool hasCommaPasting = Record[NextIndex++];
1740         MacroParams.clear();
1741         unsigned NumArgs = Record[NextIndex++];
1742         for (unsigned i = 0; i != NumArgs; ++i)
1743           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1744 
1745         // Install function-like macro info.
1746         MI->setIsFunctionLike();
1747         if (isC99VarArgs) MI->setIsC99Varargs();
1748         if (isGNUVarArgs) MI->setIsGNUVarargs();
1749         if (hasCommaPasting) MI->setHasCommaPasting();
1750         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1751       }
1752 
1753       // Remember that we saw this macro last so that we add the tokens that
1754       // form its body to it.
1755       Macro = MI;
1756 
1757       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1758           Record[NextIndex]) {
1759         // We have a macro definition. Register the association
1760         PreprocessedEntityID
1761             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1762         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1763         PreprocessingRecord::PPEntityID PPID =
1764             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1765         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1766             PPRec.getPreprocessedEntity(PPID));
1767         if (PPDef)
1768           PPRec.RegisterMacroDefinition(Macro, PPDef);
1769       }
1770 
1771       ++NumMacrosRead;
1772       break;
1773     }
1774 
1775     case PP_TOKEN: {
1776       // If we see a TOKEN before a PP_MACRO_*, then the file is
1777       // erroneous, just pretend we didn't see this.
1778       if (!Macro) break;
1779 
1780       unsigned Idx = 0;
1781       Token Tok = ReadToken(F, Record, Idx);
1782       Macro->AddTokenToBody(Tok);
1783       break;
1784     }
1785     }
1786   }
1787 }
1788 
1789 PreprocessedEntityID
1790 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1791                                          unsigned LocalID) const {
1792   if (!M.ModuleOffsetMap.empty())
1793     ReadModuleOffsetMap(M);
1794 
1795   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1796     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1797   assert(I != M.PreprocessedEntityRemap.end()
1798          && "Invalid index into preprocessed entity index remap");
1799 
1800   return LocalID + I->second;
1801 }
1802 
1803 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1804   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1805 }
1806 
1807 HeaderFileInfoTrait::internal_key_type
1808 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1809   internal_key_type ikey = {FE->getSize(),
1810                             M.HasTimestamps ? FE->getModificationTime() : 0,
1811                             FE->getName(), /*Imported*/ false};
1812   return ikey;
1813 }
1814 
1815 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1816   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1817     return false;
1818 
1819   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1820     return true;
1821 
1822   // Determine whether the actual files are equivalent.
1823   FileManager &FileMgr = Reader.getFileManager();
1824   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1825     if (!Key.Imported) {
1826       if (auto File = FileMgr.getFile(Key.Filename))
1827         return *File;
1828       return nullptr;
1829     }
1830 
1831     std::string Resolved = Key.Filename;
1832     Reader.ResolveImportedPath(M, Resolved);
1833     if (auto File = FileMgr.getFile(Resolved))
1834       return *File;
1835     return nullptr;
1836   };
1837 
1838   const FileEntry *FEA = GetFile(a);
1839   const FileEntry *FEB = GetFile(b);
1840   return FEA && FEA == FEB;
1841 }
1842 
1843 std::pair<unsigned, unsigned>
1844 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1845   using namespace llvm::support;
1846 
1847   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1848   unsigned DataLen = (unsigned) *d++;
1849   return std::make_pair(KeyLen, DataLen);
1850 }
1851 
1852 HeaderFileInfoTrait::internal_key_type
1853 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1854   using namespace llvm::support;
1855 
1856   internal_key_type ikey;
1857   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1858   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1859   ikey.Filename = (const char *)d;
1860   ikey.Imported = true;
1861   return ikey;
1862 }
1863 
1864 HeaderFileInfoTrait::data_type
1865 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1866                               unsigned DataLen) {
1867   using namespace llvm::support;
1868 
1869   const unsigned char *End = d + DataLen;
1870   HeaderFileInfo HFI;
1871   unsigned Flags = *d++;
1872   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1873   HFI.isImport |= (Flags >> 5) & 0x01;
1874   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1875   HFI.DirInfo = (Flags >> 1) & 0x07;
1876   HFI.IndexHeaderMapHeader = Flags & 0x01;
1877   // FIXME: Find a better way to handle this. Maybe just store a
1878   // "has been included" flag?
1879   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1880                              HFI.NumIncludes);
1881   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1882       M, endian::readNext<uint32_t, little, unaligned>(d));
1883   if (unsigned FrameworkOffset =
1884           endian::readNext<uint32_t, little, unaligned>(d)) {
1885     // The framework offset is 1 greater than the actual offset,
1886     // since 0 is used as an indicator for "no framework name".
1887     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1888     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1889   }
1890 
1891   assert((End - d) % 4 == 0 &&
1892          "Wrong data length in HeaderFileInfo deserialization");
1893   while (d != End) {
1894     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1895     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1896     LocalSMID >>= 2;
1897 
1898     // This header is part of a module. Associate it with the module to enable
1899     // implicit module import.
1900     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1901     Module *Mod = Reader.getSubmodule(GlobalSMID);
1902     FileManager &FileMgr = Reader.getFileManager();
1903     ModuleMap &ModMap =
1904         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1905 
1906     std::string Filename = key.Filename;
1907     if (key.Imported)
1908       Reader.ResolveImportedPath(M, Filename);
1909     // FIXME: This is not always the right filename-as-written, but we're not
1910     // going to use this information to rebuild the module, so it doesn't make
1911     // a lot of difference.
1912     Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1913     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1914     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1915   }
1916 
1917   // This HeaderFileInfo was externally loaded.
1918   HFI.External = true;
1919   HFI.IsValid = true;
1920   return HFI;
1921 }
1922 
1923 void ASTReader::addPendingMacro(IdentifierInfo *II,
1924                                 ModuleFile *M,
1925                                 uint64_t MacroDirectivesOffset) {
1926   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1927   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1928 }
1929 
1930 void ASTReader::ReadDefinedMacros() {
1931   // Note that we are loading defined macros.
1932   Deserializing Macros(this);
1933 
1934   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1935     BitstreamCursor &MacroCursor = I.MacroCursor;
1936 
1937     // If there was no preprocessor block, skip this file.
1938     if (MacroCursor.getBitcodeBytes().empty())
1939       continue;
1940 
1941     BitstreamCursor Cursor = MacroCursor;
1942     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1943       Error(std::move(Err));
1944       return;
1945     }
1946 
1947     RecordData Record;
1948     while (true) {
1949       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1950       if (!MaybeE) {
1951         Error(MaybeE.takeError());
1952         return;
1953       }
1954       llvm::BitstreamEntry E = MaybeE.get();
1955 
1956       switch (E.Kind) {
1957       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1958       case llvm::BitstreamEntry::Error:
1959         Error("malformed block record in AST file");
1960         return;
1961       case llvm::BitstreamEntry::EndBlock:
1962         goto NextCursor;
1963 
1964       case llvm::BitstreamEntry::Record: {
1965         Record.clear();
1966         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1967         if (!MaybeRecord) {
1968           Error(MaybeRecord.takeError());
1969           return;
1970         }
1971         switch (MaybeRecord.get()) {
1972         default:  // Default behavior: ignore.
1973           break;
1974 
1975         case PP_MACRO_OBJECT_LIKE:
1976         case PP_MACRO_FUNCTION_LIKE: {
1977           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1978           if (II->isOutOfDate())
1979             updateOutOfDateIdentifier(*II);
1980           break;
1981         }
1982 
1983         case PP_TOKEN:
1984           // Ignore tokens.
1985           break;
1986         }
1987         break;
1988       }
1989       }
1990     }
1991     NextCursor:  ;
1992   }
1993 }
1994 
1995 namespace {
1996 
1997   /// Visitor class used to look up identifirs in an AST file.
1998   class IdentifierLookupVisitor {
1999     StringRef Name;
2000     unsigned NameHash;
2001     unsigned PriorGeneration;
2002     unsigned &NumIdentifierLookups;
2003     unsigned &NumIdentifierLookupHits;
2004     IdentifierInfo *Found = nullptr;
2005 
2006   public:
2007     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2008                             unsigned &NumIdentifierLookups,
2009                             unsigned &NumIdentifierLookupHits)
2010       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2011         PriorGeneration(PriorGeneration),
2012         NumIdentifierLookups(NumIdentifierLookups),
2013         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2014 
2015     bool operator()(ModuleFile &M) {
2016       // If we've already searched this module file, skip it now.
2017       if (M.Generation <= PriorGeneration)
2018         return true;
2019 
2020       ASTIdentifierLookupTable *IdTable
2021         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2022       if (!IdTable)
2023         return false;
2024 
2025       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2026                                      Found);
2027       ++NumIdentifierLookups;
2028       ASTIdentifierLookupTable::iterator Pos =
2029           IdTable->find_hashed(Name, NameHash, &Trait);
2030       if (Pos == IdTable->end())
2031         return false;
2032 
2033       // Dereferencing the iterator has the effect of building the
2034       // IdentifierInfo node and populating it with the various
2035       // declarations it needs.
2036       ++NumIdentifierLookupHits;
2037       Found = *Pos;
2038       return true;
2039     }
2040 
2041     // Retrieve the identifier info found within the module
2042     // files.
2043     IdentifierInfo *getIdentifierInfo() const { return Found; }
2044   };
2045 
2046 } // namespace
2047 
2048 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2049   // Note that we are loading an identifier.
2050   Deserializing AnIdentifier(this);
2051 
2052   unsigned PriorGeneration = 0;
2053   if (getContext().getLangOpts().Modules)
2054     PriorGeneration = IdentifierGeneration[&II];
2055 
2056   // If there is a global index, look there first to determine which modules
2057   // provably do not have any results for this identifier.
2058   GlobalModuleIndex::HitSet Hits;
2059   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2060   if (!loadGlobalIndex()) {
2061     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2062       HitsPtr = &Hits;
2063     }
2064   }
2065 
2066   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2067                                   NumIdentifierLookups,
2068                                   NumIdentifierLookupHits);
2069   ModuleMgr.visit(Visitor, HitsPtr);
2070   markIdentifierUpToDate(&II);
2071 }
2072 
2073 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2074   if (!II)
2075     return;
2076 
2077   II->setOutOfDate(false);
2078 
2079   // Update the generation for this identifier.
2080   if (getContext().getLangOpts().Modules)
2081     IdentifierGeneration[II] = getGeneration();
2082 }
2083 
2084 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2085                                     const PendingMacroInfo &PMInfo) {
2086   ModuleFile &M = *PMInfo.M;
2087 
2088   BitstreamCursor &Cursor = M.MacroCursor;
2089   SavedStreamPosition SavedPosition(Cursor);
2090   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2091     Error(std::move(Err));
2092     return;
2093   }
2094 
2095   struct ModuleMacroRecord {
2096     SubmoduleID SubModID;
2097     MacroInfo *MI;
2098     SmallVector<SubmoduleID, 8> Overrides;
2099   };
2100   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2101 
2102   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2103   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2104   // macro histroy.
2105   RecordData Record;
2106   while (true) {
2107     Expected<llvm::BitstreamEntry> MaybeEntry =
2108         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2109     if (!MaybeEntry) {
2110       Error(MaybeEntry.takeError());
2111       return;
2112     }
2113     llvm::BitstreamEntry Entry = MaybeEntry.get();
2114 
2115     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2116       Error("malformed block record in AST file");
2117       return;
2118     }
2119 
2120     Record.clear();
2121     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2122     if (!MaybePP) {
2123       Error(MaybePP.takeError());
2124       return;
2125     }
2126     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2127     case PP_MACRO_DIRECTIVE_HISTORY:
2128       break;
2129 
2130     case PP_MODULE_MACRO: {
2131       ModuleMacros.push_back(ModuleMacroRecord());
2132       auto &Info = ModuleMacros.back();
2133       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2134       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2135       for (int I = 2, N = Record.size(); I != N; ++I)
2136         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2137       continue;
2138     }
2139 
2140     default:
2141       Error("malformed block record in AST file");
2142       return;
2143     }
2144 
2145     // We found the macro directive history; that's the last record
2146     // for this macro.
2147     break;
2148   }
2149 
2150   // Module macros are listed in reverse dependency order.
2151   {
2152     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2153     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2154     for (auto &MMR : ModuleMacros) {
2155       Overrides.clear();
2156       for (unsigned ModID : MMR.Overrides) {
2157         Module *Mod = getSubmodule(ModID);
2158         auto *Macro = PP.getModuleMacro(Mod, II);
2159         assert(Macro && "missing definition for overridden macro");
2160         Overrides.push_back(Macro);
2161       }
2162 
2163       bool Inserted = false;
2164       Module *Owner = getSubmodule(MMR.SubModID);
2165       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2166     }
2167   }
2168 
2169   // Don't read the directive history for a module; we don't have anywhere
2170   // to put it.
2171   if (M.isModule())
2172     return;
2173 
2174   // Deserialize the macro directives history in reverse source-order.
2175   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2176   unsigned Idx = 0, N = Record.size();
2177   while (Idx < N) {
2178     MacroDirective *MD = nullptr;
2179     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2180     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2181     switch (K) {
2182     case MacroDirective::MD_Define: {
2183       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2184       MD = PP.AllocateDefMacroDirective(MI, Loc);
2185       break;
2186     }
2187     case MacroDirective::MD_Undefine:
2188       MD = PP.AllocateUndefMacroDirective(Loc);
2189       break;
2190     case MacroDirective::MD_Visibility:
2191       bool isPublic = Record[Idx++];
2192       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2193       break;
2194     }
2195 
2196     if (!Latest)
2197       Latest = MD;
2198     if (Earliest)
2199       Earliest->setPrevious(MD);
2200     Earliest = MD;
2201   }
2202 
2203   if (Latest)
2204     PP.setLoadedMacroDirective(II, Earliest, Latest);
2205 }
2206 
2207 ASTReader::InputFileInfo
2208 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2209   // Go find this input file.
2210   BitstreamCursor &Cursor = F.InputFilesCursor;
2211   SavedStreamPosition SavedPosition(Cursor);
2212   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2213     // FIXME this drops errors on the floor.
2214     consumeError(std::move(Err));
2215   }
2216 
2217   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2218   if (!MaybeCode) {
2219     // FIXME this drops errors on the floor.
2220     consumeError(MaybeCode.takeError());
2221   }
2222   unsigned Code = MaybeCode.get();
2223   RecordData Record;
2224   StringRef Blob;
2225 
2226   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2227     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2228            "invalid record type for input file");
2229   else {
2230     // FIXME this drops errors on the floor.
2231     consumeError(Maybe.takeError());
2232   }
2233 
2234   assert(Record[0] == ID && "Bogus stored ID or offset");
2235   InputFileInfo R;
2236   R.StoredSize = static_cast<off_t>(Record[1]);
2237   R.StoredTime = static_cast<time_t>(Record[2]);
2238   R.Overridden = static_cast<bool>(Record[3]);
2239   R.Transient = static_cast<bool>(Record[4]);
2240   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2241   R.Filename = Blob;
2242   ResolveImportedPath(F, R.Filename);
2243   return R;
2244 }
2245 
2246 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2247 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2248   // If this ID is bogus, just return an empty input file.
2249   if (ID == 0 || ID > F.InputFilesLoaded.size())
2250     return InputFile();
2251 
2252   // If we've already loaded this input file, return it.
2253   if (F.InputFilesLoaded[ID-1].getFile())
2254     return F.InputFilesLoaded[ID-1];
2255 
2256   if (F.InputFilesLoaded[ID-1].isNotFound())
2257     return InputFile();
2258 
2259   // Go find this input file.
2260   BitstreamCursor &Cursor = F.InputFilesCursor;
2261   SavedStreamPosition SavedPosition(Cursor);
2262   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2263     // FIXME this drops errors on the floor.
2264     consumeError(std::move(Err));
2265   }
2266 
2267   InputFileInfo FI = readInputFileInfo(F, ID);
2268   off_t StoredSize = FI.StoredSize;
2269   time_t StoredTime = FI.StoredTime;
2270   bool Overridden = FI.Overridden;
2271   bool Transient = FI.Transient;
2272   StringRef Filename = FI.Filename;
2273 
2274   const FileEntry *File = nullptr;
2275   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2276     File = *FE;
2277 
2278   // If we didn't find the file, resolve it relative to the
2279   // original directory from which this AST file was created.
2280   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2281       F.OriginalDir != F.BaseDirectory) {
2282     std::string Resolved = resolveFileRelativeToOriginalDir(
2283         Filename, F.OriginalDir, F.BaseDirectory);
2284     if (!Resolved.empty())
2285       if (auto FE = FileMgr.getFile(Resolved))
2286         File = *FE;
2287   }
2288 
2289   // For an overridden file, create a virtual file with the stored
2290   // size/timestamp.
2291   if ((Overridden || Transient) && File == nullptr)
2292     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2293 
2294   if (File == nullptr) {
2295     if (Complain) {
2296       std::string ErrorStr = "could not find file '";
2297       ErrorStr += Filename;
2298       ErrorStr += "' referenced by AST file '";
2299       ErrorStr += F.FileName;
2300       ErrorStr += "'";
2301       Error(ErrorStr);
2302     }
2303     // Record that we didn't find the file.
2304     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2305     return InputFile();
2306   }
2307 
2308   // Check if there was a request to override the contents of the file
2309   // that was part of the precompiled header. Overriding such a file
2310   // can lead to problems when lexing using the source locations from the
2311   // PCH.
2312   SourceManager &SM = getSourceManager();
2313   // FIXME: Reject if the overrides are different.
2314   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2315     if (Complain)
2316       Error(diag::err_fe_pch_file_overridden, Filename);
2317     // After emitting the diagnostic, recover by disabling the override so
2318     // that the original file will be used.
2319     //
2320     // FIXME: This recovery is just as broken as the original state; there may
2321     // be another precompiled module that's using the overridden contents, or
2322     // we might be half way through parsing it. Instead, we should treat the
2323     // overridden contents as belonging to a separate FileEntry.
2324     SM.disableFileContentsOverride(File);
2325     // The FileEntry is a virtual file entry with the size of the contents
2326     // that would override the original contents. Set it to the original's
2327     // size/time.
2328     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2329                             StoredSize, StoredTime);
2330   }
2331 
2332   bool IsOutOfDate = false;
2333 
2334   // For an overridden file, there is nothing to validate.
2335   if (!Overridden && //
2336       (StoredSize != File->getSize() ||
2337        (StoredTime && StoredTime != File->getModificationTime() &&
2338         !DisableValidation)
2339        )) {
2340     if (Complain) {
2341       // Build a list of the PCH imports that got us here (in reverse).
2342       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2343       while (!ImportStack.back()->ImportedBy.empty())
2344         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2345 
2346       // The top-level PCH is stale.
2347       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2348       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2349       if (DiagnosticKind == 0)
2350         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2351       else if (DiagnosticKind == 1)
2352         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2353       else
2354         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2355 
2356       // Print the import stack.
2357       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2358         Diag(diag::note_pch_required_by)
2359           << Filename << ImportStack[0]->FileName;
2360         for (unsigned I = 1; I < ImportStack.size(); ++I)
2361           Diag(diag::note_pch_required_by)
2362             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2363       }
2364 
2365       if (!Diags.isDiagnosticInFlight())
2366         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2367     }
2368 
2369     IsOutOfDate = true;
2370   }
2371   // FIXME: If the file is overridden and we've already opened it,
2372   // issue an error (or split it into a separate FileEntry).
2373 
2374   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2375 
2376   // Note that we've loaded this input file.
2377   F.InputFilesLoaded[ID-1] = IF;
2378   return IF;
2379 }
2380 
2381 /// If we are loading a relocatable PCH or module file, and the filename
2382 /// is not an absolute path, add the system or module root to the beginning of
2383 /// the file name.
2384 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2385   // Resolve relative to the base directory, if we have one.
2386   if (!M.BaseDirectory.empty())
2387     return ResolveImportedPath(Filename, M.BaseDirectory);
2388 }
2389 
2390 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2391   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2392     return;
2393 
2394   SmallString<128> Buffer;
2395   llvm::sys::path::append(Buffer, Prefix, Filename);
2396   Filename.assign(Buffer.begin(), Buffer.end());
2397 }
2398 
2399 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2400   switch (ARR) {
2401   case ASTReader::Failure: return true;
2402   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2403   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2404   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2405   case ASTReader::ConfigurationMismatch:
2406     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2407   case ASTReader::HadErrors: return true;
2408   case ASTReader::Success: return false;
2409   }
2410 
2411   llvm_unreachable("unknown ASTReadResult");
2412 }
2413 
2414 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2415     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2416     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2417     std::string &SuggestedPredefines) {
2418   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2419     // FIXME this drops errors on the floor.
2420     consumeError(std::move(Err));
2421     return Failure;
2422   }
2423 
2424   // Read all of the records in the options block.
2425   RecordData Record;
2426   ASTReadResult Result = Success;
2427   while (true) {
2428     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2429     if (!MaybeEntry) {
2430       // FIXME this drops errors on the floor.
2431       consumeError(MaybeEntry.takeError());
2432       return Failure;
2433     }
2434     llvm::BitstreamEntry Entry = MaybeEntry.get();
2435 
2436     switch (Entry.Kind) {
2437     case llvm::BitstreamEntry::Error:
2438     case llvm::BitstreamEntry::SubBlock:
2439       return Failure;
2440 
2441     case llvm::BitstreamEntry::EndBlock:
2442       return Result;
2443 
2444     case llvm::BitstreamEntry::Record:
2445       // The interesting case.
2446       break;
2447     }
2448 
2449     // Read and process a record.
2450     Record.clear();
2451     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2452     if (!MaybeRecordType) {
2453       // FIXME this drops errors on the floor.
2454       consumeError(MaybeRecordType.takeError());
2455       return Failure;
2456     }
2457     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2458     case LANGUAGE_OPTIONS: {
2459       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2460       if (ParseLanguageOptions(Record, Complain, Listener,
2461                                AllowCompatibleConfigurationMismatch))
2462         Result = ConfigurationMismatch;
2463       break;
2464     }
2465 
2466     case TARGET_OPTIONS: {
2467       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2468       if (ParseTargetOptions(Record, Complain, Listener,
2469                              AllowCompatibleConfigurationMismatch))
2470         Result = ConfigurationMismatch;
2471       break;
2472     }
2473 
2474     case FILE_SYSTEM_OPTIONS: {
2475       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2476       if (!AllowCompatibleConfigurationMismatch &&
2477           ParseFileSystemOptions(Record, Complain, Listener))
2478         Result = ConfigurationMismatch;
2479       break;
2480     }
2481 
2482     case HEADER_SEARCH_OPTIONS: {
2483       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2484       if (!AllowCompatibleConfigurationMismatch &&
2485           ParseHeaderSearchOptions(Record, Complain, Listener))
2486         Result = ConfigurationMismatch;
2487       break;
2488     }
2489 
2490     case PREPROCESSOR_OPTIONS:
2491       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2492       if (!AllowCompatibleConfigurationMismatch &&
2493           ParsePreprocessorOptions(Record, Complain, Listener,
2494                                    SuggestedPredefines))
2495         Result = ConfigurationMismatch;
2496       break;
2497     }
2498   }
2499 }
2500 
2501 ASTReader::ASTReadResult
2502 ASTReader::ReadControlBlock(ModuleFile &F,
2503                             SmallVectorImpl<ImportedModule> &Loaded,
2504                             const ModuleFile *ImportedBy,
2505                             unsigned ClientLoadCapabilities) {
2506   BitstreamCursor &Stream = F.Stream;
2507   ASTReadResult Result = Success;
2508 
2509   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2510     Error(std::move(Err));
2511     return Failure;
2512   }
2513 
2514   // Lambda to read the unhashed control block the first time it's called.
2515   //
2516   // For PCM files, the unhashed control block cannot be read until after the
2517   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2518   // need to look ahead before reading the IMPORTS record.  For consistency,
2519   // this block is always read somehow (see BitstreamEntry::EndBlock).
2520   bool HasReadUnhashedControlBlock = false;
2521   auto readUnhashedControlBlockOnce = [&]() {
2522     if (!HasReadUnhashedControlBlock) {
2523       HasReadUnhashedControlBlock = true;
2524       if (ASTReadResult Result =
2525               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2526         return Result;
2527     }
2528     return Success;
2529   };
2530 
2531   // Read all of the records and blocks in the control block.
2532   RecordData Record;
2533   unsigned NumInputs = 0;
2534   unsigned NumUserInputs = 0;
2535   StringRef BaseDirectoryAsWritten;
2536   while (true) {
2537     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2538     if (!MaybeEntry) {
2539       Error(MaybeEntry.takeError());
2540       return Failure;
2541     }
2542     llvm::BitstreamEntry Entry = MaybeEntry.get();
2543 
2544     switch (Entry.Kind) {
2545     case llvm::BitstreamEntry::Error:
2546       Error("malformed block record in AST file");
2547       return Failure;
2548     case llvm::BitstreamEntry::EndBlock: {
2549       // Validate the module before returning.  This call catches an AST with
2550       // no module name and no imports.
2551       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2552         return Result;
2553 
2554       // Validate input files.
2555       const HeaderSearchOptions &HSOpts =
2556           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2557 
2558       // All user input files reside at the index range [0, NumUserInputs), and
2559       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2560       // loaded module files, ignore missing inputs.
2561       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2562           F.Kind != MK_PrebuiltModule) {
2563         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2564 
2565         // If we are reading a module, we will create a verification timestamp,
2566         // so we verify all input files.  Otherwise, verify only user input
2567         // files.
2568 
2569         unsigned N = NumUserInputs;
2570         if (ValidateSystemInputs ||
2571             (HSOpts.ModulesValidateOncePerBuildSession &&
2572              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2573              F.Kind == MK_ImplicitModule))
2574           N = NumInputs;
2575 
2576         for (unsigned I = 0; I < N; ++I) {
2577           InputFile IF = getInputFile(F, I+1, Complain);
2578           if (!IF.getFile() || IF.isOutOfDate())
2579             return OutOfDate;
2580         }
2581       }
2582 
2583       if (Listener)
2584         Listener->visitModuleFile(F.FileName, F.Kind);
2585 
2586       if (Listener && Listener->needsInputFileVisitation()) {
2587         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2588                                                                 : NumUserInputs;
2589         for (unsigned I = 0; I < N; ++I) {
2590           bool IsSystem = I >= NumUserInputs;
2591           InputFileInfo FI = readInputFileInfo(F, I+1);
2592           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2593                                    F.Kind == MK_ExplicitModule ||
2594                                    F.Kind == MK_PrebuiltModule);
2595         }
2596       }
2597 
2598       return Result;
2599     }
2600 
2601     case llvm::BitstreamEntry::SubBlock:
2602       switch (Entry.ID) {
2603       case INPUT_FILES_BLOCK_ID:
2604         F.InputFilesCursor = Stream;
2605         if (llvm::Error Err = Stream.SkipBlock()) {
2606           Error(std::move(Err));
2607           return Failure;
2608         }
2609         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2610           Error("malformed block record in AST file");
2611           return Failure;
2612         }
2613         continue;
2614 
2615       case OPTIONS_BLOCK_ID:
2616         // If we're reading the first module for this group, check its options
2617         // are compatible with ours. For modules it imports, no further checking
2618         // is required, because we checked them when we built it.
2619         if (Listener && !ImportedBy) {
2620           // Should we allow the configuration of the module file to differ from
2621           // the configuration of the current translation unit in a compatible
2622           // way?
2623           //
2624           // FIXME: Allow this for files explicitly specified with -include-pch.
2625           bool AllowCompatibleConfigurationMismatch =
2626               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2627 
2628           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2629                                     AllowCompatibleConfigurationMismatch,
2630                                     *Listener, SuggestedPredefines);
2631           if (Result == Failure) {
2632             Error("malformed block record in AST file");
2633             return Result;
2634           }
2635 
2636           if (DisableValidation ||
2637               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2638             Result = Success;
2639 
2640           // If we can't load the module, exit early since we likely
2641           // will rebuild the module anyway. The stream may be in the
2642           // middle of a block.
2643           if (Result != Success)
2644             return Result;
2645         } else if (llvm::Error Err = Stream.SkipBlock()) {
2646           Error(std::move(Err));
2647           return Failure;
2648         }
2649         continue;
2650 
2651       default:
2652         if (llvm::Error Err = Stream.SkipBlock()) {
2653           Error(std::move(Err));
2654           return Failure;
2655         }
2656         continue;
2657       }
2658 
2659     case llvm::BitstreamEntry::Record:
2660       // The interesting case.
2661       break;
2662     }
2663 
2664     // Read and process a record.
2665     Record.clear();
2666     StringRef Blob;
2667     Expected<unsigned> MaybeRecordType =
2668         Stream.readRecord(Entry.ID, Record, &Blob);
2669     if (!MaybeRecordType) {
2670       Error(MaybeRecordType.takeError());
2671       return Failure;
2672     }
2673     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2674     case METADATA: {
2675       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2676         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2677           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2678                                         : diag::err_pch_version_too_new);
2679         return VersionMismatch;
2680       }
2681 
2682       bool hasErrors = Record[7];
2683       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2684         Diag(diag::err_pch_with_compiler_errors);
2685         return HadErrors;
2686       }
2687       if (hasErrors) {
2688         Diags.ErrorOccurred = true;
2689         Diags.UncompilableErrorOccurred = true;
2690         Diags.UnrecoverableErrorOccurred = true;
2691       }
2692 
2693       F.RelocatablePCH = Record[4];
2694       // Relative paths in a relocatable PCH are relative to our sysroot.
2695       if (F.RelocatablePCH)
2696         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2697 
2698       F.HasTimestamps = Record[5];
2699 
2700       F.PCHHasObjectFile = Record[6];
2701 
2702       const std::string &CurBranch = getClangFullRepositoryVersion();
2703       StringRef ASTBranch = Blob;
2704       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2705         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2706           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2707         return VersionMismatch;
2708       }
2709       break;
2710     }
2711 
2712     case IMPORTS: {
2713       // Validate the AST before processing any imports (otherwise, untangling
2714       // them can be error-prone and expensive).  A module will have a name and
2715       // will already have been validated, but this catches the PCH case.
2716       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2717         return Result;
2718 
2719       // Load each of the imported PCH files.
2720       unsigned Idx = 0, N = Record.size();
2721       while (Idx < N) {
2722         // Read information about the AST file.
2723         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2724         // The import location will be the local one for now; we will adjust
2725         // all import locations of module imports after the global source
2726         // location info are setup, in ReadAST.
2727         SourceLocation ImportLoc =
2728             ReadUntranslatedSourceLocation(Record[Idx++]);
2729         off_t StoredSize = (off_t)Record[Idx++];
2730         time_t StoredModTime = (time_t)Record[Idx++];
2731         ASTFileSignature StoredSignature = {
2732             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2733               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2734               (uint32_t)Record[Idx++]}}};
2735 
2736         std::string ImportedName = ReadString(Record, Idx);
2737         std::string ImportedFile;
2738 
2739         // For prebuilt and explicit modules first consult the file map for
2740         // an override. Note that here we don't search prebuilt module
2741         // directories, only the explicit name to file mappings. Also, we will
2742         // still verify the size/signature making sure it is essentially the
2743         // same file but perhaps in a different location.
2744         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2745           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2746             ImportedName, /*FileMapOnly*/ true);
2747 
2748         if (ImportedFile.empty())
2749           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2750           // ModuleCache as when writing.
2751           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2752         else
2753           SkipPath(Record, Idx);
2754 
2755         // If our client can't cope with us being out of date, we can't cope with
2756         // our dependency being missing.
2757         unsigned Capabilities = ClientLoadCapabilities;
2758         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2759           Capabilities &= ~ARR_Missing;
2760 
2761         // Load the AST file.
2762         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2763                                   Loaded, StoredSize, StoredModTime,
2764                                   StoredSignature, Capabilities);
2765 
2766         // If we diagnosed a problem, produce a backtrace.
2767         if (isDiagnosedResult(Result, Capabilities))
2768           Diag(diag::note_module_file_imported_by)
2769               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2770 
2771         switch (Result) {
2772         case Failure: return Failure;
2773           // If we have to ignore the dependency, we'll have to ignore this too.
2774         case Missing:
2775         case OutOfDate: return OutOfDate;
2776         case VersionMismatch: return VersionMismatch;
2777         case ConfigurationMismatch: return ConfigurationMismatch;
2778         case HadErrors: return HadErrors;
2779         case Success: break;
2780         }
2781       }
2782       break;
2783     }
2784 
2785     case ORIGINAL_FILE:
2786       F.OriginalSourceFileID = FileID::get(Record[0]);
2787       F.ActualOriginalSourceFileName = Blob;
2788       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2789       ResolveImportedPath(F, F.OriginalSourceFileName);
2790       break;
2791 
2792     case ORIGINAL_FILE_ID:
2793       F.OriginalSourceFileID = FileID::get(Record[0]);
2794       break;
2795 
2796     case ORIGINAL_PCH_DIR:
2797       F.OriginalDir = Blob;
2798       break;
2799 
2800     case MODULE_NAME:
2801       F.ModuleName = Blob;
2802       Diag(diag::remark_module_import)
2803           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2804           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2805       if (Listener)
2806         Listener->ReadModuleName(F.ModuleName);
2807 
2808       // Validate the AST as soon as we have a name so we can exit early on
2809       // failure.
2810       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2811         return Result;
2812 
2813       break;
2814 
2815     case MODULE_DIRECTORY: {
2816       // Save the BaseDirectory as written in the PCM for computing the module
2817       // filename for the ModuleCache.
2818       BaseDirectoryAsWritten = Blob;
2819       assert(!F.ModuleName.empty() &&
2820              "MODULE_DIRECTORY found before MODULE_NAME");
2821       // If we've already loaded a module map file covering this module, we may
2822       // have a better path for it (relative to the current build).
2823       Module *M = PP.getHeaderSearchInfo().lookupModule(
2824           F.ModuleName, /*AllowSearch*/ true,
2825           /*AllowExtraModuleMapSearch*/ true);
2826       if (M && M->Directory) {
2827         // If we're implicitly loading a module, the base directory can't
2828         // change between the build and use.
2829         // Don't emit module relocation error if we have -fno-validate-pch
2830         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2831             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2832           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2833           if (!BuildDir || *BuildDir != M->Directory) {
2834             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2835               Diag(diag::err_imported_module_relocated)
2836                   << F.ModuleName << Blob << M->Directory->getName();
2837             return OutOfDate;
2838           }
2839         }
2840         F.BaseDirectory = M->Directory->getName();
2841       } else {
2842         F.BaseDirectory = Blob;
2843       }
2844       break;
2845     }
2846 
2847     case MODULE_MAP_FILE:
2848       if (ASTReadResult Result =
2849               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2850         return Result;
2851       break;
2852 
2853     case INPUT_FILE_OFFSETS:
2854       NumInputs = Record[0];
2855       NumUserInputs = Record[1];
2856       F.InputFileOffsets =
2857           (const llvm::support::unaligned_uint64_t *)Blob.data();
2858       F.InputFilesLoaded.resize(NumInputs);
2859       F.NumUserInputFiles = NumUserInputs;
2860       break;
2861     }
2862   }
2863 }
2864 
2865 ASTReader::ASTReadResult
2866 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2867   BitstreamCursor &Stream = F.Stream;
2868 
2869   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2870     Error(std::move(Err));
2871     return Failure;
2872   }
2873 
2874   // Read all of the records and blocks for the AST file.
2875   RecordData Record;
2876   while (true) {
2877     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2878     if (!MaybeEntry) {
2879       Error(MaybeEntry.takeError());
2880       return Failure;
2881     }
2882     llvm::BitstreamEntry Entry = MaybeEntry.get();
2883 
2884     switch (Entry.Kind) {
2885     case llvm::BitstreamEntry::Error:
2886       Error("error at end of module block in AST file");
2887       return Failure;
2888     case llvm::BitstreamEntry::EndBlock:
2889       // Outside of C++, we do not store a lookup map for the translation unit.
2890       // Instead, mark it as needing a lookup map to be built if this module
2891       // contains any declarations lexically within it (which it always does!).
2892       // This usually has no cost, since we very rarely need the lookup map for
2893       // the translation unit outside C++.
2894       if (ASTContext *Ctx = ContextObj) {
2895         DeclContext *DC = Ctx->getTranslationUnitDecl();
2896         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2897           DC->setMustBuildLookupTable();
2898       }
2899 
2900       return Success;
2901     case llvm::BitstreamEntry::SubBlock:
2902       switch (Entry.ID) {
2903       case DECLTYPES_BLOCK_ID:
2904         // We lazily load the decls block, but we want to set up the
2905         // DeclsCursor cursor to point into it.  Clone our current bitcode
2906         // cursor to it, enter the block and read the abbrevs in that block.
2907         // With the main cursor, we just skip over it.
2908         F.DeclsCursor = Stream;
2909         if (llvm::Error Err = Stream.SkipBlock()) {
2910           Error(std::move(Err));
2911           return Failure;
2912         }
2913         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2914           Error("malformed block record in AST file");
2915           return Failure;
2916         }
2917         break;
2918 
2919       case PREPROCESSOR_BLOCK_ID:
2920         F.MacroCursor = Stream;
2921         if (!PP.getExternalSource())
2922           PP.setExternalSource(this);
2923 
2924         if (llvm::Error Err = Stream.SkipBlock()) {
2925           Error(std::move(Err));
2926           return Failure;
2927         }
2928         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2929           Error("malformed block record in AST file");
2930           return Failure;
2931         }
2932         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2933         break;
2934 
2935       case PREPROCESSOR_DETAIL_BLOCK_ID:
2936         F.PreprocessorDetailCursor = Stream;
2937 
2938         if (llvm::Error Err = Stream.SkipBlock()) {
2939           Error(std::move(Err));
2940           return Failure;
2941         }
2942         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2943                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2944           Error("malformed preprocessor detail record in AST file");
2945           return Failure;
2946         }
2947         F.PreprocessorDetailStartOffset
2948         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2949 
2950         if (!PP.getPreprocessingRecord())
2951           PP.createPreprocessingRecord();
2952         if (!PP.getPreprocessingRecord()->getExternalSource())
2953           PP.getPreprocessingRecord()->SetExternalSource(*this);
2954         break;
2955 
2956       case SOURCE_MANAGER_BLOCK_ID:
2957         if (ReadSourceManagerBlock(F))
2958           return Failure;
2959         break;
2960 
2961       case SUBMODULE_BLOCK_ID:
2962         if (ASTReadResult Result =
2963                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2964           return Result;
2965         break;
2966 
2967       case COMMENTS_BLOCK_ID: {
2968         BitstreamCursor C = Stream;
2969 
2970         if (llvm::Error Err = Stream.SkipBlock()) {
2971           Error(std::move(Err));
2972           return Failure;
2973         }
2974         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2975           Error("malformed comments block in AST file");
2976           return Failure;
2977         }
2978         CommentsCursors.push_back(std::make_pair(C, &F));
2979         break;
2980       }
2981 
2982       default:
2983         if (llvm::Error Err = Stream.SkipBlock()) {
2984           Error(std::move(Err));
2985           return Failure;
2986         }
2987         break;
2988       }
2989       continue;
2990 
2991     case llvm::BitstreamEntry::Record:
2992       // The interesting case.
2993       break;
2994     }
2995 
2996     // Read and process a record.
2997     Record.clear();
2998     StringRef Blob;
2999     Expected<unsigned> MaybeRecordType =
3000         Stream.readRecord(Entry.ID, Record, &Blob);
3001     if (!MaybeRecordType) {
3002       Error(MaybeRecordType.takeError());
3003       return Failure;
3004     }
3005     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3006 
3007     // If we're not loading an AST context, we don't care about most records.
3008     if (!ContextObj) {
3009       switch (RecordType) {
3010       case IDENTIFIER_TABLE:
3011       case IDENTIFIER_OFFSET:
3012       case INTERESTING_IDENTIFIERS:
3013       case STATISTICS:
3014       case PP_CONDITIONAL_STACK:
3015       case PP_COUNTER_VALUE:
3016       case SOURCE_LOCATION_OFFSETS:
3017       case MODULE_OFFSET_MAP:
3018       case SOURCE_MANAGER_LINE_TABLE:
3019       case SOURCE_LOCATION_PRELOADS:
3020       case PPD_ENTITIES_OFFSETS:
3021       case HEADER_SEARCH_TABLE:
3022       case IMPORTED_MODULES:
3023       case MACRO_OFFSET:
3024         break;
3025       default:
3026         continue;
3027       }
3028     }
3029 
3030     switch (RecordType) {
3031     default:  // Default behavior: ignore.
3032       break;
3033 
3034     case TYPE_OFFSET: {
3035       if (F.LocalNumTypes != 0) {
3036         Error("duplicate TYPE_OFFSET record in AST file");
3037         return Failure;
3038       }
3039       F.TypeOffsets = (const uint32_t *)Blob.data();
3040       F.LocalNumTypes = Record[0];
3041       unsigned LocalBaseTypeIndex = Record[1];
3042       F.BaseTypeIndex = getTotalNumTypes();
3043 
3044       if (F.LocalNumTypes > 0) {
3045         // Introduce the global -> local mapping for types within this module.
3046         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3047 
3048         // Introduce the local -> global mapping for types within this module.
3049         F.TypeRemap.insertOrReplace(
3050           std::make_pair(LocalBaseTypeIndex,
3051                          F.BaseTypeIndex - LocalBaseTypeIndex));
3052 
3053         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3054       }
3055       break;
3056     }
3057 
3058     case DECL_OFFSET: {
3059       if (F.LocalNumDecls != 0) {
3060         Error("duplicate DECL_OFFSET record in AST file");
3061         return Failure;
3062       }
3063       F.DeclOffsets = (const DeclOffset *)Blob.data();
3064       F.LocalNumDecls = Record[0];
3065       unsigned LocalBaseDeclID = Record[1];
3066       F.BaseDeclID = getTotalNumDecls();
3067 
3068       if (F.LocalNumDecls > 0) {
3069         // Introduce the global -> local mapping for declarations within this
3070         // module.
3071         GlobalDeclMap.insert(
3072           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3073 
3074         // Introduce the local -> global mapping for declarations within this
3075         // module.
3076         F.DeclRemap.insertOrReplace(
3077           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3078 
3079         // Introduce the global -> local mapping for declarations within this
3080         // module.
3081         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3082 
3083         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3084       }
3085       break;
3086     }
3087 
3088     case TU_UPDATE_LEXICAL: {
3089       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3090       LexicalContents Contents(
3091           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3092               Blob.data()),
3093           static_cast<unsigned int>(Blob.size() / 4));
3094       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3095       TU->setHasExternalLexicalStorage(true);
3096       break;
3097     }
3098 
3099     case UPDATE_VISIBLE: {
3100       unsigned Idx = 0;
3101       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3102       auto *Data = (const unsigned char*)Blob.data();
3103       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3104       // If we've already loaded the decl, perform the updates when we finish
3105       // loading this block.
3106       if (Decl *D = GetExistingDecl(ID))
3107         PendingUpdateRecords.push_back(
3108             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3109       break;
3110     }
3111 
3112     case IDENTIFIER_TABLE:
3113       F.IdentifierTableData = Blob.data();
3114       if (Record[0]) {
3115         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3116             (const unsigned char *)F.IdentifierTableData + Record[0],
3117             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3118             (const unsigned char *)F.IdentifierTableData,
3119             ASTIdentifierLookupTrait(*this, F));
3120 
3121         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3122       }
3123       break;
3124 
3125     case IDENTIFIER_OFFSET: {
3126       if (F.LocalNumIdentifiers != 0) {
3127         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3128         return Failure;
3129       }
3130       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3131       F.LocalNumIdentifiers = Record[0];
3132       unsigned LocalBaseIdentifierID = Record[1];
3133       F.BaseIdentifierID = getTotalNumIdentifiers();
3134 
3135       if (F.LocalNumIdentifiers > 0) {
3136         // Introduce the global -> local mapping for identifiers within this
3137         // module.
3138         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3139                                                   &F));
3140 
3141         // Introduce the local -> global mapping for identifiers within this
3142         // module.
3143         F.IdentifierRemap.insertOrReplace(
3144           std::make_pair(LocalBaseIdentifierID,
3145                          F.BaseIdentifierID - LocalBaseIdentifierID));
3146 
3147         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3148                                  + F.LocalNumIdentifiers);
3149       }
3150       break;
3151     }
3152 
3153     case INTERESTING_IDENTIFIERS:
3154       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3155       break;
3156 
3157     case EAGERLY_DESERIALIZED_DECLS:
3158       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3159       // about "interesting" decls (for instance, if we're building a module).
3160       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3161         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3162       break;
3163 
3164     case MODULAR_CODEGEN_DECLS:
3165       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3166       // them (ie: if we're not codegenerating this module).
3167       if (F.Kind == MK_MainFile)
3168         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3169           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3170       break;
3171 
3172     case SPECIAL_TYPES:
3173       if (SpecialTypes.empty()) {
3174         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3175           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3176         break;
3177       }
3178 
3179       if (SpecialTypes.size() != Record.size()) {
3180         Error("invalid special-types record");
3181         return Failure;
3182       }
3183 
3184       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3185         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3186         if (!SpecialTypes[I])
3187           SpecialTypes[I] = ID;
3188         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3189         // merge step?
3190       }
3191       break;
3192 
3193     case STATISTICS:
3194       TotalNumStatements += Record[0];
3195       TotalNumMacros += Record[1];
3196       TotalLexicalDeclContexts += Record[2];
3197       TotalVisibleDeclContexts += Record[3];
3198       break;
3199 
3200     case UNUSED_FILESCOPED_DECLS:
3201       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3202         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3203       break;
3204 
3205     case DELEGATING_CTORS:
3206       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3207         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3208       break;
3209 
3210     case WEAK_UNDECLARED_IDENTIFIERS:
3211       if (Record.size() % 4 != 0) {
3212         Error("invalid weak identifiers record");
3213         return Failure;
3214       }
3215 
3216       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3217       // files. This isn't the way to do it :)
3218       WeakUndeclaredIdentifiers.clear();
3219 
3220       // Translate the weak, undeclared identifiers into global IDs.
3221       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3222         WeakUndeclaredIdentifiers.push_back(
3223           getGlobalIdentifierID(F, Record[I++]));
3224         WeakUndeclaredIdentifiers.push_back(
3225           getGlobalIdentifierID(F, Record[I++]));
3226         WeakUndeclaredIdentifiers.push_back(
3227           ReadSourceLocation(F, Record, I).getRawEncoding());
3228         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3229       }
3230       break;
3231 
3232     case SELECTOR_OFFSETS: {
3233       F.SelectorOffsets = (const uint32_t *)Blob.data();
3234       F.LocalNumSelectors = Record[0];
3235       unsigned LocalBaseSelectorID = Record[1];
3236       F.BaseSelectorID = getTotalNumSelectors();
3237 
3238       if (F.LocalNumSelectors > 0) {
3239         // Introduce the global -> local mapping for selectors within this
3240         // module.
3241         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3242 
3243         // Introduce the local -> global mapping for selectors within this
3244         // module.
3245         F.SelectorRemap.insertOrReplace(
3246           std::make_pair(LocalBaseSelectorID,
3247                          F.BaseSelectorID - LocalBaseSelectorID));
3248 
3249         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3250       }
3251       break;
3252     }
3253 
3254     case METHOD_POOL:
3255       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3256       if (Record[0])
3257         F.SelectorLookupTable
3258           = ASTSelectorLookupTable::Create(
3259                         F.SelectorLookupTableData + Record[0],
3260                         F.SelectorLookupTableData,
3261                         ASTSelectorLookupTrait(*this, F));
3262       TotalNumMethodPoolEntries += Record[1];
3263       break;
3264 
3265     case REFERENCED_SELECTOR_POOL:
3266       if (!Record.empty()) {
3267         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3268           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3269                                                                 Record[Idx++]));
3270           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3271                                               getRawEncoding());
3272         }
3273       }
3274       break;
3275 
3276     case PP_CONDITIONAL_STACK:
3277       if (!Record.empty()) {
3278         unsigned Idx = 0, End = Record.size() - 1;
3279         bool ReachedEOFWhileSkipping = Record[Idx++];
3280         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3281         if (ReachedEOFWhileSkipping) {
3282           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3283           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3284           bool FoundNonSkipPortion = Record[Idx++];
3285           bool FoundElse = Record[Idx++];
3286           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3287           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3288                            FoundElse, ElseLoc);
3289         }
3290         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3291         while (Idx < End) {
3292           auto Loc = ReadSourceLocation(F, Record, Idx);
3293           bool WasSkipping = Record[Idx++];
3294           bool FoundNonSkip = Record[Idx++];
3295           bool FoundElse = Record[Idx++];
3296           ConditionalStack.push_back(
3297               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3298         }
3299         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3300       }
3301       break;
3302 
3303     case PP_COUNTER_VALUE:
3304       if (!Record.empty() && Listener)
3305         Listener->ReadCounter(F, Record[0]);
3306       break;
3307 
3308     case FILE_SORTED_DECLS:
3309       F.FileSortedDecls = (const DeclID *)Blob.data();
3310       F.NumFileSortedDecls = Record[0];
3311       break;
3312 
3313     case SOURCE_LOCATION_OFFSETS: {
3314       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3315       F.LocalNumSLocEntries = Record[0];
3316       unsigned SLocSpaceSize = Record[1];
3317       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3318           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3319                                               SLocSpaceSize);
3320       if (!F.SLocEntryBaseID) {
3321         Error("ran out of source locations");
3322         break;
3323       }
3324       // Make our entry in the range map. BaseID is negative and growing, so
3325       // we invert it. Because we invert it, though, we need the other end of
3326       // the range.
3327       unsigned RangeStart =
3328           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3329       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3330       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3331 
3332       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3333       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3334       GlobalSLocOffsetMap.insert(
3335           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3336                            - SLocSpaceSize,&F));
3337 
3338       // Initialize the remapping table.
3339       // Invalid stays invalid.
3340       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3341       // This module. Base was 2 when being compiled.
3342       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3343                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3344 
3345       TotalNumSLocEntries += F.LocalNumSLocEntries;
3346       break;
3347     }
3348 
3349     case MODULE_OFFSET_MAP:
3350       F.ModuleOffsetMap = Blob;
3351       break;
3352 
3353     case SOURCE_MANAGER_LINE_TABLE:
3354       if (ParseLineTable(F, Record))
3355         return Failure;
3356       break;
3357 
3358     case SOURCE_LOCATION_PRELOADS: {
3359       // Need to transform from the local view (1-based IDs) to the global view,
3360       // which is based off F.SLocEntryBaseID.
3361       if (!F.PreloadSLocEntries.empty()) {
3362         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3363         return Failure;
3364       }
3365 
3366       F.PreloadSLocEntries.swap(Record);
3367       break;
3368     }
3369 
3370     case EXT_VECTOR_DECLS:
3371       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3372         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3373       break;
3374 
3375     case VTABLE_USES:
3376       if (Record.size() % 3 != 0) {
3377         Error("Invalid VTABLE_USES record");
3378         return Failure;
3379       }
3380 
3381       // Later tables overwrite earlier ones.
3382       // FIXME: Modules will have some trouble with this. This is clearly not
3383       // the right way to do this.
3384       VTableUses.clear();
3385 
3386       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3387         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3388         VTableUses.push_back(
3389           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3390         VTableUses.push_back(Record[Idx++]);
3391       }
3392       break;
3393 
3394     case PENDING_IMPLICIT_INSTANTIATIONS:
3395       if (PendingInstantiations.size() % 2 != 0) {
3396         Error("Invalid existing PendingInstantiations");
3397         return Failure;
3398       }
3399 
3400       if (Record.size() % 2 != 0) {
3401         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3402         return Failure;
3403       }
3404 
3405       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3406         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3407         PendingInstantiations.push_back(
3408           ReadSourceLocation(F, Record, I).getRawEncoding());
3409       }
3410       break;
3411 
3412     case SEMA_DECL_REFS:
3413       if (Record.size() != 3) {
3414         Error("Invalid SEMA_DECL_REFS block");
3415         return Failure;
3416       }
3417       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3418         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3419       break;
3420 
3421     case PPD_ENTITIES_OFFSETS: {
3422       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3423       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3424       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3425 
3426       unsigned LocalBasePreprocessedEntityID = Record[0];
3427 
3428       unsigned StartingID;
3429       if (!PP.getPreprocessingRecord())
3430         PP.createPreprocessingRecord();
3431       if (!PP.getPreprocessingRecord()->getExternalSource())
3432         PP.getPreprocessingRecord()->SetExternalSource(*this);
3433       StartingID
3434         = PP.getPreprocessingRecord()
3435             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3436       F.BasePreprocessedEntityID = StartingID;
3437 
3438       if (F.NumPreprocessedEntities > 0) {
3439         // Introduce the global -> local mapping for preprocessed entities in
3440         // this module.
3441         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3442 
3443         // Introduce the local -> global mapping for preprocessed entities in
3444         // this module.
3445         F.PreprocessedEntityRemap.insertOrReplace(
3446           std::make_pair(LocalBasePreprocessedEntityID,
3447             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3448       }
3449 
3450       break;
3451     }
3452 
3453     case PPD_SKIPPED_RANGES: {
3454       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3455       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3456       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3457 
3458       if (!PP.getPreprocessingRecord())
3459         PP.createPreprocessingRecord();
3460       if (!PP.getPreprocessingRecord()->getExternalSource())
3461         PP.getPreprocessingRecord()->SetExternalSource(*this);
3462       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3463           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3464 
3465       if (F.NumPreprocessedSkippedRanges > 0)
3466         GlobalSkippedRangeMap.insert(
3467             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3468       break;
3469     }
3470 
3471     case DECL_UPDATE_OFFSETS:
3472       if (Record.size() % 2 != 0) {
3473         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3474         return Failure;
3475       }
3476       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3477         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3478         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3479 
3480         // If we've already loaded the decl, perform the updates when we finish
3481         // loading this block.
3482         if (Decl *D = GetExistingDecl(ID))
3483           PendingUpdateRecords.push_back(
3484               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3485       }
3486       break;
3487 
3488     case OBJC_CATEGORIES_MAP:
3489       if (F.LocalNumObjCCategoriesInMap != 0) {
3490         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3491         return Failure;
3492       }
3493 
3494       F.LocalNumObjCCategoriesInMap = Record[0];
3495       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3496       break;
3497 
3498     case OBJC_CATEGORIES:
3499       F.ObjCCategories.swap(Record);
3500       break;
3501 
3502     case CUDA_SPECIAL_DECL_REFS:
3503       // Later tables overwrite earlier ones.
3504       // FIXME: Modules will have trouble with this.
3505       CUDASpecialDeclRefs.clear();
3506       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3507         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3508       break;
3509 
3510     case HEADER_SEARCH_TABLE:
3511       F.HeaderFileInfoTableData = Blob.data();
3512       F.LocalNumHeaderFileInfos = Record[1];
3513       if (Record[0]) {
3514         F.HeaderFileInfoTable
3515           = HeaderFileInfoLookupTable::Create(
3516                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3517                    (const unsigned char *)F.HeaderFileInfoTableData,
3518                    HeaderFileInfoTrait(*this, F,
3519                                        &PP.getHeaderSearchInfo(),
3520                                        Blob.data() + Record[2]));
3521 
3522         PP.getHeaderSearchInfo().SetExternalSource(this);
3523         if (!PP.getHeaderSearchInfo().getExternalLookup())
3524           PP.getHeaderSearchInfo().SetExternalLookup(this);
3525       }
3526       break;
3527 
3528     case FP_PRAGMA_OPTIONS:
3529       // Later tables overwrite earlier ones.
3530       FPPragmaOptions.swap(Record);
3531       break;
3532 
3533     case OPENCL_EXTENSIONS:
3534       for (unsigned I = 0, E = Record.size(); I != E; ) {
3535         auto Name = ReadString(Record, I);
3536         auto &Opt = OpenCLExtensions.OptMap[Name];
3537         Opt.Supported = Record[I++] != 0;
3538         Opt.Enabled = Record[I++] != 0;
3539         Opt.Avail = Record[I++];
3540         Opt.Core = Record[I++];
3541       }
3542       break;
3543 
3544     case OPENCL_EXTENSION_TYPES:
3545       for (unsigned I = 0, E = Record.size(); I != E;) {
3546         auto TypeID = static_cast<::TypeID>(Record[I++]);
3547         auto *Type = GetType(TypeID).getTypePtr();
3548         auto NumExt = static_cast<unsigned>(Record[I++]);
3549         for (unsigned II = 0; II != NumExt; ++II) {
3550           auto Ext = ReadString(Record, I);
3551           OpenCLTypeExtMap[Type].insert(Ext);
3552         }
3553       }
3554       break;
3555 
3556     case OPENCL_EXTENSION_DECLS:
3557       for (unsigned I = 0, E = Record.size(); I != E;) {
3558         auto DeclID = static_cast<::DeclID>(Record[I++]);
3559         auto *Decl = GetDecl(DeclID);
3560         auto NumExt = static_cast<unsigned>(Record[I++]);
3561         for (unsigned II = 0; II != NumExt; ++II) {
3562           auto Ext = ReadString(Record, I);
3563           OpenCLDeclExtMap[Decl].insert(Ext);
3564         }
3565       }
3566       break;
3567 
3568     case TENTATIVE_DEFINITIONS:
3569       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3570         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3571       break;
3572 
3573     case KNOWN_NAMESPACES:
3574       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3575         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3576       break;
3577 
3578     case UNDEFINED_BUT_USED:
3579       if (UndefinedButUsed.size() % 2 != 0) {
3580         Error("Invalid existing UndefinedButUsed");
3581         return Failure;
3582       }
3583 
3584       if (Record.size() % 2 != 0) {
3585         Error("invalid undefined-but-used record");
3586         return Failure;
3587       }
3588       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3589         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3590         UndefinedButUsed.push_back(
3591             ReadSourceLocation(F, Record, I).getRawEncoding());
3592       }
3593       break;
3594 
3595     case DELETE_EXPRS_TO_ANALYZE:
3596       for (unsigned I = 0, N = Record.size(); I != N;) {
3597         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3598         const uint64_t Count = Record[I++];
3599         DelayedDeleteExprs.push_back(Count);
3600         for (uint64_t C = 0; C < Count; ++C) {
3601           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3602           bool IsArrayForm = Record[I++] == 1;
3603           DelayedDeleteExprs.push_back(IsArrayForm);
3604         }
3605       }
3606       break;
3607 
3608     case IMPORTED_MODULES:
3609       if (!F.isModule()) {
3610         // If we aren't loading a module (which has its own exports), make
3611         // all of the imported modules visible.
3612         // FIXME: Deal with macros-only imports.
3613         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3614           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3615           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3616           if (GlobalID) {
3617             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3618             if (DeserializationListener)
3619               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3620           }
3621         }
3622       }
3623       break;
3624 
3625     case MACRO_OFFSET: {
3626       if (F.LocalNumMacros != 0) {
3627         Error("duplicate MACRO_OFFSET record in AST file");
3628         return Failure;
3629       }
3630       F.MacroOffsets = (const uint32_t *)Blob.data();
3631       F.LocalNumMacros = Record[0];
3632       unsigned LocalBaseMacroID = Record[1];
3633       F.BaseMacroID = getTotalNumMacros();
3634 
3635       if (F.LocalNumMacros > 0) {
3636         // Introduce the global -> local mapping for macros within this module.
3637         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3638 
3639         // Introduce the local -> global mapping for macros within this module.
3640         F.MacroRemap.insertOrReplace(
3641           std::make_pair(LocalBaseMacroID,
3642                          F.BaseMacroID - LocalBaseMacroID));
3643 
3644         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3645       }
3646       break;
3647     }
3648 
3649     case LATE_PARSED_TEMPLATE:
3650       LateParsedTemplates.append(Record.begin(), Record.end());
3651       break;
3652 
3653     case OPTIMIZE_PRAGMA_OPTIONS:
3654       if (Record.size() != 1) {
3655         Error("invalid pragma optimize record");
3656         return Failure;
3657       }
3658       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3659       break;
3660 
3661     case MSSTRUCT_PRAGMA_OPTIONS:
3662       if (Record.size() != 1) {
3663         Error("invalid pragma ms_struct record");
3664         return Failure;
3665       }
3666       PragmaMSStructState = Record[0];
3667       break;
3668 
3669     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3670       if (Record.size() != 2) {
3671         Error("invalid pragma ms_struct record");
3672         return Failure;
3673       }
3674       PragmaMSPointersToMembersState = Record[0];
3675       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3676       break;
3677 
3678     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3679       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3680         UnusedLocalTypedefNameCandidates.push_back(
3681             getGlobalDeclID(F, Record[I]));
3682       break;
3683 
3684     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3685       if (Record.size() != 1) {
3686         Error("invalid cuda pragma options record");
3687         return Failure;
3688       }
3689       ForceCUDAHostDeviceDepth = Record[0];
3690       break;
3691 
3692     case PACK_PRAGMA_OPTIONS: {
3693       if (Record.size() < 3) {
3694         Error("invalid pragma pack record");
3695         return Failure;
3696       }
3697       PragmaPackCurrentValue = Record[0];
3698       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3699       unsigned NumStackEntries = Record[2];
3700       unsigned Idx = 3;
3701       // Reset the stack when importing a new module.
3702       PragmaPackStack.clear();
3703       for (unsigned I = 0; I < NumStackEntries; ++I) {
3704         PragmaPackStackEntry Entry;
3705         Entry.Value = Record[Idx++];
3706         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3707         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3708         PragmaPackStrings.push_back(ReadString(Record, Idx));
3709         Entry.SlotLabel = PragmaPackStrings.back();
3710         PragmaPackStack.push_back(Entry);
3711       }
3712       break;
3713     }
3714     }
3715   }
3716 }
3717 
3718 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3719   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3720 
3721   // Additional remapping information.
3722   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3723   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3724   F.ModuleOffsetMap = StringRef();
3725 
3726   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3727   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3728     F.SLocRemap.insert(std::make_pair(0U, 0));
3729     F.SLocRemap.insert(std::make_pair(2U, 1));
3730   }
3731 
3732   // Continuous range maps we may be updating in our module.
3733   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3734   RemapBuilder SLocRemap(F.SLocRemap);
3735   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3736   RemapBuilder MacroRemap(F.MacroRemap);
3737   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3738   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3739   RemapBuilder SelectorRemap(F.SelectorRemap);
3740   RemapBuilder DeclRemap(F.DeclRemap);
3741   RemapBuilder TypeRemap(F.TypeRemap);
3742 
3743   while (Data < DataEnd) {
3744     // FIXME: Looking up dependency modules by filename is horrible. Let's
3745     // start fixing this with prebuilt and explicit modules and see how it
3746     // goes...
3747     using namespace llvm::support;
3748     ModuleKind Kind = static_cast<ModuleKind>(
3749       endian::readNext<uint8_t, little, unaligned>(Data));
3750     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3751     StringRef Name = StringRef((const char*)Data, Len);
3752     Data += Len;
3753     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3754                       ? ModuleMgr.lookupByModuleName(Name)
3755                       : ModuleMgr.lookupByFileName(Name));
3756     if (!OM) {
3757       std::string Msg =
3758           "SourceLocation remap refers to unknown module, cannot find ";
3759       Msg.append(Name);
3760       Error(Msg);
3761       return;
3762     }
3763 
3764     uint32_t SLocOffset =
3765         endian::readNext<uint32_t, little, unaligned>(Data);
3766     uint32_t IdentifierIDOffset =
3767         endian::readNext<uint32_t, little, unaligned>(Data);
3768     uint32_t MacroIDOffset =
3769         endian::readNext<uint32_t, little, unaligned>(Data);
3770     uint32_t PreprocessedEntityIDOffset =
3771         endian::readNext<uint32_t, little, unaligned>(Data);
3772     uint32_t SubmoduleIDOffset =
3773         endian::readNext<uint32_t, little, unaligned>(Data);
3774     uint32_t SelectorIDOffset =
3775         endian::readNext<uint32_t, little, unaligned>(Data);
3776     uint32_t DeclIDOffset =
3777         endian::readNext<uint32_t, little, unaligned>(Data);
3778     uint32_t TypeIndexOffset =
3779         endian::readNext<uint32_t, little, unaligned>(Data);
3780 
3781     uint32_t None = std::numeric_limits<uint32_t>::max();
3782 
3783     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3784                          RemapBuilder &Remap) {
3785       if (Offset != None)
3786         Remap.insert(std::make_pair(Offset,
3787                                     static_cast<int>(BaseOffset - Offset)));
3788     };
3789     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3790     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3791     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3792     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3793               PreprocessedEntityRemap);
3794     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3795     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3796     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3797     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3798 
3799     // Global -> local mappings.
3800     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3801   }
3802 }
3803 
3804 ASTReader::ASTReadResult
3805 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3806                                   const ModuleFile *ImportedBy,
3807                                   unsigned ClientLoadCapabilities) {
3808   unsigned Idx = 0;
3809   F.ModuleMapPath = ReadPath(F, Record, Idx);
3810 
3811   // Try to resolve ModuleName in the current header search context and
3812   // verify that it is found in the same module map file as we saved. If the
3813   // top-level AST file is a main file, skip this check because there is no
3814   // usable header search context.
3815   assert(!F.ModuleName.empty() &&
3816          "MODULE_NAME should come before MODULE_MAP_FILE");
3817   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3818     // An implicitly-loaded module file should have its module listed in some
3819     // module map file that we've already loaded.
3820     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3821     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3822     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3823     // Don't emit module relocation error if we have -fno-validate-pch
3824     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3825       assert(ImportedBy && "top-level import should be verified");
3826       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3827         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3828           // This module was defined by an imported (explicit) module.
3829           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3830                                                << ASTFE->getName();
3831         } else {
3832           // This module was built with a different module map.
3833           Diag(diag::err_imported_module_not_found)
3834               << F.ModuleName << F.FileName << ImportedBy->FileName
3835               << F.ModuleMapPath;
3836           // In case it was imported by a PCH, there's a chance the user is
3837           // just missing to include the search path to the directory containing
3838           // the modulemap.
3839           if (ImportedBy->Kind == MK_PCH)
3840             Diag(diag::note_imported_by_pch_module_not_found)
3841                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3842         }
3843       }
3844       return OutOfDate;
3845     }
3846 
3847     assert(M->Name == F.ModuleName && "found module with different name");
3848 
3849     // Check the primary module map file.
3850     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3851     if (!StoredModMap || *StoredModMap != ModMap) {
3852       assert(ModMap && "found module is missing module map file");
3853       assert(ImportedBy && "top-level import should be verified");
3854       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3855         Diag(diag::err_imported_module_modmap_changed)
3856           << F.ModuleName << ImportedBy->FileName
3857           << ModMap->getName() << F.ModuleMapPath;
3858       return OutOfDate;
3859     }
3860 
3861     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3862     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3863       // FIXME: we should use input files rather than storing names.
3864       std::string Filename = ReadPath(F, Record, Idx);
3865       auto F = FileMgr.getFile(Filename, false, false);
3866       if (!F) {
3867         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3868           Error("could not find file '" + Filename +"' referenced by AST file");
3869         return OutOfDate;
3870       }
3871       AdditionalStoredMaps.insert(*F);
3872     }
3873 
3874     // Check any additional module map files (e.g. module.private.modulemap)
3875     // that are not in the pcm.
3876     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3877       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3878         // Remove files that match
3879         // Note: SmallPtrSet::erase is really remove
3880         if (!AdditionalStoredMaps.erase(ModMap)) {
3881           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3882             Diag(diag::err_module_different_modmap)
3883               << F.ModuleName << /*new*/0 << ModMap->getName();
3884           return OutOfDate;
3885         }
3886       }
3887     }
3888 
3889     // Check any additional module map files that are in the pcm, but not
3890     // found in header search. Cases that match are already removed.
3891     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3892       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3893         Diag(diag::err_module_different_modmap)
3894           << F.ModuleName << /*not new*/1 << ModMap->getName();
3895       return OutOfDate;
3896     }
3897   }
3898 
3899   if (Listener)
3900     Listener->ReadModuleMapFile(F.ModuleMapPath);
3901   return Success;
3902 }
3903 
3904 /// Move the given method to the back of the global list of methods.
3905 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3906   // Find the entry for this selector in the method pool.
3907   Sema::GlobalMethodPool::iterator Known
3908     = S.MethodPool.find(Method->getSelector());
3909   if (Known == S.MethodPool.end())
3910     return;
3911 
3912   // Retrieve the appropriate method list.
3913   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3914                                                     : Known->second.second;
3915   bool Found = false;
3916   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3917     if (!Found) {
3918       if (List->getMethod() == Method) {
3919         Found = true;
3920       } else {
3921         // Keep searching.
3922         continue;
3923       }
3924     }
3925 
3926     if (List->getNext())
3927       List->setMethod(List->getNext()->getMethod());
3928     else
3929       List->setMethod(Method);
3930   }
3931 }
3932 
3933 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3934   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3935   for (Decl *D : Names) {
3936     bool wasHidden = D->isHidden();
3937     D->setVisibleDespiteOwningModule();
3938 
3939     if (wasHidden && SemaObj) {
3940       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3941         moveMethodToBackOfGlobalList(*SemaObj, Method);
3942       }
3943     }
3944   }
3945 }
3946 
3947 void ASTReader::makeModuleVisible(Module *Mod,
3948                                   Module::NameVisibilityKind NameVisibility,
3949                                   SourceLocation ImportLoc) {
3950   llvm::SmallPtrSet<Module *, 4> Visited;
3951   SmallVector<Module *, 4> Stack;
3952   Stack.push_back(Mod);
3953   while (!Stack.empty()) {
3954     Mod = Stack.pop_back_val();
3955 
3956     if (NameVisibility <= Mod->NameVisibility) {
3957       // This module already has this level of visibility (or greater), so
3958       // there is nothing more to do.
3959       continue;
3960     }
3961 
3962     if (!Mod->isAvailable()) {
3963       // Modules that aren't available cannot be made visible.
3964       continue;
3965     }
3966 
3967     // Update the module's name visibility.
3968     Mod->NameVisibility = NameVisibility;
3969 
3970     // If we've already deserialized any names from this module,
3971     // mark them as visible.
3972     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3973     if (Hidden != HiddenNamesMap.end()) {
3974       auto HiddenNames = std::move(*Hidden);
3975       HiddenNamesMap.erase(Hidden);
3976       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3977       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3978              "making names visible added hidden names");
3979     }
3980 
3981     // Push any exported modules onto the stack to be marked as visible.
3982     SmallVector<Module *, 16> Exports;
3983     Mod->getExportedModules(Exports);
3984     for (SmallVectorImpl<Module *>::iterator
3985            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3986       Module *Exported = *I;
3987       if (Visited.insert(Exported).second)
3988         Stack.push_back(Exported);
3989     }
3990   }
3991 }
3992 
3993 /// We've merged the definition \p MergedDef into the existing definition
3994 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3995 /// visible.
3996 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3997                                           NamedDecl *MergedDef) {
3998   if (Def->isHidden()) {
3999     // If MergedDef is visible or becomes visible, make the definition visible.
4000     if (!MergedDef->isHidden())
4001       Def->setVisibleDespiteOwningModule();
4002     else {
4003       getContext().mergeDefinitionIntoModule(
4004           Def, MergedDef->getImportedOwningModule(),
4005           /*NotifyListeners*/ false);
4006       PendingMergedDefinitionsToDeduplicate.insert(Def);
4007     }
4008   }
4009 }
4010 
4011 bool ASTReader::loadGlobalIndex() {
4012   if (GlobalIndex)
4013     return false;
4014 
4015   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4016       !PP.getLangOpts().Modules)
4017     return true;
4018 
4019   // Try to load the global index.
4020   TriedLoadingGlobalIndex = true;
4021   StringRef ModuleCachePath
4022     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4023   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4024       GlobalModuleIndex::readIndex(ModuleCachePath);
4025   if (llvm::Error Err = std::move(Result.second)) {
4026     assert(!Result.first);
4027     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4028     return true;
4029   }
4030 
4031   GlobalIndex.reset(Result.first);
4032   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4033   return false;
4034 }
4035 
4036 bool ASTReader::isGlobalIndexUnavailable() const {
4037   return PP.getLangOpts().Modules && UseGlobalIndex &&
4038          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4039 }
4040 
4041 static void updateModuleTimestamp(ModuleFile &MF) {
4042   // Overwrite the timestamp file contents so that file's mtime changes.
4043   std::string TimestampFilename = MF.getTimestampFilename();
4044   std::error_code EC;
4045   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4046   if (EC)
4047     return;
4048   OS << "Timestamp file\n";
4049   OS.close();
4050   OS.clear_error(); // Avoid triggering a fatal error.
4051 }
4052 
4053 /// Given a cursor at the start of an AST file, scan ahead and drop the
4054 /// cursor into the start of the given block ID, returning false on success and
4055 /// true on failure.
4056 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4057   while (true) {
4058     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4059     if (!MaybeEntry) {
4060       // FIXME this drops errors on the floor.
4061       consumeError(MaybeEntry.takeError());
4062       return true;
4063     }
4064     llvm::BitstreamEntry Entry = MaybeEntry.get();
4065 
4066     switch (Entry.Kind) {
4067     case llvm::BitstreamEntry::Error:
4068     case llvm::BitstreamEntry::EndBlock:
4069       return true;
4070 
4071     case llvm::BitstreamEntry::Record:
4072       // Ignore top-level records.
4073       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4074         break;
4075       else {
4076         // FIXME this drops errors on the floor.
4077         consumeError(Skipped.takeError());
4078         return true;
4079       }
4080 
4081     case llvm::BitstreamEntry::SubBlock:
4082       if (Entry.ID == BlockID) {
4083         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4084           // FIXME this drops the error on the floor.
4085           consumeError(std::move(Err));
4086           return true;
4087         }
4088         // Found it!
4089         return false;
4090       }
4091 
4092       if (llvm::Error Err = Cursor.SkipBlock()) {
4093         // FIXME this drops the error on the floor.
4094         consumeError(std::move(Err));
4095         return true;
4096       }
4097     }
4098   }
4099 }
4100 
4101 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4102                                             ModuleKind Type,
4103                                             SourceLocation ImportLoc,
4104                                             unsigned ClientLoadCapabilities,
4105                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4106   llvm::SaveAndRestore<SourceLocation>
4107     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4108 
4109   // Defer any pending actions until we get to the end of reading the AST file.
4110   Deserializing AnASTFile(this);
4111 
4112   // Bump the generation number.
4113   unsigned PreviousGeneration = 0;
4114   if (ContextObj)
4115     PreviousGeneration = incrementGeneration(*ContextObj);
4116 
4117   unsigned NumModules = ModuleMgr.size();
4118   SmallVector<ImportedModule, 4> Loaded;
4119   switch (ASTReadResult ReadResult =
4120               ReadASTCore(FileName, Type, ImportLoc,
4121                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4122                           ASTFileSignature(), ClientLoadCapabilities)) {
4123   case Failure:
4124   case Missing:
4125   case OutOfDate:
4126   case VersionMismatch:
4127   case ConfigurationMismatch:
4128   case HadErrors: {
4129     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4130     for (const ImportedModule &IM : Loaded)
4131       LoadedSet.insert(IM.Mod);
4132 
4133     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4134                             PP.getLangOpts().Modules
4135                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4136                                 : nullptr);
4137 
4138     // If we find that any modules are unusable, the global index is going
4139     // to be out-of-date. Just remove it.
4140     GlobalIndex.reset();
4141     ModuleMgr.setGlobalIndex(nullptr);
4142     return ReadResult;
4143   }
4144   case Success:
4145     break;
4146   }
4147 
4148   // Here comes stuff that we only do once the entire chain is loaded.
4149 
4150   // Load the AST blocks of all of the modules that we loaded.
4151   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4152                                               MEnd = Loaded.end();
4153        M != MEnd; ++M) {
4154     ModuleFile &F = *M->Mod;
4155 
4156     // Read the AST block.
4157     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4158       return Result;
4159 
4160     // Read the extension blocks.
4161     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4162       if (ASTReadResult Result = ReadExtensionBlock(F))
4163         return Result;
4164     }
4165 
4166     // Once read, set the ModuleFile bit base offset and update the size in
4167     // bits of all files we've seen.
4168     F.GlobalBitOffset = TotalModulesSizeInBits;
4169     TotalModulesSizeInBits += F.SizeInBits;
4170     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4171 
4172     // Preload SLocEntries.
4173     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4174       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4175       // Load it through the SourceManager and don't call ReadSLocEntry()
4176       // directly because the entry may have already been loaded in which case
4177       // calling ReadSLocEntry() directly would trigger an assertion in
4178       // SourceManager.
4179       SourceMgr.getLoadedSLocEntryByID(Index);
4180     }
4181 
4182     // Map the original source file ID into the ID space of the current
4183     // compilation.
4184     if (F.OriginalSourceFileID.isValid()) {
4185       F.OriginalSourceFileID = FileID::get(
4186           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4187     }
4188 
4189     // Preload all the pending interesting identifiers by marking them out of
4190     // date.
4191     for (auto Offset : F.PreloadIdentifierOffsets) {
4192       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4193           F.IdentifierTableData + Offset);
4194 
4195       ASTIdentifierLookupTrait Trait(*this, F);
4196       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4197       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4198       auto &II = PP.getIdentifierTable().getOwn(Key);
4199       II.setOutOfDate(true);
4200 
4201       // Mark this identifier as being from an AST file so that we can track
4202       // whether we need to serialize it.
4203       markIdentifierFromAST(*this, II);
4204 
4205       // Associate the ID with the identifier so that the writer can reuse it.
4206       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4207       SetIdentifierInfo(ID, &II);
4208     }
4209   }
4210 
4211   // Setup the import locations and notify the module manager that we've
4212   // committed to these module files.
4213   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4214                                               MEnd = Loaded.end();
4215        M != MEnd; ++M) {
4216     ModuleFile &F = *M->Mod;
4217 
4218     ModuleMgr.moduleFileAccepted(&F);
4219 
4220     // Set the import location.
4221     F.DirectImportLoc = ImportLoc;
4222     // FIXME: We assume that locations from PCH / preamble do not need
4223     // any translation.
4224     if (!M->ImportedBy)
4225       F.ImportLoc = M->ImportLoc;
4226     else
4227       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4228   }
4229 
4230   if (!PP.getLangOpts().CPlusPlus ||
4231       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4232        Type != MK_PrebuiltModule)) {
4233     // Mark all of the identifiers in the identifier table as being out of date,
4234     // so that various accessors know to check the loaded modules when the
4235     // identifier is used.
4236     //
4237     // For C++ modules, we don't need information on many identifiers (just
4238     // those that provide macros or are poisoned), so we mark all of
4239     // the interesting ones via PreloadIdentifierOffsets.
4240     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4241                                 IdEnd = PP.getIdentifierTable().end();
4242          Id != IdEnd; ++Id)
4243       Id->second->setOutOfDate(true);
4244   }
4245   // Mark selectors as out of date.
4246   for (auto Sel : SelectorGeneration)
4247     SelectorOutOfDate[Sel.first] = true;
4248 
4249   // Resolve any unresolved module exports.
4250   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4251     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4252     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4253     Module *ResolvedMod = getSubmodule(GlobalID);
4254 
4255     switch (Unresolved.Kind) {
4256     case UnresolvedModuleRef::Conflict:
4257       if (ResolvedMod) {
4258         Module::Conflict Conflict;
4259         Conflict.Other = ResolvedMod;
4260         Conflict.Message = Unresolved.String.str();
4261         Unresolved.Mod->Conflicts.push_back(Conflict);
4262       }
4263       continue;
4264 
4265     case UnresolvedModuleRef::Import:
4266       if (ResolvedMod)
4267         Unresolved.Mod->Imports.insert(ResolvedMod);
4268       continue;
4269 
4270     case UnresolvedModuleRef::Export:
4271       if (ResolvedMod || Unresolved.IsWildcard)
4272         Unresolved.Mod->Exports.push_back(
4273           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4274       continue;
4275     }
4276   }
4277   UnresolvedModuleRefs.clear();
4278 
4279   if (Imported)
4280     Imported->append(ImportedModules.begin(),
4281                      ImportedModules.end());
4282 
4283   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4284   // Might be unnecessary as use declarations are only used to build the
4285   // module itself.
4286 
4287   if (ContextObj)
4288     InitializeContext();
4289 
4290   if (SemaObj)
4291     UpdateSema();
4292 
4293   if (DeserializationListener)
4294     DeserializationListener->ReaderInitialized(this);
4295 
4296   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4297   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4298     // If this AST file is a precompiled preamble, then set the
4299     // preamble file ID of the source manager to the file source file
4300     // from which the preamble was built.
4301     if (Type == MK_Preamble) {
4302       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4303     } else if (Type == MK_MainFile) {
4304       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4305     }
4306   }
4307 
4308   // For any Objective-C class definitions we have already loaded, make sure
4309   // that we load any additional categories.
4310   if (ContextObj) {
4311     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4312       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4313                          ObjCClassesLoaded[I],
4314                          PreviousGeneration);
4315     }
4316   }
4317 
4318   if (PP.getHeaderSearchInfo()
4319           .getHeaderSearchOpts()
4320           .ModulesValidateOncePerBuildSession) {
4321     // Now we are certain that the module and all modules it depends on are
4322     // up to date.  Create or update timestamp files for modules that are
4323     // located in the module cache (not for PCH files that could be anywhere
4324     // in the filesystem).
4325     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4326       ImportedModule &M = Loaded[I];
4327       if (M.Mod->Kind == MK_ImplicitModule) {
4328         updateModuleTimestamp(*M.Mod);
4329       }
4330     }
4331   }
4332 
4333   return Success;
4334 }
4335 
4336 static ASTFileSignature readASTFileSignature(StringRef PCH);
4337 
4338 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4339 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4340   // FIXME checking magic headers is done in other places such as
4341   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4342   // always done the same. Unify it all with a helper.
4343   if (!Stream.canSkipToPos(4))
4344     return llvm::createStringError(std::errc::illegal_byte_sequence,
4345                                    "file too small to contain AST file magic");
4346   for (unsigned C : {'C', 'P', 'C', 'H'})
4347     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4348       if (Res.get() != C)
4349         return llvm::createStringError(
4350             std::errc::illegal_byte_sequence,
4351             "file doesn't start with AST file magic");
4352     } else
4353       return Res.takeError();
4354   return llvm::Error::success();
4355 }
4356 
4357 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4358   switch (Kind) {
4359   case MK_PCH:
4360     return 0; // PCH
4361   case MK_ImplicitModule:
4362   case MK_ExplicitModule:
4363   case MK_PrebuiltModule:
4364     return 1; // module
4365   case MK_MainFile:
4366   case MK_Preamble:
4367     return 2; // main source file
4368   }
4369   llvm_unreachable("unknown module kind");
4370 }
4371 
4372 ASTReader::ASTReadResult
4373 ASTReader::ReadASTCore(StringRef FileName,
4374                        ModuleKind Type,
4375                        SourceLocation ImportLoc,
4376                        ModuleFile *ImportedBy,
4377                        SmallVectorImpl<ImportedModule> &Loaded,
4378                        off_t ExpectedSize, time_t ExpectedModTime,
4379                        ASTFileSignature ExpectedSignature,
4380                        unsigned ClientLoadCapabilities) {
4381   ModuleFile *M;
4382   std::string ErrorStr;
4383   ModuleManager::AddModuleResult AddResult
4384     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4385                           getGeneration(), ExpectedSize, ExpectedModTime,
4386                           ExpectedSignature, readASTFileSignature,
4387                           M, ErrorStr);
4388 
4389   switch (AddResult) {
4390   case ModuleManager::AlreadyLoaded:
4391     Diag(diag::remark_module_import)
4392         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4393         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4394     return Success;
4395 
4396   case ModuleManager::NewlyLoaded:
4397     // Load module file below.
4398     break;
4399 
4400   case ModuleManager::Missing:
4401     // The module file was missing; if the client can handle that, return
4402     // it.
4403     if (ClientLoadCapabilities & ARR_Missing)
4404       return Missing;
4405 
4406     // Otherwise, return an error.
4407     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4408                                           << FileName << !ErrorStr.empty()
4409                                           << ErrorStr;
4410     return Failure;
4411 
4412   case ModuleManager::OutOfDate:
4413     // We couldn't load the module file because it is out-of-date. If the
4414     // client can handle out-of-date, return it.
4415     if (ClientLoadCapabilities & ARR_OutOfDate)
4416       return OutOfDate;
4417 
4418     // Otherwise, return an error.
4419     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4420                                             << FileName << !ErrorStr.empty()
4421                                             << ErrorStr;
4422     return Failure;
4423   }
4424 
4425   assert(M && "Missing module file");
4426 
4427   bool ShouldFinalizePCM = false;
4428   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4429     auto &MC = getModuleManager().getModuleCache();
4430     if (ShouldFinalizePCM)
4431       MC.finalizePCM(FileName);
4432     else
4433       MC.tryToDropPCM(FileName);
4434   });
4435   ModuleFile &F = *M;
4436   BitstreamCursor &Stream = F.Stream;
4437   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4438   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4439 
4440   // Sniff for the signature.
4441   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4442     Diag(diag::err_module_file_invalid)
4443         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4444     return Failure;
4445   }
4446 
4447   // This is used for compatibility with older PCH formats.
4448   bool HaveReadControlBlock = false;
4449   while (true) {
4450     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4451     if (!MaybeEntry) {
4452       Error(MaybeEntry.takeError());
4453       return Failure;
4454     }
4455     llvm::BitstreamEntry Entry = MaybeEntry.get();
4456 
4457     switch (Entry.Kind) {
4458     case llvm::BitstreamEntry::Error:
4459     case llvm::BitstreamEntry::Record:
4460     case llvm::BitstreamEntry::EndBlock:
4461       Error("invalid record at top-level of AST file");
4462       return Failure;
4463 
4464     case llvm::BitstreamEntry::SubBlock:
4465       break;
4466     }
4467 
4468     switch (Entry.ID) {
4469     case CONTROL_BLOCK_ID:
4470       HaveReadControlBlock = true;
4471       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4472       case Success:
4473         // Check that we didn't try to load a non-module AST file as a module.
4474         //
4475         // FIXME: Should we also perform the converse check? Loading a module as
4476         // a PCH file sort of works, but it's a bit wonky.
4477         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4478              Type == MK_PrebuiltModule) &&
4479             F.ModuleName.empty()) {
4480           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4481           if (Result != OutOfDate ||
4482               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4483             Diag(diag::err_module_file_not_module) << FileName;
4484           return Result;
4485         }
4486         break;
4487 
4488       case Failure: return Failure;
4489       case Missing: return Missing;
4490       case OutOfDate: return OutOfDate;
4491       case VersionMismatch: return VersionMismatch;
4492       case ConfigurationMismatch: return ConfigurationMismatch;
4493       case HadErrors: return HadErrors;
4494       }
4495       break;
4496 
4497     case AST_BLOCK_ID:
4498       if (!HaveReadControlBlock) {
4499         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4500           Diag(diag::err_pch_version_too_old);
4501         return VersionMismatch;
4502       }
4503 
4504       // Record that we've loaded this module.
4505       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4506       ShouldFinalizePCM = true;
4507       return Success;
4508 
4509     case UNHASHED_CONTROL_BLOCK_ID:
4510       // This block is handled using look-ahead during ReadControlBlock.  We
4511       // shouldn't get here!
4512       Error("malformed block record in AST file");
4513       return Failure;
4514 
4515     default:
4516       if (llvm::Error Err = Stream.SkipBlock()) {
4517         Error(std::move(Err));
4518         return Failure;
4519       }
4520       break;
4521     }
4522   }
4523 
4524   llvm_unreachable("unexpected break; expected return");
4525 }
4526 
4527 ASTReader::ASTReadResult
4528 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4529                                     unsigned ClientLoadCapabilities) {
4530   const HeaderSearchOptions &HSOpts =
4531       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4532   bool AllowCompatibleConfigurationMismatch =
4533       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4534 
4535   ASTReadResult Result = readUnhashedControlBlockImpl(
4536       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4537       Listener.get(),
4538       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4539 
4540   // If F was directly imported by another module, it's implicitly validated by
4541   // the importing module.
4542   if (DisableValidation || WasImportedBy ||
4543       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4544     return Success;
4545 
4546   if (Result == Failure) {
4547     Error("malformed block record in AST file");
4548     return Failure;
4549   }
4550 
4551   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4552     // If this module has already been finalized in the ModuleCache, we're stuck
4553     // with it; we can only load a single version of each module.
4554     //
4555     // This can happen when a module is imported in two contexts: in one, as a
4556     // user module; in another, as a system module (due to an import from
4557     // another module marked with the [system] flag).  It usually indicates a
4558     // bug in the module map: this module should also be marked with [system].
4559     //
4560     // If -Wno-system-headers (the default), and the first import is as a
4561     // system module, then validation will fail during the as-user import,
4562     // since -Werror flags won't have been validated.  However, it's reasonable
4563     // to treat this consistently as a system module.
4564     //
4565     // If -Wsystem-headers, the PCM on disk was built with
4566     // -Wno-system-headers, and the first import is as a user module, then
4567     // validation will fail during the as-system import since the PCM on disk
4568     // doesn't guarantee that -Werror was respected.  However, the -Werror
4569     // flags were checked during the initial as-user import.
4570     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4571       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4572       return Success;
4573     }
4574   }
4575 
4576   return Result;
4577 }
4578 
4579 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4580     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4581     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4582     bool ValidateDiagnosticOptions) {
4583   // Initialize a stream.
4584   BitstreamCursor Stream(StreamData);
4585 
4586   // Sniff for the signature.
4587   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4588     // FIXME this drops the error on the floor.
4589     consumeError(std::move(Err));
4590     return Failure;
4591   }
4592 
4593   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4594   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4595     return Failure;
4596 
4597   // Read all of the records in the options block.
4598   RecordData Record;
4599   ASTReadResult Result = Success;
4600   while (true) {
4601     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4602     if (!MaybeEntry) {
4603       // FIXME this drops the error on the floor.
4604       consumeError(MaybeEntry.takeError());
4605       return Failure;
4606     }
4607     llvm::BitstreamEntry Entry = MaybeEntry.get();
4608 
4609     switch (Entry.Kind) {
4610     case llvm::BitstreamEntry::Error:
4611     case llvm::BitstreamEntry::SubBlock:
4612       return Failure;
4613 
4614     case llvm::BitstreamEntry::EndBlock:
4615       return Result;
4616 
4617     case llvm::BitstreamEntry::Record:
4618       // The interesting case.
4619       break;
4620     }
4621 
4622     // Read and process a record.
4623     Record.clear();
4624     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4625     if (!MaybeRecordType) {
4626       // FIXME this drops the error.
4627       return Failure;
4628     }
4629     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4630     case SIGNATURE:
4631       if (F)
4632         std::copy(Record.begin(), Record.end(), F->Signature.data());
4633       break;
4634     case DIAGNOSTIC_OPTIONS: {
4635       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4636       if (Listener && ValidateDiagnosticOptions &&
4637           !AllowCompatibleConfigurationMismatch &&
4638           ParseDiagnosticOptions(Record, Complain, *Listener))
4639         Result = OutOfDate; // Don't return early.  Read the signature.
4640       break;
4641     }
4642     case DIAG_PRAGMA_MAPPINGS:
4643       if (!F)
4644         break;
4645       if (F->PragmaDiagMappings.empty())
4646         F->PragmaDiagMappings.swap(Record);
4647       else
4648         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4649                                      Record.begin(), Record.end());
4650       break;
4651     }
4652   }
4653 }
4654 
4655 /// Parse a record and blob containing module file extension metadata.
4656 static bool parseModuleFileExtensionMetadata(
4657               const SmallVectorImpl<uint64_t> &Record,
4658               StringRef Blob,
4659               ModuleFileExtensionMetadata &Metadata) {
4660   if (Record.size() < 4) return true;
4661 
4662   Metadata.MajorVersion = Record[0];
4663   Metadata.MinorVersion = Record[1];
4664 
4665   unsigned BlockNameLen = Record[2];
4666   unsigned UserInfoLen = Record[3];
4667 
4668   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4669 
4670   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4671   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4672                                   Blob.data() + BlockNameLen + UserInfoLen);
4673   return false;
4674 }
4675 
4676 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4677   BitstreamCursor &Stream = F.Stream;
4678 
4679   RecordData Record;
4680   while (true) {
4681     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4682     if (!MaybeEntry) {
4683       Error(MaybeEntry.takeError());
4684       return Failure;
4685     }
4686     llvm::BitstreamEntry Entry = MaybeEntry.get();
4687 
4688     switch (Entry.Kind) {
4689     case llvm::BitstreamEntry::SubBlock:
4690       if (llvm::Error Err = Stream.SkipBlock()) {
4691         Error(std::move(Err));
4692         return Failure;
4693       }
4694       continue;
4695 
4696     case llvm::BitstreamEntry::EndBlock:
4697       return Success;
4698 
4699     case llvm::BitstreamEntry::Error:
4700       return HadErrors;
4701 
4702     case llvm::BitstreamEntry::Record:
4703       break;
4704     }
4705 
4706     Record.clear();
4707     StringRef Blob;
4708     Expected<unsigned> MaybeRecCode =
4709         Stream.readRecord(Entry.ID, Record, &Blob);
4710     if (!MaybeRecCode) {
4711       Error(MaybeRecCode.takeError());
4712       return Failure;
4713     }
4714     switch (MaybeRecCode.get()) {
4715     case EXTENSION_METADATA: {
4716       ModuleFileExtensionMetadata Metadata;
4717       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4718         return Failure;
4719 
4720       // Find a module file extension with this block name.
4721       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4722       if (Known == ModuleFileExtensions.end()) break;
4723 
4724       // Form a reader.
4725       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4726                                                              F, Stream)) {
4727         F.ExtensionReaders.push_back(std::move(Reader));
4728       }
4729 
4730       break;
4731     }
4732     }
4733   }
4734 
4735   return Success;
4736 }
4737 
4738 void ASTReader::InitializeContext() {
4739   assert(ContextObj && "no context to initialize");
4740   ASTContext &Context = *ContextObj;
4741 
4742   // If there's a listener, notify them that we "read" the translation unit.
4743   if (DeserializationListener)
4744     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4745                                       Context.getTranslationUnitDecl());
4746 
4747   // FIXME: Find a better way to deal with collisions between these
4748   // built-in types. Right now, we just ignore the problem.
4749 
4750   // Load the special types.
4751   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4752     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4753       if (!Context.CFConstantStringTypeDecl)
4754         Context.setCFConstantStringType(GetType(String));
4755     }
4756 
4757     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4758       QualType FileType = GetType(File);
4759       if (FileType.isNull()) {
4760         Error("FILE type is NULL");
4761         return;
4762       }
4763 
4764       if (!Context.FILEDecl) {
4765         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4766           Context.setFILEDecl(Typedef->getDecl());
4767         else {
4768           const TagType *Tag = FileType->getAs<TagType>();
4769           if (!Tag) {
4770             Error("Invalid FILE type in AST file");
4771             return;
4772           }
4773           Context.setFILEDecl(Tag->getDecl());
4774         }
4775       }
4776     }
4777 
4778     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4779       QualType Jmp_bufType = GetType(Jmp_buf);
4780       if (Jmp_bufType.isNull()) {
4781         Error("jmp_buf type is NULL");
4782         return;
4783       }
4784 
4785       if (!Context.jmp_bufDecl) {
4786         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4787           Context.setjmp_bufDecl(Typedef->getDecl());
4788         else {
4789           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4790           if (!Tag) {
4791             Error("Invalid jmp_buf type in AST file");
4792             return;
4793           }
4794           Context.setjmp_bufDecl(Tag->getDecl());
4795         }
4796       }
4797     }
4798 
4799     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4800       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4801       if (Sigjmp_bufType.isNull()) {
4802         Error("sigjmp_buf type is NULL");
4803         return;
4804       }
4805 
4806       if (!Context.sigjmp_bufDecl) {
4807         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4808           Context.setsigjmp_bufDecl(Typedef->getDecl());
4809         else {
4810           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4811           assert(Tag && "Invalid sigjmp_buf type in AST file");
4812           Context.setsigjmp_bufDecl(Tag->getDecl());
4813         }
4814       }
4815     }
4816 
4817     if (unsigned ObjCIdRedef
4818           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4819       if (Context.ObjCIdRedefinitionType.isNull())
4820         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4821     }
4822 
4823     if (unsigned ObjCClassRedef
4824           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4825       if (Context.ObjCClassRedefinitionType.isNull())
4826         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4827     }
4828 
4829     if (unsigned ObjCSelRedef
4830           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4831       if (Context.ObjCSelRedefinitionType.isNull())
4832         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4833     }
4834 
4835     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4836       QualType Ucontext_tType = GetType(Ucontext_t);
4837       if (Ucontext_tType.isNull()) {
4838         Error("ucontext_t type is NULL");
4839         return;
4840       }
4841 
4842       if (!Context.ucontext_tDecl) {
4843         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4844           Context.setucontext_tDecl(Typedef->getDecl());
4845         else {
4846           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4847           assert(Tag && "Invalid ucontext_t type in AST file");
4848           Context.setucontext_tDecl(Tag->getDecl());
4849         }
4850       }
4851     }
4852   }
4853 
4854   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4855 
4856   // If there were any CUDA special declarations, deserialize them.
4857   if (!CUDASpecialDeclRefs.empty()) {
4858     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4859     Context.setcudaConfigureCallDecl(
4860                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4861   }
4862 
4863   // Re-export any modules that were imported by a non-module AST file.
4864   // FIXME: This does not make macro-only imports visible again.
4865   for (auto &Import : ImportedModules) {
4866     if (Module *Imported = getSubmodule(Import.ID)) {
4867       makeModuleVisible(Imported, Module::AllVisible,
4868                         /*ImportLoc=*/Import.ImportLoc);
4869       if (Import.ImportLoc.isValid())
4870         PP.makeModuleVisible(Imported, Import.ImportLoc);
4871       // FIXME: should we tell Sema to make the module visible too?
4872     }
4873   }
4874   ImportedModules.clear();
4875 }
4876 
4877 void ASTReader::finalizeForWriting() {
4878   // Nothing to do for now.
4879 }
4880 
4881 /// Reads and return the signature record from \p PCH's control block, or
4882 /// else returns 0.
4883 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4884   BitstreamCursor Stream(PCH);
4885   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4886     // FIXME this drops the error on the floor.
4887     consumeError(std::move(Err));
4888     return ASTFileSignature();
4889   }
4890 
4891   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4892   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4893     return ASTFileSignature();
4894 
4895   // Scan for SIGNATURE inside the diagnostic options block.
4896   ASTReader::RecordData Record;
4897   while (true) {
4898     Expected<llvm::BitstreamEntry> MaybeEntry =
4899         Stream.advanceSkippingSubblocks();
4900     if (!MaybeEntry) {
4901       // FIXME this drops the error on the floor.
4902       consumeError(MaybeEntry.takeError());
4903       return ASTFileSignature();
4904     }
4905     llvm::BitstreamEntry Entry = MaybeEntry.get();
4906 
4907     if (Entry.Kind != llvm::BitstreamEntry::Record)
4908       return ASTFileSignature();
4909 
4910     Record.clear();
4911     StringRef Blob;
4912     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4913     if (!MaybeRecord) {
4914       // FIXME this drops the error on the floor.
4915       consumeError(MaybeRecord.takeError());
4916       return ASTFileSignature();
4917     }
4918     if (SIGNATURE == MaybeRecord.get())
4919       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4920                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4921   }
4922 }
4923 
4924 /// Retrieve the name of the original source file name
4925 /// directly from the AST file, without actually loading the AST
4926 /// file.
4927 std::string ASTReader::getOriginalSourceFile(
4928     const std::string &ASTFileName, FileManager &FileMgr,
4929     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4930   // Open the AST file.
4931   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4932   if (!Buffer) {
4933     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4934         << ASTFileName << Buffer.getError().message();
4935     return std::string();
4936   }
4937 
4938   // Initialize the stream
4939   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4940 
4941   // Sniff for the signature.
4942   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4943     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
4944     return std::string();
4945   }
4946 
4947   // Scan for the CONTROL_BLOCK_ID block.
4948   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4949     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4950     return std::string();
4951   }
4952 
4953   // Scan for ORIGINAL_FILE inside the control block.
4954   RecordData Record;
4955   while (true) {
4956     Expected<llvm::BitstreamEntry> MaybeEntry =
4957         Stream.advanceSkippingSubblocks();
4958     if (!MaybeEntry) {
4959       // FIXME this drops errors on the floor.
4960       consumeError(MaybeEntry.takeError());
4961       return std::string();
4962     }
4963     llvm::BitstreamEntry Entry = MaybeEntry.get();
4964 
4965     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4966       return std::string();
4967 
4968     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4969       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4970       return std::string();
4971     }
4972 
4973     Record.clear();
4974     StringRef Blob;
4975     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4976     if (!MaybeRecord) {
4977       // FIXME this drops the errors on the floor.
4978       consumeError(MaybeRecord.takeError());
4979       return std::string();
4980     }
4981     if (ORIGINAL_FILE == MaybeRecord.get())
4982       return Blob.str();
4983   }
4984 }
4985 
4986 namespace {
4987 
4988   class SimplePCHValidator : public ASTReaderListener {
4989     const LangOptions &ExistingLangOpts;
4990     const TargetOptions &ExistingTargetOpts;
4991     const PreprocessorOptions &ExistingPPOpts;
4992     std::string ExistingModuleCachePath;
4993     FileManager &FileMgr;
4994 
4995   public:
4996     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4997                        const TargetOptions &ExistingTargetOpts,
4998                        const PreprocessorOptions &ExistingPPOpts,
4999                        StringRef ExistingModuleCachePath,
5000                        FileManager &FileMgr)
5001       : ExistingLangOpts(ExistingLangOpts),
5002         ExistingTargetOpts(ExistingTargetOpts),
5003         ExistingPPOpts(ExistingPPOpts),
5004         ExistingModuleCachePath(ExistingModuleCachePath),
5005         FileMgr(FileMgr) {}
5006 
5007     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5008                              bool AllowCompatibleDifferences) override {
5009       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5010                                   AllowCompatibleDifferences);
5011     }
5012 
5013     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5014                            bool AllowCompatibleDifferences) override {
5015       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5016                                 AllowCompatibleDifferences);
5017     }
5018 
5019     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5020                                  StringRef SpecificModuleCachePath,
5021                                  bool Complain) override {
5022       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5023                                       ExistingModuleCachePath,
5024                                       nullptr, ExistingLangOpts);
5025     }
5026 
5027     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5028                                  bool Complain,
5029                                  std::string &SuggestedPredefines) override {
5030       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5031                                       SuggestedPredefines, ExistingLangOpts);
5032     }
5033   };
5034 
5035 } // namespace
5036 
5037 bool ASTReader::readASTFileControlBlock(
5038     StringRef Filename, FileManager &FileMgr,
5039     const PCHContainerReader &PCHContainerRdr,
5040     bool FindModuleFileExtensions,
5041     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5042   // Open the AST file.
5043   // FIXME: This allows use of the VFS; we do not allow use of the
5044   // VFS when actually loading a module.
5045   auto Buffer = FileMgr.getBufferForFile(Filename);
5046   if (!Buffer) {
5047     return true;
5048   }
5049 
5050   // Initialize the stream
5051   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5052   BitstreamCursor Stream(Bytes);
5053 
5054   // Sniff for the signature.
5055   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5056     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5057     return true;
5058   }
5059 
5060   // Scan for the CONTROL_BLOCK_ID block.
5061   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5062     return true;
5063 
5064   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5065   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5066   bool NeedsImports = Listener.needsImportVisitation();
5067   BitstreamCursor InputFilesCursor;
5068 
5069   RecordData Record;
5070   std::string ModuleDir;
5071   bool DoneWithControlBlock = false;
5072   while (!DoneWithControlBlock) {
5073     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5074     if (!MaybeEntry) {
5075       // FIXME this drops the error on the floor.
5076       consumeError(MaybeEntry.takeError());
5077       return true;
5078     }
5079     llvm::BitstreamEntry Entry = MaybeEntry.get();
5080 
5081     switch (Entry.Kind) {
5082     case llvm::BitstreamEntry::SubBlock: {
5083       switch (Entry.ID) {
5084       case OPTIONS_BLOCK_ID: {
5085         std::string IgnoredSuggestedPredefines;
5086         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5087                              /*AllowCompatibleConfigurationMismatch*/ false,
5088                              Listener, IgnoredSuggestedPredefines) != Success)
5089           return true;
5090         break;
5091       }
5092 
5093       case INPUT_FILES_BLOCK_ID:
5094         InputFilesCursor = Stream;
5095         if (llvm::Error Err = Stream.SkipBlock()) {
5096           // FIXME this drops the error on the floor.
5097           consumeError(std::move(Err));
5098           return true;
5099         }
5100         if (NeedsInputFiles &&
5101             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5102           return true;
5103         break;
5104 
5105       default:
5106         if (llvm::Error Err = Stream.SkipBlock()) {
5107           // FIXME this drops the error on the floor.
5108           consumeError(std::move(Err));
5109           return true;
5110         }
5111         break;
5112       }
5113 
5114       continue;
5115     }
5116 
5117     case llvm::BitstreamEntry::EndBlock:
5118       DoneWithControlBlock = true;
5119       break;
5120 
5121     case llvm::BitstreamEntry::Error:
5122       return true;
5123 
5124     case llvm::BitstreamEntry::Record:
5125       break;
5126     }
5127 
5128     if (DoneWithControlBlock) break;
5129 
5130     Record.clear();
5131     StringRef Blob;
5132     Expected<unsigned> MaybeRecCode =
5133         Stream.readRecord(Entry.ID, Record, &Blob);
5134     if (!MaybeRecCode) {
5135       // FIXME this drops the error.
5136       return Failure;
5137     }
5138     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5139     case METADATA:
5140       if (Record[0] != VERSION_MAJOR)
5141         return true;
5142       if (Listener.ReadFullVersionInformation(Blob))
5143         return true;
5144       break;
5145     case MODULE_NAME:
5146       Listener.ReadModuleName(Blob);
5147       break;
5148     case MODULE_DIRECTORY:
5149       ModuleDir = Blob;
5150       break;
5151     case MODULE_MAP_FILE: {
5152       unsigned Idx = 0;
5153       auto Path = ReadString(Record, Idx);
5154       ResolveImportedPath(Path, ModuleDir);
5155       Listener.ReadModuleMapFile(Path);
5156       break;
5157     }
5158     case INPUT_FILE_OFFSETS: {
5159       if (!NeedsInputFiles)
5160         break;
5161 
5162       unsigned NumInputFiles = Record[0];
5163       unsigned NumUserFiles = Record[1];
5164       const llvm::support::unaligned_uint64_t *InputFileOffs =
5165           (const llvm::support::unaligned_uint64_t *)Blob.data();
5166       for (unsigned I = 0; I != NumInputFiles; ++I) {
5167         // Go find this input file.
5168         bool isSystemFile = I >= NumUserFiles;
5169 
5170         if (isSystemFile && !NeedsSystemInputFiles)
5171           break; // the rest are system input files
5172 
5173         BitstreamCursor &Cursor = InputFilesCursor;
5174         SavedStreamPosition SavedPosition(Cursor);
5175         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5176           // FIXME this drops errors on the floor.
5177           consumeError(std::move(Err));
5178         }
5179 
5180         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5181         if (!MaybeCode) {
5182           // FIXME this drops errors on the floor.
5183           consumeError(MaybeCode.takeError());
5184         }
5185         unsigned Code = MaybeCode.get();
5186 
5187         RecordData Record;
5188         StringRef Blob;
5189         bool shouldContinue = false;
5190         Expected<unsigned> MaybeRecordType =
5191             Cursor.readRecord(Code, Record, &Blob);
5192         if (!MaybeRecordType) {
5193           // FIXME this drops errors on the floor.
5194           consumeError(MaybeRecordType.takeError());
5195         }
5196         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5197         case INPUT_FILE:
5198           bool Overridden = static_cast<bool>(Record[3]);
5199           std::string Filename = Blob;
5200           ResolveImportedPath(Filename, ModuleDir);
5201           shouldContinue = Listener.visitInputFile(
5202               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5203           break;
5204         }
5205         if (!shouldContinue)
5206           break;
5207       }
5208       break;
5209     }
5210 
5211     case IMPORTS: {
5212       if (!NeedsImports)
5213         break;
5214 
5215       unsigned Idx = 0, N = Record.size();
5216       while (Idx < N) {
5217         // Read information about the AST file.
5218         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5219         std::string ModuleName = ReadString(Record, Idx);
5220         std::string Filename = ReadString(Record, Idx);
5221         ResolveImportedPath(Filename, ModuleDir);
5222         Listener.visitImport(ModuleName, Filename);
5223       }
5224       break;
5225     }
5226 
5227     default:
5228       // No other validation to perform.
5229       break;
5230     }
5231   }
5232 
5233   // Look for module file extension blocks, if requested.
5234   if (FindModuleFileExtensions) {
5235     BitstreamCursor SavedStream = Stream;
5236     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5237       bool DoneWithExtensionBlock = false;
5238       while (!DoneWithExtensionBlock) {
5239         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5240         if (!MaybeEntry) {
5241           // FIXME this drops the error.
5242           return true;
5243         }
5244         llvm::BitstreamEntry Entry = MaybeEntry.get();
5245 
5246         switch (Entry.Kind) {
5247         case llvm::BitstreamEntry::SubBlock:
5248           if (llvm::Error Err = Stream.SkipBlock()) {
5249             // FIXME this drops the error on the floor.
5250             consumeError(std::move(Err));
5251             return true;
5252           }
5253           continue;
5254 
5255         case llvm::BitstreamEntry::EndBlock:
5256           DoneWithExtensionBlock = true;
5257           continue;
5258 
5259         case llvm::BitstreamEntry::Error:
5260           return true;
5261 
5262         case llvm::BitstreamEntry::Record:
5263           break;
5264         }
5265 
5266        Record.clear();
5267        StringRef Blob;
5268        Expected<unsigned> MaybeRecCode =
5269            Stream.readRecord(Entry.ID, Record, &Blob);
5270        if (!MaybeRecCode) {
5271          // FIXME this drops the error.
5272          return true;
5273        }
5274        switch (MaybeRecCode.get()) {
5275        case EXTENSION_METADATA: {
5276          ModuleFileExtensionMetadata Metadata;
5277          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5278            return true;
5279 
5280          Listener.readModuleFileExtension(Metadata);
5281          break;
5282        }
5283        }
5284       }
5285     }
5286     Stream = SavedStream;
5287   }
5288 
5289   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5290   if (readUnhashedControlBlockImpl(
5291           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5292           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5293           ValidateDiagnosticOptions) != Success)
5294     return true;
5295 
5296   return false;
5297 }
5298 
5299 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5300                                     const PCHContainerReader &PCHContainerRdr,
5301                                     const LangOptions &LangOpts,
5302                                     const TargetOptions &TargetOpts,
5303                                     const PreprocessorOptions &PPOpts,
5304                                     StringRef ExistingModuleCachePath) {
5305   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5306                                ExistingModuleCachePath, FileMgr);
5307   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5308                                   /*FindModuleFileExtensions=*/false,
5309                                   validator,
5310                                   /*ValidateDiagnosticOptions=*/true);
5311 }
5312 
5313 ASTReader::ASTReadResult
5314 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5315   // Enter the submodule block.
5316   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5317     Error(std::move(Err));
5318     return Failure;
5319   }
5320 
5321   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5322   bool First = true;
5323   Module *CurrentModule = nullptr;
5324   RecordData Record;
5325   while (true) {
5326     Expected<llvm::BitstreamEntry> MaybeEntry =
5327         F.Stream.advanceSkippingSubblocks();
5328     if (!MaybeEntry) {
5329       Error(MaybeEntry.takeError());
5330       return Failure;
5331     }
5332     llvm::BitstreamEntry Entry = MaybeEntry.get();
5333 
5334     switch (Entry.Kind) {
5335     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5336     case llvm::BitstreamEntry::Error:
5337       Error("malformed block record in AST file");
5338       return Failure;
5339     case llvm::BitstreamEntry::EndBlock:
5340       return Success;
5341     case llvm::BitstreamEntry::Record:
5342       // The interesting case.
5343       break;
5344     }
5345 
5346     // Read a record.
5347     StringRef Blob;
5348     Record.clear();
5349     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5350     if (!MaybeKind) {
5351       Error(MaybeKind.takeError());
5352       return Failure;
5353     }
5354     unsigned Kind = MaybeKind.get();
5355 
5356     if ((Kind == SUBMODULE_METADATA) != First) {
5357       Error("submodule metadata record should be at beginning of block");
5358       return Failure;
5359     }
5360     First = false;
5361 
5362     // Submodule information is only valid if we have a current module.
5363     // FIXME: Should we error on these cases?
5364     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5365         Kind != SUBMODULE_DEFINITION)
5366       continue;
5367 
5368     switch (Kind) {
5369     default:  // Default behavior: ignore.
5370       break;
5371 
5372     case SUBMODULE_DEFINITION: {
5373       if (Record.size() < 12) {
5374         Error("malformed module definition");
5375         return Failure;
5376       }
5377 
5378       StringRef Name = Blob;
5379       unsigned Idx = 0;
5380       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5381       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5382       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5383       bool IsFramework = Record[Idx++];
5384       bool IsExplicit = Record[Idx++];
5385       bool IsSystem = Record[Idx++];
5386       bool IsExternC = Record[Idx++];
5387       bool InferSubmodules = Record[Idx++];
5388       bool InferExplicitSubmodules = Record[Idx++];
5389       bool InferExportWildcard = Record[Idx++];
5390       bool ConfigMacrosExhaustive = Record[Idx++];
5391       bool ModuleMapIsPrivate = Record[Idx++];
5392 
5393       Module *ParentModule = nullptr;
5394       if (Parent)
5395         ParentModule = getSubmodule(Parent);
5396 
5397       // Retrieve this (sub)module from the module map, creating it if
5398       // necessary.
5399       CurrentModule =
5400           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5401               .first;
5402 
5403       // FIXME: set the definition loc for CurrentModule, or call
5404       // ModMap.setInferredModuleAllowedBy()
5405 
5406       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5407       if (GlobalIndex >= SubmodulesLoaded.size() ||
5408           SubmodulesLoaded[GlobalIndex]) {
5409         Error("too many submodules");
5410         return Failure;
5411       }
5412 
5413       if (!ParentModule) {
5414         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5415           // Don't emit module relocation error if we have -fno-validate-pch
5416           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5417               CurFile != F.File) {
5418             if (!Diags.isDiagnosticInFlight()) {
5419               Diag(diag::err_module_file_conflict)
5420                 << CurrentModule->getTopLevelModuleName()
5421                 << CurFile->getName()
5422                 << F.File->getName();
5423             }
5424             return Failure;
5425           }
5426         }
5427 
5428         CurrentModule->setASTFile(F.File);
5429         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5430       }
5431 
5432       CurrentModule->Kind = Kind;
5433       CurrentModule->Signature = F.Signature;
5434       CurrentModule->IsFromModuleFile = true;
5435       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5436       CurrentModule->IsExternC = IsExternC;
5437       CurrentModule->InferSubmodules = InferSubmodules;
5438       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5439       CurrentModule->InferExportWildcard = InferExportWildcard;
5440       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5441       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5442       if (DeserializationListener)
5443         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5444 
5445       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5446 
5447       // Clear out data that will be replaced by what is in the module file.
5448       CurrentModule->LinkLibraries.clear();
5449       CurrentModule->ConfigMacros.clear();
5450       CurrentModule->UnresolvedConflicts.clear();
5451       CurrentModule->Conflicts.clear();
5452 
5453       // The module is available unless it's missing a requirement; relevant
5454       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5455       // Missing headers that were present when the module was built do not
5456       // make it unavailable -- if we got this far, this must be an explicitly
5457       // imported module file.
5458       CurrentModule->Requirements.clear();
5459       CurrentModule->MissingHeaders.clear();
5460       CurrentModule->IsMissingRequirement =
5461           ParentModule && ParentModule->IsMissingRequirement;
5462       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5463       break;
5464     }
5465 
5466     case SUBMODULE_UMBRELLA_HEADER: {
5467       std::string Filename = Blob;
5468       ResolveImportedPath(F, Filename);
5469       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5470         if (!CurrentModule->getUmbrellaHeader())
5471           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5472         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5473           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5474             Error("mismatched umbrella headers in submodule");
5475           return OutOfDate;
5476         }
5477       }
5478       break;
5479     }
5480 
5481     case SUBMODULE_HEADER:
5482     case SUBMODULE_EXCLUDED_HEADER:
5483     case SUBMODULE_PRIVATE_HEADER:
5484       // We lazily associate headers with their modules via the HeaderInfo table.
5485       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5486       // of complete filenames or remove it entirely.
5487       break;
5488 
5489     case SUBMODULE_TEXTUAL_HEADER:
5490     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5491       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5492       // them here.
5493       break;
5494 
5495     case SUBMODULE_TOPHEADER:
5496       CurrentModule->addTopHeaderFilename(Blob);
5497       break;
5498 
5499     case SUBMODULE_UMBRELLA_DIR: {
5500       std::string Dirname = Blob;
5501       ResolveImportedPath(F, Dirname);
5502       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5503         if (!CurrentModule->getUmbrellaDir())
5504           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5505         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5506           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5507             Error("mismatched umbrella directories in submodule");
5508           return OutOfDate;
5509         }
5510       }
5511       break;
5512     }
5513 
5514     case SUBMODULE_METADATA: {
5515       F.BaseSubmoduleID = getTotalNumSubmodules();
5516       F.LocalNumSubmodules = Record[0];
5517       unsigned LocalBaseSubmoduleID = Record[1];
5518       if (F.LocalNumSubmodules > 0) {
5519         // Introduce the global -> local mapping for submodules within this
5520         // module.
5521         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5522 
5523         // Introduce the local -> global mapping for submodules within this
5524         // module.
5525         F.SubmoduleRemap.insertOrReplace(
5526           std::make_pair(LocalBaseSubmoduleID,
5527                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5528 
5529         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5530       }
5531       break;
5532     }
5533 
5534     case SUBMODULE_IMPORTS:
5535       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5536         UnresolvedModuleRef Unresolved;
5537         Unresolved.File = &F;
5538         Unresolved.Mod = CurrentModule;
5539         Unresolved.ID = Record[Idx];
5540         Unresolved.Kind = UnresolvedModuleRef::Import;
5541         Unresolved.IsWildcard = false;
5542         UnresolvedModuleRefs.push_back(Unresolved);
5543       }
5544       break;
5545 
5546     case SUBMODULE_EXPORTS:
5547       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5548         UnresolvedModuleRef Unresolved;
5549         Unresolved.File = &F;
5550         Unresolved.Mod = CurrentModule;
5551         Unresolved.ID = Record[Idx];
5552         Unresolved.Kind = UnresolvedModuleRef::Export;
5553         Unresolved.IsWildcard = Record[Idx + 1];
5554         UnresolvedModuleRefs.push_back(Unresolved);
5555       }
5556 
5557       // Once we've loaded the set of exports, there's no reason to keep
5558       // the parsed, unresolved exports around.
5559       CurrentModule->UnresolvedExports.clear();
5560       break;
5561 
5562     case SUBMODULE_REQUIRES:
5563       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5564                                     PP.getTargetInfo());
5565       break;
5566 
5567     case SUBMODULE_LINK_LIBRARY:
5568       ModMap.resolveLinkAsDependencies(CurrentModule);
5569       CurrentModule->LinkLibraries.push_back(
5570                                          Module::LinkLibrary(Blob, Record[0]));
5571       break;
5572 
5573     case SUBMODULE_CONFIG_MACRO:
5574       CurrentModule->ConfigMacros.push_back(Blob.str());
5575       break;
5576 
5577     case SUBMODULE_CONFLICT: {
5578       UnresolvedModuleRef Unresolved;
5579       Unresolved.File = &F;
5580       Unresolved.Mod = CurrentModule;
5581       Unresolved.ID = Record[0];
5582       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5583       Unresolved.IsWildcard = false;
5584       Unresolved.String = Blob;
5585       UnresolvedModuleRefs.push_back(Unresolved);
5586       break;
5587     }
5588 
5589     case SUBMODULE_INITIALIZERS: {
5590       if (!ContextObj)
5591         break;
5592       SmallVector<uint32_t, 16> Inits;
5593       for (auto &ID : Record)
5594         Inits.push_back(getGlobalDeclID(F, ID));
5595       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5596       break;
5597     }
5598 
5599     case SUBMODULE_EXPORT_AS:
5600       CurrentModule->ExportAsModule = Blob.str();
5601       ModMap.addLinkAsDependency(CurrentModule);
5602       break;
5603     }
5604   }
5605 }
5606 
5607 /// Parse the record that corresponds to a LangOptions data
5608 /// structure.
5609 ///
5610 /// This routine parses the language options from the AST file and then gives
5611 /// them to the AST listener if one is set.
5612 ///
5613 /// \returns true if the listener deems the file unacceptable, false otherwise.
5614 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5615                                      bool Complain,
5616                                      ASTReaderListener &Listener,
5617                                      bool AllowCompatibleDifferences) {
5618   LangOptions LangOpts;
5619   unsigned Idx = 0;
5620 #define LANGOPT(Name, Bits, Default, Description) \
5621   LangOpts.Name = Record[Idx++];
5622 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5623   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5624 #include "clang/Basic/LangOptions.def"
5625 #define SANITIZER(NAME, ID)                                                    \
5626   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5627 #include "clang/Basic/Sanitizers.def"
5628 
5629   for (unsigned N = Record[Idx++]; N; --N)
5630     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5631 
5632   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5633   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5634   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5635 
5636   LangOpts.CurrentModule = ReadString(Record, Idx);
5637 
5638   // Comment options.
5639   for (unsigned N = Record[Idx++]; N; --N) {
5640     LangOpts.CommentOpts.BlockCommandNames.push_back(
5641       ReadString(Record, Idx));
5642   }
5643   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5644 
5645   // OpenMP offloading options.
5646   for (unsigned N = Record[Idx++]; N; --N) {
5647     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5648   }
5649 
5650   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5651 
5652   return Listener.ReadLanguageOptions(LangOpts, Complain,
5653                                       AllowCompatibleDifferences);
5654 }
5655 
5656 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5657                                    ASTReaderListener &Listener,
5658                                    bool AllowCompatibleDifferences) {
5659   unsigned Idx = 0;
5660   TargetOptions TargetOpts;
5661   TargetOpts.Triple = ReadString(Record, Idx);
5662   TargetOpts.CPU = ReadString(Record, Idx);
5663   TargetOpts.ABI = ReadString(Record, Idx);
5664   for (unsigned N = Record[Idx++]; N; --N) {
5665     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5666   }
5667   for (unsigned N = Record[Idx++]; N; --N) {
5668     TargetOpts.Features.push_back(ReadString(Record, Idx));
5669   }
5670 
5671   return Listener.ReadTargetOptions(TargetOpts, Complain,
5672                                     AllowCompatibleDifferences);
5673 }
5674 
5675 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5676                                        ASTReaderListener &Listener) {
5677   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5678   unsigned Idx = 0;
5679 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5680 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5681   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5682 #include "clang/Basic/DiagnosticOptions.def"
5683 
5684   for (unsigned N = Record[Idx++]; N; --N)
5685     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5686   for (unsigned N = Record[Idx++]; N; --N)
5687     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5688 
5689   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5690 }
5691 
5692 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5693                                        ASTReaderListener &Listener) {
5694   FileSystemOptions FSOpts;
5695   unsigned Idx = 0;
5696   FSOpts.WorkingDir = ReadString(Record, Idx);
5697   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5698 }
5699 
5700 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5701                                          bool Complain,
5702                                          ASTReaderListener &Listener) {
5703   HeaderSearchOptions HSOpts;
5704   unsigned Idx = 0;
5705   HSOpts.Sysroot = ReadString(Record, Idx);
5706 
5707   // Include entries.
5708   for (unsigned N = Record[Idx++]; N; --N) {
5709     std::string Path = ReadString(Record, Idx);
5710     frontend::IncludeDirGroup Group
5711       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5712     bool IsFramework = Record[Idx++];
5713     bool IgnoreSysRoot = Record[Idx++];
5714     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5715                                     IgnoreSysRoot);
5716   }
5717 
5718   // System header prefixes.
5719   for (unsigned N = Record[Idx++]; N; --N) {
5720     std::string Prefix = ReadString(Record, Idx);
5721     bool IsSystemHeader = Record[Idx++];
5722     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5723   }
5724 
5725   HSOpts.ResourceDir = ReadString(Record, Idx);
5726   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5727   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5728   HSOpts.DisableModuleHash = Record[Idx++];
5729   HSOpts.ImplicitModuleMaps = Record[Idx++];
5730   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5731   HSOpts.UseBuiltinIncludes = Record[Idx++];
5732   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5733   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5734   HSOpts.UseLibcxx = Record[Idx++];
5735   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5736 
5737   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5738                                           Complain);
5739 }
5740 
5741 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5742                                          bool Complain,
5743                                          ASTReaderListener &Listener,
5744                                          std::string &SuggestedPredefines) {
5745   PreprocessorOptions PPOpts;
5746   unsigned Idx = 0;
5747 
5748   // Macro definitions/undefs
5749   for (unsigned N = Record[Idx++]; N; --N) {
5750     std::string Macro = ReadString(Record, Idx);
5751     bool IsUndef = Record[Idx++];
5752     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5753   }
5754 
5755   // Includes
5756   for (unsigned N = Record[Idx++]; N; --N) {
5757     PPOpts.Includes.push_back(ReadString(Record, Idx));
5758   }
5759 
5760   // Macro Includes
5761   for (unsigned N = Record[Idx++]; N; --N) {
5762     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5763   }
5764 
5765   PPOpts.UsePredefines = Record[Idx++];
5766   PPOpts.DetailedRecord = Record[Idx++];
5767   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5768   PPOpts.ObjCXXARCStandardLibrary =
5769     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5770   SuggestedPredefines.clear();
5771   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5772                                           SuggestedPredefines);
5773 }
5774 
5775 std::pair<ModuleFile *, unsigned>
5776 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5777   GlobalPreprocessedEntityMapType::iterator
5778   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5779   assert(I != GlobalPreprocessedEntityMap.end() &&
5780          "Corrupted global preprocessed entity map");
5781   ModuleFile *M = I->second;
5782   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5783   return std::make_pair(M, LocalIndex);
5784 }
5785 
5786 llvm::iterator_range<PreprocessingRecord::iterator>
5787 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5788   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5789     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5790                                              Mod.NumPreprocessedEntities);
5791 
5792   return llvm::make_range(PreprocessingRecord::iterator(),
5793                           PreprocessingRecord::iterator());
5794 }
5795 
5796 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5797 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5798   return llvm::make_range(
5799       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5800       ModuleDeclIterator(this, &Mod,
5801                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5802 }
5803 
5804 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5805   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5806   assert(I != GlobalSkippedRangeMap.end() &&
5807     "Corrupted global skipped range map");
5808   ModuleFile *M = I->second;
5809   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5810   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5811   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5812   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5813                     TranslateSourceLocation(*M, RawRange.getEnd()));
5814   assert(Range.isValid());
5815   return Range;
5816 }
5817 
5818 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5819   PreprocessedEntityID PPID = Index+1;
5820   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5821   ModuleFile &M = *PPInfo.first;
5822   unsigned LocalIndex = PPInfo.second;
5823   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5824 
5825   if (!PP.getPreprocessingRecord()) {
5826     Error("no preprocessing record");
5827     return nullptr;
5828   }
5829 
5830   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5831   if (llvm::Error Err =
5832           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5833     Error(std::move(Err));
5834     return nullptr;
5835   }
5836 
5837   Expected<llvm::BitstreamEntry> MaybeEntry =
5838       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5839   if (!MaybeEntry) {
5840     Error(MaybeEntry.takeError());
5841     return nullptr;
5842   }
5843   llvm::BitstreamEntry Entry = MaybeEntry.get();
5844 
5845   if (Entry.Kind != llvm::BitstreamEntry::Record)
5846     return nullptr;
5847 
5848   // Read the record.
5849   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5850                     TranslateSourceLocation(M, PPOffs.getEnd()));
5851   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5852   StringRef Blob;
5853   RecordData Record;
5854   Expected<unsigned> MaybeRecType =
5855       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5856   if (!MaybeRecType) {
5857     Error(MaybeRecType.takeError());
5858     return nullptr;
5859   }
5860   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5861   case PPD_MACRO_EXPANSION: {
5862     bool isBuiltin = Record[0];
5863     IdentifierInfo *Name = nullptr;
5864     MacroDefinitionRecord *Def = nullptr;
5865     if (isBuiltin)
5866       Name = getLocalIdentifier(M, Record[1]);
5867     else {
5868       PreprocessedEntityID GlobalID =
5869           getGlobalPreprocessedEntityID(M, Record[1]);
5870       Def = cast<MacroDefinitionRecord>(
5871           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5872     }
5873 
5874     MacroExpansion *ME;
5875     if (isBuiltin)
5876       ME = new (PPRec) MacroExpansion(Name, Range);
5877     else
5878       ME = new (PPRec) MacroExpansion(Def, Range);
5879 
5880     return ME;
5881   }
5882 
5883   case PPD_MACRO_DEFINITION: {
5884     // Decode the identifier info and then check again; if the macro is
5885     // still defined and associated with the identifier,
5886     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5887     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5888 
5889     if (DeserializationListener)
5890       DeserializationListener->MacroDefinitionRead(PPID, MD);
5891 
5892     return MD;
5893   }
5894 
5895   case PPD_INCLUSION_DIRECTIVE: {
5896     const char *FullFileNameStart = Blob.data() + Record[0];
5897     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5898     const FileEntry *File = nullptr;
5899     if (!FullFileName.empty())
5900       if (auto FE = PP.getFileManager().getFile(FullFileName))
5901         File = *FE;
5902 
5903     // FIXME: Stable encoding
5904     InclusionDirective::InclusionKind Kind
5905       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5906     InclusionDirective *ID
5907       = new (PPRec) InclusionDirective(PPRec, Kind,
5908                                        StringRef(Blob.data(), Record[0]),
5909                                        Record[1], Record[3],
5910                                        File,
5911                                        Range);
5912     return ID;
5913   }
5914   }
5915 
5916   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5917 }
5918 
5919 /// Find the next module that contains entities and return the ID
5920 /// of the first entry.
5921 ///
5922 /// \param SLocMapI points at a chunk of a module that contains no
5923 /// preprocessed entities or the entities it contains are not the ones we are
5924 /// looking for.
5925 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5926                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5927   ++SLocMapI;
5928   for (GlobalSLocOffsetMapType::const_iterator
5929          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5930     ModuleFile &M = *SLocMapI->second;
5931     if (M.NumPreprocessedEntities)
5932       return M.BasePreprocessedEntityID;
5933   }
5934 
5935   return getTotalNumPreprocessedEntities();
5936 }
5937 
5938 namespace {
5939 
5940 struct PPEntityComp {
5941   const ASTReader &Reader;
5942   ModuleFile &M;
5943 
5944   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5945 
5946   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5947     SourceLocation LHS = getLoc(L);
5948     SourceLocation RHS = getLoc(R);
5949     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5950   }
5951 
5952   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5953     SourceLocation LHS = getLoc(L);
5954     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5955   }
5956 
5957   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5958     SourceLocation RHS = getLoc(R);
5959     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5960   }
5961 
5962   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5963     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5964   }
5965 };
5966 
5967 } // namespace
5968 
5969 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5970                                                        bool EndsAfter) const {
5971   if (SourceMgr.isLocalSourceLocation(Loc))
5972     return getTotalNumPreprocessedEntities();
5973 
5974   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5975       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5976   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5977          "Corrupted global sloc offset map");
5978 
5979   if (SLocMapI->second->NumPreprocessedEntities == 0)
5980     return findNextPreprocessedEntity(SLocMapI);
5981 
5982   ModuleFile &M = *SLocMapI->second;
5983 
5984   using pp_iterator = const PPEntityOffset *;
5985 
5986   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5987   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5988 
5989   size_t Count = M.NumPreprocessedEntities;
5990   size_t Half;
5991   pp_iterator First = pp_begin;
5992   pp_iterator PPI;
5993 
5994   if (EndsAfter) {
5995     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5996                            PPEntityComp(*this, M));
5997   } else {
5998     // Do a binary search manually instead of using std::lower_bound because
5999     // The end locations of entities may be unordered (when a macro expansion
6000     // is inside another macro argument), but for this case it is not important
6001     // whether we get the first macro expansion or its containing macro.
6002     while (Count > 0) {
6003       Half = Count / 2;
6004       PPI = First;
6005       std::advance(PPI, Half);
6006       if (SourceMgr.isBeforeInTranslationUnit(
6007               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6008         First = PPI;
6009         ++First;
6010         Count = Count - Half - 1;
6011       } else
6012         Count = Half;
6013     }
6014   }
6015 
6016   if (PPI == pp_end)
6017     return findNextPreprocessedEntity(SLocMapI);
6018 
6019   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6020 }
6021 
6022 /// Returns a pair of [Begin, End) indices of preallocated
6023 /// preprocessed entities that \arg Range encompasses.
6024 std::pair<unsigned, unsigned>
6025     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6026   if (Range.isInvalid())
6027     return std::make_pair(0,0);
6028   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6029 
6030   PreprocessedEntityID BeginID =
6031       findPreprocessedEntity(Range.getBegin(), false);
6032   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6033   return std::make_pair(BeginID, EndID);
6034 }
6035 
6036 /// Optionally returns true or false if the preallocated preprocessed
6037 /// entity with index \arg Index came from file \arg FID.
6038 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6039                                                              FileID FID) {
6040   if (FID.isInvalid())
6041     return false;
6042 
6043   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6044   ModuleFile &M = *PPInfo.first;
6045   unsigned LocalIndex = PPInfo.second;
6046   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6047 
6048   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6049   if (Loc.isInvalid())
6050     return false;
6051 
6052   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6053     return true;
6054   else
6055     return false;
6056 }
6057 
6058 namespace {
6059 
6060   /// Visitor used to search for information about a header file.
6061   class HeaderFileInfoVisitor {
6062     const FileEntry *FE;
6063     Optional<HeaderFileInfo> HFI;
6064 
6065   public:
6066     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6067 
6068     bool operator()(ModuleFile &M) {
6069       HeaderFileInfoLookupTable *Table
6070         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6071       if (!Table)
6072         return false;
6073 
6074       // Look in the on-disk hash table for an entry for this file name.
6075       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6076       if (Pos == Table->end())
6077         return false;
6078 
6079       HFI = *Pos;
6080       return true;
6081     }
6082 
6083     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6084   };
6085 
6086 } // namespace
6087 
6088 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6089   HeaderFileInfoVisitor Visitor(FE);
6090   ModuleMgr.visit(Visitor);
6091   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6092     return *HFI;
6093 
6094   return HeaderFileInfo();
6095 }
6096 
6097 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6098   using DiagState = DiagnosticsEngine::DiagState;
6099   SmallVector<DiagState *, 32> DiagStates;
6100 
6101   for (ModuleFile &F : ModuleMgr) {
6102     unsigned Idx = 0;
6103     auto &Record = F.PragmaDiagMappings;
6104     if (Record.empty())
6105       continue;
6106 
6107     DiagStates.clear();
6108 
6109     auto ReadDiagState =
6110         [&](const DiagState &BasedOn, SourceLocation Loc,
6111             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6112       unsigned BackrefID = Record[Idx++];
6113       if (BackrefID != 0)
6114         return DiagStates[BackrefID - 1];
6115 
6116       // A new DiagState was created here.
6117       Diag.DiagStates.push_back(BasedOn);
6118       DiagState *NewState = &Diag.DiagStates.back();
6119       DiagStates.push_back(NewState);
6120       unsigned Size = Record[Idx++];
6121       assert(Idx + Size * 2 <= Record.size() &&
6122              "Invalid data, not enough diag/map pairs");
6123       while (Size--) {
6124         unsigned DiagID = Record[Idx++];
6125         DiagnosticMapping NewMapping =
6126             DiagnosticMapping::deserialize(Record[Idx++]);
6127         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6128           continue;
6129 
6130         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6131 
6132         // If this mapping was specified as a warning but the severity was
6133         // upgraded due to diagnostic settings, simulate the current diagnostic
6134         // settings (and use a warning).
6135         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6136           NewMapping.setSeverity(diag::Severity::Warning);
6137           NewMapping.setUpgradedFromWarning(false);
6138         }
6139 
6140         Mapping = NewMapping;
6141       }
6142       return NewState;
6143     };
6144 
6145     // Read the first state.
6146     DiagState *FirstState;
6147     if (F.Kind == MK_ImplicitModule) {
6148       // Implicitly-built modules are reused with different diagnostic
6149       // settings.  Use the initial diagnostic state from Diag to simulate this
6150       // compilation's diagnostic settings.
6151       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6152       DiagStates.push_back(FirstState);
6153 
6154       // Skip the initial diagnostic state from the serialized module.
6155       assert(Record[1] == 0 &&
6156              "Invalid data, unexpected backref in initial state");
6157       Idx = 3 + Record[2] * 2;
6158       assert(Idx < Record.size() &&
6159              "Invalid data, not enough state change pairs in initial state");
6160     } else if (F.isModule()) {
6161       // For an explicit module, preserve the flags from the module build
6162       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6163       // -Wblah flags.
6164       unsigned Flags = Record[Idx++];
6165       DiagState Initial;
6166       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6167       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6168       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6169       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6170       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6171       Initial.ExtBehavior = (diag::Severity)Flags;
6172       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6173 
6174       assert(F.OriginalSourceFileID.isValid());
6175 
6176       // Set up the root buffer of the module to start with the initial
6177       // diagnostic state of the module itself, to cover files that contain no
6178       // explicit transitions (for which we did not serialize anything).
6179       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6180           .StateTransitions.push_back({FirstState, 0});
6181     } else {
6182       // For prefix ASTs, start with whatever the user configured on the
6183       // command line.
6184       Idx++; // Skip flags.
6185       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6186                                  SourceLocation(), false);
6187     }
6188 
6189     // Read the state transitions.
6190     unsigned NumLocations = Record[Idx++];
6191     while (NumLocations--) {
6192       assert(Idx < Record.size() &&
6193              "Invalid data, missing pragma diagnostic states");
6194       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6195       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6196       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6197       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6198       unsigned Transitions = Record[Idx++];
6199 
6200       // Note that we don't need to set up Parent/ParentOffset here, because
6201       // we won't be changing the diagnostic state within imported FileIDs
6202       // (other than perhaps appending to the main source file, which has no
6203       // parent).
6204       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6205       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6206       for (unsigned I = 0; I != Transitions; ++I) {
6207         unsigned Offset = Record[Idx++];
6208         auto *State =
6209             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6210         F.StateTransitions.push_back({State, Offset});
6211       }
6212     }
6213 
6214     // Read the final state.
6215     assert(Idx < Record.size() &&
6216            "Invalid data, missing final pragma diagnostic state");
6217     SourceLocation CurStateLoc =
6218         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6219     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6220 
6221     if (!F.isModule()) {
6222       Diag.DiagStatesByLoc.CurDiagState = CurState;
6223       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6224 
6225       // Preserve the property that the imaginary root file describes the
6226       // current state.
6227       FileID NullFile;
6228       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6229       if (T.empty())
6230         T.push_back({CurState, 0});
6231       else
6232         T[0].State = CurState;
6233     }
6234 
6235     // Don't try to read these mappings again.
6236     Record.clear();
6237   }
6238 }
6239 
6240 /// Get the correct cursor and offset for loading a type.
6241 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6242   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6243   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6244   ModuleFile *M = I->second;
6245   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6246 }
6247 
6248 /// Read and return the type with the given index..
6249 ///
6250 /// The index is the type ID, shifted and minus the number of predefs. This
6251 /// routine actually reads the record corresponding to the type at the given
6252 /// location. It is a helper routine for GetType, which deals with reading type
6253 /// IDs.
6254 QualType ASTReader::readTypeRecord(unsigned Index) {
6255   assert(ContextObj && "reading type with no AST context");
6256   ASTContext &Context = *ContextObj;
6257   RecordLocation Loc = TypeCursorForIndex(Index);
6258   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6259 
6260   // Keep track of where we are in the stream, then jump back there
6261   // after reading this type.
6262   SavedStreamPosition SavedPosition(DeclsCursor);
6263 
6264   ReadingKindTracker ReadingKind(Read_Type, *this);
6265 
6266   // Note that we are loading a type record.
6267   Deserializing AType(this);
6268 
6269   unsigned Idx = 0;
6270   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6271     Error(std::move(Err));
6272     return QualType();
6273   }
6274   RecordData Record;
6275   Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6276   if (!MaybeCode) {
6277     Error(MaybeCode.takeError());
6278     return QualType();
6279   }
6280   unsigned Code = MaybeCode.get();
6281 
6282   Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6283   if (!MaybeTypeCode) {
6284     Error(MaybeTypeCode.takeError());
6285     return QualType();
6286   }
6287   switch ((TypeCode)MaybeTypeCode.get()) {
6288   case TYPE_EXT_QUAL: {
6289     if (Record.size() != 2) {
6290       Error("Incorrect encoding of extended qualifier type");
6291       return QualType();
6292     }
6293     QualType Base = readType(*Loc.F, Record, Idx);
6294     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6295     return Context.getQualifiedType(Base, Quals);
6296   }
6297 
6298   case TYPE_COMPLEX: {
6299     if (Record.size() != 1) {
6300       Error("Incorrect encoding of complex type");
6301       return QualType();
6302     }
6303     QualType ElemType = readType(*Loc.F, Record, Idx);
6304     return Context.getComplexType(ElemType);
6305   }
6306 
6307   case TYPE_POINTER: {
6308     if (Record.size() != 1) {
6309       Error("Incorrect encoding of pointer type");
6310       return QualType();
6311     }
6312     QualType PointeeType = readType(*Loc.F, Record, Idx);
6313     return Context.getPointerType(PointeeType);
6314   }
6315 
6316   case TYPE_DECAYED: {
6317     if (Record.size() != 1) {
6318       Error("Incorrect encoding of decayed type");
6319       return QualType();
6320     }
6321     QualType OriginalType = readType(*Loc.F, Record, Idx);
6322     QualType DT = Context.getAdjustedParameterType(OriginalType);
6323     if (!isa<DecayedType>(DT))
6324       Error("Decayed type does not decay");
6325     return DT;
6326   }
6327 
6328   case TYPE_ADJUSTED: {
6329     if (Record.size() != 2) {
6330       Error("Incorrect encoding of adjusted type");
6331       return QualType();
6332     }
6333     QualType OriginalTy = readType(*Loc.F, Record, Idx);
6334     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6335     return Context.getAdjustedType(OriginalTy, AdjustedTy);
6336   }
6337 
6338   case TYPE_BLOCK_POINTER: {
6339     if (Record.size() != 1) {
6340       Error("Incorrect encoding of block pointer type");
6341       return QualType();
6342     }
6343     QualType PointeeType = readType(*Loc.F, Record, Idx);
6344     return Context.getBlockPointerType(PointeeType);
6345   }
6346 
6347   case TYPE_LVALUE_REFERENCE: {
6348     if (Record.size() != 2) {
6349       Error("Incorrect encoding of lvalue reference type");
6350       return QualType();
6351     }
6352     QualType PointeeType = readType(*Loc.F, Record, Idx);
6353     return Context.getLValueReferenceType(PointeeType, Record[1]);
6354   }
6355 
6356   case TYPE_RVALUE_REFERENCE: {
6357     if (Record.size() != 1) {
6358       Error("Incorrect encoding of rvalue reference type");
6359       return QualType();
6360     }
6361     QualType PointeeType = readType(*Loc.F, Record, Idx);
6362     return Context.getRValueReferenceType(PointeeType);
6363   }
6364 
6365   case TYPE_MEMBER_POINTER: {
6366     if (Record.size() != 2) {
6367       Error("Incorrect encoding of member pointer type");
6368       return QualType();
6369     }
6370     QualType PointeeType = readType(*Loc.F, Record, Idx);
6371     QualType ClassType = readType(*Loc.F, Record, Idx);
6372     if (PointeeType.isNull() || ClassType.isNull())
6373       return QualType();
6374 
6375     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6376   }
6377 
6378   case TYPE_CONSTANT_ARRAY: {
6379     QualType ElementType = readType(*Loc.F, Record, Idx);
6380     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6381     unsigned IndexTypeQuals = Record[2];
6382     unsigned Idx = 3;
6383     llvm::APInt Size = ReadAPInt(Record, Idx);
6384     return Context.getConstantArrayType(ElementType, Size,
6385                                          ASM, IndexTypeQuals);
6386   }
6387 
6388   case TYPE_INCOMPLETE_ARRAY: {
6389     QualType ElementType = readType(*Loc.F, Record, Idx);
6390     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6391     unsigned IndexTypeQuals = Record[2];
6392     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6393   }
6394 
6395   case TYPE_VARIABLE_ARRAY: {
6396     QualType ElementType = readType(*Loc.F, Record, Idx);
6397     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6398     unsigned IndexTypeQuals = Record[2];
6399     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6400     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6401     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6402                                          ASM, IndexTypeQuals,
6403                                          SourceRange(LBLoc, RBLoc));
6404   }
6405 
6406   case TYPE_VECTOR: {
6407     if (Record.size() != 3) {
6408       Error("incorrect encoding of vector type in AST file");
6409       return QualType();
6410     }
6411 
6412     QualType ElementType = readType(*Loc.F, Record, Idx);
6413     unsigned NumElements = Record[1];
6414     unsigned VecKind = Record[2];
6415     return Context.getVectorType(ElementType, NumElements,
6416                                   (VectorType::VectorKind)VecKind);
6417   }
6418 
6419   case TYPE_EXT_VECTOR: {
6420     if (Record.size() != 3) {
6421       Error("incorrect encoding of extended vector type in AST file");
6422       return QualType();
6423     }
6424 
6425     QualType ElementType = readType(*Loc.F, Record, Idx);
6426     unsigned NumElements = Record[1];
6427     return Context.getExtVectorType(ElementType, NumElements);
6428   }
6429 
6430   case TYPE_FUNCTION_NO_PROTO: {
6431     if (Record.size() != 8) {
6432       Error("incorrect encoding of no-proto function type");
6433       return QualType();
6434     }
6435     QualType ResultType = readType(*Loc.F, Record, Idx);
6436     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6437                                (CallingConv)Record[4], Record[5], Record[6],
6438                                Record[7]);
6439     return Context.getFunctionNoProtoType(ResultType, Info);
6440   }
6441 
6442   case TYPE_FUNCTION_PROTO: {
6443     QualType ResultType = readType(*Loc.F, Record, Idx);
6444 
6445     FunctionProtoType::ExtProtoInfo EPI;
6446     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6447                                         /*hasregparm*/ Record[2],
6448                                         /*regparm*/ Record[3],
6449                                         static_cast<CallingConv>(Record[4]),
6450                                         /*produces*/ Record[5],
6451                                         /*nocallersavedregs*/ Record[6],
6452                                         /*nocfcheck*/ Record[7]);
6453 
6454     unsigned Idx = 8;
6455 
6456     EPI.Variadic = Record[Idx++];
6457     EPI.HasTrailingReturn = Record[Idx++];
6458     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6459     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6460     SmallVector<QualType, 8> ExceptionStorage;
6461     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6462 
6463     unsigned NumParams = Record[Idx++];
6464     SmallVector<QualType, 16> ParamTypes;
6465     for (unsigned I = 0; I != NumParams; ++I)
6466       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6467 
6468     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6469     if (Idx != Record.size()) {
6470       for (unsigned I = 0; I != NumParams; ++I)
6471         ExtParameterInfos.push_back(
6472           FunctionProtoType::ExtParameterInfo
6473                            ::getFromOpaqueValue(Record[Idx++]));
6474       EPI.ExtParameterInfos = ExtParameterInfos.data();
6475     }
6476 
6477     assert(Idx == Record.size());
6478 
6479     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6480   }
6481 
6482   case TYPE_UNRESOLVED_USING: {
6483     unsigned Idx = 0;
6484     return Context.getTypeDeclType(
6485                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6486   }
6487 
6488   case TYPE_TYPEDEF: {
6489     if (Record.size() != 2) {
6490       Error("incorrect encoding of typedef type");
6491       return QualType();
6492     }
6493     unsigned Idx = 0;
6494     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6495     QualType Canonical = readType(*Loc.F, Record, Idx);
6496     if (!Canonical.isNull())
6497       Canonical = Context.getCanonicalType(Canonical);
6498     return Context.getTypedefType(Decl, Canonical);
6499   }
6500 
6501   case TYPE_TYPEOF_EXPR:
6502     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6503 
6504   case TYPE_TYPEOF: {
6505     if (Record.size() != 1) {
6506       Error("incorrect encoding of typeof(type) in AST file");
6507       return QualType();
6508     }
6509     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6510     return Context.getTypeOfType(UnderlyingType);
6511   }
6512 
6513   case TYPE_DECLTYPE: {
6514     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6515     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6516   }
6517 
6518   case TYPE_UNARY_TRANSFORM: {
6519     QualType BaseType = readType(*Loc.F, Record, Idx);
6520     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6521     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6522     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6523   }
6524 
6525   case TYPE_AUTO: {
6526     QualType Deduced = readType(*Loc.F, Record, Idx);
6527     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6528     bool IsDependent = false, IsPack = false;
6529     if (Deduced.isNull()) {
6530       IsDependent = Record[Idx] > 0;
6531       IsPack = Record[Idx] > 1;
6532       ++Idx;
6533     }
6534     return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6535   }
6536 
6537   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6538     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6539     QualType Deduced = readType(*Loc.F, Record, Idx);
6540     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6541     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6542                                                         IsDependent);
6543   }
6544 
6545   case TYPE_RECORD: {
6546     if (Record.size() != 2) {
6547       Error("incorrect encoding of record type");
6548       return QualType();
6549     }
6550     unsigned Idx = 0;
6551     bool IsDependent = Record[Idx++];
6552     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6553     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6554     QualType T = Context.getRecordType(RD);
6555     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6556     return T;
6557   }
6558 
6559   case TYPE_ENUM: {
6560     if (Record.size() != 2) {
6561       Error("incorrect encoding of enum type");
6562       return QualType();
6563     }
6564     unsigned Idx = 0;
6565     bool IsDependent = Record[Idx++];
6566     QualType T
6567       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6568     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6569     return T;
6570   }
6571 
6572   case TYPE_ATTRIBUTED: {
6573     if (Record.size() != 3) {
6574       Error("incorrect encoding of attributed type");
6575       return QualType();
6576     }
6577     QualType modifiedType = readType(*Loc.F, Record, Idx);
6578     QualType equivalentType = readType(*Loc.F, Record, Idx);
6579     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6580     return Context.getAttributedType(kind, modifiedType, equivalentType);
6581   }
6582 
6583   case TYPE_PAREN: {
6584     if (Record.size() != 1) {
6585       Error("incorrect encoding of paren type");
6586       return QualType();
6587     }
6588     QualType InnerType = readType(*Loc.F, Record, Idx);
6589     return Context.getParenType(InnerType);
6590   }
6591 
6592   case TYPE_MACRO_QUALIFIED: {
6593     if (Record.size() != 2) {
6594       Error("incorrect encoding of macro defined type");
6595       return QualType();
6596     }
6597     QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6598     IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6599     return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6600   }
6601 
6602   case TYPE_PACK_EXPANSION: {
6603     if (Record.size() != 2) {
6604       Error("incorrect encoding of pack expansion type");
6605       return QualType();
6606     }
6607     QualType Pattern = readType(*Loc.F, Record, Idx);
6608     if (Pattern.isNull())
6609       return QualType();
6610     Optional<unsigned> NumExpansions;
6611     if (Record[1])
6612       NumExpansions = Record[1] - 1;
6613     return Context.getPackExpansionType(Pattern, NumExpansions);
6614   }
6615 
6616   case TYPE_ELABORATED: {
6617     unsigned Idx = 0;
6618     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6619     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6620     QualType NamedType = readType(*Loc.F, Record, Idx);
6621     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6622     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6623   }
6624 
6625   case TYPE_OBJC_INTERFACE: {
6626     unsigned Idx = 0;
6627     ObjCInterfaceDecl *ItfD
6628       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6629     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6630   }
6631 
6632   case TYPE_OBJC_TYPE_PARAM: {
6633     unsigned Idx = 0;
6634     ObjCTypeParamDecl *Decl
6635       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6636     unsigned NumProtos = Record[Idx++];
6637     SmallVector<ObjCProtocolDecl*, 4> Protos;
6638     for (unsigned I = 0; I != NumProtos; ++I)
6639       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6640     return Context.getObjCTypeParamType(Decl, Protos);
6641   }
6642 
6643   case TYPE_OBJC_OBJECT: {
6644     unsigned Idx = 0;
6645     QualType Base = readType(*Loc.F, Record, Idx);
6646     unsigned NumTypeArgs = Record[Idx++];
6647     SmallVector<QualType, 4> TypeArgs;
6648     for (unsigned I = 0; I != NumTypeArgs; ++I)
6649       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6650     unsigned NumProtos = Record[Idx++];
6651     SmallVector<ObjCProtocolDecl*, 4> Protos;
6652     for (unsigned I = 0; I != NumProtos; ++I)
6653       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6654     bool IsKindOf = Record[Idx++];
6655     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6656   }
6657 
6658   case TYPE_OBJC_OBJECT_POINTER: {
6659     unsigned Idx = 0;
6660     QualType Pointee = readType(*Loc.F, Record, Idx);
6661     return Context.getObjCObjectPointerType(Pointee);
6662   }
6663 
6664   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6665     unsigned Idx = 0;
6666     QualType Parm = readType(*Loc.F, Record, Idx);
6667     QualType Replacement = readType(*Loc.F, Record, Idx);
6668     return Context.getSubstTemplateTypeParmType(
6669         cast<TemplateTypeParmType>(Parm),
6670         Context.getCanonicalType(Replacement));
6671   }
6672 
6673   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6674     unsigned Idx = 0;
6675     QualType Parm = readType(*Loc.F, Record, Idx);
6676     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6677     return Context.getSubstTemplateTypeParmPackType(
6678                                                cast<TemplateTypeParmType>(Parm),
6679                                                      ArgPack);
6680   }
6681 
6682   case TYPE_INJECTED_CLASS_NAME: {
6683     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6684     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6685     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6686     // for AST reading, too much interdependencies.
6687     const Type *T = nullptr;
6688     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6689       if (const Type *Existing = DI->getTypeForDecl()) {
6690         T = Existing;
6691         break;
6692       }
6693     }
6694     if (!T) {
6695       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6696       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6697         DI->setTypeForDecl(T);
6698     }
6699     return QualType(T, 0);
6700   }
6701 
6702   case TYPE_TEMPLATE_TYPE_PARM: {
6703     unsigned Idx = 0;
6704     unsigned Depth = Record[Idx++];
6705     unsigned Index = Record[Idx++];
6706     bool Pack = Record[Idx++];
6707     TemplateTypeParmDecl *D
6708       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6709     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6710   }
6711 
6712   case TYPE_DEPENDENT_NAME: {
6713     unsigned Idx = 0;
6714     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6715     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6716     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6717     QualType Canon = readType(*Loc.F, Record, Idx);
6718     if (!Canon.isNull())
6719       Canon = Context.getCanonicalType(Canon);
6720     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6721   }
6722 
6723   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6724     unsigned Idx = 0;
6725     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6726     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6727     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6728     unsigned NumArgs = Record[Idx++];
6729     SmallVector<TemplateArgument, 8> Args;
6730     Args.reserve(NumArgs);
6731     while (NumArgs--)
6732       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6733     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6734                                                           Args);
6735   }
6736 
6737   case TYPE_DEPENDENT_SIZED_ARRAY: {
6738     unsigned Idx = 0;
6739 
6740     // ArrayType
6741     QualType ElementType = readType(*Loc.F, Record, Idx);
6742     ArrayType::ArraySizeModifier ASM
6743       = (ArrayType::ArraySizeModifier)Record[Idx++];
6744     unsigned IndexTypeQuals = Record[Idx++];
6745 
6746     // DependentSizedArrayType
6747     Expr *NumElts = ReadExpr(*Loc.F);
6748     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6749 
6750     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6751                                                IndexTypeQuals, Brackets);
6752   }
6753 
6754   case TYPE_TEMPLATE_SPECIALIZATION: {
6755     unsigned Idx = 0;
6756     bool IsDependent = Record[Idx++];
6757     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6758     SmallVector<TemplateArgument, 8> Args;
6759     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6760     QualType Underlying = readType(*Loc.F, Record, Idx);
6761     QualType T;
6762     if (Underlying.isNull())
6763       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6764     else
6765       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6766     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6767     return T;
6768   }
6769 
6770   case TYPE_ATOMIC: {
6771     if (Record.size() != 1) {
6772       Error("Incorrect encoding of atomic type");
6773       return QualType();
6774     }
6775     QualType ValueType = readType(*Loc.F, Record, Idx);
6776     return Context.getAtomicType(ValueType);
6777   }
6778 
6779   case TYPE_PIPE: {
6780     if (Record.size() != 2) {
6781       Error("Incorrect encoding of pipe type");
6782       return QualType();
6783     }
6784 
6785     // Reading the pipe element type.
6786     QualType ElementType = readType(*Loc.F, Record, Idx);
6787     unsigned ReadOnly = Record[1];
6788     return Context.getPipeType(ElementType, ReadOnly);
6789   }
6790 
6791   case TYPE_DEPENDENT_SIZED_VECTOR: {
6792     unsigned Idx = 0;
6793     QualType ElementType = readType(*Loc.F, Record, Idx);
6794     Expr *SizeExpr = ReadExpr(*Loc.F);
6795     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6796     unsigned VecKind = Record[Idx];
6797 
6798     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6799                                                (VectorType::VectorKind)VecKind);
6800   }
6801 
6802   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6803     unsigned Idx = 0;
6804 
6805     // DependentSizedExtVectorType
6806     QualType ElementType = readType(*Loc.F, Record, Idx);
6807     Expr *SizeExpr = ReadExpr(*Loc.F);
6808     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6809 
6810     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6811                                                   AttrLoc);
6812   }
6813 
6814   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6815     unsigned Idx = 0;
6816 
6817     // DependentAddressSpaceType
6818     QualType PointeeType = readType(*Loc.F, Record, Idx);
6819     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6820     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6821 
6822     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6823                                                    AttrLoc);
6824   }
6825   }
6826   llvm_unreachable("Invalid TypeCode!");
6827 }
6828 
6829 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6830                                   SmallVectorImpl<QualType> &Exceptions,
6831                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6832                                   const RecordData &Record, unsigned &Idx) {
6833   ExceptionSpecificationType EST =
6834       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6835   ESI.Type = EST;
6836   if (EST == EST_Dynamic) {
6837     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6838       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6839     ESI.Exceptions = Exceptions;
6840   } else if (isComputedNoexcept(EST)) {
6841     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6842   } else if (EST == EST_Uninstantiated) {
6843     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6844     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6845   } else if (EST == EST_Unevaluated) {
6846     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6847   }
6848 }
6849 
6850 namespace clang {
6851 
6852 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6853   ModuleFile *F;
6854   ASTReader *Reader;
6855   const ASTReader::RecordData &Record;
6856   unsigned &Idx;
6857 
6858   SourceLocation ReadSourceLocation() {
6859     return Reader->ReadSourceLocation(*F, Record, Idx);
6860   }
6861 
6862   TypeSourceInfo *GetTypeSourceInfo() {
6863     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6864   }
6865 
6866   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6867     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6868   }
6869 
6870   Attr *ReadAttr() {
6871     return Reader->ReadAttr(*F, Record, Idx);
6872   }
6873 
6874 public:
6875   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6876                 const ASTReader::RecordData &Record, unsigned &Idx)
6877       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6878 
6879   // We want compile-time assurance that we've enumerated all of
6880   // these, so unfortunately we have to declare them first, then
6881   // define them out-of-line.
6882 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6883 #define TYPELOC(CLASS, PARENT) \
6884   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6885 #include "clang/AST/TypeLocNodes.def"
6886 
6887   void VisitFunctionTypeLoc(FunctionTypeLoc);
6888   void VisitArrayTypeLoc(ArrayTypeLoc);
6889 };
6890 
6891 } // namespace clang
6892 
6893 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6894   // nothing to do
6895 }
6896 
6897 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6898   TL.setBuiltinLoc(ReadSourceLocation());
6899   if (TL.needsExtraLocalData()) {
6900     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6901     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6902     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6903     TL.setModeAttr(Record[Idx++]);
6904   }
6905 }
6906 
6907 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6908   TL.setNameLoc(ReadSourceLocation());
6909 }
6910 
6911 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6912   TL.setStarLoc(ReadSourceLocation());
6913 }
6914 
6915 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6916   // nothing to do
6917 }
6918 
6919 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6920   // nothing to do
6921 }
6922 
6923 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6924   TL.setExpansionLoc(ReadSourceLocation());
6925 }
6926 
6927 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6928   TL.setCaretLoc(ReadSourceLocation());
6929 }
6930 
6931 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6932   TL.setAmpLoc(ReadSourceLocation());
6933 }
6934 
6935 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6936   TL.setAmpAmpLoc(ReadSourceLocation());
6937 }
6938 
6939 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6940   TL.setStarLoc(ReadSourceLocation());
6941   TL.setClassTInfo(GetTypeSourceInfo());
6942 }
6943 
6944 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6945   TL.setLBracketLoc(ReadSourceLocation());
6946   TL.setRBracketLoc(ReadSourceLocation());
6947   if (Record[Idx++])
6948     TL.setSizeExpr(Reader->ReadExpr(*F));
6949   else
6950     TL.setSizeExpr(nullptr);
6951 }
6952 
6953 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6954   VisitArrayTypeLoc(TL);
6955 }
6956 
6957 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6958   VisitArrayTypeLoc(TL);
6959 }
6960 
6961 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6962   VisitArrayTypeLoc(TL);
6963 }
6964 
6965 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6966                                             DependentSizedArrayTypeLoc TL) {
6967   VisitArrayTypeLoc(TL);
6968 }
6969 
6970 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6971     DependentAddressSpaceTypeLoc TL) {
6972 
6973     TL.setAttrNameLoc(ReadSourceLocation());
6974     SourceRange range;
6975     range.setBegin(ReadSourceLocation());
6976     range.setEnd(ReadSourceLocation());
6977     TL.setAttrOperandParensRange(range);
6978     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6979 }
6980 
6981 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6982                                         DependentSizedExtVectorTypeLoc TL) {
6983   TL.setNameLoc(ReadSourceLocation());
6984 }
6985 
6986 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6987   TL.setNameLoc(ReadSourceLocation());
6988 }
6989 
6990 void TypeLocReader::VisitDependentVectorTypeLoc(
6991     DependentVectorTypeLoc TL) {
6992   TL.setNameLoc(ReadSourceLocation());
6993 }
6994 
6995 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6996   TL.setNameLoc(ReadSourceLocation());
6997 }
6998 
6999 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7000   TL.setLocalRangeBegin(ReadSourceLocation());
7001   TL.setLParenLoc(ReadSourceLocation());
7002   TL.setRParenLoc(ReadSourceLocation());
7003   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
7004                                        Reader->ReadSourceLocation(*F, Record, Idx)));
7005   TL.setLocalRangeEnd(ReadSourceLocation());
7006   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7007     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7008   }
7009 }
7010 
7011 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7012   VisitFunctionTypeLoc(TL);
7013 }
7014 
7015 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7016   VisitFunctionTypeLoc(TL);
7017 }
7018 
7019 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7020   TL.setNameLoc(ReadSourceLocation());
7021 }
7022 
7023 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7024   TL.setNameLoc(ReadSourceLocation());
7025 }
7026 
7027 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7028   TL.setTypeofLoc(ReadSourceLocation());
7029   TL.setLParenLoc(ReadSourceLocation());
7030   TL.setRParenLoc(ReadSourceLocation());
7031 }
7032 
7033 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7034   TL.setTypeofLoc(ReadSourceLocation());
7035   TL.setLParenLoc(ReadSourceLocation());
7036   TL.setRParenLoc(ReadSourceLocation());
7037   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7038 }
7039 
7040 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7041   TL.setNameLoc(ReadSourceLocation());
7042 }
7043 
7044 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7045   TL.setKWLoc(ReadSourceLocation());
7046   TL.setLParenLoc(ReadSourceLocation());
7047   TL.setRParenLoc(ReadSourceLocation());
7048   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7049 }
7050 
7051 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7052   TL.setNameLoc(ReadSourceLocation());
7053 }
7054 
7055 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7056     DeducedTemplateSpecializationTypeLoc TL) {
7057   TL.setTemplateNameLoc(ReadSourceLocation());
7058 }
7059 
7060 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7061   TL.setNameLoc(ReadSourceLocation());
7062 }
7063 
7064 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7065   TL.setNameLoc(ReadSourceLocation());
7066 }
7067 
7068 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7069   TL.setAttr(ReadAttr());
7070 }
7071 
7072 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7073   TL.setNameLoc(ReadSourceLocation());
7074 }
7075 
7076 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7077                                             SubstTemplateTypeParmTypeLoc TL) {
7078   TL.setNameLoc(ReadSourceLocation());
7079 }
7080 
7081 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7082                                           SubstTemplateTypeParmPackTypeLoc TL) {
7083   TL.setNameLoc(ReadSourceLocation());
7084 }
7085 
7086 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7087                                            TemplateSpecializationTypeLoc TL) {
7088   TL.setTemplateKeywordLoc(ReadSourceLocation());
7089   TL.setTemplateNameLoc(ReadSourceLocation());
7090   TL.setLAngleLoc(ReadSourceLocation());
7091   TL.setRAngleLoc(ReadSourceLocation());
7092   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7093     TL.setArgLocInfo(
7094         i,
7095         Reader->GetTemplateArgumentLocInfo(
7096             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7097 }
7098 
7099 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7100   TL.setLParenLoc(ReadSourceLocation());
7101   TL.setRParenLoc(ReadSourceLocation());
7102 }
7103 
7104 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7105   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7106   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7107 }
7108 
7109 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7110   TL.setNameLoc(ReadSourceLocation());
7111 }
7112 
7113 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7114   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7115   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7116   TL.setNameLoc(ReadSourceLocation());
7117 }
7118 
7119 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7120        DependentTemplateSpecializationTypeLoc TL) {
7121   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7122   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7123   TL.setTemplateKeywordLoc(ReadSourceLocation());
7124   TL.setTemplateNameLoc(ReadSourceLocation());
7125   TL.setLAngleLoc(ReadSourceLocation());
7126   TL.setRAngleLoc(ReadSourceLocation());
7127   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7128     TL.setArgLocInfo(
7129         I,
7130         Reader->GetTemplateArgumentLocInfo(
7131             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7132 }
7133 
7134 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7135   TL.setEllipsisLoc(ReadSourceLocation());
7136 }
7137 
7138 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7139   TL.setNameLoc(ReadSourceLocation());
7140 }
7141 
7142 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7143   if (TL.getNumProtocols()) {
7144     TL.setProtocolLAngleLoc(ReadSourceLocation());
7145     TL.setProtocolRAngleLoc(ReadSourceLocation());
7146   }
7147   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7148     TL.setProtocolLoc(i, ReadSourceLocation());
7149 }
7150 
7151 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7152   TL.setHasBaseTypeAsWritten(Record[Idx++]);
7153   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7154   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7155   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7156     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7157   TL.setProtocolLAngleLoc(ReadSourceLocation());
7158   TL.setProtocolRAngleLoc(ReadSourceLocation());
7159   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7160     TL.setProtocolLoc(i, ReadSourceLocation());
7161 }
7162 
7163 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7164   TL.setStarLoc(ReadSourceLocation());
7165 }
7166 
7167 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7168   TL.setKWLoc(ReadSourceLocation());
7169   TL.setLParenLoc(ReadSourceLocation());
7170   TL.setRParenLoc(ReadSourceLocation());
7171 }
7172 
7173 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7174   TL.setKWLoc(ReadSourceLocation());
7175 }
7176 
7177 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7178                             unsigned &Idx, TypeLoc TL) {
7179   TypeLocReader TLR(F, *this, Record, Idx);
7180   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7181     TLR.Visit(TL);
7182 }
7183 
7184 TypeSourceInfo *
7185 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7186                              unsigned &Idx) {
7187   QualType InfoTy = readType(F, Record, Idx);
7188   if (InfoTy.isNull())
7189     return nullptr;
7190 
7191   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7192   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7193   return TInfo;
7194 }
7195 
7196 QualType ASTReader::GetType(TypeID ID) {
7197   assert(ContextObj && "reading type with no AST context");
7198   ASTContext &Context = *ContextObj;
7199 
7200   unsigned FastQuals = ID & Qualifiers::FastMask;
7201   unsigned Index = ID >> Qualifiers::FastWidth;
7202 
7203   if (Index < NUM_PREDEF_TYPE_IDS) {
7204     QualType T;
7205     switch ((PredefinedTypeIDs)Index) {
7206     case PREDEF_TYPE_NULL_ID:
7207       return QualType();
7208     case PREDEF_TYPE_VOID_ID:
7209       T = Context.VoidTy;
7210       break;
7211     case PREDEF_TYPE_BOOL_ID:
7212       T = Context.BoolTy;
7213       break;
7214     case PREDEF_TYPE_CHAR_U_ID:
7215     case PREDEF_TYPE_CHAR_S_ID:
7216       // FIXME: Check that the signedness of CharTy is correct!
7217       T = Context.CharTy;
7218       break;
7219     case PREDEF_TYPE_UCHAR_ID:
7220       T = Context.UnsignedCharTy;
7221       break;
7222     case PREDEF_TYPE_USHORT_ID:
7223       T = Context.UnsignedShortTy;
7224       break;
7225     case PREDEF_TYPE_UINT_ID:
7226       T = Context.UnsignedIntTy;
7227       break;
7228     case PREDEF_TYPE_ULONG_ID:
7229       T = Context.UnsignedLongTy;
7230       break;
7231     case PREDEF_TYPE_ULONGLONG_ID:
7232       T = Context.UnsignedLongLongTy;
7233       break;
7234     case PREDEF_TYPE_UINT128_ID:
7235       T = Context.UnsignedInt128Ty;
7236       break;
7237     case PREDEF_TYPE_SCHAR_ID:
7238       T = Context.SignedCharTy;
7239       break;
7240     case PREDEF_TYPE_WCHAR_ID:
7241       T = Context.WCharTy;
7242       break;
7243     case PREDEF_TYPE_SHORT_ID:
7244       T = Context.ShortTy;
7245       break;
7246     case PREDEF_TYPE_INT_ID:
7247       T = Context.IntTy;
7248       break;
7249     case PREDEF_TYPE_LONG_ID:
7250       T = Context.LongTy;
7251       break;
7252     case PREDEF_TYPE_LONGLONG_ID:
7253       T = Context.LongLongTy;
7254       break;
7255     case PREDEF_TYPE_INT128_ID:
7256       T = Context.Int128Ty;
7257       break;
7258     case PREDEF_TYPE_HALF_ID:
7259       T = Context.HalfTy;
7260       break;
7261     case PREDEF_TYPE_FLOAT_ID:
7262       T = Context.FloatTy;
7263       break;
7264     case PREDEF_TYPE_DOUBLE_ID:
7265       T = Context.DoubleTy;
7266       break;
7267     case PREDEF_TYPE_LONGDOUBLE_ID:
7268       T = Context.LongDoubleTy;
7269       break;
7270     case PREDEF_TYPE_SHORT_ACCUM_ID:
7271       T = Context.ShortAccumTy;
7272       break;
7273     case PREDEF_TYPE_ACCUM_ID:
7274       T = Context.AccumTy;
7275       break;
7276     case PREDEF_TYPE_LONG_ACCUM_ID:
7277       T = Context.LongAccumTy;
7278       break;
7279     case PREDEF_TYPE_USHORT_ACCUM_ID:
7280       T = Context.UnsignedShortAccumTy;
7281       break;
7282     case PREDEF_TYPE_UACCUM_ID:
7283       T = Context.UnsignedAccumTy;
7284       break;
7285     case PREDEF_TYPE_ULONG_ACCUM_ID:
7286       T = Context.UnsignedLongAccumTy;
7287       break;
7288     case PREDEF_TYPE_SHORT_FRACT_ID:
7289       T = Context.ShortFractTy;
7290       break;
7291     case PREDEF_TYPE_FRACT_ID:
7292       T = Context.FractTy;
7293       break;
7294     case PREDEF_TYPE_LONG_FRACT_ID:
7295       T = Context.LongFractTy;
7296       break;
7297     case PREDEF_TYPE_USHORT_FRACT_ID:
7298       T = Context.UnsignedShortFractTy;
7299       break;
7300     case PREDEF_TYPE_UFRACT_ID:
7301       T = Context.UnsignedFractTy;
7302       break;
7303     case PREDEF_TYPE_ULONG_FRACT_ID:
7304       T = Context.UnsignedLongFractTy;
7305       break;
7306     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7307       T = Context.SatShortAccumTy;
7308       break;
7309     case PREDEF_TYPE_SAT_ACCUM_ID:
7310       T = Context.SatAccumTy;
7311       break;
7312     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7313       T = Context.SatLongAccumTy;
7314       break;
7315     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7316       T = Context.SatUnsignedShortAccumTy;
7317       break;
7318     case PREDEF_TYPE_SAT_UACCUM_ID:
7319       T = Context.SatUnsignedAccumTy;
7320       break;
7321     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7322       T = Context.SatUnsignedLongAccumTy;
7323       break;
7324     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7325       T = Context.SatShortFractTy;
7326       break;
7327     case PREDEF_TYPE_SAT_FRACT_ID:
7328       T = Context.SatFractTy;
7329       break;
7330     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7331       T = Context.SatLongFractTy;
7332       break;
7333     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7334       T = Context.SatUnsignedShortFractTy;
7335       break;
7336     case PREDEF_TYPE_SAT_UFRACT_ID:
7337       T = Context.SatUnsignedFractTy;
7338       break;
7339     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7340       T = Context.SatUnsignedLongFractTy;
7341       break;
7342     case PREDEF_TYPE_FLOAT16_ID:
7343       T = Context.Float16Ty;
7344       break;
7345     case PREDEF_TYPE_FLOAT128_ID:
7346       T = Context.Float128Ty;
7347       break;
7348     case PREDEF_TYPE_OVERLOAD_ID:
7349       T = Context.OverloadTy;
7350       break;
7351     case PREDEF_TYPE_BOUND_MEMBER:
7352       T = Context.BoundMemberTy;
7353       break;
7354     case PREDEF_TYPE_PSEUDO_OBJECT:
7355       T = Context.PseudoObjectTy;
7356       break;
7357     case PREDEF_TYPE_DEPENDENT_ID:
7358       T = Context.DependentTy;
7359       break;
7360     case PREDEF_TYPE_UNKNOWN_ANY:
7361       T = Context.UnknownAnyTy;
7362       break;
7363     case PREDEF_TYPE_NULLPTR_ID:
7364       T = Context.NullPtrTy;
7365       break;
7366     case PREDEF_TYPE_CHAR8_ID:
7367       T = Context.Char8Ty;
7368       break;
7369     case PREDEF_TYPE_CHAR16_ID:
7370       T = Context.Char16Ty;
7371       break;
7372     case PREDEF_TYPE_CHAR32_ID:
7373       T = Context.Char32Ty;
7374       break;
7375     case PREDEF_TYPE_OBJC_ID:
7376       T = Context.ObjCBuiltinIdTy;
7377       break;
7378     case PREDEF_TYPE_OBJC_CLASS:
7379       T = Context.ObjCBuiltinClassTy;
7380       break;
7381     case PREDEF_TYPE_OBJC_SEL:
7382       T = Context.ObjCBuiltinSelTy;
7383       break;
7384 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7385     case PREDEF_TYPE_##Id##_ID: \
7386       T = Context.SingletonId; \
7387       break;
7388 #include "clang/Basic/OpenCLImageTypes.def"
7389 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7390     case PREDEF_TYPE_##Id##_ID: \
7391       T = Context.Id##Ty; \
7392       break;
7393 #include "clang/Basic/OpenCLExtensionTypes.def"
7394     case PREDEF_TYPE_SAMPLER_ID:
7395       T = Context.OCLSamplerTy;
7396       break;
7397     case PREDEF_TYPE_EVENT_ID:
7398       T = Context.OCLEventTy;
7399       break;
7400     case PREDEF_TYPE_CLK_EVENT_ID:
7401       T = Context.OCLClkEventTy;
7402       break;
7403     case PREDEF_TYPE_QUEUE_ID:
7404       T = Context.OCLQueueTy;
7405       break;
7406     case PREDEF_TYPE_RESERVE_ID_ID:
7407       T = Context.OCLReserveIDTy;
7408       break;
7409     case PREDEF_TYPE_AUTO_DEDUCT:
7410       T = Context.getAutoDeductType();
7411       break;
7412     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7413       T = Context.getAutoRRefDeductType();
7414       break;
7415     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7416       T = Context.ARCUnbridgedCastTy;
7417       break;
7418     case PREDEF_TYPE_BUILTIN_FN:
7419       T = Context.BuiltinFnTy;
7420       break;
7421     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7422       T = Context.OMPArraySectionTy;
7423       break;
7424 #define SVE_TYPE(Name, Id, SingletonId) \
7425     case PREDEF_TYPE_##Id##_ID: \
7426       T = Context.SingletonId; \
7427       break;
7428 #include "clang/Basic/AArch64SVEACLETypes.def"
7429     }
7430 
7431     assert(!T.isNull() && "Unknown predefined type");
7432     return T.withFastQualifiers(FastQuals);
7433   }
7434 
7435   Index -= NUM_PREDEF_TYPE_IDS;
7436   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7437   if (TypesLoaded[Index].isNull()) {
7438     TypesLoaded[Index] = readTypeRecord(Index);
7439     if (TypesLoaded[Index].isNull())
7440       return QualType();
7441 
7442     TypesLoaded[Index]->setFromAST();
7443     if (DeserializationListener)
7444       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7445                                         TypesLoaded[Index]);
7446   }
7447 
7448   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7449 }
7450 
7451 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7452   return GetType(getGlobalTypeID(F, LocalID));
7453 }
7454 
7455 serialization::TypeID
7456 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7457   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7458   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7459 
7460   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7461     return LocalID;
7462 
7463   if (!F.ModuleOffsetMap.empty())
7464     ReadModuleOffsetMap(F);
7465 
7466   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7467     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7468   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7469 
7470   unsigned GlobalIndex = LocalIndex + I->second;
7471   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7472 }
7473 
7474 TemplateArgumentLocInfo
7475 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7476                                       TemplateArgument::ArgKind Kind,
7477                                       const RecordData &Record,
7478                                       unsigned &Index) {
7479   switch (Kind) {
7480   case TemplateArgument::Expression:
7481     return ReadExpr(F);
7482   case TemplateArgument::Type:
7483     return GetTypeSourceInfo(F, Record, Index);
7484   case TemplateArgument::Template: {
7485     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7486                                                                      Index);
7487     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7488     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7489                                    SourceLocation());
7490   }
7491   case TemplateArgument::TemplateExpansion: {
7492     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7493                                                                      Index);
7494     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7495     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7496     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7497                                    EllipsisLoc);
7498   }
7499   case TemplateArgument::Null:
7500   case TemplateArgument::Integral:
7501   case TemplateArgument::Declaration:
7502   case TemplateArgument::NullPtr:
7503   case TemplateArgument::Pack:
7504     // FIXME: Is this right?
7505     return TemplateArgumentLocInfo();
7506   }
7507   llvm_unreachable("unexpected template argument loc");
7508 }
7509 
7510 TemplateArgumentLoc
7511 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7512                                    const RecordData &Record, unsigned &Index) {
7513   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7514 
7515   if (Arg.getKind() == TemplateArgument::Expression) {
7516     if (Record[Index++]) // bool InfoHasSameExpr.
7517       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7518   }
7519   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7520                                                              Record, Index));
7521 }
7522 
7523 const ASTTemplateArgumentListInfo*
7524 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7525                                            const RecordData &Record,
7526                                            unsigned &Index) {
7527   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7528   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7529   unsigned NumArgsAsWritten = Record[Index++];
7530   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7531   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7532     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7533   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7534 }
7535 
7536 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7537   return GetDecl(ID);
7538 }
7539 
7540 void ASTReader::CompleteRedeclChain(const Decl *D) {
7541   if (NumCurrentElementsDeserializing) {
7542     // We arrange to not care about the complete redeclaration chain while we're
7543     // deserializing. Just remember that the AST has marked this one as complete
7544     // but that it's not actually complete yet, so we know we still need to
7545     // complete it later.
7546     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7547     return;
7548   }
7549 
7550   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7551 
7552   // If this is a named declaration, complete it by looking it up
7553   // within its context.
7554   //
7555   // FIXME: Merging a function definition should merge
7556   // all mergeable entities within it.
7557   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7558       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7559     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7560       if (!getContext().getLangOpts().CPlusPlus &&
7561           isa<TranslationUnitDecl>(DC)) {
7562         // Outside of C++, we don't have a lookup table for the TU, so update
7563         // the identifier instead. (For C++ modules, we don't store decls
7564         // in the serialized identifier table, so we do the lookup in the TU.)
7565         auto *II = Name.getAsIdentifierInfo();
7566         assert(II && "non-identifier name in C?");
7567         if (II->isOutOfDate())
7568           updateOutOfDateIdentifier(*II);
7569       } else
7570         DC->lookup(Name);
7571     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7572       // Find all declarations of this kind from the relevant context.
7573       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7574         auto *DC = cast<DeclContext>(DCDecl);
7575         SmallVector<Decl*, 8> Decls;
7576         FindExternalLexicalDecls(
7577             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7578       }
7579     }
7580   }
7581 
7582   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7583     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7584   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7585     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7586   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7587     if (auto *Template = FD->getPrimaryTemplate())
7588       Template->LoadLazySpecializations();
7589   }
7590 }
7591 
7592 CXXCtorInitializer **
7593 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7594   RecordLocation Loc = getLocalBitOffset(Offset);
7595   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7596   SavedStreamPosition SavedPosition(Cursor);
7597   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7598     Error(std::move(Err));
7599     return nullptr;
7600   }
7601   ReadingKindTracker ReadingKind(Read_Decl, *this);
7602 
7603   RecordData Record;
7604   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7605   if (!MaybeCode) {
7606     Error(MaybeCode.takeError());
7607     return nullptr;
7608   }
7609   unsigned Code = MaybeCode.get();
7610 
7611   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7612   if (!MaybeRecCode) {
7613     Error(MaybeRecCode.takeError());
7614     return nullptr;
7615   }
7616   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7617     Error("malformed AST file: missing C++ ctor initializers");
7618     return nullptr;
7619   }
7620 
7621   unsigned Idx = 0;
7622   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7623 }
7624 
7625 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7626   assert(ContextObj && "reading base specifiers with no AST context");
7627   ASTContext &Context = *ContextObj;
7628 
7629   RecordLocation Loc = getLocalBitOffset(Offset);
7630   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7631   SavedStreamPosition SavedPosition(Cursor);
7632   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7633     Error(std::move(Err));
7634     return nullptr;
7635   }
7636   ReadingKindTracker ReadingKind(Read_Decl, *this);
7637   RecordData Record;
7638 
7639   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7640   if (!MaybeCode) {
7641     Error(MaybeCode.takeError());
7642     return nullptr;
7643   }
7644   unsigned Code = MaybeCode.get();
7645 
7646   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7647   if (!MaybeRecCode) {
7648     Error(MaybeCode.takeError());
7649     return nullptr;
7650   }
7651   unsigned RecCode = MaybeRecCode.get();
7652 
7653   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7654     Error("malformed AST file: missing C++ base specifiers");
7655     return nullptr;
7656   }
7657 
7658   unsigned Idx = 0;
7659   unsigned NumBases = Record[Idx++];
7660   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7661   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7662   for (unsigned I = 0; I != NumBases; ++I)
7663     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7664   return Bases;
7665 }
7666 
7667 serialization::DeclID
7668 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7669   if (LocalID < NUM_PREDEF_DECL_IDS)
7670     return LocalID;
7671 
7672   if (!F.ModuleOffsetMap.empty())
7673     ReadModuleOffsetMap(F);
7674 
7675   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7676     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7677   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7678 
7679   return LocalID + I->second;
7680 }
7681 
7682 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7683                                    ModuleFile &M) const {
7684   // Predefined decls aren't from any module.
7685   if (ID < NUM_PREDEF_DECL_IDS)
7686     return false;
7687 
7688   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7689          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7690 }
7691 
7692 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7693   if (!D->isFromASTFile())
7694     return nullptr;
7695   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7696   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7697   return I->second;
7698 }
7699 
7700 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7701   if (ID < NUM_PREDEF_DECL_IDS)
7702     return SourceLocation();
7703 
7704   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7705 
7706   if (Index > DeclsLoaded.size()) {
7707     Error("declaration ID out-of-range for AST file");
7708     return SourceLocation();
7709   }
7710 
7711   if (Decl *D = DeclsLoaded[Index])
7712     return D->getLocation();
7713 
7714   SourceLocation Loc;
7715   DeclCursorForID(ID, Loc);
7716   return Loc;
7717 }
7718 
7719 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7720   switch (ID) {
7721   case PREDEF_DECL_NULL_ID:
7722     return nullptr;
7723 
7724   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7725     return Context.getTranslationUnitDecl();
7726 
7727   case PREDEF_DECL_OBJC_ID_ID:
7728     return Context.getObjCIdDecl();
7729 
7730   case PREDEF_DECL_OBJC_SEL_ID:
7731     return Context.getObjCSelDecl();
7732 
7733   case PREDEF_DECL_OBJC_CLASS_ID:
7734     return Context.getObjCClassDecl();
7735 
7736   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7737     return Context.getObjCProtocolDecl();
7738 
7739   case PREDEF_DECL_INT_128_ID:
7740     return Context.getInt128Decl();
7741 
7742   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7743     return Context.getUInt128Decl();
7744 
7745   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7746     return Context.getObjCInstanceTypeDecl();
7747 
7748   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7749     return Context.getBuiltinVaListDecl();
7750 
7751   case PREDEF_DECL_VA_LIST_TAG:
7752     return Context.getVaListTagDecl();
7753 
7754   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7755     return Context.getBuiltinMSVaListDecl();
7756 
7757   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7758     return Context.getExternCContextDecl();
7759 
7760   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7761     return Context.getMakeIntegerSeqDecl();
7762 
7763   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7764     return Context.getCFConstantStringDecl();
7765 
7766   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7767     return Context.getCFConstantStringTagDecl();
7768 
7769   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7770     return Context.getTypePackElementDecl();
7771   }
7772   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7773 }
7774 
7775 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7776   assert(ContextObj && "reading decl with no AST context");
7777   if (ID < NUM_PREDEF_DECL_IDS) {
7778     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7779     if (D) {
7780       // Track that we have merged the declaration with ID \p ID into the
7781       // pre-existing predefined declaration \p D.
7782       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7783       if (Merged.empty())
7784         Merged.push_back(ID);
7785     }
7786     return D;
7787   }
7788 
7789   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7790 
7791   if (Index >= DeclsLoaded.size()) {
7792     assert(0 && "declaration ID out-of-range for AST file");
7793     Error("declaration ID out-of-range for AST file");
7794     return nullptr;
7795   }
7796 
7797   return DeclsLoaded[Index];
7798 }
7799 
7800 Decl *ASTReader::GetDecl(DeclID ID) {
7801   if (ID < NUM_PREDEF_DECL_IDS)
7802     return GetExistingDecl(ID);
7803 
7804   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7805 
7806   if (Index >= DeclsLoaded.size()) {
7807     assert(0 && "declaration ID out-of-range for AST file");
7808     Error("declaration ID out-of-range for AST file");
7809     return nullptr;
7810   }
7811 
7812   if (!DeclsLoaded[Index]) {
7813     ReadDeclRecord(ID);
7814     if (DeserializationListener)
7815       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7816   }
7817 
7818   return DeclsLoaded[Index];
7819 }
7820 
7821 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7822                                                   DeclID GlobalID) {
7823   if (GlobalID < NUM_PREDEF_DECL_IDS)
7824     return GlobalID;
7825 
7826   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7827   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7828   ModuleFile *Owner = I->second;
7829 
7830   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7831     = M.GlobalToLocalDeclIDs.find(Owner);
7832   if (Pos == M.GlobalToLocalDeclIDs.end())
7833     return 0;
7834 
7835   return GlobalID - Owner->BaseDeclID + Pos->second;
7836 }
7837 
7838 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7839                                             const RecordData &Record,
7840                                             unsigned &Idx) {
7841   if (Idx >= Record.size()) {
7842     Error("Corrupted AST file");
7843     return 0;
7844   }
7845 
7846   return getGlobalDeclID(F, Record[Idx++]);
7847 }
7848 
7849 /// Resolve the offset of a statement into a statement.
7850 ///
7851 /// This operation will read a new statement from the external
7852 /// source each time it is called, and is meant to be used via a
7853 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7854 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7855   // Switch case IDs are per Decl.
7856   ClearSwitchCaseIDs();
7857 
7858   // Offset here is a global offset across the entire chain.
7859   RecordLocation Loc = getLocalBitOffset(Offset);
7860   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7861     Error(std::move(Err));
7862     return nullptr;
7863   }
7864   assert(NumCurrentElementsDeserializing == 0 &&
7865          "should not be called while already deserializing");
7866   Deserializing D(this);
7867   return ReadStmtFromStream(*Loc.F);
7868 }
7869 
7870 void ASTReader::FindExternalLexicalDecls(
7871     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7872     SmallVectorImpl<Decl *> &Decls) {
7873   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7874 
7875   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7876     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7877     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7878       auto K = (Decl::Kind)+LexicalDecls[I];
7879       if (!IsKindWeWant(K))
7880         continue;
7881 
7882       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7883 
7884       // Don't add predefined declarations to the lexical context more
7885       // than once.
7886       if (ID < NUM_PREDEF_DECL_IDS) {
7887         if (PredefsVisited[ID])
7888           continue;
7889 
7890         PredefsVisited[ID] = true;
7891       }
7892 
7893       if (Decl *D = GetLocalDecl(*M, ID)) {
7894         assert(D->getKind() == K && "wrong kind for lexical decl");
7895         if (!DC->isDeclInLexicalTraversal(D))
7896           Decls.push_back(D);
7897       }
7898     }
7899   };
7900 
7901   if (isa<TranslationUnitDecl>(DC)) {
7902     for (auto Lexical : TULexicalDecls)
7903       Visit(Lexical.first, Lexical.second);
7904   } else {
7905     auto I = LexicalDecls.find(DC);
7906     if (I != LexicalDecls.end())
7907       Visit(I->second.first, I->second.second);
7908   }
7909 
7910   ++NumLexicalDeclContextsRead;
7911 }
7912 
7913 namespace {
7914 
7915 class DeclIDComp {
7916   ASTReader &Reader;
7917   ModuleFile &Mod;
7918 
7919 public:
7920   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7921 
7922   bool operator()(LocalDeclID L, LocalDeclID R) const {
7923     SourceLocation LHS = getLocation(L);
7924     SourceLocation RHS = getLocation(R);
7925     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7926   }
7927 
7928   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7929     SourceLocation RHS = getLocation(R);
7930     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7931   }
7932 
7933   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7934     SourceLocation LHS = getLocation(L);
7935     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7936   }
7937 
7938   SourceLocation getLocation(LocalDeclID ID) const {
7939     return Reader.getSourceManager().getFileLoc(
7940             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7941   }
7942 };
7943 
7944 } // namespace
7945 
7946 void ASTReader::FindFileRegionDecls(FileID File,
7947                                     unsigned Offset, unsigned Length,
7948                                     SmallVectorImpl<Decl *> &Decls) {
7949   SourceManager &SM = getSourceManager();
7950 
7951   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7952   if (I == FileDeclIDs.end())
7953     return;
7954 
7955   FileDeclsInfo &DInfo = I->second;
7956   if (DInfo.Decls.empty())
7957     return;
7958 
7959   SourceLocation
7960     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7961   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7962 
7963   DeclIDComp DIDComp(*this, *DInfo.Mod);
7964   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7965       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7966   if (BeginIt != DInfo.Decls.begin())
7967     --BeginIt;
7968 
7969   // If we are pointing at a top-level decl inside an objc container, we need
7970   // to backtrack until we find it otherwise we will fail to report that the
7971   // region overlaps with an objc container.
7972   while (BeginIt != DInfo.Decls.begin() &&
7973          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7974              ->isTopLevelDeclInObjCContainer())
7975     --BeginIt;
7976 
7977   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7978       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7979   if (EndIt != DInfo.Decls.end())
7980     ++EndIt;
7981 
7982   for (ArrayRef<serialization::LocalDeclID>::iterator
7983          DIt = BeginIt; DIt != EndIt; ++DIt)
7984     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7985 }
7986 
7987 bool
7988 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7989                                           DeclarationName Name) {
7990   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7991          "DeclContext has no visible decls in storage");
7992   if (!Name)
7993     return false;
7994 
7995   auto It = Lookups.find(DC);
7996   if (It == Lookups.end())
7997     return false;
7998 
7999   Deserializing LookupResults(this);
8000 
8001   // Load the list of declarations.
8002   SmallVector<NamedDecl *, 64> Decls;
8003   for (DeclID ID : It->second.Table.find(Name)) {
8004     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8005     if (ND->getDeclName() == Name)
8006       Decls.push_back(ND);
8007   }
8008 
8009   ++NumVisibleDeclContextsRead;
8010   SetExternalVisibleDeclsForName(DC, Name, Decls);
8011   return !Decls.empty();
8012 }
8013 
8014 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8015   if (!DC->hasExternalVisibleStorage())
8016     return;
8017 
8018   auto It = Lookups.find(DC);
8019   assert(It != Lookups.end() &&
8020          "have external visible storage but no lookup tables");
8021 
8022   DeclsMap Decls;
8023 
8024   for (DeclID ID : It->second.Table.findAll()) {
8025     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8026     Decls[ND->getDeclName()].push_back(ND);
8027   }
8028 
8029   ++NumVisibleDeclContextsRead;
8030 
8031   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8032     SetExternalVisibleDeclsForName(DC, I->first, I->second);
8033   }
8034   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8035 }
8036 
8037 const serialization::reader::DeclContextLookupTable *
8038 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8039   auto I = Lookups.find(Primary);
8040   return I == Lookups.end() ? nullptr : &I->second;
8041 }
8042 
8043 /// Under non-PCH compilation the consumer receives the objc methods
8044 /// before receiving the implementation, and codegen depends on this.
8045 /// We simulate this by deserializing and passing to consumer the methods of the
8046 /// implementation before passing the deserialized implementation decl.
8047 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8048                                        ASTConsumer *Consumer) {
8049   assert(ImplD && Consumer);
8050 
8051   for (auto *I : ImplD->methods())
8052     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8053 
8054   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8055 }
8056 
8057 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8058   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8059     PassObjCImplDeclToConsumer(ImplD, Consumer);
8060   else
8061     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8062 }
8063 
8064 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8065   this->Consumer = Consumer;
8066 
8067   if (Consumer)
8068     PassInterestingDeclsToConsumer();
8069 
8070   if (DeserializationListener)
8071     DeserializationListener->ReaderInitialized(this);
8072 }
8073 
8074 void ASTReader::PrintStats() {
8075   std::fprintf(stderr, "*** AST File Statistics:\n");
8076 
8077   unsigned NumTypesLoaded
8078     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8079                                       QualType());
8080   unsigned NumDeclsLoaded
8081     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8082                                       (Decl *)nullptr);
8083   unsigned NumIdentifiersLoaded
8084     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8085                                             IdentifiersLoaded.end(),
8086                                             (IdentifierInfo *)nullptr);
8087   unsigned NumMacrosLoaded
8088     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8089                                        MacrosLoaded.end(),
8090                                        (MacroInfo *)nullptr);
8091   unsigned NumSelectorsLoaded
8092     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8093                                           SelectorsLoaded.end(),
8094                                           Selector());
8095 
8096   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8097     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8098                  NumSLocEntriesRead, TotalNumSLocEntries,
8099                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8100   if (!TypesLoaded.empty())
8101     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8102                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8103                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8104   if (!DeclsLoaded.empty())
8105     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8106                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8107                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8108   if (!IdentifiersLoaded.empty())
8109     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8110                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8111                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8112   if (!MacrosLoaded.empty())
8113     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8114                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8115                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8116   if (!SelectorsLoaded.empty())
8117     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8118                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8119                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8120   if (TotalNumStatements)
8121     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8122                  NumStatementsRead, TotalNumStatements,
8123                  ((float)NumStatementsRead/TotalNumStatements * 100));
8124   if (TotalNumMacros)
8125     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8126                  NumMacrosRead, TotalNumMacros,
8127                  ((float)NumMacrosRead/TotalNumMacros * 100));
8128   if (TotalLexicalDeclContexts)
8129     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8130                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8131                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8132                   * 100));
8133   if (TotalVisibleDeclContexts)
8134     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8135                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8136                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8137                   * 100));
8138   if (TotalNumMethodPoolEntries)
8139     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8140                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8141                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8142                   * 100));
8143   if (NumMethodPoolLookups)
8144     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8145                  NumMethodPoolHits, NumMethodPoolLookups,
8146                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8147   if (NumMethodPoolTableLookups)
8148     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8149                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8150                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8151                   * 100.0));
8152   if (NumIdentifierLookupHits)
8153     std::fprintf(stderr,
8154                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8155                  NumIdentifierLookupHits, NumIdentifierLookups,
8156                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8157 
8158   if (GlobalIndex) {
8159     std::fprintf(stderr, "\n");
8160     GlobalIndex->printStats();
8161   }
8162 
8163   std::fprintf(stderr, "\n");
8164   dump();
8165   std::fprintf(stderr, "\n");
8166 }
8167 
8168 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8169 LLVM_DUMP_METHOD static void
8170 dumpModuleIDMap(StringRef Name,
8171                 const ContinuousRangeMap<Key, ModuleFile *,
8172                                          InitialCapacity> &Map) {
8173   if (Map.begin() == Map.end())
8174     return;
8175 
8176   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8177 
8178   llvm::errs() << Name << ":\n";
8179   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8180        I != IEnd; ++I) {
8181     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8182       << "\n";
8183   }
8184 }
8185 
8186 LLVM_DUMP_METHOD void ASTReader::dump() {
8187   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8188   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8189   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8190   dumpModuleIDMap("Global type map", GlobalTypeMap);
8191   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8192   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8193   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8194   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8195   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8196   dumpModuleIDMap("Global preprocessed entity map",
8197                   GlobalPreprocessedEntityMap);
8198 
8199   llvm::errs() << "\n*** PCH/Modules Loaded:";
8200   for (ModuleFile &M : ModuleMgr)
8201     M.dump();
8202 }
8203 
8204 /// Return the amount of memory used by memory buffers, breaking down
8205 /// by heap-backed versus mmap'ed memory.
8206 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8207   for (ModuleFile &I : ModuleMgr) {
8208     if (llvm::MemoryBuffer *buf = I.Buffer) {
8209       size_t bytes = buf->getBufferSize();
8210       switch (buf->getBufferKind()) {
8211         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8212           sizes.malloc_bytes += bytes;
8213           break;
8214         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8215           sizes.mmap_bytes += bytes;
8216           break;
8217       }
8218     }
8219   }
8220 }
8221 
8222 void ASTReader::InitializeSema(Sema &S) {
8223   SemaObj = &S;
8224   S.addExternalSource(this);
8225 
8226   // Makes sure any declarations that were deserialized "too early"
8227   // still get added to the identifier's declaration chains.
8228   for (uint64_t ID : PreloadedDeclIDs) {
8229     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8230     pushExternalDeclIntoScope(D, D->getDeclName());
8231   }
8232   PreloadedDeclIDs.clear();
8233 
8234   // FIXME: What happens if these are changed by a module import?
8235   if (!FPPragmaOptions.empty()) {
8236     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8237     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8238   }
8239 
8240   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8241   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8242   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8243 
8244   UpdateSema();
8245 }
8246 
8247 void ASTReader::UpdateSema() {
8248   assert(SemaObj && "no Sema to update");
8249 
8250   // Load the offsets of the declarations that Sema references.
8251   // They will be lazily deserialized when needed.
8252   if (!SemaDeclRefs.empty()) {
8253     assert(SemaDeclRefs.size() % 3 == 0);
8254     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8255       if (!SemaObj->StdNamespace)
8256         SemaObj->StdNamespace = SemaDeclRefs[I];
8257       if (!SemaObj->StdBadAlloc)
8258         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8259       if (!SemaObj->StdAlignValT)
8260         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8261     }
8262     SemaDeclRefs.clear();
8263   }
8264 
8265   // Update the state of pragmas. Use the same API as if we had encountered the
8266   // pragma in the source.
8267   if(OptimizeOffPragmaLocation.isValid())
8268     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8269   if (PragmaMSStructState != -1)
8270     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8271   if (PointersToMembersPragmaLocation.isValid()) {
8272     SemaObj->ActOnPragmaMSPointersToMembers(
8273         (LangOptions::PragmaMSPointersToMembersKind)
8274             PragmaMSPointersToMembersState,
8275         PointersToMembersPragmaLocation);
8276   }
8277   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8278 
8279   if (PragmaPackCurrentValue) {
8280     // The bottom of the stack might have a default value. It must be adjusted
8281     // to the current value to ensure that the packing state is preserved after
8282     // popping entries that were included/imported from a PCH/module.
8283     bool DropFirst = false;
8284     if (!PragmaPackStack.empty() &&
8285         PragmaPackStack.front().Location.isInvalid()) {
8286       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8287              "Expected a default alignment value");
8288       SemaObj->PackStack.Stack.emplace_back(
8289           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8290           SemaObj->PackStack.CurrentPragmaLocation,
8291           PragmaPackStack.front().PushLocation);
8292       DropFirst = true;
8293     }
8294     for (const auto &Entry :
8295          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8296       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8297                                             Entry.Location, Entry.PushLocation);
8298     if (PragmaPackCurrentLocation.isInvalid()) {
8299       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8300              "Expected a default alignment value");
8301       // Keep the current values.
8302     } else {
8303       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8304       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8305     }
8306   }
8307 }
8308 
8309 IdentifierInfo *ASTReader::get(StringRef Name) {
8310   // Note that we are loading an identifier.
8311   Deserializing AnIdentifier(this);
8312 
8313   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8314                                   NumIdentifierLookups,
8315                                   NumIdentifierLookupHits);
8316 
8317   // We don't need to do identifier table lookups in C++ modules (we preload
8318   // all interesting declarations, and don't need to use the scope for name
8319   // lookups). Perform the lookup in PCH files, though, since we don't build
8320   // a complete initial identifier table if we're carrying on from a PCH.
8321   if (PP.getLangOpts().CPlusPlus) {
8322     for (auto F : ModuleMgr.pch_modules())
8323       if (Visitor(*F))
8324         break;
8325   } else {
8326     // If there is a global index, look there first to determine which modules
8327     // provably do not have any results for this identifier.
8328     GlobalModuleIndex::HitSet Hits;
8329     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8330     if (!loadGlobalIndex()) {
8331       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8332         HitsPtr = &Hits;
8333       }
8334     }
8335 
8336     ModuleMgr.visit(Visitor, HitsPtr);
8337   }
8338 
8339   IdentifierInfo *II = Visitor.getIdentifierInfo();
8340   markIdentifierUpToDate(II);
8341   return II;
8342 }
8343 
8344 namespace clang {
8345 
8346   /// An identifier-lookup iterator that enumerates all of the
8347   /// identifiers stored within a set of AST files.
8348   class ASTIdentifierIterator : public IdentifierIterator {
8349     /// The AST reader whose identifiers are being enumerated.
8350     const ASTReader &Reader;
8351 
8352     /// The current index into the chain of AST files stored in
8353     /// the AST reader.
8354     unsigned Index;
8355 
8356     /// The current position within the identifier lookup table
8357     /// of the current AST file.
8358     ASTIdentifierLookupTable::key_iterator Current;
8359 
8360     /// The end position within the identifier lookup table of
8361     /// the current AST file.
8362     ASTIdentifierLookupTable::key_iterator End;
8363 
8364     /// Whether to skip any modules in the ASTReader.
8365     bool SkipModules;
8366 
8367   public:
8368     explicit ASTIdentifierIterator(const ASTReader &Reader,
8369                                    bool SkipModules = false);
8370 
8371     StringRef Next() override;
8372   };
8373 
8374 } // namespace clang
8375 
8376 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8377                                              bool SkipModules)
8378     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8379 }
8380 
8381 StringRef ASTIdentifierIterator::Next() {
8382   while (Current == End) {
8383     // If we have exhausted all of our AST files, we're done.
8384     if (Index == 0)
8385       return StringRef();
8386 
8387     --Index;
8388     ModuleFile &F = Reader.ModuleMgr[Index];
8389     if (SkipModules && F.isModule())
8390       continue;
8391 
8392     ASTIdentifierLookupTable *IdTable =
8393         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8394     Current = IdTable->key_begin();
8395     End = IdTable->key_end();
8396   }
8397 
8398   // We have any identifiers remaining in the current AST file; return
8399   // the next one.
8400   StringRef Result = *Current;
8401   ++Current;
8402   return Result;
8403 }
8404 
8405 namespace {
8406 
8407 /// A utility for appending two IdentifierIterators.
8408 class ChainedIdentifierIterator : public IdentifierIterator {
8409   std::unique_ptr<IdentifierIterator> Current;
8410   std::unique_ptr<IdentifierIterator> Queued;
8411 
8412 public:
8413   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8414                             std::unique_ptr<IdentifierIterator> Second)
8415       : Current(std::move(First)), Queued(std::move(Second)) {}
8416 
8417   StringRef Next() override {
8418     if (!Current)
8419       return StringRef();
8420 
8421     StringRef result = Current->Next();
8422     if (!result.empty())
8423       return result;
8424 
8425     // Try the queued iterator, which may itself be empty.
8426     Current.reset();
8427     std::swap(Current, Queued);
8428     return Next();
8429   }
8430 };
8431 
8432 } // namespace
8433 
8434 IdentifierIterator *ASTReader::getIdentifiers() {
8435   if (!loadGlobalIndex()) {
8436     std::unique_ptr<IdentifierIterator> ReaderIter(
8437         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8438     std::unique_ptr<IdentifierIterator> ModulesIter(
8439         GlobalIndex->createIdentifierIterator());
8440     return new ChainedIdentifierIterator(std::move(ReaderIter),
8441                                          std::move(ModulesIter));
8442   }
8443 
8444   return new ASTIdentifierIterator(*this);
8445 }
8446 
8447 namespace clang {
8448 namespace serialization {
8449 
8450   class ReadMethodPoolVisitor {
8451     ASTReader &Reader;
8452     Selector Sel;
8453     unsigned PriorGeneration;
8454     unsigned InstanceBits = 0;
8455     unsigned FactoryBits = 0;
8456     bool InstanceHasMoreThanOneDecl = false;
8457     bool FactoryHasMoreThanOneDecl = false;
8458     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8459     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8460 
8461   public:
8462     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8463                           unsigned PriorGeneration)
8464         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8465 
8466     bool operator()(ModuleFile &M) {
8467       if (!M.SelectorLookupTable)
8468         return false;
8469 
8470       // If we've already searched this module file, skip it now.
8471       if (M.Generation <= PriorGeneration)
8472         return true;
8473 
8474       ++Reader.NumMethodPoolTableLookups;
8475       ASTSelectorLookupTable *PoolTable
8476         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8477       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8478       if (Pos == PoolTable->end())
8479         return false;
8480 
8481       ++Reader.NumMethodPoolTableHits;
8482       ++Reader.NumSelectorsRead;
8483       // FIXME: Not quite happy with the statistics here. We probably should
8484       // disable this tracking when called via LoadSelector.
8485       // Also, should entries without methods count as misses?
8486       ++Reader.NumMethodPoolEntriesRead;
8487       ASTSelectorLookupTrait::data_type Data = *Pos;
8488       if (Reader.DeserializationListener)
8489         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8490 
8491       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8492       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8493       InstanceBits = Data.InstanceBits;
8494       FactoryBits = Data.FactoryBits;
8495       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8496       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8497       return true;
8498     }
8499 
8500     /// Retrieve the instance methods found by this visitor.
8501     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8502       return InstanceMethods;
8503     }
8504 
8505     /// Retrieve the instance methods found by this visitor.
8506     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8507       return FactoryMethods;
8508     }
8509 
8510     unsigned getInstanceBits() const { return InstanceBits; }
8511     unsigned getFactoryBits() const { return FactoryBits; }
8512 
8513     bool instanceHasMoreThanOneDecl() const {
8514       return InstanceHasMoreThanOneDecl;
8515     }
8516 
8517     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8518   };
8519 
8520 } // namespace serialization
8521 } // namespace clang
8522 
8523 /// Add the given set of methods to the method list.
8524 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8525                              ObjCMethodList &List) {
8526   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8527     S.addMethodToGlobalList(&List, Methods[I]);
8528   }
8529 }
8530 
8531 void ASTReader::ReadMethodPool(Selector Sel) {
8532   // Get the selector generation and update it to the current generation.
8533   unsigned &Generation = SelectorGeneration[Sel];
8534   unsigned PriorGeneration = Generation;
8535   Generation = getGeneration();
8536   SelectorOutOfDate[Sel] = false;
8537 
8538   // Search for methods defined with this selector.
8539   ++NumMethodPoolLookups;
8540   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8541   ModuleMgr.visit(Visitor);
8542 
8543   if (Visitor.getInstanceMethods().empty() &&
8544       Visitor.getFactoryMethods().empty())
8545     return;
8546 
8547   ++NumMethodPoolHits;
8548 
8549   if (!getSema())
8550     return;
8551 
8552   Sema &S = *getSema();
8553   Sema::GlobalMethodPool::iterator Pos
8554     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8555 
8556   Pos->second.first.setBits(Visitor.getInstanceBits());
8557   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8558   Pos->second.second.setBits(Visitor.getFactoryBits());
8559   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8560 
8561   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8562   // when building a module we keep every method individually and may need to
8563   // update hasMoreThanOneDecl as we add the methods.
8564   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8565   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8566 }
8567 
8568 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8569   if (SelectorOutOfDate[Sel])
8570     ReadMethodPool(Sel);
8571 }
8572 
8573 void ASTReader::ReadKnownNamespaces(
8574                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8575   Namespaces.clear();
8576 
8577   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8578     if (NamespaceDecl *Namespace
8579                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8580       Namespaces.push_back(Namespace);
8581   }
8582 }
8583 
8584 void ASTReader::ReadUndefinedButUsed(
8585     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8586   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8587     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8588     SourceLocation Loc =
8589         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8590     Undefined.insert(std::make_pair(D, Loc));
8591   }
8592 }
8593 
8594 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8595     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8596                                                      Exprs) {
8597   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8598     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8599     uint64_t Count = DelayedDeleteExprs[Idx++];
8600     for (uint64_t C = 0; C < Count; ++C) {
8601       SourceLocation DeleteLoc =
8602           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8603       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8604       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8605     }
8606   }
8607 }
8608 
8609 void ASTReader::ReadTentativeDefinitions(
8610                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8611   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8612     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8613     if (Var)
8614       TentativeDefs.push_back(Var);
8615   }
8616   TentativeDefinitions.clear();
8617 }
8618 
8619 void ASTReader::ReadUnusedFileScopedDecls(
8620                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8621   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8622     DeclaratorDecl *D
8623       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8624     if (D)
8625       Decls.push_back(D);
8626   }
8627   UnusedFileScopedDecls.clear();
8628 }
8629 
8630 void ASTReader::ReadDelegatingConstructors(
8631                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8632   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8633     CXXConstructorDecl *D
8634       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8635     if (D)
8636       Decls.push_back(D);
8637   }
8638   DelegatingCtorDecls.clear();
8639 }
8640 
8641 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8642   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8643     TypedefNameDecl *D
8644       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8645     if (D)
8646       Decls.push_back(D);
8647   }
8648   ExtVectorDecls.clear();
8649 }
8650 
8651 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8652     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8653   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8654        ++I) {
8655     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8656         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8657     if (D)
8658       Decls.insert(D);
8659   }
8660   UnusedLocalTypedefNameCandidates.clear();
8661 }
8662 
8663 void ASTReader::ReadReferencedSelectors(
8664        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8665   if (ReferencedSelectorsData.empty())
8666     return;
8667 
8668   // If there are @selector references added them to its pool. This is for
8669   // implementation of -Wselector.
8670   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8671   unsigned I = 0;
8672   while (I < DataSize) {
8673     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8674     SourceLocation SelLoc
8675       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8676     Sels.push_back(std::make_pair(Sel, SelLoc));
8677   }
8678   ReferencedSelectorsData.clear();
8679 }
8680 
8681 void ASTReader::ReadWeakUndeclaredIdentifiers(
8682        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8683   if (WeakUndeclaredIdentifiers.empty())
8684     return;
8685 
8686   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8687     IdentifierInfo *WeakId
8688       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8689     IdentifierInfo *AliasId
8690       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8691     SourceLocation Loc
8692       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8693     bool Used = WeakUndeclaredIdentifiers[I++];
8694     WeakInfo WI(AliasId, Loc);
8695     WI.setUsed(Used);
8696     WeakIDs.push_back(std::make_pair(WeakId, WI));
8697   }
8698   WeakUndeclaredIdentifiers.clear();
8699 }
8700 
8701 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8702   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8703     ExternalVTableUse VT;
8704     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8705     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8706     VT.DefinitionRequired = VTableUses[Idx++];
8707     VTables.push_back(VT);
8708   }
8709 
8710   VTableUses.clear();
8711 }
8712 
8713 void ASTReader::ReadPendingInstantiations(
8714        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8715   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8716     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8717     SourceLocation Loc
8718       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8719 
8720     Pending.push_back(std::make_pair(D, Loc));
8721   }
8722   PendingInstantiations.clear();
8723 }
8724 
8725 void ASTReader::ReadLateParsedTemplates(
8726     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8727         &LPTMap) {
8728   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8729        /* In loop */) {
8730     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8731 
8732     auto LT = std::make_unique<LateParsedTemplate>();
8733     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8734 
8735     ModuleFile *F = getOwningModuleFile(LT->D);
8736     assert(F && "No module");
8737 
8738     unsigned TokN = LateParsedTemplates[Idx++];
8739     LT->Toks.reserve(TokN);
8740     for (unsigned T = 0; T < TokN; ++T)
8741       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8742 
8743     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8744   }
8745 
8746   LateParsedTemplates.clear();
8747 }
8748 
8749 void ASTReader::LoadSelector(Selector Sel) {
8750   // It would be complicated to avoid reading the methods anyway. So don't.
8751   ReadMethodPool(Sel);
8752 }
8753 
8754 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8755   assert(ID && "Non-zero identifier ID required");
8756   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8757   IdentifiersLoaded[ID - 1] = II;
8758   if (DeserializationListener)
8759     DeserializationListener->IdentifierRead(ID, II);
8760 }
8761 
8762 /// Set the globally-visible declarations associated with the given
8763 /// identifier.
8764 ///
8765 /// If the AST reader is currently in a state where the given declaration IDs
8766 /// cannot safely be resolved, they are queued until it is safe to resolve
8767 /// them.
8768 ///
8769 /// \param II an IdentifierInfo that refers to one or more globally-visible
8770 /// declarations.
8771 ///
8772 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8773 /// visible at global scope.
8774 ///
8775 /// \param Decls if non-null, this vector will be populated with the set of
8776 /// deserialized declarations. These declarations will not be pushed into
8777 /// scope.
8778 void
8779 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8780                               const SmallVectorImpl<uint32_t> &DeclIDs,
8781                                    SmallVectorImpl<Decl *> *Decls) {
8782   if (NumCurrentElementsDeserializing && !Decls) {
8783     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8784     return;
8785   }
8786 
8787   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8788     if (!SemaObj) {
8789       // Queue this declaration so that it will be added to the
8790       // translation unit scope and identifier's declaration chain
8791       // once a Sema object is known.
8792       PreloadedDeclIDs.push_back(DeclIDs[I]);
8793       continue;
8794     }
8795 
8796     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8797 
8798     // If we're simply supposed to record the declarations, do so now.
8799     if (Decls) {
8800       Decls->push_back(D);
8801       continue;
8802     }
8803 
8804     // Introduce this declaration into the translation-unit scope
8805     // and add it to the declaration chain for this identifier, so
8806     // that (unqualified) name lookup will find it.
8807     pushExternalDeclIntoScope(D, II);
8808   }
8809 }
8810 
8811 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8812   if (ID == 0)
8813     return nullptr;
8814 
8815   if (IdentifiersLoaded.empty()) {
8816     Error("no identifier table in AST file");
8817     return nullptr;
8818   }
8819 
8820   ID -= 1;
8821   if (!IdentifiersLoaded[ID]) {
8822     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8823     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8824     ModuleFile *M = I->second;
8825     unsigned Index = ID - M->BaseIdentifierID;
8826     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8827 
8828     // All of the strings in the AST file are preceded by a 16-bit length.
8829     // Extract that 16-bit length to avoid having to execute strlen().
8830     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8831     //  unsigned integers.  This is important to avoid integer overflow when
8832     //  we cast them to 'unsigned'.
8833     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8834     unsigned StrLen = (((unsigned) StrLenPtr[0])
8835                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8836     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8837     IdentifiersLoaded[ID] = &II;
8838     markIdentifierFromAST(*this,  II);
8839     if (DeserializationListener)
8840       DeserializationListener->IdentifierRead(ID + 1, &II);
8841   }
8842 
8843   return IdentifiersLoaded[ID];
8844 }
8845 
8846 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8847   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8848 }
8849 
8850 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8851   if (LocalID < NUM_PREDEF_IDENT_IDS)
8852     return LocalID;
8853 
8854   if (!M.ModuleOffsetMap.empty())
8855     ReadModuleOffsetMap(M);
8856 
8857   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8858     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8859   assert(I != M.IdentifierRemap.end()
8860          && "Invalid index into identifier index remap");
8861 
8862   return LocalID + I->second;
8863 }
8864 
8865 MacroInfo *ASTReader::getMacro(MacroID ID) {
8866   if (ID == 0)
8867     return nullptr;
8868 
8869   if (MacrosLoaded.empty()) {
8870     Error("no macro table in AST file");
8871     return nullptr;
8872   }
8873 
8874   ID -= NUM_PREDEF_MACRO_IDS;
8875   if (!MacrosLoaded[ID]) {
8876     GlobalMacroMapType::iterator I
8877       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8878     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8879     ModuleFile *M = I->second;
8880     unsigned Index = ID - M->BaseMacroID;
8881     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8882 
8883     if (DeserializationListener)
8884       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8885                                          MacrosLoaded[ID]);
8886   }
8887 
8888   return MacrosLoaded[ID];
8889 }
8890 
8891 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8892   if (LocalID < NUM_PREDEF_MACRO_IDS)
8893     return LocalID;
8894 
8895   if (!M.ModuleOffsetMap.empty())
8896     ReadModuleOffsetMap(M);
8897 
8898   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8899     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8900   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8901 
8902   return LocalID + I->second;
8903 }
8904 
8905 serialization::SubmoduleID
8906 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8907   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8908     return LocalID;
8909 
8910   if (!M.ModuleOffsetMap.empty())
8911     ReadModuleOffsetMap(M);
8912 
8913   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8914     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8915   assert(I != M.SubmoduleRemap.end()
8916          && "Invalid index into submodule index remap");
8917 
8918   return LocalID + I->second;
8919 }
8920 
8921 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8922   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8923     assert(GlobalID == 0 && "Unhandled global submodule ID");
8924     return nullptr;
8925   }
8926 
8927   if (GlobalID > SubmodulesLoaded.size()) {
8928     Error("submodule ID out of range in AST file");
8929     return nullptr;
8930   }
8931 
8932   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8933 }
8934 
8935 Module *ASTReader::getModule(unsigned ID) {
8936   return getSubmodule(ID);
8937 }
8938 
8939 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8940   ModuleFile *MF = getOwningModuleFile(D);
8941   return MF && MF->PCHHasObjectFile;
8942 }
8943 
8944 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8945   if (ID & 1) {
8946     // It's a module, look it up by submodule ID.
8947     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8948     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8949   } else {
8950     // It's a prefix (preamble, PCH, ...). Look it up by index.
8951     unsigned IndexFromEnd = ID >> 1;
8952     assert(IndexFromEnd && "got reference to unknown module file");
8953     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8954   }
8955 }
8956 
8957 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8958   if (!F)
8959     return 1;
8960 
8961   // For a file representing a module, use the submodule ID of the top-level
8962   // module as the file ID. For any other kind of file, the number of such
8963   // files loaded beforehand will be the same on reload.
8964   // FIXME: Is this true even if we have an explicit module file and a PCH?
8965   if (F->isModule())
8966     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8967 
8968   auto PCHModules = getModuleManager().pch_modules();
8969   auto I = llvm::find(PCHModules, F);
8970   assert(I != PCHModules.end() && "emitting reference to unknown file");
8971   return (I - PCHModules.end()) << 1;
8972 }
8973 
8974 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8975 ASTReader::getSourceDescriptor(unsigned ID) {
8976   if (const Module *M = getSubmodule(ID))
8977     return ExternalASTSource::ASTSourceDescriptor(*M);
8978 
8979   // If there is only a single PCH, return it instead.
8980   // Chained PCH are not supported.
8981   const auto &PCHChain = ModuleMgr.pch_modules();
8982   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8983     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8984     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8985     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8986     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8987                                           MF.Signature);
8988   }
8989   return None;
8990 }
8991 
8992 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8993   auto I = DefinitionSource.find(FD);
8994   if (I == DefinitionSource.end())
8995     return EK_ReplyHazy;
8996   return I->second ? EK_Never : EK_Always;
8997 }
8998 
8999 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9000   return DecodeSelector(getGlobalSelectorID(M, LocalID));
9001 }
9002 
9003 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9004   if (ID == 0)
9005     return Selector();
9006 
9007   if (ID > SelectorsLoaded.size()) {
9008     Error("selector ID out of range in AST file");
9009     return Selector();
9010   }
9011 
9012   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9013     // Load this selector from the selector table.
9014     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9015     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9016     ModuleFile &M = *I->second;
9017     ASTSelectorLookupTrait Trait(*this, M);
9018     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9019     SelectorsLoaded[ID - 1] =
9020       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9021     if (DeserializationListener)
9022       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9023   }
9024 
9025   return SelectorsLoaded[ID - 1];
9026 }
9027 
9028 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9029   return DecodeSelector(ID);
9030 }
9031 
9032 uint32_t ASTReader::GetNumExternalSelectors() {
9033   // ID 0 (the null selector) is considered an external selector.
9034   return getTotalNumSelectors() + 1;
9035 }
9036 
9037 serialization::SelectorID
9038 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9039   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9040     return LocalID;
9041 
9042   if (!M.ModuleOffsetMap.empty())
9043     ReadModuleOffsetMap(M);
9044 
9045   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9046     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9047   assert(I != M.SelectorRemap.end()
9048          && "Invalid index into selector index remap");
9049 
9050   return LocalID + I->second;
9051 }
9052 
9053 DeclarationName
9054 ASTReader::ReadDeclarationName(ModuleFile &F,
9055                                const RecordData &Record, unsigned &Idx) {
9056   ASTContext &Context = getContext();
9057   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9058   switch (Kind) {
9059   case DeclarationName::Identifier:
9060     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9061 
9062   case DeclarationName::ObjCZeroArgSelector:
9063   case DeclarationName::ObjCOneArgSelector:
9064   case DeclarationName::ObjCMultiArgSelector:
9065     return DeclarationName(ReadSelector(F, Record, Idx));
9066 
9067   case DeclarationName::CXXConstructorName:
9068     return Context.DeclarationNames.getCXXConstructorName(
9069                           Context.getCanonicalType(readType(F, Record, Idx)));
9070 
9071   case DeclarationName::CXXDestructorName:
9072     return Context.DeclarationNames.getCXXDestructorName(
9073                           Context.getCanonicalType(readType(F, Record, Idx)));
9074 
9075   case DeclarationName::CXXDeductionGuideName:
9076     return Context.DeclarationNames.getCXXDeductionGuideName(
9077                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
9078 
9079   case DeclarationName::CXXConversionFunctionName:
9080     return Context.DeclarationNames.getCXXConversionFunctionName(
9081                           Context.getCanonicalType(readType(F, Record, Idx)));
9082 
9083   case DeclarationName::CXXOperatorName:
9084     return Context.DeclarationNames.getCXXOperatorName(
9085                                        (OverloadedOperatorKind)Record[Idx++]);
9086 
9087   case DeclarationName::CXXLiteralOperatorName:
9088     return Context.DeclarationNames.getCXXLiteralOperatorName(
9089                                        GetIdentifierInfo(F, Record, Idx));
9090 
9091   case DeclarationName::CXXUsingDirective:
9092     return DeclarationName::getUsingDirectiveName();
9093   }
9094 
9095   llvm_unreachable("Invalid NameKind!");
9096 }
9097 
9098 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9099                                        DeclarationNameLoc &DNLoc,
9100                                        DeclarationName Name,
9101                                       const RecordData &Record, unsigned &Idx) {
9102   switch (Name.getNameKind()) {
9103   case DeclarationName::CXXConstructorName:
9104   case DeclarationName::CXXDestructorName:
9105   case DeclarationName::CXXConversionFunctionName:
9106     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9107     break;
9108 
9109   case DeclarationName::CXXOperatorName:
9110     DNLoc.CXXOperatorName.BeginOpNameLoc
9111         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9112     DNLoc.CXXOperatorName.EndOpNameLoc
9113         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9114     break;
9115 
9116   case DeclarationName::CXXLiteralOperatorName:
9117     DNLoc.CXXLiteralOperatorName.OpNameLoc
9118         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9119     break;
9120 
9121   case DeclarationName::Identifier:
9122   case DeclarationName::ObjCZeroArgSelector:
9123   case DeclarationName::ObjCOneArgSelector:
9124   case DeclarationName::ObjCMultiArgSelector:
9125   case DeclarationName::CXXUsingDirective:
9126   case DeclarationName::CXXDeductionGuideName:
9127     break;
9128   }
9129 }
9130 
9131 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9132                                         DeclarationNameInfo &NameInfo,
9133                                       const RecordData &Record, unsigned &Idx) {
9134   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9135   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9136   DeclarationNameLoc DNLoc;
9137   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9138   NameInfo.setInfo(DNLoc);
9139 }
9140 
9141 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9142                                   const RecordData &Record, unsigned &Idx) {
9143   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9144   unsigned NumTPLists = Record[Idx++];
9145   Info.NumTemplParamLists = NumTPLists;
9146   if (NumTPLists) {
9147     Info.TemplParamLists =
9148         new (getContext()) TemplateParameterList *[NumTPLists];
9149     for (unsigned i = 0; i != NumTPLists; ++i)
9150       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9151   }
9152 }
9153 
9154 TemplateName
9155 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9156                             unsigned &Idx) {
9157   ASTContext &Context = getContext();
9158   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9159   switch (Kind) {
9160   case TemplateName::Template:
9161       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9162 
9163   case TemplateName::OverloadedTemplate: {
9164     unsigned size = Record[Idx++];
9165     UnresolvedSet<8> Decls;
9166     while (size--)
9167       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9168 
9169     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9170   }
9171 
9172   case TemplateName::AssumedTemplate: {
9173     DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9174     return Context.getAssumedTemplateName(Name);
9175   }
9176 
9177   case TemplateName::QualifiedTemplate: {
9178     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9179     bool hasTemplKeyword = Record[Idx++];
9180     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9181     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9182   }
9183 
9184   case TemplateName::DependentTemplate: {
9185     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9186     if (Record[Idx++])  // isIdentifier
9187       return Context.getDependentTemplateName(NNS,
9188                                                GetIdentifierInfo(F, Record,
9189                                                                  Idx));
9190     return Context.getDependentTemplateName(NNS,
9191                                          (OverloadedOperatorKind)Record[Idx++]);
9192   }
9193 
9194   case TemplateName::SubstTemplateTemplateParm: {
9195     TemplateTemplateParmDecl *param
9196       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9197     if (!param) return TemplateName();
9198     TemplateName replacement = ReadTemplateName(F, Record, Idx);
9199     return Context.getSubstTemplateTemplateParm(param, replacement);
9200   }
9201 
9202   case TemplateName::SubstTemplateTemplateParmPack: {
9203     TemplateTemplateParmDecl *Param
9204       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9205     if (!Param)
9206       return TemplateName();
9207 
9208     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9209     if (ArgPack.getKind() != TemplateArgument::Pack)
9210       return TemplateName();
9211 
9212     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9213   }
9214   }
9215 
9216   llvm_unreachable("Unhandled template name kind!");
9217 }
9218 
9219 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9220                                                  const RecordData &Record,
9221                                                  unsigned &Idx,
9222                                                  bool Canonicalize) {
9223   ASTContext &Context = getContext();
9224   if (Canonicalize) {
9225     // The caller wants a canonical template argument. Sometimes the AST only
9226     // wants template arguments in canonical form (particularly as the template
9227     // argument lists of template specializations) so ensure we preserve that
9228     // canonical form across serialization.
9229     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9230     return Context.getCanonicalTemplateArgument(Arg);
9231   }
9232 
9233   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9234   switch (Kind) {
9235   case TemplateArgument::Null:
9236     return TemplateArgument();
9237   case TemplateArgument::Type:
9238     return TemplateArgument(readType(F, Record, Idx));
9239   case TemplateArgument::Declaration: {
9240     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9241     return TemplateArgument(D, readType(F, Record, Idx));
9242   }
9243   case TemplateArgument::NullPtr:
9244     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9245   case TemplateArgument::Integral: {
9246     llvm::APSInt Value = ReadAPSInt(Record, Idx);
9247     QualType T = readType(F, Record, Idx);
9248     return TemplateArgument(Context, Value, T);
9249   }
9250   case TemplateArgument::Template:
9251     return TemplateArgument(ReadTemplateName(F, Record, Idx));
9252   case TemplateArgument::TemplateExpansion: {
9253     TemplateName Name = ReadTemplateName(F, Record, Idx);
9254     Optional<unsigned> NumTemplateExpansions;
9255     if (unsigned NumExpansions = Record[Idx++])
9256       NumTemplateExpansions = NumExpansions - 1;
9257     return TemplateArgument(Name, NumTemplateExpansions);
9258   }
9259   case TemplateArgument::Expression:
9260     return TemplateArgument(ReadExpr(F));
9261   case TemplateArgument::Pack: {
9262     unsigned NumArgs = Record[Idx++];
9263     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9264     for (unsigned I = 0; I != NumArgs; ++I)
9265       Args[I] = ReadTemplateArgument(F, Record, Idx);
9266     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9267   }
9268   }
9269 
9270   llvm_unreachable("Unhandled template argument kind!");
9271 }
9272 
9273 TemplateParameterList *
9274 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9275                                      const RecordData &Record, unsigned &Idx) {
9276   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9277   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9278   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9279 
9280   unsigned NumParams = Record[Idx++];
9281   SmallVector<NamedDecl *, 16> Params;
9282   Params.reserve(NumParams);
9283   while (NumParams--)
9284     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9285 
9286   // TODO: Concepts
9287   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9288       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
9289   return TemplateParams;
9290 }
9291 
9292 void
9293 ASTReader::
9294 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9295                          ModuleFile &F, const RecordData &Record,
9296                          unsigned &Idx, bool Canonicalize) {
9297   unsigned NumTemplateArgs = Record[Idx++];
9298   TemplArgs.reserve(NumTemplateArgs);
9299   while (NumTemplateArgs--)
9300     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9301 }
9302 
9303 /// Read a UnresolvedSet structure.
9304 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9305                                   const RecordData &Record, unsigned &Idx) {
9306   unsigned NumDecls = Record[Idx++];
9307   Set.reserve(getContext(), NumDecls);
9308   while (NumDecls--) {
9309     DeclID ID = ReadDeclID(F, Record, Idx);
9310     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9311     Set.addLazyDecl(getContext(), ID, AS);
9312   }
9313 }
9314 
9315 CXXBaseSpecifier
9316 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9317                                 const RecordData &Record, unsigned &Idx) {
9318   bool isVirtual = static_cast<bool>(Record[Idx++]);
9319   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9320   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9321   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9322   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9323   SourceRange Range = ReadSourceRange(F, Record, Idx);
9324   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9325   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9326                           EllipsisLoc);
9327   Result.setInheritConstructors(inheritConstructors);
9328   return Result;
9329 }
9330 
9331 CXXCtorInitializer **
9332 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9333                                    unsigned &Idx) {
9334   ASTContext &Context = getContext();
9335   unsigned NumInitializers = Record[Idx++];
9336   assert(NumInitializers && "wrote ctor initializers but have no inits");
9337   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9338   for (unsigned i = 0; i != NumInitializers; ++i) {
9339     TypeSourceInfo *TInfo = nullptr;
9340     bool IsBaseVirtual = false;
9341     FieldDecl *Member = nullptr;
9342     IndirectFieldDecl *IndirectMember = nullptr;
9343 
9344     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9345     switch (Type) {
9346     case CTOR_INITIALIZER_BASE:
9347       TInfo = GetTypeSourceInfo(F, Record, Idx);
9348       IsBaseVirtual = Record[Idx++];
9349       break;
9350 
9351     case CTOR_INITIALIZER_DELEGATING:
9352       TInfo = GetTypeSourceInfo(F, Record, Idx);
9353       break;
9354 
9355      case CTOR_INITIALIZER_MEMBER:
9356       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9357       break;
9358 
9359      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9360       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9361       break;
9362     }
9363 
9364     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9365     Expr *Init = ReadExpr(F);
9366     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9367     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9368 
9369     CXXCtorInitializer *BOMInit;
9370     if (Type == CTOR_INITIALIZER_BASE)
9371       BOMInit = new (Context)
9372           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9373                              RParenLoc, MemberOrEllipsisLoc);
9374     else if (Type == CTOR_INITIALIZER_DELEGATING)
9375       BOMInit = new (Context)
9376           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9377     else if (Member)
9378       BOMInit = new (Context)
9379           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9380                              Init, RParenLoc);
9381     else
9382       BOMInit = new (Context)
9383           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9384                              LParenLoc, Init, RParenLoc);
9385 
9386     if (/*IsWritten*/Record[Idx++]) {
9387       unsigned SourceOrder = Record[Idx++];
9388       BOMInit->setSourceOrder(SourceOrder);
9389     }
9390 
9391     CtorInitializers[i] = BOMInit;
9392   }
9393 
9394   return CtorInitializers;
9395 }
9396 
9397 NestedNameSpecifier *
9398 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9399                                    const RecordData &Record, unsigned &Idx) {
9400   ASTContext &Context = getContext();
9401   unsigned N = Record[Idx++];
9402   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9403   for (unsigned I = 0; I != N; ++I) {
9404     NestedNameSpecifier::SpecifierKind Kind
9405       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9406     switch (Kind) {
9407     case NestedNameSpecifier::Identifier: {
9408       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9409       NNS = NestedNameSpecifier::Create(Context, Prev, II);
9410       break;
9411     }
9412 
9413     case NestedNameSpecifier::Namespace: {
9414       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9415       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9416       break;
9417     }
9418 
9419     case NestedNameSpecifier::NamespaceAlias: {
9420       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9421       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9422       break;
9423     }
9424 
9425     case NestedNameSpecifier::TypeSpec:
9426     case NestedNameSpecifier::TypeSpecWithTemplate: {
9427       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9428       if (!T)
9429         return nullptr;
9430 
9431       bool Template = Record[Idx++];
9432       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9433       break;
9434     }
9435 
9436     case NestedNameSpecifier::Global:
9437       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9438       // No associated value, and there can't be a prefix.
9439       break;
9440 
9441     case NestedNameSpecifier::Super: {
9442       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9443       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9444       break;
9445     }
9446     }
9447     Prev = NNS;
9448   }
9449   return NNS;
9450 }
9451 
9452 NestedNameSpecifierLoc
9453 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9454                                       unsigned &Idx) {
9455   ASTContext &Context = getContext();
9456   unsigned N = Record[Idx++];
9457   NestedNameSpecifierLocBuilder Builder;
9458   for (unsigned I = 0; I != N; ++I) {
9459     NestedNameSpecifier::SpecifierKind Kind
9460       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9461     switch (Kind) {
9462     case NestedNameSpecifier::Identifier: {
9463       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9464       SourceRange Range = ReadSourceRange(F, Record, Idx);
9465       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9466       break;
9467     }
9468 
9469     case NestedNameSpecifier::Namespace: {
9470       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9471       SourceRange Range = ReadSourceRange(F, Record, Idx);
9472       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9473       break;
9474     }
9475 
9476     case NestedNameSpecifier::NamespaceAlias: {
9477       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9478       SourceRange Range = ReadSourceRange(F, Record, Idx);
9479       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9480       break;
9481     }
9482 
9483     case NestedNameSpecifier::TypeSpec:
9484     case NestedNameSpecifier::TypeSpecWithTemplate: {
9485       bool Template = Record[Idx++];
9486       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9487       if (!T)
9488         return NestedNameSpecifierLoc();
9489       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9490 
9491       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9492       Builder.Extend(Context,
9493                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9494                      T->getTypeLoc(), ColonColonLoc);
9495       break;
9496     }
9497 
9498     case NestedNameSpecifier::Global: {
9499       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9500       Builder.MakeGlobal(Context, ColonColonLoc);
9501       break;
9502     }
9503 
9504     case NestedNameSpecifier::Super: {
9505       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9506       SourceRange Range = ReadSourceRange(F, Record, Idx);
9507       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9508       break;
9509     }
9510     }
9511   }
9512 
9513   return Builder.getWithLocInContext(Context);
9514 }
9515 
9516 SourceRange
9517 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9518                            unsigned &Idx) {
9519   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9520   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9521   return SourceRange(beg, end);
9522 }
9523 
9524 static FixedPointSemantics
9525 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9526                         unsigned &Idx) {
9527   unsigned Width = Record[Idx++];
9528   unsigned Scale = Record[Idx++];
9529   uint64_t Tmp = Record[Idx++];
9530   bool IsSigned = Tmp & 0x1;
9531   bool IsSaturated = Tmp & 0x2;
9532   bool HasUnsignedPadding = Tmp & 0x4;
9533   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9534                              HasUnsignedPadding);
9535 }
9536 
9537 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9538   unsigned Kind = Record[Idx++];
9539   switch (Kind) {
9540   case APValue::None:
9541     return APValue();
9542   case APValue::Indeterminate:
9543     return APValue::IndeterminateValue();
9544   case APValue::Int:
9545     return APValue(ReadAPSInt(Record, Idx));
9546   case APValue::Float: {
9547     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9548         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9549     return APValue(ReadAPFloat(Record, FloatSema, Idx));
9550   }
9551   case APValue::FixedPoint: {
9552     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9553     return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9554   }
9555   case APValue::ComplexInt: {
9556     llvm::APSInt First = ReadAPSInt(Record, Idx);
9557     return APValue(std::move(First), ReadAPSInt(Record, Idx));
9558   }
9559   case APValue::ComplexFloat: {
9560     const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9561         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9562     llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9563     const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9564         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9565     return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9566   }
9567   case APValue::LValue:
9568   case APValue::Vector:
9569   case APValue::Array:
9570   case APValue::Struct:
9571   case APValue::Union:
9572   case APValue::MemberPointer:
9573   case APValue::AddrLabelDiff:
9574     // TODO : Handle all these APValue::ValueKind.
9575     return APValue();
9576   }
9577   llvm_unreachable("Invalid APValue::ValueKind");
9578 }
9579 
9580 /// Read an integral value
9581 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9582   unsigned BitWidth = Record[Idx++];
9583   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9584   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9585   Idx += NumWords;
9586   return Result;
9587 }
9588 
9589 /// Read a signed integral value
9590 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9591   bool isUnsigned = Record[Idx++];
9592   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9593 }
9594 
9595 /// Read a floating-point value
9596 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9597                                      const llvm::fltSemantics &Sem,
9598                                      unsigned &Idx) {
9599   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9600 }
9601 
9602 // Read a string
9603 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9604   unsigned Len = Record[Idx++];
9605   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9606   Idx += Len;
9607   return Result;
9608 }
9609 
9610 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9611                                 unsigned &Idx) {
9612   std::string Filename = ReadString(Record, Idx);
9613   ResolveImportedPath(F, Filename);
9614   return Filename;
9615 }
9616 
9617 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9618                                 const RecordData &Record, unsigned &Idx) {
9619   std::string Filename = ReadString(Record, Idx);
9620   if (!BaseDirectory.empty())
9621     ResolveImportedPath(Filename, BaseDirectory);
9622   return Filename;
9623 }
9624 
9625 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9626                                          unsigned &Idx) {
9627   unsigned Major = Record[Idx++];
9628   unsigned Minor = Record[Idx++];
9629   unsigned Subminor = Record[Idx++];
9630   if (Minor == 0)
9631     return VersionTuple(Major);
9632   if (Subminor == 0)
9633     return VersionTuple(Major, Minor - 1);
9634   return VersionTuple(Major, Minor - 1, Subminor - 1);
9635 }
9636 
9637 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9638                                           const RecordData &Record,
9639                                           unsigned &Idx) {
9640   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9641   return CXXTemporary::Create(getContext(), Decl);
9642 }
9643 
9644 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9645   return Diag(CurrentImportLoc, DiagID);
9646 }
9647 
9648 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9649   return Diags.Report(Loc, DiagID);
9650 }
9651 
9652 /// Retrieve the identifier table associated with the
9653 /// preprocessor.
9654 IdentifierTable &ASTReader::getIdentifierTable() {
9655   return PP.getIdentifierTable();
9656 }
9657 
9658 /// Record that the given ID maps to the given switch-case
9659 /// statement.
9660 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9661   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9662          "Already have a SwitchCase with this ID");
9663   (*CurrSwitchCaseStmts)[ID] = SC;
9664 }
9665 
9666 /// Retrieve the switch-case statement with the given ID.
9667 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9668   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9669   return (*CurrSwitchCaseStmts)[ID];
9670 }
9671 
9672 void ASTReader::ClearSwitchCaseIDs() {
9673   CurrSwitchCaseStmts->clear();
9674 }
9675 
9676 void ASTReader::ReadComments() {
9677   ASTContext &Context = getContext();
9678   std::vector<RawComment *> Comments;
9679   for (SmallVectorImpl<std::pair<BitstreamCursor,
9680                                  serialization::ModuleFile *>>::iterator
9681        I = CommentsCursors.begin(),
9682        E = CommentsCursors.end();
9683        I != E; ++I) {
9684     Comments.clear();
9685     BitstreamCursor &Cursor = I->first;
9686     serialization::ModuleFile &F = *I->second;
9687     SavedStreamPosition SavedPosition(Cursor);
9688 
9689     RecordData Record;
9690     while (true) {
9691       Expected<llvm::BitstreamEntry> MaybeEntry =
9692           Cursor.advanceSkippingSubblocks(
9693               BitstreamCursor::AF_DontPopBlockAtEnd);
9694       if (!MaybeEntry) {
9695         Error(MaybeEntry.takeError());
9696         return;
9697       }
9698       llvm::BitstreamEntry Entry = MaybeEntry.get();
9699 
9700       switch (Entry.Kind) {
9701       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9702       case llvm::BitstreamEntry::Error:
9703         Error("malformed block record in AST file");
9704         return;
9705       case llvm::BitstreamEntry::EndBlock:
9706         goto NextCursor;
9707       case llvm::BitstreamEntry::Record:
9708         // The interesting case.
9709         break;
9710       }
9711 
9712       // Read a record.
9713       Record.clear();
9714       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9715       if (!MaybeComment) {
9716         Error(MaybeComment.takeError());
9717         return;
9718       }
9719       switch ((CommentRecordTypes)MaybeComment.get()) {
9720       case COMMENTS_RAW_COMMENT: {
9721         unsigned Idx = 0;
9722         SourceRange SR = ReadSourceRange(F, Record, Idx);
9723         RawComment::CommentKind Kind =
9724             (RawComment::CommentKind) Record[Idx++];
9725         bool IsTrailingComment = Record[Idx++];
9726         bool IsAlmostTrailingComment = Record[Idx++];
9727         Comments.push_back(new (Context) RawComment(
9728             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9729         break;
9730       }
9731       }
9732     }
9733   NextCursor:
9734     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9735         FileToOffsetToComment;
9736     for (RawComment *C : Comments) {
9737       SourceLocation CommentLoc = C->getBeginLoc();
9738       if (CommentLoc.isValid()) {
9739         std::pair<FileID, unsigned> Loc =
9740             SourceMgr.getDecomposedLoc(CommentLoc);
9741         if (Loc.first.isValid())
9742           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9743       }
9744     }
9745   }
9746 }
9747 
9748 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9749                                 bool IncludeSystem, bool Complain,
9750                     llvm::function_ref<void(const serialization::InputFile &IF,
9751                                             bool isSystem)> Visitor) {
9752   unsigned NumUserInputs = MF.NumUserInputFiles;
9753   unsigned NumInputs = MF.InputFilesLoaded.size();
9754   assert(NumUserInputs <= NumInputs);
9755   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9756   for (unsigned I = 0; I < N; ++I) {
9757     bool IsSystem = I >= NumUserInputs;
9758     InputFile IF = getInputFile(MF, I+1, Complain);
9759     Visitor(IF, IsSystem);
9760   }
9761 }
9762 
9763 void ASTReader::visitTopLevelModuleMaps(
9764     serialization::ModuleFile &MF,
9765     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9766   unsigned NumInputs = MF.InputFilesLoaded.size();
9767   for (unsigned I = 0; I < NumInputs; ++I) {
9768     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9769     if (IFI.TopLevelModuleMap)
9770       // FIXME: This unnecessarily re-reads the InputFileInfo.
9771       if (auto *FE = getInputFile(MF, I + 1).getFile())
9772         Visitor(FE);
9773   }
9774 }
9775 
9776 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9777   // If we know the owning module, use it.
9778   if (Module *M = D->getImportedOwningModule())
9779     return M->getFullModuleName();
9780 
9781   // Otherwise, use the name of the top-level module the decl is within.
9782   if (ModuleFile *M = getOwningModuleFile(D))
9783     return M->ModuleName;
9784 
9785   // Not from a module.
9786   return {};
9787 }
9788 
9789 void ASTReader::finishPendingActions() {
9790   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9791          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9792          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9793          !PendingUpdateRecords.empty()) {
9794     // If any identifiers with corresponding top-level declarations have
9795     // been loaded, load those declarations now.
9796     using TopLevelDeclsMap =
9797         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9798     TopLevelDeclsMap TopLevelDecls;
9799 
9800     while (!PendingIdentifierInfos.empty()) {
9801       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9802       SmallVector<uint32_t, 4> DeclIDs =
9803           std::move(PendingIdentifierInfos.back().second);
9804       PendingIdentifierInfos.pop_back();
9805 
9806       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9807     }
9808 
9809     // Load each function type that we deferred loading because it was a
9810     // deduced type that might refer to a local type declared within itself.
9811     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9812       auto *FD = PendingFunctionTypes[I].first;
9813       FD->setType(GetType(PendingFunctionTypes[I].second));
9814 
9815       // If we gave a function a deduced return type, remember that we need to
9816       // propagate that along the redeclaration chain.
9817       auto *DT = FD->getReturnType()->getContainedDeducedType();
9818       if (DT && DT->isDeduced())
9819         PendingDeducedTypeUpdates.insert(
9820             {FD->getCanonicalDecl(), FD->getReturnType()});
9821     }
9822     PendingFunctionTypes.clear();
9823 
9824     // For each decl chain that we wanted to complete while deserializing, mark
9825     // it as "still needs to be completed".
9826     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9827       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9828     }
9829     PendingIncompleteDeclChains.clear();
9830 
9831     // Load pending declaration chains.
9832     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9833       loadPendingDeclChain(PendingDeclChains[I].first,
9834                            PendingDeclChains[I].second);
9835     PendingDeclChains.clear();
9836 
9837     // Make the most recent of the top-level declarations visible.
9838     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9839            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9840       IdentifierInfo *II = TLD->first;
9841       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9842         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9843       }
9844     }
9845 
9846     // Load any pending macro definitions.
9847     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9848       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9849       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9850       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9851       // Initialize the macro history from chained-PCHs ahead of module imports.
9852       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9853            ++IDIdx) {
9854         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9855         if (!Info.M->isModule())
9856           resolvePendingMacro(II, Info);
9857       }
9858       // Handle module imports.
9859       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9860            ++IDIdx) {
9861         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9862         if (Info.M->isModule())
9863           resolvePendingMacro(II, Info);
9864       }
9865     }
9866     PendingMacroIDs.clear();
9867 
9868     // Wire up the DeclContexts for Decls that we delayed setting until
9869     // recursive loading is completed.
9870     while (!PendingDeclContextInfos.empty()) {
9871       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9872       PendingDeclContextInfos.pop_front();
9873       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9874       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9875       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9876     }
9877 
9878     // Perform any pending declaration updates.
9879     while (!PendingUpdateRecords.empty()) {
9880       auto Update = PendingUpdateRecords.pop_back_val();
9881       ReadingKindTracker ReadingKind(Read_Decl, *this);
9882       loadDeclUpdateRecords(Update);
9883     }
9884   }
9885 
9886   // At this point, all update records for loaded decls are in place, so any
9887   // fake class definitions should have become real.
9888   assert(PendingFakeDefinitionData.empty() &&
9889          "faked up a class definition but never saw the real one");
9890 
9891   // If we deserialized any C++ or Objective-C class definitions, any
9892   // Objective-C protocol definitions, or any redeclarable templates, make sure
9893   // that all redeclarations point to the definitions. Note that this can only
9894   // happen now, after the redeclaration chains have been fully wired.
9895   for (Decl *D : PendingDefinitions) {
9896     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9897       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9898         // Make sure that the TagType points at the definition.
9899         const_cast<TagType*>(TagT)->decl = TD;
9900       }
9901 
9902       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9903         for (auto *R = getMostRecentExistingDecl(RD); R;
9904              R = R->getPreviousDecl()) {
9905           assert((R == D) ==
9906                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9907                  "declaration thinks it's the definition but it isn't");
9908           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9909         }
9910       }
9911 
9912       continue;
9913     }
9914 
9915     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9916       // Make sure that the ObjCInterfaceType points at the definition.
9917       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9918         ->Decl = ID;
9919 
9920       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9921         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9922 
9923       continue;
9924     }
9925 
9926     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9927       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9928         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9929 
9930       continue;
9931     }
9932 
9933     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9934     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9935       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9936   }
9937   PendingDefinitions.clear();
9938 
9939   // Load the bodies of any functions or methods we've encountered. We do
9940   // this now (delayed) so that we can be sure that the declaration chains
9941   // have been fully wired up (hasBody relies on this).
9942   // FIXME: We shouldn't require complete redeclaration chains here.
9943   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9944                                PBEnd = PendingBodies.end();
9945        PB != PBEnd; ++PB) {
9946     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9947       // For a function defined inline within a class template, force the
9948       // canonical definition to be the one inside the canonical definition of
9949       // the template. This ensures that we instantiate from a correct view
9950       // of the template.
9951       //
9952       // Sadly we can't do this more generally: we can't be sure that all
9953       // copies of an arbitrary class definition will have the same members
9954       // defined (eg, some member functions may not be instantiated, and some
9955       // special members may or may not have been implicitly defined).
9956       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9957         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9958           continue;
9959 
9960       // FIXME: Check for =delete/=default?
9961       // FIXME: Complain about ODR violations here?
9962       const FunctionDecl *Defn = nullptr;
9963       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9964         FD->setLazyBody(PB->second);
9965       } else {
9966         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9967         mergeDefinitionVisibility(NonConstDefn, FD);
9968 
9969         if (!FD->isLateTemplateParsed() &&
9970             !NonConstDefn->isLateTemplateParsed() &&
9971             FD->getODRHash() != NonConstDefn->getODRHash()) {
9972           if (!isa<CXXMethodDecl>(FD)) {
9973             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9974           } else if (FD->getLexicalParent()->isFileContext() &&
9975                      NonConstDefn->getLexicalParent()->isFileContext()) {
9976             // Only diagnose out-of-line method definitions.  If they are
9977             // in class definitions, then an error will be generated when
9978             // processing the class bodies.
9979             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9980           }
9981         }
9982       }
9983       continue;
9984     }
9985 
9986     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9987     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9988       MD->setLazyBody(PB->second);
9989   }
9990   PendingBodies.clear();
9991 
9992   // Do some cleanup.
9993   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9994     getContext().deduplicateMergedDefinitonsFor(ND);
9995   PendingMergedDefinitionsToDeduplicate.clear();
9996 }
9997 
9998 void ASTReader::diagnoseOdrViolations() {
9999   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10000       PendingFunctionOdrMergeFailures.empty() &&
10001       PendingEnumOdrMergeFailures.empty())
10002     return;
10003 
10004   // Trigger the import of the full definition of each class that had any
10005   // odr-merging problems, so we can produce better diagnostics for them.
10006   // These updates may in turn find and diagnose some ODR failures, so take
10007   // ownership of the set first.
10008   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10009   PendingOdrMergeFailures.clear();
10010   for (auto &Merge : OdrMergeFailures) {
10011     Merge.first->buildLookup();
10012     Merge.first->decls_begin();
10013     Merge.first->bases_begin();
10014     Merge.first->vbases_begin();
10015     for (auto &RecordPair : Merge.second) {
10016       auto *RD = RecordPair.first;
10017       RD->decls_begin();
10018       RD->bases_begin();
10019       RD->vbases_begin();
10020     }
10021   }
10022 
10023   // Trigger the import of functions.
10024   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10025   PendingFunctionOdrMergeFailures.clear();
10026   for (auto &Merge : FunctionOdrMergeFailures) {
10027     Merge.first->buildLookup();
10028     Merge.first->decls_begin();
10029     Merge.first->getBody();
10030     for (auto &FD : Merge.second) {
10031       FD->buildLookup();
10032       FD->decls_begin();
10033       FD->getBody();
10034     }
10035   }
10036 
10037   // Trigger the import of enums.
10038   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10039   PendingEnumOdrMergeFailures.clear();
10040   for (auto &Merge : EnumOdrMergeFailures) {
10041     Merge.first->decls_begin();
10042     for (auto &Enum : Merge.second) {
10043       Enum->decls_begin();
10044     }
10045   }
10046 
10047   // For each declaration from a merged context, check that the canonical
10048   // definition of that context also contains a declaration of the same
10049   // entity.
10050   //
10051   // Caution: this loop does things that might invalidate iterators into
10052   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10053   while (!PendingOdrMergeChecks.empty()) {
10054     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10055 
10056     // FIXME: Skip over implicit declarations for now. This matters for things
10057     // like implicitly-declared special member functions. This isn't entirely
10058     // correct; we can end up with multiple unmerged declarations of the same
10059     // implicit entity.
10060     if (D->isImplicit())
10061       continue;
10062 
10063     DeclContext *CanonDef = D->getDeclContext();
10064 
10065     bool Found = false;
10066     const Decl *DCanon = D->getCanonicalDecl();
10067 
10068     for (auto RI : D->redecls()) {
10069       if (RI->getLexicalDeclContext() == CanonDef) {
10070         Found = true;
10071         break;
10072       }
10073     }
10074     if (Found)
10075       continue;
10076 
10077     // Quick check failed, time to do the slow thing. Note, we can't just
10078     // look up the name of D in CanonDef here, because the member that is
10079     // in CanonDef might not be found by name lookup (it might have been
10080     // replaced by a more recent declaration in the lookup table), and we
10081     // can't necessarily find it in the redeclaration chain because it might
10082     // be merely mergeable, not redeclarable.
10083     llvm::SmallVector<const NamedDecl*, 4> Candidates;
10084     for (auto *CanonMember : CanonDef->decls()) {
10085       if (CanonMember->getCanonicalDecl() == DCanon) {
10086         // This can happen if the declaration is merely mergeable and not
10087         // actually redeclarable (we looked for redeclarations earlier).
10088         //
10089         // FIXME: We should be able to detect this more efficiently, without
10090         // pulling in all of the members of CanonDef.
10091         Found = true;
10092         break;
10093       }
10094       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10095         if (ND->getDeclName() == D->getDeclName())
10096           Candidates.push_back(ND);
10097     }
10098 
10099     if (!Found) {
10100       // The AST doesn't like TagDecls becoming invalid after they've been
10101       // completed. We only really need to mark FieldDecls as invalid here.
10102       if (!isa<TagDecl>(D))
10103         D->setInvalidDecl();
10104 
10105       // Ensure we don't accidentally recursively enter deserialization while
10106       // we're producing our diagnostic.
10107       Deserializing RecursionGuard(this);
10108 
10109       std::string CanonDefModule =
10110           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10111       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10112         << D << getOwningModuleNameForDiagnostic(D)
10113         << CanonDef << CanonDefModule.empty() << CanonDefModule;
10114 
10115       if (Candidates.empty())
10116         Diag(cast<Decl>(CanonDef)->getLocation(),
10117              diag::note_module_odr_violation_no_possible_decls) << D;
10118       else {
10119         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10120           Diag(Candidates[I]->getLocation(),
10121                diag::note_module_odr_violation_possible_decl)
10122             << Candidates[I];
10123       }
10124 
10125       DiagnosedOdrMergeFailures.insert(CanonDef);
10126     }
10127   }
10128 
10129   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10130       EnumOdrMergeFailures.empty())
10131     return;
10132 
10133   // Ensure we don't accidentally recursively enter deserialization while
10134   // we're producing our diagnostics.
10135   Deserializing RecursionGuard(this);
10136 
10137   // Common code for hashing helpers.
10138   ODRHash Hash;
10139   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10140     Hash.clear();
10141     Hash.AddQualType(Ty);
10142     return Hash.CalculateHash();
10143   };
10144 
10145   auto ComputeODRHash = [&Hash](const Stmt *S) {
10146     assert(S);
10147     Hash.clear();
10148     Hash.AddStmt(S);
10149     return Hash.CalculateHash();
10150   };
10151 
10152   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10153     assert(D);
10154     Hash.clear();
10155     Hash.AddSubDecl(D);
10156     return Hash.CalculateHash();
10157   };
10158 
10159   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10160     Hash.clear();
10161     Hash.AddTemplateArgument(TA);
10162     return Hash.CalculateHash();
10163   };
10164 
10165   auto ComputeTemplateParameterListODRHash =
10166       [&Hash](const TemplateParameterList *TPL) {
10167         assert(TPL);
10168         Hash.clear();
10169         Hash.AddTemplateParameterList(TPL);
10170         return Hash.CalculateHash();
10171       };
10172 
10173   // Issue any pending ODR-failure diagnostics.
10174   for (auto &Merge : OdrMergeFailures) {
10175     // If we've already pointed out a specific problem with this class, don't
10176     // bother issuing a general "something's different" diagnostic.
10177     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10178       continue;
10179 
10180     bool Diagnosed = false;
10181     CXXRecordDecl *FirstRecord = Merge.first;
10182     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10183     for (auto &RecordPair : Merge.second) {
10184       CXXRecordDecl *SecondRecord = RecordPair.first;
10185       // Multiple different declarations got merged together; tell the user
10186       // where they came from.
10187       if (FirstRecord == SecondRecord)
10188         continue;
10189 
10190       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10191 
10192       auto *FirstDD = FirstRecord->DefinitionData;
10193       auto *SecondDD = RecordPair.second;
10194 
10195       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10196 
10197       // Diagnostics from DefinitionData are emitted here.
10198       if (FirstDD != SecondDD) {
10199         enum ODRDefinitionDataDifference {
10200           NumBases,
10201           NumVBases,
10202           BaseType,
10203           BaseVirtual,
10204           BaseAccess,
10205         };
10206         auto ODRDiagError = [FirstRecord, &FirstModule,
10207                              this](SourceLocation Loc, SourceRange Range,
10208                                    ODRDefinitionDataDifference DiffType) {
10209           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10210                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10211                  << DiffType;
10212         };
10213         auto ODRDiagNote = [&SecondModule,
10214                             this](SourceLocation Loc, SourceRange Range,
10215                                   ODRDefinitionDataDifference DiffType) {
10216           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10217                  << SecondModule << Range << DiffType;
10218         };
10219 
10220         unsigned FirstNumBases = FirstDD->NumBases;
10221         unsigned FirstNumVBases = FirstDD->NumVBases;
10222         unsigned SecondNumBases = SecondDD->NumBases;
10223         unsigned SecondNumVBases = SecondDD->NumVBases;
10224 
10225         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10226           unsigned NumBases = DD->NumBases;
10227           if (NumBases == 0) return SourceRange();
10228           auto bases = DD->bases();
10229           return SourceRange(bases[0].getBeginLoc(),
10230                              bases[NumBases - 1].getEndLoc());
10231         };
10232 
10233         if (FirstNumBases != SecondNumBases) {
10234           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10235                        NumBases)
10236               << FirstNumBases;
10237           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10238                       NumBases)
10239               << SecondNumBases;
10240           Diagnosed = true;
10241           break;
10242         }
10243 
10244         if (FirstNumVBases != SecondNumVBases) {
10245           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10246                        NumVBases)
10247               << FirstNumVBases;
10248           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10249                       NumVBases)
10250               << SecondNumVBases;
10251           Diagnosed = true;
10252           break;
10253         }
10254 
10255         auto FirstBases = FirstDD->bases();
10256         auto SecondBases = SecondDD->bases();
10257         unsigned i = 0;
10258         for (i = 0; i < FirstNumBases; ++i) {
10259           auto FirstBase = FirstBases[i];
10260           auto SecondBase = SecondBases[i];
10261           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10262               ComputeQualTypeODRHash(SecondBase.getType())) {
10263             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10264                          BaseType)
10265                 << (i + 1) << FirstBase.getType();
10266             ODRDiagNote(SecondRecord->getLocation(),
10267                         SecondBase.getSourceRange(), BaseType)
10268                 << (i + 1) << SecondBase.getType();
10269             break;
10270           }
10271 
10272           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10273             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10274                          BaseVirtual)
10275                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10276             ODRDiagNote(SecondRecord->getLocation(),
10277                         SecondBase.getSourceRange(), BaseVirtual)
10278                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10279             break;
10280           }
10281 
10282           if (FirstBase.getAccessSpecifierAsWritten() !=
10283               SecondBase.getAccessSpecifierAsWritten()) {
10284             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10285                          BaseAccess)
10286                 << (i + 1) << FirstBase.getType()
10287                 << (int)FirstBase.getAccessSpecifierAsWritten();
10288             ODRDiagNote(SecondRecord->getLocation(),
10289                         SecondBase.getSourceRange(), BaseAccess)
10290                 << (i + 1) << SecondBase.getType()
10291                 << (int)SecondBase.getAccessSpecifierAsWritten();
10292             break;
10293           }
10294         }
10295 
10296         if (i != FirstNumBases) {
10297           Diagnosed = true;
10298           break;
10299         }
10300       }
10301 
10302       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10303 
10304       const ClassTemplateDecl *FirstTemplate =
10305           FirstRecord->getDescribedClassTemplate();
10306       const ClassTemplateDecl *SecondTemplate =
10307           SecondRecord->getDescribedClassTemplate();
10308 
10309       assert(!FirstTemplate == !SecondTemplate &&
10310              "Both pointers should be null or non-null");
10311 
10312       enum ODRTemplateDifference {
10313         ParamEmptyName,
10314         ParamName,
10315         ParamSingleDefaultArgument,
10316         ParamDifferentDefaultArgument,
10317       };
10318 
10319       if (FirstTemplate && SecondTemplate) {
10320         DeclHashes FirstTemplateHashes;
10321         DeclHashes SecondTemplateHashes;
10322 
10323         auto PopulateTemplateParameterHashs =
10324             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10325                                      const ClassTemplateDecl *TD) {
10326               for (auto *D : TD->getTemplateParameters()->asArray()) {
10327                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10328               }
10329             };
10330 
10331         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10332         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10333 
10334         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10335                "Number of template parameters should be equal.");
10336 
10337         auto FirstIt = FirstTemplateHashes.begin();
10338         auto FirstEnd = FirstTemplateHashes.end();
10339         auto SecondIt = SecondTemplateHashes.begin();
10340         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10341           if (FirstIt->second == SecondIt->second)
10342             continue;
10343 
10344           auto ODRDiagError = [FirstRecord, &FirstModule,
10345                                this](SourceLocation Loc, SourceRange Range,
10346                                      ODRTemplateDifference DiffType) {
10347             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10348                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10349                    << DiffType;
10350           };
10351           auto ODRDiagNote = [&SecondModule,
10352                               this](SourceLocation Loc, SourceRange Range,
10353                                     ODRTemplateDifference DiffType) {
10354             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10355                    << SecondModule << Range << DiffType;
10356           };
10357 
10358           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10359           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10360 
10361           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10362                  "Parameter Decl's should be the same kind.");
10363 
10364           DeclarationName FirstName = FirstDecl->getDeclName();
10365           DeclarationName SecondName = SecondDecl->getDeclName();
10366 
10367           if (FirstName != SecondName) {
10368             const bool FirstNameEmpty =
10369                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10370             const bool SecondNameEmpty =
10371                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10372             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10373                    "Both template parameters cannot be unnamed.");
10374             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10375                          FirstNameEmpty ? ParamEmptyName : ParamName)
10376                 << FirstName;
10377             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10378                         SecondNameEmpty ? ParamEmptyName : ParamName)
10379                 << SecondName;
10380             break;
10381           }
10382 
10383           switch (FirstDecl->getKind()) {
10384           default:
10385             llvm_unreachable("Invalid template parameter type.");
10386           case Decl::TemplateTypeParm: {
10387             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10388             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10389             const bool HasFirstDefaultArgument =
10390                 FirstParam->hasDefaultArgument() &&
10391                 !FirstParam->defaultArgumentWasInherited();
10392             const bool HasSecondDefaultArgument =
10393                 SecondParam->hasDefaultArgument() &&
10394                 !SecondParam->defaultArgumentWasInherited();
10395 
10396             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10397               ODRDiagError(FirstDecl->getLocation(),
10398                            FirstDecl->getSourceRange(),
10399                            ParamSingleDefaultArgument)
10400                   << HasFirstDefaultArgument;
10401               ODRDiagNote(SecondDecl->getLocation(),
10402                           SecondDecl->getSourceRange(),
10403                           ParamSingleDefaultArgument)
10404                   << HasSecondDefaultArgument;
10405               break;
10406             }
10407 
10408             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10409                    "Expecting default arguments.");
10410 
10411             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10412                          ParamDifferentDefaultArgument);
10413             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10414                         ParamDifferentDefaultArgument);
10415 
10416             break;
10417           }
10418           case Decl::NonTypeTemplateParm: {
10419             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10420             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10421             const bool HasFirstDefaultArgument =
10422                 FirstParam->hasDefaultArgument() &&
10423                 !FirstParam->defaultArgumentWasInherited();
10424             const bool HasSecondDefaultArgument =
10425                 SecondParam->hasDefaultArgument() &&
10426                 !SecondParam->defaultArgumentWasInherited();
10427 
10428             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10429               ODRDiagError(FirstDecl->getLocation(),
10430                            FirstDecl->getSourceRange(),
10431                            ParamSingleDefaultArgument)
10432                   << HasFirstDefaultArgument;
10433               ODRDiagNote(SecondDecl->getLocation(),
10434                           SecondDecl->getSourceRange(),
10435                           ParamSingleDefaultArgument)
10436                   << HasSecondDefaultArgument;
10437               break;
10438             }
10439 
10440             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10441                    "Expecting default arguments.");
10442 
10443             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10444                          ParamDifferentDefaultArgument);
10445             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10446                         ParamDifferentDefaultArgument);
10447 
10448             break;
10449           }
10450           case Decl::TemplateTemplateParm: {
10451             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10452             const auto *SecondParam =
10453                 cast<TemplateTemplateParmDecl>(SecondDecl);
10454             const bool HasFirstDefaultArgument =
10455                 FirstParam->hasDefaultArgument() &&
10456                 !FirstParam->defaultArgumentWasInherited();
10457             const bool HasSecondDefaultArgument =
10458                 SecondParam->hasDefaultArgument() &&
10459                 !SecondParam->defaultArgumentWasInherited();
10460 
10461             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10462               ODRDiagError(FirstDecl->getLocation(),
10463                            FirstDecl->getSourceRange(),
10464                            ParamSingleDefaultArgument)
10465                   << HasFirstDefaultArgument;
10466               ODRDiagNote(SecondDecl->getLocation(),
10467                           SecondDecl->getSourceRange(),
10468                           ParamSingleDefaultArgument)
10469                   << HasSecondDefaultArgument;
10470               break;
10471             }
10472 
10473             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10474                    "Expecting default arguments.");
10475 
10476             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10477                          ParamDifferentDefaultArgument);
10478             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10479                         ParamDifferentDefaultArgument);
10480 
10481             break;
10482           }
10483           }
10484 
10485           break;
10486         }
10487 
10488         if (FirstIt != FirstEnd) {
10489           Diagnosed = true;
10490           break;
10491         }
10492       }
10493 
10494       DeclHashes FirstHashes;
10495       DeclHashes SecondHashes;
10496 
10497       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10498                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
10499         for (auto *D : Record->decls()) {
10500           // Due to decl merging, the first CXXRecordDecl is the parent of
10501           // Decls in both records.
10502           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10503             continue;
10504           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10505         }
10506       };
10507       PopulateHashes(FirstHashes, FirstRecord);
10508       PopulateHashes(SecondHashes, SecondRecord);
10509 
10510       // Used with err_module_odr_violation_mismatch_decl and
10511       // note_module_odr_violation_mismatch_decl
10512       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10513       enum {
10514         EndOfClass,
10515         PublicSpecifer,
10516         PrivateSpecifer,
10517         ProtectedSpecifer,
10518         StaticAssert,
10519         Field,
10520         CXXMethod,
10521         TypeAlias,
10522         TypeDef,
10523         Var,
10524         Friend,
10525         FunctionTemplate,
10526         Other
10527       } FirstDiffType = Other,
10528         SecondDiffType = Other;
10529 
10530       auto DifferenceSelector = [](Decl *D) {
10531         assert(D && "valid Decl required");
10532         switch (D->getKind()) {
10533         default:
10534           return Other;
10535         case Decl::AccessSpec:
10536           switch (D->getAccess()) {
10537           case AS_public:
10538             return PublicSpecifer;
10539           case AS_private:
10540             return PrivateSpecifer;
10541           case AS_protected:
10542             return ProtectedSpecifer;
10543           case AS_none:
10544             break;
10545           }
10546           llvm_unreachable("Invalid access specifier");
10547         case Decl::StaticAssert:
10548           return StaticAssert;
10549         case Decl::Field:
10550           return Field;
10551         case Decl::CXXMethod:
10552         case Decl::CXXConstructor:
10553         case Decl::CXXDestructor:
10554           return CXXMethod;
10555         case Decl::TypeAlias:
10556           return TypeAlias;
10557         case Decl::Typedef:
10558           return TypeDef;
10559         case Decl::Var:
10560           return Var;
10561         case Decl::Friend:
10562           return Friend;
10563         case Decl::FunctionTemplate:
10564           return FunctionTemplate;
10565         }
10566       };
10567 
10568       Decl *FirstDecl = nullptr;
10569       Decl *SecondDecl = nullptr;
10570       auto FirstIt = FirstHashes.begin();
10571       auto SecondIt = SecondHashes.begin();
10572 
10573       // If there is a diagnoseable difference, FirstDiffType and
10574       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10575       // filled in if not EndOfClass.
10576       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10577         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10578             FirstIt->second == SecondIt->second) {
10579           ++FirstIt;
10580           ++SecondIt;
10581           continue;
10582         }
10583 
10584         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10585         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10586 
10587         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10588         SecondDiffType =
10589             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10590 
10591         break;
10592       }
10593 
10594       if (FirstDiffType == Other || SecondDiffType == Other) {
10595         // Reaching this point means an unexpected Decl was encountered
10596         // or no difference was detected.  This causes a generic error
10597         // message to be emitted.
10598         Diag(FirstRecord->getLocation(),
10599              diag::err_module_odr_violation_different_definitions)
10600             << FirstRecord << FirstModule.empty() << FirstModule;
10601 
10602         if (FirstDecl) {
10603           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10604               << FirstRecord << FirstDecl->getSourceRange();
10605         }
10606 
10607         Diag(SecondRecord->getLocation(),
10608              diag::note_module_odr_violation_different_definitions)
10609             << SecondModule;
10610 
10611         if (SecondDecl) {
10612           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10613               << SecondDecl->getSourceRange();
10614         }
10615 
10616         Diagnosed = true;
10617         break;
10618       }
10619 
10620       if (FirstDiffType != SecondDiffType) {
10621         SourceLocation FirstLoc;
10622         SourceRange FirstRange;
10623         if (FirstDiffType == EndOfClass) {
10624           FirstLoc = FirstRecord->getBraceRange().getEnd();
10625         } else {
10626           FirstLoc = FirstIt->first->getLocation();
10627           FirstRange = FirstIt->first->getSourceRange();
10628         }
10629         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10630             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10631             << FirstDiffType;
10632 
10633         SourceLocation SecondLoc;
10634         SourceRange SecondRange;
10635         if (SecondDiffType == EndOfClass) {
10636           SecondLoc = SecondRecord->getBraceRange().getEnd();
10637         } else {
10638           SecondLoc = SecondDecl->getLocation();
10639           SecondRange = SecondDecl->getSourceRange();
10640         }
10641         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10642             << SecondModule << SecondRange << SecondDiffType;
10643         Diagnosed = true;
10644         break;
10645       }
10646 
10647       assert(FirstDiffType == SecondDiffType);
10648 
10649       // Used with err_module_odr_violation_mismatch_decl_diff and
10650       // note_module_odr_violation_mismatch_decl_diff
10651       enum ODRDeclDifference {
10652         StaticAssertCondition,
10653         StaticAssertMessage,
10654         StaticAssertOnlyMessage,
10655         FieldName,
10656         FieldTypeName,
10657         FieldSingleBitField,
10658         FieldDifferentWidthBitField,
10659         FieldSingleMutable,
10660         FieldSingleInitializer,
10661         FieldDifferentInitializers,
10662         MethodName,
10663         MethodDeleted,
10664         MethodDefaulted,
10665         MethodVirtual,
10666         MethodStatic,
10667         MethodVolatile,
10668         MethodConst,
10669         MethodInline,
10670         MethodNumberParameters,
10671         MethodParameterType,
10672         MethodParameterName,
10673         MethodParameterSingleDefaultArgument,
10674         MethodParameterDifferentDefaultArgument,
10675         MethodNoTemplateArguments,
10676         MethodDifferentNumberTemplateArguments,
10677         MethodDifferentTemplateArgument,
10678         MethodSingleBody,
10679         MethodDifferentBody,
10680         TypedefName,
10681         TypedefType,
10682         VarName,
10683         VarType,
10684         VarSingleInitializer,
10685         VarDifferentInitializer,
10686         VarConstexpr,
10687         FriendTypeFunction,
10688         FriendType,
10689         FriendFunction,
10690         FunctionTemplateDifferentNumberParameters,
10691         FunctionTemplateParameterDifferentKind,
10692         FunctionTemplateParameterName,
10693         FunctionTemplateParameterSingleDefaultArgument,
10694         FunctionTemplateParameterDifferentDefaultArgument,
10695         FunctionTemplateParameterDifferentType,
10696         FunctionTemplatePackParameter,
10697       };
10698 
10699       // These lambdas have the common portions of the ODR diagnostics.  This
10700       // has the same return as Diag(), so addition parameters can be passed
10701       // in with operator<<
10702       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10703           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10704         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10705                << FirstRecord << FirstModule.empty() << FirstModule << Range
10706                << DiffType;
10707       };
10708       auto ODRDiagNote = [&SecondModule, this](
10709           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10710         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10711                << SecondModule << Range << DiffType;
10712       };
10713 
10714       switch (FirstDiffType) {
10715       case Other:
10716       case EndOfClass:
10717       case PublicSpecifer:
10718       case PrivateSpecifer:
10719       case ProtectedSpecifer:
10720         llvm_unreachable("Invalid diff type");
10721 
10722       case StaticAssert: {
10723         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10724         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10725 
10726         Expr *FirstExpr = FirstSA->getAssertExpr();
10727         Expr *SecondExpr = SecondSA->getAssertExpr();
10728         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10729         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10730         if (FirstODRHash != SecondODRHash) {
10731           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10732                        StaticAssertCondition);
10733           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10734                       StaticAssertCondition);
10735           Diagnosed = true;
10736           break;
10737         }
10738 
10739         StringLiteral *FirstStr = FirstSA->getMessage();
10740         StringLiteral *SecondStr = SecondSA->getMessage();
10741         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10742         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10743           SourceLocation FirstLoc, SecondLoc;
10744           SourceRange FirstRange, SecondRange;
10745           if (FirstStr) {
10746             FirstLoc = FirstStr->getBeginLoc();
10747             FirstRange = FirstStr->getSourceRange();
10748           } else {
10749             FirstLoc = FirstSA->getBeginLoc();
10750             FirstRange = FirstSA->getSourceRange();
10751           }
10752           if (SecondStr) {
10753             SecondLoc = SecondStr->getBeginLoc();
10754             SecondRange = SecondStr->getSourceRange();
10755           } else {
10756             SecondLoc = SecondSA->getBeginLoc();
10757             SecondRange = SecondSA->getSourceRange();
10758           }
10759           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10760               << (FirstStr == nullptr);
10761           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10762               << (SecondStr == nullptr);
10763           Diagnosed = true;
10764           break;
10765         }
10766 
10767         if (FirstStr && SecondStr &&
10768             FirstStr->getString() != SecondStr->getString()) {
10769           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10770                        StaticAssertMessage);
10771           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10772                       StaticAssertMessage);
10773           Diagnosed = true;
10774           break;
10775         }
10776         break;
10777       }
10778       case Field: {
10779         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10780         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10781         IdentifierInfo *FirstII = FirstField->getIdentifier();
10782         IdentifierInfo *SecondII = SecondField->getIdentifier();
10783         if (FirstII->getName() != SecondII->getName()) {
10784           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10785                        FieldName)
10786               << FirstII;
10787           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10788                       FieldName)
10789               << SecondII;
10790 
10791           Diagnosed = true;
10792           break;
10793         }
10794 
10795         assert(getContext().hasSameType(FirstField->getType(),
10796                                         SecondField->getType()));
10797 
10798         QualType FirstType = FirstField->getType();
10799         QualType SecondType = SecondField->getType();
10800         if (ComputeQualTypeODRHash(FirstType) !=
10801             ComputeQualTypeODRHash(SecondType)) {
10802           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10803                        FieldTypeName)
10804               << FirstII << FirstType;
10805           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10806                       FieldTypeName)
10807               << SecondII << SecondType;
10808 
10809           Diagnosed = true;
10810           break;
10811         }
10812 
10813         const bool IsFirstBitField = FirstField->isBitField();
10814         const bool IsSecondBitField = SecondField->isBitField();
10815         if (IsFirstBitField != IsSecondBitField) {
10816           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10817                        FieldSingleBitField)
10818               << FirstII << IsFirstBitField;
10819           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10820                       FieldSingleBitField)
10821               << SecondII << IsSecondBitField;
10822           Diagnosed = true;
10823           break;
10824         }
10825 
10826         if (IsFirstBitField && IsSecondBitField) {
10827           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10828                        FieldDifferentWidthBitField)
10829               << FirstII << FirstField->getBitWidth()->getSourceRange();
10830           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10831                       FieldDifferentWidthBitField)
10832               << SecondII << SecondField->getBitWidth()->getSourceRange();
10833           Diagnosed = true;
10834           break;
10835         }
10836 
10837         const bool IsFirstMutable = FirstField->isMutable();
10838         const bool IsSecondMutable = SecondField->isMutable();
10839         if (IsFirstMutable != IsSecondMutable) {
10840           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10841                        FieldSingleMutable)
10842               << FirstII << IsFirstMutable;
10843           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10844                       FieldSingleMutable)
10845               << SecondII << IsSecondMutable;
10846           Diagnosed = true;
10847           break;
10848         }
10849 
10850         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10851         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10852         if ((!FirstInitializer && SecondInitializer) ||
10853             (FirstInitializer && !SecondInitializer)) {
10854           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10855                        FieldSingleInitializer)
10856               << FirstII << (FirstInitializer != nullptr);
10857           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10858                       FieldSingleInitializer)
10859               << SecondII << (SecondInitializer != nullptr);
10860           Diagnosed = true;
10861           break;
10862         }
10863 
10864         if (FirstInitializer && SecondInitializer) {
10865           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10866           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10867           if (FirstInitHash != SecondInitHash) {
10868             ODRDiagError(FirstField->getLocation(),
10869                          FirstField->getSourceRange(),
10870                          FieldDifferentInitializers)
10871                 << FirstII << FirstInitializer->getSourceRange();
10872             ODRDiagNote(SecondField->getLocation(),
10873                         SecondField->getSourceRange(),
10874                         FieldDifferentInitializers)
10875                 << SecondII << SecondInitializer->getSourceRange();
10876             Diagnosed = true;
10877             break;
10878           }
10879         }
10880 
10881         break;
10882       }
10883       case CXXMethod: {
10884         enum {
10885           DiagMethod,
10886           DiagConstructor,
10887           DiagDestructor,
10888         } FirstMethodType,
10889             SecondMethodType;
10890         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10891           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10892           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10893           return DiagMethod;
10894         };
10895         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10896         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10897         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10898         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10899         auto FirstName = FirstMethod->getDeclName();
10900         auto SecondName = SecondMethod->getDeclName();
10901         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10902           ODRDiagError(FirstMethod->getLocation(),
10903                        FirstMethod->getSourceRange(), MethodName)
10904               << FirstMethodType << FirstName;
10905           ODRDiagNote(SecondMethod->getLocation(),
10906                       SecondMethod->getSourceRange(), MethodName)
10907               << SecondMethodType << SecondName;
10908 
10909           Diagnosed = true;
10910           break;
10911         }
10912 
10913         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10914         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10915         if (FirstDeleted != SecondDeleted) {
10916           ODRDiagError(FirstMethod->getLocation(),
10917                        FirstMethod->getSourceRange(), MethodDeleted)
10918               << FirstMethodType << FirstName << FirstDeleted;
10919 
10920           ODRDiagNote(SecondMethod->getLocation(),
10921                       SecondMethod->getSourceRange(), MethodDeleted)
10922               << SecondMethodType << SecondName << SecondDeleted;
10923           Diagnosed = true;
10924           break;
10925         }
10926 
10927         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10928         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10929         if (FirstDefaulted != SecondDefaulted) {
10930           ODRDiagError(FirstMethod->getLocation(),
10931                        FirstMethod->getSourceRange(), MethodDefaulted)
10932               << FirstMethodType << FirstName << FirstDefaulted;
10933 
10934           ODRDiagNote(SecondMethod->getLocation(),
10935                       SecondMethod->getSourceRange(), MethodDefaulted)
10936               << SecondMethodType << SecondName << SecondDefaulted;
10937           Diagnosed = true;
10938           break;
10939         }
10940 
10941         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10942         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10943         const bool FirstPure = FirstMethod->isPure();
10944         const bool SecondPure = SecondMethod->isPure();
10945         if ((FirstVirtual || SecondVirtual) &&
10946             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10947           ODRDiagError(FirstMethod->getLocation(),
10948                        FirstMethod->getSourceRange(), MethodVirtual)
10949               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10950           ODRDiagNote(SecondMethod->getLocation(),
10951                       SecondMethod->getSourceRange(), MethodVirtual)
10952               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10953           Diagnosed = true;
10954           break;
10955         }
10956 
10957         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10958         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10959         // class needs to be checked instead.
10960         const auto FirstStorage = FirstMethod->getStorageClass();
10961         const auto SecondStorage = SecondMethod->getStorageClass();
10962         const bool FirstStatic = FirstStorage == SC_Static;
10963         const bool SecondStatic = SecondStorage == SC_Static;
10964         if (FirstStatic != SecondStatic) {
10965           ODRDiagError(FirstMethod->getLocation(),
10966                        FirstMethod->getSourceRange(), MethodStatic)
10967               << FirstMethodType << FirstName << FirstStatic;
10968           ODRDiagNote(SecondMethod->getLocation(),
10969                       SecondMethod->getSourceRange(), MethodStatic)
10970               << SecondMethodType << SecondName << SecondStatic;
10971           Diagnosed = true;
10972           break;
10973         }
10974 
10975         const bool FirstVolatile = FirstMethod->isVolatile();
10976         const bool SecondVolatile = SecondMethod->isVolatile();
10977         if (FirstVolatile != SecondVolatile) {
10978           ODRDiagError(FirstMethod->getLocation(),
10979                        FirstMethod->getSourceRange(), MethodVolatile)
10980               << FirstMethodType << FirstName << FirstVolatile;
10981           ODRDiagNote(SecondMethod->getLocation(),
10982                       SecondMethod->getSourceRange(), MethodVolatile)
10983               << SecondMethodType << SecondName << SecondVolatile;
10984           Diagnosed = true;
10985           break;
10986         }
10987 
10988         const bool FirstConst = FirstMethod->isConst();
10989         const bool SecondConst = SecondMethod->isConst();
10990         if (FirstConst != SecondConst) {
10991           ODRDiagError(FirstMethod->getLocation(),
10992                        FirstMethod->getSourceRange(), MethodConst)
10993               << FirstMethodType << FirstName << FirstConst;
10994           ODRDiagNote(SecondMethod->getLocation(),
10995                       SecondMethod->getSourceRange(), MethodConst)
10996               << SecondMethodType << SecondName << SecondConst;
10997           Diagnosed = true;
10998           break;
10999         }
11000 
11001         const bool FirstInline = FirstMethod->isInlineSpecified();
11002         const bool SecondInline = SecondMethod->isInlineSpecified();
11003         if (FirstInline != SecondInline) {
11004           ODRDiagError(FirstMethod->getLocation(),
11005                        FirstMethod->getSourceRange(), MethodInline)
11006               << FirstMethodType << FirstName << FirstInline;
11007           ODRDiagNote(SecondMethod->getLocation(),
11008                       SecondMethod->getSourceRange(), MethodInline)
11009               << SecondMethodType << SecondName << SecondInline;
11010           Diagnosed = true;
11011           break;
11012         }
11013 
11014         const unsigned FirstNumParameters = FirstMethod->param_size();
11015         const unsigned SecondNumParameters = SecondMethod->param_size();
11016         if (FirstNumParameters != SecondNumParameters) {
11017           ODRDiagError(FirstMethod->getLocation(),
11018                        FirstMethod->getSourceRange(), MethodNumberParameters)
11019               << FirstMethodType << FirstName << FirstNumParameters;
11020           ODRDiagNote(SecondMethod->getLocation(),
11021                       SecondMethod->getSourceRange(), MethodNumberParameters)
11022               << SecondMethodType << SecondName << SecondNumParameters;
11023           Diagnosed = true;
11024           break;
11025         }
11026 
11027         // Need this status boolean to know when break out of the switch.
11028         bool ParameterMismatch = false;
11029         for (unsigned I = 0; I < FirstNumParameters; ++I) {
11030           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11031           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11032 
11033           QualType FirstParamType = FirstParam->getType();
11034           QualType SecondParamType = SecondParam->getType();
11035           if (FirstParamType != SecondParamType &&
11036               ComputeQualTypeODRHash(FirstParamType) !=
11037                   ComputeQualTypeODRHash(SecondParamType)) {
11038             if (const DecayedType *ParamDecayedType =
11039                     FirstParamType->getAs<DecayedType>()) {
11040               ODRDiagError(FirstMethod->getLocation(),
11041                            FirstMethod->getSourceRange(), MethodParameterType)
11042                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11043                   << true << ParamDecayedType->getOriginalType();
11044             } else {
11045               ODRDiagError(FirstMethod->getLocation(),
11046                            FirstMethod->getSourceRange(), MethodParameterType)
11047                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11048                   << false;
11049             }
11050 
11051             if (const DecayedType *ParamDecayedType =
11052                     SecondParamType->getAs<DecayedType>()) {
11053               ODRDiagNote(SecondMethod->getLocation(),
11054                           SecondMethod->getSourceRange(), MethodParameterType)
11055                   << SecondMethodType << SecondName << (I + 1)
11056                   << SecondParamType << true
11057                   << ParamDecayedType->getOriginalType();
11058             } else {
11059               ODRDiagNote(SecondMethod->getLocation(),
11060                           SecondMethod->getSourceRange(), MethodParameterType)
11061                   << SecondMethodType << SecondName << (I + 1)
11062                   << SecondParamType << false;
11063             }
11064             ParameterMismatch = true;
11065             break;
11066           }
11067 
11068           DeclarationName FirstParamName = FirstParam->getDeclName();
11069           DeclarationName SecondParamName = SecondParam->getDeclName();
11070           if (FirstParamName != SecondParamName) {
11071             ODRDiagError(FirstMethod->getLocation(),
11072                          FirstMethod->getSourceRange(), MethodParameterName)
11073                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11074             ODRDiagNote(SecondMethod->getLocation(),
11075                         SecondMethod->getSourceRange(), MethodParameterName)
11076                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11077             ParameterMismatch = true;
11078             break;
11079           }
11080 
11081           const Expr *FirstInit = FirstParam->getInit();
11082           const Expr *SecondInit = SecondParam->getInit();
11083           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11084             ODRDiagError(FirstMethod->getLocation(),
11085                          FirstMethod->getSourceRange(),
11086                          MethodParameterSingleDefaultArgument)
11087                 << FirstMethodType << FirstName << (I + 1)
11088                 << (FirstInit == nullptr)
11089                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11090             ODRDiagNote(SecondMethod->getLocation(),
11091                         SecondMethod->getSourceRange(),
11092                         MethodParameterSingleDefaultArgument)
11093                 << SecondMethodType << SecondName << (I + 1)
11094                 << (SecondInit == nullptr)
11095                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11096             ParameterMismatch = true;
11097             break;
11098           }
11099 
11100           if (FirstInit && SecondInit &&
11101               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11102             ODRDiagError(FirstMethod->getLocation(),
11103                          FirstMethod->getSourceRange(),
11104                          MethodParameterDifferentDefaultArgument)
11105                 << FirstMethodType << FirstName << (I + 1)
11106                 << FirstInit->getSourceRange();
11107             ODRDiagNote(SecondMethod->getLocation(),
11108                         SecondMethod->getSourceRange(),
11109                         MethodParameterDifferentDefaultArgument)
11110                 << SecondMethodType << SecondName << (I + 1)
11111                 << SecondInit->getSourceRange();
11112             ParameterMismatch = true;
11113             break;
11114 
11115           }
11116         }
11117 
11118         if (ParameterMismatch) {
11119           Diagnosed = true;
11120           break;
11121         }
11122 
11123         const auto *FirstTemplateArgs =
11124             FirstMethod->getTemplateSpecializationArgs();
11125         const auto *SecondTemplateArgs =
11126             SecondMethod->getTemplateSpecializationArgs();
11127 
11128         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11129             (!FirstTemplateArgs && SecondTemplateArgs)) {
11130           ODRDiagError(FirstMethod->getLocation(),
11131                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11132               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11133           ODRDiagNote(SecondMethod->getLocation(),
11134                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11135               << SecondMethodType << SecondName
11136               << (SecondTemplateArgs != nullptr);
11137 
11138           Diagnosed = true;
11139           break;
11140         }
11141 
11142         if (FirstTemplateArgs && SecondTemplateArgs) {
11143           // Remove pack expansions from argument list.
11144           auto ExpandTemplateArgumentList =
11145               [](const TemplateArgumentList *TAL) {
11146                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11147                 for (const TemplateArgument &TA : TAL->asArray()) {
11148                   if (TA.getKind() != TemplateArgument::Pack) {
11149                     ExpandedList.push_back(&TA);
11150                     continue;
11151                   }
11152                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11153                     ExpandedList.push_back(&PackTA);
11154                   }
11155                 }
11156                 return ExpandedList;
11157               };
11158           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11159               ExpandTemplateArgumentList(FirstTemplateArgs);
11160           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11161               ExpandTemplateArgumentList(SecondTemplateArgs);
11162 
11163           if (FirstExpandedList.size() != SecondExpandedList.size()) {
11164             ODRDiagError(FirstMethod->getLocation(),
11165                          FirstMethod->getSourceRange(),
11166                          MethodDifferentNumberTemplateArguments)
11167                 << FirstMethodType << FirstName
11168                 << (unsigned)FirstExpandedList.size();
11169             ODRDiagNote(SecondMethod->getLocation(),
11170                         SecondMethod->getSourceRange(),
11171                         MethodDifferentNumberTemplateArguments)
11172                 << SecondMethodType << SecondName
11173                 << (unsigned)SecondExpandedList.size();
11174 
11175             Diagnosed = true;
11176             break;
11177           }
11178 
11179           bool TemplateArgumentMismatch = false;
11180           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11181             const TemplateArgument &FirstTA = *FirstExpandedList[i],
11182                                    &SecondTA = *SecondExpandedList[i];
11183             if (ComputeTemplateArgumentODRHash(FirstTA) ==
11184                 ComputeTemplateArgumentODRHash(SecondTA)) {
11185               continue;
11186             }
11187 
11188             ODRDiagError(FirstMethod->getLocation(),
11189                          FirstMethod->getSourceRange(),
11190                          MethodDifferentTemplateArgument)
11191                 << FirstMethodType << FirstName << FirstTA << i + 1;
11192             ODRDiagNote(SecondMethod->getLocation(),
11193                         SecondMethod->getSourceRange(),
11194                         MethodDifferentTemplateArgument)
11195                 << SecondMethodType << SecondName << SecondTA << i + 1;
11196 
11197             TemplateArgumentMismatch = true;
11198             break;
11199           }
11200 
11201           if (TemplateArgumentMismatch) {
11202             Diagnosed = true;
11203             break;
11204           }
11205         }
11206 
11207         // Compute the hash of the method as if it has no body.
11208         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11209           Hash.clear();
11210           Hash.AddFunctionDecl(D, true /*SkipBody*/);
11211           return Hash.CalculateHash();
11212         };
11213 
11214         // Compare the hash generated to the hash stored.  A difference means
11215         // that a body was present in the original source.  Due to merging,
11216         // the stardard way of detecting a body will not work.
11217         const bool HasFirstBody =
11218             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11219         const bool HasSecondBody =
11220             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11221 
11222         if (HasFirstBody != HasSecondBody) {
11223           ODRDiagError(FirstMethod->getLocation(),
11224                        FirstMethod->getSourceRange(), MethodSingleBody)
11225               << FirstMethodType << FirstName << HasFirstBody;
11226           ODRDiagNote(SecondMethod->getLocation(),
11227                       SecondMethod->getSourceRange(), MethodSingleBody)
11228               << SecondMethodType << SecondName << HasSecondBody;
11229           Diagnosed = true;
11230           break;
11231         }
11232 
11233         if (HasFirstBody && HasSecondBody) {
11234           ODRDiagError(FirstMethod->getLocation(),
11235                        FirstMethod->getSourceRange(), MethodDifferentBody)
11236               << FirstMethodType << FirstName;
11237           ODRDiagNote(SecondMethod->getLocation(),
11238                       SecondMethod->getSourceRange(), MethodDifferentBody)
11239               << SecondMethodType << SecondName;
11240           Diagnosed = true;
11241           break;
11242         }
11243 
11244         break;
11245       }
11246       case TypeAlias:
11247       case TypeDef: {
11248         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11249         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11250         auto FirstName = FirstTD->getDeclName();
11251         auto SecondName = SecondTD->getDeclName();
11252         if (FirstName != SecondName) {
11253           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11254                        TypedefName)
11255               << (FirstDiffType == TypeAlias) << FirstName;
11256           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11257                       TypedefName)
11258               << (FirstDiffType == TypeAlias) << SecondName;
11259           Diagnosed = true;
11260           break;
11261         }
11262 
11263         QualType FirstType = FirstTD->getUnderlyingType();
11264         QualType SecondType = SecondTD->getUnderlyingType();
11265         if (ComputeQualTypeODRHash(FirstType) !=
11266             ComputeQualTypeODRHash(SecondType)) {
11267           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11268                        TypedefType)
11269               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11270           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11271                       TypedefType)
11272               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11273           Diagnosed = true;
11274           break;
11275         }
11276         break;
11277       }
11278       case Var: {
11279         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11280         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11281         auto FirstName = FirstVD->getDeclName();
11282         auto SecondName = SecondVD->getDeclName();
11283         if (FirstName != SecondName) {
11284           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11285                        VarName)
11286               << FirstName;
11287           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11288                       VarName)
11289               << SecondName;
11290           Diagnosed = true;
11291           break;
11292         }
11293 
11294         QualType FirstType = FirstVD->getType();
11295         QualType SecondType = SecondVD->getType();
11296         if (ComputeQualTypeODRHash(FirstType) !=
11297                         ComputeQualTypeODRHash(SecondType)) {
11298           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11299                        VarType)
11300               << FirstName << FirstType;
11301           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11302                       VarType)
11303               << SecondName << SecondType;
11304           Diagnosed = true;
11305           break;
11306         }
11307 
11308         const Expr *FirstInit = FirstVD->getInit();
11309         const Expr *SecondInit = SecondVD->getInit();
11310         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11311           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11312                        VarSingleInitializer)
11313               << FirstName << (FirstInit == nullptr)
11314               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11315           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11316                       VarSingleInitializer)
11317               << SecondName << (SecondInit == nullptr)
11318               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11319           Diagnosed = true;
11320           break;
11321         }
11322 
11323         if (FirstInit && SecondInit &&
11324             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11325           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11326                        VarDifferentInitializer)
11327               << FirstName << FirstInit->getSourceRange();
11328           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11329                       VarDifferentInitializer)
11330               << SecondName << SecondInit->getSourceRange();
11331           Diagnosed = true;
11332           break;
11333         }
11334 
11335         const bool FirstIsConstexpr = FirstVD->isConstexpr();
11336         const bool SecondIsConstexpr = SecondVD->isConstexpr();
11337         if (FirstIsConstexpr != SecondIsConstexpr) {
11338           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11339                        VarConstexpr)
11340               << FirstName << FirstIsConstexpr;
11341           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11342                       VarConstexpr)
11343               << SecondName << SecondIsConstexpr;
11344           Diagnosed = true;
11345           break;
11346         }
11347         break;
11348       }
11349       case Friend: {
11350         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11351         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11352 
11353         NamedDecl *FirstND = FirstFriend->getFriendDecl();
11354         NamedDecl *SecondND = SecondFriend->getFriendDecl();
11355 
11356         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11357         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11358 
11359         if (FirstND && SecondND) {
11360           ODRDiagError(FirstFriend->getFriendLoc(),
11361                        FirstFriend->getSourceRange(), FriendFunction)
11362               << FirstND;
11363           ODRDiagNote(SecondFriend->getFriendLoc(),
11364                       SecondFriend->getSourceRange(), FriendFunction)
11365               << SecondND;
11366 
11367           Diagnosed = true;
11368           break;
11369         }
11370 
11371         if (FirstTSI && SecondTSI) {
11372           QualType FirstFriendType = FirstTSI->getType();
11373           QualType SecondFriendType = SecondTSI->getType();
11374           assert(ComputeQualTypeODRHash(FirstFriendType) !=
11375                  ComputeQualTypeODRHash(SecondFriendType));
11376           ODRDiagError(FirstFriend->getFriendLoc(),
11377                        FirstFriend->getSourceRange(), FriendType)
11378               << FirstFriendType;
11379           ODRDiagNote(SecondFriend->getFriendLoc(),
11380                       SecondFriend->getSourceRange(), FriendType)
11381               << SecondFriendType;
11382           Diagnosed = true;
11383           break;
11384         }
11385 
11386         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11387                      FriendTypeFunction)
11388             << (FirstTSI == nullptr);
11389         ODRDiagNote(SecondFriend->getFriendLoc(),
11390                     SecondFriend->getSourceRange(), FriendTypeFunction)
11391             << (SecondTSI == nullptr);
11392 
11393         Diagnosed = true;
11394         break;
11395       }
11396       case FunctionTemplate: {
11397         FunctionTemplateDecl *FirstTemplate =
11398             cast<FunctionTemplateDecl>(FirstDecl);
11399         FunctionTemplateDecl *SecondTemplate =
11400             cast<FunctionTemplateDecl>(SecondDecl);
11401 
11402         TemplateParameterList *FirstTPL =
11403             FirstTemplate->getTemplateParameters();
11404         TemplateParameterList *SecondTPL =
11405             SecondTemplate->getTemplateParameters();
11406 
11407         if (FirstTPL->size() != SecondTPL->size()) {
11408           ODRDiagError(FirstTemplate->getLocation(),
11409                        FirstTemplate->getSourceRange(),
11410                        FunctionTemplateDifferentNumberParameters)
11411               << FirstTemplate << FirstTPL->size();
11412           ODRDiagNote(SecondTemplate->getLocation(),
11413                       SecondTemplate->getSourceRange(),
11414                       FunctionTemplateDifferentNumberParameters)
11415               << SecondTemplate  << SecondTPL->size();
11416 
11417           Diagnosed = true;
11418           break;
11419         }
11420 
11421         bool ParameterMismatch = false;
11422         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11423           NamedDecl *FirstParam = FirstTPL->getParam(i);
11424           NamedDecl *SecondParam = SecondTPL->getParam(i);
11425 
11426           if (FirstParam->getKind() != SecondParam->getKind()) {
11427             enum {
11428               TemplateTypeParameter,
11429               NonTypeTemplateParameter,
11430               TemplateTemplateParameter,
11431             };
11432             auto GetParamType = [](NamedDecl *D) {
11433               switch (D->getKind()) {
11434                 default:
11435                   llvm_unreachable("Unexpected template parameter type");
11436                 case Decl::TemplateTypeParm:
11437                   return TemplateTypeParameter;
11438                 case Decl::NonTypeTemplateParm:
11439                   return NonTypeTemplateParameter;
11440                 case Decl::TemplateTemplateParm:
11441                   return TemplateTemplateParameter;
11442               }
11443             };
11444 
11445             ODRDiagError(FirstTemplate->getLocation(),
11446                          FirstTemplate->getSourceRange(),
11447                          FunctionTemplateParameterDifferentKind)
11448                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11449             ODRDiagNote(SecondTemplate->getLocation(),
11450                         SecondTemplate->getSourceRange(),
11451                         FunctionTemplateParameterDifferentKind)
11452                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11453 
11454             ParameterMismatch = true;
11455             break;
11456           }
11457 
11458           if (FirstParam->getName() != SecondParam->getName()) {
11459             ODRDiagError(FirstTemplate->getLocation(),
11460                          FirstTemplate->getSourceRange(),
11461                          FunctionTemplateParameterName)
11462                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11463                 << FirstParam;
11464             ODRDiagNote(SecondTemplate->getLocation(),
11465                         SecondTemplate->getSourceRange(),
11466                         FunctionTemplateParameterName)
11467                 << SecondTemplate << (i + 1)
11468                 << (bool)SecondParam->getIdentifier() << SecondParam;
11469             ParameterMismatch = true;
11470             break;
11471           }
11472 
11473           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11474               isa<TemplateTypeParmDecl>(SecondParam)) {
11475             TemplateTypeParmDecl *FirstTTPD =
11476                 cast<TemplateTypeParmDecl>(FirstParam);
11477             TemplateTypeParmDecl *SecondTTPD =
11478                 cast<TemplateTypeParmDecl>(SecondParam);
11479             bool HasFirstDefaultArgument =
11480                 FirstTTPD->hasDefaultArgument() &&
11481                 !FirstTTPD->defaultArgumentWasInherited();
11482             bool HasSecondDefaultArgument =
11483                 SecondTTPD->hasDefaultArgument() &&
11484                 !SecondTTPD->defaultArgumentWasInherited();
11485             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11486               ODRDiagError(FirstTemplate->getLocation(),
11487                            FirstTemplate->getSourceRange(),
11488                            FunctionTemplateParameterSingleDefaultArgument)
11489                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11490               ODRDiagNote(SecondTemplate->getLocation(),
11491                           SecondTemplate->getSourceRange(),
11492                           FunctionTemplateParameterSingleDefaultArgument)
11493                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11494               ParameterMismatch = true;
11495               break;
11496             }
11497 
11498             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11499               QualType FirstType = FirstTTPD->getDefaultArgument();
11500               QualType SecondType = SecondTTPD->getDefaultArgument();
11501               if (ComputeQualTypeODRHash(FirstType) !=
11502                   ComputeQualTypeODRHash(SecondType)) {
11503                 ODRDiagError(FirstTemplate->getLocation(),
11504                              FirstTemplate->getSourceRange(),
11505                              FunctionTemplateParameterDifferentDefaultArgument)
11506                     << FirstTemplate << (i + 1) << FirstType;
11507                 ODRDiagNote(SecondTemplate->getLocation(),
11508                             SecondTemplate->getSourceRange(),
11509                             FunctionTemplateParameterDifferentDefaultArgument)
11510                     << SecondTemplate << (i + 1) << SecondType;
11511                 ParameterMismatch = true;
11512                 break;
11513               }
11514             }
11515 
11516             if (FirstTTPD->isParameterPack() !=
11517                 SecondTTPD->isParameterPack()) {
11518               ODRDiagError(FirstTemplate->getLocation(),
11519                            FirstTemplate->getSourceRange(),
11520                            FunctionTemplatePackParameter)
11521                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11522               ODRDiagNote(SecondTemplate->getLocation(),
11523                           SecondTemplate->getSourceRange(),
11524                           FunctionTemplatePackParameter)
11525                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11526               ParameterMismatch = true;
11527               break;
11528             }
11529           }
11530 
11531           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11532               isa<TemplateTemplateParmDecl>(SecondParam)) {
11533             TemplateTemplateParmDecl *FirstTTPD =
11534                 cast<TemplateTemplateParmDecl>(FirstParam);
11535             TemplateTemplateParmDecl *SecondTTPD =
11536                 cast<TemplateTemplateParmDecl>(SecondParam);
11537 
11538             TemplateParameterList *FirstTPL =
11539                 FirstTTPD->getTemplateParameters();
11540             TemplateParameterList *SecondTPL =
11541                 SecondTTPD->getTemplateParameters();
11542 
11543             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11544                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11545               ODRDiagError(FirstTemplate->getLocation(),
11546                            FirstTemplate->getSourceRange(),
11547                            FunctionTemplateParameterDifferentType)
11548                   << FirstTemplate << (i + 1);
11549               ODRDiagNote(SecondTemplate->getLocation(),
11550                           SecondTemplate->getSourceRange(),
11551                           FunctionTemplateParameterDifferentType)
11552                   << SecondTemplate << (i + 1);
11553               ParameterMismatch = true;
11554               break;
11555             }
11556 
11557             bool HasFirstDefaultArgument =
11558                 FirstTTPD->hasDefaultArgument() &&
11559                 !FirstTTPD->defaultArgumentWasInherited();
11560             bool HasSecondDefaultArgument =
11561                 SecondTTPD->hasDefaultArgument() &&
11562                 !SecondTTPD->defaultArgumentWasInherited();
11563             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11564               ODRDiagError(FirstTemplate->getLocation(),
11565                            FirstTemplate->getSourceRange(),
11566                            FunctionTemplateParameterSingleDefaultArgument)
11567                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11568               ODRDiagNote(SecondTemplate->getLocation(),
11569                           SecondTemplate->getSourceRange(),
11570                           FunctionTemplateParameterSingleDefaultArgument)
11571                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11572               ParameterMismatch = true;
11573               break;
11574             }
11575 
11576             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11577               TemplateArgument FirstTA =
11578                   FirstTTPD->getDefaultArgument().getArgument();
11579               TemplateArgument SecondTA =
11580                   SecondTTPD->getDefaultArgument().getArgument();
11581               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11582                   ComputeTemplateArgumentODRHash(SecondTA)) {
11583                 ODRDiagError(FirstTemplate->getLocation(),
11584                              FirstTemplate->getSourceRange(),
11585                              FunctionTemplateParameterDifferentDefaultArgument)
11586                     << FirstTemplate << (i + 1) << FirstTA;
11587                 ODRDiagNote(SecondTemplate->getLocation(),
11588                             SecondTemplate->getSourceRange(),
11589                             FunctionTemplateParameterDifferentDefaultArgument)
11590                     << SecondTemplate << (i + 1) << SecondTA;
11591                 ParameterMismatch = true;
11592                 break;
11593               }
11594             }
11595 
11596             if (FirstTTPD->isParameterPack() !=
11597                 SecondTTPD->isParameterPack()) {
11598               ODRDiagError(FirstTemplate->getLocation(),
11599                            FirstTemplate->getSourceRange(),
11600                            FunctionTemplatePackParameter)
11601                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11602               ODRDiagNote(SecondTemplate->getLocation(),
11603                           SecondTemplate->getSourceRange(),
11604                           FunctionTemplatePackParameter)
11605                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11606               ParameterMismatch = true;
11607               break;
11608             }
11609           }
11610 
11611           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11612               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11613             NonTypeTemplateParmDecl *FirstNTTPD =
11614                 cast<NonTypeTemplateParmDecl>(FirstParam);
11615             NonTypeTemplateParmDecl *SecondNTTPD =
11616                 cast<NonTypeTemplateParmDecl>(SecondParam);
11617 
11618             QualType FirstType = FirstNTTPD->getType();
11619             QualType SecondType = SecondNTTPD->getType();
11620             if (ComputeQualTypeODRHash(FirstType) !=
11621                 ComputeQualTypeODRHash(SecondType)) {
11622               ODRDiagError(FirstTemplate->getLocation(),
11623                            FirstTemplate->getSourceRange(),
11624                            FunctionTemplateParameterDifferentType)
11625                   << FirstTemplate << (i + 1);
11626               ODRDiagNote(SecondTemplate->getLocation(),
11627                           SecondTemplate->getSourceRange(),
11628                           FunctionTemplateParameterDifferentType)
11629                   << SecondTemplate << (i + 1);
11630               ParameterMismatch = true;
11631               break;
11632             }
11633 
11634             bool HasFirstDefaultArgument =
11635                 FirstNTTPD->hasDefaultArgument() &&
11636                 !FirstNTTPD->defaultArgumentWasInherited();
11637             bool HasSecondDefaultArgument =
11638                 SecondNTTPD->hasDefaultArgument() &&
11639                 !SecondNTTPD->defaultArgumentWasInherited();
11640             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11641               ODRDiagError(FirstTemplate->getLocation(),
11642                            FirstTemplate->getSourceRange(),
11643                            FunctionTemplateParameterSingleDefaultArgument)
11644                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11645               ODRDiagNote(SecondTemplate->getLocation(),
11646                           SecondTemplate->getSourceRange(),
11647                           FunctionTemplateParameterSingleDefaultArgument)
11648                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11649               ParameterMismatch = true;
11650               break;
11651             }
11652 
11653             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11654               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11655               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11656               if (ComputeODRHash(FirstDefaultArgument) !=
11657                   ComputeODRHash(SecondDefaultArgument)) {
11658                 ODRDiagError(FirstTemplate->getLocation(),
11659                              FirstTemplate->getSourceRange(),
11660                              FunctionTemplateParameterDifferentDefaultArgument)
11661                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11662                 ODRDiagNote(SecondTemplate->getLocation(),
11663                             SecondTemplate->getSourceRange(),
11664                             FunctionTemplateParameterDifferentDefaultArgument)
11665                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11666                 ParameterMismatch = true;
11667                 break;
11668               }
11669             }
11670 
11671             if (FirstNTTPD->isParameterPack() !=
11672                 SecondNTTPD->isParameterPack()) {
11673               ODRDiagError(FirstTemplate->getLocation(),
11674                            FirstTemplate->getSourceRange(),
11675                            FunctionTemplatePackParameter)
11676                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11677               ODRDiagNote(SecondTemplate->getLocation(),
11678                           SecondTemplate->getSourceRange(),
11679                           FunctionTemplatePackParameter)
11680                   << SecondTemplate << (i + 1)
11681                   << SecondNTTPD->isParameterPack();
11682               ParameterMismatch = true;
11683               break;
11684             }
11685           }
11686         }
11687 
11688         if (ParameterMismatch) {
11689           Diagnosed = true;
11690           break;
11691         }
11692 
11693         break;
11694       }
11695       }
11696 
11697       if (Diagnosed)
11698         continue;
11699 
11700       Diag(FirstDecl->getLocation(),
11701            diag::err_module_odr_violation_mismatch_decl_unknown)
11702           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11703           << FirstDecl->getSourceRange();
11704       Diag(SecondDecl->getLocation(),
11705            diag::note_module_odr_violation_mismatch_decl_unknown)
11706           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11707       Diagnosed = true;
11708     }
11709 
11710     if (!Diagnosed) {
11711       // All definitions are updates to the same declaration. This happens if a
11712       // module instantiates the declaration of a class template specialization
11713       // and two or more other modules instantiate its definition.
11714       //
11715       // FIXME: Indicate which modules had instantiations of this definition.
11716       // FIXME: How can this even happen?
11717       Diag(Merge.first->getLocation(),
11718            diag::err_module_odr_violation_different_instantiations)
11719         << Merge.first;
11720     }
11721   }
11722 
11723   // Issue ODR failures diagnostics for functions.
11724   for (auto &Merge : FunctionOdrMergeFailures) {
11725     enum ODRFunctionDifference {
11726       ReturnType,
11727       ParameterName,
11728       ParameterType,
11729       ParameterSingleDefaultArgument,
11730       ParameterDifferentDefaultArgument,
11731       FunctionBody,
11732     };
11733 
11734     FunctionDecl *FirstFunction = Merge.first;
11735     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11736 
11737     bool Diagnosed = false;
11738     for (auto &SecondFunction : Merge.second) {
11739 
11740       if (FirstFunction == SecondFunction)
11741         continue;
11742 
11743       std::string SecondModule =
11744           getOwningModuleNameForDiagnostic(SecondFunction);
11745 
11746       auto ODRDiagError = [FirstFunction, &FirstModule,
11747                            this](SourceLocation Loc, SourceRange Range,
11748                                  ODRFunctionDifference DiffType) {
11749         return Diag(Loc, diag::err_module_odr_violation_function)
11750                << FirstFunction << FirstModule.empty() << FirstModule << Range
11751                << DiffType;
11752       };
11753       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11754                                                SourceRange Range,
11755                                                ODRFunctionDifference DiffType) {
11756         return Diag(Loc, diag::note_module_odr_violation_function)
11757                << SecondModule << Range << DiffType;
11758       };
11759 
11760       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11761           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11762         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11763                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11764             << FirstFunction->getReturnType();
11765         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11766                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11767             << SecondFunction->getReturnType();
11768         Diagnosed = true;
11769         break;
11770       }
11771 
11772       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11773              "Merged functions with different number of parameters");
11774 
11775       auto ParamSize = FirstFunction->param_size();
11776       bool ParameterMismatch = false;
11777       for (unsigned I = 0; I < ParamSize; ++I) {
11778         auto *FirstParam = FirstFunction->getParamDecl(I);
11779         auto *SecondParam = SecondFunction->getParamDecl(I);
11780 
11781         assert(getContext().hasSameType(FirstParam->getType(),
11782                                       SecondParam->getType()) &&
11783                "Merged function has different parameter types.");
11784 
11785         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11786           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11787                        ParameterName)
11788               << I + 1 << FirstParam->getDeclName();
11789           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11790                       ParameterName)
11791               << I + 1 << SecondParam->getDeclName();
11792           ParameterMismatch = true;
11793           break;
11794         };
11795 
11796         QualType FirstParamType = FirstParam->getType();
11797         QualType SecondParamType = SecondParam->getType();
11798         if (FirstParamType != SecondParamType &&
11799             ComputeQualTypeODRHash(FirstParamType) !=
11800                 ComputeQualTypeODRHash(SecondParamType)) {
11801           if (const DecayedType *ParamDecayedType =
11802                   FirstParamType->getAs<DecayedType>()) {
11803             ODRDiagError(FirstParam->getLocation(),
11804                          FirstParam->getSourceRange(), ParameterType)
11805                 << (I + 1) << FirstParamType << true
11806                 << ParamDecayedType->getOriginalType();
11807           } else {
11808             ODRDiagError(FirstParam->getLocation(),
11809                          FirstParam->getSourceRange(), ParameterType)
11810                 << (I + 1) << FirstParamType << false;
11811           }
11812 
11813           if (const DecayedType *ParamDecayedType =
11814                   SecondParamType->getAs<DecayedType>()) {
11815             ODRDiagNote(SecondParam->getLocation(),
11816                         SecondParam->getSourceRange(), ParameterType)
11817                 << (I + 1) << SecondParamType << true
11818                 << ParamDecayedType->getOriginalType();
11819           } else {
11820             ODRDiagNote(SecondParam->getLocation(),
11821                         SecondParam->getSourceRange(), ParameterType)
11822                 << (I + 1) << SecondParamType << false;
11823           }
11824           ParameterMismatch = true;
11825           break;
11826         }
11827 
11828         const Expr *FirstInit = FirstParam->getInit();
11829         const Expr *SecondInit = SecondParam->getInit();
11830         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11831           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11832                        ParameterSingleDefaultArgument)
11833               << (I + 1) << (FirstInit == nullptr)
11834               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11835           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11836                       ParameterSingleDefaultArgument)
11837               << (I + 1) << (SecondInit == nullptr)
11838               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11839           ParameterMismatch = true;
11840           break;
11841         }
11842 
11843         if (FirstInit && SecondInit &&
11844             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11845           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11846                        ParameterDifferentDefaultArgument)
11847               << (I + 1) << FirstInit->getSourceRange();
11848           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11849                       ParameterDifferentDefaultArgument)
11850               << (I + 1) << SecondInit->getSourceRange();
11851           ParameterMismatch = true;
11852           break;
11853         }
11854 
11855         assert(ComputeSubDeclODRHash(FirstParam) ==
11856                    ComputeSubDeclODRHash(SecondParam) &&
11857                "Undiagnosed parameter difference.");
11858       }
11859 
11860       if (ParameterMismatch) {
11861         Diagnosed = true;
11862         break;
11863       }
11864 
11865       // If no error has been generated before now, assume the problem is in
11866       // the body and generate a message.
11867       ODRDiagError(FirstFunction->getLocation(),
11868                    FirstFunction->getSourceRange(), FunctionBody);
11869       ODRDiagNote(SecondFunction->getLocation(),
11870                   SecondFunction->getSourceRange(), FunctionBody);
11871       Diagnosed = true;
11872       break;
11873     }
11874     (void)Diagnosed;
11875     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11876   }
11877 
11878   // Issue ODR failures diagnostics for enums.
11879   for (auto &Merge : EnumOdrMergeFailures) {
11880     enum ODREnumDifference {
11881       SingleScopedEnum,
11882       EnumTagKeywordMismatch,
11883       SingleSpecifiedType,
11884       DifferentSpecifiedTypes,
11885       DifferentNumberEnumConstants,
11886       EnumConstantName,
11887       EnumConstantSingleInitilizer,
11888       EnumConstantDifferentInitilizer,
11889     };
11890 
11891     // If we've already pointed out a specific problem with this enum, don't
11892     // bother issuing a general "something's different" diagnostic.
11893     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11894       continue;
11895 
11896     EnumDecl *FirstEnum = Merge.first;
11897     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11898 
11899     using DeclHashes =
11900         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11901     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11902                               DeclHashes &Hashes, EnumDecl *Enum) {
11903       for (auto *D : Enum->decls()) {
11904         // Due to decl merging, the first EnumDecl is the parent of
11905         // Decls in both records.
11906         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11907           continue;
11908         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11909         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11910                             ComputeSubDeclODRHash(D));
11911       }
11912     };
11913     DeclHashes FirstHashes;
11914     PopulateHashes(FirstHashes, FirstEnum);
11915     bool Diagnosed = false;
11916     for (auto &SecondEnum : Merge.second) {
11917 
11918       if (FirstEnum == SecondEnum)
11919         continue;
11920 
11921       std::string SecondModule =
11922           getOwningModuleNameForDiagnostic(SecondEnum);
11923 
11924       auto ODRDiagError = [FirstEnum, &FirstModule,
11925                            this](SourceLocation Loc, SourceRange Range,
11926                                  ODREnumDifference DiffType) {
11927         return Diag(Loc, diag::err_module_odr_violation_enum)
11928                << FirstEnum << FirstModule.empty() << FirstModule << Range
11929                << DiffType;
11930       };
11931       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11932                                                SourceRange Range,
11933                                                ODREnumDifference DiffType) {
11934         return Diag(Loc, diag::note_module_odr_violation_enum)
11935                << SecondModule << Range << DiffType;
11936       };
11937 
11938       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11939         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11940                      SingleScopedEnum)
11941             << FirstEnum->isScoped();
11942         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11943                     SingleScopedEnum)
11944             << SecondEnum->isScoped();
11945         Diagnosed = true;
11946         continue;
11947       }
11948 
11949       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11950         if (FirstEnum->isScopedUsingClassTag() !=
11951             SecondEnum->isScopedUsingClassTag()) {
11952           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11953                        EnumTagKeywordMismatch)
11954               << FirstEnum->isScopedUsingClassTag();
11955           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11956                       EnumTagKeywordMismatch)
11957               << SecondEnum->isScopedUsingClassTag();
11958           Diagnosed = true;
11959           continue;
11960         }
11961       }
11962 
11963       QualType FirstUnderlyingType =
11964           FirstEnum->getIntegerTypeSourceInfo()
11965               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11966               : QualType();
11967       QualType SecondUnderlyingType =
11968           SecondEnum->getIntegerTypeSourceInfo()
11969               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11970               : QualType();
11971       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11972           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11973                        SingleSpecifiedType)
11974               << !FirstUnderlyingType.isNull();
11975           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11976                       SingleSpecifiedType)
11977               << !SecondUnderlyingType.isNull();
11978           Diagnosed = true;
11979           continue;
11980       }
11981 
11982       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11983         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11984             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11985           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11986                        DifferentSpecifiedTypes)
11987               << FirstUnderlyingType;
11988           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11989                       DifferentSpecifiedTypes)
11990               << SecondUnderlyingType;
11991           Diagnosed = true;
11992           continue;
11993         }
11994       }
11995 
11996       DeclHashes SecondHashes;
11997       PopulateHashes(SecondHashes, SecondEnum);
11998 
11999       if (FirstHashes.size() != SecondHashes.size()) {
12000         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12001                      DifferentNumberEnumConstants)
12002             << (int)FirstHashes.size();
12003         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12004                     DifferentNumberEnumConstants)
12005             << (int)SecondHashes.size();
12006         Diagnosed = true;
12007         continue;
12008       }
12009 
12010       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
12011         if (FirstHashes[I].second == SecondHashes[I].second)
12012           continue;
12013         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
12014         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
12015 
12016         if (FirstEnumConstant->getDeclName() !=
12017             SecondEnumConstant->getDeclName()) {
12018 
12019           ODRDiagError(FirstEnumConstant->getLocation(),
12020                        FirstEnumConstant->getSourceRange(), EnumConstantName)
12021               << I + 1 << FirstEnumConstant;
12022           ODRDiagNote(SecondEnumConstant->getLocation(),
12023                       SecondEnumConstant->getSourceRange(), EnumConstantName)
12024               << I + 1 << SecondEnumConstant;
12025           Diagnosed = true;
12026           break;
12027         }
12028 
12029         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12030         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12031         if (!FirstInit && !SecondInit)
12032           continue;
12033 
12034         if (!FirstInit || !SecondInit) {
12035           ODRDiagError(FirstEnumConstant->getLocation(),
12036                        FirstEnumConstant->getSourceRange(),
12037                        EnumConstantSingleInitilizer)
12038               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12039           ODRDiagNote(SecondEnumConstant->getLocation(),
12040                       SecondEnumConstant->getSourceRange(),
12041                       EnumConstantSingleInitilizer)
12042               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12043           Diagnosed = true;
12044           break;
12045         }
12046 
12047         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12048           ODRDiagError(FirstEnumConstant->getLocation(),
12049                        FirstEnumConstant->getSourceRange(),
12050                        EnumConstantDifferentInitilizer)
12051               << I + 1 << FirstEnumConstant;
12052           ODRDiagNote(SecondEnumConstant->getLocation(),
12053                       SecondEnumConstant->getSourceRange(),
12054                       EnumConstantDifferentInitilizer)
12055               << I + 1 << SecondEnumConstant;
12056           Diagnosed = true;
12057           break;
12058         }
12059       }
12060     }
12061 
12062     (void)Diagnosed;
12063     assert(Diagnosed && "Unable to emit ODR diagnostic.");
12064   }
12065 }
12066 
12067 void ASTReader::StartedDeserializing() {
12068   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12069     ReadTimer->startTimer();
12070 }
12071 
12072 void ASTReader::FinishedDeserializing() {
12073   assert(NumCurrentElementsDeserializing &&
12074          "FinishedDeserializing not paired with StartedDeserializing");
12075   if (NumCurrentElementsDeserializing == 1) {
12076     // We decrease NumCurrentElementsDeserializing only after pending actions
12077     // are finished, to avoid recursively re-calling finishPendingActions().
12078     finishPendingActions();
12079   }
12080   --NumCurrentElementsDeserializing;
12081 
12082   if (NumCurrentElementsDeserializing == 0) {
12083     // Propagate exception specification and deduced type updates along
12084     // redeclaration chains.
12085     //
12086     // We do this now rather than in finishPendingActions because we want to
12087     // be able to walk the complete redeclaration chains of the updated decls.
12088     while (!PendingExceptionSpecUpdates.empty() ||
12089            !PendingDeducedTypeUpdates.empty()) {
12090       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12091       PendingExceptionSpecUpdates.clear();
12092       for (auto Update : ESUpdates) {
12093         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12094         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12095         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12096         if (auto *Listener = getContext().getASTMutationListener())
12097           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12098         for (auto *Redecl : Update.second->redecls())
12099           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12100       }
12101 
12102       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12103       PendingDeducedTypeUpdates.clear();
12104       for (auto Update : DTUpdates) {
12105         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12106         // FIXME: If the return type is already deduced, check that it matches.
12107         getContext().adjustDeducedFunctionResultType(Update.first,
12108                                                      Update.second);
12109       }
12110     }
12111 
12112     if (ReadTimer)
12113       ReadTimer->stopTimer();
12114 
12115     diagnoseOdrViolations();
12116 
12117     // We are not in recursive loading, so it's safe to pass the "interesting"
12118     // decls to the consumer.
12119     if (Consumer)
12120       PassInterestingDeclsToConsumer();
12121   }
12122 }
12123 
12124 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12125   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12126     // Remove any fake results before adding any real ones.
12127     auto It = PendingFakeLookupResults.find(II);
12128     if (It != PendingFakeLookupResults.end()) {
12129       for (auto *ND : It->second)
12130         SemaObj->IdResolver.RemoveDecl(ND);
12131       // FIXME: this works around module+PCH performance issue.
12132       // Rather than erase the result from the map, which is O(n), just clear
12133       // the vector of NamedDecls.
12134       It->second.clear();
12135     }
12136   }
12137 
12138   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12139     SemaObj->TUScope->AddDecl(D);
12140   } else if (SemaObj->TUScope) {
12141     // Adding the decl to IdResolver may have failed because it was already in
12142     // (even though it was not added in scope). If it is already in, make sure
12143     // it gets in the scope as well.
12144     if (std::find(SemaObj->IdResolver.begin(Name),
12145                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12146       SemaObj->TUScope->AddDecl(D);
12147   }
12148 }
12149 
12150 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12151                      ASTContext *Context,
12152                      const PCHContainerReader &PCHContainerRdr,
12153                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12154                      StringRef isysroot, bool DisableValidation,
12155                      bool AllowASTWithCompilerErrors,
12156                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12157                      bool UseGlobalIndex,
12158                      std::unique_ptr<llvm::Timer> ReadTimer)
12159     : Listener(DisableValidation
12160                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12161                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12162       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12163       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12164       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12165                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
12166       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12167       DisableValidation(DisableValidation),
12168       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12169       AllowConfigurationMismatch(AllowConfigurationMismatch),
12170       ValidateSystemInputs(ValidateSystemInputs),
12171       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12172   SourceMgr.setExternalSLocEntrySource(this);
12173 
12174   for (const auto &Ext : Extensions) {
12175     auto BlockName = Ext->getExtensionMetadata().BlockName;
12176     auto Known = ModuleFileExtensions.find(BlockName);
12177     if (Known != ModuleFileExtensions.end()) {
12178       Diags.Report(diag::warn_duplicate_module_file_extension)
12179         << BlockName;
12180       continue;
12181     }
12182 
12183     ModuleFileExtensions.insert({BlockName, Ext});
12184   }
12185 }
12186 
12187 ASTReader::~ASTReader() {
12188   if (OwnsDeserializationListener)
12189     delete DeserializationListener;
12190 }
12191 
12192 IdentifierResolver &ASTReader::getIdResolver() {
12193   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12194 }
12195 
12196 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12197                                                unsigned AbbrevID) {
12198   Idx = 0;
12199   Record.clear();
12200   return Cursor.readRecord(AbbrevID, Record);
12201 }
12202 //===----------------------------------------------------------------------===//
12203 //// OMPClauseReader implementation
12204 ////===----------------------------------------------------------------------===//
12205 
12206 OMPClause *OMPClauseReader::readClause() {
12207   OMPClause *C;
12208   switch (Record.readInt()) {
12209   case OMPC_if:
12210     C = new (Context) OMPIfClause();
12211     break;
12212   case OMPC_final:
12213     C = new (Context) OMPFinalClause();
12214     break;
12215   case OMPC_num_threads:
12216     C = new (Context) OMPNumThreadsClause();
12217     break;
12218   case OMPC_safelen:
12219     C = new (Context) OMPSafelenClause();
12220     break;
12221   case OMPC_simdlen:
12222     C = new (Context) OMPSimdlenClause();
12223     break;
12224   case OMPC_allocator:
12225     C = new (Context) OMPAllocatorClause();
12226     break;
12227   case OMPC_collapse:
12228     C = new (Context) OMPCollapseClause();
12229     break;
12230   case OMPC_default:
12231     C = new (Context) OMPDefaultClause();
12232     break;
12233   case OMPC_proc_bind:
12234     C = new (Context) OMPProcBindClause();
12235     break;
12236   case OMPC_schedule:
12237     C = new (Context) OMPScheduleClause();
12238     break;
12239   case OMPC_ordered:
12240     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12241     break;
12242   case OMPC_nowait:
12243     C = new (Context) OMPNowaitClause();
12244     break;
12245   case OMPC_untied:
12246     C = new (Context) OMPUntiedClause();
12247     break;
12248   case OMPC_mergeable:
12249     C = new (Context) OMPMergeableClause();
12250     break;
12251   case OMPC_read:
12252     C = new (Context) OMPReadClause();
12253     break;
12254   case OMPC_write:
12255     C = new (Context) OMPWriteClause();
12256     break;
12257   case OMPC_update:
12258     C = new (Context) OMPUpdateClause();
12259     break;
12260   case OMPC_capture:
12261     C = new (Context) OMPCaptureClause();
12262     break;
12263   case OMPC_seq_cst:
12264     C = new (Context) OMPSeqCstClause();
12265     break;
12266   case OMPC_threads:
12267     C = new (Context) OMPThreadsClause();
12268     break;
12269   case OMPC_simd:
12270     C = new (Context) OMPSIMDClause();
12271     break;
12272   case OMPC_nogroup:
12273     C = new (Context) OMPNogroupClause();
12274     break;
12275   case OMPC_unified_address:
12276     C = new (Context) OMPUnifiedAddressClause();
12277     break;
12278   case OMPC_unified_shared_memory:
12279     C = new (Context) OMPUnifiedSharedMemoryClause();
12280     break;
12281   case OMPC_reverse_offload:
12282     C = new (Context) OMPReverseOffloadClause();
12283     break;
12284   case OMPC_dynamic_allocators:
12285     C = new (Context) OMPDynamicAllocatorsClause();
12286     break;
12287   case OMPC_atomic_default_mem_order:
12288     C = new (Context) OMPAtomicDefaultMemOrderClause();
12289     break;
12290  case OMPC_private:
12291     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12292     break;
12293   case OMPC_firstprivate:
12294     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12295     break;
12296   case OMPC_lastprivate:
12297     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12298     break;
12299   case OMPC_shared:
12300     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12301     break;
12302   case OMPC_reduction:
12303     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12304     break;
12305   case OMPC_task_reduction:
12306     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12307     break;
12308   case OMPC_in_reduction:
12309     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12310     break;
12311   case OMPC_linear:
12312     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12313     break;
12314   case OMPC_aligned:
12315     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12316     break;
12317   case OMPC_copyin:
12318     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12319     break;
12320   case OMPC_copyprivate:
12321     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12322     break;
12323   case OMPC_flush:
12324     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12325     break;
12326   case OMPC_depend: {
12327     unsigned NumVars = Record.readInt();
12328     unsigned NumLoops = Record.readInt();
12329     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12330     break;
12331   }
12332   case OMPC_device:
12333     C = new (Context) OMPDeviceClause();
12334     break;
12335   case OMPC_map: {
12336     OMPMappableExprListSizeTy Sizes;
12337     Sizes.NumVars = Record.readInt();
12338     Sizes.NumUniqueDeclarations = Record.readInt();
12339     Sizes.NumComponentLists = Record.readInt();
12340     Sizes.NumComponents = Record.readInt();
12341     C = OMPMapClause::CreateEmpty(Context, Sizes);
12342     break;
12343   }
12344   case OMPC_num_teams:
12345     C = new (Context) OMPNumTeamsClause();
12346     break;
12347   case OMPC_thread_limit:
12348     C = new (Context) OMPThreadLimitClause();
12349     break;
12350   case OMPC_priority:
12351     C = new (Context) OMPPriorityClause();
12352     break;
12353   case OMPC_grainsize:
12354     C = new (Context) OMPGrainsizeClause();
12355     break;
12356   case OMPC_num_tasks:
12357     C = new (Context) OMPNumTasksClause();
12358     break;
12359   case OMPC_hint:
12360     C = new (Context) OMPHintClause();
12361     break;
12362   case OMPC_dist_schedule:
12363     C = new (Context) OMPDistScheduleClause();
12364     break;
12365   case OMPC_defaultmap:
12366     C = new (Context) OMPDefaultmapClause();
12367     break;
12368   case OMPC_to: {
12369     OMPMappableExprListSizeTy Sizes;
12370     Sizes.NumVars = Record.readInt();
12371     Sizes.NumUniqueDeclarations = Record.readInt();
12372     Sizes.NumComponentLists = Record.readInt();
12373     Sizes.NumComponents = Record.readInt();
12374     C = OMPToClause::CreateEmpty(Context, Sizes);
12375     break;
12376   }
12377   case OMPC_from: {
12378     OMPMappableExprListSizeTy Sizes;
12379     Sizes.NumVars = Record.readInt();
12380     Sizes.NumUniqueDeclarations = Record.readInt();
12381     Sizes.NumComponentLists = Record.readInt();
12382     Sizes.NumComponents = Record.readInt();
12383     C = OMPFromClause::CreateEmpty(Context, Sizes);
12384     break;
12385   }
12386   case OMPC_use_device_ptr: {
12387     OMPMappableExprListSizeTy Sizes;
12388     Sizes.NumVars = Record.readInt();
12389     Sizes.NumUniqueDeclarations = Record.readInt();
12390     Sizes.NumComponentLists = Record.readInt();
12391     Sizes.NumComponents = Record.readInt();
12392     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12393     break;
12394   }
12395   case OMPC_is_device_ptr: {
12396     OMPMappableExprListSizeTy Sizes;
12397     Sizes.NumVars = Record.readInt();
12398     Sizes.NumUniqueDeclarations = Record.readInt();
12399     Sizes.NumComponentLists = Record.readInt();
12400     Sizes.NumComponents = Record.readInt();
12401     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12402     break;
12403   }
12404   case OMPC_allocate:
12405     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12406     break;
12407   }
12408   Visit(C);
12409   C->setLocStart(Record.readSourceLocation());
12410   C->setLocEnd(Record.readSourceLocation());
12411 
12412   return C;
12413 }
12414 
12415 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12416   C->setPreInitStmt(Record.readSubStmt(),
12417                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12418 }
12419 
12420 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12421   VisitOMPClauseWithPreInit(C);
12422   C->setPostUpdateExpr(Record.readSubExpr());
12423 }
12424 
12425 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12426   VisitOMPClauseWithPreInit(C);
12427   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12428   C->setNameModifierLoc(Record.readSourceLocation());
12429   C->setColonLoc(Record.readSourceLocation());
12430   C->setCondition(Record.readSubExpr());
12431   C->setLParenLoc(Record.readSourceLocation());
12432 }
12433 
12434 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12435   C->setCondition(Record.readSubExpr());
12436   C->setLParenLoc(Record.readSourceLocation());
12437 }
12438 
12439 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12440   VisitOMPClauseWithPreInit(C);
12441   C->setNumThreads(Record.readSubExpr());
12442   C->setLParenLoc(Record.readSourceLocation());
12443 }
12444 
12445 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12446   C->setSafelen(Record.readSubExpr());
12447   C->setLParenLoc(Record.readSourceLocation());
12448 }
12449 
12450 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12451   C->setSimdlen(Record.readSubExpr());
12452   C->setLParenLoc(Record.readSourceLocation());
12453 }
12454 
12455 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12456   C->setAllocator(Record.readExpr());
12457   C->setLParenLoc(Record.readSourceLocation());
12458 }
12459 
12460 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12461   C->setNumForLoops(Record.readSubExpr());
12462   C->setLParenLoc(Record.readSourceLocation());
12463 }
12464 
12465 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12466   C->setDefaultKind(
12467        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12468   C->setLParenLoc(Record.readSourceLocation());
12469   C->setDefaultKindKwLoc(Record.readSourceLocation());
12470 }
12471 
12472 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12473   C->setProcBindKind(
12474        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12475   C->setLParenLoc(Record.readSourceLocation());
12476   C->setProcBindKindKwLoc(Record.readSourceLocation());
12477 }
12478 
12479 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12480   VisitOMPClauseWithPreInit(C);
12481   C->setScheduleKind(
12482        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12483   C->setFirstScheduleModifier(
12484       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12485   C->setSecondScheduleModifier(
12486       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12487   C->setChunkSize(Record.readSubExpr());
12488   C->setLParenLoc(Record.readSourceLocation());
12489   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12490   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12491   C->setScheduleKindLoc(Record.readSourceLocation());
12492   C->setCommaLoc(Record.readSourceLocation());
12493 }
12494 
12495 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12496   C->setNumForLoops(Record.readSubExpr());
12497   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12498     C->setLoopNumIterations(I, Record.readSubExpr());
12499   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12500     C->setLoopCounter(I, Record.readSubExpr());
12501   C->setLParenLoc(Record.readSourceLocation());
12502 }
12503 
12504 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12505 
12506 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12507 
12508 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12509 
12510 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12511 
12512 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12513 
12514 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12515 
12516 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12517 
12518 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12519 
12520 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12521 
12522 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12523 
12524 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12525 
12526 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12527 
12528 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12529     OMPUnifiedSharedMemoryClause *) {}
12530 
12531 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12532 
12533 void
12534 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12535 }
12536 
12537 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12538     OMPAtomicDefaultMemOrderClause *C) {
12539   C->setAtomicDefaultMemOrderKind(
12540       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12541   C->setLParenLoc(Record.readSourceLocation());
12542   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12543 }
12544 
12545 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12546   C->setLParenLoc(Record.readSourceLocation());
12547   unsigned NumVars = C->varlist_size();
12548   SmallVector<Expr *, 16> Vars;
12549   Vars.reserve(NumVars);
12550   for (unsigned i = 0; i != NumVars; ++i)
12551     Vars.push_back(Record.readSubExpr());
12552   C->setVarRefs(Vars);
12553   Vars.clear();
12554   for (unsigned i = 0; i != NumVars; ++i)
12555     Vars.push_back(Record.readSubExpr());
12556   C->setPrivateCopies(Vars);
12557 }
12558 
12559 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12560   VisitOMPClauseWithPreInit(C);
12561   C->setLParenLoc(Record.readSourceLocation());
12562   unsigned NumVars = C->varlist_size();
12563   SmallVector<Expr *, 16> Vars;
12564   Vars.reserve(NumVars);
12565   for (unsigned i = 0; i != NumVars; ++i)
12566     Vars.push_back(Record.readSubExpr());
12567   C->setVarRefs(Vars);
12568   Vars.clear();
12569   for (unsigned i = 0; i != NumVars; ++i)
12570     Vars.push_back(Record.readSubExpr());
12571   C->setPrivateCopies(Vars);
12572   Vars.clear();
12573   for (unsigned i = 0; i != NumVars; ++i)
12574     Vars.push_back(Record.readSubExpr());
12575   C->setInits(Vars);
12576 }
12577 
12578 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12579   VisitOMPClauseWithPostUpdate(C);
12580   C->setLParenLoc(Record.readSourceLocation());
12581   unsigned NumVars = C->varlist_size();
12582   SmallVector<Expr *, 16> Vars;
12583   Vars.reserve(NumVars);
12584   for (unsigned i = 0; i != NumVars; ++i)
12585     Vars.push_back(Record.readSubExpr());
12586   C->setVarRefs(Vars);
12587   Vars.clear();
12588   for (unsigned i = 0; i != NumVars; ++i)
12589     Vars.push_back(Record.readSubExpr());
12590   C->setPrivateCopies(Vars);
12591   Vars.clear();
12592   for (unsigned i = 0; i != NumVars; ++i)
12593     Vars.push_back(Record.readSubExpr());
12594   C->setSourceExprs(Vars);
12595   Vars.clear();
12596   for (unsigned i = 0; i != NumVars; ++i)
12597     Vars.push_back(Record.readSubExpr());
12598   C->setDestinationExprs(Vars);
12599   Vars.clear();
12600   for (unsigned i = 0; i != NumVars; ++i)
12601     Vars.push_back(Record.readSubExpr());
12602   C->setAssignmentOps(Vars);
12603 }
12604 
12605 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12606   C->setLParenLoc(Record.readSourceLocation());
12607   unsigned NumVars = C->varlist_size();
12608   SmallVector<Expr *, 16> Vars;
12609   Vars.reserve(NumVars);
12610   for (unsigned i = 0; i != NumVars; ++i)
12611     Vars.push_back(Record.readSubExpr());
12612   C->setVarRefs(Vars);
12613 }
12614 
12615 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12616   VisitOMPClauseWithPostUpdate(C);
12617   C->setLParenLoc(Record.readSourceLocation());
12618   C->setColonLoc(Record.readSourceLocation());
12619   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12620   DeclarationNameInfo DNI;
12621   Record.readDeclarationNameInfo(DNI);
12622   C->setQualifierLoc(NNSL);
12623   C->setNameInfo(DNI);
12624 
12625   unsigned NumVars = C->varlist_size();
12626   SmallVector<Expr *, 16> Vars;
12627   Vars.reserve(NumVars);
12628   for (unsigned i = 0; i != NumVars; ++i)
12629     Vars.push_back(Record.readSubExpr());
12630   C->setVarRefs(Vars);
12631   Vars.clear();
12632   for (unsigned i = 0; i != NumVars; ++i)
12633     Vars.push_back(Record.readSubExpr());
12634   C->setPrivates(Vars);
12635   Vars.clear();
12636   for (unsigned i = 0; i != NumVars; ++i)
12637     Vars.push_back(Record.readSubExpr());
12638   C->setLHSExprs(Vars);
12639   Vars.clear();
12640   for (unsigned i = 0; i != NumVars; ++i)
12641     Vars.push_back(Record.readSubExpr());
12642   C->setRHSExprs(Vars);
12643   Vars.clear();
12644   for (unsigned i = 0; i != NumVars; ++i)
12645     Vars.push_back(Record.readSubExpr());
12646   C->setReductionOps(Vars);
12647 }
12648 
12649 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12650   VisitOMPClauseWithPostUpdate(C);
12651   C->setLParenLoc(Record.readSourceLocation());
12652   C->setColonLoc(Record.readSourceLocation());
12653   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12654   DeclarationNameInfo DNI;
12655   Record.readDeclarationNameInfo(DNI);
12656   C->setQualifierLoc(NNSL);
12657   C->setNameInfo(DNI);
12658 
12659   unsigned NumVars = C->varlist_size();
12660   SmallVector<Expr *, 16> Vars;
12661   Vars.reserve(NumVars);
12662   for (unsigned I = 0; I != NumVars; ++I)
12663     Vars.push_back(Record.readSubExpr());
12664   C->setVarRefs(Vars);
12665   Vars.clear();
12666   for (unsigned I = 0; I != NumVars; ++I)
12667     Vars.push_back(Record.readSubExpr());
12668   C->setPrivates(Vars);
12669   Vars.clear();
12670   for (unsigned I = 0; I != NumVars; ++I)
12671     Vars.push_back(Record.readSubExpr());
12672   C->setLHSExprs(Vars);
12673   Vars.clear();
12674   for (unsigned I = 0; I != NumVars; ++I)
12675     Vars.push_back(Record.readSubExpr());
12676   C->setRHSExprs(Vars);
12677   Vars.clear();
12678   for (unsigned I = 0; I != NumVars; ++I)
12679     Vars.push_back(Record.readSubExpr());
12680   C->setReductionOps(Vars);
12681 }
12682 
12683 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12684   VisitOMPClauseWithPostUpdate(C);
12685   C->setLParenLoc(Record.readSourceLocation());
12686   C->setColonLoc(Record.readSourceLocation());
12687   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12688   DeclarationNameInfo DNI;
12689   Record.readDeclarationNameInfo(DNI);
12690   C->setQualifierLoc(NNSL);
12691   C->setNameInfo(DNI);
12692 
12693   unsigned NumVars = C->varlist_size();
12694   SmallVector<Expr *, 16> Vars;
12695   Vars.reserve(NumVars);
12696   for (unsigned I = 0; I != NumVars; ++I)
12697     Vars.push_back(Record.readSubExpr());
12698   C->setVarRefs(Vars);
12699   Vars.clear();
12700   for (unsigned I = 0; I != NumVars; ++I)
12701     Vars.push_back(Record.readSubExpr());
12702   C->setPrivates(Vars);
12703   Vars.clear();
12704   for (unsigned I = 0; I != NumVars; ++I)
12705     Vars.push_back(Record.readSubExpr());
12706   C->setLHSExprs(Vars);
12707   Vars.clear();
12708   for (unsigned I = 0; I != NumVars; ++I)
12709     Vars.push_back(Record.readSubExpr());
12710   C->setRHSExprs(Vars);
12711   Vars.clear();
12712   for (unsigned I = 0; I != NumVars; ++I)
12713     Vars.push_back(Record.readSubExpr());
12714   C->setReductionOps(Vars);
12715   Vars.clear();
12716   for (unsigned I = 0; I != NumVars; ++I)
12717     Vars.push_back(Record.readSubExpr());
12718   C->setTaskgroupDescriptors(Vars);
12719 }
12720 
12721 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12722   VisitOMPClauseWithPostUpdate(C);
12723   C->setLParenLoc(Record.readSourceLocation());
12724   C->setColonLoc(Record.readSourceLocation());
12725   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12726   C->setModifierLoc(Record.readSourceLocation());
12727   unsigned NumVars = C->varlist_size();
12728   SmallVector<Expr *, 16> Vars;
12729   Vars.reserve(NumVars);
12730   for (unsigned i = 0; i != NumVars; ++i)
12731     Vars.push_back(Record.readSubExpr());
12732   C->setVarRefs(Vars);
12733   Vars.clear();
12734   for (unsigned i = 0; i != NumVars; ++i)
12735     Vars.push_back(Record.readSubExpr());
12736   C->setPrivates(Vars);
12737   Vars.clear();
12738   for (unsigned i = 0; i != NumVars; ++i)
12739     Vars.push_back(Record.readSubExpr());
12740   C->setInits(Vars);
12741   Vars.clear();
12742   for (unsigned i = 0; i != NumVars; ++i)
12743     Vars.push_back(Record.readSubExpr());
12744   C->setUpdates(Vars);
12745   Vars.clear();
12746   for (unsigned i = 0; i != NumVars; ++i)
12747     Vars.push_back(Record.readSubExpr());
12748   C->setFinals(Vars);
12749   C->setStep(Record.readSubExpr());
12750   C->setCalcStep(Record.readSubExpr());
12751   Vars.clear();
12752   for (unsigned I = 0; I != NumVars + 1; ++I)
12753     Vars.push_back(Record.readSubExpr());
12754   C->setUsedExprs(Vars);
12755 }
12756 
12757 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12758   C->setLParenLoc(Record.readSourceLocation());
12759   C->setColonLoc(Record.readSourceLocation());
12760   unsigned NumVars = C->varlist_size();
12761   SmallVector<Expr *, 16> Vars;
12762   Vars.reserve(NumVars);
12763   for (unsigned i = 0; i != NumVars; ++i)
12764     Vars.push_back(Record.readSubExpr());
12765   C->setVarRefs(Vars);
12766   C->setAlignment(Record.readSubExpr());
12767 }
12768 
12769 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12770   C->setLParenLoc(Record.readSourceLocation());
12771   unsigned NumVars = C->varlist_size();
12772   SmallVector<Expr *, 16> Exprs;
12773   Exprs.reserve(NumVars);
12774   for (unsigned i = 0; i != NumVars; ++i)
12775     Exprs.push_back(Record.readSubExpr());
12776   C->setVarRefs(Exprs);
12777   Exprs.clear();
12778   for (unsigned i = 0; i != NumVars; ++i)
12779     Exprs.push_back(Record.readSubExpr());
12780   C->setSourceExprs(Exprs);
12781   Exprs.clear();
12782   for (unsigned i = 0; i != NumVars; ++i)
12783     Exprs.push_back(Record.readSubExpr());
12784   C->setDestinationExprs(Exprs);
12785   Exprs.clear();
12786   for (unsigned i = 0; i != NumVars; ++i)
12787     Exprs.push_back(Record.readSubExpr());
12788   C->setAssignmentOps(Exprs);
12789 }
12790 
12791 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12792   C->setLParenLoc(Record.readSourceLocation());
12793   unsigned NumVars = C->varlist_size();
12794   SmallVector<Expr *, 16> Exprs;
12795   Exprs.reserve(NumVars);
12796   for (unsigned i = 0; i != NumVars; ++i)
12797     Exprs.push_back(Record.readSubExpr());
12798   C->setVarRefs(Exprs);
12799   Exprs.clear();
12800   for (unsigned i = 0; i != NumVars; ++i)
12801     Exprs.push_back(Record.readSubExpr());
12802   C->setSourceExprs(Exprs);
12803   Exprs.clear();
12804   for (unsigned i = 0; i != NumVars; ++i)
12805     Exprs.push_back(Record.readSubExpr());
12806   C->setDestinationExprs(Exprs);
12807   Exprs.clear();
12808   for (unsigned i = 0; i != NumVars; ++i)
12809     Exprs.push_back(Record.readSubExpr());
12810   C->setAssignmentOps(Exprs);
12811 }
12812 
12813 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12814   C->setLParenLoc(Record.readSourceLocation());
12815   unsigned NumVars = C->varlist_size();
12816   SmallVector<Expr *, 16> Vars;
12817   Vars.reserve(NumVars);
12818   for (unsigned i = 0; i != NumVars; ++i)
12819     Vars.push_back(Record.readSubExpr());
12820   C->setVarRefs(Vars);
12821 }
12822 
12823 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12824   C->setLParenLoc(Record.readSourceLocation());
12825   C->setDependencyKind(
12826       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12827   C->setDependencyLoc(Record.readSourceLocation());
12828   C->setColonLoc(Record.readSourceLocation());
12829   unsigned NumVars = C->varlist_size();
12830   SmallVector<Expr *, 16> Vars;
12831   Vars.reserve(NumVars);
12832   for (unsigned I = 0; I != NumVars; ++I)
12833     Vars.push_back(Record.readSubExpr());
12834   C->setVarRefs(Vars);
12835   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12836     C->setLoopData(I, Record.readSubExpr());
12837 }
12838 
12839 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12840   VisitOMPClauseWithPreInit(C);
12841   C->setDevice(Record.readSubExpr());
12842   C->setLParenLoc(Record.readSourceLocation());
12843 }
12844 
12845 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12846   C->setLParenLoc(Record.readSourceLocation());
12847   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12848     C->setMapTypeModifier(
12849         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12850     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12851   }
12852   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12853   DeclarationNameInfo DNI;
12854   Record.readDeclarationNameInfo(DNI);
12855   C->setMapperIdInfo(DNI);
12856   C->setMapType(
12857      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12858   C->setMapLoc(Record.readSourceLocation());
12859   C->setColonLoc(Record.readSourceLocation());
12860   auto NumVars = C->varlist_size();
12861   auto UniqueDecls = C->getUniqueDeclarationsNum();
12862   auto TotalLists = C->getTotalComponentListNum();
12863   auto TotalComponents = C->getTotalComponentsNum();
12864 
12865   SmallVector<Expr *, 16> Vars;
12866   Vars.reserve(NumVars);
12867   for (unsigned i = 0; i != NumVars; ++i)
12868     Vars.push_back(Record.readExpr());
12869   C->setVarRefs(Vars);
12870 
12871   SmallVector<Expr *, 16> UDMappers;
12872   UDMappers.reserve(NumVars);
12873   for (unsigned I = 0; I < NumVars; ++I)
12874     UDMappers.push_back(Record.readExpr());
12875   C->setUDMapperRefs(UDMappers);
12876 
12877   SmallVector<ValueDecl *, 16> Decls;
12878   Decls.reserve(UniqueDecls);
12879   for (unsigned i = 0; i < UniqueDecls; ++i)
12880     Decls.push_back(Record.readDeclAs<ValueDecl>());
12881   C->setUniqueDecls(Decls);
12882 
12883   SmallVector<unsigned, 16> ListsPerDecl;
12884   ListsPerDecl.reserve(UniqueDecls);
12885   for (unsigned i = 0; i < UniqueDecls; ++i)
12886     ListsPerDecl.push_back(Record.readInt());
12887   C->setDeclNumLists(ListsPerDecl);
12888 
12889   SmallVector<unsigned, 32> ListSizes;
12890   ListSizes.reserve(TotalLists);
12891   for (unsigned i = 0; i < TotalLists; ++i)
12892     ListSizes.push_back(Record.readInt());
12893   C->setComponentListSizes(ListSizes);
12894 
12895   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12896   Components.reserve(TotalComponents);
12897   for (unsigned i = 0; i < TotalComponents; ++i) {
12898     Expr *AssociatedExpr = Record.readExpr();
12899     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12900     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12901         AssociatedExpr, AssociatedDecl));
12902   }
12903   C->setComponents(Components, ListSizes);
12904 }
12905 
12906 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12907   C->setLParenLoc(Record.readSourceLocation());
12908   C->setColonLoc(Record.readSourceLocation());
12909   C->setAllocator(Record.readSubExpr());
12910   unsigned NumVars = C->varlist_size();
12911   SmallVector<Expr *, 16> Vars;
12912   Vars.reserve(NumVars);
12913   for (unsigned i = 0; i != NumVars; ++i)
12914     Vars.push_back(Record.readSubExpr());
12915   C->setVarRefs(Vars);
12916 }
12917 
12918 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12919   VisitOMPClauseWithPreInit(C);
12920   C->setNumTeams(Record.readSubExpr());
12921   C->setLParenLoc(Record.readSourceLocation());
12922 }
12923 
12924 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12925   VisitOMPClauseWithPreInit(C);
12926   C->setThreadLimit(Record.readSubExpr());
12927   C->setLParenLoc(Record.readSourceLocation());
12928 }
12929 
12930 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12931   C->setPriority(Record.readSubExpr());
12932   C->setLParenLoc(Record.readSourceLocation());
12933 }
12934 
12935 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12936   C->setGrainsize(Record.readSubExpr());
12937   C->setLParenLoc(Record.readSourceLocation());
12938 }
12939 
12940 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12941   C->setNumTasks(Record.readSubExpr());
12942   C->setLParenLoc(Record.readSourceLocation());
12943 }
12944 
12945 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12946   C->setHint(Record.readSubExpr());
12947   C->setLParenLoc(Record.readSourceLocation());
12948 }
12949 
12950 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12951   VisitOMPClauseWithPreInit(C);
12952   C->setDistScheduleKind(
12953       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12954   C->setChunkSize(Record.readSubExpr());
12955   C->setLParenLoc(Record.readSourceLocation());
12956   C->setDistScheduleKindLoc(Record.readSourceLocation());
12957   C->setCommaLoc(Record.readSourceLocation());
12958 }
12959 
12960 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12961   C->setDefaultmapKind(
12962        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12963   C->setDefaultmapModifier(
12964       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12965   C->setLParenLoc(Record.readSourceLocation());
12966   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12967   C->setDefaultmapKindLoc(Record.readSourceLocation());
12968 }
12969 
12970 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12971   C->setLParenLoc(Record.readSourceLocation());
12972   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12973   DeclarationNameInfo DNI;
12974   Record.readDeclarationNameInfo(DNI);
12975   C->setMapperIdInfo(DNI);
12976   auto NumVars = C->varlist_size();
12977   auto UniqueDecls = C->getUniqueDeclarationsNum();
12978   auto TotalLists = C->getTotalComponentListNum();
12979   auto TotalComponents = C->getTotalComponentsNum();
12980 
12981   SmallVector<Expr *, 16> Vars;
12982   Vars.reserve(NumVars);
12983   for (unsigned i = 0; i != NumVars; ++i)
12984     Vars.push_back(Record.readSubExpr());
12985   C->setVarRefs(Vars);
12986 
12987   SmallVector<Expr *, 16> UDMappers;
12988   UDMappers.reserve(NumVars);
12989   for (unsigned I = 0; I < NumVars; ++I)
12990     UDMappers.push_back(Record.readSubExpr());
12991   C->setUDMapperRefs(UDMappers);
12992 
12993   SmallVector<ValueDecl *, 16> Decls;
12994   Decls.reserve(UniqueDecls);
12995   for (unsigned i = 0; i < UniqueDecls; ++i)
12996     Decls.push_back(Record.readDeclAs<ValueDecl>());
12997   C->setUniqueDecls(Decls);
12998 
12999   SmallVector<unsigned, 16> ListsPerDecl;
13000   ListsPerDecl.reserve(UniqueDecls);
13001   for (unsigned i = 0; i < UniqueDecls; ++i)
13002     ListsPerDecl.push_back(Record.readInt());
13003   C->setDeclNumLists(ListsPerDecl);
13004 
13005   SmallVector<unsigned, 32> ListSizes;
13006   ListSizes.reserve(TotalLists);
13007   for (unsigned i = 0; i < TotalLists; ++i)
13008     ListSizes.push_back(Record.readInt());
13009   C->setComponentListSizes(ListSizes);
13010 
13011   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13012   Components.reserve(TotalComponents);
13013   for (unsigned i = 0; i < TotalComponents; ++i) {
13014     Expr *AssociatedExpr = Record.readSubExpr();
13015     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13016     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13017         AssociatedExpr, AssociatedDecl));
13018   }
13019   C->setComponents(Components, ListSizes);
13020 }
13021 
13022 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
13023   C->setLParenLoc(Record.readSourceLocation());
13024   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13025   DeclarationNameInfo DNI;
13026   Record.readDeclarationNameInfo(DNI);
13027   C->setMapperIdInfo(DNI);
13028   auto NumVars = C->varlist_size();
13029   auto UniqueDecls = C->getUniqueDeclarationsNum();
13030   auto TotalLists = C->getTotalComponentListNum();
13031   auto TotalComponents = C->getTotalComponentsNum();
13032 
13033   SmallVector<Expr *, 16> Vars;
13034   Vars.reserve(NumVars);
13035   for (unsigned i = 0; i != NumVars; ++i)
13036     Vars.push_back(Record.readSubExpr());
13037   C->setVarRefs(Vars);
13038 
13039   SmallVector<Expr *, 16> UDMappers;
13040   UDMappers.reserve(NumVars);
13041   for (unsigned I = 0; I < NumVars; ++I)
13042     UDMappers.push_back(Record.readSubExpr());
13043   C->setUDMapperRefs(UDMappers);
13044 
13045   SmallVector<ValueDecl *, 16> Decls;
13046   Decls.reserve(UniqueDecls);
13047   for (unsigned i = 0; i < UniqueDecls; ++i)
13048     Decls.push_back(Record.readDeclAs<ValueDecl>());
13049   C->setUniqueDecls(Decls);
13050 
13051   SmallVector<unsigned, 16> ListsPerDecl;
13052   ListsPerDecl.reserve(UniqueDecls);
13053   for (unsigned i = 0; i < UniqueDecls; ++i)
13054     ListsPerDecl.push_back(Record.readInt());
13055   C->setDeclNumLists(ListsPerDecl);
13056 
13057   SmallVector<unsigned, 32> ListSizes;
13058   ListSizes.reserve(TotalLists);
13059   for (unsigned i = 0; i < TotalLists; ++i)
13060     ListSizes.push_back(Record.readInt());
13061   C->setComponentListSizes(ListSizes);
13062 
13063   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13064   Components.reserve(TotalComponents);
13065   for (unsigned i = 0; i < TotalComponents; ++i) {
13066     Expr *AssociatedExpr = Record.readSubExpr();
13067     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13068     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13069         AssociatedExpr, AssociatedDecl));
13070   }
13071   C->setComponents(Components, ListSizes);
13072 }
13073 
13074 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13075   C->setLParenLoc(Record.readSourceLocation());
13076   auto NumVars = C->varlist_size();
13077   auto UniqueDecls = C->getUniqueDeclarationsNum();
13078   auto TotalLists = C->getTotalComponentListNum();
13079   auto TotalComponents = C->getTotalComponentsNum();
13080 
13081   SmallVector<Expr *, 16> Vars;
13082   Vars.reserve(NumVars);
13083   for (unsigned i = 0; i != NumVars; ++i)
13084     Vars.push_back(Record.readSubExpr());
13085   C->setVarRefs(Vars);
13086   Vars.clear();
13087   for (unsigned i = 0; i != NumVars; ++i)
13088     Vars.push_back(Record.readSubExpr());
13089   C->setPrivateCopies(Vars);
13090   Vars.clear();
13091   for (unsigned i = 0; i != NumVars; ++i)
13092     Vars.push_back(Record.readSubExpr());
13093   C->setInits(Vars);
13094 
13095   SmallVector<ValueDecl *, 16> Decls;
13096   Decls.reserve(UniqueDecls);
13097   for (unsigned i = 0; i < UniqueDecls; ++i)
13098     Decls.push_back(Record.readDeclAs<ValueDecl>());
13099   C->setUniqueDecls(Decls);
13100 
13101   SmallVector<unsigned, 16> ListsPerDecl;
13102   ListsPerDecl.reserve(UniqueDecls);
13103   for (unsigned i = 0; i < UniqueDecls; ++i)
13104     ListsPerDecl.push_back(Record.readInt());
13105   C->setDeclNumLists(ListsPerDecl);
13106 
13107   SmallVector<unsigned, 32> ListSizes;
13108   ListSizes.reserve(TotalLists);
13109   for (unsigned i = 0; i < TotalLists; ++i)
13110     ListSizes.push_back(Record.readInt());
13111   C->setComponentListSizes(ListSizes);
13112 
13113   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13114   Components.reserve(TotalComponents);
13115   for (unsigned i = 0; i < TotalComponents; ++i) {
13116     Expr *AssociatedExpr = Record.readSubExpr();
13117     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13118     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13119         AssociatedExpr, AssociatedDecl));
13120   }
13121   C->setComponents(Components, ListSizes);
13122 }
13123 
13124 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13125   C->setLParenLoc(Record.readSourceLocation());
13126   auto NumVars = C->varlist_size();
13127   auto UniqueDecls = C->getUniqueDeclarationsNum();
13128   auto TotalLists = C->getTotalComponentListNum();
13129   auto TotalComponents = C->getTotalComponentsNum();
13130 
13131   SmallVector<Expr *, 16> Vars;
13132   Vars.reserve(NumVars);
13133   for (unsigned i = 0; i != NumVars; ++i)
13134     Vars.push_back(Record.readSubExpr());
13135   C->setVarRefs(Vars);
13136   Vars.clear();
13137 
13138   SmallVector<ValueDecl *, 16> Decls;
13139   Decls.reserve(UniqueDecls);
13140   for (unsigned i = 0; i < UniqueDecls; ++i)
13141     Decls.push_back(Record.readDeclAs<ValueDecl>());
13142   C->setUniqueDecls(Decls);
13143 
13144   SmallVector<unsigned, 16> ListsPerDecl;
13145   ListsPerDecl.reserve(UniqueDecls);
13146   for (unsigned i = 0; i < UniqueDecls; ++i)
13147     ListsPerDecl.push_back(Record.readInt());
13148   C->setDeclNumLists(ListsPerDecl);
13149 
13150   SmallVector<unsigned, 32> ListSizes;
13151   ListSizes.reserve(TotalLists);
13152   for (unsigned i = 0; i < TotalLists; ++i)
13153     ListSizes.push_back(Record.readInt());
13154   C->setComponentListSizes(ListSizes);
13155 
13156   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13157   Components.reserve(TotalComponents);
13158   for (unsigned i = 0; i < TotalComponents; ++i) {
13159     Expr *AssociatedExpr = Record.readSubExpr();
13160     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13161     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13162         AssociatedExpr, AssociatedDecl));
13163   }
13164   C->setComponents(Components, ListSizes);
13165 }
13166