1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Serialization/ASTReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/CommentOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/DiagnosticOptions.h"
43 #include "clang/Basic/ExceptionSpecificationType.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/FileSystemOptions.h"
46 #include "clang/Basic/IdentifierTable.h"
47 #include "clang/Basic/LLVM.h"
48 #include "clang/Basic/LangOptions.h"
49 #include "clang/Basic/Module.h"
50 #include "clang/Basic/ObjCRuntime.h"
51 #include "clang/Basic/OperatorKinds.h"
52 #include "clang/Basic/PragmaKinds.h"
53 #include "clang/Basic/Sanitizers.h"
54 #include "clang/Basic/SourceLocation.h"
55 #include "clang/Basic/SourceManager.h"
56 #include "clang/Basic/SourceManagerInternals.h"
57 #include "clang/Basic/Specifiers.h"
58 #include "clang/Basic/TargetInfo.h"
59 #include "clang/Basic/TargetOptions.h"
60 #include "clang/Basic/TokenKinds.h"
61 #include "clang/Basic/Version.h"
62 #include "clang/Lex/HeaderSearch.h"
63 #include "clang/Lex/HeaderSearchOptions.h"
64 #include "clang/Lex/MacroInfo.h"
65 #include "clang/Lex/ModuleMap.h"
66 #include "clang/Lex/PreprocessingRecord.h"
67 #include "clang/Lex/Preprocessor.h"
68 #include "clang/Lex/PreprocessorOptions.h"
69 #include "clang/Lex/Token.h"
70 #include "clang/Sema/ObjCMethodList.h"
71 #include "clang/Sema/Scope.h"
72 #include "clang/Sema/Sema.h"
73 #include "clang/Sema/Weak.h"
74 #include "clang/Serialization/ASTBitCodes.h"
75 #include "clang/Serialization/ASTDeserializationListener.h"
76 #include "clang/Serialization/ContinuousRangeMap.h"
77 #include "clang/Serialization/GlobalModuleIndex.h"
78 #include "clang/Serialization/InMemoryModuleCache.h"
79 #include "clang/Serialization/Module.h"
80 #include "clang/Serialization/ModuleFileExtension.h"
81 #include "clang/Serialization/ModuleManager.h"
82 #include "clang/Serialization/PCHContainerOperations.h"
83 #include "clang/Serialization/SerializationDiagnostic.h"
84 #include "llvm/ADT/APFloat.h"
85 #include "llvm/ADT/APInt.h"
86 #include "llvm/ADT/APSInt.h"
87 #include "llvm/ADT/ArrayRef.h"
88 #include "llvm/ADT/DenseMap.h"
89 #include "llvm/ADT/FoldingSet.h"
90 #include "llvm/ADT/Hashing.h"
91 #include "llvm/ADT/IntrusiveRefCntPtr.h"
92 #include "llvm/ADT/None.h"
93 #include "llvm/ADT/Optional.h"
94 #include "llvm/ADT/STLExtras.h"
95 #include "llvm/ADT/ScopeExit.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/Bitstream/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   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1152     Error(std::move(Err));
1153     return true;
1154   }
1155 
1156   RecordData Record;
1157   StringRef Blob;
1158   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1159   if (!MaybeCode) {
1160     Error(MaybeCode.takeError());
1161     return true;
1162   }
1163   unsigned Code = MaybeCode.get();
1164 
1165   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1166   if (!MaybeRecCode) {
1167     Error(MaybeRecCode.takeError());
1168     return true;
1169   }
1170   unsigned RecCode = MaybeRecCode.get();
1171   if (RecCode != DECL_CONTEXT_LEXICAL) {
1172     Error("Expected lexical block");
1173     return true;
1174   }
1175 
1176   assert(!isa<TranslationUnitDecl>(DC) &&
1177          "expected a TU_UPDATE_LEXICAL record for TU");
1178   // If we are handling a C++ class template instantiation, we can see multiple
1179   // lexical updates for the same record. It's important that we select only one
1180   // of them, so that field numbering works properly. Just pick the first one we
1181   // see.
1182   auto &Lex = LexicalDecls[DC];
1183   if (!Lex.first) {
1184     Lex = std::make_pair(
1185         &M, llvm::makeArrayRef(
1186                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1187                     Blob.data()),
1188                 Blob.size() / 4));
1189   }
1190   DC->setHasExternalLexicalStorage(true);
1191   return false;
1192 }
1193 
1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1195                                               BitstreamCursor &Cursor,
1196                                               uint64_t Offset,
1197                                               DeclID ID) {
1198   assert(Offset != 0);
1199 
1200   SavedStreamPosition SavedPosition(Cursor);
1201   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1202     Error(std::move(Err));
1203     return true;
1204   }
1205 
1206   RecordData Record;
1207   StringRef Blob;
1208   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1209   if (!MaybeCode) {
1210     Error(MaybeCode.takeError());
1211     return true;
1212   }
1213   unsigned Code = MaybeCode.get();
1214 
1215   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1216   if (!MaybeRecCode) {
1217     Error(MaybeRecCode.takeError());
1218     return true;
1219   }
1220   unsigned RecCode = MaybeRecCode.get();
1221   if (RecCode != DECL_CONTEXT_VISIBLE) {
1222     Error("Expected visible lookup table block");
1223     return true;
1224   }
1225 
1226   // We can't safely determine the primary context yet, so delay attaching the
1227   // lookup table until we're done with recursive deserialization.
1228   auto *Data = (const unsigned char*)Blob.data();
1229   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1230   return false;
1231 }
1232 
1233 void ASTReader::Error(StringRef Msg) const {
1234   Error(diag::err_fe_pch_malformed, Msg);
1235   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1236       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1237     Diag(diag::note_module_cache_path)
1238       << PP.getHeaderSearchInfo().getModuleCachePath();
1239   }
1240 }
1241 
1242 void ASTReader::Error(unsigned DiagID,
1243                       StringRef Arg1, StringRef Arg2) const {
1244   if (Diags.isDiagnosticInFlight())
1245     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1246   else
1247     Diag(DiagID) << Arg1 << Arg2;
1248 }
1249 
1250 void ASTReader::Error(llvm::Error &&Err) const {
1251   Error(toString(std::move(Err)));
1252 }
1253 
1254 //===----------------------------------------------------------------------===//
1255 // Source Manager Deserialization
1256 //===----------------------------------------------------------------------===//
1257 
1258 /// Read the line table in the source manager block.
1259 /// \returns true if there was an error.
1260 bool ASTReader::ParseLineTable(ModuleFile &F,
1261                                const RecordData &Record) {
1262   unsigned Idx = 0;
1263   LineTableInfo &LineTable = SourceMgr.getLineTable();
1264 
1265   // Parse the file names
1266   std::map<int, int> FileIDs;
1267   FileIDs[-1] = -1; // For unspecified filenames.
1268   for (unsigned I = 0; Record[Idx]; ++I) {
1269     // Extract the file name
1270     auto Filename = ReadPath(F, Record, Idx);
1271     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1272   }
1273   ++Idx;
1274 
1275   // Parse the line entries
1276   std::vector<LineEntry> Entries;
1277   while (Idx < Record.size()) {
1278     int FID = Record[Idx++];
1279     assert(FID >= 0 && "Serialized line entries for non-local file.");
1280     // Remap FileID from 1-based old view.
1281     FID += F.SLocEntryBaseID - 1;
1282 
1283     // Extract the line entries
1284     unsigned NumEntries = Record[Idx++];
1285     assert(NumEntries && "no line entries for file ID");
1286     Entries.clear();
1287     Entries.reserve(NumEntries);
1288     for (unsigned I = 0; I != NumEntries; ++I) {
1289       unsigned FileOffset = Record[Idx++];
1290       unsigned LineNo = Record[Idx++];
1291       int FilenameID = FileIDs[Record[Idx++]];
1292       SrcMgr::CharacteristicKind FileKind
1293         = (SrcMgr::CharacteristicKind)Record[Idx++];
1294       unsigned IncludeOffset = Record[Idx++];
1295       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1296                                        FileKind, IncludeOffset));
1297     }
1298     LineTable.AddEntry(FileID::get(FID), Entries);
1299   }
1300 
1301   return false;
1302 }
1303 
1304 /// Read a source manager block
1305 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1306   using namespace SrcMgr;
1307 
1308   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1309 
1310   // Set the source-location entry cursor to the current position in
1311   // the stream. This cursor will be used to read the contents of the
1312   // source manager block initially, and then lazily read
1313   // source-location entries as needed.
1314   SLocEntryCursor = F.Stream;
1315 
1316   // The stream itself is going to skip over the source manager block.
1317   if (llvm::Error Err = F.Stream.SkipBlock()) {
1318     Error(std::move(Err));
1319     return true;
1320   }
1321 
1322   // Enter the source manager block.
1323   if (llvm::Error Err =
1324           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1325     Error(std::move(Err));
1326     return true;
1327   }
1328 
1329   RecordData Record;
1330   while (true) {
1331     Expected<llvm::BitstreamEntry> MaybeE =
1332         SLocEntryCursor.advanceSkippingSubblocks();
1333     if (!MaybeE) {
1334       Error(MaybeE.takeError());
1335       return true;
1336     }
1337     llvm::BitstreamEntry E = MaybeE.get();
1338 
1339     switch (E.Kind) {
1340     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1341     case llvm::BitstreamEntry::Error:
1342       Error("malformed block record in AST file");
1343       return true;
1344     case llvm::BitstreamEntry::EndBlock:
1345       return false;
1346     case llvm::BitstreamEntry::Record:
1347       // The interesting case.
1348       break;
1349     }
1350 
1351     // Read a record.
1352     Record.clear();
1353     StringRef Blob;
1354     Expected<unsigned> MaybeRecord =
1355         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1356     if (!MaybeRecord) {
1357       Error(MaybeRecord.takeError());
1358       return true;
1359     }
1360     switch (MaybeRecord.get()) {
1361     default:  // Default behavior: ignore.
1362       break;
1363 
1364     case SM_SLOC_FILE_ENTRY:
1365     case SM_SLOC_BUFFER_ENTRY:
1366     case SM_SLOC_EXPANSION_ENTRY:
1367       // Once we hit one of the source location entries, we're done.
1368       return false;
1369     }
1370   }
1371 }
1372 
1373 /// If a header file is not found at the path that we expect it to be
1374 /// and the PCH file was moved from its original location, try to resolve the
1375 /// file by assuming that header+PCH were moved together and the header is in
1376 /// the same place relative to the PCH.
1377 static std::string
1378 resolveFileRelativeToOriginalDir(const std::string &Filename,
1379                                  const std::string &OriginalDir,
1380                                  const std::string &CurrDir) {
1381   assert(OriginalDir != CurrDir &&
1382          "No point trying to resolve the file if the PCH dir didn't change");
1383 
1384   using namespace llvm::sys;
1385 
1386   SmallString<128> filePath(Filename);
1387   fs::make_absolute(filePath);
1388   assert(path::is_absolute(OriginalDir));
1389   SmallString<128> currPCHPath(CurrDir);
1390 
1391   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1392                        fileDirE = path::end(path::parent_path(filePath));
1393   path::const_iterator origDirI = path::begin(OriginalDir),
1394                        origDirE = path::end(OriginalDir);
1395   // Skip the common path components from filePath and OriginalDir.
1396   while (fileDirI != fileDirE && origDirI != origDirE &&
1397          *fileDirI == *origDirI) {
1398     ++fileDirI;
1399     ++origDirI;
1400   }
1401   for (; origDirI != origDirE; ++origDirI)
1402     path::append(currPCHPath, "..");
1403   path::append(currPCHPath, fileDirI, fileDirE);
1404   path::append(currPCHPath, path::filename(Filename));
1405   return currPCHPath.str();
1406 }
1407 
1408 bool ASTReader::ReadSLocEntry(int ID) {
1409   if (ID == 0)
1410     return false;
1411 
1412   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1413     Error("source location entry ID out-of-range for AST file");
1414     return true;
1415   }
1416 
1417   // Local helper to read the (possibly-compressed) buffer data following the
1418   // entry record.
1419   auto ReadBuffer = [this](
1420       BitstreamCursor &SLocEntryCursor,
1421       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1422     RecordData Record;
1423     StringRef Blob;
1424     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1425     if (!MaybeCode) {
1426       Error(MaybeCode.takeError());
1427       return nullptr;
1428     }
1429     unsigned Code = MaybeCode.get();
1430 
1431     Expected<unsigned> MaybeRecCode =
1432         SLocEntryCursor.readRecord(Code, Record, &Blob);
1433     if (!MaybeRecCode) {
1434       Error(MaybeRecCode.takeError());
1435       return nullptr;
1436     }
1437     unsigned RecCode = MaybeRecCode.get();
1438 
1439     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1440       if (!llvm::zlib::isAvailable()) {
1441         Error("zlib is not available");
1442         return nullptr;
1443       }
1444       SmallString<0> Uncompressed;
1445       if (llvm::Error E =
1446               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1447         Error("could not decompress embedded file contents: " +
1448               llvm::toString(std::move(E)));
1449         return nullptr;
1450       }
1451       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1452     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1453       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1454     } else {
1455       Error("AST record has invalid code");
1456       return nullptr;
1457     }
1458   };
1459 
1460   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1461   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1462           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1463     Error(std::move(Err));
1464     return true;
1465   }
1466 
1467   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1468   unsigned BaseOffset = F->SLocEntryBaseOffset;
1469 
1470   ++NumSLocEntriesRead;
1471   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1472   if (!MaybeEntry) {
1473     Error(MaybeEntry.takeError());
1474     return true;
1475   }
1476   llvm::BitstreamEntry Entry = MaybeEntry.get();
1477 
1478   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1479     Error("incorrectly-formatted source location entry in AST file");
1480     return true;
1481   }
1482 
1483   RecordData Record;
1484   StringRef Blob;
1485   Expected<unsigned> MaybeSLOC =
1486       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1487   if (!MaybeSLOC) {
1488     Error(MaybeSLOC.takeError());
1489     return true;
1490   }
1491   switch (MaybeSLOC.get()) {
1492   default:
1493     Error("incorrectly-formatted source location entry in AST file");
1494     return true;
1495 
1496   case SM_SLOC_FILE_ENTRY: {
1497     // We will detect whether a file changed and return 'Failure' for it, but
1498     // we will also try to fail gracefully by setting up the SLocEntry.
1499     unsigned InputID = Record[4];
1500     InputFile IF = getInputFile(*F, InputID);
1501     const FileEntry *File = IF.getFile();
1502     bool OverriddenBuffer = IF.isOverridden();
1503 
1504     // Note that we only check if a File was returned. If it was out-of-date
1505     // we have complained but we will continue creating a FileID to recover
1506     // gracefully.
1507     if (!File)
1508       return true;
1509 
1510     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1511     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1512       // This is the module's main file.
1513       IncludeLoc = getImportLocation(F);
1514     }
1515     SrcMgr::CharacteristicKind
1516       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1517     // FIXME: The FileID should be created from the FileEntryRef.
1518     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1519                                         ID, BaseOffset + Record[0]);
1520     SrcMgr::FileInfo &FileInfo =
1521           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1522     FileInfo.NumCreatedFIDs = Record[5];
1523     if (Record[3])
1524       FileInfo.setHasLineDirectives();
1525 
1526     unsigned NumFileDecls = Record[7];
1527     if (NumFileDecls && ContextObj) {
1528       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1529       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1530       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1531                                                              NumFileDecls));
1532     }
1533 
1534     const SrcMgr::ContentCache *ContentCache
1535       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1536     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1537         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1538         !ContentCache->getRawBuffer()) {
1539       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1540       if (!Buffer)
1541         return true;
1542       SourceMgr.overrideFileContents(File, std::move(Buffer));
1543     }
1544 
1545     break;
1546   }
1547 
1548   case SM_SLOC_BUFFER_ENTRY: {
1549     const char *Name = Blob.data();
1550     unsigned Offset = Record[0];
1551     SrcMgr::CharacteristicKind
1552       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1553     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1554     if (IncludeLoc.isInvalid() && F->isModule()) {
1555       IncludeLoc = getImportLocation(F);
1556     }
1557 
1558     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1559     if (!Buffer)
1560       return true;
1561     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1562                            BaseOffset + Offset, IncludeLoc);
1563     break;
1564   }
1565 
1566   case SM_SLOC_EXPANSION_ENTRY: {
1567     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1568     SourceMgr.createExpansionLoc(SpellingLoc,
1569                                      ReadSourceLocation(*F, Record[2]),
1570                                      ReadSourceLocation(*F, Record[3]),
1571                                      Record[5],
1572                                      Record[4],
1573                                      ID,
1574                                      BaseOffset + Record[0]);
1575     break;
1576   }
1577   }
1578 
1579   return false;
1580 }
1581 
1582 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1583   if (ID == 0)
1584     return std::make_pair(SourceLocation(), "");
1585 
1586   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1587     Error("source location entry ID out-of-range for AST file");
1588     return std::make_pair(SourceLocation(), "");
1589   }
1590 
1591   // Find which module file this entry lands in.
1592   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1593   if (!M->isModule())
1594     return std::make_pair(SourceLocation(), "");
1595 
1596   // FIXME: Can we map this down to a particular submodule? That would be
1597   // ideal.
1598   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1599 }
1600 
1601 /// Find the location where the module F is imported.
1602 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1603   if (F->ImportLoc.isValid())
1604     return F->ImportLoc;
1605 
1606   // Otherwise we have a PCH. It's considered to be "imported" at the first
1607   // location of its includer.
1608   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1609     // Main file is the importer.
1610     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1611     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1612   }
1613   return F->ImportedBy[0]->FirstLoc;
1614 }
1615 
1616 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1617 /// the abbreviations that are at the top of the block and then leave the cursor
1618 /// pointing into the block.
1619 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1620   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1621     // FIXME this drops errors on the floor.
1622     consumeError(std::move(Err));
1623     return true;
1624   }
1625 
1626   while (true) {
1627     uint64_t Offset = Cursor.GetCurrentBitNo();
1628     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1629     if (!MaybeCode) {
1630       // FIXME this drops errors on the floor.
1631       consumeError(MaybeCode.takeError());
1632       return true;
1633     }
1634     unsigned Code = MaybeCode.get();
1635 
1636     // We expect all abbrevs to be at the start of the block.
1637     if (Code != llvm::bitc::DEFINE_ABBREV) {
1638       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1639         // FIXME this drops errors on the floor.
1640         consumeError(std::move(Err));
1641         return true;
1642       }
1643       return false;
1644     }
1645     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1646       // FIXME this drops errors on the floor.
1647       consumeError(std::move(Err));
1648       return true;
1649     }
1650   }
1651 }
1652 
1653 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1654                            unsigned &Idx) {
1655   Token Tok;
1656   Tok.startToken();
1657   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1658   Tok.setLength(Record[Idx++]);
1659   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1660     Tok.setIdentifierInfo(II);
1661   Tok.setKind((tok::TokenKind)Record[Idx++]);
1662   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1663   return Tok;
1664 }
1665 
1666 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1667   BitstreamCursor &Stream = F.MacroCursor;
1668 
1669   // Keep track of where we are in the stream, then jump back there
1670   // after reading this macro.
1671   SavedStreamPosition SavedPosition(Stream);
1672 
1673   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1674     // FIXME this drops errors on the floor.
1675     consumeError(std::move(Err));
1676     return nullptr;
1677   }
1678   RecordData Record;
1679   SmallVector<IdentifierInfo*, 16> MacroParams;
1680   MacroInfo *Macro = nullptr;
1681 
1682   while (true) {
1683     // Advance to the next record, but if we get to the end of the block, don't
1684     // pop it (removing all the abbreviations from the cursor) since we want to
1685     // be able to reseek within the block and read entries.
1686     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1687     Expected<llvm::BitstreamEntry> MaybeEntry =
1688         Stream.advanceSkippingSubblocks(Flags);
1689     if (!MaybeEntry) {
1690       Error(MaybeEntry.takeError());
1691       return Macro;
1692     }
1693     llvm::BitstreamEntry Entry = MaybeEntry.get();
1694 
1695     switch (Entry.Kind) {
1696     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1697     case llvm::BitstreamEntry::Error:
1698       Error("malformed block record in AST file");
1699       return Macro;
1700     case llvm::BitstreamEntry::EndBlock:
1701       return Macro;
1702     case llvm::BitstreamEntry::Record:
1703       // The interesting case.
1704       break;
1705     }
1706 
1707     // Read a record.
1708     Record.clear();
1709     PreprocessorRecordTypes RecType;
1710     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1711       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1712     else {
1713       Error(MaybeRecType.takeError());
1714       return Macro;
1715     }
1716     switch (RecType) {
1717     case PP_MODULE_MACRO:
1718     case PP_MACRO_DIRECTIVE_HISTORY:
1719       return Macro;
1720 
1721     case PP_MACRO_OBJECT_LIKE:
1722     case PP_MACRO_FUNCTION_LIKE: {
1723       // If we already have a macro, that means that we've hit the end
1724       // of the definition of the macro we were looking for. We're
1725       // done.
1726       if (Macro)
1727         return Macro;
1728 
1729       unsigned NextIndex = 1; // Skip identifier ID.
1730       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1731       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1732       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1733       MI->setIsUsed(Record[NextIndex++]);
1734       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1735 
1736       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1737         // Decode function-like macro info.
1738         bool isC99VarArgs = Record[NextIndex++];
1739         bool isGNUVarArgs = Record[NextIndex++];
1740         bool hasCommaPasting = Record[NextIndex++];
1741         MacroParams.clear();
1742         unsigned NumArgs = Record[NextIndex++];
1743         for (unsigned i = 0; i != NumArgs; ++i)
1744           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1745 
1746         // Install function-like macro info.
1747         MI->setIsFunctionLike();
1748         if (isC99VarArgs) MI->setIsC99Varargs();
1749         if (isGNUVarArgs) MI->setIsGNUVarargs();
1750         if (hasCommaPasting) MI->setHasCommaPasting();
1751         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1752       }
1753 
1754       // Remember that we saw this macro last so that we add the tokens that
1755       // form its body to it.
1756       Macro = MI;
1757 
1758       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1759           Record[NextIndex]) {
1760         // We have a macro definition. Register the association
1761         PreprocessedEntityID
1762             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1763         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1764         PreprocessingRecord::PPEntityID PPID =
1765             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1766         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1767             PPRec.getPreprocessedEntity(PPID));
1768         if (PPDef)
1769           PPRec.RegisterMacroDefinition(Macro, PPDef);
1770       }
1771 
1772       ++NumMacrosRead;
1773       break;
1774     }
1775 
1776     case PP_TOKEN: {
1777       // If we see a TOKEN before a PP_MACRO_*, then the file is
1778       // erroneous, just pretend we didn't see this.
1779       if (!Macro) break;
1780 
1781       unsigned Idx = 0;
1782       Token Tok = ReadToken(F, Record, Idx);
1783       Macro->AddTokenToBody(Tok);
1784       break;
1785     }
1786     }
1787   }
1788 }
1789 
1790 PreprocessedEntityID
1791 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1792                                          unsigned LocalID) const {
1793   if (!M.ModuleOffsetMap.empty())
1794     ReadModuleOffsetMap(M);
1795 
1796   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1797     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1798   assert(I != M.PreprocessedEntityRemap.end()
1799          && "Invalid index into preprocessed entity index remap");
1800 
1801   return LocalID + I->second;
1802 }
1803 
1804 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1805   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1806 }
1807 
1808 HeaderFileInfoTrait::internal_key_type
1809 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1810   internal_key_type ikey = {FE->getSize(),
1811                             M.HasTimestamps ? FE->getModificationTime() : 0,
1812                             FE->getName(), /*Imported*/ false};
1813   return ikey;
1814 }
1815 
1816 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1817   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1818     return false;
1819 
1820   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1821     return true;
1822 
1823   // Determine whether the actual files are equivalent.
1824   FileManager &FileMgr = Reader.getFileManager();
1825   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1826     if (!Key.Imported) {
1827       if (auto File = FileMgr.getFile(Key.Filename))
1828         return *File;
1829       return nullptr;
1830     }
1831 
1832     std::string Resolved = Key.Filename;
1833     Reader.ResolveImportedPath(M, Resolved);
1834     if (auto File = FileMgr.getFile(Resolved))
1835       return *File;
1836     return nullptr;
1837   };
1838 
1839   const FileEntry *FEA = GetFile(a);
1840   const FileEntry *FEB = GetFile(b);
1841   return FEA && FEA == FEB;
1842 }
1843 
1844 std::pair<unsigned, unsigned>
1845 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1846   using namespace llvm::support;
1847 
1848   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1849   unsigned DataLen = (unsigned) *d++;
1850   return std::make_pair(KeyLen, DataLen);
1851 }
1852 
1853 HeaderFileInfoTrait::internal_key_type
1854 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1855   using namespace llvm::support;
1856 
1857   internal_key_type ikey;
1858   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1859   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1860   ikey.Filename = (const char *)d;
1861   ikey.Imported = true;
1862   return ikey;
1863 }
1864 
1865 HeaderFileInfoTrait::data_type
1866 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1867                               unsigned DataLen) {
1868   using namespace llvm::support;
1869 
1870   const unsigned char *End = d + DataLen;
1871   HeaderFileInfo HFI;
1872   unsigned Flags = *d++;
1873   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1874   HFI.isImport |= (Flags >> 5) & 0x01;
1875   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1876   HFI.DirInfo = (Flags >> 1) & 0x07;
1877   HFI.IndexHeaderMapHeader = Flags & 0x01;
1878   // FIXME: Find a better way to handle this. Maybe just store a
1879   // "has been included" flag?
1880   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1881                              HFI.NumIncludes);
1882   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1883       M, endian::readNext<uint32_t, little, unaligned>(d));
1884   if (unsigned FrameworkOffset =
1885           endian::readNext<uint32_t, little, unaligned>(d)) {
1886     // The framework offset is 1 greater than the actual offset,
1887     // since 0 is used as an indicator for "no framework name".
1888     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1889     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1890   }
1891 
1892   assert((End - d) % 4 == 0 &&
1893          "Wrong data length in HeaderFileInfo deserialization");
1894   while (d != End) {
1895     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1896     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1897     LocalSMID >>= 2;
1898 
1899     // This header is part of a module. Associate it with the module to enable
1900     // implicit module import.
1901     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1902     Module *Mod = Reader.getSubmodule(GlobalSMID);
1903     FileManager &FileMgr = Reader.getFileManager();
1904     ModuleMap &ModMap =
1905         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1906 
1907     std::string Filename = key.Filename;
1908     if (key.Imported)
1909       Reader.ResolveImportedPath(M, Filename);
1910     // FIXME: This is not always the right filename-as-written, but we're not
1911     // going to use this information to rebuild the module, so it doesn't make
1912     // a lot of difference.
1913     Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1914     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1915     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1916   }
1917 
1918   // This HeaderFileInfo was externally loaded.
1919   HFI.External = true;
1920   HFI.IsValid = true;
1921   return HFI;
1922 }
1923 
1924 void ASTReader::addPendingMacro(IdentifierInfo *II,
1925                                 ModuleFile *M,
1926                                 uint64_t MacroDirectivesOffset) {
1927   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1928   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1929 }
1930 
1931 void ASTReader::ReadDefinedMacros() {
1932   // Note that we are loading defined macros.
1933   Deserializing Macros(this);
1934 
1935   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1936     BitstreamCursor &MacroCursor = I.MacroCursor;
1937 
1938     // If there was no preprocessor block, skip this file.
1939     if (MacroCursor.getBitcodeBytes().empty())
1940       continue;
1941 
1942     BitstreamCursor Cursor = MacroCursor;
1943     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1944       Error(std::move(Err));
1945       return;
1946     }
1947 
1948     RecordData Record;
1949     while (true) {
1950       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1951       if (!MaybeE) {
1952         Error(MaybeE.takeError());
1953         return;
1954       }
1955       llvm::BitstreamEntry E = MaybeE.get();
1956 
1957       switch (E.Kind) {
1958       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1959       case llvm::BitstreamEntry::Error:
1960         Error("malformed block record in AST file");
1961         return;
1962       case llvm::BitstreamEntry::EndBlock:
1963         goto NextCursor;
1964 
1965       case llvm::BitstreamEntry::Record: {
1966         Record.clear();
1967         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1968         if (!MaybeRecord) {
1969           Error(MaybeRecord.takeError());
1970           return;
1971         }
1972         switch (MaybeRecord.get()) {
1973         default:  // Default behavior: ignore.
1974           break;
1975 
1976         case PP_MACRO_OBJECT_LIKE:
1977         case PP_MACRO_FUNCTION_LIKE: {
1978           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1979           if (II->isOutOfDate())
1980             updateOutOfDateIdentifier(*II);
1981           break;
1982         }
1983 
1984         case PP_TOKEN:
1985           // Ignore tokens.
1986           break;
1987         }
1988         break;
1989       }
1990       }
1991     }
1992     NextCursor:  ;
1993   }
1994 }
1995 
1996 namespace {
1997 
1998   /// Visitor class used to look up identifirs in an AST file.
1999   class IdentifierLookupVisitor {
2000     StringRef Name;
2001     unsigned NameHash;
2002     unsigned PriorGeneration;
2003     unsigned &NumIdentifierLookups;
2004     unsigned &NumIdentifierLookupHits;
2005     IdentifierInfo *Found = nullptr;
2006 
2007   public:
2008     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2009                             unsigned &NumIdentifierLookups,
2010                             unsigned &NumIdentifierLookupHits)
2011       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2012         PriorGeneration(PriorGeneration),
2013         NumIdentifierLookups(NumIdentifierLookups),
2014         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2015 
2016     bool operator()(ModuleFile &M) {
2017       // If we've already searched this module file, skip it now.
2018       if (M.Generation <= PriorGeneration)
2019         return true;
2020 
2021       ASTIdentifierLookupTable *IdTable
2022         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2023       if (!IdTable)
2024         return false;
2025 
2026       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2027                                      Found);
2028       ++NumIdentifierLookups;
2029       ASTIdentifierLookupTable::iterator Pos =
2030           IdTable->find_hashed(Name, NameHash, &Trait);
2031       if (Pos == IdTable->end())
2032         return false;
2033 
2034       // Dereferencing the iterator has the effect of building the
2035       // IdentifierInfo node and populating it with the various
2036       // declarations it needs.
2037       ++NumIdentifierLookupHits;
2038       Found = *Pos;
2039       return true;
2040     }
2041 
2042     // Retrieve the identifier info found within the module
2043     // files.
2044     IdentifierInfo *getIdentifierInfo() const { return Found; }
2045   };
2046 
2047 } // namespace
2048 
2049 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2050   // Note that we are loading an identifier.
2051   Deserializing AnIdentifier(this);
2052 
2053   unsigned PriorGeneration = 0;
2054   if (getContext().getLangOpts().Modules)
2055     PriorGeneration = IdentifierGeneration[&II];
2056 
2057   // If there is a global index, look there first to determine which modules
2058   // provably do not have any results for this identifier.
2059   GlobalModuleIndex::HitSet Hits;
2060   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2061   if (!loadGlobalIndex()) {
2062     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2063       HitsPtr = &Hits;
2064     }
2065   }
2066 
2067   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2068                                   NumIdentifierLookups,
2069                                   NumIdentifierLookupHits);
2070   ModuleMgr.visit(Visitor, HitsPtr);
2071   markIdentifierUpToDate(&II);
2072 }
2073 
2074 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2075   if (!II)
2076     return;
2077 
2078   II->setOutOfDate(false);
2079 
2080   // Update the generation for this identifier.
2081   if (getContext().getLangOpts().Modules)
2082     IdentifierGeneration[II] = getGeneration();
2083 }
2084 
2085 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2086                                     const PendingMacroInfo &PMInfo) {
2087   ModuleFile &M = *PMInfo.M;
2088 
2089   BitstreamCursor &Cursor = M.MacroCursor;
2090   SavedStreamPosition SavedPosition(Cursor);
2091   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2092     Error(std::move(Err));
2093     return;
2094   }
2095 
2096   struct ModuleMacroRecord {
2097     SubmoduleID SubModID;
2098     MacroInfo *MI;
2099     SmallVector<SubmoduleID, 8> Overrides;
2100   };
2101   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2102 
2103   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2104   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2105   // macro histroy.
2106   RecordData Record;
2107   while (true) {
2108     Expected<llvm::BitstreamEntry> MaybeEntry =
2109         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2110     if (!MaybeEntry) {
2111       Error(MaybeEntry.takeError());
2112       return;
2113     }
2114     llvm::BitstreamEntry Entry = MaybeEntry.get();
2115 
2116     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2117       Error("malformed block record in AST file");
2118       return;
2119     }
2120 
2121     Record.clear();
2122     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2123     if (!MaybePP) {
2124       Error(MaybePP.takeError());
2125       return;
2126     }
2127     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2128     case PP_MACRO_DIRECTIVE_HISTORY:
2129       break;
2130 
2131     case PP_MODULE_MACRO: {
2132       ModuleMacros.push_back(ModuleMacroRecord());
2133       auto &Info = ModuleMacros.back();
2134       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2135       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2136       for (int I = 2, N = Record.size(); I != N; ++I)
2137         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2138       continue;
2139     }
2140 
2141     default:
2142       Error("malformed block record in AST file");
2143       return;
2144     }
2145 
2146     // We found the macro directive history; that's the last record
2147     // for this macro.
2148     break;
2149   }
2150 
2151   // Module macros are listed in reverse dependency order.
2152   {
2153     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2154     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2155     for (auto &MMR : ModuleMacros) {
2156       Overrides.clear();
2157       for (unsigned ModID : MMR.Overrides) {
2158         Module *Mod = getSubmodule(ModID);
2159         auto *Macro = PP.getModuleMacro(Mod, II);
2160         assert(Macro && "missing definition for overridden macro");
2161         Overrides.push_back(Macro);
2162       }
2163 
2164       bool Inserted = false;
2165       Module *Owner = getSubmodule(MMR.SubModID);
2166       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2167     }
2168   }
2169 
2170   // Don't read the directive history for a module; we don't have anywhere
2171   // to put it.
2172   if (M.isModule())
2173     return;
2174 
2175   // Deserialize the macro directives history in reverse source-order.
2176   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2177   unsigned Idx = 0, N = Record.size();
2178   while (Idx < N) {
2179     MacroDirective *MD = nullptr;
2180     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2181     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2182     switch (K) {
2183     case MacroDirective::MD_Define: {
2184       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2185       MD = PP.AllocateDefMacroDirective(MI, Loc);
2186       break;
2187     }
2188     case MacroDirective::MD_Undefine:
2189       MD = PP.AllocateUndefMacroDirective(Loc);
2190       break;
2191     case MacroDirective::MD_Visibility:
2192       bool isPublic = Record[Idx++];
2193       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2194       break;
2195     }
2196 
2197     if (!Latest)
2198       Latest = MD;
2199     if (Earliest)
2200       Earliest->setPrevious(MD);
2201     Earliest = MD;
2202   }
2203 
2204   if (Latest)
2205     PP.setLoadedMacroDirective(II, Earliest, Latest);
2206 }
2207 
2208 ASTReader::InputFileInfo
2209 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2210   // Go find this input file.
2211   BitstreamCursor &Cursor = F.InputFilesCursor;
2212   SavedStreamPosition SavedPosition(Cursor);
2213   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2214     // FIXME this drops errors on the floor.
2215     consumeError(std::move(Err));
2216   }
2217 
2218   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2219   if (!MaybeCode) {
2220     // FIXME this drops errors on the floor.
2221     consumeError(MaybeCode.takeError());
2222   }
2223   unsigned Code = MaybeCode.get();
2224   RecordData Record;
2225   StringRef Blob;
2226 
2227   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2228     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2229            "invalid record type for input file");
2230   else {
2231     // FIXME this drops errors on the floor.
2232     consumeError(Maybe.takeError());
2233   }
2234 
2235   assert(Record[0] == ID && "Bogus stored ID or offset");
2236   InputFileInfo R;
2237   R.StoredSize = static_cast<off_t>(Record[1]);
2238   R.StoredTime = static_cast<time_t>(Record[2]);
2239   R.Overridden = static_cast<bool>(Record[3]);
2240   R.Transient = static_cast<bool>(Record[4]);
2241   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2242   R.Filename = Blob;
2243   ResolveImportedPath(F, R.Filename);
2244   return R;
2245 }
2246 
2247 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2248 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2249   // If this ID is bogus, just return an empty input file.
2250   if (ID == 0 || ID > F.InputFilesLoaded.size())
2251     return InputFile();
2252 
2253   // If we've already loaded this input file, return it.
2254   if (F.InputFilesLoaded[ID-1].getFile())
2255     return F.InputFilesLoaded[ID-1];
2256 
2257   if (F.InputFilesLoaded[ID-1].isNotFound())
2258     return InputFile();
2259 
2260   // Go find this input file.
2261   BitstreamCursor &Cursor = F.InputFilesCursor;
2262   SavedStreamPosition SavedPosition(Cursor);
2263   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2264     // FIXME this drops errors on the floor.
2265     consumeError(std::move(Err));
2266   }
2267 
2268   InputFileInfo FI = readInputFileInfo(F, ID);
2269   off_t StoredSize = FI.StoredSize;
2270   time_t StoredTime = FI.StoredTime;
2271   bool Overridden = FI.Overridden;
2272   bool Transient = FI.Transient;
2273   StringRef Filename = FI.Filename;
2274 
2275   const FileEntry *File = nullptr;
2276   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2277     File = *FE;
2278 
2279   // If we didn't find the file, resolve it relative to the
2280   // original directory from which this AST file was created.
2281   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2282       F.OriginalDir != F.BaseDirectory) {
2283     std::string Resolved = resolveFileRelativeToOriginalDir(
2284         Filename, F.OriginalDir, F.BaseDirectory);
2285     if (!Resolved.empty())
2286       if (auto FE = FileMgr.getFile(Resolved))
2287         File = *FE;
2288   }
2289 
2290   // For an overridden file, create a virtual file with the stored
2291   // size/timestamp.
2292   if ((Overridden || Transient) && File == nullptr)
2293     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2294 
2295   if (File == nullptr) {
2296     if (Complain) {
2297       std::string ErrorStr = "could not find file '";
2298       ErrorStr += Filename;
2299       ErrorStr += "' referenced by AST file '";
2300       ErrorStr += F.FileName;
2301       ErrorStr += "'";
2302       Error(ErrorStr);
2303     }
2304     // Record that we didn't find the file.
2305     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2306     return InputFile();
2307   }
2308 
2309   // Check if there was a request to override the contents of the file
2310   // that was part of the precompiled header. Overriding such a file
2311   // can lead to problems when lexing using the source locations from the
2312   // PCH.
2313   SourceManager &SM = getSourceManager();
2314   // FIXME: Reject if the overrides are different.
2315   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2316     if (Complain)
2317       Error(diag::err_fe_pch_file_overridden, Filename);
2318 
2319     // After emitting the diagnostic, bypass the overriding file to recover
2320     // (this creates a separate FileEntry).
2321     File = SM.bypassFileContentsOverride(*File);
2322     if (!File) {
2323       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2324       return InputFile();
2325     }
2326   }
2327 
2328   bool IsOutOfDate = false;
2329 
2330   // For an overridden file, there is nothing to validate.
2331   if (!Overridden && //
2332       (StoredSize != File->getSize() ||
2333        (StoredTime && StoredTime != File->getModificationTime() &&
2334         !DisableValidation)
2335        )) {
2336     if (Complain) {
2337       // Build a list of the PCH imports that got us here (in reverse).
2338       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2339       while (!ImportStack.back()->ImportedBy.empty())
2340         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2341 
2342       // The top-level PCH is stale.
2343       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2344       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2345       if (DiagnosticKind == 0)
2346         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2347       else if (DiagnosticKind == 1)
2348         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2349       else
2350         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2351 
2352       // Print the import stack.
2353       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2354         Diag(diag::note_pch_required_by)
2355           << Filename << ImportStack[0]->FileName;
2356         for (unsigned I = 1; I < ImportStack.size(); ++I)
2357           Diag(diag::note_pch_required_by)
2358             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2359       }
2360 
2361       if (!Diags.isDiagnosticInFlight())
2362         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2363     }
2364 
2365     IsOutOfDate = true;
2366   }
2367   // FIXME: If the file is overridden and we've already opened it,
2368   // issue an error (or split it into a separate FileEntry).
2369 
2370   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2371 
2372   // Note that we've loaded this input file.
2373   F.InputFilesLoaded[ID-1] = IF;
2374   return IF;
2375 }
2376 
2377 /// If we are loading a relocatable PCH or module file, and the filename
2378 /// is not an absolute path, add the system or module root to the beginning of
2379 /// the file name.
2380 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2381   // Resolve relative to the base directory, if we have one.
2382   if (!M.BaseDirectory.empty())
2383     return ResolveImportedPath(Filename, M.BaseDirectory);
2384 }
2385 
2386 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2387   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2388     return;
2389 
2390   SmallString<128> Buffer;
2391   llvm::sys::path::append(Buffer, Prefix, Filename);
2392   Filename.assign(Buffer.begin(), Buffer.end());
2393 }
2394 
2395 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2396   switch (ARR) {
2397   case ASTReader::Failure: return true;
2398   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2399   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2400   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2401   case ASTReader::ConfigurationMismatch:
2402     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2403   case ASTReader::HadErrors: return true;
2404   case ASTReader::Success: return false;
2405   }
2406 
2407   llvm_unreachable("unknown ASTReadResult");
2408 }
2409 
2410 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2411     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2412     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2413     std::string &SuggestedPredefines) {
2414   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2415     // FIXME this drops errors on the floor.
2416     consumeError(std::move(Err));
2417     return Failure;
2418   }
2419 
2420   // Read all of the records in the options block.
2421   RecordData Record;
2422   ASTReadResult Result = Success;
2423   while (true) {
2424     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2425     if (!MaybeEntry) {
2426       // FIXME this drops errors on the floor.
2427       consumeError(MaybeEntry.takeError());
2428       return Failure;
2429     }
2430     llvm::BitstreamEntry Entry = MaybeEntry.get();
2431 
2432     switch (Entry.Kind) {
2433     case llvm::BitstreamEntry::Error:
2434     case llvm::BitstreamEntry::SubBlock:
2435       return Failure;
2436 
2437     case llvm::BitstreamEntry::EndBlock:
2438       return Result;
2439 
2440     case llvm::BitstreamEntry::Record:
2441       // The interesting case.
2442       break;
2443     }
2444 
2445     // Read and process a record.
2446     Record.clear();
2447     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2448     if (!MaybeRecordType) {
2449       // FIXME this drops errors on the floor.
2450       consumeError(MaybeRecordType.takeError());
2451       return Failure;
2452     }
2453     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2454     case LANGUAGE_OPTIONS: {
2455       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2456       if (ParseLanguageOptions(Record, Complain, Listener,
2457                                AllowCompatibleConfigurationMismatch))
2458         Result = ConfigurationMismatch;
2459       break;
2460     }
2461 
2462     case TARGET_OPTIONS: {
2463       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2464       if (ParseTargetOptions(Record, Complain, Listener,
2465                              AllowCompatibleConfigurationMismatch))
2466         Result = ConfigurationMismatch;
2467       break;
2468     }
2469 
2470     case FILE_SYSTEM_OPTIONS: {
2471       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2472       if (!AllowCompatibleConfigurationMismatch &&
2473           ParseFileSystemOptions(Record, Complain, Listener))
2474         Result = ConfigurationMismatch;
2475       break;
2476     }
2477 
2478     case HEADER_SEARCH_OPTIONS: {
2479       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2480       if (!AllowCompatibleConfigurationMismatch &&
2481           ParseHeaderSearchOptions(Record, Complain, Listener))
2482         Result = ConfigurationMismatch;
2483       break;
2484     }
2485 
2486     case PREPROCESSOR_OPTIONS:
2487       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2488       if (!AllowCompatibleConfigurationMismatch &&
2489           ParsePreprocessorOptions(Record, Complain, Listener,
2490                                    SuggestedPredefines))
2491         Result = ConfigurationMismatch;
2492       break;
2493     }
2494   }
2495 }
2496 
2497 ASTReader::ASTReadResult
2498 ASTReader::ReadControlBlock(ModuleFile &F,
2499                             SmallVectorImpl<ImportedModule> &Loaded,
2500                             const ModuleFile *ImportedBy,
2501                             unsigned ClientLoadCapabilities) {
2502   BitstreamCursor &Stream = F.Stream;
2503   ASTReadResult Result = Success;
2504 
2505   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2506     Error(std::move(Err));
2507     return Failure;
2508   }
2509 
2510   // Lambda to read the unhashed control block the first time it's called.
2511   //
2512   // For PCM files, the unhashed control block cannot be read until after the
2513   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2514   // need to look ahead before reading the IMPORTS record.  For consistency,
2515   // this block is always read somehow (see BitstreamEntry::EndBlock).
2516   bool HasReadUnhashedControlBlock = false;
2517   auto readUnhashedControlBlockOnce = [&]() {
2518     if (!HasReadUnhashedControlBlock) {
2519       HasReadUnhashedControlBlock = true;
2520       if (ASTReadResult Result =
2521               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2522         return Result;
2523     }
2524     return Success;
2525   };
2526 
2527   // Read all of the records and blocks in the control block.
2528   RecordData Record;
2529   unsigned NumInputs = 0;
2530   unsigned NumUserInputs = 0;
2531   StringRef BaseDirectoryAsWritten;
2532   while (true) {
2533     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2534     if (!MaybeEntry) {
2535       Error(MaybeEntry.takeError());
2536       return Failure;
2537     }
2538     llvm::BitstreamEntry Entry = MaybeEntry.get();
2539 
2540     switch (Entry.Kind) {
2541     case llvm::BitstreamEntry::Error:
2542       Error("malformed block record in AST file");
2543       return Failure;
2544     case llvm::BitstreamEntry::EndBlock: {
2545       // Validate the module before returning.  This call catches an AST with
2546       // no module name and no imports.
2547       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2548         return Result;
2549 
2550       // Validate input files.
2551       const HeaderSearchOptions &HSOpts =
2552           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2553 
2554       // All user input files reside at the index range [0, NumUserInputs), and
2555       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2556       // loaded module files, ignore missing inputs.
2557       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2558           F.Kind != MK_PrebuiltModule) {
2559         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2560 
2561         // If we are reading a module, we will create a verification timestamp,
2562         // so we verify all input files.  Otherwise, verify only user input
2563         // files.
2564 
2565         unsigned N = NumUserInputs;
2566         if (ValidateSystemInputs ||
2567             (HSOpts.ModulesValidateOncePerBuildSession &&
2568              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2569              F.Kind == MK_ImplicitModule))
2570           N = NumInputs;
2571 
2572         for (unsigned I = 0; I < N; ++I) {
2573           InputFile IF = getInputFile(F, I+1, Complain);
2574           if (!IF.getFile() || IF.isOutOfDate())
2575             return OutOfDate;
2576         }
2577       }
2578 
2579       if (Listener)
2580         Listener->visitModuleFile(F.FileName, F.Kind);
2581 
2582       if (Listener && Listener->needsInputFileVisitation()) {
2583         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2584                                                                 : NumUserInputs;
2585         for (unsigned I = 0; I < N; ++I) {
2586           bool IsSystem = I >= NumUserInputs;
2587           InputFileInfo FI = readInputFileInfo(F, I+1);
2588           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2589                                    F.Kind == MK_ExplicitModule ||
2590                                    F.Kind == MK_PrebuiltModule);
2591         }
2592       }
2593 
2594       return Result;
2595     }
2596 
2597     case llvm::BitstreamEntry::SubBlock:
2598       switch (Entry.ID) {
2599       case INPUT_FILES_BLOCK_ID:
2600         F.InputFilesCursor = Stream;
2601         if (llvm::Error Err = Stream.SkipBlock()) {
2602           Error(std::move(Err));
2603           return Failure;
2604         }
2605         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2606           Error("malformed block record in AST file");
2607           return Failure;
2608         }
2609         continue;
2610 
2611       case OPTIONS_BLOCK_ID:
2612         // If we're reading the first module for this group, check its options
2613         // are compatible with ours. For modules it imports, no further checking
2614         // is required, because we checked them when we built it.
2615         if (Listener && !ImportedBy) {
2616           // Should we allow the configuration of the module file to differ from
2617           // the configuration of the current translation unit in a compatible
2618           // way?
2619           //
2620           // FIXME: Allow this for files explicitly specified with -include-pch.
2621           bool AllowCompatibleConfigurationMismatch =
2622               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2623 
2624           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2625                                     AllowCompatibleConfigurationMismatch,
2626                                     *Listener, SuggestedPredefines);
2627           if (Result == Failure) {
2628             Error("malformed block record in AST file");
2629             return Result;
2630           }
2631 
2632           if (DisableValidation ||
2633               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2634             Result = Success;
2635 
2636           // If we can't load the module, exit early since we likely
2637           // will rebuild the module anyway. The stream may be in the
2638           // middle of a block.
2639           if (Result != Success)
2640             return Result;
2641         } else if (llvm::Error Err = Stream.SkipBlock()) {
2642           Error(std::move(Err));
2643           return Failure;
2644         }
2645         continue;
2646 
2647       default:
2648         if (llvm::Error Err = Stream.SkipBlock()) {
2649           Error(std::move(Err));
2650           return Failure;
2651         }
2652         continue;
2653       }
2654 
2655     case llvm::BitstreamEntry::Record:
2656       // The interesting case.
2657       break;
2658     }
2659 
2660     // Read and process a record.
2661     Record.clear();
2662     StringRef Blob;
2663     Expected<unsigned> MaybeRecordType =
2664         Stream.readRecord(Entry.ID, Record, &Blob);
2665     if (!MaybeRecordType) {
2666       Error(MaybeRecordType.takeError());
2667       return Failure;
2668     }
2669     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2670     case METADATA: {
2671       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2672         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2673           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2674                                         : diag::err_pch_version_too_new);
2675         return VersionMismatch;
2676       }
2677 
2678       bool hasErrors = Record[7];
2679       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2680         Diag(diag::err_pch_with_compiler_errors);
2681         return HadErrors;
2682       }
2683       if (hasErrors) {
2684         Diags.ErrorOccurred = true;
2685         Diags.UncompilableErrorOccurred = true;
2686         Diags.UnrecoverableErrorOccurred = true;
2687       }
2688 
2689       F.RelocatablePCH = Record[4];
2690       // Relative paths in a relocatable PCH are relative to our sysroot.
2691       if (F.RelocatablePCH)
2692         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2693 
2694       F.HasTimestamps = Record[5];
2695 
2696       F.PCHHasObjectFile = Record[6];
2697 
2698       const std::string &CurBranch = getClangFullRepositoryVersion();
2699       StringRef ASTBranch = Blob;
2700       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2701         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2702           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2703         return VersionMismatch;
2704       }
2705       break;
2706     }
2707 
2708     case IMPORTS: {
2709       // Validate the AST before processing any imports (otherwise, untangling
2710       // them can be error-prone and expensive).  A module will have a name and
2711       // will already have been validated, but this catches the PCH case.
2712       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2713         return Result;
2714 
2715       // Load each of the imported PCH files.
2716       unsigned Idx = 0, N = Record.size();
2717       while (Idx < N) {
2718         // Read information about the AST file.
2719         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2720         // The import location will be the local one for now; we will adjust
2721         // all import locations of module imports after the global source
2722         // location info are setup, in ReadAST.
2723         SourceLocation ImportLoc =
2724             ReadUntranslatedSourceLocation(Record[Idx++]);
2725         off_t StoredSize = (off_t)Record[Idx++];
2726         time_t StoredModTime = (time_t)Record[Idx++];
2727         ASTFileSignature StoredSignature = {
2728             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2729               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2730               (uint32_t)Record[Idx++]}}};
2731 
2732         std::string ImportedName = ReadString(Record, Idx);
2733         std::string ImportedFile;
2734 
2735         // For prebuilt and explicit modules first consult the file map for
2736         // an override. Note that here we don't search prebuilt module
2737         // directories, only the explicit name to file mappings. Also, we will
2738         // still verify the size/signature making sure it is essentially the
2739         // same file but perhaps in a different location.
2740         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2741           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2742             ImportedName, /*FileMapOnly*/ true);
2743 
2744         if (ImportedFile.empty())
2745           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2746           // ModuleCache as when writing.
2747           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2748         else
2749           SkipPath(Record, Idx);
2750 
2751         // If our client can't cope with us being out of date, we can't cope with
2752         // our dependency being missing.
2753         unsigned Capabilities = ClientLoadCapabilities;
2754         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2755           Capabilities &= ~ARR_Missing;
2756 
2757         // Load the AST file.
2758         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2759                                   Loaded, StoredSize, StoredModTime,
2760                                   StoredSignature, Capabilities);
2761 
2762         // If we diagnosed a problem, produce a backtrace.
2763         if (isDiagnosedResult(Result, Capabilities))
2764           Diag(diag::note_module_file_imported_by)
2765               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2766 
2767         switch (Result) {
2768         case Failure: return Failure;
2769           // If we have to ignore the dependency, we'll have to ignore this too.
2770         case Missing:
2771         case OutOfDate: return OutOfDate;
2772         case VersionMismatch: return VersionMismatch;
2773         case ConfigurationMismatch: return ConfigurationMismatch;
2774         case HadErrors: return HadErrors;
2775         case Success: break;
2776         }
2777       }
2778       break;
2779     }
2780 
2781     case ORIGINAL_FILE:
2782       F.OriginalSourceFileID = FileID::get(Record[0]);
2783       F.ActualOriginalSourceFileName = Blob;
2784       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2785       ResolveImportedPath(F, F.OriginalSourceFileName);
2786       break;
2787 
2788     case ORIGINAL_FILE_ID:
2789       F.OriginalSourceFileID = FileID::get(Record[0]);
2790       break;
2791 
2792     case ORIGINAL_PCH_DIR:
2793       F.OriginalDir = Blob;
2794       break;
2795 
2796     case MODULE_NAME:
2797       F.ModuleName = Blob;
2798       Diag(diag::remark_module_import)
2799           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2800           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2801       if (Listener)
2802         Listener->ReadModuleName(F.ModuleName);
2803 
2804       // Validate the AST as soon as we have a name so we can exit early on
2805       // failure.
2806       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2807         return Result;
2808 
2809       break;
2810 
2811     case MODULE_DIRECTORY: {
2812       // Save the BaseDirectory as written in the PCM for computing the module
2813       // filename for the ModuleCache.
2814       BaseDirectoryAsWritten = Blob;
2815       assert(!F.ModuleName.empty() &&
2816              "MODULE_DIRECTORY found before MODULE_NAME");
2817       // If we've already loaded a module map file covering this module, we may
2818       // have a better path for it (relative to the current build).
2819       Module *M = PP.getHeaderSearchInfo().lookupModule(
2820           F.ModuleName, /*AllowSearch*/ true,
2821           /*AllowExtraModuleMapSearch*/ true);
2822       if (M && M->Directory) {
2823         // If we're implicitly loading a module, the base directory can't
2824         // change between the build and use.
2825         // Don't emit module relocation error if we have -fno-validate-pch
2826         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2827             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2828           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2829           if (!BuildDir || *BuildDir != M->Directory) {
2830             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2831               Diag(diag::err_imported_module_relocated)
2832                   << F.ModuleName << Blob << M->Directory->getName();
2833             return OutOfDate;
2834           }
2835         }
2836         F.BaseDirectory = M->Directory->getName();
2837       } else {
2838         F.BaseDirectory = Blob;
2839       }
2840       break;
2841     }
2842 
2843     case MODULE_MAP_FILE:
2844       if (ASTReadResult Result =
2845               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2846         return Result;
2847       break;
2848 
2849     case INPUT_FILE_OFFSETS:
2850       NumInputs = Record[0];
2851       NumUserInputs = Record[1];
2852       F.InputFileOffsets =
2853           (const llvm::support::unaligned_uint64_t *)Blob.data();
2854       F.InputFilesLoaded.resize(NumInputs);
2855       F.NumUserInputFiles = NumUserInputs;
2856       break;
2857     }
2858   }
2859 }
2860 
2861 ASTReader::ASTReadResult
2862 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2863   BitstreamCursor &Stream = F.Stream;
2864 
2865   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2866     Error(std::move(Err));
2867     return Failure;
2868   }
2869 
2870   // Read all of the records and blocks for the AST file.
2871   RecordData Record;
2872   while (true) {
2873     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2874     if (!MaybeEntry) {
2875       Error(MaybeEntry.takeError());
2876       return Failure;
2877     }
2878     llvm::BitstreamEntry Entry = MaybeEntry.get();
2879 
2880     switch (Entry.Kind) {
2881     case llvm::BitstreamEntry::Error:
2882       Error("error at end of module block in AST file");
2883       return Failure;
2884     case llvm::BitstreamEntry::EndBlock:
2885       // Outside of C++, we do not store a lookup map for the translation unit.
2886       // Instead, mark it as needing a lookup map to be built if this module
2887       // contains any declarations lexically within it (which it always does!).
2888       // This usually has no cost, since we very rarely need the lookup map for
2889       // the translation unit outside C++.
2890       if (ASTContext *Ctx = ContextObj) {
2891         DeclContext *DC = Ctx->getTranslationUnitDecl();
2892         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2893           DC->setMustBuildLookupTable();
2894       }
2895 
2896       return Success;
2897     case llvm::BitstreamEntry::SubBlock:
2898       switch (Entry.ID) {
2899       case DECLTYPES_BLOCK_ID:
2900         // We lazily load the decls block, but we want to set up the
2901         // DeclsCursor cursor to point into it.  Clone our current bitcode
2902         // cursor to it, enter the block and read the abbrevs in that block.
2903         // With the main cursor, we just skip over it.
2904         F.DeclsCursor = Stream;
2905         if (llvm::Error Err = Stream.SkipBlock()) {
2906           Error(std::move(Err));
2907           return Failure;
2908         }
2909         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2910           Error("malformed block record in AST file");
2911           return Failure;
2912         }
2913         break;
2914 
2915       case PREPROCESSOR_BLOCK_ID:
2916         F.MacroCursor = Stream;
2917         if (!PP.getExternalSource())
2918           PP.setExternalSource(this);
2919 
2920         if (llvm::Error Err = Stream.SkipBlock()) {
2921           Error(std::move(Err));
2922           return Failure;
2923         }
2924         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2925           Error("malformed block record in AST file");
2926           return Failure;
2927         }
2928         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2929         break;
2930 
2931       case PREPROCESSOR_DETAIL_BLOCK_ID:
2932         F.PreprocessorDetailCursor = Stream;
2933 
2934         if (llvm::Error Err = Stream.SkipBlock()) {
2935           Error(std::move(Err));
2936           return Failure;
2937         }
2938         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2939                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2940           Error("malformed preprocessor detail record in AST file");
2941           return Failure;
2942         }
2943         F.PreprocessorDetailStartOffset
2944         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2945 
2946         if (!PP.getPreprocessingRecord())
2947           PP.createPreprocessingRecord();
2948         if (!PP.getPreprocessingRecord()->getExternalSource())
2949           PP.getPreprocessingRecord()->SetExternalSource(*this);
2950         break;
2951 
2952       case SOURCE_MANAGER_BLOCK_ID:
2953         if (ReadSourceManagerBlock(F))
2954           return Failure;
2955         break;
2956 
2957       case SUBMODULE_BLOCK_ID:
2958         if (ASTReadResult Result =
2959                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2960           return Result;
2961         break;
2962 
2963       case COMMENTS_BLOCK_ID: {
2964         BitstreamCursor C = Stream;
2965 
2966         if (llvm::Error Err = Stream.SkipBlock()) {
2967           Error(std::move(Err));
2968           return Failure;
2969         }
2970         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2971           Error("malformed comments block in AST file");
2972           return Failure;
2973         }
2974         CommentsCursors.push_back(std::make_pair(C, &F));
2975         break;
2976       }
2977 
2978       default:
2979         if (llvm::Error Err = Stream.SkipBlock()) {
2980           Error(std::move(Err));
2981           return Failure;
2982         }
2983         break;
2984       }
2985       continue;
2986 
2987     case llvm::BitstreamEntry::Record:
2988       // The interesting case.
2989       break;
2990     }
2991 
2992     // Read and process a record.
2993     Record.clear();
2994     StringRef Blob;
2995     Expected<unsigned> MaybeRecordType =
2996         Stream.readRecord(Entry.ID, Record, &Blob);
2997     if (!MaybeRecordType) {
2998       Error(MaybeRecordType.takeError());
2999       return Failure;
3000     }
3001     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3002 
3003     // If we're not loading an AST context, we don't care about most records.
3004     if (!ContextObj) {
3005       switch (RecordType) {
3006       case IDENTIFIER_TABLE:
3007       case IDENTIFIER_OFFSET:
3008       case INTERESTING_IDENTIFIERS:
3009       case STATISTICS:
3010       case PP_CONDITIONAL_STACK:
3011       case PP_COUNTER_VALUE:
3012       case SOURCE_LOCATION_OFFSETS:
3013       case MODULE_OFFSET_MAP:
3014       case SOURCE_MANAGER_LINE_TABLE:
3015       case SOURCE_LOCATION_PRELOADS:
3016       case PPD_ENTITIES_OFFSETS:
3017       case HEADER_SEARCH_TABLE:
3018       case IMPORTED_MODULES:
3019       case MACRO_OFFSET:
3020         break;
3021       default:
3022         continue;
3023       }
3024     }
3025 
3026     switch (RecordType) {
3027     default:  // Default behavior: ignore.
3028       break;
3029 
3030     case TYPE_OFFSET: {
3031       if (F.LocalNumTypes != 0) {
3032         Error("duplicate TYPE_OFFSET record in AST file");
3033         return Failure;
3034       }
3035       F.TypeOffsets = (const uint32_t *)Blob.data();
3036       F.LocalNumTypes = Record[0];
3037       unsigned LocalBaseTypeIndex = Record[1];
3038       F.BaseTypeIndex = getTotalNumTypes();
3039 
3040       if (F.LocalNumTypes > 0) {
3041         // Introduce the global -> local mapping for types within this module.
3042         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3043 
3044         // Introduce the local -> global mapping for types within this module.
3045         F.TypeRemap.insertOrReplace(
3046           std::make_pair(LocalBaseTypeIndex,
3047                          F.BaseTypeIndex - LocalBaseTypeIndex));
3048 
3049         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3050       }
3051       break;
3052     }
3053 
3054     case DECL_OFFSET: {
3055       if (F.LocalNumDecls != 0) {
3056         Error("duplicate DECL_OFFSET record in AST file");
3057         return Failure;
3058       }
3059       F.DeclOffsets = (const DeclOffset *)Blob.data();
3060       F.LocalNumDecls = Record[0];
3061       unsigned LocalBaseDeclID = Record[1];
3062       F.BaseDeclID = getTotalNumDecls();
3063 
3064       if (F.LocalNumDecls > 0) {
3065         // Introduce the global -> local mapping for declarations within this
3066         // module.
3067         GlobalDeclMap.insert(
3068           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3069 
3070         // Introduce the local -> global mapping for declarations within this
3071         // module.
3072         F.DeclRemap.insertOrReplace(
3073           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3074 
3075         // Introduce the global -> local mapping for declarations within this
3076         // module.
3077         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3078 
3079         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3080       }
3081       break;
3082     }
3083 
3084     case TU_UPDATE_LEXICAL: {
3085       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3086       LexicalContents Contents(
3087           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3088               Blob.data()),
3089           static_cast<unsigned int>(Blob.size() / 4));
3090       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3091       TU->setHasExternalLexicalStorage(true);
3092       break;
3093     }
3094 
3095     case UPDATE_VISIBLE: {
3096       unsigned Idx = 0;
3097       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3098       auto *Data = (const unsigned char*)Blob.data();
3099       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3100       // If we've already loaded the decl, perform the updates when we finish
3101       // loading this block.
3102       if (Decl *D = GetExistingDecl(ID))
3103         PendingUpdateRecords.push_back(
3104             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3105       break;
3106     }
3107 
3108     case IDENTIFIER_TABLE:
3109       F.IdentifierTableData = Blob.data();
3110       if (Record[0]) {
3111         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3112             (const unsigned char *)F.IdentifierTableData + Record[0],
3113             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3114             (const unsigned char *)F.IdentifierTableData,
3115             ASTIdentifierLookupTrait(*this, F));
3116 
3117         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3118       }
3119       break;
3120 
3121     case IDENTIFIER_OFFSET: {
3122       if (F.LocalNumIdentifiers != 0) {
3123         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3124         return Failure;
3125       }
3126       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3127       F.LocalNumIdentifiers = Record[0];
3128       unsigned LocalBaseIdentifierID = Record[1];
3129       F.BaseIdentifierID = getTotalNumIdentifiers();
3130 
3131       if (F.LocalNumIdentifiers > 0) {
3132         // Introduce the global -> local mapping for identifiers within this
3133         // module.
3134         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3135                                                   &F));
3136 
3137         // Introduce the local -> global mapping for identifiers within this
3138         // module.
3139         F.IdentifierRemap.insertOrReplace(
3140           std::make_pair(LocalBaseIdentifierID,
3141                          F.BaseIdentifierID - LocalBaseIdentifierID));
3142 
3143         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3144                                  + F.LocalNumIdentifiers);
3145       }
3146       break;
3147     }
3148 
3149     case INTERESTING_IDENTIFIERS:
3150       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3151       break;
3152 
3153     case EAGERLY_DESERIALIZED_DECLS:
3154       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3155       // about "interesting" decls (for instance, if we're building a module).
3156       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3157         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3158       break;
3159 
3160     case MODULAR_CODEGEN_DECLS:
3161       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3162       // them (ie: if we're not codegenerating this module).
3163       if (F.Kind == MK_MainFile)
3164         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3165           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3166       break;
3167 
3168     case SPECIAL_TYPES:
3169       if (SpecialTypes.empty()) {
3170         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3171           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3172         break;
3173       }
3174 
3175       if (SpecialTypes.size() != Record.size()) {
3176         Error("invalid special-types record");
3177         return Failure;
3178       }
3179 
3180       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3181         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3182         if (!SpecialTypes[I])
3183           SpecialTypes[I] = ID;
3184         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3185         // merge step?
3186       }
3187       break;
3188 
3189     case STATISTICS:
3190       TotalNumStatements += Record[0];
3191       TotalNumMacros += Record[1];
3192       TotalLexicalDeclContexts += Record[2];
3193       TotalVisibleDeclContexts += Record[3];
3194       break;
3195 
3196     case UNUSED_FILESCOPED_DECLS:
3197       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3198         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3199       break;
3200 
3201     case DELEGATING_CTORS:
3202       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3203         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3204       break;
3205 
3206     case WEAK_UNDECLARED_IDENTIFIERS:
3207       if (Record.size() % 4 != 0) {
3208         Error("invalid weak identifiers record");
3209         return Failure;
3210       }
3211 
3212       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3213       // files. This isn't the way to do it :)
3214       WeakUndeclaredIdentifiers.clear();
3215 
3216       // Translate the weak, undeclared identifiers into global IDs.
3217       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3218         WeakUndeclaredIdentifiers.push_back(
3219           getGlobalIdentifierID(F, Record[I++]));
3220         WeakUndeclaredIdentifiers.push_back(
3221           getGlobalIdentifierID(F, Record[I++]));
3222         WeakUndeclaredIdentifiers.push_back(
3223           ReadSourceLocation(F, Record, I).getRawEncoding());
3224         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3225       }
3226       break;
3227 
3228     case SELECTOR_OFFSETS: {
3229       F.SelectorOffsets = (const uint32_t *)Blob.data();
3230       F.LocalNumSelectors = Record[0];
3231       unsigned LocalBaseSelectorID = Record[1];
3232       F.BaseSelectorID = getTotalNumSelectors();
3233 
3234       if (F.LocalNumSelectors > 0) {
3235         // Introduce the global -> local mapping for selectors within this
3236         // module.
3237         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3238 
3239         // Introduce the local -> global mapping for selectors within this
3240         // module.
3241         F.SelectorRemap.insertOrReplace(
3242           std::make_pair(LocalBaseSelectorID,
3243                          F.BaseSelectorID - LocalBaseSelectorID));
3244 
3245         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3246       }
3247       break;
3248     }
3249 
3250     case METHOD_POOL:
3251       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3252       if (Record[0])
3253         F.SelectorLookupTable
3254           = ASTSelectorLookupTable::Create(
3255                         F.SelectorLookupTableData + Record[0],
3256                         F.SelectorLookupTableData,
3257                         ASTSelectorLookupTrait(*this, F));
3258       TotalNumMethodPoolEntries += Record[1];
3259       break;
3260 
3261     case REFERENCED_SELECTOR_POOL:
3262       if (!Record.empty()) {
3263         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3264           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3265                                                                 Record[Idx++]));
3266           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3267                                               getRawEncoding());
3268         }
3269       }
3270       break;
3271 
3272     case PP_CONDITIONAL_STACK:
3273       if (!Record.empty()) {
3274         unsigned Idx = 0, End = Record.size() - 1;
3275         bool ReachedEOFWhileSkipping = Record[Idx++];
3276         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3277         if (ReachedEOFWhileSkipping) {
3278           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3279           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3280           bool FoundNonSkipPortion = Record[Idx++];
3281           bool FoundElse = Record[Idx++];
3282           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3283           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3284                            FoundElse, ElseLoc);
3285         }
3286         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3287         while (Idx < End) {
3288           auto Loc = ReadSourceLocation(F, Record, Idx);
3289           bool WasSkipping = Record[Idx++];
3290           bool FoundNonSkip = Record[Idx++];
3291           bool FoundElse = Record[Idx++];
3292           ConditionalStack.push_back(
3293               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3294         }
3295         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3296       }
3297       break;
3298 
3299     case PP_COUNTER_VALUE:
3300       if (!Record.empty() && Listener)
3301         Listener->ReadCounter(F, Record[0]);
3302       break;
3303 
3304     case FILE_SORTED_DECLS:
3305       F.FileSortedDecls = (const DeclID *)Blob.data();
3306       F.NumFileSortedDecls = Record[0];
3307       break;
3308 
3309     case SOURCE_LOCATION_OFFSETS: {
3310       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3311       F.LocalNumSLocEntries = Record[0];
3312       unsigned SLocSpaceSize = Record[1];
3313       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3314           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3315                                               SLocSpaceSize);
3316       if (!F.SLocEntryBaseID) {
3317         Error("ran out of source locations");
3318         break;
3319       }
3320       // Make our entry in the range map. BaseID is negative and growing, so
3321       // we invert it. Because we invert it, though, we need the other end of
3322       // the range.
3323       unsigned RangeStart =
3324           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3325       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3326       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3327 
3328       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3329       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3330       GlobalSLocOffsetMap.insert(
3331           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3332                            - SLocSpaceSize,&F));
3333 
3334       // Initialize the remapping table.
3335       // Invalid stays invalid.
3336       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3337       // This module. Base was 2 when being compiled.
3338       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3339                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3340 
3341       TotalNumSLocEntries += F.LocalNumSLocEntries;
3342       break;
3343     }
3344 
3345     case MODULE_OFFSET_MAP:
3346       F.ModuleOffsetMap = Blob;
3347       break;
3348 
3349     case SOURCE_MANAGER_LINE_TABLE:
3350       if (ParseLineTable(F, Record))
3351         return Failure;
3352       break;
3353 
3354     case SOURCE_LOCATION_PRELOADS: {
3355       // Need to transform from the local view (1-based IDs) to the global view,
3356       // which is based off F.SLocEntryBaseID.
3357       if (!F.PreloadSLocEntries.empty()) {
3358         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3359         return Failure;
3360       }
3361 
3362       F.PreloadSLocEntries.swap(Record);
3363       break;
3364     }
3365 
3366     case EXT_VECTOR_DECLS:
3367       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3368         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3369       break;
3370 
3371     case VTABLE_USES:
3372       if (Record.size() % 3 != 0) {
3373         Error("Invalid VTABLE_USES record");
3374         return Failure;
3375       }
3376 
3377       // Later tables overwrite earlier ones.
3378       // FIXME: Modules will have some trouble with this. This is clearly not
3379       // the right way to do this.
3380       VTableUses.clear();
3381 
3382       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3383         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3384         VTableUses.push_back(
3385           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3386         VTableUses.push_back(Record[Idx++]);
3387       }
3388       break;
3389 
3390     case PENDING_IMPLICIT_INSTANTIATIONS:
3391       if (PendingInstantiations.size() % 2 != 0) {
3392         Error("Invalid existing PendingInstantiations");
3393         return Failure;
3394       }
3395 
3396       if (Record.size() % 2 != 0) {
3397         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3398         return Failure;
3399       }
3400 
3401       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3402         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3403         PendingInstantiations.push_back(
3404           ReadSourceLocation(F, Record, I).getRawEncoding());
3405       }
3406       break;
3407 
3408     case SEMA_DECL_REFS:
3409       if (Record.size() != 3) {
3410         Error("Invalid SEMA_DECL_REFS block");
3411         return Failure;
3412       }
3413       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3414         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3415       break;
3416 
3417     case PPD_ENTITIES_OFFSETS: {
3418       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3419       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3420       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3421 
3422       unsigned LocalBasePreprocessedEntityID = Record[0];
3423 
3424       unsigned StartingID;
3425       if (!PP.getPreprocessingRecord())
3426         PP.createPreprocessingRecord();
3427       if (!PP.getPreprocessingRecord()->getExternalSource())
3428         PP.getPreprocessingRecord()->SetExternalSource(*this);
3429       StartingID
3430         = PP.getPreprocessingRecord()
3431             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3432       F.BasePreprocessedEntityID = StartingID;
3433 
3434       if (F.NumPreprocessedEntities > 0) {
3435         // Introduce the global -> local mapping for preprocessed entities in
3436         // this module.
3437         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3438 
3439         // Introduce the local -> global mapping for preprocessed entities in
3440         // this module.
3441         F.PreprocessedEntityRemap.insertOrReplace(
3442           std::make_pair(LocalBasePreprocessedEntityID,
3443             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3444       }
3445 
3446       break;
3447     }
3448 
3449     case PPD_SKIPPED_RANGES: {
3450       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3451       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3452       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3453 
3454       if (!PP.getPreprocessingRecord())
3455         PP.createPreprocessingRecord();
3456       if (!PP.getPreprocessingRecord()->getExternalSource())
3457         PP.getPreprocessingRecord()->SetExternalSource(*this);
3458       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3459           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3460 
3461       if (F.NumPreprocessedSkippedRanges > 0)
3462         GlobalSkippedRangeMap.insert(
3463             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3464       break;
3465     }
3466 
3467     case DECL_UPDATE_OFFSETS:
3468       if (Record.size() % 2 != 0) {
3469         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3470         return Failure;
3471       }
3472       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3473         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3474         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3475 
3476         // If we've already loaded the decl, perform the updates when we finish
3477         // loading this block.
3478         if (Decl *D = GetExistingDecl(ID))
3479           PendingUpdateRecords.push_back(
3480               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3481       }
3482       break;
3483 
3484     case OBJC_CATEGORIES_MAP:
3485       if (F.LocalNumObjCCategoriesInMap != 0) {
3486         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3487         return Failure;
3488       }
3489 
3490       F.LocalNumObjCCategoriesInMap = Record[0];
3491       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3492       break;
3493 
3494     case OBJC_CATEGORIES:
3495       F.ObjCCategories.swap(Record);
3496       break;
3497 
3498     case CUDA_SPECIAL_DECL_REFS:
3499       // Later tables overwrite earlier ones.
3500       // FIXME: Modules will have trouble with this.
3501       CUDASpecialDeclRefs.clear();
3502       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3503         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3504       break;
3505 
3506     case HEADER_SEARCH_TABLE:
3507       F.HeaderFileInfoTableData = Blob.data();
3508       F.LocalNumHeaderFileInfos = Record[1];
3509       if (Record[0]) {
3510         F.HeaderFileInfoTable
3511           = HeaderFileInfoLookupTable::Create(
3512                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3513                    (const unsigned char *)F.HeaderFileInfoTableData,
3514                    HeaderFileInfoTrait(*this, F,
3515                                        &PP.getHeaderSearchInfo(),
3516                                        Blob.data() + Record[2]));
3517 
3518         PP.getHeaderSearchInfo().SetExternalSource(this);
3519         if (!PP.getHeaderSearchInfo().getExternalLookup())
3520           PP.getHeaderSearchInfo().SetExternalLookup(this);
3521       }
3522       break;
3523 
3524     case FP_PRAGMA_OPTIONS:
3525       // Later tables overwrite earlier ones.
3526       FPPragmaOptions.swap(Record);
3527       break;
3528 
3529     case OPENCL_EXTENSIONS:
3530       for (unsigned I = 0, E = Record.size(); I != E; ) {
3531         auto Name = ReadString(Record, I);
3532         auto &Opt = OpenCLExtensions.OptMap[Name];
3533         Opt.Supported = Record[I++] != 0;
3534         Opt.Enabled = Record[I++] != 0;
3535         Opt.Avail = Record[I++];
3536         Opt.Core = Record[I++];
3537       }
3538       break;
3539 
3540     case OPENCL_EXTENSION_TYPES:
3541       for (unsigned I = 0, E = Record.size(); I != E;) {
3542         auto TypeID = static_cast<::TypeID>(Record[I++]);
3543         auto *Type = GetType(TypeID).getTypePtr();
3544         auto NumExt = static_cast<unsigned>(Record[I++]);
3545         for (unsigned II = 0; II != NumExt; ++II) {
3546           auto Ext = ReadString(Record, I);
3547           OpenCLTypeExtMap[Type].insert(Ext);
3548         }
3549       }
3550       break;
3551 
3552     case OPENCL_EXTENSION_DECLS:
3553       for (unsigned I = 0, E = Record.size(); I != E;) {
3554         auto DeclID = static_cast<::DeclID>(Record[I++]);
3555         auto *Decl = GetDecl(DeclID);
3556         auto NumExt = static_cast<unsigned>(Record[I++]);
3557         for (unsigned II = 0; II != NumExt; ++II) {
3558           auto Ext = ReadString(Record, I);
3559           OpenCLDeclExtMap[Decl].insert(Ext);
3560         }
3561       }
3562       break;
3563 
3564     case TENTATIVE_DEFINITIONS:
3565       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3566         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3567       break;
3568 
3569     case KNOWN_NAMESPACES:
3570       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3571         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3572       break;
3573 
3574     case UNDEFINED_BUT_USED:
3575       if (UndefinedButUsed.size() % 2 != 0) {
3576         Error("Invalid existing UndefinedButUsed");
3577         return Failure;
3578       }
3579 
3580       if (Record.size() % 2 != 0) {
3581         Error("invalid undefined-but-used record");
3582         return Failure;
3583       }
3584       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3585         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3586         UndefinedButUsed.push_back(
3587             ReadSourceLocation(F, Record, I).getRawEncoding());
3588       }
3589       break;
3590 
3591     case DELETE_EXPRS_TO_ANALYZE:
3592       for (unsigned I = 0, N = Record.size(); I != N;) {
3593         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3594         const uint64_t Count = Record[I++];
3595         DelayedDeleteExprs.push_back(Count);
3596         for (uint64_t C = 0; C < Count; ++C) {
3597           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3598           bool IsArrayForm = Record[I++] == 1;
3599           DelayedDeleteExprs.push_back(IsArrayForm);
3600         }
3601       }
3602       break;
3603 
3604     case IMPORTED_MODULES:
3605       if (!F.isModule()) {
3606         // If we aren't loading a module (which has its own exports), make
3607         // all of the imported modules visible.
3608         // FIXME: Deal with macros-only imports.
3609         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3610           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3611           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3612           if (GlobalID) {
3613             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3614             if (DeserializationListener)
3615               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3616           }
3617         }
3618       }
3619       break;
3620 
3621     case MACRO_OFFSET: {
3622       if (F.LocalNumMacros != 0) {
3623         Error("duplicate MACRO_OFFSET record in AST file");
3624         return Failure;
3625       }
3626       F.MacroOffsets = (const uint32_t *)Blob.data();
3627       F.LocalNumMacros = Record[0];
3628       unsigned LocalBaseMacroID = Record[1];
3629       F.BaseMacroID = getTotalNumMacros();
3630 
3631       if (F.LocalNumMacros > 0) {
3632         // Introduce the global -> local mapping for macros within this module.
3633         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3634 
3635         // Introduce the local -> global mapping for macros within this module.
3636         F.MacroRemap.insertOrReplace(
3637           std::make_pair(LocalBaseMacroID,
3638                          F.BaseMacroID - LocalBaseMacroID));
3639 
3640         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3641       }
3642       break;
3643     }
3644 
3645     case LATE_PARSED_TEMPLATE:
3646       LateParsedTemplates.append(Record.begin(), Record.end());
3647       break;
3648 
3649     case OPTIMIZE_PRAGMA_OPTIONS:
3650       if (Record.size() != 1) {
3651         Error("invalid pragma optimize record");
3652         return Failure;
3653       }
3654       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3655       break;
3656 
3657     case MSSTRUCT_PRAGMA_OPTIONS:
3658       if (Record.size() != 1) {
3659         Error("invalid pragma ms_struct record");
3660         return Failure;
3661       }
3662       PragmaMSStructState = Record[0];
3663       break;
3664 
3665     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3666       if (Record.size() != 2) {
3667         Error("invalid pragma ms_struct record");
3668         return Failure;
3669       }
3670       PragmaMSPointersToMembersState = Record[0];
3671       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3672       break;
3673 
3674     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3675       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3676         UnusedLocalTypedefNameCandidates.push_back(
3677             getGlobalDeclID(F, Record[I]));
3678       break;
3679 
3680     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3681       if (Record.size() != 1) {
3682         Error("invalid cuda pragma options record");
3683         return Failure;
3684       }
3685       ForceCUDAHostDeviceDepth = Record[0];
3686       break;
3687 
3688     case PACK_PRAGMA_OPTIONS: {
3689       if (Record.size() < 3) {
3690         Error("invalid pragma pack record");
3691         return Failure;
3692       }
3693       PragmaPackCurrentValue = Record[0];
3694       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3695       unsigned NumStackEntries = Record[2];
3696       unsigned Idx = 3;
3697       // Reset the stack when importing a new module.
3698       PragmaPackStack.clear();
3699       for (unsigned I = 0; I < NumStackEntries; ++I) {
3700         PragmaPackStackEntry Entry;
3701         Entry.Value = Record[Idx++];
3702         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3703         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3704         PragmaPackStrings.push_back(ReadString(Record, Idx));
3705         Entry.SlotLabel = PragmaPackStrings.back();
3706         PragmaPackStack.push_back(Entry);
3707       }
3708       break;
3709     }
3710     }
3711   }
3712 }
3713 
3714 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3715   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3716 
3717   // Additional remapping information.
3718   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3719   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3720   F.ModuleOffsetMap = StringRef();
3721 
3722   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3723   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3724     F.SLocRemap.insert(std::make_pair(0U, 0));
3725     F.SLocRemap.insert(std::make_pair(2U, 1));
3726   }
3727 
3728   // Continuous range maps we may be updating in our module.
3729   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3730   RemapBuilder SLocRemap(F.SLocRemap);
3731   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3732   RemapBuilder MacroRemap(F.MacroRemap);
3733   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3734   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3735   RemapBuilder SelectorRemap(F.SelectorRemap);
3736   RemapBuilder DeclRemap(F.DeclRemap);
3737   RemapBuilder TypeRemap(F.TypeRemap);
3738 
3739   while (Data < DataEnd) {
3740     // FIXME: Looking up dependency modules by filename is horrible. Let's
3741     // start fixing this with prebuilt and explicit modules and see how it
3742     // goes...
3743     using namespace llvm::support;
3744     ModuleKind Kind = static_cast<ModuleKind>(
3745       endian::readNext<uint8_t, little, unaligned>(Data));
3746     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3747     StringRef Name = StringRef((const char*)Data, Len);
3748     Data += Len;
3749     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3750                       ? ModuleMgr.lookupByModuleName(Name)
3751                       : ModuleMgr.lookupByFileName(Name));
3752     if (!OM) {
3753       std::string Msg =
3754           "SourceLocation remap refers to unknown module, cannot find ";
3755       Msg.append(Name);
3756       Error(Msg);
3757       return;
3758     }
3759 
3760     uint32_t SLocOffset =
3761         endian::readNext<uint32_t, little, unaligned>(Data);
3762     uint32_t IdentifierIDOffset =
3763         endian::readNext<uint32_t, little, unaligned>(Data);
3764     uint32_t MacroIDOffset =
3765         endian::readNext<uint32_t, little, unaligned>(Data);
3766     uint32_t PreprocessedEntityIDOffset =
3767         endian::readNext<uint32_t, little, unaligned>(Data);
3768     uint32_t SubmoduleIDOffset =
3769         endian::readNext<uint32_t, little, unaligned>(Data);
3770     uint32_t SelectorIDOffset =
3771         endian::readNext<uint32_t, little, unaligned>(Data);
3772     uint32_t DeclIDOffset =
3773         endian::readNext<uint32_t, little, unaligned>(Data);
3774     uint32_t TypeIndexOffset =
3775         endian::readNext<uint32_t, little, unaligned>(Data);
3776 
3777     uint32_t None = std::numeric_limits<uint32_t>::max();
3778 
3779     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3780                          RemapBuilder &Remap) {
3781       if (Offset != None)
3782         Remap.insert(std::make_pair(Offset,
3783                                     static_cast<int>(BaseOffset - Offset)));
3784     };
3785     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3786     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3787     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3788     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3789               PreprocessedEntityRemap);
3790     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3791     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3792     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3793     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3794 
3795     // Global -> local mappings.
3796     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3797   }
3798 }
3799 
3800 ASTReader::ASTReadResult
3801 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3802                                   const ModuleFile *ImportedBy,
3803                                   unsigned ClientLoadCapabilities) {
3804   unsigned Idx = 0;
3805   F.ModuleMapPath = ReadPath(F, Record, Idx);
3806 
3807   // Try to resolve ModuleName in the current header search context and
3808   // verify that it is found in the same module map file as we saved. If the
3809   // top-level AST file is a main file, skip this check because there is no
3810   // usable header search context.
3811   assert(!F.ModuleName.empty() &&
3812          "MODULE_NAME should come before MODULE_MAP_FILE");
3813   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3814     // An implicitly-loaded module file should have its module listed in some
3815     // module map file that we've already loaded.
3816     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3817     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3818     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3819     // Don't emit module relocation error if we have -fno-validate-pch
3820     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3821       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3822         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3823           // This module was defined by an imported (explicit) module.
3824           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3825                                                << ASTFE->getName();
3826         } else {
3827           // This module was built with a different module map.
3828           Diag(diag::err_imported_module_not_found)
3829               << F.ModuleName << F.FileName
3830               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3831               << !ImportedBy;
3832           // In case it was imported by a PCH, there's a chance the user is
3833           // just missing to include the search path to the directory containing
3834           // the modulemap.
3835           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3836             Diag(diag::note_imported_by_pch_module_not_found)
3837                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3838         }
3839       }
3840       return OutOfDate;
3841     }
3842 
3843     assert(M->Name == F.ModuleName && "found module with different name");
3844 
3845     // Check the primary module map file.
3846     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3847     if (!StoredModMap || *StoredModMap != ModMap) {
3848       assert(ModMap && "found module is missing module map file");
3849       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3850              "top-level import should be verified");
3851       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3852       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3853         Diag(diag::err_imported_module_modmap_changed)
3854             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3855             << ModMap->getName() << F.ModuleMapPath << NotImported;
3856       return OutOfDate;
3857     }
3858 
3859     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3860     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3861       // FIXME: we should use input files rather than storing names.
3862       std::string Filename = ReadPath(F, Record, Idx);
3863       auto F = FileMgr.getFile(Filename, false, false);
3864       if (!F) {
3865         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3866           Error("could not find file '" + Filename +"' referenced by AST file");
3867         return OutOfDate;
3868       }
3869       AdditionalStoredMaps.insert(*F);
3870     }
3871 
3872     // Check any additional module map files (e.g. module.private.modulemap)
3873     // that are not in the pcm.
3874     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3875       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3876         // Remove files that match
3877         // Note: SmallPtrSet::erase is really remove
3878         if (!AdditionalStoredMaps.erase(ModMap)) {
3879           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3880             Diag(diag::err_module_different_modmap)
3881               << F.ModuleName << /*new*/0 << ModMap->getName();
3882           return OutOfDate;
3883         }
3884       }
3885     }
3886 
3887     // Check any additional module map files that are in the pcm, but not
3888     // found in header search. Cases that match are already removed.
3889     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3890       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3891         Diag(diag::err_module_different_modmap)
3892           << F.ModuleName << /*not new*/1 << ModMap->getName();
3893       return OutOfDate;
3894     }
3895   }
3896 
3897   if (Listener)
3898     Listener->ReadModuleMapFile(F.ModuleMapPath);
3899   return Success;
3900 }
3901 
3902 /// Move the given method to the back of the global list of methods.
3903 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3904   // Find the entry for this selector in the method pool.
3905   Sema::GlobalMethodPool::iterator Known
3906     = S.MethodPool.find(Method->getSelector());
3907   if (Known == S.MethodPool.end())
3908     return;
3909 
3910   // Retrieve the appropriate method list.
3911   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3912                                                     : Known->second.second;
3913   bool Found = false;
3914   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3915     if (!Found) {
3916       if (List->getMethod() == Method) {
3917         Found = true;
3918       } else {
3919         // Keep searching.
3920         continue;
3921       }
3922     }
3923 
3924     if (List->getNext())
3925       List->setMethod(List->getNext()->getMethod());
3926     else
3927       List->setMethod(Method);
3928   }
3929 }
3930 
3931 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3932   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3933   for (Decl *D : Names) {
3934     bool wasHidden = D->isHidden();
3935     D->setVisibleDespiteOwningModule();
3936 
3937     if (wasHidden && SemaObj) {
3938       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3939         moveMethodToBackOfGlobalList(*SemaObj, Method);
3940       }
3941     }
3942   }
3943 }
3944 
3945 void ASTReader::makeModuleVisible(Module *Mod,
3946                                   Module::NameVisibilityKind NameVisibility,
3947                                   SourceLocation ImportLoc) {
3948   llvm::SmallPtrSet<Module *, 4> Visited;
3949   SmallVector<Module *, 4> Stack;
3950   Stack.push_back(Mod);
3951   while (!Stack.empty()) {
3952     Mod = Stack.pop_back_val();
3953 
3954     if (NameVisibility <= Mod->NameVisibility) {
3955       // This module already has this level of visibility (or greater), so
3956       // there is nothing more to do.
3957       continue;
3958     }
3959 
3960     if (!Mod->isAvailable()) {
3961       // Modules that aren't available cannot be made visible.
3962       continue;
3963     }
3964 
3965     // Update the module's name visibility.
3966     Mod->NameVisibility = NameVisibility;
3967 
3968     // If we've already deserialized any names from this module,
3969     // mark them as visible.
3970     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3971     if (Hidden != HiddenNamesMap.end()) {
3972       auto HiddenNames = std::move(*Hidden);
3973       HiddenNamesMap.erase(Hidden);
3974       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3975       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3976              "making names visible added hidden names");
3977     }
3978 
3979     // Push any exported modules onto the stack to be marked as visible.
3980     SmallVector<Module *, 16> Exports;
3981     Mod->getExportedModules(Exports);
3982     for (SmallVectorImpl<Module *>::iterator
3983            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3984       Module *Exported = *I;
3985       if (Visited.insert(Exported).second)
3986         Stack.push_back(Exported);
3987     }
3988   }
3989 }
3990 
3991 /// We've merged the definition \p MergedDef into the existing definition
3992 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3993 /// visible.
3994 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3995                                           NamedDecl *MergedDef) {
3996   if (Def->isHidden()) {
3997     // If MergedDef is visible or becomes visible, make the definition visible.
3998     if (!MergedDef->isHidden())
3999       Def->setVisibleDespiteOwningModule();
4000     else {
4001       getContext().mergeDefinitionIntoModule(
4002           Def, MergedDef->getImportedOwningModule(),
4003           /*NotifyListeners*/ false);
4004       PendingMergedDefinitionsToDeduplicate.insert(Def);
4005     }
4006   }
4007 }
4008 
4009 bool ASTReader::loadGlobalIndex() {
4010   if (GlobalIndex)
4011     return false;
4012 
4013   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4014       !PP.getLangOpts().Modules)
4015     return true;
4016 
4017   // Try to load the global index.
4018   TriedLoadingGlobalIndex = true;
4019   StringRef ModuleCachePath
4020     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4021   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4022       GlobalModuleIndex::readIndex(ModuleCachePath);
4023   if (llvm::Error Err = std::move(Result.second)) {
4024     assert(!Result.first);
4025     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4026     return true;
4027   }
4028 
4029   GlobalIndex.reset(Result.first);
4030   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4031   return false;
4032 }
4033 
4034 bool ASTReader::isGlobalIndexUnavailable() const {
4035   return PP.getLangOpts().Modules && UseGlobalIndex &&
4036          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4037 }
4038 
4039 static void updateModuleTimestamp(ModuleFile &MF) {
4040   // Overwrite the timestamp file contents so that file's mtime changes.
4041   std::string TimestampFilename = MF.getTimestampFilename();
4042   std::error_code EC;
4043   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4044   if (EC)
4045     return;
4046   OS << "Timestamp file\n";
4047   OS.close();
4048   OS.clear_error(); // Avoid triggering a fatal error.
4049 }
4050 
4051 /// Given a cursor at the start of an AST file, scan ahead and drop the
4052 /// cursor into the start of the given block ID, returning false on success and
4053 /// true on failure.
4054 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4055   while (true) {
4056     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4057     if (!MaybeEntry) {
4058       // FIXME this drops errors on the floor.
4059       consumeError(MaybeEntry.takeError());
4060       return true;
4061     }
4062     llvm::BitstreamEntry Entry = MaybeEntry.get();
4063 
4064     switch (Entry.Kind) {
4065     case llvm::BitstreamEntry::Error:
4066     case llvm::BitstreamEntry::EndBlock:
4067       return true;
4068 
4069     case llvm::BitstreamEntry::Record:
4070       // Ignore top-level records.
4071       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4072         break;
4073       else {
4074         // FIXME this drops errors on the floor.
4075         consumeError(Skipped.takeError());
4076         return true;
4077       }
4078 
4079     case llvm::BitstreamEntry::SubBlock:
4080       if (Entry.ID == BlockID) {
4081         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4082           // FIXME this drops the error on the floor.
4083           consumeError(std::move(Err));
4084           return true;
4085         }
4086         // Found it!
4087         return false;
4088       }
4089 
4090       if (llvm::Error Err = Cursor.SkipBlock()) {
4091         // FIXME this drops the error on the floor.
4092         consumeError(std::move(Err));
4093         return true;
4094       }
4095     }
4096   }
4097 }
4098 
4099 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4100                                             ModuleKind Type,
4101                                             SourceLocation ImportLoc,
4102                                             unsigned ClientLoadCapabilities,
4103                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4104   llvm::SaveAndRestore<SourceLocation>
4105     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4106 
4107   // Defer any pending actions until we get to the end of reading the AST file.
4108   Deserializing AnASTFile(this);
4109 
4110   // Bump the generation number.
4111   unsigned PreviousGeneration = 0;
4112   if (ContextObj)
4113     PreviousGeneration = incrementGeneration(*ContextObj);
4114 
4115   unsigned NumModules = ModuleMgr.size();
4116   SmallVector<ImportedModule, 4> Loaded;
4117   switch (ASTReadResult ReadResult =
4118               ReadASTCore(FileName, Type, ImportLoc,
4119                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4120                           ASTFileSignature(), ClientLoadCapabilities)) {
4121   case Failure:
4122   case Missing:
4123   case OutOfDate:
4124   case VersionMismatch:
4125   case ConfigurationMismatch:
4126   case HadErrors: {
4127     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4128     for (const ImportedModule &IM : Loaded)
4129       LoadedSet.insert(IM.Mod);
4130 
4131     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4132                             PP.getLangOpts().Modules
4133                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4134                                 : nullptr);
4135 
4136     // If we find that any modules are unusable, the global index is going
4137     // to be out-of-date. Just remove it.
4138     GlobalIndex.reset();
4139     ModuleMgr.setGlobalIndex(nullptr);
4140     return ReadResult;
4141   }
4142   case Success:
4143     break;
4144   }
4145 
4146   // Here comes stuff that we only do once the entire chain is loaded.
4147 
4148   // Load the AST blocks of all of the modules that we loaded.
4149   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4150                                               MEnd = Loaded.end();
4151        M != MEnd; ++M) {
4152     ModuleFile &F = *M->Mod;
4153 
4154     // Read the AST block.
4155     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4156       return Result;
4157 
4158     // Read the extension blocks.
4159     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4160       if (ASTReadResult Result = ReadExtensionBlock(F))
4161         return Result;
4162     }
4163 
4164     // Once read, set the ModuleFile bit base offset and update the size in
4165     // bits of all files we've seen.
4166     F.GlobalBitOffset = TotalModulesSizeInBits;
4167     TotalModulesSizeInBits += F.SizeInBits;
4168     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4169 
4170     // Preload SLocEntries.
4171     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4172       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4173       // Load it through the SourceManager and don't call ReadSLocEntry()
4174       // directly because the entry may have already been loaded in which case
4175       // calling ReadSLocEntry() directly would trigger an assertion in
4176       // SourceManager.
4177       SourceMgr.getLoadedSLocEntryByID(Index);
4178     }
4179 
4180     // Map the original source file ID into the ID space of the current
4181     // compilation.
4182     if (F.OriginalSourceFileID.isValid()) {
4183       F.OriginalSourceFileID = FileID::get(
4184           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4185     }
4186 
4187     // Preload all the pending interesting identifiers by marking them out of
4188     // date.
4189     for (auto Offset : F.PreloadIdentifierOffsets) {
4190       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4191           F.IdentifierTableData + Offset);
4192 
4193       ASTIdentifierLookupTrait Trait(*this, F);
4194       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4195       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4196       auto &II = PP.getIdentifierTable().getOwn(Key);
4197       II.setOutOfDate(true);
4198 
4199       // Mark this identifier as being from an AST file so that we can track
4200       // whether we need to serialize it.
4201       markIdentifierFromAST(*this, II);
4202 
4203       // Associate the ID with the identifier so that the writer can reuse it.
4204       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4205       SetIdentifierInfo(ID, &II);
4206     }
4207   }
4208 
4209   // Setup the import locations and notify the module manager that we've
4210   // committed to these module files.
4211   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4212                                               MEnd = Loaded.end();
4213        M != MEnd; ++M) {
4214     ModuleFile &F = *M->Mod;
4215 
4216     ModuleMgr.moduleFileAccepted(&F);
4217 
4218     // Set the import location.
4219     F.DirectImportLoc = ImportLoc;
4220     // FIXME: We assume that locations from PCH / preamble do not need
4221     // any translation.
4222     if (!M->ImportedBy)
4223       F.ImportLoc = M->ImportLoc;
4224     else
4225       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4226   }
4227 
4228   if (!PP.getLangOpts().CPlusPlus ||
4229       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4230        Type != MK_PrebuiltModule)) {
4231     // Mark all of the identifiers in the identifier table as being out of date,
4232     // so that various accessors know to check the loaded modules when the
4233     // identifier is used.
4234     //
4235     // For C++ modules, we don't need information on many identifiers (just
4236     // those that provide macros or are poisoned), so we mark all of
4237     // the interesting ones via PreloadIdentifierOffsets.
4238     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4239                                 IdEnd = PP.getIdentifierTable().end();
4240          Id != IdEnd; ++Id)
4241       Id->second->setOutOfDate(true);
4242   }
4243   // Mark selectors as out of date.
4244   for (auto Sel : SelectorGeneration)
4245     SelectorOutOfDate[Sel.first] = true;
4246 
4247   // Resolve any unresolved module exports.
4248   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4249     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4250     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4251     Module *ResolvedMod = getSubmodule(GlobalID);
4252 
4253     switch (Unresolved.Kind) {
4254     case UnresolvedModuleRef::Conflict:
4255       if (ResolvedMod) {
4256         Module::Conflict Conflict;
4257         Conflict.Other = ResolvedMod;
4258         Conflict.Message = Unresolved.String.str();
4259         Unresolved.Mod->Conflicts.push_back(Conflict);
4260       }
4261       continue;
4262 
4263     case UnresolvedModuleRef::Import:
4264       if (ResolvedMod)
4265         Unresolved.Mod->Imports.insert(ResolvedMod);
4266       continue;
4267 
4268     case UnresolvedModuleRef::Export:
4269       if (ResolvedMod || Unresolved.IsWildcard)
4270         Unresolved.Mod->Exports.push_back(
4271           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4272       continue;
4273     }
4274   }
4275   UnresolvedModuleRefs.clear();
4276 
4277   if (Imported)
4278     Imported->append(ImportedModules.begin(),
4279                      ImportedModules.end());
4280 
4281   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4282   // Might be unnecessary as use declarations are only used to build the
4283   // module itself.
4284 
4285   if (ContextObj)
4286     InitializeContext();
4287 
4288   if (SemaObj)
4289     UpdateSema();
4290 
4291   if (DeserializationListener)
4292     DeserializationListener->ReaderInitialized(this);
4293 
4294   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4295   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4296     // If this AST file is a precompiled preamble, then set the
4297     // preamble file ID of the source manager to the file source file
4298     // from which the preamble was built.
4299     if (Type == MK_Preamble) {
4300       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4301     } else if (Type == MK_MainFile) {
4302       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4303     }
4304   }
4305 
4306   // For any Objective-C class definitions we have already loaded, make sure
4307   // that we load any additional categories.
4308   if (ContextObj) {
4309     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4310       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4311                          ObjCClassesLoaded[I],
4312                          PreviousGeneration);
4313     }
4314   }
4315 
4316   if (PP.getHeaderSearchInfo()
4317           .getHeaderSearchOpts()
4318           .ModulesValidateOncePerBuildSession) {
4319     // Now we are certain that the module and all modules it depends on are
4320     // up to date.  Create or update timestamp files for modules that are
4321     // located in the module cache (not for PCH files that could be anywhere
4322     // in the filesystem).
4323     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4324       ImportedModule &M = Loaded[I];
4325       if (M.Mod->Kind == MK_ImplicitModule) {
4326         updateModuleTimestamp(*M.Mod);
4327       }
4328     }
4329   }
4330 
4331   return Success;
4332 }
4333 
4334 static ASTFileSignature readASTFileSignature(StringRef PCH);
4335 
4336 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4337 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4338   // FIXME checking magic headers is done in other places such as
4339   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4340   // always done the same. Unify it all with a helper.
4341   if (!Stream.canSkipToPos(4))
4342     return llvm::createStringError(std::errc::illegal_byte_sequence,
4343                                    "file too small to contain AST file magic");
4344   for (unsigned C : {'C', 'P', 'C', 'H'})
4345     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4346       if (Res.get() != C)
4347         return llvm::createStringError(
4348             std::errc::illegal_byte_sequence,
4349             "file doesn't start with AST file magic");
4350     } else
4351       return Res.takeError();
4352   return llvm::Error::success();
4353 }
4354 
4355 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4356   switch (Kind) {
4357   case MK_PCH:
4358     return 0; // PCH
4359   case MK_ImplicitModule:
4360   case MK_ExplicitModule:
4361   case MK_PrebuiltModule:
4362     return 1; // module
4363   case MK_MainFile:
4364   case MK_Preamble:
4365     return 2; // main source file
4366   }
4367   llvm_unreachable("unknown module kind");
4368 }
4369 
4370 ASTReader::ASTReadResult
4371 ASTReader::ReadASTCore(StringRef FileName,
4372                        ModuleKind Type,
4373                        SourceLocation ImportLoc,
4374                        ModuleFile *ImportedBy,
4375                        SmallVectorImpl<ImportedModule> &Loaded,
4376                        off_t ExpectedSize, time_t ExpectedModTime,
4377                        ASTFileSignature ExpectedSignature,
4378                        unsigned ClientLoadCapabilities) {
4379   ModuleFile *M;
4380   std::string ErrorStr;
4381   ModuleManager::AddModuleResult AddResult
4382     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4383                           getGeneration(), ExpectedSize, ExpectedModTime,
4384                           ExpectedSignature, readASTFileSignature,
4385                           M, ErrorStr);
4386 
4387   switch (AddResult) {
4388   case ModuleManager::AlreadyLoaded:
4389     Diag(diag::remark_module_import)
4390         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4391         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4392     return Success;
4393 
4394   case ModuleManager::NewlyLoaded:
4395     // Load module file below.
4396     break;
4397 
4398   case ModuleManager::Missing:
4399     // The module file was missing; if the client can handle that, return
4400     // it.
4401     if (ClientLoadCapabilities & ARR_Missing)
4402       return Missing;
4403 
4404     // Otherwise, return an error.
4405     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4406                                           << FileName << !ErrorStr.empty()
4407                                           << ErrorStr;
4408     return Failure;
4409 
4410   case ModuleManager::OutOfDate:
4411     // We couldn't load the module file because it is out-of-date. If the
4412     // client can handle out-of-date, return it.
4413     if (ClientLoadCapabilities & ARR_OutOfDate)
4414       return OutOfDate;
4415 
4416     // Otherwise, return an error.
4417     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4418                                             << FileName << !ErrorStr.empty()
4419                                             << ErrorStr;
4420     return Failure;
4421   }
4422 
4423   assert(M && "Missing module file");
4424 
4425   bool ShouldFinalizePCM = false;
4426   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4427     auto &MC = getModuleManager().getModuleCache();
4428     if (ShouldFinalizePCM)
4429       MC.finalizePCM(FileName);
4430     else
4431       MC.tryToDropPCM(FileName);
4432   });
4433   ModuleFile &F = *M;
4434   BitstreamCursor &Stream = F.Stream;
4435   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4436   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4437 
4438   // Sniff for the signature.
4439   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4440     Diag(diag::err_module_file_invalid)
4441         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4442     return Failure;
4443   }
4444 
4445   // This is used for compatibility with older PCH formats.
4446   bool HaveReadControlBlock = false;
4447   while (true) {
4448     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4449     if (!MaybeEntry) {
4450       Error(MaybeEntry.takeError());
4451       return Failure;
4452     }
4453     llvm::BitstreamEntry Entry = MaybeEntry.get();
4454 
4455     switch (Entry.Kind) {
4456     case llvm::BitstreamEntry::Error:
4457     case llvm::BitstreamEntry::Record:
4458     case llvm::BitstreamEntry::EndBlock:
4459       Error("invalid record at top-level of AST file");
4460       return Failure;
4461 
4462     case llvm::BitstreamEntry::SubBlock:
4463       break;
4464     }
4465 
4466     switch (Entry.ID) {
4467     case CONTROL_BLOCK_ID:
4468       HaveReadControlBlock = true;
4469       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4470       case Success:
4471         // Check that we didn't try to load a non-module AST file as a module.
4472         //
4473         // FIXME: Should we also perform the converse check? Loading a module as
4474         // a PCH file sort of works, but it's a bit wonky.
4475         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4476              Type == MK_PrebuiltModule) &&
4477             F.ModuleName.empty()) {
4478           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4479           if (Result != OutOfDate ||
4480               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4481             Diag(diag::err_module_file_not_module) << FileName;
4482           return Result;
4483         }
4484         break;
4485 
4486       case Failure: return Failure;
4487       case Missing: return Missing;
4488       case OutOfDate: return OutOfDate;
4489       case VersionMismatch: return VersionMismatch;
4490       case ConfigurationMismatch: return ConfigurationMismatch;
4491       case HadErrors: return HadErrors;
4492       }
4493       break;
4494 
4495     case AST_BLOCK_ID:
4496       if (!HaveReadControlBlock) {
4497         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4498           Diag(diag::err_pch_version_too_old);
4499         return VersionMismatch;
4500       }
4501 
4502       // Record that we've loaded this module.
4503       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4504       ShouldFinalizePCM = true;
4505       return Success;
4506 
4507     case UNHASHED_CONTROL_BLOCK_ID:
4508       // This block is handled using look-ahead during ReadControlBlock.  We
4509       // shouldn't get here!
4510       Error("malformed block record in AST file");
4511       return Failure;
4512 
4513     default:
4514       if (llvm::Error Err = Stream.SkipBlock()) {
4515         Error(std::move(Err));
4516         return Failure;
4517       }
4518       break;
4519     }
4520   }
4521 
4522   llvm_unreachable("unexpected break; expected return");
4523 }
4524 
4525 ASTReader::ASTReadResult
4526 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4527                                     unsigned ClientLoadCapabilities) {
4528   const HeaderSearchOptions &HSOpts =
4529       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4530   bool AllowCompatibleConfigurationMismatch =
4531       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4532 
4533   ASTReadResult Result = readUnhashedControlBlockImpl(
4534       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4535       Listener.get(),
4536       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4537 
4538   // If F was directly imported by another module, it's implicitly validated by
4539   // the importing module.
4540   if (DisableValidation || WasImportedBy ||
4541       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4542     return Success;
4543 
4544   if (Result == Failure) {
4545     Error("malformed block record in AST file");
4546     return Failure;
4547   }
4548 
4549   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4550     // If this module has already been finalized in the ModuleCache, we're stuck
4551     // with it; we can only load a single version of each module.
4552     //
4553     // This can happen when a module is imported in two contexts: in one, as a
4554     // user module; in another, as a system module (due to an import from
4555     // another module marked with the [system] flag).  It usually indicates a
4556     // bug in the module map: this module should also be marked with [system].
4557     //
4558     // If -Wno-system-headers (the default), and the first import is as a
4559     // system module, then validation will fail during the as-user import,
4560     // since -Werror flags won't have been validated.  However, it's reasonable
4561     // to treat this consistently as a system module.
4562     //
4563     // If -Wsystem-headers, the PCM on disk was built with
4564     // -Wno-system-headers, and the first import is as a user module, then
4565     // validation will fail during the as-system import since the PCM on disk
4566     // doesn't guarantee that -Werror was respected.  However, the -Werror
4567     // flags were checked during the initial as-user import.
4568     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4569       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4570       return Success;
4571     }
4572   }
4573 
4574   return Result;
4575 }
4576 
4577 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4578     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4579     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4580     bool ValidateDiagnosticOptions) {
4581   // Initialize a stream.
4582   BitstreamCursor Stream(StreamData);
4583 
4584   // Sniff for the signature.
4585   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4586     // FIXME this drops the error on the floor.
4587     consumeError(std::move(Err));
4588     return Failure;
4589   }
4590 
4591   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4592   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4593     return Failure;
4594 
4595   // Read all of the records in the options block.
4596   RecordData Record;
4597   ASTReadResult Result = Success;
4598   while (true) {
4599     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4600     if (!MaybeEntry) {
4601       // FIXME this drops the error on the floor.
4602       consumeError(MaybeEntry.takeError());
4603       return Failure;
4604     }
4605     llvm::BitstreamEntry Entry = MaybeEntry.get();
4606 
4607     switch (Entry.Kind) {
4608     case llvm::BitstreamEntry::Error:
4609     case llvm::BitstreamEntry::SubBlock:
4610       return Failure;
4611 
4612     case llvm::BitstreamEntry::EndBlock:
4613       return Result;
4614 
4615     case llvm::BitstreamEntry::Record:
4616       // The interesting case.
4617       break;
4618     }
4619 
4620     // Read and process a record.
4621     Record.clear();
4622     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4623     if (!MaybeRecordType) {
4624       // FIXME this drops the error.
4625       return Failure;
4626     }
4627     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4628     case SIGNATURE:
4629       if (F)
4630         std::copy(Record.begin(), Record.end(), F->Signature.data());
4631       break;
4632     case DIAGNOSTIC_OPTIONS: {
4633       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4634       if (Listener && ValidateDiagnosticOptions &&
4635           !AllowCompatibleConfigurationMismatch &&
4636           ParseDiagnosticOptions(Record, Complain, *Listener))
4637         Result = OutOfDate; // Don't return early.  Read the signature.
4638       break;
4639     }
4640     case DIAG_PRAGMA_MAPPINGS:
4641       if (!F)
4642         break;
4643       if (F->PragmaDiagMappings.empty())
4644         F->PragmaDiagMappings.swap(Record);
4645       else
4646         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4647                                      Record.begin(), Record.end());
4648       break;
4649     }
4650   }
4651 }
4652 
4653 /// Parse a record and blob containing module file extension metadata.
4654 static bool parseModuleFileExtensionMetadata(
4655               const SmallVectorImpl<uint64_t> &Record,
4656               StringRef Blob,
4657               ModuleFileExtensionMetadata &Metadata) {
4658   if (Record.size() < 4) return true;
4659 
4660   Metadata.MajorVersion = Record[0];
4661   Metadata.MinorVersion = Record[1];
4662 
4663   unsigned BlockNameLen = Record[2];
4664   unsigned UserInfoLen = Record[3];
4665 
4666   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4667 
4668   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4669   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4670                                   Blob.data() + BlockNameLen + UserInfoLen);
4671   return false;
4672 }
4673 
4674 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4675   BitstreamCursor &Stream = F.Stream;
4676 
4677   RecordData Record;
4678   while (true) {
4679     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4680     if (!MaybeEntry) {
4681       Error(MaybeEntry.takeError());
4682       return Failure;
4683     }
4684     llvm::BitstreamEntry Entry = MaybeEntry.get();
4685 
4686     switch (Entry.Kind) {
4687     case llvm::BitstreamEntry::SubBlock:
4688       if (llvm::Error Err = Stream.SkipBlock()) {
4689         Error(std::move(Err));
4690         return Failure;
4691       }
4692       continue;
4693 
4694     case llvm::BitstreamEntry::EndBlock:
4695       return Success;
4696 
4697     case llvm::BitstreamEntry::Error:
4698       return HadErrors;
4699 
4700     case llvm::BitstreamEntry::Record:
4701       break;
4702     }
4703 
4704     Record.clear();
4705     StringRef Blob;
4706     Expected<unsigned> MaybeRecCode =
4707         Stream.readRecord(Entry.ID, Record, &Blob);
4708     if (!MaybeRecCode) {
4709       Error(MaybeRecCode.takeError());
4710       return Failure;
4711     }
4712     switch (MaybeRecCode.get()) {
4713     case EXTENSION_METADATA: {
4714       ModuleFileExtensionMetadata Metadata;
4715       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4716         return Failure;
4717 
4718       // Find a module file extension with this block name.
4719       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4720       if (Known == ModuleFileExtensions.end()) break;
4721 
4722       // Form a reader.
4723       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4724                                                              F, Stream)) {
4725         F.ExtensionReaders.push_back(std::move(Reader));
4726       }
4727 
4728       break;
4729     }
4730     }
4731   }
4732 
4733   return Success;
4734 }
4735 
4736 void ASTReader::InitializeContext() {
4737   assert(ContextObj && "no context to initialize");
4738   ASTContext &Context = *ContextObj;
4739 
4740   // If there's a listener, notify them that we "read" the translation unit.
4741   if (DeserializationListener)
4742     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4743                                       Context.getTranslationUnitDecl());
4744 
4745   // FIXME: Find a better way to deal with collisions between these
4746   // built-in types. Right now, we just ignore the problem.
4747 
4748   // Load the special types.
4749   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4750     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4751       if (!Context.CFConstantStringTypeDecl)
4752         Context.setCFConstantStringType(GetType(String));
4753     }
4754 
4755     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4756       QualType FileType = GetType(File);
4757       if (FileType.isNull()) {
4758         Error("FILE type is NULL");
4759         return;
4760       }
4761 
4762       if (!Context.FILEDecl) {
4763         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4764           Context.setFILEDecl(Typedef->getDecl());
4765         else {
4766           const TagType *Tag = FileType->getAs<TagType>();
4767           if (!Tag) {
4768             Error("Invalid FILE type in AST file");
4769             return;
4770           }
4771           Context.setFILEDecl(Tag->getDecl());
4772         }
4773       }
4774     }
4775 
4776     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4777       QualType Jmp_bufType = GetType(Jmp_buf);
4778       if (Jmp_bufType.isNull()) {
4779         Error("jmp_buf type is NULL");
4780         return;
4781       }
4782 
4783       if (!Context.jmp_bufDecl) {
4784         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4785           Context.setjmp_bufDecl(Typedef->getDecl());
4786         else {
4787           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4788           if (!Tag) {
4789             Error("Invalid jmp_buf type in AST file");
4790             return;
4791           }
4792           Context.setjmp_bufDecl(Tag->getDecl());
4793         }
4794       }
4795     }
4796 
4797     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4798       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4799       if (Sigjmp_bufType.isNull()) {
4800         Error("sigjmp_buf type is NULL");
4801         return;
4802       }
4803 
4804       if (!Context.sigjmp_bufDecl) {
4805         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4806           Context.setsigjmp_bufDecl(Typedef->getDecl());
4807         else {
4808           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4809           assert(Tag && "Invalid sigjmp_buf type in AST file");
4810           Context.setsigjmp_bufDecl(Tag->getDecl());
4811         }
4812       }
4813     }
4814 
4815     if (unsigned ObjCIdRedef
4816           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4817       if (Context.ObjCIdRedefinitionType.isNull())
4818         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4819     }
4820 
4821     if (unsigned ObjCClassRedef
4822           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4823       if (Context.ObjCClassRedefinitionType.isNull())
4824         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4825     }
4826 
4827     if (unsigned ObjCSelRedef
4828           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4829       if (Context.ObjCSelRedefinitionType.isNull())
4830         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4831     }
4832 
4833     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4834       QualType Ucontext_tType = GetType(Ucontext_t);
4835       if (Ucontext_tType.isNull()) {
4836         Error("ucontext_t type is NULL");
4837         return;
4838       }
4839 
4840       if (!Context.ucontext_tDecl) {
4841         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4842           Context.setucontext_tDecl(Typedef->getDecl());
4843         else {
4844           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4845           assert(Tag && "Invalid ucontext_t type in AST file");
4846           Context.setucontext_tDecl(Tag->getDecl());
4847         }
4848       }
4849     }
4850   }
4851 
4852   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4853 
4854   // If there were any CUDA special declarations, deserialize them.
4855   if (!CUDASpecialDeclRefs.empty()) {
4856     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4857     Context.setcudaConfigureCallDecl(
4858                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4859   }
4860 
4861   // Re-export any modules that were imported by a non-module AST file.
4862   // FIXME: This does not make macro-only imports visible again.
4863   for (auto &Import : ImportedModules) {
4864     if (Module *Imported = getSubmodule(Import.ID)) {
4865       makeModuleVisible(Imported, Module::AllVisible,
4866                         /*ImportLoc=*/Import.ImportLoc);
4867       if (Import.ImportLoc.isValid())
4868         PP.makeModuleVisible(Imported, Import.ImportLoc);
4869       // FIXME: should we tell Sema to make the module visible too?
4870     }
4871   }
4872   ImportedModules.clear();
4873 }
4874 
4875 void ASTReader::finalizeForWriting() {
4876   // Nothing to do for now.
4877 }
4878 
4879 /// Reads and return the signature record from \p PCH's control block, or
4880 /// else returns 0.
4881 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4882   BitstreamCursor Stream(PCH);
4883   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4884     // FIXME this drops the error on the floor.
4885     consumeError(std::move(Err));
4886     return ASTFileSignature();
4887   }
4888 
4889   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4890   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4891     return ASTFileSignature();
4892 
4893   // Scan for SIGNATURE inside the diagnostic options block.
4894   ASTReader::RecordData Record;
4895   while (true) {
4896     Expected<llvm::BitstreamEntry> MaybeEntry =
4897         Stream.advanceSkippingSubblocks();
4898     if (!MaybeEntry) {
4899       // FIXME this drops the error on the floor.
4900       consumeError(MaybeEntry.takeError());
4901       return ASTFileSignature();
4902     }
4903     llvm::BitstreamEntry Entry = MaybeEntry.get();
4904 
4905     if (Entry.Kind != llvm::BitstreamEntry::Record)
4906       return ASTFileSignature();
4907 
4908     Record.clear();
4909     StringRef Blob;
4910     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4911     if (!MaybeRecord) {
4912       // FIXME this drops the error on the floor.
4913       consumeError(MaybeRecord.takeError());
4914       return ASTFileSignature();
4915     }
4916     if (SIGNATURE == MaybeRecord.get())
4917       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4918                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4919   }
4920 }
4921 
4922 /// Retrieve the name of the original source file name
4923 /// directly from the AST file, without actually loading the AST
4924 /// file.
4925 std::string ASTReader::getOriginalSourceFile(
4926     const std::string &ASTFileName, FileManager &FileMgr,
4927     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4928   // Open the AST file.
4929   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4930   if (!Buffer) {
4931     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4932         << ASTFileName << Buffer.getError().message();
4933     return std::string();
4934   }
4935 
4936   // Initialize the stream
4937   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4938 
4939   // Sniff for the signature.
4940   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4941     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
4942     return std::string();
4943   }
4944 
4945   // Scan for the CONTROL_BLOCK_ID block.
4946   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4947     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4948     return std::string();
4949   }
4950 
4951   // Scan for ORIGINAL_FILE inside the control block.
4952   RecordData Record;
4953   while (true) {
4954     Expected<llvm::BitstreamEntry> MaybeEntry =
4955         Stream.advanceSkippingSubblocks();
4956     if (!MaybeEntry) {
4957       // FIXME this drops errors on the floor.
4958       consumeError(MaybeEntry.takeError());
4959       return std::string();
4960     }
4961     llvm::BitstreamEntry Entry = MaybeEntry.get();
4962 
4963     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4964       return std::string();
4965 
4966     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4967       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4968       return std::string();
4969     }
4970 
4971     Record.clear();
4972     StringRef Blob;
4973     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4974     if (!MaybeRecord) {
4975       // FIXME this drops the errors on the floor.
4976       consumeError(MaybeRecord.takeError());
4977       return std::string();
4978     }
4979     if (ORIGINAL_FILE == MaybeRecord.get())
4980       return Blob.str();
4981   }
4982 }
4983 
4984 namespace {
4985 
4986   class SimplePCHValidator : public ASTReaderListener {
4987     const LangOptions &ExistingLangOpts;
4988     const TargetOptions &ExistingTargetOpts;
4989     const PreprocessorOptions &ExistingPPOpts;
4990     std::string ExistingModuleCachePath;
4991     FileManager &FileMgr;
4992 
4993   public:
4994     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4995                        const TargetOptions &ExistingTargetOpts,
4996                        const PreprocessorOptions &ExistingPPOpts,
4997                        StringRef ExistingModuleCachePath,
4998                        FileManager &FileMgr)
4999       : ExistingLangOpts(ExistingLangOpts),
5000         ExistingTargetOpts(ExistingTargetOpts),
5001         ExistingPPOpts(ExistingPPOpts),
5002         ExistingModuleCachePath(ExistingModuleCachePath),
5003         FileMgr(FileMgr) {}
5004 
5005     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5006                              bool AllowCompatibleDifferences) override {
5007       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5008                                   AllowCompatibleDifferences);
5009     }
5010 
5011     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5012                            bool AllowCompatibleDifferences) override {
5013       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5014                                 AllowCompatibleDifferences);
5015     }
5016 
5017     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5018                                  StringRef SpecificModuleCachePath,
5019                                  bool Complain) override {
5020       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5021                                       ExistingModuleCachePath,
5022                                       nullptr, ExistingLangOpts);
5023     }
5024 
5025     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5026                                  bool Complain,
5027                                  std::string &SuggestedPredefines) override {
5028       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5029                                       SuggestedPredefines, ExistingLangOpts);
5030     }
5031   };
5032 
5033 } // namespace
5034 
5035 bool ASTReader::readASTFileControlBlock(
5036     StringRef Filename, FileManager &FileMgr,
5037     const PCHContainerReader &PCHContainerRdr,
5038     bool FindModuleFileExtensions,
5039     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5040   // Open the AST file.
5041   // FIXME: This allows use of the VFS; we do not allow use of the
5042   // VFS when actually loading a module.
5043   auto Buffer = FileMgr.getBufferForFile(Filename);
5044   if (!Buffer) {
5045     return true;
5046   }
5047 
5048   // Initialize the stream
5049   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5050   BitstreamCursor Stream(Bytes);
5051 
5052   // Sniff for the signature.
5053   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5054     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5055     return true;
5056   }
5057 
5058   // Scan for the CONTROL_BLOCK_ID block.
5059   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5060     return true;
5061 
5062   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5063   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5064   bool NeedsImports = Listener.needsImportVisitation();
5065   BitstreamCursor InputFilesCursor;
5066 
5067   RecordData Record;
5068   std::string ModuleDir;
5069   bool DoneWithControlBlock = false;
5070   while (!DoneWithControlBlock) {
5071     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5072     if (!MaybeEntry) {
5073       // FIXME this drops the error on the floor.
5074       consumeError(MaybeEntry.takeError());
5075       return true;
5076     }
5077     llvm::BitstreamEntry Entry = MaybeEntry.get();
5078 
5079     switch (Entry.Kind) {
5080     case llvm::BitstreamEntry::SubBlock: {
5081       switch (Entry.ID) {
5082       case OPTIONS_BLOCK_ID: {
5083         std::string IgnoredSuggestedPredefines;
5084         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5085                              /*AllowCompatibleConfigurationMismatch*/ false,
5086                              Listener, IgnoredSuggestedPredefines) != Success)
5087           return true;
5088         break;
5089       }
5090 
5091       case INPUT_FILES_BLOCK_ID:
5092         InputFilesCursor = Stream;
5093         if (llvm::Error Err = Stream.SkipBlock()) {
5094           // FIXME this drops the error on the floor.
5095           consumeError(std::move(Err));
5096           return true;
5097         }
5098         if (NeedsInputFiles &&
5099             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5100           return true;
5101         break;
5102 
5103       default:
5104         if (llvm::Error Err = Stream.SkipBlock()) {
5105           // FIXME this drops the error on the floor.
5106           consumeError(std::move(Err));
5107           return true;
5108         }
5109         break;
5110       }
5111 
5112       continue;
5113     }
5114 
5115     case llvm::BitstreamEntry::EndBlock:
5116       DoneWithControlBlock = true;
5117       break;
5118 
5119     case llvm::BitstreamEntry::Error:
5120       return true;
5121 
5122     case llvm::BitstreamEntry::Record:
5123       break;
5124     }
5125 
5126     if (DoneWithControlBlock) break;
5127 
5128     Record.clear();
5129     StringRef Blob;
5130     Expected<unsigned> MaybeRecCode =
5131         Stream.readRecord(Entry.ID, Record, &Blob);
5132     if (!MaybeRecCode) {
5133       // FIXME this drops the error.
5134       return Failure;
5135     }
5136     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5137     case METADATA:
5138       if (Record[0] != VERSION_MAJOR)
5139         return true;
5140       if (Listener.ReadFullVersionInformation(Blob))
5141         return true;
5142       break;
5143     case MODULE_NAME:
5144       Listener.ReadModuleName(Blob);
5145       break;
5146     case MODULE_DIRECTORY:
5147       ModuleDir = Blob;
5148       break;
5149     case MODULE_MAP_FILE: {
5150       unsigned Idx = 0;
5151       auto Path = ReadString(Record, Idx);
5152       ResolveImportedPath(Path, ModuleDir);
5153       Listener.ReadModuleMapFile(Path);
5154       break;
5155     }
5156     case INPUT_FILE_OFFSETS: {
5157       if (!NeedsInputFiles)
5158         break;
5159 
5160       unsigned NumInputFiles = Record[0];
5161       unsigned NumUserFiles = Record[1];
5162       const llvm::support::unaligned_uint64_t *InputFileOffs =
5163           (const llvm::support::unaligned_uint64_t *)Blob.data();
5164       for (unsigned I = 0; I != NumInputFiles; ++I) {
5165         // Go find this input file.
5166         bool isSystemFile = I >= NumUserFiles;
5167 
5168         if (isSystemFile && !NeedsSystemInputFiles)
5169           break; // the rest are system input files
5170 
5171         BitstreamCursor &Cursor = InputFilesCursor;
5172         SavedStreamPosition SavedPosition(Cursor);
5173         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5174           // FIXME this drops errors on the floor.
5175           consumeError(std::move(Err));
5176         }
5177 
5178         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5179         if (!MaybeCode) {
5180           // FIXME this drops errors on the floor.
5181           consumeError(MaybeCode.takeError());
5182         }
5183         unsigned Code = MaybeCode.get();
5184 
5185         RecordData Record;
5186         StringRef Blob;
5187         bool shouldContinue = false;
5188         Expected<unsigned> MaybeRecordType =
5189             Cursor.readRecord(Code, Record, &Blob);
5190         if (!MaybeRecordType) {
5191           // FIXME this drops errors on the floor.
5192           consumeError(MaybeRecordType.takeError());
5193         }
5194         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5195         case INPUT_FILE:
5196           bool Overridden = static_cast<bool>(Record[3]);
5197           std::string Filename = Blob;
5198           ResolveImportedPath(Filename, ModuleDir);
5199           shouldContinue = Listener.visitInputFile(
5200               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5201           break;
5202         }
5203         if (!shouldContinue)
5204           break;
5205       }
5206       break;
5207     }
5208 
5209     case IMPORTS: {
5210       if (!NeedsImports)
5211         break;
5212 
5213       unsigned Idx = 0, N = Record.size();
5214       while (Idx < N) {
5215         // Read information about the AST file.
5216         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5217         std::string ModuleName = ReadString(Record, Idx);
5218         std::string Filename = ReadString(Record, Idx);
5219         ResolveImportedPath(Filename, ModuleDir);
5220         Listener.visitImport(ModuleName, Filename);
5221       }
5222       break;
5223     }
5224 
5225     default:
5226       // No other validation to perform.
5227       break;
5228     }
5229   }
5230 
5231   // Look for module file extension blocks, if requested.
5232   if (FindModuleFileExtensions) {
5233     BitstreamCursor SavedStream = Stream;
5234     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5235       bool DoneWithExtensionBlock = false;
5236       while (!DoneWithExtensionBlock) {
5237         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5238         if (!MaybeEntry) {
5239           // FIXME this drops the error.
5240           return true;
5241         }
5242         llvm::BitstreamEntry Entry = MaybeEntry.get();
5243 
5244         switch (Entry.Kind) {
5245         case llvm::BitstreamEntry::SubBlock:
5246           if (llvm::Error Err = Stream.SkipBlock()) {
5247             // FIXME this drops the error on the floor.
5248             consumeError(std::move(Err));
5249             return true;
5250           }
5251           continue;
5252 
5253         case llvm::BitstreamEntry::EndBlock:
5254           DoneWithExtensionBlock = true;
5255           continue;
5256 
5257         case llvm::BitstreamEntry::Error:
5258           return true;
5259 
5260         case llvm::BitstreamEntry::Record:
5261           break;
5262         }
5263 
5264        Record.clear();
5265        StringRef Blob;
5266        Expected<unsigned> MaybeRecCode =
5267            Stream.readRecord(Entry.ID, Record, &Blob);
5268        if (!MaybeRecCode) {
5269          // FIXME this drops the error.
5270          return true;
5271        }
5272        switch (MaybeRecCode.get()) {
5273        case EXTENSION_METADATA: {
5274          ModuleFileExtensionMetadata Metadata;
5275          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5276            return true;
5277 
5278          Listener.readModuleFileExtension(Metadata);
5279          break;
5280        }
5281        }
5282       }
5283     }
5284     Stream = SavedStream;
5285   }
5286 
5287   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5288   if (readUnhashedControlBlockImpl(
5289           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5290           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5291           ValidateDiagnosticOptions) != Success)
5292     return true;
5293 
5294   return false;
5295 }
5296 
5297 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5298                                     const PCHContainerReader &PCHContainerRdr,
5299                                     const LangOptions &LangOpts,
5300                                     const TargetOptions &TargetOpts,
5301                                     const PreprocessorOptions &PPOpts,
5302                                     StringRef ExistingModuleCachePath) {
5303   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5304                                ExistingModuleCachePath, FileMgr);
5305   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5306                                   /*FindModuleFileExtensions=*/false,
5307                                   validator,
5308                                   /*ValidateDiagnosticOptions=*/true);
5309 }
5310 
5311 ASTReader::ASTReadResult
5312 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5313   // Enter the submodule block.
5314   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5315     Error(std::move(Err));
5316     return Failure;
5317   }
5318 
5319   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5320   bool First = true;
5321   Module *CurrentModule = nullptr;
5322   RecordData Record;
5323   while (true) {
5324     Expected<llvm::BitstreamEntry> MaybeEntry =
5325         F.Stream.advanceSkippingSubblocks();
5326     if (!MaybeEntry) {
5327       Error(MaybeEntry.takeError());
5328       return Failure;
5329     }
5330     llvm::BitstreamEntry Entry = MaybeEntry.get();
5331 
5332     switch (Entry.Kind) {
5333     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5334     case llvm::BitstreamEntry::Error:
5335       Error("malformed block record in AST file");
5336       return Failure;
5337     case llvm::BitstreamEntry::EndBlock:
5338       return Success;
5339     case llvm::BitstreamEntry::Record:
5340       // The interesting case.
5341       break;
5342     }
5343 
5344     // Read a record.
5345     StringRef Blob;
5346     Record.clear();
5347     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5348     if (!MaybeKind) {
5349       Error(MaybeKind.takeError());
5350       return Failure;
5351     }
5352     unsigned Kind = MaybeKind.get();
5353 
5354     if ((Kind == SUBMODULE_METADATA) != First) {
5355       Error("submodule metadata record should be at beginning of block");
5356       return Failure;
5357     }
5358     First = false;
5359 
5360     // Submodule information is only valid if we have a current module.
5361     // FIXME: Should we error on these cases?
5362     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5363         Kind != SUBMODULE_DEFINITION)
5364       continue;
5365 
5366     switch (Kind) {
5367     default:  // Default behavior: ignore.
5368       break;
5369 
5370     case SUBMODULE_DEFINITION: {
5371       if (Record.size() < 12) {
5372         Error("malformed module definition");
5373         return Failure;
5374       }
5375 
5376       StringRef Name = Blob;
5377       unsigned Idx = 0;
5378       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5379       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5380       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5381       bool IsFramework = Record[Idx++];
5382       bool IsExplicit = Record[Idx++];
5383       bool IsSystem = Record[Idx++];
5384       bool IsExternC = Record[Idx++];
5385       bool InferSubmodules = Record[Idx++];
5386       bool InferExplicitSubmodules = Record[Idx++];
5387       bool InferExportWildcard = Record[Idx++];
5388       bool ConfigMacrosExhaustive = Record[Idx++];
5389       bool ModuleMapIsPrivate = Record[Idx++];
5390 
5391       Module *ParentModule = nullptr;
5392       if (Parent)
5393         ParentModule = getSubmodule(Parent);
5394 
5395       // Retrieve this (sub)module from the module map, creating it if
5396       // necessary.
5397       CurrentModule =
5398           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5399               .first;
5400 
5401       // FIXME: set the definition loc for CurrentModule, or call
5402       // ModMap.setInferredModuleAllowedBy()
5403 
5404       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5405       if (GlobalIndex >= SubmodulesLoaded.size() ||
5406           SubmodulesLoaded[GlobalIndex]) {
5407         Error("too many submodules");
5408         return Failure;
5409       }
5410 
5411       if (!ParentModule) {
5412         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5413           // Don't emit module relocation error if we have -fno-validate-pch
5414           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5415               CurFile != F.File) {
5416             if (!Diags.isDiagnosticInFlight()) {
5417               Diag(diag::err_module_file_conflict)
5418                 << CurrentModule->getTopLevelModuleName()
5419                 << CurFile->getName()
5420                 << F.File->getName();
5421             }
5422             return Failure;
5423           }
5424         }
5425 
5426         CurrentModule->setASTFile(F.File);
5427         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5428       }
5429 
5430       CurrentModule->Kind = Kind;
5431       CurrentModule->Signature = F.Signature;
5432       CurrentModule->IsFromModuleFile = true;
5433       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5434       CurrentModule->IsExternC = IsExternC;
5435       CurrentModule->InferSubmodules = InferSubmodules;
5436       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5437       CurrentModule->InferExportWildcard = InferExportWildcard;
5438       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5439       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5440       if (DeserializationListener)
5441         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5442 
5443       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5444 
5445       // Clear out data that will be replaced by what is in the module file.
5446       CurrentModule->LinkLibraries.clear();
5447       CurrentModule->ConfigMacros.clear();
5448       CurrentModule->UnresolvedConflicts.clear();
5449       CurrentModule->Conflicts.clear();
5450 
5451       // The module is available unless it's missing a requirement; relevant
5452       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5453       // Missing headers that were present when the module was built do not
5454       // make it unavailable -- if we got this far, this must be an explicitly
5455       // imported module file.
5456       CurrentModule->Requirements.clear();
5457       CurrentModule->MissingHeaders.clear();
5458       CurrentModule->IsMissingRequirement =
5459           ParentModule && ParentModule->IsMissingRequirement;
5460       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5461       break;
5462     }
5463 
5464     case SUBMODULE_UMBRELLA_HEADER: {
5465       std::string Filename = Blob;
5466       ResolveImportedPath(F, Filename);
5467       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5468         if (!CurrentModule->getUmbrellaHeader())
5469           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5470         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5471           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5472             Error("mismatched umbrella headers in submodule");
5473           return OutOfDate;
5474         }
5475       }
5476       break;
5477     }
5478 
5479     case SUBMODULE_HEADER:
5480     case SUBMODULE_EXCLUDED_HEADER:
5481     case SUBMODULE_PRIVATE_HEADER:
5482       // We lazily associate headers with their modules via the HeaderInfo table.
5483       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5484       // of complete filenames or remove it entirely.
5485       break;
5486 
5487     case SUBMODULE_TEXTUAL_HEADER:
5488     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5489       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5490       // them here.
5491       break;
5492 
5493     case SUBMODULE_TOPHEADER:
5494       CurrentModule->addTopHeaderFilename(Blob);
5495       break;
5496 
5497     case SUBMODULE_UMBRELLA_DIR: {
5498       std::string Dirname = Blob;
5499       ResolveImportedPath(F, Dirname);
5500       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5501         if (!CurrentModule->getUmbrellaDir())
5502           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5503         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5504           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5505             Error("mismatched umbrella directories in submodule");
5506           return OutOfDate;
5507         }
5508       }
5509       break;
5510     }
5511 
5512     case SUBMODULE_METADATA: {
5513       F.BaseSubmoduleID = getTotalNumSubmodules();
5514       F.LocalNumSubmodules = Record[0];
5515       unsigned LocalBaseSubmoduleID = Record[1];
5516       if (F.LocalNumSubmodules > 0) {
5517         // Introduce the global -> local mapping for submodules within this
5518         // module.
5519         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5520 
5521         // Introduce the local -> global mapping for submodules within this
5522         // module.
5523         F.SubmoduleRemap.insertOrReplace(
5524           std::make_pair(LocalBaseSubmoduleID,
5525                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5526 
5527         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5528       }
5529       break;
5530     }
5531 
5532     case SUBMODULE_IMPORTS:
5533       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5534         UnresolvedModuleRef Unresolved;
5535         Unresolved.File = &F;
5536         Unresolved.Mod = CurrentModule;
5537         Unresolved.ID = Record[Idx];
5538         Unresolved.Kind = UnresolvedModuleRef::Import;
5539         Unresolved.IsWildcard = false;
5540         UnresolvedModuleRefs.push_back(Unresolved);
5541       }
5542       break;
5543 
5544     case SUBMODULE_EXPORTS:
5545       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5546         UnresolvedModuleRef Unresolved;
5547         Unresolved.File = &F;
5548         Unresolved.Mod = CurrentModule;
5549         Unresolved.ID = Record[Idx];
5550         Unresolved.Kind = UnresolvedModuleRef::Export;
5551         Unresolved.IsWildcard = Record[Idx + 1];
5552         UnresolvedModuleRefs.push_back(Unresolved);
5553       }
5554 
5555       // Once we've loaded the set of exports, there's no reason to keep
5556       // the parsed, unresolved exports around.
5557       CurrentModule->UnresolvedExports.clear();
5558       break;
5559 
5560     case SUBMODULE_REQUIRES:
5561       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5562                                     PP.getTargetInfo());
5563       break;
5564 
5565     case SUBMODULE_LINK_LIBRARY:
5566       ModMap.resolveLinkAsDependencies(CurrentModule);
5567       CurrentModule->LinkLibraries.push_back(
5568                                          Module::LinkLibrary(Blob, Record[0]));
5569       break;
5570 
5571     case SUBMODULE_CONFIG_MACRO:
5572       CurrentModule->ConfigMacros.push_back(Blob.str());
5573       break;
5574 
5575     case SUBMODULE_CONFLICT: {
5576       UnresolvedModuleRef Unresolved;
5577       Unresolved.File = &F;
5578       Unresolved.Mod = CurrentModule;
5579       Unresolved.ID = Record[0];
5580       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5581       Unresolved.IsWildcard = false;
5582       Unresolved.String = Blob;
5583       UnresolvedModuleRefs.push_back(Unresolved);
5584       break;
5585     }
5586 
5587     case SUBMODULE_INITIALIZERS: {
5588       if (!ContextObj)
5589         break;
5590       SmallVector<uint32_t, 16> Inits;
5591       for (auto &ID : Record)
5592         Inits.push_back(getGlobalDeclID(F, ID));
5593       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5594       break;
5595     }
5596 
5597     case SUBMODULE_EXPORT_AS:
5598       CurrentModule->ExportAsModule = Blob.str();
5599       ModMap.addLinkAsDependency(CurrentModule);
5600       break;
5601     }
5602   }
5603 }
5604 
5605 /// Parse the record that corresponds to a LangOptions data
5606 /// structure.
5607 ///
5608 /// This routine parses the language options from the AST file and then gives
5609 /// them to the AST listener if one is set.
5610 ///
5611 /// \returns true if the listener deems the file unacceptable, false otherwise.
5612 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5613                                      bool Complain,
5614                                      ASTReaderListener &Listener,
5615                                      bool AllowCompatibleDifferences) {
5616   LangOptions LangOpts;
5617   unsigned Idx = 0;
5618 #define LANGOPT(Name, Bits, Default, Description) \
5619   LangOpts.Name = Record[Idx++];
5620 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5621   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5622 #include "clang/Basic/LangOptions.def"
5623 #define SANITIZER(NAME, ID)                                                    \
5624   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5625 #include "clang/Basic/Sanitizers.def"
5626 
5627   for (unsigned N = Record[Idx++]; N; --N)
5628     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5629 
5630   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5631   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5632   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5633 
5634   LangOpts.CurrentModule = ReadString(Record, Idx);
5635 
5636   // Comment options.
5637   for (unsigned N = Record[Idx++]; N; --N) {
5638     LangOpts.CommentOpts.BlockCommandNames.push_back(
5639       ReadString(Record, Idx));
5640   }
5641   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5642 
5643   // OpenMP offloading options.
5644   for (unsigned N = Record[Idx++]; N; --N) {
5645     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5646   }
5647 
5648   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5649 
5650   return Listener.ReadLanguageOptions(LangOpts, Complain,
5651                                       AllowCompatibleDifferences);
5652 }
5653 
5654 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5655                                    ASTReaderListener &Listener,
5656                                    bool AllowCompatibleDifferences) {
5657   unsigned Idx = 0;
5658   TargetOptions TargetOpts;
5659   TargetOpts.Triple = ReadString(Record, Idx);
5660   TargetOpts.CPU = ReadString(Record, Idx);
5661   TargetOpts.ABI = ReadString(Record, Idx);
5662   for (unsigned N = Record[Idx++]; N; --N) {
5663     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5664   }
5665   for (unsigned N = Record[Idx++]; N; --N) {
5666     TargetOpts.Features.push_back(ReadString(Record, Idx));
5667   }
5668 
5669   return Listener.ReadTargetOptions(TargetOpts, Complain,
5670                                     AllowCompatibleDifferences);
5671 }
5672 
5673 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5674                                        ASTReaderListener &Listener) {
5675   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5676   unsigned Idx = 0;
5677 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5678 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5679   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5680 #include "clang/Basic/DiagnosticOptions.def"
5681 
5682   for (unsigned N = Record[Idx++]; N; --N)
5683     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5684   for (unsigned N = Record[Idx++]; N; --N)
5685     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5686 
5687   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5688 }
5689 
5690 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5691                                        ASTReaderListener &Listener) {
5692   FileSystemOptions FSOpts;
5693   unsigned Idx = 0;
5694   FSOpts.WorkingDir = ReadString(Record, Idx);
5695   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5696 }
5697 
5698 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5699                                          bool Complain,
5700                                          ASTReaderListener &Listener) {
5701   HeaderSearchOptions HSOpts;
5702   unsigned Idx = 0;
5703   HSOpts.Sysroot = ReadString(Record, Idx);
5704 
5705   // Include entries.
5706   for (unsigned N = Record[Idx++]; N; --N) {
5707     std::string Path = ReadString(Record, Idx);
5708     frontend::IncludeDirGroup Group
5709       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5710     bool IsFramework = Record[Idx++];
5711     bool IgnoreSysRoot = Record[Idx++];
5712     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5713                                     IgnoreSysRoot);
5714   }
5715 
5716   // System header prefixes.
5717   for (unsigned N = Record[Idx++]; N; --N) {
5718     std::string Prefix = ReadString(Record, Idx);
5719     bool IsSystemHeader = Record[Idx++];
5720     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5721   }
5722 
5723   HSOpts.ResourceDir = ReadString(Record, Idx);
5724   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5725   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5726   HSOpts.DisableModuleHash = Record[Idx++];
5727   HSOpts.ImplicitModuleMaps = Record[Idx++];
5728   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5729   HSOpts.UseBuiltinIncludes = Record[Idx++];
5730   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5731   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5732   HSOpts.UseLibcxx = Record[Idx++];
5733   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5734 
5735   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5736                                           Complain);
5737 }
5738 
5739 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5740                                          bool Complain,
5741                                          ASTReaderListener &Listener,
5742                                          std::string &SuggestedPredefines) {
5743   PreprocessorOptions PPOpts;
5744   unsigned Idx = 0;
5745 
5746   // Macro definitions/undefs
5747   for (unsigned N = Record[Idx++]; N; --N) {
5748     std::string Macro = ReadString(Record, Idx);
5749     bool IsUndef = Record[Idx++];
5750     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5751   }
5752 
5753   // Includes
5754   for (unsigned N = Record[Idx++]; N; --N) {
5755     PPOpts.Includes.push_back(ReadString(Record, Idx));
5756   }
5757 
5758   // Macro Includes
5759   for (unsigned N = Record[Idx++]; N; --N) {
5760     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5761   }
5762 
5763   PPOpts.UsePredefines = Record[Idx++];
5764   PPOpts.DetailedRecord = Record[Idx++];
5765   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5766   PPOpts.ObjCXXARCStandardLibrary =
5767     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5768   SuggestedPredefines.clear();
5769   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5770                                           SuggestedPredefines);
5771 }
5772 
5773 std::pair<ModuleFile *, unsigned>
5774 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5775   GlobalPreprocessedEntityMapType::iterator
5776   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5777   assert(I != GlobalPreprocessedEntityMap.end() &&
5778          "Corrupted global preprocessed entity map");
5779   ModuleFile *M = I->second;
5780   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5781   return std::make_pair(M, LocalIndex);
5782 }
5783 
5784 llvm::iterator_range<PreprocessingRecord::iterator>
5785 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5786   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5787     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5788                                              Mod.NumPreprocessedEntities);
5789 
5790   return llvm::make_range(PreprocessingRecord::iterator(),
5791                           PreprocessingRecord::iterator());
5792 }
5793 
5794 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5795 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5796   return llvm::make_range(
5797       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5798       ModuleDeclIterator(this, &Mod,
5799                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5800 }
5801 
5802 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5803   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5804   assert(I != GlobalSkippedRangeMap.end() &&
5805     "Corrupted global skipped range map");
5806   ModuleFile *M = I->second;
5807   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5808   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5809   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5810   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5811                     TranslateSourceLocation(*M, RawRange.getEnd()));
5812   assert(Range.isValid());
5813   return Range;
5814 }
5815 
5816 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5817   PreprocessedEntityID PPID = Index+1;
5818   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5819   ModuleFile &M = *PPInfo.first;
5820   unsigned LocalIndex = PPInfo.second;
5821   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5822 
5823   if (!PP.getPreprocessingRecord()) {
5824     Error("no preprocessing record");
5825     return nullptr;
5826   }
5827 
5828   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5829   if (llvm::Error Err =
5830           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5831     Error(std::move(Err));
5832     return nullptr;
5833   }
5834 
5835   Expected<llvm::BitstreamEntry> MaybeEntry =
5836       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5837   if (!MaybeEntry) {
5838     Error(MaybeEntry.takeError());
5839     return nullptr;
5840   }
5841   llvm::BitstreamEntry Entry = MaybeEntry.get();
5842 
5843   if (Entry.Kind != llvm::BitstreamEntry::Record)
5844     return nullptr;
5845 
5846   // Read the record.
5847   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5848                     TranslateSourceLocation(M, PPOffs.getEnd()));
5849   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5850   StringRef Blob;
5851   RecordData Record;
5852   Expected<unsigned> MaybeRecType =
5853       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5854   if (!MaybeRecType) {
5855     Error(MaybeRecType.takeError());
5856     return nullptr;
5857   }
5858   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5859   case PPD_MACRO_EXPANSION: {
5860     bool isBuiltin = Record[0];
5861     IdentifierInfo *Name = nullptr;
5862     MacroDefinitionRecord *Def = nullptr;
5863     if (isBuiltin)
5864       Name = getLocalIdentifier(M, Record[1]);
5865     else {
5866       PreprocessedEntityID GlobalID =
5867           getGlobalPreprocessedEntityID(M, Record[1]);
5868       Def = cast<MacroDefinitionRecord>(
5869           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5870     }
5871 
5872     MacroExpansion *ME;
5873     if (isBuiltin)
5874       ME = new (PPRec) MacroExpansion(Name, Range);
5875     else
5876       ME = new (PPRec) MacroExpansion(Def, Range);
5877 
5878     return ME;
5879   }
5880 
5881   case PPD_MACRO_DEFINITION: {
5882     // Decode the identifier info and then check again; if the macro is
5883     // still defined and associated with the identifier,
5884     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5885     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5886 
5887     if (DeserializationListener)
5888       DeserializationListener->MacroDefinitionRead(PPID, MD);
5889 
5890     return MD;
5891   }
5892 
5893   case PPD_INCLUSION_DIRECTIVE: {
5894     const char *FullFileNameStart = Blob.data() + Record[0];
5895     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5896     const FileEntry *File = nullptr;
5897     if (!FullFileName.empty())
5898       if (auto FE = PP.getFileManager().getFile(FullFileName))
5899         File = *FE;
5900 
5901     // FIXME: Stable encoding
5902     InclusionDirective::InclusionKind Kind
5903       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5904     InclusionDirective *ID
5905       = new (PPRec) InclusionDirective(PPRec, Kind,
5906                                        StringRef(Blob.data(), Record[0]),
5907                                        Record[1], Record[3],
5908                                        File,
5909                                        Range);
5910     return ID;
5911   }
5912   }
5913 
5914   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5915 }
5916 
5917 /// Find the next module that contains entities and return the ID
5918 /// of the first entry.
5919 ///
5920 /// \param SLocMapI points at a chunk of a module that contains no
5921 /// preprocessed entities or the entities it contains are not the ones we are
5922 /// looking for.
5923 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5924                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5925   ++SLocMapI;
5926   for (GlobalSLocOffsetMapType::const_iterator
5927          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5928     ModuleFile &M = *SLocMapI->second;
5929     if (M.NumPreprocessedEntities)
5930       return M.BasePreprocessedEntityID;
5931   }
5932 
5933   return getTotalNumPreprocessedEntities();
5934 }
5935 
5936 namespace {
5937 
5938 struct PPEntityComp {
5939   const ASTReader &Reader;
5940   ModuleFile &M;
5941 
5942   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5943 
5944   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5945     SourceLocation LHS = getLoc(L);
5946     SourceLocation RHS = getLoc(R);
5947     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5948   }
5949 
5950   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5951     SourceLocation LHS = getLoc(L);
5952     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5953   }
5954 
5955   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5956     SourceLocation RHS = getLoc(R);
5957     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5958   }
5959 
5960   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5961     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5962   }
5963 };
5964 
5965 } // namespace
5966 
5967 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5968                                                        bool EndsAfter) const {
5969   if (SourceMgr.isLocalSourceLocation(Loc))
5970     return getTotalNumPreprocessedEntities();
5971 
5972   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5973       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5974   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5975          "Corrupted global sloc offset map");
5976 
5977   if (SLocMapI->second->NumPreprocessedEntities == 0)
5978     return findNextPreprocessedEntity(SLocMapI);
5979 
5980   ModuleFile &M = *SLocMapI->second;
5981 
5982   using pp_iterator = const PPEntityOffset *;
5983 
5984   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5985   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5986 
5987   size_t Count = M.NumPreprocessedEntities;
5988   size_t Half;
5989   pp_iterator First = pp_begin;
5990   pp_iterator PPI;
5991 
5992   if (EndsAfter) {
5993     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5994                            PPEntityComp(*this, M));
5995   } else {
5996     // Do a binary search manually instead of using std::lower_bound because
5997     // The end locations of entities may be unordered (when a macro expansion
5998     // is inside another macro argument), but for this case it is not important
5999     // whether we get the first macro expansion or its containing macro.
6000     while (Count > 0) {
6001       Half = Count / 2;
6002       PPI = First;
6003       std::advance(PPI, Half);
6004       if (SourceMgr.isBeforeInTranslationUnit(
6005               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6006         First = PPI;
6007         ++First;
6008         Count = Count - Half - 1;
6009       } else
6010         Count = Half;
6011     }
6012   }
6013 
6014   if (PPI == pp_end)
6015     return findNextPreprocessedEntity(SLocMapI);
6016 
6017   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6018 }
6019 
6020 /// Returns a pair of [Begin, End) indices of preallocated
6021 /// preprocessed entities that \arg Range encompasses.
6022 std::pair<unsigned, unsigned>
6023     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6024   if (Range.isInvalid())
6025     return std::make_pair(0,0);
6026   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6027 
6028   PreprocessedEntityID BeginID =
6029       findPreprocessedEntity(Range.getBegin(), false);
6030   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6031   return std::make_pair(BeginID, EndID);
6032 }
6033 
6034 /// Optionally returns true or false if the preallocated preprocessed
6035 /// entity with index \arg Index came from file \arg FID.
6036 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6037                                                              FileID FID) {
6038   if (FID.isInvalid())
6039     return false;
6040 
6041   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6042   ModuleFile &M = *PPInfo.first;
6043   unsigned LocalIndex = PPInfo.second;
6044   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6045 
6046   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6047   if (Loc.isInvalid())
6048     return false;
6049 
6050   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6051     return true;
6052   else
6053     return false;
6054 }
6055 
6056 namespace {
6057 
6058   /// Visitor used to search for information about a header file.
6059   class HeaderFileInfoVisitor {
6060     const FileEntry *FE;
6061     Optional<HeaderFileInfo> HFI;
6062 
6063   public:
6064     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6065 
6066     bool operator()(ModuleFile &M) {
6067       HeaderFileInfoLookupTable *Table
6068         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6069       if (!Table)
6070         return false;
6071 
6072       // Look in the on-disk hash table for an entry for this file name.
6073       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6074       if (Pos == Table->end())
6075         return false;
6076 
6077       HFI = *Pos;
6078       return true;
6079     }
6080 
6081     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6082   };
6083 
6084 } // namespace
6085 
6086 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6087   HeaderFileInfoVisitor Visitor(FE);
6088   ModuleMgr.visit(Visitor);
6089   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6090     return *HFI;
6091 
6092   return HeaderFileInfo();
6093 }
6094 
6095 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6096   using DiagState = DiagnosticsEngine::DiagState;
6097   SmallVector<DiagState *, 32> DiagStates;
6098 
6099   for (ModuleFile &F : ModuleMgr) {
6100     unsigned Idx = 0;
6101     auto &Record = F.PragmaDiagMappings;
6102     if (Record.empty())
6103       continue;
6104 
6105     DiagStates.clear();
6106 
6107     auto ReadDiagState =
6108         [&](const DiagState &BasedOn, SourceLocation Loc,
6109             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6110       unsigned BackrefID = Record[Idx++];
6111       if (BackrefID != 0)
6112         return DiagStates[BackrefID - 1];
6113 
6114       // A new DiagState was created here.
6115       Diag.DiagStates.push_back(BasedOn);
6116       DiagState *NewState = &Diag.DiagStates.back();
6117       DiagStates.push_back(NewState);
6118       unsigned Size = Record[Idx++];
6119       assert(Idx + Size * 2 <= Record.size() &&
6120              "Invalid data, not enough diag/map pairs");
6121       while (Size--) {
6122         unsigned DiagID = Record[Idx++];
6123         DiagnosticMapping NewMapping =
6124             DiagnosticMapping::deserialize(Record[Idx++]);
6125         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6126           continue;
6127 
6128         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6129 
6130         // If this mapping was specified as a warning but the severity was
6131         // upgraded due to diagnostic settings, simulate the current diagnostic
6132         // settings (and use a warning).
6133         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6134           NewMapping.setSeverity(diag::Severity::Warning);
6135           NewMapping.setUpgradedFromWarning(false);
6136         }
6137 
6138         Mapping = NewMapping;
6139       }
6140       return NewState;
6141     };
6142 
6143     // Read the first state.
6144     DiagState *FirstState;
6145     if (F.Kind == MK_ImplicitModule) {
6146       // Implicitly-built modules are reused with different diagnostic
6147       // settings.  Use the initial diagnostic state from Diag to simulate this
6148       // compilation's diagnostic settings.
6149       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6150       DiagStates.push_back(FirstState);
6151 
6152       // Skip the initial diagnostic state from the serialized module.
6153       assert(Record[1] == 0 &&
6154              "Invalid data, unexpected backref in initial state");
6155       Idx = 3 + Record[2] * 2;
6156       assert(Idx < Record.size() &&
6157              "Invalid data, not enough state change pairs in initial state");
6158     } else if (F.isModule()) {
6159       // For an explicit module, preserve the flags from the module build
6160       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6161       // -Wblah flags.
6162       unsigned Flags = Record[Idx++];
6163       DiagState Initial;
6164       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6165       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6166       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6167       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6168       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6169       Initial.ExtBehavior = (diag::Severity)Flags;
6170       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6171 
6172       assert(F.OriginalSourceFileID.isValid());
6173 
6174       // Set up the root buffer of the module to start with the initial
6175       // diagnostic state of the module itself, to cover files that contain no
6176       // explicit transitions (for which we did not serialize anything).
6177       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6178           .StateTransitions.push_back({FirstState, 0});
6179     } else {
6180       // For prefix ASTs, start with whatever the user configured on the
6181       // command line.
6182       Idx++; // Skip flags.
6183       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6184                                  SourceLocation(), false);
6185     }
6186 
6187     // Read the state transitions.
6188     unsigned NumLocations = Record[Idx++];
6189     while (NumLocations--) {
6190       assert(Idx < Record.size() &&
6191              "Invalid data, missing pragma diagnostic states");
6192       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6193       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6194       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6195       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6196       unsigned Transitions = Record[Idx++];
6197 
6198       // Note that we don't need to set up Parent/ParentOffset here, because
6199       // we won't be changing the diagnostic state within imported FileIDs
6200       // (other than perhaps appending to the main source file, which has no
6201       // parent).
6202       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6203       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6204       for (unsigned I = 0; I != Transitions; ++I) {
6205         unsigned Offset = Record[Idx++];
6206         auto *State =
6207             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6208         F.StateTransitions.push_back({State, Offset});
6209       }
6210     }
6211 
6212     // Read the final state.
6213     assert(Idx < Record.size() &&
6214            "Invalid data, missing final pragma diagnostic state");
6215     SourceLocation CurStateLoc =
6216         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6217     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6218 
6219     if (!F.isModule()) {
6220       Diag.DiagStatesByLoc.CurDiagState = CurState;
6221       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6222 
6223       // Preserve the property that the imaginary root file describes the
6224       // current state.
6225       FileID NullFile;
6226       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6227       if (T.empty())
6228         T.push_back({CurState, 0});
6229       else
6230         T[0].State = CurState;
6231     }
6232 
6233     // Don't try to read these mappings again.
6234     Record.clear();
6235   }
6236 }
6237 
6238 /// Get the correct cursor and offset for loading a type.
6239 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6240   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6241   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6242   ModuleFile *M = I->second;
6243   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6244 }
6245 
6246 /// Read and return the type with the given index..
6247 ///
6248 /// The index is the type ID, shifted and minus the number of predefs. This
6249 /// routine actually reads the record corresponding to the type at the given
6250 /// location. It is a helper routine for GetType, which deals with reading type
6251 /// IDs.
6252 QualType ASTReader::readTypeRecord(unsigned Index) {
6253   assert(ContextObj && "reading type with no AST context");
6254   ASTContext &Context = *ContextObj;
6255   RecordLocation Loc = TypeCursorForIndex(Index);
6256   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6257 
6258   // Keep track of where we are in the stream, then jump back there
6259   // after reading this type.
6260   SavedStreamPosition SavedPosition(DeclsCursor);
6261 
6262   ReadingKindTracker ReadingKind(Read_Type, *this);
6263 
6264   // Note that we are loading a type record.
6265   Deserializing AType(this);
6266 
6267   unsigned Idx = 0;
6268   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6269     Error(std::move(Err));
6270     return QualType();
6271   }
6272   RecordData Record;
6273   Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6274   if (!MaybeCode) {
6275     Error(MaybeCode.takeError());
6276     return QualType();
6277   }
6278   unsigned Code = MaybeCode.get();
6279 
6280   Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6281   if (!MaybeTypeCode) {
6282     Error(MaybeTypeCode.takeError());
6283     return QualType();
6284   }
6285   switch ((TypeCode)MaybeTypeCode.get()) {
6286   case TYPE_EXT_QUAL: {
6287     if (Record.size() != 2) {
6288       Error("Incorrect encoding of extended qualifier type");
6289       return QualType();
6290     }
6291     QualType Base = readType(*Loc.F, Record, Idx);
6292     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6293     return Context.getQualifiedType(Base, Quals);
6294   }
6295 
6296   case TYPE_COMPLEX: {
6297     if (Record.size() != 1) {
6298       Error("Incorrect encoding of complex type");
6299       return QualType();
6300     }
6301     QualType ElemType = readType(*Loc.F, Record, Idx);
6302     return Context.getComplexType(ElemType);
6303   }
6304 
6305   case TYPE_POINTER: {
6306     if (Record.size() != 1) {
6307       Error("Incorrect encoding of pointer type");
6308       return QualType();
6309     }
6310     QualType PointeeType = readType(*Loc.F, Record, Idx);
6311     return Context.getPointerType(PointeeType);
6312   }
6313 
6314   case TYPE_DECAYED: {
6315     if (Record.size() != 1) {
6316       Error("Incorrect encoding of decayed type");
6317       return QualType();
6318     }
6319     QualType OriginalType = readType(*Loc.F, Record, Idx);
6320     QualType DT = Context.getAdjustedParameterType(OriginalType);
6321     if (!isa<DecayedType>(DT))
6322       Error("Decayed type does not decay");
6323     return DT;
6324   }
6325 
6326   case TYPE_ADJUSTED: {
6327     if (Record.size() != 2) {
6328       Error("Incorrect encoding of adjusted type");
6329       return QualType();
6330     }
6331     QualType OriginalTy = readType(*Loc.F, Record, Idx);
6332     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6333     return Context.getAdjustedType(OriginalTy, AdjustedTy);
6334   }
6335 
6336   case TYPE_BLOCK_POINTER: {
6337     if (Record.size() != 1) {
6338       Error("Incorrect encoding of block pointer type");
6339       return QualType();
6340     }
6341     QualType PointeeType = readType(*Loc.F, Record, Idx);
6342     return Context.getBlockPointerType(PointeeType);
6343   }
6344 
6345   case TYPE_LVALUE_REFERENCE: {
6346     if (Record.size() != 2) {
6347       Error("Incorrect encoding of lvalue reference type");
6348       return QualType();
6349     }
6350     QualType PointeeType = readType(*Loc.F, Record, Idx);
6351     return Context.getLValueReferenceType(PointeeType, Record[1]);
6352   }
6353 
6354   case TYPE_RVALUE_REFERENCE: {
6355     if (Record.size() != 1) {
6356       Error("Incorrect encoding of rvalue reference type");
6357       return QualType();
6358     }
6359     QualType PointeeType = readType(*Loc.F, Record, Idx);
6360     return Context.getRValueReferenceType(PointeeType);
6361   }
6362 
6363   case TYPE_MEMBER_POINTER: {
6364     if (Record.size() != 2) {
6365       Error("Incorrect encoding of member pointer type");
6366       return QualType();
6367     }
6368     QualType PointeeType = readType(*Loc.F, Record, Idx);
6369     QualType ClassType = readType(*Loc.F, Record, Idx);
6370     if (PointeeType.isNull() || ClassType.isNull())
6371       return QualType();
6372 
6373     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6374   }
6375 
6376   case TYPE_CONSTANT_ARRAY: {
6377     QualType ElementType = readType(*Loc.F, Record, Idx);
6378     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6379     unsigned IndexTypeQuals = Record[2];
6380     unsigned Idx = 3;
6381     llvm::APInt Size = ReadAPInt(Record, Idx);
6382     Expr *SizeExpr = ReadExpr(*Loc.F);
6383     return Context.getConstantArrayType(ElementType, Size, SizeExpr,
6384                                          ASM, IndexTypeQuals);
6385   }
6386 
6387   case TYPE_INCOMPLETE_ARRAY: {
6388     QualType ElementType = readType(*Loc.F, Record, Idx);
6389     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6390     unsigned IndexTypeQuals = Record[2];
6391     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6392   }
6393 
6394   case TYPE_VARIABLE_ARRAY: {
6395     QualType ElementType = readType(*Loc.F, Record, Idx);
6396     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6397     unsigned IndexTypeQuals = Record[2];
6398     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6399     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6400     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6401                                          ASM, IndexTypeQuals,
6402                                          SourceRange(LBLoc, RBLoc));
6403   }
6404 
6405   case TYPE_VECTOR: {
6406     if (Record.size() != 3) {
6407       Error("incorrect encoding of vector type in AST file");
6408       return QualType();
6409     }
6410 
6411     QualType ElementType = readType(*Loc.F, Record, Idx);
6412     unsigned NumElements = Record[1];
6413     unsigned VecKind = Record[2];
6414     return Context.getVectorType(ElementType, NumElements,
6415                                   (VectorType::VectorKind)VecKind);
6416   }
6417 
6418   case TYPE_EXT_VECTOR: {
6419     if (Record.size() != 3) {
6420       Error("incorrect encoding of extended vector type in AST file");
6421       return QualType();
6422     }
6423 
6424     QualType ElementType = readType(*Loc.F, Record, Idx);
6425     unsigned NumElements = Record[1];
6426     return Context.getExtVectorType(ElementType, NumElements);
6427   }
6428 
6429   case TYPE_FUNCTION_NO_PROTO: {
6430     if (Record.size() != 8) {
6431       Error("incorrect encoding of no-proto function type");
6432       return QualType();
6433     }
6434     QualType ResultType = readType(*Loc.F, Record, Idx);
6435     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6436                                (CallingConv)Record[4], Record[5], Record[6],
6437                                Record[7]);
6438     return Context.getFunctionNoProtoType(ResultType, Info);
6439   }
6440 
6441   case TYPE_FUNCTION_PROTO: {
6442     QualType ResultType = readType(*Loc.F, Record, Idx);
6443 
6444     FunctionProtoType::ExtProtoInfo EPI;
6445     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6446                                         /*hasregparm*/ Record[2],
6447                                         /*regparm*/ Record[3],
6448                                         static_cast<CallingConv>(Record[4]),
6449                                         /*produces*/ Record[5],
6450                                         /*nocallersavedregs*/ Record[6],
6451                                         /*nocfcheck*/ Record[7]);
6452 
6453     unsigned Idx = 8;
6454 
6455     EPI.Variadic = Record[Idx++];
6456     EPI.HasTrailingReturn = Record[Idx++];
6457     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6458     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6459     SmallVector<QualType, 8> ExceptionStorage;
6460     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6461 
6462     unsigned NumParams = Record[Idx++];
6463     SmallVector<QualType, 16> ParamTypes;
6464     for (unsigned I = 0; I != NumParams; ++I)
6465       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6466 
6467     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6468     if (Idx != Record.size()) {
6469       for (unsigned I = 0; I != NumParams; ++I)
6470         ExtParameterInfos.push_back(
6471           FunctionProtoType::ExtParameterInfo
6472                            ::getFromOpaqueValue(Record[Idx++]));
6473       EPI.ExtParameterInfos = ExtParameterInfos.data();
6474     }
6475 
6476     assert(Idx == Record.size());
6477 
6478     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6479   }
6480 
6481   case TYPE_UNRESOLVED_USING: {
6482     unsigned Idx = 0;
6483     return Context.getTypeDeclType(
6484                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6485   }
6486 
6487   case TYPE_TYPEDEF: {
6488     if (Record.size() != 2) {
6489       Error("incorrect encoding of typedef type");
6490       return QualType();
6491     }
6492     unsigned Idx = 0;
6493     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6494     QualType Canonical = readType(*Loc.F, Record, Idx);
6495     if (!Canonical.isNull())
6496       Canonical = Context.getCanonicalType(Canonical);
6497     return Context.getTypedefType(Decl, Canonical);
6498   }
6499 
6500   case TYPE_TYPEOF_EXPR:
6501     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6502 
6503   case TYPE_TYPEOF: {
6504     if (Record.size() != 1) {
6505       Error("incorrect encoding of typeof(type) in AST file");
6506       return QualType();
6507     }
6508     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6509     return Context.getTypeOfType(UnderlyingType);
6510   }
6511 
6512   case TYPE_DECLTYPE: {
6513     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6514     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6515   }
6516 
6517   case TYPE_UNARY_TRANSFORM: {
6518     QualType BaseType = readType(*Loc.F, Record, Idx);
6519     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6520     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6521     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6522   }
6523 
6524   case TYPE_AUTO: {
6525     QualType Deduced = readType(*Loc.F, Record, Idx);
6526     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6527     bool IsDependent = false, IsPack = false;
6528     if (Deduced.isNull()) {
6529       IsDependent = Record[Idx] > 0;
6530       IsPack = Record[Idx] > 1;
6531       ++Idx;
6532     }
6533     return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6534   }
6535 
6536   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6537     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6538     QualType Deduced = readType(*Loc.F, Record, Idx);
6539     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6540     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6541                                                         IsDependent);
6542   }
6543 
6544   case TYPE_RECORD: {
6545     if (Record.size() != 2) {
6546       Error("incorrect encoding of record type");
6547       return QualType();
6548     }
6549     unsigned Idx = 0;
6550     bool IsDependent = Record[Idx++];
6551     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6552     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6553     QualType T = Context.getRecordType(RD);
6554     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6555     return T;
6556   }
6557 
6558   case TYPE_ENUM: {
6559     if (Record.size() != 2) {
6560       Error("incorrect encoding of enum type");
6561       return QualType();
6562     }
6563     unsigned Idx = 0;
6564     bool IsDependent = Record[Idx++];
6565     QualType T
6566       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6567     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6568     return T;
6569   }
6570 
6571   case TYPE_ATTRIBUTED: {
6572     if (Record.size() != 3) {
6573       Error("incorrect encoding of attributed type");
6574       return QualType();
6575     }
6576     QualType modifiedType = readType(*Loc.F, Record, Idx);
6577     QualType equivalentType = readType(*Loc.F, Record, Idx);
6578     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6579     return Context.getAttributedType(kind, modifiedType, equivalentType);
6580   }
6581 
6582   case TYPE_PAREN: {
6583     if (Record.size() != 1) {
6584       Error("incorrect encoding of paren type");
6585       return QualType();
6586     }
6587     QualType InnerType = readType(*Loc.F, Record, Idx);
6588     return Context.getParenType(InnerType);
6589   }
6590 
6591   case TYPE_MACRO_QUALIFIED: {
6592     if (Record.size() != 2) {
6593       Error("incorrect encoding of macro defined type");
6594       return QualType();
6595     }
6596     QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6597     IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6598     return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6599   }
6600 
6601   case TYPE_PACK_EXPANSION: {
6602     if (Record.size() != 2) {
6603       Error("incorrect encoding of pack expansion type");
6604       return QualType();
6605     }
6606     QualType Pattern = readType(*Loc.F, Record, Idx);
6607     if (Pattern.isNull())
6608       return QualType();
6609     Optional<unsigned> NumExpansions;
6610     if (Record[1])
6611       NumExpansions = Record[1] - 1;
6612     return Context.getPackExpansionType(Pattern, NumExpansions);
6613   }
6614 
6615   case TYPE_ELABORATED: {
6616     unsigned Idx = 0;
6617     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6618     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6619     QualType NamedType = readType(*Loc.F, Record, Idx);
6620     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6621     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6622   }
6623 
6624   case TYPE_OBJC_INTERFACE: {
6625     unsigned Idx = 0;
6626     ObjCInterfaceDecl *ItfD
6627       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6628     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6629   }
6630 
6631   case TYPE_OBJC_TYPE_PARAM: {
6632     unsigned Idx = 0;
6633     ObjCTypeParamDecl *Decl
6634       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6635     unsigned NumProtos = Record[Idx++];
6636     SmallVector<ObjCProtocolDecl*, 4> Protos;
6637     for (unsigned I = 0; I != NumProtos; ++I)
6638       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6639     return Context.getObjCTypeParamType(Decl, Protos);
6640   }
6641 
6642   case TYPE_OBJC_OBJECT: {
6643     unsigned Idx = 0;
6644     QualType Base = readType(*Loc.F, Record, Idx);
6645     unsigned NumTypeArgs = Record[Idx++];
6646     SmallVector<QualType, 4> TypeArgs;
6647     for (unsigned I = 0; I != NumTypeArgs; ++I)
6648       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6649     unsigned NumProtos = Record[Idx++];
6650     SmallVector<ObjCProtocolDecl*, 4> Protos;
6651     for (unsigned I = 0; I != NumProtos; ++I)
6652       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6653     bool IsKindOf = Record[Idx++];
6654     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6655   }
6656 
6657   case TYPE_OBJC_OBJECT_POINTER: {
6658     unsigned Idx = 0;
6659     QualType Pointee = readType(*Loc.F, Record, Idx);
6660     return Context.getObjCObjectPointerType(Pointee);
6661   }
6662 
6663   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6664     unsigned Idx = 0;
6665     QualType Parm = readType(*Loc.F, Record, Idx);
6666     QualType Replacement = readType(*Loc.F, Record, Idx);
6667     return Context.getSubstTemplateTypeParmType(
6668         cast<TemplateTypeParmType>(Parm),
6669         Context.getCanonicalType(Replacement));
6670   }
6671 
6672   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6673     unsigned Idx = 0;
6674     QualType Parm = readType(*Loc.F, Record, Idx);
6675     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6676     return Context.getSubstTemplateTypeParmPackType(
6677                                                cast<TemplateTypeParmType>(Parm),
6678                                                      ArgPack);
6679   }
6680 
6681   case TYPE_INJECTED_CLASS_NAME: {
6682     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6683     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6684     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6685     // for AST reading, too much interdependencies.
6686     const Type *T = nullptr;
6687     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6688       if (const Type *Existing = DI->getTypeForDecl()) {
6689         T = Existing;
6690         break;
6691       }
6692     }
6693     if (!T) {
6694       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6695       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6696         DI->setTypeForDecl(T);
6697     }
6698     return QualType(T, 0);
6699   }
6700 
6701   case TYPE_TEMPLATE_TYPE_PARM: {
6702     unsigned Idx = 0;
6703     unsigned Depth = Record[Idx++];
6704     unsigned Index = Record[Idx++];
6705     bool Pack = Record[Idx++];
6706     TemplateTypeParmDecl *D
6707       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6708     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6709   }
6710 
6711   case TYPE_DEPENDENT_NAME: {
6712     unsigned Idx = 0;
6713     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6714     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6715     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6716     QualType Canon = readType(*Loc.F, Record, Idx);
6717     if (!Canon.isNull())
6718       Canon = Context.getCanonicalType(Canon);
6719     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6720   }
6721 
6722   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6723     unsigned Idx = 0;
6724     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6725     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6726     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6727     unsigned NumArgs = Record[Idx++];
6728     SmallVector<TemplateArgument, 8> Args;
6729     Args.reserve(NumArgs);
6730     while (NumArgs--)
6731       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6732     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6733                                                           Args);
6734   }
6735 
6736   case TYPE_DEPENDENT_SIZED_ARRAY: {
6737     unsigned Idx = 0;
6738 
6739     // ArrayType
6740     QualType ElementType = readType(*Loc.F, Record, Idx);
6741     ArrayType::ArraySizeModifier ASM
6742       = (ArrayType::ArraySizeModifier)Record[Idx++];
6743     unsigned IndexTypeQuals = Record[Idx++];
6744 
6745     // DependentSizedArrayType
6746     Expr *NumElts = ReadExpr(*Loc.F);
6747     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6748 
6749     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6750                                                IndexTypeQuals, Brackets);
6751   }
6752 
6753   case TYPE_TEMPLATE_SPECIALIZATION: {
6754     unsigned Idx = 0;
6755     bool IsDependent = Record[Idx++];
6756     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6757     SmallVector<TemplateArgument, 8> Args;
6758     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6759     QualType Underlying = readType(*Loc.F, Record, Idx);
6760     QualType T;
6761     if (Underlying.isNull())
6762       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6763     else
6764       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6765     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6766     return T;
6767   }
6768 
6769   case TYPE_ATOMIC: {
6770     if (Record.size() != 1) {
6771       Error("Incorrect encoding of atomic type");
6772       return QualType();
6773     }
6774     QualType ValueType = readType(*Loc.F, Record, Idx);
6775     return Context.getAtomicType(ValueType);
6776   }
6777 
6778   case TYPE_PIPE: {
6779     if (Record.size() != 2) {
6780       Error("Incorrect encoding of pipe type");
6781       return QualType();
6782     }
6783 
6784     // Reading the pipe element type.
6785     QualType ElementType = readType(*Loc.F, Record, Idx);
6786     unsigned ReadOnly = Record[1];
6787     return Context.getPipeType(ElementType, ReadOnly);
6788   }
6789 
6790   case TYPE_DEPENDENT_SIZED_VECTOR: {
6791     unsigned Idx = 0;
6792     QualType ElementType = readType(*Loc.F, Record, Idx);
6793     Expr *SizeExpr = ReadExpr(*Loc.F);
6794     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6795     unsigned VecKind = Record[Idx];
6796 
6797     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6798                                                (VectorType::VectorKind)VecKind);
6799   }
6800 
6801   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6802     unsigned Idx = 0;
6803 
6804     // DependentSizedExtVectorType
6805     QualType ElementType = readType(*Loc.F, Record, Idx);
6806     Expr *SizeExpr = ReadExpr(*Loc.F);
6807     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6808 
6809     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6810                                                   AttrLoc);
6811   }
6812 
6813   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6814     unsigned Idx = 0;
6815 
6816     // DependentAddressSpaceType
6817     QualType PointeeType = readType(*Loc.F, Record, Idx);
6818     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6819     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6820 
6821     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6822                                                    AttrLoc);
6823   }
6824   }
6825   llvm_unreachable("Invalid TypeCode!");
6826 }
6827 
6828 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6829                                   SmallVectorImpl<QualType> &Exceptions,
6830                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6831                                   const RecordData &Record, unsigned &Idx) {
6832   ExceptionSpecificationType EST =
6833       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6834   ESI.Type = EST;
6835   if (EST == EST_Dynamic) {
6836     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6837       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6838     ESI.Exceptions = Exceptions;
6839   } else if (isComputedNoexcept(EST)) {
6840     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6841   } else if (EST == EST_Uninstantiated) {
6842     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6843     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6844   } else if (EST == EST_Unevaluated) {
6845     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6846   }
6847 }
6848 
6849 namespace clang {
6850 
6851 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6852   ModuleFile *F;
6853   ASTReader *Reader;
6854   const ASTReader::RecordData &Record;
6855   unsigned &Idx;
6856 
6857   SourceLocation ReadSourceLocation() {
6858     return Reader->ReadSourceLocation(*F, Record, Idx);
6859   }
6860 
6861   TypeSourceInfo *GetTypeSourceInfo() {
6862     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6863   }
6864 
6865   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6866     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6867   }
6868 
6869   Attr *ReadAttr() {
6870     return Reader->ReadAttr(*F, Record, Idx);
6871   }
6872 
6873 public:
6874   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6875                 const ASTReader::RecordData &Record, unsigned &Idx)
6876       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6877 
6878   // We want compile-time assurance that we've enumerated all of
6879   // these, so unfortunately we have to declare them first, then
6880   // define them out-of-line.
6881 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6882 #define TYPELOC(CLASS, PARENT) \
6883   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6884 #include "clang/AST/TypeLocNodes.def"
6885 
6886   void VisitFunctionTypeLoc(FunctionTypeLoc);
6887   void VisitArrayTypeLoc(ArrayTypeLoc);
6888 };
6889 
6890 } // namespace clang
6891 
6892 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6893   // nothing to do
6894 }
6895 
6896 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6897   TL.setBuiltinLoc(ReadSourceLocation());
6898   if (TL.needsExtraLocalData()) {
6899     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6900     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6901     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6902     TL.setModeAttr(Record[Idx++]);
6903   }
6904 }
6905 
6906 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6907   TL.setNameLoc(ReadSourceLocation());
6908 }
6909 
6910 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6911   TL.setStarLoc(ReadSourceLocation());
6912 }
6913 
6914 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6915   // nothing to do
6916 }
6917 
6918 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6919   // nothing to do
6920 }
6921 
6922 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6923   TL.setExpansionLoc(ReadSourceLocation());
6924 }
6925 
6926 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6927   TL.setCaretLoc(ReadSourceLocation());
6928 }
6929 
6930 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6931   TL.setAmpLoc(ReadSourceLocation());
6932 }
6933 
6934 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6935   TL.setAmpAmpLoc(ReadSourceLocation());
6936 }
6937 
6938 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6939   TL.setStarLoc(ReadSourceLocation());
6940   TL.setClassTInfo(GetTypeSourceInfo());
6941 }
6942 
6943 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6944   TL.setLBracketLoc(ReadSourceLocation());
6945   TL.setRBracketLoc(ReadSourceLocation());
6946   if (Record[Idx++])
6947     TL.setSizeExpr(Reader->ReadExpr(*F));
6948   else
6949     TL.setSizeExpr(nullptr);
6950 }
6951 
6952 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6953   VisitArrayTypeLoc(TL);
6954 }
6955 
6956 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6957   VisitArrayTypeLoc(TL);
6958 }
6959 
6960 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6961   VisitArrayTypeLoc(TL);
6962 }
6963 
6964 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6965                                             DependentSizedArrayTypeLoc TL) {
6966   VisitArrayTypeLoc(TL);
6967 }
6968 
6969 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6970     DependentAddressSpaceTypeLoc TL) {
6971 
6972     TL.setAttrNameLoc(ReadSourceLocation());
6973     SourceRange range;
6974     range.setBegin(ReadSourceLocation());
6975     range.setEnd(ReadSourceLocation());
6976     TL.setAttrOperandParensRange(range);
6977     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6978 }
6979 
6980 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6981                                         DependentSizedExtVectorTypeLoc TL) {
6982   TL.setNameLoc(ReadSourceLocation());
6983 }
6984 
6985 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6986   TL.setNameLoc(ReadSourceLocation());
6987 }
6988 
6989 void TypeLocReader::VisitDependentVectorTypeLoc(
6990     DependentVectorTypeLoc TL) {
6991   TL.setNameLoc(ReadSourceLocation());
6992 }
6993 
6994 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6995   TL.setNameLoc(ReadSourceLocation());
6996 }
6997 
6998 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6999   TL.setLocalRangeBegin(ReadSourceLocation());
7000   TL.setLParenLoc(ReadSourceLocation());
7001   TL.setRParenLoc(ReadSourceLocation());
7002   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
7003                                        Reader->ReadSourceLocation(*F, Record, Idx)));
7004   TL.setLocalRangeEnd(ReadSourceLocation());
7005   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7006     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7007   }
7008 }
7009 
7010 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7011   VisitFunctionTypeLoc(TL);
7012 }
7013 
7014 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7015   VisitFunctionTypeLoc(TL);
7016 }
7017 
7018 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7019   TL.setNameLoc(ReadSourceLocation());
7020 }
7021 
7022 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7023   TL.setNameLoc(ReadSourceLocation());
7024 }
7025 
7026 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7027   TL.setTypeofLoc(ReadSourceLocation());
7028   TL.setLParenLoc(ReadSourceLocation());
7029   TL.setRParenLoc(ReadSourceLocation());
7030 }
7031 
7032 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7033   TL.setTypeofLoc(ReadSourceLocation());
7034   TL.setLParenLoc(ReadSourceLocation());
7035   TL.setRParenLoc(ReadSourceLocation());
7036   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7037 }
7038 
7039 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7040   TL.setNameLoc(ReadSourceLocation());
7041 }
7042 
7043 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7044   TL.setKWLoc(ReadSourceLocation());
7045   TL.setLParenLoc(ReadSourceLocation());
7046   TL.setRParenLoc(ReadSourceLocation());
7047   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7048 }
7049 
7050 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7051   TL.setNameLoc(ReadSourceLocation());
7052 }
7053 
7054 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7055     DeducedTemplateSpecializationTypeLoc TL) {
7056   TL.setTemplateNameLoc(ReadSourceLocation());
7057 }
7058 
7059 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7060   TL.setNameLoc(ReadSourceLocation());
7061 }
7062 
7063 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7064   TL.setNameLoc(ReadSourceLocation());
7065 }
7066 
7067 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7068   TL.setAttr(ReadAttr());
7069 }
7070 
7071 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7072   TL.setNameLoc(ReadSourceLocation());
7073 }
7074 
7075 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7076                                             SubstTemplateTypeParmTypeLoc TL) {
7077   TL.setNameLoc(ReadSourceLocation());
7078 }
7079 
7080 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7081                                           SubstTemplateTypeParmPackTypeLoc TL) {
7082   TL.setNameLoc(ReadSourceLocation());
7083 }
7084 
7085 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7086                                            TemplateSpecializationTypeLoc TL) {
7087   TL.setTemplateKeywordLoc(ReadSourceLocation());
7088   TL.setTemplateNameLoc(ReadSourceLocation());
7089   TL.setLAngleLoc(ReadSourceLocation());
7090   TL.setRAngleLoc(ReadSourceLocation());
7091   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7092     TL.setArgLocInfo(
7093         i,
7094         Reader->GetTemplateArgumentLocInfo(
7095             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7096 }
7097 
7098 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7099   TL.setLParenLoc(ReadSourceLocation());
7100   TL.setRParenLoc(ReadSourceLocation());
7101 }
7102 
7103 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7104   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7105   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7106 }
7107 
7108 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7109   TL.setNameLoc(ReadSourceLocation());
7110 }
7111 
7112 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7113   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7114   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7115   TL.setNameLoc(ReadSourceLocation());
7116 }
7117 
7118 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7119        DependentTemplateSpecializationTypeLoc TL) {
7120   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7121   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7122   TL.setTemplateKeywordLoc(ReadSourceLocation());
7123   TL.setTemplateNameLoc(ReadSourceLocation());
7124   TL.setLAngleLoc(ReadSourceLocation());
7125   TL.setRAngleLoc(ReadSourceLocation());
7126   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7127     TL.setArgLocInfo(
7128         I,
7129         Reader->GetTemplateArgumentLocInfo(
7130             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7131 }
7132 
7133 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7134   TL.setEllipsisLoc(ReadSourceLocation());
7135 }
7136 
7137 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7138   TL.setNameLoc(ReadSourceLocation());
7139 }
7140 
7141 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7142   if (TL.getNumProtocols()) {
7143     TL.setProtocolLAngleLoc(ReadSourceLocation());
7144     TL.setProtocolRAngleLoc(ReadSourceLocation());
7145   }
7146   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7147     TL.setProtocolLoc(i, ReadSourceLocation());
7148 }
7149 
7150 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7151   TL.setHasBaseTypeAsWritten(Record[Idx++]);
7152   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7153   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7154   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7155     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7156   TL.setProtocolLAngleLoc(ReadSourceLocation());
7157   TL.setProtocolRAngleLoc(ReadSourceLocation());
7158   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7159     TL.setProtocolLoc(i, ReadSourceLocation());
7160 }
7161 
7162 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7163   TL.setStarLoc(ReadSourceLocation());
7164 }
7165 
7166 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7167   TL.setKWLoc(ReadSourceLocation());
7168   TL.setLParenLoc(ReadSourceLocation());
7169   TL.setRParenLoc(ReadSourceLocation());
7170 }
7171 
7172 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7173   TL.setKWLoc(ReadSourceLocation());
7174 }
7175 
7176 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7177                             unsigned &Idx, TypeLoc TL) {
7178   TypeLocReader TLR(F, *this, Record, Idx);
7179   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7180     TLR.Visit(TL);
7181 }
7182 
7183 TypeSourceInfo *
7184 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7185                              unsigned &Idx) {
7186   QualType InfoTy = readType(F, Record, Idx);
7187   if (InfoTy.isNull())
7188     return nullptr;
7189 
7190   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7191   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7192   return TInfo;
7193 }
7194 
7195 QualType ASTReader::GetType(TypeID ID) {
7196   assert(ContextObj && "reading type with no AST context");
7197   ASTContext &Context = *ContextObj;
7198 
7199   unsigned FastQuals = ID & Qualifiers::FastMask;
7200   unsigned Index = ID >> Qualifiers::FastWidth;
7201 
7202   if (Index < NUM_PREDEF_TYPE_IDS) {
7203     QualType T;
7204     switch ((PredefinedTypeIDs)Index) {
7205     case PREDEF_TYPE_NULL_ID:
7206       return QualType();
7207     case PREDEF_TYPE_VOID_ID:
7208       T = Context.VoidTy;
7209       break;
7210     case PREDEF_TYPE_BOOL_ID:
7211       T = Context.BoolTy;
7212       break;
7213     case PREDEF_TYPE_CHAR_U_ID:
7214     case PREDEF_TYPE_CHAR_S_ID:
7215       // FIXME: Check that the signedness of CharTy is correct!
7216       T = Context.CharTy;
7217       break;
7218     case PREDEF_TYPE_UCHAR_ID:
7219       T = Context.UnsignedCharTy;
7220       break;
7221     case PREDEF_TYPE_USHORT_ID:
7222       T = Context.UnsignedShortTy;
7223       break;
7224     case PREDEF_TYPE_UINT_ID:
7225       T = Context.UnsignedIntTy;
7226       break;
7227     case PREDEF_TYPE_ULONG_ID:
7228       T = Context.UnsignedLongTy;
7229       break;
7230     case PREDEF_TYPE_ULONGLONG_ID:
7231       T = Context.UnsignedLongLongTy;
7232       break;
7233     case PREDEF_TYPE_UINT128_ID:
7234       T = Context.UnsignedInt128Ty;
7235       break;
7236     case PREDEF_TYPE_SCHAR_ID:
7237       T = Context.SignedCharTy;
7238       break;
7239     case PREDEF_TYPE_WCHAR_ID:
7240       T = Context.WCharTy;
7241       break;
7242     case PREDEF_TYPE_SHORT_ID:
7243       T = Context.ShortTy;
7244       break;
7245     case PREDEF_TYPE_INT_ID:
7246       T = Context.IntTy;
7247       break;
7248     case PREDEF_TYPE_LONG_ID:
7249       T = Context.LongTy;
7250       break;
7251     case PREDEF_TYPE_LONGLONG_ID:
7252       T = Context.LongLongTy;
7253       break;
7254     case PREDEF_TYPE_INT128_ID:
7255       T = Context.Int128Ty;
7256       break;
7257     case PREDEF_TYPE_HALF_ID:
7258       T = Context.HalfTy;
7259       break;
7260     case PREDEF_TYPE_FLOAT_ID:
7261       T = Context.FloatTy;
7262       break;
7263     case PREDEF_TYPE_DOUBLE_ID:
7264       T = Context.DoubleTy;
7265       break;
7266     case PREDEF_TYPE_LONGDOUBLE_ID:
7267       T = Context.LongDoubleTy;
7268       break;
7269     case PREDEF_TYPE_SHORT_ACCUM_ID:
7270       T = Context.ShortAccumTy;
7271       break;
7272     case PREDEF_TYPE_ACCUM_ID:
7273       T = Context.AccumTy;
7274       break;
7275     case PREDEF_TYPE_LONG_ACCUM_ID:
7276       T = Context.LongAccumTy;
7277       break;
7278     case PREDEF_TYPE_USHORT_ACCUM_ID:
7279       T = Context.UnsignedShortAccumTy;
7280       break;
7281     case PREDEF_TYPE_UACCUM_ID:
7282       T = Context.UnsignedAccumTy;
7283       break;
7284     case PREDEF_TYPE_ULONG_ACCUM_ID:
7285       T = Context.UnsignedLongAccumTy;
7286       break;
7287     case PREDEF_TYPE_SHORT_FRACT_ID:
7288       T = Context.ShortFractTy;
7289       break;
7290     case PREDEF_TYPE_FRACT_ID:
7291       T = Context.FractTy;
7292       break;
7293     case PREDEF_TYPE_LONG_FRACT_ID:
7294       T = Context.LongFractTy;
7295       break;
7296     case PREDEF_TYPE_USHORT_FRACT_ID:
7297       T = Context.UnsignedShortFractTy;
7298       break;
7299     case PREDEF_TYPE_UFRACT_ID:
7300       T = Context.UnsignedFractTy;
7301       break;
7302     case PREDEF_TYPE_ULONG_FRACT_ID:
7303       T = Context.UnsignedLongFractTy;
7304       break;
7305     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7306       T = Context.SatShortAccumTy;
7307       break;
7308     case PREDEF_TYPE_SAT_ACCUM_ID:
7309       T = Context.SatAccumTy;
7310       break;
7311     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7312       T = Context.SatLongAccumTy;
7313       break;
7314     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7315       T = Context.SatUnsignedShortAccumTy;
7316       break;
7317     case PREDEF_TYPE_SAT_UACCUM_ID:
7318       T = Context.SatUnsignedAccumTy;
7319       break;
7320     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7321       T = Context.SatUnsignedLongAccumTy;
7322       break;
7323     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7324       T = Context.SatShortFractTy;
7325       break;
7326     case PREDEF_TYPE_SAT_FRACT_ID:
7327       T = Context.SatFractTy;
7328       break;
7329     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7330       T = Context.SatLongFractTy;
7331       break;
7332     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7333       T = Context.SatUnsignedShortFractTy;
7334       break;
7335     case PREDEF_TYPE_SAT_UFRACT_ID:
7336       T = Context.SatUnsignedFractTy;
7337       break;
7338     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7339       T = Context.SatUnsignedLongFractTy;
7340       break;
7341     case PREDEF_TYPE_FLOAT16_ID:
7342       T = Context.Float16Ty;
7343       break;
7344     case PREDEF_TYPE_FLOAT128_ID:
7345       T = Context.Float128Ty;
7346       break;
7347     case PREDEF_TYPE_OVERLOAD_ID:
7348       T = Context.OverloadTy;
7349       break;
7350     case PREDEF_TYPE_BOUND_MEMBER:
7351       T = Context.BoundMemberTy;
7352       break;
7353     case PREDEF_TYPE_PSEUDO_OBJECT:
7354       T = Context.PseudoObjectTy;
7355       break;
7356     case PREDEF_TYPE_DEPENDENT_ID:
7357       T = Context.DependentTy;
7358       break;
7359     case PREDEF_TYPE_UNKNOWN_ANY:
7360       T = Context.UnknownAnyTy;
7361       break;
7362     case PREDEF_TYPE_NULLPTR_ID:
7363       T = Context.NullPtrTy;
7364       break;
7365     case PREDEF_TYPE_CHAR8_ID:
7366       T = Context.Char8Ty;
7367       break;
7368     case PREDEF_TYPE_CHAR16_ID:
7369       T = Context.Char16Ty;
7370       break;
7371     case PREDEF_TYPE_CHAR32_ID:
7372       T = Context.Char32Ty;
7373       break;
7374     case PREDEF_TYPE_OBJC_ID:
7375       T = Context.ObjCBuiltinIdTy;
7376       break;
7377     case PREDEF_TYPE_OBJC_CLASS:
7378       T = Context.ObjCBuiltinClassTy;
7379       break;
7380     case PREDEF_TYPE_OBJC_SEL:
7381       T = Context.ObjCBuiltinSelTy;
7382       break;
7383 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7384     case PREDEF_TYPE_##Id##_ID: \
7385       T = Context.SingletonId; \
7386       break;
7387 #include "clang/Basic/OpenCLImageTypes.def"
7388 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7389     case PREDEF_TYPE_##Id##_ID: \
7390       T = Context.Id##Ty; \
7391       break;
7392 #include "clang/Basic/OpenCLExtensionTypes.def"
7393     case PREDEF_TYPE_SAMPLER_ID:
7394       T = Context.OCLSamplerTy;
7395       break;
7396     case PREDEF_TYPE_EVENT_ID:
7397       T = Context.OCLEventTy;
7398       break;
7399     case PREDEF_TYPE_CLK_EVENT_ID:
7400       T = Context.OCLClkEventTy;
7401       break;
7402     case PREDEF_TYPE_QUEUE_ID:
7403       T = Context.OCLQueueTy;
7404       break;
7405     case PREDEF_TYPE_RESERVE_ID_ID:
7406       T = Context.OCLReserveIDTy;
7407       break;
7408     case PREDEF_TYPE_AUTO_DEDUCT:
7409       T = Context.getAutoDeductType();
7410       break;
7411     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7412       T = Context.getAutoRRefDeductType();
7413       break;
7414     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7415       T = Context.ARCUnbridgedCastTy;
7416       break;
7417     case PREDEF_TYPE_BUILTIN_FN:
7418       T = Context.BuiltinFnTy;
7419       break;
7420     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7421       T = Context.OMPArraySectionTy;
7422       break;
7423 #define SVE_TYPE(Name, Id, SingletonId) \
7424     case PREDEF_TYPE_##Id##_ID: \
7425       T = Context.SingletonId; \
7426       break;
7427 #include "clang/Basic/AArch64SVEACLETypes.def"
7428     }
7429 
7430     assert(!T.isNull() && "Unknown predefined type");
7431     return T.withFastQualifiers(FastQuals);
7432   }
7433 
7434   Index -= NUM_PREDEF_TYPE_IDS;
7435   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7436   if (TypesLoaded[Index].isNull()) {
7437     TypesLoaded[Index] = readTypeRecord(Index);
7438     if (TypesLoaded[Index].isNull())
7439       return QualType();
7440 
7441     TypesLoaded[Index]->setFromAST();
7442     if (DeserializationListener)
7443       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7444                                         TypesLoaded[Index]);
7445   }
7446 
7447   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7448 }
7449 
7450 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7451   return GetType(getGlobalTypeID(F, LocalID));
7452 }
7453 
7454 serialization::TypeID
7455 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7456   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7457   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7458 
7459   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7460     return LocalID;
7461 
7462   if (!F.ModuleOffsetMap.empty())
7463     ReadModuleOffsetMap(F);
7464 
7465   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7466     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7467   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7468 
7469   unsigned GlobalIndex = LocalIndex + I->second;
7470   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7471 }
7472 
7473 TemplateArgumentLocInfo
7474 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7475                                       TemplateArgument::ArgKind Kind,
7476                                       const RecordData &Record,
7477                                       unsigned &Index) {
7478   switch (Kind) {
7479   case TemplateArgument::Expression:
7480     return ReadExpr(F);
7481   case TemplateArgument::Type:
7482     return GetTypeSourceInfo(F, Record, Index);
7483   case TemplateArgument::Template: {
7484     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7485                                                                      Index);
7486     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7487     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7488                                    SourceLocation());
7489   }
7490   case TemplateArgument::TemplateExpansion: {
7491     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7492                                                                      Index);
7493     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7494     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7495     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7496                                    EllipsisLoc);
7497   }
7498   case TemplateArgument::Null:
7499   case TemplateArgument::Integral:
7500   case TemplateArgument::Declaration:
7501   case TemplateArgument::NullPtr:
7502   case TemplateArgument::Pack:
7503     // FIXME: Is this right?
7504     return TemplateArgumentLocInfo();
7505   }
7506   llvm_unreachable("unexpected template argument loc");
7507 }
7508 
7509 TemplateArgumentLoc
7510 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7511                                    const RecordData &Record, unsigned &Index) {
7512   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7513 
7514   if (Arg.getKind() == TemplateArgument::Expression) {
7515     if (Record[Index++]) // bool InfoHasSameExpr.
7516       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7517   }
7518   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7519                                                              Record, Index));
7520 }
7521 
7522 const ASTTemplateArgumentListInfo*
7523 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7524                                            const RecordData &Record,
7525                                            unsigned &Index) {
7526   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7527   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7528   unsigned NumArgsAsWritten = Record[Index++];
7529   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7530   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7531     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7532   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7533 }
7534 
7535 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7536   return GetDecl(ID);
7537 }
7538 
7539 void ASTReader::CompleteRedeclChain(const Decl *D) {
7540   if (NumCurrentElementsDeserializing) {
7541     // We arrange to not care about the complete redeclaration chain while we're
7542     // deserializing. Just remember that the AST has marked this one as complete
7543     // but that it's not actually complete yet, so we know we still need to
7544     // complete it later.
7545     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7546     return;
7547   }
7548 
7549   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7550 
7551   // If this is a named declaration, complete it by looking it up
7552   // within its context.
7553   //
7554   // FIXME: Merging a function definition should merge
7555   // all mergeable entities within it.
7556   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7557       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7558     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7559       if (!getContext().getLangOpts().CPlusPlus &&
7560           isa<TranslationUnitDecl>(DC)) {
7561         // Outside of C++, we don't have a lookup table for the TU, so update
7562         // the identifier instead. (For C++ modules, we don't store decls
7563         // in the serialized identifier table, so we do the lookup in the TU.)
7564         auto *II = Name.getAsIdentifierInfo();
7565         assert(II && "non-identifier name in C?");
7566         if (II->isOutOfDate())
7567           updateOutOfDateIdentifier(*II);
7568       } else
7569         DC->lookup(Name);
7570     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7571       // Find all declarations of this kind from the relevant context.
7572       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7573         auto *DC = cast<DeclContext>(DCDecl);
7574         SmallVector<Decl*, 8> Decls;
7575         FindExternalLexicalDecls(
7576             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7577       }
7578     }
7579   }
7580 
7581   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7582     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7583   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7584     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7585   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7586     if (auto *Template = FD->getPrimaryTemplate())
7587       Template->LoadLazySpecializations();
7588   }
7589 }
7590 
7591 CXXCtorInitializer **
7592 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7593   RecordLocation Loc = getLocalBitOffset(Offset);
7594   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7595   SavedStreamPosition SavedPosition(Cursor);
7596   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7597     Error(std::move(Err));
7598     return nullptr;
7599   }
7600   ReadingKindTracker ReadingKind(Read_Decl, *this);
7601 
7602   RecordData Record;
7603   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7604   if (!MaybeCode) {
7605     Error(MaybeCode.takeError());
7606     return nullptr;
7607   }
7608   unsigned Code = MaybeCode.get();
7609 
7610   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7611   if (!MaybeRecCode) {
7612     Error(MaybeRecCode.takeError());
7613     return nullptr;
7614   }
7615   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7616     Error("malformed AST file: missing C++ ctor initializers");
7617     return nullptr;
7618   }
7619 
7620   unsigned Idx = 0;
7621   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7622 }
7623 
7624 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7625   assert(ContextObj && "reading base specifiers with no AST context");
7626   ASTContext &Context = *ContextObj;
7627 
7628   RecordLocation Loc = getLocalBitOffset(Offset);
7629   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7630   SavedStreamPosition SavedPosition(Cursor);
7631   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7632     Error(std::move(Err));
7633     return nullptr;
7634   }
7635   ReadingKindTracker ReadingKind(Read_Decl, *this);
7636   RecordData Record;
7637 
7638   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7639   if (!MaybeCode) {
7640     Error(MaybeCode.takeError());
7641     return nullptr;
7642   }
7643   unsigned Code = MaybeCode.get();
7644 
7645   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7646   if (!MaybeRecCode) {
7647     Error(MaybeCode.takeError());
7648     return nullptr;
7649   }
7650   unsigned RecCode = MaybeRecCode.get();
7651 
7652   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7653     Error("malformed AST file: missing C++ base specifiers");
7654     return nullptr;
7655   }
7656 
7657   unsigned Idx = 0;
7658   unsigned NumBases = Record[Idx++];
7659   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7660   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7661   for (unsigned I = 0; I != NumBases; ++I)
7662     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7663   return Bases;
7664 }
7665 
7666 serialization::DeclID
7667 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7668   if (LocalID < NUM_PREDEF_DECL_IDS)
7669     return LocalID;
7670 
7671   if (!F.ModuleOffsetMap.empty())
7672     ReadModuleOffsetMap(F);
7673 
7674   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7675     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7676   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7677 
7678   return LocalID + I->second;
7679 }
7680 
7681 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7682                                    ModuleFile &M) const {
7683   // Predefined decls aren't from any module.
7684   if (ID < NUM_PREDEF_DECL_IDS)
7685     return false;
7686 
7687   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7688          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7689 }
7690 
7691 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7692   if (!D->isFromASTFile())
7693     return nullptr;
7694   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7695   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7696   return I->second;
7697 }
7698 
7699 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7700   if (ID < NUM_PREDEF_DECL_IDS)
7701     return SourceLocation();
7702 
7703   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7704 
7705   if (Index > DeclsLoaded.size()) {
7706     Error("declaration ID out-of-range for AST file");
7707     return SourceLocation();
7708   }
7709 
7710   if (Decl *D = DeclsLoaded[Index])
7711     return D->getLocation();
7712 
7713   SourceLocation Loc;
7714   DeclCursorForID(ID, Loc);
7715   return Loc;
7716 }
7717 
7718 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7719   switch (ID) {
7720   case PREDEF_DECL_NULL_ID:
7721     return nullptr;
7722 
7723   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7724     return Context.getTranslationUnitDecl();
7725 
7726   case PREDEF_DECL_OBJC_ID_ID:
7727     return Context.getObjCIdDecl();
7728 
7729   case PREDEF_DECL_OBJC_SEL_ID:
7730     return Context.getObjCSelDecl();
7731 
7732   case PREDEF_DECL_OBJC_CLASS_ID:
7733     return Context.getObjCClassDecl();
7734 
7735   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7736     return Context.getObjCProtocolDecl();
7737 
7738   case PREDEF_DECL_INT_128_ID:
7739     return Context.getInt128Decl();
7740 
7741   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7742     return Context.getUInt128Decl();
7743 
7744   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7745     return Context.getObjCInstanceTypeDecl();
7746 
7747   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7748     return Context.getBuiltinVaListDecl();
7749 
7750   case PREDEF_DECL_VA_LIST_TAG:
7751     return Context.getVaListTagDecl();
7752 
7753   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7754     return Context.getBuiltinMSVaListDecl();
7755 
7756   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7757     return Context.getExternCContextDecl();
7758 
7759   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7760     return Context.getMakeIntegerSeqDecl();
7761 
7762   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7763     return Context.getCFConstantStringDecl();
7764 
7765   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7766     return Context.getCFConstantStringTagDecl();
7767 
7768   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7769     return Context.getTypePackElementDecl();
7770   }
7771   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7772 }
7773 
7774 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7775   assert(ContextObj && "reading decl with no AST context");
7776   if (ID < NUM_PREDEF_DECL_IDS) {
7777     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7778     if (D) {
7779       // Track that we have merged the declaration with ID \p ID into the
7780       // pre-existing predefined declaration \p D.
7781       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7782       if (Merged.empty())
7783         Merged.push_back(ID);
7784     }
7785     return D;
7786   }
7787 
7788   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7789 
7790   if (Index >= DeclsLoaded.size()) {
7791     assert(0 && "declaration ID out-of-range for AST file");
7792     Error("declaration ID out-of-range for AST file");
7793     return nullptr;
7794   }
7795 
7796   return DeclsLoaded[Index];
7797 }
7798 
7799 Decl *ASTReader::GetDecl(DeclID ID) {
7800   if (ID < NUM_PREDEF_DECL_IDS)
7801     return GetExistingDecl(ID);
7802 
7803   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7804 
7805   if (Index >= DeclsLoaded.size()) {
7806     assert(0 && "declaration ID out-of-range for AST file");
7807     Error("declaration ID out-of-range for AST file");
7808     return nullptr;
7809   }
7810 
7811   if (!DeclsLoaded[Index]) {
7812     ReadDeclRecord(ID);
7813     if (DeserializationListener)
7814       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7815   }
7816 
7817   return DeclsLoaded[Index];
7818 }
7819 
7820 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7821                                                   DeclID GlobalID) {
7822   if (GlobalID < NUM_PREDEF_DECL_IDS)
7823     return GlobalID;
7824 
7825   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7826   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7827   ModuleFile *Owner = I->second;
7828 
7829   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7830     = M.GlobalToLocalDeclIDs.find(Owner);
7831   if (Pos == M.GlobalToLocalDeclIDs.end())
7832     return 0;
7833 
7834   return GlobalID - Owner->BaseDeclID + Pos->second;
7835 }
7836 
7837 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7838                                             const RecordData &Record,
7839                                             unsigned &Idx) {
7840   if (Idx >= Record.size()) {
7841     Error("Corrupted AST file");
7842     return 0;
7843   }
7844 
7845   return getGlobalDeclID(F, Record[Idx++]);
7846 }
7847 
7848 /// Resolve the offset of a statement into a statement.
7849 ///
7850 /// This operation will read a new statement from the external
7851 /// source each time it is called, and is meant to be used via a
7852 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7853 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7854   // Switch case IDs are per Decl.
7855   ClearSwitchCaseIDs();
7856 
7857   // Offset here is a global offset across the entire chain.
7858   RecordLocation Loc = getLocalBitOffset(Offset);
7859   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7860     Error(std::move(Err));
7861     return nullptr;
7862   }
7863   assert(NumCurrentElementsDeserializing == 0 &&
7864          "should not be called while already deserializing");
7865   Deserializing D(this);
7866   return ReadStmtFromStream(*Loc.F);
7867 }
7868 
7869 void ASTReader::FindExternalLexicalDecls(
7870     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7871     SmallVectorImpl<Decl *> &Decls) {
7872   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7873 
7874   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7875     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7876     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7877       auto K = (Decl::Kind)+LexicalDecls[I];
7878       if (!IsKindWeWant(K))
7879         continue;
7880 
7881       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7882 
7883       // Don't add predefined declarations to the lexical context more
7884       // than once.
7885       if (ID < NUM_PREDEF_DECL_IDS) {
7886         if (PredefsVisited[ID])
7887           continue;
7888 
7889         PredefsVisited[ID] = true;
7890       }
7891 
7892       if (Decl *D = GetLocalDecl(*M, ID)) {
7893         assert(D->getKind() == K && "wrong kind for lexical decl");
7894         if (!DC->isDeclInLexicalTraversal(D))
7895           Decls.push_back(D);
7896       }
7897     }
7898   };
7899 
7900   if (isa<TranslationUnitDecl>(DC)) {
7901     for (auto Lexical : TULexicalDecls)
7902       Visit(Lexical.first, Lexical.second);
7903   } else {
7904     auto I = LexicalDecls.find(DC);
7905     if (I != LexicalDecls.end())
7906       Visit(I->second.first, I->second.second);
7907   }
7908 
7909   ++NumLexicalDeclContextsRead;
7910 }
7911 
7912 namespace {
7913 
7914 class DeclIDComp {
7915   ASTReader &Reader;
7916   ModuleFile &Mod;
7917 
7918 public:
7919   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7920 
7921   bool operator()(LocalDeclID L, LocalDeclID R) const {
7922     SourceLocation LHS = getLocation(L);
7923     SourceLocation RHS = getLocation(R);
7924     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7925   }
7926 
7927   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7928     SourceLocation RHS = getLocation(R);
7929     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7930   }
7931 
7932   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7933     SourceLocation LHS = getLocation(L);
7934     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7935   }
7936 
7937   SourceLocation getLocation(LocalDeclID ID) const {
7938     return Reader.getSourceManager().getFileLoc(
7939             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7940   }
7941 };
7942 
7943 } // namespace
7944 
7945 void ASTReader::FindFileRegionDecls(FileID File,
7946                                     unsigned Offset, unsigned Length,
7947                                     SmallVectorImpl<Decl *> &Decls) {
7948   SourceManager &SM = getSourceManager();
7949 
7950   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7951   if (I == FileDeclIDs.end())
7952     return;
7953 
7954   FileDeclsInfo &DInfo = I->second;
7955   if (DInfo.Decls.empty())
7956     return;
7957 
7958   SourceLocation
7959     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7960   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7961 
7962   DeclIDComp DIDComp(*this, *DInfo.Mod);
7963   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7964       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7965   if (BeginIt != DInfo.Decls.begin())
7966     --BeginIt;
7967 
7968   // If we are pointing at a top-level decl inside an objc container, we need
7969   // to backtrack until we find it otherwise we will fail to report that the
7970   // region overlaps with an objc container.
7971   while (BeginIt != DInfo.Decls.begin() &&
7972          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7973              ->isTopLevelDeclInObjCContainer())
7974     --BeginIt;
7975 
7976   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7977       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7978   if (EndIt != DInfo.Decls.end())
7979     ++EndIt;
7980 
7981   for (ArrayRef<serialization::LocalDeclID>::iterator
7982          DIt = BeginIt; DIt != EndIt; ++DIt)
7983     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7984 }
7985 
7986 bool
7987 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7988                                           DeclarationName Name) {
7989   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7990          "DeclContext has no visible decls in storage");
7991   if (!Name)
7992     return false;
7993 
7994   auto It = Lookups.find(DC);
7995   if (It == Lookups.end())
7996     return false;
7997 
7998   Deserializing LookupResults(this);
7999 
8000   // Load the list of declarations.
8001   SmallVector<NamedDecl *, 64> Decls;
8002   for (DeclID ID : It->second.Table.find(Name)) {
8003     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8004     if (ND->getDeclName() == Name)
8005       Decls.push_back(ND);
8006   }
8007 
8008   ++NumVisibleDeclContextsRead;
8009   SetExternalVisibleDeclsForName(DC, Name, Decls);
8010   return !Decls.empty();
8011 }
8012 
8013 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8014   if (!DC->hasExternalVisibleStorage())
8015     return;
8016 
8017   auto It = Lookups.find(DC);
8018   assert(It != Lookups.end() &&
8019          "have external visible storage but no lookup tables");
8020 
8021   DeclsMap Decls;
8022 
8023   for (DeclID ID : It->second.Table.findAll()) {
8024     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8025     Decls[ND->getDeclName()].push_back(ND);
8026   }
8027 
8028   ++NumVisibleDeclContextsRead;
8029 
8030   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8031     SetExternalVisibleDeclsForName(DC, I->first, I->second);
8032   }
8033   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8034 }
8035 
8036 const serialization::reader::DeclContextLookupTable *
8037 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8038   auto I = Lookups.find(Primary);
8039   return I == Lookups.end() ? nullptr : &I->second;
8040 }
8041 
8042 /// Under non-PCH compilation the consumer receives the objc methods
8043 /// before receiving the implementation, and codegen depends on this.
8044 /// We simulate this by deserializing and passing to consumer the methods of the
8045 /// implementation before passing the deserialized implementation decl.
8046 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8047                                        ASTConsumer *Consumer) {
8048   assert(ImplD && Consumer);
8049 
8050   for (auto *I : ImplD->methods())
8051     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8052 
8053   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8054 }
8055 
8056 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8057   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8058     PassObjCImplDeclToConsumer(ImplD, Consumer);
8059   else
8060     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8061 }
8062 
8063 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8064   this->Consumer = Consumer;
8065 
8066   if (Consumer)
8067     PassInterestingDeclsToConsumer();
8068 
8069   if (DeserializationListener)
8070     DeserializationListener->ReaderInitialized(this);
8071 }
8072 
8073 void ASTReader::PrintStats() {
8074   std::fprintf(stderr, "*** AST File Statistics:\n");
8075 
8076   unsigned NumTypesLoaded
8077     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8078                                       QualType());
8079   unsigned NumDeclsLoaded
8080     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8081                                       (Decl *)nullptr);
8082   unsigned NumIdentifiersLoaded
8083     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8084                                             IdentifiersLoaded.end(),
8085                                             (IdentifierInfo *)nullptr);
8086   unsigned NumMacrosLoaded
8087     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8088                                        MacrosLoaded.end(),
8089                                        (MacroInfo *)nullptr);
8090   unsigned NumSelectorsLoaded
8091     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8092                                           SelectorsLoaded.end(),
8093                                           Selector());
8094 
8095   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8096     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8097                  NumSLocEntriesRead, TotalNumSLocEntries,
8098                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8099   if (!TypesLoaded.empty())
8100     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8101                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8102                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8103   if (!DeclsLoaded.empty())
8104     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8105                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8106                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8107   if (!IdentifiersLoaded.empty())
8108     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8109                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8110                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8111   if (!MacrosLoaded.empty())
8112     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8113                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8114                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8115   if (!SelectorsLoaded.empty())
8116     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8117                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8118                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8119   if (TotalNumStatements)
8120     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8121                  NumStatementsRead, TotalNumStatements,
8122                  ((float)NumStatementsRead/TotalNumStatements * 100));
8123   if (TotalNumMacros)
8124     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8125                  NumMacrosRead, TotalNumMacros,
8126                  ((float)NumMacrosRead/TotalNumMacros * 100));
8127   if (TotalLexicalDeclContexts)
8128     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8129                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8130                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8131                   * 100));
8132   if (TotalVisibleDeclContexts)
8133     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8134                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8135                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8136                   * 100));
8137   if (TotalNumMethodPoolEntries)
8138     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8139                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8140                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8141                   * 100));
8142   if (NumMethodPoolLookups)
8143     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8144                  NumMethodPoolHits, NumMethodPoolLookups,
8145                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8146   if (NumMethodPoolTableLookups)
8147     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8148                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8149                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8150                   * 100.0));
8151   if (NumIdentifierLookupHits)
8152     std::fprintf(stderr,
8153                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8154                  NumIdentifierLookupHits, NumIdentifierLookups,
8155                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8156 
8157   if (GlobalIndex) {
8158     std::fprintf(stderr, "\n");
8159     GlobalIndex->printStats();
8160   }
8161 
8162   std::fprintf(stderr, "\n");
8163   dump();
8164   std::fprintf(stderr, "\n");
8165 }
8166 
8167 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8168 LLVM_DUMP_METHOD static void
8169 dumpModuleIDMap(StringRef Name,
8170                 const ContinuousRangeMap<Key, ModuleFile *,
8171                                          InitialCapacity> &Map) {
8172   if (Map.begin() == Map.end())
8173     return;
8174 
8175   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8176 
8177   llvm::errs() << Name << ":\n";
8178   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8179        I != IEnd; ++I) {
8180     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8181       << "\n";
8182   }
8183 }
8184 
8185 LLVM_DUMP_METHOD void ASTReader::dump() {
8186   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8187   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8188   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8189   dumpModuleIDMap("Global type map", GlobalTypeMap);
8190   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8191   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8192   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8193   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8194   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8195   dumpModuleIDMap("Global preprocessed entity map",
8196                   GlobalPreprocessedEntityMap);
8197 
8198   llvm::errs() << "\n*** PCH/Modules Loaded:";
8199   for (ModuleFile &M : ModuleMgr)
8200     M.dump();
8201 }
8202 
8203 /// Return the amount of memory used by memory buffers, breaking down
8204 /// by heap-backed versus mmap'ed memory.
8205 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8206   for (ModuleFile &I : ModuleMgr) {
8207     if (llvm::MemoryBuffer *buf = I.Buffer) {
8208       size_t bytes = buf->getBufferSize();
8209       switch (buf->getBufferKind()) {
8210         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8211           sizes.malloc_bytes += bytes;
8212           break;
8213         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8214           sizes.mmap_bytes += bytes;
8215           break;
8216       }
8217     }
8218   }
8219 }
8220 
8221 void ASTReader::InitializeSema(Sema &S) {
8222   SemaObj = &S;
8223   S.addExternalSource(this);
8224 
8225   // Makes sure any declarations that were deserialized "too early"
8226   // still get added to the identifier's declaration chains.
8227   for (uint64_t ID : PreloadedDeclIDs) {
8228     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8229     pushExternalDeclIntoScope(D, D->getDeclName());
8230   }
8231   PreloadedDeclIDs.clear();
8232 
8233   // FIXME: What happens if these are changed by a module import?
8234   if (!FPPragmaOptions.empty()) {
8235     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8236     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8237   }
8238 
8239   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8240   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8241   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8242 
8243   UpdateSema();
8244 }
8245 
8246 void ASTReader::UpdateSema() {
8247   assert(SemaObj && "no Sema to update");
8248 
8249   // Load the offsets of the declarations that Sema references.
8250   // They will be lazily deserialized when needed.
8251   if (!SemaDeclRefs.empty()) {
8252     assert(SemaDeclRefs.size() % 3 == 0);
8253     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8254       if (!SemaObj->StdNamespace)
8255         SemaObj->StdNamespace = SemaDeclRefs[I];
8256       if (!SemaObj->StdBadAlloc)
8257         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8258       if (!SemaObj->StdAlignValT)
8259         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8260     }
8261     SemaDeclRefs.clear();
8262   }
8263 
8264   // Update the state of pragmas. Use the same API as if we had encountered the
8265   // pragma in the source.
8266   if(OptimizeOffPragmaLocation.isValid())
8267     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8268   if (PragmaMSStructState != -1)
8269     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8270   if (PointersToMembersPragmaLocation.isValid()) {
8271     SemaObj->ActOnPragmaMSPointersToMembers(
8272         (LangOptions::PragmaMSPointersToMembersKind)
8273             PragmaMSPointersToMembersState,
8274         PointersToMembersPragmaLocation);
8275   }
8276   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8277 
8278   if (PragmaPackCurrentValue) {
8279     // The bottom of the stack might have a default value. It must be adjusted
8280     // to the current value to ensure that the packing state is preserved after
8281     // popping entries that were included/imported from a PCH/module.
8282     bool DropFirst = false;
8283     if (!PragmaPackStack.empty() &&
8284         PragmaPackStack.front().Location.isInvalid()) {
8285       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8286              "Expected a default alignment value");
8287       SemaObj->PackStack.Stack.emplace_back(
8288           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8289           SemaObj->PackStack.CurrentPragmaLocation,
8290           PragmaPackStack.front().PushLocation);
8291       DropFirst = true;
8292     }
8293     for (const auto &Entry :
8294          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8295       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8296                                             Entry.Location, Entry.PushLocation);
8297     if (PragmaPackCurrentLocation.isInvalid()) {
8298       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8299              "Expected a default alignment value");
8300       // Keep the current values.
8301     } else {
8302       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8303       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8304     }
8305   }
8306 }
8307 
8308 IdentifierInfo *ASTReader::get(StringRef Name) {
8309   // Note that we are loading an identifier.
8310   Deserializing AnIdentifier(this);
8311 
8312   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8313                                   NumIdentifierLookups,
8314                                   NumIdentifierLookupHits);
8315 
8316   // We don't need to do identifier table lookups in C++ modules (we preload
8317   // all interesting declarations, and don't need to use the scope for name
8318   // lookups). Perform the lookup in PCH files, though, since we don't build
8319   // a complete initial identifier table if we're carrying on from a PCH.
8320   if (PP.getLangOpts().CPlusPlus) {
8321     for (auto F : ModuleMgr.pch_modules())
8322       if (Visitor(*F))
8323         break;
8324   } else {
8325     // If there is a global index, look there first to determine which modules
8326     // provably do not have any results for this identifier.
8327     GlobalModuleIndex::HitSet Hits;
8328     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8329     if (!loadGlobalIndex()) {
8330       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8331         HitsPtr = &Hits;
8332       }
8333     }
8334 
8335     ModuleMgr.visit(Visitor, HitsPtr);
8336   }
8337 
8338   IdentifierInfo *II = Visitor.getIdentifierInfo();
8339   markIdentifierUpToDate(II);
8340   return II;
8341 }
8342 
8343 namespace clang {
8344 
8345   /// An identifier-lookup iterator that enumerates all of the
8346   /// identifiers stored within a set of AST files.
8347   class ASTIdentifierIterator : public IdentifierIterator {
8348     /// The AST reader whose identifiers are being enumerated.
8349     const ASTReader &Reader;
8350 
8351     /// The current index into the chain of AST files stored in
8352     /// the AST reader.
8353     unsigned Index;
8354 
8355     /// The current position within the identifier lookup table
8356     /// of the current AST file.
8357     ASTIdentifierLookupTable::key_iterator Current;
8358 
8359     /// The end position within the identifier lookup table of
8360     /// the current AST file.
8361     ASTIdentifierLookupTable::key_iterator End;
8362 
8363     /// Whether to skip any modules in the ASTReader.
8364     bool SkipModules;
8365 
8366   public:
8367     explicit ASTIdentifierIterator(const ASTReader &Reader,
8368                                    bool SkipModules = false);
8369 
8370     StringRef Next() override;
8371   };
8372 
8373 } // namespace clang
8374 
8375 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8376                                              bool SkipModules)
8377     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8378 }
8379 
8380 StringRef ASTIdentifierIterator::Next() {
8381   while (Current == End) {
8382     // If we have exhausted all of our AST files, we're done.
8383     if (Index == 0)
8384       return StringRef();
8385 
8386     --Index;
8387     ModuleFile &F = Reader.ModuleMgr[Index];
8388     if (SkipModules && F.isModule())
8389       continue;
8390 
8391     ASTIdentifierLookupTable *IdTable =
8392         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8393     Current = IdTable->key_begin();
8394     End = IdTable->key_end();
8395   }
8396 
8397   // We have any identifiers remaining in the current AST file; return
8398   // the next one.
8399   StringRef Result = *Current;
8400   ++Current;
8401   return Result;
8402 }
8403 
8404 namespace {
8405 
8406 /// A utility for appending two IdentifierIterators.
8407 class ChainedIdentifierIterator : public IdentifierIterator {
8408   std::unique_ptr<IdentifierIterator> Current;
8409   std::unique_ptr<IdentifierIterator> Queued;
8410 
8411 public:
8412   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8413                             std::unique_ptr<IdentifierIterator> Second)
8414       : Current(std::move(First)), Queued(std::move(Second)) {}
8415 
8416   StringRef Next() override {
8417     if (!Current)
8418       return StringRef();
8419 
8420     StringRef result = Current->Next();
8421     if (!result.empty())
8422       return result;
8423 
8424     // Try the queued iterator, which may itself be empty.
8425     Current.reset();
8426     std::swap(Current, Queued);
8427     return Next();
8428   }
8429 };
8430 
8431 } // namespace
8432 
8433 IdentifierIterator *ASTReader::getIdentifiers() {
8434   if (!loadGlobalIndex()) {
8435     std::unique_ptr<IdentifierIterator> ReaderIter(
8436         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8437     std::unique_ptr<IdentifierIterator> ModulesIter(
8438         GlobalIndex->createIdentifierIterator());
8439     return new ChainedIdentifierIterator(std::move(ReaderIter),
8440                                          std::move(ModulesIter));
8441   }
8442 
8443   return new ASTIdentifierIterator(*this);
8444 }
8445 
8446 namespace clang {
8447 namespace serialization {
8448 
8449   class ReadMethodPoolVisitor {
8450     ASTReader &Reader;
8451     Selector Sel;
8452     unsigned PriorGeneration;
8453     unsigned InstanceBits = 0;
8454     unsigned FactoryBits = 0;
8455     bool InstanceHasMoreThanOneDecl = false;
8456     bool FactoryHasMoreThanOneDecl = false;
8457     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8458     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8459 
8460   public:
8461     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8462                           unsigned PriorGeneration)
8463         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8464 
8465     bool operator()(ModuleFile &M) {
8466       if (!M.SelectorLookupTable)
8467         return false;
8468 
8469       // If we've already searched this module file, skip it now.
8470       if (M.Generation <= PriorGeneration)
8471         return true;
8472 
8473       ++Reader.NumMethodPoolTableLookups;
8474       ASTSelectorLookupTable *PoolTable
8475         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8476       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8477       if (Pos == PoolTable->end())
8478         return false;
8479 
8480       ++Reader.NumMethodPoolTableHits;
8481       ++Reader.NumSelectorsRead;
8482       // FIXME: Not quite happy with the statistics here. We probably should
8483       // disable this tracking when called via LoadSelector.
8484       // Also, should entries without methods count as misses?
8485       ++Reader.NumMethodPoolEntriesRead;
8486       ASTSelectorLookupTrait::data_type Data = *Pos;
8487       if (Reader.DeserializationListener)
8488         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8489 
8490       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8491       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8492       InstanceBits = Data.InstanceBits;
8493       FactoryBits = Data.FactoryBits;
8494       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8495       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8496       return true;
8497     }
8498 
8499     /// Retrieve the instance methods found by this visitor.
8500     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8501       return InstanceMethods;
8502     }
8503 
8504     /// Retrieve the instance methods found by this visitor.
8505     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8506       return FactoryMethods;
8507     }
8508 
8509     unsigned getInstanceBits() const { return InstanceBits; }
8510     unsigned getFactoryBits() const { return FactoryBits; }
8511 
8512     bool instanceHasMoreThanOneDecl() const {
8513       return InstanceHasMoreThanOneDecl;
8514     }
8515 
8516     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8517   };
8518 
8519 } // namespace serialization
8520 } // namespace clang
8521 
8522 /// Add the given set of methods to the method list.
8523 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8524                              ObjCMethodList &List) {
8525   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8526     S.addMethodToGlobalList(&List, Methods[I]);
8527   }
8528 }
8529 
8530 void ASTReader::ReadMethodPool(Selector Sel) {
8531   // Get the selector generation and update it to the current generation.
8532   unsigned &Generation = SelectorGeneration[Sel];
8533   unsigned PriorGeneration = Generation;
8534   Generation = getGeneration();
8535   SelectorOutOfDate[Sel] = false;
8536 
8537   // Search for methods defined with this selector.
8538   ++NumMethodPoolLookups;
8539   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8540   ModuleMgr.visit(Visitor);
8541 
8542   if (Visitor.getInstanceMethods().empty() &&
8543       Visitor.getFactoryMethods().empty())
8544     return;
8545 
8546   ++NumMethodPoolHits;
8547 
8548   if (!getSema())
8549     return;
8550 
8551   Sema &S = *getSema();
8552   Sema::GlobalMethodPool::iterator Pos
8553     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8554 
8555   Pos->second.first.setBits(Visitor.getInstanceBits());
8556   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8557   Pos->second.second.setBits(Visitor.getFactoryBits());
8558   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8559 
8560   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8561   // when building a module we keep every method individually and may need to
8562   // update hasMoreThanOneDecl as we add the methods.
8563   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8564   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8565 }
8566 
8567 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8568   if (SelectorOutOfDate[Sel])
8569     ReadMethodPool(Sel);
8570 }
8571 
8572 void ASTReader::ReadKnownNamespaces(
8573                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8574   Namespaces.clear();
8575 
8576   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8577     if (NamespaceDecl *Namespace
8578                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8579       Namespaces.push_back(Namespace);
8580   }
8581 }
8582 
8583 void ASTReader::ReadUndefinedButUsed(
8584     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8585   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8586     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8587     SourceLocation Loc =
8588         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8589     Undefined.insert(std::make_pair(D, Loc));
8590   }
8591 }
8592 
8593 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8594     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8595                                                      Exprs) {
8596   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8597     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8598     uint64_t Count = DelayedDeleteExprs[Idx++];
8599     for (uint64_t C = 0; C < Count; ++C) {
8600       SourceLocation DeleteLoc =
8601           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8602       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8603       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8604     }
8605   }
8606 }
8607 
8608 void ASTReader::ReadTentativeDefinitions(
8609                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8610   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8611     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8612     if (Var)
8613       TentativeDefs.push_back(Var);
8614   }
8615   TentativeDefinitions.clear();
8616 }
8617 
8618 void ASTReader::ReadUnusedFileScopedDecls(
8619                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8620   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8621     DeclaratorDecl *D
8622       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8623     if (D)
8624       Decls.push_back(D);
8625   }
8626   UnusedFileScopedDecls.clear();
8627 }
8628 
8629 void ASTReader::ReadDelegatingConstructors(
8630                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8631   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8632     CXXConstructorDecl *D
8633       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8634     if (D)
8635       Decls.push_back(D);
8636   }
8637   DelegatingCtorDecls.clear();
8638 }
8639 
8640 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8641   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8642     TypedefNameDecl *D
8643       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8644     if (D)
8645       Decls.push_back(D);
8646   }
8647   ExtVectorDecls.clear();
8648 }
8649 
8650 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8651     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8652   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8653        ++I) {
8654     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8655         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8656     if (D)
8657       Decls.insert(D);
8658   }
8659   UnusedLocalTypedefNameCandidates.clear();
8660 }
8661 
8662 void ASTReader::ReadReferencedSelectors(
8663        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8664   if (ReferencedSelectorsData.empty())
8665     return;
8666 
8667   // If there are @selector references added them to its pool. This is for
8668   // implementation of -Wselector.
8669   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8670   unsigned I = 0;
8671   while (I < DataSize) {
8672     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8673     SourceLocation SelLoc
8674       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8675     Sels.push_back(std::make_pair(Sel, SelLoc));
8676   }
8677   ReferencedSelectorsData.clear();
8678 }
8679 
8680 void ASTReader::ReadWeakUndeclaredIdentifiers(
8681        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8682   if (WeakUndeclaredIdentifiers.empty())
8683     return;
8684 
8685   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8686     IdentifierInfo *WeakId
8687       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8688     IdentifierInfo *AliasId
8689       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8690     SourceLocation Loc
8691       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8692     bool Used = WeakUndeclaredIdentifiers[I++];
8693     WeakInfo WI(AliasId, Loc);
8694     WI.setUsed(Used);
8695     WeakIDs.push_back(std::make_pair(WeakId, WI));
8696   }
8697   WeakUndeclaredIdentifiers.clear();
8698 }
8699 
8700 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8701   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8702     ExternalVTableUse VT;
8703     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8704     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8705     VT.DefinitionRequired = VTableUses[Idx++];
8706     VTables.push_back(VT);
8707   }
8708 
8709   VTableUses.clear();
8710 }
8711 
8712 void ASTReader::ReadPendingInstantiations(
8713        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8714   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8715     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8716     SourceLocation Loc
8717       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8718 
8719     Pending.push_back(std::make_pair(D, Loc));
8720   }
8721   PendingInstantiations.clear();
8722 }
8723 
8724 void ASTReader::ReadLateParsedTemplates(
8725     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8726         &LPTMap) {
8727   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8728        /* In loop */) {
8729     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8730 
8731     auto LT = std::make_unique<LateParsedTemplate>();
8732     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8733 
8734     ModuleFile *F = getOwningModuleFile(LT->D);
8735     assert(F && "No module");
8736 
8737     unsigned TokN = LateParsedTemplates[Idx++];
8738     LT->Toks.reserve(TokN);
8739     for (unsigned T = 0; T < TokN; ++T)
8740       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8741 
8742     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8743   }
8744 
8745   LateParsedTemplates.clear();
8746 }
8747 
8748 void ASTReader::LoadSelector(Selector Sel) {
8749   // It would be complicated to avoid reading the methods anyway. So don't.
8750   ReadMethodPool(Sel);
8751 }
8752 
8753 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8754   assert(ID && "Non-zero identifier ID required");
8755   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8756   IdentifiersLoaded[ID - 1] = II;
8757   if (DeserializationListener)
8758     DeserializationListener->IdentifierRead(ID, II);
8759 }
8760 
8761 /// Set the globally-visible declarations associated with the given
8762 /// identifier.
8763 ///
8764 /// If the AST reader is currently in a state where the given declaration IDs
8765 /// cannot safely be resolved, they are queued until it is safe to resolve
8766 /// them.
8767 ///
8768 /// \param II an IdentifierInfo that refers to one or more globally-visible
8769 /// declarations.
8770 ///
8771 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8772 /// visible at global scope.
8773 ///
8774 /// \param Decls if non-null, this vector will be populated with the set of
8775 /// deserialized declarations. These declarations will not be pushed into
8776 /// scope.
8777 void
8778 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8779                               const SmallVectorImpl<uint32_t> &DeclIDs,
8780                                    SmallVectorImpl<Decl *> *Decls) {
8781   if (NumCurrentElementsDeserializing && !Decls) {
8782     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8783     return;
8784   }
8785 
8786   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8787     if (!SemaObj) {
8788       // Queue this declaration so that it will be added to the
8789       // translation unit scope and identifier's declaration chain
8790       // once a Sema object is known.
8791       PreloadedDeclIDs.push_back(DeclIDs[I]);
8792       continue;
8793     }
8794 
8795     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8796 
8797     // If we're simply supposed to record the declarations, do so now.
8798     if (Decls) {
8799       Decls->push_back(D);
8800       continue;
8801     }
8802 
8803     // Introduce this declaration into the translation-unit scope
8804     // and add it to the declaration chain for this identifier, so
8805     // that (unqualified) name lookup will find it.
8806     pushExternalDeclIntoScope(D, II);
8807   }
8808 }
8809 
8810 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8811   if (ID == 0)
8812     return nullptr;
8813 
8814   if (IdentifiersLoaded.empty()) {
8815     Error("no identifier table in AST file");
8816     return nullptr;
8817   }
8818 
8819   ID -= 1;
8820   if (!IdentifiersLoaded[ID]) {
8821     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8822     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8823     ModuleFile *M = I->second;
8824     unsigned Index = ID - M->BaseIdentifierID;
8825     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8826 
8827     // All of the strings in the AST file are preceded by a 16-bit length.
8828     // Extract that 16-bit length to avoid having to execute strlen().
8829     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8830     //  unsigned integers.  This is important to avoid integer overflow when
8831     //  we cast them to 'unsigned'.
8832     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8833     unsigned StrLen = (((unsigned) StrLenPtr[0])
8834                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8835     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8836     IdentifiersLoaded[ID] = &II;
8837     markIdentifierFromAST(*this,  II);
8838     if (DeserializationListener)
8839       DeserializationListener->IdentifierRead(ID + 1, &II);
8840   }
8841 
8842   return IdentifiersLoaded[ID];
8843 }
8844 
8845 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8846   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8847 }
8848 
8849 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8850   if (LocalID < NUM_PREDEF_IDENT_IDS)
8851     return LocalID;
8852 
8853   if (!M.ModuleOffsetMap.empty())
8854     ReadModuleOffsetMap(M);
8855 
8856   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8857     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8858   assert(I != M.IdentifierRemap.end()
8859          && "Invalid index into identifier index remap");
8860 
8861   return LocalID + I->second;
8862 }
8863 
8864 MacroInfo *ASTReader::getMacro(MacroID ID) {
8865   if (ID == 0)
8866     return nullptr;
8867 
8868   if (MacrosLoaded.empty()) {
8869     Error("no macro table in AST file");
8870     return nullptr;
8871   }
8872 
8873   ID -= NUM_PREDEF_MACRO_IDS;
8874   if (!MacrosLoaded[ID]) {
8875     GlobalMacroMapType::iterator I
8876       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8877     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8878     ModuleFile *M = I->second;
8879     unsigned Index = ID - M->BaseMacroID;
8880     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8881 
8882     if (DeserializationListener)
8883       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8884                                          MacrosLoaded[ID]);
8885   }
8886 
8887   return MacrosLoaded[ID];
8888 }
8889 
8890 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8891   if (LocalID < NUM_PREDEF_MACRO_IDS)
8892     return LocalID;
8893 
8894   if (!M.ModuleOffsetMap.empty())
8895     ReadModuleOffsetMap(M);
8896 
8897   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8898     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8899   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8900 
8901   return LocalID + I->second;
8902 }
8903 
8904 serialization::SubmoduleID
8905 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8906   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8907     return LocalID;
8908 
8909   if (!M.ModuleOffsetMap.empty())
8910     ReadModuleOffsetMap(M);
8911 
8912   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8913     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8914   assert(I != M.SubmoduleRemap.end()
8915          && "Invalid index into submodule index remap");
8916 
8917   return LocalID + I->second;
8918 }
8919 
8920 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8921   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8922     assert(GlobalID == 0 && "Unhandled global submodule ID");
8923     return nullptr;
8924   }
8925 
8926   if (GlobalID > SubmodulesLoaded.size()) {
8927     Error("submodule ID out of range in AST file");
8928     return nullptr;
8929   }
8930 
8931   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8932 }
8933 
8934 Module *ASTReader::getModule(unsigned ID) {
8935   return getSubmodule(ID);
8936 }
8937 
8938 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8939   ModuleFile *MF = getOwningModuleFile(D);
8940   return MF && MF->PCHHasObjectFile;
8941 }
8942 
8943 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8944   if (ID & 1) {
8945     // It's a module, look it up by submodule ID.
8946     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8947     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8948   } else {
8949     // It's a prefix (preamble, PCH, ...). Look it up by index.
8950     unsigned IndexFromEnd = ID >> 1;
8951     assert(IndexFromEnd && "got reference to unknown module file");
8952     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8953   }
8954 }
8955 
8956 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8957   if (!F)
8958     return 1;
8959 
8960   // For a file representing a module, use the submodule ID of the top-level
8961   // module as the file ID. For any other kind of file, the number of such
8962   // files loaded beforehand will be the same on reload.
8963   // FIXME: Is this true even if we have an explicit module file and a PCH?
8964   if (F->isModule())
8965     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8966 
8967   auto PCHModules = getModuleManager().pch_modules();
8968   auto I = llvm::find(PCHModules, F);
8969   assert(I != PCHModules.end() && "emitting reference to unknown file");
8970   return (I - PCHModules.end()) << 1;
8971 }
8972 
8973 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8974 ASTReader::getSourceDescriptor(unsigned ID) {
8975   if (const Module *M = getSubmodule(ID))
8976     return ExternalASTSource::ASTSourceDescriptor(*M);
8977 
8978   // If there is only a single PCH, return it instead.
8979   // Chained PCH are not supported.
8980   const auto &PCHChain = ModuleMgr.pch_modules();
8981   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8982     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8983     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8984     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8985     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8986                                           MF.Signature);
8987   }
8988   return None;
8989 }
8990 
8991 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8992   auto I = DefinitionSource.find(FD);
8993   if (I == DefinitionSource.end())
8994     return EK_ReplyHazy;
8995   return I->second ? EK_Never : EK_Always;
8996 }
8997 
8998 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8999   return DecodeSelector(getGlobalSelectorID(M, LocalID));
9000 }
9001 
9002 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9003   if (ID == 0)
9004     return Selector();
9005 
9006   if (ID > SelectorsLoaded.size()) {
9007     Error("selector ID out of range in AST file");
9008     return Selector();
9009   }
9010 
9011   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9012     // Load this selector from the selector table.
9013     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9014     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9015     ModuleFile &M = *I->second;
9016     ASTSelectorLookupTrait Trait(*this, M);
9017     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9018     SelectorsLoaded[ID - 1] =
9019       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9020     if (DeserializationListener)
9021       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9022   }
9023 
9024   return SelectorsLoaded[ID - 1];
9025 }
9026 
9027 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9028   return DecodeSelector(ID);
9029 }
9030 
9031 uint32_t ASTReader::GetNumExternalSelectors() {
9032   // ID 0 (the null selector) is considered an external selector.
9033   return getTotalNumSelectors() + 1;
9034 }
9035 
9036 serialization::SelectorID
9037 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9038   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9039     return LocalID;
9040 
9041   if (!M.ModuleOffsetMap.empty())
9042     ReadModuleOffsetMap(M);
9043 
9044   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9045     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9046   assert(I != M.SelectorRemap.end()
9047          && "Invalid index into selector index remap");
9048 
9049   return LocalID + I->second;
9050 }
9051 
9052 DeclarationName
9053 ASTReader::ReadDeclarationName(ModuleFile &F,
9054                                const RecordData &Record, unsigned &Idx) {
9055   ASTContext &Context = getContext();
9056   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9057   switch (Kind) {
9058   case DeclarationName::Identifier:
9059     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9060 
9061   case DeclarationName::ObjCZeroArgSelector:
9062   case DeclarationName::ObjCOneArgSelector:
9063   case DeclarationName::ObjCMultiArgSelector:
9064     return DeclarationName(ReadSelector(F, Record, Idx));
9065 
9066   case DeclarationName::CXXConstructorName:
9067     return Context.DeclarationNames.getCXXConstructorName(
9068                           Context.getCanonicalType(readType(F, Record, Idx)));
9069 
9070   case DeclarationName::CXXDestructorName:
9071     return Context.DeclarationNames.getCXXDestructorName(
9072                           Context.getCanonicalType(readType(F, Record, Idx)));
9073 
9074   case DeclarationName::CXXDeductionGuideName:
9075     return Context.DeclarationNames.getCXXDeductionGuideName(
9076                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
9077 
9078   case DeclarationName::CXXConversionFunctionName:
9079     return Context.DeclarationNames.getCXXConversionFunctionName(
9080                           Context.getCanonicalType(readType(F, Record, Idx)));
9081 
9082   case DeclarationName::CXXOperatorName:
9083     return Context.DeclarationNames.getCXXOperatorName(
9084                                        (OverloadedOperatorKind)Record[Idx++]);
9085 
9086   case DeclarationName::CXXLiteralOperatorName:
9087     return Context.DeclarationNames.getCXXLiteralOperatorName(
9088                                        GetIdentifierInfo(F, Record, Idx));
9089 
9090   case DeclarationName::CXXUsingDirective:
9091     return DeclarationName::getUsingDirectiveName();
9092   }
9093 
9094   llvm_unreachable("Invalid NameKind!");
9095 }
9096 
9097 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9098                                        DeclarationNameLoc &DNLoc,
9099                                        DeclarationName Name,
9100                                       const RecordData &Record, unsigned &Idx) {
9101   switch (Name.getNameKind()) {
9102   case DeclarationName::CXXConstructorName:
9103   case DeclarationName::CXXDestructorName:
9104   case DeclarationName::CXXConversionFunctionName:
9105     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9106     break;
9107 
9108   case DeclarationName::CXXOperatorName:
9109     DNLoc.CXXOperatorName.BeginOpNameLoc
9110         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9111     DNLoc.CXXOperatorName.EndOpNameLoc
9112         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9113     break;
9114 
9115   case DeclarationName::CXXLiteralOperatorName:
9116     DNLoc.CXXLiteralOperatorName.OpNameLoc
9117         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9118     break;
9119 
9120   case DeclarationName::Identifier:
9121   case DeclarationName::ObjCZeroArgSelector:
9122   case DeclarationName::ObjCOneArgSelector:
9123   case DeclarationName::ObjCMultiArgSelector:
9124   case DeclarationName::CXXUsingDirective:
9125   case DeclarationName::CXXDeductionGuideName:
9126     break;
9127   }
9128 }
9129 
9130 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9131                                         DeclarationNameInfo &NameInfo,
9132                                       const RecordData &Record, unsigned &Idx) {
9133   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9134   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9135   DeclarationNameLoc DNLoc;
9136   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9137   NameInfo.setInfo(DNLoc);
9138 }
9139 
9140 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9141                                   const RecordData &Record, unsigned &Idx) {
9142   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9143   unsigned NumTPLists = Record[Idx++];
9144   Info.NumTemplParamLists = NumTPLists;
9145   if (NumTPLists) {
9146     Info.TemplParamLists =
9147         new (getContext()) TemplateParameterList *[NumTPLists];
9148     for (unsigned i = 0; i != NumTPLists; ++i)
9149       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9150   }
9151 }
9152 
9153 TemplateName
9154 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9155                             unsigned &Idx) {
9156   ASTContext &Context = getContext();
9157   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9158   switch (Kind) {
9159   case TemplateName::Template:
9160       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9161 
9162   case TemplateName::OverloadedTemplate: {
9163     unsigned size = Record[Idx++];
9164     UnresolvedSet<8> Decls;
9165     while (size--)
9166       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9167 
9168     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9169   }
9170 
9171   case TemplateName::AssumedTemplate: {
9172     DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9173     return Context.getAssumedTemplateName(Name);
9174   }
9175 
9176   case TemplateName::QualifiedTemplate: {
9177     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9178     bool hasTemplKeyword = Record[Idx++];
9179     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9180     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9181   }
9182 
9183   case TemplateName::DependentTemplate: {
9184     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9185     if (Record[Idx++])  // isIdentifier
9186       return Context.getDependentTemplateName(NNS,
9187                                                GetIdentifierInfo(F, Record,
9188                                                                  Idx));
9189     return Context.getDependentTemplateName(NNS,
9190                                          (OverloadedOperatorKind)Record[Idx++]);
9191   }
9192 
9193   case TemplateName::SubstTemplateTemplateParm: {
9194     TemplateTemplateParmDecl *param
9195       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9196     if (!param) return TemplateName();
9197     TemplateName replacement = ReadTemplateName(F, Record, Idx);
9198     return Context.getSubstTemplateTemplateParm(param, replacement);
9199   }
9200 
9201   case TemplateName::SubstTemplateTemplateParmPack: {
9202     TemplateTemplateParmDecl *Param
9203       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9204     if (!Param)
9205       return TemplateName();
9206 
9207     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9208     if (ArgPack.getKind() != TemplateArgument::Pack)
9209       return TemplateName();
9210 
9211     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9212   }
9213   }
9214 
9215   llvm_unreachable("Unhandled template name kind!");
9216 }
9217 
9218 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9219                                                  const RecordData &Record,
9220                                                  unsigned &Idx,
9221                                                  bool Canonicalize) {
9222   ASTContext &Context = getContext();
9223   if (Canonicalize) {
9224     // The caller wants a canonical template argument. Sometimes the AST only
9225     // wants template arguments in canonical form (particularly as the template
9226     // argument lists of template specializations) so ensure we preserve that
9227     // canonical form across serialization.
9228     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9229     return Context.getCanonicalTemplateArgument(Arg);
9230   }
9231 
9232   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9233   switch (Kind) {
9234   case TemplateArgument::Null:
9235     return TemplateArgument();
9236   case TemplateArgument::Type:
9237     return TemplateArgument(readType(F, Record, Idx));
9238   case TemplateArgument::Declaration: {
9239     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9240     return TemplateArgument(D, readType(F, Record, Idx));
9241   }
9242   case TemplateArgument::NullPtr:
9243     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9244   case TemplateArgument::Integral: {
9245     llvm::APSInt Value = ReadAPSInt(Record, Idx);
9246     QualType T = readType(F, Record, Idx);
9247     return TemplateArgument(Context, Value, T);
9248   }
9249   case TemplateArgument::Template:
9250     return TemplateArgument(ReadTemplateName(F, Record, Idx));
9251   case TemplateArgument::TemplateExpansion: {
9252     TemplateName Name = ReadTemplateName(F, Record, Idx);
9253     Optional<unsigned> NumTemplateExpansions;
9254     if (unsigned NumExpansions = Record[Idx++])
9255       NumTemplateExpansions = NumExpansions - 1;
9256     return TemplateArgument(Name, NumTemplateExpansions);
9257   }
9258   case TemplateArgument::Expression:
9259     return TemplateArgument(ReadExpr(F));
9260   case TemplateArgument::Pack: {
9261     unsigned NumArgs = Record[Idx++];
9262     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9263     for (unsigned I = 0; I != NumArgs; ++I)
9264       Args[I] = ReadTemplateArgument(F, Record, Idx);
9265     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9266   }
9267   }
9268 
9269   llvm_unreachable("Unhandled template argument kind!");
9270 }
9271 
9272 TemplateParameterList *
9273 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9274                                      const RecordData &Record, unsigned &Idx) {
9275   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9276   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9277   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9278 
9279   unsigned NumParams = Record[Idx++];
9280   SmallVector<NamedDecl *, 16> Params;
9281   Params.reserve(NumParams);
9282   while (NumParams--)
9283     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9284 
9285   // TODO: Concepts
9286   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9287       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
9288   return TemplateParams;
9289 }
9290 
9291 void
9292 ASTReader::
9293 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9294                          ModuleFile &F, const RecordData &Record,
9295                          unsigned &Idx, bool Canonicalize) {
9296   unsigned NumTemplateArgs = Record[Idx++];
9297   TemplArgs.reserve(NumTemplateArgs);
9298   while (NumTemplateArgs--)
9299     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9300 }
9301 
9302 /// Read a UnresolvedSet structure.
9303 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9304                                   const RecordData &Record, unsigned &Idx) {
9305   unsigned NumDecls = Record[Idx++];
9306   Set.reserve(getContext(), NumDecls);
9307   while (NumDecls--) {
9308     DeclID ID = ReadDeclID(F, Record, Idx);
9309     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9310     Set.addLazyDecl(getContext(), ID, AS);
9311   }
9312 }
9313 
9314 CXXBaseSpecifier
9315 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9316                                 const RecordData &Record, unsigned &Idx) {
9317   bool isVirtual = static_cast<bool>(Record[Idx++]);
9318   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9319   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9320   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9321   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9322   SourceRange Range = ReadSourceRange(F, Record, Idx);
9323   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9324   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9325                           EllipsisLoc);
9326   Result.setInheritConstructors(inheritConstructors);
9327   return Result;
9328 }
9329 
9330 CXXCtorInitializer **
9331 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9332                                    unsigned &Idx) {
9333   ASTContext &Context = getContext();
9334   unsigned NumInitializers = Record[Idx++];
9335   assert(NumInitializers && "wrote ctor initializers but have no inits");
9336   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9337   for (unsigned i = 0; i != NumInitializers; ++i) {
9338     TypeSourceInfo *TInfo = nullptr;
9339     bool IsBaseVirtual = false;
9340     FieldDecl *Member = nullptr;
9341     IndirectFieldDecl *IndirectMember = nullptr;
9342 
9343     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9344     switch (Type) {
9345     case CTOR_INITIALIZER_BASE:
9346       TInfo = GetTypeSourceInfo(F, Record, Idx);
9347       IsBaseVirtual = Record[Idx++];
9348       break;
9349 
9350     case CTOR_INITIALIZER_DELEGATING:
9351       TInfo = GetTypeSourceInfo(F, Record, Idx);
9352       break;
9353 
9354      case CTOR_INITIALIZER_MEMBER:
9355       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9356       break;
9357 
9358      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9359       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9360       break;
9361     }
9362 
9363     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9364     Expr *Init = ReadExpr(F);
9365     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9366     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9367 
9368     CXXCtorInitializer *BOMInit;
9369     if (Type == CTOR_INITIALIZER_BASE)
9370       BOMInit = new (Context)
9371           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9372                              RParenLoc, MemberOrEllipsisLoc);
9373     else if (Type == CTOR_INITIALIZER_DELEGATING)
9374       BOMInit = new (Context)
9375           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9376     else if (Member)
9377       BOMInit = new (Context)
9378           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9379                              Init, RParenLoc);
9380     else
9381       BOMInit = new (Context)
9382           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9383                              LParenLoc, Init, RParenLoc);
9384 
9385     if (/*IsWritten*/Record[Idx++]) {
9386       unsigned SourceOrder = Record[Idx++];
9387       BOMInit->setSourceOrder(SourceOrder);
9388     }
9389 
9390     CtorInitializers[i] = BOMInit;
9391   }
9392 
9393   return CtorInitializers;
9394 }
9395 
9396 NestedNameSpecifier *
9397 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9398                                    const RecordData &Record, unsigned &Idx) {
9399   ASTContext &Context = getContext();
9400   unsigned N = Record[Idx++];
9401   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9402   for (unsigned I = 0; I != N; ++I) {
9403     NestedNameSpecifier::SpecifierKind Kind
9404       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9405     switch (Kind) {
9406     case NestedNameSpecifier::Identifier: {
9407       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9408       NNS = NestedNameSpecifier::Create(Context, Prev, II);
9409       break;
9410     }
9411 
9412     case NestedNameSpecifier::Namespace: {
9413       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9414       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9415       break;
9416     }
9417 
9418     case NestedNameSpecifier::NamespaceAlias: {
9419       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9420       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9421       break;
9422     }
9423 
9424     case NestedNameSpecifier::TypeSpec:
9425     case NestedNameSpecifier::TypeSpecWithTemplate: {
9426       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9427       if (!T)
9428         return nullptr;
9429 
9430       bool Template = Record[Idx++];
9431       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9432       break;
9433     }
9434 
9435     case NestedNameSpecifier::Global:
9436       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9437       // No associated value, and there can't be a prefix.
9438       break;
9439 
9440     case NestedNameSpecifier::Super: {
9441       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9442       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9443       break;
9444     }
9445     }
9446     Prev = NNS;
9447   }
9448   return NNS;
9449 }
9450 
9451 NestedNameSpecifierLoc
9452 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9453                                       unsigned &Idx) {
9454   ASTContext &Context = getContext();
9455   unsigned N = Record[Idx++];
9456   NestedNameSpecifierLocBuilder Builder;
9457   for (unsigned I = 0; I != N; ++I) {
9458     NestedNameSpecifier::SpecifierKind Kind
9459       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9460     switch (Kind) {
9461     case NestedNameSpecifier::Identifier: {
9462       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9463       SourceRange Range = ReadSourceRange(F, Record, Idx);
9464       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9465       break;
9466     }
9467 
9468     case NestedNameSpecifier::Namespace: {
9469       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9470       SourceRange Range = ReadSourceRange(F, Record, Idx);
9471       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9472       break;
9473     }
9474 
9475     case NestedNameSpecifier::NamespaceAlias: {
9476       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9477       SourceRange Range = ReadSourceRange(F, Record, Idx);
9478       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9479       break;
9480     }
9481 
9482     case NestedNameSpecifier::TypeSpec:
9483     case NestedNameSpecifier::TypeSpecWithTemplate: {
9484       bool Template = Record[Idx++];
9485       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9486       if (!T)
9487         return NestedNameSpecifierLoc();
9488       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9489 
9490       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9491       Builder.Extend(Context,
9492                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9493                      T->getTypeLoc(), ColonColonLoc);
9494       break;
9495     }
9496 
9497     case NestedNameSpecifier::Global: {
9498       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9499       Builder.MakeGlobal(Context, ColonColonLoc);
9500       break;
9501     }
9502 
9503     case NestedNameSpecifier::Super: {
9504       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9505       SourceRange Range = ReadSourceRange(F, Record, Idx);
9506       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9507       break;
9508     }
9509     }
9510   }
9511 
9512   return Builder.getWithLocInContext(Context);
9513 }
9514 
9515 SourceRange
9516 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9517                            unsigned &Idx) {
9518   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9519   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9520   return SourceRange(beg, end);
9521 }
9522 
9523 static FixedPointSemantics
9524 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9525                         unsigned &Idx) {
9526   unsigned Width = Record[Idx++];
9527   unsigned Scale = Record[Idx++];
9528   uint64_t Tmp = Record[Idx++];
9529   bool IsSigned = Tmp & 0x1;
9530   bool IsSaturated = Tmp & 0x2;
9531   bool HasUnsignedPadding = Tmp & 0x4;
9532   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9533                              HasUnsignedPadding);
9534 }
9535 
9536 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9537   unsigned Kind = Record[Idx++];
9538   switch (Kind) {
9539   case APValue::None:
9540     return APValue();
9541   case APValue::Indeterminate:
9542     return APValue::IndeterminateValue();
9543   case APValue::Int:
9544     return APValue(ReadAPSInt(Record, Idx));
9545   case APValue::Float: {
9546     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9547         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9548     return APValue(ReadAPFloat(Record, FloatSema, Idx));
9549   }
9550   case APValue::FixedPoint: {
9551     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9552     return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9553   }
9554   case APValue::ComplexInt: {
9555     llvm::APSInt First = ReadAPSInt(Record, Idx);
9556     return APValue(std::move(First), ReadAPSInt(Record, Idx));
9557   }
9558   case APValue::ComplexFloat: {
9559     const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9560         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9561     llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9562     const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9563         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9564     return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9565   }
9566   case APValue::LValue:
9567   case APValue::Vector:
9568   case APValue::Array:
9569   case APValue::Struct:
9570   case APValue::Union:
9571   case APValue::MemberPointer:
9572   case APValue::AddrLabelDiff:
9573     // TODO : Handle all these APValue::ValueKind.
9574     return APValue();
9575   }
9576   llvm_unreachable("Invalid APValue::ValueKind");
9577 }
9578 
9579 /// Read an integral value
9580 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9581   unsigned BitWidth = Record[Idx++];
9582   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9583   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9584   Idx += NumWords;
9585   return Result;
9586 }
9587 
9588 /// Read a signed integral value
9589 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9590   bool isUnsigned = Record[Idx++];
9591   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9592 }
9593 
9594 /// Read a floating-point value
9595 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9596                                      const llvm::fltSemantics &Sem,
9597                                      unsigned &Idx) {
9598   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9599 }
9600 
9601 // Read a string
9602 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9603   unsigned Len = Record[Idx++];
9604   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9605   Idx += Len;
9606   return Result;
9607 }
9608 
9609 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9610                                 unsigned &Idx) {
9611   std::string Filename = ReadString(Record, Idx);
9612   ResolveImportedPath(F, Filename);
9613   return Filename;
9614 }
9615 
9616 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9617                                 const RecordData &Record, unsigned &Idx) {
9618   std::string Filename = ReadString(Record, Idx);
9619   if (!BaseDirectory.empty())
9620     ResolveImportedPath(Filename, BaseDirectory);
9621   return Filename;
9622 }
9623 
9624 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9625                                          unsigned &Idx) {
9626   unsigned Major = Record[Idx++];
9627   unsigned Minor = Record[Idx++];
9628   unsigned Subminor = Record[Idx++];
9629   if (Minor == 0)
9630     return VersionTuple(Major);
9631   if (Subminor == 0)
9632     return VersionTuple(Major, Minor - 1);
9633   return VersionTuple(Major, Minor - 1, Subminor - 1);
9634 }
9635 
9636 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9637                                           const RecordData &Record,
9638                                           unsigned &Idx) {
9639   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9640   return CXXTemporary::Create(getContext(), Decl);
9641 }
9642 
9643 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9644   return Diag(CurrentImportLoc, DiagID);
9645 }
9646 
9647 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9648   return Diags.Report(Loc, DiagID);
9649 }
9650 
9651 /// Retrieve the identifier table associated with the
9652 /// preprocessor.
9653 IdentifierTable &ASTReader::getIdentifierTable() {
9654   return PP.getIdentifierTable();
9655 }
9656 
9657 /// Record that the given ID maps to the given switch-case
9658 /// statement.
9659 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9660   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9661          "Already have a SwitchCase with this ID");
9662   (*CurrSwitchCaseStmts)[ID] = SC;
9663 }
9664 
9665 /// Retrieve the switch-case statement with the given ID.
9666 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9667   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9668   return (*CurrSwitchCaseStmts)[ID];
9669 }
9670 
9671 void ASTReader::ClearSwitchCaseIDs() {
9672   CurrSwitchCaseStmts->clear();
9673 }
9674 
9675 void ASTReader::ReadComments() {
9676   ASTContext &Context = getContext();
9677   std::vector<RawComment *> Comments;
9678   for (SmallVectorImpl<std::pair<BitstreamCursor,
9679                                  serialization::ModuleFile *>>::iterator
9680        I = CommentsCursors.begin(),
9681        E = CommentsCursors.end();
9682        I != E; ++I) {
9683     Comments.clear();
9684     BitstreamCursor &Cursor = I->first;
9685     serialization::ModuleFile &F = *I->second;
9686     SavedStreamPosition SavedPosition(Cursor);
9687 
9688     RecordData Record;
9689     while (true) {
9690       Expected<llvm::BitstreamEntry> MaybeEntry =
9691           Cursor.advanceSkippingSubblocks(
9692               BitstreamCursor::AF_DontPopBlockAtEnd);
9693       if (!MaybeEntry) {
9694         Error(MaybeEntry.takeError());
9695         return;
9696       }
9697       llvm::BitstreamEntry Entry = MaybeEntry.get();
9698 
9699       switch (Entry.Kind) {
9700       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9701       case llvm::BitstreamEntry::Error:
9702         Error("malformed block record in AST file");
9703         return;
9704       case llvm::BitstreamEntry::EndBlock:
9705         goto NextCursor;
9706       case llvm::BitstreamEntry::Record:
9707         // The interesting case.
9708         break;
9709       }
9710 
9711       // Read a record.
9712       Record.clear();
9713       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9714       if (!MaybeComment) {
9715         Error(MaybeComment.takeError());
9716         return;
9717       }
9718       switch ((CommentRecordTypes)MaybeComment.get()) {
9719       case COMMENTS_RAW_COMMENT: {
9720         unsigned Idx = 0;
9721         SourceRange SR = ReadSourceRange(F, Record, Idx);
9722         RawComment::CommentKind Kind =
9723             (RawComment::CommentKind) Record[Idx++];
9724         bool IsTrailingComment = Record[Idx++];
9725         bool IsAlmostTrailingComment = Record[Idx++];
9726         Comments.push_back(new (Context) RawComment(
9727             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9728         break;
9729       }
9730       }
9731     }
9732   NextCursor:
9733     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9734         FileToOffsetToComment;
9735     for (RawComment *C : Comments) {
9736       SourceLocation CommentLoc = C->getBeginLoc();
9737       if (CommentLoc.isValid()) {
9738         std::pair<FileID, unsigned> Loc =
9739             SourceMgr.getDecomposedLoc(CommentLoc);
9740         if (Loc.first.isValid())
9741           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9742       }
9743     }
9744   }
9745 }
9746 
9747 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9748                                 bool IncludeSystem, bool Complain,
9749                     llvm::function_ref<void(const serialization::InputFile &IF,
9750                                             bool isSystem)> Visitor) {
9751   unsigned NumUserInputs = MF.NumUserInputFiles;
9752   unsigned NumInputs = MF.InputFilesLoaded.size();
9753   assert(NumUserInputs <= NumInputs);
9754   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9755   for (unsigned I = 0; I < N; ++I) {
9756     bool IsSystem = I >= NumUserInputs;
9757     InputFile IF = getInputFile(MF, I+1, Complain);
9758     Visitor(IF, IsSystem);
9759   }
9760 }
9761 
9762 void ASTReader::visitTopLevelModuleMaps(
9763     serialization::ModuleFile &MF,
9764     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9765   unsigned NumInputs = MF.InputFilesLoaded.size();
9766   for (unsigned I = 0; I < NumInputs; ++I) {
9767     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9768     if (IFI.TopLevelModuleMap)
9769       // FIXME: This unnecessarily re-reads the InputFileInfo.
9770       if (auto *FE = getInputFile(MF, I + 1).getFile())
9771         Visitor(FE);
9772   }
9773 }
9774 
9775 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9776   // If we know the owning module, use it.
9777   if (Module *M = D->getImportedOwningModule())
9778     return M->getFullModuleName();
9779 
9780   // Otherwise, use the name of the top-level module the decl is within.
9781   if (ModuleFile *M = getOwningModuleFile(D))
9782     return M->ModuleName;
9783 
9784   // Not from a module.
9785   return {};
9786 }
9787 
9788 void ASTReader::finishPendingActions() {
9789   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9790          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9791          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9792          !PendingUpdateRecords.empty()) {
9793     // If any identifiers with corresponding top-level declarations have
9794     // been loaded, load those declarations now.
9795     using TopLevelDeclsMap =
9796         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9797     TopLevelDeclsMap TopLevelDecls;
9798 
9799     while (!PendingIdentifierInfos.empty()) {
9800       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9801       SmallVector<uint32_t, 4> DeclIDs =
9802           std::move(PendingIdentifierInfos.back().second);
9803       PendingIdentifierInfos.pop_back();
9804 
9805       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9806     }
9807 
9808     // Load each function type that we deferred loading because it was a
9809     // deduced type that might refer to a local type declared within itself.
9810     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9811       auto *FD = PendingFunctionTypes[I].first;
9812       FD->setType(GetType(PendingFunctionTypes[I].second));
9813 
9814       // If we gave a function a deduced return type, remember that we need to
9815       // propagate that along the redeclaration chain.
9816       auto *DT = FD->getReturnType()->getContainedDeducedType();
9817       if (DT && DT->isDeduced())
9818         PendingDeducedTypeUpdates.insert(
9819             {FD->getCanonicalDecl(), FD->getReturnType()});
9820     }
9821     PendingFunctionTypes.clear();
9822 
9823     // For each decl chain that we wanted to complete while deserializing, mark
9824     // it as "still needs to be completed".
9825     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9826       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9827     }
9828     PendingIncompleteDeclChains.clear();
9829 
9830     // Load pending declaration chains.
9831     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9832       loadPendingDeclChain(PendingDeclChains[I].first,
9833                            PendingDeclChains[I].second);
9834     PendingDeclChains.clear();
9835 
9836     // Make the most recent of the top-level declarations visible.
9837     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9838            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9839       IdentifierInfo *II = TLD->first;
9840       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9841         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9842       }
9843     }
9844 
9845     // Load any pending macro definitions.
9846     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9847       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9848       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9849       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9850       // Initialize the macro history from chained-PCHs ahead of module imports.
9851       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9852            ++IDIdx) {
9853         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9854         if (!Info.M->isModule())
9855           resolvePendingMacro(II, Info);
9856       }
9857       // Handle module imports.
9858       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9859            ++IDIdx) {
9860         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9861         if (Info.M->isModule())
9862           resolvePendingMacro(II, Info);
9863       }
9864     }
9865     PendingMacroIDs.clear();
9866 
9867     // Wire up the DeclContexts for Decls that we delayed setting until
9868     // recursive loading is completed.
9869     while (!PendingDeclContextInfos.empty()) {
9870       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9871       PendingDeclContextInfos.pop_front();
9872       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9873       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9874       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9875     }
9876 
9877     // Perform any pending declaration updates.
9878     while (!PendingUpdateRecords.empty()) {
9879       auto Update = PendingUpdateRecords.pop_back_val();
9880       ReadingKindTracker ReadingKind(Read_Decl, *this);
9881       loadDeclUpdateRecords(Update);
9882     }
9883   }
9884 
9885   // At this point, all update records for loaded decls are in place, so any
9886   // fake class definitions should have become real.
9887   assert(PendingFakeDefinitionData.empty() &&
9888          "faked up a class definition but never saw the real one");
9889 
9890   // If we deserialized any C++ or Objective-C class definitions, any
9891   // Objective-C protocol definitions, or any redeclarable templates, make sure
9892   // that all redeclarations point to the definitions. Note that this can only
9893   // happen now, after the redeclaration chains have been fully wired.
9894   for (Decl *D : PendingDefinitions) {
9895     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9896       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9897         // Make sure that the TagType points at the definition.
9898         const_cast<TagType*>(TagT)->decl = TD;
9899       }
9900 
9901       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9902         for (auto *R = getMostRecentExistingDecl(RD); R;
9903              R = R->getPreviousDecl()) {
9904           assert((R == D) ==
9905                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9906                  "declaration thinks it's the definition but it isn't");
9907           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9908         }
9909       }
9910 
9911       continue;
9912     }
9913 
9914     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9915       // Make sure that the ObjCInterfaceType points at the definition.
9916       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9917         ->Decl = ID;
9918 
9919       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9920         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9921 
9922       continue;
9923     }
9924 
9925     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9926       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9927         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9928 
9929       continue;
9930     }
9931 
9932     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9933     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9934       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9935   }
9936   PendingDefinitions.clear();
9937 
9938   // Load the bodies of any functions or methods we've encountered. We do
9939   // this now (delayed) so that we can be sure that the declaration chains
9940   // have been fully wired up (hasBody relies on this).
9941   // FIXME: We shouldn't require complete redeclaration chains here.
9942   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9943                                PBEnd = PendingBodies.end();
9944        PB != PBEnd; ++PB) {
9945     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9946       // For a function defined inline within a class template, force the
9947       // canonical definition to be the one inside the canonical definition of
9948       // the template. This ensures that we instantiate from a correct view
9949       // of the template.
9950       //
9951       // Sadly we can't do this more generally: we can't be sure that all
9952       // copies of an arbitrary class definition will have the same members
9953       // defined (eg, some member functions may not be instantiated, and some
9954       // special members may or may not have been implicitly defined).
9955       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9956         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9957           continue;
9958 
9959       // FIXME: Check for =delete/=default?
9960       // FIXME: Complain about ODR violations here?
9961       const FunctionDecl *Defn = nullptr;
9962       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9963         FD->setLazyBody(PB->second);
9964       } else {
9965         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9966         mergeDefinitionVisibility(NonConstDefn, FD);
9967 
9968         if (!FD->isLateTemplateParsed() &&
9969             !NonConstDefn->isLateTemplateParsed() &&
9970             FD->getODRHash() != NonConstDefn->getODRHash()) {
9971           if (!isa<CXXMethodDecl>(FD)) {
9972             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9973           } else if (FD->getLexicalParent()->isFileContext() &&
9974                      NonConstDefn->getLexicalParent()->isFileContext()) {
9975             // Only diagnose out-of-line method definitions.  If they are
9976             // in class definitions, then an error will be generated when
9977             // processing the class bodies.
9978             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9979           }
9980         }
9981       }
9982       continue;
9983     }
9984 
9985     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9986     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9987       MD->setLazyBody(PB->second);
9988   }
9989   PendingBodies.clear();
9990 
9991   // Do some cleanup.
9992   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9993     getContext().deduplicateMergedDefinitonsFor(ND);
9994   PendingMergedDefinitionsToDeduplicate.clear();
9995 }
9996 
9997 void ASTReader::diagnoseOdrViolations() {
9998   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9999       PendingFunctionOdrMergeFailures.empty() &&
10000       PendingEnumOdrMergeFailures.empty())
10001     return;
10002 
10003   // Trigger the import of the full definition of each class that had any
10004   // odr-merging problems, so we can produce better diagnostics for them.
10005   // These updates may in turn find and diagnose some ODR failures, so take
10006   // ownership of the set first.
10007   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10008   PendingOdrMergeFailures.clear();
10009   for (auto &Merge : OdrMergeFailures) {
10010     Merge.first->buildLookup();
10011     Merge.first->decls_begin();
10012     Merge.first->bases_begin();
10013     Merge.first->vbases_begin();
10014     for (auto &RecordPair : Merge.second) {
10015       auto *RD = RecordPair.first;
10016       RD->decls_begin();
10017       RD->bases_begin();
10018       RD->vbases_begin();
10019     }
10020   }
10021 
10022   // Trigger the import of functions.
10023   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10024   PendingFunctionOdrMergeFailures.clear();
10025   for (auto &Merge : FunctionOdrMergeFailures) {
10026     Merge.first->buildLookup();
10027     Merge.first->decls_begin();
10028     Merge.first->getBody();
10029     for (auto &FD : Merge.second) {
10030       FD->buildLookup();
10031       FD->decls_begin();
10032       FD->getBody();
10033     }
10034   }
10035 
10036   // Trigger the import of enums.
10037   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10038   PendingEnumOdrMergeFailures.clear();
10039   for (auto &Merge : EnumOdrMergeFailures) {
10040     Merge.first->decls_begin();
10041     for (auto &Enum : Merge.second) {
10042       Enum->decls_begin();
10043     }
10044   }
10045 
10046   // For each declaration from a merged context, check that the canonical
10047   // definition of that context also contains a declaration of the same
10048   // entity.
10049   //
10050   // Caution: this loop does things that might invalidate iterators into
10051   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10052   while (!PendingOdrMergeChecks.empty()) {
10053     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10054 
10055     // FIXME: Skip over implicit declarations for now. This matters for things
10056     // like implicitly-declared special member functions. This isn't entirely
10057     // correct; we can end up with multiple unmerged declarations of the same
10058     // implicit entity.
10059     if (D->isImplicit())
10060       continue;
10061 
10062     DeclContext *CanonDef = D->getDeclContext();
10063 
10064     bool Found = false;
10065     const Decl *DCanon = D->getCanonicalDecl();
10066 
10067     for (auto RI : D->redecls()) {
10068       if (RI->getLexicalDeclContext() == CanonDef) {
10069         Found = true;
10070         break;
10071       }
10072     }
10073     if (Found)
10074       continue;
10075 
10076     // Quick check failed, time to do the slow thing. Note, we can't just
10077     // look up the name of D in CanonDef here, because the member that is
10078     // in CanonDef might not be found by name lookup (it might have been
10079     // replaced by a more recent declaration in the lookup table), and we
10080     // can't necessarily find it in the redeclaration chain because it might
10081     // be merely mergeable, not redeclarable.
10082     llvm::SmallVector<const NamedDecl*, 4> Candidates;
10083     for (auto *CanonMember : CanonDef->decls()) {
10084       if (CanonMember->getCanonicalDecl() == DCanon) {
10085         // This can happen if the declaration is merely mergeable and not
10086         // actually redeclarable (we looked for redeclarations earlier).
10087         //
10088         // FIXME: We should be able to detect this more efficiently, without
10089         // pulling in all of the members of CanonDef.
10090         Found = true;
10091         break;
10092       }
10093       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10094         if (ND->getDeclName() == D->getDeclName())
10095           Candidates.push_back(ND);
10096     }
10097 
10098     if (!Found) {
10099       // The AST doesn't like TagDecls becoming invalid after they've been
10100       // completed. We only really need to mark FieldDecls as invalid here.
10101       if (!isa<TagDecl>(D))
10102         D->setInvalidDecl();
10103 
10104       // Ensure we don't accidentally recursively enter deserialization while
10105       // we're producing our diagnostic.
10106       Deserializing RecursionGuard(this);
10107 
10108       std::string CanonDefModule =
10109           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10110       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10111         << D << getOwningModuleNameForDiagnostic(D)
10112         << CanonDef << CanonDefModule.empty() << CanonDefModule;
10113 
10114       if (Candidates.empty())
10115         Diag(cast<Decl>(CanonDef)->getLocation(),
10116              diag::note_module_odr_violation_no_possible_decls) << D;
10117       else {
10118         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10119           Diag(Candidates[I]->getLocation(),
10120                diag::note_module_odr_violation_possible_decl)
10121             << Candidates[I];
10122       }
10123 
10124       DiagnosedOdrMergeFailures.insert(CanonDef);
10125     }
10126   }
10127 
10128   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10129       EnumOdrMergeFailures.empty())
10130     return;
10131 
10132   // Ensure we don't accidentally recursively enter deserialization while
10133   // we're producing our diagnostics.
10134   Deserializing RecursionGuard(this);
10135 
10136   // Common code for hashing helpers.
10137   ODRHash Hash;
10138   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10139     Hash.clear();
10140     Hash.AddQualType(Ty);
10141     return Hash.CalculateHash();
10142   };
10143 
10144   auto ComputeODRHash = [&Hash](const Stmt *S) {
10145     assert(S);
10146     Hash.clear();
10147     Hash.AddStmt(S);
10148     return Hash.CalculateHash();
10149   };
10150 
10151   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10152     assert(D);
10153     Hash.clear();
10154     Hash.AddSubDecl(D);
10155     return Hash.CalculateHash();
10156   };
10157 
10158   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10159     Hash.clear();
10160     Hash.AddTemplateArgument(TA);
10161     return Hash.CalculateHash();
10162   };
10163 
10164   auto ComputeTemplateParameterListODRHash =
10165       [&Hash](const TemplateParameterList *TPL) {
10166         assert(TPL);
10167         Hash.clear();
10168         Hash.AddTemplateParameterList(TPL);
10169         return Hash.CalculateHash();
10170       };
10171 
10172   // Issue any pending ODR-failure diagnostics.
10173   for (auto &Merge : OdrMergeFailures) {
10174     // If we've already pointed out a specific problem with this class, don't
10175     // bother issuing a general "something's different" diagnostic.
10176     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10177       continue;
10178 
10179     bool Diagnosed = false;
10180     CXXRecordDecl *FirstRecord = Merge.first;
10181     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10182     for (auto &RecordPair : Merge.second) {
10183       CXXRecordDecl *SecondRecord = RecordPair.first;
10184       // Multiple different declarations got merged together; tell the user
10185       // where they came from.
10186       if (FirstRecord == SecondRecord)
10187         continue;
10188 
10189       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10190 
10191       auto *FirstDD = FirstRecord->DefinitionData;
10192       auto *SecondDD = RecordPair.second;
10193 
10194       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10195 
10196       // Diagnostics from DefinitionData are emitted here.
10197       if (FirstDD != SecondDD) {
10198         enum ODRDefinitionDataDifference {
10199           NumBases,
10200           NumVBases,
10201           BaseType,
10202           BaseVirtual,
10203           BaseAccess,
10204         };
10205         auto ODRDiagError = [FirstRecord, &FirstModule,
10206                              this](SourceLocation Loc, SourceRange Range,
10207                                    ODRDefinitionDataDifference DiffType) {
10208           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10209                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10210                  << DiffType;
10211         };
10212         auto ODRDiagNote = [&SecondModule,
10213                             this](SourceLocation Loc, SourceRange Range,
10214                                   ODRDefinitionDataDifference DiffType) {
10215           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10216                  << SecondModule << Range << DiffType;
10217         };
10218 
10219         unsigned FirstNumBases = FirstDD->NumBases;
10220         unsigned FirstNumVBases = FirstDD->NumVBases;
10221         unsigned SecondNumBases = SecondDD->NumBases;
10222         unsigned SecondNumVBases = SecondDD->NumVBases;
10223 
10224         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10225           unsigned NumBases = DD->NumBases;
10226           if (NumBases == 0) return SourceRange();
10227           auto bases = DD->bases();
10228           return SourceRange(bases[0].getBeginLoc(),
10229                              bases[NumBases - 1].getEndLoc());
10230         };
10231 
10232         if (FirstNumBases != SecondNumBases) {
10233           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10234                        NumBases)
10235               << FirstNumBases;
10236           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10237                       NumBases)
10238               << SecondNumBases;
10239           Diagnosed = true;
10240           break;
10241         }
10242 
10243         if (FirstNumVBases != SecondNumVBases) {
10244           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10245                        NumVBases)
10246               << FirstNumVBases;
10247           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10248                       NumVBases)
10249               << SecondNumVBases;
10250           Diagnosed = true;
10251           break;
10252         }
10253 
10254         auto FirstBases = FirstDD->bases();
10255         auto SecondBases = SecondDD->bases();
10256         unsigned i = 0;
10257         for (i = 0; i < FirstNumBases; ++i) {
10258           auto FirstBase = FirstBases[i];
10259           auto SecondBase = SecondBases[i];
10260           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10261               ComputeQualTypeODRHash(SecondBase.getType())) {
10262             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10263                          BaseType)
10264                 << (i + 1) << FirstBase.getType();
10265             ODRDiagNote(SecondRecord->getLocation(),
10266                         SecondBase.getSourceRange(), BaseType)
10267                 << (i + 1) << SecondBase.getType();
10268             break;
10269           }
10270 
10271           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10272             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10273                          BaseVirtual)
10274                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10275             ODRDiagNote(SecondRecord->getLocation(),
10276                         SecondBase.getSourceRange(), BaseVirtual)
10277                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10278             break;
10279           }
10280 
10281           if (FirstBase.getAccessSpecifierAsWritten() !=
10282               SecondBase.getAccessSpecifierAsWritten()) {
10283             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10284                          BaseAccess)
10285                 << (i + 1) << FirstBase.getType()
10286                 << (int)FirstBase.getAccessSpecifierAsWritten();
10287             ODRDiagNote(SecondRecord->getLocation(),
10288                         SecondBase.getSourceRange(), BaseAccess)
10289                 << (i + 1) << SecondBase.getType()
10290                 << (int)SecondBase.getAccessSpecifierAsWritten();
10291             break;
10292           }
10293         }
10294 
10295         if (i != FirstNumBases) {
10296           Diagnosed = true;
10297           break;
10298         }
10299       }
10300 
10301       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10302 
10303       const ClassTemplateDecl *FirstTemplate =
10304           FirstRecord->getDescribedClassTemplate();
10305       const ClassTemplateDecl *SecondTemplate =
10306           SecondRecord->getDescribedClassTemplate();
10307 
10308       assert(!FirstTemplate == !SecondTemplate &&
10309              "Both pointers should be null or non-null");
10310 
10311       enum ODRTemplateDifference {
10312         ParamEmptyName,
10313         ParamName,
10314         ParamSingleDefaultArgument,
10315         ParamDifferentDefaultArgument,
10316       };
10317 
10318       if (FirstTemplate && SecondTemplate) {
10319         DeclHashes FirstTemplateHashes;
10320         DeclHashes SecondTemplateHashes;
10321 
10322         auto PopulateTemplateParameterHashs =
10323             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10324                                      const ClassTemplateDecl *TD) {
10325               for (auto *D : TD->getTemplateParameters()->asArray()) {
10326                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10327               }
10328             };
10329 
10330         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10331         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10332 
10333         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10334                "Number of template parameters should be equal.");
10335 
10336         auto FirstIt = FirstTemplateHashes.begin();
10337         auto FirstEnd = FirstTemplateHashes.end();
10338         auto SecondIt = SecondTemplateHashes.begin();
10339         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10340           if (FirstIt->second == SecondIt->second)
10341             continue;
10342 
10343           auto ODRDiagError = [FirstRecord, &FirstModule,
10344                                this](SourceLocation Loc, SourceRange Range,
10345                                      ODRTemplateDifference DiffType) {
10346             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10347                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10348                    << DiffType;
10349           };
10350           auto ODRDiagNote = [&SecondModule,
10351                               this](SourceLocation Loc, SourceRange Range,
10352                                     ODRTemplateDifference DiffType) {
10353             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10354                    << SecondModule << Range << DiffType;
10355           };
10356 
10357           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10358           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10359 
10360           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10361                  "Parameter Decl's should be the same kind.");
10362 
10363           DeclarationName FirstName = FirstDecl->getDeclName();
10364           DeclarationName SecondName = SecondDecl->getDeclName();
10365 
10366           if (FirstName != SecondName) {
10367             const bool FirstNameEmpty =
10368                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10369             const bool SecondNameEmpty =
10370                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10371             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10372                    "Both template parameters cannot be unnamed.");
10373             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10374                          FirstNameEmpty ? ParamEmptyName : ParamName)
10375                 << FirstName;
10376             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10377                         SecondNameEmpty ? ParamEmptyName : ParamName)
10378                 << SecondName;
10379             break;
10380           }
10381 
10382           switch (FirstDecl->getKind()) {
10383           default:
10384             llvm_unreachable("Invalid template parameter type.");
10385           case Decl::TemplateTypeParm: {
10386             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10387             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10388             const bool HasFirstDefaultArgument =
10389                 FirstParam->hasDefaultArgument() &&
10390                 !FirstParam->defaultArgumentWasInherited();
10391             const bool HasSecondDefaultArgument =
10392                 SecondParam->hasDefaultArgument() &&
10393                 !SecondParam->defaultArgumentWasInherited();
10394 
10395             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10396               ODRDiagError(FirstDecl->getLocation(),
10397                            FirstDecl->getSourceRange(),
10398                            ParamSingleDefaultArgument)
10399                   << HasFirstDefaultArgument;
10400               ODRDiagNote(SecondDecl->getLocation(),
10401                           SecondDecl->getSourceRange(),
10402                           ParamSingleDefaultArgument)
10403                   << HasSecondDefaultArgument;
10404               break;
10405             }
10406 
10407             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10408                    "Expecting default arguments.");
10409 
10410             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10411                          ParamDifferentDefaultArgument);
10412             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10413                         ParamDifferentDefaultArgument);
10414 
10415             break;
10416           }
10417           case Decl::NonTypeTemplateParm: {
10418             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10419             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10420             const bool HasFirstDefaultArgument =
10421                 FirstParam->hasDefaultArgument() &&
10422                 !FirstParam->defaultArgumentWasInherited();
10423             const bool HasSecondDefaultArgument =
10424                 SecondParam->hasDefaultArgument() &&
10425                 !SecondParam->defaultArgumentWasInherited();
10426 
10427             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10428               ODRDiagError(FirstDecl->getLocation(),
10429                            FirstDecl->getSourceRange(),
10430                            ParamSingleDefaultArgument)
10431                   << HasFirstDefaultArgument;
10432               ODRDiagNote(SecondDecl->getLocation(),
10433                           SecondDecl->getSourceRange(),
10434                           ParamSingleDefaultArgument)
10435                   << HasSecondDefaultArgument;
10436               break;
10437             }
10438 
10439             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10440                    "Expecting default arguments.");
10441 
10442             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10443                          ParamDifferentDefaultArgument);
10444             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10445                         ParamDifferentDefaultArgument);
10446 
10447             break;
10448           }
10449           case Decl::TemplateTemplateParm: {
10450             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10451             const auto *SecondParam =
10452                 cast<TemplateTemplateParmDecl>(SecondDecl);
10453             const bool HasFirstDefaultArgument =
10454                 FirstParam->hasDefaultArgument() &&
10455                 !FirstParam->defaultArgumentWasInherited();
10456             const bool HasSecondDefaultArgument =
10457                 SecondParam->hasDefaultArgument() &&
10458                 !SecondParam->defaultArgumentWasInherited();
10459 
10460             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10461               ODRDiagError(FirstDecl->getLocation(),
10462                            FirstDecl->getSourceRange(),
10463                            ParamSingleDefaultArgument)
10464                   << HasFirstDefaultArgument;
10465               ODRDiagNote(SecondDecl->getLocation(),
10466                           SecondDecl->getSourceRange(),
10467                           ParamSingleDefaultArgument)
10468                   << HasSecondDefaultArgument;
10469               break;
10470             }
10471 
10472             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10473                    "Expecting default arguments.");
10474 
10475             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10476                          ParamDifferentDefaultArgument);
10477             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10478                         ParamDifferentDefaultArgument);
10479 
10480             break;
10481           }
10482           }
10483 
10484           break;
10485         }
10486 
10487         if (FirstIt != FirstEnd) {
10488           Diagnosed = true;
10489           break;
10490         }
10491       }
10492 
10493       DeclHashes FirstHashes;
10494       DeclHashes SecondHashes;
10495 
10496       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10497                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
10498         for (auto *D : Record->decls()) {
10499           // Due to decl merging, the first CXXRecordDecl is the parent of
10500           // Decls in both records.
10501           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10502             continue;
10503           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10504         }
10505       };
10506       PopulateHashes(FirstHashes, FirstRecord);
10507       PopulateHashes(SecondHashes, SecondRecord);
10508 
10509       // Used with err_module_odr_violation_mismatch_decl and
10510       // note_module_odr_violation_mismatch_decl
10511       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10512       enum {
10513         EndOfClass,
10514         PublicSpecifer,
10515         PrivateSpecifer,
10516         ProtectedSpecifer,
10517         StaticAssert,
10518         Field,
10519         CXXMethod,
10520         TypeAlias,
10521         TypeDef,
10522         Var,
10523         Friend,
10524         FunctionTemplate,
10525         Other
10526       } FirstDiffType = Other,
10527         SecondDiffType = Other;
10528 
10529       auto DifferenceSelector = [](Decl *D) {
10530         assert(D && "valid Decl required");
10531         switch (D->getKind()) {
10532         default:
10533           return Other;
10534         case Decl::AccessSpec:
10535           switch (D->getAccess()) {
10536           case AS_public:
10537             return PublicSpecifer;
10538           case AS_private:
10539             return PrivateSpecifer;
10540           case AS_protected:
10541             return ProtectedSpecifer;
10542           case AS_none:
10543             break;
10544           }
10545           llvm_unreachable("Invalid access specifier");
10546         case Decl::StaticAssert:
10547           return StaticAssert;
10548         case Decl::Field:
10549           return Field;
10550         case Decl::CXXMethod:
10551         case Decl::CXXConstructor:
10552         case Decl::CXXDestructor:
10553           return CXXMethod;
10554         case Decl::TypeAlias:
10555           return TypeAlias;
10556         case Decl::Typedef:
10557           return TypeDef;
10558         case Decl::Var:
10559           return Var;
10560         case Decl::Friend:
10561           return Friend;
10562         case Decl::FunctionTemplate:
10563           return FunctionTemplate;
10564         }
10565       };
10566 
10567       Decl *FirstDecl = nullptr;
10568       Decl *SecondDecl = nullptr;
10569       auto FirstIt = FirstHashes.begin();
10570       auto SecondIt = SecondHashes.begin();
10571 
10572       // If there is a diagnoseable difference, FirstDiffType and
10573       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10574       // filled in if not EndOfClass.
10575       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10576         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10577             FirstIt->second == SecondIt->second) {
10578           ++FirstIt;
10579           ++SecondIt;
10580           continue;
10581         }
10582 
10583         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10584         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10585 
10586         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10587         SecondDiffType =
10588             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10589 
10590         break;
10591       }
10592 
10593       if (FirstDiffType == Other || SecondDiffType == Other) {
10594         // Reaching this point means an unexpected Decl was encountered
10595         // or no difference was detected.  This causes a generic error
10596         // message to be emitted.
10597         Diag(FirstRecord->getLocation(),
10598              diag::err_module_odr_violation_different_definitions)
10599             << FirstRecord << FirstModule.empty() << FirstModule;
10600 
10601         if (FirstDecl) {
10602           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10603               << FirstRecord << FirstDecl->getSourceRange();
10604         }
10605 
10606         Diag(SecondRecord->getLocation(),
10607              diag::note_module_odr_violation_different_definitions)
10608             << SecondModule;
10609 
10610         if (SecondDecl) {
10611           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10612               << SecondDecl->getSourceRange();
10613         }
10614 
10615         Diagnosed = true;
10616         break;
10617       }
10618 
10619       if (FirstDiffType != SecondDiffType) {
10620         SourceLocation FirstLoc;
10621         SourceRange FirstRange;
10622         if (FirstDiffType == EndOfClass) {
10623           FirstLoc = FirstRecord->getBraceRange().getEnd();
10624         } else {
10625           FirstLoc = FirstIt->first->getLocation();
10626           FirstRange = FirstIt->first->getSourceRange();
10627         }
10628         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10629             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10630             << FirstDiffType;
10631 
10632         SourceLocation SecondLoc;
10633         SourceRange SecondRange;
10634         if (SecondDiffType == EndOfClass) {
10635           SecondLoc = SecondRecord->getBraceRange().getEnd();
10636         } else {
10637           SecondLoc = SecondDecl->getLocation();
10638           SecondRange = SecondDecl->getSourceRange();
10639         }
10640         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10641             << SecondModule << SecondRange << SecondDiffType;
10642         Diagnosed = true;
10643         break;
10644       }
10645 
10646       assert(FirstDiffType == SecondDiffType);
10647 
10648       // Used with err_module_odr_violation_mismatch_decl_diff and
10649       // note_module_odr_violation_mismatch_decl_diff
10650       enum ODRDeclDifference {
10651         StaticAssertCondition,
10652         StaticAssertMessage,
10653         StaticAssertOnlyMessage,
10654         FieldName,
10655         FieldTypeName,
10656         FieldSingleBitField,
10657         FieldDifferentWidthBitField,
10658         FieldSingleMutable,
10659         FieldSingleInitializer,
10660         FieldDifferentInitializers,
10661         MethodName,
10662         MethodDeleted,
10663         MethodDefaulted,
10664         MethodVirtual,
10665         MethodStatic,
10666         MethodVolatile,
10667         MethodConst,
10668         MethodInline,
10669         MethodNumberParameters,
10670         MethodParameterType,
10671         MethodParameterName,
10672         MethodParameterSingleDefaultArgument,
10673         MethodParameterDifferentDefaultArgument,
10674         MethodNoTemplateArguments,
10675         MethodDifferentNumberTemplateArguments,
10676         MethodDifferentTemplateArgument,
10677         MethodSingleBody,
10678         MethodDifferentBody,
10679         TypedefName,
10680         TypedefType,
10681         VarName,
10682         VarType,
10683         VarSingleInitializer,
10684         VarDifferentInitializer,
10685         VarConstexpr,
10686         FriendTypeFunction,
10687         FriendType,
10688         FriendFunction,
10689         FunctionTemplateDifferentNumberParameters,
10690         FunctionTemplateParameterDifferentKind,
10691         FunctionTemplateParameterName,
10692         FunctionTemplateParameterSingleDefaultArgument,
10693         FunctionTemplateParameterDifferentDefaultArgument,
10694         FunctionTemplateParameterDifferentType,
10695         FunctionTemplatePackParameter,
10696       };
10697 
10698       // These lambdas have the common portions of the ODR diagnostics.  This
10699       // has the same return as Diag(), so addition parameters can be passed
10700       // in with operator<<
10701       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10702           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10703         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10704                << FirstRecord << FirstModule.empty() << FirstModule << Range
10705                << DiffType;
10706       };
10707       auto ODRDiagNote = [&SecondModule, this](
10708           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10709         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10710                << SecondModule << Range << DiffType;
10711       };
10712 
10713       switch (FirstDiffType) {
10714       case Other:
10715       case EndOfClass:
10716       case PublicSpecifer:
10717       case PrivateSpecifer:
10718       case ProtectedSpecifer:
10719         llvm_unreachable("Invalid diff type");
10720 
10721       case StaticAssert: {
10722         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10723         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10724 
10725         Expr *FirstExpr = FirstSA->getAssertExpr();
10726         Expr *SecondExpr = SecondSA->getAssertExpr();
10727         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10728         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10729         if (FirstODRHash != SecondODRHash) {
10730           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10731                        StaticAssertCondition);
10732           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10733                       StaticAssertCondition);
10734           Diagnosed = true;
10735           break;
10736         }
10737 
10738         StringLiteral *FirstStr = FirstSA->getMessage();
10739         StringLiteral *SecondStr = SecondSA->getMessage();
10740         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10741         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10742           SourceLocation FirstLoc, SecondLoc;
10743           SourceRange FirstRange, SecondRange;
10744           if (FirstStr) {
10745             FirstLoc = FirstStr->getBeginLoc();
10746             FirstRange = FirstStr->getSourceRange();
10747           } else {
10748             FirstLoc = FirstSA->getBeginLoc();
10749             FirstRange = FirstSA->getSourceRange();
10750           }
10751           if (SecondStr) {
10752             SecondLoc = SecondStr->getBeginLoc();
10753             SecondRange = SecondStr->getSourceRange();
10754           } else {
10755             SecondLoc = SecondSA->getBeginLoc();
10756             SecondRange = SecondSA->getSourceRange();
10757           }
10758           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10759               << (FirstStr == nullptr);
10760           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10761               << (SecondStr == nullptr);
10762           Diagnosed = true;
10763           break;
10764         }
10765 
10766         if (FirstStr && SecondStr &&
10767             FirstStr->getString() != SecondStr->getString()) {
10768           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10769                        StaticAssertMessage);
10770           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10771                       StaticAssertMessage);
10772           Diagnosed = true;
10773           break;
10774         }
10775         break;
10776       }
10777       case Field: {
10778         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10779         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10780         IdentifierInfo *FirstII = FirstField->getIdentifier();
10781         IdentifierInfo *SecondII = SecondField->getIdentifier();
10782         if (FirstII->getName() != SecondII->getName()) {
10783           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10784                        FieldName)
10785               << FirstII;
10786           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10787                       FieldName)
10788               << SecondII;
10789 
10790           Diagnosed = true;
10791           break;
10792         }
10793 
10794         assert(getContext().hasSameType(FirstField->getType(),
10795                                         SecondField->getType()));
10796 
10797         QualType FirstType = FirstField->getType();
10798         QualType SecondType = SecondField->getType();
10799         if (ComputeQualTypeODRHash(FirstType) !=
10800             ComputeQualTypeODRHash(SecondType)) {
10801           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10802                        FieldTypeName)
10803               << FirstII << FirstType;
10804           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10805                       FieldTypeName)
10806               << SecondII << SecondType;
10807 
10808           Diagnosed = true;
10809           break;
10810         }
10811 
10812         const bool IsFirstBitField = FirstField->isBitField();
10813         const bool IsSecondBitField = SecondField->isBitField();
10814         if (IsFirstBitField != IsSecondBitField) {
10815           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10816                        FieldSingleBitField)
10817               << FirstII << IsFirstBitField;
10818           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10819                       FieldSingleBitField)
10820               << SecondII << IsSecondBitField;
10821           Diagnosed = true;
10822           break;
10823         }
10824 
10825         if (IsFirstBitField && IsSecondBitField) {
10826           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10827                        FieldDifferentWidthBitField)
10828               << FirstII << FirstField->getBitWidth()->getSourceRange();
10829           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10830                       FieldDifferentWidthBitField)
10831               << SecondII << SecondField->getBitWidth()->getSourceRange();
10832           Diagnosed = true;
10833           break;
10834         }
10835 
10836         const bool IsFirstMutable = FirstField->isMutable();
10837         const bool IsSecondMutable = SecondField->isMutable();
10838         if (IsFirstMutable != IsSecondMutable) {
10839           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10840                        FieldSingleMutable)
10841               << FirstII << IsFirstMutable;
10842           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10843                       FieldSingleMutable)
10844               << SecondII << IsSecondMutable;
10845           Diagnosed = true;
10846           break;
10847         }
10848 
10849         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10850         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10851         if ((!FirstInitializer && SecondInitializer) ||
10852             (FirstInitializer && !SecondInitializer)) {
10853           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10854                        FieldSingleInitializer)
10855               << FirstII << (FirstInitializer != nullptr);
10856           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10857                       FieldSingleInitializer)
10858               << SecondII << (SecondInitializer != nullptr);
10859           Diagnosed = true;
10860           break;
10861         }
10862 
10863         if (FirstInitializer && SecondInitializer) {
10864           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10865           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10866           if (FirstInitHash != SecondInitHash) {
10867             ODRDiagError(FirstField->getLocation(),
10868                          FirstField->getSourceRange(),
10869                          FieldDifferentInitializers)
10870                 << FirstII << FirstInitializer->getSourceRange();
10871             ODRDiagNote(SecondField->getLocation(),
10872                         SecondField->getSourceRange(),
10873                         FieldDifferentInitializers)
10874                 << SecondII << SecondInitializer->getSourceRange();
10875             Diagnosed = true;
10876             break;
10877           }
10878         }
10879 
10880         break;
10881       }
10882       case CXXMethod: {
10883         enum {
10884           DiagMethod,
10885           DiagConstructor,
10886           DiagDestructor,
10887         } FirstMethodType,
10888             SecondMethodType;
10889         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10890           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10891           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10892           return DiagMethod;
10893         };
10894         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10895         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10896         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10897         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10898         auto FirstName = FirstMethod->getDeclName();
10899         auto SecondName = SecondMethod->getDeclName();
10900         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10901           ODRDiagError(FirstMethod->getLocation(),
10902                        FirstMethod->getSourceRange(), MethodName)
10903               << FirstMethodType << FirstName;
10904           ODRDiagNote(SecondMethod->getLocation(),
10905                       SecondMethod->getSourceRange(), MethodName)
10906               << SecondMethodType << SecondName;
10907 
10908           Diagnosed = true;
10909           break;
10910         }
10911 
10912         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10913         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10914         if (FirstDeleted != SecondDeleted) {
10915           ODRDiagError(FirstMethod->getLocation(),
10916                        FirstMethod->getSourceRange(), MethodDeleted)
10917               << FirstMethodType << FirstName << FirstDeleted;
10918 
10919           ODRDiagNote(SecondMethod->getLocation(),
10920                       SecondMethod->getSourceRange(), MethodDeleted)
10921               << SecondMethodType << SecondName << SecondDeleted;
10922           Diagnosed = true;
10923           break;
10924         }
10925 
10926         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10927         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10928         if (FirstDefaulted != SecondDefaulted) {
10929           ODRDiagError(FirstMethod->getLocation(),
10930                        FirstMethod->getSourceRange(), MethodDefaulted)
10931               << FirstMethodType << FirstName << FirstDefaulted;
10932 
10933           ODRDiagNote(SecondMethod->getLocation(),
10934                       SecondMethod->getSourceRange(), MethodDefaulted)
10935               << SecondMethodType << SecondName << SecondDefaulted;
10936           Diagnosed = true;
10937           break;
10938         }
10939 
10940         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10941         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10942         const bool FirstPure = FirstMethod->isPure();
10943         const bool SecondPure = SecondMethod->isPure();
10944         if ((FirstVirtual || SecondVirtual) &&
10945             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10946           ODRDiagError(FirstMethod->getLocation(),
10947                        FirstMethod->getSourceRange(), MethodVirtual)
10948               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10949           ODRDiagNote(SecondMethod->getLocation(),
10950                       SecondMethod->getSourceRange(), MethodVirtual)
10951               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10952           Diagnosed = true;
10953           break;
10954         }
10955 
10956         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10957         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10958         // class needs to be checked instead.
10959         const auto FirstStorage = FirstMethod->getStorageClass();
10960         const auto SecondStorage = SecondMethod->getStorageClass();
10961         const bool FirstStatic = FirstStorage == SC_Static;
10962         const bool SecondStatic = SecondStorage == SC_Static;
10963         if (FirstStatic != SecondStatic) {
10964           ODRDiagError(FirstMethod->getLocation(),
10965                        FirstMethod->getSourceRange(), MethodStatic)
10966               << FirstMethodType << FirstName << FirstStatic;
10967           ODRDiagNote(SecondMethod->getLocation(),
10968                       SecondMethod->getSourceRange(), MethodStatic)
10969               << SecondMethodType << SecondName << SecondStatic;
10970           Diagnosed = true;
10971           break;
10972         }
10973 
10974         const bool FirstVolatile = FirstMethod->isVolatile();
10975         const bool SecondVolatile = SecondMethod->isVolatile();
10976         if (FirstVolatile != SecondVolatile) {
10977           ODRDiagError(FirstMethod->getLocation(),
10978                        FirstMethod->getSourceRange(), MethodVolatile)
10979               << FirstMethodType << FirstName << FirstVolatile;
10980           ODRDiagNote(SecondMethod->getLocation(),
10981                       SecondMethod->getSourceRange(), MethodVolatile)
10982               << SecondMethodType << SecondName << SecondVolatile;
10983           Diagnosed = true;
10984           break;
10985         }
10986 
10987         const bool FirstConst = FirstMethod->isConst();
10988         const bool SecondConst = SecondMethod->isConst();
10989         if (FirstConst != SecondConst) {
10990           ODRDiagError(FirstMethod->getLocation(),
10991                        FirstMethod->getSourceRange(), MethodConst)
10992               << FirstMethodType << FirstName << FirstConst;
10993           ODRDiagNote(SecondMethod->getLocation(),
10994                       SecondMethod->getSourceRange(), MethodConst)
10995               << SecondMethodType << SecondName << SecondConst;
10996           Diagnosed = true;
10997           break;
10998         }
10999 
11000         const bool FirstInline = FirstMethod->isInlineSpecified();
11001         const bool SecondInline = SecondMethod->isInlineSpecified();
11002         if (FirstInline != SecondInline) {
11003           ODRDiagError(FirstMethod->getLocation(),
11004                        FirstMethod->getSourceRange(), MethodInline)
11005               << FirstMethodType << FirstName << FirstInline;
11006           ODRDiagNote(SecondMethod->getLocation(),
11007                       SecondMethod->getSourceRange(), MethodInline)
11008               << SecondMethodType << SecondName << SecondInline;
11009           Diagnosed = true;
11010           break;
11011         }
11012 
11013         const unsigned FirstNumParameters = FirstMethod->param_size();
11014         const unsigned SecondNumParameters = SecondMethod->param_size();
11015         if (FirstNumParameters != SecondNumParameters) {
11016           ODRDiagError(FirstMethod->getLocation(),
11017                        FirstMethod->getSourceRange(), MethodNumberParameters)
11018               << FirstMethodType << FirstName << FirstNumParameters;
11019           ODRDiagNote(SecondMethod->getLocation(),
11020                       SecondMethod->getSourceRange(), MethodNumberParameters)
11021               << SecondMethodType << SecondName << SecondNumParameters;
11022           Diagnosed = true;
11023           break;
11024         }
11025 
11026         // Need this status boolean to know when break out of the switch.
11027         bool ParameterMismatch = false;
11028         for (unsigned I = 0; I < FirstNumParameters; ++I) {
11029           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11030           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11031 
11032           QualType FirstParamType = FirstParam->getType();
11033           QualType SecondParamType = SecondParam->getType();
11034           if (FirstParamType != SecondParamType &&
11035               ComputeQualTypeODRHash(FirstParamType) !=
11036                   ComputeQualTypeODRHash(SecondParamType)) {
11037             if (const DecayedType *ParamDecayedType =
11038                     FirstParamType->getAs<DecayedType>()) {
11039               ODRDiagError(FirstMethod->getLocation(),
11040                            FirstMethod->getSourceRange(), MethodParameterType)
11041                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11042                   << true << ParamDecayedType->getOriginalType();
11043             } else {
11044               ODRDiagError(FirstMethod->getLocation(),
11045                            FirstMethod->getSourceRange(), MethodParameterType)
11046                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11047                   << false;
11048             }
11049 
11050             if (const DecayedType *ParamDecayedType =
11051                     SecondParamType->getAs<DecayedType>()) {
11052               ODRDiagNote(SecondMethod->getLocation(),
11053                           SecondMethod->getSourceRange(), MethodParameterType)
11054                   << SecondMethodType << SecondName << (I + 1)
11055                   << SecondParamType << true
11056                   << ParamDecayedType->getOriginalType();
11057             } else {
11058               ODRDiagNote(SecondMethod->getLocation(),
11059                           SecondMethod->getSourceRange(), MethodParameterType)
11060                   << SecondMethodType << SecondName << (I + 1)
11061                   << SecondParamType << false;
11062             }
11063             ParameterMismatch = true;
11064             break;
11065           }
11066 
11067           DeclarationName FirstParamName = FirstParam->getDeclName();
11068           DeclarationName SecondParamName = SecondParam->getDeclName();
11069           if (FirstParamName != SecondParamName) {
11070             ODRDiagError(FirstMethod->getLocation(),
11071                          FirstMethod->getSourceRange(), MethodParameterName)
11072                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11073             ODRDiagNote(SecondMethod->getLocation(),
11074                         SecondMethod->getSourceRange(), MethodParameterName)
11075                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11076             ParameterMismatch = true;
11077             break;
11078           }
11079 
11080           const Expr *FirstInit = FirstParam->getInit();
11081           const Expr *SecondInit = SecondParam->getInit();
11082           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11083             ODRDiagError(FirstMethod->getLocation(),
11084                          FirstMethod->getSourceRange(),
11085                          MethodParameterSingleDefaultArgument)
11086                 << FirstMethodType << FirstName << (I + 1)
11087                 << (FirstInit == nullptr)
11088                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11089             ODRDiagNote(SecondMethod->getLocation(),
11090                         SecondMethod->getSourceRange(),
11091                         MethodParameterSingleDefaultArgument)
11092                 << SecondMethodType << SecondName << (I + 1)
11093                 << (SecondInit == nullptr)
11094                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11095             ParameterMismatch = true;
11096             break;
11097           }
11098 
11099           if (FirstInit && SecondInit &&
11100               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11101             ODRDiagError(FirstMethod->getLocation(),
11102                          FirstMethod->getSourceRange(),
11103                          MethodParameterDifferentDefaultArgument)
11104                 << FirstMethodType << FirstName << (I + 1)
11105                 << FirstInit->getSourceRange();
11106             ODRDiagNote(SecondMethod->getLocation(),
11107                         SecondMethod->getSourceRange(),
11108                         MethodParameterDifferentDefaultArgument)
11109                 << SecondMethodType << SecondName << (I + 1)
11110                 << SecondInit->getSourceRange();
11111             ParameterMismatch = true;
11112             break;
11113 
11114           }
11115         }
11116 
11117         if (ParameterMismatch) {
11118           Diagnosed = true;
11119           break;
11120         }
11121 
11122         const auto *FirstTemplateArgs =
11123             FirstMethod->getTemplateSpecializationArgs();
11124         const auto *SecondTemplateArgs =
11125             SecondMethod->getTemplateSpecializationArgs();
11126 
11127         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11128             (!FirstTemplateArgs && SecondTemplateArgs)) {
11129           ODRDiagError(FirstMethod->getLocation(),
11130                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11131               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11132           ODRDiagNote(SecondMethod->getLocation(),
11133                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11134               << SecondMethodType << SecondName
11135               << (SecondTemplateArgs != nullptr);
11136 
11137           Diagnosed = true;
11138           break;
11139         }
11140 
11141         if (FirstTemplateArgs && SecondTemplateArgs) {
11142           // Remove pack expansions from argument list.
11143           auto ExpandTemplateArgumentList =
11144               [](const TemplateArgumentList *TAL) {
11145                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11146                 for (const TemplateArgument &TA : TAL->asArray()) {
11147                   if (TA.getKind() != TemplateArgument::Pack) {
11148                     ExpandedList.push_back(&TA);
11149                     continue;
11150                   }
11151                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11152                     ExpandedList.push_back(&PackTA);
11153                   }
11154                 }
11155                 return ExpandedList;
11156               };
11157           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11158               ExpandTemplateArgumentList(FirstTemplateArgs);
11159           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11160               ExpandTemplateArgumentList(SecondTemplateArgs);
11161 
11162           if (FirstExpandedList.size() != SecondExpandedList.size()) {
11163             ODRDiagError(FirstMethod->getLocation(),
11164                          FirstMethod->getSourceRange(),
11165                          MethodDifferentNumberTemplateArguments)
11166                 << FirstMethodType << FirstName
11167                 << (unsigned)FirstExpandedList.size();
11168             ODRDiagNote(SecondMethod->getLocation(),
11169                         SecondMethod->getSourceRange(),
11170                         MethodDifferentNumberTemplateArguments)
11171                 << SecondMethodType << SecondName
11172                 << (unsigned)SecondExpandedList.size();
11173 
11174             Diagnosed = true;
11175             break;
11176           }
11177 
11178           bool TemplateArgumentMismatch = false;
11179           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11180             const TemplateArgument &FirstTA = *FirstExpandedList[i],
11181                                    &SecondTA = *SecondExpandedList[i];
11182             if (ComputeTemplateArgumentODRHash(FirstTA) ==
11183                 ComputeTemplateArgumentODRHash(SecondTA)) {
11184               continue;
11185             }
11186 
11187             ODRDiagError(FirstMethod->getLocation(),
11188                          FirstMethod->getSourceRange(),
11189                          MethodDifferentTemplateArgument)
11190                 << FirstMethodType << FirstName << FirstTA << i + 1;
11191             ODRDiagNote(SecondMethod->getLocation(),
11192                         SecondMethod->getSourceRange(),
11193                         MethodDifferentTemplateArgument)
11194                 << SecondMethodType << SecondName << SecondTA << i + 1;
11195 
11196             TemplateArgumentMismatch = true;
11197             break;
11198           }
11199 
11200           if (TemplateArgumentMismatch) {
11201             Diagnosed = true;
11202             break;
11203           }
11204         }
11205 
11206         // Compute the hash of the method as if it has no body.
11207         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11208           Hash.clear();
11209           Hash.AddFunctionDecl(D, true /*SkipBody*/);
11210           return Hash.CalculateHash();
11211         };
11212 
11213         // Compare the hash generated to the hash stored.  A difference means
11214         // that a body was present in the original source.  Due to merging,
11215         // the stardard way of detecting a body will not work.
11216         const bool HasFirstBody =
11217             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11218         const bool HasSecondBody =
11219             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11220 
11221         if (HasFirstBody != HasSecondBody) {
11222           ODRDiagError(FirstMethod->getLocation(),
11223                        FirstMethod->getSourceRange(), MethodSingleBody)
11224               << FirstMethodType << FirstName << HasFirstBody;
11225           ODRDiagNote(SecondMethod->getLocation(),
11226                       SecondMethod->getSourceRange(), MethodSingleBody)
11227               << SecondMethodType << SecondName << HasSecondBody;
11228           Diagnosed = true;
11229           break;
11230         }
11231 
11232         if (HasFirstBody && HasSecondBody) {
11233           ODRDiagError(FirstMethod->getLocation(),
11234                        FirstMethod->getSourceRange(), MethodDifferentBody)
11235               << FirstMethodType << FirstName;
11236           ODRDiagNote(SecondMethod->getLocation(),
11237                       SecondMethod->getSourceRange(), MethodDifferentBody)
11238               << SecondMethodType << SecondName;
11239           Diagnosed = true;
11240           break;
11241         }
11242 
11243         break;
11244       }
11245       case TypeAlias:
11246       case TypeDef: {
11247         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11248         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11249         auto FirstName = FirstTD->getDeclName();
11250         auto SecondName = SecondTD->getDeclName();
11251         if (FirstName != SecondName) {
11252           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11253                        TypedefName)
11254               << (FirstDiffType == TypeAlias) << FirstName;
11255           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11256                       TypedefName)
11257               << (FirstDiffType == TypeAlias) << SecondName;
11258           Diagnosed = true;
11259           break;
11260         }
11261 
11262         QualType FirstType = FirstTD->getUnderlyingType();
11263         QualType SecondType = SecondTD->getUnderlyingType();
11264         if (ComputeQualTypeODRHash(FirstType) !=
11265             ComputeQualTypeODRHash(SecondType)) {
11266           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11267                        TypedefType)
11268               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11269           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11270                       TypedefType)
11271               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11272           Diagnosed = true;
11273           break;
11274         }
11275         break;
11276       }
11277       case Var: {
11278         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11279         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11280         auto FirstName = FirstVD->getDeclName();
11281         auto SecondName = SecondVD->getDeclName();
11282         if (FirstName != SecondName) {
11283           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11284                        VarName)
11285               << FirstName;
11286           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11287                       VarName)
11288               << SecondName;
11289           Diagnosed = true;
11290           break;
11291         }
11292 
11293         QualType FirstType = FirstVD->getType();
11294         QualType SecondType = SecondVD->getType();
11295         if (ComputeQualTypeODRHash(FirstType) !=
11296                         ComputeQualTypeODRHash(SecondType)) {
11297           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11298                        VarType)
11299               << FirstName << FirstType;
11300           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11301                       VarType)
11302               << SecondName << SecondType;
11303           Diagnosed = true;
11304           break;
11305         }
11306 
11307         const Expr *FirstInit = FirstVD->getInit();
11308         const Expr *SecondInit = SecondVD->getInit();
11309         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11310           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11311                        VarSingleInitializer)
11312               << FirstName << (FirstInit == nullptr)
11313               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11314           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11315                       VarSingleInitializer)
11316               << SecondName << (SecondInit == nullptr)
11317               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11318           Diagnosed = true;
11319           break;
11320         }
11321 
11322         if (FirstInit && SecondInit &&
11323             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11324           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11325                        VarDifferentInitializer)
11326               << FirstName << FirstInit->getSourceRange();
11327           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11328                       VarDifferentInitializer)
11329               << SecondName << SecondInit->getSourceRange();
11330           Diagnosed = true;
11331           break;
11332         }
11333 
11334         const bool FirstIsConstexpr = FirstVD->isConstexpr();
11335         const bool SecondIsConstexpr = SecondVD->isConstexpr();
11336         if (FirstIsConstexpr != SecondIsConstexpr) {
11337           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11338                        VarConstexpr)
11339               << FirstName << FirstIsConstexpr;
11340           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11341                       VarConstexpr)
11342               << SecondName << SecondIsConstexpr;
11343           Diagnosed = true;
11344           break;
11345         }
11346         break;
11347       }
11348       case Friend: {
11349         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11350         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11351 
11352         NamedDecl *FirstND = FirstFriend->getFriendDecl();
11353         NamedDecl *SecondND = SecondFriend->getFriendDecl();
11354 
11355         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11356         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11357 
11358         if (FirstND && SecondND) {
11359           ODRDiagError(FirstFriend->getFriendLoc(),
11360                        FirstFriend->getSourceRange(), FriendFunction)
11361               << FirstND;
11362           ODRDiagNote(SecondFriend->getFriendLoc(),
11363                       SecondFriend->getSourceRange(), FriendFunction)
11364               << SecondND;
11365 
11366           Diagnosed = true;
11367           break;
11368         }
11369 
11370         if (FirstTSI && SecondTSI) {
11371           QualType FirstFriendType = FirstTSI->getType();
11372           QualType SecondFriendType = SecondTSI->getType();
11373           assert(ComputeQualTypeODRHash(FirstFriendType) !=
11374                  ComputeQualTypeODRHash(SecondFriendType));
11375           ODRDiagError(FirstFriend->getFriendLoc(),
11376                        FirstFriend->getSourceRange(), FriendType)
11377               << FirstFriendType;
11378           ODRDiagNote(SecondFriend->getFriendLoc(),
11379                       SecondFriend->getSourceRange(), FriendType)
11380               << SecondFriendType;
11381           Diagnosed = true;
11382           break;
11383         }
11384 
11385         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11386                      FriendTypeFunction)
11387             << (FirstTSI == nullptr);
11388         ODRDiagNote(SecondFriend->getFriendLoc(),
11389                     SecondFriend->getSourceRange(), FriendTypeFunction)
11390             << (SecondTSI == nullptr);
11391 
11392         Diagnosed = true;
11393         break;
11394       }
11395       case FunctionTemplate: {
11396         FunctionTemplateDecl *FirstTemplate =
11397             cast<FunctionTemplateDecl>(FirstDecl);
11398         FunctionTemplateDecl *SecondTemplate =
11399             cast<FunctionTemplateDecl>(SecondDecl);
11400 
11401         TemplateParameterList *FirstTPL =
11402             FirstTemplate->getTemplateParameters();
11403         TemplateParameterList *SecondTPL =
11404             SecondTemplate->getTemplateParameters();
11405 
11406         if (FirstTPL->size() != SecondTPL->size()) {
11407           ODRDiagError(FirstTemplate->getLocation(),
11408                        FirstTemplate->getSourceRange(),
11409                        FunctionTemplateDifferentNumberParameters)
11410               << FirstTemplate << FirstTPL->size();
11411           ODRDiagNote(SecondTemplate->getLocation(),
11412                       SecondTemplate->getSourceRange(),
11413                       FunctionTemplateDifferentNumberParameters)
11414               << SecondTemplate  << SecondTPL->size();
11415 
11416           Diagnosed = true;
11417           break;
11418         }
11419 
11420         bool ParameterMismatch = false;
11421         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11422           NamedDecl *FirstParam = FirstTPL->getParam(i);
11423           NamedDecl *SecondParam = SecondTPL->getParam(i);
11424 
11425           if (FirstParam->getKind() != SecondParam->getKind()) {
11426             enum {
11427               TemplateTypeParameter,
11428               NonTypeTemplateParameter,
11429               TemplateTemplateParameter,
11430             };
11431             auto GetParamType = [](NamedDecl *D) {
11432               switch (D->getKind()) {
11433                 default:
11434                   llvm_unreachable("Unexpected template parameter type");
11435                 case Decl::TemplateTypeParm:
11436                   return TemplateTypeParameter;
11437                 case Decl::NonTypeTemplateParm:
11438                   return NonTypeTemplateParameter;
11439                 case Decl::TemplateTemplateParm:
11440                   return TemplateTemplateParameter;
11441               }
11442             };
11443 
11444             ODRDiagError(FirstTemplate->getLocation(),
11445                          FirstTemplate->getSourceRange(),
11446                          FunctionTemplateParameterDifferentKind)
11447                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11448             ODRDiagNote(SecondTemplate->getLocation(),
11449                         SecondTemplate->getSourceRange(),
11450                         FunctionTemplateParameterDifferentKind)
11451                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11452 
11453             ParameterMismatch = true;
11454             break;
11455           }
11456 
11457           if (FirstParam->getName() != SecondParam->getName()) {
11458             ODRDiagError(FirstTemplate->getLocation(),
11459                          FirstTemplate->getSourceRange(),
11460                          FunctionTemplateParameterName)
11461                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11462                 << FirstParam;
11463             ODRDiagNote(SecondTemplate->getLocation(),
11464                         SecondTemplate->getSourceRange(),
11465                         FunctionTemplateParameterName)
11466                 << SecondTemplate << (i + 1)
11467                 << (bool)SecondParam->getIdentifier() << SecondParam;
11468             ParameterMismatch = true;
11469             break;
11470           }
11471 
11472           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11473               isa<TemplateTypeParmDecl>(SecondParam)) {
11474             TemplateTypeParmDecl *FirstTTPD =
11475                 cast<TemplateTypeParmDecl>(FirstParam);
11476             TemplateTypeParmDecl *SecondTTPD =
11477                 cast<TemplateTypeParmDecl>(SecondParam);
11478             bool HasFirstDefaultArgument =
11479                 FirstTTPD->hasDefaultArgument() &&
11480                 !FirstTTPD->defaultArgumentWasInherited();
11481             bool HasSecondDefaultArgument =
11482                 SecondTTPD->hasDefaultArgument() &&
11483                 !SecondTTPD->defaultArgumentWasInherited();
11484             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11485               ODRDiagError(FirstTemplate->getLocation(),
11486                            FirstTemplate->getSourceRange(),
11487                            FunctionTemplateParameterSingleDefaultArgument)
11488                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11489               ODRDiagNote(SecondTemplate->getLocation(),
11490                           SecondTemplate->getSourceRange(),
11491                           FunctionTemplateParameterSingleDefaultArgument)
11492                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11493               ParameterMismatch = true;
11494               break;
11495             }
11496 
11497             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11498               QualType FirstType = FirstTTPD->getDefaultArgument();
11499               QualType SecondType = SecondTTPD->getDefaultArgument();
11500               if (ComputeQualTypeODRHash(FirstType) !=
11501                   ComputeQualTypeODRHash(SecondType)) {
11502                 ODRDiagError(FirstTemplate->getLocation(),
11503                              FirstTemplate->getSourceRange(),
11504                              FunctionTemplateParameterDifferentDefaultArgument)
11505                     << FirstTemplate << (i + 1) << FirstType;
11506                 ODRDiagNote(SecondTemplate->getLocation(),
11507                             SecondTemplate->getSourceRange(),
11508                             FunctionTemplateParameterDifferentDefaultArgument)
11509                     << SecondTemplate << (i + 1) << SecondType;
11510                 ParameterMismatch = true;
11511                 break;
11512               }
11513             }
11514 
11515             if (FirstTTPD->isParameterPack() !=
11516                 SecondTTPD->isParameterPack()) {
11517               ODRDiagError(FirstTemplate->getLocation(),
11518                            FirstTemplate->getSourceRange(),
11519                            FunctionTemplatePackParameter)
11520                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11521               ODRDiagNote(SecondTemplate->getLocation(),
11522                           SecondTemplate->getSourceRange(),
11523                           FunctionTemplatePackParameter)
11524                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11525               ParameterMismatch = true;
11526               break;
11527             }
11528           }
11529 
11530           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11531               isa<TemplateTemplateParmDecl>(SecondParam)) {
11532             TemplateTemplateParmDecl *FirstTTPD =
11533                 cast<TemplateTemplateParmDecl>(FirstParam);
11534             TemplateTemplateParmDecl *SecondTTPD =
11535                 cast<TemplateTemplateParmDecl>(SecondParam);
11536 
11537             TemplateParameterList *FirstTPL =
11538                 FirstTTPD->getTemplateParameters();
11539             TemplateParameterList *SecondTPL =
11540                 SecondTTPD->getTemplateParameters();
11541 
11542             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11543                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11544               ODRDiagError(FirstTemplate->getLocation(),
11545                            FirstTemplate->getSourceRange(),
11546                            FunctionTemplateParameterDifferentType)
11547                   << FirstTemplate << (i + 1);
11548               ODRDiagNote(SecondTemplate->getLocation(),
11549                           SecondTemplate->getSourceRange(),
11550                           FunctionTemplateParameterDifferentType)
11551                   << SecondTemplate << (i + 1);
11552               ParameterMismatch = true;
11553               break;
11554             }
11555 
11556             bool HasFirstDefaultArgument =
11557                 FirstTTPD->hasDefaultArgument() &&
11558                 !FirstTTPD->defaultArgumentWasInherited();
11559             bool HasSecondDefaultArgument =
11560                 SecondTTPD->hasDefaultArgument() &&
11561                 !SecondTTPD->defaultArgumentWasInherited();
11562             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11563               ODRDiagError(FirstTemplate->getLocation(),
11564                            FirstTemplate->getSourceRange(),
11565                            FunctionTemplateParameterSingleDefaultArgument)
11566                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11567               ODRDiagNote(SecondTemplate->getLocation(),
11568                           SecondTemplate->getSourceRange(),
11569                           FunctionTemplateParameterSingleDefaultArgument)
11570                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11571               ParameterMismatch = true;
11572               break;
11573             }
11574 
11575             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11576               TemplateArgument FirstTA =
11577                   FirstTTPD->getDefaultArgument().getArgument();
11578               TemplateArgument SecondTA =
11579                   SecondTTPD->getDefaultArgument().getArgument();
11580               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11581                   ComputeTemplateArgumentODRHash(SecondTA)) {
11582                 ODRDiagError(FirstTemplate->getLocation(),
11583                              FirstTemplate->getSourceRange(),
11584                              FunctionTemplateParameterDifferentDefaultArgument)
11585                     << FirstTemplate << (i + 1) << FirstTA;
11586                 ODRDiagNote(SecondTemplate->getLocation(),
11587                             SecondTemplate->getSourceRange(),
11588                             FunctionTemplateParameterDifferentDefaultArgument)
11589                     << SecondTemplate << (i + 1) << SecondTA;
11590                 ParameterMismatch = true;
11591                 break;
11592               }
11593             }
11594 
11595             if (FirstTTPD->isParameterPack() !=
11596                 SecondTTPD->isParameterPack()) {
11597               ODRDiagError(FirstTemplate->getLocation(),
11598                            FirstTemplate->getSourceRange(),
11599                            FunctionTemplatePackParameter)
11600                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11601               ODRDiagNote(SecondTemplate->getLocation(),
11602                           SecondTemplate->getSourceRange(),
11603                           FunctionTemplatePackParameter)
11604                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11605               ParameterMismatch = true;
11606               break;
11607             }
11608           }
11609 
11610           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11611               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11612             NonTypeTemplateParmDecl *FirstNTTPD =
11613                 cast<NonTypeTemplateParmDecl>(FirstParam);
11614             NonTypeTemplateParmDecl *SecondNTTPD =
11615                 cast<NonTypeTemplateParmDecl>(SecondParam);
11616 
11617             QualType FirstType = FirstNTTPD->getType();
11618             QualType SecondType = SecondNTTPD->getType();
11619             if (ComputeQualTypeODRHash(FirstType) !=
11620                 ComputeQualTypeODRHash(SecondType)) {
11621               ODRDiagError(FirstTemplate->getLocation(),
11622                            FirstTemplate->getSourceRange(),
11623                            FunctionTemplateParameterDifferentType)
11624                   << FirstTemplate << (i + 1);
11625               ODRDiagNote(SecondTemplate->getLocation(),
11626                           SecondTemplate->getSourceRange(),
11627                           FunctionTemplateParameterDifferentType)
11628                   << SecondTemplate << (i + 1);
11629               ParameterMismatch = true;
11630               break;
11631             }
11632 
11633             bool HasFirstDefaultArgument =
11634                 FirstNTTPD->hasDefaultArgument() &&
11635                 !FirstNTTPD->defaultArgumentWasInherited();
11636             bool HasSecondDefaultArgument =
11637                 SecondNTTPD->hasDefaultArgument() &&
11638                 !SecondNTTPD->defaultArgumentWasInherited();
11639             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11640               ODRDiagError(FirstTemplate->getLocation(),
11641                            FirstTemplate->getSourceRange(),
11642                            FunctionTemplateParameterSingleDefaultArgument)
11643                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11644               ODRDiagNote(SecondTemplate->getLocation(),
11645                           SecondTemplate->getSourceRange(),
11646                           FunctionTemplateParameterSingleDefaultArgument)
11647                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11648               ParameterMismatch = true;
11649               break;
11650             }
11651 
11652             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11653               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11654               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11655               if (ComputeODRHash(FirstDefaultArgument) !=
11656                   ComputeODRHash(SecondDefaultArgument)) {
11657                 ODRDiagError(FirstTemplate->getLocation(),
11658                              FirstTemplate->getSourceRange(),
11659                              FunctionTemplateParameterDifferentDefaultArgument)
11660                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11661                 ODRDiagNote(SecondTemplate->getLocation(),
11662                             SecondTemplate->getSourceRange(),
11663                             FunctionTemplateParameterDifferentDefaultArgument)
11664                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11665                 ParameterMismatch = true;
11666                 break;
11667               }
11668             }
11669 
11670             if (FirstNTTPD->isParameterPack() !=
11671                 SecondNTTPD->isParameterPack()) {
11672               ODRDiagError(FirstTemplate->getLocation(),
11673                            FirstTemplate->getSourceRange(),
11674                            FunctionTemplatePackParameter)
11675                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11676               ODRDiagNote(SecondTemplate->getLocation(),
11677                           SecondTemplate->getSourceRange(),
11678                           FunctionTemplatePackParameter)
11679                   << SecondTemplate << (i + 1)
11680                   << SecondNTTPD->isParameterPack();
11681               ParameterMismatch = true;
11682               break;
11683             }
11684           }
11685         }
11686 
11687         if (ParameterMismatch) {
11688           Diagnosed = true;
11689           break;
11690         }
11691 
11692         break;
11693       }
11694       }
11695 
11696       if (Diagnosed)
11697         continue;
11698 
11699       Diag(FirstDecl->getLocation(),
11700            diag::err_module_odr_violation_mismatch_decl_unknown)
11701           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11702           << FirstDecl->getSourceRange();
11703       Diag(SecondDecl->getLocation(),
11704            diag::note_module_odr_violation_mismatch_decl_unknown)
11705           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11706       Diagnosed = true;
11707     }
11708 
11709     if (!Diagnosed) {
11710       // All definitions are updates to the same declaration. This happens if a
11711       // module instantiates the declaration of a class template specialization
11712       // and two or more other modules instantiate its definition.
11713       //
11714       // FIXME: Indicate which modules had instantiations of this definition.
11715       // FIXME: How can this even happen?
11716       Diag(Merge.first->getLocation(),
11717            diag::err_module_odr_violation_different_instantiations)
11718         << Merge.first;
11719     }
11720   }
11721 
11722   // Issue ODR failures diagnostics for functions.
11723   for (auto &Merge : FunctionOdrMergeFailures) {
11724     enum ODRFunctionDifference {
11725       ReturnType,
11726       ParameterName,
11727       ParameterType,
11728       ParameterSingleDefaultArgument,
11729       ParameterDifferentDefaultArgument,
11730       FunctionBody,
11731     };
11732 
11733     FunctionDecl *FirstFunction = Merge.first;
11734     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11735 
11736     bool Diagnosed = false;
11737     for (auto &SecondFunction : Merge.second) {
11738 
11739       if (FirstFunction == SecondFunction)
11740         continue;
11741 
11742       std::string SecondModule =
11743           getOwningModuleNameForDiagnostic(SecondFunction);
11744 
11745       auto ODRDiagError = [FirstFunction, &FirstModule,
11746                            this](SourceLocation Loc, SourceRange Range,
11747                                  ODRFunctionDifference DiffType) {
11748         return Diag(Loc, diag::err_module_odr_violation_function)
11749                << FirstFunction << FirstModule.empty() << FirstModule << Range
11750                << DiffType;
11751       };
11752       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11753                                                SourceRange Range,
11754                                                ODRFunctionDifference DiffType) {
11755         return Diag(Loc, diag::note_module_odr_violation_function)
11756                << SecondModule << Range << DiffType;
11757       };
11758 
11759       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11760           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11761         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11762                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11763             << FirstFunction->getReturnType();
11764         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11765                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11766             << SecondFunction->getReturnType();
11767         Diagnosed = true;
11768         break;
11769       }
11770 
11771       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11772              "Merged functions with different number of parameters");
11773 
11774       auto ParamSize = FirstFunction->param_size();
11775       bool ParameterMismatch = false;
11776       for (unsigned I = 0; I < ParamSize; ++I) {
11777         auto *FirstParam = FirstFunction->getParamDecl(I);
11778         auto *SecondParam = SecondFunction->getParamDecl(I);
11779 
11780         assert(getContext().hasSameType(FirstParam->getType(),
11781                                       SecondParam->getType()) &&
11782                "Merged function has different parameter types.");
11783 
11784         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11785           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11786                        ParameterName)
11787               << I + 1 << FirstParam->getDeclName();
11788           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11789                       ParameterName)
11790               << I + 1 << SecondParam->getDeclName();
11791           ParameterMismatch = true;
11792           break;
11793         };
11794 
11795         QualType FirstParamType = FirstParam->getType();
11796         QualType SecondParamType = SecondParam->getType();
11797         if (FirstParamType != SecondParamType &&
11798             ComputeQualTypeODRHash(FirstParamType) !=
11799                 ComputeQualTypeODRHash(SecondParamType)) {
11800           if (const DecayedType *ParamDecayedType =
11801                   FirstParamType->getAs<DecayedType>()) {
11802             ODRDiagError(FirstParam->getLocation(),
11803                          FirstParam->getSourceRange(), ParameterType)
11804                 << (I + 1) << FirstParamType << true
11805                 << ParamDecayedType->getOriginalType();
11806           } else {
11807             ODRDiagError(FirstParam->getLocation(),
11808                          FirstParam->getSourceRange(), ParameterType)
11809                 << (I + 1) << FirstParamType << false;
11810           }
11811 
11812           if (const DecayedType *ParamDecayedType =
11813                   SecondParamType->getAs<DecayedType>()) {
11814             ODRDiagNote(SecondParam->getLocation(),
11815                         SecondParam->getSourceRange(), ParameterType)
11816                 << (I + 1) << SecondParamType << true
11817                 << ParamDecayedType->getOriginalType();
11818           } else {
11819             ODRDiagNote(SecondParam->getLocation(),
11820                         SecondParam->getSourceRange(), ParameterType)
11821                 << (I + 1) << SecondParamType << false;
11822           }
11823           ParameterMismatch = true;
11824           break;
11825         }
11826 
11827         const Expr *FirstInit = FirstParam->getInit();
11828         const Expr *SecondInit = SecondParam->getInit();
11829         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11830           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11831                        ParameterSingleDefaultArgument)
11832               << (I + 1) << (FirstInit == nullptr)
11833               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11834           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11835                       ParameterSingleDefaultArgument)
11836               << (I + 1) << (SecondInit == nullptr)
11837               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11838           ParameterMismatch = true;
11839           break;
11840         }
11841 
11842         if (FirstInit && SecondInit &&
11843             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11844           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11845                        ParameterDifferentDefaultArgument)
11846               << (I + 1) << FirstInit->getSourceRange();
11847           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11848                       ParameterDifferentDefaultArgument)
11849               << (I + 1) << SecondInit->getSourceRange();
11850           ParameterMismatch = true;
11851           break;
11852         }
11853 
11854         assert(ComputeSubDeclODRHash(FirstParam) ==
11855                    ComputeSubDeclODRHash(SecondParam) &&
11856                "Undiagnosed parameter difference.");
11857       }
11858 
11859       if (ParameterMismatch) {
11860         Diagnosed = true;
11861         break;
11862       }
11863 
11864       // If no error has been generated before now, assume the problem is in
11865       // the body and generate a message.
11866       ODRDiagError(FirstFunction->getLocation(),
11867                    FirstFunction->getSourceRange(), FunctionBody);
11868       ODRDiagNote(SecondFunction->getLocation(),
11869                   SecondFunction->getSourceRange(), FunctionBody);
11870       Diagnosed = true;
11871       break;
11872     }
11873     (void)Diagnosed;
11874     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11875   }
11876 
11877   // Issue ODR failures diagnostics for enums.
11878   for (auto &Merge : EnumOdrMergeFailures) {
11879     enum ODREnumDifference {
11880       SingleScopedEnum,
11881       EnumTagKeywordMismatch,
11882       SingleSpecifiedType,
11883       DifferentSpecifiedTypes,
11884       DifferentNumberEnumConstants,
11885       EnumConstantName,
11886       EnumConstantSingleInitilizer,
11887       EnumConstantDifferentInitilizer,
11888     };
11889 
11890     // If we've already pointed out a specific problem with this enum, don't
11891     // bother issuing a general "something's different" diagnostic.
11892     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11893       continue;
11894 
11895     EnumDecl *FirstEnum = Merge.first;
11896     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11897 
11898     using DeclHashes =
11899         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11900     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11901                               DeclHashes &Hashes, EnumDecl *Enum) {
11902       for (auto *D : Enum->decls()) {
11903         // Due to decl merging, the first EnumDecl is the parent of
11904         // Decls in both records.
11905         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11906           continue;
11907         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11908         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11909                             ComputeSubDeclODRHash(D));
11910       }
11911     };
11912     DeclHashes FirstHashes;
11913     PopulateHashes(FirstHashes, FirstEnum);
11914     bool Diagnosed = false;
11915     for (auto &SecondEnum : Merge.second) {
11916 
11917       if (FirstEnum == SecondEnum)
11918         continue;
11919 
11920       std::string SecondModule =
11921           getOwningModuleNameForDiagnostic(SecondEnum);
11922 
11923       auto ODRDiagError = [FirstEnum, &FirstModule,
11924                            this](SourceLocation Loc, SourceRange Range,
11925                                  ODREnumDifference DiffType) {
11926         return Diag(Loc, diag::err_module_odr_violation_enum)
11927                << FirstEnum << FirstModule.empty() << FirstModule << Range
11928                << DiffType;
11929       };
11930       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11931                                                SourceRange Range,
11932                                                ODREnumDifference DiffType) {
11933         return Diag(Loc, diag::note_module_odr_violation_enum)
11934                << SecondModule << Range << DiffType;
11935       };
11936 
11937       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11938         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11939                      SingleScopedEnum)
11940             << FirstEnum->isScoped();
11941         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11942                     SingleScopedEnum)
11943             << SecondEnum->isScoped();
11944         Diagnosed = true;
11945         continue;
11946       }
11947 
11948       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11949         if (FirstEnum->isScopedUsingClassTag() !=
11950             SecondEnum->isScopedUsingClassTag()) {
11951           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11952                        EnumTagKeywordMismatch)
11953               << FirstEnum->isScopedUsingClassTag();
11954           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11955                       EnumTagKeywordMismatch)
11956               << SecondEnum->isScopedUsingClassTag();
11957           Diagnosed = true;
11958           continue;
11959         }
11960       }
11961 
11962       QualType FirstUnderlyingType =
11963           FirstEnum->getIntegerTypeSourceInfo()
11964               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11965               : QualType();
11966       QualType SecondUnderlyingType =
11967           SecondEnum->getIntegerTypeSourceInfo()
11968               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11969               : QualType();
11970       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11971           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11972                        SingleSpecifiedType)
11973               << !FirstUnderlyingType.isNull();
11974           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11975                       SingleSpecifiedType)
11976               << !SecondUnderlyingType.isNull();
11977           Diagnosed = true;
11978           continue;
11979       }
11980 
11981       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11982         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11983             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11984           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11985                        DifferentSpecifiedTypes)
11986               << FirstUnderlyingType;
11987           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11988                       DifferentSpecifiedTypes)
11989               << SecondUnderlyingType;
11990           Diagnosed = true;
11991           continue;
11992         }
11993       }
11994 
11995       DeclHashes SecondHashes;
11996       PopulateHashes(SecondHashes, SecondEnum);
11997 
11998       if (FirstHashes.size() != SecondHashes.size()) {
11999         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
12000                      DifferentNumberEnumConstants)
12001             << (int)FirstHashes.size();
12002         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12003                     DifferentNumberEnumConstants)
12004             << (int)SecondHashes.size();
12005         Diagnosed = true;
12006         continue;
12007       }
12008 
12009       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
12010         if (FirstHashes[I].second == SecondHashes[I].second)
12011           continue;
12012         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
12013         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
12014 
12015         if (FirstEnumConstant->getDeclName() !=
12016             SecondEnumConstant->getDeclName()) {
12017 
12018           ODRDiagError(FirstEnumConstant->getLocation(),
12019                        FirstEnumConstant->getSourceRange(), EnumConstantName)
12020               << I + 1 << FirstEnumConstant;
12021           ODRDiagNote(SecondEnumConstant->getLocation(),
12022                       SecondEnumConstant->getSourceRange(), EnumConstantName)
12023               << I + 1 << SecondEnumConstant;
12024           Diagnosed = true;
12025           break;
12026         }
12027 
12028         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12029         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12030         if (!FirstInit && !SecondInit)
12031           continue;
12032 
12033         if (!FirstInit || !SecondInit) {
12034           ODRDiagError(FirstEnumConstant->getLocation(),
12035                        FirstEnumConstant->getSourceRange(),
12036                        EnumConstantSingleInitilizer)
12037               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12038           ODRDiagNote(SecondEnumConstant->getLocation(),
12039                       SecondEnumConstant->getSourceRange(),
12040                       EnumConstantSingleInitilizer)
12041               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12042           Diagnosed = true;
12043           break;
12044         }
12045 
12046         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12047           ODRDiagError(FirstEnumConstant->getLocation(),
12048                        FirstEnumConstant->getSourceRange(),
12049                        EnumConstantDifferentInitilizer)
12050               << I + 1 << FirstEnumConstant;
12051           ODRDiagNote(SecondEnumConstant->getLocation(),
12052                       SecondEnumConstant->getSourceRange(),
12053                       EnumConstantDifferentInitilizer)
12054               << I + 1 << SecondEnumConstant;
12055           Diagnosed = true;
12056           break;
12057         }
12058       }
12059     }
12060 
12061     (void)Diagnosed;
12062     assert(Diagnosed && "Unable to emit ODR diagnostic.");
12063   }
12064 }
12065 
12066 void ASTReader::StartedDeserializing() {
12067   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12068     ReadTimer->startTimer();
12069 }
12070 
12071 void ASTReader::FinishedDeserializing() {
12072   assert(NumCurrentElementsDeserializing &&
12073          "FinishedDeserializing not paired with StartedDeserializing");
12074   if (NumCurrentElementsDeserializing == 1) {
12075     // We decrease NumCurrentElementsDeserializing only after pending actions
12076     // are finished, to avoid recursively re-calling finishPendingActions().
12077     finishPendingActions();
12078   }
12079   --NumCurrentElementsDeserializing;
12080 
12081   if (NumCurrentElementsDeserializing == 0) {
12082     // Propagate exception specification and deduced type updates along
12083     // redeclaration chains.
12084     //
12085     // We do this now rather than in finishPendingActions because we want to
12086     // be able to walk the complete redeclaration chains of the updated decls.
12087     while (!PendingExceptionSpecUpdates.empty() ||
12088            !PendingDeducedTypeUpdates.empty()) {
12089       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12090       PendingExceptionSpecUpdates.clear();
12091       for (auto Update : ESUpdates) {
12092         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12093         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12094         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12095         if (auto *Listener = getContext().getASTMutationListener())
12096           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12097         for (auto *Redecl : Update.second->redecls())
12098           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12099       }
12100 
12101       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12102       PendingDeducedTypeUpdates.clear();
12103       for (auto Update : DTUpdates) {
12104         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12105         // FIXME: If the return type is already deduced, check that it matches.
12106         getContext().adjustDeducedFunctionResultType(Update.first,
12107                                                      Update.second);
12108       }
12109     }
12110 
12111     if (ReadTimer)
12112       ReadTimer->stopTimer();
12113 
12114     diagnoseOdrViolations();
12115 
12116     // We are not in recursive loading, so it's safe to pass the "interesting"
12117     // decls to the consumer.
12118     if (Consumer)
12119       PassInterestingDeclsToConsumer();
12120   }
12121 }
12122 
12123 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12124   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12125     // Remove any fake results before adding any real ones.
12126     auto It = PendingFakeLookupResults.find(II);
12127     if (It != PendingFakeLookupResults.end()) {
12128       for (auto *ND : It->second)
12129         SemaObj->IdResolver.RemoveDecl(ND);
12130       // FIXME: this works around module+PCH performance issue.
12131       // Rather than erase the result from the map, which is O(n), just clear
12132       // the vector of NamedDecls.
12133       It->second.clear();
12134     }
12135   }
12136 
12137   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12138     SemaObj->TUScope->AddDecl(D);
12139   } else if (SemaObj->TUScope) {
12140     // Adding the decl to IdResolver may have failed because it was already in
12141     // (even though it was not added in scope). If it is already in, make sure
12142     // it gets in the scope as well.
12143     if (std::find(SemaObj->IdResolver.begin(Name),
12144                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12145       SemaObj->TUScope->AddDecl(D);
12146   }
12147 }
12148 
12149 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12150                      ASTContext *Context,
12151                      const PCHContainerReader &PCHContainerRdr,
12152                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12153                      StringRef isysroot, bool DisableValidation,
12154                      bool AllowASTWithCompilerErrors,
12155                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12156                      bool UseGlobalIndex,
12157                      std::unique_ptr<llvm::Timer> ReadTimer)
12158     : Listener(DisableValidation
12159                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12160                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12161       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12162       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12163       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12164                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
12165       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12166       DisableValidation(DisableValidation),
12167       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12168       AllowConfigurationMismatch(AllowConfigurationMismatch),
12169       ValidateSystemInputs(ValidateSystemInputs),
12170       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12171   SourceMgr.setExternalSLocEntrySource(this);
12172 
12173   for (const auto &Ext : Extensions) {
12174     auto BlockName = Ext->getExtensionMetadata().BlockName;
12175     auto Known = ModuleFileExtensions.find(BlockName);
12176     if (Known != ModuleFileExtensions.end()) {
12177       Diags.Report(diag::warn_duplicate_module_file_extension)
12178         << BlockName;
12179       continue;
12180     }
12181 
12182     ModuleFileExtensions.insert({BlockName, Ext});
12183   }
12184 }
12185 
12186 ASTReader::~ASTReader() {
12187   if (OwnsDeserializationListener)
12188     delete DeserializationListener;
12189 }
12190 
12191 IdentifierResolver &ASTReader::getIdResolver() {
12192   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12193 }
12194 
12195 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12196                                                unsigned AbbrevID) {
12197   Idx = 0;
12198   Record.clear();
12199   return Cursor.readRecord(AbbrevID, Record);
12200 }
12201 //===----------------------------------------------------------------------===//
12202 //// OMPClauseReader implementation
12203 ////===----------------------------------------------------------------------===//
12204 
12205 OMPClause *OMPClauseReader::readClause() {
12206   OMPClause *C = nullptr;
12207   switch (Record.readInt()) {
12208   case OMPC_if:
12209     C = new (Context) OMPIfClause();
12210     break;
12211   case OMPC_final:
12212     C = new (Context) OMPFinalClause();
12213     break;
12214   case OMPC_num_threads:
12215     C = new (Context) OMPNumThreadsClause();
12216     break;
12217   case OMPC_safelen:
12218     C = new (Context) OMPSafelenClause();
12219     break;
12220   case OMPC_simdlen:
12221     C = new (Context) OMPSimdlenClause();
12222     break;
12223   case OMPC_allocator:
12224     C = new (Context) OMPAllocatorClause();
12225     break;
12226   case OMPC_collapse:
12227     C = new (Context) OMPCollapseClause();
12228     break;
12229   case OMPC_default:
12230     C = new (Context) OMPDefaultClause();
12231     break;
12232   case OMPC_proc_bind:
12233     C = new (Context) OMPProcBindClause();
12234     break;
12235   case OMPC_schedule:
12236     C = new (Context) OMPScheduleClause();
12237     break;
12238   case OMPC_ordered:
12239     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12240     break;
12241   case OMPC_nowait:
12242     C = new (Context) OMPNowaitClause();
12243     break;
12244   case OMPC_untied:
12245     C = new (Context) OMPUntiedClause();
12246     break;
12247   case OMPC_mergeable:
12248     C = new (Context) OMPMergeableClause();
12249     break;
12250   case OMPC_read:
12251     C = new (Context) OMPReadClause();
12252     break;
12253   case OMPC_write:
12254     C = new (Context) OMPWriteClause();
12255     break;
12256   case OMPC_update:
12257     C = new (Context) OMPUpdateClause();
12258     break;
12259   case OMPC_capture:
12260     C = new (Context) OMPCaptureClause();
12261     break;
12262   case OMPC_seq_cst:
12263     C = new (Context) OMPSeqCstClause();
12264     break;
12265   case OMPC_threads:
12266     C = new (Context) OMPThreadsClause();
12267     break;
12268   case OMPC_simd:
12269     C = new (Context) OMPSIMDClause();
12270     break;
12271   case OMPC_nogroup:
12272     C = new (Context) OMPNogroupClause();
12273     break;
12274   case OMPC_unified_address:
12275     C = new (Context) OMPUnifiedAddressClause();
12276     break;
12277   case OMPC_unified_shared_memory:
12278     C = new (Context) OMPUnifiedSharedMemoryClause();
12279     break;
12280   case OMPC_reverse_offload:
12281     C = new (Context) OMPReverseOffloadClause();
12282     break;
12283   case OMPC_dynamic_allocators:
12284     C = new (Context) OMPDynamicAllocatorsClause();
12285     break;
12286   case OMPC_atomic_default_mem_order:
12287     C = new (Context) OMPAtomicDefaultMemOrderClause();
12288     break;
12289  case OMPC_private:
12290     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12291     break;
12292   case OMPC_firstprivate:
12293     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12294     break;
12295   case OMPC_lastprivate:
12296     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12297     break;
12298   case OMPC_shared:
12299     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12300     break;
12301   case OMPC_reduction:
12302     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12303     break;
12304   case OMPC_task_reduction:
12305     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12306     break;
12307   case OMPC_in_reduction:
12308     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12309     break;
12310   case OMPC_linear:
12311     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12312     break;
12313   case OMPC_aligned:
12314     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12315     break;
12316   case OMPC_copyin:
12317     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12318     break;
12319   case OMPC_copyprivate:
12320     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12321     break;
12322   case OMPC_flush:
12323     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12324     break;
12325   case OMPC_depend: {
12326     unsigned NumVars = Record.readInt();
12327     unsigned NumLoops = Record.readInt();
12328     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12329     break;
12330   }
12331   case OMPC_device:
12332     C = new (Context) OMPDeviceClause();
12333     break;
12334   case OMPC_map: {
12335     OMPMappableExprListSizeTy Sizes;
12336     Sizes.NumVars = Record.readInt();
12337     Sizes.NumUniqueDeclarations = Record.readInt();
12338     Sizes.NumComponentLists = Record.readInt();
12339     Sizes.NumComponents = Record.readInt();
12340     C = OMPMapClause::CreateEmpty(Context, Sizes);
12341     break;
12342   }
12343   case OMPC_num_teams:
12344     C = new (Context) OMPNumTeamsClause();
12345     break;
12346   case OMPC_thread_limit:
12347     C = new (Context) OMPThreadLimitClause();
12348     break;
12349   case OMPC_priority:
12350     C = new (Context) OMPPriorityClause();
12351     break;
12352   case OMPC_grainsize:
12353     C = new (Context) OMPGrainsizeClause();
12354     break;
12355   case OMPC_num_tasks:
12356     C = new (Context) OMPNumTasksClause();
12357     break;
12358   case OMPC_hint:
12359     C = new (Context) OMPHintClause();
12360     break;
12361   case OMPC_dist_schedule:
12362     C = new (Context) OMPDistScheduleClause();
12363     break;
12364   case OMPC_defaultmap:
12365     C = new (Context) OMPDefaultmapClause();
12366     break;
12367   case OMPC_to: {
12368     OMPMappableExprListSizeTy Sizes;
12369     Sizes.NumVars = Record.readInt();
12370     Sizes.NumUniqueDeclarations = Record.readInt();
12371     Sizes.NumComponentLists = Record.readInt();
12372     Sizes.NumComponents = Record.readInt();
12373     C = OMPToClause::CreateEmpty(Context, Sizes);
12374     break;
12375   }
12376   case OMPC_from: {
12377     OMPMappableExprListSizeTy Sizes;
12378     Sizes.NumVars = Record.readInt();
12379     Sizes.NumUniqueDeclarations = Record.readInt();
12380     Sizes.NumComponentLists = Record.readInt();
12381     Sizes.NumComponents = Record.readInt();
12382     C = OMPFromClause::CreateEmpty(Context, Sizes);
12383     break;
12384   }
12385   case OMPC_use_device_ptr: {
12386     OMPMappableExprListSizeTy Sizes;
12387     Sizes.NumVars = Record.readInt();
12388     Sizes.NumUniqueDeclarations = Record.readInt();
12389     Sizes.NumComponentLists = Record.readInt();
12390     Sizes.NumComponents = Record.readInt();
12391     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12392     break;
12393   }
12394   case OMPC_is_device_ptr: {
12395     OMPMappableExprListSizeTy Sizes;
12396     Sizes.NumVars = Record.readInt();
12397     Sizes.NumUniqueDeclarations = Record.readInt();
12398     Sizes.NumComponentLists = Record.readInt();
12399     Sizes.NumComponents = Record.readInt();
12400     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12401     break;
12402   }
12403   case OMPC_allocate:
12404     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12405     break;
12406   }
12407   assert(C && "Unknown OMPClause type");
12408 
12409   Visit(C);
12410   C->setLocStart(Record.readSourceLocation());
12411   C->setLocEnd(Record.readSourceLocation());
12412 
12413   return C;
12414 }
12415 
12416 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12417   C->setPreInitStmt(Record.readSubStmt(),
12418                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12419 }
12420 
12421 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12422   VisitOMPClauseWithPreInit(C);
12423   C->setPostUpdateExpr(Record.readSubExpr());
12424 }
12425 
12426 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12427   VisitOMPClauseWithPreInit(C);
12428   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12429   C->setNameModifierLoc(Record.readSourceLocation());
12430   C->setColonLoc(Record.readSourceLocation());
12431   C->setCondition(Record.readSubExpr());
12432   C->setLParenLoc(Record.readSourceLocation());
12433 }
12434 
12435 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12436   C->setCondition(Record.readSubExpr());
12437   C->setLParenLoc(Record.readSourceLocation());
12438 }
12439 
12440 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12441   VisitOMPClauseWithPreInit(C);
12442   C->setNumThreads(Record.readSubExpr());
12443   C->setLParenLoc(Record.readSourceLocation());
12444 }
12445 
12446 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12447   C->setSafelen(Record.readSubExpr());
12448   C->setLParenLoc(Record.readSourceLocation());
12449 }
12450 
12451 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12452   C->setSimdlen(Record.readSubExpr());
12453   C->setLParenLoc(Record.readSourceLocation());
12454 }
12455 
12456 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12457   C->setAllocator(Record.readExpr());
12458   C->setLParenLoc(Record.readSourceLocation());
12459 }
12460 
12461 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12462   C->setNumForLoops(Record.readSubExpr());
12463   C->setLParenLoc(Record.readSourceLocation());
12464 }
12465 
12466 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12467   C->setDefaultKind(
12468        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12469   C->setLParenLoc(Record.readSourceLocation());
12470   C->setDefaultKindKwLoc(Record.readSourceLocation());
12471 }
12472 
12473 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12474   C->setProcBindKind(
12475        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12476   C->setLParenLoc(Record.readSourceLocation());
12477   C->setProcBindKindKwLoc(Record.readSourceLocation());
12478 }
12479 
12480 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12481   VisitOMPClauseWithPreInit(C);
12482   C->setScheduleKind(
12483        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12484   C->setFirstScheduleModifier(
12485       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12486   C->setSecondScheduleModifier(
12487       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12488   C->setChunkSize(Record.readSubExpr());
12489   C->setLParenLoc(Record.readSourceLocation());
12490   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12491   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12492   C->setScheduleKindLoc(Record.readSourceLocation());
12493   C->setCommaLoc(Record.readSourceLocation());
12494 }
12495 
12496 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12497   C->setNumForLoops(Record.readSubExpr());
12498   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12499     C->setLoopNumIterations(I, Record.readSubExpr());
12500   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12501     C->setLoopCounter(I, Record.readSubExpr());
12502   C->setLParenLoc(Record.readSourceLocation());
12503 }
12504 
12505 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12506 
12507 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12508 
12509 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12510 
12511 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12512 
12513 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12514 
12515 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12516 
12517 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12518 
12519 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12520 
12521 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12522 
12523 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12524 
12525 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12526 
12527 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12528 
12529 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12530     OMPUnifiedSharedMemoryClause *) {}
12531 
12532 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12533 
12534 void
12535 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12536 }
12537 
12538 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12539     OMPAtomicDefaultMemOrderClause *C) {
12540   C->setAtomicDefaultMemOrderKind(
12541       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12542   C->setLParenLoc(Record.readSourceLocation());
12543   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12544 }
12545 
12546 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12547   C->setLParenLoc(Record.readSourceLocation());
12548   unsigned NumVars = C->varlist_size();
12549   SmallVector<Expr *, 16> Vars;
12550   Vars.reserve(NumVars);
12551   for (unsigned i = 0; i != NumVars; ++i)
12552     Vars.push_back(Record.readSubExpr());
12553   C->setVarRefs(Vars);
12554   Vars.clear();
12555   for (unsigned i = 0; i != NumVars; ++i)
12556     Vars.push_back(Record.readSubExpr());
12557   C->setPrivateCopies(Vars);
12558 }
12559 
12560 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12561   VisitOMPClauseWithPreInit(C);
12562   C->setLParenLoc(Record.readSourceLocation());
12563   unsigned NumVars = C->varlist_size();
12564   SmallVector<Expr *, 16> Vars;
12565   Vars.reserve(NumVars);
12566   for (unsigned i = 0; i != NumVars; ++i)
12567     Vars.push_back(Record.readSubExpr());
12568   C->setVarRefs(Vars);
12569   Vars.clear();
12570   for (unsigned i = 0; i != NumVars; ++i)
12571     Vars.push_back(Record.readSubExpr());
12572   C->setPrivateCopies(Vars);
12573   Vars.clear();
12574   for (unsigned i = 0; i != NumVars; ++i)
12575     Vars.push_back(Record.readSubExpr());
12576   C->setInits(Vars);
12577 }
12578 
12579 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12580   VisitOMPClauseWithPostUpdate(C);
12581   C->setLParenLoc(Record.readSourceLocation());
12582   unsigned NumVars = C->varlist_size();
12583   SmallVector<Expr *, 16> Vars;
12584   Vars.reserve(NumVars);
12585   for (unsigned i = 0; i != NumVars; ++i)
12586     Vars.push_back(Record.readSubExpr());
12587   C->setVarRefs(Vars);
12588   Vars.clear();
12589   for (unsigned i = 0; i != NumVars; ++i)
12590     Vars.push_back(Record.readSubExpr());
12591   C->setPrivateCopies(Vars);
12592   Vars.clear();
12593   for (unsigned i = 0; i != NumVars; ++i)
12594     Vars.push_back(Record.readSubExpr());
12595   C->setSourceExprs(Vars);
12596   Vars.clear();
12597   for (unsigned i = 0; i != NumVars; ++i)
12598     Vars.push_back(Record.readSubExpr());
12599   C->setDestinationExprs(Vars);
12600   Vars.clear();
12601   for (unsigned i = 0; i != NumVars; ++i)
12602     Vars.push_back(Record.readSubExpr());
12603   C->setAssignmentOps(Vars);
12604 }
12605 
12606 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12607   C->setLParenLoc(Record.readSourceLocation());
12608   unsigned NumVars = C->varlist_size();
12609   SmallVector<Expr *, 16> Vars;
12610   Vars.reserve(NumVars);
12611   for (unsigned i = 0; i != NumVars; ++i)
12612     Vars.push_back(Record.readSubExpr());
12613   C->setVarRefs(Vars);
12614 }
12615 
12616 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12617   VisitOMPClauseWithPostUpdate(C);
12618   C->setLParenLoc(Record.readSourceLocation());
12619   C->setColonLoc(Record.readSourceLocation());
12620   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12621   DeclarationNameInfo DNI;
12622   Record.readDeclarationNameInfo(DNI);
12623   C->setQualifierLoc(NNSL);
12624   C->setNameInfo(DNI);
12625 
12626   unsigned NumVars = C->varlist_size();
12627   SmallVector<Expr *, 16> Vars;
12628   Vars.reserve(NumVars);
12629   for (unsigned i = 0; i != NumVars; ++i)
12630     Vars.push_back(Record.readSubExpr());
12631   C->setVarRefs(Vars);
12632   Vars.clear();
12633   for (unsigned i = 0; i != NumVars; ++i)
12634     Vars.push_back(Record.readSubExpr());
12635   C->setPrivates(Vars);
12636   Vars.clear();
12637   for (unsigned i = 0; i != NumVars; ++i)
12638     Vars.push_back(Record.readSubExpr());
12639   C->setLHSExprs(Vars);
12640   Vars.clear();
12641   for (unsigned i = 0; i != NumVars; ++i)
12642     Vars.push_back(Record.readSubExpr());
12643   C->setRHSExprs(Vars);
12644   Vars.clear();
12645   for (unsigned i = 0; i != NumVars; ++i)
12646     Vars.push_back(Record.readSubExpr());
12647   C->setReductionOps(Vars);
12648 }
12649 
12650 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12651   VisitOMPClauseWithPostUpdate(C);
12652   C->setLParenLoc(Record.readSourceLocation());
12653   C->setColonLoc(Record.readSourceLocation());
12654   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12655   DeclarationNameInfo DNI;
12656   Record.readDeclarationNameInfo(DNI);
12657   C->setQualifierLoc(NNSL);
12658   C->setNameInfo(DNI);
12659 
12660   unsigned NumVars = C->varlist_size();
12661   SmallVector<Expr *, 16> Vars;
12662   Vars.reserve(NumVars);
12663   for (unsigned I = 0; I != NumVars; ++I)
12664     Vars.push_back(Record.readSubExpr());
12665   C->setVarRefs(Vars);
12666   Vars.clear();
12667   for (unsigned I = 0; I != NumVars; ++I)
12668     Vars.push_back(Record.readSubExpr());
12669   C->setPrivates(Vars);
12670   Vars.clear();
12671   for (unsigned I = 0; I != NumVars; ++I)
12672     Vars.push_back(Record.readSubExpr());
12673   C->setLHSExprs(Vars);
12674   Vars.clear();
12675   for (unsigned I = 0; I != NumVars; ++I)
12676     Vars.push_back(Record.readSubExpr());
12677   C->setRHSExprs(Vars);
12678   Vars.clear();
12679   for (unsigned I = 0; I != NumVars; ++I)
12680     Vars.push_back(Record.readSubExpr());
12681   C->setReductionOps(Vars);
12682 }
12683 
12684 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12685   VisitOMPClauseWithPostUpdate(C);
12686   C->setLParenLoc(Record.readSourceLocation());
12687   C->setColonLoc(Record.readSourceLocation());
12688   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12689   DeclarationNameInfo DNI;
12690   Record.readDeclarationNameInfo(DNI);
12691   C->setQualifierLoc(NNSL);
12692   C->setNameInfo(DNI);
12693 
12694   unsigned NumVars = C->varlist_size();
12695   SmallVector<Expr *, 16> Vars;
12696   Vars.reserve(NumVars);
12697   for (unsigned I = 0; I != NumVars; ++I)
12698     Vars.push_back(Record.readSubExpr());
12699   C->setVarRefs(Vars);
12700   Vars.clear();
12701   for (unsigned I = 0; I != NumVars; ++I)
12702     Vars.push_back(Record.readSubExpr());
12703   C->setPrivates(Vars);
12704   Vars.clear();
12705   for (unsigned I = 0; I != NumVars; ++I)
12706     Vars.push_back(Record.readSubExpr());
12707   C->setLHSExprs(Vars);
12708   Vars.clear();
12709   for (unsigned I = 0; I != NumVars; ++I)
12710     Vars.push_back(Record.readSubExpr());
12711   C->setRHSExprs(Vars);
12712   Vars.clear();
12713   for (unsigned I = 0; I != NumVars; ++I)
12714     Vars.push_back(Record.readSubExpr());
12715   C->setReductionOps(Vars);
12716   Vars.clear();
12717   for (unsigned I = 0; I != NumVars; ++I)
12718     Vars.push_back(Record.readSubExpr());
12719   C->setTaskgroupDescriptors(Vars);
12720 }
12721 
12722 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12723   VisitOMPClauseWithPostUpdate(C);
12724   C->setLParenLoc(Record.readSourceLocation());
12725   C->setColonLoc(Record.readSourceLocation());
12726   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12727   C->setModifierLoc(Record.readSourceLocation());
12728   unsigned NumVars = C->varlist_size();
12729   SmallVector<Expr *, 16> Vars;
12730   Vars.reserve(NumVars);
12731   for (unsigned i = 0; i != NumVars; ++i)
12732     Vars.push_back(Record.readSubExpr());
12733   C->setVarRefs(Vars);
12734   Vars.clear();
12735   for (unsigned i = 0; i != NumVars; ++i)
12736     Vars.push_back(Record.readSubExpr());
12737   C->setPrivates(Vars);
12738   Vars.clear();
12739   for (unsigned i = 0; i != NumVars; ++i)
12740     Vars.push_back(Record.readSubExpr());
12741   C->setInits(Vars);
12742   Vars.clear();
12743   for (unsigned i = 0; i != NumVars; ++i)
12744     Vars.push_back(Record.readSubExpr());
12745   C->setUpdates(Vars);
12746   Vars.clear();
12747   for (unsigned i = 0; i != NumVars; ++i)
12748     Vars.push_back(Record.readSubExpr());
12749   C->setFinals(Vars);
12750   C->setStep(Record.readSubExpr());
12751   C->setCalcStep(Record.readSubExpr());
12752   Vars.clear();
12753   for (unsigned I = 0; I != NumVars + 1; ++I)
12754     Vars.push_back(Record.readSubExpr());
12755   C->setUsedExprs(Vars);
12756 }
12757 
12758 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12759   C->setLParenLoc(Record.readSourceLocation());
12760   C->setColonLoc(Record.readSourceLocation());
12761   unsigned NumVars = C->varlist_size();
12762   SmallVector<Expr *, 16> Vars;
12763   Vars.reserve(NumVars);
12764   for (unsigned i = 0; i != NumVars; ++i)
12765     Vars.push_back(Record.readSubExpr());
12766   C->setVarRefs(Vars);
12767   C->setAlignment(Record.readSubExpr());
12768 }
12769 
12770 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12771   C->setLParenLoc(Record.readSourceLocation());
12772   unsigned NumVars = C->varlist_size();
12773   SmallVector<Expr *, 16> Exprs;
12774   Exprs.reserve(NumVars);
12775   for (unsigned i = 0; i != NumVars; ++i)
12776     Exprs.push_back(Record.readSubExpr());
12777   C->setVarRefs(Exprs);
12778   Exprs.clear();
12779   for (unsigned i = 0; i != NumVars; ++i)
12780     Exprs.push_back(Record.readSubExpr());
12781   C->setSourceExprs(Exprs);
12782   Exprs.clear();
12783   for (unsigned i = 0; i != NumVars; ++i)
12784     Exprs.push_back(Record.readSubExpr());
12785   C->setDestinationExprs(Exprs);
12786   Exprs.clear();
12787   for (unsigned i = 0; i != NumVars; ++i)
12788     Exprs.push_back(Record.readSubExpr());
12789   C->setAssignmentOps(Exprs);
12790 }
12791 
12792 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12793   C->setLParenLoc(Record.readSourceLocation());
12794   unsigned NumVars = C->varlist_size();
12795   SmallVector<Expr *, 16> Exprs;
12796   Exprs.reserve(NumVars);
12797   for (unsigned i = 0; i != NumVars; ++i)
12798     Exprs.push_back(Record.readSubExpr());
12799   C->setVarRefs(Exprs);
12800   Exprs.clear();
12801   for (unsigned i = 0; i != NumVars; ++i)
12802     Exprs.push_back(Record.readSubExpr());
12803   C->setSourceExprs(Exprs);
12804   Exprs.clear();
12805   for (unsigned i = 0; i != NumVars; ++i)
12806     Exprs.push_back(Record.readSubExpr());
12807   C->setDestinationExprs(Exprs);
12808   Exprs.clear();
12809   for (unsigned i = 0; i != NumVars; ++i)
12810     Exprs.push_back(Record.readSubExpr());
12811   C->setAssignmentOps(Exprs);
12812 }
12813 
12814 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12815   C->setLParenLoc(Record.readSourceLocation());
12816   unsigned NumVars = C->varlist_size();
12817   SmallVector<Expr *, 16> Vars;
12818   Vars.reserve(NumVars);
12819   for (unsigned i = 0; i != NumVars; ++i)
12820     Vars.push_back(Record.readSubExpr());
12821   C->setVarRefs(Vars);
12822 }
12823 
12824 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12825   C->setLParenLoc(Record.readSourceLocation());
12826   C->setDependencyKind(
12827       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12828   C->setDependencyLoc(Record.readSourceLocation());
12829   C->setColonLoc(Record.readSourceLocation());
12830   unsigned NumVars = C->varlist_size();
12831   SmallVector<Expr *, 16> Vars;
12832   Vars.reserve(NumVars);
12833   for (unsigned I = 0; I != NumVars; ++I)
12834     Vars.push_back(Record.readSubExpr());
12835   C->setVarRefs(Vars);
12836   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12837     C->setLoopData(I, Record.readSubExpr());
12838 }
12839 
12840 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12841   VisitOMPClauseWithPreInit(C);
12842   C->setDevice(Record.readSubExpr());
12843   C->setLParenLoc(Record.readSourceLocation());
12844 }
12845 
12846 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12847   C->setLParenLoc(Record.readSourceLocation());
12848   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12849     C->setMapTypeModifier(
12850         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12851     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12852   }
12853   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12854   DeclarationNameInfo DNI;
12855   Record.readDeclarationNameInfo(DNI);
12856   C->setMapperIdInfo(DNI);
12857   C->setMapType(
12858      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12859   C->setMapLoc(Record.readSourceLocation());
12860   C->setColonLoc(Record.readSourceLocation());
12861   auto NumVars = C->varlist_size();
12862   auto UniqueDecls = C->getUniqueDeclarationsNum();
12863   auto TotalLists = C->getTotalComponentListNum();
12864   auto TotalComponents = C->getTotalComponentsNum();
12865 
12866   SmallVector<Expr *, 16> Vars;
12867   Vars.reserve(NumVars);
12868   for (unsigned i = 0; i != NumVars; ++i)
12869     Vars.push_back(Record.readExpr());
12870   C->setVarRefs(Vars);
12871 
12872   SmallVector<Expr *, 16> UDMappers;
12873   UDMappers.reserve(NumVars);
12874   for (unsigned I = 0; I < NumVars; ++I)
12875     UDMappers.push_back(Record.readExpr());
12876   C->setUDMapperRefs(UDMappers);
12877 
12878   SmallVector<ValueDecl *, 16> Decls;
12879   Decls.reserve(UniqueDecls);
12880   for (unsigned i = 0; i < UniqueDecls; ++i)
12881     Decls.push_back(Record.readDeclAs<ValueDecl>());
12882   C->setUniqueDecls(Decls);
12883 
12884   SmallVector<unsigned, 16> ListsPerDecl;
12885   ListsPerDecl.reserve(UniqueDecls);
12886   for (unsigned i = 0; i < UniqueDecls; ++i)
12887     ListsPerDecl.push_back(Record.readInt());
12888   C->setDeclNumLists(ListsPerDecl);
12889 
12890   SmallVector<unsigned, 32> ListSizes;
12891   ListSizes.reserve(TotalLists);
12892   for (unsigned i = 0; i < TotalLists; ++i)
12893     ListSizes.push_back(Record.readInt());
12894   C->setComponentListSizes(ListSizes);
12895 
12896   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12897   Components.reserve(TotalComponents);
12898   for (unsigned i = 0; i < TotalComponents; ++i) {
12899     Expr *AssociatedExpr = Record.readExpr();
12900     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12901     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12902         AssociatedExpr, AssociatedDecl));
12903   }
12904   C->setComponents(Components, ListSizes);
12905 }
12906 
12907 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12908   C->setLParenLoc(Record.readSourceLocation());
12909   C->setColonLoc(Record.readSourceLocation());
12910   C->setAllocator(Record.readSubExpr());
12911   unsigned NumVars = C->varlist_size();
12912   SmallVector<Expr *, 16> Vars;
12913   Vars.reserve(NumVars);
12914   for (unsigned i = 0; i != NumVars; ++i)
12915     Vars.push_back(Record.readSubExpr());
12916   C->setVarRefs(Vars);
12917 }
12918 
12919 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12920   VisitOMPClauseWithPreInit(C);
12921   C->setNumTeams(Record.readSubExpr());
12922   C->setLParenLoc(Record.readSourceLocation());
12923 }
12924 
12925 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12926   VisitOMPClauseWithPreInit(C);
12927   C->setThreadLimit(Record.readSubExpr());
12928   C->setLParenLoc(Record.readSourceLocation());
12929 }
12930 
12931 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12932   C->setPriority(Record.readSubExpr());
12933   C->setLParenLoc(Record.readSourceLocation());
12934 }
12935 
12936 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12937   C->setGrainsize(Record.readSubExpr());
12938   C->setLParenLoc(Record.readSourceLocation());
12939 }
12940 
12941 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12942   C->setNumTasks(Record.readSubExpr());
12943   C->setLParenLoc(Record.readSourceLocation());
12944 }
12945 
12946 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12947   C->setHint(Record.readSubExpr());
12948   C->setLParenLoc(Record.readSourceLocation());
12949 }
12950 
12951 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12952   VisitOMPClauseWithPreInit(C);
12953   C->setDistScheduleKind(
12954       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12955   C->setChunkSize(Record.readSubExpr());
12956   C->setLParenLoc(Record.readSourceLocation());
12957   C->setDistScheduleKindLoc(Record.readSourceLocation());
12958   C->setCommaLoc(Record.readSourceLocation());
12959 }
12960 
12961 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12962   C->setDefaultmapKind(
12963        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12964   C->setDefaultmapModifier(
12965       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12966   C->setLParenLoc(Record.readSourceLocation());
12967   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12968   C->setDefaultmapKindLoc(Record.readSourceLocation());
12969 }
12970 
12971 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12972   C->setLParenLoc(Record.readSourceLocation());
12973   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12974   DeclarationNameInfo DNI;
12975   Record.readDeclarationNameInfo(DNI);
12976   C->setMapperIdInfo(DNI);
12977   auto NumVars = C->varlist_size();
12978   auto UniqueDecls = C->getUniqueDeclarationsNum();
12979   auto TotalLists = C->getTotalComponentListNum();
12980   auto TotalComponents = C->getTotalComponentsNum();
12981 
12982   SmallVector<Expr *, 16> Vars;
12983   Vars.reserve(NumVars);
12984   for (unsigned i = 0; i != NumVars; ++i)
12985     Vars.push_back(Record.readSubExpr());
12986   C->setVarRefs(Vars);
12987 
12988   SmallVector<Expr *, 16> UDMappers;
12989   UDMappers.reserve(NumVars);
12990   for (unsigned I = 0; I < NumVars; ++I)
12991     UDMappers.push_back(Record.readSubExpr());
12992   C->setUDMapperRefs(UDMappers);
12993 
12994   SmallVector<ValueDecl *, 16> Decls;
12995   Decls.reserve(UniqueDecls);
12996   for (unsigned i = 0; i < UniqueDecls; ++i)
12997     Decls.push_back(Record.readDeclAs<ValueDecl>());
12998   C->setUniqueDecls(Decls);
12999 
13000   SmallVector<unsigned, 16> ListsPerDecl;
13001   ListsPerDecl.reserve(UniqueDecls);
13002   for (unsigned i = 0; i < UniqueDecls; ++i)
13003     ListsPerDecl.push_back(Record.readInt());
13004   C->setDeclNumLists(ListsPerDecl);
13005 
13006   SmallVector<unsigned, 32> ListSizes;
13007   ListSizes.reserve(TotalLists);
13008   for (unsigned i = 0; i < TotalLists; ++i)
13009     ListSizes.push_back(Record.readInt());
13010   C->setComponentListSizes(ListSizes);
13011 
13012   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13013   Components.reserve(TotalComponents);
13014   for (unsigned i = 0; i < TotalComponents; ++i) {
13015     Expr *AssociatedExpr = Record.readSubExpr();
13016     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13017     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13018         AssociatedExpr, AssociatedDecl));
13019   }
13020   C->setComponents(Components, ListSizes);
13021 }
13022 
13023 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
13024   C->setLParenLoc(Record.readSourceLocation());
13025   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13026   DeclarationNameInfo DNI;
13027   Record.readDeclarationNameInfo(DNI);
13028   C->setMapperIdInfo(DNI);
13029   auto NumVars = C->varlist_size();
13030   auto UniqueDecls = C->getUniqueDeclarationsNum();
13031   auto TotalLists = C->getTotalComponentListNum();
13032   auto TotalComponents = C->getTotalComponentsNum();
13033 
13034   SmallVector<Expr *, 16> Vars;
13035   Vars.reserve(NumVars);
13036   for (unsigned i = 0; i != NumVars; ++i)
13037     Vars.push_back(Record.readSubExpr());
13038   C->setVarRefs(Vars);
13039 
13040   SmallVector<Expr *, 16> UDMappers;
13041   UDMappers.reserve(NumVars);
13042   for (unsigned I = 0; I < NumVars; ++I)
13043     UDMappers.push_back(Record.readSubExpr());
13044   C->setUDMapperRefs(UDMappers);
13045 
13046   SmallVector<ValueDecl *, 16> Decls;
13047   Decls.reserve(UniqueDecls);
13048   for (unsigned i = 0; i < UniqueDecls; ++i)
13049     Decls.push_back(Record.readDeclAs<ValueDecl>());
13050   C->setUniqueDecls(Decls);
13051 
13052   SmallVector<unsigned, 16> ListsPerDecl;
13053   ListsPerDecl.reserve(UniqueDecls);
13054   for (unsigned i = 0; i < UniqueDecls; ++i)
13055     ListsPerDecl.push_back(Record.readInt());
13056   C->setDeclNumLists(ListsPerDecl);
13057 
13058   SmallVector<unsigned, 32> ListSizes;
13059   ListSizes.reserve(TotalLists);
13060   for (unsigned i = 0; i < TotalLists; ++i)
13061     ListSizes.push_back(Record.readInt());
13062   C->setComponentListSizes(ListSizes);
13063 
13064   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13065   Components.reserve(TotalComponents);
13066   for (unsigned i = 0; i < TotalComponents; ++i) {
13067     Expr *AssociatedExpr = Record.readSubExpr();
13068     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13069     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13070         AssociatedExpr, AssociatedDecl));
13071   }
13072   C->setComponents(Components, ListSizes);
13073 }
13074 
13075 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13076   C->setLParenLoc(Record.readSourceLocation());
13077   auto NumVars = C->varlist_size();
13078   auto UniqueDecls = C->getUniqueDeclarationsNum();
13079   auto TotalLists = C->getTotalComponentListNum();
13080   auto TotalComponents = C->getTotalComponentsNum();
13081 
13082   SmallVector<Expr *, 16> Vars;
13083   Vars.reserve(NumVars);
13084   for (unsigned i = 0; i != NumVars; ++i)
13085     Vars.push_back(Record.readSubExpr());
13086   C->setVarRefs(Vars);
13087   Vars.clear();
13088   for (unsigned i = 0; i != NumVars; ++i)
13089     Vars.push_back(Record.readSubExpr());
13090   C->setPrivateCopies(Vars);
13091   Vars.clear();
13092   for (unsigned i = 0; i != NumVars; ++i)
13093     Vars.push_back(Record.readSubExpr());
13094   C->setInits(Vars);
13095 
13096   SmallVector<ValueDecl *, 16> Decls;
13097   Decls.reserve(UniqueDecls);
13098   for (unsigned i = 0; i < UniqueDecls; ++i)
13099     Decls.push_back(Record.readDeclAs<ValueDecl>());
13100   C->setUniqueDecls(Decls);
13101 
13102   SmallVector<unsigned, 16> ListsPerDecl;
13103   ListsPerDecl.reserve(UniqueDecls);
13104   for (unsigned i = 0; i < UniqueDecls; ++i)
13105     ListsPerDecl.push_back(Record.readInt());
13106   C->setDeclNumLists(ListsPerDecl);
13107 
13108   SmallVector<unsigned, 32> ListSizes;
13109   ListSizes.reserve(TotalLists);
13110   for (unsigned i = 0; i < TotalLists; ++i)
13111     ListSizes.push_back(Record.readInt());
13112   C->setComponentListSizes(ListSizes);
13113 
13114   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13115   Components.reserve(TotalComponents);
13116   for (unsigned i = 0; i < TotalComponents; ++i) {
13117     Expr *AssociatedExpr = Record.readSubExpr();
13118     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13119     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13120         AssociatedExpr, AssociatedDecl));
13121   }
13122   C->setComponents(Components, ListSizes);
13123 }
13124 
13125 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13126   C->setLParenLoc(Record.readSourceLocation());
13127   auto NumVars = C->varlist_size();
13128   auto UniqueDecls = C->getUniqueDeclarationsNum();
13129   auto TotalLists = C->getTotalComponentListNum();
13130   auto TotalComponents = C->getTotalComponentsNum();
13131 
13132   SmallVector<Expr *, 16> Vars;
13133   Vars.reserve(NumVars);
13134   for (unsigned i = 0; i != NumVars; ++i)
13135     Vars.push_back(Record.readSubExpr());
13136   C->setVarRefs(Vars);
13137   Vars.clear();
13138 
13139   SmallVector<ValueDecl *, 16> Decls;
13140   Decls.reserve(UniqueDecls);
13141   for (unsigned i = 0; i < UniqueDecls; ++i)
13142     Decls.push_back(Record.readDeclAs<ValueDecl>());
13143   C->setUniqueDecls(Decls);
13144 
13145   SmallVector<unsigned, 16> ListsPerDecl;
13146   ListsPerDecl.reserve(UniqueDecls);
13147   for (unsigned i = 0; i < UniqueDecls; ++i)
13148     ListsPerDecl.push_back(Record.readInt());
13149   C->setDeclNumLists(ListsPerDecl);
13150 
13151   SmallVector<unsigned, 32> ListSizes;
13152   ListSizes.reserve(TotalLists);
13153   for (unsigned i = 0; i < TotalLists; ++i)
13154     ListSizes.push_back(Record.readInt());
13155   C->setComponentListSizes(ListSizes);
13156 
13157   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13158   Components.reserve(TotalComponents);
13159   for (unsigned i = 0; i < TotalComponents; ++i) {
13160     Expr *AssociatedExpr = Record.readSubExpr();
13161     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13162     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13163         AssociatedExpr, AssociatedDecl));
13164   }
13165   C->setComponents(Components, ListSizes);
13166 }
13167