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     // FIXME: The FileID should be created from the FileEntryRef.
1518     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1519                                         ID, BaseOffset + Record[0]);
1520     SrcMgr::FileInfo &FileInfo =
1521           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1522     FileInfo.NumCreatedFIDs = Record[5];
1523     if (Record[3])
1524       FileInfo.setHasLineDirectives();
1525 
1526     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1527     unsigned NumFileDecls = Record[7];
1528     if (NumFileDecls && ContextObj) {
1529       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1530       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1531                                                              NumFileDecls));
1532     }
1533 
1534     const SrcMgr::ContentCache *ContentCache
1535       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1536     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1537         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1538         !ContentCache->getRawBuffer()) {
1539       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1540       if (!Buffer)
1541         return true;
1542       SourceMgr.overrideFileContents(File, std::move(Buffer));
1543     }
1544 
1545     break;
1546   }
1547 
1548   case SM_SLOC_BUFFER_ENTRY: {
1549     const char *Name = Blob.data();
1550     unsigned Offset = Record[0];
1551     SrcMgr::CharacteristicKind
1552       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1553     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1554     if (IncludeLoc.isInvalid() && F->isModule()) {
1555       IncludeLoc = getImportLocation(F);
1556     }
1557 
1558     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1559     if (!Buffer)
1560       return true;
1561     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1562                            BaseOffset + Offset, IncludeLoc);
1563     break;
1564   }
1565 
1566   case SM_SLOC_EXPANSION_ENTRY: {
1567     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1568     SourceMgr.createExpansionLoc(SpellingLoc,
1569                                      ReadSourceLocation(*F, Record[2]),
1570                                      ReadSourceLocation(*F, Record[3]),
1571                                      Record[5],
1572                                      Record[4],
1573                                      ID,
1574                                      BaseOffset + Record[0]);
1575     break;
1576   }
1577   }
1578 
1579   return false;
1580 }
1581 
1582 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1583   if (ID == 0)
1584     return std::make_pair(SourceLocation(), "");
1585 
1586   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1587     Error("source location entry ID out-of-range for AST file");
1588     return std::make_pair(SourceLocation(), "");
1589   }
1590 
1591   // Find which module file this entry lands in.
1592   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1593   if (!M->isModule())
1594     return std::make_pair(SourceLocation(), "");
1595 
1596   // FIXME: Can we map this down to a particular submodule? That would be
1597   // ideal.
1598   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1599 }
1600 
1601 /// Find the location where the module F is imported.
1602 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1603   if (F->ImportLoc.isValid())
1604     return F->ImportLoc;
1605 
1606   // Otherwise we have a PCH. It's considered to be "imported" at the first
1607   // location of its includer.
1608   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1609     // Main file is the importer.
1610     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1611     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1612   }
1613   return F->ImportedBy[0]->FirstLoc;
1614 }
1615 
1616 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1617 /// the abbreviations that are at the top of the block and then leave the cursor
1618 /// pointing into the block.
1619 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1620   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1621     // FIXME this drops errors on the floor.
1622     consumeError(std::move(Err));
1623     return true;
1624   }
1625 
1626   while (true) {
1627     uint64_t Offset = Cursor.GetCurrentBitNo();
1628     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1629     if (!MaybeCode) {
1630       // FIXME this drops errors on the floor.
1631       consumeError(MaybeCode.takeError());
1632       return true;
1633     }
1634     unsigned Code = MaybeCode.get();
1635 
1636     // We expect all abbrevs to be at the start of the block.
1637     if (Code != llvm::bitc::DEFINE_ABBREV) {
1638       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1639         // FIXME this drops errors on the floor.
1640         consumeError(std::move(Err));
1641         return true;
1642       }
1643       return false;
1644     }
1645     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1646       // FIXME this drops errors on the floor.
1647       consumeError(std::move(Err));
1648       return true;
1649     }
1650   }
1651 }
1652 
1653 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1654                            unsigned &Idx) {
1655   Token Tok;
1656   Tok.startToken();
1657   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1658   Tok.setLength(Record[Idx++]);
1659   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1660     Tok.setIdentifierInfo(II);
1661   Tok.setKind((tok::TokenKind)Record[Idx++]);
1662   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1663   return Tok;
1664 }
1665 
1666 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1667   BitstreamCursor &Stream = F.MacroCursor;
1668 
1669   // Keep track of where we are in the stream, then jump back there
1670   // after reading this macro.
1671   SavedStreamPosition SavedPosition(Stream);
1672 
1673   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1674     // FIXME this drops errors on the floor.
1675     consumeError(std::move(Err));
1676     return nullptr;
1677   }
1678   RecordData Record;
1679   SmallVector<IdentifierInfo*, 16> MacroParams;
1680   MacroInfo *Macro = nullptr;
1681 
1682   while (true) {
1683     // Advance to the next record, but if we get to the end of the block, don't
1684     // pop it (removing all the abbreviations from the cursor) since we want to
1685     // be able to reseek within the block and read entries.
1686     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1687     Expected<llvm::BitstreamEntry> MaybeEntry =
1688         Stream.advanceSkippingSubblocks(Flags);
1689     if (!MaybeEntry) {
1690       Error(MaybeEntry.takeError());
1691       return Macro;
1692     }
1693     llvm::BitstreamEntry Entry = MaybeEntry.get();
1694 
1695     switch (Entry.Kind) {
1696     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1697     case llvm::BitstreamEntry::Error:
1698       Error("malformed block record in AST file");
1699       return Macro;
1700     case llvm::BitstreamEntry::EndBlock:
1701       return Macro;
1702     case llvm::BitstreamEntry::Record:
1703       // The interesting case.
1704       break;
1705     }
1706 
1707     // Read a record.
1708     Record.clear();
1709     PreprocessorRecordTypes RecType;
1710     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1711       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1712     else {
1713       Error(MaybeRecType.takeError());
1714       return Macro;
1715     }
1716     switch (RecType) {
1717     case PP_MODULE_MACRO:
1718     case PP_MACRO_DIRECTIVE_HISTORY:
1719       return Macro;
1720 
1721     case PP_MACRO_OBJECT_LIKE:
1722     case PP_MACRO_FUNCTION_LIKE: {
1723       // If we already have a macro, that means that we've hit the end
1724       // of the definition of the macro we were looking for. We're
1725       // done.
1726       if (Macro)
1727         return Macro;
1728 
1729       unsigned NextIndex = 1; // Skip identifier ID.
1730       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1731       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1732       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1733       MI->setIsUsed(Record[NextIndex++]);
1734       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1735 
1736       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1737         // Decode function-like macro info.
1738         bool isC99VarArgs = Record[NextIndex++];
1739         bool isGNUVarArgs = Record[NextIndex++];
1740         bool hasCommaPasting = Record[NextIndex++];
1741         MacroParams.clear();
1742         unsigned NumArgs = Record[NextIndex++];
1743         for (unsigned i = 0; i != NumArgs; ++i)
1744           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1745 
1746         // Install function-like macro info.
1747         MI->setIsFunctionLike();
1748         if (isC99VarArgs) MI->setIsC99Varargs();
1749         if (isGNUVarArgs) MI->setIsGNUVarargs();
1750         if (hasCommaPasting) MI->setHasCommaPasting();
1751         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1752       }
1753 
1754       // Remember that we saw this macro last so that we add the tokens that
1755       // form its body to it.
1756       Macro = MI;
1757 
1758       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1759           Record[NextIndex]) {
1760         // We have a macro definition. Register the association
1761         PreprocessedEntityID
1762             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1763         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1764         PreprocessingRecord::PPEntityID PPID =
1765             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1766         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1767             PPRec.getPreprocessedEntity(PPID));
1768         if (PPDef)
1769           PPRec.RegisterMacroDefinition(Macro, PPDef);
1770       }
1771 
1772       ++NumMacrosRead;
1773       break;
1774     }
1775 
1776     case PP_TOKEN: {
1777       // If we see a TOKEN before a PP_MACRO_*, then the file is
1778       // erroneous, just pretend we didn't see this.
1779       if (!Macro) break;
1780 
1781       unsigned Idx = 0;
1782       Token Tok = ReadToken(F, Record, Idx);
1783       Macro->AddTokenToBody(Tok);
1784       break;
1785     }
1786     }
1787   }
1788 }
1789 
1790 PreprocessedEntityID
1791 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1792                                          unsigned LocalID) const {
1793   if (!M.ModuleOffsetMap.empty())
1794     ReadModuleOffsetMap(M);
1795 
1796   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1797     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1798   assert(I != M.PreprocessedEntityRemap.end()
1799          && "Invalid index into preprocessed entity index remap");
1800 
1801   return LocalID + I->second;
1802 }
1803 
1804 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1805   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1806 }
1807 
1808 HeaderFileInfoTrait::internal_key_type
1809 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1810   internal_key_type ikey = {FE->getSize(),
1811                             M.HasTimestamps ? FE->getModificationTime() : 0,
1812                             FE->getName(), /*Imported*/ false};
1813   return ikey;
1814 }
1815 
1816 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1817   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1818     return false;
1819 
1820   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1821     return true;
1822 
1823   // Determine whether the actual files are equivalent.
1824   FileManager &FileMgr = Reader.getFileManager();
1825   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1826     if (!Key.Imported) {
1827       if (auto File = FileMgr.getFile(Key.Filename))
1828         return *File;
1829       return nullptr;
1830     }
1831 
1832     std::string Resolved = Key.Filename;
1833     Reader.ResolveImportedPath(M, Resolved);
1834     if (auto File = FileMgr.getFile(Resolved))
1835       return *File;
1836     return nullptr;
1837   };
1838 
1839   const FileEntry *FEA = GetFile(a);
1840   const FileEntry *FEB = GetFile(b);
1841   return FEA && FEA == FEB;
1842 }
1843 
1844 std::pair<unsigned, unsigned>
1845 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1846   using namespace llvm::support;
1847 
1848   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1849   unsigned DataLen = (unsigned) *d++;
1850   return std::make_pair(KeyLen, DataLen);
1851 }
1852 
1853 HeaderFileInfoTrait::internal_key_type
1854 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1855   using namespace llvm::support;
1856 
1857   internal_key_type ikey;
1858   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1859   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1860   ikey.Filename = (const char *)d;
1861   ikey.Imported = true;
1862   return ikey;
1863 }
1864 
1865 HeaderFileInfoTrait::data_type
1866 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1867                               unsigned DataLen) {
1868   using namespace llvm::support;
1869 
1870   const unsigned char *End = d + DataLen;
1871   HeaderFileInfo HFI;
1872   unsigned Flags = *d++;
1873   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1874   HFI.isImport |= (Flags >> 5) & 0x01;
1875   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1876   HFI.DirInfo = (Flags >> 1) & 0x07;
1877   HFI.IndexHeaderMapHeader = Flags & 0x01;
1878   // FIXME: Find a better way to handle this. Maybe just store a
1879   // "has been included" flag?
1880   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1881                              HFI.NumIncludes);
1882   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1883       M, endian::readNext<uint32_t, little, unaligned>(d));
1884   if (unsigned FrameworkOffset =
1885           endian::readNext<uint32_t, little, unaligned>(d)) {
1886     // The framework offset is 1 greater than the actual offset,
1887     // since 0 is used as an indicator for "no framework name".
1888     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1889     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1890   }
1891 
1892   assert((End - d) % 4 == 0 &&
1893          "Wrong data length in HeaderFileInfo deserialization");
1894   while (d != End) {
1895     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1896     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1897     LocalSMID >>= 2;
1898 
1899     // This header is part of a module. Associate it with the module to enable
1900     // implicit module import.
1901     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1902     Module *Mod = Reader.getSubmodule(GlobalSMID);
1903     FileManager &FileMgr = Reader.getFileManager();
1904     ModuleMap &ModMap =
1905         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1906 
1907     std::string Filename = key.Filename;
1908     if (key.Imported)
1909       Reader.ResolveImportedPath(M, Filename);
1910     // FIXME: This is not always the right filename-as-written, but we're not
1911     // going to use this information to rebuild the module, so it doesn't make
1912     // a lot of difference.
1913     Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1914     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1915     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1916   }
1917 
1918   // This HeaderFileInfo was externally loaded.
1919   HFI.External = true;
1920   HFI.IsValid = true;
1921   return HFI;
1922 }
1923 
1924 void ASTReader::addPendingMacro(IdentifierInfo *II,
1925                                 ModuleFile *M,
1926                                 uint64_t MacroDirectivesOffset) {
1927   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1928   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1929 }
1930 
1931 void ASTReader::ReadDefinedMacros() {
1932   // Note that we are loading defined macros.
1933   Deserializing Macros(this);
1934 
1935   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1936     BitstreamCursor &MacroCursor = I.MacroCursor;
1937 
1938     // If there was no preprocessor block, skip this file.
1939     if (MacroCursor.getBitcodeBytes().empty())
1940       continue;
1941 
1942     BitstreamCursor Cursor = MacroCursor;
1943     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1944       Error(std::move(Err));
1945       return;
1946     }
1947 
1948     RecordData Record;
1949     while (true) {
1950       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1951       if (!MaybeE) {
1952         Error(MaybeE.takeError());
1953         return;
1954       }
1955       llvm::BitstreamEntry E = MaybeE.get();
1956 
1957       switch (E.Kind) {
1958       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1959       case llvm::BitstreamEntry::Error:
1960         Error("malformed block record in AST file");
1961         return;
1962       case llvm::BitstreamEntry::EndBlock:
1963         goto NextCursor;
1964 
1965       case llvm::BitstreamEntry::Record: {
1966         Record.clear();
1967         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1968         if (!MaybeRecord) {
1969           Error(MaybeRecord.takeError());
1970           return;
1971         }
1972         switch (MaybeRecord.get()) {
1973         default:  // Default behavior: ignore.
1974           break;
1975 
1976         case PP_MACRO_OBJECT_LIKE:
1977         case PP_MACRO_FUNCTION_LIKE: {
1978           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1979           if (II->isOutOfDate())
1980             updateOutOfDateIdentifier(*II);
1981           break;
1982         }
1983 
1984         case PP_TOKEN:
1985           // Ignore tokens.
1986           break;
1987         }
1988         break;
1989       }
1990       }
1991     }
1992     NextCursor:  ;
1993   }
1994 }
1995 
1996 namespace {
1997 
1998   /// Visitor class used to look up identifirs in an AST file.
1999   class IdentifierLookupVisitor {
2000     StringRef Name;
2001     unsigned NameHash;
2002     unsigned PriorGeneration;
2003     unsigned &NumIdentifierLookups;
2004     unsigned &NumIdentifierLookupHits;
2005     IdentifierInfo *Found = nullptr;
2006 
2007   public:
2008     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2009                             unsigned &NumIdentifierLookups,
2010                             unsigned &NumIdentifierLookupHits)
2011       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2012         PriorGeneration(PriorGeneration),
2013         NumIdentifierLookups(NumIdentifierLookups),
2014         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2015 
2016     bool operator()(ModuleFile &M) {
2017       // If we've already searched this module file, skip it now.
2018       if (M.Generation <= PriorGeneration)
2019         return true;
2020 
2021       ASTIdentifierLookupTable *IdTable
2022         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2023       if (!IdTable)
2024         return false;
2025 
2026       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2027                                      Found);
2028       ++NumIdentifierLookups;
2029       ASTIdentifierLookupTable::iterator Pos =
2030           IdTable->find_hashed(Name, NameHash, &Trait);
2031       if (Pos == IdTable->end())
2032         return false;
2033 
2034       // Dereferencing the iterator has the effect of building the
2035       // IdentifierInfo node and populating it with the various
2036       // declarations it needs.
2037       ++NumIdentifierLookupHits;
2038       Found = *Pos;
2039       return true;
2040     }
2041 
2042     // Retrieve the identifier info found within the module
2043     // files.
2044     IdentifierInfo *getIdentifierInfo() const { return Found; }
2045   };
2046 
2047 } // namespace
2048 
2049 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2050   // Note that we are loading an identifier.
2051   Deserializing AnIdentifier(this);
2052 
2053   unsigned PriorGeneration = 0;
2054   if (getContext().getLangOpts().Modules)
2055     PriorGeneration = IdentifierGeneration[&II];
2056 
2057   // If there is a global index, look there first to determine which modules
2058   // provably do not have any results for this identifier.
2059   GlobalModuleIndex::HitSet Hits;
2060   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2061   if (!loadGlobalIndex()) {
2062     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2063       HitsPtr = &Hits;
2064     }
2065   }
2066 
2067   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2068                                   NumIdentifierLookups,
2069                                   NumIdentifierLookupHits);
2070   ModuleMgr.visit(Visitor, HitsPtr);
2071   markIdentifierUpToDate(&II);
2072 }
2073 
2074 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2075   if (!II)
2076     return;
2077 
2078   II->setOutOfDate(false);
2079 
2080   // Update the generation for this identifier.
2081   if (getContext().getLangOpts().Modules)
2082     IdentifierGeneration[II] = getGeneration();
2083 }
2084 
2085 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2086                                     const PendingMacroInfo &PMInfo) {
2087   ModuleFile &M = *PMInfo.M;
2088 
2089   BitstreamCursor &Cursor = M.MacroCursor;
2090   SavedStreamPosition SavedPosition(Cursor);
2091   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2092     Error(std::move(Err));
2093     return;
2094   }
2095 
2096   struct ModuleMacroRecord {
2097     SubmoduleID SubModID;
2098     MacroInfo *MI;
2099     SmallVector<SubmoduleID, 8> Overrides;
2100   };
2101   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2102 
2103   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2104   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2105   // macro histroy.
2106   RecordData Record;
2107   while (true) {
2108     Expected<llvm::BitstreamEntry> MaybeEntry =
2109         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2110     if (!MaybeEntry) {
2111       Error(MaybeEntry.takeError());
2112       return;
2113     }
2114     llvm::BitstreamEntry Entry = MaybeEntry.get();
2115 
2116     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2117       Error("malformed block record in AST file");
2118       return;
2119     }
2120 
2121     Record.clear();
2122     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2123     if (!MaybePP) {
2124       Error(MaybePP.takeError());
2125       return;
2126     }
2127     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2128     case PP_MACRO_DIRECTIVE_HISTORY:
2129       break;
2130 
2131     case PP_MODULE_MACRO: {
2132       ModuleMacros.push_back(ModuleMacroRecord());
2133       auto &Info = ModuleMacros.back();
2134       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2135       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2136       for (int I = 2, N = Record.size(); I != N; ++I)
2137         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2138       continue;
2139     }
2140 
2141     default:
2142       Error("malformed block record in AST file");
2143       return;
2144     }
2145 
2146     // We found the macro directive history; that's the last record
2147     // for this macro.
2148     break;
2149   }
2150 
2151   // Module macros are listed in reverse dependency order.
2152   {
2153     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2154     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2155     for (auto &MMR : ModuleMacros) {
2156       Overrides.clear();
2157       for (unsigned ModID : MMR.Overrides) {
2158         Module *Mod = getSubmodule(ModID);
2159         auto *Macro = PP.getModuleMacro(Mod, II);
2160         assert(Macro && "missing definition for overridden macro");
2161         Overrides.push_back(Macro);
2162       }
2163 
2164       bool Inserted = false;
2165       Module *Owner = getSubmodule(MMR.SubModID);
2166       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2167     }
2168   }
2169 
2170   // Don't read the directive history for a module; we don't have anywhere
2171   // to put it.
2172   if (M.isModule())
2173     return;
2174 
2175   // Deserialize the macro directives history in reverse source-order.
2176   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2177   unsigned Idx = 0, N = Record.size();
2178   while (Idx < N) {
2179     MacroDirective *MD = nullptr;
2180     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2181     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2182     switch (K) {
2183     case MacroDirective::MD_Define: {
2184       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2185       MD = PP.AllocateDefMacroDirective(MI, Loc);
2186       break;
2187     }
2188     case MacroDirective::MD_Undefine:
2189       MD = PP.AllocateUndefMacroDirective(Loc);
2190       break;
2191     case MacroDirective::MD_Visibility:
2192       bool isPublic = Record[Idx++];
2193       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2194       break;
2195     }
2196 
2197     if (!Latest)
2198       Latest = MD;
2199     if (Earliest)
2200       Earliest->setPrevious(MD);
2201     Earliest = MD;
2202   }
2203 
2204   if (Latest)
2205     PP.setLoadedMacroDirective(II, Earliest, Latest);
2206 }
2207 
2208 ASTReader::InputFileInfo
2209 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2210   // Go find this input file.
2211   BitstreamCursor &Cursor = F.InputFilesCursor;
2212   SavedStreamPosition SavedPosition(Cursor);
2213   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2214     // FIXME this drops errors on the floor.
2215     consumeError(std::move(Err));
2216   }
2217 
2218   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2219   if (!MaybeCode) {
2220     // FIXME this drops errors on the floor.
2221     consumeError(MaybeCode.takeError());
2222   }
2223   unsigned Code = MaybeCode.get();
2224   RecordData Record;
2225   StringRef Blob;
2226 
2227   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2228     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2229            "invalid record type for input file");
2230   else {
2231     // FIXME this drops errors on the floor.
2232     consumeError(Maybe.takeError());
2233   }
2234 
2235   assert(Record[0] == ID && "Bogus stored ID or offset");
2236   InputFileInfo R;
2237   R.StoredSize = static_cast<off_t>(Record[1]);
2238   R.StoredTime = static_cast<time_t>(Record[2]);
2239   R.Overridden = static_cast<bool>(Record[3]);
2240   R.Transient = static_cast<bool>(Record[4]);
2241   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2242   R.Filename = Blob;
2243   ResolveImportedPath(F, R.Filename);
2244   return R;
2245 }
2246 
2247 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2248 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2249   // If this ID is bogus, just return an empty input file.
2250   if (ID == 0 || ID > F.InputFilesLoaded.size())
2251     return InputFile();
2252 
2253   // If we've already loaded this input file, return it.
2254   if (F.InputFilesLoaded[ID-1].getFile())
2255     return F.InputFilesLoaded[ID-1];
2256 
2257   if (F.InputFilesLoaded[ID-1].isNotFound())
2258     return InputFile();
2259 
2260   // Go find this input file.
2261   BitstreamCursor &Cursor = F.InputFilesCursor;
2262   SavedStreamPosition SavedPosition(Cursor);
2263   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2264     // FIXME this drops errors on the floor.
2265     consumeError(std::move(Err));
2266   }
2267 
2268   InputFileInfo FI = readInputFileInfo(F, ID);
2269   off_t StoredSize = FI.StoredSize;
2270   time_t StoredTime = FI.StoredTime;
2271   bool Overridden = FI.Overridden;
2272   bool Transient = FI.Transient;
2273   StringRef Filename = FI.Filename;
2274 
2275   const FileEntry *File = nullptr;
2276   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2277     File = *FE;
2278 
2279   // If we didn't find the file, resolve it relative to the
2280   // original directory from which this AST file was created.
2281   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2282       F.OriginalDir != F.BaseDirectory) {
2283     std::string Resolved = resolveFileRelativeToOriginalDir(
2284         Filename, F.OriginalDir, F.BaseDirectory);
2285     if (!Resolved.empty())
2286       if (auto FE = FileMgr.getFile(Resolved))
2287         File = *FE;
2288   }
2289 
2290   // For an overridden file, create a virtual file with the stored
2291   // size/timestamp.
2292   if ((Overridden || Transient) && File == nullptr)
2293     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2294 
2295   if (File == nullptr) {
2296     if (Complain) {
2297       std::string ErrorStr = "could not find file '";
2298       ErrorStr += Filename;
2299       ErrorStr += "' referenced by AST file '";
2300       ErrorStr += F.FileName;
2301       ErrorStr += "'";
2302       Error(ErrorStr);
2303     }
2304     // Record that we didn't find the file.
2305     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2306     return InputFile();
2307   }
2308 
2309   // Check if there was a request to override the contents of the file
2310   // that was part of the precompiled header. Overriding such a file
2311   // can lead to problems when lexing using the source locations from the
2312   // PCH.
2313   SourceManager &SM = getSourceManager();
2314   // FIXME: Reject if the overrides are different.
2315   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2316     if (Complain)
2317       Error(diag::err_fe_pch_file_overridden, Filename);
2318     // After emitting the diagnostic, recover by disabling the override so
2319     // that the original file will be used.
2320     //
2321     // FIXME: This recovery is just as broken as the original state; there may
2322     // be another precompiled module that's using the overridden contents, or
2323     // we might be half way through parsing it. Instead, we should treat the
2324     // overridden contents as belonging to a separate FileEntry.
2325     SM.disableFileContentsOverride(File);
2326     // The FileEntry is a virtual file entry with the size of the contents
2327     // that would override the original contents. Set it to the original's
2328     // size/time.
2329     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2330                             StoredSize, StoredTime);
2331   }
2332 
2333   bool IsOutOfDate = false;
2334 
2335   // For an overridden file, there is nothing to validate.
2336   if (!Overridden && //
2337       (StoredSize != File->getSize() ||
2338        (StoredTime && StoredTime != File->getModificationTime() &&
2339         !DisableValidation)
2340        )) {
2341     if (Complain) {
2342       // Build a list of the PCH imports that got us here (in reverse).
2343       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2344       while (!ImportStack.back()->ImportedBy.empty())
2345         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2346 
2347       // The top-level PCH is stale.
2348       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2349       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2350       if (DiagnosticKind == 0)
2351         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2352       else if (DiagnosticKind == 1)
2353         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2354       else
2355         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2356 
2357       // Print the import stack.
2358       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2359         Diag(diag::note_pch_required_by)
2360           << Filename << ImportStack[0]->FileName;
2361         for (unsigned I = 1; I < ImportStack.size(); ++I)
2362           Diag(diag::note_pch_required_by)
2363             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2364       }
2365 
2366       if (!Diags.isDiagnosticInFlight())
2367         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2368     }
2369 
2370     IsOutOfDate = true;
2371   }
2372   // FIXME: If the file is overridden and we've already opened it,
2373   // issue an error (or split it into a separate FileEntry).
2374 
2375   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2376 
2377   // Note that we've loaded this input file.
2378   F.InputFilesLoaded[ID-1] = IF;
2379   return IF;
2380 }
2381 
2382 /// If we are loading a relocatable PCH or module file, and the filename
2383 /// is not an absolute path, add the system or module root to the beginning of
2384 /// the file name.
2385 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2386   // Resolve relative to the base directory, if we have one.
2387   if (!M.BaseDirectory.empty())
2388     return ResolveImportedPath(Filename, M.BaseDirectory);
2389 }
2390 
2391 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2392   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2393     return;
2394 
2395   SmallString<128> Buffer;
2396   llvm::sys::path::append(Buffer, Prefix, Filename);
2397   Filename.assign(Buffer.begin(), Buffer.end());
2398 }
2399 
2400 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2401   switch (ARR) {
2402   case ASTReader::Failure: return true;
2403   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2404   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2405   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2406   case ASTReader::ConfigurationMismatch:
2407     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2408   case ASTReader::HadErrors: return true;
2409   case ASTReader::Success: return false;
2410   }
2411 
2412   llvm_unreachable("unknown ASTReadResult");
2413 }
2414 
2415 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2416     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2417     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2418     std::string &SuggestedPredefines) {
2419   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2420     // FIXME this drops errors on the floor.
2421     consumeError(std::move(Err));
2422     return Failure;
2423   }
2424 
2425   // Read all of the records in the options block.
2426   RecordData Record;
2427   ASTReadResult Result = Success;
2428   while (true) {
2429     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2430     if (!MaybeEntry) {
2431       // FIXME this drops errors on the floor.
2432       consumeError(MaybeEntry.takeError());
2433       return Failure;
2434     }
2435     llvm::BitstreamEntry Entry = MaybeEntry.get();
2436 
2437     switch (Entry.Kind) {
2438     case llvm::BitstreamEntry::Error:
2439     case llvm::BitstreamEntry::SubBlock:
2440       return Failure;
2441 
2442     case llvm::BitstreamEntry::EndBlock:
2443       return Result;
2444 
2445     case llvm::BitstreamEntry::Record:
2446       // The interesting case.
2447       break;
2448     }
2449 
2450     // Read and process a record.
2451     Record.clear();
2452     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2453     if (!MaybeRecordType) {
2454       // FIXME this drops errors on the floor.
2455       consumeError(MaybeRecordType.takeError());
2456       return Failure;
2457     }
2458     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2459     case LANGUAGE_OPTIONS: {
2460       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2461       if (ParseLanguageOptions(Record, Complain, Listener,
2462                                AllowCompatibleConfigurationMismatch))
2463         Result = ConfigurationMismatch;
2464       break;
2465     }
2466 
2467     case TARGET_OPTIONS: {
2468       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2469       if (ParseTargetOptions(Record, Complain, Listener,
2470                              AllowCompatibleConfigurationMismatch))
2471         Result = ConfigurationMismatch;
2472       break;
2473     }
2474 
2475     case FILE_SYSTEM_OPTIONS: {
2476       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2477       if (!AllowCompatibleConfigurationMismatch &&
2478           ParseFileSystemOptions(Record, Complain, Listener))
2479         Result = ConfigurationMismatch;
2480       break;
2481     }
2482 
2483     case HEADER_SEARCH_OPTIONS: {
2484       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2485       if (!AllowCompatibleConfigurationMismatch &&
2486           ParseHeaderSearchOptions(Record, Complain, Listener))
2487         Result = ConfigurationMismatch;
2488       break;
2489     }
2490 
2491     case PREPROCESSOR_OPTIONS:
2492       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2493       if (!AllowCompatibleConfigurationMismatch &&
2494           ParsePreprocessorOptions(Record, Complain, Listener,
2495                                    SuggestedPredefines))
2496         Result = ConfigurationMismatch;
2497       break;
2498     }
2499   }
2500 }
2501 
2502 ASTReader::ASTReadResult
2503 ASTReader::ReadControlBlock(ModuleFile &F,
2504                             SmallVectorImpl<ImportedModule> &Loaded,
2505                             const ModuleFile *ImportedBy,
2506                             unsigned ClientLoadCapabilities) {
2507   BitstreamCursor &Stream = F.Stream;
2508   ASTReadResult Result = Success;
2509 
2510   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2511     Error(std::move(Err));
2512     return Failure;
2513   }
2514 
2515   // Lambda to read the unhashed control block the first time it's called.
2516   //
2517   // For PCM files, the unhashed control block cannot be read until after the
2518   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2519   // need to look ahead before reading the IMPORTS record.  For consistency,
2520   // this block is always read somehow (see BitstreamEntry::EndBlock).
2521   bool HasReadUnhashedControlBlock = false;
2522   auto readUnhashedControlBlockOnce = [&]() {
2523     if (!HasReadUnhashedControlBlock) {
2524       HasReadUnhashedControlBlock = true;
2525       if (ASTReadResult Result =
2526               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2527         return Result;
2528     }
2529     return Success;
2530   };
2531 
2532   // Read all of the records and blocks in the control block.
2533   RecordData Record;
2534   unsigned NumInputs = 0;
2535   unsigned NumUserInputs = 0;
2536   StringRef BaseDirectoryAsWritten;
2537   while (true) {
2538     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2539     if (!MaybeEntry) {
2540       Error(MaybeEntry.takeError());
2541       return Failure;
2542     }
2543     llvm::BitstreamEntry Entry = MaybeEntry.get();
2544 
2545     switch (Entry.Kind) {
2546     case llvm::BitstreamEntry::Error:
2547       Error("malformed block record in AST file");
2548       return Failure;
2549     case llvm::BitstreamEntry::EndBlock: {
2550       // Validate the module before returning.  This call catches an AST with
2551       // no module name and no imports.
2552       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2553         return Result;
2554 
2555       // Validate input files.
2556       const HeaderSearchOptions &HSOpts =
2557           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2558 
2559       // All user input files reside at the index range [0, NumUserInputs), and
2560       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2561       // loaded module files, ignore missing inputs.
2562       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2563           F.Kind != MK_PrebuiltModule) {
2564         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2565 
2566         // If we are reading a module, we will create a verification timestamp,
2567         // so we verify all input files.  Otherwise, verify only user input
2568         // files.
2569 
2570         unsigned N = NumUserInputs;
2571         if (ValidateSystemInputs ||
2572             (HSOpts.ModulesValidateOncePerBuildSession &&
2573              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2574              F.Kind == MK_ImplicitModule))
2575           N = NumInputs;
2576 
2577         for (unsigned I = 0; I < N; ++I) {
2578           InputFile IF = getInputFile(F, I+1, Complain);
2579           if (!IF.getFile() || IF.isOutOfDate())
2580             return OutOfDate;
2581         }
2582       }
2583 
2584       if (Listener)
2585         Listener->visitModuleFile(F.FileName, F.Kind);
2586 
2587       if (Listener && Listener->needsInputFileVisitation()) {
2588         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2589                                                                 : NumUserInputs;
2590         for (unsigned I = 0; I < N; ++I) {
2591           bool IsSystem = I >= NumUserInputs;
2592           InputFileInfo FI = readInputFileInfo(F, I+1);
2593           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2594                                    F.Kind == MK_ExplicitModule ||
2595                                    F.Kind == MK_PrebuiltModule);
2596         }
2597       }
2598 
2599       return Result;
2600     }
2601 
2602     case llvm::BitstreamEntry::SubBlock:
2603       switch (Entry.ID) {
2604       case INPUT_FILES_BLOCK_ID:
2605         F.InputFilesCursor = Stream;
2606         if (llvm::Error Err = Stream.SkipBlock()) {
2607           Error(std::move(Err));
2608           return Failure;
2609         }
2610         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2611           Error("malformed block record in AST file");
2612           return Failure;
2613         }
2614         continue;
2615 
2616       case OPTIONS_BLOCK_ID:
2617         // If we're reading the first module for this group, check its options
2618         // are compatible with ours. For modules it imports, no further checking
2619         // is required, because we checked them when we built it.
2620         if (Listener && !ImportedBy) {
2621           // Should we allow the configuration of the module file to differ from
2622           // the configuration of the current translation unit in a compatible
2623           // way?
2624           //
2625           // FIXME: Allow this for files explicitly specified with -include-pch.
2626           bool AllowCompatibleConfigurationMismatch =
2627               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2628 
2629           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2630                                     AllowCompatibleConfigurationMismatch,
2631                                     *Listener, SuggestedPredefines);
2632           if (Result == Failure) {
2633             Error("malformed block record in AST file");
2634             return Result;
2635           }
2636 
2637           if (DisableValidation ||
2638               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2639             Result = Success;
2640 
2641           // If we can't load the module, exit early since we likely
2642           // will rebuild the module anyway. The stream may be in the
2643           // middle of a block.
2644           if (Result != Success)
2645             return Result;
2646         } else if (llvm::Error Err = Stream.SkipBlock()) {
2647           Error(std::move(Err));
2648           return Failure;
2649         }
2650         continue;
2651 
2652       default:
2653         if (llvm::Error Err = Stream.SkipBlock()) {
2654           Error(std::move(Err));
2655           return Failure;
2656         }
2657         continue;
2658       }
2659 
2660     case llvm::BitstreamEntry::Record:
2661       // The interesting case.
2662       break;
2663     }
2664 
2665     // Read and process a record.
2666     Record.clear();
2667     StringRef Blob;
2668     Expected<unsigned> MaybeRecordType =
2669         Stream.readRecord(Entry.ID, Record, &Blob);
2670     if (!MaybeRecordType) {
2671       Error(MaybeRecordType.takeError());
2672       return Failure;
2673     }
2674     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2675     case METADATA: {
2676       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2677         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2678           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2679                                         : diag::err_pch_version_too_new);
2680         return VersionMismatch;
2681       }
2682 
2683       bool hasErrors = Record[7];
2684       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2685         Diag(diag::err_pch_with_compiler_errors);
2686         return HadErrors;
2687       }
2688       if (hasErrors) {
2689         Diags.ErrorOccurred = true;
2690         Diags.UncompilableErrorOccurred = true;
2691         Diags.UnrecoverableErrorOccurred = true;
2692       }
2693 
2694       F.RelocatablePCH = Record[4];
2695       // Relative paths in a relocatable PCH are relative to our sysroot.
2696       if (F.RelocatablePCH)
2697         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2698 
2699       F.HasTimestamps = Record[5];
2700 
2701       F.PCHHasObjectFile = Record[6];
2702 
2703       const std::string &CurBranch = getClangFullRepositoryVersion();
2704       StringRef ASTBranch = Blob;
2705       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2706         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2707           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2708         return VersionMismatch;
2709       }
2710       break;
2711     }
2712 
2713     case IMPORTS: {
2714       // Validate the AST before processing any imports (otherwise, untangling
2715       // them can be error-prone and expensive).  A module will have a name and
2716       // will already have been validated, but this catches the PCH case.
2717       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2718         return Result;
2719 
2720       // Load each of the imported PCH files.
2721       unsigned Idx = 0, N = Record.size();
2722       while (Idx < N) {
2723         // Read information about the AST file.
2724         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2725         // The import location will be the local one for now; we will adjust
2726         // all import locations of module imports after the global source
2727         // location info are setup, in ReadAST.
2728         SourceLocation ImportLoc =
2729             ReadUntranslatedSourceLocation(Record[Idx++]);
2730         off_t StoredSize = (off_t)Record[Idx++];
2731         time_t StoredModTime = (time_t)Record[Idx++];
2732         ASTFileSignature StoredSignature = {
2733             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2734               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2735               (uint32_t)Record[Idx++]}}};
2736 
2737         std::string ImportedName = ReadString(Record, Idx);
2738         std::string ImportedFile;
2739 
2740         // For prebuilt and explicit modules first consult the file map for
2741         // an override. Note that here we don't search prebuilt module
2742         // directories, only the explicit name to file mappings. Also, we will
2743         // still verify the size/signature making sure it is essentially the
2744         // same file but perhaps in a different location.
2745         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2746           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2747             ImportedName, /*FileMapOnly*/ true);
2748 
2749         if (ImportedFile.empty())
2750           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2751           // ModuleCache as when writing.
2752           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2753         else
2754           SkipPath(Record, Idx);
2755 
2756         // If our client can't cope with us being out of date, we can't cope with
2757         // our dependency being missing.
2758         unsigned Capabilities = ClientLoadCapabilities;
2759         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2760           Capabilities &= ~ARR_Missing;
2761 
2762         // Load the AST file.
2763         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2764                                   Loaded, StoredSize, StoredModTime,
2765                                   StoredSignature, Capabilities);
2766 
2767         // If we diagnosed a problem, produce a backtrace.
2768         if (isDiagnosedResult(Result, Capabilities))
2769           Diag(diag::note_module_file_imported_by)
2770               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2771 
2772         switch (Result) {
2773         case Failure: return Failure;
2774           // If we have to ignore the dependency, we'll have to ignore this too.
2775         case Missing:
2776         case OutOfDate: return OutOfDate;
2777         case VersionMismatch: return VersionMismatch;
2778         case ConfigurationMismatch: return ConfigurationMismatch;
2779         case HadErrors: return HadErrors;
2780         case Success: break;
2781         }
2782       }
2783       break;
2784     }
2785 
2786     case ORIGINAL_FILE:
2787       F.OriginalSourceFileID = FileID::get(Record[0]);
2788       F.ActualOriginalSourceFileName = Blob;
2789       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2790       ResolveImportedPath(F, F.OriginalSourceFileName);
2791       break;
2792 
2793     case ORIGINAL_FILE_ID:
2794       F.OriginalSourceFileID = FileID::get(Record[0]);
2795       break;
2796 
2797     case ORIGINAL_PCH_DIR:
2798       F.OriginalDir = Blob;
2799       break;
2800 
2801     case MODULE_NAME:
2802       F.ModuleName = Blob;
2803       Diag(diag::remark_module_import)
2804           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2805           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2806       if (Listener)
2807         Listener->ReadModuleName(F.ModuleName);
2808 
2809       // Validate the AST as soon as we have a name so we can exit early on
2810       // failure.
2811       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2812         return Result;
2813 
2814       break;
2815 
2816     case MODULE_DIRECTORY: {
2817       // Save the BaseDirectory as written in the PCM for computing the module
2818       // filename for the ModuleCache.
2819       BaseDirectoryAsWritten = Blob;
2820       assert(!F.ModuleName.empty() &&
2821              "MODULE_DIRECTORY found before MODULE_NAME");
2822       // If we've already loaded a module map file covering this module, we may
2823       // have a better path for it (relative to the current build).
2824       Module *M = PP.getHeaderSearchInfo().lookupModule(
2825           F.ModuleName, /*AllowSearch*/ true,
2826           /*AllowExtraModuleMapSearch*/ true);
2827       if (M && M->Directory) {
2828         // If we're implicitly loading a module, the base directory can't
2829         // change between the build and use.
2830         // Don't emit module relocation error if we have -fno-validate-pch
2831         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2832             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2833           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2834           if (!BuildDir || *BuildDir != M->Directory) {
2835             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2836               Diag(diag::err_imported_module_relocated)
2837                   << F.ModuleName << Blob << M->Directory->getName();
2838             return OutOfDate;
2839           }
2840         }
2841         F.BaseDirectory = M->Directory->getName();
2842       } else {
2843         F.BaseDirectory = Blob;
2844       }
2845       break;
2846     }
2847 
2848     case MODULE_MAP_FILE:
2849       if (ASTReadResult Result =
2850               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2851         return Result;
2852       break;
2853 
2854     case INPUT_FILE_OFFSETS:
2855       NumInputs = Record[0];
2856       NumUserInputs = Record[1];
2857       F.InputFileOffsets =
2858           (const llvm::support::unaligned_uint64_t *)Blob.data();
2859       F.InputFilesLoaded.resize(NumInputs);
2860       F.NumUserInputFiles = NumUserInputs;
2861       break;
2862     }
2863   }
2864 }
2865 
2866 ASTReader::ASTReadResult
2867 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2868   BitstreamCursor &Stream = F.Stream;
2869 
2870   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2871     Error(std::move(Err));
2872     return Failure;
2873   }
2874 
2875   // Read all of the records and blocks for the AST file.
2876   RecordData Record;
2877   while (true) {
2878     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2879     if (!MaybeEntry) {
2880       Error(MaybeEntry.takeError());
2881       return Failure;
2882     }
2883     llvm::BitstreamEntry Entry = MaybeEntry.get();
2884 
2885     switch (Entry.Kind) {
2886     case llvm::BitstreamEntry::Error:
2887       Error("error at end of module block in AST file");
2888       return Failure;
2889     case llvm::BitstreamEntry::EndBlock:
2890       // Outside of C++, we do not store a lookup map for the translation unit.
2891       // Instead, mark it as needing a lookup map to be built if this module
2892       // contains any declarations lexically within it (which it always does!).
2893       // This usually has no cost, since we very rarely need the lookup map for
2894       // the translation unit outside C++.
2895       if (ASTContext *Ctx = ContextObj) {
2896         DeclContext *DC = Ctx->getTranslationUnitDecl();
2897         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2898           DC->setMustBuildLookupTable();
2899       }
2900 
2901       return Success;
2902     case llvm::BitstreamEntry::SubBlock:
2903       switch (Entry.ID) {
2904       case DECLTYPES_BLOCK_ID:
2905         // We lazily load the decls block, but we want to set up the
2906         // DeclsCursor cursor to point into it.  Clone our current bitcode
2907         // cursor to it, enter the block and read the abbrevs in that block.
2908         // With the main cursor, we just skip over it.
2909         F.DeclsCursor = Stream;
2910         if (llvm::Error Err = Stream.SkipBlock()) {
2911           Error(std::move(Err));
2912           return Failure;
2913         }
2914         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2915           Error("malformed block record in AST file");
2916           return Failure;
2917         }
2918         break;
2919 
2920       case PREPROCESSOR_BLOCK_ID:
2921         F.MacroCursor = Stream;
2922         if (!PP.getExternalSource())
2923           PP.setExternalSource(this);
2924 
2925         if (llvm::Error Err = Stream.SkipBlock()) {
2926           Error(std::move(Err));
2927           return Failure;
2928         }
2929         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2930           Error("malformed block record in AST file");
2931           return Failure;
2932         }
2933         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2934         break;
2935 
2936       case PREPROCESSOR_DETAIL_BLOCK_ID:
2937         F.PreprocessorDetailCursor = Stream;
2938 
2939         if (llvm::Error Err = Stream.SkipBlock()) {
2940           Error(std::move(Err));
2941           return Failure;
2942         }
2943         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2944                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2945           Error("malformed preprocessor detail record in AST file");
2946           return Failure;
2947         }
2948         F.PreprocessorDetailStartOffset
2949         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2950 
2951         if (!PP.getPreprocessingRecord())
2952           PP.createPreprocessingRecord();
2953         if (!PP.getPreprocessingRecord()->getExternalSource())
2954           PP.getPreprocessingRecord()->SetExternalSource(*this);
2955         break;
2956 
2957       case SOURCE_MANAGER_BLOCK_ID:
2958         if (ReadSourceManagerBlock(F))
2959           return Failure;
2960         break;
2961 
2962       case SUBMODULE_BLOCK_ID:
2963         if (ASTReadResult Result =
2964                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2965           return Result;
2966         break;
2967 
2968       case COMMENTS_BLOCK_ID: {
2969         BitstreamCursor C = Stream;
2970 
2971         if (llvm::Error Err = Stream.SkipBlock()) {
2972           Error(std::move(Err));
2973           return Failure;
2974         }
2975         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2976           Error("malformed comments block in AST file");
2977           return Failure;
2978         }
2979         CommentsCursors.push_back(std::make_pair(C, &F));
2980         break;
2981       }
2982 
2983       default:
2984         if (llvm::Error Err = Stream.SkipBlock()) {
2985           Error(std::move(Err));
2986           return Failure;
2987         }
2988         break;
2989       }
2990       continue;
2991 
2992     case llvm::BitstreamEntry::Record:
2993       // The interesting case.
2994       break;
2995     }
2996 
2997     // Read and process a record.
2998     Record.clear();
2999     StringRef Blob;
3000     Expected<unsigned> MaybeRecordType =
3001         Stream.readRecord(Entry.ID, Record, &Blob);
3002     if (!MaybeRecordType) {
3003       Error(MaybeRecordType.takeError());
3004       return Failure;
3005     }
3006     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3007 
3008     // If we're not loading an AST context, we don't care about most records.
3009     if (!ContextObj) {
3010       switch (RecordType) {
3011       case IDENTIFIER_TABLE:
3012       case IDENTIFIER_OFFSET:
3013       case INTERESTING_IDENTIFIERS:
3014       case STATISTICS:
3015       case PP_CONDITIONAL_STACK:
3016       case PP_COUNTER_VALUE:
3017       case SOURCE_LOCATION_OFFSETS:
3018       case MODULE_OFFSET_MAP:
3019       case SOURCE_MANAGER_LINE_TABLE:
3020       case SOURCE_LOCATION_PRELOADS:
3021       case PPD_ENTITIES_OFFSETS:
3022       case HEADER_SEARCH_TABLE:
3023       case IMPORTED_MODULES:
3024       case MACRO_OFFSET:
3025         break;
3026       default:
3027         continue;
3028       }
3029     }
3030 
3031     switch (RecordType) {
3032     default:  // Default behavior: ignore.
3033       break;
3034 
3035     case TYPE_OFFSET: {
3036       if (F.LocalNumTypes != 0) {
3037         Error("duplicate TYPE_OFFSET record in AST file");
3038         return Failure;
3039       }
3040       F.TypeOffsets = (const uint32_t *)Blob.data();
3041       F.LocalNumTypes = Record[0];
3042       unsigned LocalBaseTypeIndex = Record[1];
3043       F.BaseTypeIndex = getTotalNumTypes();
3044 
3045       if (F.LocalNumTypes > 0) {
3046         // Introduce the global -> local mapping for types within this module.
3047         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3048 
3049         // Introduce the local -> global mapping for types within this module.
3050         F.TypeRemap.insertOrReplace(
3051           std::make_pair(LocalBaseTypeIndex,
3052                          F.BaseTypeIndex - LocalBaseTypeIndex));
3053 
3054         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3055       }
3056       break;
3057     }
3058 
3059     case DECL_OFFSET: {
3060       if (F.LocalNumDecls != 0) {
3061         Error("duplicate DECL_OFFSET record in AST file");
3062         return Failure;
3063       }
3064       F.DeclOffsets = (const DeclOffset *)Blob.data();
3065       F.LocalNumDecls = Record[0];
3066       unsigned LocalBaseDeclID = Record[1];
3067       F.BaseDeclID = getTotalNumDecls();
3068 
3069       if (F.LocalNumDecls > 0) {
3070         // Introduce the global -> local mapping for declarations within this
3071         // module.
3072         GlobalDeclMap.insert(
3073           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3074 
3075         // Introduce the local -> global mapping for declarations within this
3076         // module.
3077         F.DeclRemap.insertOrReplace(
3078           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3079 
3080         // Introduce the global -> local mapping for declarations within this
3081         // module.
3082         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3083 
3084         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3085       }
3086       break;
3087     }
3088 
3089     case TU_UPDATE_LEXICAL: {
3090       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3091       LexicalContents Contents(
3092           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3093               Blob.data()),
3094           static_cast<unsigned int>(Blob.size() / 4));
3095       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3096       TU->setHasExternalLexicalStorage(true);
3097       break;
3098     }
3099 
3100     case UPDATE_VISIBLE: {
3101       unsigned Idx = 0;
3102       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3103       auto *Data = (const unsigned char*)Blob.data();
3104       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3105       // If we've already loaded the decl, perform the updates when we finish
3106       // loading this block.
3107       if (Decl *D = GetExistingDecl(ID))
3108         PendingUpdateRecords.push_back(
3109             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3110       break;
3111     }
3112 
3113     case IDENTIFIER_TABLE:
3114       F.IdentifierTableData = Blob.data();
3115       if (Record[0]) {
3116         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3117             (const unsigned char *)F.IdentifierTableData + Record[0],
3118             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3119             (const unsigned char *)F.IdentifierTableData,
3120             ASTIdentifierLookupTrait(*this, F));
3121 
3122         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3123       }
3124       break;
3125 
3126     case IDENTIFIER_OFFSET: {
3127       if (F.LocalNumIdentifiers != 0) {
3128         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3129         return Failure;
3130       }
3131       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3132       F.LocalNumIdentifiers = Record[0];
3133       unsigned LocalBaseIdentifierID = Record[1];
3134       F.BaseIdentifierID = getTotalNumIdentifiers();
3135 
3136       if (F.LocalNumIdentifiers > 0) {
3137         // Introduce the global -> local mapping for identifiers within this
3138         // module.
3139         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3140                                                   &F));
3141 
3142         // Introduce the local -> global mapping for identifiers within this
3143         // module.
3144         F.IdentifierRemap.insertOrReplace(
3145           std::make_pair(LocalBaseIdentifierID,
3146                          F.BaseIdentifierID - LocalBaseIdentifierID));
3147 
3148         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3149                                  + F.LocalNumIdentifiers);
3150       }
3151       break;
3152     }
3153 
3154     case INTERESTING_IDENTIFIERS:
3155       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3156       break;
3157 
3158     case EAGERLY_DESERIALIZED_DECLS:
3159       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3160       // about "interesting" decls (for instance, if we're building a module).
3161       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3162         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3163       break;
3164 
3165     case MODULAR_CODEGEN_DECLS:
3166       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3167       // them (ie: if we're not codegenerating this module).
3168       if (F.Kind == MK_MainFile)
3169         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3170           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3171       break;
3172 
3173     case SPECIAL_TYPES:
3174       if (SpecialTypes.empty()) {
3175         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3176           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3177         break;
3178       }
3179 
3180       if (SpecialTypes.size() != Record.size()) {
3181         Error("invalid special-types record");
3182         return Failure;
3183       }
3184 
3185       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3186         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3187         if (!SpecialTypes[I])
3188           SpecialTypes[I] = ID;
3189         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3190         // merge step?
3191       }
3192       break;
3193 
3194     case STATISTICS:
3195       TotalNumStatements += Record[0];
3196       TotalNumMacros += Record[1];
3197       TotalLexicalDeclContexts += Record[2];
3198       TotalVisibleDeclContexts += Record[3];
3199       break;
3200 
3201     case UNUSED_FILESCOPED_DECLS:
3202       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3203         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3204       break;
3205 
3206     case DELEGATING_CTORS:
3207       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3208         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3209       break;
3210 
3211     case WEAK_UNDECLARED_IDENTIFIERS:
3212       if (Record.size() % 4 != 0) {
3213         Error("invalid weak identifiers record");
3214         return Failure;
3215       }
3216 
3217       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3218       // files. This isn't the way to do it :)
3219       WeakUndeclaredIdentifiers.clear();
3220 
3221       // Translate the weak, undeclared identifiers into global IDs.
3222       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3223         WeakUndeclaredIdentifiers.push_back(
3224           getGlobalIdentifierID(F, Record[I++]));
3225         WeakUndeclaredIdentifiers.push_back(
3226           getGlobalIdentifierID(F, Record[I++]));
3227         WeakUndeclaredIdentifiers.push_back(
3228           ReadSourceLocation(F, Record, I).getRawEncoding());
3229         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3230       }
3231       break;
3232 
3233     case SELECTOR_OFFSETS: {
3234       F.SelectorOffsets = (const uint32_t *)Blob.data();
3235       F.LocalNumSelectors = Record[0];
3236       unsigned LocalBaseSelectorID = Record[1];
3237       F.BaseSelectorID = getTotalNumSelectors();
3238 
3239       if (F.LocalNumSelectors > 0) {
3240         // Introduce the global -> local mapping for selectors within this
3241         // module.
3242         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3243 
3244         // Introduce the local -> global mapping for selectors within this
3245         // module.
3246         F.SelectorRemap.insertOrReplace(
3247           std::make_pair(LocalBaseSelectorID,
3248                          F.BaseSelectorID - LocalBaseSelectorID));
3249 
3250         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3251       }
3252       break;
3253     }
3254 
3255     case METHOD_POOL:
3256       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3257       if (Record[0])
3258         F.SelectorLookupTable
3259           = ASTSelectorLookupTable::Create(
3260                         F.SelectorLookupTableData + Record[0],
3261                         F.SelectorLookupTableData,
3262                         ASTSelectorLookupTrait(*this, F));
3263       TotalNumMethodPoolEntries += Record[1];
3264       break;
3265 
3266     case REFERENCED_SELECTOR_POOL:
3267       if (!Record.empty()) {
3268         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3269           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3270                                                                 Record[Idx++]));
3271           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3272                                               getRawEncoding());
3273         }
3274       }
3275       break;
3276 
3277     case PP_CONDITIONAL_STACK:
3278       if (!Record.empty()) {
3279         unsigned Idx = 0, End = Record.size() - 1;
3280         bool ReachedEOFWhileSkipping = Record[Idx++];
3281         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3282         if (ReachedEOFWhileSkipping) {
3283           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3284           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3285           bool FoundNonSkipPortion = Record[Idx++];
3286           bool FoundElse = Record[Idx++];
3287           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3288           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3289                            FoundElse, ElseLoc);
3290         }
3291         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3292         while (Idx < End) {
3293           auto Loc = ReadSourceLocation(F, Record, Idx);
3294           bool WasSkipping = Record[Idx++];
3295           bool FoundNonSkip = Record[Idx++];
3296           bool FoundElse = Record[Idx++];
3297           ConditionalStack.push_back(
3298               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3299         }
3300         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3301       }
3302       break;
3303 
3304     case PP_COUNTER_VALUE:
3305       if (!Record.empty() && Listener)
3306         Listener->ReadCounter(F, Record[0]);
3307       break;
3308 
3309     case FILE_SORTED_DECLS:
3310       F.FileSortedDecls = (const DeclID *)Blob.data();
3311       F.NumFileSortedDecls = Record[0];
3312       break;
3313 
3314     case SOURCE_LOCATION_OFFSETS: {
3315       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3316       F.LocalNumSLocEntries = Record[0];
3317       unsigned SLocSpaceSize = Record[1];
3318       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3319           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3320                                               SLocSpaceSize);
3321       if (!F.SLocEntryBaseID) {
3322         Error("ran out of source locations");
3323         break;
3324       }
3325       // Make our entry in the range map. BaseID is negative and growing, so
3326       // we invert it. Because we invert it, though, we need the other end of
3327       // the range.
3328       unsigned RangeStart =
3329           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3330       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3331       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3332 
3333       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3334       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3335       GlobalSLocOffsetMap.insert(
3336           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3337                            - SLocSpaceSize,&F));
3338 
3339       // Initialize the remapping table.
3340       // Invalid stays invalid.
3341       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3342       // This module. Base was 2 when being compiled.
3343       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3344                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3345 
3346       TotalNumSLocEntries += F.LocalNumSLocEntries;
3347       break;
3348     }
3349 
3350     case MODULE_OFFSET_MAP:
3351       F.ModuleOffsetMap = Blob;
3352       break;
3353 
3354     case SOURCE_MANAGER_LINE_TABLE:
3355       if (ParseLineTable(F, Record))
3356         return Failure;
3357       break;
3358 
3359     case SOURCE_LOCATION_PRELOADS: {
3360       // Need to transform from the local view (1-based IDs) to the global view,
3361       // which is based off F.SLocEntryBaseID.
3362       if (!F.PreloadSLocEntries.empty()) {
3363         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3364         return Failure;
3365       }
3366 
3367       F.PreloadSLocEntries.swap(Record);
3368       break;
3369     }
3370 
3371     case EXT_VECTOR_DECLS:
3372       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3373         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3374       break;
3375 
3376     case VTABLE_USES:
3377       if (Record.size() % 3 != 0) {
3378         Error("Invalid VTABLE_USES record");
3379         return Failure;
3380       }
3381 
3382       // Later tables overwrite earlier ones.
3383       // FIXME: Modules will have some trouble with this. This is clearly not
3384       // the right way to do this.
3385       VTableUses.clear();
3386 
3387       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3388         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3389         VTableUses.push_back(
3390           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3391         VTableUses.push_back(Record[Idx++]);
3392       }
3393       break;
3394 
3395     case PENDING_IMPLICIT_INSTANTIATIONS:
3396       if (PendingInstantiations.size() % 2 != 0) {
3397         Error("Invalid existing PendingInstantiations");
3398         return Failure;
3399       }
3400 
3401       if (Record.size() % 2 != 0) {
3402         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3403         return Failure;
3404       }
3405 
3406       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3407         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3408         PendingInstantiations.push_back(
3409           ReadSourceLocation(F, Record, I).getRawEncoding());
3410       }
3411       break;
3412 
3413     case SEMA_DECL_REFS:
3414       if (Record.size() != 3) {
3415         Error("Invalid SEMA_DECL_REFS block");
3416         return Failure;
3417       }
3418       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3419         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3420       break;
3421 
3422     case PPD_ENTITIES_OFFSETS: {
3423       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3424       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3425       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3426 
3427       unsigned LocalBasePreprocessedEntityID = Record[0];
3428 
3429       unsigned StartingID;
3430       if (!PP.getPreprocessingRecord())
3431         PP.createPreprocessingRecord();
3432       if (!PP.getPreprocessingRecord()->getExternalSource())
3433         PP.getPreprocessingRecord()->SetExternalSource(*this);
3434       StartingID
3435         = PP.getPreprocessingRecord()
3436             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3437       F.BasePreprocessedEntityID = StartingID;
3438 
3439       if (F.NumPreprocessedEntities > 0) {
3440         // Introduce the global -> local mapping for preprocessed entities in
3441         // this module.
3442         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3443 
3444         // Introduce the local -> global mapping for preprocessed entities in
3445         // this module.
3446         F.PreprocessedEntityRemap.insertOrReplace(
3447           std::make_pair(LocalBasePreprocessedEntityID,
3448             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3449       }
3450 
3451       break;
3452     }
3453 
3454     case PPD_SKIPPED_RANGES: {
3455       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3456       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3457       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3458 
3459       if (!PP.getPreprocessingRecord())
3460         PP.createPreprocessingRecord();
3461       if (!PP.getPreprocessingRecord()->getExternalSource())
3462         PP.getPreprocessingRecord()->SetExternalSource(*this);
3463       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3464           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3465 
3466       if (F.NumPreprocessedSkippedRanges > 0)
3467         GlobalSkippedRangeMap.insert(
3468             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3469       break;
3470     }
3471 
3472     case DECL_UPDATE_OFFSETS:
3473       if (Record.size() % 2 != 0) {
3474         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3475         return Failure;
3476       }
3477       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3478         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3479         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3480 
3481         // If we've already loaded the decl, perform the updates when we finish
3482         // loading this block.
3483         if (Decl *D = GetExistingDecl(ID))
3484           PendingUpdateRecords.push_back(
3485               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3486       }
3487       break;
3488 
3489     case OBJC_CATEGORIES_MAP:
3490       if (F.LocalNumObjCCategoriesInMap != 0) {
3491         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3492         return Failure;
3493       }
3494 
3495       F.LocalNumObjCCategoriesInMap = Record[0];
3496       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3497       break;
3498 
3499     case OBJC_CATEGORIES:
3500       F.ObjCCategories.swap(Record);
3501       break;
3502 
3503     case CUDA_SPECIAL_DECL_REFS:
3504       // Later tables overwrite earlier ones.
3505       // FIXME: Modules will have trouble with this.
3506       CUDASpecialDeclRefs.clear();
3507       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3508         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3509       break;
3510 
3511     case HEADER_SEARCH_TABLE:
3512       F.HeaderFileInfoTableData = Blob.data();
3513       F.LocalNumHeaderFileInfos = Record[1];
3514       if (Record[0]) {
3515         F.HeaderFileInfoTable
3516           = HeaderFileInfoLookupTable::Create(
3517                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3518                    (const unsigned char *)F.HeaderFileInfoTableData,
3519                    HeaderFileInfoTrait(*this, F,
3520                                        &PP.getHeaderSearchInfo(),
3521                                        Blob.data() + Record[2]));
3522 
3523         PP.getHeaderSearchInfo().SetExternalSource(this);
3524         if (!PP.getHeaderSearchInfo().getExternalLookup())
3525           PP.getHeaderSearchInfo().SetExternalLookup(this);
3526       }
3527       break;
3528 
3529     case FP_PRAGMA_OPTIONS:
3530       // Later tables overwrite earlier ones.
3531       FPPragmaOptions.swap(Record);
3532       break;
3533 
3534     case OPENCL_EXTENSIONS:
3535       for (unsigned I = 0, E = Record.size(); I != E; ) {
3536         auto Name = ReadString(Record, I);
3537         auto &Opt = OpenCLExtensions.OptMap[Name];
3538         Opt.Supported = Record[I++] != 0;
3539         Opt.Enabled = Record[I++] != 0;
3540         Opt.Avail = Record[I++];
3541         Opt.Core = Record[I++];
3542       }
3543       break;
3544 
3545     case OPENCL_EXTENSION_TYPES:
3546       for (unsigned I = 0, E = Record.size(); I != E;) {
3547         auto TypeID = static_cast<::TypeID>(Record[I++]);
3548         auto *Type = GetType(TypeID).getTypePtr();
3549         auto NumExt = static_cast<unsigned>(Record[I++]);
3550         for (unsigned II = 0; II != NumExt; ++II) {
3551           auto Ext = ReadString(Record, I);
3552           OpenCLTypeExtMap[Type].insert(Ext);
3553         }
3554       }
3555       break;
3556 
3557     case OPENCL_EXTENSION_DECLS:
3558       for (unsigned I = 0, E = Record.size(); I != E;) {
3559         auto DeclID = static_cast<::DeclID>(Record[I++]);
3560         auto *Decl = GetDecl(DeclID);
3561         auto NumExt = static_cast<unsigned>(Record[I++]);
3562         for (unsigned II = 0; II != NumExt; ++II) {
3563           auto Ext = ReadString(Record, I);
3564           OpenCLDeclExtMap[Decl].insert(Ext);
3565         }
3566       }
3567       break;
3568 
3569     case TENTATIVE_DEFINITIONS:
3570       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3571         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3572       break;
3573 
3574     case KNOWN_NAMESPACES:
3575       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3576         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3577       break;
3578 
3579     case UNDEFINED_BUT_USED:
3580       if (UndefinedButUsed.size() % 2 != 0) {
3581         Error("Invalid existing UndefinedButUsed");
3582         return Failure;
3583       }
3584 
3585       if (Record.size() % 2 != 0) {
3586         Error("invalid undefined-but-used record");
3587         return Failure;
3588       }
3589       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3590         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3591         UndefinedButUsed.push_back(
3592             ReadSourceLocation(F, Record, I).getRawEncoding());
3593       }
3594       break;
3595 
3596     case DELETE_EXPRS_TO_ANALYZE:
3597       for (unsigned I = 0, N = Record.size(); I != N;) {
3598         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3599         const uint64_t Count = Record[I++];
3600         DelayedDeleteExprs.push_back(Count);
3601         for (uint64_t C = 0; C < Count; ++C) {
3602           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3603           bool IsArrayForm = Record[I++] == 1;
3604           DelayedDeleteExprs.push_back(IsArrayForm);
3605         }
3606       }
3607       break;
3608 
3609     case IMPORTED_MODULES:
3610       if (!F.isModule()) {
3611         // If we aren't loading a module (which has its own exports), make
3612         // all of the imported modules visible.
3613         // FIXME: Deal with macros-only imports.
3614         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3615           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3616           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3617           if (GlobalID) {
3618             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3619             if (DeserializationListener)
3620               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3621           }
3622         }
3623       }
3624       break;
3625 
3626     case MACRO_OFFSET: {
3627       if (F.LocalNumMacros != 0) {
3628         Error("duplicate MACRO_OFFSET record in AST file");
3629         return Failure;
3630       }
3631       F.MacroOffsets = (const uint32_t *)Blob.data();
3632       F.LocalNumMacros = Record[0];
3633       unsigned LocalBaseMacroID = Record[1];
3634       F.BaseMacroID = getTotalNumMacros();
3635 
3636       if (F.LocalNumMacros > 0) {
3637         // Introduce the global -> local mapping for macros within this module.
3638         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3639 
3640         // Introduce the local -> global mapping for macros within this module.
3641         F.MacroRemap.insertOrReplace(
3642           std::make_pair(LocalBaseMacroID,
3643                          F.BaseMacroID - LocalBaseMacroID));
3644 
3645         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3646       }
3647       break;
3648     }
3649 
3650     case LATE_PARSED_TEMPLATE:
3651       LateParsedTemplates.append(Record.begin(), Record.end());
3652       break;
3653 
3654     case OPTIMIZE_PRAGMA_OPTIONS:
3655       if (Record.size() != 1) {
3656         Error("invalid pragma optimize record");
3657         return Failure;
3658       }
3659       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3660       break;
3661 
3662     case MSSTRUCT_PRAGMA_OPTIONS:
3663       if (Record.size() != 1) {
3664         Error("invalid pragma ms_struct record");
3665         return Failure;
3666       }
3667       PragmaMSStructState = Record[0];
3668       break;
3669 
3670     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3671       if (Record.size() != 2) {
3672         Error("invalid pragma ms_struct record");
3673         return Failure;
3674       }
3675       PragmaMSPointersToMembersState = Record[0];
3676       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3677       break;
3678 
3679     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3680       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3681         UnusedLocalTypedefNameCandidates.push_back(
3682             getGlobalDeclID(F, Record[I]));
3683       break;
3684 
3685     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3686       if (Record.size() != 1) {
3687         Error("invalid cuda pragma options record");
3688         return Failure;
3689       }
3690       ForceCUDAHostDeviceDepth = Record[0];
3691       break;
3692 
3693     case PACK_PRAGMA_OPTIONS: {
3694       if (Record.size() < 3) {
3695         Error("invalid pragma pack record");
3696         return Failure;
3697       }
3698       PragmaPackCurrentValue = Record[0];
3699       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3700       unsigned NumStackEntries = Record[2];
3701       unsigned Idx = 3;
3702       // Reset the stack when importing a new module.
3703       PragmaPackStack.clear();
3704       for (unsigned I = 0; I < NumStackEntries; ++I) {
3705         PragmaPackStackEntry Entry;
3706         Entry.Value = Record[Idx++];
3707         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3708         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3709         PragmaPackStrings.push_back(ReadString(Record, Idx));
3710         Entry.SlotLabel = PragmaPackStrings.back();
3711         PragmaPackStack.push_back(Entry);
3712       }
3713       break;
3714     }
3715     }
3716   }
3717 }
3718 
3719 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3720   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3721 
3722   // Additional remapping information.
3723   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3724   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3725   F.ModuleOffsetMap = StringRef();
3726 
3727   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3728   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3729     F.SLocRemap.insert(std::make_pair(0U, 0));
3730     F.SLocRemap.insert(std::make_pair(2U, 1));
3731   }
3732 
3733   // Continuous range maps we may be updating in our module.
3734   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3735   RemapBuilder SLocRemap(F.SLocRemap);
3736   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3737   RemapBuilder MacroRemap(F.MacroRemap);
3738   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3739   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3740   RemapBuilder SelectorRemap(F.SelectorRemap);
3741   RemapBuilder DeclRemap(F.DeclRemap);
3742   RemapBuilder TypeRemap(F.TypeRemap);
3743 
3744   while (Data < DataEnd) {
3745     // FIXME: Looking up dependency modules by filename is horrible. Let's
3746     // start fixing this with prebuilt and explicit modules and see how it
3747     // goes...
3748     using namespace llvm::support;
3749     ModuleKind Kind = static_cast<ModuleKind>(
3750       endian::readNext<uint8_t, little, unaligned>(Data));
3751     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3752     StringRef Name = StringRef((const char*)Data, Len);
3753     Data += Len;
3754     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3755                       ? ModuleMgr.lookupByModuleName(Name)
3756                       : ModuleMgr.lookupByFileName(Name));
3757     if (!OM) {
3758       std::string Msg =
3759           "SourceLocation remap refers to unknown module, cannot find ";
3760       Msg.append(Name);
3761       Error(Msg);
3762       return;
3763     }
3764 
3765     uint32_t SLocOffset =
3766         endian::readNext<uint32_t, little, unaligned>(Data);
3767     uint32_t IdentifierIDOffset =
3768         endian::readNext<uint32_t, little, unaligned>(Data);
3769     uint32_t MacroIDOffset =
3770         endian::readNext<uint32_t, little, unaligned>(Data);
3771     uint32_t PreprocessedEntityIDOffset =
3772         endian::readNext<uint32_t, little, unaligned>(Data);
3773     uint32_t SubmoduleIDOffset =
3774         endian::readNext<uint32_t, little, unaligned>(Data);
3775     uint32_t SelectorIDOffset =
3776         endian::readNext<uint32_t, little, unaligned>(Data);
3777     uint32_t DeclIDOffset =
3778         endian::readNext<uint32_t, little, unaligned>(Data);
3779     uint32_t TypeIndexOffset =
3780         endian::readNext<uint32_t, little, unaligned>(Data);
3781 
3782     uint32_t None = std::numeric_limits<uint32_t>::max();
3783 
3784     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3785                          RemapBuilder &Remap) {
3786       if (Offset != None)
3787         Remap.insert(std::make_pair(Offset,
3788                                     static_cast<int>(BaseOffset - Offset)));
3789     };
3790     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3791     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3792     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3793     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3794               PreprocessedEntityRemap);
3795     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3796     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3797     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3798     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3799 
3800     // Global -> local mappings.
3801     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3802   }
3803 }
3804 
3805 ASTReader::ASTReadResult
3806 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3807                                   const ModuleFile *ImportedBy,
3808                                   unsigned ClientLoadCapabilities) {
3809   unsigned Idx = 0;
3810   F.ModuleMapPath = ReadPath(F, Record, Idx);
3811 
3812   // Try to resolve ModuleName in the current header search context and
3813   // verify that it is found in the same module map file as we saved. If the
3814   // top-level AST file is a main file, skip this check because there is no
3815   // usable header search context.
3816   assert(!F.ModuleName.empty() &&
3817          "MODULE_NAME should come before MODULE_MAP_FILE");
3818   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3819     // An implicitly-loaded module file should have its module listed in some
3820     // module map file that we've already loaded.
3821     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3822     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3823     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3824     // Don't emit module relocation error if we have -fno-validate-pch
3825     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3826       assert(ImportedBy && "top-level import should be verified");
3827       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3828         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3829           // This module was defined by an imported (explicit) module.
3830           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3831                                                << ASTFE->getName();
3832         } else {
3833           // This module was built with a different module map.
3834           Diag(diag::err_imported_module_not_found)
3835               << F.ModuleName << F.FileName << ImportedBy->FileName
3836               << F.ModuleMapPath;
3837           // In case it was imported by a PCH, there's a chance the user is
3838           // just missing to include the search path to the directory containing
3839           // the modulemap.
3840           if (ImportedBy->Kind == MK_PCH)
3841             Diag(diag::note_imported_by_pch_module_not_found)
3842                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3843         }
3844       }
3845       return OutOfDate;
3846     }
3847 
3848     assert(M->Name == F.ModuleName && "found module with different name");
3849 
3850     // Check the primary module map file.
3851     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3852     if (!StoredModMap || *StoredModMap != ModMap) {
3853       assert(ModMap && "found module is missing module map file");
3854       assert(ImportedBy && "top-level import should be verified");
3855       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3856         Diag(diag::err_imported_module_modmap_changed)
3857           << F.ModuleName << ImportedBy->FileName
3858           << ModMap->getName() << F.ModuleMapPath;
3859       return OutOfDate;
3860     }
3861 
3862     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3863     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3864       // FIXME: we should use input files rather than storing names.
3865       std::string Filename = ReadPath(F, Record, Idx);
3866       auto F = FileMgr.getFile(Filename, false, false);
3867       if (!F) {
3868         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3869           Error("could not find file '" + Filename +"' referenced by AST file");
3870         return OutOfDate;
3871       }
3872       AdditionalStoredMaps.insert(*F);
3873     }
3874 
3875     // Check any additional module map files (e.g. module.private.modulemap)
3876     // that are not in the pcm.
3877     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3878       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3879         // Remove files that match
3880         // Note: SmallPtrSet::erase is really remove
3881         if (!AdditionalStoredMaps.erase(ModMap)) {
3882           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3883             Diag(diag::err_module_different_modmap)
3884               << F.ModuleName << /*new*/0 << ModMap->getName();
3885           return OutOfDate;
3886         }
3887       }
3888     }
3889 
3890     // Check any additional module map files that are in the pcm, but not
3891     // found in header search. Cases that match are already removed.
3892     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3893       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3894         Diag(diag::err_module_different_modmap)
3895           << F.ModuleName << /*not new*/1 << ModMap->getName();
3896       return OutOfDate;
3897     }
3898   }
3899 
3900   if (Listener)
3901     Listener->ReadModuleMapFile(F.ModuleMapPath);
3902   return Success;
3903 }
3904 
3905 /// Move the given method to the back of the global list of methods.
3906 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3907   // Find the entry for this selector in the method pool.
3908   Sema::GlobalMethodPool::iterator Known
3909     = S.MethodPool.find(Method->getSelector());
3910   if (Known == S.MethodPool.end())
3911     return;
3912 
3913   // Retrieve the appropriate method list.
3914   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3915                                                     : Known->second.second;
3916   bool Found = false;
3917   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3918     if (!Found) {
3919       if (List->getMethod() == Method) {
3920         Found = true;
3921       } else {
3922         // Keep searching.
3923         continue;
3924       }
3925     }
3926 
3927     if (List->getNext())
3928       List->setMethod(List->getNext()->getMethod());
3929     else
3930       List->setMethod(Method);
3931   }
3932 }
3933 
3934 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3935   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3936   for (Decl *D : Names) {
3937     bool wasHidden = D->isHidden();
3938     D->setVisibleDespiteOwningModule();
3939 
3940     if (wasHidden && SemaObj) {
3941       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3942         moveMethodToBackOfGlobalList(*SemaObj, Method);
3943       }
3944     }
3945   }
3946 }
3947 
3948 void ASTReader::makeModuleVisible(Module *Mod,
3949                                   Module::NameVisibilityKind NameVisibility,
3950                                   SourceLocation ImportLoc) {
3951   llvm::SmallPtrSet<Module *, 4> Visited;
3952   SmallVector<Module *, 4> Stack;
3953   Stack.push_back(Mod);
3954   while (!Stack.empty()) {
3955     Mod = Stack.pop_back_val();
3956 
3957     if (NameVisibility <= Mod->NameVisibility) {
3958       // This module already has this level of visibility (or greater), so
3959       // there is nothing more to do.
3960       continue;
3961     }
3962 
3963     if (!Mod->isAvailable()) {
3964       // Modules that aren't available cannot be made visible.
3965       continue;
3966     }
3967 
3968     // Update the module's name visibility.
3969     Mod->NameVisibility = NameVisibility;
3970 
3971     // If we've already deserialized any names from this module,
3972     // mark them as visible.
3973     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3974     if (Hidden != HiddenNamesMap.end()) {
3975       auto HiddenNames = std::move(*Hidden);
3976       HiddenNamesMap.erase(Hidden);
3977       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3978       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3979              "making names visible added hidden names");
3980     }
3981 
3982     // Push any exported modules onto the stack to be marked as visible.
3983     SmallVector<Module *, 16> Exports;
3984     Mod->getExportedModules(Exports);
3985     for (SmallVectorImpl<Module *>::iterator
3986            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3987       Module *Exported = *I;
3988       if (Visited.insert(Exported).second)
3989         Stack.push_back(Exported);
3990     }
3991   }
3992 }
3993 
3994 /// We've merged the definition \p MergedDef into the existing definition
3995 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3996 /// visible.
3997 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3998                                           NamedDecl *MergedDef) {
3999   if (Def->isHidden()) {
4000     // If MergedDef is visible or becomes visible, make the definition visible.
4001     if (!MergedDef->isHidden())
4002       Def->setVisibleDespiteOwningModule();
4003     else {
4004       getContext().mergeDefinitionIntoModule(
4005           Def, MergedDef->getImportedOwningModule(),
4006           /*NotifyListeners*/ false);
4007       PendingMergedDefinitionsToDeduplicate.insert(Def);
4008     }
4009   }
4010 }
4011 
4012 bool ASTReader::loadGlobalIndex() {
4013   if (GlobalIndex)
4014     return false;
4015 
4016   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4017       !PP.getLangOpts().Modules)
4018     return true;
4019 
4020   // Try to load the global index.
4021   TriedLoadingGlobalIndex = true;
4022   StringRef ModuleCachePath
4023     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4024   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4025       GlobalModuleIndex::readIndex(ModuleCachePath);
4026   if (llvm::Error Err = std::move(Result.second)) {
4027     assert(!Result.first);
4028     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4029     return true;
4030   }
4031 
4032   GlobalIndex.reset(Result.first);
4033   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4034   return false;
4035 }
4036 
4037 bool ASTReader::isGlobalIndexUnavailable() const {
4038   return PP.getLangOpts().Modules && UseGlobalIndex &&
4039          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4040 }
4041 
4042 static void updateModuleTimestamp(ModuleFile &MF) {
4043   // Overwrite the timestamp file contents so that file's mtime changes.
4044   std::string TimestampFilename = MF.getTimestampFilename();
4045   std::error_code EC;
4046   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4047   if (EC)
4048     return;
4049   OS << "Timestamp file\n";
4050   OS.close();
4051   OS.clear_error(); // Avoid triggering a fatal error.
4052 }
4053 
4054 /// Given a cursor at the start of an AST file, scan ahead and drop the
4055 /// cursor into the start of the given block ID, returning false on success and
4056 /// true on failure.
4057 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4058   while (true) {
4059     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4060     if (!MaybeEntry) {
4061       // FIXME this drops errors on the floor.
4062       consumeError(MaybeEntry.takeError());
4063       return true;
4064     }
4065     llvm::BitstreamEntry Entry = MaybeEntry.get();
4066 
4067     switch (Entry.Kind) {
4068     case llvm::BitstreamEntry::Error:
4069     case llvm::BitstreamEntry::EndBlock:
4070       return true;
4071 
4072     case llvm::BitstreamEntry::Record:
4073       // Ignore top-level records.
4074       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4075         break;
4076       else {
4077         // FIXME this drops errors on the floor.
4078         consumeError(Skipped.takeError());
4079         return true;
4080       }
4081 
4082     case llvm::BitstreamEntry::SubBlock:
4083       if (Entry.ID == BlockID) {
4084         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4085           // FIXME this drops the error on the floor.
4086           consumeError(std::move(Err));
4087           return true;
4088         }
4089         // Found it!
4090         return false;
4091       }
4092 
4093       if (llvm::Error Err = Cursor.SkipBlock()) {
4094         // FIXME this drops the error on the floor.
4095         consumeError(std::move(Err));
4096         return true;
4097       }
4098     }
4099   }
4100 }
4101 
4102 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4103                                             ModuleKind Type,
4104                                             SourceLocation ImportLoc,
4105                                             unsigned ClientLoadCapabilities,
4106                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4107   llvm::SaveAndRestore<SourceLocation>
4108     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4109 
4110   // Defer any pending actions until we get to the end of reading the AST file.
4111   Deserializing AnASTFile(this);
4112 
4113   // Bump the generation number.
4114   unsigned PreviousGeneration = 0;
4115   if (ContextObj)
4116     PreviousGeneration = incrementGeneration(*ContextObj);
4117 
4118   unsigned NumModules = ModuleMgr.size();
4119   SmallVector<ImportedModule, 4> Loaded;
4120   switch (ASTReadResult ReadResult =
4121               ReadASTCore(FileName, Type, ImportLoc,
4122                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4123                           ASTFileSignature(), ClientLoadCapabilities)) {
4124   case Failure:
4125   case Missing:
4126   case OutOfDate:
4127   case VersionMismatch:
4128   case ConfigurationMismatch:
4129   case HadErrors: {
4130     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4131     for (const ImportedModule &IM : Loaded)
4132       LoadedSet.insert(IM.Mod);
4133 
4134     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4135                             PP.getLangOpts().Modules
4136                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4137                                 : nullptr);
4138 
4139     // If we find that any modules are unusable, the global index is going
4140     // to be out-of-date. Just remove it.
4141     GlobalIndex.reset();
4142     ModuleMgr.setGlobalIndex(nullptr);
4143     return ReadResult;
4144   }
4145   case Success:
4146     break;
4147   }
4148 
4149   // Here comes stuff that we only do once the entire chain is loaded.
4150 
4151   // Load the AST blocks of all of the modules that we loaded.
4152   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4153                                               MEnd = Loaded.end();
4154        M != MEnd; ++M) {
4155     ModuleFile &F = *M->Mod;
4156 
4157     // Read the AST block.
4158     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4159       return Result;
4160 
4161     // Read the extension blocks.
4162     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4163       if (ASTReadResult Result = ReadExtensionBlock(F))
4164         return Result;
4165     }
4166 
4167     // Once read, set the ModuleFile bit base offset and update the size in
4168     // bits of all files we've seen.
4169     F.GlobalBitOffset = TotalModulesSizeInBits;
4170     TotalModulesSizeInBits += F.SizeInBits;
4171     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4172 
4173     // Preload SLocEntries.
4174     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4175       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4176       // Load it through the SourceManager and don't call ReadSLocEntry()
4177       // directly because the entry may have already been loaded in which case
4178       // calling ReadSLocEntry() directly would trigger an assertion in
4179       // SourceManager.
4180       SourceMgr.getLoadedSLocEntryByID(Index);
4181     }
4182 
4183     // Map the original source file ID into the ID space of the current
4184     // compilation.
4185     if (F.OriginalSourceFileID.isValid()) {
4186       F.OriginalSourceFileID = FileID::get(
4187           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4188     }
4189 
4190     // Preload all the pending interesting identifiers by marking them out of
4191     // date.
4192     for (auto Offset : F.PreloadIdentifierOffsets) {
4193       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4194           F.IdentifierTableData + Offset);
4195 
4196       ASTIdentifierLookupTrait Trait(*this, F);
4197       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4198       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4199       auto &II = PP.getIdentifierTable().getOwn(Key);
4200       II.setOutOfDate(true);
4201 
4202       // Mark this identifier as being from an AST file so that we can track
4203       // whether we need to serialize it.
4204       markIdentifierFromAST(*this, II);
4205 
4206       // Associate the ID with the identifier so that the writer can reuse it.
4207       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4208       SetIdentifierInfo(ID, &II);
4209     }
4210   }
4211 
4212   // Setup the import locations and notify the module manager that we've
4213   // committed to these module files.
4214   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4215                                               MEnd = Loaded.end();
4216        M != MEnd; ++M) {
4217     ModuleFile &F = *M->Mod;
4218 
4219     ModuleMgr.moduleFileAccepted(&F);
4220 
4221     // Set the import location.
4222     F.DirectImportLoc = ImportLoc;
4223     // FIXME: We assume that locations from PCH / preamble do not need
4224     // any translation.
4225     if (!M->ImportedBy)
4226       F.ImportLoc = M->ImportLoc;
4227     else
4228       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4229   }
4230 
4231   if (!PP.getLangOpts().CPlusPlus ||
4232       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4233        Type != MK_PrebuiltModule)) {
4234     // Mark all of the identifiers in the identifier table as being out of date,
4235     // so that various accessors know to check the loaded modules when the
4236     // identifier is used.
4237     //
4238     // For C++ modules, we don't need information on many identifiers (just
4239     // those that provide macros or are poisoned), so we mark all of
4240     // the interesting ones via PreloadIdentifierOffsets.
4241     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4242                                 IdEnd = PP.getIdentifierTable().end();
4243          Id != IdEnd; ++Id)
4244       Id->second->setOutOfDate(true);
4245   }
4246   // Mark selectors as out of date.
4247   for (auto Sel : SelectorGeneration)
4248     SelectorOutOfDate[Sel.first] = true;
4249 
4250   // Resolve any unresolved module exports.
4251   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4252     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4253     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4254     Module *ResolvedMod = getSubmodule(GlobalID);
4255 
4256     switch (Unresolved.Kind) {
4257     case UnresolvedModuleRef::Conflict:
4258       if (ResolvedMod) {
4259         Module::Conflict Conflict;
4260         Conflict.Other = ResolvedMod;
4261         Conflict.Message = Unresolved.String.str();
4262         Unresolved.Mod->Conflicts.push_back(Conflict);
4263       }
4264       continue;
4265 
4266     case UnresolvedModuleRef::Import:
4267       if (ResolvedMod)
4268         Unresolved.Mod->Imports.insert(ResolvedMod);
4269       continue;
4270 
4271     case UnresolvedModuleRef::Export:
4272       if (ResolvedMod || Unresolved.IsWildcard)
4273         Unresolved.Mod->Exports.push_back(
4274           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4275       continue;
4276     }
4277   }
4278   UnresolvedModuleRefs.clear();
4279 
4280   if (Imported)
4281     Imported->append(ImportedModules.begin(),
4282                      ImportedModules.end());
4283 
4284   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4285   // Might be unnecessary as use declarations are only used to build the
4286   // module itself.
4287 
4288   if (ContextObj)
4289     InitializeContext();
4290 
4291   if (SemaObj)
4292     UpdateSema();
4293 
4294   if (DeserializationListener)
4295     DeserializationListener->ReaderInitialized(this);
4296 
4297   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4298   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4299     // If this AST file is a precompiled preamble, then set the
4300     // preamble file ID of the source manager to the file source file
4301     // from which the preamble was built.
4302     if (Type == MK_Preamble) {
4303       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4304     } else if (Type == MK_MainFile) {
4305       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4306     }
4307   }
4308 
4309   // For any Objective-C class definitions we have already loaded, make sure
4310   // that we load any additional categories.
4311   if (ContextObj) {
4312     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4313       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4314                          ObjCClassesLoaded[I],
4315                          PreviousGeneration);
4316     }
4317   }
4318 
4319   if (PP.getHeaderSearchInfo()
4320           .getHeaderSearchOpts()
4321           .ModulesValidateOncePerBuildSession) {
4322     // Now we are certain that the module and all modules it depends on are
4323     // up to date.  Create or update timestamp files for modules that are
4324     // located in the module cache (not for PCH files that could be anywhere
4325     // in the filesystem).
4326     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4327       ImportedModule &M = Loaded[I];
4328       if (M.Mod->Kind == MK_ImplicitModule) {
4329         updateModuleTimestamp(*M.Mod);
4330       }
4331     }
4332   }
4333 
4334   return Success;
4335 }
4336 
4337 static ASTFileSignature readASTFileSignature(StringRef PCH);
4338 
4339 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4340 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4341   // FIXME checking magic headers is done in other places such as
4342   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4343   // always done the same. Unify it all with a helper.
4344   if (!Stream.canSkipToPos(4))
4345     return llvm::createStringError(std::errc::illegal_byte_sequence,
4346                                    "file too small to contain AST file magic");
4347   for (unsigned C : {'C', 'P', 'C', 'H'})
4348     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4349       if (Res.get() != C)
4350         return llvm::createStringError(
4351             std::errc::illegal_byte_sequence,
4352             "file doesn't start with AST file magic");
4353     } else
4354       return Res.takeError();
4355   return llvm::Error::success();
4356 }
4357 
4358 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4359   switch (Kind) {
4360   case MK_PCH:
4361     return 0; // PCH
4362   case MK_ImplicitModule:
4363   case MK_ExplicitModule:
4364   case MK_PrebuiltModule:
4365     return 1; // module
4366   case MK_MainFile:
4367   case MK_Preamble:
4368     return 2; // main source file
4369   }
4370   llvm_unreachable("unknown module kind");
4371 }
4372 
4373 ASTReader::ASTReadResult
4374 ASTReader::ReadASTCore(StringRef FileName,
4375                        ModuleKind Type,
4376                        SourceLocation ImportLoc,
4377                        ModuleFile *ImportedBy,
4378                        SmallVectorImpl<ImportedModule> &Loaded,
4379                        off_t ExpectedSize, time_t ExpectedModTime,
4380                        ASTFileSignature ExpectedSignature,
4381                        unsigned ClientLoadCapabilities) {
4382   ModuleFile *M;
4383   std::string ErrorStr;
4384   ModuleManager::AddModuleResult AddResult
4385     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4386                           getGeneration(), ExpectedSize, ExpectedModTime,
4387                           ExpectedSignature, readASTFileSignature,
4388                           M, ErrorStr);
4389 
4390   switch (AddResult) {
4391   case ModuleManager::AlreadyLoaded:
4392     Diag(diag::remark_module_import)
4393         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4394         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4395     return Success;
4396 
4397   case ModuleManager::NewlyLoaded:
4398     // Load module file below.
4399     break;
4400 
4401   case ModuleManager::Missing:
4402     // The module file was missing; if the client can handle that, return
4403     // it.
4404     if (ClientLoadCapabilities & ARR_Missing)
4405       return Missing;
4406 
4407     // Otherwise, return an error.
4408     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4409                                           << FileName << !ErrorStr.empty()
4410                                           << ErrorStr;
4411     return Failure;
4412 
4413   case ModuleManager::OutOfDate:
4414     // We couldn't load the module file because it is out-of-date. If the
4415     // client can handle out-of-date, return it.
4416     if (ClientLoadCapabilities & ARR_OutOfDate)
4417       return OutOfDate;
4418 
4419     // Otherwise, return an error.
4420     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4421                                             << FileName << !ErrorStr.empty()
4422                                             << ErrorStr;
4423     return Failure;
4424   }
4425 
4426   assert(M && "Missing module file");
4427 
4428   bool ShouldFinalizePCM = false;
4429   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4430     auto &MC = getModuleManager().getModuleCache();
4431     if (ShouldFinalizePCM)
4432       MC.finalizePCM(FileName);
4433     else
4434       MC.tryToDropPCM(FileName);
4435   });
4436   ModuleFile &F = *M;
4437   BitstreamCursor &Stream = F.Stream;
4438   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4439   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4440 
4441   // Sniff for the signature.
4442   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4443     Diag(diag::err_module_file_invalid)
4444         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4445     return Failure;
4446   }
4447 
4448   // This is used for compatibility with older PCH formats.
4449   bool HaveReadControlBlock = false;
4450   while (true) {
4451     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4452     if (!MaybeEntry) {
4453       Error(MaybeEntry.takeError());
4454       return Failure;
4455     }
4456     llvm::BitstreamEntry Entry = MaybeEntry.get();
4457 
4458     switch (Entry.Kind) {
4459     case llvm::BitstreamEntry::Error:
4460     case llvm::BitstreamEntry::Record:
4461     case llvm::BitstreamEntry::EndBlock:
4462       Error("invalid record at top-level of AST file");
4463       return Failure;
4464 
4465     case llvm::BitstreamEntry::SubBlock:
4466       break;
4467     }
4468 
4469     switch (Entry.ID) {
4470     case CONTROL_BLOCK_ID:
4471       HaveReadControlBlock = true;
4472       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4473       case Success:
4474         // Check that we didn't try to load a non-module AST file as a module.
4475         //
4476         // FIXME: Should we also perform the converse check? Loading a module as
4477         // a PCH file sort of works, but it's a bit wonky.
4478         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4479              Type == MK_PrebuiltModule) &&
4480             F.ModuleName.empty()) {
4481           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4482           if (Result != OutOfDate ||
4483               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4484             Diag(diag::err_module_file_not_module) << FileName;
4485           return Result;
4486         }
4487         break;
4488 
4489       case Failure: return Failure;
4490       case Missing: return Missing;
4491       case OutOfDate: return OutOfDate;
4492       case VersionMismatch: return VersionMismatch;
4493       case ConfigurationMismatch: return ConfigurationMismatch;
4494       case HadErrors: return HadErrors;
4495       }
4496       break;
4497 
4498     case AST_BLOCK_ID:
4499       if (!HaveReadControlBlock) {
4500         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4501           Diag(diag::err_pch_version_too_old);
4502         return VersionMismatch;
4503       }
4504 
4505       // Record that we've loaded this module.
4506       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4507       ShouldFinalizePCM = true;
4508       return Success;
4509 
4510     case UNHASHED_CONTROL_BLOCK_ID:
4511       // This block is handled using look-ahead during ReadControlBlock.  We
4512       // shouldn't get here!
4513       Error("malformed block record in AST file");
4514       return Failure;
4515 
4516     default:
4517       if (llvm::Error Err = Stream.SkipBlock()) {
4518         Error(std::move(Err));
4519         return Failure;
4520       }
4521       break;
4522     }
4523   }
4524 
4525   llvm_unreachable("unexpected break; expected return");
4526 }
4527 
4528 ASTReader::ASTReadResult
4529 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4530                                     unsigned ClientLoadCapabilities) {
4531   const HeaderSearchOptions &HSOpts =
4532       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4533   bool AllowCompatibleConfigurationMismatch =
4534       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4535 
4536   ASTReadResult Result = readUnhashedControlBlockImpl(
4537       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4538       Listener.get(),
4539       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4540 
4541   // If F was directly imported by another module, it's implicitly validated by
4542   // the importing module.
4543   if (DisableValidation || WasImportedBy ||
4544       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4545     return Success;
4546 
4547   if (Result == Failure) {
4548     Error("malformed block record in AST file");
4549     return Failure;
4550   }
4551 
4552   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4553     // If this module has already been finalized in the ModuleCache, we're stuck
4554     // with it; we can only load a single version of each module.
4555     //
4556     // This can happen when a module is imported in two contexts: in one, as a
4557     // user module; in another, as a system module (due to an import from
4558     // another module marked with the [system] flag).  It usually indicates a
4559     // bug in the module map: this module should also be marked with [system].
4560     //
4561     // If -Wno-system-headers (the default), and the first import is as a
4562     // system module, then validation will fail during the as-user import,
4563     // since -Werror flags won't have been validated.  However, it's reasonable
4564     // to treat this consistently as a system module.
4565     //
4566     // If -Wsystem-headers, the PCM on disk was built with
4567     // -Wno-system-headers, and the first import is as a user module, then
4568     // validation will fail during the as-system import since the PCM on disk
4569     // doesn't guarantee that -Werror was respected.  However, the -Werror
4570     // flags were checked during the initial as-user import.
4571     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4572       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4573       return Success;
4574     }
4575   }
4576 
4577   return Result;
4578 }
4579 
4580 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4581     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4582     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4583     bool ValidateDiagnosticOptions) {
4584   // Initialize a stream.
4585   BitstreamCursor Stream(StreamData);
4586 
4587   // Sniff for the signature.
4588   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4589     // FIXME this drops the error on the floor.
4590     consumeError(std::move(Err));
4591     return Failure;
4592   }
4593 
4594   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4595   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4596     return Failure;
4597 
4598   // Read all of the records in the options block.
4599   RecordData Record;
4600   ASTReadResult Result = Success;
4601   while (true) {
4602     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4603     if (!MaybeEntry) {
4604       // FIXME this drops the error on the floor.
4605       consumeError(MaybeEntry.takeError());
4606       return Failure;
4607     }
4608     llvm::BitstreamEntry Entry = MaybeEntry.get();
4609 
4610     switch (Entry.Kind) {
4611     case llvm::BitstreamEntry::Error:
4612     case llvm::BitstreamEntry::SubBlock:
4613       return Failure;
4614 
4615     case llvm::BitstreamEntry::EndBlock:
4616       return Result;
4617 
4618     case llvm::BitstreamEntry::Record:
4619       // The interesting case.
4620       break;
4621     }
4622 
4623     // Read and process a record.
4624     Record.clear();
4625     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4626     if (!MaybeRecordType) {
4627       // FIXME this drops the error.
4628       return Failure;
4629     }
4630     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4631     case SIGNATURE:
4632       if (F)
4633         std::copy(Record.begin(), Record.end(), F->Signature.data());
4634       break;
4635     case DIAGNOSTIC_OPTIONS: {
4636       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4637       if (Listener && ValidateDiagnosticOptions &&
4638           !AllowCompatibleConfigurationMismatch &&
4639           ParseDiagnosticOptions(Record, Complain, *Listener))
4640         Result = OutOfDate; // Don't return early.  Read the signature.
4641       break;
4642     }
4643     case DIAG_PRAGMA_MAPPINGS:
4644       if (!F)
4645         break;
4646       if (F->PragmaDiagMappings.empty())
4647         F->PragmaDiagMappings.swap(Record);
4648       else
4649         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4650                                      Record.begin(), Record.end());
4651       break;
4652     }
4653   }
4654 }
4655 
4656 /// Parse a record and blob containing module file extension metadata.
4657 static bool parseModuleFileExtensionMetadata(
4658               const SmallVectorImpl<uint64_t> &Record,
4659               StringRef Blob,
4660               ModuleFileExtensionMetadata &Metadata) {
4661   if (Record.size() < 4) return true;
4662 
4663   Metadata.MajorVersion = Record[0];
4664   Metadata.MinorVersion = Record[1];
4665 
4666   unsigned BlockNameLen = Record[2];
4667   unsigned UserInfoLen = Record[3];
4668 
4669   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4670 
4671   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4672   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4673                                   Blob.data() + BlockNameLen + UserInfoLen);
4674   return false;
4675 }
4676 
4677 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4678   BitstreamCursor &Stream = F.Stream;
4679 
4680   RecordData Record;
4681   while (true) {
4682     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4683     if (!MaybeEntry) {
4684       Error(MaybeEntry.takeError());
4685       return Failure;
4686     }
4687     llvm::BitstreamEntry Entry = MaybeEntry.get();
4688 
4689     switch (Entry.Kind) {
4690     case llvm::BitstreamEntry::SubBlock:
4691       if (llvm::Error Err = Stream.SkipBlock()) {
4692         Error(std::move(Err));
4693         return Failure;
4694       }
4695       continue;
4696 
4697     case llvm::BitstreamEntry::EndBlock:
4698       return Success;
4699 
4700     case llvm::BitstreamEntry::Error:
4701       return HadErrors;
4702 
4703     case llvm::BitstreamEntry::Record:
4704       break;
4705     }
4706 
4707     Record.clear();
4708     StringRef Blob;
4709     Expected<unsigned> MaybeRecCode =
4710         Stream.readRecord(Entry.ID, Record, &Blob);
4711     if (!MaybeRecCode) {
4712       Error(MaybeRecCode.takeError());
4713       return Failure;
4714     }
4715     switch (MaybeRecCode.get()) {
4716     case EXTENSION_METADATA: {
4717       ModuleFileExtensionMetadata Metadata;
4718       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4719         return Failure;
4720 
4721       // Find a module file extension with this block name.
4722       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4723       if (Known == ModuleFileExtensions.end()) break;
4724 
4725       // Form a reader.
4726       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4727                                                              F, Stream)) {
4728         F.ExtensionReaders.push_back(std::move(Reader));
4729       }
4730 
4731       break;
4732     }
4733     }
4734   }
4735 
4736   return Success;
4737 }
4738 
4739 void ASTReader::InitializeContext() {
4740   assert(ContextObj && "no context to initialize");
4741   ASTContext &Context = *ContextObj;
4742 
4743   // If there's a listener, notify them that we "read" the translation unit.
4744   if (DeserializationListener)
4745     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4746                                       Context.getTranslationUnitDecl());
4747 
4748   // FIXME: Find a better way to deal with collisions between these
4749   // built-in types. Right now, we just ignore the problem.
4750 
4751   // Load the special types.
4752   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4753     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4754       if (!Context.CFConstantStringTypeDecl)
4755         Context.setCFConstantStringType(GetType(String));
4756     }
4757 
4758     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4759       QualType FileType = GetType(File);
4760       if (FileType.isNull()) {
4761         Error("FILE type is NULL");
4762         return;
4763       }
4764 
4765       if (!Context.FILEDecl) {
4766         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4767           Context.setFILEDecl(Typedef->getDecl());
4768         else {
4769           const TagType *Tag = FileType->getAs<TagType>();
4770           if (!Tag) {
4771             Error("Invalid FILE type in AST file");
4772             return;
4773           }
4774           Context.setFILEDecl(Tag->getDecl());
4775         }
4776       }
4777     }
4778 
4779     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4780       QualType Jmp_bufType = GetType(Jmp_buf);
4781       if (Jmp_bufType.isNull()) {
4782         Error("jmp_buf type is NULL");
4783         return;
4784       }
4785 
4786       if (!Context.jmp_bufDecl) {
4787         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4788           Context.setjmp_bufDecl(Typedef->getDecl());
4789         else {
4790           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4791           if (!Tag) {
4792             Error("Invalid jmp_buf type in AST file");
4793             return;
4794           }
4795           Context.setjmp_bufDecl(Tag->getDecl());
4796         }
4797       }
4798     }
4799 
4800     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4801       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4802       if (Sigjmp_bufType.isNull()) {
4803         Error("sigjmp_buf type is NULL");
4804         return;
4805       }
4806 
4807       if (!Context.sigjmp_bufDecl) {
4808         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4809           Context.setsigjmp_bufDecl(Typedef->getDecl());
4810         else {
4811           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4812           assert(Tag && "Invalid sigjmp_buf type in AST file");
4813           Context.setsigjmp_bufDecl(Tag->getDecl());
4814         }
4815       }
4816     }
4817 
4818     if (unsigned ObjCIdRedef
4819           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4820       if (Context.ObjCIdRedefinitionType.isNull())
4821         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4822     }
4823 
4824     if (unsigned ObjCClassRedef
4825           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4826       if (Context.ObjCClassRedefinitionType.isNull())
4827         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4828     }
4829 
4830     if (unsigned ObjCSelRedef
4831           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4832       if (Context.ObjCSelRedefinitionType.isNull())
4833         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4834     }
4835 
4836     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4837       QualType Ucontext_tType = GetType(Ucontext_t);
4838       if (Ucontext_tType.isNull()) {
4839         Error("ucontext_t type is NULL");
4840         return;
4841       }
4842 
4843       if (!Context.ucontext_tDecl) {
4844         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4845           Context.setucontext_tDecl(Typedef->getDecl());
4846         else {
4847           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4848           assert(Tag && "Invalid ucontext_t type in AST file");
4849           Context.setucontext_tDecl(Tag->getDecl());
4850         }
4851       }
4852     }
4853   }
4854 
4855   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4856 
4857   // If there were any CUDA special declarations, deserialize them.
4858   if (!CUDASpecialDeclRefs.empty()) {
4859     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4860     Context.setcudaConfigureCallDecl(
4861                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4862   }
4863 
4864   // Re-export any modules that were imported by a non-module AST file.
4865   // FIXME: This does not make macro-only imports visible again.
4866   for (auto &Import : ImportedModules) {
4867     if (Module *Imported = getSubmodule(Import.ID)) {
4868       makeModuleVisible(Imported, Module::AllVisible,
4869                         /*ImportLoc=*/Import.ImportLoc);
4870       if (Import.ImportLoc.isValid())
4871         PP.makeModuleVisible(Imported, Import.ImportLoc);
4872       // FIXME: should we tell Sema to make the module visible too?
4873     }
4874   }
4875   ImportedModules.clear();
4876 }
4877 
4878 void ASTReader::finalizeForWriting() {
4879   // Nothing to do for now.
4880 }
4881 
4882 /// Reads and return the signature record from \p PCH's control block, or
4883 /// else returns 0.
4884 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4885   BitstreamCursor Stream(PCH);
4886   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4887     // FIXME this drops the error on the floor.
4888     consumeError(std::move(Err));
4889     return ASTFileSignature();
4890   }
4891 
4892   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4893   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4894     return ASTFileSignature();
4895 
4896   // Scan for SIGNATURE inside the diagnostic options block.
4897   ASTReader::RecordData Record;
4898   while (true) {
4899     Expected<llvm::BitstreamEntry> MaybeEntry =
4900         Stream.advanceSkippingSubblocks();
4901     if (!MaybeEntry) {
4902       // FIXME this drops the error on the floor.
4903       consumeError(MaybeEntry.takeError());
4904       return ASTFileSignature();
4905     }
4906     llvm::BitstreamEntry Entry = MaybeEntry.get();
4907 
4908     if (Entry.Kind != llvm::BitstreamEntry::Record)
4909       return ASTFileSignature();
4910 
4911     Record.clear();
4912     StringRef Blob;
4913     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4914     if (!MaybeRecord) {
4915       // FIXME this drops the error on the floor.
4916       consumeError(MaybeRecord.takeError());
4917       return ASTFileSignature();
4918     }
4919     if (SIGNATURE == MaybeRecord.get())
4920       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4921                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4922   }
4923 }
4924 
4925 /// Retrieve the name of the original source file name
4926 /// directly from the AST file, without actually loading the AST
4927 /// file.
4928 std::string ASTReader::getOriginalSourceFile(
4929     const std::string &ASTFileName, FileManager &FileMgr,
4930     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4931   // Open the AST file.
4932   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4933   if (!Buffer) {
4934     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4935         << ASTFileName << Buffer.getError().message();
4936     return std::string();
4937   }
4938 
4939   // Initialize the stream
4940   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4941 
4942   // Sniff for the signature.
4943   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4944     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
4945     return std::string();
4946   }
4947 
4948   // Scan for the CONTROL_BLOCK_ID block.
4949   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4950     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4951     return std::string();
4952   }
4953 
4954   // Scan for ORIGINAL_FILE inside the control block.
4955   RecordData Record;
4956   while (true) {
4957     Expected<llvm::BitstreamEntry> MaybeEntry =
4958         Stream.advanceSkippingSubblocks();
4959     if (!MaybeEntry) {
4960       // FIXME this drops errors on the floor.
4961       consumeError(MaybeEntry.takeError());
4962       return std::string();
4963     }
4964     llvm::BitstreamEntry Entry = MaybeEntry.get();
4965 
4966     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4967       return std::string();
4968 
4969     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4970       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4971       return std::string();
4972     }
4973 
4974     Record.clear();
4975     StringRef Blob;
4976     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4977     if (!MaybeRecord) {
4978       // FIXME this drops the errors on the floor.
4979       consumeError(MaybeRecord.takeError());
4980       return std::string();
4981     }
4982     if (ORIGINAL_FILE == MaybeRecord.get())
4983       return Blob.str();
4984   }
4985 }
4986 
4987 namespace {
4988 
4989   class SimplePCHValidator : public ASTReaderListener {
4990     const LangOptions &ExistingLangOpts;
4991     const TargetOptions &ExistingTargetOpts;
4992     const PreprocessorOptions &ExistingPPOpts;
4993     std::string ExistingModuleCachePath;
4994     FileManager &FileMgr;
4995 
4996   public:
4997     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4998                        const TargetOptions &ExistingTargetOpts,
4999                        const PreprocessorOptions &ExistingPPOpts,
5000                        StringRef ExistingModuleCachePath,
5001                        FileManager &FileMgr)
5002       : ExistingLangOpts(ExistingLangOpts),
5003         ExistingTargetOpts(ExistingTargetOpts),
5004         ExistingPPOpts(ExistingPPOpts),
5005         ExistingModuleCachePath(ExistingModuleCachePath),
5006         FileMgr(FileMgr) {}
5007 
5008     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5009                              bool AllowCompatibleDifferences) override {
5010       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5011                                   AllowCompatibleDifferences);
5012     }
5013 
5014     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5015                            bool AllowCompatibleDifferences) override {
5016       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5017                                 AllowCompatibleDifferences);
5018     }
5019 
5020     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5021                                  StringRef SpecificModuleCachePath,
5022                                  bool Complain) override {
5023       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5024                                       ExistingModuleCachePath,
5025                                       nullptr, ExistingLangOpts);
5026     }
5027 
5028     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5029                                  bool Complain,
5030                                  std::string &SuggestedPredefines) override {
5031       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5032                                       SuggestedPredefines, ExistingLangOpts);
5033     }
5034   };
5035 
5036 } // namespace
5037 
5038 bool ASTReader::readASTFileControlBlock(
5039     StringRef Filename, FileManager &FileMgr,
5040     const PCHContainerReader &PCHContainerRdr,
5041     bool FindModuleFileExtensions,
5042     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5043   // Open the AST file.
5044   // FIXME: This allows use of the VFS; we do not allow use of the
5045   // VFS when actually loading a module.
5046   auto Buffer = FileMgr.getBufferForFile(Filename);
5047   if (!Buffer) {
5048     return true;
5049   }
5050 
5051   // Initialize the stream
5052   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5053   BitstreamCursor Stream(Bytes);
5054 
5055   // Sniff for the signature.
5056   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5057     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5058     return true;
5059   }
5060 
5061   // Scan for the CONTROL_BLOCK_ID block.
5062   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5063     return true;
5064 
5065   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5066   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5067   bool NeedsImports = Listener.needsImportVisitation();
5068   BitstreamCursor InputFilesCursor;
5069 
5070   RecordData Record;
5071   std::string ModuleDir;
5072   bool DoneWithControlBlock = false;
5073   while (!DoneWithControlBlock) {
5074     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5075     if (!MaybeEntry) {
5076       // FIXME this drops the error on the floor.
5077       consumeError(MaybeEntry.takeError());
5078       return true;
5079     }
5080     llvm::BitstreamEntry Entry = MaybeEntry.get();
5081 
5082     switch (Entry.Kind) {
5083     case llvm::BitstreamEntry::SubBlock: {
5084       switch (Entry.ID) {
5085       case OPTIONS_BLOCK_ID: {
5086         std::string IgnoredSuggestedPredefines;
5087         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5088                              /*AllowCompatibleConfigurationMismatch*/ false,
5089                              Listener, IgnoredSuggestedPredefines) != Success)
5090           return true;
5091         break;
5092       }
5093 
5094       case INPUT_FILES_BLOCK_ID:
5095         InputFilesCursor = Stream;
5096         if (llvm::Error Err = Stream.SkipBlock()) {
5097           // FIXME this drops the error on the floor.
5098           consumeError(std::move(Err));
5099           return true;
5100         }
5101         if (NeedsInputFiles &&
5102             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5103           return true;
5104         break;
5105 
5106       default:
5107         if (llvm::Error Err = Stream.SkipBlock()) {
5108           // FIXME this drops the error on the floor.
5109           consumeError(std::move(Err));
5110           return true;
5111         }
5112         break;
5113       }
5114 
5115       continue;
5116     }
5117 
5118     case llvm::BitstreamEntry::EndBlock:
5119       DoneWithControlBlock = true;
5120       break;
5121 
5122     case llvm::BitstreamEntry::Error:
5123       return true;
5124 
5125     case llvm::BitstreamEntry::Record:
5126       break;
5127     }
5128 
5129     if (DoneWithControlBlock) break;
5130 
5131     Record.clear();
5132     StringRef Blob;
5133     Expected<unsigned> MaybeRecCode =
5134         Stream.readRecord(Entry.ID, Record, &Blob);
5135     if (!MaybeRecCode) {
5136       // FIXME this drops the error.
5137       return Failure;
5138     }
5139     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5140     case METADATA:
5141       if (Record[0] != VERSION_MAJOR)
5142         return true;
5143       if (Listener.ReadFullVersionInformation(Blob))
5144         return true;
5145       break;
5146     case MODULE_NAME:
5147       Listener.ReadModuleName(Blob);
5148       break;
5149     case MODULE_DIRECTORY:
5150       ModuleDir = Blob;
5151       break;
5152     case MODULE_MAP_FILE: {
5153       unsigned Idx = 0;
5154       auto Path = ReadString(Record, Idx);
5155       ResolveImportedPath(Path, ModuleDir);
5156       Listener.ReadModuleMapFile(Path);
5157       break;
5158     }
5159     case INPUT_FILE_OFFSETS: {
5160       if (!NeedsInputFiles)
5161         break;
5162 
5163       unsigned NumInputFiles = Record[0];
5164       unsigned NumUserFiles = Record[1];
5165       const llvm::support::unaligned_uint64_t *InputFileOffs =
5166           (const llvm::support::unaligned_uint64_t *)Blob.data();
5167       for (unsigned I = 0; I != NumInputFiles; ++I) {
5168         // Go find this input file.
5169         bool isSystemFile = I >= NumUserFiles;
5170 
5171         if (isSystemFile && !NeedsSystemInputFiles)
5172           break; // the rest are system input files
5173 
5174         BitstreamCursor &Cursor = InputFilesCursor;
5175         SavedStreamPosition SavedPosition(Cursor);
5176         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5177           // FIXME this drops errors on the floor.
5178           consumeError(std::move(Err));
5179         }
5180 
5181         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5182         if (!MaybeCode) {
5183           // FIXME this drops errors on the floor.
5184           consumeError(MaybeCode.takeError());
5185         }
5186         unsigned Code = MaybeCode.get();
5187 
5188         RecordData Record;
5189         StringRef Blob;
5190         bool shouldContinue = false;
5191         Expected<unsigned> MaybeRecordType =
5192             Cursor.readRecord(Code, Record, &Blob);
5193         if (!MaybeRecordType) {
5194           // FIXME this drops errors on the floor.
5195           consumeError(MaybeRecordType.takeError());
5196         }
5197         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5198         case INPUT_FILE:
5199           bool Overridden = static_cast<bool>(Record[3]);
5200           std::string Filename = Blob;
5201           ResolveImportedPath(Filename, ModuleDir);
5202           shouldContinue = Listener.visitInputFile(
5203               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5204           break;
5205         }
5206         if (!shouldContinue)
5207           break;
5208       }
5209       break;
5210     }
5211 
5212     case IMPORTS: {
5213       if (!NeedsImports)
5214         break;
5215 
5216       unsigned Idx = 0, N = Record.size();
5217       while (Idx < N) {
5218         // Read information about the AST file.
5219         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5220         std::string ModuleName = ReadString(Record, Idx);
5221         std::string Filename = ReadString(Record, Idx);
5222         ResolveImportedPath(Filename, ModuleDir);
5223         Listener.visitImport(ModuleName, Filename);
5224       }
5225       break;
5226     }
5227 
5228     default:
5229       // No other validation to perform.
5230       break;
5231     }
5232   }
5233 
5234   // Look for module file extension blocks, if requested.
5235   if (FindModuleFileExtensions) {
5236     BitstreamCursor SavedStream = Stream;
5237     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5238       bool DoneWithExtensionBlock = false;
5239       while (!DoneWithExtensionBlock) {
5240         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5241         if (!MaybeEntry) {
5242           // FIXME this drops the error.
5243           return true;
5244         }
5245         llvm::BitstreamEntry Entry = MaybeEntry.get();
5246 
5247         switch (Entry.Kind) {
5248         case llvm::BitstreamEntry::SubBlock:
5249           if (llvm::Error Err = Stream.SkipBlock()) {
5250             // FIXME this drops the error on the floor.
5251             consumeError(std::move(Err));
5252             return true;
5253           }
5254           continue;
5255 
5256         case llvm::BitstreamEntry::EndBlock:
5257           DoneWithExtensionBlock = true;
5258           continue;
5259 
5260         case llvm::BitstreamEntry::Error:
5261           return true;
5262 
5263         case llvm::BitstreamEntry::Record:
5264           break;
5265         }
5266 
5267        Record.clear();
5268        StringRef Blob;
5269        Expected<unsigned> MaybeRecCode =
5270            Stream.readRecord(Entry.ID, Record, &Blob);
5271        if (!MaybeRecCode) {
5272          // FIXME this drops the error.
5273          return true;
5274        }
5275        switch (MaybeRecCode.get()) {
5276        case EXTENSION_METADATA: {
5277          ModuleFileExtensionMetadata Metadata;
5278          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5279            return true;
5280 
5281          Listener.readModuleFileExtension(Metadata);
5282          break;
5283        }
5284        }
5285       }
5286     }
5287     Stream = SavedStream;
5288   }
5289 
5290   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5291   if (readUnhashedControlBlockImpl(
5292           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5293           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5294           ValidateDiagnosticOptions) != Success)
5295     return true;
5296 
5297   return false;
5298 }
5299 
5300 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5301                                     const PCHContainerReader &PCHContainerRdr,
5302                                     const LangOptions &LangOpts,
5303                                     const TargetOptions &TargetOpts,
5304                                     const PreprocessorOptions &PPOpts,
5305                                     StringRef ExistingModuleCachePath) {
5306   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5307                                ExistingModuleCachePath, FileMgr);
5308   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5309                                   /*FindModuleFileExtensions=*/false,
5310                                   validator,
5311                                   /*ValidateDiagnosticOptions=*/true);
5312 }
5313 
5314 ASTReader::ASTReadResult
5315 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5316   // Enter the submodule block.
5317   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5318     Error(std::move(Err));
5319     return Failure;
5320   }
5321 
5322   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5323   bool First = true;
5324   Module *CurrentModule = nullptr;
5325   RecordData Record;
5326   while (true) {
5327     Expected<llvm::BitstreamEntry> MaybeEntry =
5328         F.Stream.advanceSkippingSubblocks();
5329     if (!MaybeEntry) {
5330       Error(MaybeEntry.takeError());
5331       return Failure;
5332     }
5333     llvm::BitstreamEntry Entry = MaybeEntry.get();
5334 
5335     switch (Entry.Kind) {
5336     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5337     case llvm::BitstreamEntry::Error:
5338       Error("malformed block record in AST file");
5339       return Failure;
5340     case llvm::BitstreamEntry::EndBlock:
5341       return Success;
5342     case llvm::BitstreamEntry::Record:
5343       // The interesting case.
5344       break;
5345     }
5346 
5347     // Read a record.
5348     StringRef Blob;
5349     Record.clear();
5350     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5351     if (!MaybeKind) {
5352       Error(MaybeKind.takeError());
5353       return Failure;
5354     }
5355     unsigned Kind = MaybeKind.get();
5356 
5357     if ((Kind == SUBMODULE_METADATA) != First) {
5358       Error("submodule metadata record should be at beginning of block");
5359       return Failure;
5360     }
5361     First = false;
5362 
5363     // Submodule information is only valid if we have a current module.
5364     // FIXME: Should we error on these cases?
5365     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5366         Kind != SUBMODULE_DEFINITION)
5367       continue;
5368 
5369     switch (Kind) {
5370     default:  // Default behavior: ignore.
5371       break;
5372 
5373     case SUBMODULE_DEFINITION: {
5374       if (Record.size() < 12) {
5375         Error("malformed module definition");
5376         return Failure;
5377       }
5378 
5379       StringRef Name = Blob;
5380       unsigned Idx = 0;
5381       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5382       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5383       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5384       bool IsFramework = Record[Idx++];
5385       bool IsExplicit = Record[Idx++];
5386       bool IsSystem = Record[Idx++];
5387       bool IsExternC = Record[Idx++];
5388       bool InferSubmodules = Record[Idx++];
5389       bool InferExplicitSubmodules = Record[Idx++];
5390       bool InferExportWildcard = Record[Idx++];
5391       bool ConfigMacrosExhaustive = Record[Idx++];
5392       bool ModuleMapIsPrivate = Record[Idx++];
5393 
5394       Module *ParentModule = nullptr;
5395       if (Parent)
5396         ParentModule = getSubmodule(Parent);
5397 
5398       // Retrieve this (sub)module from the module map, creating it if
5399       // necessary.
5400       CurrentModule =
5401           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5402               .first;
5403 
5404       // FIXME: set the definition loc for CurrentModule, or call
5405       // ModMap.setInferredModuleAllowedBy()
5406 
5407       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5408       if (GlobalIndex >= SubmodulesLoaded.size() ||
5409           SubmodulesLoaded[GlobalIndex]) {
5410         Error("too many submodules");
5411         return Failure;
5412       }
5413 
5414       if (!ParentModule) {
5415         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5416           // Don't emit module relocation error if we have -fno-validate-pch
5417           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5418               CurFile != F.File) {
5419             if (!Diags.isDiagnosticInFlight()) {
5420               Diag(diag::err_module_file_conflict)
5421                 << CurrentModule->getTopLevelModuleName()
5422                 << CurFile->getName()
5423                 << F.File->getName();
5424             }
5425             return Failure;
5426           }
5427         }
5428 
5429         CurrentModule->setASTFile(F.File);
5430         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5431       }
5432 
5433       CurrentModule->Kind = Kind;
5434       CurrentModule->Signature = F.Signature;
5435       CurrentModule->IsFromModuleFile = true;
5436       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5437       CurrentModule->IsExternC = IsExternC;
5438       CurrentModule->InferSubmodules = InferSubmodules;
5439       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5440       CurrentModule->InferExportWildcard = InferExportWildcard;
5441       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5442       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5443       if (DeserializationListener)
5444         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5445 
5446       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5447 
5448       // Clear out data that will be replaced by what is in the module file.
5449       CurrentModule->LinkLibraries.clear();
5450       CurrentModule->ConfigMacros.clear();
5451       CurrentModule->UnresolvedConflicts.clear();
5452       CurrentModule->Conflicts.clear();
5453 
5454       // The module is available unless it's missing a requirement; relevant
5455       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5456       // Missing headers that were present when the module was built do not
5457       // make it unavailable -- if we got this far, this must be an explicitly
5458       // imported module file.
5459       CurrentModule->Requirements.clear();
5460       CurrentModule->MissingHeaders.clear();
5461       CurrentModule->IsMissingRequirement =
5462           ParentModule && ParentModule->IsMissingRequirement;
5463       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5464       break;
5465     }
5466 
5467     case SUBMODULE_UMBRELLA_HEADER: {
5468       std::string Filename = Blob;
5469       ResolveImportedPath(F, Filename);
5470       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5471         if (!CurrentModule->getUmbrellaHeader())
5472           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5473         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5474           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5475             Error("mismatched umbrella headers in submodule");
5476           return OutOfDate;
5477         }
5478       }
5479       break;
5480     }
5481 
5482     case SUBMODULE_HEADER:
5483     case SUBMODULE_EXCLUDED_HEADER:
5484     case SUBMODULE_PRIVATE_HEADER:
5485       // We lazily associate headers with their modules via the HeaderInfo table.
5486       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5487       // of complete filenames or remove it entirely.
5488       break;
5489 
5490     case SUBMODULE_TEXTUAL_HEADER:
5491     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5492       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5493       // them here.
5494       break;
5495 
5496     case SUBMODULE_TOPHEADER:
5497       CurrentModule->addTopHeaderFilename(Blob);
5498       break;
5499 
5500     case SUBMODULE_UMBRELLA_DIR: {
5501       std::string Dirname = Blob;
5502       ResolveImportedPath(F, Dirname);
5503       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5504         if (!CurrentModule->getUmbrellaDir())
5505           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5506         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5507           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5508             Error("mismatched umbrella directories in submodule");
5509           return OutOfDate;
5510         }
5511       }
5512       break;
5513     }
5514 
5515     case SUBMODULE_METADATA: {
5516       F.BaseSubmoduleID = getTotalNumSubmodules();
5517       F.LocalNumSubmodules = Record[0];
5518       unsigned LocalBaseSubmoduleID = Record[1];
5519       if (F.LocalNumSubmodules > 0) {
5520         // Introduce the global -> local mapping for submodules within this
5521         // module.
5522         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5523 
5524         // Introduce the local -> global mapping for submodules within this
5525         // module.
5526         F.SubmoduleRemap.insertOrReplace(
5527           std::make_pair(LocalBaseSubmoduleID,
5528                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5529 
5530         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5531       }
5532       break;
5533     }
5534 
5535     case SUBMODULE_IMPORTS:
5536       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5537         UnresolvedModuleRef Unresolved;
5538         Unresolved.File = &F;
5539         Unresolved.Mod = CurrentModule;
5540         Unresolved.ID = Record[Idx];
5541         Unresolved.Kind = UnresolvedModuleRef::Import;
5542         Unresolved.IsWildcard = false;
5543         UnresolvedModuleRefs.push_back(Unresolved);
5544       }
5545       break;
5546 
5547     case SUBMODULE_EXPORTS:
5548       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5549         UnresolvedModuleRef Unresolved;
5550         Unresolved.File = &F;
5551         Unresolved.Mod = CurrentModule;
5552         Unresolved.ID = Record[Idx];
5553         Unresolved.Kind = UnresolvedModuleRef::Export;
5554         Unresolved.IsWildcard = Record[Idx + 1];
5555         UnresolvedModuleRefs.push_back(Unresolved);
5556       }
5557 
5558       // Once we've loaded the set of exports, there's no reason to keep
5559       // the parsed, unresolved exports around.
5560       CurrentModule->UnresolvedExports.clear();
5561       break;
5562 
5563     case SUBMODULE_REQUIRES:
5564       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5565                                     PP.getTargetInfo());
5566       break;
5567 
5568     case SUBMODULE_LINK_LIBRARY:
5569       ModMap.resolveLinkAsDependencies(CurrentModule);
5570       CurrentModule->LinkLibraries.push_back(
5571                                          Module::LinkLibrary(Blob, Record[0]));
5572       break;
5573 
5574     case SUBMODULE_CONFIG_MACRO:
5575       CurrentModule->ConfigMacros.push_back(Blob.str());
5576       break;
5577 
5578     case SUBMODULE_CONFLICT: {
5579       UnresolvedModuleRef Unresolved;
5580       Unresolved.File = &F;
5581       Unresolved.Mod = CurrentModule;
5582       Unresolved.ID = Record[0];
5583       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5584       Unresolved.IsWildcard = false;
5585       Unresolved.String = Blob;
5586       UnresolvedModuleRefs.push_back(Unresolved);
5587       break;
5588     }
5589 
5590     case SUBMODULE_INITIALIZERS: {
5591       if (!ContextObj)
5592         break;
5593       SmallVector<uint32_t, 16> Inits;
5594       for (auto &ID : Record)
5595         Inits.push_back(getGlobalDeclID(F, ID));
5596       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5597       break;
5598     }
5599 
5600     case SUBMODULE_EXPORT_AS:
5601       CurrentModule->ExportAsModule = Blob.str();
5602       ModMap.addLinkAsDependency(CurrentModule);
5603       break;
5604     }
5605   }
5606 }
5607 
5608 /// Parse the record that corresponds to a LangOptions data
5609 /// structure.
5610 ///
5611 /// This routine parses the language options from the AST file and then gives
5612 /// them to the AST listener if one is set.
5613 ///
5614 /// \returns true if the listener deems the file unacceptable, false otherwise.
5615 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5616                                      bool Complain,
5617                                      ASTReaderListener &Listener,
5618                                      bool AllowCompatibleDifferences) {
5619   LangOptions LangOpts;
5620   unsigned Idx = 0;
5621 #define LANGOPT(Name, Bits, Default, Description) \
5622   LangOpts.Name = Record[Idx++];
5623 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5624   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5625 #include "clang/Basic/LangOptions.def"
5626 #define SANITIZER(NAME, ID)                                                    \
5627   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5628 #include "clang/Basic/Sanitizers.def"
5629 
5630   for (unsigned N = Record[Idx++]; N; --N)
5631     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5632 
5633   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5634   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5635   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5636 
5637   LangOpts.CurrentModule = ReadString(Record, Idx);
5638 
5639   // Comment options.
5640   for (unsigned N = Record[Idx++]; N; --N) {
5641     LangOpts.CommentOpts.BlockCommandNames.push_back(
5642       ReadString(Record, Idx));
5643   }
5644   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5645 
5646   // OpenMP offloading options.
5647   for (unsigned N = Record[Idx++]; N; --N) {
5648     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5649   }
5650 
5651   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5652 
5653   return Listener.ReadLanguageOptions(LangOpts, Complain,
5654                                       AllowCompatibleDifferences);
5655 }
5656 
5657 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5658                                    ASTReaderListener &Listener,
5659                                    bool AllowCompatibleDifferences) {
5660   unsigned Idx = 0;
5661   TargetOptions TargetOpts;
5662   TargetOpts.Triple = ReadString(Record, Idx);
5663   TargetOpts.CPU = ReadString(Record, Idx);
5664   TargetOpts.ABI = ReadString(Record, Idx);
5665   for (unsigned N = Record[Idx++]; N; --N) {
5666     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5667   }
5668   for (unsigned N = Record[Idx++]; N; --N) {
5669     TargetOpts.Features.push_back(ReadString(Record, Idx));
5670   }
5671 
5672   return Listener.ReadTargetOptions(TargetOpts, Complain,
5673                                     AllowCompatibleDifferences);
5674 }
5675 
5676 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5677                                        ASTReaderListener &Listener) {
5678   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5679   unsigned Idx = 0;
5680 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5681 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5682   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5683 #include "clang/Basic/DiagnosticOptions.def"
5684 
5685   for (unsigned N = Record[Idx++]; N; --N)
5686     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5687   for (unsigned N = Record[Idx++]; N; --N)
5688     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5689 
5690   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5691 }
5692 
5693 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5694                                        ASTReaderListener &Listener) {
5695   FileSystemOptions FSOpts;
5696   unsigned Idx = 0;
5697   FSOpts.WorkingDir = ReadString(Record, Idx);
5698   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5699 }
5700 
5701 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5702                                          bool Complain,
5703                                          ASTReaderListener &Listener) {
5704   HeaderSearchOptions HSOpts;
5705   unsigned Idx = 0;
5706   HSOpts.Sysroot = ReadString(Record, Idx);
5707 
5708   // Include entries.
5709   for (unsigned N = Record[Idx++]; N; --N) {
5710     std::string Path = ReadString(Record, Idx);
5711     frontend::IncludeDirGroup Group
5712       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5713     bool IsFramework = Record[Idx++];
5714     bool IgnoreSysRoot = Record[Idx++];
5715     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5716                                     IgnoreSysRoot);
5717   }
5718 
5719   // System header prefixes.
5720   for (unsigned N = Record[Idx++]; N; --N) {
5721     std::string Prefix = ReadString(Record, Idx);
5722     bool IsSystemHeader = Record[Idx++];
5723     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5724   }
5725 
5726   HSOpts.ResourceDir = ReadString(Record, Idx);
5727   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5728   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5729   HSOpts.DisableModuleHash = Record[Idx++];
5730   HSOpts.ImplicitModuleMaps = Record[Idx++];
5731   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5732   HSOpts.UseBuiltinIncludes = Record[Idx++];
5733   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5734   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5735   HSOpts.UseLibcxx = Record[Idx++];
5736   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5737 
5738   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5739                                           Complain);
5740 }
5741 
5742 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5743                                          bool Complain,
5744                                          ASTReaderListener &Listener,
5745                                          std::string &SuggestedPredefines) {
5746   PreprocessorOptions PPOpts;
5747   unsigned Idx = 0;
5748 
5749   // Macro definitions/undefs
5750   for (unsigned N = Record[Idx++]; N; --N) {
5751     std::string Macro = ReadString(Record, Idx);
5752     bool IsUndef = Record[Idx++];
5753     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5754   }
5755 
5756   // Includes
5757   for (unsigned N = Record[Idx++]; N; --N) {
5758     PPOpts.Includes.push_back(ReadString(Record, Idx));
5759   }
5760 
5761   // Macro Includes
5762   for (unsigned N = Record[Idx++]; N; --N) {
5763     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5764   }
5765 
5766   PPOpts.UsePredefines = Record[Idx++];
5767   PPOpts.DetailedRecord = Record[Idx++];
5768   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5769   PPOpts.ObjCXXARCStandardLibrary =
5770     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5771   SuggestedPredefines.clear();
5772   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5773                                           SuggestedPredefines);
5774 }
5775 
5776 std::pair<ModuleFile *, unsigned>
5777 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5778   GlobalPreprocessedEntityMapType::iterator
5779   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5780   assert(I != GlobalPreprocessedEntityMap.end() &&
5781          "Corrupted global preprocessed entity map");
5782   ModuleFile *M = I->second;
5783   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5784   return std::make_pair(M, LocalIndex);
5785 }
5786 
5787 llvm::iterator_range<PreprocessingRecord::iterator>
5788 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5789   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5790     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5791                                              Mod.NumPreprocessedEntities);
5792 
5793   return llvm::make_range(PreprocessingRecord::iterator(),
5794                           PreprocessingRecord::iterator());
5795 }
5796 
5797 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5798 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5799   return llvm::make_range(
5800       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5801       ModuleDeclIterator(this, &Mod,
5802                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5803 }
5804 
5805 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5806   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5807   assert(I != GlobalSkippedRangeMap.end() &&
5808     "Corrupted global skipped range map");
5809   ModuleFile *M = I->second;
5810   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5811   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5812   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5813   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5814                     TranslateSourceLocation(*M, RawRange.getEnd()));
5815   assert(Range.isValid());
5816   return Range;
5817 }
5818 
5819 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5820   PreprocessedEntityID PPID = Index+1;
5821   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5822   ModuleFile &M = *PPInfo.first;
5823   unsigned LocalIndex = PPInfo.second;
5824   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5825 
5826   if (!PP.getPreprocessingRecord()) {
5827     Error("no preprocessing record");
5828     return nullptr;
5829   }
5830 
5831   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5832   if (llvm::Error Err =
5833           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5834     Error(std::move(Err));
5835     return nullptr;
5836   }
5837 
5838   Expected<llvm::BitstreamEntry> MaybeEntry =
5839       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5840   if (!MaybeEntry) {
5841     Error(MaybeEntry.takeError());
5842     return nullptr;
5843   }
5844   llvm::BitstreamEntry Entry = MaybeEntry.get();
5845 
5846   if (Entry.Kind != llvm::BitstreamEntry::Record)
5847     return nullptr;
5848 
5849   // Read the record.
5850   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5851                     TranslateSourceLocation(M, PPOffs.getEnd()));
5852   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5853   StringRef Blob;
5854   RecordData Record;
5855   Expected<unsigned> MaybeRecType =
5856       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5857   if (!MaybeRecType) {
5858     Error(MaybeRecType.takeError());
5859     return nullptr;
5860   }
5861   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5862   case PPD_MACRO_EXPANSION: {
5863     bool isBuiltin = Record[0];
5864     IdentifierInfo *Name = nullptr;
5865     MacroDefinitionRecord *Def = nullptr;
5866     if (isBuiltin)
5867       Name = getLocalIdentifier(M, Record[1]);
5868     else {
5869       PreprocessedEntityID GlobalID =
5870           getGlobalPreprocessedEntityID(M, Record[1]);
5871       Def = cast<MacroDefinitionRecord>(
5872           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5873     }
5874 
5875     MacroExpansion *ME;
5876     if (isBuiltin)
5877       ME = new (PPRec) MacroExpansion(Name, Range);
5878     else
5879       ME = new (PPRec) MacroExpansion(Def, Range);
5880 
5881     return ME;
5882   }
5883 
5884   case PPD_MACRO_DEFINITION: {
5885     // Decode the identifier info and then check again; if the macro is
5886     // still defined and associated with the identifier,
5887     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5888     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5889 
5890     if (DeserializationListener)
5891       DeserializationListener->MacroDefinitionRead(PPID, MD);
5892 
5893     return MD;
5894   }
5895 
5896   case PPD_INCLUSION_DIRECTIVE: {
5897     const char *FullFileNameStart = Blob.data() + Record[0];
5898     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5899     const FileEntry *File = nullptr;
5900     if (!FullFileName.empty())
5901       if (auto FE = PP.getFileManager().getFile(FullFileName))
5902         File = *FE;
5903 
5904     // FIXME: Stable encoding
5905     InclusionDirective::InclusionKind Kind
5906       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5907     InclusionDirective *ID
5908       = new (PPRec) InclusionDirective(PPRec, Kind,
5909                                        StringRef(Blob.data(), Record[0]),
5910                                        Record[1], Record[3],
5911                                        File,
5912                                        Range);
5913     return ID;
5914   }
5915   }
5916 
5917   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5918 }
5919 
5920 /// Find the next module that contains entities and return the ID
5921 /// of the first entry.
5922 ///
5923 /// \param SLocMapI points at a chunk of a module that contains no
5924 /// preprocessed entities or the entities it contains are not the ones we are
5925 /// looking for.
5926 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5927                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5928   ++SLocMapI;
5929   for (GlobalSLocOffsetMapType::const_iterator
5930          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5931     ModuleFile &M = *SLocMapI->second;
5932     if (M.NumPreprocessedEntities)
5933       return M.BasePreprocessedEntityID;
5934   }
5935 
5936   return getTotalNumPreprocessedEntities();
5937 }
5938 
5939 namespace {
5940 
5941 struct PPEntityComp {
5942   const ASTReader &Reader;
5943   ModuleFile &M;
5944 
5945   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5946 
5947   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5948     SourceLocation LHS = getLoc(L);
5949     SourceLocation RHS = getLoc(R);
5950     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5951   }
5952 
5953   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5954     SourceLocation LHS = getLoc(L);
5955     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5956   }
5957 
5958   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5959     SourceLocation RHS = getLoc(R);
5960     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5961   }
5962 
5963   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5964     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5965   }
5966 };
5967 
5968 } // namespace
5969 
5970 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5971                                                        bool EndsAfter) const {
5972   if (SourceMgr.isLocalSourceLocation(Loc))
5973     return getTotalNumPreprocessedEntities();
5974 
5975   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5976       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5977   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5978          "Corrupted global sloc offset map");
5979 
5980   if (SLocMapI->second->NumPreprocessedEntities == 0)
5981     return findNextPreprocessedEntity(SLocMapI);
5982 
5983   ModuleFile &M = *SLocMapI->second;
5984 
5985   using pp_iterator = const PPEntityOffset *;
5986 
5987   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5988   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5989 
5990   size_t Count = M.NumPreprocessedEntities;
5991   size_t Half;
5992   pp_iterator First = pp_begin;
5993   pp_iterator PPI;
5994 
5995   if (EndsAfter) {
5996     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5997                            PPEntityComp(*this, M));
5998   } else {
5999     // Do a binary search manually instead of using std::lower_bound because
6000     // The end locations of entities may be unordered (when a macro expansion
6001     // is inside another macro argument), but for this case it is not important
6002     // whether we get the first macro expansion or its containing macro.
6003     while (Count > 0) {
6004       Half = Count / 2;
6005       PPI = First;
6006       std::advance(PPI, Half);
6007       if (SourceMgr.isBeforeInTranslationUnit(
6008               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6009         First = PPI;
6010         ++First;
6011         Count = Count - Half - 1;
6012       } else
6013         Count = Half;
6014     }
6015   }
6016 
6017   if (PPI == pp_end)
6018     return findNextPreprocessedEntity(SLocMapI);
6019 
6020   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6021 }
6022 
6023 /// Returns a pair of [Begin, End) indices of preallocated
6024 /// preprocessed entities that \arg Range encompasses.
6025 std::pair<unsigned, unsigned>
6026     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6027   if (Range.isInvalid())
6028     return std::make_pair(0,0);
6029   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6030 
6031   PreprocessedEntityID BeginID =
6032       findPreprocessedEntity(Range.getBegin(), false);
6033   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6034   return std::make_pair(BeginID, EndID);
6035 }
6036 
6037 /// Optionally returns true or false if the preallocated preprocessed
6038 /// entity with index \arg Index came from file \arg FID.
6039 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6040                                                              FileID FID) {
6041   if (FID.isInvalid())
6042     return false;
6043 
6044   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6045   ModuleFile &M = *PPInfo.first;
6046   unsigned LocalIndex = PPInfo.second;
6047   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6048 
6049   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6050   if (Loc.isInvalid())
6051     return false;
6052 
6053   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6054     return true;
6055   else
6056     return false;
6057 }
6058 
6059 namespace {
6060 
6061   /// Visitor used to search for information about a header file.
6062   class HeaderFileInfoVisitor {
6063     const FileEntry *FE;
6064     Optional<HeaderFileInfo> HFI;
6065 
6066   public:
6067     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6068 
6069     bool operator()(ModuleFile &M) {
6070       HeaderFileInfoLookupTable *Table
6071         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6072       if (!Table)
6073         return false;
6074 
6075       // Look in the on-disk hash table for an entry for this file name.
6076       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6077       if (Pos == Table->end())
6078         return false;
6079 
6080       HFI = *Pos;
6081       return true;
6082     }
6083 
6084     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6085   };
6086 
6087 } // namespace
6088 
6089 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6090   HeaderFileInfoVisitor Visitor(FE);
6091   ModuleMgr.visit(Visitor);
6092   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6093     return *HFI;
6094 
6095   return HeaderFileInfo();
6096 }
6097 
6098 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6099   using DiagState = DiagnosticsEngine::DiagState;
6100   SmallVector<DiagState *, 32> DiagStates;
6101 
6102   for (ModuleFile &F : ModuleMgr) {
6103     unsigned Idx = 0;
6104     auto &Record = F.PragmaDiagMappings;
6105     if (Record.empty())
6106       continue;
6107 
6108     DiagStates.clear();
6109 
6110     auto ReadDiagState =
6111         [&](const DiagState &BasedOn, SourceLocation Loc,
6112             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6113       unsigned BackrefID = Record[Idx++];
6114       if (BackrefID != 0)
6115         return DiagStates[BackrefID - 1];
6116 
6117       // A new DiagState was created here.
6118       Diag.DiagStates.push_back(BasedOn);
6119       DiagState *NewState = &Diag.DiagStates.back();
6120       DiagStates.push_back(NewState);
6121       unsigned Size = Record[Idx++];
6122       assert(Idx + Size * 2 <= Record.size() &&
6123              "Invalid data, not enough diag/map pairs");
6124       while (Size--) {
6125         unsigned DiagID = Record[Idx++];
6126         DiagnosticMapping NewMapping =
6127             DiagnosticMapping::deserialize(Record[Idx++]);
6128         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6129           continue;
6130 
6131         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6132 
6133         // If this mapping was specified as a warning but the severity was
6134         // upgraded due to diagnostic settings, simulate the current diagnostic
6135         // settings (and use a warning).
6136         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6137           NewMapping.setSeverity(diag::Severity::Warning);
6138           NewMapping.setUpgradedFromWarning(false);
6139         }
6140 
6141         Mapping = NewMapping;
6142       }
6143       return NewState;
6144     };
6145 
6146     // Read the first state.
6147     DiagState *FirstState;
6148     if (F.Kind == MK_ImplicitModule) {
6149       // Implicitly-built modules are reused with different diagnostic
6150       // settings.  Use the initial diagnostic state from Diag to simulate this
6151       // compilation's diagnostic settings.
6152       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6153       DiagStates.push_back(FirstState);
6154 
6155       // Skip the initial diagnostic state from the serialized module.
6156       assert(Record[1] == 0 &&
6157              "Invalid data, unexpected backref in initial state");
6158       Idx = 3 + Record[2] * 2;
6159       assert(Idx < Record.size() &&
6160              "Invalid data, not enough state change pairs in initial state");
6161     } else if (F.isModule()) {
6162       // For an explicit module, preserve the flags from the module build
6163       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6164       // -Wblah flags.
6165       unsigned Flags = Record[Idx++];
6166       DiagState Initial;
6167       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6168       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6169       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6170       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6171       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6172       Initial.ExtBehavior = (diag::Severity)Flags;
6173       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6174 
6175       assert(F.OriginalSourceFileID.isValid());
6176 
6177       // Set up the root buffer of the module to start with the initial
6178       // diagnostic state of the module itself, to cover files that contain no
6179       // explicit transitions (for which we did not serialize anything).
6180       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6181           .StateTransitions.push_back({FirstState, 0});
6182     } else {
6183       // For prefix ASTs, start with whatever the user configured on the
6184       // command line.
6185       Idx++; // Skip flags.
6186       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6187                                  SourceLocation(), false);
6188     }
6189 
6190     // Read the state transitions.
6191     unsigned NumLocations = Record[Idx++];
6192     while (NumLocations--) {
6193       assert(Idx < Record.size() &&
6194              "Invalid data, missing pragma diagnostic states");
6195       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6196       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6197       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6198       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6199       unsigned Transitions = Record[Idx++];
6200 
6201       // Note that we don't need to set up Parent/ParentOffset here, because
6202       // we won't be changing the diagnostic state within imported FileIDs
6203       // (other than perhaps appending to the main source file, which has no
6204       // parent).
6205       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6206       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6207       for (unsigned I = 0; I != Transitions; ++I) {
6208         unsigned Offset = Record[Idx++];
6209         auto *State =
6210             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6211         F.StateTransitions.push_back({State, Offset});
6212       }
6213     }
6214 
6215     // Read the final state.
6216     assert(Idx < Record.size() &&
6217            "Invalid data, missing final pragma diagnostic state");
6218     SourceLocation CurStateLoc =
6219         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6220     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6221 
6222     if (!F.isModule()) {
6223       Diag.DiagStatesByLoc.CurDiagState = CurState;
6224       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6225 
6226       // Preserve the property that the imaginary root file describes the
6227       // current state.
6228       FileID NullFile;
6229       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6230       if (T.empty())
6231         T.push_back({CurState, 0});
6232       else
6233         T[0].State = CurState;
6234     }
6235 
6236     // Don't try to read these mappings again.
6237     Record.clear();
6238   }
6239 }
6240 
6241 /// Get the correct cursor and offset for loading a type.
6242 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6243   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6244   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6245   ModuleFile *M = I->second;
6246   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6247 }
6248 
6249 /// Read and return the type with the given index..
6250 ///
6251 /// The index is the type ID, shifted and minus the number of predefs. This
6252 /// routine actually reads the record corresponding to the type at the given
6253 /// location. It is a helper routine for GetType, which deals with reading type
6254 /// IDs.
6255 QualType ASTReader::readTypeRecord(unsigned Index) {
6256   assert(ContextObj && "reading type with no AST context");
6257   ASTContext &Context = *ContextObj;
6258   RecordLocation Loc = TypeCursorForIndex(Index);
6259   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6260 
6261   // Keep track of where we are in the stream, then jump back there
6262   // after reading this type.
6263   SavedStreamPosition SavedPosition(DeclsCursor);
6264 
6265   ReadingKindTracker ReadingKind(Read_Type, *this);
6266 
6267   // Note that we are loading a type record.
6268   Deserializing AType(this);
6269 
6270   unsigned Idx = 0;
6271   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6272     Error(std::move(Err));
6273     return QualType();
6274   }
6275   RecordData Record;
6276   Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6277   if (!MaybeCode) {
6278     Error(MaybeCode.takeError());
6279     return QualType();
6280   }
6281   unsigned Code = MaybeCode.get();
6282 
6283   Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6284   if (!MaybeTypeCode) {
6285     Error(MaybeTypeCode.takeError());
6286     return QualType();
6287   }
6288   switch ((TypeCode)MaybeTypeCode.get()) {
6289   case TYPE_EXT_QUAL: {
6290     if (Record.size() != 2) {
6291       Error("Incorrect encoding of extended qualifier type");
6292       return QualType();
6293     }
6294     QualType Base = readType(*Loc.F, Record, Idx);
6295     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6296     return Context.getQualifiedType(Base, Quals);
6297   }
6298 
6299   case TYPE_COMPLEX: {
6300     if (Record.size() != 1) {
6301       Error("Incorrect encoding of complex type");
6302       return QualType();
6303     }
6304     QualType ElemType = readType(*Loc.F, Record, Idx);
6305     return Context.getComplexType(ElemType);
6306   }
6307 
6308   case TYPE_POINTER: {
6309     if (Record.size() != 1) {
6310       Error("Incorrect encoding of pointer type");
6311       return QualType();
6312     }
6313     QualType PointeeType = readType(*Loc.F, Record, Idx);
6314     return Context.getPointerType(PointeeType);
6315   }
6316 
6317   case TYPE_DECAYED: {
6318     if (Record.size() != 1) {
6319       Error("Incorrect encoding of decayed type");
6320       return QualType();
6321     }
6322     QualType OriginalType = readType(*Loc.F, Record, Idx);
6323     QualType DT = Context.getAdjustedParameterType(OriginalType);
6324     if (!isa<DecayedType>(DT))
6325       Error("Decayed type does not decay");
6326     return DT;
6327   }
6328 
6329   case TYPE_ADJUSTED: {
6330     if (Record.size() != 2) {
6331       Error("Incorrect encoding of adjusted type");
6332       return QualType();
6333     }
6334     QualType OriginalTy = readType(*Loc.F, Record, Idx);
6335     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6336     return Context.getAdjustedType(OriginalTy, AdjustedTy);
6337   }
6338 
6339   case TYPE_BLOCK_POINTER: {
6340     if (Record.size() != 1) {
6341       Error("Incorrect encoding of block pointer type");
6342       return QualType();
6343     }
6344     QualType PointeeType = readType(*Loc.F, Record, Idx);
6345     return Context.getBlockPointerType(PointeeType);
6346   }
6347 
6348   case TYPE_LVALUE_REFERENCE: {
6349     if (Record.size() != 2) {
6350       Error("Incorrect encoding of lvalue reference type");
6351       return QualType();
6352     }
6353     QualType PointeeType = readType(*Loc.F, Record, Idx);
6354     return Context.getLValueReferenceType(PointeeType, Record[1]);
6355   }
6356 
6357   case TYPE_RVALUE_REFERENCE: {
6358     if (Record.size() != 1) {
6359       Error("Incorrect encoding of rvalue reference type");
6360       return QualType();
6361     }
6362     QualType PointeeType = readType(*Loc.F, Record, Idx);
6363     return Context.getRValueReferenceType(PointeeType);
6364   }
6365 
6366   case TYPE_MEMBER_POINTER: {
6367     if (Record.size() != 2) {
6368       Error("Incorrect encoding of member pointer type");
6369       return QualType();
6370     }
6371     QualType PointeeType = readType(*Loc.F, Record, Idx);
6372     QualType ClassType = readType(*Loc.F, Record, Idx);
6373     if (PointeeType.isNull() || ClassType.isNull())
6374       return QualType();
6375 
6376     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6377   }
6378 
6379   case TYPE_CONSTANT_ARRAY: {
6380     QualType ElementType = readType(*Loc.F, Record, Idx);
6381     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6382     unsigned IndexTypeQuals = Record[2];
6383     unsigned Idx = 3;
6384     llvm::APInt Size = ReadAPInt(Record, Idx);
6385     return Context.getConstantArrayType(ElementType, Size,
6386                                          ASM, IndexTypeQuals);
6387   }
6388 
6389   case TYPE_INCOMPLETE_ARRAY: {
6390     QualType ElementType = readType(*Loc.F, Record, Idx);
6391     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6392     unsigned IndexTypeQuals = Record[2];
6393     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6394   }
6395 
6396   case TYPE_VARIABLE_ARRAY: {
6397     QualType ElementType = readType(*Loc.F, Record, Idx);
6398     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6399     unsigned IndexTypeQuals = Record[2];
6400     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6401     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6402     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6403                                          ASM, IndexTypeQuals,
6404                                          SourceRange(LBLoc, RBLoc));
6405   }
6406 
6407   case TYPE_VECTOR: {
6408     if (Record.size() != 3) {
6409       Error("incorrect encoding of vector type in AST file");
6410       return QualType();
6411     }
6412 
6413     QualType ElementType = readType(*Loc.F, Record, Idx);
6414     unsigned NumElements = Record[1];
6415     unsigned VecKind = Record[2];
6416     return Context.getVectorType(ElementType, NumElements,
6417                                   (VectorType::VectorKind)VecKind);
6418   }
6419 
6420   case TYPE_EXT_VECTOR: {
6421     if (Record.size() != 3) {
6422       Error("incorrect encoding of extended vector type in AST file");
6423       return QualType();
6424     }
6425 
6426     QualType ElementType = readType(*Loc.F, Record, Idx);
6427     unsigned NumElements = Record[1];
6428     return Context.getExtVectorType(ElementType, NumElements);
6429   }
6430 
6431   case TYPE_FUNCTION_NO_PROTO: {
6432     if (Record.size() != 8) {
6433       Error("incorrect encoding of no-proto function type");
6434       return QualType();
6435     }
6436     QualType ResultType = readType(*Loc.F, Record, Idx);
6437     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6438                                (CallingConv)Record[4], Record[5], Record[6],
6439                                Record[7]);
6440     return Context.getFunctionNoProtoType(ResultType, Info);
6441   }
6442 
6443   case TYPE_FUNCTION_PROTO: {
6444     QualType ResultType = readType(*Loc.F, Record, Idx);
6445 
6446     FunctionProtoType::ExtProtoInfo EPI;
6447     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6448                                         /*hasregparm*/ Record[2],
6449                                         /*regparm*/ Record[3],
6450                                         static_cast<CallingConv>(Record[4]),
6451                                         /*produces*/ Record[5],
6452                                         /*nocallersavedregs*/ Record[6],
6453                                         /*nocfcheck*/ Record[7]);
6454 
6455     unsigned Idx = 8;
6456 
6457     EPI.Variadic = Record[Idx++];
6458     EPI.HasTrailingReturn = Record[Idx++];
6459     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6460     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6461     SmallVector<QualType, 8> ExceptionStorage;
6462     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6463 
6464     unsigned NumParams = Record[Idx++];
6465     SmallVector<QualType, 16> ParamTypes;
6466     for (unsigned I = 0; I != NumParams; ++I)
6467       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6468 
6469     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6470     if (Idx != Record.size()) {
6471       for (unsigned I = 0; I != NumParams; ++I)
6472         ExtParameterInfos.push_back(
6473           FunctionProtoType::ExtParameterInfo
6474                            ::getFromOpaqueValue(Record[Idx++]));
6475       EPI.ExtParameterInfos = ExtParameterInfos.data();
6476     }
6477 
6478     assert(Idx == Record.size());
6479 
6480     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6481   }
6482 
6483   case TYPE_UNRESOLVED_USING: {
6484     unsigned Idx = 0;
6485     return Context.getTypeDeclType(
6486                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6487   }
6488 
6489   case TYPE_TYPEDEF: {
6490     if (Record.size() != 2) {
6491       Error("incorrect encoding of typedef type");
6492       return QualType();
6493     }
6494     unsigned Idx = 0;
6495     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6496     QualType Canonical = readType(*Loc.F, Record, Idx);
6497     if (!Canonical.isNull())
6498       Canonical = Context.getCanonicalType(Canonical);
6499     return Context.getTypedefType(Decl, Canonical);
6500   }
6501 
6502   case TYPE_TYPEOF_EXPR:
6503     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6504 
6505   case TYPE_TYPEOF: {
6506     if (Record.size() != 1) {
6507       Error("incorrect encoding of typeof(type) in AST file");
6508       return QualType();
6509     }
6510     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6511     return Context.getTypeOfType(UnderlyingType);
6512   }
6513 
6514   case TYPE_DECLTYPE: {
6515     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6516     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6517   }
6518 
6519   case TYPE_UNARY_TRANSFORM: {
6520     QualType BaseType = readType(*Loc.F, Record, Idx);
6521     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6522     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6523     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6524   }
6525 
6526   case TYPE_AUTO: {
6527     QualType Deduced = readType(*Loc.F, Record, Idx);
6528     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6529     bool IsDependent = false, IsPack = false;
6530     if (Deduced.isNull()) {
6531       IsDependent = Record[Idx] > 0;
6532       IsPack = Record[Idx] > 1;
6533       ++Idx;
6534     }
6535     return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6536   }
6537 
6538   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6539     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6540     QualType Deduced = readType(*Loc.F, Record, Idx);
6541     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6542     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6543                                                         IsDependent);
6544   }
6545 
6546   case TYPE_RECORD: {
6547     if (Record.size() != 2) {
6548       Error("incorrect encoding of record type");
6549       return QualType();
6550     }
6551     unsigned Idx = 0;
6552     bool IsDependent = Record[Idx++];
6553     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6554     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6555     QualType T = Context.getRecordType(RD);
6556     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6557     return T;
6558   }
6559 
6560   case TYPE_ENUM: {
6561     if (Record.size() != 2) {
6562       Error("incorrect encoding of enum type");
6563       return QualType();
6564     }
6565     unsigned Idx = 0;
6566     bool IsDependent = Record[Idx++];
6567     QualType T
6568       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6569     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6570     return T;
6571   }
6572 
6573   case TYPE_ATTRIBUTED: {
6574     if (Record.size() != 3) {
6575       Error("incorrect encoding of attributed type");
6576       return QualType();
6577     }
6578     QualType modifiedType = readType(*Loc.F, Record, Idx);
6579     QualType equivalentType = readType(*Loc.F, Record, Idx);
6580     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6581     return Context.getAttributedType(kind, modifiedType, equivalentType);
6582   }
6583 
6584   case TYPE_PAREN: {
6585     if (Record.size() != 1) {
6586       Error("incorrect encoding of paren type");
6587       return QualType();
6588     }
6589     QualType InnerType = readType(*Loc.F, Record, Idx);
6590     return Context.getParenType(InnerType);
6591   }
6592 
6593   case TYPE_MACRO_QUALIFIED: {
6594     if (Record.size() != 2) {
6595       Error("incorrect encoding of macro defined type");
6596       return QualType();
6597     }
6598     QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6599     IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6600     return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6601   }
6602 
6603   case TYPE_PACK_EXPANSION: {
6604     if (Record.size() != 2) {
6605       Error("incorrect encoding of pack expansion type");
6606       return QualType();
6607     }
6608     QualType Pattern = readType(*Loc.F, Record, Idx);
6609     if (Pattern.isNull())
6610       return QualType();
6611     Optional<unsigned> NumExpansions;
6612     if (Record[1])
6613       NumExpansions = Record[1] - 1;
6614     return Context.getPackExpansionType(Pattern, NumExpansions);
6615   }
6616 
6617   case TYPE_ELABORATED: {
6618     unsigned Idx = 0;
6619     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6620     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6621     QualType NamedType = readType(*Loc.F, Record, Idx);
6622     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6623     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6624   }
6625 
6626   case TYPE_OBJC_INTERFACE: {
6627     unsigned Idx = 0;
6628     ObjCInterfaceDecl *ItfD
6629       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6630     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6631   }
6632 
6633   case TYPE_OBJC_TYPE_PARAM: {
6634     unsigned Idx = 0;
6635     ObjCTypeParamDecl *Decl
6636       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6637     unsigned NumProtos = Record[Idx++];
6638     SmallVector<ObjCProtocolDecl*, 4> Protos;
6639     for (unsigned I = 0; I != NumProtos; ++I)
6640       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6641     return Context.getObjCTypeParamType(Decl, Protos);
6642   }
6643 
6644   case TYPE_OBJC_OBJECT: {
6645     unsigned Idx = 0;
6646     QualType Base = readType(*Loc.F, Record, Idx);
6647     unsigned NumTypeArgs = Record[Idx++];
6648     SmallVector<QualType, 4> TypeArgs;
6649     for (unsigned I = 0; I != NumTypeArgs; ++I)
6650       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6651     unsigned NumProtos = Record[Idx++];
6652     SmallVector<ObjCProtocolDecl*, 4> Protos;
6653     for (unsigned I = 0; I != NumProtos; ++I)
6654       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6655     bool IsKindOf = Record[Idx++];
6656     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6657   }
6658 
6659   case TYPE_OBJC_OBJECT_POINTER: {
6660     unsigned Idx = 0;
6661     QualType Pointee = readType(*Loc.F, Record, Idx);
6662     return Context.getObjCObjectPointerType(Pointee);
6663   }
6664 
6665   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6666     unsigned Idx = 0;
6667     QualType Parm = readType(*Loc.F, Record, Idx);
6668     QualType Replacement = readType(*Loc.F, Record, Idx);
6669     return Context.getSubstTemplateTypeParmType(
6670         cast<TemplateTypeParmType>(Parm),
6671         Context.getCanonicalType(Replacement));
6672   }
6673 
6674   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6675     unsigned Idx = 0;
6676     QualType Parm = readType(*Loc.F, Record, Idx);
6677     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6678     return Context.getSubstTemplateTypeParmPackType(
6679                                                cast<TemplateTypeParmType>(Parm),
6680                                                      ArgPack);
6681   }
6682 
6683   case TYPE_INJECTED_CLASS_NAME: {
6684     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6685     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6686     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6687     // for AST reading, too much interdependencies.
6688     const Type *T = nullptr;
6689     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6690       if (const Type *Existing = DI->getTypeForDecl()) {
6691         T = Existing;
6692         break;
6693       }
6694     }
6695     if (!T) {
6696       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6697       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6698         DI->setTypeForDecl(T);
6699     }
6700     return QualType(T, 0);
6701   }
6702 
6703   case TYPE_TEMPLATE_TYPE_PARM: {
6704     unsigned Idx = 0;
6705     unsigned Depth = Record[Idx++];
6706     unsigned Index = Record[Idx++];
6707     bool Pack = Record[Idx++];
6708     TemplateTypeParmDecl *D
6709       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6710     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6711   }
6712 
6713   case TYPE_DEPENDENT_NAME: {
6714     unsigned Idx = 0;
6715     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6716     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6717     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6718     QualType Canon = readType(*Loc.F, Record, Idx);
6719     if (!Canon.isNull())
6720       Canon = Context.getCanonicalType(Canon);
6721     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6722   }
6723 
6724   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6725     unsigned Idx = 0;
6726     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6727     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6728     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6729     unsigned NumArgs = Record[Idx++];
6730     SmallVector<TemplateArgument, 8> Args;
6731     Args.reserve(NumArgs);
6732     while (NumArgs--)
6733       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6734     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6735                                                           Args);
6736   }
6737 
6738   case TYPE_DEPENDENT_SIZED_ARRAY: {
6739     unsigned Idx = 0;
6740 
6741     // ArrayType
6742     QualType ElementType = readType(*Loc.F, Record, Idx);
6743     ArrayType::ArraySizeModifier ASM
6744       = (ArrayType::ArraySizeModifier)Record[Idx++];
6745     unsigned IndexTypeQuals = Record[Idx++];
6746 
6747     // DependentSizedArrayType
6748     Expr *NumElts = ReadExpr(*Loc.F);
6749     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6750 
6751     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6752                                                IndexTypeQuals, Brackets);
6753   }
6754 
6755   case TYPE_TEMPLATE_SPECIALIZATION: {
6756     unsigned Idx = 0;
6757     bool IsDependent = Record[Idx++];
6758     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6759     SmallVector<TemplateArgument, 8> Args;
6760     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6761     QualType Underlying = readType(*Loc.F, Record, Idx);
6762     QualType T;
6763     if (Underlying.isNull())
6764       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6765     else
6766       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6767     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6768     return T;
6769   }
6770 
6771   case TYPE_ATOMIC: {
6772     if (Record.size() != 1) {
6773       Error("Incorrect encoding of atomic type");
6774       return QualType();
6775     }
6776     QualType ValueType = readType(*Loc.F, Record, Idx);
6777     return Context.getAtomicType(ValueType);
6778   }
6779 
6780   case TYPE_PIPE: {
6781     if (Record.size() != 2) {
6782       Error("Incorrect encoding of pipe type");
6783       return QualType();
6784     }
6785 
6786     // Reading the pipe element type.
6787     QualType ElementType = readType(*Loc.F, Record, Idx);
6788     unsigned ReadOnly = Record[1];
6789     return Context.getPipeType(ElementType, ReadOnly);
6790   }
6791 
6792   case TYPE_DEPENDENT_SIZED_VECTOR: {
6793     unsigned Idx = 0;
6794     QualType ElementType = readType(*Loc.F, Record, Idx);
6795     Expr *SizeExpr = ReadExpr(*Loc.F);
6796     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6797     unsigned VecKind = Record[Idx];
6798 
6799     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6800                                                (VectorType::VectorKind)VecKind);
6801   }
6802 
6803   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6804     unsigned Idx = 0;
6805 
6806     // DependentSizedExtVectorType
6807     QualType ElementType = readType(*Loc.F, Record, Idx);
6808     Expr *SizeExpr = ReadExpr(*Loc.F);
6809     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6810 
6811     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6812                                                   AttrLoc);
6813   }
6814 
6815   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6816     unsigned Idx = 0;
6817 
6818     // DependentAddressSpaceType
6819     QualType PointeeType = readType(*Loc.F, Record, Idx);
6820     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6821     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6822 
6823     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6824                                                    AttrLoc);
6825   }
6826   }
6827   llvm_unreachable("Invalid TypeCode!");
6828 }
6829 
6830 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6831                                   SmallVectorImpl<QualType> &Exceptions,
6832                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6833                                   const RecordData &Record, unsigned &Idx) {
6834   ExceptionSpecificationType EST =
6835       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6836   ESI.Type = EST;
6837   if (EST == EST_Dynamic) {
6838     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6839       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6840     ESI.Exceptions = Exceptions;
6841   } else if (isComputedNoexcept(EST)) {
6842     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6843   } else if (EST == EST_Uninstantiated) {
6844     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6845     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6846   } else if (EST == EST_Unevaluated) {
6847     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6848   }
6849 }
6850 
6851 namespace clang {
6852 
6853 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6854   ModuleFile *F;
6855   ASTReader *Reader;
6856   const ASTReader::RecordData &Record;
6857   unsigned &Idx;
6858 
6859   SourceLocation ReadSourceLocation() {
6860     return Reader->ReadSourceLocation(*F, Record, Idx);
6861   }
6862 
6863   TypeSourceInfo *GetTypeSourceInfo() {
6864     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6865   }
6866 
6867   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6868     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6869   }
6870 
6871   Attr *ReadAttr() {
6872     return Reader->ReadAttr(*F, Record, Idx);
6873   }
6874 
6875 public:
6876   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6877                 const ASTReader::RecordData &Record, unsigned &Idx)
6878       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6879 
6880   // We want compile-time assurance that we've enumerated all of
6881   // these, so unfortunately we have to declare them first, then
6882   // define them out-of-line.
6883 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6884 #define TYPELOC(CLASS, PARENT) \
6885   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6886 #include "clang/AST/TypeLocNodes.def"
6887 
6888   void VisitFunctionTypeLoc(FunctionTypeLoc);
6889   void VisitArrayTypeLoc(ArrayTypeLoc);
6890 };
6891 
6892 } // namespace clang
6893 
6894 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6895   // nothing to do
6896 }
6897 
6898 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6899   TL.setBuiltinLoc(ReadSourceLocation());
6900   if (TL.needsExtraLocalData()) {
6901     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6902     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6903     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6904     TL.setModeAttr(Record[Idx++]);
6905   }
6906 }
6907 
6908 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6909   TL.setNameLoc(ReadSourceLocation());
6910 }
6911 
6912 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6913   TL.setStarLoc(ReadSourceLocation());
6914 }
6915 
6916 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6917   // nothing to do
6918 }
6919 
6920 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6921   // nothing to do
6922 }
6923 
6924 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6925   TL.setExpansionLoc(ReadSourceLocation());
6926 }
6927 
6928 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6929   TL.setCaretLoc(ReadSourceLocation());
6930 }
6931 
6932 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6933   TL.setAmpLoc(ReadSourceLocation());
6934 }
6935 
6936 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6937   TL.setAmpAmpLoc(ReadSourceLocation());
6938 }
6939 
6940 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6941   TL.setStarLoc(ReadSourceLocation());
6942   TL.setClassTInfo(GetTypeSourceInfo());
6943 }
6944 
6945 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6946   TL.setLBracketLoc(ReadSourceLocation());
6947   TL.setRBracketLoc(ReadSourceLocation());
6948   if (Record[Idx++])
6949     TL.setSizeExpr(Reader->ReadExpr(*F));
6950   else
6951     TL.setSizeExpr(nullptr);
6952 }
6953 
6954 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6955   VisitArrayTypeLoc(TL);
6956 }
6957 
6958 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6959   VisitArrayTypeLoc(TL);
6960 }
6961 
6962 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6963   VisitArrayTypeLoc(TL);
6964 }
6965 
6966 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6967                                             DependentSizedArrayTypeLoc TL) {
6968   VisitArrayTypeLoc(TL);
6969 }
6970 
6971 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6972     DependentAddressSpaceTypeLoc TL) {
6973 
6974     TL.setAttrNameLoc(ReadSourceLocation());
6975     SourceRange range;
6976     range.setBegin(ReadSourceLocation());
6977     range.setEnd(ReadSourceLocation());
6978     TL.setAttrOperandParensRange(range);
6979     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6980 }
6981 
6982 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6983                                         DependentSizedExtVectorTypeLoc TL) {
6984   TL.setNameLoc(ReadSourceLocation());
6985 }
6986 
6987 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6988   TL.setNameLoc(ReadSourceLocation());
6989 }
6990 
6991 void TypeLocReader::VisitDependentVectorTypeLoc(
6992     DependentVectorTypeLoc TL) {
6993   TL.setNameLoc(ReadSourceLocation());
6994 }
6995 
6996 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6997   TL.setNameLoc(ReadSourceLocation());
6998 }
6999 
7000 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7001   TL.setLocalRangeBegin(ReadSourceLocation());
7002   TL.setLParenLoc(ReadSourceLocation());
7003   TL.setRParenLoc(ReadSourceLocation());
7004   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
7005                                        Reader->ReadSourceLocation(*F, Record, Idx)));
7006   TL.setLocalRangeEnd(ReadSourceLocation());
7007   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7008     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7009   }
7010 }
7011 
7012 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7013   VisitFunctionTypeLoc(TL);
7014 }
7015 
7016 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7017   VisitFunctionTypeLoc(TL);
7018 }
7019 
7020 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7021   TL.setNameLoc(ReadSourceLocation());
7022 }
7023 
7024 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7025   TL.setNameLoc(ReadSourceLocation());
7026 }
7027 
7028 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7029   TL.setTypeofLoc(ReadSourceLocation());
7030   TL.setLParenLoc(ReadSourceLocation());
7031   TL.setRParenLoc(ReadSourceLocation());
7032 }
7033 
7034 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7035   TL.setTypeofLoc(ReadSourceLocation());
7036   TL.setLParenLoc(ReadSourceLocation());
7037   TL.setRParenLoc(ReadSourceLocation());
7038   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7039 }
7040 
7041 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7042   TL.setNameLoc(ReadSourceLocation());
7043 }
7044 
7045 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7046   TL.setKWLoc(ReadSourceLocation());
7047   TL.setLParenLoc(ReadSourceLocation());
7048   TL.setRParenLoc(ReadSourceLocation());
7049   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7050 }
7051 
7052 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7053   TL.setNameLoc(ReadSourceLocation());
7054 }
7055 
7056 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7057     DeducedTemplateSpecializationTypeLoc TL) {
7058   TL.setTemplateNameLoc(ReadSourceLocation());
7059 }
7060 
7061 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7062   TL.setNameLoc(ReadSourceLocation());
7063 }
7064 
7065 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7066   TL.setNameLoc(ReadSourceLocation());
7067 }
7068 
7069 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7070   TL.setAttr(ReadAttr());
7071 }
7072 
7073 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7074   TL.setNameLoc(ReadSourceLocation());
7075 }
7076 
7077 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7078                                             SubstTemplateTypeParmTypeLoc TL) {
7079   TL.setNameLoc(ReadSourceLocation());
7080 }
7081 
7082 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7083                                           SubstTemplateTypeParmPackTypeLoc TL) {
7084   TL.setNameLoc(ReadSourceLocation());
7085 }
7086 
7087 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7088                                            TemplateSpecializationTypeLoc TL) {
7089   TL.setTemplateKeywordLoc(ReadSourceLocation());
7090   TL.setTemplateNameLoc(ReadSourceLocation());
7091   TL.setLAngleLoc(ReadSourceLocation());
7092   TL.setRAngleLoc(ReadSourceLocation());
7093   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7094     TL.setArgLocInfo(
7095         i,
7096         Reader->GetTemplateArgumentLocInfo(
7097             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7098 }
7099 
7100 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7101   TL.setLParenLoc(ReadSourceLocation());
7102   TL.setRParenLoc(ReadSourceLocation());
7103 }
7104 
7105 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7106   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7107   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7108 }
7109 
7110 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7111   TL.setNameLoc(ReadSourceLocation());
7112 }
7113 
7114 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7115   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7116   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7117   TL.setNameLoc(ReadSourceLocation());
7118 }
7119 
7120 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7121        DependentTemplateSpecializationTypeLoc TL) {
7122   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7123   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7124   TL.setTemplateKeywordLoc(ReadSourceLocation());
7125   TL.setTemplateNameLoc(ReadSourceLocation());
7126   TL.setLAngleLoc(ReadSourceLocation());
7127   TL.setRAngleLoc(ReadSourceLocation());
7128   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7129     TL.setArgLocInfo(
7130         I,
7131         Reader->GetTemplateArgumentLocInfo(
7132             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7133 }
7134 
7135 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7136   TL.setEllipsisLoc(ReadSourceLocation());
7137 }
7138 
7139 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7140   TL.setNameLoc(ReadSourceLocation());
7141 }
7142 
7143 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7144   if (TL.getNumProtocols()) {
7145     TL.setProtocolLAngleLoc(ReadSourceLocation());
7146     TL.setProtocolRAngleLoc(ReadSourceLocation());
7147   }
7148   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7149     TL.setProtocolLoc(i, ReadSourceLocation());
7150 }
7151 
7152 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7153   TL.setHasBaseTypeAsWritten(Record[Idx++]);
7154   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7155   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7156   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7157     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7158   TL.setProtocolLAngleLoc(ReadSourceLocation());
7159   TL.setProtocolRAngleLoc(ReadSourceLocation());
7160   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7161     TL.setProtocolLoc(i, ReadSourceLocation());
7162 }
7163 
7164 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7165   TL.setStarLoc(ReadSourceLocation());
7166 }
7167 
7168 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7169   TL.setKWLoc(ReadSourceLocation());
7170   TL.setLParenLoc(ReadSourceLocation());
7171   TL.setRParenLoc(ReadSourceLocation());
7172 }
7173 
7174 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7175   TL.setKWLoc(ReadSourceLocation());
7176 }
7177 
7178 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7179                             unsigned &Idx, TypeLoc TL) {
7180   TypeLocReader TLR(F, *this, Record, Idx);
7181   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7182     TLR.Visit(TL);
7183 }
7184 
7185 TypeSourceInfo *
7186 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7187                              unsigned &Idx) {
7188   QualType InfoTy = readType(F, Record, Idx);
7189   if (InfoTy.isNull())
7190     return nullptr;
7191 
7192   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7193   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7194   return TInfo;
7195 }
7196 
7197 QualType ASTReader::GetType(TypeID ID) {
7198   assert(ContextObj && "reading type with no AST context");
7199   ASTContext &Context = *ContextObj;
7200 
7201   unsigned FastQuals = ID & Qualifiers::FastMask;
7202   unsigned Index = ID >> Qualifiers::FastWidth;
7203 
7204   if (Index < NUM_PREDEF_TYPE_IDS) {
7205     QualType T;
7206     switch ((PredefinedTypeIDs)Index) {
7207     case PREDEF_TYPE_NULL_ID:
7208       return QualType();
7209     case PREDEF_TYPE_VOID_ID:
7210       T = Context.VoidTy;
7211       break;
7212     case PREDEF_TYPE_BOOL_ID:
7213       T = Context.BoolTy;
7214       break;
7215     case PREDEF_TYPE_CHAR_U_ID:
7216     case PREDEF_TYPE_CHAR_S_ID:
7217       // FIXME: Check that the signedness of CharTy is correct!
7218       T = Context.CharTy;
7219       break;
7220     case PREDEF_TYPE_UCHAR_ID:
7221       T = Context.UnsignedCharTy;
7222       break;
7223     case PREDEF_TYPE_USHORT_ID:
7224       T = Context.UnsignedShortTy;
7225       break;
7226     case PREDEF_TYPE_UINT_ID:
7227       T = Context.UnsignedIntTy;
7228       break;
7229     case PREDEF_TYPE_ULONG_ID:
7230       T = Context.UnsignedLongTy;
7231       break;
7232     case PREDEF_TYPE_ULONGLONG_ID:
7233       T = Context.UnsignedLongLongTy;
7234       break;
7235     case PREDEF_TYPE_UINT128_ID:
7236       T = Context.UnsignedInt128Ty;
7237       break;
7238     case PREDEF_TYPE_SCHAR_ID:
7239       T = Context.SignedCharTy;
7240       break;
7241     case PREDEF_TYPE_WCHAR_ID:
7242       T = Context.WCharTy;
7243       break;
7244     case PREDEF_TYPE_SHORT_ID:
7245       T = Context.ShortTy;
7246       break;
7247     case PREDEF_TYPE_INT_ID:
7248       T = Context.IntTy;
7249       break;
7250     case PREDEF_TYPE_LONG_ID:
7251       T = Context.LongTy;
7252       break;
7253     case PREDEF_TYPE_LONGLONG_ID:
7254       T = Context.LongLongTy;
7255       break;
7256     case PREDEF_TYPE_INT128_ID:
7257       T = Context.Int128Ty;
7258       break;
7259     case PREDEF_TYPE_HALF_ID:
7260       T = Context.HalfTy;
7261       break;
7262     case PREDEF_TYPE_FLOAT_ID:
7263       T = Context.FloatTy;
7264       break;
7265     case PREDEF_TYPE_DOUBLE_ID:
7266       T = Context.DoubleTy;
7267       break;
7268     case PREDEF_TYPE_LONGDOUBLE_ID:
7269       T = Context.LongDoubleTy;
7270       break;
7271     case PREDEF_TYPE_SHORT_ACCUM_ID:
7272       T = Context.ShortAccumTy;
7273       break;
7274     case PREDEF_TYPE_ACCUM_ID:
7275       T = Context.AccumTy;
7276       break;
7277     case PREDEF_TYPE_LONG_ACCUM_ID:
7278       T = Context.LongAccumTy;
7279       break;
7280     case PREDEF_TYPE_USHORT_ACCUM_ID:
7281       T = Context.UnsignedShortAccumTy;
7282       break;
7283     case PREDEF_TYPE_UACCUM_ID:
7284       T = Context.UnsignedAccumTy;
7285       break;
7286     case PREDEF_TYPE_ULONG_ACCUM_ID:
7287       T = Context.UnsignedLongAccumTy;
7288       break;
7289     case PREDEF_TYPE_SHORT_FRACT_ID:
7290       T = Context.ShortFractTy;
7291       break;
7292     case PREDEF_TYPE_FRACT_ID:
7293       T = Context.FractTy;
7294       break;
7295     case PREDEF_TYPE_LONG_FRACT_ID:
7296       T = Context.LongFractTy;
7297       break;
7298     case PREDEF_TYPE_USHORT_FRACT_ID:
7299       T = Context.UnsignedShortFractTy;
7300       break;
7301     case PREDEF_TYPE_UFRACT_ID:
7302       T = Context.UnsignedFractTy;
7303       break;
7304     case PREDEF_TYPE_ULONG_FRACT_ID:
7305       T = Context.UnsignedLongFractTy;
7306       break;
7307     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7308       T = Context.SatShortAccumTy;
7309       break;
7310     case PREDEF_TYPE_SAT_ACCUM_ID:
7311       T = Context.SatAccumTy;
7312       break;
7313     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7314       T = Context.SatLongAccumTy;
7315       break;
7316     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7317       T = Context.SatUnsignedShortAccumTy;
7318       break;
7319     case PREDEF_TYPE_SAT_UACCUM_ID:
7320       T = Context.SatUnsignedAccumTy;
7321       break;
7322     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7323       T = Context.SatUnsignedLongAccumTy;
7324       break;
7325     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7326       T = Context.SatShortFractTy;
7327       break;
7328     case PREDEF_TYPE_SAT_FRACT_ID:
7329       T = Context.SatFractTy;
7330       break;
7331     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7332       T = Context.SatLongFractTy;
7333       break;
7334     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7335       T = Context.SatUnsignedShortFractTy;
7336       break;
7337     case PREDEF_TYPE_SAT_UFRACT_ID:
7338       T = Context.SatUnsignedFractTy;
7339       break;
7340     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7341       T = Context.SatUnsignedLongFractTy;
7342       break;
7343     case PREDEF_TYPE_FLOAT16_ID:
7344       T = Context.Float16Ty;
7345       break;
7346     case PREDEF_TYPE_FLOAT128_ID:
7347       T = Context.Float128Ty;
7348       break;
7349     case PREDEF_TYPE_OVERLOAD_ID:
7350       T = Context.OverloadTy;
7351       break;
7352     case PREDEF_TYPE_BOUND_MEMBER:
7353       T = Context.BoundMemberTy;
7354       break;
7355     case PREDEF_TYPE_PSEUDO_OBJECT:
7356       T = Context.PseudoObjectTy;
7357       break;
7358     case PREDEF_TYPE_DEPENDENT_ID:
7359       T = Context.DependentTy;
7360       break;
7361     case PREDEF_TYPE_UNKNOWN_ANY:
7362       T = Context.UnknownAnyTy;
7363       break;
7364     case PREDEF_TYPE_NULLPTR_ID:
7365       T = Context.NullPtrTy;
7366       break;
7367     case PREDEF_TYPE_CHAR8_ID:
7368       T = Context.Char8Ty;
7369       break;
7370     case PREDEF_TYPE_CHAR16_ID:
7371       T = Context.Char16Ty;
7372       break;
7373     case PREDEF_TYPE_CHAR32_ID:
7374       T = Context.Char32Ty;
7375       break;
7376     case PREDEF_TYPE_OBJC_ID:
7377       T = Context.ObjCBuiltinIdTy;
7378       break;
7379     case PREDEF_TYPE_OBJC_CLASS:
7380       T = Context.ObjCBuiltinClassTy;
7381       break;
7382     case PREDEF_TYPE_OBJC_SEL:
7383       T = Context.ObjCBuiltinSelTy;
7384       break;
7385 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7386     case PREDEF_TYPE_##Id##_ID: \
7387       T = Context.SingletonId; \
7388       break;
7389 #include "clang/Basic/OpenCLImageTypes.def"
7390 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7391     case PREDEF_TYPE_##Id##_ID: \
7392       T = Context.Id##Ty; \
7393       break;
7394 #include "clang/Basic/OpenCLExtensionTypes.def"
7395     case PREDEF_TYPE_SAMPLER_ID:
7396       T = Context.OCLSamplerTy;
7397       break;
7398     case PREDEF_TYPE_EVENT_ID:
7399       T = Context.OCLEventTy;
7400       break;
7401     case PREDEF_TYPE_CLK_EVENT_ID:
7402       T = Context.OCLClkEventTy;
7403       break;
7404     case PREDEF_TYPE_QUEUE_ID:
7405       T = Context.OCLQueueTy;
7406       break;
7407     case PREDEF_TYPE_RESERVE_ID_ID:
7408       T = Context.OCLReserveIDTy;
7409       break;
7410     case PREDEF_TYPE_AUTO_DEDUCT:
7411       T = Context.getAutoDeductType();
7412       break;
7413     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7414       T = Context.getAutoRRefDeductType();
7415       break;
7416     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7417       T = Context.ARCUnbridgedCastTy;
7418       break;
7419     case PREDEF_TYPE_BUILTIN_FN:
7420       T = Context.BuiltinFnTy;
7421       break;
7422     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7423       T = Context.OMPArraySectionTy;
7424       break;
7425 #define SVE_TYPE(Name, Id, SingletonId) \
7426     case PREDEF_TYPE_##Id##_ID: \
7427       T = Context.SingletonId; \
7428       break;
7429 #include "clang/Basic/AArch64SVEACLETypes.def"
7430     }
7431 
7432     assert(!T.isNull() && "Unknown predefined type");
7433     return T.withFastQualifiers(FastQuals);
7434   }
7435 
7436   Index -= NUM_PREDEF_TYPE_IDS;
7437   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7438   if (TypesLoaded[Index].isNull()) {
7439     TypesLoaded[Index] = readTypeRecord(Index);
7440     if (TypesLoaded[Index].isNull())
7441       return QualType();
7442 
7443     TypesLoaded[Index]->setFromAST();
7444     if (DeserializationListener)
7445       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7446                                         TypesLoaded[Index]);
7447   }
7448 
7449   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7450 }
7451 
7452 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7453   return GetType(getGlobalTypeID(F, LocalID));
7454 }
7455 
7456 serialization::TypeID
7457 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7458   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7459   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7460 
7461   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7462     return LocalID;
7463 
7464   if (!F.ModuleOffsetMap.empty())
7465     ReadModuleOffsetMap(F);
7466 
7467   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7468     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7469   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7470 
7471   unsigned GlobalIndex = LocalIndex + I->second;
7472   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7473 }
7474 
7475 TemplateArgumentLocInfo
7476 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7477                                       TemplateArgument::ArgKind Kind,
7478                                       const RecordData &Record,
7479                                       unsigned &Index) {
7480   switch (Kind) {
7481   case TemplateArgument::Expression:
7482     return ReadExpr(F);
7483   case TemplateArgument::Type:
7484     return GetTypeSourceInfo(F, Record, Index);
7485   case TemplateArgument::Template: {
7486     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7487                                                                      Index);
7488     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7489     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7490                                    SourceLocation());
7491   }
7492   case TemplateArgument::TemplateExpansion: {
7493     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7494                                                                      Index);
7495     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7496     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7497     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7498                                    EllipsisLoc);
7499   }
7500   case TemplateArgument::Null:
7501   case TemplateArgument::Integral:
7502   case TemplateArgument::Declaration:
7503   case TemplateArgument::NullPtr:
7504   case TemplateArgument::Pack:
7505     // FIXME: Is this right?
7506     return TemplateArgumentLocInfo();
7507   }
7508   llvm_unreachable("unexpected template argument loc");
7509 }
7510 
7511 TemplateArgumentLoc
7512 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7513                                    const RecordData &Record, unsigned &Index) {
7514   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7515 
7516   if (Arg.getKind() == TemplateArgument::Expression) {
7517     if (Record[Index++]) // bool InfoHasSameExpr.
7518       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7519   }
7520   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7521                                                              Record, Index));
7522 }
7523 
7524 const ASTTemplateArgumentListInfo*
7525 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7526                                            const RecordData &Record,
7527                                            unsigned &Index) {
7528   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7529   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7530   unsigned NumArgsAsWritten = Record[Index++];
7531   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7532   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7533     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7534   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7535 }
7536 
7537 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7538   return GetDecl(ID);
7539 }
7540 
7541 void ASTReader::CompleteRedeclChain(const Decl *D) {
7542   if (NumCurrentElementsDeserializing) {
7543     // We arrange to not care about the complete redeclaration chain while we're
7544     // deserializing. Just remember that the AST has marked this one as complete
7545     // but that it's not actually complete yet, so we know we still need to
7546     // complete it later.
7547     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7548     return;
7549   }
7550 
7551   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7552 
7553   // If this is a named declaration, complete it by looking it up
7554   // within its context.
7555   //
7556   // FIXME: Merging a function definition should merge
7557   // all mergeable entities within it.
7558   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7559       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7560     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7561       if (!getContext().getLangOpts().CPlusPlus &&
7562           isa<TranslationUnitDecl>(DC)) {
7563         // Outside of C++, we don't have a lookup table for the TU, so update
7564         // the identifier instead. (For C++ modules, we don't store decls
7565         // in the serialized identifier table, so we do the lookup in the TU.)
7566         auto *II = Name.getAsIdentifierInfo();
7567         assert(II && "non-identifier name in C?");
7568         if (II->isOutOfDate())
7569           updateOutOfDateIdentifier(*II);
7570       } else
7571         DC->lookup(Name);
7572     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7573       // Find all declarations of this kind from the relevant context.
7574       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7575         auto *DC = cast<DeclContext>(DCDecl);
7576         SmallVector<Decl*, 8> Decls;
7577         FindExternalLexicalDecls(
7578             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7579       }
7580     }
7581   }
7582 
7583   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7584     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7585   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7586     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7587   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7588     if (auto *Template = FD->getPrimaryTemplate())
7589       Template->LoadLazySpecializations();
7590   }
7591 }
7592 
7593 CXXCtorInitializer **
7594 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7595   RecordLocation Loc = getLocalBitOffset(Offset);
7596   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7597   SavedStreamPosition SavedPosition(Cursor);
7598   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7599     Error(std::move(Err));
7600     return nullptr;
7601   }
7602   ReadingKindTracker ReadingKind(Read_Decl, *this);
7603 
7604   RecordData Record;
7605   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7606   if (!MaybeCode) {
7607     Error(MaybeCode.takeError());
7608     return nullptr;
7609   }
7610   unsigned Code = MaybeCode.get();
7611 
7612   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7613   if (!MaybeRecCode) {
7614     Error(MaybeRecCode.takeError());
7615     return nullptr;
7616   }
7617   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7618     Error("malformed AST file: missing C++ ctor initializers");
7619     return nullptr;
7620   }
7621 
7622   unsigned Idx = 0;
7623   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7624 }
7625 
7626 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7627   assert(ContextObj && "reading base specifiers with no AST context");
7628   ASTContext &Context = *ContextObj;
7629 
7630   RecordLocation Loc = getLocalBitOffset(Offset);
7631   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7632   SavedStreamPosition SavedPosition(Cursor);
7633   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7634     Error(std::move(Err));
7635     return nullptr;
7636   }
7637   ReadingKindTracker ReadingKind(Read_Decl, *this);
7638   RecordData Record;
7639 
7640   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7641   if (!MaybeCode) {
7642     Error(MaybeCode.takeError());
7643     return nullptr;
7644   }
7645   unsigned Code = MaybeCode.get();
7646 
7647   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7648   if (!MaybeRecCode) {
7649     Error(MaybeCode.takeError());
7650     return nullptr;
7651   }
7652   unsigned RecCode = MaybeRecCode.get();
7653 
7654   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7655     Error("malformed AST file: missing C++ base specifiers");
7656     return nullptr;
7657   }
7658 
7659   unsigned Idx = 0;
7660   unsigned NumBases = Record[Idx++];
7661   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7662   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7663   for (unsigned I = 0; I != NumBases; ++I)
7664     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7665   return Bases;
7666 }
7667 
7668 serialization::DeclID
7669 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7670   if (LocalID < NUM_PREDEF_DECL_IDS)
7671     return LocalID;
7672 
7673   if (!F.ModuleOffsetMap.empty())
7674     ReadModuleOffsetMap(F);
7675 
7676   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7677     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7678   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7679 
7680   return LocalID + I->second;
7681 }
7682 
7683 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7684                                    ModuleFile &M) const {
7685   // Predefined decls aren't from any module.
7686   if (ID < NUM_PREDEF_DECL_IDS)
7687     return false;
7688 
7689   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7690          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7691 }
7692 
7693 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7694   if (!D->isFromASTFile())
7695     return nullptr;
7696   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7697   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7698   return I->second;
7699 }
7700 
7701 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7702   if (ID < NUM_PREDEF_DECL_IDS)
7703     return SourceLocation();
7704 
7705   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7706 
7707   if (Index > DeclsLoaded.size()) {
7708     Error("declaration ID out-of-range for AST file");
7709     return SourceLocation();
7710   }
7711 
7712   if (Decl *D = DeclsLoaded[Index])
7713     return D->getLocation();
7714 
7715   SourceLocation Loc;
7716   DeclCursorForID(ID, Loc);
7717   return Loc;
7718 }
7719 
7720 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7721   switch (ID) {
7722   case PREDEF_DECL_NULL_ID:
7723     return nullptr;
7724 
7725   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7726     return Context.getTranslationUnitDecl();
7727 
7728   case PREDEF_DECL_OBJC_ID_ID:
7729     return Context.getObjCIdDecl();
7730 
7731   case PREDEF_DECL_OBJC_SEL_ID:
7732     return Context.getObjCSelDecl();
7733 
7734   case PREDEF_DECL_OBJC_CLASS_ID:
7735     return Context.getObjCClassDecl();
7736 
7737   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7738     return Context.getObjCProtocolDecl();
7739 
7740   case PREDEF_DECL_INT_128_ID:
7741     return Context.getInt128Decl();
7742 
7743   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7744     return Context.getUInt128Decl();
7745 
7746   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7747     return Context.getObjCInstanceTypeDecl();
7748 
7749   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7750     return Context.getBuiltinVaListDecl();
7751 
7752   case PREDEF_DECL_VA_LIST_TAG:
7753     return Context.getVaListTagDecl();
7754 
7755   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7756     return Context.getBuiltinMSVaListDecl();
7757 
7758   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7759     return Context.getExternCContextDecl();
7760 
7761   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7762     return Context.getMakeIntegerSeqDecl();
7763 
7764   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7765     return Context.getCFConstantStringDecl();
7766 
7767   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7768     return Context.getCFConstantStringTagDecl();
7769 
7770   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7771     return Context.getTypePackElementDecl();
7772   }
7773   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7774 }
7775 
7776 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7777   assert(ContextObj && "reading decl with no AST context");
7778   if (ID < NUM_PREDEF_DECL_IDS) {
7779     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7780     if (D) {
7781       // Track that we have merged the declaration with ID \p ID into the
7782       // pre-existing predefined declaration \p D.
7783       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7784       if (Merged.empty())
7785         Merged.push_back(ID);
7786     }
7787     return D;
7788   }
7789 
7790   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7791 
7792   if (Index >= DeclsLoaded.size()) {
7793     assert(0 && "declaration ID out-of-range for AST file");
7794     Error("declaration ID out-of-range for AST file");
7795     return nullptr;
7796   }
7797 
7798   return DeclsLoaded[Index];
7799 }
7800 
7801 Decl *ASTReader::GetDecl(DeclID ID) {
7802   if (ID < NUM_PREDEF_DECL_IDS)
7803     return GetExistingDecl(ID);
7804 
7805   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7806 
7807   if (Index >= DeclsLoaded.size()) {
7808     assert(0 && "declaration ID out-of-range for AST file");
7809     Error("declaration ID out-of-range for AST file");
7810     return nullptr;
7811   }
7812 
7813   if (!DeclsLoaded[Index]) {
7814     ReadDeclRecord(ID);
7815     if (DeserializationListener)
7816       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7817   }
7818 
7819   return DeclsLoaded[Index];
7820 }
7821 
7822 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7823                                                   DeclID GlobalID) {
7824   if (GlobalID < NUM_PREDEF_DECL_IDS)
7825     return GlobalID;
7826 
7827   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7828   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7829   ModuleFile *Owner = I->second;
7830 
7831   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7832     = M.GlobalToLocalDeclIDs.find(Owner);
7833   if (Pos == M.GlobalToLocalDeclIDs.end())
7834     return 0;
7835 
7836   return GlobalID - Owner->BaseDeclID + Pos->second;
7837 }
7838 
7839 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7840                                             const RecordData &Record,
7841                                             unsigned &Idx) {
7842   if (Idx >= Record.size()) {
7843     Error("Corrupted AST file");
7844     return 0;
7845   }
7846 
7847   return getGlobalDeclID(F, Record[Idx++]);
7848 }
7849 
7850 /// Resolve the offset of a statement into a statement.
7851 ///
7852 /// This operation will read a new statement from the external
7853 /// source each time it is called, and is meant to be used via a
7854 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7855 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7856   // Switch case IDs are per Decl.
7857   ClearSwitchCaseIDs();
7858 
7859   // Offset here is a global offset across the entire chain.
7860   RecordLocation Loc = getLocalBitOffset(Offset);
7861   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7862     Error(std::move(Err));
7863     return nullptr;
7864   }
7865   assert(NumCurrentElementsDeserializing == 0 &&
7866          "should not be called while already deserializing");
7867   Deserializing D(this);
7868   return ReadStmtFromStream(*Loc.F);
7869 }
7870 
7871 void ASTReader::FindExternalLexicalDecls(
7872     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7873     SmallVectorImpl<Decl *> &Decls) {
7874   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7875 
7876   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7877     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7878     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7879       auto K = (Decl::Kind)+LexicalDecls[I];
7880       if (!IsKindWeWant(K))
7881         continue;
7882 
7883       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7884 
7885       // Don't add predefined declarations to the lexical context more
7886       // than once.
7887       if (ID < NUM_PREDEF_DECL_IDS) {
7888         if (PredefsVisited[ID])
7889           continue;
7890 
7891         PredefsVisited[ID] = true;
7892       }
7893 
7894       if (Decl *D = GetLocalDecl(*M, ID)) {
7895         assert(D->getKind() == K && "wrong kind for lexical decl");
7896         if (!DC->isDeclInLexicalTraversal(D))
7897           Decls.push_back(D);
7898       }
7899     }
7900   };
7901 
7902   if (isa<TranslationUnitDecl>(DC)) {
7903     for (auto Lexical : TULexicalDecls)
7904       Visit(Lexical.first, Lexical.second);
7905   } else {
7906     auto I = LexicalDecls.find(DC);
7907     if (I != LexicalDecls.end())
7908       Visit(I->second.first, I->second.second);
7909   }
7910 
7911   ++NumLexicalDeclContextsRead;
7912 }
7913 
7914 namespace {
7915 
7916 class DeclIDComp {
7917   ASTReader &Reader;
7918   ModuleFile &Mod;
7919 
7920 public:
7921   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7922 
7923   bool operator()(LocalDeclID L, LocalDeclID R) const {
7924     SourceLocation LHS = getLocation(L);
7925     SourceLocation RHS = getLocation(R);
7926     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7927   }
7928 
7929   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7930     SourceLocation RHS = getLocation(R);
7931     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7932   }
7933 
7934   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7935     SourceLocation LHS = getLocation(L);
7936     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7937   }
7938 
7939   SourceLocation getLocation(LocalDeclID ID) const {
7940     return Reader.getSourceManager().getFileLoc(
7941             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7942   }
7943 };
7944 
7945 } // namespace
7946 
7947 void ASTReader::FindFileRegionDecls(FileID File,
7948                                     unsigned Offset, unsigned Length,
7949                                     SmallVectorImpl<Decl *> &Decls) {
7950   SourceManager &SM = getSourceManager();
7951 
7952   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7953   if (I == FileDeclIDs.end())
7954     return;
7955 
7956   FileDeclsInfo &DInfo = I->second;
7957   if (DInfo.Decls.empty())
7958     return;
7959 
7960   SourceLocation
7961     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7962   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7963 
7964   DeclIDComp DIDComp(*this, *DInfo.Mod);
7965   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7966       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7967   if (BeginIt != DInfo.Decls.begin())
7968     --BeginIt;
7969 
7970   // If we are pointing at a top-level decl inside an objc container, we need
7971   // to backtrack until we find it otherwise we will fail to report that the
7972   // region overlaps with an objc container.
7973   while (BeginIt != DInfo.Decls.begin() &&
7974          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7975              ->isTopLevelDeclInObjCContainer())
7976     --BeginIt;
7977 
7978   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7979       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7980   if (EndIt != DInfo.Decls.end())
7981     ++EndIt;
7982 
7983   for (ArrayRef<serialization::LocalDeclID>::iterator
7984          DIt = BeginIt; DIt != EndIt; ++DIt)
7985     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7986 }
7987 
7988 bool
7989 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7990                                           DeclarationName Name) {
7991   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7992          "DeclContext has no visible decls in storage");
7993   if (!Name)
7994     return false;
7995 
7996   auto It = Lookups.find(DC);
7997   if (It == Lookups.end())
7998     return false;
7999 
8000   Deserializing LookupResults(this);
8001 
8002   // Load the list of declarations.
8003   SmallVector<NamedDecl *, 64> Decls;
8004   for (DeclID ID : It->second.Table.find(Name)) {
8005     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8006     if (ND->getDeclName() == Name)
8007       Decls.push_back(ND);
8008   }
8009 
8010   ++NumVisibleDeclContextsRead;
8011   SetExternalVisibleDeclsForName(DC, Name, Decls);
8012   return !Decls.empty();
8013 }
8014 
8015 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8016   if (!DC->hasExternalVisibleStorage())
8017     return;
8018 
8019   auto It = Lookups.find(DC);
8020   assert(It != Lookups.end() &&
8021          "have external visible storage but no lookup tables");
8022 
8023   DeclsMap Decls;
8024 
8025   for (DeclID ID : It->second.Table.findAll()) {
8026     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8027     Decls[ND->getDeclName()].push_back(ND);
8028   }
8029 
8030   ++NumVisibleDeclContextsRead;
8031 
8032   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8033     SetExternalVisibleDeclsForName(DC, I->first, I->second);
8034   }
8035   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8036 }
8037 
8038 const serialization::reader::DeclContextLookupTable *
8039 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8040   auto I = Lookups.find(Primary);
8041   return I == Lookups.end() ? nullptr : &I->second;
8042 }
8043 
8044 /// Under non-PCH compilation the consumer receives the objc methods
8045 /// before receiving the implementation, and codegen depends on this.
8046 /// We simulate this by deserializing and passing to consumer the methods of the
8047 /// implementation before passing the deserialized implementation decl.
8048 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8049                                        ASTConsumer *Consumer) {
8050   assert(ImplD && Consumer);
8051 
8052   for (auto *I : ImplD->methods())
8053     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8054 
8055   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8056 }
8057 
8058 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8059   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8060     PassObjCImplDeclToConsumer(ImplD, Consumer);
8061   else
8062     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8063 }
8064 
8065 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8066   this->Consumer = Consumer;
8067 
8068   if (Consumer)
8069     PassInterestingDeclsToConsumer();
8070 
8071   if (DeserializationListener)
8072     DeserializationListener->ReaderInitialized(this);
8073 }
8074 
8075 void ASTReader::PrintStats() {
8076   std::fprintf(stderr, "*** AST File Statistics:\n");
8077 
8078   unsigned NumTypesLoaded
8079     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8080                                       QualType());
8081   unsigned NumDeclsLoaded
8082     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8083                                       (Decl *)nullptr);
8084   unsigned NumIdentifiersLoaded
8085     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8086                                             IdentifiersLoaded.end(),
8087                                             (IdentifierInfo *)nullptr);
8088   unsigned NumMacrosLoaded
8089     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8090                                        MacrosLoaded.end(),
8091                                        (MacroInfo *)nullptr);
8092   unsigned NumSelectorsLoaded
8093     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8094                                           SelectorsLoaded.end(),
8095                                           Selector());
8096 
8097   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8098     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8099                  NumSLocEntriesRead, TotalNumSLocEntries,
8100                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8101   if (!TypesLoaded.empty())
8102     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8103                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8104                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8105   if (!DeclsLoaded.empty())
8106     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8107                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8108                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8109   if (!IdentifiersLoaded.empty())
8110     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8111                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8112                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8113   if (!MacrosLoaded.empty())
8114     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8115                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8116                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8117   if (!SelectorsLoaded.empty())
8118     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8119                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8120                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8121   if (TotalNumStatements)
8122     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8123                  NumStatementsRead, TotalNumStatements,
8124                  ((float)NumStatementsRead/TotalNumStatements * 100));
8125   if (TotalNumMacros)
8126     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8127                  NumMacrosRead, TotalNumMacros,
8128                  ((float)NumMacrosRead/TotalNumMacros * 100));
8129   if (TotalLexicalDeclContexts)
8130     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8131                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8132                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8133                   * 100));
8134   if (TotalVisibleDeclContexts)
8135     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8136                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8137                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8138                   * 100));
8139   if (TotalNumMethodPoolEntries)
8140     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8141                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8142                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8143                   * 100));
8144   if (NumMethodPoolLookups)
8145     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8146                  NumMethodPoolHits, NumMethodPoolLookups,
8147                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8148   if (NumMethodPoolTableLookups)
8149     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8150                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8151                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8152                   * 100.0));
8153   if (NumIdentifierLookupHits)
8154     std::fprintf(stderr,
8155                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8156                  NumIdentifierLookupHits, NumIdentifierLookups,
8157                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8158 
8159   if (GlobalIndex) {
8160     std::fprintf(stderr, "\n");
8161     GlobalIndex->printStats();
8162   }
8163 
8164   std::fprintf(stderr, "\n");
8165   dump();
8166   std::fprintf(stderr, "\n");
8167 }
8168 
8169 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8170 LLVM_DUMP_METHOD static void
8171 dumpModuleIDMap(StringRef Name,
8172                 const ContinuousRangeMap<Key, ModuleFile *,
8173                                          InitialCapacity> &Map) {
8174   if (Map.begin() == Map.end())
8175     return;
8176 
8177   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8178 
8179   llvm::errs() << Name << ":\n";
8180   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8181        I != IEnd; ++I) {
8182     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8183       << "\n";
8184   }
8185 }
8186 
8187 LLVM_DUMP_METHOD void ASTReader::dump() {
8188   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8189   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8190   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8191   dumpModuleIDMap("Global type map", GlobalTypeMap);
8192   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8193   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8194   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8195   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8196   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8197   dumpModuleIDMap("Global preprocessed entity map",
8198                   GlobalPreprocessedEntityMap);
8199 
8200   llvm::errs() << "\n*** PCH/Modules Loaded:";
8201   for (ModuleFile &M : ModuleMgr)
8202     M.dump();
8203 }
8204 
8205 /// Return the amount of memory used by memory buffers, breaking down
8206 /// by heap-backed versus mmap'ed memory.
8207 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8208   for (ModuleFile &I : ModuleMgr) {
8209     if (llvm::MemoryBuffer *buf = I.Buffer) {
8210       size_t bytes = buf->getBufferSize();
8211       switch (buf->getBufferKind()) {
8212         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8213           sizes.malloc_bytes += bytes;
8214           break;
8215         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8216           sizes.mmap_bytes += bytes;
8217           break;
8218       }
8219     }
8220   }
8221 }
8222 
8223 void ASTReader::InitializeSema(Sema &S) {
8224   SemaObj = &S;
8225   S.addExternalSource(this);
8226 
8227   // Makes sure any declarations that were deserialized "too early"
8228   // still get added to the identifier's declaration chains.
8229   for (uint64_t ID : PreloadedDeclIDs) {
8230     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8231     pushExternalDeclIntoScope(D, D->getDeclName());
8232   }
8233   PreloadedDeclIDs.clear();
8234 
8235   // FIXME: What happens if these are changed by a module import?
8236   if (!FPPragmaOptions.empty()) {
8237     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8238     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8239   }
8240 
8241   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8242   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8243   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8244 
8245   UpdateSema();
8246 }
8247 
8248 void ASTReader::UpdateSema() {
8249   assert(SemaObj && "no Sema to update");
8250 
8251   // Load the offsets of the declarations that Sema references.
8252   // They will be lazily deserialized when needed.
8253   if (!SemaDeclRefs.empty()) {
8254     assert(SemaDeclRefs.size() % 3 == 0);
8255     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8256       if (!SemaObj->StdNamespace)
8257         SemaObj->StdNamespace = SemaDeclRefs[I];
8258       if (!SemaObj->StdBadAlloc)
8259         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8260       if (!SemaObj->StdAlignValT)
8261         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8262     }
8263     SemaDeclRefs.clear();
8264   }
8265 
8266   // Update the state of pragmas. Use the same API as if we had encountered the
8267   // pragma in the source.
8268   if(OptimizeOffPragmaLocation.isValid())
8269     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8270   if (PragmaMSStructState != -1)
8271     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8272   if (PointersToMembersPragmaLocation.isValid()) {
8273     SemaObj->ActOnPragmaMSPointersToMembers(
8274         (LangOptions::PragmaMSPointersToMembersKind)
8275             PragmaMSPointersToMembersState,
8276         PointersToMembersPragmaLocation);
8277   }
8278   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8279 
8280   if (PragmaPackCurrentValue) {
8281     // The bottom of the stack might have a default value. It must be adjusted
8282     // to the current value to ensure that the packing state is preserved after
8283     // popping entries that were included/imported from a PCH/module.
8284     bool DropFirst = false;
8285     if (!PragmaPackStack.empty() &&
8286         PragmaPackStack.front().Location.isInvalid()) {
8287       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8288              "Expected a default alignment value");
8289       SemaObj->PackStack.Stack.emplace_back(
8290           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8291           SemaObj->PackStack.CurrentPragmaLocation,
8292           PragmaPackStack.front().PushLocation);
8293       DropFirst = true;
8294     }
8295     for (const auto &Entry :
8296          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8297       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8298                                             Entry.Location, Entry.PushLocation);
8299     if (PragmaPackCurrentLocation.isInvalid()) {
8300       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8301              "Expected a default alignment value");
8302       // Keep the current values.
8303     } else {
8304       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8305       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8306     }
8307   }
8308 }
8309 
8310 IdentifierInfo *ASTReader::get(StringRef Name) {
8311   // Note that we are loading an identifier.
8312   Deserializing AnIdentifier(this);
8313 
8314   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8315                                   NumIdentifierLookups,
8316                                   NumIdentifierLookupHits);
8317 
8318   // We don't need to do identifier table lookups in C++ modules (we preload
8319   // all interesting declarations, and don't need to use the scope for name
8320   // lookups). Perform the lookup in PCH files, though, since we don't build
8321   // a complete initial identifier table if we're carrying on from a PCH.
8322   if (PP.getLangOpts().CPlusPlus) {
8323     for (auto F : ModuleMgr.pch_modules())
8324       if (Visitor(*F))
8325         break;
8326   } else {
8327     // If there is a global index, look there first to determine which modules
8328     // provably do not have any results for this identifier.
8329     GlobalModuleIndex::HitSet Hits;
8330     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8331     if (!loadGlobalIndex()) {
8332       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8333         HitsPtr = &Hits;
8334       }
8335     }
8336 
8337     ModuleMgr.visit(Visitor, HitsPtr);
8338   }
8339 
8340   IdentifierInfo *II = Visitor.getIdentifierInfo();
8341   markIdentifierUpToDate(II);
8342   return II;
8343 }
8344 
8345 namespace clang {
8346 
8347   /// An identifier-lookup iterator that enumerates all of the
8348   /// identifiers stored within a set of AST files.
8349   class ASTIdentifierIterator : public IdentifierIterator {
8350     /// The AST reader whose identifiers are being enumerated.
8351     const ASTReader &Reader;
8352 
8353     /// The current index into the chain of AST files stored in
8354     /// the AST reader.
8355     unsigned Index;
8356 
8357     /// The current position within the identifier lookup table
8358     /// of the current AST file.
8359     ASTIdentifierLookupTable::key_iterator Current;
8360 
8361     /// The end position within the identifier lookup table of
8362     /// the current AST file.
8363     ASTIdentifierLookupTable::key_iterator End;
8364 
8365     /// Whether to skip any modules in the ASTReader.
8366     bool SkipModules;
8367 
8368   public:
8369     explicit ASTIdentifierIterator(const ASTReader &Reader,
8370                                    bool SkipModules = false);
8371 
8372     StringRef Next() override;
8373   };
8374 
8375 } // namespace clang
8376 
8377 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8378                                              bool SkipModules)
8379     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8380 }
8381 
8382 StringRef ASTIdentifierIterator::Next() {
8383   while (Current == End) {
8384     // If we have exhausted all of our AST files, we're done.
8385     if (Index == 0)
8386       return StringRef();
8387 
8388     --Index;
8389     ModuleFile &F = Reader.ModuleMgr[Index];
8390     if (SkipModules && F.isModule())
8391       continue;
8392 
8393     ASTIdentifierLookupTable *IdTable =
8394         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8395     Current = IdTable->key_begin();
8396     End = IdTable->key_end();
8397   }
8398 
8399   // We have any identifiers remaining in the current AST file; return
8400   // the next one.
8401   StringRef Result = *Current;
8402   ++Current;
8403   return Result;
8404 }
8405 
8406 namespace {
8407 
8408 /// A utility for appending two IdentifierIterators.
8409 class ChainedIdentifierIterator : public IdentifierIterator {
8410   std::unique_ptr<IdentifierIterator> Current;
8411   std::unique_ptr<IdentifierIterator> Queued;
8412 
8413 public:
8414   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8415                             std::unique_ptr<IdentifierIterator> Second)
8416       : Current(std::move(First)), Queued(std::move(Second)) {}
8417 
8418   StringRef Next() override {
8419     if (!Current)
8420       return StringRef();
8421 
8422     StringRef result = Current->Next();
8423     if (!result.empty())
8424       return result;
8425 
8426     // Try the queued iterator, which may itself be empty.
8427     Current.reset();
8428     std::swap(Current, Queued);
8429     return Next();
8430   }
8431 };
8432 
8433 } // namespace
8434 
8435 IdentifierIterator *ASTReader::getIdentifiers() {
8436   if (!loadGlobalIndex()) {
8437     std::unique_ptr<IdentifierIterator> ReaderIter(
8438         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8439     std::unique_ptr<IdentifierIterator> ModulesIter(
8440         GlobalIndex->createIdentifierIterator());
8441     return new ChainedIdentifierIterator(std::move(ReaderIter),
8442                                          std::move(ModulesIter));
8443   }
8444 
8445   return new ASTIdentifierIterator(*this);
8446 }
8447 
8448 namespace clang {
8449 namespace serialization {
8450 
8451   class ReadMethodPoolVisitor {
8452     ASTReader &Reader;
8453     Selector Sel;
8454     unsigned PriorGeneration;
8455     unsigned InstanceBits = 0;
8456     unsigned FactoryBits = 0;
8457     bool InstanceHasMoreThanOneDecl = false;
8458     bool FactoryHasMoreThanOneDecl = false;
8459     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8460     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8461 
8462   public:
8463     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8464                           unsigned PriorGeneration)
8465         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8466 
8467     bool operator()(ModuleFile &M) {
8468       if (!M.SelectorLookupTable)
8469         return false;
8470 
8471       // If we've already searched this module file, skip it now.
8472       if (M.Generation <= PriorGeneration)
8473         return true;
8474 
8475       ++Reader.NumMethodPoolTableLookups;
8476       ASTSelectorLookupTable *PoolTable
8477         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8478       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8479       if (Pos == PoolTable->end())
8480         return false;
8481 
8482       ++Reader.NumMethodPoolTableHits;
8483       ++Reader.NumSelectorsRead;
8484       // FIXME: Not quite happy with the statistics here. We probably should
8485       // disable this tracking when called via LoadSelector.
8486       // Also, should entries without methods count as misses?
8487       ++Reader.NumMethodPoolEntriesRead;
8488       ASTSelectorLookupTrait::data_type Data = *Pos;
8489       if (Reader.DeserializationListener)
8490         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8491 
8492       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8493       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8494       InstanceBits = Data.InstanceBits;
8495       FactoryBits = Data.FactoryBits;
8496       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8497       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8498       return true;
8499     }
8500 
8501     /// Retrieve the instance methods found by this visitor.
8502     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8503       return InstanceMethods;
8504     }
8505 
8506     /// Retrieve the instance methods found by this visitor.
8507     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8508       return FactoryMethods;
8509     }
8510 
8511     unsigned getInstanceBits() const { return InstanceBits; }
8512     unsigned getFactoryBits() const { return FactoryBits; }
8513 
8514     bool instanceHasMoreThanOneDecl() const {
8515       return InstanceHasMoreThanOneDecl;
8516     }
8517 
8518     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8519   };
8520 
8521 } // namespace serialization
8522 } // namespace clang
8523 
8524 /// Add the given set of methods to the method list.
8525 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8526                              ObjCMethodList &List) {
8527   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8528     S.addMethodToGlobalList(&List, Methods[I]);
8529   }
8530 }
8531 
8532 void ASTReader::ReadMethodPool(Selector Sel) {
8533   // Get the selector generation and update it to the current generation.
8534   unsigned &Generation = SelectorGeneration[Sel];
8535   unsigned PriorGeneration = Generation;
8536   Generation = getGeneration();
8537   SelectorOutOfDate[Sel] = false;
8538 
8539   // Search for methods defined with this selector.
8540   ++NumMethodPoolLookups;
8541   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8542   ModuleMgr.visit(Visitor);
8543 
8544   if (Visitor.getInstanceMethods().empty() &&
8545       Visitor.getFactoryMethods().empty())
8546     return;
8547 
8548   ++NumMethodPoolHits;
8549 
8550   if (!getSema())
8551     return;
8552 
8553   Sema &S = *getSema();
8554   Sema::GlobalMethodPool::iterator Pos
8555     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8556 
8557   Pos->second.first.setBits(Visitor.getInstanceBits());
8558   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8559   Pos->second.second.setBits(Visitor.getFactoryBits());
8560   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8561 
8562   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8563   // when building a module we keep every method individually and may need to
8564   // update hasMoreThanOneDecl as we add the methods.
8565   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8566   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8567 }
8568 
8569 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8570   if (SelectorOutOfDate[Sel])
8571     ReadMethodPool(Sel);
8572 }
8573 
8574 void ASTReader::ReadKnownNamespaces(
8575                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8576   Namespaces.clear();
8577 
8578   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8579     if (NamespaceDecl *Namespace
8580                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8581       Namespaces.push_back(Namespace);
8582   }
8583 }
8584 
8585 void ASTReader::ReadUndefinedButUsed(
8586     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8587   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8588     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8589     SourceLocation Loc =
8590         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8591     Undefined.insert(std::make_pair(D, Loc));
8592   }
8593 }
8594 
8595 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8596     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8597                                                      Exprs) {
8598   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8599     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8600     uint64_t Count = DelayedDeleteExprs[Idx++];
8601     for (uint64_t C = 0; C < Count; ++C) {
8602       SourceLocation DeleteLoc =
8603           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8604       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8605       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8606     }
8607   }
8608 }
8609 
8610 void ASTReader::ReadTentativeDefinitions(
8611                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8612   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8613     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8614     if (Var)
8615       TentativeDefs.push_back(Var);
8616   }
8617   TentativeDefinitions.clear();
8618 }
8619 
8620 void ASTReader::ReadUnusedFileScopedDecls(
8621                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8622   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8623     DeclaratorDecl *D
8624       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8625     if (D)
8626       Decls.push_back(D);
8627   }
8628   UnusedFileScopedDecls.clear();
8629 }
8630 
8631 void ASTReader::ReadDelegatingConstructors(
8632                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8633   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8634     CXXConstructorDecl *D
8635       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8636     if (D)
8637       Decls.push_back(D);
8638   }
8639   DelegatingCtorDecls.clear();
8640 }
8641 
8642 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8643   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8644     TypedefNameDecl *D
8645       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8646     if (D)
8647       Decls.push_back(D);
8648   }
8649   ExtVectorDecls.clear();
8650 }
8651 
8652 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8653     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8654   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8655        ++I) {
8656     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8657         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8658     if (D)
8659       Decls.insert(D);
8660   }
8661   UnusedLocalTypedefNameCandidates.clear();
8662 }
8663 
8664 void ASTReader::ReadReferencedSelectors(
8665        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8666   if (ReferencedSelectorsData.empty())
8667     return;
8668 
8669   // If there are @selector references added them to its pool. This is for
8670   // implementation of -Wselector.
8671   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8672   unsigned I = 0;
8673   while (I < DataSize) {
8674     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8675     SourceLocation SelLoc
8676       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8677     Sels.push_back(std::make_pair(Sel, SelLoc));
8678   }
8679   ReferencedSelectorsData.clear();
8680 }
8681 
8682 void ASTReader::ReadWeakUndeclaredIdentifiers(
8683        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8684   if (WeakUndeclaredIdentifiers.empty())
8685     return;
8686 
8687   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8688     IdentifierInfo *WeakId
8689       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8690     IdentifierInfo *AliasId
8691       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8692     SourceLocation Loc
8693       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8694     bool Used = WeakUndeclaredIdentifiers[I++];
8695     WeakInfo WI(AliasId, Loc);
8696     WI.setUsed(Used);
8697     WeakIDs.push_back(std::make_pair(WeakId, WI));
8698   }
8699   WeakUndeclaredIdentifiers.clear();
8700 }
8701 
8702 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8703   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8704     ExternalVTableUse VT;
8705     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8706     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8707     VT.DefinitionRequired = VTableUses[Idx++];
8708     VTables.push_back(VT);
8709   }
8710 
8711   VTableUses.clear();
8712 }
8713 
8714 void ASTReader::ReadPendingInstantiations(
8715        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8716   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8717     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8718     SourceLocation Loc
8719       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8720 
8721     Pending.push_back(std::make_pair(D, Loc));
8722   }
8723   PendingInstantiations.clear();
8724 }
8725 
8726 void ASTReader::ReadLateParsedTemplates(
8727     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8728         &LPTMap) {
8729   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8730        /* In loop */) {
8731     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8732 
8733     auto LT = std::make_unique<LateParsedTemplate>();
8734     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8735 
8736     ModuleFile *F = getOwningModuleFile(LT->D);
8737     assert(F && "No module");
8738 
8739     unsigned TokN = LateParsedTemplates[Idx++];
8740     LT->Toks.reserve(TokN);
8741     for (unsigned T = 0; T < TokN; ++T)
8742       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8743 
8744     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8745   }
8746 
8747   LateParsedTemplates.clear();
8748 }
8749 
8750 void ASTReader::LoadSelector(Selector Sel) {
8751   // It would be complicated to avoid reading the methods anyway. So don't.
8752   ReadMethodPool(Sel);
8753 }
8754 
8755 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8756   assert(ID && "Non-zero identifier ID required");
8757   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8758   IdentifiersLoaded[ID - 1] = II;
8759   if (DeserializationListener)
8760     DeserializationListener->IdentifierRead(ID, II);
8761 }
8762 
8763 /// Set the globally-visible declarations associated with the given
8764 /// identifier.
8765 ///
8766 /// If the AST reader is currently in a state where the given declaration IDs
8767 /// cannot safely be resolved, they are queued until it is safe to resolve
8768 /// them.
8769 ///
8770 /// \param II an IdentifierInfo that refers to one or more globally-visible
8771 /// declarations.
8772 ///
8773 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8774 /// visible at global scope.
8775 ///
8776 /// \param Decls if non-null, this vector will be populated with the set of
8777 /// deserialized declarations. These declarations will not be pushed into
8778 /// scope.
8779 void
8780 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8781                               const SmallVectorImpl<uint32_t> &DeclIDs,
8782                                    SmallVectorImpl<Decl *> *Decls) {
8783   if (NumCurrentElementsDeserializing && !Decls) {
8784     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8785     return;
8786   }
8787 
8788   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8789     if (!SemaObj) {
8790       // Queue this declaration so that it will be added to the
8791       // translation unit scope and identifier's declaration chain
8792       // once a Sema object is known.
8793       PreloadedDeclIDs.push_back(DeclIDs[I]);
8794       continue;
8795     }
8796 
8797     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8798 
8799     // If we're simply supposed to record the declarations, do so now.
8800     if (Decls) {
8801       Decls->push_back(D);
8802       continue;
8803     }
8804 
8805     // Introduce this declaration into the translation-unit scope
8806     // and add it to the declaration chain for this identifier, so
8807     // that (unqualified) name lookup will find it.
8808     pushExternalDeclIntoScope(D, II);
8809   }
8810 }
8811 
8812 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8813   if (ID == 0)
8814     return nullptr;
8815 
8816   if (IdentifiersLoaded.empty()) {
8817     Error("no identifier table in AST file");
8818     return nullptr;
8819   }
8820 
8821   ID -= 1;
8822   if (!IdentifiersLoaded[ID]) {
8823     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8824     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8825     ModuleFile *M = I->second;
8826     unsigned Index = ID - M->BaseIdentifierID;
8827     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8828 
8829     // All of the strings in the AST file are preceded by a 16-bit length.
8830     // Extract that 16-bit length to avoid having to execute strlen().
8831     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8832     //  unsigned integers.  This is important to avoid integer overflow when
8833     //  we cast them to 'unsigned'.
8834     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8835     unsigned StrLen = (((unsigned) StrLenPtr[0])
8836                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8837     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8838     IdentifiersLoaded[ID] = &II;
8839     markIdentifierFromAST(*this,  II);
8840     if (DeserializationListener)
8841       DeserializationListener->IdentifierRead(ID + 1, &II);
8842   }
8843 
8844   return IdentifiersLoaded[ID];
8845 }
8846 
8847 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8848   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8849 }
8850 
8851 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8852   if (LocalID < NUM_PREDEF_IDENT_IDS)
8853     return LocalID;
8854 
8855   if (!M.ModuleOffsetMap.empty())
8856     ReadModuleOffsetMap(M);
8857 
8858   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8859     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8860   assert(I != M.IdentifierRemap.end()
8861          && "Invalid index into identifier index remap");
8862 
8863   return LocalID + I->second;
8864 }
8865 
8866 MacroInfo *ASTReader::getMacro(MacroID ID) {
8867   if (ID == 0)
8868     return nullptr;
8869 
8870   if (MacrosLoaded.empty()) {
8871     Error("no macro table in AST file");
8872     return nullptr;
8873   }
8874 
8875   ID -= NUM_PREDEF_MACRO_IDS;
8876   if (!MacrosLoaded[ID]) {
8877     GlobalMacroMapType::iterator I
8878       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8879     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8880     ModuleFile *M = I->second;
8881     unsigned Index = ID - M->BaseMacroID;
8882     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8883 
8884     if (DeserializationListener)
8885       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8886                                          MacrosLoaded[ID]);
8887   }
8888 
8889   return MacrosLoaded[ID];
8890 }
8891 
8892 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8893   if (LocalID < NUM_PREDEF_MACRO_IDS)
8894     return LocalID;
8895 
8896   if (!M.ModuleOffsetMap.empty())
8897     ReadModuleOffsetMap(M);
8898 
8899   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8900     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8901   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8902 
8903   return LocalID + I->second;
8904 }
8905 
8906 serialization::SubmoduleID
8907 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8908   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8909     return LocalID;
8910 
8911   if (!M.ModuleOffsetMap.empty())
8912     ReadModuleOffsetMap(M);
8913 
8914   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8915     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8916   assert(I != M.SubmoduleRemap.end()
8917          && "Invalid index into submodule index remap");
8918 
8919   return LocalID + I->second;
8920 }
8921 
8922 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8923   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8924     assert(GlobalID == 0 && "Unhandled global submodule ID");
8925     return nullptr;
8926   }
8927 
8928   if (GlobalID > SubmodulesLoaded.size()) {
8929     Error("submodule ID out of range in AST file");
8930     return nullptr;
8931   }
8932 
8933   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8934 }
8935 
8936 Module *ASTReader::getModule(unsigned ID) {
8937   return getSubmodule(ID);
8938 }
8939 
8940 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8941   ModuleFile *MF = getOwningModuleFile(D);
8942   return MF && MF->PCHHasObjectFile;
8943 }
8944 
8945 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8946   if (ID & 1) {
8947     // It's a module, look it up by submodule ID.
8948     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8949     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8950   } else {
8951     // It's a prefix (preamble, PCH, ...). Look it up by index.
8952     unsigned IndexFromEnd = ID >> 1;
8953     assert(IndexFromEnd && "got reference to unknown module file");
8954     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8955   }
8956 }
8957 
8958 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8959   if (!F)
8960     return 1;
8961 
8962   // For a file representing a module, use the submodule ID of the top-level
8963   // module as the file ID. For any other kind of file, the number of such
8964   // files loaded beforehand will be the same on reload.
8965   // FIXME: Is this true even if we have an explicit module file and a PCH?
8966   if (F->isModule())
8967     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8968 
8969   auto PCHModules = getModuleManager().pch_modules();
8970   auto I = llvm::find(PCHModules, F);
8971   assert(I != PCHModules.end() && "emitting reference to unknown file");
8972   return (I - PCHModules.end()) << 1;
8973 }
8974 
8975 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8976 ASTReader::getSourceDescriptor(unsigned ID) {
8977   if (const Module *M = getSubmodule(ID))
8978     return ExternalASTSource::ASTSourceDescriptor(*M);
8979 
8980   // If there is only a single PCH, return it instead.
8981   // Chained PCH are not supported.
8982   const auto &PCHChain = ModuleMgr.pch_modules();
8983   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8984     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8985     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8986     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8987     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8988                                           MF.Signature);
8989   }
8990   return None;
8991 }
8992 
8993 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8994   auto I = DefinitionSource.find(FD);
8995   if (I == DefinitionSource.end())
8996     return EK_ReplyHazy;
8997   return I->second ? EK_Never : EK_Always;
8998 }
8999 
9000 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9001   return DecodeSelector(getGlobalSelectorID(M, LocalID));
9002 }
9003 
9004 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9005   if (ID == 0)
9006     return Selector();
9007 
9008   if (ID > SelectorsLoaded.size()) {
9009     Error("selector ID out of range in AST file");
9010     return Selector();
9011   }
9012 
9013   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9014     // Load this selector from the selector table.
9015     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9016     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9017     ModuleFile &M = *I->second;
9018     ASTSelectorLookupTrait Trait(*this, M);
9019     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9020     SelectorsLoaded[ID - 1] =
9021       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9022     if (DeserializationListener)
9023       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9024   }
9025 
9026   return SelectorsLoaded[ID - 1];
9027 }
9028 
9029 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9030   return DecodeSelector(ID);
9031 }
9032 
9033 uint32_t ASTReader::GetNumExternalSelectors() {
9034   // ID 0 (the null selector) is considered an external selector.
9035   return getTotalNumSelectors() + 1;
9036 }
9037 
9038 serialization::SelectorID
9039 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9040   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9041     return LocalID;
9042 
9043   if (!M.ModuleOffsetMap.empty())
9044     ReadModuleOffsetMap(M);
9045 
9046   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9047     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9048   assert(I != M.SelectorRemap.end()
9049          && "Invalid index into selector index remap");
9050 
9051   return LocalID + I->second;
9052 }
9053 
9054 DeclarationName
9055 ASTReader::ReadDeclarationName(ModuleFile &F,
9056                                const RecordData &Record, unsigned &Idx) {
9057   ASTContext &Context = getContext();
9058   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9059   switch (Kind) {
9060   case DeclarationName::Identifier:
9061     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9062 
9063   case DeclarationName::ObjCZeroArgSelector:
9064   case DeclarationName::ObjCOneArgSelector:
9065   case DeclarationName::ObjCMultiArgSelector:
9066     return DeclarationName(ReadSelector(F, Record, Idx));
9067 
9068   case DeclarationName::CXXConstructorName:
9069     return Context.DeclarationNames.getCXXConstructorName(
9070                           Context.getCanonicalType(readType(F, Record, Idx)));
9071 
9072   case DeclarationName::CXXDestructorName:
9073     return Context.DeclarationNames.getCXXDestructorName(
9074                           Context.getCanonicalType(readType(F, Record, Idx)));
9075 
9076   case DeclarationName::CXXDeductionGuideName:
9077     return Context.DeclarationNames.getCXXDeductionGuideName(
9078                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
9079 
9080   case DeclarationName::CXXConversionFunctionName:
9081     return Context.DeclarationNames.getCXXConversionFunctionName(
9082                           Context.getCanonicalType(readType(F, Record, Idx)));
9083 
9084   case DeclarationName::CXXOperatorName:
9085     return Context.DeclarationNames.getCXXOperatorName(
9086                                        (OverloadedOperatorKind)Record[Idx++]);
9087 
9088   case DeclarationName::CXXLiteralOperatorName:
9089     return Context.DeclarationNames.getCXXLiteralOperatorName(
9090                                        GetIdentifierInfo(F, Record, Idx));
9091 
9092   case DeclarationName::CXXUsingDirective:
9093     return DeclarationName::getUsingDirectiveName();
9094   }
9095 
9096   llvm_unreachable("Invalid NameKind!");
9097 }
9098 
9099 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9100                                        DeclarationNameLoc &DNLoc,
9101                                        DeclarationName Name,
9102                                       const RecordData &Record, unsigned &Idx) {
9103   switch (Name.getNameKind()) {
9104   case DeclarationName::CXXConstructorName:
9105   case DeclarationName::CXXDestructorName:
9106   case DeclarationName::CXXConversionFunctionName:
9107     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9108     break;
9109 
9110   case DeclarationName::CXXOperatorName:
9111     DNLoc.CXXOperatorName.BeginOpNameLoc
9112         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9113     DNLoc.CXXOperatorName.EndOpNameLoc
9114         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9115     break;
9116 
9117   case DeclarationName::CXXLiteralOperatorName:
9118     DNLoc.CXXLiteralOperatorName.OpNameLoc
9119         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9120     break;
9121 
9122   case DeclarationName::Identifier:
9123   case DeclarationName::ObjCZeroArgSelector:
9124   case DeclarationName::ObjCOneArgSelector:
9125   case DeclarationName::ObjCMultiArgSelector:
9126   case DeclarationName::CXXUsingDirective:
9127   case DeclarationName::CXXDeductionGuideName:
9128     break;
9129   }
9130 }
9131 
9132 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9133                                         DeclarationNameInfo &NameInfo,
9134                                       const RecordData &Record, unsigned &Idx) {
9135   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9136   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9137   DeclarationNameLoc DNLoc;
9138   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9139   NameInfo.setInfo(DNLoc);
9140 }
9141 
9142 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9143                                   const RecordData &Record, unsigned &Idx) {
9144   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9145   unsigned NumTPLists = Record[Idx++];
9146   Info.NumTemplParamLists = NumTPLists;
9147   if (NumTPLists) {
9148     Info.TemplParamLists =
9149         new (getContext()) TemplateParameterList *[NumTPLists];
9150     for (unsigned i = 0; i != NumTPLists; ++i)
9151       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9152   }
9153 }
9154 
9155 TemplateName
9156 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9157                             unsigned &Idx) {
9158   ASTContext &Context = getContext();
9159   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9160   switch (Kind) {
9161   case TemplateName::Template:
9162       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9163 
9164   case TemplateName::OverloadedTemplate: {
9165     unsigned size = Record[Idx++];
9166     UnresolvedSet<8> Decls;
9167     while (size--)
9168       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9169 
9170     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9171   }
9172 
9173   case TemplateName::AssumedTemplate: {
9174     DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9175     return Context.getAssumedTemplateName(Name);
9176   }
9177 
9178   case TemplateName::QualifiedTemplate: {
9179     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9180     bool hasTemplKeyword = Record[Idx++];
9181     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9182     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9183   }
9184 
9185   case TemplateName::DependentTemplate: {
9186     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9187     if (Record[Idx++])  // isIdentifier
9188       return Context.getDependentTemplateName(NNS,
9189                                                GetIdentifierInfo(F, Record,
9190                                                                  Idx));
9191     return Context.getDependentTemplateName(NNS,
9192                                          (OverloadedOperatorKind)Record[Idx++]);
9193   }
9194 
9195   case TemplateName::SubstTemplateTemplateParm: {
9196     TemplateTemplateParmDecl *param
9197       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9198     if (!param) return TemplateName();
9199     TemplateName replacement = ReadTemplateName(F, Record, Idx);
9200     return Context.getSubstTemplateTemplateParm(param, replacement);
9201   }
9202 
9203   case TemplateName::SubstTemplateTemplateParmPack: {
9204     TemplateTemplateParmDecl *Param
9205       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9206     if (!Param)
9207       return TemplateName();
9208 
9209     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9210     if (ArgPack.getKind() != TemplateArgument::Pack)
9211       return TemplateName();
9212 
9213     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9214   }
9215   }
9216 
9217   llvm_unreachable("Unhandled template name kind!");
9218 }
9219 
9220 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9221                                                  const RecordData &Record,
9222                                                  unsigned &Idx,
9223                                                  bool Canonicalize) {
9224   ASTContext &Context = getContext();
9225   if (Canonicalize) {
9226     // The caller wants a canonical template argument. Sometimes the AST only
9227     // wants template arguments in canonical form (particularly as the template
9228     // argument lists of template specializations) so ensure we preserve that
9229     // canonical form across serialization.
9230     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9231     return Context.getCanonicalTemplateArgument(Arg);
9232   }
9233 
9234   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9235   switch (Kind) {
9236   case TemplateArgument::Null:
9237     return TemplateArgument();
9238   case TemplateArgument::Type:
9239     return TemplateArgument(readType(F, Record, Idx));
9240   case TemplateArgument::Declaration: {
9241     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9242     return TemplateArgument(D, readType(F, Record, Idx));
9243   }
9244   case TemplateArgument::NullPtr:
9245     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9246   case TemplateArgument::Integral: {
9247     llvm::APSInt Value = ReadAPSInt(Record, Idx);
9248     QualType T = readType(F, Record, Idx);
9249     return TemplateArgument(Context, Value, T);
9250   }
9251   case TemplateArgument::Template:
9252     return TemplateArgument(ReadTemplateName(F, Record, Idx));
9253   case TemplateArgument::TemplateExpansion: {
9254     TemplateName Name = ReadTemplateName(F, Record, Idx);
9255     Optional<unsigned> NumTemplateExpansions;
9256     if (unsigned NumExpansions = Record[Idx++])
9257       NumTemplateExpansions = NumExpansions - 1;
9258     return TemplateArgument(Name, NumTemplateExpansions);
9259   }
9260   case TemplateArgument::Expression:
9261     return TemplateArgument(ReadExpr(F));
9262   case TemplateArgument::Pack: {
9263     unsigned NumArgs = Record[Idx++];
9264     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9265     for (unsigned I = 0; I != NumArgs; ++I)
9266       Args[I] = ReadTemplateArgument(F, Record, Idx);
9267     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9268   }
9269   }
9270 
9271   llvm_unreachable("Unhandled template argument kind!");
9272 }
9273 
9274 TemplateParameterList *
9275 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9276                                      const RecordData &Record, unsigned &Idx) {
9277   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9278   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9279   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9280 
9281   unsigned NumParams = Record[Idx++];
9282   SmallVector<NamedDecl *, 16> Params;
9283   Params.reserve(NumParams);
9284   while (NumParams--)
9285     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9286 
9287   // TODO: Concepts
9288   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9289       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
9290   return TemplateParams;
9291 }
9292 
9293 void
9294 ASTReader::
9295 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9296                          ModuleFile &F, const RecordData &Record,
9297                          unsigned &Idx, bool Canonicalize) {
9298   unsigned NumTemplateArgs = Record[Idx++];
9299   TemplArgs.reserve(NumTemplateArgs);
9300   while (NumTemplateArgs--)
9301     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9302 }
9303 
9304 /// Read a UnresolvedSet structure.
9305 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9306                                   const RecordData &Record, unsigned &Idx) {
9307   unsigned NumDecls = Record[Idx++];
9308   Set.reserve(getContext(), NumDecls);
9309   while (NumDecls--) {
9310     DeclID ID = ReadDeclID(F, Record, Idx);
9311     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9312     Set.addLazyDecl(getContext(), ID, AS);
9313   }
9314 }
9315 
9316 CXXBaseSpecifier
9317 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9318                                 const RecordData &Record, unsigned &Idx) {
9319   bool isVirtual = static_cast<bool>(Record[Idx++]);
9320   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9321   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9322   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9323   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9324   SourceRange Range = ReadSourceRange(F, Record, Idx);
9325   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9326   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9327                           EllipsisLoc);
9328   Result.setInheritConstructors(inheritConstructors);
9329   return Result;
9330 }
9331 
9332 CXXCtorInitializer **
9333 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9334                                    unsigned &Idx) {
9335   ASTContext &Context = getContext();
9336   unsigned NumInitializers = Record[Idx++];
9337   assert(NumInitializers && "wrote ctor initializers but have no inits");
9338   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9339   for (unsigned i = 0; i != NumInitializers; ++i) {
9340     TypeSourceInfo *TInfo = nullptr;
9341     bool IsBaseVirtual = false;
9342     FieldDecl *Member = nullptr;
9343     IndirectFieldDecl *IndirectMember = nullptr;
9344 
9345     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9346     switch (Type) {
9347     case CTOR_INITIALIZER_BASE:
9348       TInfo = GetTypeSourceInfo(F, Record, Idx);
9349       IsBaseVirtual = Record[Idx++];
9350       break;
9351 
9352     case CTOR_INITIALIZER_DELEGATING:
9353       TInfo = GetTypeSourceInfo(F, Record, Idx);
9354       break;
9355 
9356      case CTOR_INITIALIZER_MEMBER:
9357       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9358       break;
9359 
9360      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9361       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9362       break;
9363     }
9364 
9365     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9366     Expr *Init = ReadExpr(F);
9367     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9368     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9369 
9370     CXXCtorInitializer *BOMInit;
9371     if (Type == CTOR_INITIALIZER_BASE)
9372       BOMInit = new (Context)
9373           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9374                              RParenLoc, MemberOrEllipsisLoc);
9375     else if (Type == CTOR_INITIALIZER_DELEGATING)
9376       BOMInit = new (Context)
9377           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9378     else if (Member)
9379       BOMInit = new (Context)
9380           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9381                              Init, RParenLoc);
9382     else
9383       BOMInit = new (Context)
9384           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9385                              LParenLoc, Init, RParenLoc);
9386 
9387     if (/*IsWritten*/Record[Idx++]) {
9388       unsigned SourceOrder = Record[Idx++];
9389       BOMInit->setSourceOrder(SourceOrder);
9390     }
9391 
9392     CtorInitializers[i] = BOMInit;
9393   }
9394 
9395   return CtorInitializers;
9396 }
9397 
9398 NestedNameSpecifier *
9399 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9400                                    const RecordData &Record, unsigned &Idx) {
9401   ASTContext &Context = getContext();
9402   unsigned N = Record[Idx++];
9403   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9404   for (unsigned I = 0; I != N; ++I) {
9405     NestedNameSpecifier::SpecifierKind Kind
9406       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9407     switch (Kind) {
9408     case NestedNameSpecifier::Identifier: {
9409       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9410       NNS = NestedNameSpecifier::Create(Context, Prev, II);
9411       break;
9412     }
9413 
9414     case NestedNameSpecifier::Namespace: {
9415       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9416       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9417       break;
9418     }
9419 
9420     case NestedNameSpecifier::NamespaceAlias: {
9421       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9422       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9423       break;
9424     }
9425 
9426     case NestedNameSpecifier::TypeSpec:
9427     case NestedNameSpecifier::TypeSpecWithTemplate: {
9428       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9429       if (!T)
9430         return nullptr;
9431 
9432       bool Template = Record[Idx++];
9433       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9434       break;
9435     }
9436 
9437     case NestedNameSpecifier::Global:
9438       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9439       // No associated value, and there can't be a prefix.
9440       break;
9441 
9442     case NestedNameSpecifier::Super: {
9443       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9444       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9445       break;
9446     }
9447     }
9448     Prev = NNS;
9449   }
9450   return NNS;
9451 }
9452 
9453 NestedNameSpecifierLoc
9454 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9455                                       unsigned &Idx) {
9456   ASTContext &Context = getContext();
9457   unsigned N = Record[Idx++];
9458   NestedNameSpecifierLocBuilder Builder;
9459   for (unsigned I = 0; I != N; ++I) {
9460     NestedNameSpecifier::SpecifierKind Kind
9461       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9462     switch (Kind) {
9463     case NestedNameSpecifier::Identifier: {
9464       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9465       SourceRange Range = ReadSourceRange(F, Record, Idx);
9466       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9467       break;
9468     }
9469 
9470     case NestedNameSpecifier::Namespace: {
9471       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9472       SourceRange Range = ReadSourceRange(F, Record, Idx);
9473       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9474       break;
9475     }
9476 
9477     case NestedNameSpecifier::NamespaceAlias: {
9478       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9479       SourceRange Range = ReadSourceRange(F, Record, Idx);
9480       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9481       break;
9482     }
9483 
9484     case NestedNameSpecifier::TypeSpec:
9485     case NestedNameSpecifier::TypeSpecWithTemplate: {
9486       bool Template = Record[Idx++];
9487       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9488       if (!T)
9489         return NestedNameSpecifierLoc();
9490       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9491 
9492       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9493       Builder.Extend(Context,
9494                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9495                      T->getTypeLoc(), ColonColonLoc);
9496       break;
9497     }
9498 
9499     case NestedNameSpecifier::Global: {
9500       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9501       Builder.MakeGlobal(Context, ColonColonLoc);
9502       break;
9503     }
9504 
9505     case NestedNameSpecifier::Super: {
9506       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9507       SourceRange Range = ReadSourceRange(F, Record, Idx);
9508       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9509       break;
9510     }
9511     }
9512   }
9513 
9514   return Builder.getWithLocInContext(Context);
9515 }
9516 
9517 SourceRange
9518 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9519                            unsigned &Idx) {
9520   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9521   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9522   return SourceRange(beg, end);
9523 }
9524 
9525 static FixedPointSemantics
9526 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9527                         unsigned &Idx) {
9528   unsigned Width = Record[Idx++];
9529   unsigned Scale = Record[Idx++];
9530   uint64_t Tmp = Record[Idx++];
9531   bool IsSigned = Tmp & 0x1;
9532   bool IsSaturated = Tmp & 0x2;
9533   bool HasUnsignedPadding = Tmp & 0x4;
9534   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9535                              HasUnsignedPadding);
9536 }
9537 
9538 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9539   unsigned Kind = Record[Idx++];
9540   switch (Kind) {
9541   case APValue::None:
9542     return APValue();
9543   case APValue::Indeterminate:
9544     return APValue::IndeterminateValue();
9545   case APValue::Int:
9546     return APValue(ReadAPSInt(Record, Idx));
9547   case APValue::Float: {
9548     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9549         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9550     return APValue(ReadAPFloat(Record, FloatSema, Idx));
9551   }
9552   case APValue::FixedPoint: {
9553     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9554     return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9555   }
9556   case APValue::ComplexInt: {
9557     llvm::APSInt First = ReadAPSInt(Record, Idx);
9558     return APValue(std::move(First), ReadAPSInt(Record, Idx));
9559   }
9560   case APValue::ComplexFloat: {
9561     const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9562         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9563     llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9564     const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9565         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9566     return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9567   }
9568   case APValue::LValue:
9569   case APValue::Vector:
9570   case APValue::Array:
9571   case APValue::Struct:
9572   case APValue::Union:
9573   case APValue::MemberPointer:
9574   case APValue::AddrLabelDiff:
9575     // TODO : Handle all these APValue::ValueKind.
9576     return APValue();
9577   }
9578   llvm_unreachable("Invalid APValue::ValueKind");
9579 }
9580 
9581 /// Read an integral value
9582 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9583   unsigned BitWidth = Record[Idx++];
9584   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9585   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9586   Idx += NumWords;
9587   return Result;
9588 }
9589 
9590 /// Read a signed integral value
9591 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9592   bool isUnsigned = Record[Idx++];
9593   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9594 }
9595 
9596 /// Read a floating-point value
9597 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9598                                      const llvm::fltSemantics &Sem,
9599                                      unsigned &Idx) {
9600   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9601 }
9602 
9603 // Read a string
9604 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9605   unsigned Len = Record[Idx++];
9606   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9607   Idx += Len;
9608   return Result;
9609 }
9610 
9611 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9612                                 unsigned &Idx) {
9613   std::string Filename = ReadString(Record, Idx);
9614   ResolveImportedPath(F, Filename);
9615   return Filename;
9616 }
9617 
9618 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9619                                 const RecordData &Record, unsigned &Idx) {
9620   std::string Filename = ReadString(Record, Idx);
9621   if (!BaseDirectory.empty())
9622     ResolveImportedPath(Filename, BaseDirectory);
9623   return Filename;
9624 }
9625 
9626 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9627                                          unsigned &Idx) {
9628   unsigned Major = Record[Idx++];
9629   unsigned Minor = Record[Idx++];
9630   unsigned Subminor = Record[Idx++];
9631   if (Minor == 0)
9632     return VersionTuple(Major);
9633   if (Subminor == 0)
9634     return VersionTuple(Major, Minor - 1);
9635   return VersionTuple(Major, Minor - 1, Subminor - 1);
9636 }
9637 
9638 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9639                                           const RecordData &Record,
9640                                           unsigned &Idx) {
9641   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9642   return CXXTemporary::Create(getContext(), Decl);
9643 }
9644 
9645 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9646   return Diag(CurrentImportLoc, DiagID);
9647 }
9648 
9649 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9650   return Diags.Report(Loc, DiagID);
9651 }
9652 
9653 /// Retrieve the identifier table associated with the
9654 /// preprocessor.
9655 IdentifierTable &ASTReader::getIdentifierTable() {
9656   return PP.getIdentifierTable();
9657 }
9658 
9659 /// Record that the given ID maps to the given switch-case
9660 /// statement.
9661 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9662   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9663          "Already have a SwitchCase with this ID");
9664   (*CurrSwitchCaseStmts)[ID] = SC;
9665 }
9666 
9667 /// Retrieve the switch-case statement with the given ID.
9668 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9669   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9670   return (*CurrSwitchCaseStmts)[ID];
9671 }
9672 
9673 void ASTReader::ClearSwitchCaseIDs() {
9674   CurrSwitchCaseStmts->clear();
9675 }
9676 
9677 void ASTReader::ReadComments() {
9678   ASTContext &Context = getContext();
9679   std::vector<RawComment *> Comments;
9680   for (SmallVectorImpl<std::pair<BitstreamCursor,
9681                                  serialization::ModuleFile *>>::iterator
9682        I = CommentsCursors.begin(),
9683        E = CommentsCursors.end();
9684        I != E; ++I) {
9685     Comments.clear();
9686     BitstreamCursor &Cursor = I->first;
9687     serialization::ModuleFile &F = *I->second;
9688     SavedStreamPosition SavedPosition(Cursor);
9689 
9690     RecordData Record;
9691     while (true) {
9692       Expected<llvm::BitstreamEntry> MaybeEntry =
9693           Cursor.advanceSkippingSubblocks(
9694               BitstreamCursor::AF_DontPopBlockAtEnd);
9695       if (!MaybeEntry) {
9696         Error(MaybeEntry.takeError());
9697         return;
9698       }
9699       llvm::BitstreamEntry Entry = MaybeEntry.get();
9700 
9701       switch (Entry.Kind) {
9702       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9703       case llvm::BitstreamEntry::Error:
9704         Error("malformed block record in AST file");
9705         return;
9706       case llvm::BitstreamEntry::EndBlock:
9707         goto NextCursor;
9708       case llvm::BitstreamEntry::Record:
9709         // The interesting case.
9710         break;
9711       }
9712 
9713       // Read a record.
9714       Record.clear();
9715       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9716       if (!MaybeComment) {
9717         Error(MaybeComment.takeError());
9718         return;
9719       }
9720       switch ((CommentRecordTypes)MaybeComment.get()) {
9721       case COMMENTS_RAW_COMMENT: {
9722         unsigned Idx = 0;
9723         SourceRange SR = ReadSourceRange(F, Record, Idx);
9724         RawComment::CommentKind Kind =
9725             (RawComment::CommentKind) Record[Idx++];
9726         bool IsTrailingComment = Record[Idx++];
9727         bool IsAlmostTrailingComment = Record[Idx++];
9728         Comments.push_back(new (Context) RawComment(
9729             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9730         break;
9731       }
9732       }
9733     }
9734   NextCursor:
9735     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9736         FileToOffsetToComment;
9737     for (RawComment *C : Comments) {
9738       SourceLocation CommentLoc = C->getBeginLoc();
9739       if (CommentLoc.isValid()) {
9740         std::pair<FileID, unsigned> Loc =
9741             SourceMgr.getDecomposedLoc(CommentLoc);
9742         if (Loc.first.isValid())
9743           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9744       }
9745     }
9746   }
9747 }
9748 
9749 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9750                                 bool IncludeSystem, bool Complain,
9751                     llvm::function_ref<void(const serialization::InputFile &IF,
9752                                             bool isSystem)> Visitor) {
9753   unsigned NumUserInputs = MF.NumUserInputFiles;
9754   unsigned NumInputs = MF.InputFilesLoaded.size();
9755   assert(NumUserInputs <= NumInputs);
9756   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9757   for (unsigned I = 0; I < N; ++I) {
9758     bool IsSystem = I >= NumUserInputs;
9759     InputFile IF = getInputFile(MF, I+1, Complain);
9760     Visitor(IF, IsSystem);
9761   }
9762 }
9763 
9764 void ASTReader::visitTopLevelModuleMaps(
9765     serialization::ModuleFile &MF,
9766     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9767   unsigned NumInputs = MF.InputFilesLoaded.size();
9768   for (unsigned I = 0; I < NumInputs; ++I) {
9769     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9770     if (IFI.TopLevelModuleMap)
9771       // FIXME: This unnecessarily re-reads the InputFileInfo.
9772       if (auto *FE = getInputFile(MF, I + 1).getFile())
9773         Visitor(FE);
9774   }
9775 }
9776 
9777 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9778   // If we know the owning module, use it.
9779   if (Module *M = D->getImportedOwningModule())
9780     return M->getFullModuleName();
9781 
9782   // Otherwise, use the name of the top-level module the decl is within.
9783   if (ModuleFile *M = getOwningModuleFile(D))
9784     return M->ModuleName;
9785 
9786   // Not from a module.
9787   return {};
9788 }
9789 
9790 void ASTReader::finishPendingActions() {
9791   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9792          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9793          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9794          !PendingUpdateRecords.empty()) {
9795     // If any identifiers with corresponding top-level declarations have
9796     // been loaded, load those declarations now.
9797     using TopLevelDeclsMap =
9798         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9799     TopLevelDeclsMap TopLevelDecls;
9800 
9801     while (!PendingIdentifierInfos.empty()) {
9802       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9803       SmallVector<uint32_t, 4> DeclIDs =
9804           std::move(PendingIdentifierInfos.back().second);
9805       PendingIdentifierInfos.pop_back();
9806 
9807       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9808     }
9809 
9810     // Load each function type that we deferred loading because it was a
9811     // deduced type that might refer to a local type declared within itself.
9812     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9813       auto *FD = PendingFunctionTypes[I].first;
9814       FD->setType(GetType(PendingFunctionTypes[I].second));
9815 
9816       // If we gave a function a deduced return type, remember that we need to
9817       // propagate that along the redeclaration chain.
9818       auto *DT = FD->getReturnType()->getContainedDeducedType();
9819       if (DT && DT->isDeduced())
9820         PendingDeducedTypeUpdates.insert(
9821             {FD->getCanonicalDecl(), FD->getReturnType()});
9822     }
9823     PendingFunctionTypes.clear();
9824 
9825     // For each decl chain that we wanted to complete while deserializing, mark
9826     // it as "still needs to be completed".
9827     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9828       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9829     }
9830     PendingIncompleteDeclChains.clear();
9831 
9832     // Load pending declaration chains.
9833     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9834       loadPendingDeclChain(PendingDeclChains[I].first,
9835                            PendingDeclChains[I].second);
9836     PendingDeclChains.clear();
9837 
9838     // Make the most recent of the top-level declarations visible.
9839     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9840            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9841       IdentifierInfo *II = TLD->first;
9842       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9843         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9844       }
9845     }
9846 
9847     // Load any pending macro definitions.
9848     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9849       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9850       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9851       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9852       // Initialize the macro history from chained-PCHs ahead of module imports.
9853       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9854            ++IDIdx) {
9855         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9856         if (!Info.M->isModule())
9857           resolvePendingMacro(II, Info);
9858       }
9859       // Handle module imports.
9860       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9861            ++IDIdx) {
9862         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9863         if (Info.M->isModule())
9864           resolvePendingMacro(II, Info);
9865       }
9866     }
9867     PendingMacroIDs.clear();
9868 
9869     // Wire up the DeclContexts for Decls that we delayed setting until
9870     // recursive loading is completed.
9871     while (!PendingDeclContextInfos.empty()) {
9872       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9873       PendingDeclContextInfos.pop_front();
9874       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9875       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9876       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9877     }
9878 
9879     // Perform any pending declaration updates.
9880     while (!PendingUpdateRecords.empty()) {
9881       auto Update = PendingUpdateRecords.pop_back_val();
9882       ReadingKindTracker ReadingKind(Read_Decl, *this);
9883       loadDeclUpdateRecords(Update);
9884     }
9885   }
9886 
9887   // At this point, all update records for loaded decls are in place, so any
9888   // fake class definitions should have become real.
9889   assert(PendingFakeDefinitionData.empty() &&
9890          "faked up a class definition but never saw the real one");
9891 
9892   // If we deserialized any C++ or Objective-C class definitions, any
9893   // Objective-C protocol definitions, or any redeclarable templates, make sure
9894   // that all redeclarations point to the definitions. Note that this can only
9895   // happen now, after the redeclaration chains have been fully wired.
9896   for (Decl *D : PendingDefinitions) {
9897     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9898       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9899         // Make sure that the TagType points at the definition.
9900         const_cast<TagType*>(TagT)->decl = TD;
9901       }
9902 
9903       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9904         for (auto *R = getMostRecentExistingDecl(RD); R;
9905              R = R->getPreviousDecl()) {
9906           assert((R == D) ==
9907                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9908                  "declaration thinks it's the definition but it isn't");
9909           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9910         }
9911       }
9912 
9913       continue;
9914     }
9915 
9916     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9917       // Make sure that the ObjCInterfaceType points at the definition.
9918       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9919         ->Decl = ID;
9920 
9921       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9922         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9923 
9924       continue;
9925     }
9926 
9927     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9928       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9929         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9930 
9931       continue;
9932     }
9933 
9934     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9935     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9936       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9937   }
9938   PendingDefinitions.clear();
9939 
9940   // Load the bodies of any functions or methods we've encountered. We do
9941   // this now (delayed) so that we can be sure that the declaration chains
9942   // have been fully wired up (hasBody relies on this).
9943   // FIXME: We shouldn't require complete redeclaration chains here.
9944   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9945                                PBEnd = PendingBodies.end();
9946        PB != PBEnd; ++PB) {
9947     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9948       // For a function defined inline within a class template, force the
9949       // canonical definition to be the one inside the canonical definition of
9950       // the template. This ensures that we instantiate from a correct view
9951       // of the template.
9952       //
9953       // Sadly we can't do this more generally: we can't be sure that all
9954       // copies of an arbitrary class definition will have the same members
9955       // defined (eg, some member functions may not be instantiated, and some
9956       // special members may or may not have been implicitly defined).
9957       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9958         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9959           continue;
9960 
9961       // FIXME: Check for =delete/=default?
9962       // FIXME: Complain about ODR violations here?
9963       const FunctionDecl *Defn = nullptr;
9964       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9965         FD->setLazyBody(PB->second);
9966       } else {
9967         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9968         mergeDefinitionVisibility(NonConstDefn, FD);
9969 
9970         if (!FD->isLateTemplateParsed() &&
9971             !NonConstDefn->isLateTemplateParsed() &&
9972             FD->getODRHash() != NonConstDefn->getODRHash()) {
9973           if (!isa<CXXMethodDecl>(FD)) {
9974             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9975           } else if (FD->getLexicalParent()->isFileContext() &&
9976                      NonConstDefn->getLexicalParent()->isFileContext()) {
9977             // Only diagnose out-of-line method definitions.  If they are
9978             // in class definitions, then an error will be generated when
9979             // processing the class bodies.
9980             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9981           }
9982         }
9983       }
9984       continue;
9985     }
9986 
9987     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9988     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9989       MD->setLazyBody(PB->second);
9990   }
9991   PendingBodies.clear();
9992 
9993   // Do some cleanup.
9994   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9995     getContext().deduplicateMergedDefinitonsFor(ND);
9996   PendingMergedDefinitionsToDeduplicate.clear();
9997 }
9998 
9999 void ASTReader::diagnoseOdrViolations() {
10000   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10001       PendingFunctionOdrMergeFailures.empty() &&
10002       PendingEnumOdrMergeFailures.empty())
10003     return;
10004 
10005   // Trigger the import of the full definition of each class that had any
10006   // odr-merging problems, so we can produce better diagnostics for them.
10007   // These updates may in turn find and diagnose some ODR failures, so take
10008   // ownership of the set first.
10009   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10010   PendingOdrMergeFailures.clear();
10011   for (auto &Merge : OdrMergeFailures) {
10012     Merge.first->buildLookup();
10013     Merge.first->decls_begin();
10014     Merge.first->bases_begin();
10015     Merge.first->vbases_begin();
10016     for (auto &RecordPair : Merge.second) {
10017       auto *RD = RecordPair.first;
10018       RD->decls_begin();
10019       RD->bases_begin();
10020       RD->vbases_begin();
10021     }
10022   }
10023 
10024   // Trigger the import of functions.
10025   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10026   PendingFunctionOdrMergeFailures.clear();
10027   for (auto &Merge : FunctionOdrMergeFailures) {
10028     Merge.first->buildLookup();
10029     Merge.first->decls_begin();
10030     Merge.first->getBody();
10031     for (auto &FD : Merge.second) {
10032       FD->buildLookup();
10033       FD->decls_begin();
10034       FD->getBody();
10035     }
10036   }
10037 
10038   // Trigger the import of enums.
10039   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10040   PendingEnumOdrMergeFailures.clear();
10041   for (auto &Merge : EnumOdrMergeFailures) {
10042     Merge.first->decls_begin();
10043     for (auto &Enum : Merge.second) {
10044       Enum->decls_begin();
10045     }
10046   }
10047 
10048   // For each declaration from a merged context, check that the canonical
10049   // definition of that context also contains a declaration of the same
10050   // entity.
10051   //
10052   // Caution: this loop does things that might invalidate iterators into
10053   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10054   while (!PendingOdrMergeChecks.empty()) {
10055     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10056 
10057     // FIXME: Skip over implicit declarations for now. This matters for things
10058     // like implicitly-declared special member functions. This isn't entirely
10059     // correct; we can end up with multiple unmerged declarations of the same
10060     // implicit entity.
10061     if (D->isImplicit())
10062       continue;
10063 
10064     DeclContext *CanonDef = D->getDeclContext();
10065 
10066     bool Found = false;
10067     const Decl *DCanon = D->getCanonicalDecl();
10068 
10069     for (auto RI : D->redecls()) {
10070       if (RI->getLexicalDeclContext() == CanonDef) {
10071         Found = true;
10072         break;
10073       }
10074     }
10075     if (Found)
10076       continue;
10077 
10078     // Quick check failed, time to do the slow thing. Note, we can't just
10079     // look up the name of D in CanonDef here, because the member that is
10080     // in CanonDef might not be found by name lookup (it might have been
10081     // replaced by a more recent declaration in the lookup table), and we
10082     // can't necessarily find it in the redeclaration chain because it might
10083     // be merely mergeable, not redeclarable.
10084     llvm::SmallVector<const NamedDecl*, 4> Candidates;
10085     for (auto *CanonMember : CanonDef->decls()) {
10086       if (CanonMember->getCanonicalDecl() == DCanon) {
10087         // This can happen if the declaration is merely mergeable and not
10088         // actually redeclarable (we looked for redeclarations earlier).
10089         //
10090         // FIXME: We should be able to detect this more efficiently, without
10091         // pulling in all of the members of CanonDef.
10092         Found = true;
10093         break;
10094       }
10095       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10096         if (ND->getDeclName() == D->getDeclName())
10097           Candidates.push_back(ND);
10098     }
10099 
10100     if (!Found) {
10101       // The AST doesn't like TagDecls becoming invalid after they've been
10102       // completed. We only really need to mark FieldDecls as invalid here.
10103       if (!isa<TagDecl>(D))
10104         D->setInvalidDecl();
10105 
10106       // Ensure we don't accidentally recursively enter deserialization while
10107       // we're producing our diagnostic.
10108       Deserializing RecursionGuard(this);
10109 
10110       std::string CanonDefModule =
10111           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10112       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10113         << D << getOwningModuleNameForDiagnostic(D)
10114         << CanonDef << CanonDefModule.empty() << CanonDefModule;
10115 
10116       if (Candidates.empty())
10117         Diag(cast<Decl>(CanonDef)->getLocation(),
10118              diag::note_module_odr_violation_no_possible_decls) << D;
10119       else {
10120         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10121           Diag(Candidates[I]->getLocation(),
10122                diag::note_module_odr_violation_possible_decl)
10123             << Candidates[I];
10124       }
10125 
10126       DiagnosedOdrMergeFailures.insert(CanonDef);
10127     }
10128   }
10129 
10130   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10131       EnumOdrMergeFailures.empty())
10132     return;
10133 
10134   // Ensure we don't accidentally recursively enter deserialization while
10135   // we're producing our diagnostics.
10136   Deserializing RecursionGuard(this);
10137 
10138   // Common code for hashing helpers.
10139   ODRHash Hash;
10140   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10141     Hash.clear();
10142     Hash.AddQualType(Ty);
10143     return Hash.CalculateHash();
10144   };
10145 
10146   auto ComputeODRHash = [&Hash](const Stmt *S) {
10147     assert(S);
10148     Hash.clear();
10149     Hash.AddStmt(S);
10150     return Hash.CalculateHash();
10151   };
10152 
10153   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10154     assert(D);
10155     Hash.clear();
10156     Hash.AddSubDecl(D);
10157     return Hash.CalculateHash();
10158   };
10159 
10160   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10161     Hash.clear();
10162     Hash.AddTemplateArgument(TA);
10163     return Hash.CalculateHash();
10164   };
10165 
10166   auto ComputeTemplateParameterListODRHash =
10167       [&Hash](const TemplateParameterList *TPL) {
10168         assert(TPL);
10169         Hash.clear();
10170         Hash.AddTemplateParameterList(TPL);
10171         return Hash.CalculateHash();
10172       };
10173 
10174   // Issue any pending ODR-failure diagnostics.
10175   for (auto &Merge : OdrMergeFailures) {
10176     // If we've already pointed out a specific problem with this class, don't
10177     // bother issuing a general "something's different" diagnostic.
10178     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10179       continue;
10180 
10181     bool Diagnosed = false;
10182     CXXRecordDecl *FirstRecord = Merge.first;
10183     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10184     for (auto &RecordPair : Merge.second) {
10185       CXXRecordDecl *SecondRecord = RecordPair.first;
10186       // Multiple different declarations got merged together; tell the user
10187       // where they came from.
10188       if (FirstRecord == SecondRecord)
10189         continue;
10190 
10191       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10192 
10193       auto *FirstDD = FirstRecord->DefinitionData;
10194       auto *SecondDD = RecordPair.second;
10195 
10196       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10197 
10198       // Diagnostics from DefinitionData are emitted here.
10199       if (FirstDD != SecondDD) {
10200         enum ODRDefinitionDataDifference {
10201           NumBases,
10202           NumVBases,
10203           BaseType,
10204           BaseVirtual,
10205           BaseAccess,
10206         };
10207         auto ODRDiagError = [FirstRecord, &FirstModule,
10208                              this](SourceLocation Loc, SourceRange Range,
10209                                    ODRDefinitionDataDifference DiffType) {
10210           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10211                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10212                  << DiffType;
10213         };
10214         auto ODRDiagNote = [&SecondModule,
10215                             this](SourceLocation Loc, SourceRange Range,
10216                                   ODRDefinitionDataDifference DiffType) {
10217           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10218                  << SecondModule << Range << DiffType;
10219         };
10220 
10221         unsigned FirstNumBases = FirstDD->NumBases;
10222         unsigned FirstNumVBases = FirstDD->NumVBases;
10223         unsigned SecondNumBases = SecondDD->NumBases;
10224         unsigned SecondNumVBases = SecondDD->NumVBases;
10225 
10226         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10227           unsigned NumBases = DD->NumBases;
10228           if (NumBases == 0) return SourceRange();
10229           auto bases = DD->bases();
10230           return SourceRange(bases[0].getBeginLoc(),
10231                              bases[NumBases - 1].getEndLoc());
10232         };
10233 
10234         if (FirstNumBases != SecondNumBases) {
10235           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10236                        NumBases)
10237               << FirstNumBases;
10238           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10239                       NumBases)
10240               << SecondNumBases;
10241           Diagnosed = true;
10242           break;
10243         }
10244 
10245         if (FirstNumVBases != SecondNumVBases) {
10246           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10247                        NumVBases)
10248               << FirstNumVBases;
10249           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10250                       NumVBases)
10251               << SecondNumVBases;
10252           Diagnosed = true;
10253           break;
10254         }
10255 
10256         auto FirstBases = FirstDD->bases();
10257         auto SecondBases = SecondDD->bases();
10258         unsigned i = 0;
10259         for (i = 0; i < FirstNumBases; ++i) {
10260           auto FirstBase = FirstBases[i];
10261           auto SecondBase = SecondBases[i];
10262           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10263               ComputeQualTypeODRHash(SecondBase.getType())) {
10264             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10265                          BaseType)
10266                 << (i + 1) << FirstBase.getType();
10267             ODRDiagNote(SecondRecord->getLocation(),
10268                         SecondBase.getSourceRange(), BaseType)
10269                 << (i + 1) << SecondBase.getType();
10270             break;
10271           }
10272 
10273           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10274             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10275                          BaseVirtual)
10276                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10277             ODRDiagNote(SecondRecord->getLocation(),
10278                         SecondBase.getSourceRange(), BaseVirtual)
10279                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10280             break;
10281           }
10282 
10283           if (FirstBase.getAccessSpecifierAsWritten() !=
10284               SecondBase.getAccessSpecifierAsWritten()) {
10285             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10286                          BaseAccess)
10287                 << (i + 1) << FirstBase.getType()
10288                 << (int)FirstBase.getAccessSpecifierAsWritten();
10289             ODRDiagNote(SecondRecord->getLocation(),
10290                         SecondBase.getSourceRange(), BaseAccess)
10291                 << (i + 1) << SecondBase.getType()
10292                 << (int)SecondBase.getAccessSpecifierAsWritten();
10293             break;
10294           }
10295         }
10296 
10297         if (i != FirstNumBases) {
10298           Diagnosed = true;
10299           break;
10300         }
10301       }
10302 
10303       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10304 
10305       const ClassTemplateDecl *FirstTemplate =
10306           FirstRecord->getDescribedClassTemplate();
10307       const ClassTemplateDecl *SecondTemplate =
10308           SecondRecord->getDescribedClassTemplate();
10309 
10310       assert(!FirstTemplate == !SecondTemplate &&
10311              "Both pointers should be null or non-null");
10312 
10313       enum ODRTemplateDifference {
10314         ParamEmptyName,
10315         ParamName,
10316         ParamSingleDefaultArgument,
10317         ParamDifferentDefaultArgument,
10318       };
10319 
10320       if (FirstTemplate && SecondTemplate) {
10321         DeclHashes FirstTemplateHashes;
10322         DeclHashes SecondTemplateHashes;
10323 
10324         auto PopulateTemplateParameterHashs =
10325             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10326                                      const ClassTemplateDecl *TD) {
10327               for (auto *D : TD->getTemplateParameters()->asArray()) {
10328                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10329               }
10330             };
10331 
10332         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10333         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10334 
10335         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10336                "Number of template parameters should be equal.");
10337 
10338         auto FirstIt = FirstTemplateHashes.begin();
10339         auto FirstEnd = FirstTemplateHashes.end();
10340         auto SecondIt = SecondTemplateHashes.begin();
10341         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10342           if (FirstIt->second == SecondIt->second)
10343             continue;
10344 
10345           auto ODRDiagError = [FirstRecord, &FirstModule,
10346                                this](SourceLocation Loc, SourceRange Range,
10347                                      ODRTemplateDifference DiffType) {
10348             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10349                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10350                    << DiffType;
10351           };
10352           auto ODRDiagNote = [&SecondModule,
10353                               this](SourceLocation Loc, SourceRange Range,
10354                                     ODRTemplateDifference DiffType) {
10355             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10356                    << SecondModule << Range << DiffType;
10357           };
10358 
10359           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10360           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10361 
10362           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10363                  "Parameter Decl's should be the same kind.");
10364 
10365           DeclarationName FirstName = FirstDecl->getDeclName();
10366           DeclarationName SecondName = SecondDecl->getDeclName();
10367 
10368           if (FirstName != SecondName) {
10369             const bool FirstNameEmpty =
10370                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10371             const bool SecondNameEmpty =
10372                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10373             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10374                    "Both template parameters cannot be unnamed.");
10375             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10376                          FirstNameEmpty ? ParamEmptyName : ParamName)
10377                 << FirstName;
10378             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10379                         SecondNameEmpty ? ParamEmptyName : ParamName)
10380                 << SecondName;
10381             break;
10382           }
10383 
10384           switch (FirstDecl->getKind()) {
10385           default:
10386             llvm_unreachable("Invalid template parameter type.");
10387           case Decl::TemplateTypeParm: {
10388             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10389             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10390             const bool HasFirstDefaultArgument =
10391                 FirstParam->hasDefaultArgument() &&
10392                 !FirstParam->defaultArgumentWasInherited();
10393             const bool HasSecondDefaultArgument =
10394                 SecondParam->hasDefaultArgument() &&
10395                 !SecondParam->defaultArgumentWasInherited();
10396 
10397             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10398               ODRDiagError(FirstDecl->getLocation(),
10399                            FirstDecl->getSourceRange(),
10400                            ParamSingleDefaultArgument)
10401                   << HasFirstDefaultArgument;
10402               ODRDiagNote(SecondDecl->getLocation(),
10403                           SecondDecl->getSourceRange(),
10404                           ParamSingleDefaultArgument)
10405                   << HasSecondDefaultArgument;
10406               break;
10407             }
10408 
10409             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10410                    "Expecting default arguments.");
10411 
10412             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10413                          ParamDifferentDefaultArgument);
10414             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10415                         ParamDifferentDefaultArgument);
10416 
10417             break;
10418           }
10419           case Decl::NonTypeTemplateParm: {
10420             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10421             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10422             const bool HasFirstDefaultArgument =
10423                 FirstParam->hasDefaultArgument() &&
10424                 !FirstParam->defaultArgumentWasInherited();
10425             const bool HasSecondDefaultArgument =
10426                 SecondParam->hasDefaultArgument() &&
10427                 !SecondParam->defaultArgumentWasInherited();
10428 
10429             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10430               ODRDiagError(FirstDecl->getLocation(),
10431                            FirstDecl->getSourceRange(),
10432                            ParamSingleDefaultArgument)
10433                   << HasFirstDefaultArgument;
10434               ODRDiagNote(SecondDecl->getLocation(),
10435                           SecondDecl->getSourceRange(),
10436                           ParamSingleDefaultArgument)
10437                   << HasSecondDefaultArgument;
10438               break;
10439             }
10440 
10441             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10442                    "Expecting default arguments.");
10443 
10444             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10445                          ParamDifferentDefaultArgument);
10446             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10447                         ParamDifferentDefaultArgument);
10448 
10449             break;
10450           }
10451           case Decl::TemplateTemplateParm: {
10452             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10453             const auto *SecondParam =
10454                 cast<TemplateTemplateParmDecl>(SecondDecl);
10455             const bool HasFirstDefaultArgument =
10456                 FirstParam->hasDefaultArgument() &&
10457                 !FirstParam->defaultArgumentWasInherited();
10458             const bool HasSecondDefaultArgument =
10459                 SecondParam->hasDefaultArgument() &&
10460                 !SecondParam->defaultArgumentWasInherited();
10461 
10462             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10463               ODRDiagError(FirstDecl->getLocation(),
10464                            FirstDecl->getSourceRange(),
10465                            ParamSingleDefaultArgument)
10466                   << HasFirstDefaultArgument;
10467               ODRDiagNote(SecondDecl->getLocation(),
10468                           SecondDecl->getSourceRange(),
10469                           ParamSingleDefaultArgument)
10470                   << HasSecondDefaultArgument;
10471               break;
10472             }
10473 
10474             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10475                    "Expecting default arguments.");
10476 
10477             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10478                          ParamDifferentDefaultArgument);
10479             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10480                         ParamDifferentDefaultArgument);
10481 
10482             break;
10483           }
10484           }
10485 
10486           break;
10487         }
10488 
10489         if (FirstIt != FirstEnd) {
10490           Diagnosed = true;
10491           break;
10492         }
10493       }
10494 
10495       DeclHashes FirstHashes;
10496       DeclHashes SecondHashes;
10497 
10498       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10499                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
10500         for (auto *D : Record->decls()) {
10501           // Due to decl merging, the first CXXRecordDecl is the parent of
10502           // Decls in both records.
10503           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10504             continue;
10505           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10506         }
10507       };
10508       PopulateHashes(FirstHashes, FirstRecord);
10509       PopulateHashes(SecondHashes, SecondRecord);
10510 
10511       // Used with err_module_odr_violation_mismatch_decl and
10512       // note_module_odr_violation_mismatch_decl
10513       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10514       enum {
10515         EndOfClass,
10516         PublicSpecifer,
10517         PrivateSpecifer,
10518         ProtectedSpecifer,
10519         StaticAssert,
10520         Field,
10521         CXXMethod,
10522         TypeAlias,
10523         TypeDef,
10524         Var,
10525         Friend,
10526         FunctionTemplate,
10527         Other
10528       } FirstDiffType = Other,
10529         SecondDiffType = Other;
10530 
10531       auto DifferenceSelector = [](Decl *D) {
10532         assert(D && "valid Decl required");
10533         switch (D->getKind()) {
10534         default:
10535           return Other;
10536         case Decl::AccessSpec:
10537           switch (D->getAccess()) {
10538           case AS_public:
10539             return PublicSpecifer;
10540           case AS_private:
10541             return PrivateSpecifer;
10542           case AS_protected:
10543             return ProtectedSpecifer;
10544           case AS_none:
10545             break;
10546           }
10547           llvm_unreachable("Invalid access specifier");
10548         case Decl::StaticAssert:
10549           return StaticAssert;
10550         case Decl::Field:
10551           return Field;
10552         case Decl::CXXMethod:
10553         case Decl::CXXConstructor:
10554         case Decl::CXXDestructor:
10555           return CXXMethod;
10556         case Decl::TypeAlias:
10557           return TypeAlias;
10558         case Decl::Typedef:
10559           return TypeDef;
10560         case Decl::Var:
10561           return Var;
10562         case Decl::Friend:
10563           return Friend;
10564         case Decl::FunctionTemplate:
10565           return FunctionTemplate;
10566         }
10567       };
10568 
10569       Decl *FirstDecl = nullptr;
10570       Decl *SecondDecl = nullptr;
10571       auto FirstIt = FirstHashes.begin();
10572       auto SecondIt = SecondHashes.begin();
10573 
10574       // If there is a diagnoseable difference, FirstDiffType and
10575       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10576       // filled in if not EndOfClass.
10577       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10578         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10579             FirstIt->second == SecondIt->second) {
10580           ++FirstIt;
10581           ++SecondIt;
10582           continue;
10583         }
10584 
10585         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10586         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10587 
10588         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10589         SecondDiffType =
10590             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10591 
10592         break;
10593       }
10594 
10595       if (FirstDiffType == Other || SecondDiffType == Other) {
10596         // Reaching this point means an unexpected Decl was encountered
10597         // or no difference was detected.  This causes a generic error
10598         // message to be emitted.
10599         Diag(FirstRecord->getLocation(),
10600              diag::err_module_odr_violation_different_definitions)
10601             << FirstRecord << FirstModule.empty() << FirstModule;
10602 
10603         if (FirstDecl) {
10604           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10605               << FirstRecord << FirstDecl->getSourceRange();
10606         }
10607 
10608         Diag(SecondRecord->getLocation(),
10609              diag::note_module_odr_violation_different_definitions)
10610             << SecondModule;
10611 
10612         if (SecondDecl) {
10613           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10614               << SecondDecl->getSourceRange();
10615         }
10616 
10617         Diagnosed = true;
10618         break;
10619       }
10620 
10621       if (FirstDiffType != SecondDiffType) {
10622         SourceLocation FirstLoc;
10623         SourceRange FirstRange;
10624         if (FirstDiffType == EndOfClass) {
10625           FirstLoc = FirstRecord->getBraceRange().getEnd();
10626         } else {
10627           FirstLoc = FirstIt->first->getLocation();
10628           FirstRange = FirstIt->first->getSourceRange();
10629         }
10630         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10631             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10632             << FirstDiffType;
10633 
10634         SourceLocation SecondLoc;
10635         SourceRange SecondRange;
10636         if (SecondDiffType == EndOfClass) {
10637           SecondLoc = SecondRecord->getBraceRange().getEnd();
10638         } else {
10639           SecondLoc = SecondDecl->getLocation();
10640           SecondRange = SecondDecl->getSourceRange();
10641         }
10642         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10643             << SecondModule << SecondRange << SecondDiffType;
10644         Diagnosed = true;
10645         break;
10646       }
10647 
10648       assert(FirstDiffType == SecondDiffType);
10649 
10650       // Used with err_module_odr_violation_mismatch_decl_diff and
10651       // note_module_odr_violation_mismatch_decl_diff
10652       enum ODRDeclDifference {
10653         StaticAssertCondition,
10654         StaticAssertMessage,
10655         StaticAssertOnlyMessage,
10656         FieldName,
10657         FieldTypeName,
10658         FieldSingleBitField,
10659         FieldDifferentWidthBitField,
10660         FieldSingleMutable,
10661         FieldSingleInitializer,
10662         FieldDifferentInitializers,
10663         MethodName,
10664         MethodDeleted,
10665         MethodDefaulted,
10666         MethodVirtual,
10667         MethodStatic,
10668         MethodVolatile,
10669         MethodConst,
10670         MethodInline,
10671         MethodNumberParameters,
10672         MethodParameterType,
10673         MethodParameterName,
10674         MethodParameterSingleDefaultArgument,
10675         MethodParameterDifferentDefaultArgument,
10676         MethodNoTemplateArguments,
10677         MethodDifferentNumberTemplateArguments,
10678         MethodDifferentTemplateArgument,
10679         MethodSingleBody,
10680         MethodDifferentBody,
10681         TypedefName,
10682         TypedefType,
10683         VarName,
10684         VarType,
10685         VarSingleInitializer,
10686         VarDifferentInitializer,
10687         VarConstexpr,
10688         FriendTypeFunction,
10689         FriendType,
10690         FriendFunction,
10691         FunctionTemplateDifferentNumberParameters,
10692         FunctionTemplateParameterDifferentKind,
10693         FunctionTemplateParameterName,
10694         FunctionTemplateParameterSingleDefaultArgument,
10695         FunctionTemplateParameterDifferentDefaultArgument,
10696         FunctionTemplateParameterDifferentType,
10697         FunctionTemplatePackParameter,
10698       };
10699 
10700       // These lambdas have the common portions of the ODR diagnostics.  This
10701       // has the same return as Diag(), so addition parameters can be passed
10702       // in with operator<<
10703       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10704           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10705         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10706                << FirstRecord << FirstModule.empty() << FirstModule << Range
10707                << DiffType;
10708       };
10709       auto ODRDiagNote = [&SecondModule, this](
10710           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10711         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10712                << SecondModule << Range << DiffType;
10713       };
10714 
10715       switch (FirstDiffType) {
10716       case Other:
10717       case EndOfClass:
10718       case PublicSpecifer:
10719       case PrivateSpecifer:
10720       case ProtectedSpecifer:
10721         llvm_unreachable("Invalid diff type");
10722 
10723       case StaticAssert: {
10724         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10725         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10726 
10727         Expr *FirstExpr = FirstSA->getAssertExpr();
10728         Expr *SecondExpr = SecondSA->getAssertExpr();
10729         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10730         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10731         if (FirstODRHash != SecondODRHash) {
10732           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10733                        StaticAssertCondition);
10734           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10735                       StaticAssertCondition);
10736           Diagnosed = true;
10737           break;
10738         }
10739 
10740         StringLiteral *FirstStr = FirstSA->getMessage();
10741         StringLiteral *SecondStr = SecondSA->getMessage();
10742         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10743         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10744           SourceLocation FirstLoc, SecondLoc;
10745           SourceRange FirstRange, SecondRange;
10746           if (FirstStr) {
10747             FirstLoc = FirstStr->getBeginLoc();
10748             FirstRange = FirstStr->getSourceRange();
10749           } else {
10750             FirstLoc = FirstSA->getBeginLoc();
10751             FirstRange = FirstSA->getSourceRange();
10752           }
10753           if (SecondStr) {
10754             SecondLoc = SecondStr->getBeginLoc();
10755             SecondRange = SecondStr->getSourceRange();
10756           } else {
10757             SecondLoc = SecondSA->getBeginLoc();
10758             SecondRange = SecondSA->getSourceRange();
10759           }
10760           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10761               << (FirstStr == nullptr);
10762           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10763               << (SecondStr == nullptr);
10764           Diagnosed = true;
10765           break;
10766         }
10767 
10768         if (FirstStr && SecondStr &&
10769             FirstStr->getString() != SecondStr->getString()) {
10770           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10771                        StaticAssertMessage);
10772           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10773                       StaticAssertMessage);
10774           Diagnosed = true;
10775           break;
10776         }
10777         break;
10778       }
10779       case Field: {
10780         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10781         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10782         IdentifierInfo *FirstII = FirstField->getIdentifier();
10783         IdentifierInfo *SecondII = SecondField->getIdentifier();
10784         if (FirstII->getName() != SecondII->getName()) {
10785           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10786                        FieldName)
10787               << FirstII;
10788           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10789                       FieldName)
10790               << SecondII;
10791 
10792           Diagnosed = true;
10793           break;
10794         }
10795 
10796         assert(getContext().hasSameType(FirstField->getType(),
10797                                         SecondField->getType()));
10798 
10799         QualType FirstType = FirstField->getType();
10800         QualType SecondType = SecondField->getType();
10801         if (ComputeQualTypeODRHash(FirstType) !=
10802             ComputeQualTypeODRHash(SecondType)) {
10803           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10804                        FieldTypeName)
10805               << FirstII << FirstType;
10806           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10807                       FieldTypeName)
10808               << SecondII << SecondType;
10809 
10810           Diagnosed = true;
10811           break;
10812         }
10813 
10814         const bool IsFirstBitField = FirstField->isBitField();
10815         const bool IsSecondBitField = SecondField->isBitField();
10816         if (IsFirstBitField != IsSecondBitField) {
10817           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10818                        FieldSingleBitField)
10819               << FirstII << IsFirstBitField;
10820           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10821                       FieldSingleBitField)
10822               << SecondII << IsSecondBitField;
10823           Diagnosed = true;
10824           break;
10825         }
10826 
10827         if (IsFirstBitField && IsSecondBitField) {
10828           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10829                        FieldDifferentWidthBitField)
10830               << FirstII << FirstField->getBitWidth()->getSourceRange();
10831           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10832                       FieldDifferentWidthBitField)
10833               << SecondII << SecondField->getBitWidth()->getSourceRange();
10834           Diagnosed = true;
10835           break;
10836         }
10837 
10838         const bool IsFirstMutable = FirstField->isMutable();
10839         const bool IsSecondMutable = SecondField->isMutable();
10840         if (IsFirstMutable != IsSecondMutable) {
10841           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10842                        FieldSingleMutable)
10843               << FirstII << IsFirstMutable;
10844           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10845                       FieldSingleMutable)
10846               << SecondII << IsSecondMutable;
10847           Diagnosed = true;
10848           break;
10849         }
10850 
10851         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10852         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10853         if ((!FirstInitializer && SecondInitializer) ||
10854             (FirstInitializer && !SecondInitializer)) {
10855           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10856                        FieldSingleInitializer)
10857               << FirstII << (FirstInitializer != nullptr);
10858           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10859                       FieldSingleInitializer)
10860               << SecondII << (SecondInitializer != nullptr);
10861           Diagnosed = true;
10862           break;
10863         }
10864 
10865         if (FirstInitializer && SecondInitializer) {
10866           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10867           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10868           if (FirstInitHash != SecondInitHash) {
10869             ODRDiagError(FirstField->getLocation(),
10870                          FirstField->getSourceRange(),
10871                          FieldDifferentInitializers)
10872                 << FirstII << FirstInitializer->getSourceRange();
10873             ODRDiagNote(SecondField->getLocation(),
10874                         SecondField->getSourceRange(),
10875                         FieldDifferentInitializers)
10876                 << SecondII << SecondInitializer->getSourceRange();
10877             Diagnosed = true;
10878             break;
10879           }
10880         }
10881 
10882         break;
10883       }
10884       case CXXMethod: {
10885         enum {
10886           DiagMethod,
10887           DiagConstructor,
10888           DiagDestructor,
10889         } FirstMethodType,
10890             SecondMethodType;
10891         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10892           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10893           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10894           return DiagMethod;
10895         };
10896         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10897         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10898         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10899         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10900         auto FirstName = FirstMethod->getDeclName();
10901         auto SecondName = SecondMethod->getDeclName();
10902         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10903           ODRDiagError(FirstMethod->getLocation(),
10904                        FirstMethod->getSourceRange(), MethodName)
10905               << FirstMethodType << FirstName;
10906           ODRDiagNote(SecondMethod->getLocation(),
10907                       SecondMethod->getSourceRange(), MethodName)
10908               << SecondMethodType << SecondName;
10909 
10910           Diagnosed = true;
10911           break;
10912         }
10913 
10914         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10915         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10916         if (FirstDeleted != SecondDeleted) {
10917           ODRDiagError(FirstMethod->getLocation(),
10918                        FirstMethod->getSourceRange(), MethodDeleted)
10919               << FirstMethodType << FirstName << FirstDeleted;
10920 
10921           ODRDiagNote(SecondMethod->getLocation(),
10922                       SecondMethod->getSourceRange(), MethodDeleted)
10923               << SecondMethodType << SecondName << SecondDeleted;
10924           Diagnosed = true;
10925           break;
10926         }
10927 
10928         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10929         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10930         if (FirstDefaulted != SecondDefaulted) {
10931           ODRDiagError(FirstMethod->getLocation(),
10932                        FirstMethod->getSourceRange(), MethodDefaulted)
10933               << FirstMethodType << FirstName << FirstDefaulted;
10934 
10935           ODRDiagNote(SecondMethod->getLocation(),
10936                       SecondMethod->getSourceRange(), MethodDefaulted)
10937               << SecondMethodType << SecondName << SecondDefaulted;
10938           Diagnosed = true;
10939           break;
10940         }
10941 
10942         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10943         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10944         const bool FirstPure = FirstMethod->isPure();
10945         const bool SecondPure = SecondMethod->isPure();
10946         if ((FirstVirtual || SecondVirtual) &&
10947             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10948           ODRDiagError(FirstMethod->getLocation(),
10949                        FirstMethod->getSourceRange(), MethodVirtual)
10950               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10951           ODRDiagNote(SecondMethod->getLocation(),
10952                       SecondMethod->getSourceRange(), MethodVirtual)
10953               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10954           Diagnosed = true;
10955           break;
10956         }
10957 
10958         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10959         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10960         // class needs to be checked instead.
10961         const auto FirstStorage = FirstMethod->getStorageClass();
10962         const auto SecondStorage = SecondMethod->getStorageClass();
10963         const bool FirstStatic = FirstStorage == SC_Static;
10964         const bool SecondStatic = SecondStorage == SC_Static;
10965         if (FirstStatic != SecondStatic) {
10966           ODRDiagError(FirstMethod->getLocation(),
10967                        FirstMethod->getSourceRange(), MethodStatic)
10968               << FirstMethodType << FirstName << FirstStatic;
10969           ODRDiagNote(SecondMethod->getLocation(),
10970                       SecondMethod->getSourceRange(), MethodStatic)
10971               << SecondMethodType << SecondName << SecondStatic;
10972           Diagnosed = true;
10973           break;
10974         }
10975 
10976         const bool FirstVolatile = FirstMethod->isVolatile();
10977         const bool SecondVolatile = SecondMethod->isVolatile();
10978         if (FirstVolatile != SecondVolatile) {
10979           ODRDiagError(FirstMethod->getLocation(),
10980                        FirstMethod->getSourceRange(), MethodVolatile)
10981               << FirstMethodType << FirstName << FirstVolatile;
10982           ODRDiagNote(SecondMethod->getLocation(),
10983                       SecondMethod->getSourceRange(), MethodVolatile)
10984               << SecondMethodType << SecondName << SecondVolatile;
10985           Diagnosed = true;
10986           break;
10987         }
10988 
10989         const bool FirstConst = FirstMethod->isConst();
10990         const bool SecondConst = SecondMethod->isConst();
10991         if (FirstConst != SecondConst) {
10992           ODRDiagError(FirstMethod->getLocation(),
10993                        FirstMethod->getSourceRange(), MethodConst)
10994               << FirstMethodType << FirstName << FirstConst;
10995           ODRDiagNote(SecondMethod->getLocation(),
10996                       SecondMethod->getSourceRange(), MethodConst)
10997               << SecondMethodType << SecondName << SecondConst;
10998           Diagnosed = true;
10999           break;
11000         }
11001 
11002         const bool FirstInline = FirstMethod->isInlineSpecified();
11003         const bool SecondInline = SecondMethod->isInlineSpecified();
11004         if (FirstInline != SecondInline) {
11005           ODRDiagError(FirstMethod->getLocation(),
11006                        FirstMethod->getSourceRange(), MethodInline)
11007               << FirstMethodType << FirstName << FirstInline;
11008           ODRDiagNote(SecondMethod->getLocation(),
11009                       SecondMethod->getSourceRange(), MethodInline)
11010               << SecondMethodType << SecondName << SecondInline;
11011           Diagnosed = true;
11012           break;
11013         }
11014 
11015         const unsigned FirstNumParameters = FirstMethod->param_size();
11016         const unsigned SecondNumParameters = SecondMethod->param_size();
11017         if (FirstNumParameters != SecondNumParameters) {
11018           ODRDiagError(FirstMethod->getLocation(),
11019                        FirstMethod->getSourceRange(), MethodNumberParameters)
11020               << FirstMethodType << FirstName << FirstNumParameters;
11021           ODRDiagNote(SecondMethod->getLocation(),
11022                       SecondMethod->getSourceRange(), MethodNumberParameters)
11023               << SecondMethodType << SecondName << SecondNumParameters;
11024           Diagnosed = true;
11025           break;
11026         }
11027 
11028         // Need this status boolean to know when break out of the switch.
11029         bool ParameterMismatch = false;
11030         for (unsigned I = 0; I < FirstNumParameters; ++I) {
11031           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11032           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11033 
11034           QualType FirstParamType = FirstParam->getType();
11035           QualType SecondParamType = SecondParam->getType();
11036           if (FirstParamType != SecondParamType &&
11037               ComputeQualTypeODRHash(FirstParamType) !=
11038                   ComputeQualTypeODRHash(SecondParamType)) {
11039             if (const DecayedType *ParamDecayedType =
11040                     FirstParamType->getAs<DecayedType>()) {
11041               ODRDiagError(FirstMethod->getLocation(),
11042                            FirstMethod->getSourceRange(), MethodParameterType)
11043                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11044                   << true << ParamDecayedType->getOriginalType();
11045             } else {
11046               ODRDiagError(FirstMethod->getLocation(),
11047                            FirstMethod->getSourceRange(), MethodParameterType)
11048                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11049                   << false;
11050             }
11051 
11052             if (const DecayedType *ParamDecayedType =
11053                     SecondParamType->getAs<DecayedType>()) {
11054               ODRDiagNote(SecondMethod->getLocation(),
11055                           SecondMethod->getSourceRange(), MethodParameterType)
11056                   << SecondMethodType << SecondName << (I + 1)
11057                   << SecondParamType << true
11058                   << ParamDecayedType->getOriginalType();
11059             } else {
11060               ODRDiagNote(SecondMethod->getLocation(),
11061                           SecondMethod->getSourceRange(), MethodParameterType)
11062                   << SecondMethodType << SecondName << (I + 1)
11063                   << SecondParamType << false;
11064             }
11065             ParameterMismatch = true;
11066             break;
11067           }
11068 
11069           DeclarationName FirstParamName = FirstParam->getDeclName();
11070           DeclarationName SecondParamName = SecondParam->getDeclName();
11071           if (FirstParamName != SecondParamName) {
11072             ODRDiagError(FirstMethod->getLocation(),
11073                          FirstMethod->getSourceRange(), MethodParameterName)
11074                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11075             ODRDiagNote(SecondMethod->getLocation(),
11076                         SecondMethod->getSourceRange(), MethodParameterName)
11077                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11078             ParameterMismatch = true;
11079             break;
11080           }
11081 
11082           const Expr *FirstInit = FirstParam->getInit();
11083           const Expr *SecondInit = SecondParam->getInit();
11084           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11085             ODRDiagError(FirstMethod->getLocation(),
11086                          FirstMethod->getSourceRange(),
11087                          MethodParameterSingleDefaultArgument)
11088                 << FirstMethodType << FirstName << (I + 1)
11089                 << (FirstInit == nullptr)
11090                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11091             ODRDiagNote(SecondMethod->getLocation(),
11092                         SecondMethod->getSourceRange(),
11093                         MethodParameterSingleDefaultArgument)
11094                 << SecondMethodType << SecondName << (I + 1)
11095                 << (SecondInit == nullptr)
11096                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11097             ParameterMismatch = true;
11098             break;
11099           }
11100 
11101           if (FirstInit && SecondInit &&
11102               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11103             ODRDiagError(FirstMethod->getLocation(),
11104                          FirstMethod->getSourceRange(),
11105                          MethodParameterDifferentDefaultArgument)
11106                 << FirstMethodType << FirstName << (I + 1)
11107                 << FirstInit->getSourceRange();
11108             ODRDiagNote(SecondMethod->getLocation(),
11109                         SecondMethod->getSourceRange(),
11110                         MethodParameterDifferentDefaultArgument)
11111                 << SecondMethodType << SecondName << (I + 1)
11112                 << SecondInit->getSourceRange();
11113             ParameterMismatch = true;
11114             break;
11115 
11116           }
11117         }
11118 
11119         if (ParameterMismatch) {
11120           Diagnosed = true;
11121           break;
11122         }
11123 
11124         const auto *FirstTemplateArgs =
11125             FirstMethod->getTemplateSpecializationArgs();
11126         const auto *SecondTemplateArgs =
11127             SecondMethod->getTemplateSpecializationArgs();
11128 
11129         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11130             (!FirstTemplateArgs && SecondTemplateArgs)) {
11131           ODRDiagError(FirstMethod->getLocation(),
11132                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11133               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11134           ODRDiagNote(SecondMethod->getLocation(),
11135                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11136               << SecondMethodType << SecondName
11137               << (SecondTemplateArgs != nullptr);
11138 
11139           Diagnosed = true;
11140           break;
11141         }
11142 
11143         if (FirstTemplateArgs && SecondTemplateArgs) {
11144           // Remove pack expansions from argument list.
11145           auto ExpandTemplateArgumentList =
11146               [](const TemplateArgumentList *TAL) {
11147                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11148                 for (const TemplateArgument &TA : TAL->asArray()) {
11149                   if (TA.getKind() != TemplateArgument::Pack) {
11150                     ExpandedList.push_back(&TA);
11151                     continue;
11152                   }
11153                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11154                     ExpandedList.push_back(&PackTA);
11155                   }
11156                 }
11157                 return ExpandedList;
11158               };
11159           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11160               ExpandTemplateArgumentList(FirstTemplateArgs);
11161           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11162               ExpandTemplateArgumentList(SecondTemplateArgs);
11163 
11164           if (FirstExpandedList.size() != SecondExpandedList.size()) {
11165             ODRDiagError(FirstMethod->getLocation(),
11166                          FirstMethod->getSourceRange(),
11167                          MethodDifferentNumberTemplateArguments)
11168                 << FirstMethodType << FirstName
11169                 << (unsigned)FirstExpandedList.size();
11170             ODRDiagNote(SecondMethod->getLocation(),
11171                         SecondMethod->getSourceRange(),
11172                         MethodDifferentNumberTemplateArguments)
11173                 << SecondMethodType << SecondName
11174                 << (unsigned)SecondExpandedList.size();
11175 
11176             Diagnosed = true;
11177             break;
11178           }
11179 
11180           bool TemplateArgumentMismatch = false;
11181           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11182             const TemplateArgument &FirstTA = *FirstExpandedList[i],
11183                                    &SecondTA = *SecondExpandedList[i];
11184             if (ComputeTemplateArgumentODRHash(FirstTA) ==
11185                 ComputeTemplateArgumentODRHash(SecondTA)) {
11186               continue;
11187             }
11188 
11189             ODRDiagError(FirstMethod->getLocation(),
11190                          FirstMethod->getSourceRange(),
11191                          MethodDifferentTemplateArgument)
11192                 << FirstMethodType << FirstName << FirstTA << i + 1;
11193             ODRDiagNote(SecondMethod->getLocation(),
11194                         SecondMethod->getSourceRange(),
11195                         MethodDifferentTemplateArgument)
11196                 << SecondMethodType << SecondName << SecondTA << i + 1;
11197 
11198             TemplateArgumentMismatch = true;
11199             break;
11200           }
11201 
11202           if (TemplateArgumentMismatch) {
11203             Diagnosed = true;
11204             break;
11205           }
11206         }
11207 
11208         // Compute the hash of the method as if it has no body.
11209         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11210           Hash.clear();
11211           Hash.AddFunctionDecl(D, true /*SkipBody*/);
11212           return Hash.CalculateHash();
11213         };
11214 
11215         // Compare the hash generated to the hash stored.  A difference means
11216         // that a body was present in the original source.  Due to merging,
11217         // the stardard way of detecting a body will not work.
11218         const bool HasFirstBody =
11219             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11220         const bool HasSecondBody =
11221             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11222 
11223         if (HasFirstBody != HasSecondBody) {
11224           ODRDiagError(FirstMethod->getLocation(),
11225                        FirstMethod->getSourceRange(), MethodSingleBody)
11226               << FirstMethodType << FirstName << HasFirstBody;
11227           ODRDiagNote(SecondMethod->getLocation(),
11228                       SecondMethod->getSourceRange(), MethodSingleBody)
11229               << SecondMethodType << SecondName << HasSecondBody;
11230           Diagnosed = true;
11231           break;
11232         }
11233 
11234         if (HasFirstBody && HasSecondBody) {
11235           ODRDiagError(FirstMethod->getLocation(),
11236                        FirstMethod->getSourceRange(), MethodDifferentBody)
11237               << FirstMethodType << FirstName;
11238           ODRDiagNote(SecondMethod->getLocation(),
11239                       SecondMethod->getSourceRange(), MethodDifferentBody)
11240               << SecondMethodType << SecondName;
11241           Diagnosed = true;
11242           break;
11243         }
11244 
11245         break;
11246       }
11247       case TypeAlias:
11248       case TypeDef: {
11249         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11250         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11251         auto FirstName = FirstTD->getDeclName();
11252         auto SecondName = SecondTD->getDeclName();
11253         if (FirstName != SecondName) {
11254           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11255                        TypedefName)
11256               << (FirstDiffType == TypeAlias) << FirstName;
11257           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11258                       TypedefName)
11259               << (FirstDiffType == TypeAlias) << SecondName;
11260           Diagnosed = true;
11261           break;
11262         }
11263 
11264         QualType FirstType = FirstTD->getUnderlyingType();
11265         QualType SecondType = SecondTD->getUnderlyingType();
11266         if (ComputeQualTypeODRHash(FirstType) !=
11267             ComputeQualTypeODRHash(SecondType)) {
11268           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11269                        TypedefType)
11270               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11271           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11272                       TypedefType)
11273               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11274           Diagnosed = true;
11275           break;
11276         }
11277         break;
11278       }
11279       case Var: {
11280         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11281         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11282         auto FirstName = FirstVD->getDeclName();
11283         auto SecondName = SecondVD->getDeclName();
11284         if (FirstName != SecondName) {
11285           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11286                        VarName)
11287               << FirstName;
11288           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11289                       VarName)
11290               << SecondName;
11291           Diagnosed = true;
11292           break;
11293         }
11294 
11295         QualType FirstType = FirstVD->getType();
11296         QualType SecondType = SecondVD->getType();
11297         if (ComputeQualTypeODRHash(FirstType) !=
11298                         ComputeQualTypeODRHash(SecondType)) {
11299           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11300                        VarType)
11301               << FirstName << FirstType;
11302           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11303                       VarType)
11304               << SecondName << SecondType;
11305           Diagnosed = true;
11306           break;
11307         }
11308 
11309         const Expr *FirstInit = FirstVD->getInit();
11310         const Expr *SecondInit = SecondVD->getInit();
11311         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11312           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11313                        VarSingleInitializer)
11314               << FirstName << (FirstInit == nullptr)
11315               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11316           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11317                       VarSingleInitializer)
11318               << SecondName << (SecondInit == nullptr)
11319               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11320           Diagnosed = true;
11321           break;
11322         }
11323 
11324         if (FirstInit && SecondInit &&
11325             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11326           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11327                        VarDifferentInitializer)
11328               << FirstName << FirstInit->getSourceRange();
11329           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11330                       VarDifferentInitializer)
11331               << SecondName << SecondInit->getSourceRange();
11332           Diagnosed = true;
11333           break;
11334         }
11335 
11336         const bool FirstIsConstexpr = FirstVD->isConstexpr();
11337         const bool SecondIsConstexpr = SecondVD->isConstexpr();
11338         if (FirstIsConstexpr != SecondIsConstexpr) {
11339           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11340                        VarConstexpr)
11341               << FirstName << FirstIsConstexpr;
11342           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11343                       VarConstexpr)
11344               << SecondName << SecondIsConstexpr;
11345           Diagnosed = true;
11346           break;
11347         }
11348         break;
11349       }
11350       case Friend: {
11351         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11352         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11353 
11354         NamedDecl *FirstND = FirstFriend->getFriendDecl();
11355         NamedDecl *SecondND = SecondFriend->getFriendDecl();
11356 
11357         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11358         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11359 
11360         if (FirstND && SecondND) {
11361           ODRDiagError(FirstFriend->getFriendLoc(),
11362                        FirstFriend->getSourceRange(), FriendFunction)
11363               << FirstND;
11364           ODRDiagNote(SecondFriend->getFriendLoc(),
11365                       SecondFriend->getSourceRange(), FriendFunction)
11366               << SecondND;
11367 
11368           Diagnosed = true;
11369           break;
11370         }
11371 
11372         if (FirstTSI && SecondTSI) {
11373           QualType FirstFriendType = FirstTSI->getType();
11374           QualType SecondFriendType = SecondTSI->getType();
11375           assert(ComputeQualTypeODRHash(FirstFriendType) !=
11376                  ComputeQualTypeODRHash(SecondFriendType));
11377           ODRDiagError(FirstFriend->getFriendLoc(),
11378                        FirstFriend->getSourceRange(), FriendType)
11379               << FirstFriendType;
11380           ODRDiagNote(SecondFriend->getFriendLoc(),
11381                       SecondFriend->getSourceRange(), FriendType)
11382               << SecondFriendType;
11383           Diagnosed = true;
11384           break;
11385         }
11386 
11387         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11388                      FriendTypeFunction)
11389             << (FirstTSI == nullptr);
11390         ODRDiagNote(SecondFriend->getFriendLoc(),
11391                     SecondFriend->getSourceRange(), FriendTypeFunction)
11392             << (SecondTSI == nullptr);
11393 
11394         Diagnosed = true;
11395         break;
11396       }
11397       case FunctionTemplate: {
11398         FunctionTemplateDecl *FirstTemplate =
11399             cast<FunctionTemplateDecl>(FirstDecl);
11400         FunctionTemplateDecl *SecondTemplate =
11401             cast<FunctionTemplateDecl>(SecondDecl);
11402 
11403         TemplateParameterList *FirstTPL =
11404             FirstTemplate->getTemplateParameters();
11405         TemplateParameterList *SecondTPL =
11406             SecondTemplate->getTemplateParameters();
11407 
11408         if (FirstTPL->size() != SecondTPL->size()) {
11409           ODRDiagError(FirstTemplate->getLocation(),
11410                        FirstTemplate->getSourceRange(),
11411                        FunctionTemplateDifferentNumberParameters)
11412               << FirstTemplate << FirstTPL->size();
11413           ODRDiagNote(SecondTemplate->getLocation(),
11414                       SecondTemplate->getSourceRange(),
11415                       FunctionTemplateDifferentNumberParameters)
11416               << SecondTemplate  << SecondTPL->size();
11417 
11418           Diagnosed = true;
11419           break;
11420         }
11421 
11422         bool ParameterMismatch = false;
11423         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11424           NamedDecl *FirstParam = FirstTPL->getParam(i);
11425           NamedDecl *SecondParam = SecondTPL->getParam(i);
11426 
11427           if (FirstParam->getKind() != SecondParam->getKind()) {
11428             enum {
11429               TemplateTypeParameter,
11430               NonTypeTemplateParameter,
11431               TemplateTemplateParameter,
11432             };
11433             auto GetParamType = [](NamedDecl *D) {
11434               switch (D->getKind()) {
11435                 default:
11436                   llvm_unreachable("Unexpected template parameter type");
11437                 case Decl::TemplateTypeParm:
11438                   return TemplateTypeParameter;
11439                 case Decl::NonTypeTemplateParm:
11440                   return NonTypeTemplateParameter;
11441                 case Decl::TemplateTemplateParm:
11442                   return TemplateTemplateParameter;
11443               }
11444             };
11445 
11446             ODRDiagError(FirstTemplate->getLocation(),
11447                          FirstTemplate->getSourceRange(),
11448                          FunctionTemplateParameterDifferentKind)
11449                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11450             ODRDiagNote(SecondTemplate->getLocation(),
11451                         SecondTemplate->getSourceRange(),
11452                         FunctionTemplateParameterDifferentKind)
11453                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11454 
11455             ParameterMismatch = true;
11456             break;
11457           }
11458 
11459           if (FirstParam->getName() != SecondParam->getName()) {
11460             ODRDiagError(FirstTemplate->getLocation(),
11461                          FirstTemplate->getSourceRange(),
11462                          FunctionTemplateParameterName)
11463                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11464                 << FirstParam;
11465             ODRDiagNote(SecondTemplate->getLocation(),
11466                         SecondTemplate->getSourceRange(),
11467                         FunctionTemplateParameterName)
11468                 << SecondTemplate << (i + 1)
11469                 << (bool)SecondParam->getIdentifier() << SecondParam;
11470             ParameterMismatch = true;
11471             break;
11472           }
11473 
11474           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11475               isa<TemplateTypeParmDecl>(SecondParam)) {
11476             TemplateTypeParmDecl *FirstTTPD =
11477                 cast<TemplateTypeParmDecl>(FirstParam);
11478             TemplateTypeParmDecl *SecondTTPD =
11479                 cast<TemplateTypeParmDecl>(SecondParam);
11480             bool HasFirstDefaultArgument =
11481                 FirstTTPD->hasDefaultArgument() &&
11482                 !FirstTTPD->defaultArgumentWasInherited();
11483             bool HasSecondDefaultArgument =
11484                 SecondTTPD->hasDefaultArgument() &&
11485                 !SecondTTPD->defaultArgumentWasInherited();
11486             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11487               ODRDiagError(FirstTemplate->getLocation(),
11488                            FirstTemplate->getSourceRange(),
11489                            FunctionTemplateParameterSingleDefaultArgument)
11490                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11491               ODRDiagNote(SecondTemplate->getLocation(),
11492                           SecondTemplate->getSourceRange(),
11493                           FunctionTemplateParameterSingleDefaultArgument)
11494                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11495               ParameterMismatch = true;
11496               break;
11497             }
11498 
11499             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11500               QualType FirstType = FirstTTPD->getDefaultArgument();
11501               QualType SecondType = SecondTTPD->getDefaultArgument();
11502               if (ComputeQualTypeODRHash(FirstType) !=
11503                   ComputeQualTypeODRHash(SecondType)) {
11504                 ODRDiagError(FirstTemplate->getLocation(),
11505                              FirstTemplate->getSourceRange(),
11506                              FunctionTemplateParameterDifferentDefaultArgument)
11507                     << FirstTemplate << (i + 1) << FirstType;
11508                 ODRDiagNote(SecondTemplate->getLocation(),
11509                             SecondTemplate->getSourceRange(),
11510                             FunctionTemplateParameterDifferentDefaultArgument)
11511                     << SecondTemplate << (i + 1) << SecondType;
11512                 ParameterMismatch = true;
11513                 break;
11514               }
11515             }
11516 
11517             if (FirstTTPD->isParameterPack() !=
11518                 SecondTTPD->isParameterPack()) {
11519               ODRDiagError(FirstTemplate->getLocation(),
11520                            FirstTemplate->getSourceRange(),
11521                            FunctionTemplatePackParameter)
11522                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11523               ODRDiagNote(SecondTemplate->getLocation(),
11524                           SecondTemplate->getSourceRange(),
11525                           FunctionTemplatePackParameter)
11526                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11527               ParameterMismatch = true;
11528               break;
11529             }
11530           }
11531 
11532           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11533               isa<TemplateTemplateParmDecl>(SecondParam)) {
11534             TemplateTemplateParmDecl *FirstTTPD =
11535                 cast<TemplateTemplateParmDecl>(FirstParam);
11536             TemplateTemplateParmDecl *SecondTTPD =
11537                 cast<TemplateTemplateParmDecl>(SecondParam);
11538 
11539             TemplateParameterList *FirstTPL =
11540                 FirstTTPD->getTemplateParameters();
11541             TemplateParameterList *SecondTPL =
11542                 SecondTTPD->getTemplateParameters();
11543 
11544             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11545                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11546               ODRDiagError(FirstTemplate->getLocation(),
11547                            FirstTemplate->getSourceRange(),
11548                            FunctionTemplateParameterDifferentType)
11549                   << FirstTemplate << (i + 1);
11550               ODRDiagNote(SecondTemplate->getLocation(),
11551                           SecondTemplate->getSourceRange(),
11552                           FunctionTemplateParameterDifferentType)
11553                   << SecondTemplate << (i + 1);
11554               ParameterMismatch = true;
11555               break;
11556             }
11557 
11558             bool HasFirstDefaultArgument =
11559                 FirstTTPD->hasDefaultArgument() &&
11560                 !FirstTTPD->defaultArgumentWasInherited();
11561             bool HasSecondDefaultArgument =
11562                 SecondTTPD->hasDefaultArgument() &&
11563                 !SecondTTPD->defaultArgumentWasInherited();
11564             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11565               ODRDiagError(FirstTemplate->getLocation(),
11566                            FirstTemplate->getSourceRange(),
11567                            FunctionTemplateParameterSingleDefaultArgument)
11568                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11569               ODRDiagNote(SecondTemplate->getLocation(),
11570                           SecondTemplate->getSourceRange(),
11571                           FunctionTemplateParameterSingleDefaultArgument)
11572                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11573               ParameterMismatch = true;
11574               break;
11575             }
11576 
11577             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11578               TemplateArgument FirstTA =
11579                   FirstTTPD->getDefaultArgument().getArgument();
11580               TemplateArgument SecondTA =
11581                   SecondTTPD->getDefaultArgument().getArgument();
11582               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11583                   ComputeTemplateArgumentODRHash(SecondTA)) {
11584                 ODRDiagError(FirstTemplate->getLocation(),
11585                              FirstTemplate->getSourceRange(),
11586                              FunctionTemplateParameterDifferentDefaultArgument)
11587                     << FirstTemplate << (i + 1) << FirstTA;
11588                 ODRDiagNote(SecondTemplate->getLocation(),
11589                             SecondTemplate->getSourceRange(),
11590                             FunctionTemplateParameterDifferentDefaultArgument)
11591                     << SecondTemplate << (i + 1) << SecondTA;
11592                 ParameterMismatch = true;
11593                 break;
11594               }
11595             }
11596 
11597             if (FirstTTPD->isParameterPack() !=
11598                 SecondTTPD->isParameterPack()) {
11599               ODRDiagError(FirstTemplate->getLocation(),
11600                            FirstTemplate->getSourceRange(),
11601                            FunctionTemplatePackParameter)
11602                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11603               ODRDiagNote(SecondTemplate->getLocation(),
11604                           SecondTemplate->getSourceRange(),
11605                           FunctionTemplatePackParameter)
11606                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11607               ParameterMismatch = true;
11608               break;
11609             }
11610           }
11611 
11612           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11613               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11614             NonTypeTemplateParmDecl *FirstNTTPD =
11615                 cast<NonTypeTemplateParmDecl>(FirstParam);
11616             NonTypeTemplateParmDecl *SecondNTTPD =
11617                 cast<NonTypeTemplateParmDecl>(SecondParam);
11618 
11619             QualType FirstType = FirstNTTPD->getType();
11620             QualType SecondType = SecondNTTPD->getType();
11621             if (ComputeQualTypeODRHash(FirstType) !=
11622                 ComputeQualTypeODRHash(SecondType)) {
11623               ODRDiagError(FirstTemplate->getLocation(),
11624                            FirstTemplate->getSourceRange(),
11625                            FunctionTemplateParameterDifferentType)
11626                   << FirstTemplate << (i + 1);
11627               ODRDiagNote(SecondTemplate->getLocation(),
11628                           SecondTemplate->getSourceRange(),
11629                           FunctionTemplateParameterDifferentType)
11630                   << SecondTemplate << (i + 1);
11631               ParameterMismatch = true;
11632               break;
11633             }
11634 
11635             bool HasFirstDefaultArgument =
11636                 FirstNTTPD->hasDefaultArgument() &&
11637                 !FirstNTTPD->defaultArgumentWasInherited();
11638             bool HasSecondDefaultArgument =
11639                 SecondNTTPD->hasDefaultArgument() &&
11640                 !SecondNTTPD->defaultArgumentWasInherited();
11641             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11642               ODRDiagError(FirstTemplate->getLocation(),
11643                            FirstTemplate->getSourceRange(),
11644                            FunctionTemplateParameterSingleDefaultArgument)
11645                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11646               ODRDiagNote(SecondTemplate->getLocation(),
11647                           SecondTemplate->getSourceRange(),
11648                           FunctionTemplateParameterSingleDefaultArgument)
11649                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11650               ParameterMismatch = true;
11651               break;
11652             }
11653 
11654             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11655               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11656               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11657               if (ComputeODRHash(FirstDefaultArgument) !=
11658                   ComputeODRHash(SecondDefaultArgument)) {
11659                 ODRDiagError(FirstTemplate->getLocation(),
11660                              FirstTemplate->getSourceRange(),
11661                              FunctionTemplateParameterDifferentDefaultArgument)
11662                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11663                 ODRDiagNote(SecondTemplate->getLocation(),
11664                             SecondTemplate->getSourceRange(),
11665                             FunctionTemplateParameterDifferentDefaultArgument)
11666                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11667                 ParameterMismatch = true;
11668                 break;
11669               }
11670             }
11671 
11672             if (FirstNTTPD->isParameterPack() !=
11673                 SecondNTTPD->isParameterPack()) {
11674               ODRDiagError(FirstTemplate->getLocation(),
11675                            FirstTemplate->getSourceRange(),
11676                            FunctionTemplatePackParameter)
11677                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11678               ODRDiagNote(SecondTemplate->getLocation(),
11679                           SecondTemplate->getSourceRange(),
11680                           FunctionTemplatePackParameter)
11681                   << SecondTemplate << (i + 1)
11682                   << SecondNTTPD->isParameterPack();
11683               ParameterMismatch = true;
11684               break;
11685             }
11686           }
11687         }
11688 
11689         if (ParameterMismatch) {
11690           Diagnosed = true;
11691           break;
11692         }
11693 
11694         break;
11695       }
11696       }
11697 
11698       if (Diagnosed)
11699         continue;
11700 
11701       Diag(FirstDecl->getLocation(),
11702            diag::err_module_odr_violation_mismatch_decl_unknown)
11703           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11704           << FirstDecl->getSourceRange();
11705       Diag(SecondDecl->getLocation(),
11706            diag::note_module_odr_violation_mismatch_decl_unknown)
11707           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11708       Diagnosed = true;
11709     }
11710 
11711     if (!Diagnosed) {
11712       // All definitions are updates to the same declaration. This happens if a
11713       // module instantiates the declaration of a class template specialization
11714       // and two or more other modules instantiate its definition.
11715       //
11716       // FIXME: Indicate which modules had instantiations of this definition.
11717       // FIXME: How can this even happen?
11718       Diag(Merge.first->getLocation(),
11719            diag::err_module_odr_violation_different_instantiations)
11720         << Merge.first;
11721     }
11722   }
11723 
11724   // Issue ODR failures diagnostics for functions.
11725   for (auto &Merge : FunctionOdrMergeFailures) {
11726     enum ODRFunctionDifference {
11727       ReturnType,
11728       ParameterName,
11729       ParameterType,
11730       ParameterSingleDefaultArgument,
11731       ParameterDifferentDefaultArgument,
11732       FunctionBody,
11733     };
11734 
11735     FunctionDecl *FirstFunction = Merge.first;
11736     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11737 
11738     bool Diagnosed = false;
11739     for (auto &SecondFunction : Merge.second) {
11740 
11741       if (FirstFunction == SecondFunction)
11742         continue;
11743 
11744       std::string SecondModule =
11745           getOwningModuleNameForDiagnostic(SecondFunction);
11746 
11747       auto ODRDiagError = [FirstFunction, &FirstModule,
11748                            this](SourceLocation Loc, SourceRange Range,
11749                                  ODRFunctionDifference DiffType) {
11750         return Diag(Loc, diag::err_module_odr_violation_function)
11751                << FirstFunction << FirstModule.empty() << FirstModule << Range
11752                << DiffType;
11753       };
11754       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11755                                                SourceRange Range,
11756                                                ODRFunctionDifference DiffType) {
11757         return Diag(Loc, diag::note_module_odr_violation_function)
11758                << SecondModule << Range << DiffType;
11759       };
11760 
11761       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11762           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11763         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11764                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11765             << FirstFunction->getReturnType();
11766         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11767                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11768             << SecondFunction->getReturnType();
11769         Diagnosed = true;
11770         break;
11771       }
11772 
11773       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11774              "Merged functions with different number of parameters");
11775 
11776       auto ParamSize = FirstFunction->param_size();
11777       bool ParameterMismatch = false;
11778       for (unsigned I = 0; I < ParamSize; ++I) {
11779         auto *FirstParam = FirstFunction->getParamDecl(I);
11780         auto *SecondParam = SecondFunction->getParamDecl(I);
11781 
11782         assert(getContext().hasSameType(FirstParam->getType(),
11783                                       SecondParam->getType()) &&
11784                "Merged function has different parameter types.");
11785 
11786         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11787           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11788                        ParameterName)
11789               << I + 1 << FirstParam->getDeclName();
11790           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11791                       ParameterName)
11792               << I + 1 << SecondParam->getDeclName();
11793           ParameterMismatch = true;
11794           break;
11795         };
11796 
11797         QualType FirstParamType = FirstParam->getType();
11798         QualType SecondParamType = SecondParam->getType();
11799         if (FirstParamType != SecondParamType &&
11800             ComputeQualTypeODRHash(FirstParamType) !=
11801                 ComputeQualTypeODRHash(SecondParamType)) {
11802           if (const DecayedType *ParamDecayedType =
11803                   FirstParamType->getAs<DecayedType>()) {
11804             ODRDiagError(FirstParam->getLocation(),
11805                          FirstParam->getSourceRange(), ParameterType)
11806                 << (I + 1) << FirstParamType << true
11807                 << ParamDecayedType->getOriginalType();
11808           } else {
11809             ODRDiagError(FirstParam->getLocation(),
11810                          FirstParam->getSourceRange(), ParameterType)
11811                 << (I + 1) << FirstParamType << false;
11812           }
11813 
11814           if (const DecayedType *ParamDecayedType =
11815                   SecondParamType->getAs<DecayedType>()) {
11816             ODRDiagNote(SecondParam->getLocation(),
11817                         SecondParam->getSourceRange(), ParameterType)
11818                 << (I + 1) << SecondParamType << true
11819                 << ParamDecayedType->getOriginalType();
11820           } else {
11821             ODRDiagNote(SecondParam->getLocation(),
11822                         SecondParam->getSourceRange(), ParameterType)
11823                 << (I + 1) << SecondParamType << false;
11824           }
11825           ParameterMismatch = true;
11826           break;
11827         }
11828 
11829         const Expr *FirstInit = FirstParam->getInit();
11830         const Expr *SecondInit = SecondParam->getInit();
11831         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11832           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11833                        ParameterSingleDefaultArgument)
11834               << (I + 1) << (FirstInit == nullptr)
11835               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11836           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11837                       ParameterSingleDefaultArgument)
11838               << (I + 1) << (SecondInit == nullptr)
11839               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11840           ParameterMismatch = true;
11841           break;
11842         }
11843 
11844         if (FirstInit && SecondInit &&
11845             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11846           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11847                        ParameterDifferentDefaultArgument)
11848               << (I + 1) << FirstInit->getSourceRange();
11849           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11850                       ParameterDifferentDefaultArgument)
11851               << (I + 1) << SecondInit->getSourceRange();
11852           ParameterMismatch = true;
11853           break;
11854         }
11855 
11856         assert(ComputeSubDeclODRHash(FirstParam) ==
11857                    ComputeSubDeclODRHash(SecondParam) &&
11858                "Undiagnosed parameter difference.");
11859       }
11860 
11861       if (ParameterMismatch) {
11862         Diagnosed = true;
11863         break;
11864       }
11865 
11866       // If no error has been generated before now, assume the problem is in
11867       // the body and generate a message.
11868       ODRDiagError(FirstFunction->getLocation(),
11869                    FirstFunction->getSourceRange(), FunctionBody);
11870       ODRDiagNote(SecondFunction->getLocation(),
11871                   SecondFunction->getSourceRange(), FunctionBody);
11872       Diagnosed = true;
11873       break;
11874     }
11875     (void)Diagnosed;
11876     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11877   }
11878 
11879   // Issue ODR failures diagnostics for enums.
11880   for (auto &Merge : EnumOdrMergeFailures) {
11881     enum ODREnumDifference {
11882       SingleScopedEnum,
11883       EnumTagKeywordMismatch,
11884       SingleSpecifiedType,
11885       DifferentSpecifiedTypes,
11886       DifferentNumberEnumConstants,
11887       EnumConstantName,
11888       EnumConstantSingleInitilizer,
11889       EnumConstantDifferentInitilizer,
11890     };
11891 
11892     // If we've already pointed out a specific problem with this enum, don't
11893     // bother issuing a general "something's different" diagnostic.
11894     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11895       continue;
11896 
11897     EnumDecl *FirstEnum = Merge.first;
11898     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11899 
11900     using DeclHashes =
11901         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11902     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11903                               DeclHashes &Hashes, EnumDecl *Enum) {
11904       for (auto *D : Enum->decls()) {
11905         // Due to decl merging, the first EnumDecl is the parent of
11906         // Decls in both records.
11907         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11908           continue;
11909         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11910         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11911                             ComputeSubDeclODRHash(D));
11912       }
11913     };
11914     DeclHashes FirstHashes;
11915     PopulateHashes(FirstHashes, FirstEnum);
11916     bool Diagnosed = false;
11917     for (auto &SecondEnum : Merge.second) {
11918 
11919       if (FirstEnum == SecondEnum)
11920         continue;
11921 
11922       std::string SecondModule =
11923           getOwningModuleNameForDiagnostic(SecondEnum);
11924 
11925       auto ODRDiagError = [FirstEnum, &FirstModule,
11926                            this](SourceLocation Loc, SourceRange Range,
11927                                  ODREnumDifference DiffType) {
11928         return Diag(Loc, diag::err_module_odr_violation_enum)
11929                << FirstEnum << FirstModule.empty() << FirstModule << Range
11930                << DiffType;
11931       };
11932       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11933                                                SourceRange Range,
11934                                                ODREnumDifference DiffType) {
11935         return Diag(Loc, diag::note_module_odr_violation_enum)
11936                << SecondModule << Range << DiffType;
11937       };
11938 
11939       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11940         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11941                      SingleScopedEnum)
11942             << FirstEnum->isScoped();
11943         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11944                     SingleScopedEnum)
11945             << SecondEnum->isScoped();
11946         Diagnosed = true;
11947         continue;
11948       }
11949 
11950       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11951         if (FirstEnum->isScopedUsingClassTag() !=
11952             SecondEnum->isScopedUsingClassTag()) {
11953           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11954                        EnumTagKeywordMismatch)
11955               << FirstEnum->isScopedUsingClassTag();
11956           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11957                       EnumTagKeywordMismatch)
11958               << SecondEnum->isScopedUsingClassTag();
11959           Diagnosed = true;
11960           continue;
11961         }
11962       }
11963 
11964       QualType FirstUnderlyingType =
11965           FirstEnum->getIntegerTypeSourceInfo()
11966               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11967               : QualType();
11968       QualType SecondUnderlyingType =
11969           SecondEnum->getIntegerTypeSourceInfo()
11970               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11971               : QualType();
11972       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11973           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11974                        SingleSpecifiedType)
11975               << !FirstUnderlyingType.isNull();
11976           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11977                       SingleSpecifiedType)
11978               << !SecondUnderlyingType.isNull();
11979           Diagnosed = true;
11980           continue;
11981       }
11982 
11983       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11984         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11985             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11986           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11987                        DifferentSpecifiedTypes)
11988               << FirstUnderlyingType;
11989           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11990                       DifferentSpecifiedTypes)
11991               << SecondUnderlyingType;
11992           Diagnosed = true;
11993           continue;
11994         }
11995       }
11996 
11997       DeclHashes SecondHashes;
11998       PopulateHashes(SecondHashes, SecondEnum);
11999 
12000       if (FirstHashes.size() != SecondHashes.size()) {
12001         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12002                      DifferentNumberEnumConstants)
12003             << (int)FirstHashes.size();
12004         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12005                     DifferentNumberEnumConstants)
12006             << (int)SecondHashes.size();
12007         Diagnosed = true;
12008         continue;
12009       }
12010 
12011       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
12012         if (FirstHashes[I].second == SecondHashes[I].second)
12013           continue;
12014         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
12015         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
12016 
12017         if (FirstEnumConstant->getDeclName() !=
12018             SecondEnumConstant->getDeclName()) {
12019 
12020           ODRDiagError(FirstEnumConstant->getLocation(),
12021                        FirstEnumConstant->getSourceRange(), EnumConstantName)
12022               << I + 1 << FirstEnumConstant;
12023           ODRDiagNote(SecondEnumConstant->getLocation(),
12024                       SecondEnumConstant->getSourceRange(), EnumConstantName)
12025               << I + 1 << SecondEnumConstant;
12026           Diagnosed = true;
12027           break;
12028         }
12029 
12030         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12031         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12032         if (!FirstInit && !SecondInit)
12033           continue;
12034 
12035         if (!FirstInit || !SecondInit) {
12036           ODRDiagError(FirstEnumConstant->getLocation(),
12037                        FirstEnumConstant->getSourceRange(),
12038                        EnumConstantSingleInitilizer)
12039               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12040           ODRDiagNote(SecondEnumConstant->getLocation(),
12041                       SecondEnumConstant->getSourceRange(),
12042                       EnumConstantSingleInitilizer)
12043               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12044           Diagnosed = true;
12045           break;
12046         }
12047 
12048         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12049           ODRDiagError(FirstEnumConstant->getLocation(),
12050                        FirstEnumConstant->getSourceRange(),
12051                        EnumConstantDifferentInitilizer)
12052               << I + 1 << FirstEnumConstant;
12053           ODRDiagNote(SecondEnumConstant->getLocation(),
12054                       SecondEnumConstant->getSourceRange(),
12055                       EnumConstantDifferentInitilizer)
12056               << I + 1 << SecondEnumConstant;
12057           Diagnosed = true;
12058           break;
12059         }
12060       }
12061     }
12062 
12063     (void)Diagnosed;
12064     assert(Diagnosed && "Unable to emit ODR diagnostic.");
12065   }
12066 }
12067 
12068 void ASTReader::StartedDeserializing() {
12069   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12070     ReadTimer->startTimer();
12071 }
12072 
12073 void ASTReader::FinishedDeserializing() {
12074   assert(NumCurrentElementsDeserializing &&
12075          "FinishedDeserializing not paired with StartedDeserializing");
12076   if (NumCurrentElementsDeserializing == 1) {
12077     // We decrease NumCurrentElementsDeserializing only after pending actions
12078     // are finished, to avoid recursively re-calling finishPendingActions().
12079     finishPendingActions();
12080   }
12081   --NumCurrentElementsDeserializing;
12082 
12083   if (NumCurrentElementsDeserializing == 0) {
12084     // Propagate exception specification and deduced type updates along
12085     // redeclaration chains.
12086     //
12087     // We do this now rather than in finishPendingActions because we want to
12088     // be able to walk the complete redeclaration chains of the updated decls.
12089     while (!PendingExceptionSpecUpdates.empty() ||
12090            !PendingDeducedTypeUpdates.empty()) {
12091       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12092       PendingExceptionSpecUpdates.clear();
12093       for (auto Update : ESUpdates) {
12094         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12095         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12096         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12097         if (auto *Listener = getContext().getASTMutationListener())
12098           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12099         for (auto *Redecl : Update.second->redecls())
12100           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12101       }
12102 
12103       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12104       PendingDeducedTypeUpdates.clear();
12105       for (auto Update : DTUpdates) {
12106         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12107         // FIXME: If the return type is already deduced, check that it matches.
12108         getContext().adjustDeducedFunctionResultType(Update.first,
12109                                                      Update.second);
12110       }
12111     }
12112 
12113     if (ReadTimer)
12114       ReadTimer->stopTimer();
12115 
12116     diagnoseOdrViolations();
12117 
12118     // We are not in recursive loading, so it's safe to pass the "interesting"
12119     // decls to the consumer.
12120     if (Consumer)
12121       PassInterestingDeclsToConsumer();
12122   }
12123 }
12124 
12125 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12126   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12127     // Remove any fake results before adding any real ones.
12128     auto It = PendingFakeLookupResults.find(II);
12129     if (It != PendingFakeLookupResults.end()) {
12130       for (auto *ND : It->second)
12131         SemaObj->IdResolver.RemoveDecl(ND);
12132       // FIXME: this works around module+PCH performance issue.
12133       // Rather than erase the result from the map, which is O(n), just clear
12134       // the vector of NamedDecls.
12135       It->second.clear();
12136     }
12137   }
12138 
12139   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12140     SemaObj->TUScope->AddDecl(D);
12141   } else if (SemaObj->TUScope) {
12142     // Adding the decl to IdResolver may have failed because it was already in
12143     // (even though it was not added in scope). If it is already in, make sure
12144     // it gets in the scope as well.
12145     if (std::find(SemaObj->IdResolver.begin(Name),
12146                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12147       SemaObj->TUScope->AddDecl(D);
12148   }
12149 }
12150 
12151 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12152                      ASTContext *Context,
12153                      const PCHContainerReader &PCHContainerRdr,
12154                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12155                      StringRef isysroot, bool DisableValidation,
12156                      bool AllowASTWithCompilerErrors,
12157                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12158                      bool UseGlobalIndex,
12159                      std::unique_ptr<llvm::Timer> ReadTimer)
12160     : Listener(DisableValidation
12161                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12162                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12163       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12164       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12165       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12166                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
12167       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12168       DisableValidation(DisableValidation),
12169       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12170       AllowConfigurationMismatch(AllowConfigurationMismatch),
12171       ValidateSystemInputs(ValidateSystemInputs),
12172       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12173   SourceMgr.setExternalSLocEntrySource(this);
12174 
12175   for (const auto &Ext : Extensions) {
12176     auto BlockName = Ext->getExtensionMetadata().BlockName;
12177     auto Known = ModuleFileExtensions.find(BlockName);
12178     if (Known != ModuleFileExtensions.end()) {
12179       Diags.Report(diag::warn_duplicate_module_file_extension)
12180         << BlockName;
12181       continue;
12182     }
12183 
12184     ModuleFileExtensions.insert({BlockName, Ext});
12185   }
12186 }
12187 
12188 ASTReader::~ASTReader() {
12189   if (OwnsDeserializationListener)
12190     delete DeserializationListener;
12191 }
12192 
12193 IdentifierResolver &ASTReader::getIdResolver() {
12194   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12195 }
12196 
12197 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12198                                                unsigned AbbrevID) {
12199   Idx = 0;
12200   Record.clear();
12201   return Cursor.readRecord(AbbrevID, Record);
12202 }
12203 //===----------------------------------------------------------------------===//
12204 //// OMPClauseReader implementation
12205 ////===----------------------------------------------------------------------===//
12206 
12207 OMPClause *OMPClauseReader::readClause() {
12208   OMPClause *C;
12209   switch (Record.readInt()) {
12210   case OMPC_if:
12211     C = new (Context) OMPIfClause();
12212     break;
12213   case OMPC_final:
12214     C = new (Context) OMPFinalClause();
12215     break;
12216   case OMPC_num_threads:
12217     C = new (Context) OMPNumThreadsClause();
12218     break;
12219   case OMPC_safelen:
12220     C = new (Context) OMPSafelenClause();
12221     break;
12222   case OMPC_simdlen:
12223     C = new (Context) OMPSimdlenClause();
12224     break;
12225   case OMPC_allocator:
12226     C = new (Context) OMPAllocatorClause();
12227     break;
12228   case OMPC_collapse:
12229     C = new (Context) OMPCollapseClause();
12230     break;
12231   case OMPC_default:
12232     C = new (Context) OMPDefaultClause();
12233     break;
12234   case OMPC_proc_bind:
12235     C = new (Context) OMPProcBindClause();
12236     break;
12237   case OMPC_schedule:
12238     C = new (Context) OMPScheduleClause();
12239     break;
12240   case OMPC_ordered:
12241     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12242     break;
12243   case OMPC_nowait:
12244     C = new (Context) OMPNowaitClause();
12245     break;
12246   case OMPC_untied:
12247     C = new (Context) OMPUntiedClause();
12248     break;
12249   case OMPC_mergeable:
12250     C = new (Context) OMPMergeableClause();
12251     break;
12252   case OMPC_read:
12253     C = new (Context) OMPReadClause();
12254     break;
12255   case OMPC_write:
12256     C = new (Context) OMPWriteClause();
12257     break;
12258   case OMPC_update:
12259     C = new (Context) OMPUpdateClause();
12260     break;
12261   case OMPC_capture:
12262     C = new (Context) OMPCaptureClause();
12263     break;
12264   case OMPC_seq_cst:
12265     C = new (Context) OMPSeqCstClause();
12266     break;
12267   case OMPC_threads:
12268     C = new (Context) OMPThreadsClause();
12269     break;
12270   case OMPC_simd:
12271     C = new (Context) OMPSIMDClause();
12272     break;
12273   case OMPC_nogroup:
12274     C = new (Context) OMPNogroupClause();
12275     break;
12276   case OMPC_unified_address:
12277     C = new (Context) OMPUnifiedAddressClause();
12278     break;
12279   case OMPC_unified_shared_memory:
12280     C = new (Context) OMPUnifiedSharedMemoryClause();
12281     break;
12282   case OMPC_reverse_offload:
12283     C = new (Context) OMPReverseOffloadClause();
12284     break;
12285   case OMPC_dynamic_allocators:
12286     C = new (Context) OMPDynamicAllocatorsClause();
12287     break;
12288   case OMPC_atomic_default_mem_order:
12289     C = new (Context) OMPAtomicDefaultMemOrderClause();
12290     break;
12291  case OMPC_private:
12292     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12293     break;
12294   case OMPC_firstprivate:
12295     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12296     break;
12297   case OMPC_lastprivate:
12298     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12299     break;
12300   case OMPC_shared:
12301     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12302     break;
12303   case OMPC_reduction:
12304     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12305     break;
12306   case OMPC_task_reduction:
12307     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12308     break;
12309   case OMPC_in_reduction:
12310     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12311     break;
12312   case OMPC_linear:
12313     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12314     break;
12315   case OMPC_aligned:
12316     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12317     break;
12318   case OMPC_copyin:
12319     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12320     break;
12321   case OMPC_copyprivate:
12322     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12323     break;
12324   case OMPC_flush:
12325     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12326     break;
12327   case OMPC_depend: {
12328     unsigned NumVars = Record.readInt();
12329     unsigned NumLoops = Record.readInt();
12330     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12331     break;
12332   }
12333   case OMPC_device:
12334     C = new (Context) OMPDeviceClause();
12335     break;
12336   case OMPC_map: {
12337     OMPMappableExprListSizeTy Sizes;
12338     Sizes.NumVars = Record.readInt();
12339     Sizes.NumUniqueDeclarations = Record.readInt();
12340     Sizes.NumComponentLists = Record.readInt();
12341     Sizes.NumComponents = Record.readInt();
12342     C = OMPMapClause::CreateEmpty(Context, Sizes);
12343     break;
12344   }
12345   case OMPC_num_teams:
12346     C = new (Context) OMPNumTeamsClause();
12347     break;
12348   case OMPC_thread_limit:
12349     C = new (Context) OMPThreadLimitClause();
12350     break;
12351   case OMPC_priority:
12352     C = new (Context) OMPPriorityClause();
12353     break;
12354   case OMPC_grainsize:
12355     C = new (Context) OMPGrainsizeClause();
12356     break;
12357   case OMPC_num_tasks:
12358     C = new (Context) OMPNumTasksClause();
12359     break;
12360   case OMPC_hint:
12361     C = new (Context) OMPHintClause();
12362     break;
12363   case OMPC_dist_schedule:
12364     C = new (Context) OMPDistScheduleClause();
12365     break;
12366   case OMPC_defaultmap:
12367     C = new (Context) OMPDefaultmapClause();
12368     break;
12369   case OMPC_to: {
12370     OMPMappableExprListSizeTy Sizes;
12371     Sizes.NumVars = Record.readInt();
12372     Sizes.NumUniqueDeclarations = Record.readInt();
12373     Sizes.NumComponentLists = Record.readInt();
12374     Sizes.NumComponents = Record.readInt();
12375     C = OMPToClause::CreateEmpty(Context, Sizes);
12376     break;
12377   }
12378   case OMPC_from: {
12379     OMPMappableExprListSizeTy Sizes;
12380     Sizes.NumVars = Record.readInt();
12381     Sizes.NumUniqueDeclarations = Record.readInt();
12382     Sizes.NumComponentLists = Record.readInt();
12383     Sizes.NumComponents = Record.readInt();
12384     C = OMPFromClause::CreateEmpty(Context, Sizes);
12385     break;
12386   }
12387   case OMPC_use_device_ptr: {
12388     OMPMappableExprListSizeTy Sizes;
12389     Sizes.NumVars = Record.readInt();
12390     Sizes.NumUniqueDeclarations = Record.readInt();
12391     Sizes.NumComponentLists = Record.readInt();
12392     Sizes.NumComponents = Record.readInt();
12393     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12394     break;
12395   }
12396   case OMPC_is_device_ptr: {
12397     OMPMappableExprListSizeTy Sizes;
12398     Sizes.NumVars = Record.readInt();
12399     Sizes.NumUniqueDeclarations = Record.readInt();
12400     Sizes.NumComponentLists = Record.readInt();
12401     Sizes.NumComponents = Record.readInt();
12402     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12403     break;
12404   }
12405   case OMPC_allocate:
12406     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12407     break;
12408   }
12409   Visit(C);
12410   C->setLocStart(Record.readSourceLocation());
12411   C->setLocEnd(Record.readSourceLocation());
12412 
12413   return C;
12414 }
12415 
12416 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12417   C->setPreInitStmt(Record.readSubStmt(),
12418                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12419 }
12420 
12421 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12422   VisitOMPClauseWithPreInit(C);
12423   C->setPostUpdateExpr(Record.readSubExpr());
12424 }
12425 
12426 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12427   VisitOMPClauseWithPreInit(C);
12428   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12429   C->setNameModifierLoc(Record.readSourceLocation());
12430   C->setColonLoc(Record.readSourceLocation());
12431   C->setCondition(Record.readSubExpr());
12432   C->setLParenLoc(Record.readSourceLocation());
12433 }
12434 
12435 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12436   C->setCondition(Record.readSubExpr());
12437   C->setLParenLoc(Record.readSourceLocation());
12438 }
12439 
12440 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12441   VisitOMPClauseWithPreInit(C);
12442   C->setNumThreads(Record.readSubExpr());
12443   C->setLParenLoc(Record.readSourceLocation());
12444 }
12445 
12446 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12447   C->setSafelen(Record.readSubExpr());
12448   C->setLParenLoc(Record.readSourceLocation());
12449 }
12450 
12451 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12452   C->setSimdlen(Record.readSubExpr());
12453   C->setLParenLoc(Record.readSourceLocation());
12454 }
12455 
12456 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12457   C->setAllocator(Record.readExpr());
12458   C->setLParenLoc(Record.readSourceLocation());
12459 }
12460 
12461 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12462   C->setNumForLoops(Record.readSubExpr());
12463   C->setLParenLoc(Record.readSourceLocation());
12464 }
12465 
12466 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12467   C->setDefaultKind(
12468        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12469   C->setLParenLoc(Record.readSourceLocation());
12470   C->setDefaultKindKwLoc(Record.readSourceLocation());
12471 }
12472 
12473 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12474   C->setProcBindKind(
12475        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12476   C->setLParenLoc(Record.readSourceLocation());
12477   C->setProcBindKindKwLoc(Record.readSourceLocation());
12478 }
12479 
12480 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12481   VisitOMPClauseWithPreInit(C);
12482   C->setScheduleKind(
12483        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12484   C->setFirstScheduleModifier(
12485       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12486   C->setSecondScheduleModifier(
12487       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12488   C->setChunkSize(Record.readSubExpr());
12489   C->setLParenLoc(Record.readSourceLocation());
12490   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12491   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12492   C->setScheduleKindLoc(Record.readSourceLocation());
12493   C->setCommaLoc(Record.readSourceLocation());
12494 }
12495 
12496 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12497   C->setNumForLoops(Record.readSubExpr());
12498   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12499     C->setLoopNumIterations(I, Record.readSubExpr());
12500   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12501     C->setLoopCounter(I, Record.readSubExpr());
12502   C->setLParenLoc(Record.readSourceLocation());
12503 }
12504 
12505 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12506 
12507 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12508 
12509 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12510 
12511 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12512 
12513 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12514 
12515 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12516 
12517 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12518 
12519 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12520 
12521 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12522 
12523 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12524 
12525 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12526 
12527 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12528 
12529 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12530     OMPUnifiedSharedMemoryClause *) {}
12531 
12532 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12533 
12534 void
12535 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12536 }
12537 
12538 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12539     OMPAtomicDefaultMemOrderClause *C) {
12540   C->setAtomicDefaultMemOrderKind(
12541       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12542   C->setLParenLoc(Record.readSourceLocation());
12543   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12544 }
12545 
12546 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12547   C->setLParenLoc(Record.readSourceLocation());
12548   unsigned NumVars = C->varlist_size();
12549   SmallVector<Expr *, 16> Vars;
12550   Vars.reserve(NumVars);
12551   for (unsigned i = 0; i != NumVars; ++i)
12552     Vars.push_back(Record.readSubExpr());
12553   C->setVarRefs(Vars);
12554   Vars.clear();
12555   for (unsigned i = 0; i != NumVars; ++i)
12556     Vars.push_back(Record.readSubExpr());
12557   C->setPrivateCopies(Vars);
12558 }
12559 
12560 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12561   VisitOMPClauseWithPreInit(C);
12562   C->setLParenLoc(Record.readSourceLocation());
12563   unsigned NumVars = C->varlist_size();
12564   SmallVector<Expr *, 16> Vars;
12565   Vars.reserve(NumVars);
12566   for (unsigned i = 0; i != NumVars; ++i)
12567     Vars.push_back(Record.readSubExpr());
12568   C->setVarRefs(Vars);
12569   Vars.clear();
12570   for (unsigned i = 0; i != NumVars; ++i)
12571     Vars.push_back(Record.readSubExpr());
12572   C->setPrivateCopies(Vars);
12573   Vars.clear();
12574   for (unsigned i = 0; i != NumVars; ++i)
12575     Vars.push_back(Record.readSubExpr());
12576   C->setInits(Vars);
12577 }
12578 
12579 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12580   VisitOMPClauseWithPostUpdate(C);
12581   C->setLParenLoc(Record.readSourceLocation());
12582   unsigned NumVars = C->varlist_size();
12583   SmallVector<Expr *, 16> Vars;
12584   Vars.reserve(NumVars);
12585   for (unsigned i = 0; i != NumVars; ++i)
12586     Vars.push_back(Record.readSubExpr());
12587   C->setVarRefs(Vars);
12588   Vars.clear();
12589   for (unsigned i = 0; i != NumVars; ++i)
12590     Vars.push_back(Record.readSubExpr());
12591   C->setPrivateCopies(Vars);
12592   Vars.clear();
12593   for (unsigned i = 0; i != NumVars; ++i)
12594     Vars.push_back(Record.readSubExpr());
12595   C->setSourceExprs(Vars);
12596   Vars.clear();
12597   for (unsigned i = 0; i != NumVars; ++i)
12598     Vars.push_back(Record.readSubExpr());
12599   C->setDestinationExprs(Vars);
12600   Vars.clear();
12601   for (unsigned i = 0; i != NumVars; ++i)
12602     Vars.push_back(Record.readSubExpr());
12603   C->setAssignmentOps(Vars);
12604 }
12605 
12606 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12607   C->setLParenLoc(Record.readSourceLocation());
12608   unsigned NumVars = C->varlist_size();
12609   SmallVector<Expr *, 16> Vars;
12610   Vars.reserve(NumVars);
12611   for (unsigned i = 0; i != NumVars; ++i)
12612     Vars.push_back(Record.readSubExpr());
12613   C->setVarRefs(Vars);
12614 }
12615 
12616 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12617   VisitOMPClauseWithPostUpdate(C);
12618   C->setLParenLoc(Record.readSourceLocation());
12619   C->setColonLoc(Record.readSourceLocation());
12620   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12621   DeclarationNameInfo DNI;
12622   Record.readDeclarationNameInfo(DNI);
12623   C->setQualifierLoc(NNSL);
12624   C->setNameInfo(DNI);
12625 
12626   unsigned NumVars = C->varlist_size();
12627   SmallVector<Expr *, 16> Vars;
12628   Vars.reserve(NumVars);
12629   for (unsigned i = 0; i != NumVars; ++i)
12630     Vars.push_back(Record.readSubExpr());
12631   C->setVarRefs(Vars);
12632   Vars.clear();
12633   for (unsigned i = 0; i != NumVars; ++i)
12634     Vars.push_back(Record.readSubExpr());
12635   C->setPrivates(Vars);
12636   Vars.clear();
12637   for (unsigned i = 0; i != NumVars; ++i)
12638     Vars.push_back(Record.readSubExpr());
12639   C->setLHSExprs(Vars);
12640   Vars.clear();
12641   for (unsigned i = 0; i != NumVars; ++i)
12642     Vars.push_back(Record.readSubExpr());
12643   C->setRHSExprs(Vars);
12644   Vars.clear();
12645   for (unsigned i = 0; i != NumVars; ++i)
12646     Vars.push_back(Record.readSubExpr());
12647   C->setReductionOps(Vars);
12648 }
12649 
12650 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12651   VisitOMPClauseWithPostUpdate(C);
12652   C->setLParenLoc(Record.readSourceLocation());
12653   C->setColonLoc(Record.readSourceLocation());
12654   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12655   DeclarationNameInfo DNI;
12656   Record.readDeclarationNameInfo(DNI);
12657   C->setQualifierLoc(NNSL);
12658   C->setNameInfo(DNI);
12659 
12660   unsigned NumVars = C->varlist_size();
12661   SmallVector<Expr *, 16> Vars;
12662   Vars.reserve(NumVars);
12663   for (unsigned I = 0; I != NumVars; ++I)
12664     Vars.push_back(Record.readSubExpr());
12665   C->setVarRefs(Vars);
12666   Vars.clear();
12667   for (unsigned I = 0; I != NumVars; ++I)
12668     Vars.push_back(Record.readSubExpr());
12669   C->setPrivates(Vars);
12670   Vars.clear();
12671   for (unsigned I = 0; I != NumVars; ++I)
12672     Vars.push_back(Record.readSubExpr());
12673   C->setLHSExprs(Vars);
12674   Vars.clear();
12675   for (unsigned I = 0; I != NumVars; ++I)
12676     Vars.push_back(Record.readSubExpr());
12677   C->setRHSExprs(Vars);
12678   Vars.clear();
12679   for (unsigned I = 0; I != NumVars; ++I)
12680     Vars.push_back(Record.readSubExpr());
12681   C->setReductionOps(Vars);
12682 }
12683 
12684 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12685   VisitOMPClauseWithPostUpdate(C);
12686   C->setLParenLoc(Record.readSourceLocation());
12687   C->setColonLoc(Record.readSourceLocation());
12688   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12689   DeclarationNameInfo DNI;
12690   Record.readDeclarationNameInfo(DNI);
12691   C->setQualifierLoc(NNSL);
12692   C->setNameInfo(DNI);
12693 
12694   unsigned NumVars = C->varlist_size();
12695   SmallVector<Expr *, 16> Vars;
12696   Vars.reserve(NumVars);
12697   for (unsigned I = 0; I != NumVars; ++I)
12698     Vars.push_back(Record.readSubExpr());
12699   C->setVarRefs(Vars);
12700   Vars.clear();
12701   for (unsigned I = 0; I != NumVars; ++I)
12702     Vars.push_back(Record.readSubExpr());
12703   C->setPrivates(Vars);
12704   Vars.clear();
12705   for (unsigned I = 0; I != NumVars; ++I)
12706     Vars.push_back(Record.readSubExpr());
12707   C->setLHSExprs(Vars);
12708   Vars.clear();
12709   for (unsigned I = 0; I != NumVars; ++I)
12710     Vars.push_back(Record.readSubExpr());
12711   C->setRHSExprs(Vars);
12712   Vars.clear();
12713   for (unsigned I = 0; I != NumVars; ++I)
12714     Vars.push_back(Record.readSubExpr());
12715   C->setReductionOps(Vars);
12716   Vars.clear();
12717   for (unsigned I = 0; I != NumVars; ++I)
12718     Vars.push_back(Record.readSubExpr());
12719   C->setTaskgroupDescriptors(Vars);
12720 }
12721 
12722 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12723   VisitOMPClauseWithPostUpdate(C);
12724   C->setLParenLoc(Record.readSourceLocation());
12725   C->setColonLoc(Record.readSourceLocation());
12726   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12727   C->setModifierLoc(Record.readSourceLocation());
12728   unsigned NumVars = C->varlist_size();
12729   SmallVector<Expr *, 16> Vars;
12730   Vars.reserve(NumVars);
12731   for (unsigned i = 0; i != NumVars; ++i)
12732     Vars.push_back(Record.readSubExpr());
12733   C->setVarRefs(Vars);
12734   Vars.clear();
12735   for (unsigned i = 0; i != NumVars; ++i)
12736     Vars.push_back(Record.readSubExpr());
12737   C->setPrivates(Vars);
12738   Vars.clear();
12739   for (unsigned i = 0; i != NumVars; ++i)
12740     Vars.push_back(Record.readSubExpr());
12741   C->setInits(Vars);
12742   Vars.clear();
12743   for (unsigned i = 0; i != NumVars; ++i)
12744     Vars.push_back(Record.readSubExpr());
12745   C->setUpdates(Vars);
12746   Vars.clear();
12747   for (unsigned i = 0; i != NumVars; ++i)
12748     Vars.push_back(Record.readSubExpr());
12749   C->setFinals(Vars);
12750   C->setStep(Record.readSubExpr());
12751   C->setCalcStep(Record.readSubExpr());
12752   Vars.clear();
12753   for (unsigned I = 0; I != NumVars + 1; ++I)
12754     Vars.push_back(Record.readSubExpr());
12755   C->setUsedExprs(Vars);
12756 }
12757 
12758 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12759   C->setLParenLoc(Record.readSourceLocation());
12760   C->setColonLoc(Record.readSourceLocation());
12761   unsigned NumVars = C->varlist_size();
12762   SmallVector<Expr *, 16> Vars;
12763   Vars.reserve(NumVars);
12764   for (unsigned i = 0; i != NumVars; ++i)
12765     Vars.push_back(Record.readSubExpr());
12766   C->setVarRefs(Vars);
12767   C->setAlignment(Record.readSubExpr());
12768 }
12769 
12770 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12771   C->setLParenLoc(Record.readSourceLocation());
12772   unsigned NumVars = C->varlist_size();
12773   SmallVector<Expr *, 16> Exprs;
12774   Exprs.reserve(NumVars);
12775   for (unsigned i = 0; i != NumVars; ++i)
12776     Exprs.push_back(Record.readSubExpr());
12777   C->setVarRefs(Exprs);
12778   Exprs.clear();
12779   for (unsigned i = 0; i != NumVars; ++i)
12780     Exprs.push_back(Record.readSubExpr());
12781   C->setSourceExprs(Exprs);
12782   Exprs.clear();
12783   for (unsigned i = 0; i != NumVars; ++i)
12784     Exprs.push_back(Record.readSubExpr());
12785   C->setDestinationExprs(Exprs);
12786   Exprs.clear();
12787   for (unsigned i = 0; i != NumVars; ++i)
12788     Exprs.push_back(Record.readSubExpr());
12789   C->setAssignmentOps(Exprs);
12790 }
12791 
12792 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12793   C->setLParenLoc(Record.readSourceLocation());
12794   unsigned NumVars = C->varlist_size();
12795   SmallVector<Expr *, 16> Exprs;
12796   Exprs.reserve(NumVars);
12797   for (unsigned i = 0; i != NumVars; ++i)
12798     Exprs.push_back(Record.readSubExpr());
12799   C->setVarRefs(Exprs);
12800   Exprs.clear();
12801   for (unsigned i = 0; i != NumVars; ++i)
12802     Exprs.push_back(Record.readSubExpr());
12803   C->setSourceExprs(Exprs);
12804   Exprs.clear();
12805   for (unsigned i = 0; i != NumVars; ++i)
12806     Exprs.push_back(Record.readSubExpr());
12807   C->setDestinationExprs(Exprs);
12808   Exprs.clear();
12809   for (unsigned i = 0; i != NumVars; ++i)
12810     Exprs.push_back(Record.readSubExpr());
12811   C->setAssignmentOps(Exprs);
12812 }
12813 
12814 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12815   C->setLParenLoc(Record.readSourceLocation());
12816   unsigned NumVars = C->varlist_size();
12817   SmallVector<Expr *, 16> Vars;
12818   Vars.reserve(NumVars);
12819   for (unsigned i = 0; i != NumVars; ++i)
12820     Vars.push_back(Record.readSubExpr());
12821   C->setVarRefs(Vars);
12822 }
12823 
12824 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12825   C->setLParenLoc(Record.readSourceLocation());
12826   C->setDependencyKind(
12827       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12828   C->setDependencyLoc(Record.readSourceLocation());
12829   C->setColonLoc(Record.readSourceLocation());
12830   unsigned NumVars = C->varlist_size();
12831   SmallVector<Expr *, 16> Vars;
12832   Vars.reserve(NumVars);
12833   for (unsigned I = 0; I != NumVars; ++I)
12834     Vars.push_back(Record.readSubExpr());
12835   C->setVarRefs(Vars);
12836   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12837     C->setLoopData(I, Record.readSubExpr());
12838 }
12839 
12840 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12841   VisitOMPClauseWithPreInit(C);
12842   C->setDevice(Record.readSubExpr());
12843   C->setLParenLoc(Record.readSourceLocation());
12844 }
12845 
12846 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12847   C->setLParenLoc(Record.readSourceLocation());
12848   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12849     C->setMapTypeModifier(
12850         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12851     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12852   }
12853   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12854   DeclarationNameInfo DNI;
12855   Record.readDeclarationNameInfo(DNI);
12856   C->setMapperIdInfo(DNI);
12857   C->setMapType(
12858      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12859   C->setMapLoc(Record.readSourceLocation());
12860   C->setColonLoc(Record.readSourceLocation());
12861   auto NumVars = C->varlist_size();
12862   auto UniqueDecls = C->getUniqueDeclarationsNum();
12863   auto TotalLists = C->getTotalComponentListNum();
12864   auto TotalComponents = C->getTotalComponentsNum();
12865 
12866   SmallVector<Expr *, 16> Vars;
12867   Vars.reserve(NumVars);
12868   for (unsigned i = 0; i != NumVars; ++i)
12869     Vars.push_back(Record.readExpr());
12870   C->setVarRefs(Vars);
12871 
12872   SmallVector<Expr *, 16> UDMappers;
12873   UDMappers.reserve(NumVars);
12874   for (unsigned I = 0; I < NumVars; ++I)
12875     UDMappers.push_back(Record.readExpr());
12876   C->setUDMapperRefs(UDMappers);
12877 
12878   SmallVector<ValueDecl *, 16> Decls;
12879   Decls.reserve(UniqueDecls);
12880   for (unsigned i = 0; i < UniqueDecls; ++i)
12881     Decls.push_back(Record.readDeclAs<ValueDecl>());
12882   C->setUniqueDecls(Decls);
12883 
12884   SmallVector<unsigned, 16> ListsPerDecl;
12885   ListsPerDecl.reserve(UniqueDecls);
12886   for (unsigned i = 0; i < UniqueDecls; ++i)
12887     ListsPerDecl.push_back(Record.readInt());
12888   C->setDeclNumLists(ListsPerDecl);
12889 
12890   SmallVector<unsigned, 32> ListSizes;
12891   ListSizes.reserve(TotalLists);
12892   for (unsigned i = 0; i < TotalLists; ++i)
12893     ListSizes.push_back(Record.readInt());
12894   C->setComponentListSizes(ListSizes);
12895 
12896   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12897   Components.reserve(TotalComponents);
12898   for (unsigned i = 0; i < TotalComponents; ++i) {
12899     Expr *AssociatedExpr = Record.readExpr();
12900     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12901     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12902         AssociatedExpr, AssociatedDecl));
12903   }
12904   C->setComponents(Components, ListSizes);
12905 }
12906 
12907 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12908   C->setLParenLoc(Record.readSourceLocation());
12909   C->setColonLoc(Record.readSourceLocation());
12910   C->setAllocator(Record.readSubExpr());
12911   unsigned NumVars = C->varlist_size();
12912   SmallVector<Expr *, 16> Vars;
12913   Vars.reserve(NumVars);
12914   for (unsigned i = 0; i != NumVars; ++i)
12915     Vars.push_back(Record.readSubExpr());
12916   C->setVarRefs(Vars);
12917 }
12918 
12919 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12920   VisitOMPClauseWithPreInit(C);
12921   C->setNumTeams(Record.readSubExpr());
12922   C->setLParenLoc(Record.readSourceLocation());
12923 }
12924 
12925 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12926   VisitOMPClauseWithPreInit(C);
12927   C->setThreadLimit(Record.readSubExpr());
12928   C->setLParenLoc(Record.readSourceLocation());
12929 }
12930 
12931 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12932   C->setPriority(Record.readSubExpr());
12933   C->setLParenLoc(Record.readSourceLocation());
12934 }
12935 
12936 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12937   C->setGrainsize(Record.readSubExpr());
12938   C->setLParenLoc(Record.readSourceLocation());
12939 }
12940 
12941 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12942   C->setNumTasks(Record.readSubExpr());
12943   C->setLParenLoc(Record.readSourceLocation());
12944 }
12945 
12946 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12947   C->setHint(Record.readSubExpr());
12948   C->setLParenLoc(Record.readSourceLocation());
12949 }
12950 
12951 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12952   VisitOMPClauseWithPreInit(C);
12953   C->setDistScheduleKind(
12954       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12955   C->setChunkSize(Record.readSubExpr());
12956   C->setLParenLoc(Record.readSourceLocation());
12957   C->setDistScheduleKindLoc(Record.readSourceLocation());
12958   C->setCommaLoc(Record.readSourceLocation());
12959 }
12960 
12961 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12962   C->setDefaultmapKind(
12963        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12964   C->setDefaultmapModifier(
12965       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12966   C->setLParenLoc(Record.readSourceLocation());
12967   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12968   C->setDefaultmapKindLoc(Record.readSourceLocation());
12969 }
12970 
12971 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12972   C->setLParenLoc(Record.readSourceLocation());
12973   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12974   DeclarationNameInfo DNI;
12975   Record.readDeclarationNameInfo(DNI);
12976   C->setMapperIdInfo(DNI);
12977   auto NumVars = C->varlist_size();
12978   auto UniqueDecls = C->getUniqueDeclarationsNum();
12979   auto TotalLists = C->getTotalComponentListNum();
12980   auto TotalComponents = C->getTotalComponentsNum();
12981 
12982   SmallVector<Expr *, 16> Vars;
12983   Vars.reserve(NumVars);
12984   for (unsigned i = 0; i != NumVars; ++i)
12985     Vars.push_back(Record.readSubExpr());
12986   C->setVarRefs(Vars);
12987 
12988   SmallVector<Expr *, 16> UDMappers;
12989   UDMappers.reserve(NumVars);
12990   for (unsigned I = 0; I < NumVars; ++I)
12991     UDMappers.push_back(Record.readSubExpr());
12992   C->setUDMapperRefs(UDMappers);
12993 
12994   SmallVector<ValueDecl *, 16> Decls;
12995   Decls.reserve(UniqueDecls);
12996   for (unsigned i = 0; i < UniqueDecls; ++i)
12997     Decls.push_back(Record.readDeclAs<ValueDecl>());
12998   C->setUniqueDecls(Decls);
12999 
13000   SmallVector<unsigned, 16> ListsPerDecl;
13001   ListsPerDecl.reserve(UniqueDecls);
13002   for (unsigned i = 0; i < UniqueDecls; ++i)
13003     ListsPerDecl.push_back(Record.readInt());
13004   C->setDeclNumLists(ListsPerDecl);
13005 
13006   SmallVector<unsigned, 32> ListSizes;
13007   ListSizes.reserve(TotalLists);
13008   for (unsigned i = 0; i < TotalLists; ++i)
13009     ListSizes.push_back(Record.readInt());
13010   C->setComponentListSizes(ListSizes);
13011 
13012   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13013   Components.reserve(TotalComponents);
13014   for (unsigned i = 0; i < TotalComponents; ++i) {
13015     Expr *AssociatedExpr = Record.readSubExpr();
13016     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13017     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13018         AssociatedExpr, AssociatedDecl));
13019   }
13020   C->setComponents(Components, ListSizes);
13021 }
13022 
13023 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
13024   C->setLParenLoc(Record.readSourceLocation());
13025   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13026   DeclarationNameInfo DNI;
13027   Record.readDeclarationNameInfo(DNI);
13028   C->setMapperIdInfo(DNI);
13029   auto NumVars = C->varlist_size();
13030   auto UniqueDecls = C->getUniqueDeclarationsNum();
13031   auto TotalLists = C->getTotalComponentListNum();
13032   auto TotalComponents = C->getTotalComponentsNum();
13033 
13034   SmallVector<Expr *, 16> Vars;
13035   Vars.reserve(NumVars);
13036   for (unsigned i = 0; i != NumVars; ++i)
13037     Vars.push_back(Record.readSubExpr());
13038   C->setVarRefs(Vars);
13039 
13040   SmallVector<Expr *, 16> UDMappers;
13041   UDMappers.reserve(NumVars);
13042   for (unsigned I = 0; I < NumVars; ++I)
13043     UDMappers.push_back(Record.readSubExpr());
13044   C->setUDMapperRefs(UDMappers);
13045 
13046   SmallVector<ValueDecl *, 16> Decls;
13047   Decls.reserve(UniqueDecls);
13048   for (unsigned i = 0; i < UniqueDecls; ++i)
13049     Decls.push_back(Record.readDeclAs<ValueDecl>());
13050   C->setUniqueDecls(Decls);
13051 
13052   SmallVector<unsigned, 16> ListsPerDecl;
13053   ListsPerDecl.reserve(UniqueDecls);
13054   for (unsigned i = 0; i < UniqueDecls; ++i)
13055     ListsPerDecl.push_back(Record.readInt());
13056   C->setDeclNumLists(ListsPerDecl);
13057 
13058   SmallVector<unsigned, 32> ListSizes;
13059   ListSizes.reserve(TotalLists);
13060   for (unsigned i = 0; i < TotalLists; ++i)
13061     ListSizes.push_back(Record.readInt());
13062   C->setComponentListSizes(ListSizes);
13063 
13064   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13065   Components.reserve(TotalComponents);
13066   for (unsigned i = 0; i < TotalComponents; ++i) {
13067     Expr *AssociatedExpr = Record.readSubExpr();
13068     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13069     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13070         AssociatedExpr, AssociatedDecl));
13071   }
13072   C->setComponents(Components, ListSizes);
13073 }
13074 
13075 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13076   C->setLParenLoc(Record.readSourceLocation());
13077   auto NumVars = C->varlist_size();
13078   auto UniqueDecls = C->getUniqueDeclarationsNum();
13079   auto TotalLists = C->getTotalComponentListNum();
13080   auto TotalComponents = C->getTotalComponentsNum();
13081 
13082   SmallVector<Expr *, 16> Vars;
13083   Vars.reserve(NumVars);
13084   for (unsigned i = 0; i != NumVars; ++i)
13085     Vars.push_back(Record.readSubExpr());
13086   C->setVarRefs(Vars);
13087   Vars.clear();
13088   for (unsigned i = 0; i != NumVars; ++i)
13089     Vars.push_back(Record.readSubExpr());
13090   C->setPrivateCopies(Vars);
13091   Vars.clear();
13092   for (unsigned i = 0; i != NumVars; ++i)
13093     Vars.push_back(Record.readSubExpr());
13094   C->setInits(Vars);
13095 
13096   SmallVector<ValueDecl *, 16> Decls;
13097   Decls.reserve(UniqueDecls);
13098   for (unsigned i = 0; i < UniqueDecls; ++i)
13099     Decls.push_back(Record.readDeclAs<ValueDecl>());
13100   C->setUniqueDecls(Decls);
13101 
13102   SmallVector<unsigned, 16> ListsPerDecl;
13103   ListsPerDecl.reserve(UniqueDecls);
13104   for (unsigned i = 0; i < UniqueDecls; ++i)
13105     ListsPerDecl.push_back(Record.readInt());
13106   C->setDeclNumLists(ListsPerDecl);
13107 
13108   SmallVector<unsigned, 32> ListSizes;
13109   ListSizes.reserve(TotalLists);
13110   for (unsigned i = 0; i < TotalLists; ++i)
13111     ListSizes.push_back(Record.readInt());
13112   C->setComponentListSizes(ListSizes);
13113 
13114   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13115   Components.reserve(TotalComponents);
13116   for (unsigned i = 0; i < TotalComponents; ++i) {
13117     Expr *AssociatedExpr = Record.readSubExpr();
13118     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13119     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13120         AssociatedExpr, AssociatedDecl));
13121   }
13122   C->setComponents(Components, ListSizes);
13123 }
13124 
13125 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13126   C->setLParenLoc(Record.readSourceLocation());
13127   auto NumVars = C->varlist_size();
13128   auto UniqueDecls = C->getUniqueDeclarationsNum();
13129   auto TotalLists = C->getTotalComponentListNum();
13130   auto TotalComponents = C->getTotalComponentsNum();
13131 
13132   SmallVector<Expr *, 16> Vars;
13133   Vars.reserve(NumVars);
13134   for (unsigned i = 0; i != NumVars; ++i)
13135     Vars.push_back(Record.readSubExpr());
13136   C->setVarRefs(Vars);
13137   Vars.clear();
13138 
13139   SmallVector<ValueDecl *, 16> Decls;
13140   Decls.reserve(UniqueDecls);
13141   for (unsigned i = 0; i < UniqueDecls; ++i)
13142     Decls.push_back(Record.readDeclAs<ValueDecl>());
13143   C->setUniqueDecls(Decls);
13144 
13145   SmallVector<unsigned, 16> ListsPerDecl;
13146   ListsPerDecl.reserve(UniqueDecls);
13147   for (unsigned i = 0; i < UniqueDecls; ++i)
13148     ListsPerDecl.push_back(Record.readInt());
13149   C->setDeclNumLists(ListsPerDecl);
13150 
13151   SmallVector<unsigned, 32> ListSizes;
13152   ListSizes.reserve(TotalLists);
13153   for (unsigned i = 0; i < TotalLists; ++i)
13154     ListSizes.push_back(Record.readInt());
13155   C->setComponentListSizes(ListSizes);
13156 
13157   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13158   Components.reserve(TotalComponents);
13159   for (unsigned i = 0; i < TotalComponents; ++i) {
13160     Expr *AssociatedExpr = Record.readSubExpr();
13161     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13162     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13163         AssociatedExpr, AssociatedDecl));
13164   }
13165   C->setComponents(Components, ListSizes);
13166 }
13167