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