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