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