1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ASTReader class, which reads AST files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Serialization/ASTReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/ASTMutationListener.h"
20 #include "clang/AST/ASTUnresolvedSet.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclBase.h"
23 #include "clang/AST/DeclCXX.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclGroup.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/DeclarationName.h"
29 #include "clang/AST/Expr.h"
30 #include "clang/AST/ExprCXX.h"
31 #include "clang/AST/ExternalASTSource.h"
32 #include "clang/AST/NestedNameSpecifier.h"
33 #include "clang/AST/ODRHash.h"
34 #include "clang/AST/RawCommentList.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/TemplateName.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/TypeLoc.h"
39 #include "clang/AST/TypeLocVisitor.h"
40 #include "clang/AST/UnresolvedSet.h"
41 #include "clang/Basic/CommentOptions.h"
42 #include "clang/Basic/Diagnostic.h"
43 #include "clang/Basic/DiagnosticOptions.h"
44 #include "clang/Basic/ExceptionSpecificationType.h"
45 #include "clang/Basic/FileManager.h"
46 #include "clang/Basic/FileSystemOptions.h"
47 #include "clang/Basic/IdentifierTable.h"
48 #include "clang/Basic/LLVM.h"
49 #include "clang/Basic/LangOptions.h"
50 #include "clang/Basic/MemoryBufferCache.h"
51 #include "clang/Basic/Module.h"
52 #include "clang/Basic/ObjCRuntime.h"
53 #include "clang/Basic/OperatorKinds.h"
54 #include "clang/Basic/PragmaKinds.h"
55 #include "clang/Basic/Sanitizers.h"
56 #include "clang/Basic/SourceLocation.h"
57 #include "clang/Basic/SourceManager.h"
58 #include "clang/Basic/SourceManagerInternals.h"
59 #include "clang/Basic/Specifiers.h"
60 #include "clang/Basic/TargetInfo.h"
61 #include "clang/Basic/TargetOptions.h"
62 #include "clang/Basic/TokenKinds.h"
63 #include "clang/Basic/Version.h"
64 #include "clang/Frontend/PCHContainerOperations.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/Module.h"
82 #include "clang/Serialization/ModuleFileExtension.h"
83 #include "clang/Serialization/ModuleManager.h"
84 #include "clang/Serialization/SerializationDiagnostic.h"
85 #include "llvm/ADT/APFloat.h"
86 #include "llvm/ADT/APInt.h"
87 #include "llvm/ADT/APSInt.h"
88 #include "llvm/ADT/ArrayRef.h"
89 #include "llvm/ADT/DenseMap.h"
90 #include "llvm/ADT/FoldingSet.h"
91 #include "llvm/ADT/Hashing.h"
92 #include "llvm/ADT/IntrusiveRefCntPtr.h"
93 #include "llvm/ADT/None.h"
94 #include "llvm/ADT/Optional.h"
95 #include "llvm/ADT/STLExtras.h"
96 #include "llvm/ADT/SmallPtrSet.h"
97 #include "llvm/ADT/SmallString.h"
98 #include "llvm/ADT/SmallVector.h"
99 #include "llvm/ADT/StringExtras.h"
100 #include "llvm/ADT/StringMap.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/ADT/iterator_range.h"
104 #include "llvm/Bitcode/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
119 #include <algorithm>
120 #include <cassert>
121 #include <cstddef>
122 #include <cstdint>
123 #include <cstdio>
124 #include <ctime>
125 #include <iterator>
126 #include <limits>
127 #include <map>
128 #include <memory>
129 #include <string>
130 #include <system_error>
131 #include <tuple>
132 #include <utility>
133 #include <vector>
134 
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
139 
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
143 
144 bool
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146   return First->ReadFullVersionInformation(FullVersion) ||
147          Second->ReadFullVersionInformation(FullVersion);
148 }
149 
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151   First->ReadModuleName(ModuleName);
152   Second->ReadModuleName(ModuleName);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156   First->ReadModuleMapFile(ModuleMapPath);
157   Second->ReadModuleMapFile(ModuleMapPath);
158 }
159 
160 bool
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
162                                               bool Complain,
163                                               bool AllowCompatibleDifferences) {
164   return First->ReadLanguageOptions(LangOpts, Complain,
165                                     AllowCompatibleDifferences) ||
166          Second->ReadLanguageOptions(LangOpts, Complain,
167                                      AllowCompatibleDifferences);
168 }
169 
170 bool ChainedASTReaderListener::ReadTargetOptions(
171     const TargetOptions &TargetOpts, bool Complain,
172     bool AllowCompatibleDifferences) {
173   return First->ReadTargetOptions(TargetOpts, Complain,
174                                   AllowCompatibleDifferences) ||
175          Second->ReadTargetOptions(TargetOpts, Complain,
176                                    AllowCompatibleDifferences);
177 }
178 
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182          Second->ReadDiagnosticOptions(DiagOpts, Complain);
183 }
184 
185 bool
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
187                                                 bool Complain) {
188   return First->ReadFileSystemOptions(FSOpts, Complain) ||
189          Second->ReadFileSystemOptions(FSOpts, Complain);
190 }
191 
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
194     bool Complain) {
195   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
196                                         Complain) ||
197          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198                                          Complain);
199 }
200 
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202     const PreprocessorOptions &PPOpts, bool Complain,
203     std::string &SuggestedPredefines) {
204   return First->ReadPreprocessorOptions(PPOpts, Complain,
205                                         SuggestedPredefines) ||
206          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
207 }
208 
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
210                                            unsigned Value) {
211   First->ReadCounter(M, Value);
212   Second->ReadCounter(M, Value);
213 }
214 
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216   return First->needsInputFileVisitation() ||
217          Second->needsInputFileVisitation();
218 }
219 
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221   return First->needsSystemInputFileVisitation() ||
222   Second->needsSystemInputFileVisitation();
223 }
224 
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
226                                                ModuleKind Kind) {
227   First->visitModuleFile(Filename, Kind);
228   Second->visitModuleFile(Filename, Kind);
229 }
230 
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
232                                               bool isSystem,
233                                               bool isOverridden,
234                                               bool isExplicitModule) {
235   bool Continue = false;
236   if (First->needsInputFileVisitation() &&
237       (!isSystem || First->needsSystemInputFileVisitation()))
238     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
239                                       isExplicitModule);
240   if (Second->needsInputFileVisitation() &&
241       (!isSystem || Second->needsSystemInputFileVisitation()))
242     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
243                                        isExplicitModule);
244   return Continue;
245 }
246 
247 void ChainedASTReaderListener::readModuleFileExtension(
248        const ModuleFileExtensionMetadata &Metadata) {
249   First->readModuleFileExtension(Metadata);
250   Second->readModuleFileExtension(Metadata);
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
256 
257 ASTReaderListener::~ASTReaderListener() = default;
258 
259 /// Compare the given set of language options against an existing set of
260 /// language options.
261 ///
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 ///        language options will be permitted.
265 ///
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268                                  const LangOptions &ExistingLangOpts,
269                                  DiagnosticsEngine *Diags,
270                                  bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description)                 \
272   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
273     if (Diags)                                                    \
274       Diags->Report(diag::err_pch_langopt_mismatch)               \
275         << Description << LangOpts.Name << ExistingLangOpts.Name; \
276     return true;                                                  \
277   }
278 
279 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
280   if (ExistingLangOpts.Name != LangOpts.Name) {           \
281     if (Diags)                                            \
282       Diags->Report(diag::err_pch_langopt_value_mismatch) \
283         << Description;                                   \
284     return true;                                          \
285   }
286 
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
288   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
289     if (Diags)                                                 \
290       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
291         << Description;                                        \
292     return true;                                               \
293   }
294 
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
296   if (!AllowCompatibleDifferences)                            \
297     LANGOPT(Name, Bits, Default, Description)
298 
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
300   if (!AllowCompatibleDifferences)                                 \
301     ENUM_LANGOPT(Name, Bits, Default, Description)
302 
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304   if (!AllowCompatibleDifferences)                                 \
305     VALUE_LANGOPT(Name, Bits, Default, Description)
306 
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
311 
312   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
313     if (Diags)
314       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
315     return true;
316   }
317 
318   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch)
321       << "target Objective-C runtime";
322     return true;
323   }
324 
325   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326       LangOpts.CommentOpts.BlockCommandNames) {
327     if (Diags)
328       Diags->Report(diag::err_pch_langopt_value_mismatch)
329         << "block command names";
330     return true;
331   }
332 
333   // Sanitizer feature mismatches are treated as compatible differences. If
334   // compatible differences aren't allowed, we still only want to check for
335   // mismatches of non-modular sanitizers (the only ones which can affect AST
336   // generation).
337   if (!AllowCompatibleDifferences) {
338     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341     ExistingSanitizers.clear(ModularSanitizers);
342     ImportedSanitizers.clear(ModularSanitizers);
343     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344       const std::string Flag = "-fsanitize=";
345       if (Diags) {
346 #define SANITIZER(NAME, ID)                                                    \
347   {                                                                            \
348     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
349     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
350     if (InExistingModule != InImportedModule)                                  \
351       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
352           << InExistingModule << (Flag + NAME);                                \
353   }
354 #include "clang/Basic/Sanitizers.def"
355       }
356       return true;
357     }
358   }
359 
360   return false;
361 }
362 
363 /// Compare the given set of target options against an existing set of
364 /// target options.
365 ///
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
367 ///
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370                                const TargetOptions &ExistingTargetOpts,
371                                DiagnosticsEngine *Diags,
372                                bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name)                             \
374   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
375     if (Diags)                                                    \
376       Diags->Report(diag::err_pch_targetopt_mismatch)             \
377         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
378     return true;                                                  \
379   }
380 
381   // The triple and ABI must match exactly.
382   CHECK_TARGET_OPT(Triple, "target");
383   CHECK_TARGET_OPT(ABI, "target ABI");
384 
385   // We can tolerate different CPUs in many cases, notably when one CPU
386   // supports a strict superset of another. When allowing compatible
387   // differences skip this check.
388   if (!AllowCompatibleDifferences)
389     CHECK_TARGET_OPT(CPU, "target CPU");
390 
391 #undef CHECK_TARGET_OPT
392 
393   // Compare feature sets.
394   SmallVector<StringRef, 4> ExistingFeatures(
395                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
396                                              ExistingTargetOpts.FeaturesAsWritten.end());
397   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398                                          TargetOpts.FeaturesAsWritten.end());
399   llvm::sort(ExistingFeatures);
400   llvm::sort(ReadFeatures);
401 
402   // We compute the set difference in both directions explicitly so that we can
403   // diagnose the differences differently.
404   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
405   std::set_difference(
406       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409                       ExistingFeatures.begin(), ExistingFeatures.end(),
410                       std::back_inserter(UnmatchedReadFeatures));
411 
412   // If we are allowing compatible differences and the read feature set is
413   // a strict subset of the existing feature set, there is nothing to diagnose.
414   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
415     return false;
416 
417   if (Diags) {
418     for (StringRef Feature : UnmatchedReadFeatures)
419       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420           << /* is-existing-feature */ false << Feature;
421     for (StringRef Feature : UnmatchedExistingFeatures)
422       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423           << /* is-existing-feature */ true << Feature;
424   }
425 
426   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
427 }
428 
429 bool
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
431                                   bool Complain,
432                                   bool AllowCompatibleDifferences) {
433   const LangOptions &ExistingLangOpts = PP.getLangOpts();
434   return checkLanguageOptions(LangOpts, ExistingLangOpts,
435                               Complain ? &Reader.Diags : nullptr,
436                               AllowCompatibleDifferences);
437 }
438 
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
440                                      bool Complain,
441                                      bool AllowCompatibleDifferences) {
442   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444                             Complain ? &Reader.Diags : nullptr,
445                             AllowCompatibleDifferences);
446 }
447 
448 namespace {
449 
450 using MacroDefinitionsMap =
451     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
453 
454 } // namespace
455 
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457                                          DiagnosticsEngine &Diags,
458                                          bool Complain) {
459   using Level = DiagnosticsEngine::Level;
460 
461   // Check current mappings for new -Werror mappings, and the stored mappings
462   // for cases that were explicitly mapped to *not* be errors that are now
463   // errors because of options like -Werror.
464   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
465 
466   for (DiagnosticsEngine *MappingSource : MappingSources) {
467     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468       diag::kind DiagID = DiagIDMappingPair.first;
469       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470       if (CurLevel < DiagnosticsEngine::Error)
471         continue; // not significant
472       Level StoredLevel =
473           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474       if (StoredLevel < DiagnosticsEngine::Error) {
475         if (Complain)
476           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
478         return true;
479       }
480     }
481   }
482 
483   return false;
484 }
485 
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
489     return true;
490   return Ext >= diag::Severity::Error;
491 }
492 
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494                                     DiagnosticsEngine &Diags,
495                                     bool IsSystem, bool Complain) {
496   // Top-level options
497   if (IsSystem) {
498     if (Diags.getSuppressSystemWarnings())
499       return false;
500     // If -Wsystem-headers was not enabled before, be conservative
501     if (StoredDiags.getSuppressSystemWarnings()) {
502       if (Complain)
503         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
504       return true;
505     }
506   }
507 
508   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
509     if (Complain)
510       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
511     return true;
512   }
513 
514   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515       !StoredDiags.getEnableAllWarnings()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
518     return true;
519   }
520 
521   if (isExtHandlingFromDiagsError(Diags) &&
522       !isExtHandlingFromDiagsError(StoredDiags)) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
525     return true;
526   }
527 
528   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
529 }
530 
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
533                                           Preprocessor &PP) {
534   // If the original import came from a file explicitly generated by the user,
535   // don't check the diagnostic mappings.
536   // FIXME: currently this is approximated by checking whether this is not a
537   // module import of an implicitly-loaded module file.
538   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539   // the transitive closure of its imports, since unrelated modules cannot be
540   // imported until after this module finishes validation.
541   ModuleFile *TopImport = &*ModuleMgr.rbegin();
542   while (!TopImport->ImportedBy.empty())
543     TopImport = TopImport->ImportedBy[0];
544   if (TopImport->Kind != MK_ImplicitModule)
545     return nullptr;
546 
547   StringRef ModuleName = TopImport->ModuleName;
548   assert(!ModuleName.empty() && "diagnostic options read before module name");
549 
550   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551   assert(M && "missing module");
552   return M;
553 }
554 
555 bool PCHValidator::ReadDiagnosticOptions(
556     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561   // This should never fail, because we would have processed these options
562   // before writing them to an ASTFile.
563   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
564 
565   ModuleManager &ModuleMgr = Reader.getModuleManager();
566   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
567 
568   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
569   if (!TopM)
570     return false;
571 
572   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573   // contains the union of their flags.
574   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
575                                  Complain);
576 }
577 
578 /// Collect the macro definitions provided by the given preprocessor
579 /// options.
580 static void
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582                         MacroDefinitionsMap &Macros,
583                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585     StringRef Macro = PPOpts.Macros[I].first;
586     bool IsUndef = PPOpts.Macros[I].second;
587 
588     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589     StringRef MacroName = MacroPair.first;
590     StringRef MacroBody = MacroPair.second;
591 
592     // For an #undef'd macro, we only care about the name.
593     if (IsUndef) {
594       if (MacroNames && !Macros.count(MacroName))
595         MacroNames->push_back(MacroName);
596 
597       Macros[MacroName] = std::make_pair("", true);
598       continue;
599     }
600 
601     // For a #define'd macro, figure out the actual definition.
602     if (MacroName.size() == Macro.size())
603       MacroBody = "1";
604     else {
605       // Note: GCC drops anything following an end-of-line character.
606       StringRef::size_type End = MacroBody.find_first_of("\n\r");
607       MacroBody = MacroBody.substr(0, End);
608     }
609 
610     if (MacroNames && !Macros.count(MacroName))
611       MacroNames->push_back(MacroName);
612     Macros[MacroName] = std::make_pair(MacroBody, false);
613   }
614 }
615 
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
618 ///
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 ///        macros defined by \p ExistingPPOpts to override those defined by
622 ///        \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624                                      const PreprocessorOptions &ExistingPPOpts,
625                                      DiagnosticsEngine *Diags,
626                                      FileManager &FileMgr,
627                                      std::string &SuggestedPredefines,
628                                      const LangOptions &LangOpts,
629                                      bool Validate = true) {
630   // Check macro definitions.
631   MacroDefinitionsMap ASTFileMacros;
632   collectMacroDefinitions(PPOpts, ASTFileMacros);
633   MacroDefinitionsMap ExistingMacros;
634   SmallVector<StringRef, 4> ExistingMacroNames;
635   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
636 
637   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638     // Dig out the macro definition in the existing preprocessor options.
639     StringRef MacroName = ExistingMacroNames[I];
640     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
641 
642     // Check whether we know anything about this macro name or not.
643     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644         ASTFileMacros.find(MacroName);
645     if (!Validate || Known == ASTFileMacros.end()) {
646       // FIXME: Check whether this identifier was referenced anywhere in the
647       // AST file. If so, we should reject the AST file. Unfortunately, this
648       // information isn't in the control block. What shall we do about it?
649 
650       if (Existing.second) {
651         SuggestedPredefines += "#undef ";
652         SuggestedPredefines += MacroName.str();
653         SuggestedPredefines += '\n';
654       } else {
655         SuggestedPredefines += "#define ";
656         SuggestedPredefines += MacroName.str();
657         SuggestedPredefines += ' ';
658         SuggestedPredefines += Existing.first.str();
659         SuggestedPredefines += '\n';
660       }
661       continue;
662     }
663 
664     // If the macro was defined in one but undef'd in the other, we have a
665     // conflict.
666     if (Existing.second != Known->second.second) {
667       if (Diags) {
668         Diags->Report(diag::err_pch_macro_def_undef)
669           << MacroName << Known->second.second;
670       }
671       return true;
672     }
673 
674     // If the macro was #undef'd in both, or if the macro bodies are identical,
675     // it's fine.
676     if (Existing.second || Existing.first == Known->second.first)
677       continue;
678 
679     // The macro bodies differ; complain.
680     if (Diags) {
681       Diags->Report(diag::err_pch_macro_def_conflict)
682         << MacroName << Known->second.first << Existing.first;
683     }
684     return true;
685   }
686 
687   // Check whether we're using predefines.
688   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
689     if (Diags) {
690       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
691     }
692     return true;
693   }
694 
695   // Detailed record is important since it is used for the module cache hash.
696   if (LangOpts.Modules &&
697       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
700     }
701     return true;
702   }
703 
704   // Compute the #include and #include_macros lines we need.
705   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706     StringRef File = ExistingPPOpts.Includes[I];
707 
708     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709         !ExistingPPOpts.PCHThroughHeader.empty()) {
710       // In case the through header is an include, we must add all the includes
711       // to the predefines so the start point can be determined.
712       SuggestedPredefines += "#include \"";
713       SuggestedPredefines += File;
714       SuggestedPredefines += "\"\n";
715       continue;
716     }
717 
718     if (File == ExistingPPOpts.ImplicitPCHInclude)
719       continue;
720 
721     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722           != PPOpts.Includes.end())
723       continue;
724 
725     SuggestedPredefines += "#include \"";
726     SuggestedPredefines += File;
727     SuggestedPredefines += "\"\n";
728   }
729 
730   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731     StringRef File = ExistingPPOpts.MacroIncludes[I];
732     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
733                   File)
734         != PPOpts.MacroIncludes.end())
735       continue;
736 
737     SuggestedPredefines += "#__include_macros \"";
738     SuggestedPredefines += File;
739     SuggestedPredefines += "\"\n##\n";
740   }
741 
742   return false;
743 }
744 
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
746                                            bool Complain,
747                                            std::string &SuggestedPredefines) {
748   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
749 
750   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751                                   Complain? &Reader.Diags : nullptr,
752                                   PP.getFileManager(),
753                                   SuggestedPredefines,
754                                   PP.getLangOpts());
755 }
756 
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758                                   const PreprocessorOptions &PPOpts,
759                                   bool Complain,
760                                   std::string &SuggestedPredefines) {
761   return checkPreprocessorOptions(PPOpts,
762                                   PP.getPreprocessorOpts(),
763                                   nullptr,
764                                   PP.getFileManager(),
765                                   SuggestedPredefines,
766                                   PP.getLangOpts(),
767                                   false);
768 }
769 
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
772 ///
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775                                      StringRef SpecificModuleCachePath,
776                                      StringRef ExistingModuleCachePath,
777                                      DiagnosticsEngine *Diags,
778                                      const LangOptions &LangOpts) {
779   if (LangOpts.Modules) {
780     if (SpecificModuleCachePath != ExistingModuleCachePath) {
781       if (Diags)
782         Diags->Report(diag::err_pch_modulecache_mismatch)
783           << SpecificModuleCachePath << ExistingModuleCachePath;
784       return true;
785     }
786   }
787 
788   return false;
789 }
790 
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792                                            StringRef SpecificModuleCachePath,
793                                            bool Complain) {
794   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795                                   PP.getHeaderSearchInfo().getModuleCachePath(),
796                                   Complain ? &Reader.Diags : nullptr,
797                                   PP.getLangOpts());
798 }
799 
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801   PP.setCounterValue(Value);
802 }
803 
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
807 
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809                                            bool TakeOwnership) {
810   DeserializationListener = Listener;
811   OwnsDeserializationListener = TakeOwnership;
812 }
813 
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815   return serialization::ComputeHash(Sel);
816 }
817 
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820   using namespace llvm::support;
821 
822   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824   return std::make_pair(KeyLen, DataLen);
825 }
826 
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829   using namespace llvm::support;
830 
831   SelectorTable &SelTable = Reader.getContext().Selectors;
832   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834       F, endian::readNext<uint32_t, little, unaligned>(d));
835   if (N == 0)
836     return SelTable.getNullarySelector(FirstII);
837   else if (N == 1)
838     return SelTable.getUnarySelector(FirstII);
839 
840   SmallVector<IdentifierInfo *, 16> Args;
841   Args.push_back(FirstII);
842   for (unsigned I = 1; I != N; ++I)
843     Args.push_back(Reader.getLocalIdentifier(
844         F, endian::readNext<uint32_t, little, unaligned>(d)));
845 
846   return SelTable.getSelector(N, Args.data());
847 }
848 
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
851                                  unsigned DataLen) {
852   using namespace llvm::support;
853 
854   data_type Result;
855 
856   Result.ID = Reader.getGlobalSelectorID(
857       F, endian::readNext<uint32_t, little, unaligned>(d));
858   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860   Result.InstanceBits = FullInstanceBits & 0x3;
861   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862   Result.FactoryBits = FullFactoryBits & 0x3;
863   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864   unsigned NumInstanceMethods = FullInstanceBits >> 3;
865   unsigned NumFactoryMethods = FullFactoryBits >> 3;
866 
867   // Load instance methods
868   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870             F, endian::readNext<uint32_t, little, unaligned>(d)))
871       Result.Instance.push_back(Method);
872   }
873 
874   // Load factory methods
875   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Factory.push_back(Method);
879   }
880 
881   return Result;
882 }
883 
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885   return llvm::djbHash(a);
886 }
887 
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890   using namespace llvm::support;
891 
892   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894   return std::make_pair(KeyLen, DataLen);
895 }
896 
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899   assert(n >= 2 && d[n-1] == '\0');
900   return StringRef((const char*) d, n-1);
901 }
902 
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
905                                     bool IsModule) {
906   return II.hadMacroDefinition() ||
907          II.isPoisoned() ||
908          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909          II.hasRevertedTokenIDToIdentifier() ||
910          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911           II.getFETokenInfo());
912 }
913 
914 static bool readBit(unsigned &Bits) {
915   bool Value = Bits & 0x1;
916   Bits >>= 1;
917   return Value;
918 }
919 
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921   using namespace llvm::support;
922 
923   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924   return Reader.getGlobalIdentifierID(F, RawID >> 1);
925 }
926 
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928   if (!II.isFromAST()) {
929     II.setIsFromAST();
930     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931     if (isInterestingIdentifier(Reader, II, IsModule))
932       II.setChangedSinceDeserialization();
933   }
934 }
935 
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937                                                    const unsigned char* d,
938                                                    unsigned DataLen) {
939   using namespace llvm::support;
940 
941   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942   bool IsInteresting = RawID & 0x01;
943 
944   // Wipe out the "is interesting" bit.
945   RawID = RawID >> 1;
946 
947   // Build the IdentifierInfo and link the identifier ID with it.
948   IdentifierInfo *II = KnownII;
949   if (!II) {
950     II = &Reader.getIdentifierTable().getOwn(k);
951     KnownII = II;
952   }
953   markIdentifierFromAST(Reader, *II);
954   Reader.markIdentifierUpToDate(II);
955 
956   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957   if (!IsInteresting) {
958     // For uninteresting identifiers, there's nothing else to do. Just notify
959     // the reader that we've finished loading this identifier.
960     Reader.SetIdentifierInfo(ID, II);
961     return II;
962   }
963 
964   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966   bool CPlusPlusOperatorKeyword = readBit(Bits);
967   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968   bool HasRevertedBuiltin = readBit(Bits);
969   bool Poisoned = readBit(Bits);
970   bool ExtensionToken = readBit(Bits);
971   bool HadMacroDefinition = readBit(Bits);
972 
973   assert(Bits == 0 && "Extra bits in the identifier?");
974   DataLen -= 8;
975 
976   // Set or check the various bits in the IdentifierInfo structure.
977   // Token IDs are read-only.
978   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979     II->revertTokenIDToIdentifier();
980   if (!F.isModule())
981     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982   else if (HasRevertedBuiltin && II->getBuiltinID()) {
983     II->revertBuiltin();
984     assert((II->hasRevertedBuiltin() ||
985             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986            "Incorrect ObjC keyword or builtin ID");
987   }
988   assert(II->isExtensionToken() == ExtensionToken &&
989          "Incorrect extension token flag");
990   (void)ExtensionToken;
991   if (Poisoned)
992     II->setIsPoisoned(true);
993   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994          "Incorrect C++ operator keyword flag");
995   (void)CPlusPlusOperatorKeyword;
996 
997   // If this identifier is a macro, deserialize the macro
998   // definition.
999   if (HadMacroDefinition) {
1000     uint32_t MacroDirectivesOffset =
1001         endian::readNext<uint32_t, little, unaligned>(d);
1002     DataLen -= 4;
1003 
1004     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1005   }
1006 
1007   Reader.SetIdentifierInfo(ID, II);
1008 
1009   // Read all of the declarations visible at global scope with this
1010   // name.
1011   if (DataLen > 0) {
1012     SmallVector<uint32_t, 4> DeclIDs;
1013     for (; DataLen > 0; DataLen -= 4)
1014       DeclIDs.push_back(Reader.getGlobalDeclID(
1015           F, endian::readNext<uint32_t, little, unaligned>(d)));
1016     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1017   }
1018 
1019   return II;
1020 }
1021 
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023     : Kind(Name.getNameKind()) {
1024   switch (Kind) {
1025   case DeclarationName::Identifier:
1026     Data = (uint64_t)Name.getAsIdentifierInfo();
1027     break;
1028   case DeclarationName::ObjCZeroArgSelector:
1029   case DeclarationName::ObjCOneArgSelector:
1030   case DeclarationName::ObjCMultiArgSelector:
1031     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1032     break;
1033   case DeclarationName::CXXOperatorName:
1034     Data = Name.getCXXOverloadedOperator();
1035     break;
1036   case DeclarationName::CXXLiteralOperatorName:
1037     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1038     break;
1039   case DeclarationName::CXXDeductionGuideName:
1040     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041                ->getDeclName().getAsIdentifierInfo();
1042     break;
1043   case DeclarationName::CXXConstructorName:
1044   case DeclarationName::CXXDestructorName:
1045   case DeclarationName::CXXConversionFunctionName:
1046   case DeclarationName::CXXUsingDirective:
1047     Data = 0;
1048     break;
1049   }
1050 }
1051 
1052 unsigned DeclarationNameKey::getHash() const {
1053   llvm::FoldingSetNodeID ID;
1054   ID.AddInteger(Kind);
1055 
1056   switch (Kind) {
1057   case DeclarationName::Identifier:
1058   case DeclarationName::CXXLiteralOperatorName:
1059   case DeclarationName::CXXDeductionGuideName:
1060     ID.AddString(((IdentifierInfo*)Data)->getName());
1061     break;
1062   case DeclarationName::ObjCZeroArgSelector:
1063   case DeclarationName::ObjCOneArgSelector:
1064   case DeclarationName::ObjCMultiArgSelector:
1065     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1066     break;
1067   case DeclarationName::CXXOperatorName:
1068     ID.AddInteger((OverloadedOperatorKind)Data);
1069     break;
1070   case DeclarationName::CXXConstructorName:
1071   case DeclarationName::CXXDestructorName:
1072   case DeclarationName::CXXConversionFunctionName:
1073   case DeclarationName::CXXUsingDirective:
1074     break;
1075   }
1076 
1077   return ID.ComputeHash();
1078 }
1079 
1080 ModuleFile *
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082   using namespace llvm::support;
1083 
1084   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085   return Reader.getLocalModuleFile(F, ModuleFileID);
1086 }
1087 
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090   using namespace llvm::support;
1091 
1092   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094   return std::make_pair(KeyLen, DataLen);
1095 }
1096 
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099   using namespace llvm::support;
1100 
1101   auto Kind = (DeclarationName::NameKind)*d++;
1102   uint64_t Data;
1103   switch (Kind) {
1104   case DeclarationName::Identifier:
1105   case DeclarationName::CXXLiteralOperatorName:
1106   case DeclarationName::CXXDeductionGuideName:
1107     Data = (uint64_t)Reader.getLocalIdentifier(
1108         F, endian::readNext<uint32_t, little, unaligned>(d));
1109     break;
1110   case DeclarationName::ObjCZeroArgSelector:
1111   case DeclarationName::ObjCOneArgSelector:
1112   case DeclarationName::ObjCMultiArgSelector:
1113     Data =
1114         (uint64_t)Reader.getLocalSelector(
1115                              F, endian::readNext<uint32_t, little, unaligned>(
1116                                     d)).getAsOpaquePtr();
1117     break;
1118   case DeclarationName::CXXOperatorName:
1119     Data = *d++; // OverloadedOperatorKind
1120     break;
1121   case DeclarationName::CXXConstructorName:
1122   case DeclarationName::CXXDestructorName:
1123   case DeclarationName::CXXConversionFunctionName:
1124   case DeclarationName::CXXUsingDirective:
1125     Data = 0;
1126     break;
1127   }
1128 
1129   return DeclarationNameKey(Kind, Data);
1130 }
1131 
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133                                                  const unsigned char *d,
1134                                                  unsigned DataLen,
1135                                                  data_type_builder &Val) {
1136   using namespace llvm::support;
1137 
1138   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1141   }
1142 }
1143 
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145                                               BitstreamCursor &Cursor,
1146                                               uint64_t Offset,
1147                                               DeclContext *DC) {
1148   assert(Offset != 0);
1149 
1150   SavedStreamPosition SavedPosition(Cursor);
1151   Cursor.JumpToBit(Offset);
1152 
1153   RecordData Record;
1154   StringRef Blob;
1155   unsigned Code = Cursor.ReadCode();
1156   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1157   if (RecCode != DECL_CONTEXT_LEXICAL) {
1158     Error("Expected lexical block");
1159     return true;
1160   }
1161 
1162   assert(!isa<TranslationUnitDecl>(DC) &&
1163          "expected a TU_UPDATE_LEXICAL record for TU");
1164   // If we are handling a C++ class template instantiation, we can see multiple
1165   // lexical updates for the same record. It's important that we select only one
1166   // of them, so that field numbering works properly. Just pick the first one we
1167   // see.
1168   auto &Lex = LexicalDecls[DC];
1169   if (!Lex.first) {
1170     Lex = std::make_pair(
1171         &M, llvm::makeArrayRef(
1172                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1173                     Blob.data()),
1174                 Blob.size() / 4));
1175   }
1176   DC->setHasExternalLexicalStorage(true);
1177   return false;
1178 }
1179 
1180 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1181                                               BitstreamCursor &Cursor,
1182                                               uint64_t Offset,
1183                                               DeclID ID) {
1184   assert(Offset != 0);
1185 
1186   SavedStreamPosition SavedPosition(Cursor);
1187   Cursor.JumpToBit(Offset);
1188 
1189   RecordData Record;
1190   StringRef Blob;
1191   unsigned Code = Cursor.ReadCode();
1192   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1193   if (RecCode != DECL_CONTEXT_VISIBLE) {
1194     Error("Expected visible lookup table block");
1195     return true;
1196   }
1197 
1198   // We can't safely determine the primary context yet, so delay attaching the
1199   // lookup table until we're done with recursive deserialization.
1200   auto *Data = (const unsigned char*)Blob.data();
1201   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1202   return false;
1203 }
1204 
1205 void ASTReader::Error(StringRef Msg) const {
1206   Error(diag::err_fe_pch_malformed, Msg);
1207   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1208       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1209     Diag(diag::note_module_cache_path)
1210       << PP.getHeaderSearchInfo().getModuleCachePath();
1211   }
1212 }
1213 
1214 void ASTReader::Error(unsigned DiagID,
1215                       StringRef Arg1, StringRef Arg2) const {
1216   if (Diags.isDiagnosticInFlight())
1217     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1218   else
1219     Diag(DiagID) << Arg1 << Arg2;
1220 }
1221 
1222 //===----------------------------------------------------------------------===//
1223 // Source Manager Deserialization
1224 //===----------------------------------------------------------------------===//
1225 
1226 /// Read the line table in the source manager block.
1227 /// \returns true if there was an error.
1228 bool ASTReader::ParseLineTable(ModuleFile &F,
1229                                const RecordData &Record) {
1230   unsigned Idx = 0;
1231   LineTableInfo &LineTable = SourceMgr.getLineTable();
1232 
1233   // Parse the file names
1234   std::map<int, int> FileIDs;
1235   FileIDs[-1] = -1; // For unspecified filenames.
1236   for (unsigned I = 0; Record[Idx]; ++I) {
1237     // Extract the file name
1238     auto Filename = ReadPath(F, Record, Idx);
1239     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1240   }
1241   ++Idx;
1242 
1243   // Parse the line entries
1244   std::vector<LineEntry> Entries;
1245   while (Idx < Record.size()) {
1246     int FID = Record[Idx++];
1247     assert(FID >= 0 && "Serialized line entries for non-local file.");
1248     // Remap FileID from 1-based old view.
1249     FID += F.SLocEntryBaseID - 1;
1250 
1251     // Extract the line entries
1252     unsigned NumEntries = Record[Idx++];
1253     assert(NumEntries && "no line entries for file ID");
1254     Entries.clear();
1255     Entries.reserve(NumEntries);
1256     for (unsigned I = 0; I != NumEntries; ++I) {
1257       unsigned FileOffset = Record[Idx++];
1258       unsigned LineNo = Record[Idx++];
1259       int FilenameID = FileIDs[Record[Idx++]];
1260       SrcMgr::CharacteristicKind FileKind
1261         = (SrcMgr::CharacteristicKind)Record[Idx++];
1262       unsigned IncludeOffset = Record[Idx++];
1263       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1264                                        FileKind, IncludeOffset));
1265     }
1266     LineTable.AddEntry(FileID::get(FID), Entries);
1267   }
1268 
1269   return false;
1270 }
1271 
1272 /// Read a source manager block
1273 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1274   using namespace SrcMgr;
1275 
1276   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1277 
1278   // Set the source-location entry cursor to the current position in
1279   // the stream. This cursor will be used to read the contents of the
1280   // source manager block initially, and then lazily read
1281   // source-location entries as needed.
1282   SLocEntryCursor = F.Stream;
1283 
1284   // The stream itself is going to skip over the source manager block.
1285   if (F.Stream.SkipBlock()) {
1286     Error("malformed block record in AST file");
1287     return true;
1288   }
1289 
1290   // Enter the source manager block.
1291   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1292     Error("malformed source manager block record in AST file");
1293     return true;
1294   }
1295 
1296   RecordData Record;
1297   while (true) {
1298     llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1299 
1300     switch (E.Kind) {
1301     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1302     case llvm::BitstreamEntry::Error:
1303       Error("malformed block record in AST file");
1304       return true;
1305     case llvm::BitstreamEntry::EndBlock:
1306       return false;
1307     case llvm::BitstreamEntry::Record:
1308       // The interesting case.
1309       break;
1310     }
1311 
1312     // Read a record.
1313     Record.clear();
1314     StringRef Blob;
1315     switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1316     default:  // Default behavior: ignore.
1317       break;
1318 
1319     case SM_SLOC_FILE_ENTRY:
1320     case SM_SLOC_BUFFER_ENTRY:
1321     case SM_SLOC_EXPANSION_ENTRY:
1322       // Once we hit one of the source location entries, we're done.
1323       return false;
1324     }
1325   }
1326 }
1327 
1328 /// If a header file is not found at the path that we expect it to be
1329 /// and the PCH file was moved from its original location, try to resolve the
1330 /// file by assuming that header+PCH were moved together and the header is in
1331 /// the same place relative to the PCH.
1332 static std::string
1333 resolveFileRelativeToOriginalDir(const std::string &Filename,
1334                                  const std::string &OriginalDir,
1335                                  const std::string &CurrDir) {
1336   assert(OriginalDir != CurrDir &&
1337          "No point trying to resolve the file if the PCH dir didn't change");
1338 
1339   using namespace llvm::sys;
1340 
1341   SmallString<128> filePath(Filename);
1342   fs::make_absolute(filePath);
1343   assert(path::is_absolute(OriginalDir));
1344   SmallString<128> currPCHPath(CurrDir);
1345 
1346   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1347                        fileDirE = path::end(path::parent_path(filePath));
1348   path::const_iterator origDirI = path::begin(OriginalDir),
1349                        origDirE = path::end(OriginalDir);
1350   // Skip the common path components from filePath and OriginalDir.
1351   while (fileDirI != fileDirE && origDirI != origDirE &&
1352          *fileDirI == *origDirI) {
1353     ++fileDirI;
1354     ++origDirI;
1355   }
1356   for (; origDirI != origDirE; ++origDirI)
1357     path::append(currPCHPath, "..");
1358   path::append(currPCHPath, fileDirI, fileDirE);
1359   path::append(currPCHPath, path::filename(Filename));
1360   return currPCHPath.str();
1361 }
1362 
1363 bool ASTReader::ReadSLocEntry(int ID) {
1364   if (ID == 0)
1365     return false;
1366 
1367   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1368     Error("source location entry ID out-of-range for AST file");
1369     return true;
1370   }
1371 
1372   // Local helper to read the (possibly-compressed) buffer data following the
1373   // entry record.
1374   auto ReadBuffer = [this](
1375       BitstreamCursor &SLocEntryCursor,
1376       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1377     RecordData Record;
1378     StringRef Blob;
1379     unsigned Code = SLocEntryCursor.ReadCode();
1380     unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1381 
1382     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1383       if (!llvm::zlib::isAvailable()) {
1384         Error("zlib is not available");
1385         return nullptr;
1386       }
1387       SmallString<0> Uncompressed;
1388       if (llvm::Error E =
1389               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1390         Error("could not decompress embedded file contents: " +
1391               llvm::toString(std::move(E)));
1392         return nullptr;
1393       }
1394       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1395     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1396       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1397     } else {
1398       Error("AST record has invalid code");
1399       return nullptr;
1400     }
1401   };
1402 
1403   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1404   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1405   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1406   unsigned BaseOffset = F->SLocEntryBaseOffset;
1407 
1408   ++NumSLocEntriesRead;
1409   llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1410   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1411     Error("incorrectly-formatted source location entry in AST file");
1412     return true;
1413   }
1414 
1415   RecordData Record;
1416   StringRef Blob;
1417   switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1418   default:
1419     Error("incorrectly-formatted source location entry in AST file");
1420     return true;
1421 
1422   case SM_SLOC_FILE_ENTRY: {
1423     // We will detect whether a file changed and return 'Failure' for it, but
1424     // we will also try to fail gracefully by setting up the SLocEntry.
1425     unsigned InputID = Record[4];
1426     InputFile IF = getInputFile(*F, InputID);
1427     const FileEntry *File = IF.getFile();
1428     bool OverriddenBuffer = IF.isOverridden();
1429 
1430     // Note that we only check if a File was returned. If it was out-of-date
1431     // we have complained but we will continue creating a FileID to recover
1432     // gracefully.
1433     if (!File)
1434       return true;
1435 
1436     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1437     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1438       // This is the module's main file.
1439       IncludeLoc = getImportLocation(F);
1440     }
1441     SrcMgr::CharacteristicKind
1442       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1443     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1444                                         ID, BaseOffset + Record[0]);
1445     SrcMgr::FileInfo &FileInfo =
1446           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1447     FileInfo.NumCreatedFIDs = Record[5];
1448     if (Record[3])
1449       FileInfo.setHasLineDirectives();
1450 
1451     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1452     unsigned NumFileDecls = Record[7];
1453     if (NumFileDecls && ContextObj) {
1454       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1455       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1456                                                              NumFileDecls));
1457     }
1458 
1459     const SrcMgr::ContentCache *ContentCache
1460       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1461     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1462         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1463         !ContentCache->getRawBuffer()) {
1464       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1465       if (!Buffer)
1466         return true;
1467       SourceMgr.overrideFileContents(File, std::move(Buffer));
1468     }
1469 
1470     break;
1471   }
1472 
1473   case SM_SLOC_BUFFER_ENTRY: {
1474     const char *Name = Blob.data();
1475     unsigned Offset = Record[0];
1476     SrcMgr::CharacteristicKind
1477       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1478     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1479     if (IncludeLoc.isInvalid() && F->isModule()) {
1480       IncludeLoc = getImportLocation(F);
1481     }
1482 
1483     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1484     if (!Buffer)
1485       return true;
1486     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1487                            BaseOffset + Offset, IncludeLoc);
1488     break;
1489   }
1490 
1491   case SM_SLOC_EXPANSION_ENTRY: {
1492     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1493     SourceMgr.createExpansionLoc(SpellingLoc,
1494                                      ReadSourceLocation(*F, Record[2]),
1495                                      ReadSourceLocation(*F, Record[3]),
1496                                      Record[5],
1497                                      Record[4],
1498                                      ID,
1499                                      BaseOffset + Record[0]);
1500     break;
1501   }
1502   }
1503 
1504   return false;
1505 }
1506 
1507 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1508   if (ID == 0)
1509     return std::make_pair(SourceLocation(), "");
1510 
1511   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1512     Error("source location entry ID out-of-range for AST file");
1513     return std::make_pair(SourceLocation(), "");
1514   }
1515 
1516   // Find which module file this entry lands in.
1517   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1518   if (!M->isModule())
1519     return std::make_pair(SourceLocation(), "");
1520 
1521   // FIXME: Can we map this down to a particular submodule? That would be
1522   // ideal.
1523   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1524 }
1525 
1526 /// Find the location where the module F is imported.
1527 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1528   if (F->ImportLoc.isValid())
1529     return F->ImportLoc;
1530 
1531   // Otherwise we have a PCH. It's considered to be "imported" at the first
1532   // location of its includer.
1533   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1534     // Main file is the importer.
1535     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1536     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1537   }
1538   return F->ImportedBy[0]->FirstLoc;
1539 }
1540 
1541 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1542 /// specified cursor.  Read the abbreviations that are at the top of the block
1543 /// and then leave the cursor pointing into the block.
1544 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1545   if (Cursor.EnterSubBlock(BlockID))
1546     return true;
1547 
1548   while (true) {
1549     uint64_t Offset = Cursor.GetCurrentBitNo();
1550     unsigned Code = Cursor.ReadCode();
1551 
1552     // We expect all abbrevs to be at the start of the block.
1553     if (Code != llvm::bitc::DEFINE_ABBREV) {
1554       Cursor.JumpToBit(Offset);
1555       return false;
1556     }
1557     Cursor.ReadAbbrevRecord();
1558   }
1559 }
1560 
1561 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1562                            unsigned &Idx) {
1563   Token Tok;
1564   Tok.startToken();
1565   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1566   Tok.setLength(Record[Idx++]);
1567   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1568     Tok.setIdentifierInfo(II);
1569   Tok.setKind((tok::TokenKind)Record[Idx++]);
1570   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1571   return Tok;
1572 }
1573 
1574 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1575   BitstreamCursor &Stream = F.MacroCursor;
1576 
1577   // Keep track of where we are in the stream, then jump back there
1578   // after reading this macro.
1579   SavedStreamPosition SavedPosition(Stream);
1580 
1581   Stream.JumpToBit(Offset);
1582   RecordData Record;
1583   SmallVector<IdentifierInfo*, 16> MacroParams;
1584   MacroInfo *Macro = nullptr;
1585 
1586   while (true) {
1587     // Advance to the next record, but if we get to the end of the block, don't
1588     // pop it (removing all the abbreviations from the cursor) since we want to
1589     // be able to reseek within the block and read entries.
1590     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1591     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1592 
1593     switch (Entry.Kind) {
1594     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1595     case llvm::BitstreamEntry::Error:
1596       Error("malformed block record in AST file");
1597       return Macro;
1598     case llvm::BitstreamEntry::EndBlock:
1599       return Macro;
1600     case llvm::BitstreamEntry::Record:
1601       // The interesting case.
1602       break;
1603     }
1604 
1605     // Read a record.
1606     Record.clear();
1607     PreprocessorRecordTypes RecType =
1608       (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1609     switch (RecType) {
1610     case PP_MODULE_MACRO:
1611     case PP_MACRO_DIRECTIVE_HISTORY:
1612       return Macro;
1613 
1614     case PP_MACRO_OBJECT_LIKE:
1615     case PP_MACRO_FUNCTION_LIKE: {
1616       // If we already have a macro, that means that we've hit the end
1617       // of the definition of the macro we were looking for. We're
1618       // done.
1619       if (Macro)
1620         return Macro;
1621 
1622       unsigned NextIndex = 1; // Skip identifier ID.
1623       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1624       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1625       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1626       MI->setIsUsed(Record[NextIndex++]);
1627       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1628 
1629       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1630         // Decode function-like macro info.
1631         bool isC99VarArgs = Record[NextIndex++];
1632         bool isGNUVarArgs = Record[NextIndex++];
1633         bool hasCommaPasting = Record[NextIndex++];
1634         MacroParams.clear();
1635         unsigned NumArgs = Record[NextIndex++];
1636         for (unsigned i = 0; i != NumArgs; ++i)
1637           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1638 
1639         // Install function-like macro info.
1640         MI->setIsFunctionLike();
1641         if (isC99VarArgs) MI->setIsC99Varargs();
1642         if (isGNUVarArgs) MI->setIsGNUVarargs();
1643         if (hasCommaPasting) MI->setHasCommaPasting();
1644         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1645       }
1646 
1647       // Remember that we saw this macro last so that we add the tokens that
1648       // form its body to it.
1649       Macro = MI;
1650 
1651       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1652           Record[NextIndex]) {
1653         // We have a macro definition. Register the association
1654         PreprocessedEntityID
1655             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1656         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1657         PreprocessingRecord::PPEntityID PPID =
1658             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1659         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1660             PPRec.getPreprocessedEntity(PPID));
1661         if (PPDef)
1662           PPRec.RegisterMacroDefinition(Macro, PPDef);
1663       }
1664 
1665       ++NumMacrosRead;
1666       break;
1667     }
1668 
1669     case PP_TOKEN: {
1670       // If we see a TOKEN before a PP_MACRO_*, then the file is
1671       // erroneous, just pretend we didn't see this.
1672       if (!Macro) break;
1673 
1674       unsigned Idx = 0;
1675       Token Tok = ReadToken(F, Record, Idx);
1676       Macro->AddTokenToBody(Tok);
1677       break;
1678     }
1679     }
1680   }
1681 }
1682 
1683 PreprocessedEntityID
1684 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1685                                          unsigned LocalID) const {
1686   if (!M.ModuleOffsetMap.empty())
1687     ReadModuleOffsetMap(M);
1688 
1689   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1690     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1691   assert(I != M.PreprocessedEntityRemap.end()
1692          && "Invalid index into preprocessed entity index remap");
1693 
1694   return LocalID + I->second;
1695 }
1696 
1697 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1698   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1699 }
1700 
1701 HeaderFileInfoTrait::internal_key_type
1702 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1703   internal_key_type ikey = {FE->getSize(),
1704                             M.HasTimestamps ? FE->getModificationTime() : 0,
1705                             FE->getName(), /*Imported*/ false};
1706   return ikey;
1707 }
1708 
1709 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1710   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1711     return false;
1712 
1713   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1714     return true;
1715 
1716   // Determine whether the actual files are equivalent.
1717   FileManager &FileMgr = Reader.getFileManager();
1718   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1719     if (!Key.Imported)
1720       return FileMgr.getFile(Key.Filename);
1721 
1722     std::string Resolved = Key.Filename;
1723     Reader.ResolveImportedPath(M, Resolved);
1724     return FileMgr.getFile(Resolved);
1725   };
1726 
1727   const FileEntry *FEA = GetFile(a);
1728   const FileEntry *FEB = GetFile(b);
1729   return FEA && FEA == FEB;
1730 }
1731 
1732 std::pair<unsigned, unsigned>
1733 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1734   using namespace llvm::support;
1735 
1736   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1737   unsigned DataLen = (unsigned) *d++;
1738   return std::make_pair(KeyLen, DataLen);
1739 }
1740 
1741 HeaderFileInfoTrait::internal_key_type
1742 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1743   using namespace llvm::support;
1744 
1745   internal_key_type ikey;
1746   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1747   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1748   ikey.Filename = (const char *)d;
1749   ikey.Imported = true;
1750   return ikey;
1751 }
1752 
1753 HeaderFileInfoTrait::data_type
1754 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1755                               unsigned DataLen) {
1756   using namespace llvm::support;
1757 
1758   const unsigned char *End = d + DataLen;
1759   HeaderFileInfo HFI;
1760   unsigned Flags = *d++;
1761   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1762   HFI.isImport |= (Flags >> 5) & 0x01;
1763   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1764   HFI.DirInfo = (Flags >> 1) & 0x07;
1765   HFI.IndexHeaderMapHeader = Flags & 0x01;
1766   // FIXME: Find a better way to handle this. Maybe just store a
1767   // "has been included" flag?
1768   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1769                              HFI.NumIncludes);
1770   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1771       M, endian::readNext<uint32_t, little, unaligned>(d));
1772   if (unsigned FrameworkOffset =
1773           endian::readNext<uint32_t, little, unaligned>(d)) {
1774     // The framework offset is 1 greater than the actual offset,
1775     // since 0 is used as an indicator for "no framework name".
1776     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1777     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1778   }
1779 
1780   assert((End - d) % 4 == 0 &&
1781          "Wrong data length in HeaderFileInfo deserialization");
1782   while (d != End) {
1783     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1784     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1785     LocalSMID >>= 2;
1786 
1787     // This header is part of a module. Associate it with the module to enable
1788     // implicit module import.
1789     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1790     Module *Mod = Reader.getSubmodule(GlobalSMID);
1791     FileManager &FileMgr = Reader.getFileManager();
1792     ModuleMap &ModMap =
1793         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1794 
1795     std::string Filename = key.Filename;
1796     if (key.Imported)
1797       Reader.ResolveImportedPath(M, Filename);
1798     // FIXME: This is not always the right filename-as-written, but we're not
1799     // going to use this information to rebuild the module, so it doesn't make
1800     // a lot of difference.
1801     Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1802     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1803     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1804   }
1805 
1806   // This HeaderFileInfo was externally loaded.
1807   HFI.External = true;
1808   HFI.IsValid = true;
1809   return HFI;
1810 }
1811 
1812 void ASTReader::addPendingMacro(IdentifierInfo *II,
1813                                 ModuleFile *M,
1814                                 uint64_t MacroDirectivesOffset) {
1815   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1816   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1817 }
1818 
1819 void ASTReader::ReadDefinedMacros() {
1820   // Note that we are loading defined macros.
1821   Deserializing Macros(this);
1822 
1823   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1824     BitstreamCursor &MacroCursor = I.MacroCursor;
1825 
1826     // If there was no preprocessor block, skip this file.
1827     if (MacroCursor.getBitcodeBytes().empty())
1828       continue;
1829 
1830     BitstreamCursor Cursor = MacroCursor;
1831     Cursor.JumpToBit(I.MacroStartOffset);
1832 
1833     RecordData Record;
1834     while (true) {
1835       llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1836 
1837       switch (E.Kind) {
1838       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1839       case llvm::BitstreamEntry::Error:
1840         Error("malformed block record in AST file");
1841         return;
1842       case llvm::BitstreamEntry::EndBlock:
1843         goto NextCursor;
1844 
1845       case llvm::BitstreamEntry::Record:
1846         Record.clear();
1847         switch (Cursor.readRecord(E.ID, Record)) {
1848         default:  // Default behavior: ignore.
1849           break;
1850 
1851         case PP_MACRO_OBJECT_LIKE:
1852         case PP_MACRO_FUNCTION_LIKE: {
1853           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1854           if (II->isOutOfDate())
1855             updateOutOfDateIdentifier(*II);
1856           break;
1857         }
1858 
1859         case PP_TOKEN:
1860           // Ignore tokens.
1861           break;
1862         }
1863         break;
1864       }
1865     }
1866     NextCursor:  ;
1867   }
1868 }
1869 
1870 namespace {
1871 
1872   /// Visitor class used to look up identifirs in an AST file.
1873   class IdentifierLookupVisitor {
1874     StringRef Name;
1875     unsigned NameHash;
1876     unsigned PriorGeneration;
1877     unsigned &NumIdentifierLookups;
1878     unsigned &NumIdentifierLookupHits;
1879     IdentifierInfo *Found = nullptr;
1880 
1881   public:
1882     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1883                             unsigned &NumIdentifierLookups,
1884                             unsigned &NumIdentifierLookupHits)
1885       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1886         PriorGeneration(PriorGeneration),
1887         NumIdentifierLookups(NumIdentifierLookups),
1888         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
1889 
1890     bool operator()(ModuleFile &M) {
1891       // If we've already searched this module file, skip it now.
1892       if (M.Generation <= PriorGeneration)
1893         return true;
1894 
1895       ASTIdentifierLookupTable *IdTable
1896         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1897       if (!IdTable)
1898         return false;
1899 
1900       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
1901                                      Found);
1902       ++NumIdentifierLookups;
1903       ASTIdentifierLookupTable::iterator Pos =
1904           IdTable->find_hashed(Name, NameHash, &Trait);
1905       if (Pos == IdTable->end())
1906         return false;
1907 
1908       // Dereferencing the iterator has the effect of building the
1909       // IdentifierInfo node and populating it with the various
1910       // declarations it needs.
1911       ++NumIdentifierLookupHits;
1912       Found = *Pos;
1913       return true;
1914     }
1915 
1916     // Retrieve the identifier info found within the module
1917     // files.
1918     IdentifierInfo *getIdentifierInfo() const { return Found; }
1919   };
1920 
1921 } // namespace
1922 
1923 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1924   // Note that we are loading an identifier.
1925   Deserializing AnIdentifier(this);
1926 
1927   unsigned PriorGeneration = 0;
1928   if (getContext().getLangOpts().Modules)
1929     PriorGeneration = IdentifierGeneration[&II];
1930 
1931   // If there is a global index, look there first to determine which modules
1932   // provably do not have any results for this identifier.
1933   GlobalModuleIndex::HitSet Hits;
1934   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1935   if (!loadGlobalIndex()) {
1936     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1937       HitsPtr = &Hits;
1938     }
1939   }
1940 
1941   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1942                                   NumIdentifierLookups,
1943                                   NumIdentifierLookupHits);
1944   ModuleMgr.visit(Visitor, HitsPtr);
1945   markIdentifierUpToDate(&II);
1946 }
1947 
1948 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1949   if (!II)
1950     return;
1951 
1952   II->setOutOfDate(false);
1953 
1954   // Update the generation for this identifier.
1955   if (getContext().getLangOpts().Modules)
1956     IdentifierGeneration[II] = getGeneration();
1957 }
1958 
1959 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1960                                     const PendingMacroInfo &PMInfo) {
1961   ModuleFile &M = *PMInfo.M;
1962 
1963   BitstreamCursor &Cursor = M.MacroCursor;
1964   SavedStreamPosition SavedPosition(Cursor);
1965   Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1966 
1967   struct ModuleMacroRecord {
1968     SubmoduleID SubModID;
1969     MacroInfo *MI;
1970     SmallVector<SubmoduleID, 8> Overrides;
1971   };
1972   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1973 
1974   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1975   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1976   // macro histroy.
1977   RecordData Record;
1978   while (true) {
1979     llvm::BitstreamEntry Entry =
1980         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1981     if (Entry.Kind != llvm::BitstreamEntry::Record) {
1982       Error("malformed block record in AST file");
1983       return;
1984     }
1985 
1986     Record.clear();
1987     switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1988     case PP_MACRO_DIRECTIVE_HISTORY:
1989       break;
1990 
1991     case PP_MODULE_MACRO: {
1992       ModuleMacros.push_back(ModuleMacroRecord());
1993       auto &Info = ModuleMacros.back();
1994       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1995       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1996       for (int I = 2, N = Record.size(); I != N; ++I)
1997         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1998       continue;
1999     }
2000 
2001     default:
2002       Error("malformed block record in AST file");
2003       return;
2004     }
2005 
2006     // We found the macro directive history; that's the last record
2007     // for this macro.
2008     break;
2009   }
2010 
2011   // Module macros are listed in reverse dependency order.
2012   {
2013     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2014     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2015     for (auto &MMR : ModuleMacros) {
2016       Overrides.clear();
2017       for (unsigned ModID : MMR.Overrides) {
2018         Module *Mod = getSubmodule(ModID);
2019         auto *Macro = PP.getModuleMacro(Mod, II);
2020         assert(Macro && "missing definition for overridden macro");
2021         Overrides.push_back(Macro);
2022       }
2023 
2024       bool Inserted = false;
2025       Module *Owner = getSubmodule(MMR.SubModID);
2026       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2027     }
2028   }
2029 
2030   // Don't read the directive history for a module; we don't have anywhere
2031   // to put it.
2032   if (M.isModule())
2033     return;
2034 
2035   // Deserialize the macro directives history in reverse source-order.
2036   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2037   unsigned Idx = 0, N = Record.size();
2038   while (Idx < N) {
2039     MacroDirective *MD = nullptr;
2040     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2041     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2042     switch (K) {
2043     case MacroDirective::MD_Define: {
2044       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2045       MD = PP.AllocateDefMacroDirective(MI, Loc);
2046       break;
2047     }
2048     case MacroDirective::MD_Undefine:
2049       MD = PP.AllocateUndefMacroDirective(Loc);
2050       break;
2051     case MacroDirective::MD_Visibility:
2052       bool isPublic = Record[Idx++];
2053       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2054       break;
2055     }
2056 
2057     if (!Latest)
2058       Latest = MD;
2059     if (Earliest)
2060       Earliest->setPrevious(MD);
2061     Earliest = MD;
2062   }
2063 
2064   if (Latest)
2065     PP.setLoadedMacroDirective(II, Earliest, Latest);
2066 }
2067 
2068 ASTReader::InputFileInfo
2069 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2070   // Go find this input file.
2071   BitstreamCursor &Cursor = F.InputFilesCursor;
2072   SavedStreamPosition SavedPosition(Cursor);
2073   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2074 
2075   unsigned Code = Cursor.ReadCode();
2076   RecordData Record;
2077   StringRef Blob;
2078 
2079   unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2080   assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2081          "invalid record type for input file");
2082   (void)Result;
2083 
2084   assert(Record[0] == ID && "Bogus stored ID or offset");
2085   InputFileInfo R;
2086   R.StoredSize = static_cast<off_t>(Record[1]);
2087   R.StoredTime = static_cast<time_t>(Record[2]);
2088   R.Overridden = static_cast<bool>(Record[3]);
2089   R.Transient = static_cast<bool>(Record[4]);
2090   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2091   R.Filename = Blob;
2092   ResolveImportedPath(F, R.Filename);
2093   return R;
2094 }
2095 
2096 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2097 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2098   // If this ID is bogus, just return an empty input file.
2099   if (ID == 0 || ID > F.InputFilesLoaded.size())
2100     return InputFile();
2101 
2102   // If we've already loaded this input file, return it.
2103   if (F.InputFilesLoaded[ID-1].getFile())
2104     return F.InputFilesLoaded[ID-1];
2105 
2106   if (F.InputFilesLoaded[ID-1].isNotFound())
2107     return InputFile();
2108 
2109   // Go find this input file.
2110   BitstreamCursor &Cursor = F.InputFilesCursor;
2111   SavedStreamPosition SavedPosition(Cursor);
2112   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2113 
2114   InputFileInfo FI = readInputFileInfo(F, ID);
2115   off_t StoredSize = FI.StoredSize;
2116   time_t StoredTime = FI.StoredTime;
2117   bool Overridden = FI.Overridden;
2118   bool Transient = FI.Transient;
2119   StringRef Filename = FI.Filename;
2120 
2121   const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
2122   // If we didn't find the file, resolve it relative to the
2123   // original directory from which this AST file was created.
2124   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2125       F.OriginalDir != F.BaseDirectory) {
2126     std::string Resolved = resolveFileRelativeToOriginalDir(
2127         Filename, F.OriginalDir, F.BaseDirectory);
2128     if (!Resolved.empty())
2129       File = FileMgr.getFile(Resolved);
2130   }
2131 
2132   // For an overridden file, create a virtual file with the stored
2133   // size/timestamp.
2134   if ((Overridden || Transient) && File == nullptr)
2135     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2136 
2137   if (File == nullptr) {
2138     if (Complain) {
2139       std::string ErrorStr = "could not find file '";
2140       ErrorStr += Filename;
2141       ErrorStr += "' referenced by AST file '";
2142       ErrorStr += F.FileName;
2143       ErrorStr += "'";
2144       Error(ErrorStr);
2145     }
2146     // Record that we didn't find the file.
2147     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2148     return InputFile();
2149   }
2150 
2151   // Check if there was a request to override the contents of the file
2152   // that was part of the precompiled header. Overriding such a file
2153   // can lead to problems when lexing using the source locations from the
2154   // PCH.
2155   SourceManager &SM = getSourceManager();
2156   // FIXME: Reject if the overrides are different.
2157   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2158     if (Complain)
2159       Error(diag::err_fe_pch_file_overridden, Filename);
2160     // After emitting the diagnostic, recover by disabling the override so
2161     // that the original file will be used.
2162     //
2163     // FIXME: This recovery is just as broken as the original state; there may
2164     // be another precompiled module that's using the overridden contents, or
2165     // we might be half way through parsing it. Instead, we should treat the
2166     // overridden contents as belonging to a separate FileEntry.
2167     SM.disableFileContentsOverride(File);
2168     // The FileEntry is a virtual file entry with the size of the contents
2169     // that would override the original contents. Set it to the original's
2170     // size/time.
2171     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2172                             StoredSize, StoredTime);
2173   }
2174 
2175   bool IsOutOfDate = false;
2176 
2177   // For an overridden file, there is nothing to validate.
2178   if (!Overridden && //
2179       (StoredSize != File->getSize() ||
2180        (StoredTime && StoredTime != File->getModificationTime() &&
2181         !DisableValidation)
2182        )) {
2183     if (Complain) {
2184       // Build a list of the PCH imports that got us here (in reverse).
2185       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2186       while (!ImportStack.back()->ImportedBy.empty())
2187         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2188 
2189       // The top-level PCH is stale.
2190       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2191       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2192       if (DiagnosticKind == 0)
2193         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2194       else if (DiagnosticKind == 1)
2195         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2196       else
2197         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2198 
2199       // Print the import stack.
2200       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2201         Diag(diag::note_pch_required_by)
2202           << Filename << ImportStack[0]->FileName;
2203         for (unsigned I = 1; I < ImportStack.size(); ++I)
2204           Diag(diag::note_pch_required_by)
2205             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2206       }
2207 
2208       if (!Diags.isDiagnosticInFlight())
2209         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2210     }
2211 
2212     IsOutOfDate = true;
2213   }
2214   // FIXME: If the file is overridden and we've already opened it,
2215   // issue an error (or split it into a separate FileEntry).
2216 
2217   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2218 
2219   // Note that we've loaded this input file.
2220   F.InputFilesLoaded[ID-1] = IF;
2221   return IF;
2222 }
2223 
2224 /// If we are loading a relocatable PCH or module file, and the filename
2225 /// is not an absolute path, add the system or module root to the beginning of
2226 /// the file name.
2227 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2228   // Resolve relative to the base directory, if we have one.
2229   if (!M.BaseDirectory.empty())
2230     return ResolveImportedPath(Filename, M.BaseDirectory);
2231 }
2232 
2233 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2234   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2235     return;
2236 
2237   SmallString<128> Buffer;
2238   llvm::sys::path::append(Buffer, Prefix, Filename);
2239   Filename.assign(Buffer.begin(), Buffer.end());
2240 }
2241 
2242 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2243   switch (ARR) {
2244   case ASTReader::Failure: return true;
2245   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2246   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2247   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2248   case ASTReader::ConfigurationMismatch:
2249     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2250   case ASTReader::HadErrors: return true;
2251   case ASTReader::Success: return false;
2252   }
2253 
2254   llvm_unreachable("unknown ASTReadResult");
2255 }
2256 
2257 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2258     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2259     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2260     std::string &SuggestedPredefines) {
2261   if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2262     return Failure;
2263 
2264   // Read all of the records in the options block.
2265   RecordData Record;
2266   ASTReadResult Result = Success;
2267   while (true) {
2268     llvm::BitstreamEntry Entry = Stream.advance();
2269 
2270     switch (Entry.Kind) {
2271     case llvm::BitstreamEntry::Error:
2272     case llvm::BitstreamEntry::SubBlock:
2273       return Failure;
2274 
2275     case llvm::BitstreamEntry::EndBlock:
2276       return Result;
2277 
2278     case llvm::BitstreamEntry::Record:
2279       // The interesting case.
2280       break;
2281     }
2282 
2283     // Read and process a record.
2284     Record.clear();
2285     switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2286     case LANGUAGE_OPTIONS: {
2287       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2288       if (ParseLanguageOptions(Record, Complain, Listener,
2289                                AllowCompatibleConfigurationMismatch))
2290         Result = ConfigurationMismatch;
2291       break;
2292     }
2293 
2294     case TARGET_OPTIONS: {
2295       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2296       if (ParseTargetOptions(Record, Complain, Listener,
2297                              AllowCompatibleConfigurationMismatch))
2298         Result = ConfigurationMismatch;
2299       break;
2300     }
2301 
2302     case FILE_SYSTEM_OPTIONS: {
2303       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2304       if (!AllowCompatibleConfigurationMismatch &&
2305           ParseFileSystemOptions(Record, Complain, Listener))
2306         Result = ConfigurationMismatch;
2307       break;
2308     }
2309 
2310     case HEADER_SEARCH_OPTIONS: {
2311       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2312       if (!AllowCompatibleConfigurationMismatch &&
2313           ParseHeaderSearchOptions(Record, Complain, Listener))
2314         Result = ConfigurationMismatch;
2315       break;
2316     }
2317 
2318     case PREPROCESSOR_OPTIONS:
2319       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2320       if (!AllowCompatibleConfigurationMismatch &&
2321           ParsePreprocessorOptions(Record, Complain, Listener,
2322                                    SuggestedPredefines))
2323         Result = ConfigurationMismatch;
2324       break;
2325     }
2326   }
2327 }
2328 
2329 ASTReader::ASTReadResult
2330 ASTReader::ReadControlBlock(ModuleFile &F,
2331                             SmallVectorImpl<ImportedModule> &Loaded,
2332                             const ModuleFile *ImportedBy,
2333                             unsigned ClientLoadCapabilities) {
2334   BitstreamCursor &Stream = F.Stream;
2335   ASTReadResult Result = Success;
2336 
2337   if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2338     Error("malformed block record in AST file");
2339     return Failure;
2340   }
2341 
2342   // Lambda to read the unhashed control block the first time it's called.
2343   //
2344   // For PCM files, the unhashed control block cannot be read until after the
2345   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2346   // need to look ahead before reading the IMPORTS record.  For consistency,
2347   // this block is always read somehow (see BitstreamEntry::EndBlock).
2348   bool HasReadUnhashedControlBlock = false;
2349   auto readUnhashedControlBlockOnce = [&]() {
2350     if (!HasReadUnhashedControlBlock) {
2351       HasReadUnhashedControlBlock = true;
2352       if (ASTReadResult Result =
2353               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2354         return Result;
2355     }
2356     return Success;
2357   };
2358 
2359   // Read all of the records and blocks in the control block.
2360   RecordData Record;
2361   unsigned NumInputs = 0;
2362   unsigned NumUserInputs = 0;
2363   while (true) {
2364     llvm::BitstreamEntry Entry = Stream.advance();
2365 
2366     switch (Entry.Kind) {
2367     case llvm::BitstreamEntry::Error:
2368       Error("malformed block record in AST file");
2369       return Failure;
2370     case llvm::BitstreamEntry::EndBlock: {
2371       // Validate the module before returning.  This call catches an AST with
2372       // no module name and no imports.
2373       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2374         return Result;
2375 
2376       // Validate input files.
2377       const HeaderSearchOptions &HSOpts =
2378           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2379 
2380       // All user input files reside at the index range [0, NumUserInputs), and
2381       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2382       // loaded module files, ignore missing inputs.
2383       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2384           F.Kind != MK_PrebuiltModule) {
2385         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2386 
2387         // If we are reading a module, we will create a verification timestamp,
2388         // so we verify all input files.  Otherwise, verify only user input
2389         // files.
2390 
2391         unsigned N = NumUserInputs;
2392         if (ValidateSystemInputs ||
2393             (HSOpts.ModulesValidateOncePerBuildSession &&
2394              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2395              F.Kind == MK_ImplicitModule))
2396           N = NumInputs;
2397 
2398         for (unsigned I = 0; I < N; ++I) {
2399           InputFile IF = getInputFile(F, I+1, Complain);
2400           if (!IF.getFile() || IF.isOutOfDate())
2401             return OutOfDate;
2402         }
2403       }
2404 
2405       if (Listener)
2406         Listener->visitModuleFile(F.FileName, F.Kind);
2407 
2408       if (Listener && Listener->needsInputFileVisitation()) {
2409         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2410                                                                 : NumUserInputs;
2411         for (unsigned I = 0; I < N; ++I) {
2412           bool IsSystem = I >= NumUserInputs;
2413           InputFileInfo FI = readInputFileInfo(F, I+1);
2414           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2415                                    F.Kind == MK_ExplicitModule ||
2416                                    F.Kind == MK_PrebuiltModule);
2417         }
2418       }
2419 
2420       return Result;
2421     }
2422 
2423     case llvm::BitstreamEntry::SubBlock:
2424       switch (Entry.ID) {
2425       case INPUT_FILES_BLOCK_ID:
2426         F.InputFilesCursor = Stream;
2427         if (Stream.SkipBlock() || // Skip with the main cursor
2428             // Read the abbreviations
2429             ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2430           Error("malformed block record in AST file");
2431           return Failure;
2432         }
2433         continue;
2434 
2435       case OPTIONS_BLOCK_ID:
2436         // If we're reading the first module for this group, check its options
2437         // are compatible with ours. For modules it imports, no further checking
2438         // is required, because we checked them when we built it.
2439         if (Listener && !ImportedBy) {
2440           // Should we allow the configuration of the module file to differ from
2441           // the configuration of the current translation unit in a compatible
2442           // way?
2443           //
2444           // FIXME: Allow this for files explicitly specified with -include-pch.
2445           bool AllowCompatibleConfigurationMismatch =
2446               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2447 
2448           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2449                                     AllowCompatibleConfigurationMismatch,
2450                                     *Listener, SuggestedPredefines);
2451           if (Result == Failure) {
2452             Error("malformed block record in AST file");
2453             return Result;
2454           }
2455 
2456           if (DisableValidation ||
2457               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2458             Result = Success;
2459 
2460           // If we can't load the module, exit early since we likely
2461           // will rebuild the module anyway. The stream may be in the
2462           // middle of a block.
2463           if (Result != Success)
2464             return Result;
2465         } else if (Stream.SkipBlock()) {
2466           Error("malformed block record in AST file");
2467           return Failure;
2468         }
2469         continue;
2470 
2471       default:
2472         if (Stream.SkipBlock()) {
2473           Error("malformed block record in AST file");
2474           return Failure;
2475         }
2476         continue;
2477       }
2478 
2479     case llvm::BitstreamEntry::Record:
2480       // The interesting case.
2481       break;
2482     }
2483 
2484     // Read and process a record.
2485     Record.clear();
2486     StringRef Blob;
2487     switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2488     case METADATA: {
2489       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2490         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2491           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2492                                         : diag::err_pch_version_too_new);
2493         return VersionMismatch;
2494       }
2495 
2496       bool hasErrors = Record[7];
2497       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2498         Diag(diag::err_pch_with_compiler_errors);
2499         return HadErrors;
2500       }
2501       if (hasErrors) {
2502         Diags.ErrorOccurred = true;
2503         Diags.UncompilableErrorOccurred = true;
2504         Diags.UnrecoverableErrorOccurred = true;
2505       }
2506 
2507       F.RelocatablePCH = Record[4];
2508       // Relative paths in a relocatable PCH are relative to our sysroot.
2509       if (F.RelocatablePCH)
2510         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2511 
2512       F.HasTimestamps = Record[5];
2513 
2514       F.PCHHasObjectFile = Record[6];
2515 
2516       const std::string &CurBranch = getClangFullRepositoryVersion();
2517       StringRef ASTBranch = Blob;
2518       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2519         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2520           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2521         return VersionMismatch;
2522       }
2523       break;
2524     }
2525 
2526     case IMPORTS: {
2527       // Validate the AST before processing any imports (otherwise, untangling
2528       // them can be error-prone and expensive).  A module will have a name and
2529       // will already have been validated, but this catches the PCH case.
2530       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2531         return Result;
2532 
2533       // Load each of the imported PCH files.
2534       unsigned Idx = 0, N = Record.size();
2535       while (Idx < N) {
2536         // Read information about the AST file.
2537         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2538         // The import location will be the local one for now; we will adjust
2539         // all import locations of module imports after the global source
2540         // location info are setup, in ReadAST.
2541         SourceLocation ImportLoc =
2542             ReadUntranslatedSourceLocation(Record[Idx++]);
2543         off_t StoredSize = (off_t)Record[Idx++];
2544         time_t StoredModTime = (time_t)Record[Idx++];
2545         ASTFileSignature StoredSignature = {
2546             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2547               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2548               (uint32_t)Record[Idx++]}}};
2549 
2550         std::string ImportedName = ReadString(Record, Idx);
2551         std::string ImportedFile;
2552 
2553         // For prebuilt and explicit modules first consult the file map for
2554         // an override. Note that here we don't search prebuilt module
2555         // directories, only the explicit name to file mappings. Also, we will
2556         // still verify the size/signature making sure it is essentially the
2557         // same file but perhaps in a different location.
2558         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2559           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2560             ImportedName, /*FileMapOnly*/ true);
2561 
2562         if (ImportedFile.empty())
2563           ImportedFile = ReadPath(F, Record, Idx);
2564         else
2565           SkipPath(Record, Idx);
2566 
2567         // If our client can't cope with us being out of date, we can't cope with
2568         // our dependency being missing.
2569         unsigned Capabilities = ClientLoadCapabilities;
2570         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2571           Capabilities &= ~ARR_Missing;
2572 
2573         // Load the AST file.
2574         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2575                                   Loaded, StoredSize, StoredModTime,
2576                                   StoredSignature, Capabilities);
2577 
2578         // If we diagnosed a problem, produce a backtrace.
2579         if (isDiagnosedResult(Result, Capabilities))
2580           Diag(diag::note_module_file_imported_by)
2581               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2582 
2583         switch (Result) {
2584         case Failure: return Failure;
2585           // If we have to ignore the dependency, we'll have to ignore this too.
2586         case Missing:
2587         case OutOfDate: return OutOfDate;
2588         case VersionMismatch: return VersionMismatch;
2589         case ConfigurationMismatch: return ConfigurationMismatch;
2590         case HadErrors: return HadErrors;
2591         case Success: break;
2592         }
2593       }
2594       break;
2595     }
2596 
2597     case ORIGINAL_FILE:
2598       F.OriginalSourceFileID = FileID::get(Record[0]);
2599       F.ActualOriginalSourceFileName = Blob;
2600       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2601       ResolveImportedPath(F, F.OriginalSourceFileName);
2602       break;
2603 
2604     case ORIGINAL_FILE_ID:
2605       F.OriginalSourceFileID = FileID::get(Record[0]);
2606       break;
2607 
2608     case ORIGINAL_PCH_DIR:
2609       F.OriginalDir = Blob;
2610       break;
2611 
2612     case MODULE_NAME:
2613       F.ModuleName = Blob;
2614       if (Listener)
2615         Listener->ReadModuleName(F.ModuleName);
2616 
2617       // Validate the AST as soon as we have a name so we can exit early on
2618       // failure.
2619       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2620         return Result;
2621 
2622       break;
2623 
2624     case MODULE_DIRECTORY: {
2625       assert(!F.ModuleName.empty() &&
2626              "MODULE_DIRECTORY found before MODULE_NAME");
2627       // If we've already loaded a module map file covering this module, we may
2628       // have a better path for it (relative to the current build).
2629       Module *M = PP.getHeaderSearchInfo().lookupModule(
2630           F.ModuleName, /*AllowSearch*/ true,
2631           /*AllowExtraModuleMapSearch*/ true);
2632       if (M && M->Directory) {
2633         // If we're implicitly loading a module, the base directory can't
2634         // change between the build and use.
2635         // Don't emit module relocation error if we have -fno-validate-pch
2636         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2637             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2638           const DirectoryEntry *BuildDir =
2639               PP.getFileManager().getDirectory(Blob);
2640           if (!BuildDir || BuildDir != M->Directory) {
2641             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2642               Diag(diag::err_imported_module_relocated)
2643                   << F.ModuleName << Blob << M->Directory->getName();
2644             return OutOfDate;
2645           }
2646         }
2647         F.BaseDirectory = M->Directory->getName();
2648       } else {
2649         F.BaseDirectory = Blob;
2650       }
2651       break;
2652     }
2653 
2654     case MODULE_MAP_FILE:
2655       if (ASTReadResult Result =
2656               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2657         return Result;
2658       break;
2659 
2660     case INPUT_FILE_OFFSETS:
2661       NumInputs = Record[0];
2662       NumUserInputs = Record[1];
2663       F.InputFileOffsets =
2664           (const llvm::support::unaligned_uint64_t *)Blob.data();
2665       F.InputFilesLoaded.resize(NumInputs);
2666       F.NumUserInputFiles = NumUserInputs;
2667       break;
2668     }
2669   }
2670 }
2671 
2672 ASTReader::ASTReadResult
2673 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2674   BitstreamCursor &Stream = F.Stream;
2675 
2676   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2677     Error("malformed block record in AST file");
2678     return Failure;
2679   }
2680 
2681   // Read all of the records and blocks for the AST file.
2682   RecordData Record;
2683   while (true) {
2684     llvm::BitstreamEntry Entry = Stream.advance();
2685 
2686     switch (Entry.Kind) {
2687     case llvm::BitstreamEntry::Error:
2688       Error("error at end of module block in AST file");
2689       return Failure;
2690     case llvm::BitstreamEntry::EndBlock:
2691       // Outside of C++, we do not store a lookup map for the translation unit.
2692       // Instead, mark it as needing a lookup map to be built if this module
2693       // contains any declarations lexically within it (which it always does!).
2694       // This usually has no cost, since we very rarely need the lookup map for
2695       // the translation unit outside C++.
2696       if (ASTContext *Ctx = ContextObj) {
2697         DeclContext *DC = Ctx->getTranslationUnitDecl();
2698         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2699           DC->setMustBuildLookupTable();
2700       }
2701 
2702       return Success;
2703     case llvm::BitstreamEntry::SubBlock:
2704       switch (Entry.ID) {
2705       case DECLTYPES_BLOCK_ID:
2706         // We lazily load the decls block, but we want to set up the
2707         // DeclsCursor cursor to point into it.  Clone our current bitcode
2708         // cursor to it, enter the block and read the abbrevs in that block.
2709         // With the main cursor, we just skip over it.
2710         F.DeclsCursor = Stream;
2711         if (Stream.SkipBlock() ||  // Skip with the main cursor.
2712             // Read the abbrevs.
2713             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2714           Error("malformed block record in AST file");
2715           return Failure;
2716         }
2717         break;
2718 
2719       case PREPROCESSOR_BLOCK_ID:
2720         F.MacroCursor = Stream;
2721         if (!PP.getExternalSource())
2722           PP.setExternalSource(this);
2723 
2724         if (Stream.SkipBlock() ||
2725             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2726           Error("malformed block record in AST file");
2727           return Failure;
2728         }
2729         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2730         break;
2731 
2732       case PREPROCESSOR_DETAIL_BLOCK_ID:
2733         F.PreprocessorDetailCursor = Stream;
2734         if (Stream.SkipBlock() ||
2735             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2736                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2737               Error("malformed preprocessor detail record in AST file");
2738               return Failure;
2739             }
2740         F.PreprocessorDetailStartOffset
2741         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2742 
2743         if (!PP.getPreprocessingRecord())
2744           PP.createPreprocessingRecord();
2745         if (!PP.getPreprocessingRecord()->getExternalSource())
2746           PP.getPreprocessingRecord()->SetExternalSource(*this);
2747         break;
2748 
2749       case SOURCE_MANAGER_BLOCK_ID:
2750         if (ReadSourceManagerBlock(F))
2751           return Failure;
2752         break;
2753 
2754       case SUBMODULE_BLOCK_ID:
2755         if (ASTReadResult Result =
2756                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2757           return Result;
2758         break;
2759 
2760       case COMMENTS_BLOCK_ID: {
2761         BitstreamCursor C = Stream;
2762         if (Stream.SkipBlock() ||
2763             ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2764           Error("malformed comments block in AST file");
2765           return Failure;
2766         }
2767         CommentsCursors.push_back(std::make_pair(C, &F));
2768         break;
2769       }
2770 
2771       default:
2772         if (Stream.SkipBlock()) {
2773           Error("malformed block record in AST file");
2774           return Failure;
2775         }
2776         break;
2777       }
2778       continue;
2779 
2780     case llvm::BitstreamEntry::Record:
2781       // The interesting case.
2782       break;
2783     }
2784 
2785     // Read and process a record.
2786     Record.clear();
2787     StringRef Blob;
2788     auto RecordType =
2789         (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
2790 
2791     // If we're not loading an AST context, we don't care about most records.
2792     if (!ContextObj) {
2793       switch (RecordType) {
2794       case IDENTIFIER_TABLE:
2795       case IDENTIFIER_OFFSET:
2796       case INTERESTING_IDENTIFIERS:
2797       case STATISTICS:
2798       case PP_CONDITIONAL_STACK:
2799       case PP_COUNTER_VALUE:
2800       case SOURCE_LOCATION_OFFSETS:
2801       case MODULE_OFFSET_MAP:
2802       case SOURCE_MANAGER_LINE_TABLE:
2803       case SOURCE_LOCATION_PRELOADS:
2804       case PPD_ENTITIES_OFFSETS:
2805       case HEADER_SEARCH_TABLE:
2806       case IMPORTED_MODULES:
2807       case MACRO_OFFSET:
2808         break;
2809       default:
2810         continue;
2811       }
2812     }
2813 
2814     switch (RecordType) {
2815     default:  // Default behavior: ignore.
2816       break;
2817 
2818     case TYPE_OFFSET: {
2819       if (F.LocalNumTypes != 0) {
2820         Error("duplicate TYPE_OFFSET record in AST file");
2821         return Failure;
2822       }
2823       F.TypeOffsets = (const uint32_t *)Blob.data();
2824       F.LocalNumTypes = Record[0];
2825       unsigned LocalBaseTypeIndex = Record[1];
2826       F.BaseTypeIndex = getTotalNumTypes();
2827 
2828       if (F.LocalNumTypes > 0) {
2829         // Introduce the global -> local mapping for types within this module.
2830         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2831 
2832         // Introduce the local -> global mapping for types within this module.
2833         F.TypeRemap.insertOrReplace(
2834           std::make_pair(LocalBaseTypeIndex,
2835                          F.BaseTypeIndex - LocalBaseTypeIndex));
2836 
2837         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2838       }
2839       break;
2840     }
2841 
2842     case DECL_OFFSET: {
2843       if (F.LocalNumDecls != 0) {
2844         Error("duplicate DECL_OFFSET record in AST file");
2845         return Failure;
2846       }
2847       F.DeclOffsets = (const DeclOffset *)Blob.data();
2848       F.LocalNumDecls = Record[0];
2849       unsigned LocalBaseDeclID = Record[1];
2850       F.BaseDeclID = getTotalNumDecls();
2851 
2852       if (F.LocalNumDecls > 0) {
2853         // Introduce the global -> local mapping for declarations within this
2854         // module.
2855         GlobalDeclMap.insert(
2856           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2857 
2858         // Introduce the local -> global mapping for declarations within this
2859         // module.
2860         F.DeclRemap.insertOrReplace(
2861           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2862 
2863         // Introduce the global -> local mapping for declarations within this
2864         // module.
2865         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2866 
2867         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2868       }
2869       break;
2870     }
2871 
2872     case TU_UPDATE_LEXICAL: {
2873       DeclContext *TU = ContextObj->getTranslationUnitDecl();
2874       LexicalContents Contents(
2875           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2876               Blob.data()),
2877           static_cast<unsigned int>(Blob.size() / 4));
2878       TULexicalDecls.push_back(std::make_pair(&F, Contents));
2879       TU->setHasExternalLexicalStorage(true);
2880       break;
2881     }
2882 
2883     case UPDATE_VISIBLE: {
2884       unsigned Idx = 0;
2885       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2886       auto *Data = (const unsigned char*)Blob.data();
2887       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
2888       // If we've already loaded the decl, perform the updates when we finish
2889       // loading this block.
2890       if (Decl *D = GetExistingDecl(ID))
2891         PendingUpdateRecords.push_back(
2892             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
2893       break;
2894     }
2895 
2896     case IDENTIFIER_TABLE:
2897       F.IdentifierTableData = Blob.data();
2898       if (Record[0]) {
2899         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2900             (const unsigned char *)F.IdentifierTableData + Record[0],
2901             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2902             (const unsigned char *)F.IdentifierTableData,
2903             ASTIdentifierLookupTrait(*this, F));
2904 
2905         PP.getIdentifierTable().setExternalIdentifierLookup(this);
2906       }
2907       break;
2908 
2909     case IDENTIFIER_OFFSET: {
2910       if (F.LocalNumIdentifiers != 0) {
2911         Error("duplicate IDENTIFIER_OFFSET record in AST file");
2912         return Failure;
2913       }
2914       F.IdentifierOffsets = (const uint32_t *)Blob.data();
2915       F.LocalNumIdentifiers = Record[0];
2916       unsigned LocalBaseIdentifierID = Record[1];
2917       F.BaseIdentifierID = getTotalNumIdentifiers();
2918 
2919       if (F.LocalNumIdentifiers > 0) {
2920         // Introduce the global -> local mapping for identifiers within this
2921         // module.
2922         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2923                                                   &F));
2924 
2925         // Introduce the local -> global mapping for identifiers within this
2926         // module.
2927         F.IdentifierRemap.insertOrReplace(
2928           std::make_pair(LocalBaseIdentifierID,
2929                          F.BaseIdentifierID - LocalBaseIdentifierID));
2930 
2931         IdentifiersLoaded.resize(IdentifiersLoaded.size()
2932                                  + F.LocalNumIdentifiers);
2933       }
2934       break;
2935     }
2936 
2937     case INTERESTING_IDENTIFIERS:
2938       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2939       break;
2940 
2941     case EAGERLY_DESERIALIZED_DECLS:
2942       // FIXME: Skip reading this record if our ASTConsumer doesn't care
2943       // about "interesting" decls (for instance, if we're building a module).
2944       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2945         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2946       break;
2947 
2948     case MODULAR_CODEGEN_DECLS:
2949       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
2950       // them (ie: if we're not codegenerating this module).
2951       if (F.Kind == MK_MainFile)
2952         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2953           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2954       break;
2955 
2956     case SPECIAL_TYPES:
2957       if (SpecialTypes.empty()) {
2958         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2959           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2960         break;
2961       }
2962 
2963       if (SpecialTypes.size() != Record.size()) {
2964         Error("invalid special-types record");
2965         return Failure;
2966       }
2967 
2968       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2969         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2970         if (!SpecialTypes[I])
2971           SpecialTypes[I] = ID;
2972         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2973         // merge step?
2974       }
2975       break;
2976 
2977     case STATISTICS:
2978       TotalNumStatements += Record[0];
2979       TotalNumMacros += Record[1];
2980       TotalLexicalDeclContexts += Record[2];
2981       TotalVisibleDeclContexts += Record[3];
2982       break;
2983 
2984     case UNUSED_FILESCOPED_DECLS:
2985       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2986         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2987       break;
2988 
2989     case DELEGATING_CTORS:
2990       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2991         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2992       break;
2993 
2994     case WEAK_UNDECLARED_IDENTIFIERS:
2995       if (Record.size() % 4 != 0) {
2996         Error("invalid weak identifiers record");
2997         return Failure;
2998       }
2999 
3000       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3001       // files. This isn't the way to do it :)
3002       WeakUndeclaredIdentifiers.clear();
3003 
3004       // Translate the weak, undeclared identifiers into global IDs.
3005       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3006         WeakUndeclaredIdentifiers.push_back(
3007           getGlobalIdentifierID(F, Record[I++]));
3008         WeakUndeclaredIdentifiers.push_back(
3009           getGlobalIdentifierID(F, Record[I++]));
3010         WeakUndeclaredIdentifiers.push_back(
3011           ReadSourceLocation(F, Record, I).getRawEncoding());
3012         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3013       }
3014       break;
3015 
3016     case SELECTOR_OFFSETS: {
3017       F.SelectorOffsets = (const uint32_t *)Blob.data();
3018       F.LocalNumSelectors = Record[0];
3019       unsigned LocalBaseSelectorID = Record[1];
3020       F.BaseSelectorID = getTotalNumSelectors();
3021 
3022       if (F.LocalNumSelectors > 0) {
3023         // Introduce the global -> local mapping for selectors within this
3024         // module.
3025         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3026 
3027         // Introduce the local -> global mapping for selectors within this
3028         // module.
3029         F.SelectorRemap.insertOrReplace(
3030           std::make_pair(LocalBaseSelectorID,
3031                          F.BaseSelectorID - LocalBaseSelectorID));
3032 
3033         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3034       }
3035       break;
3036     }
3037 
3038     case METHOD_POOL:
3039       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3040       if (Record[0])
3041         F.SelectorLookupTable
3042           = ASTSelectorLookupTable::Create(
3043                         F.SelectorLookupTableData + Record[0],
3044                         F.SelectorLookupTableData,
3045                         ASTSelectorLookupTrait(*this, F));
3046       TotalNumMethodPoolEntries += Record[1];
3047       break;
3048 
3049     case REFERENCED_SELECTOR_POOL:
3050       if (!Record.empty()) {
3051         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3052           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3053                                                                 Record[Idx++]));
3054           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3055                                               getRawEncoding());
3056         }
3057       }
3058       break;
3059 
3060     case PP_CONDITIONAL_STACK:
3061       if (!Record.empty()) {
3062         unsigned Idx = 0, End = Record.size() - 1;
3063         bool ReachedEOFWhileSkipping = Record[Idx++];
3064         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3065         if (ReachedEOFWhileSkipping) {
3066           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3067           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3068           bool FoundNonSkipPortion = Record[Idx++];
3069           bool FoundElse = Record[Idx++];
3070           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3071           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3072                            FoundElse, ElseLoc);
3073         }
3074         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3075         while (Idx < End) {
3076           auto Loc = ReadSourceLocation(F, Record, Idx);
3077           bool WasSkipping = Record[Idx++];
3078           bool FoundNonSkip = Record[Idx++];
3079           bool FoundElse = Record[Idx++];
3080           ConditionalStack.push_back(
3081               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3082         }
3083         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3084       }
3085       break;
3086 
3087     case PP_COUNTER_VALUE:
3088       if (!Record.empty() && Listener)
3089         Listener->ReadCounter(F, Record[0]);
3090       break;
3091 
3092     case FILE_SORTED_DECLS:
3093       F.FileSortedDecls = (const DeclID *)Blob.data();
3094       F.NumFileSortedDecls = Record[0];
3095       break;
3096 
3097     case SOURCE_LOCATION_OFFSETS: {
3098       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3099       F.LocalNumSLocEntries = Record[0];
3100       unsigned SLocSpaceSize = Record[1];
3101       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3102           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3103                                               SLocSpaceSize);
3104       if (!F.SLocEntryBaseID) {
3105         Error("ran out of source locations");
3106         break;
3107       }
3108       // Make our entry in the range map. BaseID is negative and growing, so
3109       // we invert it. Because we invert it, though, we need the other end of
3110       // the range.
3111       unsigned RangeStart =
3112           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3113       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3114       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3115 
3116       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3117       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3118       GlobalSLocOffsetMap.insert(
3119           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3120                            - SLocSpaceSize,&F));
3121 
3122       // Initialize the remapping table.
3123       // Invalid stays invalid.
3124       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3125       // This module. Base was 2 when being compiled.
3126       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3127                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3128 
3129       TotalNumSLocEntries += F.LocalNumSLocEntries;
3130       break;
3131     }
3132 
3133     case MODULE_OFFSET_MAP:
3134       F.ModuleOffsetMap = Blob;
3135       break;
3136 
3137     case SOURCE_MANAGER_LINE_TABLE:
3138       if (ParseLineTable(F, Record))
3139         return Failure;
3140       break;
3141 
3142     case SOURCE_LOCATION_PRELOADS: {
3143       // Need to transform from the local view (1-based IDs) to the global view,
3144       // which is based off F.SLocEntryBaseID.
3145       if (!F.PreloadSLocEntries.empty()) {
3146         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3147         return Failure;
3148       }
3149 
3150       F.PreloadSLocEntries.swap(Record);
3151       break;
3152     }
3153 
3154     case EXT_VECTOR_DECLS:
3155       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3156         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3157       break;
3158 
3159     case VTABLE_USES:
3160       if (Record.size() % 3 != 0) {
3161         Error("Invalid VTABLE_USES record");
3162         return Failure;
3163       }
3164 
3165       // Later tables overwrite earlier ones.
3166       // FIXME: Modules will have some trouble with this. This is clearly not
3167       // the right way to do this.
3168       VTableUses.clear();
3169 
3170       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3171         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3172         VTableUses.push_back(
3173           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3174         VTableUses.push_back(Record[Idx++]);
3175       }
3176       break;
3177 
3178     case PENDING_IMPLICIT_INSTANTIATIONS:
3179       if (PendingInstantiations.size() % 2 != 0) {
3180         Error("Invalid existing PendingInstantiations");
3181         return Failure;
3182       }
3183 
3184       if (Record.size() % 2 != 0) {
3185         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3186         return Failure;
3187       }
3188 
3189       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3190         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3191         PendingInstantiations.push_back(
3192           ReadSourceLocation(F, Record, I).getRawEncoding());
3193       }
3194       break;
3195 
3196     case SEMA_DECL_REFS:
3197       if (Record.size() != 3) {
3198         Error("Invalid SEMA_DECL_REFS block");
3199         return Failure;
3200       }
3201       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3202         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3203       break;
3204 
3205     case PPD_ENTITIES_OFFSETS: {
3206       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3207       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3208       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3209 
3210       unsigned LocalBasePreprocessedEntityID = Record[0];
3211 
3212       unsigned StartingID;
3213       if (!PP.getPreprocessingRecord())
3214         PP.createPreprocessingRecord();
3215       if (!PP.getPreprocessingRecord()->getExternalSource())
3216         PP.getPreprocessingRecord()->SetExternalSource(*this);
3217       StartingID
3218         = PP.getPreprocessingRecord()
3219             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3220       F.BasePreprocessedEntityID = StartingID;
3221 
3222       if (F.NumPreprocessedEntities > 0) {
3223         // Introduce the global -> local mapping for preprocessed entities in
3224         // this module.
3225         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3226 
3227         // Introduce the local -> global mapping for preprocessed entities in
3228         // this module.
3229         F.PreprocessedEntityRemap.insertOrReplace(
3230           std::make_pair(LocalBasePreprocessedEntityID,
3231             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3232       }
3233 
3234       break;
3235     }
3236 
3237     case PPD_SKIPPED_RANGES: {
3238       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3239       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3240       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3241 
3242       if (!PP.getPreprocessingRecord())
3243         PP.createPreprocessingRecord();
3244       if (!PP.getPreprocessingRecord()->getExternalSource())
3245         PP.getPreprocessingRecord()->SetExternalSource(*this);
3246       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3247           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3248 
3249       if (F.NumPreprocessedSkippedRanges > 0)
3250         GlobalSkippedRangeMap.insert(
3251             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3252       break;
3253     }
3254 
3255     case DECL_UPDATE_OFFSETS:
3256       if (Record.size() % 2 != 0) {
3257         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3258         return Failure;
3259       }
3260       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3261         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3262         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3263 
3264         // If we've already loaded the decl, perform the updates when we finish
3265         // loading this block.
3266         if (Decl *D = GetExistingDecl(ID))
3267           PendingUpdateRecords.push_back(
3268               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3269       }
3270       break;
3271 
3272     case OBJC_CATEGORIES_MAP:
3273       if (F.LocalNumObjCCategoriesInMap != 0) {
3274         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3275         return Failure;
3276       }
3277 
3278       F.LocalNumObjCCategoriesInMap = Record[0];
3279       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3280       break;
3281 
3282     case OBJC_CATEGORIES:
3283       F.ObjCCategories.swap(Record);
3284       break;
3285 
3286     case CUDA_SPECIAL_DECL_REFS:
3287       // Later tables overwrite earlier ones.
3288       // FIXME: Modules will have trouble with this.
3289       CUDASpecialDeclRefs.clear();
3290       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3291         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3292       break;
3293 
3294     case HEADER_SEARCH_TABLE:
3295       F.HeaderFileInfoTableData = Blob.data();
3296       F.LocalNumHeaderFileInfos = Record[1];
3297       if (Record[0]) {
3298         F.HeaderFileInfoTable
3299           = HeaderFileInfoLookupTable::Create(
3300                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3301                    (const unsigned char *)F.HeaderFileInfoTableData,
3302                    HeaderFileInfoTrait(*this, F,
3303                                        &PP.getHeaderSearchInfo(),
3304                                        Blob.data() + Record[2]));
3305 
3306         PP.getHeaderSearchInfo().SetExternalSource(this);
3307         if (!PP.getHeaderSearchInfo().getExternalLookup())
3308           PP.getHeaderSearchInfo().SetExternalLookup(this);
3309       }
3310       break;
3311 
3312     case FP_PRAGMA_OPTIONS:
3313       // Later tables overwrite earlier ones.
3314       FPPragmaOptions.swap(Record);
3315       break;
3316 
3317     case OPENCL_EXTENSIONS:
3318       for (unsigned I = 0, E = Record.size(); I != E; ) {
3319         auto Name = ReadString(Record, I);
3320         auto &Opt = OpenCLExtensions.OptMap[Name];
3321         Opt.Supported = Record[I++] != 0;
3322         Opt.Enabled = Record[I++] != 0;
3323         Opt.Avail = Record[I++];
3324         Opt.Core = Record[I++];
3325       }
3326       break;
3327 
3328     case OPENCL_EXTENSION_TYPES:
3329       for (unsigned I = 0, E = Record.size(); I != E;) {
3330         auto TypeID = static_cast<::TypeID>(Record[I++]);
3331         auto *Type = GetType(TypeID).getTypePtr();
3332         auto NumExt = static_cast<unsigned>(Record[I++]);
3333         for (unsigned II = 0; II != NumExt; ++II) {
3334           auto Ext = ReadString(Record, I);
3335           OpenCLTypeExtMap[Type].insert(Ext);
3336         }
3337       }
3338       break;
3339 
3340     case OPENCL_EXTENSION_DECLS:
3341       for (unsigned I = 0, E = Record.size(); I != E;) {
3342         auto DeclID = static_cast<::DeclID>(Record[I++]);
3343         auto *Decl = GetDecl(DeclID);
3344         auto NumExt = static_cast<unsigned>(Record[I++]);
3345         for (unsigned II = 0; II != NumExt; ++II) {
3346           auto Ext = ReadString(Record, I);
3347           OpenCLDeclExtMap[Decl].insert(Ext);
3348         }
3349       }
3350       break;
3351 
3352     case TENTATIVE_DEFINITIONS:
3353       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3354         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3355       break;
3356 
3357     case KNOWN_NAMESPACES:
3358       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3359         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3360       break;
3361 
3362     case UNDEFINED_BUT_USED:
3363       if (UndefinedButUsed.size() % 2 != 0) {
3364         Error("Invalid existing UndefinedButUsed");
3365         return Failure;
3366       }
3367 
3368       if (Record.size() % 2 != 0) {
3369         Error("invalid undefined-but-used record");
3370         return Failure;
3371       }
3372       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3373         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3374         UndefinedButUsed.push_back(
3375             ReadSourceLocation(F, Record, I).getRawEncoding());
3376       }
3377       break;
3378 
3379     case DELETE_EXPRS_TO_ANALYZE:
3380       for (unsigned I = 0, N = Record.size(); I != N;) {
3381         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3382         const uint64_t Count = Record[I++];
3383         DelayedDeleteExprs.push_back(Count);
3384         for (uint64_t C = 0; C < Count; ++C) {
3385           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3386           bool IsArrayForm = Record[I++] == 1;
3387           DelayedDeleteExprs.push_back(IsArrayForm);
3388         }
3389       }
3390       break;
3391 
3392     case IMPORTED_MODULES:
3393       if (!F.isModule()) {
3394         // If we aren't loading a module (which has its own exports), make
3395         // all of the imported modules visible.
3396         // FIXME: Deal with macros-only imports.
3397         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3398           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3399           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3400           if (GlobalID) {
3401             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3402             if (DeserializationListener)
3403               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3404           }
3405         }
3406       }
3407       break;
3408 
3409     case MACRO_OFFSET: {
3410       if (F.LocalNumMacros != 0) {
3411         Error("duplicate MACRO_OFFSET record in AST file");
3412         return Failure;
3413       }
3414       F.MacroOffsets = (const uint32_t *)Blob.data();
3415       F.LocalNumMacros = Record[0];
3416       unsigned LocalBaseMacroID = Record[1];
3417       F.BaseMacroID = getTotalNumMacros();
3418 
3419       if (F.LocalNumMacros > 0) {
3420         // Introduce the global -> local mapping for macros within this module.
3421         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3422 
3423         // Introduce the local -> global mapping for macros within this module.
3424         F.MacroRemap.insertOrReplace(
3425           std::make_pair(LocalBaseMacroID,
3426                          F.BaseMacroID - LocalBaseMacroID));
3427 
3428         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3429       }
3430       break;
3431     }
3432 
3433     case LATE_PARSED_TEMPLATE:
3434       LateParsedTemplates.append(Record.begin(), Record.end());
3435       break;
3436 
3437     case OPTIMIZE_PRAGMA_OPTIONS:
3438       if (Record.size() != 1) {
3439         Error("invalid pragma optimize record");
3440         return Failure;
3441       }
3442       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3443       break;
3444 
3445     case MSSTRUCT_PRAGMA_OPTIONS:
3446       if (Record.size() != 1) {
3447         Error("invalid pragma ms_struct record");
3448         return Failure;
3449       }
3450       PragmaMSStructState = Record[0];
3451       break;
3452 
3453     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3454       if (Record.size() != 2) {
3455         Error("invalid pragma ms_struct record");
3456         return Failure;
3457       }
3458       PragmaMSPointersToMembersState = Record[0];
3459       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3460       break;
3461 
3462     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3463       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3464         UnusedLocalTypedefNameCandidates.push_back(
3465             getGlobalDeclID(F, Record[I]));
3466       break;
3467 
3468     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3469       if (Record.size() != 1) {
3470         Error("invalid cuda pragma options record");
3471         return Failure;
3472       }
3473       ForceCUDAHostDeviceDepth = Record[0];
3474       break;
3475 
3476     case PACK_PRAGMA_OPTIONS: {
3477       if (Record.size() < 3) {
3478         Error("invalid pragma pack record");
3479         return Failure;
3480       }
3481       PragmaPackCurrentValue = Record[0];
3482       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3483       unsigned NumStackEntries = Record[2];
3484       unsigned Idx = 3;
3485       // Reset the stack when importing a new module.
3486       PragmaPackStack.clear();
3487       for (unsigned I = 0; I < NumStackEntries; ++I) {
3488         PragmaPackStackEntry Entry;
3489         Entry.Value = Record[Idx++];
3490         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3491         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3492         PragmaPackStrings.push_back(ReadString(Record, Idx));
3493         Entry.SlotLabel = PragmaPackStrings.back();
3494         PragmaPackStack.push_back(Entry);
3495       }
3496       break;
3497     }
3498     }
3499   }
3500 }
3501 
3502 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3503   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3504 
3505   // Additional remapping information.
3506   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3507   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3508   F.ModuleOffsetMap = StringRef();
3509 
3510   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3511   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3512     F.SLocRemap.insert(std::make_pair(0U, 0));
3513     F.SLocRemap.insert(std::make_pair(2U, 1));
3514   }
3515 
3516   // Continuous range maps we may be updating in our module.
3517   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3518   RemapBuilder SLocRemap(F.SLocRemap);
3519   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3520   RemapBuilder MacroRemap(F.MacroRemap);
3521   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3522   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3523   RemapBuilder SelectorRemap(F.SelectorRemap);
3524   RemapBuilder DeclRemap(F.DeclRemap);
3525   RemapBuilder TypeRemap(F.TypeRemap);
3526 
3527   while (Data < DataEnd) {
3528     // FIXME: Looking up dependency modules by filename is horrible. Let's
3529     // start fixing this with prebuilt and explicit modules and see how it
3530     // goes...
3531     using namespace llvm::support;
3532     ModuleKind Kind = static_cast<ModuleKind>(
3533       endian::readNext<uint8_t, little, unaligned>(Data));
3534     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3535     StringRef Name = StringRef((const char*)Data, Len);
3536     Data += Len;
3537     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3538                       ? ModuleMgr.lookupByModuleName(Name)
3539                       : ModuleMgr.lookupByFileName(Name));
3540     if (!OM) {
3541       std::string Msg =
3542           "SourceLocation remap refers to unknown module, cannot find ";
3543       Msg.append(Name);
3544       Error(Msg);
3545       return;
3546     }
3547 
3548     uint32_t SLocOffset =
3549         endian::readNext<uint32_t, little, unaligned>(Data);
3550     uint32_t IdentifierIDOffset =
3551         endian::readNext<uint32_t, little, unaligned>(Data);
3552     uint32_t MacroIDOffset =
3553         endian::readNext<uint32_t, little, unaligned>(Data);
3554     uint32_t PreprocessedEntityIDOffset =
3555         endian::readNext<uint32_t, little, unaligned>(Data);
3556     uint32_t SubmoduleIDOffset =
3557         endian::readNext<uint32_t, little, unaligned>(Data);
3558     uint32_t SelectorIDOffset =
3559         endian::readNext<uint32_t, little, unaligned>(Data);
3560     uint32_t DeclIDOffset =
3561         endian::readNext<uint32_t, little, unaligned>(Data);
3562     uint32_t TypeIndexOffset =
3563         endian::readNext<uint32_t, little, unaligned>(Data);
3564 
3565     uint32_t None = std::numeric_limits<uint32_t>::max();
3566 
3567     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3568                          RemapBuilder &Remap) {
3569       if (Offset != None)
3570         Remap.insert(std::make_pair(Offset,
3571                                     static_cast<int>(BaseOffset - Offset)));
3572     };
3573     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3574     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3575     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3576     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3577               PreprocessedEntityRemap);
3578     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3579     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3580     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3581     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3582 
3583     // Global -> local mappings.
3584     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3585   }
3586 }
3587 
3588 ASTReader::ASTReadResult
3589 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3590                                   const ModuleFile *ImportedBy,
3591                                   unsigned ClientLoadCapabilities) {
3592   unsigned Idx = 0;
3593   F.ModuleMapPath = ReadPath(F, Record, Idx);
3594 
3595   // Try to resolve ModuleName in the current header search context and
3596   // verify that it is found in the same module map file as we saved. If the
3597   // top-level AST file is a main file, skip this check because there is no
3598   // usable header search context.
3599   assert(!F.ModuleName.empty() &&
3600          "MODULE_NAME should come before MODULE_MAP_FILE");
3601   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3602     // An implicitly-loaded module file should have its module listed in some
3603     // module map file that we've already loaded.
3604     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3605     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3606     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3607     // Don't emit module relocation error if we have -fno-validate-pch
3608     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3609       assert(ImportedBy && "top-level import should be verified");
3610       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3611         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3612           // This module was defined by an imported (explicit) module.
3613           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3614                                                << ASTFE->getName();
3615         } else {
3616           // This module was built with a different module map.
3617           Diag(diag::err_imported_module_not_found)
3618               << F.ModuleName << F.FileName << ImportedBy->FileName
3619               << F.ModuleMapPath;
3620           // In case it was imported by a PCH, there's a chance the user is
3621           // just missing to include the search path to the directory containing
3622           // the modulemap.
3623           if (ImportedBy->Kind == MK_PCH)
3624             Diag(diag::note_imported_by_pch_module_not_found)
3625                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3626         }
3627       }
3628       return OutOfDate;
3629     }
3630 
3631     assert(M->Name == F.ModuleName && "found module with different name");
3632 
3633     // Check the primary module map file.
3634     const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3635     if (StoredModMap == nullptr || StoredModMap != ModMap) {
3636       assert(ModMap && "found module is missing module map file");
3637       assert(ImportedBy && "top-level import should be verified");
3638       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3639         Diag(diag::err_imported_module_modmap_changed)
3640           << F.ModuleName << ImportedBy->FileName
3641           << ModMap->getName() << F.ModuleMapPath;
3642       return OutOfDate;
3643     }
3644 
3645     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3646     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3647       // FIXME: we should use input files rather than storing names.
3648       std::string Filename = ReadPath(F, Record, Idx);
3649       const FileEntry *F =
3650           FileMgr.getFile(Filename, false, false);
3651       if (F == nullptr) {
3652         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3653           Error("could not find file '" + Filename +"' referenced by AST file");
3654         return OutOfDate;
3655       }
3656       AdditionalStoredMaps.insert(F);
3657     }
3658 
3659     // Check any additional module map files (e.g. module.private.modulemap)
3660     // that are not in the pcm.
3661     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3662       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3663         // Remove files that match
3664         // Note: SmallPtrSet::erase is really remove
3665         if (!AdditionalStoredMaps.erase(ModMap)) {
3666           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3667             Diag(diag::err_module_different_modmap)
3668               << F.ModuleName << /*new*/0 << ModMap->getName();
3669           return OutOfDate;
3670         }
3671       }
3672     }
3673 
3674     // Check any additional module map files that are in the pcm, but not
3675     // found in header search. Cases that match are already removed.
3676     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3677       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3678         Diag(diag::err_module_different_modmap)
3679           << F.ModuleName << /*not new*/1 << ModMap->getName();
3680       return OutOfDate;
3681     }
3682   }
3683 
3684   if (Listener)
3685     Listener->ReadModuleMapFile(F.ModuleMapPath);
3686   return Success;
3687 }
3688 
3689 /// Move the given method to the back of the global list of methods.
3690 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3691   // Find the entry for this selector in the method pool.
3692   Sema::GlobalMethodPool::iterator Known
3693     = S.MethodPool.find(Method->getSelector());
3694   if (Known == S.MethodPool.end())
3695     return;
3696 
3697   // Retrieve the appropriate method list.
3698   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3699                                                     : Known->second.second;
3700   bool Found = false;
3701   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3702     if (!Found) {
3703       if (List->getMethod() == Method) {
3704         Found = true;
3705       } else {
3706         // Keep searching.
3707         continue;
3708       }
3709     }
3710 
3711     if (List->getNext())
3712       List->setMethod(List->getNext()->getMethod());
3713     else
3714       List->setMethod(Method);
3715   }
3716 }
3717 
3718 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3719   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3720   for (Decl *D : Names) {
3721     bool wasHidden = D->isHidden();
3722     D->setVisibleDespiteOwningModule();
3723 
3724     if (wasHidden && SemaObj) {
3725       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3726         moveMethodToBackOfGlobalList(*SemaObj, Method);
3727       }
3728     }
3729   }
3730 }
3731 
3732 void ASTReader::makeModuleVisible(Module *Mod,
3733                                   Module::NameVisibilityKind NameVisibility,
3734                                   SourceLocation ImportLoc) {
3735   llvm::SmallPtrSet<Module *, 4> Visited;
3736   SmallVector<Module *, 4> Stack;
3737   Stack.push_back(Mod);
3738   while (!Stack.empty()) {
3739     Mod = Stack.pop_back_val();
3740 
3741     if (NameVisibility <= Mod->NameVisibility) {
3742       // This module already has this level of visibility (or greater), so
3743       // there is nothing more to do.
3744       continue;
3745     }
3746 
3747     if (!Mod->isAvailable()) {
3748       // Modules that aren't available cannot be made visible.
3749       continue;
3750     }
3751 
3752     // Update the module's name visibility.
3753     Mod->NameVisibility = NameVisibility;
3754 
3755     // If we've already deserialized any names from this module,
3756     // mark them as visible.
3757     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3758     if (Hidden != HiddenNamesMap.end()) {
3759       auto HiddenNames = std::move(*Hidden);
3760       HiddenNamesMap.erase(Hidden);
3761       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3762       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3763              "making names visible added hidden names");
3764     }
3765 
3766     // Push any exported modules onto the stack to be marked as visible.
3767     SmallVector<Module *, 16> Exports;
3768     Mod->getExportedModules(Exports);
3769     for (SmallVectorImpl<Module *>::iterator
3770            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3771       Module *Exported = *I;
3772       if (Visited.insert(Exported).second)
3773         Stack.push_back(Exported);
3774     }
3775   }
3776 }
3777 
3778 /// We've merged the definition \p MergedDef into the existing definition
3779 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3780 /// visible.
3781 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3782                                           NamedDecl *MergedDef) {
3783   if (Def->isHidden()) {
3784     // If MergedDef is visible or becomes visible, make the definition visible.
3785     if (!MergedDef->isHidden())
3786       Def->setVisibleDespiteOwningModule();
3787     else {
3788       getContext().mergeDefinitionIntoModule(
3789           Def, MergedDef->getImportedOwningModule(),
3790           /*NotifyListeners*/ false);
3791       PendingMergedDefinitionsToDeduplicate.insert(Def);
3792     }
3793   }
3794 }
3795 
3796 bool ASTReader::loadGlobalIndex() {
3797   if (GlobalIndex)
3798     return false;
3799 
3800   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3801       !PP.getLangOpts().Modules)
3802     return true;
3803 
3804   // Try to load the global index.
3805   TriedLoadingGlobalIndex = true;
3806   StringRef ModuleCachePath
3807     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3808   std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3809     = GlobalModuleIndex::readIndex(ModuleCachePath);
3810   if (!Result.first)
3811     return true;
3812 
3813   GlobalIndex.reset(Result.first);
3814   ModuleMgr.setGlobalIndex(GlobalIndex.get());
3815   return false;
3816 }
3817 
3818 bool ASTReader::isGlobalIndexUnavailable() const {
3819   return PP.getLangOpts().Modules && UseGlobalIndex &&
3820          !hasGlobalIndex() && TriedLoadingGlobalIndex;
3821 }
3822 
3823 static void updateModuleTimestamp(ModuleFile &MF) {
3824   // Overwrite the timestamp file contents so that file's mtime changes.
3825   std::string TimestampFilename = MF.getTimestampFilename();
3826   std::error_code EC;
3827   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3828   if (EC)
3829     return;
3830   OS << "Timestamp file\n";
3831   OS.close();
3832   OS.clear_error(); // Avoid triggering a fatal error.
3833 }
3834 
3835 /// Given a cursor at the start of an AST file, scan ahead and drop the
3836 /// cursor into the start of the given block ID, returning false on success and
3837 /// true on failure.
3838 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3839   while (true) {
3840     llvm::BitstreamEntry Entry = Cursor.advance();
3841     switch (Entry.Kind) {
3842     case llvm::BitstreamEntry::Error:
3843     case llvm::BitstreamEntry::EndBlock:
3844       return true;
3845 
3846     case llvm::BitstreamEntry::Record:
3847       // Ignore top-level records.
3848       Cursor.skipRecord(Entry.ID);
3849       break;
3850 
3851     case llvm::BitstreamEntry::SubBlock:
3852       if (Entry.ID == BlockID) {
3853         if (Cursor.EnterSubBlock(BlockID))
3854           return true;
3855         // Found it!
3856         return false;
3857       }
3858 
3859       if (Cursor.SkipBlock())
3860         return true;
3861     }
3862   }
3863 }
3864 
3865 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
3866                                             ModuleKind Type,
3867                                             SourceLocation ImportLoc,
3868                                             unsigned ClientLoadCapabilities,
3869                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
3870   llvm::SaveAndRestore<SourceLocation>
3871     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3872 
3873   // Defer any pending actions until we get to the end of reading the AST file.
3874   Deserializing AnASTFile(this);
3875 
3876   // Bump the generation number.
3877   unsigned PreviousGeneration = 0;
3878   if (ContextObj)
3879     PreviousGeneration = incrementGeneration(*ContextObj);
3880 
3881   unsigned NumModules = ModuleMgr.size();
3882   SmallVector<ImportedModule, 4> Loaded;
3883   switch (ASTReadResult ReadResult =
3884               ReadASTCore(FileName, Type, ImportLoc,
3885                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
3886                           ASTFileSignature(), ClientLoadCapabilities)) {
3887   case Failure:
3888   case Missing:
3889   case OutOfDate:
3890   case VersionMismatch:
3891   case ConfigurationMismatch:
3892   case HadErrors: {
3893     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3894     for (const ImportedModule &IM : Loaded)
3895       LoadedSet.insert(IM.Mod);
3896 
3897     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
3898                             PP.getLangOpts().Modules
3899                                 ? &PP.getHeaderSearchInfo().getModuleMap()
3900                                 : nullptr);
3901 
3902     // If we find that any modules are unusable, the global index is going
3903     // to be out-of-date. Just remove it.
3904     GlobalIndex.reset();
3905     ModuleMgr.setGlobalIndex(nullptr);
3906     return ReadResult;
3907   }
3908   case Success:
3909     break;
3910   }
3911 
3912   // Here comes stuff that we only do once the entire chain is loaded.
3913 
3914   // Load the AST blocks of all of the modules that we loaded.
3915   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3916                                               MEnd = Loaded.end();
3917        M != MEnd; ++M) {
3918     ModuleFile &F = *M->Mod;
3919 
3920     // Read the AST block.
3921     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3922       return Result;
3923 
3924     // Read the extension blocks.
3925     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3926       if (ASTReadResult Result = ReadExtensionBlock(F))
3927         return Result;
3928     }
3929 
3930     // Once read, set the ModuleFile bit base offset and update the size in
3931     // bits of all files we've seen.
3932     F.GlobalBitOffset = TotalModulesSizeInBits;
3933     TotalModulesSizeInBits += F.SizeInBits;
3934     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3935 
3936     // Preload SLocEntries.
3937     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3938       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3939       // Load it through the SourceManager and don't call ReadSLocEntry()
3940       // directly because the entry may have already been loaded in which case
3941       // calling ReadSLocEntry() directly would trigger an assertion in
3942       // SourceManager.
3943       SourceMgr.getLoadedSLocEntryByID(Index);
3944     }
3945 
3946     // Map the original source file ID into the ID space of the current
3947     // compilation.
3948     if (F.OriginalSourceFileID.isValid()) {
3949       F.OriginalSourceFileID = FileID::get(
3950           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
3951     }
3952 
3953     // Preload all the pending interesting identifiers by marking them out of
3954     // date.
3955     for (auto Offset : F.PreloadIdentifierOffsets) {
3956       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3957           F.IdentifierTableData + Offset);
3958 
3959       ASTIdentifierLookupTrait Trait(*this, F);
3960       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3961       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
3962       auto &II = PP.getIdentifierTable().getOwn(Key);
3963       II.setOutOfDate(true);
3964 
3965       // Mark this identifier as being from an AST file so that we can track
3966       // whether we need to serialize it.
3967       markIdentifierFromAST(*this, II);
3968 
3969       // Associate the ID with the identifier so that the writer can reuse it.
3970       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3971       SetIdentifierInfo(ID, &II);
3972     }
3973   }
3974 
3975   // Setup the import locations and notify the module manager that we've
3976   // committed to these module files.
3977   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3978                                               MEnd = Loaded.end();
3979        M != MEnd; ++M) {
3980     ModuleFile &F = *M->Mod;
3981 
3982     ModuleMgr.moduleFileAccepted(&F);
3983 
3984     // Set the import location.
3985     F.DirectImportLoc = ImportLoc;
3986     // FIXME: We assume that locations from PCH / preamble do not need
3987     // any translation.
3988     if (!M->ImportedBy)
3989       F.ImportLoc = M->ImportLoc;
3990     else
3991       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
3992   }
3993 
3994   if (!PP.getLangOpts().CPlusPlus ||
3995       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
3996        Type != MK_PrebuiltModule)) {
3997     // Mark all of the identifiers in the identifier table as being out of date,
3998     // so that various accessors know to check the loaded modules when the
3999     // identifier is used.
4000     //
4001     // For C++ modules, we don't need information on many identifiers (just
4002     // those that provide macros or are poisoned), so we mark all of
4003     // the interesting ones via PreloadIdentifierOffsets.
4004     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4005                                 IdEnd = PP.getIdentifierTable().end();
4006          Id != IdEnd; ++Id)
4007       Id->second->setOutOfDate(true);
4008   }
4009   // Mark selectors as out of date.
4010   for (auto Sel : SelectorGeneration)
4011     SelectorOutOfDate[Sel.first] = true;
4012 
4013   // Resolve any unresolved module exports.
4014   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4015     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4016     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4017     Module *ResolvedMod = getSubmodule(GlobalID);
4018 
4019     switch (Unresolved.Kind) {
4020     case UnresolvedModuleRef::Conflict:
4021       if (ResolvedMod) {
4022         Module::Conflict Conflict;
4023         Conflict.Other = ResolvedMod;
4024         Conflict.Message = Unresolved.String.str();
4025         Unresolved.Mod->Conflicts.push_back(Conflict);
4026       }
4027       continue;
4028 
4029     case UnresolvedModuleRef::Import:
4030       if (ResolvedMod)
4031         Unresolved.Mod->Imports.insert(ResolvedMod);
4032       continue;
4033 
4034     case UnresolvedModuleRef::Export:
4035       if (ResolvedMod || Unresolved.IsWildcard)
4036         Unresolved.Mod->Exports.push_back(
4037           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4038       continue;
4039     }
4040   }
4041   UnresolvedModuleRefs.clear();
4042 
4043   if (Imported)
4044     Imported->append(ImportedModules.begin(),
4045                      ImportedModules.end());
4046 
4047   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4048   // Might be unnecessary as use declarations are only used to build the
4049   // module itself.
4050 
4051   if (ContextObj)
4052     InitializeContext();
4053 
4054   if (SemaObj)
4055     UpdateSema();
4056 
4057   if (DeserializationListener)
4058     DeserializationListener->ReaderInitialized(this);
4059 
4060   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4061   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4062     // If this AST file is a precompiled preamble, then set the
4063     // preamble file ID of the source manager to the file source file
4064     // from which the preamble was built.
4065     if (Type == MK_Preamble) {
4066       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4067     } else if (Type == MK_MainFile) {
4068       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4069     }
4070   }
4071 
4072   // For any Objective-C class definitions we have already loaded, make sure
4073   // that we load any additional categories.
4074   if (ContextObj) {
4075     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4076       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4077                          ObjCClassesLoaded[I],
4078                          PreviousGeneration);
4079     }
4080   }
4081 
4082   if (PP.getHeaderSearchInfo()
4083           .getHeaderSearchOpts()
4084           .ModulesValidateOncePerBuildSession) {
4085     // Now we are certain that the module and all modules it depends on are
4086     // up to date.  Create or update timestamp files for modules that are
4087     // located in the module cache (not for PCH files that could be anywhere
4088     // in the filesystem).
4089     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4090       ImportedModule &M = Loaded[I];
4091       if (M.Mod->Kind == MK_ImplicitModule) {
4092         updateModuleTimestamp(*M.Mod);
4093       }
4094     }
4095   }
4096 
4097   return Success;
4098 }
4099 
4100 static ASTFileSignature readASTFileSignature(StringRef PCH);
4101 
4102 /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
4103 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
4104   return Stream.canSkipToPos(4) &&
4105          Stream.Read(8) == 'C' &&
4106          Stream.Read(8) == 'P' &&
4107          Stream.Read(8) == 'C' &&
4108          Stream.Read(8) == 'H';
4109 }
4110 
4111 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4112   switch (Kind) {
4113   case MK_PCH:
4114     return 0; // PCH
4115   case MK_ImplicitModule:
4116   case MK_ExplicitModule:
4117   case MK_PrebuiltModule:
4118     return 1; // module
4119   case MK_MainFile:
4120   case MK_Preamble:
4121     return 2; // main source file
4122   }
4123   llvm_unreachable("unknown module kind");
4124 }
4125 
4126 ASTReader::ASTReadResult
4127 ASTReader::ReadASTCore(StringRef FileName,
4128                        ModuleKind Type,
4129                        SourceLocation ImportLoc,
4130                        ModuleFile *ImportedBy,
4131                        SmallVectorImpl<ImportedModule> &Loaded,
4132                        off_t ExpectedSize, time_t ExpectedModTime,
4133                        ASTFileSignature ExpectedSignature,
4134                        unsigned ClientLoadCapabilities) {
4135   ModuleFile *M;
4136   std::string ErrorStr;
4137   ModuleManager::AddModuleResult AddResult
4138     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4139                           getGeneration(), ExpectedSize, ExpectedModTime,
4140                           ExpectedSignature, readASTFileSignature,
4141                           M, ErrorStr);
4142 
4143   switch (AddResult) {
4144   case ModuleManager::AlreadyLoaded:
4145     return Success;
4146 
4147   case ModuleManager::NewlyLoaded:
4148     // Load module file below.
4149     break;
4150 
4151   case ModuleManager::Missing:
4152     // The module file was missing; if the client can handle that, return
4153     // it.
4154     if (ClientLoadCapabilities & ARR_Missing)
4155       return Missing;
4156 
4157     // Otherwise, return an error.
4158     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4159                                           << FileName << !ErrorStr.empty()
4160                                           << ErrorStr;
4161     return Failure;
4162 
4163   case ModuleManager::OutOfDate:
4164     // We couldn't load the module file because it is out-of-date. If the
4165     // client can handle out-of-date, return it.
4166     if (ClientLoadCapabilities & ARR_OutOfDate)
4167       return OutOfDate;
4168 
4169     // Otherwise, return an error.
4170     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4171                                             << FileName << !ErrorStr.empty()
4172                                             << ErrorStr;
4173     return Failure;
4174   }
4175 
4176   assert(M && "Missing module file");
4177 
4178   ModuleFile &F = *M;
4179   BitstreamCursor &Stream = F.Stream;
4180   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4181   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4182 
4183   // Sniff for the signature.
4184   if (!startsWithASTFileMagic(Stream)) {
4185     Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
4186                                         << FileName;
4187     return Failure;
4188   }
4189 
4190   // This is used for compatibility with older PCH formats.
4191   bool HaveReadControlBlock = false;
4192   while (true) {
4193     llvm::BitstreamEntry Entry = Stream.advance();
4194 
4195     switch (Entry.Kind) {
4196     case llvm::BitstreamEntry::Error:
4197     case llvm::BitstreamEntry::Record:
4198     case llvm::BitstreamEntry::EndBlock:
4199       Error("invalid record at top-level of AST file");
4200       return Failure;
4201 
4202     case llvm::BitstreamEntry::SubBlock:
4203       break;
4204     }
4205 
4206     switch (Entry.ID) {
4207     case CONTROL_BLOCK_ID:
4208       HaveReadControlBlock = true;
4209       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4210       case Success:
4211         // Check that we didn't try to load a non-module AST file as a module.
4212         //
4213         // FIXME: Should we also perform the converse check? Loading a module as
4214         // a PCH file sort of works, but it's a bit wonky.
4215         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4216              Type == MK_PrebuiltModule) &&
4217             F.ModuleName.empty()) {
4218           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4219           if (Result != OutOfDate ||
4220               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4221             Diag(diag::err_module_file_not_module) << FileName;
4222           return Result;
4223         }
4224         break;
4225 
4226       case Failure: return Failure;
4227       case Missing: return Missing;
4228       case OutOfDate: return OutOfDate;
4229       case VersionMismatch: return VersionMismatch;
4230       case ConfigurationMismatch: return ConfigurationMismatch;
4231       case HadErrors: return HadErrors;
4232       }
4233       break;
4234 
4235     case AST_BLOCK_ID:
4236       if (!HaveReadControlBlock) {
4237         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4238           Diag(diag::err_pch_version_too_old);
4239         return VersionMismatch;
4240       }
4241 
4242       // Record that we've loaded this module.
4243       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4244       return Success;
4245 
4246     case UNHASHED_CONTROL_BLOCK_ID:
4247       // This block is handled using look-ahead during ReadControlBlock.  We
4248       // shouldn't get here!
4249       Error("malformed block record in AST file");
4250       return Failure;
4251 
4252     default:
4253       if (Stream.SkipBlock()) {
4254         Error("malformed block record in AST file");
4255         return Failure;
4256       }
4257       break;
4258     }
4259   }
4260 
4261   return Success;
4262 }
4263 
4264 ASTReader::ASTReadResult
4265 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4266                                     unsigned ClientLoadCapabilities) {
4267   const HeaderSearchOptions &HSOpts =
4268       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4269   bool AllowCompatibleConfigurationMismatch =
4270       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4271 
4272   ASTReadResult Result = readUnhashedControlBlockImpl(
4273       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4274       Listener.get(),
4275       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4276 
4277   // If F was directly imported by another module, it's implicitly validated by
4278   // the importing module.
4279   if (DisableValidation || WasImportedBy ||
4280       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4281     return Success;
4282 
4283   if (Result == Failure) {
4284     Error("malformed block record in AST file");
4285     return Failure;
4286   }
4287 
4288   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4289     // If this module has already been finalized in the PCMCache, we're stuck
4290     // with it; we can only load a single version of each module.
4291     //
4292     // This can happen when a module is imported in two contexts: in one, as a
4293     // user module; in another, as a system module (due to an import from
4294     // another module marked with the [system] flag).  It usually indicates a
4295     // bug in the module map: this module should also be marked with [system].
4296     //
4297     // If -Wno-system-headers (the default), and the first import is as a
4298     // system module, then validation will fail during the as-user import,
4299     // since -Werror flags won't have been validated.  However, it's reasonable
4300     // to treat this consistently as a system module.
4301     //
4302     // If -Wsystem-headers, the PCM on disk was built with
4303     // -Wno-system-headers, and the first import is as a user module, then
4304     // validation will fail during the as-system import since the PCM on disk
4305     // doesn't guarantee that -Werror was respected.  However, the -Werror
4306     // flags were checked during the initial as-user import.
4307     if (PCMCache.isBufferFinal(F.FileName)) {
4308       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4309       return Success;
4310     }
4311   }
4312 
4313   return Result;
4314 }
4315 
4316 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4317     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4318     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4319     bool ValidateDiagnosticOptions) {
4320   // Initialize a stream.
4321   BitstreamCursor Stream(StreamData);
4322 
4323   // Sniff for the signature.
4324   if (!startsWithASTFileMagic(Stream))
4325     return Failure;
4326 
4327   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4328   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4329     return Failure;
4330 
4331   // Read all of the records in the options block.
4332   RecordData Record;
4333   ASTReadResult Result = Success;
4334   while (true) {
4335     llvm::BitstreamEntry Entry = Stream.advance();
4336 
4337     switch (Entry.Kind) {
4338     case llvm::BitstreamEntry::Error:
4339     case llvm::BitstreamEntry::SubBlock:
4340       return Failure;
4341 
4342     case llvm::BitstreamEntry::EndBlock:
4343       return Result;
4344 
4345     case llvm::BitstreamEntry::Record:
4346       // The interesting case.
4347       break;
4348     }
4349 
4350     // Read and process a record.
4351     Record.clear();
4352     switch (
4353         (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
4354     case SIGNATURE:
4355       if (F)
4356         std::copy(Record.begin(), Record.end(), F->Signature.data());
4357       break;
4358     case DIAGNOSTIC_OPTIONS: {
4359       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4360       if (Listener && ValidateDiagnosticOptions &&
4361           !AllowCompatibleConfigurationMismatch &&
4362           ParseDiagnosticOptions(Record, Complain, *Listener))
4363         Result = OutOfDate; // Don't return early.  Read the signature.
4364       break;
4365     }
4366     case DIAG_PRAGMA_MAPPINGS:
4367       if (!F)
4368         break;
4369       if (F->PragmaDiagMappings.empty())
4370         F->PragmaDiagMappings.swap(Record);
4371       else
4372         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4373                                      Record.begin(), Record.end());
4374       break;
4375     }
4376   }
4377 }
4378 
4379 /// Parse a record and blob containing module file extension metadata.
4380 static bool parseModuleFileExtensionMetadata(
4381               const SmallVectorImpl<uint64_t> &Record,
4382               StringRef Blob,
4383               ModuleFileExtensionMetadata &Metadata) {
4384   if (Record.size() < 4) return true;
4385 
4386   Metadata.MajorVersion = Record[0];
4387   Metadata.MinorVersion = Record[1];
4388 
4389   unsigned BlockNameLen = Record[2];
4390   unsigned UserInfoLen = Record[3];
4391 
4392   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4393 
4394   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4395   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4396                                   Blob.data() + BlockNameLen + UserInfoLen);
4397   return false;
4398 }
4399 
4400 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4401   BitstreamCursor &Stream = F.Stream;
4402 
4403   RecordData Record;
4404   while (true) {
4405     llvm::BitstreamEntry Entry = Stream.advance();
4406     switch (Entry.Kind) {
4407     case llvm::BitstreamEntry::SubBlock:
4408       if (Stream.SkipBlock())
4409         return Failure;
4410 
4411       continue;
4412 
4413     case llvm::BitstreamEntry::EndBlock:
4414       return Success;
4415 
4416     case llvm::BitstreamEntry::Error:
4417       return HadErrors;
4418 
4419     case llvm::BitstreamEntry::Record:
4420       break;
4421     }
4422 
4423     Record.clear();
4424     StringRef Blob;
4425     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4426     switch (RecCode) {
4427     case EXTENSION_METADATA: {
4428       ModuleFileExtensionMetadata Metadata;
4429       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4430         return Failure;
4431 
4432       // Find a module file extension with this block name.
4433       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4434       if (Known == ModuleFileExtensions.end()) break;
4435 
4436       // Form a reader.
4437       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4438                                                              F, Stream)) {
4439         F.ExtensionReaders.push_back(std::move(Reader));
4440       }
4441 
4442       break;
4443     }
4444     }
4445   }
4446 
4447   return Success;
4448 }
4449 
4450 void ASTReader::InitializeContext() {
4451   assert(ContextObj && "no context to initialize");
4452   ASTContext &Context = *ContextObj;
4453 
4454   // If there's a listener, notify them that we "read" the translation unit.
4455   if (DeserializationListener)
4456     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4457                                       Context.getTranslationUnitDecl());
4458 
4459   // FIXME: Find a better way to deal with collisions between these
4460   // built-in types. Right now, we just ignore the problem.
4461 
4462   // Load the special types.
4463   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4464     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4465       if (!Context.CFConstantStringTypeDecl)
4466         Context.setCFConstantStringType(GetType(String));
4467     }
4468 
4469     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4470       QualType FileType = GetType(File);
4471       if (FileType.isNull()) {
4472         Error("FILE type is NULL");
4473         return;
4474       }
4475 
4476       if (!Context.FILEDecl) {
4477         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4478           Context.setFILEDecl(Typedef->getDecl());
4479         else {
4480           const TagType *Tag = FileType->getAs<TagType>();
4481           if (!Tag) {
4482             Error("Invalid FILE type in AST file");
4483             return;
4484           }
4485           Context.setFILEDecl(Tag->getDecl());
4486         }
4487       }
4488     }
4489 
4490     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4491       QualType Jmp_bufType = GetType(Jmp_buf);
4492       if (Jmp_bufType.isNull()) {
4493         Error("jmp_buf type is NULL");
4494         return;
4495       }
4496 
4497       if (!Context.jmp_bufDecl) {
4498         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4499           Context.setjmp_bufDecl(Typedef->getDecl());
4500         else {
4501           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4502           if (!Tag) {
4503             Error("Invalid jmp_buf type in AST file");
4504             return;
4505           }
4506           Context.setjmp_bufDecl(Tag->getDecl());
4507         }
4508       }
4509     }
4510 
4511     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4512       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4513       if (Sigjmp_bufType.isNull()) {
4514         Error("sigjmp_buf type is NULL");
4515         return;
4516       }
4517 
4518       if (!Context.sigjmp_bufDecl) {
4519         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4520           Context.setsigjmp_bufDecl(Typedef->getDecl());
4521         else {
4522           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4523           assert(Tag && "Invalid sigjmp_buf type in AST file");
4524           Context.setsigjmp_bufDecl(Tag->getDecl());
4525         }
4526       }
4527     }
4528 
4529     if (unsigned ObjCIdRedef
4530           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4531       if (Context.ObjCIdRedefinitionType.isNull())
4532         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4533     }
4534 
4535     if (unsigned ObjCClassRedef
4536           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4537       if (Context.ObjCClassRedefinitionType.isNull())
4538         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4539     }
4540 
4541     if (unsigned ObjCSelRedef
4542           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4543       if (Context.ObjCSelRedefinitionType.isNull())
4544         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4545     }
4546 
4547     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4548       QualType Ucontext_tType = GetType(Ucontext_t);
4549       if (Ucontext_tType.isNull()) {
4550         Error("ucontext_t type is NULL");
4551         return;
4552       }
4553 
4554       if (!Context.ucontext_tDecl) {
4555         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4556           Context.setucontext_tDecl(Typedef->getDecl());
4557         else {
4558           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4559           assert(Tag && "Invalid ucontext_t type in AST file");
4560           Context.setucontext_tDecl(Tag->getDecl());
4561         }
4562       }
4563     }
4564   }
4565 
4566   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4567 
4568   // If there were any CUDA special declarations, deserialize them.
4569   if (!CUDASpecialDeclRefs.empty()) {
4570     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4571     Context.setcudaConfigureCallDecl(
4572                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4573   }
4574 
4575   // Re-export any modules that were imported by a non-module AST file.
4576   // FIXME: This does not make macro-only imports visible again.
4577   for (auto &Import : ImportedModules) {
4578     if (Module *Imported = getSubmodule(Import.ID)) {
4579       makeModuleVisible(Imported, Module::AllVisible,
4580                         /*ImportLoc=*/Import.ImportLoc);
4581       if (Import.ImportLoc.isValid())
4582         PP.makeModuleVisible(Imported, Import.ImportLoc);
4583       // FIXME: should we tell Sema to make the module visible too?
4584     }
4585   }
4586   ImportedModules.clear();
4587 }
4588 
4589 void ASTReader::finalizeForWriting() {
4590   // Nothing to do for now.
4591 }
4592 
4593 /// Reads and return the signature record from \p PCH's control block, or
4594 /// else returns 0.
4595 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4596   BitstreamCursor Stream(PCH);
4597   if (!startsWithASTFileMagic(Stream))
4598     return ASTFileSignature();
4599 
4600   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4601   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4602     return ASTFileSignature();
4603 
4604   // Scan for SIGNATURE inside the diagnostic options block.
4605   ASTReader::RecordData Record;
4606   while (true) {
4607     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4608     if (Entry.Kind != llvm::BitstreamEntry::Record)
4609       return ASTFileSignature();
4610 
4611     Record.clear();
4612     StringRef Blob;
4613     if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4614       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4615                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4616   }
4617 }
4618 
4619 /// Retrieve the name of the original source file name
4620 /// directly from the AST file, without actually loading the AST
4621 /// file.
4622 std::string ASTReader::getOriginalSourceFile(
4623     const std::string &ASTFileName, FileManager &FileMgr,
4624     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4625   // Open the AST file.
4626   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4627   if (!Buffer) {
4628     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4629         << ASTFileName << Buffer.getError().message();
4630     return std::string();
4631   }
4632 
4633   // Initialize the stream
4634   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4635 
4636   // Sniff for the signature.
4637   if (!startsWithASTFileMagic(Stream)) {
4638     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4639     return std::string();
4640   }
4641 
4642   // Scan for the CONTROL_BLOCK_ID block.
4643   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4644     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4645     return std::string();
4646   }
4647 
4648   // Scan for ORIGINAL_FILE inside the control block.
4649   RecordData Record;
4650   while (true) {
4651     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4652     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4653       return std::string();
4654 
4655     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4656       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4657       return std::string();
4658     }
4659 
4660     Record.clear();
4661     StringRef Blob;
4662     if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4663       return Blob.str();
4664   }
4665 }
4666 
4667 namespace {
4668 
4669   class SimplePCHValidator : public ASTReaderListener {
4670     const LangOptions &ExistingLangOpts;
4671     const TargetOptions &ExistingTargetOpts;
4672     const PreprocessorOptions &ExistingPPOpts;
4673     std::string ExistingModuleCachePath;
4674     FileManager &FileMgr;
4675 
4676   public:
4677     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4678                        const TargetOptions &ExistingTargetOpts,
4679                        const PreprocessorOptions &ExistingPPOpts,
4680                        StringRef ExistingModuleCachePath,
4681                        FileManager &FileMgr)
4682       : ExistingLangOpts(ExistingLangOpts),
4683         ExistingTargetOpts(ExistingTargetOpts),
4684         ExistingPPOpts(ExistingPPOpts),
4685         ExistingModuleCachePath(ExistingModuleCachePath),
4686         FileMgr(FileMgr) {}
4687 
4688     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4689                              bool AllowCompatibleDifferences) override {
4690       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4691                                   AllowCompatibleDifferences);
4692     }
4693 
4694     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4695                            bool AllowCompatibleDifferences) override {
4696       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4697                                 AllowCompatibleDifferences);
4698     }
4699 
4700     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4701                                  StringRef SpecificModuleCachePath,
4702                                  bool Complain) override {
4703       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4704                                       ExistingModuleCachePath,
4705                                       nullptr, ExistingLangOpts);
4706     }
4707 
4708     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4709                                  bool Complain,
4710                                  std::string &SuggestedPredefines) override {
4711       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4712                                       SuggestedPredefines, ExistingLangOpts);
4713     }
4714   };
4715 
4716 } // namespace
4717 
4718 bool ASTReader::readASTFileControlBlock(
4719     StringRef Filename, FileManager &FileMgr,
4720     const PCHContainerReader &PCHContainerRdr,
4721     bool FindModuleFileExtensions,
4722     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
4723   // Open the AST file.
4724   // FIXME: This allows use of the VFS; we do not allow use of the
4725   // VFS when actually loading a module.
4726   auto Buffer = FileMgr.getBufferForFile(Filename);
4727   if (!Buffer) {
4728     return true;
4729   }
4730 
4731   // Initialize the stream
4732   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
4733   BitstreamCursor Stream(Bytes);
4734 
4735   // Sniff for the signature.
4736   if (!startsWithASTFileMagic(Stream))
4737     return true;
4738 
4739   // Scan for the CONTROL_BLOCK_ID block.
4740   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4741     return true;
4742 
4743   bool NeedsInputFiles = Listener.needsInputFileVisitation();
4744   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4745   bool NeedsImports = Listener.needsImportVisitation();
4746   BitstreamCursor InputFilesCursor;
4747 
4748   RecordData Record;
4749   std::string ModuleDir;
4750   bool DoneWithControlBlock = false;
4751   while (!DoneWithControlBlock) {
4752     llvm::BitstreamEntry Entry = Stream.advance();
4753 
4754     switch (Entry.Kind) {
4755     case llvm::BitstreamEntry::SubBlock: {
4756       switch (Entry.ID) {
4757       case OPTIONS_BLOCK_ID: {
4758         std::string IgnoredSuggestedPredefines;
4759         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4760                              /*AllowCompatibleConfigurationMismatch*/ false,
4761                              Listener, IgnoredSuggestedPredefines) != Success)
4762           return true;
4763         break;
4764       }
4765 
4766       case INPUT_FILES_BLOCK_ID:
4767         InputFilesCursor = Stream;
4768         if (Stream.SkipBlock() ||
4769             (NeedsInputFiles &&
4770              ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4771           return true;
4772         break;
4773 
4774       default:
4775         if (Stream.SkipBlock())
4776           return true;
4777         break;
4778       }
4779 
4780       continue;
4781     }
4782 
4783     case llvm::BitstreamEntry::EndBlock:
4784       DoneWithControlBlock = true;
4785       break;
4786 
4787     case llvm::BitstreamEntry::Error:
4788       return true;
4789 
4790     case llvm::BitstreamEntry::Record:
4791       break;
4792     }
4793 
4794     if (DoneWithControlBlock) break;
4795 
4796     Record.clear();
4797     StringRef Blob;
4798     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4799     switch ((ControlRecordTypes)RecCode) {
4800     case METADATA:
4801       if (Record[0] != VERSION_MAJOR)
4802         return true;
4803       if (Listener.ReadFullVersionInformation(Blob))
4804         return true;
4805       break;
4806     case MODULE_NAME:
4807       Listener.ReadModuleName(Blob);
4808       break;
4809     case MODULE_DIRECTORY:
4810       ModuleDir = Blob;
4811       break;
4812     case MODULE_MAP_FILE: {
4813       unsigned Idx = 0;
4814       auto Path = ReadString(Record, Idx);
4815       ResolveImportedPath(Path, ModuleDir);
4816       Listener.ReadModuleMapFile(Path);
4817       break;
4818     }
4819     case INPUT_FILE_OFFSETS: {
4820       if (!NeedsInputFiles)
4821         break;
4822 
4823       unsigned NumInputFiles = Record[0];
4824       unsigned NumUserFiles = Record[1];
4825       const llvm::support::unaligned_uint64_t *InputFileOffs =
4826           (const llvm::support::unaligned_uint64_t *)Blob.data();
4827       for (unsigned I = 0; I != NumInputFiles; ++I) {
4828         // Go find this input file.
4829         bool isSystemFile = I >= NumUserFiles;
4830 
4831         if (isSystemFile && !NeedsSystemInputFiles)
4832           break; // the rest are system input files
4833 
4834         BitstreamCursor &Cursor = InputFilesCursor;
4835         SavedStreamPosition SavedPosition(Cursor);
4836         Cursor.JumpToBit(InputFileOffs[I]);
4837 
4838         unsigned Code = Cursor.ReadCode();
4839         RecordData Record;
4840         StringRef Blob;
4841         bool shouldContinue = false;
4842         switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4843         case INPUT_FILE:
4844           bool Overridden = static_cast<bool>(Record[3]);
4845           std::string Filename = Blob;
4846           ResolveImportedPath(Filename, ModuleDir);
4847           shouldContinue = Listener.visitInputFile(
4848               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
4849           break;
4850         }
4851         if (!shouldContinue)
4852           break;
4853       }
4854       break;
4855     }
4856 
4857     case IMPORTS: {
4858       if (!NeedsImports)
4859         break;
4860 
4861       unsigned Idx = 0, N = Record.size();
4862       while (Idx < N) {
4863         // Read information about the AST file.
4864         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
4865         std::string ModuleName = ReadString(Record, Idx);
4866         std::string Filename = ReadString(Record, Idx);
4867         ResolveImportedPath(Filename, ModuleDir);
4868         Listener.visitImport(ModuleName, Filename);
4869       }
4870       break;
4871     }
4872 
4873     default:
4874       // No other validation to perform.
4875       break;
4876     }
4877   }
4878 
4879   // Look for module file extension blocks, if requested.
4880   if (FindModuleFileExtensions) {
4881     BitstreamCursor SavedStream = Stream;
4882     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4883       bool DoneWithExtensionBlock = false;
4884       while (!DoneWithExtensionBlock) {
4885        llvm::BitstreamEntry Entry = Stream.advance();
4886 
4887        switch (Entry.Kind) {
4888        case llvm::BitstreamEntry::SubBlock:
4889          if (Stream.SkipBlock())
4890            return true;
4891 
4892          continue;
4893 
4894        case llvm::BitstreamEntry::EndBlock:
4895          DoneWithExtensionBlock = true;
4896          continue;
4897 
4898        case llvm::BitstreamEntry::Error:
4899          return true;
4900 
4901        case llvm::BitstreamEntry::Record:
4902          break;
4903        }
4904 
4905        Record.clear();
4906        StringRef Blob;
4907        unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4908        switch (RecCode) {
4909        case EXTENSION_METADATA: {
4910          ModuleFileExtensionMetadata Metadata;
4911          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4912            return true;
4913 
4914          Listener.readModuleFileExtension(Metadata);
4915          break;
4916        }
4917        }
4918       }
4919     }
4920     Stream = SavedStream;
4921   }
4922 
4923   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4924   if (readUnhashedControlBlockImpl(
4925           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
4926           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
4927           ValidateDiagnosticOptions) != Success)
4928     return true;
4929 
4930   return false;
4931 }
4932 
4933 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
4934                                     const PCHContainerReader &PCHContainerRdr,
4935                                     const LangOptions &LangOpts,
4936                                     const TargetOptions &TargetOpts,
4937                                     const PreprocessorOptions &PPOpts,
4938                                     StringRef ExistingModuleCachePath) {
4939   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4940                                ExistingModuleCachePath, FileMgr);
4941   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4942                                   /*FindModuleFileExtensions=*/false,
4943                                   validator,
4944                                   /*ValidateDiagnosticOptions=*/true);
4945 }
4946 
4947 ASTReader::ASTReadResult
4948 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4949   // Enter the submodule block.
4950   if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4951     Error("malformed submodule block record in AST file");
4952     return Failure;
4953   }
4954 
4955   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4956   bool First = true;
4957   Module *CurrentModule = nullptr;
4958   RecordData Record;
4959   while (true) {
4960     llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4961 
4962     switch (Entry.Kind) {
4963     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4964     case llvm::BitstreamEntry::Error:
4965       Error("malformed block record in AST file");
4966       return Failure;
4967     case llvm::BitstreamEntry::EndBlock:
4968       return Success;
4969     case llvm::BitstreamEntry::Record:
4970       // The interesting case.
4971       break;
4972     }
4973 
4974     // Read a record.
4975     StringRef Blob;
4976     Record.clear();
4977     auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4978 
4979     if ((Kind == SUBMODULE_METADATA) != First) {
4980       Error("submodule metadata record should be at beginning of block");
4981       return Failure;
4982     }
4983     First = false;
4984 
4985     // Submodule information is only valid if we have a current module.
4986     // FIXME: Should we error on these cases?
4987     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4988         Kind != SUBMODULE_DEFINITION)
4989       continue;
4990 
4991     switch (Kind) {
4992     default:  // Default behavior: ignore.
4993       break;
4994 
4995     case SUBMODULE_DEFINITION: {
4996       if (Record.size() < 12) {
4997         Error("malformed module definition");
4998         return Failure;
4999       }
5000 
5001       StringRef Name = Blob;
5002       unsigned Idx = 0;
5003       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5004       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5005       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5006       bool IsFramework = Record[Idx++];
5007       bool IsExplicit = Record[Idx++];
5008       bool IsSystem = Record[Idx++];
5009       bool IsExternC = Record[Idx++];
5010       bool InferSubmodules = Record[Idx++];
5011       bool InferExplicitSubmodules = Record[Idx++];
5012       bool InferExportWildcard = Record[Idx++];
5013       bool ConfigMacrosExhaustive = Record[Idx++];
5014       bool ModuleMapIsPrivate = Record[Idx++];
5015 
5016       Module *ParentModule = nullptr;
5017       if (Parent)
5018         ParentModule = getSubmodule(Parent);
5019 
5020       // Retrieve this (sub)module from the module map, creating it if
5021       // necessary.
5022       CurrentModule =
5023           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5024               .first;
5025 
5026       // FIXME: set the definition loc for CurrentModule, or call
5027       // ModMap.setInferredModuleAllowedBy()
5028 
5029       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5030       if (GlobalIndex >= SubmodulesLoaded.size() ||
5031           SubmodulesLoaded[GlobalIndex]) {
5032         Error("too many submodules");
5033         return Failure;
5034       }
5035 
5036       if (!ParentModule) {
5037         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5038           // Don't emit module relocation error if we have -fno-validate-pch
5039           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5040               CurFile != F.File) {
5041             if (!Diags.isDiagnosticInFlight()) {
5042               Diag(diag::err_module_file_conflict)
5043                 << CurrentModule->getTopLevelModuleName()
5044                 << CurFile->getName()
5045                 << F.File->getName();
5046             }
5047             return Failure;
5048           }
5049         }
5050 
5051         CurrentModule->setASTFile(F.File);
5052         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5053       }
5054 
5055       CurrentModule->Kind = Kind;
5056       CurrentModule->Signature = F.Signature;
5057       CurrentModule->IsFromModuleFile = true;
5058       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5059       CurrentModule->IsExternC = IsExternC;
5060       CurrentModule->InferSubmodules = InferSubmodules;
5061       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5062       CurrentModule->InferExportWildcard = InferExportWildcard;
5063       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5064       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5065       if (DeserializationListener)
5066         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5067 
5068       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5069 
5070       // Clear out data that will be replaced by what is in the module file.
5071       CurrentModule->LinkLibraries.clear();
5072       CurrentModule->ConfigMacros.clear();
5073       CurrentModule->UnresolvedConflicts.clear();
5074       CurrentModule->Conflicts.clear();
5075 
5076       // The module is available unless it's missing a requirement; relevant
5077       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5078       // Missing headers that were present when the module was built do not
5079       // make it unavailable -- if we got this far, this must be an explicitly
5080       // imported module file.
5081       CurrentModule->Requirements.clear();
5082       CurrentModule->MissingHeaders.clear();
5083       CurrentModule->IsMissingRequirement =
5084           ParentModule && ParentModule->IsMissingRequirement;
5085       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5086       break;
5087     }
5088 
5089     case SUBMODULE_UMBRELLA_HEADER: {
5090       std::string Filename = Blob;
5091       ResolveImportedPath(F, Filename);
5092       if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
5093         if (!CurrentModule->getUmbrellaHeader())
5094           ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5095         else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
5096           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5097             Error("mismatched umbrella headers in submodule");
5098           return OutOfDate;
5099         }
5100       }
5101       break;
5102     }
5103 
5104     case SUBMODULE_HEADER:
5105     case SUBMODULE_EXCLUDED_HEADER:
5106     case SUBMODULE_PRIVATE_HEADER:
5107       // We lazily associate headers with their modules via the HeaderInfo table.
5108       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5109       // of complete filenames or remove it entirely.
5110       break;
5111 
5112     case SUBMODULE_TEXTUAL_HEADER:
5113     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5114       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5115       // them here.
5116       break;
5117 
5118     case SUBMODULE_TOPHEADER:
5119       CurrentModule->addTopHeaderFilename(Blob);
5120       break;
5121 
5122     case SUBMODULE_UMBRELLA_DIR: {
5123       std::string Dirname = Blob;
5124       ResolveImportedPath(F, Dirname);
5125       if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5126         if (!CurrentModule->getUmbrellaDir())
5127           ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5128         else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
5129           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5130             Error("mismatched umbrella directories in submodule");
5131           return OutOfDate;
5132         }
5133       }
5134       break;
5135     }
5136 
5137     case SUBMODULE_METADATA: {
5138       F.BaseSubmoduleID = getTotalNumSubmodules();
5139       F.LocalNumSubmodules = Record[0];
5140       unsigned LocalBaseSubmoduleID = Record[1];
5141       if (F.LocalNumSubmodules > 0) {
5142         // Introduce the global -> local mapping for submodules within this
5143         // module.
5144         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5145 
5146         // Introduce the local -> global mapping for submodules within this
5147         // module.
5148         F.SubmoduleRemap.insertOrReplace(
5149           std::make_pair(LocalBaseSubmoduleID,
5150                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5151 
5152         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5153       }
5154       break;
5155     }
5156 
5157     case SUBMODULE_IMPORTS:
5158       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5159         UnresolvedModuleRef Unresolved;
5160         Unresolved.File = &F;
5161         Unresolved.Mod = CurrentModule;
5162         Unresolved.ID = Record[Idx];
5163         Unresolved.Kind = UnresolvedModuleRef::Import;
5164         Unresolved.IsWildcard = false;
5165         UnresolvedModuleRefs.push_back(Unresolved);
5166       }
5167       break;
5168 
5169     case SUBMODULE_EXPORTS:
5170       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5171         UnresolvedModuleRef Unresolved;
5172         Unresolved.File = &F;
5173         Unresolved.Mod = CurrentModule;
5174         Unresolved.ID = Record[Idx];
5175         Unresolved.Kind = UnresolvedModuleRef::Export;
5176         Unresolved.IsWildcard = Record[Idx + 1];
5177         UnresolvedModuleRefs.push_back(Unresolved);
5178       }
5179 
5180       // Once we've loaded the set of exports, there's no reason to keep
5181       // the parsed, unresolved exports around.
5182       CurrentModule->UnresolvedExports.clear();
5183       break;
5184 
5185     case SUBMODULE_REQUIRES:
5186       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5187                                     PP.getTargetInfo());
5188       break;
5189 
5190     case SUBMODULE_LINK_LIBRARY:
5191       ModMap.resolveLinkAsDependencies(CurrentModule);
5192       CurrentModule->LinkLibraries.push_back(
5193                                          Module::LinkLibrary(Blob, Record[0]));
5194       break;
5195 
5196     case SUBMODULE_CONFIG_MACRO:
5197       CurrentModule->ConfigMacros.push_back(Blob.str());
5198       break;
5199 
5200     case SUBMODULE_CONFLICT: {
5201       UnresolvedModuleRef Unresolved;
5202       Unresolved.File = &F;
5203       Unresolved.Mod = CurrentModule;
5204       Unresolved.ID = Record[0];
5205       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5206       Unresolved.IsWildcard = false;
5207       Unresolved.String = Blob;
5208       UnresolvedModuleRefs.push_back(Unresolved);
5209       break;
5210     }
5211 
5212     case SUBMODULE_INITIALIZERS: {
5213       if (!ContextObj)
5214         break;
5215       SmallVector<uint32_t, 16> Inits;
5216       for (auto &ID : Record)
5217         Inits.push_back(getGlobalDeclID(F, ID));
5218       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5219       break;
5220     }
5221 
5222     case SUBMODULE_EXPORT_AS:
5223       CurrentModule->ExportAsModule = Blob.str();
5224       ModMap.addLinkAsDependency(CurrentModule);
5225       break;
5226     }
5227   }
5228 }
5229 
5230 /// Parse the record that corresponds to a LangOptions data
5231 /// structure.
5232 ///
5233 /// This routine parses the language options from the AST file and then gives
5234 /// them to the AST listener if one is set.
5235 ///
5236 /// \returns true if the listener deems the file unacceptable, false otherwise.
5237 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5238                                      bool Complain,
5239                                      ASTReaderListener &Listener,
5240                                      bool AllowCompatibleDifferences) {
5241   LangOptions LangOpts;
5242   unsigned Idx = 0;
5243 #define LANGOPT(Name, Bits, Default, Description) \
5244   LangOpts.Name = Record[Idx++];
5245 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5246   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5247 #include "clang/Basic/LangOptions.def"
5248 #define SANITIZER(NAME, ID)                                                    \
5249   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5250 #include "clang/Basic/Sanitizers.def"
5251 
5252   for (unsigned N = Record[Idx++]; N; --N)
5253     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5254 
5255   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5256   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5257   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5258 
5259   LangOpts.CurrentModule = ReadString(Record, Idx);
5260 
5261   // Comment options.
5262   for (unsigned N = Record[Idx++]; N; --N) {
5263     LangOpts.CommentOpts.BlockCommandNames.push_back(
5264       ReadString(Record, Idx));
5265   }
5266   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5267 
5268   // OpenMP offloading options.
5269   for (unsigned N = Record[Idx++]; N; --N) {
5270     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5271   }
5272 
5273   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5274 
5275   return Listener.ReadLanguageOptions(LangOpts, Complain,
5276                                       AllowCompatibleDifferences);
5277 }
5278 
5279 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5280                                    ASTReaderListener &Listener,
5281                                    bool AllowCompatibleDifferences) {
5282   unsigned Idx = 0;
5283   TargetOptions TargetOpts;
5284   TargetOpts.Triple = ReadString(Record, Idx);
5285   TargetOpts.CPU = ReadString(Record, Idx);
5286   TargetOpts.ABI = ReadString(Record, Idx);
5287   for (unsigned N = Record[Idx++]; N; --N) {
5288     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5289   }
5290   for (unsigned N = Record[Idx++]; N; --N) {
5291     TargetOpts.Features.push_back(ReadString(Record, Idx));
5292   }
5293 
5294   return Listener.ReadTargetOptions(TargetOpts, Complain,
5295                                     AllowCompatibleDifferences);
5296 }
5297 
5298 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5299                                        ASTReaderListener &Listener) {
5300   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5301   unsigned Idx = 0;
5302 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5303 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5304   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5305 #include "clang/Basic/DiagnosticOptions.def"
5306 
5307   for (unsigned N = Record[Idx++]; N; --N)
5308     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5309   for (unsigned N = Record[Idx++]; N; --N)
5310     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5311 
5312   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5313 }
5314 
5315 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5316                                        ASTReaderListener &Listener) {
5317   FileSystemOptions FSOpts;
5318   unsigned Idx = 0;
5319   FSOpts.WorkingDir = ReadString(Record, Idx);
5320   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5321 }
5322 
5323 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5324                                          bool Complain,
5325                                          ASTReaderListener &Listener) {
5326   HeaderSearchOptions HSOpts;
5327   unsigned Idx = 0;
5328   HSOpts.Sysroot = ReadString(Record, Idx);
5329 
5330   // Include entries.
5331   for (unsigned N = Record[Idx++]; N; --N) {
5332     std::string Path = ReadString(Record, Idx);
5333     frontend::IncludeDirGroup Group
5334       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5335     bool IsFramework = Record[Idx++];
5336     bool IgnoreSysRoot = Record[Idx++];
5337     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5338                                     IgnoreSysRoot);
5339   }
5340 
5341   // System header prefixes.
5342   for (unsigned N = Record[Idx++]; N; --N) {
5343     std::string Prefix = ReadString(Record, Idx);
5344     bool IsSystemHeader = Record[Idx++];
5345     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5346   }
5347 
5348   HSOpts.ResourceDir = ReadString(Record, Idx);
5349   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5350   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5351   HSOpts.DisableModuleHash = Record[Idx++];
5352   HSOpts.ImplicitModuleMaps = Record[Idx++];
5353   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5354   HSOpts.UseBuiltinIncludes = Record[Idx++];
5355   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5356   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5357   HSOpts.UseLibcxx = Record[Idx++];
5358   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5359 
5360   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5361                                           Complain);
5362 }
5363 
5364 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5365                                          bool Complain,
5366                                          ASTReaderListener &Listener,
5367                                          std::string &SuggestedPredefines) {
5368   PreprocessorOptions PPOpts;
5369   unsigned Idx = 0;
5370 
5371   // Macro definitions/undefs
5372   for (unsigned N = Record[Idx++]; N; --N) {
5373     std::string Macro = ReadString(Record, Idx);
5374     bool IsUndef = Record[Idx++];
5375     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5376   }
5377 
5378   // Includes
5379   for (unsigned N = Record[Idx++]; N; --N) {
5380     PPOpts.Includes.push_back(ReadString(Record, Idx));
5381   }
5382 
5383   // Macro Includes
5384   for (unsigned N = Record[Idx++]; N; --N) {
5385     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5386   }
5387 
5388   PPOpts.UsePredefines = Record[Idx++];
5389   PPOpts.DetailedRecord = Record[Idx++];
5390   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5391   PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
5392   PPOpts.ObjCXXARCStandardLibrary =
5393     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5394   SuggestedPredefines.clear();
5395   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5396                                           SuggestedPredefines);
5397 }
5398 
5399 std::pair<ModuleFile *, unsigned>
5400 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5401   GlobalPreprocessedEntityMapType::iterator
5402   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5403   assert(I != GlobalPreprocessedEntityMap.end() &&
5404          "Corrupted global preprocessed entity map");
5405   ModuleFile *M = I->second;
5406   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5407   return std::make_pair(M, LocalIndex);
5408 }
5409 
5410 llvm::iterator_range<PreprocessingRecord::iterator>
5411 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5412   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5413     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5414                                              Mod.NumPreprocessedEntities);
5415 
5416   return llvm::make_range(PreprocessingRecord::iterator(),
5417                           PreprocessingRecord::iterator());
5418 }
5419 
5420 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5421 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5422   return llvm::make_range(
5423       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5424       ModuleDeclIterator(this, &Mod,
5425                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5426 }
5427 
5428 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5429   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5430   assert(I != GlobalSkippedRangeMap.end() &&
5431     "Corrupted global skipped range map");
5432   ModuleFile *M = I->second;
5433   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5434   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5435   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5436   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5437                     TranslateSourceLocation(*M, RawRange.getEnd()));
5438   assert(Range.isValid());
5439   return Range;
5440 }
5441 
5442 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5443   PreprocessedEntityID PPID = Index+1;
5444   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5445   ModuleFile &M = *PPInfo.first;
5446   unsigned LocalIndex = PPInfo.second;
5447   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5448 
5449   if (!PP.getPreprocessingRecord()) {
5450     Error("no preprocessing record");
5451     return nullptr;
5452   }
5453 
5454   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5455   M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
5456 
5457   llvm::BitstreamEntry Entry =
5458     M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5459   if (Entry.Kind != llvm::BitstreamEntry::Record)
5460     return nullptr;
5461 
5462   // Read the record.
5463   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5464                     TranslateSourceLocation(M, PPOffs.getEnd()));
5465   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5466   StringRef Blob;
5467   RecordData Record;
5468   PreprocessorDetailRecordTypes RecType =
5469     (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
5470                                           Entry.ID, Record, &Blob);
5471   switch (RecType) {
5472   case PPD_MACRO_EXPANSION: {
5473     bool isBuiltin = Record[0];
5474     IdentifierInfo *Name = nullptr;
5475     MacroDefinitionRecord *Def = nullptr;
5476     if (isBuiltin)
5477       Name = getLocalIdentifier(M, Record[1]);
5478     else {
5479       PreprocessedEntityID GlobalID =
5480           getGlobalPreprocessedEntityID(M, Record[1]);
5481       Def = cast<MacroDefinitionRecord>(
5482           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5483     }
5484 
5485     MacroExpansion *ME;
5486     if (isBuiltin)
5487       ME = new (PPRec) MacroExpansion(Name, Range);
5488     else
5489       ME = new (PPRec) MacroExpansion(Def, Range);
5490 
5491     return ME;
5492   }
5493 
5494   case PPD_MACRO_DEFINITION: {
5495     // Decode the identifier info and then check again; if the macro is
5496     // still defined and associated with the identifier,
5497     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5498     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5499 
5500     if (DeserializationListener)
5501       DeserializationListener->MacroDefinitionRead(PPID, MD);
5502 
5503     return MD;
5504   }
5505 
5506   case PPD_INCLUSION_DIRECTIVE: {
5507     const char *FullFileNameStart = Blob.data() + Record[0];
5508     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5509     const FileEntry *File = nullptr;
5510     if (!FullFileName.empty())
5511       File = PP.getFileManager().getFile(FullFileName);
5512 
5513     // FIXME: Stable encoding
5514     InclusionDirective::InclusionKind Kind
5515       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5516     InclusionDirective *ID
5517       = new (PPRec) InclusionDirective(PPRec, Kind,
5518                                        StringRef(Blob.data(), Record[0]),
5519                                        Record[1], Record[3],
5520                                        File,
5521                                        Range);
5522     return ID;
5523   }
5524   }
5525 
5526   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5527 }
5528 
5529 /// Find the next module that contains entities and return the ID
5530 /// of the first entry.
5531 ///
5532 /// \param SLocMapI points at a chunk of a module that contains no
5533 /// preprocessed entities or the entities it contains are not the ones we are
5534 /// looking for.
5535 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5536                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5537   ++SLocMapI;
5538   for (GlobalSLocOffsetMapType::const_iterator
5539          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5540     ModuleFile &M = *SLocMapI->second;
5541     if (M.NumPreprocessedEntities)
5542       return M.BasePreprocessedEntityID;
5543   }
5544 
5545   return getTotalNumPreprocessedEntities();
5546 }
5547 
5548 namespace {
5549 
5550 struct PPEntityComp {
5551   const ASTReader &Reader;
5552   ModuleFile &M;
5553 
5554   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5555 
5556   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5557     SourceLocation LHS = getLoc(L);
5558     SourceLocation RHS = getLoc(R);
5559     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5560   }
5561 
5562   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5563     SourceLocation LHS = getLoc(L);
5564     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5565   }
5566 
5567   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5568     SourceLocation RHS = getLoc(R);
5569     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5570   }
5571 
5572   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5573     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5574   }
5575 };
5576 
5577 } // namespace
5578 
5579 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5580                                                        bool EndsAfter) const {
5581   if (SourceMgr.isLocalSourceLocation(Loc))
5582     return getTotalNumPreprocessedEntities();
5583 
5584   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5585       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5586   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5587          "Corrupted global sloc offset map");
5588 
5589   if (SLocMapI->second->NumPreprocessedEntities == 0)
5590     return findNextPreprocessedEntity(SLocMapI);
5591 
5592   ModuleFile &M = *SLocMapI->second;
5593 
5594   using pp_iterator = const PPEntityOffset *;
5595 
5596   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5597   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5598 
5599   size_t Count = M.NumPreprocessedEntities;
5600   size_t Half;
5601   pp_iterator First = pp_begin;
5602   pp_iterator PPI;
5603 
5604   if (EndsAfter) {
5605     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5606                            PPEntityComp(*this, M));
5607   } else {
5608     // Do a binary search manually instead of using std::lower_bound because
5609     // The end locations of entities may be unordered (when a macro expansion
5610     // is inside another macro argument), but for this case it is not important
5611     // whether we get the first macro expansion or its containing macro.
5612     while (Count > 0) {
5613       Half = Count / 2;
5614       PPI = First;
5615       std::advance(PPI, Half);
5616       if (SourceMgr.isBeforeInTranslationUnit(
5617               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
5618         First = PPI;
5619         ++First;
5620         Count = Count - Half - 1;
5621       } else
5622         Count = Half;
5623     }
5624   }
5625 
5626   if (PPI == pp_end)
5627     return findNextPreprocessedEntity(SLocMapI);
5628 
5629   return M.BasePreprocessedEntityID + (PPI - pp_begin);
5630 }
5631 
5632 /// Returns a pair of [Begin, End) indices of preallocated
5633 /// preprocessed entities that \arg Range encompasses.
5634 std::pair<unsigned, unsigned>
5635     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5636   if (Range.isInvalid())
5637     return std::make_pair(0,0);
5638   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5639 
5640   PreprocessedEntityID BeginID =
5641       findPreprocessedEntity(Range.getBegin(), false);
5642   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5643   return std::make_pair(BeginID, EndID);
5644 }
5645 
5646 /// Optionally returns true or false if the preallocated preprocessed
5647 /// entity with index \arg Index came from file \arg FID.
5648 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5649                                                              FileID FID) {
5650   if (FID.isInvalid())
5651     return false;
5652 
5653   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5654   ModuleFile &M = *PPInfo.first;
5655   unsigned LocalIndex = PPInfo.second;
5656   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5657 
5658   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
5659   if (Loc.isInvalid())
5660     return false;
5661 
5662   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5663     return true;
5664   else
5665     return false;
5666 }
5667 
5668 namespace {
5669 
5670   /// Visitor used to search for information about a header file.
5671   class HeaderFileInfoVisitor {
5672     const FileEntry *FE;
5673     Optional<HeaderFileInfo> HFI;
5674 
5675   public:
5676     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
5677 
5678     bool operator()(ModuleFile &M) {
5679       HeaderFileInfoLookupTable *Table
5680         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5681       if (!Table)
5682         return false;
5683 
5684       // Look in the on-disk hash table for an entry for this file name.
5685       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
5686       if (Pos == Table->end())
5687         return false;
5688 
5689       HFI = *Pos;
5690       return true;
5691     }
5692 
5693     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5694   };
5695 
5696 } // namespace
5697 
5698 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5699   HeaderFileInfoVisitor Visitor(FE);
5700   ModuleMgr.visit(Visitor);
5701   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5702     return *HFI;
5703 
5704   return HeaderFileInfo();
5705 }
5706 
5707 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5708   using DiagState = DiagnosticsEngine::DiagState;
5709   SmallVector<DiagState *, 32> DiagStates;
5710 
5711   for (ModuleFile &F : ModuleMgr) {
5712     unsigned Idx = 0;
5713     auto &Record = F.PragmaDiagMappings;
5714     if (Record.empty())
5715       continue;
5716 
5717     DiagStates.clear();
5718 
5719     auto ReadDiagState =
5720         [&](const DiagState &BasedOn, SourceLocation Loc,
5721             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
5722       unsigned BackrefID = Record[Idx++];
5723       if (BackrefID != 0)
5724         return DiagStates[BackrefID - 1];
5725 
5726       // A new DiagState was created here.
5727       Diag.DiagStates.push_back(BasedOn);
5728       DiagState *NewState = &Diag.DiagStates.back();
5729       DiagStates.push_back(NewState);
5730       unsigned Size = Record[Idx++];
5731       assert(Idx + Size * 2 <= Record.size() &&
5732              "Invalid data, not enough diag/map pairs");
5733       while (Size--) {
5734         unsigned DiagID = Record[Idx++];
5735         DiagnosticMapping NewMapping =
5736             DiagnosticMapping::deserialize(Record[Idx++]);
5737         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
5738           continue;
5739 
5740         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
5741 
5742         // If this mapping was specified as a warning but the severity was
5743         // upgraded due to diagnostic settings, simulate the current diagnostic
5744         // settings (and use a warning).
5745         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
5746           NewMapping.setSeverity(diag::Severity::Warning);
5747           NewMapping.setUpgradedFromWarning(false);
5748         }
5749 
5750         Mapping = NewMapping;
5751       }
5752       return NewState;
5753     };
5754 
5755     // Read the first state.
5756     DiagState *FirstState;
5757     if (F.Kind == MK_ImplicitModule) {
5758       // Implicitly-built modules are reused with different diagnostic
5759       // settings.  Use the initial diagnostic state from Diag to simulate this
5760       // compilation's diagnostic settings.
5761       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
5762       DiagStates.push_back(FirstState);
5763 
5764       // Skip the initial diagnostic state from the serialized module.
5765       assert(Record[1] == 0 &&
5766              "Invalid data, unexpected backref in initial state");
5767       Idx = 3 + Record[2] * 2;
5768       assert(Idx < Record.size() &&
5769              "Invalid data, not enough state change pairs in initial state");
5770     } else if (F.isModule()) {
5771       // For an explicit module, preserve the flags from the module build
5772       // command line (-w, -Weverything, -Werror, ...) along with any explicit
5773       // -Wblah flags.
5774       unsigned Flags = Record[Idx++];
5775       DiagState Initial;
5776       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
5777       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
5778       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
5779       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
5780       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
5781       Initial.ExtBehavior = (diag::Severity)Flags;
5782       FirstState = ReadDiagState(Initial, SourceLocation(), true);
5783 
5784       assert(F.OriginalSourceFileID.isValid());
5785 
5786       // Set up the root buffer of the module to start with the initial
5787       // diagnostic state of the module itself, to cover files that contain no
5788       // explicit transitions (for which we did not serialize anything).
5789       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
5790           .StateTransitions.push_back({FirstState, 0});
5791     } else {
5792       // For prefix ASTs, start with whatever the user configured on the
5793       // command line.
5794       Idx++; // Skip flags.
5795       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
5796                                  SourceLocation(), false);
5797     }
5798 
5799     // Read the state transitions.
5800     unsigned NumLocations = Record[Idx++];
5801     while (NumLocations--) {
5802       assert(Idx < Record.size() &&
5803              "Invalid data, missing pragma diagnostic states");
5804       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
5805       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
5806       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
5807       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
5808       unsigned Transitions = Record[Idx++];
5809 
5810       // Note that we don't need to set up Parent/ParentOffset here, because
5811       // we won't be changing the diagnostic state within imported FileIDs
5812       // (other than perhaps appending to the main source file, which has no
5813       // parent).
5814       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
5815       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
5816       for (unsigned I = 0; I != Transitions; ++I) {
5817         unsigned Offset = Record[Idx++];
5818         auto *State =
5819             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
5820         F.StateTransitions.push_back({State, Offset});
5821       }
5822     }
5823 
5824     // Read the final state.
5825     assert(Idx < Record.size() &&
5826            "Invalid data, missing final pragma diagnostic state");
5827     SourceLocation CurStateLoc =
5828         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5829     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
5830 
5831     if (!F.isModule()) {
5832       Diag.DiagStatesByLoc.CurDiagState = CurState;
5833       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
5834 
5835       // Preserve the property that the imaginary root file describes the
5836       // current state.
5837       FileID NullFile;
5838       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
5839       if (T.empty())
5840         T.push_back({CurState, 0});
5841       else
5842         T[0].State = CurState;
5843     }
5844 
5845     // Don't try to read these mappings again.
5846     Record.clear();
5847   }
5848 }
5849 
5850 /// Get the correct cursor and offset for loading a type.
5851 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5852   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5853   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5854   ModuleFile *M = I->second;
5855   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5856 }
5857 
5858 /// Read and return the type with the given index..
5859 ///
5860 /// The index is the type ID, shifted and minus the number of predefs. This
5861 /// routine actually reads the record corresponding to the type at the given
5862 /// location. It is a helper routine for GetType, which deals with reading type
5863 /// IDs.
5864 QualType ASTReader::readTypeRecord(unsigned Index) {
5865   assert(ContextObj && "reading type with no AST context");
5866   ASTContext &Context = *ContextObj;
5867   RecordLocation Loc = TypeCursorForIndex(Index);
5868   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5869 
5870   // Keep track of where we are in the stream, then jump back there
5871   // after reading this type.
5872   SavedStreamPosition SavedPosition(DeclsCursor);
5873 
5874   ReadingKindTracker ReadingKind(Read_Type, *this);
5875 
5876   // Note that we are loading a type record.
5877   Deserializing AType(this);
5878 
5879   unsigned Idx = 0;
5880   DeclsCursor.JumpToBit(Loc.Offset);
5881   RecordData Record;
5882   unsigned Code = DeclsCursor.ReadCode();
5883   switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5884   case TYPE_EXT_QUAL: {
5885     if (Record.size() != 2) {
5886       Error("Incorrect encoding of extended qualifier type");
5887       return QualType();
5888     }
5889     QualType Base = readType(*Loc.F, Record, Idx);
5890     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5891     return Context.getQualifiedType(Base, Quals);
5892   }
5893 
5894   case TYPE_COMPLEX: {
5895     if (Record.size() != 1) {
5896       Error("Incorrect encoding of complex type");
5897       return QualType();
5898     }
5899     QualType ElemType = readType(*Loc.F, Record, Idx);
5900     return Context.getComplexType(ElemType);
5901   }
5902 
5903   case TYPE_POINTER: {
5904     if (Record.size() != 1) {
5905       Error("Incorrect encoding of pointer type");
5906       return QualType();
5907     }
5908     QualType PointeeType = readType(*Loc.F, Record, Idx);
5909     return Context.getPointerType(PointeeType);
5910   }
5911 
5912   case TYPE_DECAYED: {
5913     if (Record.size() != 1) {
5914       Error("Incorrect encoding of decayed type");
5915       return QualType();
5916     }
5917     QualType OriginalType = readType(*Loc.F, Record, Idx);
5918     QualType DT = Context.getAdjustedParameterType(OriginalType);
5919     if (!isa<DecayedType>(DT))
5920       Error("Decayed type does not decay");
5921     return DT;
5922   }
5923 
5924   case TYPE_ADJUSTED: {
5925     if (Record.size() != 2) {
5926       Error("Incorrect encoding of adjusted type");
5927       return QualType();
5928     }
5929     QualType OriginalTy = readType(*Loc.F, Record, Idx);
5930     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5931     return Context.getAdjustedType(OriginalTy, AdjustedTy);
5932   }
5933 
5934   case TYPE_BLOCK_POINTER: {
5935     if (Record.size() != 1) {
5936       Error("Incorrect encoding of block pointer type");
5937       return QualType();
5938     }
5939     QualType PointeeType = readType(*Loc.F, Record, Idx);
5940     return Context.getBlockPointerType(PointeeType);
5941   }
5942 
5943   case TYPE_LVALUE_REFERENCE: {
5944     if (Record.size() != 2) {
5945       Error("Incorrect encoding of lvalue reference type");
5946       return QualType();
5947     }
5948     QualType PointeeType = readType(*Loc.F, Record, Idx);
5949     return Context.getLValueReferenceType(PointeeType, Record[1]);
5950   }
5951 
5952   case TYPE_RVALUE_REFERENCE: {
5953     if (Record.size() != 1) {
5954       Error("Incorrect encoding of rvalue reference type");
5955       return QualType();
5956     }
5957     QualType PointeeType = readType(*Loc.F, Record, Idx);
5958     return Context.getRValueReferenceType(PointeeType);
5959   }
5960 
5961   case TYPE_MEMBER_POINTER: {
5962     if (Record.size() != 2) {
5963       Error("Incorrect encoding of member pointer type");
5964       return QualType();
5965     }
5966     QualType PointeeType = readType(*Loc.F, Record, Idx);
5967     QualType ClassType = readType(*Loc.F, Record, Idx);
5968     if (PointeeType.isNull() || ClassType.isNull())
5969       return QualType();
5970 
5971     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5972   }
5973 
5974   case TYPE_CONSTANT_ARRAY: {
5975     QualType ElementType = readType(*Loc.F, Record, Idx);
5976     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5977     unsigned IndexTypeQuals = Record[2];
5978     unsigned Idx = 3;
5979     llvm::APInt Size = ReadAPInt(Record, Idx);
5980     return Context.getConstantArrayType(ElementType, Size,
5981                                          ASM, IndexTypeQuals);
5982   }
5983 
5984   case TYPE_INCOMPLETE_ARRAY: {
5985     QualType ElementType = readType(*Loc.F, Record, Idx);
5986     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5987     unsigned IndexTypeQuals = Record[2];
5988     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5989   }
5990 
5991   case TYPE_VARIABLE_ARRAY: {
5992     QualType ElementType = readType(*Loc.F, Record, Idx);
5993     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5994     unsigned IndexTypeQuals = Record[2];
5995     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5996     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5997     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5998                                          ASM, IndexTypeQuals,
5999                                          SourceRange(LBLoc, RBLoc));
6000   }
6001 
6002   case TYPE_VECTOR: {
6003     if (Record.size() != 3) {
6004       Error("incorrect encoding of vector type in AST file");
6005       return QualType();
6006     }
6007 
6008     QualType ElementType = readType(*Loc.F, Record, Idx);
6009     unsigned NumElements = Record[1];
6010     unsigned VecKind = Record[2];
6011     return Context.getVectorType(ElementType, NumElements,
6012                                   (VectorType::VectorKind)VecKind);
6013   }
6014 
6015   case TYPE_EXT_VECTOR: {
6016     if (Record.size() != 3) {
6017       Error("incorrect encoding of extended vector type in AST file");
6018       return QualType();
6019     }
6020 
6021     QualType ElementType = readType(*Loc.F, Record, Idx);
6022     unsigned NumElements = Record[1];
6023     return Context.getExtVectorType(ElementType, NumElements);
6024   }
6025 
6026   case TYPE_FUNCTION_NO_PROTO: {
6027     if (Record.size() != 8) {
6028       Error("incorrect encoding of no-proto function type");
6029       return QualType();
6030     }
6031     QualType ResultType = readType(*Loc.F, Record, Idx);
6032     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6033                                (CallingConv)Record[4], Record[5], Record[6],
6034                                Record[7]);
6035     return Context.getFunctionNoProtoType(ResultType, Info);
6036   }
6037 
6038   case TYPE_FUNCTION_PROTO: {
6039     QualType ResultType = readType(*Loc.F, Record, Idx);
6040 
6041     FunctionProtoType::ExtProtoInfo EPI;
6042     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6043                                         /*hasregparm*/ Record[2],
6044                                         /*regparm*/ Record[3],
6045                                         static_cast<CallingConv>(Record[4]),
6046                                         /*produces*/ Record[5],
6047                                         /*nocallersavedregs*/ Record[6],
6048                                         /*nocfcheck*/ Record[7]);
6049 
6050     unsigned Idx = 8;
6051 
6052     EPI.Variadic = Record[Idx++];
6053     EPI.HasTrailingReturn = Record[Idx++];
6054     EPI.TypeQuals = Record[Idx++];
6055     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6056     SmallVector<QualType, 8> ExceptionStorage;
6057     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6058 
6059     unsigned NumParams = Record[Idx++];
6060     SmallVector<QualType, 16> ParamTypes;
6061     for (unsigned I = 0; I != NumParams; ++I)
6062       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6063 
6064     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6065     if (Idx != Record.size()) {
6066       for (unsigned I = 0; I != NumParams; ++I)
6067         ExtParameterInfos.push_back(
6068           FunctionProtoType::ExtParameterInfo
6069                            ::getFromOpaqueValue(Record[Idx++]));
6070       EPI.ExtParameterInfos = ExtParameterInfos.data();
6071     }
6072 
6073     assert(Idx == Record.size());
6074 
6075     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6076   }
6077 
6078   case TYPE_UNRESOLVED_USING: {
6079     unsigned Idx = 0;
6080     return Context.getTypeDeclType(
6081                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6082   }
6083 
6084   case TYPE_TYPEDEF: {
6085     if (Record.size() != 2) {
6086       Error("incorrect encoding of typedef type");
6087       return QualType();
6088     }
6089     unsigned Idx = 0;
6090     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6091     QualType Canonical = readType(*Loc.F, Record, Idx);
6092     if (!Canonical.isNull())
6093       Canonical = Context.getCanonicalType(Canonical);
6094     return Context.getTypedefType(Decl, Canonical);
6095   }
6096 
6097   case TYPE_TYPEOF_EXPR:
6098     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6099 
6100   case TYPE_TYPEOF: {
6101     if (Record.size() != 1) {
6102       Error("incorrect encoding of typeof(type) in AST file");
6103       return QualType();
6104     }
6105     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6106     return Context.getTypeOfType(UnderlyingType);
6107   }
6108 
6109   case TYPE_DECLTYPE: {
6110     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6111     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6112   }
6113 
6114   case TYPE_UNARY_TRANSFORM: {
6115     QualType BaseType = readType(*Loc.F, Record, Idx);
6116     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6117     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6118     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6119   }
6120 
6121   case TYPE_AUTO: {
6122     QualType Deduced = readType(*Loc.F, Record, Idx);
6123     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6124     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6125     return Context.getAutoType(Deduced, Keyword, IsDependent);
6126   }
6127 
6128   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6129     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6130     QualType Deduced = readType(*Loc.F, Record, Idx);
6131     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6132     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6133                                                         IsDependent);
6134   }
6135 
6136   case TYPE_RECORD: {
6137     if (Record.size() != 2) {
6138       Error("incorrect encoding of record type");
6139       return QualType();
6140     }
6141     unsigned Idx = 0;
6142     bool IsDependent = Record[Idx++];
6143     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6144     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6145     QualType T = Context.getRecordType(RD);
6146     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6147     return T;
6148   }
6149 
6150   case TYPE_ENUM: {
6151     if (Record.size() != 2) {
6152       Error("incorrect encoding of enum type");
6153       return QualType();
6154     }
6155     unsigned Idx = 0;
6156     bool IsDependent = Record[Idx++];
6157     QualType T
6158       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6159     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6160     return T;
6161   }
6162 
6163   case TYPE_ATTRIBUTED: {
6164     if (Record.size() != 3) {
6165       Error("incorrect encoding of attributed type");
6166       return QualType();
6167     }
6168     QualType modifiedType = readType(*Loc.F, Record, Idx);
6169     QualType equivalentType = readType(*Loc.F, Record, Idx);
6170     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6171     return Context.getAttributedType(kind, modifiedType, equivalentType);
6172   }
6173 
6174   case TYPE_PAREN: {
6175     if (Record.size() != 1) {
6176       Error("incorrect encoding of paren type");
6177       return QualType();
6178     }
6179     QualType InnerType = readType(*Loc.F, Record, Idx);
6180     return Context.getParenType(InnerType);
6181   }
6182 
6183   case TYPE_PACK_EXPANSION: {
6184     if (Record.size() != 2) {
6185       Error("incorrect encoding of pack expansion type");
6186       return QualType();
6187     }
6188     QualType Pattern = readType(*Loc.F, Record, Idx);
6189     if (Pattern.isNull())
6190       return QualType();
6191     Optional<unsigned> NumExpansions;
6192     if (Record[1])
6193       NumExpansions = Record[1] - 1;
6194     return Context.getPackExpansionType(Pattern, NumExpansions);
6195   }
6196 
6197   case TYPE_ELABORATED: {
6198     unsigned Idx = 0;
6199     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6200     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6201     QualType NamedType = readType(*Loc.F, Record, Idx);
6202     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6203     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6204   }
6205 
6206   case TYPE_OBJC_INTERFACE: {
6207     unsigned Idx = 0;
6208     ObjCInterfaceDecl *ItfD
6209       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6210     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6211   }
6212 
6213   case TYPE_OBJC_TYPE_PARAM: {
6214     unsigned Idx = 0;
6215     ObjCTypeParamDecl *Decl
6216       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6217     unsigned NumProtos = Record[Idx++];
6218     SmallVector<ObjCProtocolDecl*, 4> Protos;
6219     for (unsigned I = 0; I != NumProtos; ++I)
6220       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6221     return Context.getObjCTypeParamType(Decl, Protos);
6222   }
6223 
6224   case TYPE_OBJC_OBJECT: {
6225     unsigned Idx = 0;
6226     QualType Base = readType(*Loc.F, Record, Idx);
6227     unsigned NumTypeArgs = Record[Idx++];
6228     SmallVector<QualType, 4> TypeArgs;
6229     for (unsigned I = 0; I != NumTypeArgs; ++I)
6230       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6231     unsigned NumProtos = Record[Idx++];
6232     SmallVector<ObjCProtocolDecl*, 4> Protos;
6233     for (unsigned I = 0; I != NumProtos; ++I)
6234       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6235     bool IsKindOf = Record[Idx++];
6236     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6237   }
6238 
6239   case TYPE_OBJC_OBJECT_POINTER: {
6240     unsigned Idx = 0;
6241     QualType Pointee = readType(*Loc.F, Record, Idx);
6242     return Context.getObjCObjectPointerType(Pointee);
6243   }
6244 
6245   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6246     unsigned Idx = 0;
6247     QualType Parm = readType(*Loc.F, Record, Idx);
6248     QualType Replacement = readType(*Loc.F, Record, Idx);
6249     return Context.getSubstTemplateTypeParmType(
6250         cast<TemplateTypeParmType>(Parm),
6251         Context.getCanonicalType(Replacement));
6252   }
6253 
6254   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6255     unsigned Idx = 0;
6256     QualType Parm = readType(*Loc.F, Record, Idx);
6257     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6258     return Context.getSubstTemplateTypeParmPackType(
6259                                                cast<TemplateTypeParmType>(Parm),
6260                                                      ArgPack);
6261   }
6262 
6263   case TYPE_INJECTED_CLASS_NAME: {
6264     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6265     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6266     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6267     // for AST reading, too much interdependencies.
6268     const Type *T = nullptr;
6269     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6270       if (const Type *Existing = DI->getTypeForDecl()) {
6271         T = Existing;
6272         break;
6273       }
6274     }
6275     if (!T) {
6276       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6277       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6278         DI->setTypeForDecl(T);
6279     }
6280     return QualType(T, 0);
6281   }
6282 
6283   case TYPE_TEMPLATE_TYPE_PARM: {
6284     unsigned Idx = 0;
6285     unsigned Depth = Record[Idx++];
6286     unsigned Index = Record[Idx++];
6287     bool Pack = Record[Idx++];
6288     TemplateTypeParmDecl *D
6289       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6290     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6291   }
6292 
6293   case TYPE_DEPENDENT_NAME: {
6294     unsigned Idx = 0;
6295     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6296     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6297     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6298     QualType Canon = readType(*Loc.F, Record, Idx);
6299     if (!Canon.isNull())
6300       Canon = Context.getCanonicalType(Canon);
6301     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6302   }
6303 
6304   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6305     unsigned Idx = 0;
6306     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6307     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6308     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6309     unsigned NumArgs = Record[Idx++];
6310     SmallVector<TemplateArgument, 8> Args;
6311     Args.reserve(NumArgs);
6312     while (NumArgs--)
6313       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6314     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6315                                                           Args);
6316   }
6317 
6318   case TYPE_DEPENDENT_SIZED_ARRAY: {
6319     unsigned Idx = 0;
6320 
6321     // ArrayType
6322     QualType ElementType = readType(*Loc.F, Record, Idx);
6323     ArrayType::ArraySizeModifier ASM
6324       = (ArrayType::ArraySizeModifier)Record[Idx++];
6325     unsigned IndexTypeQuals = Record[Idx++];
6326 
6327     // DependentSizedArrayType
6328     Expr *NumElts = ReadExpr(*Loc.F);
6329     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6330 
6331     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6332                                                IndexTypeQuals, Brackets);
6333   }
6334 
6335   case TYPE_TEMPLATE_SPECIALIZATION: {
6336     unsigned Idx = 0;
6337     bool IsDependent = Record[Idx++];
6338     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6339     SmallVector<TemplateArgument, 8> Args;
6340     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6341     QualType Underlying = readType(*Loc.F, Record, Idx);
6342     QualType T;
6343     if (Underlying.isNull())
6344       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6345     else
6346       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6347     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6348     return T;
6349   }
6350 
6351   case TYPE_ATOMIC: {
6352     if (Record.size() != 1) {
6353       Error("Incorrect encoding of atomic type");
6354       return QualType();
6355     }
6356     QualType ValueType = readType(*Loc.F, Record, Idx);
6357     return Context.getAtomicType(ValueType);
6358   }
6359 
6360   case TYPE_PIPE: {
6361     if (Record.size() != 2) {
6362       Error("Incorrect encoding of pipe type");
6363       return QualType();
6364     }
6365 
6366     // Reading the pipe element type.
6367     QualType ElementType = readType(*Loc.F, Record, Idx);
6368     unsigned ReadOnly = Record[1];
6369     return Context.getPipeType(ElementType, ReadOnly);
6370   }
6371 
6372   case TYPE_DEPENDENT_SIZED_VECTOR: {
6373     unsigned Idx = 0;
6374     QualType ElementType = readType(*Loc.F, Record, Idx);
6375     Expr *SizeExpr = ReadExpr(*Loc.F);
6376     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6377     unsigned VecKind = Record[Idx];
6378 
6379     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6380                                                (VectorType::VectorKind)VecKind);
6381   }
6382 
6383   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6384     unsigned Idx = 0;
6385 
6386     // DependentSizedExtVectorType
6387     QualType ElementType = readType(*Loc.F, Record, Idx);
6388     Expr *SizeExpr = ReadExpr(*Loc.F);
6389     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6390 
6391     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6392                                                   AttrLoc);
6393   }
6394 
6395   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6396     unsigned Idx = 0;
6397 
6398     // DependentAddressSpaceType
6399     QualType PointeeType = readType(*Loc.F, Record, Idx);
6400     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6401     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6402 
6403     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6404                                                    AttrLoc);
6405   }
6406   }
6407   llvm_unreachable("Invalid TypeCode!");
6408 }
6409 
6410 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6411                                   SmallVectorImpl<QualType> &Exceptions,
6412                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6413                                   const RecordData &Record, unsigned &Idx) {
6414   ExceptionSpecificationType EST =
6415       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6416   ESI.Type = EST;
6417   if (EST == EST_Dynamic) {
6418     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6419       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6420     ESI.Exceptions = Exceptions;
6421   } else if (isComputedNoexcept(EST)) {
6422     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6423   } else if (EST == EST_Uninstantiated) {
6424     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6425     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6426   } else if (EST == EST_Unevaluated) {
6427     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6428   }
6429 }
6430 
6431 namespace clang {
6432 
6433 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6434   ModuleFile *F;
6435   ASTReader *Reader;
6436   const ASTReader::RecordData &Record;
6437   unsigned &Idx;
6438 
6439   SourceLocation ReadSourceLocation() {
6440     return Reader->ReadSourceLocation(*F, Record, Idx);
6441   }
6442 
6443   TypeSourceInfo *GetTypeSourceInfo() {
6444     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6445   }
6446 
6447   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6448     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6449   }
6450 
6451   Attr *ReadAttr() {
6452     return Reader->ReadAttr(*F, Record, Idx);
6453   }
6454 
6455 public:
6456   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6457                 const ASTReader::RecordData &Record, unsigned &Idx)
6458       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6459 
6460   // We want compile-time assurance that we've enumerated all of
6461   // these, so unfortunately we have to declare them first, then
6462   // define them out-of-line.
6463 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6464 #define TYPELOC(CLASS, PARENT) \
6465   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6466 #include "clang/AST/TypeLocNodes.def"
6467 
6468   void VisitFunctionTypeLoc(FunctionTypeLoc);
6469   void VisitArrayTypeLoc(ArrayTypeLoc);
6470 };
6471 
6472 } // namespace clang
6473 
6474 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6475   // nothing to do
6476 }
6477 
6478 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6479   TL.setBuiltinLoc(ReadSourceLocation());
6480   if (TL.needsExtraLocalData()) {
6481     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6482     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6483     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6484     TL.setModeAttr(Record[Idx++]);
6485   }
6486 }
6487 
6488 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6489   TL.setNameLoc(ReadSourceLocation());
6490 }
6491 
6492 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6493   TL.setStarLoc(ReadSourceLocation());
6494 }
6495 
6496 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6497   // nothing to do
6498 }
6499 
6500 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6501   // nothing to do
6502 }
6503 
6504 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6505   TL.setCaretLoc(ReadSourceLocation());
6506 }
6507 
6508 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6509   TL.setAmpLoc(ReadSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6513   TL.setAmpAmpLoc(ReadSourceLocation());
6514 }
6515 
6516 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6517   TL.setStarLoc(ReadSourceLocation());
6518   TL.setClassTInfo(GetTypeSourceInfo());
6519 }
6520 
6521 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6522   TL.setLBracketLoc(ReadSourceLocation());
6523   TL.setRBracketLoc(ReadSourceLocation());
6524   if (Record[Idx++])
6525     TL.setSizeExpr(Reader->ReadExpr(*F));
6526   else
6527     TL.setSizeExpr(nullptr);
6528 }
6529 
6530 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6531   VisitArrayTypeLoc(TL);
6532 }
6533 
6534 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6535   VisitArrayTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6539   VisitArrayTypeLoc(TL);
6540 }
6541 
6542 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6543                                             DependentSizedArrayTypeLoc TL) {
6544   VisitArrayTypeLoc(TL);
6545 }
6546 
6547 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6548     DependentAddressSpaceTypeLoc TL) {
6549 
6550     TL.setAttrNameLoc(ReadSourceLocation());
6551     SourceRange range;
6552     range.setBegin(ReadSourceLocation());
6553     range.setEnd(ReadSourceLocation());
6554     TL.setAttrOperandParensRange(range);
6555     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6556 }
6557 
6558 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6559                                         DependentSizedExtVectorTypeLoc TL) {
6560   TL.setNameLoc(ReadSourceLocation());
6561 }
6562 
6563 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6564   TL.setNameLoc(ReadSourceLocation());
6565 }
6566 
6567 void TypeLocReader::VisitDependentVectorTypeLoc(
6568     DependentVectorTypeLoc TL) {
6569   TL.setNameLoc(ReadSourceLocation());
6570 }
6571 
6572 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6573   TL.setNameLoc(ReadSourceLocation());
6574 }
6575 
6576 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6577   TL.setLocalRangeBegin(ReadSourceLocation());
6578   TL.setLParenLoc(ReadSourceLocation());
6579   TL.setRParenLoc(ReadSourceLocation());
6580   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
6581                                        Reader->ReadSourceLocation(*F, Record, Idx)));
6582   TL.setLocalRangeEnd(ReadSourceLocation());
6583   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6584     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
6585   }
6586 }
6587 
6588 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6589   VisitFunctionTypeLoc(TL);
6590 }
6591 
6592 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6593   VisitFunctionTypeLoc(TL);
6594 }
6595 
6596 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6597   TL.setNameLoc(ReadSourceLocation());
6598 }
6599 
6600 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6601   TL.setNameLoc(ReadSourceLocation());
6602 }
6603 
6604 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6605   TL.setTypeofLoc(ReadSourceLocation());
6606   TL.setLParenLoc(ReadSourceLocation());
6607   TL.setRParenLoc(ReadSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6611   TL.setTypeofLoc(ReadSourceLocation());
6612   TL.setLParenLoc(ReadSourceLocation());
6613   TL.setRParenLoc(ReadSourceLocation());
6614   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6615 }
6616 
6617 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6618   TL.setNameLoc(ReadSourceLocation());
6619 }
6620 
6621 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6622   TL.setKWLoc(ReadSourceLocation());
6623   TL.setLParenLoc(ReadSourceLocation());
6624   TL.setRParenLoc(ReadSourceLocation());
6625   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6626 }
6627 
6628 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6629   TL.setNameLoc(ReadSourceLocation());
6630 }
6631 
6632 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6633     DeducedTemplateSpecializationTypeLoc TL) {
6634   TL.setTemplateNameLoc(ReadSourceLocation());
6635 }
6636 
6637 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6638   TL.setNameLoc(ReadSourceLocation());
6639 }
6640 
6641 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6642   TL.setNameLoc(ReadSourceLocation());
6643 }
6644 
6645 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6646   TL.setAttr(ReadAttr());
6647 }
6648 
6649 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6650   TL.setNameLoc(ReadSourceLocation());
6651 }
6652 
6653 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6654                                             SubstTemplateTypeParmTypeLoc TL) {
6655   TL.setNameLoc(ReadSourceLocation());
6656 }
6657 
6658 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6659                                           SubstTemplateTypeParmPackTypeLoc TL) {
6660   TL.setNameLoc(ReadSourceLocation());
6661 }
6662 
6663 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6664                                            TemplateSpecializationTypeLoc TL) {
6665   TL.setTemplateKeywordLoc(ReadSourceLocation());
6666   TL.setTemplateNameLoc(ReadSourceLocation());
6667   TL.setLAngleLoc(ReadSourceLocation());
6668   TL.setRAngleLoc(ReadSourceLocation());
6669   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6670     TL.setArgLocInfo(
6671         i,
6672         Reader->GetTemplateArgumentLocInfo(
6673             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
6674 }
6675 
6676 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6677   TL.setLParenLoc(ReadSourceLocation());
6678   TL.setRParenLoc(ReadSourceLocation());
6679 }
6680 
6681 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6682   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6683   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6684 }
6685 
6686 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6687   TL.setNameLoc(ReadSourceLocation());
6688 }
6689 
6690 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6691   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6692   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6693   TL.setNameLoc(ReadSourceLocation());
6694 }
6695 
6696 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6697        DependentTemplateSpecializationTypeLoc TL) {
6698   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6699   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6700   TL.setTemplateKeywordLoc(ReadSourceLocation());
6701   TL.setTemplateNameLoc(ReadSourceLocation());
6702   TL.setLAngleLoc(ReadSourceLocation());
6703   TL.setRAngleLoc(ReadSourceLocation());
6704   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6705     TL.setArgLocInfo(
6706         I,
6707         Reader->GetTemplateArgumentLocInfo(
6708             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
6709 }
6710 
6711 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6712   TL.setEllipsisLoc(ReadSourceLocation());
6713 }
6714 
6715 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6716   TL.setNameLoc(ReadSourceLocation());
6717 }
6718 
6719 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6720   if (TL.getNumProtocols()) {
6721     TL.setProtocolLAngleLoc(ReadSourceLocation());
6722     TL.setProtocolRAngleLoc(ReadSourceLocation());
6723   }
6724   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6725     TL.setProtocolLoc(i, ReadSourceLocation());
6726 }
6727 
6728 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6729   TL.setHasBaseTypeAsWritten(Record[Idx++]);
6730   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
6731   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
6732   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6733     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6734   TL.setProtocolLAngleLoc(ReadSourceLocation());
6735   TL.setProtocolRAngleLoc(ReadSourceLocation());
6736   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6737     TL.setProtocolLoc(i, ReadSourceLocation());
6738 }
6739 
6740 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6741   TL.setStarLoc(ReadSourceLocation());
6742 }
6743 
6744 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6745   TL.setKWLoc(ReadSourceLocation());
6746   TL.setLParenLoc(ReadSourceLocation());
6747   TL.setRParenLoc(ReadSourceLocation());
6748 }
6749 
6750 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6751   TL.setKWLoc(ReadSourceLocation());
6752 }
6753 
6754 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
6755                             unsigned &Idx, TypeLoc TL) {
6756   TypeLocReader TLR(F, *this, Record, Idx);
6757   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6758     TLR.Visit(TL);
6759 }
6760 
6761 TypeSourceInfo *
6762 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
6763                              unsigned &Idx) {
6764   QualType InfoTy = readType(F, Record, Idx);
6765   if (InfoTy.isNull())
6766     return nullptr;
6767 
6768   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6769   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
6770   return TInfo;
6771 }
6772 
6773 QualType ASTReader::GetType(TypeID ID) {
6774   assert(ContextObj && "reading type with no AST context");
6775   ASTContext &Context = *ContextObj;
6776 
6777   unsigned FastQuals = ID & Qualifiers::FastMask;
6778   unsigned Index = ID >> Qualifiers::FastWidth;
6779 
6780   if (Index < NUM_PREDEF_TYPE_IDS) {
6781     QualType T;
6782     switch ((PredefinedTypeIDs)Index) {
6783     case PREDEF_TYPE_NULL_ID:
6784       return QualType();
6785     case PREDEF_TYPE_VOID_ID:
6786       T = Context.VoidTy;
6787       break;
6788     case PREDEF_TYPE_BOOL_ID:
6789       T = Context.BoolTy;
6790       break;
6791     case PREDEF_TYPE_CHAR_U_ID:
6792     case PREDEF_TYPE_CHAR_S_ID:
6793       // FIXME: Check that the signedness of CharTy is correct!
6794       T = Context.CharTy;
6795       break;
6796     case PREDEF_TYPE_UCHAR_ID:
6797       T = Context.UnsignedCharTy;
6798       break;
6799     case PREDEF_TYPE_USHORT_ID:
6800       T = Context.UnsignedShortTy;
6801       break;
6802     case PREDEF_TYPE_UINT_ID:
6803       T = Context.UnsignedIntTy;
6804       break;
6805     case PREDEF_TYPE_ULONG_ID:
6806       T = Context.UnsignedLongTy;
6807       break;
6808     case PREDEF_TYPE_ULONGLONG_ID:
6809       T = Context.UnsignedLongLongTy;
6810       break;
6811     case PREDEF_TYPE_UINT128_ID:
6812       T = Context.UnsignedInt128Ty;
6813       break;
6814     case PREDEF_TYPE_SCHAR_ID:
6815       T = Context.SignedCharTy;
6816       break;
6817     case PREDEF_TYPE_WCHAR_ID:
6818       T = Context.WCharTy;
6819       break;
6820     case PREDEF_TYPE_SHORT_ID:
6821       T = Context.ShortTy;
6822       break;
6823     case PREDEF_TYPE_INT_ID:
6824       T = Context.IntTy;
6825       break;
6826     case PREDEF_TYPE_LONG_ID:
6827       T = Context.LongTy;
6828       break;
6829     case PREDEF_TYPE_LONGLONG_ID:
6830       T = Context.LongLongTy;
6831       break;
6832     case PREDEF_TYPE_INT128_ID:
6833       T = Context.Int128Ty;
6834       break;
6835     case PREDEF_TYPE_HALF_ID:
6836       T = Context.HalfTy;
6837       break;
6838     case PREDEF_TYPE_FLOAT_ID:
6839       T = Context.FloatTy;
6840       break;
6841     case PREDEF_TYPE_DOUBLE_ID:
6842       T = Context.DoubleTy;
6843       break;
6844     case PREDEF_TYPE_LONGDOUBLE_ID:
6845       T = Context.LongDoubleTy;
6846       break;
6847     case PREDEF_TYPE_SHORT_ACCUM_ID:
6848       T = Context.ShortAccumTy;
6849       break;
6850     case PREDEF_TYPE_ACCUM_ID:
6851       T = Context.AccumTy;
6852       break;
6853     case PREDEF_TYPE_LONG_ACCUM_ID:
6854       T = Context.LongAccumTy;
6855       break;
6856     case PREDEF_TYPE_USHORT_ACCUM_ID:
6857       T = Context.UnsignedShortAccumTy;
6858       break;
6859     case PREDEF_TYPE_UACCUM_ID:
6860       T = Context.UnsignedAccumTy;
6861       break;
6862     case PREDEF_TYPE_ULONG_ACCUM_ID:
6863       T = Context.UnsignedLongAccumTy;
6864       break;
6865     case PREDEF_TYPE_SHORT_FRACT_ID:
6866       T = Context.ShortFractTy;
6867       break;
6868     case PREDEF_TYPE_FRACT_ID:
6869       T = Context.FractTy;
6870       break;
6871     case PREDEF_TYPE_LONG_FRACT_ID:
6872       T = Context.LongFractTy;
6873       break;
6874     case PREDEF_TYPE_USHORT_FRACT_ID:
6875       T = Context.UnsignedShortFractTy;
6876       break;
6877     case PREDEF_TYPE_UFRACT_ID:
6878       T = Context.UnsignedFractTy;
6879       break;
6880     case PREDEF_TYPE_ULONG_FRACT_ID:
6881       T = Context.UnsignedLongFractTy;
6882       break;
6883     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6884       T = Context.SatShortAccumTy;
6885       break;
6886     case PREDEF_TYPE_SAT_ACCUM_ID:
6887       T = Context.SatAccumTy;
6888       break;
6889     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6890       T = Context.SatLongAccumTy;
6891       break;
6892     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6893       T = Context.SatUnsignedShortAccumTy;
6894       break;
6895     case PREDEF_TYPE_SAT_UACCUM_ID:
6896       T = Context.SatUnsignedAccumTy;
6897       break;
6898     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6899       T = Context.SatUnsignedLongAccumTy;
6900       break;
6901     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6902       T = Context.SatShortFractTy;
6903       break;
6904     case PREDEF_TYPE_SAT_FRACT_ID:
6905       T = Context.SatFractTy;
6906       break;
6907     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6908       T = Context.SatLongFractTy;
6909       break;
6910     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6911       T = Context.SatUnsignedShortFractTy;
6912       break;
6913     case PREDEF_TYPE_SAT_UFRACT_ID:
6914       T = Context.SatUnsignedFractTy;
6915       break;
6916     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6917       T = Context.SatUnsignedLongFractTy;
6918       break;
6919     case PREDEF_TYPE_FLOAT16_ID:
6920       T = Context.Float16Ty;
6921       break;
6922     case PREDEF_TYPE_FLOAT128_ID:
6923       T = Context.Float128Ty;
6924       break;
6925     case PREDEF_TYPE_OVERLOAD_ID:
6926       T = Context.OverloadTy;
6927       break;
6928     case PREDEF_TYPE_BOUND_MEMBER:
6929       T = Context.BoundMemberTy;
6930       break;
6931     case PREDEF_TYPE_PSEUDO_OBJECT:
6932       T = Context.PseudoObjectTy;
6933       break;
6934     case PREDEF_TYPE_DEPENDENT_ID:
6935       T = Context.DependentTy;
6936       break;
6937     case PREDEF_TYPE_UNKNOWN_ANY:
6938       T = Context.UnknownAnyTy;
6939       break;
6940     case PREDEF_TYPE_NULLPTR_ID:
6941       T = Context.NullPtrTy;
6942       break;
6943     case PREDEF_TYPE_CHAR8_ID:
6944       T = Context.Char8Ty;
6945       break;
6946     case PREDEF_TYPE_CHAR16_ID:
6947       T = Context.Char16Ty;
6948       break;
6949     case PREDEF_TYPE_CHAR32_ID:
6950       T = Context.Char32Ty;
6951       break;
6952     case PREDEF_TYPE_OBJC_ID:
6953       T = Context.ObjCBuiltinIdTy;
6954       break;
6955     case PREDEF_TYPE_OBJC_CLASS:
6956       T = Context.ObjCBuiltinClassTy;
6957       break;
6958     case PREDEF_TYPE_OBJC_SEL:
6959       T = Context.ObjCBuiltinSelTy;
6960       break;
6961 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6962     case PREDEF_TYPE_##Id##_ID: \
6963       T = Context.SingletonId; \
6964       break;
6965 #include "clang/Basic/OpenCLImageTypes.def"
6966 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6967     case PREDEF_TYPE_##Id##_ID: \
6968       T = Context.Id##Ty; \
6969       break;
6970 #include "clang/Basic/OpenCLExtensionTypes.def"
6971     case PREDEF_TYPE_SAMPLER_ID:
6972       T = Context.OCLSamplerTy;
6973       break;
6974     case PREDEF_TYPE_EVENT_ID:
6975       T = Context.OCLEventTy;
6976       break;
6977     case PREDEF_TYPE_CLK_EVENT_ID:
6978       T = Context.OCLClkEventTy;
6979       break;
6980     case PREDEF_TYPE_QUEUE_ID:
6981       T = Context.OCLQueueTy;
6982       break;
6983     case PREDEF_TYPE_RESERVE_ID_ID:
6984       T = Context.OCLReserveIDTy;
6985       break;
6986     case PREDEF_TYPE_AUTO_DEDUCT:
6987       T = Context.getAutoDeductType();
6988       break;
6989     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6990       T = Context.getAutoRRefDeductType();
6991       break;
6992     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6993       T = Context.ARCUnbridgedCastTy;
6994       break;
6995     case PREDEF_TYPE_BUILTIN_FN:
6996       T = Context.BuiltinFnTy;
6997       break;
6998     case PREDEF_TYPE_OMP_ARRAY_SECTION:
6999       T = Context.OMPArraySectionTy;
7000       break;
7001     }
7002 
7003     assert(!T.isNull() && "Unknown predefined type");
7004     return T.withFastQualifiers(FastQuals);
7005   }
7006 
7007   Index -= NUM_PREDEF_TYPE_IDS;
7008   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7009   if (TypesLoaded[Index].isNull()) {
7010     TypesLoaded[Index] = readTypeRecord(Index);
7011     if (TypesLoaded[Index].isNull())
7012       return QualType();
7013 
7014     TypesLoaded[Index]->setFromAST();
7015     if (DeserializationListener)
7016       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7017                                         TypesLoaded[Index]);
7018   }
7019 
7020   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7021 }
7022 
7023 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7024   return GetType(getGlobalTypeID(F, LocalID));
7025 }
7026 
7027 serialization::TypeID
7028 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7029   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7030   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7031 
7032   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7033     return LocalID;
7034 
7035   if (!F.ModuleOffsetMap.empty())
7036     ReadModuleOffsetMap(F);
7037 
7038   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7039     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7040   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7041 
7042   unsigned GlobalIndex = LocalIndex + I->second;
7043   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7044 }
7045 
7046 TemplateArgumentLocInfo
7047 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7048                                       TemplateArgument::ArgKind Kind,
7049                                       const RecordData &Record,
7050                                       unsigned &Index) {
7051   switch (Kind) {
7052   case TemplateArgument::Expression:
7053     return ReadExpr(F);
7054   case TemplateArgument::Type:
7055     return GetTypeSourceInfo(F, Record, Index);
7056   case TemplateArgument::Template: {
7057     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7058                                                                      Index);
7059     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7060     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7061                                    SourceLocation());
7062   }
7063   case TemplateArgument::TemplateExpansion: {
7064     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7065                                                                      Index);
7066     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7067     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7068     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7069                                    EllipsisLoc);
7070   }
7071   case TemplateArgument::Null:
7072   case TemplateArgument::Integral:
7073   case TemplateArgument::Declaration:
7074   case TemplateArgument::NullPtr:
7075   case TemplateArgument::Pack:
7076     // FIXME: Is this right?
7077     return TemplateArgumentLocInfo();
7078   }
7079   llvm_unreachable("unexpected template argument loc");
7080 }
7081 
7082 TemplateArgumentLoc
7083 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7084                                    const RecordData &Record, unsigned &Index) {
7085   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7086 
7087   if (Arg.getKind() == TemplateArgument::Expression) {
7088     if (Record[Index++]) // bool InfoHasSameExpr.
7089       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7090   }
7091   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7092                                                              Record, Index));
7093 }
7094 
7095 const ASTTemplateArgumentListInfo*
7096 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7097                                            const RecordData &Record,
7098                                            unsigned &Index) {
7099   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7100   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7101   unsigned NumArgsAsWritten = Record[Index++];
7102   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7103   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7104     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7105   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7106 }
7107 
7108 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7109   return GetDecl(ID);
7110 }
7111 
7112 void ASTReader::CompleteRedeclChain(const Decl *D) {
7113   if (NumCurrentElementsDeserializing) {
7114     // We arrange to not care about the complete redeclaration chain while we're
7115     // deserializing. Just remember that the AST has marked this one as complete
7116     // but that it's not actually complete yet, so we know we still need to
7117     // complete it later.
7118     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7119     return;
7120   }
7121 
7122   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7123 
7124   // If this is a named declaration, complete it by looking it up
7125   // within its context.
7126   //
7127   // FIXME: Merging a function definition should merge
7128   // all mergeable entities within it.
7129   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7130       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7131     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7132       if (!getContext().getLangOpts().CPlusPlus &&
7133           isa<TranslationUnitDecl>(DC)) {
7134         // Outside of C++, we don't have a lookup table for the TU, so update
7135         // the identifier instead. (For C++ modules, we don't store decls
7136         // in the serialized identifier table, so we do the lookup in the TU.)
7137         auto *II = Name.getAsIdentifierInfo();
7138         assert(II && "non-identifier name in C?");
7139         if (II->isOutOfDate())
7140           updateOutOfDateIdentifier(*II);
7141       } else
7142         DC->lookup(Name);
7143     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7144       // Find all declarations of this kind from the relevant context.
7145       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7146         auto *DC = cast<DeclContext>(DCDecl);
7147         SmallVector<Decl*, 8> Decls;
7148         FindExternalLexicalDecls(
7149             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7150       }
7151     }
7152   }
7153 
7154   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7155     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7156   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7157     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7158   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7159     if (auto *Template = FD->getPrimaryTemplate())
7160       Template->LoadLazySpecializations();
7161   }
7162 }
7163 
7164 CXXCtorInitializer **
7165 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7166   RecordLocation Loc = getLocalBitOffset(Offset);
7167   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7168   SavedStreamPosition SavedPosition(Cursor);
7169   Cursor.JumpToBit(Loc.Offset);
7170   ReadingKindTracker ReadingKind(Read_Decl, *this);
7171 
7172   RecordData Record;
7173   unsigned Code = Cursor.ReadCode();
7174   unsigned RecCode = Cursor.readRecord(Code, Record);
7175   if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
7176     Error("malformed AST file: missing C++ ctor initializers");
7177     return nullptr;
7178   }
7179 
7180   unsigned Idx = 0;
7181   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7182 }
7183 
7184 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7185   assert(ContextObj && "reading base specifiers with no AST context");
7186   ASTContext &Context = *ContextObj;
7187 
7188   RecordLocation Loc = getLocalBitOffset(Offset);
7189   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7190   SavedStreamPosition SavedPosition(Cursor);
7191   Cursor.JumpToBit(Loc.Offset);
7192   ReadingKindTracker ReadingKind(Read_Decl, *this);
7193   RecordData Record;
7194   unsigned Code = Cursor.ReadCode();
7195   unsigned RecCode = Cursor.readRecord(Code, Record);
7196   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7197     Error("malformed AST file: missing C++ base specifiers");
7198     return nullptr;
7199   }
7200 
7201   unsigned Idx = 0;
7202   unsigned NumBases = Record[Idx++];
7203   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7204   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7205   for (unsigned I = 0; I != NumBases; ++I)
7206     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7207   return Bases;
7208 }
7209 
7210 serialization::DeclID
7211 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7212   if (LocalID < NUM_PREDEF_DECL_IDS)
7213     return LocalID;
7214 
7215   if (!F.ModuleOffsetMap.empty())
7216     ReadModuleOffsetMap(F);
7217 
7218   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7219     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7220   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7221 
7222   return LocalID + I->second;
7223 }
7224 
7225 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7226                                    ModuleFile &M) const {
7227   // Predefined decls aren't from any module.
7228   if (ID < NUM_PREDEF_DECL_IDS)
7229     return false;
7230 
7231   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7232          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7233 }
7234 
7235 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7236   if (!D->isFromASTFile())
7237     return nullptr;
7238   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7239   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7240   return I->second;
7241 }
7242 
7243 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7244   if (ID < NUM_PREDEF_DECL_IDS)
7245     return SourceLocation();
7246 
7247   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7248 
7249   if (Index > DeclsLoaded.size()) {
7250     Error("declaration ID out-of-range for AST file");
7251     return SourceLocation();
7252   }
7253 
7254   if (Decl *D = DeclsLoaded[Index])
7255     return D->getLocation();
7256 
7257   SourceLocation Loc;
7258   DeclCursorForID(ID, Loc);
7259   return Loc;
7260 }
7261 
7262 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7263   switch (ID) {
7264   case PREDEF_DECL_NULL_ID:
7265     return nullptr;
7266 
7267   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7268     return Context.getTranslationUnitDecl();
7269 
7270   case PREDEF_DECL_OBJC_ID_ID:
7271     return Context.getObjCIdDecl();
7272 
7273   case PREDEF_DECL_OBJC_SEL_ID:
7274     return Context.getObjCSelDecl();
7275 
7276   case PREDEF_DECL_OBJC_CLASS_ID:
7277     return Context.getObjCClassDecl();
7278 
7279   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7280     return Context.getObjCProtocolDecl();
7281 
7282   case PREDEF_DECL_INT_128_ID:
7283     return Context.getInt128Decl();
7284 
7285   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7286     return Context.getUInt128Decl();
7287 
7288   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7289     return Context.getObjCInstanceTypeDecl();
7290 
7291   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7292     return Context.getBuiltinVaListDecl();
7293 
7294   case PREDEF_DECL_VA_LIST_TAG:
7295     return Context.getVaListTagDecl();
7296 
7297   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7298     return Context.getBuiltinMSVaListDecl();
7299 
7300   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7301     return Context.getExternCContextDecl();
7302 
7303   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7304     return Context.getMakeIntegerSeqDecl();
7305 
7306   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7307     return Context.getCFConstantStringDecl();
7308 
7309   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7310     return Context.getCFConstantStringTagDecl();
7311 
7312   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7313     return Context.getTypePackElementDecl();
7314   }
7315   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7316 }
7317 
7318 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7319   assert(ContextObj && "reading decl with no AST context");
7320   if (ID < NUM_PREDEF_DECL_IDS) {
7321     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7322     if (D) {
7323       // Track that we have merged the declaration with ID \p ID into the
7324       // pre-existing predefined declaration \p D.
7325       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7326       if (Merged.empty())
7327         Merged.push_back(ID);
7328     }
7329     return D;
7330   }
7331 
7332   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7333 
7334   if (Index >= DeclsLoaded.size()) {
7335     assert(0 && "declaration ID out-of-range for AST file");
7336     Error("declaration ID out-of-range for AST file");
7337     return nullptr;
7338   }
7339 
7340   return DeclsLoaded[Index];
7341 }
7342 
7343 Decl *ASTReader::GetDecl(DeclID ID) {
7344   if (ID < NUM_PREDEF_DECL_IDS)
7345     return GetExistingDecl(ID);
7346 
7347   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7348 
7349   if (Index >= DeclsLoaded.size()) {
7350     assert(0 && "declaration ID out-of-range for AST file");
7351     Error("declaration ID out-of-range for AST file");
7352     return nullptr;
7353   }
7354 
7355   if (!DeclsLoaded[Index]) {
7356     ReadDeclRecord(ID);
7357     if (DeserializationListener)
7358       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7359   }
7360 
7361   return DeclsLoaded[Index];
7362 }
7363 
7364 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7365                                                   DeclID GlobalID) {
7366   if (GlobalID < NUM_PREDEF_DECL_IDS)
7367     return GlobalID;
7368 
7369   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7370   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7371   ModuleFile *Owner = I->second;
7372 
7373   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7374     = M.GlobalToLocalDeclIDs.find(Owner);
7375   if (Pos == M.GlobalToLocalDeclIDs.end())
7376     return 0;
7377 
7378   return GlobalID - Owner->BaseDeclID + Pos->second;
7379 }
7380 
7381 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7382                                             const RecordData &Record,
7383                                             unsigned &Idx) {
7384   if (Idx >= Record.size()) {
7385     Error("Corrupted AST file");
7386     return 0;
7387   }
7388 
7389   return getGlobalDeclID(F, Record[Idx++]);
7390 }
7391 
7392 /// Resolve the offset of a statement into a statement.
7393 ///
7394 /// This operation will read a new statement from the external
7395 /// source each time it is called, and is meant to be used via a
7396 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7397 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7398   // Switch case IDs are per Decl.
7399   ClearSwitchCaseIDs();
7400 
7401   // Offset here is a global offset across the entire chain.
7402   RecordLocation Loc = getLocalBitOffset(Offset);
7403   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
7404   assert(NumCurrentElementsDeserializing == 0 &&
7405          "should not be called while already deserializing");
7406   Deserializing D(this);
7407   return ReadStmtFromStream(*Loc.F);
7408 }
7409 
7410 void ASTReader::FindExternalLexicalDecls(
7411     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7412     SmallVectorImpl<Decl *> &Decls) {
7413   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7414 
7415   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7416     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7417     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7418       auto K = (Decl::Kind)+LexicalDecls[I];
7419       if (!IsKindWeWant(K))
7420         continue;
7421 
7422       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7423 
7424       // Don't add predefined declarations to the lexical context more
7425       // than once.
7426       if (ID < NUM_PREDEF_DECL_IDS) {
7427         if (PredefsVisited[ID])
7428           continue;
7429 
7430         PredefsVisited[ID] = true;
7431       }
7432 
7433       if (Decl *D = GetLocalDecl(*M, ID)) {
7434         assert(D->getKind() == K && "wrong kind for lexical decl");
7435         if (!DC->isDeclInLexicalTraversal(D))
7436           Decls.push_back(D);
7437       }
7438     }
7439   };
7440 
7441   if (isa<TranslationUnitDecl>(DC)) {
7442     for (auto Lexical : TULexicalDecls)
7443       Visit(Lexical.first, Lexical.second);
7444   } else {
7445     auto I = LexicalDecls.find(DC);
7446     if (I != LexicalDecls.end())
7447       Visit(I->second.first, I->second.second);
7448   }
7449 
7450   ++NumLexicalDeclContextsRead;
7451 }
7452 
7453 namespace {
7454 
7455 class DeclIDComp {
7456   ASTReader &Reader;
7457   ModuleFile &Mod;
7458 
7459 public:
7460   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7461 
7462   bool operator()(LocalDeclID L, LocalDeclID R) const {
7463     SourceLocation LHS = getLocation(L);
7464     SourceLocation RHS = getLocation(R);
7465     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7466   }
7467 
7468   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7469     SourceLocation RHS = getLocation(R);
7470     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7471   }
7472 
7473   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7474     SourceLocation LHS = getLocation(L);
7475     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7476   }
7477 
7478   SourceLocation getLocation(LocalDeclID ID) const {
7479     return Reader.getSourceManager().getFileLoc(
7480             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7481   }
7482 };
7483 
7484 } // namespace
7485 
7486 void ASTReader::FindFileRegionDecls(FileID File,
7487                                     unsigned Offset, unsigned Length,
7488                                     SmallVectorImpl<Decl *> &Decls) {
7489   SourceManager &SM = getSourceManager();
7490 
7491   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7492   if (I == FileDeclIDs.end())
7493     return;
7494 
7495   FileDeclsInfo &DInfo = I->second;
7496   if (DInfo.Decls.empty())
7497     return;
7498 
7499   SourceLocation
7500     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7501   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7502 
7503   DeclIDComp DIDComp(*this, *DInfo.Mod);
7504   ArrayRef<serialization::LocalDeclID>::iterator
7505     BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7506                                BeginLoc, DIDComp);
7507   if (BeginIt != DInfo.Decls.begin())
7508     --BeginIt;
7509 
7510   // If we are pointing at a top-level decl inside an objc container, we need
7511   // to backtrack until we find it otherwise we will fail to report that the
7512   // region overlaps with an objc container.
7513   while (BeginIt != DInfo.Decls.begin() &&
7514          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7515              ->isTopLevelDeclInObjCContainer())
7516     --BeginIt;
7517 
7518   ArrayRef<serialization::LocalDeclID>::iterator
7519     EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7520                              EndLoc, DIDComp);
7521   if (EndIt != DInfo.Decls.end())
7522     ++EndIt;
7523 
7524   for (ArrayRef<serialization::LocalDeclID>::iterator
7525          DIt = BeginIt; DIt != EndIt; ++DIt)
7526     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7527 }
7528 
7529 bool
7530 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7531                                           DeclarationName Name) {
7532   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7533          "DeclContext has no visible decls in storage");
7534   if (!Name)
7535     return false;
7536 
7537   auto It = Lookups.find(DC);
7538   if (It == Lookups.end())
7539     return false;
7540 
7541   Deserializing LookupResults(this);
7542 
7543   // Load the list of declarations.
7544   SmallVector<NamedDecl *, 64> Decls;
7545   for (DeclID ID : It->second.Table.find(Name)) {
7546     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7547     if (ND->getDeclName() == Name)
7548       Decls.push_back(ND);
7549   }
7550 
7551   ++NumVisibleDeclContextsRead;
7552   SetExternalVisibleDeclsForName(DC, Name, Decls);
7553   return !Decls.empty();
7554 }
7555 
7556 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7557   if (!DC->hasExternalVisibleStorage())
7558     return;
7559 
7560   auto It = Lookups.find(DC);
7561   assert(It != Lookups.end() &&
7562          "have external visible storage but no lookup tables");
7563 
7564   DeclsMap Decls;
7565 
7566   for (DeclID ID : It->second.Table.findAll()) {
7567     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7568     Decls[ND->getDeclName()].push_back(ND);
7569   }
7570 
7571   ++NumVisibleDeclContextsRead;
7572 
7573   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7574     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7575   }
7576   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7577 }
7578 
7579 const serialization::reader::DeclContextLookupTable *
7580 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7581   auto I = Lookups.find(Primary);
7582   return I == Lookups.end() ? nullptr : &I->second;
7583 }
7584 
7585 /// Under non-PCH compilation the consumer receives the objc methods
7586 /// before receiving the implementation, and codegen depends on this.
7587 /// We simulate this by deserializing and passing to consumer the methods of the
7588 /// implementation before passing the deserialized implementation decl.
7589 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7590                                        ASTConsumer *Consumer) {
7591   assert(ImplD && Consumer);
7592 
7593   for (auto *I : ImplD->methods())
7594     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7595 
7596   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7597 }
7598 
7599 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7600   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7601     PassObjCImplDeclToConsumer(ImplD, Consumer);
7602   else
7603     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7604 }
7605 
7606 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7607   this->Consumer = Consumer;
7608 
7609   if (Consumer)
7610     PassInterestingDeclsToConsumer();
7611 
7612   if (DeserializationListener)
7613     DeserializationListener->ReaderInitialized(this);
7614 }
7615 
7616 void ASTReader::PrintStats() {
7617   std::fprintf(stderr, "*** AST File Statistics:\n");
7618 
7619   unsigned NumTypesLoaded
7620     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7621                                       QualType());
7622   unsigned NumDeclsLoaded
7623     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7624                                       (Decl *)nullptr);
7625   unsigned NumIdentifiersLoaded
7626     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7627                                             IdentifiersLoaded.end(),
7628                                             (IdentifierInfo *)nullptr);
7629   unsigned NumMacrosLoaded
7630     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7631                                        MacrosLoaded.end(),
7632                                        (MacroInfo *)nullptr);
7633   unsigned NumSelectorsLoaded
7634     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7635                                           SelectorsLoaded.end(),
7636                                           Selector());
7637 
7638   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7639     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7640                  NumSLocEntriesRead, TotalNumSLocEntries,
7641                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7642   if (!TypesLoaded.empty())
7643     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7644                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7645                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7646   if (!DeclsLoaded.empty())
7647     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7648                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7649                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7650   if (!IdentifiersLoaded.empty())
7651     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7652                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7653                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7654   if (!MacrosLoaded.empty())
7655     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7656                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7657                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7658   if (!SelectorsLoaded.empty())
7659     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7660                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7661                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7662   if (TotalNumStatements)
7663     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7664                  NumStatementsRead, TotalNumStatements,
7665                  ((float)NumStatementsRead/TotalNumStatements * 100));
7666   if (TotalNumMacros)
7667     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7668                  NumMacrosRead, TotalNumMacros,
7669                  ((float)NumMacrosRead/TotalNumMacros * 100));
7670   if (TotalLexicalDeclContexts)
7671     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7672                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7673                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7674                   * 100));
7675   if (TotalVisibleDeclContexts)
7676     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7677                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7678                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7679                   * 100));
7680   if (TotalNumMethodPoolEntries)
7681     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7682                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7683                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7684                   * 100));
7685   if (NumMethodPoolLookups)
7686     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7687                  NumMethodPoolHits, NumMethodPoolLookups,
7688                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7689   if (NumMethodPoolTableLookups)
7690     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7691                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7692                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7693                   * 100.0));
7694   if (NumIdentifierLookupHits)
7695     std::fprintf(stderr,
7696                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7697                  NumIdentifierLookupHits, NumIdentifierLookups,
7698                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7699 
7700   if (GlobalIndex) {
7701     std::fprintf(stderr, "\n");
7702     GlobalIndex->printStats();
7703   }
7704 
7705   std::fprintf(stderr, "\n");
7706   dump();
7707   std::fprintf(stderr, "\n");
7708 }
7709 
7710 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7711 LLVM_DUMP_METHOD static void
7712 dumpModuleIDMap(StringRef Name,
7713                 const ContinuousRangeMap<Key, ModuleFile *,
7714                                          InitialCapacity> &Map) {
7715   if (Map.begin() == Map.end())
7716     return;
7717 
7718   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7719 
7720   llvm::errs() << Name << ":\n";
7721   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7722        I != IEnd; ++I) {
7723     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7724       << "\n";
7725   }
7726 }
7727 
7728 LLVM_DUMP_METHOD void ASTReader::dump() {
7729   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7730   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7731   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7732   dumpModuleIDMap("Global type map", GlobalTypeMap);
7733   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7734   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7735   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7736   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7737   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7738   dumpModuleIDMap("Global preprocessed entity map",
7739                   GlobalPreprocessedEntityMap);
7740 
7741   llvm::errs() << "\n*** PCH/Modules Loaded:";
7742   for (ModuleFile &M : ModuleMgr)
7743     M.dump();
7744 }
7745 
7746 /// Return the amount of memory used by memory buffers, breaking down
7747 /// by heap-backed versus mmap'ed memory.
7748 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7749   for (ModuleFile &I : ModuleMgr) {
7750     if (llvm::MemoryBuffer *buf = I.Buffer) {
7751       size_t bytes = buf->getBufferSize();
7752       switch (buf->getBufferKind()) {
7753         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7754           sizes.malloc_bytes += bytes;
7755           break;
7756         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7757           sizes.mmap_bytes += bytes;
7758           break;
7759       }
7760     }
7761   }
7762 }
7763 
7764 void ASTReader::InitializeSema(Sema &S) {
7765   SemaObj = &S;
7766   S.addExternalSource(this);
7767 
7768   // Makes sure any declarations that were deserialized "too early"
7769   // still get added to the identifier's declaration chains.
7770   for (uint64_t ID : PreloadedDeclIDs) {
7771     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7772     pushExternalDeclIntoScope(D, D->getDeclName());
7773   }
7774   PreloadedDeclIDs.clear();
7775 
7776   // FIXME: What happens if these are changed by a module import?
7777   if (!FPPragmaOptions.empty()) {
7778     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7779     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7780   }
7781 
7782   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7783   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7784   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7785 
7786   UpdateSema();
7787 }
7788 
7789 void ASTReader::UpdateSema() {
7790   assert(SemaObj && "no Sema to update");
7791 
7792   // Load the offsets of the declarations that Sema references.
7793   // They will be lazily deserialized when needed.
7794   if (!SemaDeclRefs.empty()) {
7795     assert(SemaDeclRefs.size() % 3 == 0);
7796     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7797       if (!SemaObj->StdNamespace)
7798         SemaObj->StdNamespace = SemaDeclRefs[I];
7799       if (!SemaObj->StdBadAlloc)
7800         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7801       if (!SemaObj->StdAlignValT)
7802         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7803     }
7804     SemaDeclRefs.clear();
7805   }
7806 
7807   // Update the state of pragmas. Use the same API as if we had encountered the
7808   // pragma in the source.
7809   if(OptimizeOffPragmaLocation.isValid())
7810     SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
7811   if (PragmaMSStructState != -1)
7812     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7813   if (PointersToMembersPragmaLocation.isValid()) {
7814     SemaObj->ActOnPragmaMSPointersToMembers(
7815         (LangOptions::PragmaMSPointersToMembersKind)
7816             PragmaMSPointersToMembersState,
7817         PointersToMembersPragmaLocation);
7818   }
7819   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7820 
7821   if (PragmaPackCurrentValue) {
7822     // The bottom of the stack might have a default value. It must be adjusted
7823     // to the current value to ensure that the packing state is preserved after
7824     // popping entries that were included/imported from a PCH/module.
7825     bool DropFirst = false;
7826     if (!PragmaPackStack.empty() &&
7827         PragmaPackStack.front().Location.isInvalid()) {
7828       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7829              "Expected a default alignment value");
7830       SemaObj->PackStack.Stack.emplace_back(
7831           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7832           SemaObj->PackStack.CurrentPragmaLocation,
7833           PragmaPackStack.front().PushLocation);
7834       DropFirst = true;
7835     }
7836     for (const auto &Entry :
7837          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7838       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7839                                             Entry.Location, Entry.PushLocation);
7840     if (PragmaPackCurrentLocation.isInvalid()) {
7841       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7842              "Expected a default alignment value");
7843       // Keep the current values.
7844     } else {
7845       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7846       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7847     }
7848   }
7849 }
7850 
7851 IdentifierInfo *ASTReader::get(StringRef Name) {
7852   // Note that we are loading an identifier.
7853   Deserializing AnIdentifier(this);
7854 
7855   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7856                                   NumIdentifierLookups,
7857                                   NumIdentifierLookupHits);
7858 
7859   // We don't need to do identifier table lookups in C++ modules (we preload
7860   // all interesting declarations, and don't need to use the scope for name
7861   // lookups). Perform the lookup in PCH files, though, since we don't build
7862   // a complete initial identifier table if we're carrying on from a PCH.
7863   if (PP.getLangOpts().CPlusPlus) {
7864     for (auto F : ModuleMgr.pch_modules())
7865       if (Visitor(*F))
7866         break;
7867   } else {
7868     // If there is a global index, look there first to determine which modules
7869     // provably do not have any results for this identifier.
7870     GlobalModuleIndex::HitSet Hits;
7871     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7872     if (!loadGlobalIndex()) {
7873       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7874         HitsPtr = &Hits;
7875       }
7876     }
7877 
7878     ModuleMgr.visit(Visitor, HitsPtr);
7879   }
7880 
7881   IdentifierInfo *II = Visitor.getIdentifierInfo();
7882   markIdentifierUpToDate(II);
7883   return II;
7884 }
7885 
7886 namespace clang {
7887 
7888   /// An identifier-lookup iterator that enumerates all of the
7889   /// identifiers stored within a set of AST files.
7890   class ASTIdentifierIterator : public IdentifierIterator {
7891     /// The AST reader whose identifiers are being enumerated.
7892     const ASTReader &Reader;
7893 
7894     /// The current index into the chain of AST files stored in
7895     /// the AST reader.
7896     unsigned Index;
7897 
7898     /// The current position within the identifier lookup table
7899     /// of the current AST file.
7900     ASTIdentifierLookupTable::key_iterator Current;
7901 
7902     /// The end position within the identifier lookup table of
7903     /// the current AST file.
7904     ASTIdentifierLookupTable::key_iterator End;
7905 
7906     /// Whether to skip any modules in the ASTReader.
7907     bool SkipModules;
7908 
7909   public:
7910     explicit ASTIdentifierIterator(const ASTReader &Reader,
7911                                    bool SkipModules = false);
7912 
7913     StringRef Next() override;
7914   };
7915 
7916 } // namespace clang
7917 
7918 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7919                                              bool SkipModules)
7920     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7921 }
7922 
7923 StringRef ASTIdentifierIterator::Next() {
7924   while (Current == End) {
7925     // If we have exhausted all of our AST files, we're done.
7926     if (Index == 0)
7927       return StringRef();
7928 
7929     --Index;
7930     ModuleFile &F = Reader.ModuleMgr[Index];
7931     if (SkipModules && F.isModule())
7932       continue;
7933 
7934     ASTIdentifierLookupTable *IdTable =
7935         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7936     Current = IdTable->key_begin();
7937     End = IdTable->key_end();
7938   }
7939 
7940   // We have any identifiers remaining in the current AST file; return
7941   // the next one.
7942   StringRef Result = *Current;
7943   ++Current;
7944   return Result;
7945 }
7946 
7947 namespace {
7948 
7949 /// A utility for appending two IdentifierIterators.
7950 class ChainedIdentifierIterator : public IdentifierIterator {
7951   std::unique_ptr<IdentifierIterator> Current;
7952   std::unique_ptr<IdentifierIterator> Queued;
7953 
7954 public:
7955   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7956                             std::unique_ptr<IdentifierIterator> Second)
7957       : Current(std::move(First)), Queued(std::move(Second)) {}
7958 
7959   StringRef Next() override {
7960     if (!Current)
7961       return StringRef();
7962 
7963     StringRef result = Current->Next();
7964     if (!result.empty())
7965       return result;
7966 
7967     // Try the queued iterator, which may itself be empty.
7968     Current.reset();
7969     std::swap(Current, Queued);
7970     return Next();
7971   }
7972 };
7973 
7974 } // namespace
7975 
7976 IdentifierIterator *ASTReader::getIdentifiers() {
7977   if (!loadGlobalIndex()) {
7978     std::unique_ptr<IdentifierIterator> ReaderIter(
7979         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7980     std::unique_ptr<IdentifierIterator> ModulesIter(
7981         GlobalIndex->createIdentifierIterator());
7982     return new ChainedIdentifierIterator(std::move(ReaderIter),
7983                                          std::move(ModulesIter));
7984   }
7985 
7986   return new ASTIdentifierIterator(*this);
7987 }
7988 
7989 namespace clang {
7990 namespace serialization {
7991 
7992   class ReadMethodPoolVisitor {
7993     ASTReader &Reader;
7994     Selector Sel;
7995     unsigned PriorGeneration;
7996     unsigned InstanceBits = 0;
7997     unsigned FactoryBits = 0;
7998     bool InstanceHasMoreThanOneDecl = false;
7999     bool FactoryHasMoreThanOneDecl = false;
8000     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8001     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8002 
8003   public:
8004     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8005                           unsigned PriorGeneration)
8006         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8007 
8008     bool operator()(ModuleFile &M) {
8009       if (!M.SelectorLookupTable)
8010         return false;
8011 
8012       // If we've already searched this module file, skip it now.
8013       if (M.Generation <= PriorGeneration)
8014         return true;
8015 
8016       ++Reader.NumMethodPoolTableLookups;
8017       ASTSelectorLookupTable *PoolTable
8018         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8019       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8020       if (Pos == PoolTable->end())
8021         return false;
8022 
8023       ++Reader.NumMethodPoolTableHits;
8024       ++Reader.NumSelectorsRead;
8025       // FIXME: Not quite happy with the statistics here. We probably should
8026       // disable this tracking when called via LoadSelector.
8027       // Also, should entries without methods count as misses?
8028       ++Reader.NumMethodPoolEntriesRead;
8029       ASTSelectorLookupTrait::data_type Data = *Pos;
8030       if (Reader.DeserializationListener)
8031         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8032 
8033       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8034       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8035       InstanceBits = Data.InstanceBits;
8036       FactoryBits = Data.FactoryBits;
8037       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8038       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8039       return true;
8040     }
8041 
8042     /// Retrieve the instance methods found by this visitor.
8043     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8044       return InstanceMethods;
8045     }
8046 
8047     /// Retrieve the instance methods found by this visitor.
8048     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8049       return FactoryMethods;
8050     }
8051 
8052     unsigned getInstanceBits() const { return InstanceBits; }
8053     unsigned getFactoryBits() const { return FactoryBits; }
8054 
8055     bool instanceHasMoreThanOneDecl() const {
8056       return InstanceHasMoreThanOneDecl;
8057     }
8058 
8059     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8060   };
8061 
8062 } // namespace serialization
8063 } // namespace clang
8064 
8065 /// Add the given set of methods to the method list.
8066 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8067                              ObjCMethodList &List) {
8068   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8069     S.addMethodToGlobalList(&List, Methods[I]);
8070   }
8071 }
8072 
8073 void ASTReader::ReadMethodPool(Selector Sel) {
8074   // Get the selector generation and update it to the current generation.
8075   unsigned &Generation = SelectorGeneration[Sel];
8076   unsigned PriorGeneration = Generation;
8077   Generation = getGeneration();
8078   SelectorOutOfDate[Sel] = false;
8079 
8080   // Search for methods defined with this selector.
8081   ++NumMethodPoolLookups;
8082   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8083   ModuleMgr.visit(Visitor);
8084 
8085   if (Visitor.getInstanceMethods().empty() &&
8086       Visitor.getFactoryMethods().empty())
8087     return;
8088 
8089   ++NumMethodPoolHits;
8090 
8091   if (!getSema())
8092     return;
8093 
8094   Sema &S = *getSema();
8095   Sema::GlobalMethodPool::iterator Pos
8096     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8097 
8098   Pos->second.first.setBits(Visitor.getInstanceBits());
8099   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8100   Pos->second.second.setBits(Visitor.getFactoryBits());
8101   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8102 
8103   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8104   // when building a module we keep every method individually and may need to
8105   // update hasMoreThanOneDecl as we add the methods.
8106   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8107   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8108 }
8109 
8110 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8111   if (SelectorOutOfDate[Sel])
8112     ReadMethodPool(Sel);
8113 }
8114 
8115 void ASTReader::ReadKnownNamespaces(
8116                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8117   Namespaces.clear();
8118 
8119   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8120     if (NamespaceDecl *Namespace
8121                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8122       Namespaces.push_back(Namespace);
8123   }
8124 }
8125 
8126 void ASTReader::ReadUndefinedButUsed(
8127     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8128   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8129     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8130     SourceLocation Loc =
8131         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8132     Undefined.insert(std::make_pair(D, Loc));
8133   }
8134 }
8135 
8136 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8137     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8138                                                      Exprs) {
8139   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8140     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8141     uint64_t Count = DelayedDeleteExprs[Idx++];
8142     for (uint64_t C = 0; C < Count; ++C) {
8143       SourceLocation DeleteLoc =
8144           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8145       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8146       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8147     }
8148   }
8149 }
8150 
8151 void ASTReader::ReadTentativeDefinitions(
8152                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8153   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8154     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8155     if (Var)
8156       TentativeDefs.push_back(Var);
8157   }
8158   TentativeDefinitions.clear();
8159 }
8160 
8161 void ASTReader::ReadUnusedFileScopedDecls(
8162                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8163   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8164     DeclaratorDecl *D
8165       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8166     if (D)
8167       Decls.push_back(D);
8168   }
8169   UnusedFileScopedDecls.clear();
8170 }
8171 
8172 void ASTReader::ReadDelegatingConstructors(
8173                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8174   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8175     CXXConstructorDecl *D
8176       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8177     if (D)
8178       Decls.push_back(D);
8179   }
8180   DelegatingCtorDecls.clear();
8181 }
8182 
8183 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8184   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8185     TypedefNameDecl *D
8186       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8187     if (D)
8188       Decls.push_back(D);
8189   }
8190   ExtVectorDecls.clear();
8191 }
8192 
8193 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8194     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8195   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8196        ++I) {
8197     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8198         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8199     if (D)
8200       Decls.insert(D);
8201   }
8202   UnusedLocalTypedefNameCandidates.clear();
8203 }
8204 
8205 void ASTReader::ReadReferencedSelectors(
8206        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8207   if (ReferencedSelectorsData.empty())
8208     return;
8209 
8210   // If there are @selector references added them to its pool. This is for
8211   // implementation of -Wselector.
8212   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8213   unsigned I = 0;
8214   while (I < DataSize) {
8215     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8216     SourceLocation SelLoc
8217       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8218     Sels.push_back(std::make_pair(Sel, SelLoc));
8219   }
8220   ReferencedSelectorsData.clear();
8221 }
8222 
8223 void ASTReader::ReadWeakUndeclaredIdentifiers(
8224        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8225   if (WeakUndeclaredIdentifiers.empty())
8226     return;
8227 
8228   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8229     IdentifierInfo *WeakId
8230       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8231     IdentifierInfo *AliasId
8232       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8233     SourceLocation Loc
8234       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8235     bool Used = WeakUndeclaredIdentifiers[I++];
8236     WeakInfo WI(AliasId, Loc);
8237     WI.setUsed(Used);
8238     WeakIDs.push_back(std::make_pair(WeakId, WI));
8239   }
8240   WeakUndeclaredIdentifiers.clear();
8241 }
8242 
8243 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8244   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8245     ExternalVTableUse VT;
8246     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8247     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8248     VT.DefinitionRequired = VTableUses[Idx++];
8249     VTables.push_back(VT);
8250   }
8251 
8252   VTableUses.clear();
8253 }
8254 
8255 void ASTReader::ReadPendingInstantiations(
8256        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8257   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8258     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8259     SourceLocation Loc
8260       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8261 
8262     Pending.push_back(std::make_pair(D, Loc));
8263   }
8264   PendingInstantiations.clear();
8265 }
8266 
8267 void ASTReader::ReadLateParsedTemplates(
8268     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8269         &LPTMap) {
8270   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8271        /* In loop */) {
8272     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8273 
8274     auto LT = llvm::make_unique<LateParsedTemplate>();
8275     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8276 
8277     ModuleFile *F = getOwningModuleFile(LT->D);
8278     assert(F && "No module");
8279 
8280     unsigned TokN = LateParsedTemplates[Idx++];
8281     LT->Toks.reserve(TokN);
8282     for (unsigned T = 0; T < TokN; ++T)
8283       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8284 
8285     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8286   }
8287 
8288   LateParsedTemplates.clear();
8289 }
8290 
8291 void ASTReader::LoadSelector(Selector Sel) {
8292   // It would be complicated to avoid reading the methods anyway. So don't.
8293   ReadMethodPool(Sel);
8294 }
8295 
8296 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8297   assert(ID && "Non-zero identifier ID required");
8298   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8299   IdentifiersLoaded[ID - 1] = II;
8300   if (DeserializationListener)
8301     DeserializationListener->IdentifierRead(ID, II);
8302 }
8303 
8304 /// Set the globally-visible declarations associated with the given
8305 /// identifier.
8306 ///
8307 /// If the AST reader is currently in a state where the given declaration IDs
8308 /// cannot safely be resolved, they are queued until it is safe to resolve
8309 /// them.
8310 ///
8311 /// \param II an IdentifierInfo that refers to one or more globally-visible
8312 /// declarations.
8313 ///
8314 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8315 /// visible at global scope.
8316 ///
8317 /// \param Decls if non-null, this vector will be populated with the set of
8318 /// deserialized declarations. These declarations will not be pushed into
8319 /// scope.
8320 void
8321 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8322                               const SmallVectorImpl<uint32_t> &DeclIDs,
8323                                    SmallVectorImpl<Decl *> *Decls) {
8324   if (NumCurrentElementsDeserializing && !Decls) {
8325     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8326     return;
8327   }
8328 
8329   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8330     if (!SemaObj) {
8331       // Queue this declaration so that it will be added to the
8332       // translation unit scope and identifier's declaration chain
8333       // once a Sema object is known.
8334       PreloadedDeclIDs.push_back(DeclIDs[I]);
8335       continue;
8336     }
8337 
8338     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8339 
8340     // If we're simply supposed to record the declarations, do so now.
8341     if (Decls) {
8342       Decls->push_back(D);
8343       continue;
8344     }
8345 
8346     // Introduce this declaration into the translation-unit scope
8347     // and add it to the declaration chain for this identifier, so
8348     // that (unqualified) name lookup will find it.
8349     pushExternalDeclIntoScope(D, II);
8350   }
8351 }
8352 
8353 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8354   if (ID == 0)
8355     return nullptr;
8356 
8357   if (IdentifiersLoaded.empty()) {
8358     Error("no identifier table in AST file");
8359     return nullptr;
8360   }
8361 
8362   ID -= 1;
8363   if (!IdentifiersLoaded[ID]) {
8364     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8365     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8366     ModuleFile *M = I->second;
8367     unsigned Index = ID - M->BaseIdentifierID;
8368     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8369 
8370     // All of the strings in the AST file are preceded by a 16-bit length.
8371     // Extract that 16-bit length to avoid having to execute strlen().
8372     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8373     //  unsigned integers.  This is important to avoid integer overflow when
8374     //  we cast them to 'unsigned'.
8375     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8376     unsigned StrLen = (((unsigned) StrLenPtr[0])
8377                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8378     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8379     IdentifiersLoaded[ID] = &II;
8380     markIdentifierFromAST(*this,  II);
8381     if (DeserializationListener)
8382       DeserializationListener->IdentifierRead(ID + 1, &II);
8383   }
8384 
8385   return IdentifiersLoaded[ID];
8386 }
8387 
8388 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8389   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8390 }
8391 
8392 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8393   if (LocalID < NUM_PREDEF_IDENT_IDS)
8394     return LocalID;
8395 
8396   if (!M.ModuleOffsetMap.empty())
8397     ReadModuleOffsetMap(M);
8398 
8399   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8400     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8401   assert(I != M.IdentifierRemap.end()
8402          && "Invalid index into identifier index remap");
8403 
8404   return LocalID + I->second;
8405 }
8406 
8407 MacroInfo *ASTReader::getMacro(MacroID ID) {
8408   if (ID == 0)
8409     return nullptr;
8410 
8411   if (MacrosLoaded.empty()) {
8412     Error("no macro table in AST file");
8413     return nullptr;
8414   }
8415 
8416   ID -= NUM_PREDEF_MACRO_IDS;
8417   if (!MacrosLoaded[ID]) {
8418     GlobalMacroMapType::iterator I
8419       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8420     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8421     ModuleFile *M = I->second;
8422     unsigned Index = ID - M->BaseMacroID;
8423     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8424 
8425     if (DeserializationListener)
8426       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8427                                          MacrosLoaded[ID]);
8428   }
8429 
8430   return MacrosLoaded[ID];
8431 }
8432 
8433 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8434   if (LocalID < NUM_PREDEF_MACRO_IDS)
8435     return LocalID;
8436 
8437   if (!M.ModuleOffsetMap.empty())
8438     ReadModuleOffsetMap(M);
8439 
8440   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8441     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8442   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8443 
8444   return LocalID + I->second;
8445 }
8446 
8447 serialization::SubmoduleID
8448 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8449   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8450     return LocalID;
8451 
8452   if (!M.ModuleOffsetMap.empty())
8453     ReadModuleOffsetMap(M);
8454 
8455   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8456     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8457   assert(I != M.SubmoduleRemap.end()
8458          && "Invalid index into submodule index remap");
8459 
8460   return LocalID + I->second;
8461 }
8462 
8463 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8464   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8465     assert(GlobalID == 0 && "Unhandled global submodule ID");
8466     return nullptr;
8467   }
8468 
8469   if (GlobalID > SubmodulesLoaded.size()) {
8470     Error("submodule ID out of range in AST file");
8471     return nullptr;
8472   }
8473 
8474   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8475 }
8476 
8477 Module *ASTReader::getModule(unsigned ID) {
8478   return getSubmodule(ID);
8479 }
8480 
8481 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8482   ModuleFile *MF = getOwningModuleFile(D);
8483   return MF && MF->PCHHasObjectFile;
8484 }
8485 
8486 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8487   if (ID & 1) {
8488     // It's a module, look it up by submodule ID.
8489     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8490     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8491   } else {
8492     // It's a prefix (preamble, PCH, ...). Look it up by index.
8493     unsigned IndexFromEnd = ID >> 1;
8494     assert(IndexFromEnd && "got reference to unknown module file");
8495     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8496   }
8497 }
8498 
8499 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8500   if (!F)
8501     return 1;
8502 
8503   // For a file representing a module, use the submodule ID of the top-level
8504   // module as the file ID. For any other kind of file, the number of such
8505   // files loaded beforehand will be the same on reload.
8506   // FIXME: Is this true even if we have an explicit module file and a PCH?
8507   if (F->isModule())
8508     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8509 
8510   auto PCHModules = getModuleManager().pch_modules();
8511   auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
8512   assert(I != PCHModules.end() && "emitting reference to unknown file");
8513   return (I - PCHModules.end()) << 1;
8514 }
8515 
8516 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8517 ASTReader::getSourceDescriptor(unsigned ID) {
8518   if (const Module *M = getSubmodule(ID))
8519     return ExternalASTSource::ASTSourceDescriptor(*M);
8520 
8521   // If there is only a single PCH, return it instead.
8522   // Chained PCH are not supported.
8523   const auto &PCHChain = ModuleMgr.pch_modules();
8524   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8525     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8526     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8527     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8528     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8529                                           MF.Signature);
8530   }
8531   return None;
8532 }
8533 
8534 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8535   auto I = DefinitionSource.find(FD);
8536   if (I == DefinitionSource.end())
8537     return EK_ReplyHazy;
8538   return I->second ? EK_Never : EK_Always;
8539 }
8540 
8541 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8542   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8543 }
8544 
8545 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8546   if (ID == 0)
8547     return Selector();
8548 
8549   if (ID > SelectorsLoaded.size()) {
8550     Error("selector ID out of range in AST file");
8551     return Selector();
8552   }
8553 
8554   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8555     // Load this selector from the selector table.
8556     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8557     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8558     ModuleFile &M = *I->second;
8559     ASTSelectorLookupTrait Trait(*this, M);
8560     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8561     SelectorsLoaded[ID - 1] =
8562       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8563     if (DeserializationListener)
8564       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8565   }
8566 
8567   return SelectorsLoaded[ID - 1];
8568 }
8569 
8570 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8571   return DecodeSelector(ID);
8572 }
8573 
8574 uint32_t ASTReader::GetNumExternalSelectors() {
8575   // ID 0 (the null selector) is considered an external selector.
8576   return getTotalNumSelectors() + 1;
8577 }
8578 
8579 serialization::SelectorID
8580 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8581   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8582     return LocalID;
8583 
8584   if (!M.ModuleOffsetMap.empty())
8585     ReadModuleOffsetMap(M);
8586 
8587   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8588     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8589   assert(I != M.SelectorRemap.end()
8590          && "Invalid index into selector index remap");
8591 
8592   return LocalID + I->second;
8593 }
8594 
8595 DeclarationName
8596 ASTReader::ReadDeclarationName(ModuleFile &F,
8597                                const RecordData &Record, unsigned &Idx) {
8598   ASTContext &Context = getContext();
8599   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
8600   switch (Kind) {
8601   case DeclarationName::Identifier:
8602     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
8603 
8604   case DeclarationName::ObjCZeroArgSelector:
8605   case DeclarationName::ObjCOneArgSelector:
8606   case DeclarationName::ObjCMultiArgSelector:
8607     return DeclarationName(ReadSelector(F, Record, Idx));
8608 
8609   case DeclarationName::CXXConstructorName:
8610     return Context.DeclarationNames.getCXXConstructorName(
8611                           Context.getCanonicalType(readType(F, Record, Idx)));
8612 
8613   case DeclarationName::CXXDestructorName:
8614     return Context.DeclarationNames.getCXXDestructorName(
8615                           Context.getCanonicalType(readType(F, Record, Idx)));
8616 
8617   case DeclarationName::CXXDeductionGuideName:
8618     return Context.DeclarationNames.getCXXDeductionGuideName(
8619                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
8620 
8621   case DeclarationName::CXXConversionFunctionName:
8622     return Context.DeclarationNames.getCXXConversionFunctionName(
8623                           Context.getCanonicalType(readType(F, Record, Idx)));
8624 
8625   case DeclarationName::CXXOperatorName:
8626     return Context.DeclarationNames.getCXXOperatorName(
8627                                        (OverloadedOperatorKind)Record[Idx++]);
8628 
8629   case DeclarationName::CXXLiteralOperatorName:
8630     return Context.DeclarationNames.getCXXLiteralOperatorName(
8631                                        GetIdentifierInfo(F, Record, Idx));
8632 
8633   case DeclarationName::CXXUsingDirective:
8634     return DeclarationName::getUsingDirectiveName();
8635   }
8636 
8637   llvm_unreachable("Invalid NameKind!");
8638 }
8639 
8640 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
8641                                        DeclarationNameLoc &DNLoc,
8642                                        DeclarationName Name,
8643                                       const RecordData &Record, unsigned &Idx) {
8644   switch (Name.getNameKind()) {
8645   case DeclarationName::CXXConstructorName:
8646   case DeclarationName::CXXDestructorName:
8647   case DeclarationName::CXXConversionFunctionName:
8648     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
8649     break;
8650 
8651   case DeclarationName::CXXOperatorName:
8652     DNLoc.CXXOperatorName.BeginOpNameLoc
8653         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8654     DNLoc.CXXOperatorName.EndOpNameLoc
8655         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8656     break;
8657 
8658   case DeclarationName::CXXLiteralOperatorName:
8659     DNLoc.CXXLiteralOperatorName.OpNameLoc
8660         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8661     break;
8662 
8663   case DeclarationName::Identifier:
8664   case DeclarationName::ObjCZeroArgSelector:
8665   case DeclarationName::ObjCOneArgSelector:
8666   case DeclarationName::ObjCMultiArgSelector:
8667   case DeclarationName::CXXUsingDirective:
8668   case DeclarationName::CXXDeductionGuideName:
8669     break;
8670   }
8671 }
8672 
8673 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
8674                                         DeclarationNameInfo &NameInfo,
8675                                       const RecordData &Record, unsigned &Idx) {
8676   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
8677   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
8678   DeclarationNameLoc DNLoc;
8679   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
8680   NameInfo.setInfo(DNLoc);
8681 }
8682 
8683 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
8684                                   const RecordData &Record, unsigned &Idx) {
8685   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
8686   unsigned NumTPLists = Record[Idx++];
8687   Info.NumTemplParamLists = NumTPLists;
8688   if (NumTPLists) {
8689     Info.TemplParamLists =
8690         new (getContext()) TemplateParameterList *[NumTPLists];
8691     for (unsigned i = 0; i != NumTPLists; ++i)
8692       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
8693   }
8694 }
8695 
8696 TemplateName
8697 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
8698                             unsigned &Idx) {
8699   ASTContext &Context = getContext();
8700   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
8701   switch (Kind) {
8702   case TemplateName::Template:
8703       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
8704 
8705   case TemplateName::OverloadedTemplate: {
8706     unsigned size = Record[Idx++];
8707     UnresolvedSet<8> Decls;
8708     while (size--)
8709       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
8710 
8711     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
8712   }
8713 
8714   case TemplateName::QualifiedTemplate: {
8715     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8716     bool hasTemplKeyword = Record[Idx++];
8717     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
8718     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
8719   }
8720 
8721   case TemplateName::DependentTemplate: {
8722     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8723     if (Record[Idx++])  // isIdentifier
8724       return Context.getDependentTemplateName(NNS,
8725                                                GetIdentifierInfo(F, Record,
8726                                                                  Idx));
8727     return Context.getDependentTemplateName(NNS,
8728                                          (OverloadedOperatorKind)Record[Idx++]);
8729   }
8730 
8731   case TemplateName::SubstTemplateTemplateParm: {
8732     TemplateTemplateParmDecl *param
8733       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8734     if (!param) return TemplateName();
8735     TemplateName replacement = ReadTemplateName(F, Record, Idx);
8736     return Context.getSubstTemplateTemplateParm(param, replacement);
8737   }
8738 
8739   case TemplateName::SubstTemplateTemplateParmPack: {
8740     TemplateTemplateParmDecl *Param
8741       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8742     if (!Param)
8743       return TemplateName();
8744 
8745     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
8746     if (ArgPack.getKind() != TemplateArgument::Pack)
8747       return TemplateName();
8748 
8749     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
8750   }
8751   }
8752 
8753   llvm_unreachable("Unhandled template name kind!");
8754 }
8755 
8756 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
8757                                                  const RecordData &Record,
8758                                                  unsigned &Idx,
8759                                                  bool Canonicalize) {
8760   ASTContext &Context = getContext();
8761   if (Canonicalize) {
8762     // The caller wants a canonical template argument. Sometimes the AST only
8763     // wants template arguments in canonical form (particularly as the template
8764     // argument lists of template specializations) so ensure we preserve that
8765     // canonical form across serialization.
8766     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
8767     return Context.getCanonicalTemplateArgument(Arg);
8768   }
8769 
8770   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
8771   switch (Kind) {
8772   case TemplateArgument::Null:
8773     return TemplateArgument();
8774   case TemplateArgument::Type:
8775     return TemplateArgument(readType(F, Record, Idx));
8776   case TemplateArgument::Declaration: {
8777     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
8778     return TemplateArgument(D, readType(F, Record, Idx));
8779   }
8780   case TemplateArgument::NullPtr:
8781     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
8782   case TemplateArgument::Integral: {
8783     llvm::APSInt Value = ReadAPSInt(Record, Idx);
8784     QualType T = readType(F, Record, Idx);
8785     return TemplateArgument(Context, Value, T);
8786   }
8787   case TemplateArgument::Template:
8788     return TemplateArgument(ReadTemplateName(F, Record, Idx));
8789   case TemplateArgument::TemplateExpansion: {
8790     TemplateName Name = ReadTemplateName(F, Record, Idx);
8791     Optional<unsigned> NumTemplateExpansions;
8792     if (unsigned NumExpansions = Record[Idx++])
8793       NumTemplateExpansions = NumExpansions - 1;
8794     return TemplateArgument(Name, NumTemplateExpansions);
8795   }
8796   case TemplateArgument::Expression:
8797     return TemplateArgument(ReadExpr(F));
8798   case TemplateArgument::Pack: {
8799     unsigned NumArgs = Record[Idx++];
8800     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
8801     for (unsigned I = 0; I != NumArgs; ++I)
8802       Args[I] = ReadTemplateArgument(F, Record, Idx);
8803     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
8804   }
8805   }
8806 
8807   llvm_unreachable("Unhandled template argument kind!");
8808 }
8809 
8810 TemplateParameterList *
8811 ASTReader::ReadTemplateParameterList(ModuleFile &F,
8812                                      const RecordData &Record, unsigned &Idx) {
8813   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
8814   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
8815   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
8816 
8817   unsigned NumParams = Record[Idx++];
8818   SmallVector<NamedDecl *, 16> Params;
8819   Params.reserve(NumParams);
8820   while (NumParams--)
8821     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
8822 
8823   // TODO: Concepts
8824   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8825       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
8826   return TemplateParams;
8827 }
8828 
8829 void
8830 ASTReader::
8831 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
8832                          ModuleFile &F, const RecordData &Record,
8833                          unsigned &Idx, bool Canonicalize) {
8834   unsigned NumTemplateArgs = Record[Idx++];
8835   TemplArgs.reserve(NumTemplateArgs);
8836   while (NumTemplateArgs--)
8837     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
8838 }
8839 
8840 /// Read a UnresolvedSet structure.
8841 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
8842                                   const RecordData &Record, unsigned &Idx) {
8843   unsigned NumDecls = Record[Idx++];
8844   Set.reserve(getContext(), NumDecls);
8845   while (NumDecls--) {
8846     DeclID ID = ReadDeclID(F, Record, Idx);
8847     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
8848     Set.addLazyDecl(getContext(), ID, AS);
8849   }
8850 }
8851 
8852 CXXBaseSpecifier
8853 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
8854                                 const RecordData &Record, unsigned &Idx) {
8855   bool isVirtual = static_cast<bool>(Record[Idx++]);
8856   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
8857   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
8858   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
8859   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
8860   SourceRange Range = ReadSourceRange(F, Record, Idx);
8861   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
8862   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8863                           EllipsisLoc);
8864   Result.setInheritConstructors(inheritConstructors);
8865   return Result;
8866 }
8867 
8868 CXXCtorInitializer **
8869 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
8870                                    unsigned &Idx) {
8871   ASTContext &Context = getContext();
8872   unsigned NumInitializers = Record[Idx++];
8873   assert(NumInitializers && "wrote ctor initializers but have no inits");
8874   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8875   for (unsigned i = 0; i != NumInitializers; ++i) {
8876     TypeSourceInfo *TInfo = nullptr;
8877     bool IsBaseVirtual = false;
8878     FieldDecl *Member = nullptr;
8879     IndirectFieldDecl *IndirectMember = nullptr;
8880 
8881     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
8882     switch (Type) {
8883     case CTOR_INITIALIZER_BASE:
8884       TInfo = GetTypeSourceInfo(F, Record, Idx);
8885       IsBaseVirtual = Record[Idx++];
8886       break;
8887 
8888     case CTOR_INITIALIZER_DELEGATING:
8889       TInfo = GetTypeSourceInfo(F, Record, Idx);
8890       break;
8891 
8892      case CTOR_INITIALIZER_MEMBER:
8893       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
8894       break;
8895 
8896      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8897       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
8898       break;
8899     }
8900 
8901     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
8902     Expr *Init = ReadExpr(F);
8903     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
8904     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
8905 
8906     CXXCtorInitializer *BOMInit;
8907     if (Type == CTOR_INITIALIZER_BASE)
8908       BOMInit = new (Context)
8909           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8910                              RParenLoc, MemberOrEllipsisLoc);
8911     else if (Type == CTOR_INITIALIZER_DELEGATING)
8912       BOMInit = new (Context)
8913           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8914     else if (Member)
8915       BOMInit = new (Context)
8916           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8917                              Init, RParenLoc);
8918     else
8919       BOMInit = new (Context)
8920           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8921                              LParenLoc, Init, RParenLoc);
8922 
8923     if (/*IsWritten*/Record[Idx++]) {
8924       unsigned SourceOrder = Record[Idx++];
8925       BOMInit->setSourceOrder(SourceOrder);
8926     }
8927 
8928     CtorInitializers[i] = BOMInit;
8929   }
8930 
8931   return CtorInitializers;
8932 }
8933 
8934 NestedNameSpecifier *
8935 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8936                                    const RecordData &Record, unsigned &Idx) {
8937   ASTContext &Context = getContext();
8938   unsigned N = Record[Idx++];
8939   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
8940   for (unsigned I = 0; I != N; ++I) {
8941     NestedNameSpecifier::SpecifierKind Kind
8942       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8943     switch (Kind) {
8944     case NestedNameSpecifier::Identifier: {
8945       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8946       NNS = NestedNameSpecifier::Create(Context, Prev, II);
8947       break;
8948     }
8949 
8950     case NestedNameSpecifier::Namespace: {
8951       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8952       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8953       break;
8954     }
8955 
8956     case NestedNameSpecifier::NamespaceAlias: {
8957       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8958       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8959       break;
8960     }
8961 
8962     case NestedNameSpecifier::TypeSpec:
8963     case NestedNameSpecifier::TypeSpecWithTemplate: {
8964       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8965       if (!T)
8966         return nullptr;
8967 
8968       bool Template = Record[Idx++];
8969       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8970       break;
8971     }
8972 
8973     case NestedNameSpecifier::Global:
8974       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8975       // No associated value, and there can't be a prefix.
8976       break;
8977 
8978     case NestedNameSpecifier::Super: {
8979       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8980       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8981       break;
8982     }
8983     }
8984     Prev = NNS;
8985   }
8986   return NNS;
8987 }
8988 
8989 NestedNameSpecifierLoc
8990 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8991                                       unsigned &Idx) {
8992   ASTContext &Context = getContext();
8993   unsigned N = Record[Idx++];
8994   NestedNameSpecifierLocBuilder Builder;
8995   for (unsigned I = 0; I != N; ++I) {
8996     NestedNameSpecifier::SpecifierKind Kind
8997       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8998     switch (Kind) {
8999     case NestedNameSpecifier::Identifier: {
9000       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9001       SourceRange Range = ReadSourceRange(F, Record, Idx);
9002       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9003       break;
9004     }
9005 
9006     case NestedNameSpecifier::Namespace: {
9007       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9008       SourceRange Range = ReadSourceRange(F, Record, Idx);
9009       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9010       break;
9011     }
9012 
9013     case NestedNameSpecifier::NamespaceAlias: {
9014       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9015       SourceRange Range = ReadSourceRange(F, Record, Idx);
9016       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9017       break;
9018     }
9019 
9020     case NestedNameSpecifier::TypeSpec:
9021     case NestedNameSpecifier::TypeSpecWithTemplate: {
9022       bool Template = Record[Idx++];
9023       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9024       if (!T)
9025         return NestedNameSpecifierLoc();
9026       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9027 
9028       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9029       Builder.Extend(Context,
9030                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9031                      T->getTypeLoc(), ColonColonLoc);
9032       break;
9033     }
9034 
9035     case NestedNameSpecifier::Global: {
9036       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9037       Builder.MakeGlobal(Context, ColonColonLoc);
9038       break;
9039     }
9040 
9041     case NestedNameSpecifier::Super: {
9042       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9043       SourceRange Range = ReadSourceRange(F, Record, Idx);
9044       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9045       break;
9046     }
9047     }
9048   }
9049 
9050   return Builder.getWithLocInContext(Context);
9051 }
9052 
9053 SourceRange
9054 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9055                            unsigned &Idx) {
9056   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9057   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9058   return SourceRange(beg, end);
9059 }
9060 
9061 /// Read an integral value
9062 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9063   unsigned BitWidth = Record[Idx++];
9064   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9065   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9066   Idx += NumWords;
9067   return Result;
9068 }
9069 
9070 /// Read a signed integral value
9071 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9072   bool isUnsigned = Record[Idx++];
9073   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9074 }
9075 
9076 /// Read a floating-point value
9077 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9078                                      const llvm::fltSemantics &Sem,
9079                                      unsigned &Idx) {
9080   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9081 }
9082 
9083 // Read a string
9084 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9085   unsigned Len = Record[Idx++];
9086   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9087   Idx += Len;
9088   return Result;
9089 }
9090 
9091 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9092                                 unsigned &Idx) {
9093   std::string Filename = ReadString(Record, Idx);
9094   ResolveImportedPath(F, Filename);
9095   return Filename;
9096 }
9097 
9098 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9099                                          unsigned &Idx) {
9100   unsigned Major = Record[Idx++];
9101   unsigned Minor = Record[Idx++];
9102   unsigned Subminor = Record[Idx++];
9103   if (Minor == 0)
9104     return VersionTuple(Major);
9105   if (Subminor == 0)
9106     return VersionTuple(Major, Minor - 1);
9107   return VersionTuple(Major, Minor - 1, Subminor - 1);
9108 }
9109 
9110 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9111                                           const RecordData &Record,
9112                                           unsigned &Idx) {
9113   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9114   return CXXTemporary::Create(getContext(), Decl);
9115 }
9116 
9117 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9118   return Diag(CurrentImportLoc, DiagID);
9119 }
9120 
9121 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9122   return Diags.Report(Loc, DiagID);
9123 }
9124 
9125 /// Retrieve the identifier table associated with the
9126 /// preprocessor.
9127 IdentifierTable &ASTReader::getIdentifierTable() {
9128   return PP.getIdentifierTable();
9129 }
9130 
9131 /// Record that the given ID maps to the given switch-case
9132 /// statement.
9133 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9134   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9135          "Already have a SwitchCase with this ID");
9136   (*CurrSwitchCaseStmts)[ID] = SC;
9137 }
9138 
9139 /// Retrieve the switch-case statement with the given ID.
9140 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9141   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9142   return (*CurrSwitchCaseStmts)[ID];
9143 }
9144 
9145 void ASTReader::ClearSwitchCaseIDs() {
9146   CurrSwitchCaseStmts->clear();
9147 }
9148 
9149 void ASTReader::ReadComments() {
9150   ASTContext &Context = getContext();
9151   std::vector<RawComment *> Comments;
9152   for (SmallVectorImpl<std::pair<BitstreamCursor,
9153                                  serialization::ModuleFile *>>::iterator
9154        I = CommentsCursors.begin(),
9155        E = CommentsCursors.end();
9156        I != E; ++I) {
9157     Comments.clear();
9158     BitstreamCursor &Cursor = I->first;
9159     serialization::ModuleFile &F = *I->second;
9160     SavedStreamPosition SavedPosition(Cursor);
9161 
9162     RecordData Record;
9163     while (true) {
9164       llvm::BitstreamEntry Entry =
9165         Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
9166 
9167       switch (Entry.Kind) {
9168       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9169       case llvm::BitstreamEntry::Error:
9170         Error("malformed block record in AST file");
9171         return;
9172       case llvm::BitstreamEntry::EndBlock:
9173         goto NextCursor;
9174       case llvm::BitstreamEntry::Record:
9175         // The interesting case.
9176         break;
9177       }
9178 
9179       // Read a record.
9180       Record.clear();
9181       switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
9182       case COMMENTS_RAW_COMMENT: {
9183         unsigned Idx = 0;
9184         SourceRange SR = ReadSourceRange(F, Record, Idx);
9185         RawComment::CommentKind Kind =
9186             (RawComment::CommentKind) Record[Idx++];
9187         bool IsTrailingComment = Record[Idx++];
9188         bool IsAlmostTrailingComment = Record[Idx++];
9189         Comments.push_back(new (Context) RawComment(
9190             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9191         break;
9192       }
9193       }
9194     }
9195   NextCursor:
9196     // De-serialized SourceLocations get negative FileIDs for other modules,
9197     // potentially invalidating the original order. Sort it again.
9198     llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr));
9199     Context.Comments.addDeserializedComments(Comments);
9200   }
9201 }
9202 
9203 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9204                                 bool IncludeSystem, bool Complain,
9205                     llvm::function_ref<void(const serialization::InputFile &IF,
9206                                             bool isSystem)> Visitor) {
9207   unsigned NumUserInputs = MF.NumUserInputFiles;
9208   unsigned NumInputs = MF.InputFilesLoaded.size();
9209   assert(NumUserInputs <= NumInputs);
9210   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9211   for (unsigned I = 0; I < N; ++I) {
9212     bool IsSystem = I >= NumUserInputs;
9213     InputFile IF = getInputFile(MF, I+1, Complain);
9214     Visitor(IF, IsSystem);
9215   }
9216 }
9217 
9218 void ASTReader::visitTopLevelModuleMaps(
9219     serialization::ModuleFile &MF,
9220     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9221   unsigned NumInputs = MF.InputFilesLoaded.size();
9222   for (unsigned I = 0; I < NumInputs; ++I) {
9223     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9224     if (IFI.TopLevelModuleMap)
9225       // FIXME: This unnecessarily re-reads the InputFileInfo.
9226       if (auto *FE = getInputFile(MF, I + 1).getFile())
9227         Visitor(FE);
9228   }
9229 }
9230 
9231 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9232   // If we know the owning module, use it.
9233   if (Module *M = D->getImportedOwningModule())
9234     return M->getFullModuleName();
9235 
9236   // Otherwise, use the name of the top-level module the decl is within.
9237   if (ModuleFile *M = getOwningModuleFile(D))
9238     return M->ModuleName;
9239 
9240   // Not from a module.
9241   return {};
9242 }
9243 
9244 void ASTReader::finishPendingActions() {
9245   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9246          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9247          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9248          !PendingUpdateRecords.empty()) {
9249     // If any identifiers with corresponding top-level declarations have
9250     // been loaded, load those declarations now.
9251     using TopLevelDeclsMap =
9252         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9253     TopLevelDeclsMap TopLevelDecls;
9254 
9255     while (!PendingIdentifierInfos.empty()) {
9256       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9257       SmallVector<uint32_t, 4> DeclIDs =
9258           std::move(PendingIdentifierInfos.back().second);
9259       PendingIdentifierInfos.pop_back();
9260 
9261       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9262     }
9263 
9264     // Load each function type that we deferred loading because it was a
9265     // deduced type that might refer to a local type declared within itself.
9266     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9267       auto *FD = PendingFunctionTypes[I].first;
9268       FD->setType(GetType(PendingFunctionTypes[I].second));
9269 
9270       // If we gave a function a deduced return type, remember that we need to
9271       // propagate that along the redeclaration chain.
9272       auto *DT = FD->getReturnType()->getContainedDeducedType();
9273       if (DT && DT->isDeduced())
9274         PendingDeducedTypeUpdates.insert(
9275             {FD->getCanonicalDecl(), FD->getReturnType()});
9276     }
9277     PendingFunctionTypes.clear();
9278 
9279     // For each decl chain that we wanted to complete while deserializing, mark
9280     // it as "still needs to be completed".
9281     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9282       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9283     }
9284     PendingIncompleteDeclChains.clear();
9285 
9286     // Load pending declaration chains.
9287     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9288       loadPendingDeclChain(PendingDeclChains[I].first,
9289                            PendingDeclChains[I].second);
9290     PendingDeclChains.clear();
9291 
9292     // Make the most recent of the top-level declarations visible.
9293     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9294            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9295       IdentifierInfo *II = TLD->first;
9296       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9297         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9298       }
9299     }
9300 
9301     // Load any pending macro definitions.
9302     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9303       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9304       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9305       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9306       // Initialize the macro history from chained-PCHs ahead of module imports.
9307       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9308            ++IDIdx) {
9309         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9310         if (!Info.M->isModule())
9311           resolvePendingMacro(II, Info);
9312       }
9313       // Handle module imports.
9314       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9315            ++IDIdx) {
9316         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9317         if (Info.M->isModule())
9318           resolvePendingMacro(II, Info);
9319       }
9320     }
9321     PendingMacroIDs.clear();
9322 
9323     // Wire up the DeclContexts for Decls that we delayed setting until
9324     // recursive loading is completed.
9325     while (!PendingDeclContextInfos.empty()) {
9326       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9327       PendingDeclContextInfos.pop_front();
9328       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9329       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9330       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9331     }
9332 
9333     // Perform any pending declaration updates.
9334     while (!PendingUpdateRecords.empty()) {
9335       auto Update = PendingUpdateRecords.pop_back_val();
9336       ReadingKindTracker ReadingKind(Read_Decl, *this);
9337       loadDeclUpdateRecords(Update);
9338     }
9339   }
9340 
9341   // At this point, all update records for loaded decls are in place, so any
9342   // fake class definitions should have become real.
9343   assert(PendingFakeDefinitionData.empty() &&
9344          "faked up a class definition but never saw the real one");
9345 
9346   // If we deserialized any C++ or Objective-C class definitions, any
9347   // Objective-C protocol definitions, or any redeclarable templates, make sure
9348   // that all redeclarations point to the definitions. Note that this can only
9349   // happen now, after the redeclaration chains have been fully wired.
9350   for (Decl *D : PendingDefinitions) {
9351     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9352       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9353         // Make sure that the TagType points at the definition.
9354         const_cast<TagType*>(TagT)->decl = TD;
9355       }
9356 
9357       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9358         for (auto *R = getMostRecentExistingDecl(RD); R;
9359              R = R->getPreviousDecl()) {
9360           assert((R == D) ==
9361                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9362                  "declaration thinks it's the definition but it isn't");
9363           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9364         }
9365       }
9366 
9367       continue;
9368     }
9369 
9370     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9371       // Make sure that the ObjCInterfaceType points at the definition.
9372       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9373         ->Decl = ID;
9374 
9375       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9376         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9377 
9378       continue;
9379     }
9380 
9381     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9382       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9383         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9384 
9385       continue;
9386     }
9387 
9388     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9389     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9390       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9391   }
9392   PendingDefinitions.clear();
9393 
9394   // Load the bodies of any functions or methods we've encountered. We do
9395   // this now (delayed) so that we can be sure that the declaration chains
9396   // have been fully wired up (hasBody relies on this).
9397   // FIXME: We shouldn't require complete redeclaration chains here.
9398   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9399                                PBEnd = PendingBodies.end();
9400        PB != PBEnd; ++PB) {
9401     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9402       // For a function defined inline within a class template, force the
9403       // canonical definition to be the one inside the canonical definition of
9404       // the template. This ensures that we instantiate from a correct view
9405       // of the template.
9406       //
9407       // Sadly we can't do this more generally: we can't be sure that all
9408       // copies of an arbitrary class definition will have the same members
9409       // defined (eg, some member functions may not be instantiated, and some
9410       // special members may or may not have been implicitly defined).
9411       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9412         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9413           continue;
9414 
9415       // FIXME: Check for =delete/=default?
9416       // FIXME: Complain about ODR violations here?
9417       const FunctionDecl *Defn = nullptr;
9418       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9419         FD->setLazyBody(PB->second);
9420       } else {
9421         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9422         mergeDefinitionVisibility(NonConstDefn, FD);
9423 
9424         if (!FD->isLateTemplateParsed() &&
9425             !NonConstDefn->isLateTemplateParsed() &&
9426             FD->getODRHash() != NonConstDefn->getODRHash()) {
9427           if (!isa<CXXMethodDecl>(FD)) {
9428             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9429           } else if (FD->getLexicalParent()->isFileContext() &&
9430                      NonConstDefn->getLexicalParent()->isFileContext()) {
9431             // Only diagnose out-of-line method definitions.  If they are
9432             // in class definitions, then an error will be generated when
9433             // processing the class bodies.
9434             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9435           }
9436         }
9437       }
9438       continue;
9439     }
9440 
9441     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9442     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9443       MD->setLazyBody(PB->second);
9444   }
9445   PendingBodies.clear();
9446 
9447   // Do some cleanup.
9448   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9449     getContext().deduplicateMergedDefinitonsFor(ND);
9450   PendingMergedDefinitionsToDeduplicate.clear();
9451 }
9452 
9453 void ASTReader::diagnoseOdrViolations() {
9454   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9455       PendingFunctionOdrMergeFailures.empty() &&
9456       PendingEnumOdrMergeFailures.empty())
9457     return;
9458 
9459   // Trigger the import of the full definition of each class that had any
9460   // odr-merging problems, so we can produce better diagnostics for them.
9461   // These updates may in turn find and diagnose some ODR failures, so take
9462   // ownership of the set first.
9463   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9464   PendingOdrMergeFailures.clear();
9465   for (auto &Merge : OdrMergeFailures) {
9466     Merge.first->buildLookup();
9467     Merge.first->decls_begin();
9468     Merge.first->bases_begin();
9469     Merge.first->vbases_begin();
9470     for (auto &RecordPair : Merge.second) {
9471       auto *RD = RecordPair.first;
9472       RD->decls_begin();
9473       RD->bases_begin();
9474       RD->vbases_begin();
9475     }
9476   }
9477 
9478   // Trigger the import of functions.
9479   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9480   PendingFunctionOdrMergeFailures.clear();
9481   for (auto &Merge : FunctionOdrMergeFailures) {
9482     Merge.first->buildLookup();
9483     Merge.first->decls_begin();
9484     Merge.first->getBody();
9485     for (auto &FD : Merge.second) {
9486       FD->buildLookup();
9487       FD->decls_begin();
9488       FD->getBody();
9489     }
9490   }
9491 
9492   // Trigger the import of enums.
9493   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9494   PendingEnumOdrMergeFailures.clear();
9495   for (auto &Merge : EnumOdrMergeFailures) {
9496     Merge.first->decls_begin();
9497     for (auto &Enum : Merge.second) {
9498       Enum->decls_begin();
9499     }
9500   }
9501 
9502   // For each declaration from a merged context, check that the canonical
9503   // definition of that context also contains a declaration of the same
9504   // entity.
9505   //
9506   // Caution: this loop does things that might invalidate iterators into
9507   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9508   while (!PendingOdrMergeChecks.empty()) {
9509     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9510 
9511     // FIXME: Skip over implicit declarations for now. This matters for things
9512     // like implicitly-declared special member functions. This isn't entirely
9513     // correct; we can end up with multiple unmerged declarations of the same
9514     // implicit entity.
9515     if (D->isImplicit())
9516       continue;
9517 
9518     DeclContext *CanonDef = D->getDeclContext();
9519 
9520     bool Found = false;
9521     const Decl *DCanon = D->getCanonicalDecl();
9522 
9523     for (auto RI : D->redecls()) {
9524       if (RI->getLexicalDeclContext() == CanonDef) {
9525         Found = true;
9526         break;
9527       }
9528     }
9529     if (Found)
9530       continue;
9531 
9532     // Quick check failed, time to do the slow thing. Note, we can't just
9533     // look up the name of D in CanonDef here, because the member that is
9534     // in CanonDef might not be found by name lookup (it might have been
9535     // replaced by a more recent declaration in the lookup table), and we
9536     // can't necessarily find it in the redeclaration chain because it might
9537     // be merely mergeable, not redeclarable.
9538     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9539     for (auto *CanonMember : CanonDef->decls()) {
9540       if (CanonMember->getCanonicalDecl() == DCanon) {
9541         // This can happen if the declaration is merely mergeable and not
9542         // actually redeclarable (we looked for redeclarations earlier).
9543         //
9544         // FIXME: We should be able to detect this more efficiently, without
9545         // pulling in all of the members of CanonDef.
9546         Found = true;
9547         break;
9548       }
9549       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9550         if (ND->getDeclName() == D->getDeclName())
9551           Candidates.push_back(ND);
9552     }
9553 
9554     if (!Found) {
9555       // The AST doesn't like TagDecls becoming invalid after they've been
9556       // completed. We only really need to mark FieldDecls as invalid here.
9557       if (!isa<TagDecl>(D))
9558         D->setInvalidDecl();
9559 
9560       // Ensure we don't accidentally recursively enter deserialization while
9561       // we're producing our diagnostic.
9562       Deserializing RecursionGuard(this);
9563 
9564       std::string CanonDefModule =
9565           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9566       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9567         << D << getOwningModuleNameForDiagnostic(D)
9568         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9569 
9570       if (Candidates.empty())
9571         Diag(cast<Decl>(CanonDef)->getLocation(),
9572              diag::note_module_odr_violation_no_possible_decls) << D;
9573       else {
9574         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9575           Diag(Candidates[I]->getLocation(),
9576                diag::note_module_odr_violation_possible_decl)
9577             << Candidates[I];
9578       }
9579 
9580       DiagnosedOdrMergeFailures.insert(CanonDef);
9581     }
9582   }
9583 
9584   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9585       EnumOdrMergeFailures.empty())
9586     return;
9587 
9588   // Ensure we don't accidentally recursively enter deserialization while
9589   // we're producing our diagnostics.
9590   Deserializing RecursionGuard(this);
9591 
9592   // Common code for hashing helpers.
9593   ODRHash Hash;
9594   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9595     Hash.clear();
9596     Hash.AddQualType(Ty);
9597     return Hash.CalculateHash();
9598   };
9599 
9600   auto ComputeODRHash = [&Hash](const Stmt *S) {
9601     assert(S);
9602     Hash.clear();
9603     Hash.AddStmt(S);
9604     return Hash.CalculateHash();
9605   };
9606 
9607   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9608     assert(D);
9609     Hash.clear();
9610     Hash.AddSubDecl(D);
9611     return Hash.CalculateHash();
9612   };
9613 
9614   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9615     Hash.clear();
9616     Hash.AddTemplateArgument(TA);
9617     return Hash.CalculateHash();
9618   };
9619 
9620   auto ComputeTemplateParameterListODRHash =
9621       [&Hash](const TemplateParameterList *TPL) {
9622         assert(TPL);
9623         Hash.clear();
9624         Hash.AddTemplateParameterList(TPL);
9625         return Hash.CalculateHash();
9626       };
9627 
9628   // Issue any pending ODR-failure diagnostics.
9629   for (auto &Merge : OdrMergeFailures) {
9630     // If we've already pointed out a specific problem with this class, don't
9631     // bother issuing a general "something's different" diagnostic.
9632     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9633       continue;
9634 
9635     bool Diagnosed = false;
9636     CXXRecordDecl *FirstRecord = Merge.first;
9637     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9638     for (auto &RecordPair : Merge.second) {
9639       CXXRecordDecl *SecondRecord = RecordPair.first;
9640       // Multiple different declarations got merged together; tell the user
9641       // where they came from.
9642       if (FirstRecord == SecondRecord)
9643         continue;
9644 
9645       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9646 
9647       auto *FirstDD = FirstRecord->DefinitionData;
9648       auto *SecondDD = RecordPair.second;
9649 
9650       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9651 
9652       // Diagnostics from DefinitionData are emitted here.
9653       if (FirstDD != SecondDD) {
9654         enum ODRDefinitionDataDifference {
9655           NumBases,
9656           NumVBases,
9657           BaseType,
9658           BaseVirtual,
9659           BaseAccess,
9660         };
9661         auto ODRDiagError = [FirstRecord, &FirstModule,
9662                              this](SourceLocation Loc, SourceRange Range,
9663                                    ODRDefinitionDataDifference DiffType) {
9664           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9665                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9666                  << DiffType;
9667         };
9668         auto ODRDiagNote = [&SecondModule,
9669                             this](SourceLocation Loc, SourceRange Range,
9670                                   ODRDefinitionDataDifference DiffType) {
9671           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9672                  << SecondModule << Range << DiffType;
9673         };
9674 
9675         unsigned FirstNumBases = FirstDD->NumBases;
9676         unsigned FirstNumVBases = FirstDD->NumVBases;
9677         unsigned SecondNumBases = SecondDD->NumBases;
9678         unsigned SecondNumVBases = SecondDD->NumVBases;
9679 
9680         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9681           unsigned NumBases = DD->NumBases;
9682           if (NumBases == 0) return SourceRange();
9683           auto bases = DD->bases();
9684           return SourceRange(bases[0].getBeginLoc(),
9685                              bases[NumBases - 1].getEndLoc());
9686         };
9687 
9688         if (FirstNumBases != SecondNumBases) {
9689           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9690                        NumBases)
9691               << FirstNumBases;
9692           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9693                       NumBases)
9694               << SecondNumBases;
9695           Diagnosed = true;
9696           break;
9697         }
9698 
9699         if (FirstNumVBases != SecondNumVBases) {
9700           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9701                        NumVBases)
9702               << FirstNumVBases;
9703           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9704                       NumVBases)
9705               << SecondNumVBases;
9706           Diagnosed = true;
9707           break;
9708         }
9709 
9710         auto FirstBases = FirstDD->bases();
9711         auto SecondBases = SecondDD->bases();
9712         unsigned i = 0;
9713         for (i = 0; i < FirstNumBases; ++i) {
9714           auto FirstBase = FirstBases[i];
9715           auto SecondBase = SecondBases[i];
9716           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9717               ComputeQualTypeODRHash(SecondBase.getType())) {
9718             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9719                          BaseType)
9720                 << (i + 1) << FirstBase.getType();
9721             ODRDiagNote(SecondRecord->getLocation(),
9722                         SecondBase.getSourceRange(), BaseType)
9723                 << (i + 1) << SecondBase.getType();
9724             break;
9725           }
9726 
9727           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9728             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9729                          BaseVirtual)
9730                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9731             ODRDiagNote(SecondRecord->getLocation(),
9732                         SecondBase.getSourceRange(), BaseVirtual)
9733                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9734             break;
9735           }
9736 
9737           if (FirstBase.getAccessSpecifierAsWritten() !=
9738               SecondBase.getAccessSpecifierAsWritten()) {
9739             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9740                          BaseAccess)
9741                 << (i + 1) << FirstBase.getType()
9742                 << (int)FirstBase.getAccessSpecifierAsWritten();
9743             ODRDiagNote(SecondRecord->getLocation(),
9744                         SecondBase.getSourceRange(), BaseAccess)
9745                 << (i + 1) << SecondBase.getType()
9746                 << (int)SecondBase.getAccessSpecifierAsWritten();
9747             break;
9748           }
9749         }
9750 
9751         if (i != FirstNumBases) {
9752           Diagnosed = true;
9753           break;
9754         }
9755       }
9756 
9757       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9758 
9759       const ClassTemplateDecl *FirstTemplate =
9760           FirstRecord->getDescribedClassTemplate();
9761       const ClassTemplateDecl *SecondTemplate =
9762           SecondRecord->getDescribedClassTemplate();
9763 
9764       assert(!FirstTemplate == !SecondTemplate &&
9765              "Both pointers should be null or non-null");
9766 
9767       enum ODRTemplateDifference {
9768         ParamEmptyName,
9769         ParamName,
9770         ParamSingleDefaultArgument,
9771         ParamDifferentDefaultArgument,
9772       };
9773 
9774       if (FirstTemplate && SecondTemplate) {
9775         DeclHashes FirstTemplateHashes;
9776         DeclHashes SecondTemplateHashes;
9777 
9778         auto PopulateTemplateParameterHashs =
9779             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9780                                      const ClassTemplateDecl *TD) {
9781               for (auto *D : TD->getTemplateParameters()->asArray()) {
9782                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9783               }
9784             };
9785 
9786         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
9787         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
9788 
9789         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
9790                "Number of template parameters should be equal.");
9791 
9792         auto FirstIt = FirstTemplateHashes.begin();
9793         auto FirstEnd = FirstTemplateHashes.end();
9794         auto SecondIt = SecondTemplateHashes.begin();
9795         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
9796           if (FirstIt->second == SecondIt->second)
9797             continue;
9798 
9799           auto ODRDiagError = [FirstRecord, &FirstModule,
9800                                this](SourceLocation Loc, SourceRange Range,
9801                                      ODRTemplateDifference DiffType) {
9802             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
9803                    << FirstRecord << FirstModule.empty() << FirstModule << Range
9804                    << DiffType;
9805           };
9806           auto ODRDiagNote = [&SecondModule,
9807                               this](SourceLocation Loc, SourceRange Range,
9808                                     ODRTemplateDifference DiffType) {
9809             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
9810                    << SecondModule << Range << DiffType;
9811           };
9812 
9813           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
9814           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
9815 
9816           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
9817                  "Parameter Decl's should be the same kind.");
9818 
9819           DeclarationName FirstName = FirstDecl->getDeclName();
9820           DeclarationName SecondName = SecondDecl->getDeclName();
9821 
9822           if (FirstName != SecondName) {
9823             const bool FirstNameEmpty =
9824                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
9825             const bool SecondNameEmpty =
9826                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
9827             assert((!FirstNameEmpty || !SecondNameEmpty) &&
9828                    "Both template parameters cannot be unnamed.");
9829             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9830                          FirstNameEmpty ? ParamEmptyName : ParamName)
9831                 << FirstName;
9832             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9833                         SecondNameEmpty ? ParamEmptyName : ParamName)
9834                 << SecondName;
9835             break;
9836           }
9837 
9838           switch (FirstDecl->getKind()) {
9839           default:
9840             llvm_unreachable("Invalid template parameter type.");
9841           case Decl::TemplateTypeParm: {
9842             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
9843             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
9844             const bool HasFirstDefaultArgument =
9845                 FirstParam->hasDefaultArgument() &&
9846                 !FirstParam->defaultArgumentWasInherited();
9847             const bool HasSecondDefaultArgument =
9848                 SecondParam->hasDefaultArgument() &&
9849                 !SecondParam->defaultArgumentWasInherited();
9850 
9851             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9852               ODRDiagError(FirstDecl->getLocation(),
9853                            FirstDecl->getSourceRange(),
9854                            ParamSingleDefaultArgument)
9855                   << HasFirstDefaultArgument;
9856               ODRDiagNote(SecondDecl->getLocation(),
9857                           SecondDecl->getSourceRange(),
9858                           ParamSingleDefaultArgument)
9859                   << HasSecondDefaultArgument;
9860               break;
9861             }
9862 
9863             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9864                    "Expecting default arguments.");
9865 
9866             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9867                          ParamDifferentDefaultArgument);
9868             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9869                         ParamDifferentDefaultArgument);
9870 
9871             break;
9872           }
9873           case Decl::NonTypeTemplateParm: {
9874             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
9875             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
9876             const bool HasFirstDefaultArgument =
9877                 FirstParam->hasDefaultArgument() &&
9878                 !FirstParam->defaultArgumentWasInherited();
9879             const bool HasSecondDefaultArgument =
9880                 SecondParam->hasDefaultArgument() &&
9881                 !SecondParam->defaultArgumentWasInherited();
9882 
9883             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9884               ODRDiagError(FirstDecl->getLocation(),
9885                            FirstDecl->getSourceRange(),
9886                            ParamSingleDefaultArgument)
9887                   << HasFirstDefaultArgument;
9888               ODRDiagNote(SecondDecl->getLocation(),
9889                           SecondDecl->getSourceRange(),
9890                           ParamSingleDefaultArgument)
9891                   << HasSecondDefaultArgument;
9892               break;
9893             }
9894 
9895             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9896                    "Expecting default arguments.");
9897 
9898             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9899                          ParamDifferentDefaultArgument);
9900             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9901                         ParamDifferentDefaultArgument);
9902 
9903             break;
9904           }
9905           case Decl::TemplateTemplateParm: {
9906             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
9907             const auto *SecondParam =
9908                 cast<TemplateTemplateParmDecl>(SecondDecl);
9909             const bool HasFirstDefaultArgument =
9910                 FirstParam->hasDefaultArgument() &&
9911                 !FirstParam->defaultArgumentWasInherited();
9912             const bool HasSecondDefaultArgument =
9913                 SecondParam->hasDefaultArgument() &&
9914                 !SecondParam->defaultArgumentWasInherited();
9915 
9916             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9917               ODRDiagError(FirstDecl->getLocation(),
9918                            FirstDecl->getSourceRange(),
9919                            ParamSingleDefaultArgument)
9920                   << HasFirstDefaultArgument;
9921               ODRDiagNote(SecondDecl->getLocation(),
9922                           SecondDecl->getSourceRange(),
9923                           ParamSingleDefaultArgument)
9924                   << HasSecondDefaultArgument;
9925               break;
9926             }
9927 
9928             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9929                    "Expecting default arguments.");
9930 
9931             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9932                          ParamDifferentDefaultArgument);
9933             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9934                         ParamDifferentDefaultArgument);
9935 
9936             break;
9937           }
9938           }
9939 
9940           break;
9941         }
9942 
9943         if (FirstIt != FirstEnd) {
9944           Diagnosed = true;
9945           break;
9946         }
9947       }
9948 
9949       DeclHashes FirstHashes;
9950       DeclHashes SecondHashes;
9951 
9952       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
9953                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
9954         for (auto *D : Record->decls()) {
9955           // Due to decl merging, the first CXXRecordDecl is the parent of
9956           // Decls in both records.
9957           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
9958             continue;
9959           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9960         }
9961       };
9962       PopulateHashes(FirstHashes, FirstRecord);
9963       PopulateHashes(SecondHashes, SecondRecord);
9964 
9965       // Used with err_module_odr_violation_mismatch_decl and
9966       // note_module_odr_violation_mismatch_decl
9967       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9968       enum {
9969         EndOfClass,
9970         PublicSpecifer,
9971         PrivateSpecifer,
9972         ProtectedSpecifer,
9973         StaticAssert,
9974         Field,
9975         CXXMethod,
9976         TypeAlias,
9977         TypeDef,
9978         Var,
9979         Friend,
9980         FunctionTemplate,
9981         Other
9982       } FirstDiffType = Other,
9983         SecondDiffType = Other;
9984 
9985       auto DifferenceSelector = [](Decl *D) {
9986         assert(D && "valid Decl required");
9987         switch (D->getKind()) {
9988         default:
9989           return Other;
9990         case Decl::AccessSpec:
9991           switch (D->getAccess()) {
9992           case AS_public:
9993             return PublicSpecifer;
9994           case AS_private:
9995             return PrivateSpecifer;
9996           case AS_protected:
9997             return ProtectedSpecifer;
9998           case AS_none:
9999             break;
10000           }
10001           llvm_unreachable("Invalid access specifier");
10002         case Decl::StaticAssert:
10003           return StaticAssert;
10004         case Decl::Field:
10005           return Field;
10006         case Decl::CXXMethod:
10007         case Decl::CXXConstructor:
10008         case Decl::CXXDestructor:
10009           return CXXMethod;
10010         case Decl::TypeAlias:
10011           return TypeAlias;
10012         case Decl::Typedef:
10013           return TypeDef;
10014         case Decl::Var:
10015           return Var;
10016         case Decl::Friend:
10017           return Friend;
10018         case Decl::FunctionTemplate:
10019           return FunctionTemplate;
10020         }
10021       };
10022 
10023       Decl *FirstDecl = nullptr;
10024       Decl *SecondDecl = nullptr;
10025       auto FirstIt = FirstHashes.begin();
10026       auto SecondIt = SecondHashes.begin();
10027 
10028       // If there is a diagnoseable difference, FirstDiffType and
10029       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10030       // filled in if not EndOfClass.
10031       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10032         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10033             FirstIt->second == SecondIt->second) {
10034           ++FirstIt;
10035           ++SecondIt;
10036           continue;
10037         }
10038 
10039         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10040         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10041 
10042         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10043         SecondDiffType =
10044             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10045 
10046         break;
10047       }
10048 
10049       if (FirstDiffType == Other || SecondDiffType == Other) {
10050         // Reaching this point means an unexpected Decl was encountered
10051         // or no difference was detected.  This causes a generic error
10052         // message to be emitted.
10053         Diag(FirstRecord->getLocation(),
10054              diag::err_module_odr_violation_different_definitions)
10055             << FirstRecord << FirstModule.empty() << FirstModule;
10056 
10057         if (FirstDecl) {
10058           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10059               << FirstRecord << FirstDecl->getSourceRange();
10060         }
10061 
10062         Diag(SecondRecord->getLocation(),
10063              diag::note_module_odr_violation_different_definitions)
10064             << SecondModule;
10065 
10066         if (SecondDecl) {
10067           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10068               << SecondDecl->getSourceRange();
10069         }
10070 
10071         Diagnosed = true;
10072         break;
10073       }
10074 
10075       if (FirstDiffType != SecondDiffType) {
10076         SourceLocation FirstLoc;
10077         SourceRange FirstRange;
10078         if (FirstDiffType == EndOfClass) {
10079           FirstLoc = FirstRecord->getBraceRange().getEnd();
10080         } else {
10081           FirstLoc = FirstIt->first->getLocation();
10082           FirstRange = FirstIt->first->getSourceRange();
10083         }
10084         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10085             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10086             << FirstDiffType;
10087 
10088         SourceLocation SecondLoc;
10089         SourceRange SecondRange;
10090         if (SecondDiffType == EndOfClass) {
10091           SecondLoc = SecondRecord->getBraceRange().getEnd();
10092         } else {
10093           SecondLoc = SecondDecl->getLocation();
10094           SecondRange = SecondDecl->getSourceRange();
10095         }
10096         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10097             << SecondModule << SecondRange << SecondDiffType;
10098         Diagnosed = true;
10099         break;
10100       }
10101 
10102       assert(FirstDiffType == SecondDiffType);
10103 
10104       // Used with err_module_odr_violation_mismatch_decl_diff and
10105       // note_module_odr_violation_mismatch_decl_diff
10106       enum ODRDeclDifference {
10107         StaticAssertCondition,
10108         StaticAssertMessage,
10109         StaticAssertOnlyMessage,
10110         FieldName,
10111         FieldTypeName,
10112         FieldSingleBitField,
10113         FieldDifferentWidthBitField,
10114         FieldSingleMutable,
10115         FieldSingleInitializer,
10116         FieldDifferentInitializers,
10117         MethodName,
10118         MethodDeleted,
10119         MethodDefaulted,
10120         MethodVirtual,
10121         MethodStatic,
10122         MethodVolatile,
10123         MethodConst,
10124         MethodInline,
10125         MethodNumberParameters,
10126         MethodParameterType,
10127         MethodParameterName,
10128         MethodParameterSingleDefaultArgument,
10129         MethodParameterDifferentDefaultArgument,
10130         MethodNoTemplateArguments,
10131         MethodDifferentNumberTemplateArguments,
10132         MethodDifferentTemplateArgument,
10133         MethodSingleBody,
10134         MethodDifferentBody,
10135         TypedefName,
10136         TypedefType,
10137         VarName,
10138         VarType,
10139         VarSingleInitializer,
10140         VarDifferentInitializer,
10141         VarConstexpr,
10142         FriendTypeFunction,
10143         FriendType,
10144         FriendFunction,
10145         FunctionTemplateDifferentNumberParameters,
10146         FunctionTemplateParameterDifferentKind,
10147         FunctionTemplateParameterName,
10148         FunctionTemplateParameterSingleDefaultArgument,
10149         FunctionTemplateParameterDifferentDefaultArgument,
10150         FunctionTemplateParameterDifferentType,
10151         FunctionTemplatePackParameter,
10152       };
10153 
10154       // These lambdas have the common portions of the ODR diagnostics.  This
10155       // has the same return as Diag(), so addition parameters can be passed
10156       // in with operator<<
10157       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10158           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10159         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10160                << FirstRecord << FirstModule.empty() << FirstModule << Range
10161                << DiffType;
10162       };
10163       auto ODRDiagNote = [&SecondModule, this](
10164           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10165         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10166                << SecondModule << Range << DiffType;
10167       };
10168 
10169       switch (FirstDiffType) {
10170       case Other:
10171       case EndOfClass:
10172       case PublicSpecifer:
10173       case PrivateSpecifer:
10174       case ProtectedSpecifer:
10175         llvm_unreachable("Invalid diff type");
10176 
10177       case StaticAssert: {
10178         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10179         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10180 
10181         Expr *FirstExpr = FirstSA->getAssertExpr();
10182         Expr *SecondExpr = SecondSA->getAssertExpr();
10183         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10184         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10185         if (FirstODRHash != SecondODRHash) {
10186           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10187                        StaticAssertCondition);
10188           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10189                       StaticAssertCondition);
10190           Diagnosed = true;
10191           break;
10192         }
10193 
10194         StringLiteral *FirstStr = FirstSA->getMessage();
10195         StringLiteral *SecondStr = SecondSA->getMessage();
10196         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10197         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10198           SourceLocation FirstLoc, SecondLoc;
10199           SourceRange FirstRange, SecondRange;
10200           if (FirstStr) {
10201             FirstLoc = FirstStr->getBeginLoc();
10202             FirstRange = FirstStr->getSourceRange();
10203           } else {
10204             FirstLoc = FirstSA->getBeginLoc();
10205             FirstRange = FirstSA->getSourceRange();
10206           }
10207           if (SecondStr) {
10208             SecondLoc = SecondStr->getBeginLoc();
10209             SecondRange = SecondStr->getSourceRange();
10210           } else {
10211             SecondLoc = SecondSA->getBeginLoc();
10212             SecondRange = SecondSA->getSourceRange();
10213           }
10214           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10215               << (FirstStr == nullptr);
10216           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10217               << (SecondStr == nullptr);
10218           Diagnosed = true;
10219           break;
10220         }
10221 
10222         if (FirstStr && SecondStr &&
10223             FirstStr->getString() != SecondStr->getString()) {
10224           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10225                        StaticAssertMessage);
10226           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10227                       StaticAssertMessage);
10228           Diagnosed = true;
10229           break;
10230         }
10231         break;
10232       }
10233       case Field: {
10234         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10235         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10236         IdentifierInfo *FirstII = FirstField->getIdentifier();
10237         IdentifierInfo *SecondII = SecondField->getIdentifier();
10238         if (FirstII->getName() != SecondII->getName()) {
10239           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10240                        FieldName)
10241               << FirstII;
10242           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10243                       FieldName)
10244               << SecondII;
10245 
10246           Diagnosed = true;
10247           break;
10248         }
10249 
10250         assert(getContext().hasSameType(FirstField->getType(),
10251                                         SecondField->getType()));
10252 
10253         QualType FirstType = FirstField->getType();
10254         QualType SecondType = SecondField->getType();
10255         if (ComputeQualTypeODRHash(FirstType) !=
10256             ComputeQualTypeODRHash(SecondType)) {
10257           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10258                        FieldTypeName)
10259               << FirstII << FirstType;
10260           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10261                       FieldTypeName)
10262               << SecondII << SecondType;
10263 
10264           Diagnosed = true;
10265           break;
10266         }
10267 
10268         const bool IsFirstBitField = FirstField->isBitField();
10269         const bool IsSecondBitField = SecondField->isBitField();
10270         if (IsFirstBitField != IsSecondBitField) {
10271           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10272                        FieldSingleBitField)
10273               << FirstII << IsFirstBitField;
10274           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10275                       FieldSingleBitField)
10276               << SecondII << IsSecondBitField;
10277           Diagnosed = true;
10278           break;
10279         }
10280 
10281         if (IsFirstBitField && IsSecondBitField) {
10282           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10283                        FieldDifferentWidthBitField)
10284               << FirstII << FirstField->getBitWidth()->getSourceRange();
10285           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10286                       FieldDifferentWidthBitField)
10287               << SecondII << SecondField->getBitWidth()->getSourceRange();
10288           Diagnosed = true;
10289           break;
10290         }
10291 
10292         const bool IsFirstMutable = FirstField->isMutable();
10293         const bool IsSecondMutable = SecondField->isMutable();
10294         if (IsFirstMutable != IsSecondMutable) {
10295           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10296                        FieldSingleMutable)
10297               << FirstII << IsFirstMutable;
10298           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10299                       FieldSingleMutable)
10300               << SecondII << IsSecondMutable;
10301           Diagnosed = true;
10302           break;
10303         }
10304 
10305         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10306         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10307         if ((!FirstInitializer && SecondInitializer) ||
10308             (FirstInitializer && !SecondInitializer)) {
10309           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10310                        FieldSingleInitializer)
10311               << FirstII << (FirstInitializer != nullptr);
10312           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10313                       FieldSingleInitializer)
10314               << SecondII << (SecondInitializer != nullptr);
10315           Diagnosed = true;
10316           break;
10317         }
10318 
10319         if (FirstInitializer && SecondInitializer) {
10320           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10321           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10322           if (FirstInitHash != SecondInitHash) {
10323             ODRDiagError(FirstField->getLocation(),
10324                          FirstField->getSourceRange(),
10325                          FieldDifferentInitializers)
10326                 << FirstII << FirstInitializer->getSourceRange();
10327             ODRDiagNote(SecondField->getLocation(),
10328                         SecondField->getSourceRange(),
10329                         FieldDifferentInitializers)
10330                 << SecondII << SecondInitializer->getSourceRange();
10331             Diagnosed = true;
10332             break;
10333           }
10334         }
10335 
10336         break;
10337       }
10338       case CXXMethod: {
10339         enum {
10340           DiagMethod,
10341           DiagConstructor,
10342           DiagDestructor,
10343         } FirstMethodType,
10344             SecondMethodType;
10345         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10346           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10347           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10348           return DiagMethod;
10349         };
10350         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10351         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10352         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10353         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10354         auto FirstName = FirstMethod->getDeclName();
10355         auto SecondName = SecondMethod->getDeclName();
10356         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10357           ODRDiagError(FirstMethod->getLocation(),
10358                        FirstMethod->getSourceRange(), MethodName)
10359               << FirstMethodType << FirstName;
10360           ODRDiagNote(SecondMethod->getLocation(),
10361                       SecondMethod->getSourceRange(), MethodName)
10362               << SecondMethodType << SecondName;
10363 
10364           Diagnosed = true;
10365           break;
10366         }
10367 
10368         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10369         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10370         if (FirstDeleted != SecondDeleted) {
10371           ODRDiagError(FirstMethod->getLocation(),
10372                        FirstMethod->getSourceRange(), MethodDeleted)
10373               << FirstMethodType << FirstName << FirstDeleted;
10374 
10375           ODRDiagNote(SecondMethod->getLocation(),
10376                       SecondMethod->getSourceRange(), MethodDeleted)
10377               << SecondMethodType << SecondName << SecondDeleted;
10378           Diagnosed = true;
10379           break;
10380         }
10381 
10382         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10383         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10384         if (FirstDefaulted != SecondDefaulted) {
10385           ODRDiagError(FirstMethod->getLocation(),
10386                        FirstMethod->getSourceRange(), MethodDefaulted)
10387               << FirstMethodType << FirstName << FirstDefaulted;
10388 
10389           ODRDiagNote(SecondMethod->getLocation(),
10390                       SecondMethod->getSourceRange(), MethodDefaulted)
10391               << SecondMethodType << SecondName << SecondDefaulted;
10392           Diagnosed = true;
10393           break;
10394         }
10395 
10396         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10397         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10398         const bool FirstPure = FirstMethod->isPure();
10399         const bool SecondPure = SecondMethod->isPure();
10400         if ((FirstVirtual || SecondVirtual) &&
10401             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10402           ODRDiagError(FirstMethod->getLocation(),
10403                        FirstMethod->getSourceRange(), MethodVirtual)
10404               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10405           ODRDiagNote(SecondMethod->getLocation(),
10406                       SecondMethod->getSourceRange(), MethodVirtual)
10407               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10408           Diagnosed = true;
10409           break;
10410         }
10411 
10412         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10413         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10414         // class needs to be checked instead.
10415         const auto FirstStorage = FirstMethod->getStorageClass();
10416         const auto SecondStorage = SecondMethod->getStorageClass();
10417         const bool FirstStatic = FirstStorage == SC_Static;
10418         const bool SecondStatic = SecondStorage == SC_Static;
10419         if (FirstStatic != SecondStatic) {
10420           ODRDiagError(FirstMethod->getLocation(),
10421                        FirstMethod->getSourceRange(), MethodStatic)
10422               << FirstMethodType << FirstName << FirstStatic;
10423           ODRDiagNote(SecondMethod->getLocation(),
10424                       SecondMethod->getSourceRange(), MethodStatic)
10425               << SecondMethodType << SecondName << SecondStatic;
10426           Diagnosed = true;
10427           break;
10428         }
10429 
10430         const bool FirstVolatile = FirstMethod->isVolatile();
10431         const bool SecondVolatile = SecondMethod->isVolatile();
10432         if (FirstVolatile != SecondVolatile) {
10433           ODRDiagError(FirstMethod->getLocation(),
10434                        FirstMethod->getSourceRange(), MethodVolatile)
10435               << FirstMethodType << FirstName << FirstVolatile;
10436           ODRDiagNote(SecondMethod->getLocation(),
10437                       SecondMethod->getSourceRange(), MethodVolatile)
10438               << SecondMethodType << SecondName << SecondVolatile;
10439           Diagnosed = true;
10440           break;
10441         }
10442 
10443         const bool FirstConst = FirstMethod->isConst();
10444         const bool SecondConst = SecondMethod->isConst();
10445         if (FirstConst != SecondConst) {
10446           ODRDiagError(FirstMethod->getLocation(),
10447                        FirstMethod->getSourceRange(), MethodConst)
10448               << FirstMethodType << FirstName << FirstConst;
10449           ODRDiagNote(SecondMethod->getLocation(),
10450                       SecondMethod->getSourceRange(), MethodConst)
10451               << SecondMethodType << SecondName << SecondConst;
10452           Diagnosed = true;
10453           break;
10454         }
10455 
10456         const bool FirstInline = FirstMethod->isInlineSpecified();
10457         const bool SecondInline = SecondMethod->isInlineSpecified();
10458         if (FirstInline != SecondInline) {
10459           ODRDiagError(FirstMethod->getLocation(),
10460                        FirstMethod->getSourceRange(), MethodInline)
10461               << FirstMethodType << FirstName << FirstInline;
10462           ODRDiagNote(SecondMethod->getLocation(),
10463                       SecondMethod->getSourceRange(), MethodInline)
10464               << SecondMethodType << SecondName << SecondInline;
10465           Diagnosed = true;
10466           break;
10467         }
10468 
10469         const unsigned FirstNumParameters = FirstMethod->param_size();
10470         const unsigned SecondNumParameters = SecondMethod->param_size();
10471         if (FirstNumParameters != SecondNumParameters) {
10472           ODRDiagError(FirstMethod->getLocation(),
10473                        FirstMethod->getSourceRange(), MethodNumberParameters)
10474               << FirstMethodType << FirstName << FirstNumParameters;
10475           ODRDiagNote(SecondMethod->getLocation(),
10476                       SecondMethod->getSourceRange(), MethodNumberParameters)
10477               << SecondMethodType << SecondName << SecondNumParameters;
10478           Diagnosed = true;
10479           break;
10480         }
10481 
10482         // Need this status boolean to know when break out of the switch.
10483         bool ParameterMismatch = false;
10484         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10485           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10486           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10487 
10488           QualType FirstParamType = FirstParam->getType();
10489           QualType SecondParamType = SecondParam->getType();
10490           if (FirstParamType != SecondParamType &&
10491               ComputeQualTypeODRHash(FirstParamType) !=
10492                   ComputeQualTypeODRHash(SecondParamType)) {
10493             if (const DecayedType *ParamDecayedType =
10494                     FirstParamType->getAs<DecayedType>()) {
10495               ODRDiagError(FirstMethod->getLocation(),
10496                            FirstMethod->getSourceRange(), MethodParameterType)
10497                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10498                   << true << ParamDecayedType->getOriginalType();
10499             } else {
10500               ODRDiagError(FirstMethod->getLocation(),
10501                            FirstMethod->getSourceRange(), MethodParameterType)
10502                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10503                   << false;
10504             }
10505 
10506             if (const DecayedType *ParamDecayedType =
10507                     SecondParamType->getAs<DecayedType>()) {
10508               ODRDiagNote(SecondMethod->getLocation(),
10509                           SecondMethod->getSourceRange(), MethodParameterType)
10510                   << SecondMethodType << SecondName << (I + 1)
10511                   << SecondParamType << true
10512                   << ParamDecayedType->getOriginalType();
10513             } else {
10514               ODRDiagNote(SecondMethod->getLocation(),
10515                           SecondMethod->getSourceRange(), MethodParameterType)
10516                   << SecondMethodType << SecondName << (I + 1)
10517                   << SecondParamType << false;
10518             }
10519             ParameterMismatch = true;
10520             break;
10521           }
10522 
10523           DeclarationName FirstParamName = FirstParam->getDeclName();
10524           DeclarationName SecondParamName = SecondParam->getDeclName();
10525           if (FirstParamName != SecondParamName) {
10526             ODRDiagError(FirstMethod->getLocation(),
10527                          FirstMethod->getSourceRange(), MethodParameterName)
10528                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10529             ODRDiagNote(SecondMethod->getLocation(),
10530                         SecondMethod->getSourceRange(), MethodParameterName)
10531                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10532             ParameterMismatch = true;
10533             break;
10534           }
10535 
10536           const Expr *FirstInit = FirstParam->getInit();
10537           const Expr *SecondInit = SecondParam->getInit();
10538           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10539             ODRDiagError(FirstMethod->getLocation(),
10540                          FirstMethod->getSourceRange(),
10541                          MethodParameterSingleDefaultArgument)
10542                 << FirstMethodType << FirstName << (I + 1)
10543                 << (FirstInit == nullptr)
10544                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10545             ODRDiagNote(SecondMethod->getLocation(),
10546                         SecondMethod->getSourceRange(),
10547                         MethodParameterSingleDefaultArgument)
10548                 << SecondMethodType << SecondName << (I + 1)
10549                 << (SecondInit == nullptr)
10550                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10551             ParameterMismatch = true;
10552             break;
10553           }
10554 
10555           if (FirstInit && SecondInit &&
10556               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10557             ODRDiagError(FirstMethod->getLocation(),
10558                          FirstMethod->getSourceRange(),
10559                          MethodParameterDifferentDefaultArgument)
10560                 << FirstMethodType << FirstName << (I + 1)
10561                 << FirstInit->getSourceRange();
10562             ODRDiagNote(SecondMethod->getLocation(),
10563                         SecondMethod->getSourceRange(),
10564                         MethodParameterDifferentDefaultArgument)
10565                 << SecondMethodType << SecondName << (I + 1)
10566                 << SecondInit->getSourceRange();
10567             ParameterMismatch = true;
10568             break;
10569 
10570           }
10571         }
10572 
10573         if (ParameterMismatch) {
10574           Diagnosed = true;
10575           break;
10576         }
10577 
10578         const auto *FirstTemplateArgs =
10579             FirstMethod->getTemplateSpecializationArgs();
10580         const auto *SecondTemplateArgs =
10581             SecondMethod->getTemplateSpecializationArgs();
10582 
10583         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10584             (!FirstTemplateArgs && SecondTemplateArgs)) {
10585           ODRDiagError(FirstMethod->getLocation(),
10586                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
10587               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10588           ODRDiagNote(SecondMethod->getLocation(),
10589                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
10590               << SecondMethodType << SecondName
10591               << (SecondTemplateArgs != nullptr);
10592 
10593           Diagnosed = true;
10594           break;
10595         }
10596 
10597         if (FirstTemplateArgs && SecondTemplateArgs) {
10598           // Remove pack expansions from argument list.
10599           auto ExpandTemplateArgumentList =
10600               [](const TemplateArgumentList *TAL) {
10601                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10602                 for (const TemplateArgument &TA : TAL->asArray()) {
10603                   if (TA.getKind() != TemplateArgument::Pack) {
10604                     ExpandedList.push_back(&TA);
10605                     continue;
10606                   }
10607                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10608                     ExpandedList.push_back(&PackTA);
10609                   }
10610                 }
10611                 return ExpandedList;
10612               };
10613           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10614               ExpandTemplateArgumentList(FirstTemplateArgs);
10615           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10616               ExpandTemplateArgumentList(SecondTemplateArgs);
10617 
10618           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10619             ODRDiagError(FirstMethod->getLocation(),
10620                          FirstMethod->getSourceRange(),
10621                          MethodDifferentNumberTemplateArguments)
10622                 << FirstMethodType << FirstName
10623                 << (unsigned)FirstExpandedList.size();
10624             ODRDiagNote(SecondMethod->getLocation(),
10625                         SecondMethod->getSourceRange(),
10626                         MethodDifferentNumberTemplateArguments)
10627                 << SecondMethodType << SecondName
10628                 << (unsigned)SecondExpandedList.size();
10629 
10630             Diagnosed = true;
10631             break;
10632           }
10633 
10634           bool TemplateArgumentMismatch = false;
10635           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10636             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10637                                    &SecondTA = *SecondExpandedList[i];
10638             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10639                 ComputeTemplateArgumentODRHash(SecondTA)) {
10640               continue;
10641             }
10642 
10643             ODRDiagError(FirstMethod->getLocation(),
10644                          FirstMethod->getSourceRange(),
10645                          MethodDifferentTemplateArgument)
10646                 << FirstMethodType << FirstName << FirstTA << i + 1;
10647             ODRDiagNote(SecondMethod->getLocation(),
10648                         SecondMethod->getSourceRange(),
10649                         MethodDifferentTemplateArgument)
10650                 << SecondMethodType << SecondName << SecondTA << i + 1;
10651 
10652             TemplateArgumentMismatch = true;
10653             break;
10654           }
10655 
10656           if (TemplateArgumentMismatch) {
10657             Diagnosed = true;
10658             break;
10659           }
10660         }
10661 
10662         // Compute the hash of the method as if it has no body.
10663         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10664           Hash.clear();
10665           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10666           return Hash.CalculateHash();
10667         };
10668 
10669         // Compare the hash generated to the hash stored.  A difference means
10670         // that a body was present in the original source.  Due to merging,
10671         // the stardard way of detecting a body will not work.
10672         const bool HasFirstBody =
10673             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10674         const bool HasSecondBody =
10675             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10676 
10677         if (HasFirstBody != HasSecondBody) {
10678           ODRDiagError(FirstMethod->getLocation(),
10679                        FirstMethod->getSourceRange(), MethodSingleBody)
10680               << FirstMethodType << FirstName << HasFirstBody;
10681           ODRDiagNote(SecondMethod->getLocation(),
10682                       SecondMethod->getSourceRange(), MethodSingleBody)
10683               << SecondMethodType << SecondName << HasSecondBody;
10684           Diagnosed = true;
10685           break;
10686         }
10687 
10688         if (HasFirstBody && HasSecondBody) {
10689           ODRDiagError(FirstMethod->getLocation(),
10690                        FirstMethod->getSourceRange(), MethodDifferentBody)
10691               << FirstMethodType << FirstName;
10692           ODRDiagNote(SecondMethod->getLocation(),
10693                       SecondMethod->getSourceRange(), MethodDifferentBody)
10694               << SecondMethodType << SecondName;
10695           Diagnosed = true;
10696           break;
10697         }
10698 
10699         break;
10700       }
10701       case TypeAlias:
10702       case TypeDef: {
10703         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
10704         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
10705         auto FirstName = FirstTD->getDeclName();
10706         auto SecondName = SecondTD->getDeclName();
10707         if (FirstName != SecondName) {
10708           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10709                        TypedefName)
10710               << (FirstDiffType == TypeAlias) << FirstName;
10711           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10712                       TypedefName)
10713               << (FirstDiffType == TypeAlias) << SecondName;
10714           Diagnosed = true;
10715           break;
10716         }
10717 
10718         QualType FirstType = FirstTD->getUnderlyingType();
10719         QualType SecondType = SecondTD->getUnderlyingType();
10720         if (ComputeQualTypeODRHash(FirstType) !=
10721             ComputeQualTypeODRHash(SecondType)) {
10722           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10723                        TypedefType)
10724               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
10725           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10726                       TypedefType)
10727               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
10728           Diagnosed = true;
10729           break;
10730         }
10731         break;
10732       }
10733       case Var: {
10734         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
10735         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
10736         auto FirstName = FirstVD->getDeclName();
10737         auto SecondName = SecondVD->getDeclName();
10738         if (FirstName != SecondName) {
10739           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10740                        VarName)
10741               << FirstName;
10742           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10743                       VarName)
10744               << SecondName;
10745           Diagnosed = true;
10746           break;
10747         }
10748 
10749         QualType FirstType = FirstVD->getType();
10750         QualType SecondType = SecondVD->getType();
10751         if (ComputeQualTypeODRHash(FirstType) !=
10752                         ComputeQualTypeODRHash(SecondType)) {
10753           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10754                        VarType)
10755               << FirstName << FirstType;
10756           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10757                       VarType)
10758               << SecondName << SecondType;
10759           Diagnosed = true;
10760           break;
10761         }
10762 
10763         const Expr *FirstInit = FirstVD->getInit();
10764         const Expr *SecondInit = SecondVD->getInit();
10765         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10766           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10767                        VarSingleInitializer)
10768               << FirstName << (FirstInit == nullptr)
10769               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
10770           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10771                       VarSingleInitializer)
10772               << SecondName << (SecondInit == nullptr)
10773               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10774           Diagnosed = true;
10775           break;
10776         }
10777 
10778         if (FirstInit && SecondInit &&
10779             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10780           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10781                        VarDifferentInitializer)
10782               << FirstName << FirstInit->getSourceRange();
10783           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10784                       VarDifferentInitializer)
10785               << SecondName << SecondInit->getSourceRange();
10786           Diagnosed = true;
10787           break;
10788         }
10789 
10790         const bool FirstIsConstexpr = FirstVD->isConstexpr();
10791         const bool SecondIsConstexpr = SecondVD->isConstexpr();
10792         if (FirstIsConstexpr != SecondIsConstexpr) {
10793           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10794                        VarConstexpr)
10795               << FirstName << FirstIsConstexpr;
10796           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10797                       VarConstexpr)
10798               << SecondName << SecondIsConstexpr;
10799           Diagnosed = true;
10800           break;
10801         }
10802         break;
10803       }
10804       case Friend: {
10805         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10806         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10807 
10808         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10809         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10810 
10811         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10812         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10813 
10814         if (FirstND && SecondND) {
10815           ODRDiagError(FirstFriend->getFriendLoc(),
10816                        FirstFriend->getSourceRange(), FriendFunction)
10817               << FirstND;
10818           ODRDiagNote(SecondFriend->getFriendLoc(),
10819                       SecondFriend->getSourceRange(), FriendFunction)
10820               << SecondND;
10821 
10822           Diagnosed = true;
10823           break;
10824         }
10825 
10826         if (FirstTSI && SecondTSI) {
10827           QualType FirstFriendType = FirstTSI->getType();
10828           QualType SecondFriendType = SecondTSI->getType();
10829           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10830                  ComputeQualTypeODRHash(SecondFriendType));
10831           ODRDiagError(FirstFriend->getFriendLoc(),
10832                        FirstFriend->getSourceRange(), FriendType)
10833               << FirstFriendType;
10834           ODRDiagNote(SecondFriend->getFriendLoc(),
10835                       SecondFriend->getSourceRange(), FriendType)
10836               << SecondFriendType;
10837           Diagnosed = true;
10838           break;
10839         }
10840 
10841         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
10842                      FriendTypeFunction)
10843             << (FirstTSI == nullptr);
10844         ODRDiagNote(SecondFriend->getFriendLoc(),
10845                     SecondFriend->getSourceRange(), FriendTypeFunction)
10846             << (SecondTSI == nullptr);
10847 
10848         Diagnosed = true;
10849         break;
10850       }
10851       case FunctionTemplate: {
10852         FunctionTemplateDecl *FirstTemplate =
10853             cast<FunctionTemplateDecl>(FirstDecl);
10854         FunctionTemplateDecl *SecondTemplate =
10855             cast<FunctionTemplateDecl>(SecondDecl);
10856 
10857         TemplateParameterList *FirstTPL =
10858             FirstTemplate->getTemplateParameters();
10859         TemplateParameterList *SecondTPL =
10860             SecondTemplate->getTemplateParameters();
10861 
10862         if (FirstTPL->size() != SecondTPL->size()) {
10863           ODRDiagError(FirstTemplate->getLocation(),
10864                        FirstTemplate->getSourceRange(),
10865                        FunctionTemplateDifferentNumberParameters)
10866               << FirstTemplate << FirstTPL->size();
10867           ODRDiagNote(SecondTemplate->getLocation(),
10868                       SecondTemplate->getSourceRange(),
10869                       FunctionTemplateDifferentNumberParameters)
10870               << SecondTemplate  << SecondTPL->size();
10871 
10872           Diagnosed = true;
10873           break;
10874         }
10875 
10876         bool ParameterMismatch = false;
10877         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10878           NamedDecl *FirstParam = FirstTPL->getParam(i);
10879           NamedDecl *SecondParam = SecondTPL->getParam(i);
10880 
10881           if (FirstParam->getKind() != SecondParam->getKind()) {
10882             enum {
10883               TemplateTypeParameter,
10884               NonTypeTemplateParameter,
10885               TemplateTemplateParameter,
10886             };
10887             auto GetParamType = [](NamedDecl *D) {
10888               switch (D->getKind()) {
10889                 default:
10890                   llvm_unreachable("Unexpected template parameter type");
10891                 case Decl::TemplateTypeParm:
10892                   return TemplateTypeParameter;
10893                 case Decl::NonTypeTemplateParm:
10894                   return NonTypeTemplateParameter;
10895                 case Decl::TemplateTemplateParm:
10896                   return TemplateTemplateParameter;
10897               }
10898             };
10899 
10900             ODRDiagError(FirstTemplate->getLocation(),
10901                          FirstTemplate->getSourceRange(),
10902                          FunctionTemplateParameterDifferentKind)
10903                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10904             ODRDiagNote(SecondTemplate->getLocation(),
10905                         SecondTemplate->getSourceRange(),
10906                         FunctionTemplateParameterDifferentKind)
10907                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10908 
10909             ParameterMismatch = true;
10910             break;
10911           }
10912 
10913           if (FirstParam->getName() != SecondParam->getName()) {
10914             ODRDiagError(FirstTemplate->getLocation(),
10915                          FirstTemplate->getSourceRange(),
10916                          FunctionTemplateParameterName)
10917                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10918                 << FirstParam;
10919             ODRDiagNote(SecondTemplate->getLocation(),
10920                         SecondTemplate->getSourceRange(),
10921                         FunctionTemplateParameterName)
10922                 << SecondTemplate << (i + 1)
10923                 << (bool)SecondParam->getIdentifier() << SecondParam;
10924             ParameterMismatch = true;
10925             break;
10926           }
10927 
10928           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10929               isa<TemplateTypeParmDecl>(SecondParam)) {
10930             TemplateTypeParmDecl *FirstTTPD =
10931                 cast<TemplateTypeParmDecl>(FirstParam);
10932             TemplateTypeParmDecl *SecondTTPD =
10933                 cast<TemplateTypeParmDecl>(SecondParam);
10934             bool HasFirstDefaultArgument =
10935                 FirstTTPD->hasDefaultArgument() &&
10936                 !FirstTTPD->defaultArgumentWasInherited();
10937             bool HasSecondDefaultArgument =
10938                 SecondTTPD->hasDefaultArgument() &&
10939                 !SecondTTPD->defaultArgumentWasInherited();
10940             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10941               ODRDiagError(FirstTemplate->getLocation(),
10942                            FirstTemplate->getSourceRange(),
10943                            FunctionTemplateParameterSingleDefaultArgument)
10944                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10945               ODRDiagNote(SecondTemplate->getLocation(),
10946                           SecondTemplate->getSourceRange(),
10947                           FunctionTemplateParameterSingleDefaultArgument)
10948                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10949               ParameterMismatch = true;
10950               break;
10951             }
10952 
10953             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10954               QualType FirstType = FirstTTPD->getDefaultArgument();
10955               QualType SecondType = SecondTTPD->getDefaultArgument();
10956               if (ComputeQualTypeODRHash(FirstType) !=
10957                   ComputeQualTypeODRHash(SecondType)) {
10958                 ODRDiagError(FirstTemplate->getLocation(),
10959                              FirstTemplate->getSourceRange(),
10960                              FunctionTemplateParameterDifferentDefaultArgument)
10961                     << FirstTemplate << (i + 1) << FirstType;
10962                 ODRDiagNote(SecondTemplate->getLocation(),
10963                             SecondTemplate->getSourceRange(),
10964                             FunctionTemplateParameterDifferentDefaultArgument)
10965                     << SecondTemplate << (i + 1) << SecondType;
10966                 ParameterMismatch = true;
10967                 break;
10968               }
10969             }
10970 
10971             if (FirstTTPD->isParameterPack() !=
10972                 SecondTTPD->isParameterPack()) {
10973               ODRDiagError(FirstTemplate->getLocation(),
10974                            FirstTemplate->getSourceRange(),
10975                            FunctionTemplatePackParameter)
10976                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10977               ODRDiagNote(SecondTemplate->getLocation(),
10978                           SecondTemplate->getSourceRange(),
10979                           FunctionTemplatePackParameter)
10980                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10981               ParameterMismatch = true;
10982               break;
10983             }
10984           }
10985 
10986           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10987               isa<TemplateTemplateParmDecl>(SecondParam)) {
10988             TemplateTemplateParmDecl *FirstTTPD =
10989                 cast<TemplateTemplateParmDecl>(FirstParam);
10990             TemplateTemplateParmDecl *SecondTTPD =
10991                 cast<TemplateTemplateParmDecl>(SecondParam);
10992 
10993             TemplateParameterList *FirstTPL =
10994                 FirstTTPD->getTemplateParameters();
10995             TemplateParameterList *SecondTPL =
10996                 SecondTTPD->getTemplateParameters();
10997 
10998             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10999                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11000               ODRDiagError(FirstTemplate->getLocation(),
11001                            FirstTemplate->getSourceRange(),
11002                            FunctionTemplateParameterDifferentType)
11003                   << FirstTemplate << (i + 1);
11004               ODRDiagNote(SecondTemplate->getLocation(),
11005                           SecondTemplate->getSourceRange(),
11006                           FunctionTemplateParameterDifferentType)
11007                   << SecondTemplate << (i + 1);
11008               ParameterMismatch = true;
11009               break;
11010             }
11011 
11012             bool HasFirstDefaultArgument =
11013                 FirstTTPD->hasDefaultArgument() &&
11014                 !FirstTTPD->defaultArgumentWasInherited();
11015             bool HasSecondDefaultArgument =
11016                 SecondTTPD->hasDefaultArgument() &&
11017                 !SecondTTPD->defaultArgumentWasInherited();
11018             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11019               ODRDiagError(FirstTemplate->getLocation(),
11020                            FirstTemplate->getSourceRange(),
11021                            FunctionTemplateParameterSingleDefaultArgument)
11022                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11023               ODRDiagNote(SecondTemplate->getLocation(),
11024                           SecondTemplate->getSourceRange(),
11025                           FunctionTemplateParameterSingleDefaultArgument)
11026                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11027               ParameterMismatch = true;
11028               break;
11029             }
11030 
11031             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11032               TemplateArgument FirstTA =
11033                   FirstTTPD->getDefaultArgument().getArgument();
11034               TemplateArgument SecondTA =
11035                   SecondTTPD->getDefaultArgument().getArgument();
11036               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11037                   ComputeTemplateArgumentODRHash(SecondTA)) {
11038                 ODRDiagError(FirstTemplate->getLocation(),
11039                              FirstTemplate->getSourceRange(),
11040                              FunctionTemplateParameterDifferentDefaultArgument)
11041                     << FirstTemplate << (i + 1) << FirstTA;
11042                 ODRDiagNote(SecondTemplate->getLocation(),
11043                             SecondTemplate->getSourceRange(),
11044                             FunctionTemplateParameterDifferentDefaultArgument)
11045                     << SecondTemplate << (i + 1) << SecondTA;
11046                 ParameterMismatch = true;
11047                 break;
11048               }
11049             }
11050 
11051             if (FirstTTPD->isParameterPack() !=
11052                 SecondTTPD->isParameterPack()) {
11053               ODRDiagError(FirstTemplate->getLocation(),
11054                            FirstTemplate->getSourceRange(),
11055                            FunctionTemplatePackParameter)
11056                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11057               ODRDiagNote(SecondTemplate->getLocation(),
11058                           SecondTemplate->getSourceRange(),
11059                           FunctionTemplatePackParameter)
11060                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11061               ParameterMismatch = true;
11062               break;
11063             }
11064           }
11065 
11066           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11067               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11068             NonTypeTemplateParmDecl *FirstNTTPD =
11069                 cast<NonTypeTemplateParmDecl>(FirstParam);
11070             NonTypeTemplateParmDecl *SecondNTTPD =
11071                 cast<NonTypeTemplateParmDecl>(SecondParam);
11072 
11073             QualType FirstType = FirstNTTPD->getType();
11074             QualType SecondType = SecondNTTPD->getType();
11075             if (ComputeQualTypeODRHash(FirstType) !=
11076                 ComputeQualTypeODRHash(SecondType)) {
11077               ODRDiagError(FirstTemplate->getLocation(),
11078                            FirstTemplate->getSourceRange(),
11079                            FunctionTemplateParameterDifferentType)
11080                   << FirstTemplate << (i + 1);
11081               ODRDiagNote(SecondTemplate->getLocation(),
11082                           SecondTemplate->getSourceRange(),
11083                           FunctionTemplateParameterDifferentType)
11084                   << SecondTemplate << (i + 1);
11085               ParameterMismatch = true;
11086               break;
11087             }
11088 
11089             bool HasFirstDefaultArgument =
11090                 FirstNTTPD->hasDefaultArgument() &&
11091                 !FirstNTTPD->defaultArgumentWasInherited();
11092             bool HasSecondDefaultArgument =
11093                 SecondNTTPD->hasDefaultArgument() &&
11094                 !SecondNTTPD->defaultArgumentWasInherited();
11095             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11096               ODRDiagError(FirstTemplate->getLocation(),
11097                            FirstTemplate->getSourceRange(),
11098                            FunctionTemplateParameterSingleDefaultArgument)
11099                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11100               ODRDiagNote(SecondTemplate->getLocation(),
11101                           SecondTemplate->getSourceRange(),
11102                           FunctionTemplateParameterSingleDefaultArgument)
11103                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11104               ParameterMismatch = true;
11105               break;
11106             }
11107 
11108             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11109               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11110               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11111               if (ComputeODRHash(FirstDefaultArgument) !=
11112                   ComputeODRHash(SecondDefaultArgument)) {
11113                 ODRDiagError(FirstTemplate->getLocation(),
11114                              FirstTemplate->getSourceRange(),
11115                              FunctionTemplateParameterDifferentDefaultArgument)
11116                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11117                 ODRDiagNote(SecondTemplate->getLocation(),
11118                             SecondTemplate->getSourceRange(),
11119                             FunctionTemplateParameterDifferentDefaultArgument)
11120                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11121                 ParameterMismatch = true;
11122                 break;
11123               }
11124             }
11125 
11126             if (FirstNTTPD->isParameterPack() !=
11127                 SecondNTTPD->isParameterPack()) {
11128               ODRDiagError(FirstTemplate->getLocation(),
11129                            FirstTemplate->getSourceRange(),
11130                            FunctionTemplatePackParameter)
11131                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11132               ODRDiagNote(SecondTemplate->getLocation(),
11133                           SecondTemplate->getSourceRange(),
11134                           FunctionTemplatePackParameter)
11135                   << SecondTemplate << (i + 1)
11136                   << SecondNTTPD->isParameterPack();
11137               ParameterMismatch = true;
11138               break;
11139             }
11140           }
11141         }
11142 
11143         if (ParameterMismatch) {
11144           Diagnosed = true;
11145           break;
11146         }
11147 
11148         break;
11149       }
11150       }
11151 
11152       if (Diagnosed)
11153         continue;
11154 
11155       Diag(FirstDecl->getLocation(),
11156            diag::err_module_odr_violation_mismatch_decl_unknown)
11157           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11158           << FirstDecl->getSourceRange();
11159       Diag(SecondDecl->getLocation(),
11160            diag::note_module_odr_violation_mismatch_decl_unknown)
11161           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11162       Diagnosed = true;
11163     }
11164 
11165     if (!Diagnosed) {
11166       // All definitions are updates to the same declaration. This happens if a
11167       // module instantiates the declaration of a class template specialization
11168       // and two or more other modules instantiate its definition.
11169       //
11170       // FIXME: Indicate which modules had instantiations of this definition.
11171       // FIXME: How can this even happen?
11172       Diag(Merge.first->getLocation(),
11173            diag::err_module_odr_violation_different_instantiations)
11174         << Merge.first;
11175     }
11176   }
11177 
11178   // Issue ODR failures diagnostics for functions.
11179   for (auto &Merge : FunctionOdrMergeFailures) {
11180     enum ODRFunctionDifference {
11181       ReturnType,
11182       ParameterName,
11183       ParameterType,
11184       ParameterSingleDefaultArgument,
11185       ParameterDifferentDefaultArgument,
11186       FunctionBody,
11187     };
11188 
11189     FunctionDecl *FirstFunction = Merge.first;
11190     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11191 
11192     bool Diagnosed = false;
11193     for (auto &SecondFunction : Merge.second) {
11194 
11195       if (FirstFunction == SecondFunction)
11196         continue;
11197 
11198       std::string SecondModule =
11199           getOwningModuleNameForDiagnostic(SecondFunction);
11200 
11201       auto ODRDiagError = [FirstFunction, &FirstModule,
11202                            this](SourceLocation Loc, SourceRange Range,
11203                                  ODRFunctionDifference DiffType) {
11204         return Diag(Loc, diag::err_module_odr_violation_function)
11205                << FirstFunction << FirstModule.empty() << FirstModule << Range
11206                << DiffType;
11207       };
11208       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11209                                                SourceRange Range,
11210                                                ODRFunctionDifference DiffType) {
11211         return Diag(Loc, diag::note_module_odr_violation_function)
11212                << SecondModule << Range << DiffType;
11213       };
11214 
11215       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11216           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11217         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11218                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11219             << FirstFunction->getReturnType();
11220         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11221                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11222             << SecondFunction->getReturnType();
11223         Diagnosed = true;
11224         break;
11225       }
11226 
11227       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11228              "Merged functions with different number of parameters");
11229 
11230       auto ParamSize = FirstFunction->param_size();
11231       bool ParameterMismatch = false;
11232       for (unsigned I = 0; I < ParamSize; ++I) {
11233         auto *FirstParam = FirstFunction->getParamDecl(I);
11234         auto *SecondParam = SecondFunction->getParamDecl(I);
11235 
11236         assert(getContext().hasSameType(FirstParam->getType(),
11237                                       SecondParam->getType()) &&
11238                "Merged function has different parameter types.");
11239 
11240         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11241           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11242                        ParameterName)
11243               << I + 1 << FirstParam->getDeclName();
11244           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11245                       ParameterName)
11246               << I + 1 << SecondParam->getDeclName();
11247           ParameterMismatch = true;
11248           break;
11249         };
11250 
11251         QualType FirstParamType = FirstParam->getType();
11252         QualType SecondParamType = SecondParam->getType();
11253         if (FirstParamType != SecondParamType &&
11254             ComputeQualTypeODRHash(FirstParamType) !=
11255                 ComputeQualTypeODRHash(SecondParamType)) {
11256           if (const DecayedType *ParamDecayedType =
11257                   FirstParamType->getAs<DecayedType>()) {
11258             ODRDiagError(FirstParam->getLocation(),
11259                          FirstParam->getSourceRange(), ParameterType)
11260                 << (I + 1) << FirstParamType << true
11261                 << ParamDecayedType->getOriginalType();
11262           } else {
11263             ODRDiagError(FirstParam->getLocation(),
11264                          FirstParam->getSourceRange(), ParameterType)
11265                 << (I + 1) << FirstParamType << false;
11266           }
11267 
11268           if (const DecayedType *ParamDecayedType =
11269                   SecondParamType->getAs<DecayedType>()) {
11270             ODRDiagNote(SecondParam->getLocation(),
11271                         SecondParam->getSourceRange(), ParameterType)
11272                 << (I + 1) << SecondParamType << true
11273                 << ParamDecayedType->getOriginalType();
11274           } else {
11275             ODRDiagNote(SecondParam->getLocation(),
11276                         SecondParam->getSourceRange(), ParameterType)
11277                 << (I + 1) << SecondParamType << false;
11278           }
11279           ParameterMismatch = true;
11280           break;
11281         }
11282 
11283         const Expr *FirstInit = FirstParam->getInit();
11284         const Expr *SecondInit = SecondParam->getInit();
11285         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11286           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11287                        ParameterSingleDefaultArgument)
11288               << (I + 1) << (FirstInit == nullptr)
11289               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11290           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11291                       ParameterSingleDefaultArgument)
11292               << (I + 1) << (SecondInit == nullptr)
11293               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11294           ParameterMismatch = true;
11295           break;
11296         }
11297 
11298         if (FirstInit && SecondInit &&
11299             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11300           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11301                        ParameterDifferentDefaultArgument)
11302               << (I + 1) << FirstInit->getSourceRange();
11303           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11304                       ParameterDifferentDefaultArgument)
11305               << (I + 1) << SecondInit->getSourceRange();
11306           ParameterMismatch = true;
11307           break;
11308         }
11309 
11310         assert(ComputeSubDeclODRHash(FirstParam) ==
11311                    ComputeSubDeclODRHash(SecondParam) &&
11312                "Undiagnosed parameter difference.");
11313       }
11314 
11315       if (ParameterMismatch) {
11316         Diagnosed = true;
11317         break;
11318       }
11319 
11320       // If no error has been generated before now, assume the problem is in
11321       // the body and generate a message.
11322       ODRDiagError(FirstFunction->getLocation(),
11323                    FirstFunction->getSourceRange(), FunctionBody);
11324       ODRDiagNote(SecondFunction->getLocation(),
11325                   SecondFunction->getSourceRange(), FunctionBody);
11326       Diagnosed = true;
11327       break;
11328     }
11329     (void)Diagnosed;
11330     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11331   }
11332 
11333   // Issue ODR failures diagnostics for enums.
11334   for (auto &Merge : EnumOdrMergeFailures) {
11335     enum ODREnumDifference {
11336       SingleScopedEnum,
11337       EnumTagKeywordMismatch,
11338       SingleSpecifiedType,
11339       DifferentSpecifiedTypes,
11340       DifferentNumberEnumConstants,
11341       EnumConstantName,
11342       EnumConstantSingleInitilizer,
11343       EnumConstantDifferentInitilizer,
11344     };
11345 
11346     // If we've already pointed out a specific problem with this enum, don't
11347     // bother issuing a general "something's different" diagnostic.
11348     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11349       continue;
11350 
11351     EnumDecl *FirstEnum = Merge.first;
11352     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11353 
11354     using DeclHashes =
11355         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11356     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11357                               DeclHashes &Hashes, EnumDecl *Enum) {
11358       for (auto *D : Enum->decls()) {
11359         // Due to decl merging, the first EnumDecl is the parent of
11360         // Decls in both records.
11361         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11362           continue;
11363         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11364         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11365                             ComputeSubDeclODRHash(D));
11366       }
11367     };
11368     DeclHashes FirstHashes;
11369     PopulateHashes(FirstHashes, FirstEnum);
11370     bool Diagnosed = false;
11371     for (auto &SecondEnum : Merge.second) {
11372 
11373       if (FirstEnum == SecondEnum)
11374         continue;
11375 
11376       std::string SecondModule =
11377           getOwningModuleNameForDiagnostic(SecondEnum);
11378 
11379       auto ODRDiagError = [FirstEnum, &FirstModule,
11380                            this](SourceLocation Loc, SourceRange Range,
11381                                  ODREnumDifference DiffType) {
11382         return Diag(Loc, diag::err_module_odr_violation_enum)
11383                << FirstEnum << FirstModule.empty() << FirstModule << Range
11384                << DiffType;
11385       };
11386       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11387                                                SourceRange Range,
11388                                                ODREnumDifference DiffType) {
11389         return Diag(Loc, diag::note_module_odr_violation_enum)
11390                << SecondModule << Range << DiffType;
11391       };
11392 
11393       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11394         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11395                      SingleScopedEnum)
11396             << FirstEnum->isScoped();
11397         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11398                     SingleScopedEnum)
11399             << SecondEnum->isScoped();
11400         Diagnosed = true;
11401         continue;
11402       }
11403 
11404       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11405         if (FirstEnum->isScopedUsingClassTag() !=
11406             SecondEnum->isScopedUsingClassTag()) {
11407           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11408                        EnumTagKeywordMismatch)
11409               << FirstEnum->isScopedUsingClassTag();
11410           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11411                       EnumTagKeywordMismatch)
11412               << SecondEnum->isScopedUsingClassTag();
11413           Diagnosed = true;
11414           continue;
11415         }
11416       }
11417 
11418       QualType FirstUnderlyingType =
11419           FirstEnum->getIntegerTypeSourceInfo()
11420               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11421               : QualType();
11422       QualType SecondUnderlyingType =
11423           SecondEnum->getIntegerTypeSourceInfo()
11424               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11425               : QualType();
11426       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11427           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11428                        SingleSpecifiedType)
11429               << !FirstUnderlyingType.isNull();
11430           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11431                       SingleSpecifiedType)
11432               << !SecondUnderlyingType.isNull();
11433           Diagnosed = true;
11434           continue;
11435       }
11436 
11437       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11438         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11439             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11440           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11441                        DifferentSpecifiedTypes)
11442               << FirstUnderlyingType;
11443           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11444                       DifferentSpecifiedTypes)
11445               << SecondUnderlyingType;
11446           Diagnosed = true;
11447           continue;
11448         }
11449       }
11450 
11451       DeclHashes SecondHashes;
11452       PopulateHashes(SecondHashes, SecondEnum);
11453 
11454       if (FirstHashes.size() != SecondHashes.size()) {
11455         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11456                      DifferentNumberEnumConstants)
11457             << (int)FirstHashes.size();
11458         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11459                     DifferentNumberEnumConstants)
11460             << (int)SecondHashes.size();
11461         Diagnosed = true;
11462         continue;
11463       }
11464 
11465       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11466         if (FirstHashes[I].second == SecondHashes[I].second)
11467           continue;
11468         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11469         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11470 
11471         if (FirstEnumConstant->getDeclName() !=
11472             SecondEnumConstant->getDeclName()) {
11473 
11474           ODRDiagError(FirstEnumConstant->getLocation(),
11475                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11476               << I + 1 << FirstEnumConstant;
11477           ODRDiagNote(SecondEnumConstant->getLocation(),
11478                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11479               << I + 1 << SecondEnumConstant;
11480           Diagnosed = true;
11481           break;
11482         }
11483 
11484         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11485         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11486         if (!FirstInit && !SecondInit)
11487           continue;
11488 
11489         if (!FirstInit || !SecondInit) {
11490           ODRDiagError(FirstEnumConstant->getLocation(),
11491                        FirstEnumConstant->getSourceRange(),
11492                        EnumConstantSingleInitilizer)
11493               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11494           ODRDiagNote(SecondEnumConstant->getLocation(),
11495                       SecondEnumConstant->getSourceRange(),
11496                       EnumConstantSingleInitilizer)
11497               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11498           Diagnosed = true;
11499           break;
11500         }
11501 
11502         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11503           ODRDiagError(FirstEnumConstant->getLocation(),
11504                        FirstEnumConstant->getSourceRange(),
11505                        EnumConstantDifferentInitilizer)
11506               << I + 1 << FirstEnumConstant;
11507           ODRDiagNote(SecondEnumConstant->getLocation(),
11508                       SecondEnumConstant->getSourceRange(),
11509                       EnumConstantDifferentInitilizer)
11510               << I + 1 << SecondEnumConstant;
11511           Diagnosed = true;
11512           break;
11513         }
11514       }
11515     }
11516 
11517     (void)Diagnosed;
11518     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11519   }
11520 }
11521 
11522 void ASTReader::StartedDeserializing() {
11523   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11524     ReadTimer->startTimer();
11525 }
11526 
11527 void ASTReader::FinishedDeserializing() {
11528   assert(NumCurrentElementsDeserializing &&
11529          "FinishedDeserializing not paired with StartedDeserializing");
11530   if (NumCurrentElementsDeserializing == 1) {
11531     // We decrease NumCurrentElementsDeserializing only after pending actions
11532     // are finished, to avoid recursively re-calling finishPendingActions().
11533     finishPendingActions();
11534   }
11535   --NumCurrentElementsDeserializing;
11536 
11537   if (NumCurrentElementsDeserializing == 0) {
11538     // Propagate exception specification and deduced type updates along
11539     // redeclaration chains.
11540     //
11541     // We do this now rather than in finishPendingActions because we want to
11542     // be able to walk the complete redeclaration chains of the updated decls.
11543     while (!PendingExceptionSpecUpdates.empty() ||
11544            !PendingDeducedTypeUpdates.empty()) {
11545       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11546       PendingExceptionSpecUpdates.clear();
11547       for (auto Update : ESUpdates) {
11548         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11549         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11550         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11551         if (auto *Listener = getContext().getASTMutationListener())
11552           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11553         for (auto *Redecl : Update.second->redecls())
11554           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11555       }
11556 
11557       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11558       PendingDeducedTypeUpdates.clear();
11559       for (auto Update : DTUpdates) {
11560         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11561         // FIXME: If the return type is already deduced, check that it matches.
11562         getContext().adjustDeducedFunctionResultType(Update.first,
11563                                                      Update.second);
11564       }
11565     }
11566 
11567     if (ReadTimer)
11568       ReadTimer->stopTimer();
11569 
11570     diagnoseOdrViolations();
11571 
11572     // We are not in recursive loading, so it's safe to pass the "interesting"
11573     // decls to the consumer.
11574     if (Consumer)
11575       PassInterestingDeclsToConsumer();
11576   }
11577 }
11578 
11579 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11580   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11581     // Remove any fake results before adding any real ones.
11582     auto It = PendingFakeLookupResults.find(II);
11583     if (It != PendingFakeLookupResults.end()) {
11584       for (auto *ND : It->second)
11585         SemaObj->IdResolver.RemoveDecl(ND);
11586       // FIXME: this works around module+PCH performance issue.
11587       // Rather than erase the result from the map, which is O(n), just clear
11588       // the vector of NamedDecls.
11589       It->second.clear();
11590     }
11591   }
11592 
11593   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11594     SemaObj->TUScope->AddDecl(D);
11595   } else if (SemaObj->TUScope) {
11596     // Adding the decl to IdResolver may have failed because it was already in
11597     // (even though it was not added in scope). If it is already in, make sure
11598     // it gets in the scope as well.
11599     if (std::find(SemaObj->IdResolver.begin(Name),
11600                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11601       SemaObj->TUScope->AddDecl(D);
11602   }
11603 }
11604 
11605 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
11606                      const PCHContainerReader &PCHContainerRdr,
11607                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11608                      StringRef isysroot, bool DisableValidation,
11609                      bool AllowASTWithCompilerErrors,
11610                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11611                      bool UseGlobalIndex,
11612                      std::unique_ptr<llvm::Timer> ReadTimer)
11613     : Listener(DisableValidation
11614                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11615                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11616       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11617       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11618       ContextObj(Context),
11619       ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr,
11620                 PP.getHeaderSearchInfo()),
11621       PCMCache(PP.getPCMCache()), DummyIdResolver(PP),
11622       ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11623       DisableValidation(DisableValidation),
11624       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11625       AllowConfigurationMismatch(AllowConfigurationMismatch),
11626       ValidateSystemInputs(ValidateSystemInputs),
11627       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11628   SourceMgr.setExternalSLocEntrySource(this);
11629 
11630   for (const auto &Ext : Extensions) {
11631     auto BlockName = Ext->getExtensionMetadata().BlockName;
11632     auto Known = ModuleFileExtensions.find(BlockName);
11633     if (Known != ModuleFileExtensions.end()) {
11634       Diags.Report(diag::warn_duplicate_module_file_extension)
11635         << BlockName;
11636       continue;
11637     }
11638 
11639     ModuleFileExtensions.insert({BlockName, Ext});
11640   }
11641 }
11642 
11643 ASTReader::~ASTReader() {
11644   if (OwnsDeserializationListener)
11645     delete DeserializationListener;
11646 }
11647 
11648 IdentifierResolver &ASTReader::getIdResolver() {
11649   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11650 }
11651 
11652 unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11653                                      unsigned AbbrevID) {
11654   Idx = 0;
11655   Record.clear();
11656   return Cursor.readRecord(AbbrevID, Record);
11657 }
11658 //===----------------------------------------------------------------------===//
11659 //// OMPClauseReader implementation
11660 ////===----------------------------------------------------------------------===//
11661 
11662 OMPClause *OMPClauseReader::readClause() {
11663   OMPClause *C;
11664   switch (Record.readInt()) {
11665   case OMPC_if:
11666     C = new (Context) OMPIfClause();
11667     break;
11668   case OMPC_final:
11669     C = new (Context) OMPFinalClause();
11670     break;
11671   case OMPC_num_threads:
11672     C = new (Context) OMPNumThreadsClause();
11673     break;
11674   case OMPC_safelen:
11675     C = new (Context) OMPSafelenClause();
11676     break;
11677   case OMPC_simdlen:
11678     C = new (Context) OMPSimdlenClause();
11679     break;
11680   case OMPC_collapse:
11681     C = new (Context) OMPCollapseClause();
11682     break;
11683   case OMPC_default:
11684     C = new (Context) OMPDefaultClause();
11685     break;
11686   case OMPC_proc_bind:
11687     C = new (Context) OMPProcBindClause();
11688     break;
11689   case OMPC_schedule:
11690     C = new (Context) OMPScheduleClause();
11691     break;
11692   case OMPC_ordered:
11693     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11694     break;
11695   case OMPC_nowait:
11696     C = new (Context) OMPNowaitClause();
11697     break;
11698   case OMPC_untied:
11699     C = new (Context) OMPUntiedClause();
11700     break;
11701   case OMPC_mergeable:
11702     C = new (Context) OMPMergeableClause();
11703     break;
11704   case OMPC_read:
11705     C = new (Context) OMPReadClause();
11706     break;
11707   case OMPC_write:
11708     C = new (Context) OMPWriteClause();
11709     break;
11710   case OMPC_update:
11711     C = new (Context) OMPUpdateClause();
11712     break;
11713   case OMPC_capture:
11714     C = new (Context) OMPCaptureClause();
11715     break;
11716   case OMPC_seq_cst:
11717     C = new (Context) OMPSeqCstClause();
11718     break;
11719   case OMPC_threads:
11720     C = new (Context) OMPThreadsClause();
11721     break;
11722   case OMPC_simd:
11723     C = new (Context) OMPSIMDClause();
11724     break;
11725   case OMPC_nogroup:
11726     C = new (Context) OMPNogroupClause();
11727     break;
11728   case OMPC_unified_address:
11729     C = new (Context) OMPUnifiedAddressClause();
11730     break;
11731   case OMPC_unified_shared_memory:
11732     C = new (Context) OMPUnifiedSharedMemoryClause();
11733     break;
11734   case OMPC_reverse_offload:
11735     C = new (Context) OMPReverseOffloadClause();
11736     break;
11737   case OMPC_dynamic_allocators:
11738     C = new (Context) OMPDynamicAllocatorsClause();
11739     break;
11740   case OMPC_atomic_default_mem_order:
11741     C = new (Context) OMPAtomicDefaultMemOrderClause();
11742     break;
11743  case OMPC_private:
11744     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11745     break;
11746   case OMPC_firstprivate:
11747     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11748     break;
11749   case OMPC_lastprivate:
11750     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11751     break;
11752   case OMPC_shared:
11753     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11754     break;
11755   case OMPC_reduction:
11756     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11757     break;
11758   case OMPC_task_reduction:
11759     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11760     break;
11761   case OMPC_in_reduction:
11762     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11763     break;
11764   case OMPC_linear:
11765     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11766     break;
11767   case OMPC_aligned:
11768     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11769     break;
11770   case OMPC_copyin:
11771     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11772     break;
11773   case OMPC_copyprivate:
11774     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11775     break;
11776   case OMPC_flush:
11777     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11778     break;
11779   case OMPC_depend: {
11780     unsigned NumVars = Record.readInt();
11781     unsigned NumLoops = Record.readInt();
11782     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11783     break;
11784   }
11785   case OMPC_device:
11786     C = new (Context) OMPDeviceClause();
11787     break;
11788   case OMPC_map: {
11789     unsigned NumVars = Record.readInt();
11790     unsigned NumDeclarations = Record.readInt();
11791     unsigned NumLists = Record.readInt();
11792     unsigned NumComponents = Record.readInt();
11793     C = OMPMapClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
11794                                   NumComponents);
11795     break;
11796   }
11797   case OMPC_num_teams:
11798     C = new (Context) OMPNumTeamsClause();
11799     break;
11800   case OMPC_thread_limit:
11801     C = new (Context) OMPThreadLimitClause();
11802     break;
11803   case OMPC_priority:
11804     C = new (Context) OMPPriorityClause();
11805     break;
11806   case OMPC_grainsize:
11807     C = new (Context) OMPGrainsizeClause();
11808     break;
11809   case OMPC_num_tasks:
11810     C = new (Context) OMPNumTasksClause();
11811     break;
11812   case OMPC_hint:
11813     C = new (Context) OMPHintClause();
11814     break;
11815   case OMPC_dist_schedule:
11816     C = new (Context) OMPDistScheduleClause();
11817     break;
11818   case OMPC_defaultmap:
11819     C = new (Context) OMPDefaultmapClause();
11820     break;
11821   case OMPC_to: {
11822     unsigned NumVars = Record.readInt();
11823     unsigned NumDeclarations = Record.readInt();
11824     unsigned NumLists = Record.readInt();
11825     unsigned NumComponents = Record.readInt();
11826     C = OMPToClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
11827                                  NumComponents);
11828     break;
11829   }
11830   case OMPC_from: {
11831     unsigned NumVars = Record.readInt();
11832     unsigned NumDeclarations = Record.readInt();
11833     unsigned NumLists = Record.readInt();
11834     unsigned NumComponents = Record.readInt();
11835     C = OMPFromClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists,
11836                                    NumComponents);
11837     break;
11838   }
11839   case OMPC_use_device_ptr: {
11840     unsigned NumVars = Record.readInt();
11841     unsigned NumDeclarations = Record.readInt();
11842     unsigned NumLists = Record.readInt();
11843     unsigned NumComponents = Record.readInt();
11844     C = OMPUseDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations,
11845                                            NumLists, NumComponents);
11846     break;
11847   }
11848   case OMPC_is_device_ptr: {
11849     unsigned NumVars = Record.readInt();
11850     unsigned NumDeclarations = Record.readInt();
11851     unsigned NumLists = Record.readInt();
11852     unsigned NumComponents = Record.readInt();
11853     C = OMPIsDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations,
11854                                           NumLists, NumComponents);
11855     break;
11856   }
11857   }
11858   Visit(C);
11859   C->setLocStart(Record.readSourceLocation());
11860   C->setLocEnd(Record.readSourceLocation());
11861 
11862   return C;
11863 }
11864 
11865 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11866   C->setPreInitStmt(Record.readSubStmt(),
11867                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11868 }
11869 
11870 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11871   VisitOMPClauseWithPreInit(C);
11872   C->setPostUpdateExpr(Record.readSubExpr());
11873 }
11874 
11875 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11876   VisitOMPClauseWithPreInit(C);
11877   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11878   C->setNameModifierLoc(Record.readSourceLocation());
11879   C->setColonLoc(Record.readSourceLocation());
11880   C->setCondition(Record.readSubExpr());
11881   C->setLParenLoc(Record.readSourceLocation());
11882 }
11883 
11884 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11885   C->setCondition(Record.readSubExpr());
11886   C->setLParenLoc(Record.readSourceLocation());
11887 }
11888 
11889 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11890   VisitOMPClauseWithPreInit(C);
11891   C->setNumThreads(Record.readSubExpr());
11892   C->setLParenLoc(Record.readSourceLocation());
11893 }
11894 
11895 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11896   C->setSafelen(Record.readSubExpr());
11897   C->setLParenLoc(Record.readSourceLocation());
11898 }
11899 
11900 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11901   C->setSimdlen(Record.readSubExpr());
11902   C->setLParenLoc(Record.readSourceLocation());
11903 }
11904 
11905 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11906   C->setNumForLoops(Record.readSubExpr());
11907   C->setLParenLoc(Record.readSourceLocation());
11908 }
11909 
11910 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11911   C->setDefaultKind(
11912        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
11913   C->setLParenLoc(Record.readSourceLocation());
11914   C->setDefaultKindKwLoc(Record.readSourceLocation());
11915 }
11916 
11917 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11918   C->setProcBindKind(
11919        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
11920   C->setLParenLoc(Record.readSourceLocation());
11921   C->setProcBindKindKwLoc(Record.readSourceLocation());
11922 }
11923 
11924 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11925   VisitOMPClauseWithPreInit(C);
11926   C->setScheduleKind(
11927        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11928   C->setFirstScheduleModifier(
11929       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11930   C->setSecondScheduleModifier(
11931       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11932   C->setChunkSize(Record.readSubExpr());
11933   C->setLParenLoc(Record.readSourceLocation());
11934   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11935   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11936   C->setScheduleKindLoc(Record.readSourceLocation());
11937   C->setCommaLoc(Record.readSourceLocation());
11938 }
11939 
11940 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11941   C->setNumForLoops(Record.readSubExpr());
11942   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11943     C->setLoopNumIterations(I, Record.readSubExpr());
11944   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11945     C->setLoopCounter(I, Record.readSubExpr());
11946   C->setLParenLoc(Record.readSourceLocation());
11947 }
11948 
11949 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11950 
11951 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11952 
11953 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11954 
11955 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11956 
11957 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11958 
11959 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
11960 
11961 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11962 
11963 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11964 
11965 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11966 
11967 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11968 
11969 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11970 
11971 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11972 
11973 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11974     OMPUnifiedSharedMemoryClause *) {}
11975 
11976 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11977 
11978 void
11979 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11980 }
11981 
11982 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11983     OMPAtomicDefaultMemOrderClause *C) {
11984   C->setAtomicDefaultMemOrderKind(
11985       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11986   C->setLParenLoc(Record.readSourceLocation());
11987   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11988 }
11989 
11990 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11991   C->setLParenLoc(Record.readSourceLocation());
11992   unsigned NumVars = C->varlist_size();
11993   SmallVector<Expr *, 16> Vars;
11994   Vars.reserve(NumVars);
11995   for (unsigned i = 0; i != NumVars; ++i)
11996     Vars.push_back(Record.readSubExpr());
11997   C->setVarRefs(Vars);
11998   Vars.clear();
11999   for (unsigned i = 0; i != NumVars; ++i)
12000     Vars.push_back(Record.readSubExpr());
12001   C->setPrivateCopies(Vars);
12002 }
12003 
12004 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12005   VisitOMPClauseWithPreInit(C);
12006   C->setLParenLoc(Record.readSourceLocation());
12007   unsigned NumVars = C->varlist_size();
12008   SmallVector<Expr *, 16> Vars;
12009   Vars.reserve(NumVars);
12010   for (unsigned i = 0; i != NumVars; ++i)
12011     Vars.push_back(Record.readSubExpr());
12012   C->setVarRefs(Vars);
12013   Vars.clear();
12014   for (unsigned i = 0; i != NumVars; ++i)
12015     Vars.push_back(Record.readSubExpr());
12016   C->setPrivateCopies(Vars);
12017   Vars.clear();
12018   for (unsigned i = 0; i != NumVars; ++i)
12019     Vars.push_back(Record.readSubExpr());
12020   C->setInits(Vars);
12021 }
12022 
12023 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12024   VisitOMPClauseWithPostUpdate(C);
12025   C->setLParenLoc(Record.readSourceLocation());
12026   unsigned NumVars = C->varlist_size();
12027   SmallVector<Expr *, 16> Vars;
12028   Vars.reserve(NumVars);
12029   for (unsigned i = 0; i != NumVars; ++i)
12030     Vars.push_back(Record.readSubExpr());
12031   C->setVarRefs(Vars);
12032   Vars.clear();
12033   for (unsigned i = 0; i != NumVars; ++i)
12034     Vars.push_back(Record.readSubExpr());
12035   C->setPrivateCopies(Vars);
12036   Vars.clear();
12037   for (unsigned i = 0; i != NumVars; ++i)
12038     Vars.push_back(Record.readSubExpr());
12039   C->setSourceExprs(Vars);
12040   Vars.clear();
12041   for (unsigned i = 0; i != NumVars; ++i)
12042     Vars.push_back(Record.readSubExpr());
12043   C->setDestinationExprs(Vars);
12044   Vars.clear();
12045   for (unsigned i = 0; i != NumVars; ++i)
12046     Vars.push_back(Record.readSubExpr());
12047   C->setAssignmentOps(Vars);
12048 }
12049 
12050 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12051   C->setLParenLoc(Record.readSourceLocation());
12052   unsigned NumVars = C->varlist_size();
12053   SmallVector<Expr *, 16> Vars;
12054   Vars.reserve(NumVars);
12055   for (unsigned i = 0; i != NumVars; ++i)
12056     Vars.push_back(Record.readSubExpr());
12057   C->setVarRefs(Vars);
12058 }
12059 
12060 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12061   VisitOMPClauseWithPostUpdate(C);
12062   C->setLParenLoc(Record.readSourceLocation());
12063   C->setColonLoc(Record.readSourceLocation());
12064   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12065   DeclarationNameInfo DNI;
12066   Record.readDeclarationNameInfo(DNI);
12067   C->setQualifierLoc(NNSL);
12068   C->setNameInfo(DNI);
12069 
12070   unsigned NumVars = C->varlist_size();
12071   SmallVector<Expr *, 16> Vars;
12072   Vars.reserve(NumVars);
12073   for (unsigned i = 0; i != NumVars; ++i)
12074     Vars.push_back(Record.readSubExpr());
12075   C->setVarRefs(Vars);
12076   Vars.clear();
12077   for (unsigned i = 0; i != NumVars; ++i)
12078     Vars.push_back(Record.readSubExpr());
12079   C->setPrivates(Vars);
12080   Vars.clear();
12081   for (unsigned i = 0; i != NumVars; ++i)
12082     Vars.push_back(Record.readSubExpr());
12083   C->setLHSExprs(Vars);
12084   Vars.clear();
12085   for (unsigned i = 0; i != NumVars; ++i)
12086     Vars.push_back(Record.readSubExpr());
12087   C->setRHSExprs(Vars);
12088   Vars.clear();
12089   for (unsigned i = 0; i != NumVars; ++i)
12090     Vars.push_back(Record.readSubExpr());
12091   C->setReductionOps(Vars);
12092 }
12093 
12094 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12095   VisitOMPClauseWithPostUpdate(C);
12096   C->setLParenLoc(Record.readSourceLocation());
12097   C->setColonLoc(Record.readSourceLocation());
12098   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12099   DeclarationNameInfo DNI;
12100   Record.readDeclarationNameInfo(DNI);
12101   C->setQualifierLoc(NNSL);
12102   C->setNameInfo(DNI);
12103 
12104   unsigned NumVars = C->varlist_size();
12105   SmallVector<Expr *, 16> Vars;
12106   Vars.reserve(NumVars);
12107   for (unsigned I = 0; I != NumVars; ++I)
12108     Vars.push_back(Record.readSubExpr());
12109   C->setVarRefs(Vars);
12110   Vars.clear();
12111   for (unsigned I = 0; I != NumVars; ++I)
12112     Vars.push_back(Record.readSubExpr());
12113   C->setPrivates(Vars);
12114   Vars.clear();
12115   for (unsigned I = 0; I != NumVars; ++I)
12116     Vars.push_back(Record.readSubExpr());
12117   C->setLHSExprs(Vars);
12118   Vars.clear();
12119   for (unsigned I = 0; I != NumVars; ++I)
12120     Vars.push_back(Record.readSubExpr());
12121   C->setRHSExprs(Vars);
12122   Vars.clear();
12123   for (unsigned I = 0; I != NumVars; ++I)
12124     Vars.push_back(Record.readSubExpr());
12125   C->setReductionOps(Vars);
12126 }
12127 
12128 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12129   VisitOMPClauseWithPostUpdate(C);
12130   C->setLParenLoc(Record.readSourceLocation());
12131   C->setColonLoc(Record.readSourceLocation());
12132   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12133   DeclarationNameInfo DNI;
12134   Record.readDeclarationNameInfo(DNI);
12135   C->setQualifierLoc(NNSL);
12136   C->setNameInfo(DNI);
12137 
12138   unsigned NumVars = C->varlist_size();
12139   SmallVector<Expr *, 16> Vars;
12140   Vars.reserve(NumVars);
12141   for (unsigned I = 0; I != NumVars; ++I)
12142     Vars.push_back(Record.readSubExpr());
12143   C->setVarRefs(Vars);
12144   Vars.clear();
12145   for (unsigned I = 0; I != NumVars; ++I)
12146     Vars.push_back(Record.readSubExpr());
12147   C->setPrivates(Vars);
12148   Vars.clear();
12149   for (unsigned I = 0; I != NumVars; ++I)
12150     Vars.push_back(Record.readSubExpr());
12151   C->setLHSExprs(Vars);
12152   Vars.clear();
12153   for (unsigned I = 0; I != NumVars; ++I)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setRHSExprs(Vars);
12156   Vars.clear();
12157   for (unsigned I = 0; I != NumVars; ++I)
12158     Vars.push_back(Record.readSubExpr());
12159   C->setReductionOps(Vars);
12160   Vars.clear();
12161   for (unsigned I = 0; I != NumVars; ++I)
12162     Vars.push_back(Record.readSubExpr());
12163   C->setTaskgroupDescriptors(Vars);
12164 }
12165 
12166 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12167   VisitOMPClauseWithPostUpdate(C);
12168   C->setLParenLoc(Record.readSourceLocation());
12169   C->setColonLoc(Record.readSourceLocation());
12170   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12171   C->setModifierLoc(Record.readSourceLocation());
12172   unsigned NumVars = C->varlist_size();
12173   SmallVector<Expr *, 16> Vars;
12174   Vars.reserve(NumVars);
12175   for (unsigned i = 0; i != NumVars; ++i)
12176     Vars.push_back(Record.readSubExpr());
12177   C->setVarRefs(Vars);
12178   Vars.clear();
12179   for (unsigned i = 0; i != NumVars; ++i)
12180     Vars.push_back(Record.readSubExpr());
12181   C->setPrivates(Vars);
12182   Vars.clear();
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setInits(Vars);
12186   Vars.clear();
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setUpdates(Vars);
12190   Vars.clear();
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setFinals(Vars);
12194   C->setStep(Record.readSubExpr());
12195   C->setCalcStep(Record.readSubExpr());
12196 }
12197 
12198 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12199   C->setLParenLoc(Record.readSourceLocation());
12200   C->setColonLoc(Record.readSourceLocation());
12201   unsigned NumVars = C->varlist_size();
12202   SmallVector<Expr *, 16> Vars;
12203   Vars.reserve(NumVars);
12204   for (unsigned i = 0; i != NumVars; ++i)
12205     Vars.push_back(Record.readSubExpr());
12206   C->setVarRefs(Vars);
12207   C->setAlignment(Record.readSubExpr());
12208 }
12209 
12210 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12211   C->setLParenLoc(Record.readSourceLocation());
12212   unsigned NumVars = C->varlist_size();
12213   SmallVector<Expr *, 16> Exprs;
12214   Exprs.reserve(NumVars);
12215   for (unsigned i = 0; i != NumVars; ++i)
12216     Exprs.push_back(Record.readSubExpr());
12217   C->setVarRefs(Exprs);
12218   Exprs.clear();
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Exprs.push_back(Record.readSubExpr());
12221   C->setSourceExprs(Exprs);
12222   Exprs.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Exprs.push_back(Record.readSubExpr());
12225   C->setDestinationExprs(Exprs);
12226   Exprs.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Exprs.push_back(Record.readSubExpr());
12229   C->setAssignmentOps(Exprs);
12230 }
12231 
12232 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12233   C->setLParenLoc(Record.readSourceLocation());
12234   unsigned NumVars = C->varlist_size();
12235   SmallVector<Expr *, 16> Exprs;
12236   Exprs.reserve(NumVars);
12237   for (unsigned i = 0; i != NumVars; ++i)
12238     Exprs.push_back(Record.readSubExpr());
12239   C->setVarRefs(Exprs);
12240   Exprs.clear();
12241   for (unsigned i = 0; i != NumVars; ++i)
12242     Exprs.push_back(Record.readSubExpr());
12243   C->setSourceExprs(Exprs);
12244   Exprs.clear();
12245   for (unsigned i = 0; i != NumVars; ++i)
12246     Exprs.push_back(Record.readSubExpr());
12247   C->setDestinationExprs(Exprs);
12248   Exprs.clear();
12249   for (unsigned i = 0; i != NumVars; ++i)
12250     Exprs.push_back(Record.readSubExpr());
12251   C->setAssignmentOps(Exprs);
12252 }
12253 
12254 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12255   C->setLParenLoc(Record.readSourceLocation());
12256   unsigned NumVars = C->varlist_size();
12257   SmallVector<Expr *, 16> Vars;
12258   Vars.reserve(NumVars);
12259   for (unsigned i = 0; i != NumVars; ++i)
12260     Vars.push_back(Record.readSubExpr());
12261   C->setVarRefs(Vars);
12262 }
12263 
12264 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12265   C->setLParenLoc(Record.readSourceLocation());
12266   C->setDependencyKind(
12267       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12268   C->setDependencyLoc(Record.readSourceLocation());
12269   C->setColonLoc(Record.readSourceLocation());
12270   unsigned NumVars = C->varlist_size();
12271   SmallVector<Expr *, 16> Vars;
12272   Vars.reserve(NumVars);
12273   for (unsigned I = 0; I != NumVars; ++I)
12274     Vars.push_back(Record.readSubExpr());
12275   C->setVarRefs(Vars);
12276   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12277     C->setLoopData(I, Record.readSubExpr());
12278 }
12279 
12280 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12281   VisitOMPClauseWithPreInit(C);
12282   C->setDevice(Record.readSubExpr());
12283   C->setLParenLoc(Record.readSourceLocation());
12284 }
12285 
12286 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12287   C->setLParenLoc(Record.readSourceLocation());
12288   C->setMapTypeModifier(
12289      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12290   C->setMapType(
12291      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12292   C->setMapLoc(Record.readSourceLocation());
12293   C->setColonLoc(Record.readSourceLocation());
12294   auto NumVars = C->varlist_size();
12295   auto UniqueDecls = C->getUniqueDeclarationsNum();
12296   auto TotalLists = C->getTotalComponentListNum();
12297   auto TotalComponents = C->getTotalComponentsNum();
12298 
12299   SmallVector<Expr *, 16> Vars;
12300   Vars.reserve(NumVars);
12301   for (unsigned i = 0; i != NumVars; ++i)
12302     Vars.push_back(Record.readSubExpr());
12303   C->setVarRefs(Vars);
12304 
12305   SmallVector<ValueDecl *, 16> Decls;
12306   Decls.reserve(UniqueDecls);
12307   for (unsigned i = 0; i < UniqueDecls; ++i)
12308     Decls.push_back(Record.readDeclAs<ValueDecl>());
12309   C->setUniqueDecls(Decls);
12310 
12311   SmallVector<unsigned, 16> ListsPerDecl;
12312   ListsPerDecl.reserve(UniqueDecls);
12313   for (unsigned i = 0; i < UniqueDecls; ++i)
12314     ListsPerDecl.push_back(Record.readInt());
12315   C->setDeclNumLists(ListsPerDecl);
12316 
12317   SmallVector<unsigned, 32> ListSizes;
12318   ListSizes.reserve(TotalLists);
12319   for (unsigned i = 0; i < TotalLists; ++i)
12320     ListSizes.push_back(Record.readInt());
12321   C->setComponentListSizes(ListSizes);
12322 
12323   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12324   Components.reserve(TotalComponents);
12325   for (unsigned i = 0; i < TotalComponents; ++i) {
12326     Expr *AssociatedExpr = Record.readSubExpr();
12327     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12328     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12329         AssociatedExpr, AssociatedDecl));
12330   }
12331   C->setComponents(Components, ListSizes);
12332 }
12333 
12334 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12335   VisitOMPClauseWithPreInit(C);
12336   C->setNumTeams(Record.readSubExpr());
12337   C->setLParenLoc(Record.readSourceLocation());
12338 }
12339 
12340 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12341   VisitOMPClauseWithPreInit(C);
12342   C->setThreadLimit(Record.readSubExpr());
12343   C->setLParenLoc(Record.readSourceLocation());
12344 }
12345 
12346 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12347   C->setPriority(Record.readSubExpr());
12348   C->setLParenLoc(Record.readSourceLocation());
12349 }
12350 
12351 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12352   C->setGrainsize(Record.readSubExpr());
12353   C->setLParenLoc(Record.readSourceLocation());
12354 }
12355 
12356 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12357   C->setNumTasks(Record.readSubExpr());
12358   C->setLParenLoc(Record.readSourceLocation());
12359 }
12360 
12361 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12362   C->setHint(Record.readSubExpr());
12363   C->setLParenLoc(Record.readSourceLocation());
12364 }
12365 
12366 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12367   VisitOMPClauseWithPreInit(C);
12368   C->setDistScheduleKind(
12369       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12370   C->setChunkSize(Record.readSubExpr());
12371   C->setLParenLoc(Record.readSourceLocation());
12372   C->setDistScheduleKindLoc(Record.readSourceLocation());
12373   C->setCommaLoc(Record.readSourceLocation());
12374 }
12375 
12376 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12377   C->setDefaultmapKind(
12378        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12379   C->setDefaultmapModifier(
12380       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12381   C->setLParenLoc(Record.readSourceLocation());
12382   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12383   C->setDefaultmapKindLoc(Record.readSourceLocation());
12384 }
12385 
12386 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12387   C->setLParenLoc(Record.readSourceLocation());
12388   auto NumVars = C->varlist_size();
12389   auto UniqueDecls = C->getUniqueDeclarationsNum();
12390   auto TotalLists = C->getTotalComponentListNum();
12391   auto TotalComponents = C->getTotalComponentsNum();
12392 
12393   SmallVector<Expr *, 16> Vars;
12394   Vars.reserve(NumVars);
12395   for (unsigned i = 0; i != NumVars; ++i)
12396     Vars.push_back(Record.readSubExpr());
12397   C->setVarRefs(Vars);
12398 
12399   SmallVector<ValueDecl *, 16> Decls;
12400   Decls.reserve(UniqueDecls);
12401   for (unsigned i = 0; i < UniqueDecls; ++i)
12402     Decls.push_back(Record.readDeclAs<ValueDecl>());
12403   C->setUniqueDecls(Decls);
12404 
12405   SmallVector<unsigned, 16> ListsPerDecl;
12406   ListsPerDecl.reserve(UniqueDecls);
12407   for (unsigned i = 0; i < UniqueDecls; ++i)
12408     ListsPerDecl.push_back(Record.readInt());
12409   C->setDeclNumLists(ListsPerDecl);
12410 
12411   SmallVector<unsigned, 32> ListSizes;
12412   ListSizes.reserve(TotalLists);
12413   for (unsigned i = 0; i < TotalLists; ++i)
12414     ListSizes.push_back(Record.readInt());
12415   C->setComponentListSizes(ListSizes);
12416 
12417   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12418   Components.reserve(TotalComponents);
12419   for (unsigned i = 0; i < TotalComponents; ++i) {
12420     Expr *AssociatedExpr = Record.readSubExpr();
12421     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12422     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12423         AssociatedExpr, AssociatedDecl));
12424   }
12425   C->setComponents(Components, ListSizes);
12426 }
12427 
12428 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12429   C->setLParenLoc(Record.readSourceLocation());
12430   auto NumVars = C->varlist_size();
12431   auto UniqueDecls = C->getUniqueDeclarationsNum();
12432   auto TotalLists = C->getTotalComponentListNum();
12433   auto TotalComponents = C->getTotalComponentsNum();
12434 
12435   SmallVector<Expr *, 16> Vars;
12436   Vars.reserve(NumVars);
12437   for (unsigned i = 0; i != NumVars; ++i)
12438     Vars.push_back(Record.readSubExpr());
12439   C->setVarRefs(Vars);
12440 
12441   SmallVector<ValueDecl *, 16> Decls;
12442   Decls.reserve(UniqueDecls);
12443   for (unsigned i = 0; i < UniqueDecls; ++i)
12444     Decls.push_back(Record.readDeclAs<ValueDecl>());
12445   C->setUniqueDecls(Decls);
12446 
12447   SmallVector<unsigned, 16> ListsPerDecl;
12448   ListsPerDecl.reserve(UniqueDecls);
12449   for (unsigned i = 0; i < UniqueDecls; ++i)
12450     ListsPerDecl.push_back(Record.readInt());
12451   C->setDeclNumLists(ListsPerDecl);
12452 
12453   SmallVector<unsigned, 32> ListSizes;
12454   ListSizes.reserve(TotalLists);
12455   for (unsigned i = 0; i < TotalLists; ++i)
12456     ListSizes.push_back(Record.readInt());
12457   C->setComponentListSizes(ListSizes);
12458 
12459   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12460   Components.reserve(TotalComponents);
12461   for (unsigned i = 0; i < TotalComponents; ++i) {
12462     Expr *AssociatedExpr = Record.readSubExpr();
12463     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12464     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12465         AssociatedExpr, AssociatedDecl));
12466   }
12467   C->setComponents(Components, ListSizes);
12468 }
12469 
12470 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12471   C->setLParenLoc(Record.readSourceLocation());
12472   auto NumVars = C->varlist_size();
12473   auto UniqueDecls = C->getUniqueDeclarationsNum();
12474   auto TotalLists = C->getTotalComponentListNum();
12475   auto TotalComponents = C->getTotalComponentsNum();
12476 
12477   SmallVector<Expr *, 16> Vars;
12478   Vars.reserve(NumVars);
12479   for (unsigned i = 0; i != NumVars; ++i)
12480     Vars.push_back(Record.readSubExpr());
12481   C->setVarRefs(Vars);
12482   Vars.clear();
12483   for (unsigned i = 0; i != NumVars; ++i)
12484     Vars.push_back(Record.readSubExpr());
12485   C->setPrivateCopies(Vars);
12486   Vars.clear();
12487   for (unsigned i = 0; i != NumVars; ++i)
12488     Vars.push_back(Record.readSubExpr());
12489   C->setInits(Vars);
12490 
12491   SmallVector<ValueDecl *, 16> Decls;
12492   Decls.reserve(UniqueDecls);
12493   for (unsigned i = 0; i < UniqueDecls; ++i)
12494     Decls.push_back(Record.readDeclAs<ValueDecl>());
12495   C->setUniqueDecls(Decls);
12496 
12497   SmallVector<unsigned, 16> ListsPerDecl;
12498   ListsPerDecl.reserve(UniqueDecls);
12499   for (unsigned i = 0; i < UniqueDecls; ++i)
12500     ListsPerDecl.push_back(Record.readInt());
12501   C->setDeclNumLists(ListsPerDecl);
12502 
12503   SmallVector<unsigned, 32> ListSizes;
12504   ListSizes.reserve(TotalLists);
12505   for (unsigned i = 0; i < TotalLists; ++i)
12506     ListSizes.push_back(Record.readInt());
12507   C->setComponentListSizes(ListSizes);
12508 
12509   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12510   Components.reserve(TotalComponents);
12511   for (unsigned i = 0; i < TotalComponents; ++i) {
12512     Expr *AssociatedExpr = Record.readSubExpr();
12513     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12514     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12515         AssociatedExpr, AssociatedDecl));
12516   }
12517   C->setComponents(Components, ListSizes);
12518 }
12519 
12520 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12521   C->setLParenLoc(Record.readSourceLocation());
12522   auto NumVars = C->varlist_size();
12523   auto UniqueDecls = C->getUniqueDeclarationsNum();
12524   auto TotalLists = C->getTotalComponentListNum();
12525   auto TotalComponents = C->getTotalComponentsNum();
12526 
12527   SmallVector<Expr *, 16> Vars;
12528   Vars.reserve(NumVars);
12529   for (unsigned i = 0; i != NumVars; ++i)
12530     Vars.push_back(Record.readSubExpr());
12531   C->setVarRefs(Vars);
12532   Vars.clear();
12533 
12534   SmallVector<ValueDecl *, 16> Decls;
12535   Decls.reserve(UniqueDecls);
12536   for (unsigned i = 0; i < UniqueDecls; ++i)
12537     Decls.push_back(Record.readDeclAs<ValueDecl>());
12538   C->setUniqueDecls(Decls);
12539 
12540   SmallVector<unsigned, 16> ListsPerDecl;
12541   ListsPerDecl.reserve(UniqueDecls);
12542   for (unsigned i = 0; i < UniqueDecls; ++i)
12543     ListsPerDecl.push_back(Record.readInt());
12544   C->setDeclNumLists(ListsPerDecl);
12545 
12546   SmallVector<unsigned, 32> ListSizes;
12547   ListSizes.reserve(TotalLists);
12548   for (unsigned i = 0; i < TotalLists; ++i)
12549     ListSizes.push_back(Record.readInt());
12550   C->setComponentListSizes(ListSizes);
12551 
12552   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12553   Components.reserve(TotalComponents);
12554   for (unsigned i = 0; i < TotalComponents; ++i) {
12555     Expr *AssociatedExpr = Record.readSubExpr();
12556     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12557     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12558         AssociatedExpr, AssociatedDecl));
12559   }
12560   C->setComponents(Components, ListSizes);
12561 }
12562