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