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