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