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/Bitcode/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
119 #include <algorithm>
120 #include <cassert>
121 #include <cstddef>
122 #include <cstdint>
123 #include <cstdio>
124 #include <ctime>
125 #include <iterator>
126 #include <limits>
127 #include <map>
128 #include <memory>
129 #include <string>
130 #include <system_error>
131 #include <tuple>
132 #include <utility>
133 #include <vector>
134 
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
139 
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
143 
144 bool
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146   return First->ReadFullVersionInformation(FullVersion) ||
147          Second->ReadFullVersionInformation(FullVersion);
148 }
149 
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151   First->ReadModuleName(ModuleName);
152   Second->ReadModuleName(ModuleName);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156   First->ReadModuleMapFile(ModuleMapPath);
157   Second->ReadModuleMapFile(ModuleMapPath);
158 }
159 
160 bool
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
162                                               bool Complain,
163                                               bool AllowCompatibleDifferences) {
164   return First->ReadLanguageOptions(LangOpts, Complain,
165                                     AllowCompatibleDifferences) ||
166          Second->ReadLanguageOptions(LangOpts, Complain,
167                                      AllowCompatibleDifferences);
168 }
169 
170 bool ChainedASTReaderListener::ReadTargetOptions(
171     const TargetOptions &TargetOpts, bool Complain,
172     bool AllowCompatibleDifferences) {
173   return First->ReadTargetOptions(TargetOpts, Complain,
174                                   AllowCompatibleDifferences) ||
175          Second->ReadTargetOptions(TargetOpts, Complain,
176                                    AllowCompatibleDifferences);
177 }
178 
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182          Second->ReadDiagnosticOptions(DiagOpts, Complain);
183 }
184 
185 bool
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
187                                                 bool Complain) {
188   return First->ReadFileSystemOptions(FSOpts, Complain) ||
189          Second->ReadFileSystemOptions(FSOpts, Complain);
190 }
191 
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
194     bool Complain) {
195   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
196                                         Complain) ||
197          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198                                          Complain);
199 }
200 
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202     const PreprocessorOptions &PPOpts, bool Complain,
203     std::string &SuggestedPredefines) {
204   return First->ReadPreprocessorOptions(PPOpts, Complain,
205                                         SuggestedPredefines) ||
206          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
207 }
208 
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
210                                            unsigned Value) {
211   First->ReadCounter(M, Value);
212   Second->ReadCounter(M, Value);
213 }
214 
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216   return First->needsInputFileVisitation() ||
217          Second->needsInputFileVisitation();
218 }
219 
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221   return First->needsSystemInputFileVisitation() ||
222   Second->needsSystemInputFileVisitation();
223 }
224 
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
226                                                ModuleKind Kind) {
227   First->visitModuleFile(Filename, Kind);
228   Second->visitModuleFile(Filename, Kind);
229 }
230 
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
232                                               bool isSystem,
233                                               bool isOverridden,
234                                               bool isExplicitModule) {
235   bool Continue = false;
236   if (First->needsInputFileVisitation() &&
237       (!isSystem || First->needsSystemInputFileVisitation()))
238     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
239                                       isExplicitModule);
240   if (Second->needsInputFileVisitation() &&
241       (!isSystem || Second->needsSystemInputFileVisitation()))
242     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
243                                        isExplicitModule);
244   return Continue;
245 }
246 
247 void ChainedASTReaderListener::readModuleFileExtension(
248        const ModuleFileExtensionMetadata &Metadata) {
249   First->readModuleFileExtension(Metadata);
250   Second->readModuleFileExtension(Metadata);
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
256 
257 ASTReaderListener::~ASTReaderListener() = default;
258 
259 /// Compare the given set of language options against an existing set of
260 /// language options.
261 ///
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 ///        language options will be permitted.
265 ///
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268                                  const LangOptions &ExistingLangOpts,
269                                  DiagnosticsEngine *Diags,
270                                  bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description)                 \
272   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
273     if (Diags)                                                    \
274       Diags->Report(diag::err_pch_langopt_mismatch)               \
275         << Description << LangOpts.Name << ExistingLangOpts.Name; \
276     return true;                                                  \
277   }
278 
279 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
280   if (ExistingLangOpts.Name != LangOpts.Name) {           \
281     if (Diags)                                            \
282       Diags->Report(diag::err_pch_langopt_value_mismatch) \
283         << Description;                                   \
284     return true;                                          \
285   }
286 
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
288   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
289     if (Diags)                                                 \
290       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
291         << Description;                                        \
292     return true;                                               \
293   }
294 
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
296   if (!AllowCompatibleDifferences)                            \
297     LANGOPT(Name, Bits, Default, Description)
298 
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
300   if (!AllowCompatibleDifferences)                                 \
301     ENUM_LANGOPT(Name, Bits, Default, Description)
302 
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304   if (!AllowCompatibleDifferences)                                 \
305     VALUE_LANGOPT(Name, Bits, Default, Description)
306 
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
311 
312   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
313     if (Diags)
314       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
315     return true;
316   }
317 
318   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch)
321       << "target Objective-C runtime";
322     return true;
323   }
324 
325   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326       LangOpts.CommentOpts.BlockCommandNames) {
327     if (Diags)
328       Diags->Report(diag::err_pch_langopt_value_mismatch)
329         << "block command names";
330     return true;
331   }
332 
333   // Sanitizer feature mismatches are treated as compatible differences. If
334   // compatible differences aren't allowed, we still only want to check for
335   // mismatches of non-modular sanitizers (the only ones which can affect AST
336   // generation).
337   if (!AllowCompatibleDifferences) {
338     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341     ExistingSanitizers.clear(ModularSanitizers);
342     ImportedSanitizers.clear(ModularSanitizers);
343     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344       const std::string Flag = "-fsanitize=";
345       if (Diags) {
346 #define SANITIZER(NAME, ID)                                                    \
347   {                                                                            \
348     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
349     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
350     if (InExistingModule != InImportedModule)                                  \
351       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
352           << InExistingModule << (Flag + NAME);                                \
353   }
354 #include "clang/Basic/Sanitizers.def"
355       }
356       return true;
357     }
358   }
359 
360   return false;
361 }
362 
363 /// Compare the given set of target options against an existing set of
364 /// target options.
365 ///
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
367 ///
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370                                const TargetOptions &ExistingTargetOpts,
371                                DiagnosticsEngine *Diags,
372                                bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name)                             \
374   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
375     if (Diags)                                                    \
376       Diags->Report(diag::err_pch_targetopt_mismatch)             \
377         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
378     return true;                                                  \
379   }
380 
381   // The triple and ABI must match exactly.
382   CHECK_TARGET_OPT(Triple, "target");
383   CHECK_TARGET_OPT(ABI, "target ABI");
384 
385   // We can tolerate different CPUs in many cases, notably when one CPU
386   // supports a strict superset of another. When allowing compatible
387   // differences skip this check.
388   if (!AllowCompatibleDifferences)
389     CHECK_TARGET_OPT(CPU, "target CPU");
390 
391 #undef CHECK_TARGET_OPT
392 
393   // Compare feature sets.
394   SmallVector<StringRef, 4> ExistingFeatures(
395                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
396                                              ExistingTargetOpts.FeaturesAsWritten.end());
397   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398                                          TargetOpts.FeaturesAsWritten.end());
399   llvm::sort(ExistingFeatures);
400   llvm::sort(ReadFeatures);
401 
402   // We compute the set difference in both directions explicitly so that we can
403   // diagnose the differences differently.
404   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
405   std::set_difference(
406       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409                       ExistingFeatures.begin(), ExistingFeatures.end(),
410                       std::back_inserter(UnmatchedReadFeatures));
411 
412   // If we are allowing compatible differences and the read feature set is
413   // a strict subset of the existing feature set, there is nothing to diagnose.
414   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
415     return false;
416 
417   if (Diags) {
418     for (StringRef Feature : UnmatchedReadFeatures)
419       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420           << /* is-existing-feature */ false << Feature;
421     for (StringRef Feature : UnmatchedExistingFeatures)
422       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423           << /* is-existing-feature */ true << Feature;
424   }
425 
426   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
427 }
428 
429 bool
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
431                                   bool Complain,
432                                   bool AllowCompatibleDifferences) {
433   const LangOptions &ExistingLangOpts = PP.getLangOpts();
434   return checkLanguageOptions(LangOpts, ExistingLangOpts,
435                               Complain ? &Reader.Diags : nullptr,
436                               AllowCompatibleDifferences);
437 }
438 
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
440                                      bool Complain,
441                                      bool AllowCompatibleDifferences) {
442   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444                             Complain ? &Reader.Diags : nullptr,
445                             AllowCompatibleDifferences);
446 }
447 
448 namespace {
449 
450 using MacroDefinitionsMap =
451     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
453 
454 } // namespace
455 
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457                                          DiagnosticsEngine &Diags,
458                                          bool Complain) {
459   using Level = DiagnosticsEngine::Level;
460 
461   // Check current mappings for new -Werror mappings, and the stored mappings
462   // for cases that were explicitly mapped to *not* be errors that are now
463   // errors because of options like -Werror.
464   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
465 
466   for (DiagnosticsEngine *MappingSource : MappingSources) {
467     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468       diag::kind DiagID = DiagIDMappingPair.first;
469       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470       if (CurLevel < DiagnosticsEngine::Error)
471         continue; // not significant
472       Level StoredLevel =
473           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474       if (StoredLevel < DiagnosticsEngine::Error) {
475         if (Complain)
476           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
478         return true;
479       }
480     }
481   }
482 
483   return false;
484 }
485 
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
489     return true;
490   return Ext >= diag::Severity::Error;
491 }
492 
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494                                     DiagnosticsEngine &Diags,
495                                     bool IsSystem, bool Complain) {
496   // Top-level options
497   if (IsSystem) {
498     if (Diags.getSuppressSystemWarnings())
499       return false;
500     // If -Wsystem-headers was not enabled before, be conservative
501     if (StoredDiags.getSuppressSystemWarnings()) {
502       if (Complain)
503         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
504       return true;
505     }
506   }
507 
508   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
509     if (Complain)
510       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
511     return true;
512   }
513 
514   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515       !StoredDiags.getEnableAllWarnings()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
518     return true;
519   }
520 
521   if (isExtHandlingFromDiagsError(Diags) &&
522       !isExtHandlingFromDiagsError(StoredDiags)) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
525     return true;
526   }
527 
528   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
529 }
530 
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
533                                           Preprocessor &PP) {
534   // If the original import came from a file explicitly generated by the user,
535   // don't check the diagnostic mappings.
536   // FIXME: currently this is approximated by checking whether this is not a
537   // module import of an implicitly-loaded module file.
538   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539   // the transitive closure of its imports, since unrelated modules cannot be
540   // imported until after this module finishes validation.
541   ModuleFile *TopImport = &*ModuleMgr.rbegin();
542   while (!TopImport->ImportedBy.empty())
543     TopImport = TopImport->ImportedBy[0];
544   if (TopImport->Kind != MK_ImplicitModule)
545     return nullptr;
546 
547   StringRef ModuleName = TopImport->ModuleName;
548   assert(!ModuleName.empty() && "diagnostic options read before module name");
549 
550   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551   assert(M && "missing module");
552   return M;
553 }
554 
555 bool PCHValidator::ReadDiagnosticOptions(
556     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561   // This should never fail, because we would have processed these options
562   // before writing them to an ASTFile.
563   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
564 
565   ModuleManager &ModuleMgr = Reader.getModuleManager();
566   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
567 
568   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
569   if (!TopM)
570     return false;
571 
572   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573   // contains the union of their flags.
574   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
575                                  Complain);
576 }
577 
578 /// Collect the macro definitions provided by the given preprocessor
579 /// options.
580 static void
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582                         MacroDefinitionsMap &Macros,
583                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585     StringRef Macro = PPOpts.Macros[I].first;
586     bool IsUndef = PPOpts.Macros[I].second;
587 
588     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589     StringRef MacroName = MacroPair.first;
590     StringRef MacroBody = MacroPair.second;
591 
592     // For an #undef'd macro, we only care about the name.
593     if (IsUndef) {
594       if (MacroNames && !Macros.count(MacroName))
595         MacroNames->push_back(MacroName);
596 
597       Macros[MacroName] = std::make_pair("", true);
598       continue;
599     }
600 
601     // For a #define'd macro, figure out the actual definition.
602     if (MacroName.size() == Macro.size())
603       MacroBody = "1";
604     else {
605       // Note: GCC drops anything following an end-of-line character.
606       StringRef::size_type End = MacroBody.find_first_of("\n\r");
607       MacroBody = MacroBody.substr(0, End);
608     }
609 
610     if (MacroNames && !Macros.count(MacroName))
611       MacroNames->push_back(MacroName);
612     Macros[MacroName] = std::make_pair(MacroBody, false);
613   }
614 }
615 
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
618 ///
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 ///        macros defined by \p ExistingPPOpts to override those defined by
622 ///        \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624                                      const PreprocessorOptions &ExistingPPOpts,
625                                      DiagnosticsEngine *Diags,
626                                      FileManager &FileMgr,
627                                      std::string &SuggestedPredefines,
628                                      const LangOptions &LangOpts,
629                                      bool Validate = true) {
630   // Check macro definitions.
631   MacroDefinitionsMap ASTFileMacros;
632   collectMacroDefinitions(PPOpts, ASTFileMacros);
633   MacroDefinitionsMap ExistingMacros;
634   SmallVector<StringRef, 4> ExistingMacroNames;
635   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
636 
637   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638     // Dig out the macro definition in the existing preprocessor options.
639     StringRef MacroName = ExistingMacroNames[I];
640     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
641 
642     // Check whether we know anything about this macro name or not.
643     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644         ASTFileMacros.find(MacroName);
645     if (!Validate || Known == ASTFileMacros.end()) {
646       // FIXME: Check whether this identifier was referenced anywhere in the
647       // AST file. If so, we should reject the AST file. Unfortunately, this
648       // information isn't in the control block. What shall we do about it?
649 
650       if (Existing.second) {
651         SuggestedPredefines += "#undef ";
652         SuggestedPredefines += MacroName.str();
653         SuggestedPredefines += '\n';
654       } else {
655         SuggestedPredefines += "#define ";
656         SuggestedPredefines += MacroName.str();
657         SuggestedPredefines += ' ';
658         SuggestedPredefines += Existing.first.str();
659         SuggestedPredefines += '\n';
660       }
661       continue;
662     }
663 
664     // If the macro was defined in one but undef'd in the other, we have a
665     // conflict.
666     if (Existing.second != Known->second.second) {
667       if (Diags) {
668         Diags->Report(diag::err_pch_macro_def_undef)
669           << MacroName << Known->second.second;
670       }
671       return true;
672     }
673 
674     // If the macro was #undef'd in both, or if the macro bodies are identical,
675     // it's fine.
676     if (Existing.second || Existing.first == Known->second.first)
677       continue;
678 
679     // The macro bodies differ; complain.
680     if (Diags) {
681       Diags->Report(diag::err_pch_macro_def_conflict)
682         << MacroName << Known->second.first << Existing.first;
683     }
684     return true;
685   }
686 
687   // Check whether we're using predefines.
688   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
689     if (Diags) {
690       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
691     }
692     return true;
693   }
694 
695   // Detailed record is important since it is used for the module cache hash.
696   if (LangOpts.Modules &&
697       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
700     }
701     return true;
702   }
703 
704   // Compute the #include and #include_macros lines we need.
705   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706     StringRef File = ExistingPPOpts.Includes[I];
707 
708     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709         !ExistingPPOpts.PCHThroughHeader.empty()) {
710       // In case the through header is an include, we must add all the includes
711       // to the predefines so the start point can be determined.
712       SuggestedPredefines += "#include \"";
713       SuggestedPredefines += File;
714       SuggestedPredefines += "\"\n";
715       continue;
716     }
717 
718     if (File == ExistingPPOpts.ImplicitPCHInclude)
719       continue;
720 
721     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722           != PPOpts.Includes.end())
723       continue;
724 
725     SuggestedPredefines += "#include \"";
726     SuggestedPredefines += File;
727     SuggestedPredefines += "\"\n";
728   }
729 
730   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731     StringRef File = ExistingPPOpts.MacroIncludes[I];
732     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
733                   File)
734         != PPOpts.MacroIncludes.end())
735       continue;
736 
737     SuggestedPredefines += "#__include_macros \"";
738     SuggestedPredefines += File;
739     SuggestedPredefines += "\"\n##\n";
740   }
741 
742   return false;
743 }
744 
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
746                                            bool Complain,
747                                            std::string &SuggestedPredefines) {
748   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
749 
750   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751                                   Complain? &Reader.Diags : nullptr,
752                                   PP.getFileManager(),
753                                   SuggestedPredefines,
754                                   PP.getLangOpts());
755 }
756 
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758                                   const PreprocessorOptions &PPOpts,
759                                   bool Complain,
760                                   std::string &SuggestedPredefines) {
761   return checkPreprocessorOptions(PPOpts,
762                                   PP.getPreprocessorOpts(),
763                                   nullptr,
764                                   PP.getFileManager(),
765                                   SuggestedPredefines,
766                                   PP.getLangOpts(),
767                                   false);
768 }
769 
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
772 ///
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775                                      StringRef SpecificModuleCachePath,
776                                      StringRef ExistingModuleCachePath,
777                                      DiagnosticsEngine *Diags,
778                                      const LangOptions &LangOpts) {
779   if (LangOpts.Modules) {
780     if (SpecificModuleCachePath != ExistingModuleCachePath) {
781       if (Diags)
782         Diags->Report(diag::err_pch_modulecache_mismatch)
783           << SpecificModuleCachePath << ExistingModuleCachePath;
784       return true;
785     }
786   }
787 
788   return false;
789 }
790 
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792                                            StringRef SpecificModuleCachePath,
793                                            bool Complain) {
794   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795                                   PP.getHeaderSearchInfo().getModuleCachePath(),
796                                   Complain ? &Reader.Diags : nullptr,
797                                   PP.getLangOpts());
798 }
799 
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801   PP.setCounterValue(Value);
802 }
803 
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
807 
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809                                            bool TakeOwnership) {
810   DeserializationListener = Listener;
811   OwnsDeserializationListener = TakeOwnership;
812 }
813 
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815   return serialization::ComputeHash(Sel);
816 }
817 
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820   using namespace llvm::support;
821 
822   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824   return std::make_pair(KeyLen, DataLen);
825 }
826 
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829   using namespace llvm::support;
830 
831   SelectorTable &SelTable = Reader.getContext().Selectors;
832   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834       F, endian::readNext<uint32_t, little, unaligned>(d));
835   if (N == 0)
836     return SelTable.getNullarySelector(FirstII);
837   else if (N == 1)
838     return SelTable.getUnarySelector(FirstII);
839 
840   SmallVector<IdentifierInfo *, 16> Args;
841   Args.push_back(FirstII);
842   for (unsigned I = 1; I != N; ++I)
843     Args.push_back(Reader.getLocalIdentifier(
844         F, endian::readNext<uint32_t, little, unaligned>(d)));
845 
846   return SelTable.getSelector(N, Args.data());
847 }
848 
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
851                                  unsigned DataLen) {
852   using namespace llvm::support;
853 
854   data_type Result;
855 
856   Result.ID = Reader.getGlobalSelectorID(
857       F, endian::readNext<uint32_t, little, unaligned>(d));
858   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860   Result.InstanceBits = FullInstanceBits & 0x3;
861   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862   Result.FactoryBits = FullFactoryBits & 0x3;
863   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864   unsigned NumInstanceMethods = FullInstanceBits >> 3;
865   unsigned NumFactoryMethods = FullFactoryBits >> 3;
866 
867   // Load instance methods
868   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870             F, endian::readNext<uint32_t, little, unaligned>(d)))
871       Result.Instance.push_back(Method);
872   }
873 
874   // Load factory methods
875   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Factory.push_back(Method);
879   }
880 
881   return Result;
882 }
883 
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885   return llvm::djbHash(a);
886 }
887 
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890   using namespace llvm::support;
891 
892   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894   return std::make_pair(KeyLen, DataLen);
895 }
896 
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899   assert(n >= 2 && d[n-1] == '\0');
900   return StringRef((const char*) d, n-1);
901 }
902 
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
905                                     bool IsModule) {
906   return II.hadMacroDefinition() ||
907          II.isPoisoned() ||
908          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909          II.hasRevertedTokenIDToIdentifier() ||
910          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911           II.getFETokenInfo());
912 }
913 
914 static bool readBit(unsigned &Bits) {
915   bool Value = Bits & 0x1;
916   Bits >>= 1;
917   return Value;
918 }
919 
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921   using namespace llvm::support;
922 
923   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924   return Reader.getGlobalIdentifierID(F, RawID >> 1);
925 }
926 
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928   if (!II.isFromAST()) {
929     II.setIsFromAST();
930     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931     if (isInterestingIdentifier(Reader, II, IsModule))
932       II.setChangedSinceDeserialization();
933   }
934 }
935 
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937                                                    const unsigned char* d,
938                                                    unsigned DataLen) {
939   using namespace llvm::support;
940 
941   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942   bool IsInteresting = RawID & 0x01;
943 
944   // Wipe out the "is interesting" bit.
945   RawID = RawID >> 1;
946 
947   // Build the IdentifierInfo and link the identifier ID with it.
948   IdentifierInfo *II = KnownII;
949   if (!II) {
950     II = &Reader.getIdentifierTable().getOwn(k);
951     KnownII = II;
952   }
953   markIdentifierFromAST(Reader, *II);
954   Reader.markIdentifierUpToDate(II);
955 
956   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957   if (!IsInteresting) {
958     // For uninteresting identifiers, there's nothing else to do. Just notify
959     // the reader that we've finished loading this identifier.
960     Reader.SetIdentifierInfo(ID, II);
961     return II;
962   }
963 
964   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966   bool CPlusPlusOperatorKeyword = readBit(Bits);
967   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968   bool HasRevertedBuiltin = readBit(Bits);
969   bool Poisoned = readBit(Bits);
970   bool ExtensionToken = readBit(Bits);
971   bool HadMacroDefinition = readBit(Bits);
972 
973   assert(Bits == 0 && "Extra bits in the identifier?");
974   DataLen -= 8;
975 
976   // Set or check the various bits in the IdentifierInfo structure.
977   // Token IDs are read-only.
978   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979     II->revertTokenIDToIdentifier();
980   if (!F.isModule())
981     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982   else if (HasRevertedBuiltin && II->getBuiltinID()) {
983     II->revertBuiltin();
984     assert((II->hasRevertedBuiltin() ||
985             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986            "Incorrect ObjC keyword or builtin ID");
987   }
988   assert(II->isExtensionToken() == ExtensionToken &&
989          "Incorrect extension token flag");
990   (void)ExtensionToken;
991   if (Poisoned)
992     II->setIsPoisoned(true);
993   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994          "Incorrect C++ operator keyword flag");
995   (void)CPlusPlusOperatorKeyword;
996 
997   // If this identifier is a macro, deserialize the macro
998   // definition.
999   if (HadMacroDefinition) {
1000     uint32_t MacroDirectivesOffset =
1001         endian::readNext<uint32_t, little, unaligned>(d);
1002     DataLen -= 4;
1003 
1004     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1005   }
1006 
1007   Reader.SetIdentifierInfo(ID, II);
1008 
1009   // Read all of the declarations visible at global scope with this
1010   // name.
1011   if (DataLen > 0) {
1012     SmallVector<uint32_t, 4> DeclIDs;
1013     for (; DataLen > 0; DataLen -= 4)
1014       DeclIDs.push_back(Reader.getGlobalDeclID(
1015           F, endian::readNext<uint32_t, little, unaligned>(d)));
1016     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1017   }
1018 
1019   return II;
1020 }
1021 
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023     : Kind(Name.getNameKind()) {
1024   switch (Kind) {
1025   case DeclarationName::Identifier:
1026     Data = (uint64_t)Name.getAsIdentifierInfo();
1027     break;
1028   case DeclarationName::ObjCZeroArgSelector:
1029   case DeclarationName::ObjCOneArgSelector:
1030   case DeclarationName::ObjCMultiArgSelector:
1031     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1032     break;
1033   case DeclarationName::CXXOperatorName:
1034     Data = Name.getCXXOverloadedOperator();
1035     break;
1036   case DeclarationName::CXXLiteralOperatorName:
1037     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1038     break;
1039   case DeclarationName::CXXDeductionGuideName:
1040     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041                ->getDeclName().getAsIdentifierInfo();
1042     break;
1043   case DeclarationName::CXXConstructorName:
1044   case DeclarationName::CXXDestructorName:
1045   case DeclarationName::CXXConversionFunctionName:
1046   case DeclarationName::CXXUsingDirective:
1047     Data = 0;
1048     break;
1049   }
1050 }
1051 
1052 unsigned DeclarationNameKey::getHash() const {
1053   llvm::FoldingSetNodeID ID;
1054   ID.AddInteger(Kind);
1055 
1056   switch (Kind) {
1057   case DeclarationName::Identifier:
1058   case DeclarationName::CXXLiteralOperatorName:
1059   case DeclarationName::CXXDeductionGuideName:
1060     ID.AddString(((IdentifierInfo*)Data)->getName());
1061     break;
1062   case DeclarationName::ObjCZeroArgSelector:
1063   case DeclarationName::ObjCOneArgSelector:
1064   case DeclarationName::ObjCMultiArgSelector:
1065     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1066     break;
1067   case DeclarationName::CXXOperatorName:
1068     ID.AddInteger((OverloadedOperatorKind)Data);
1069     break;
1070   case DeclarationName::CXXConstructorName:
1071   case DeclarationName::CXXDestructorName:
1072   case DeclarationName::CXXConversionFunctionName:
1073   case DeclarationName::CXXUsingDirective:
1074     break;
1075   }
1076 
1077   return ID.ComputeHash();
1078 }
1079 
1080 ModuleFile *
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082   using namespace llvm::support;
1083 
1084   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085   return Reader.getLocalModuleFile(F, ModuleFileID);
1086 }
1087 
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090   using namespace llvm::support;
1091 
1092   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094   return std::make_pair(KeyLen, DataLen);
1095 }
1096 
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099   using namespace llvm::support;
1100 
1101   auto Kind = (DeclarationName::NameKind)*d++;
1102   uint64_t Data;
1103   switch (Kind) {
1104   case DeclarationName::Identifier:
1105   case DeclarationName::CXXLiteralOperatorName:
1106   case DeclarationName::CXXDeductionGuideName:
1107     Data = (uint64_t)Reader.getLocalIdentifier(
1108         F, endian::readNext<uint32_t, little, unaligned>(d));
1109     break;
1110   case DeclarationName::ObjCZeroArgSelector:
1111   case DeclarationName::ObjCOneArgSelector:
1112   case DeclarationName::ObjCMultiArgSelector:
1113     Data =
1114         (uint64_t)Reader.getLocalSelector(
1115                              F, endian::readNext<uint32_t, little, unaligned>(
1116                                     d)).getAsOpaquePtr();
1117     break;
1118   case DeclarationName::CXXOperatorName:
1119     Data = *d++; // OverloadedOperatorKind
1120     break;
1121   case DeclarationName::CXXConstructorName:
1122   case DeclarationName::CXXDestructorName:
1123   case DeclarationName::CXXConversionFunctionName:
1124   case DeclarationName::CXXUsingDirective:
1125     Data = 0;
1126     break;
1127   }
1128 
1129   return DeclarationNameKey(Kind, Data);
1130 }
1131 
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133                                                  const unsigned char *d,
1134                                                  unsigned DataLen,
1135                                                  data_type_builder &Val) {
1136   using namespace llvm::support;
1137 
1138   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1141   }
1142 }
1143 
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145                                               BitstreamCursor &Cursor,
1146                                               uint64_t Offset,
1147                                               DeclContext *DC) {
1148   assert(Offset != 0);
1149 
1150   SavedStreamPosition SavedPosition(Cursor);
1151   Cursor.JumpToBit(Offset);
1152 
1153   RecordData Record;
1154   StringRef Blob;
1155   unsigned Code = Cursor.ReadCode();
1156   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1157   if (RecCode != DECL_CONTEXT_LEXICAL) {
1158     Error("Expected lexical block");
1159     return true;
1160   }
1161 
1162   assert(!isa<TranslationUnitDecl>(DC) &&
1163          "expected a TU_UPDATE_LEXICAL record for TU");
1164   // If we are handling a C++ class template instantiation, we can see multiple
1165   // lexical updates for the same record. It's important that we select only one
1166   // of them, so that field numbering works properly. Just pick the first one we
1167   // see.
1168   auto &Lex = LexicalDecls[DC];
1169   if (!Lex.first) {
1170     Lex = std::make_pair(
1171         &M, llvm::makeArrayRef(
1172                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1173                     Blob.data()),
1174                 Blob.size() / 4));
1175   }
1176   DC->setHasExternalLexicalStorage(true);
1177   return false;
1178 }
1179 
1180 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1181                                               BitstreamCursor &Cursor,
1182                                               uint64_t Offset,
1183                                               DeclID ID) {
1184   assert(Offset != 0);
1185 
1186   SavedStreamPosition SavedPosition(Cursor);
1187   Cursor.JumpToBit(Offset);
1188 
1189   RecordData Record;
1190   StringRef Blob;
1191   unsigned Code = Cursor.ReadCode();
1192   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1193   if (RecCode != DECL_CONTEXT_VISIBLE) {
1194     Error("Expected visible lookup table block");
1195     return true;
1196   }
1197 
1198   // We can't safely determine the primary context yet, so delay attaching the
1199   // lookup table until we're done with recursive deserialization.
1200   auto *Data = (const unsigned char*)Blob.data();
1201   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1202   return false;
1203 }
1204 
1205 void ASTReader::Error(StringRef Msg) const {
1206   Error(diag::err_fe_pch_malformed, Msg);
1207   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1208       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1209     Diag(diag::note_module_cache_path)
1210       << PP.getHeaderSearchInfo().getModuleCachePath();
1211   }
1212 }
1213 
1214 void ASTReader::Error(unsigned DiagID,
1215                       StringRef Arg1, StringRef Arg2) const {
1216   if (Diags.isDiagnosticInFlight())
1217     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1218   else
1219     Diag(DiagID) << Arg1 << Arg2;
1220 }
1221 
1222 //===----------------------------------------------------------------------===//
1223 // Source Manager Deserialization
1224 //===----------------------------------------------------------------------===//
1225 
1226 /// Read the line table in the source manager block.
1227 /// \returns true if there was an error.
1228 bool ASTReader::ParseLineTable(ModuleFile &F,
1229                                const RecordData &Record) {
1230   unsigned Idx = 0;
1231   LineTableInfo &LineTable = SourceMgr.getLineTable();
1232 
1233   // Parse the file names
1234   std::map<int, int> FileIDs;
1235   FileIDs[-1] = -1; // For unspecified filenames.
1236   for (unsigned I = 0; Record[Idx]; ++I) {
1237     // Extract the file name
1238     auto Filename = ReadPath(F, Record, Idx);
1239     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1240   }
1241   ++Idx;
1242 
1243   // Parse the line entries
1244   std::vector<LineEntry> Entries;
1245   while (Idx < Record.size()) {
1246     int FID = Record[Idx++];
1247     assert(FID >= 0 && "Serialized line entries for non-local file.");
1248     // Remap FileID from 1-based old view.
1249     FID += F.SLocEntryBaseID - 1;
1250 
1251     // Extract the line entries
1252     unsigned NumEntries = Record[Idx++];
1253     assert(NumEntries && "no line entries for file ID");
1254     Entries.clear();
1255     Entries.reserve(NumEntries);
1256     for (unsigned I = 0; I != NumEntries; ++I) {
1257       unsigned FileOffset = Record[Idx++];
1258       unsigned LineNo = Record[Idx++];
1259       int FilenameID = FileIDs[Record[Idx++]];
1260       SrcMgr::CharacteristicKind FileKind
1261         = (SrcMgr::CharacteristicKind)Record[Idx++];
1262       unsigned IncludeOffset = Record[Idx++];
1263       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1264                                        FileKind, IncludeOffset));
1265     }
1266     LineTable.AddEntry(FileID::get(FID), Entries);
1267   }
1268 
1269   return false;
1270 }
1271 
1272 /// Read a source manager block
1273 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1274   using namespace SrcMgr;
1275 
1276   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1277 
1278   // Set the source-location entry cursor to the current position in
1279   // the stream. This cursor will be used to read the contents of the
1280   // source manager block initially, and then lazily read
1281   // source-location entries as needed.
1282   SLocEntryCursor = F.Stream;
1283 
1284   // The stream itself is going to skip over the source manager block.
1285   if (F.Stream.SkipBlock()) {
1286     Error("malformed block record in AST file");
1287     return true;
1288   }
1289 
1290   // Enter the source manager block.
1291   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1292     Error("malformed source manager block record in AST file");
1293     return true;
1294   }
1295 
1296   RecordData Record;
1297   while (true) {
1298     llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1299 
1300     switch (E.Kind) {
1301     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1302     case llvm::BitstreamEntry::Error:
1303       Error("malformed block record in AST file");
1304       return true;
1305     case llvm::BitstreamEntry::EndBlock:
1306       return false;
1307     case llvm::BitstreamEntry::Record:
1308       // The interesting case.
1309       break;
1310     }
1311 
1312     // Read a record.
1313     Record.clear();
1314     StringRef Blob;
1315     switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1316     default:  // Default behavior: ignore.
1317       break;
1318 
1319     case SM_SLOC_FILE_ENTRY:
1320     case SM_SLOC_BUFFER_ENTRY:
1321     case SM_SLOC_EXPANSION_ENTRY:
1322       // Once we hit one of the source location entries, we're done.
1323       return false;
1324     }
1325   }
1326 }
1327 
1328 /// If a header file is not found at the path that we expect it to be
1329 /// and the PCH file was moved from its original location, try to resolve the
1330 /// file by assuming that header+PCH were moved together and the header is in
1331 /// the same place relative to the PCH.
1332 static std::string
1333 resolveFileRelativeToOriginalDir(const std::string &Filename,
1334                                  const std::string &OriginalDir,
1335                                  const std::string &CurrDir) {
1336   assert(OriginalDir != CurrDir &&
1337          "No point trying to resolve the file if the PCH dir didn't change");
1338 
1339   using namespace llvm::sys;
1340 
1341   SmallString<128> filePath(Filename);
1342   fs::make_absolute(filePath);
1343   assert(path::is_absolute(OriginalDir));
1344   SmallString<128> currPCHPath(CurrDir);
1345 
1346   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1347                        fileDirE = path::end(path::parent_path(filePath));
1348   path::const_iterator origDirI = path::begin(OriginalDir),
1349                        origDirE = path::end(OriginalDir);
1350   // Skip the common path components from filePath and OriginalDir.
1351   while (fileDirI != fileDirE && origDirI != origDirE &&
1352          *fileDirI == *origDirI) {
1353     ++fileDirI;
1354     ++origDirI;
1355   }
1356   for (; origDirI != origDirE; ++origDirI)
1357     path::append(currPCHPath, "..");
1358   path::append(currPCHPath, fileDirI, fileDirE);
1359   path::append(currPCHPath, path::filename(Filename));
1360   return currPCHPath.str();
1361 }
1362 
1363 bool ASTReader::ReadSLocEntry(int ID) {
1364   if (ID == 0)
1365     return false;
1366 
1367   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1368     Error("source location entry ID out-of-range for AST file");
1369     return true;
1370   }
1371 
1372   // Local helper to read the (possibly-compressed) buffer data following the
1373   // entry record.
1374   auto ReadBuffer = [this](
1375       BitstreamCursor &SLocEntryCursor,
1376       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1377     RecordData Record;
1378     StringRef Blob;
1379     unsigned Code = SLocEntryCursor.ReadCode();
1380     unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1381 
1382     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1383       if (!llvm::zlib::isAvailable()) {
1384         Error("zlib is not available");
1385         return nullptr;
1386       }
1387       SmallString<0> Uncompressed;
1388       if (llvm::Error E =
1389               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1390         Error("could not decompress embedded file contents: " +
1391               llvm::toString(std::move(E)));
1392         return nullptr;
1393       }
1394       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1395     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1396       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1397     } else {
1398       Error("AST record has invalid code");
1399       return nullptr;
1400     }
1401   };
1402 
1403   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1404   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1405   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1406   unsigned BaseOffset = F->SLocEntryBaseOffset;
1407 
1408   ++NumSLocEntriesRead;
1409   llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1410   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1411     Error("incorrectly-formatted source location entry in AST file");
1412     return true;
1413   }
1414 
1415   RecordData Record;
1416   StringRef Blob;
1417   switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1418   default:
1419     Error("incorrectly-formatted source location entry in AST file");
1420     return true;
1421 
1422   case SM_SLOC_FILE_ENTRY: {
1423     // We will detect whether a file changed and return 'Failure' for it, but
1424     // we will also try to fail gracefully by setting up the SLocEntry.
1425     unsigned InputID = Record[4];
1426     InputFile IF = getInputFile(*F, InputID);
1427     const FileEntry *File = IF.getFile();
1428     bool OverriddenBuffer = IF.isOverridden();
1429 
1430     // Note that we only check if a File was returned. If it was out-of-date
1431     // we have complained but we will continue creating a FileID to recover
1432     // gracefully.
1433     if (!File)
1434       return true;
1435 
1436     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1437     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1438       // This is the module's main file.
1439       IncludeLoc = getImportLocation(F);
1440     }
1441     SrcMgr::CharacteristicKind
1442       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1443     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1444                                         ID, BaseOffset + Record[0]);
1445     SrcMgr::FileInfo &FileInfo =
1446           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1447     FileInfo.NumCreatedFIDs = Record[5];
1448     if (Record[3])
1449       FileInfo.setHasLineDirectives();
1450 
1451     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1452     unsigned NumFileDecls = Record[7];
1453     if (NumFileDecls && ContextObj) {
1454       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1455       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1456                                                              NumFileDecls));
1457     }
1458 
1459     const SrcMgr::ContentCache *ContentCache
1460       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1461     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1462         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1463         !ContentCache->getRawBuffer()) {
1464       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1465       if (!Buffer)
1466         return true;
1467       SourceMgr.overrideFileContents(File, std::move(Buffer));
1468     }
1469 
1470     break;
1471   }
1472 
1473   case SM_SLOC_BUFFER_ENTRY: {
1474     const char *Name = Blob.data();
1475     unsigned Offset = Record[0];
1476     SrcMgr::CharacteristicKind
1477       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1478     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1479     if (IncludeLoc.isInvalid() && F->isModule()) {
1480       IncludeLoc = getImportLocation(F);
1481     }
1482 
1483     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1484     if (!Buffer)
1485       return true;
1486     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1487                            BaseOffset + Offset, IncludeLoc);
1488     break;
1489   }
1490 
1491   case SM_SLOC_EXPANSION_ENTRY: {
1492     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1493     SourceMgr.createExpansionLoc(SpellingLoc,
1494                                      ReadSourceLocation(*F, Record[2]),
1495                                      ReadSourceLocation(*F, Record[3]),
1496                                      Record[5],
1497                                      Record[4],
1498                                      ID,
1499                                      BaseOffset + Record[0]);
1500     break;
1501   }
1502   }
1503 
1504   return false;
1505 }
1506 
1507 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1508   if (ID == 0)
1509     return std::make_pair(SourceLocation(), "");
1510 
1511   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1512     Error("source location entry ID out-of-range for AST file");
1513     return std::make_pair(SourceLocation(), "");
1514   }
1515 
1516   // Find which module file this entry lands in.
1517   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1518   if (!M->isModule())
1519     return std::make_pair(SourceLocation(), "");
1520 
1521   // FIXME: Can we map this down to a particular submodule? That would be
1522   // ideal.
1523   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1524 }
1525 
1526 /// Find the location where the module F is imported.
1527 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1528   if (F->ImportLoc.isValid())
1529     return F->ImportLoc;
1530 
1531   // Otherwise we have a PCH. It's considered to be "imported" at the first
1532   // location of its includer.
1533   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1534     // Main file is the importer.
1535     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1536     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1537   }
1538   return F->ImportedBy[0]->FirstLoc;
1539 }
1540 
1541 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1542 /// specified cursor.  Read the abbreviations that are at the top of the block
1543 /// and then leave the cursor pointing into the block.
1544 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1545   if (Cursor.EnterSubBlock(BlockID))
1546     return true;
1547 
1548   while (true) {
1549     uint64_t Offset = Cursor.GetCurrentBitNo();
1550     unsigned Code = Cursor.ReadCode();
1551 
1552     // We expect all abbrevs to be at the start of the block.
1553     if (Code != llvm::bitc::DEFINE_ABBREV) {
1554       Cursor.JumpToBit(Offset);
1555       return false;
1556     }
1557     Cursor.ReadAbbrevRecord();
1558   }
1559 }
1560 
1561 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1562                            unsigned &Idx) {
1563   Token Tok;
1564   Tok.startToken();
1565   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1566   Tok.setLength(Record[Idx++]);
1567   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1568     Tok.setIdentifierInfo(II);
1569   Tok.setKind((tok::TokenKind)Record[Idx++]);
1570   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1571   return Tok;
1572 }
1573 
1574 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1575   BitstreamCursor &Stream = F.MacroCursor;
1576 
1577   // Keep track of where we are in the stream, then jump back there
1578   // after reading this macro.
1579   SavedStreamPosition SavedPosition(Stream);
1580 
1581   Stream.JumpToBit(Offset);
1582   RecordData Record;
1583   SmallVector<IdentifierInfo*, 16> MacroParams;
1584   MacroInfo *Macro = nullptr;
1585 
1586   while (true) {
1587     // Advance to the next record, but if we get to the end of the block, don't
1588     // pop it (removing all the abbreviations from the cursor) since we want to
1589     // be able to reseek within the block and read entries.
1590     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1591     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1592 
1593     switch (Entry.Kind) {
1594     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1595     case llvm::BitstreamEntry::Error:
1596       Error("malformed block record in AST file");
1597       return Macro;
1598     case llvm::BitstreamEntry::EndBlock:
1599       return Macro;
1600     case llvm::BitstreamEntry::Record:
1601       // The interesting case.
1602       break;
1603     }
1604 
1605     // Read a record.
1606     Record.clear();
1607     PreprocessorRecordTypes RecType =
1608       (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1609     switch (RecType) {
1610     case PP_MODULE_MACRO:
1611     case PP_MACRO_DIRECTIVE_HISTORY:
1612       return Macro;
1613 
1614     case PP_MACRO_OBJECT_LIKE:
1615     case PP_MACRO_FUNCTION_LIKE: {
1616       // If we already have a macro, that means that we've hit the end
1617       // of the definition of the macro we were looking for. We're
1618       // done.
1619       if (Macro)
1620         return Macro;
1621 
1622       unsigned NextIndex = 1; // Skip identifier ID.
1623       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1624       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1625       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1626       MI->setIsUsed(Record[NextIndex++]);
1627       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1628 
1629       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1630         // Decode function-like macro info.
1631         bool isC99VarArgs = Record[NextIndex++];
1632         bool isGNUVarArgs = Record[NextIndex++];
1633         bool hasCommaPasting = Record[NextIndex++];
1634         MacroParams.clear();
1635         unsigned NumArgs = Record[NextIndex++];
1636         for (unsigned i = 0; i != NumArgs; ++i)
1637           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1638 
1639         // Install function-like macro info.
1640         MI->setIsFunctionLike();
1641         if (isC99VarArgs) MI->setIsC99Varargs();
1642         if (isGNUVarArgs) MI->setIsGNUVarargs();
1643         if (hasCommaPasting) MI->setHasCommaPasting();
1644         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1645       }
1646 
1647       // Remember that we saw this macro last so that we add the tokens that
1648       // form its body to it.
1649       Macro = MI;
1650 
1651       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1652           Record[NextIndex]) {
1653         // We have a macro definition. Register the association
1654         PreprocessedEntityID
1655             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1656         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1657         PreprocessingRecord::PPEntityID PPID =
1658             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1659         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1660             PPRec.getPreprocessedEntity(PPID));
1661         if (PPDef)
1662           PPRec.RegisterMacroDefinition(Macro, PPDef);
1663       }
1664 
1665       ++NumMacrosRead;
1666       break;
1667     }
1668 
1669     case PP_TOKEN: {
1670       // If we see a TOKEN before a PP_MACRO_*, then the file is
1671       // erroneous, just pretend we didn't see this.
1672       if (!Macro) break;
1673 
1674       unsigned Idx = 0;
1675       Token Tok = ReadToken(F, Record, Idx);
1676       Macro->AddTokenToBody(Tok);
1677       break;
1678     }
1679     }
1680   }
1681 }
1682 
1683 PreprocessedEntityID
1684 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1685                                          unsigned LocalID) const {
1686   if (!M.ModuleOffsetMap.empty())
1687     ReadModuleOffsetMap(M);
1688 
1689   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1690     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1691   assert(I != M.PreprocessedEntityRemap.end()
1692          && "Invalid index into preprocessed entity index remap");
1693 
1694   return LocalID + I->second;
1695 }
1696 
1697 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1698   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1699 }
1700 
1701 HeaderFileInfoTrait::internal_key_type
1702 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1703   internal_key_type ikey = {FE->getSize(),
1704                             M.HasTimestamps ? FE->getModificationTime() : 0,
1705                             FE->getName(), /*Imported*/ false};
1706   return ikey;
1707 }
1708 
1709 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1710   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1711     return false;
1712 
1713   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1714     return true;
1715 
1716   // Determine whether the actual files are equivalent.
1717   FileManager &FileMgr = Reader.getFileManager();
1718   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1719     if (!Key.Imported)
1720       return FileMgr.getFile(Key.Filename);
1721 
1722     std::string Resolved = Key.Filename;
1723     Reader.ResolveImportedPath(M, Resolved);
1724     return FileMgr.getFile(Resolved);
1725   };
1726 
1727   const FileEntry *FEA = GetFile(a);
1728   const FileEntry *FEB = GetFile(b);
1729   return FEA && FEA == FEB;
1730 }
1731 
1732 std::pair<unsigned, unsigned>
1733 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1734   using namespace llvm::support;
1735 
1736   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1737   unsigned DataLen = (unsigned) *d++;
1738   return std::make_pair(KeyLen, DataLen);
1739 }
1740 
1741 HeaderFileInfoTrait::internal_key_type
1742 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1743   using namespace llvm::support;
1744 
1745   internal_key_type ikey;
1746   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1747   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1748   ikey.Filename = (const char *)d;
1749   ikey.Imported = true;
1750   return ikey;
1751 }
1752 
1753 HeaderFileInfoTrait::data_type
1754 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1755                               unsigned DataLen) {
1756   using namespace llvm::support;
1757 
1758   const unsigned char *End = d + DataLen;
1759   HeaderFileInfo HFI;
1760   unsigned Flags = *d++;
1761   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1762   HFI.isImport |= (Flags >> 5) & 0x01;
1763   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1764   HFI.DirInfo = (Flags >> 1) & 0x07;
1765   HFI.IndexHeaderMapHeader = Flags & 0x01;
1766   // FIXME: Find a better way to handle this. Maybe just store a
1767   // "has been included" flag?
1768   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1769                              HFI.NumIncludes);
1770   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1771       M, endian::readNext<uint32_t, little, unaligned>(d));
1772   if (unsigned FrameworkOffset =
1773           endian::readNext<uint32_t, little, unaligned>(d)) {
1774     // The framework offset is 1 greater than the actual offset,
1775     // since 0 is used as an indicator for "no framework name".
1776     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1777     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1778   }
1779 
1780   assert((End - d) % 4 == 0 &&
1781          "Wrong data length in HeaderFileInfo deserialization");
1782   while (d != End) {
1783     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1784     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1785     LocalSMID >>= 2;
1786 
1787     // This header is part of a module. Associate it with the module to enable
1788     // implicit module import.
1789     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1790     Module *Mod = Reader.getSubmodule(GlobalSMID);
1791     FileManager &FileMgr = Reader.getFileManager();
1792     ModuleMap &ModMap =
1793         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1794 
1795     std::string Filename = key.Filename;
1796     if (key.Imported)
1797       Reader.ResolveImportedPath(M, Filename);
1798     // FIXME: This is not always the right filename-as-written, but we're not
1799     // going to use this information to rebuild the module, so it doesn't make
1800     // a lot of difference.
1801     Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1802     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1803     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1804   }
1805 
1806   // This HeaderFileInfo was externally loaded.
1807   HFI.External = true;
1808   HFI.IsValid = true;
1809   return HFI;
1810 }
1811 
1812 void ASTReader::addPendingMacro(IdentifierInfo *II,
1813                                 ModuleFile *M,
1814                                 uint64_t MacroDirectivesOffset) {
1815   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1816   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1817 }
1818 
1819 void ASTReader::ReadDefinedMacros() {
1820   // Note that we are loading defined macros.
1821   Deserializing Macros(this);
1822 
1823   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1824     BitstreamCursor &MacroCursor = I.MacroCursor;
1825 
1826     // If there was no preprocessor block, skip this file.
1827     if (MacroCursor.getBitcodeBytes().empty())
1828       continue;
1829 
1830     BitstreamCursor Cursor = MacroCursor;
1831     Cursor.JumpToBit(I.MacroStartOffset);
1832 
1833     RecordData Record;
1834     while (true) {
1835       llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1836 
1837       switch (E.Kind) {
1838       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1839       case llvm::BitstreamEntry::Error:
1840         Error("malformed block record in AST file");
1841         return;
1842       case llvm::BitstreamEntry::EndBlock:
1843         goto NextCursor;
1844 
1845       case llvm::BitstreamEntry::Record:
1846         Record.clear();
1847         switch (Cursor.readRecord(E.ID, Record)) {
1848         default:  // Default behavior: ignore.
1849           break;
1850 
1851         case PP_MACRO_OBJECT_LIKE:
1852         case PP_MACRO_FUNCTION_LIKE: {
1853           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1854           if (II->isOutOfDate())
1855             updateOutOfDateIdentifier(*II);
1856           break;
1857         }
1858 
1859         case PP_TOKEN:
1860           // Ignore tokens.
1861           break;
1862         }
1863         break;
1864       }
1865     }
1866     NextCursor:  ;
1867   }
1868 }
1869 
1870 namespace {
1871 
1872   /// Visitor class used to look up identifirs in an AST file.
1873   class IdentifierLookupVisitor {
1874     StringRef Name;
1875     unsigned NameHash;
1876     unsigned PriorGeneration;
1877     unsigned &NumIdentifierLookups;
1878     unsigned &NumIdentifierLookupHits;
1879     IdentifierInfo *Found = nullptr;
1880 
1881   public:
1882     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1883                             unsigned &NumIdentifierLookups,
1884                             unsigned &NumIdentifierLookupHits)
1885       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1886         PriorGeneration(PriorGeneration),
1887         NumIdentifierLookups(NumIdentifierLookups),
1888         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
1889 
1890     bool operator()(ModuleFile &M) {
1891       // If we've already searched this module file, skip it now.
1892       if (M.Generation <= PriorGeneration)
1893         return true;
1894 
1895       ASTIdentifierLookupTable *IdTable
1896         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1897       if (!IdTable)
1898         return false;
1899 
1900       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
1901                                      Found);
1902       ++NumIdentifierLookups;
1903       ASTIdentifierLookupTable::iterator Pos =
1904           IdTable->find_hashed(Name, NameHash, &Trait);
1905       if (Pos == IdTable->end())
1906         return false;
1907 
1908       // Dereferencing the iterator has the effect of building the
1909       // IdentifierInfo node and populating it with the various
1910       // declarations it needs.
1911       ++NumIdentifierLookupHits;
1912       Found = *Pos;
1913       return true;
1914     }
1915 
1916     // Retrieve the identifier info found within the module
1917     // files.
1918     IdentifierInfo *getIdentifierInfo() const { return Found; }
1919   };
1920 
1921 } // namespace
1922 
1923 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1924   // Note that we are loading an identifier.
1925   Deserializing AnIdentifier(this);
1926 
1927   unsigned PriorGeneration = 0;
1928   if (getContext().getLangOpts().Modules)
1929     PriorGeneration = IdentifierGeneration[&II];
1930 
1931   // If there is a global index, look there first to determine which modules
1932   // provably do not have any results for this identifier.
1933   GlobalModuleIndex::HitSet Hits;
1934   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1935   if (!loadGlobalIndex()) {
1936     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1937       HitsPtr = &Hits;
1938     }
1939   }
1940 
1941   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1942                                   NumIdentifierLookups,
1943                                   NumIdentifierLookupHits);
1944   ModuleMgr.visit(Visitor, HitsPtr);
1945   markIdentifierUpToDate(&II);
1946 }
1947 
1948 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1949   if (!II)
1950     return;
1951 
1952   II->setOutOfDate(false);
1953 
1954   // Update the generation for this identifier.
1955   if (getContext().getLangOpts().Modules)
1956     IdentifierGeneration[II] = getGeneration();
1957 }
1958 
1959 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1960                                     const PendingMacroInfo &PMInfo) {
1961   ModuleFile &M = *PMInfo.M;
1962 
1963   BitstreamCursor &Cursor = M.MacroCursor;
1964   SavedStreamPosition SavedPosition(Cursor);
1965   Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1966 
1967   struct ModuleMacroRecord {
1968     SubmoduleID SubModID;
1969     MacroInfo *MI;
1970     SmallVector<SubmoduleID, 8> Overrides;
1971   };
1972   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1973 
1974   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1975   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1976   // macro histroy.
1977   RecordData Record;
1978   while (true) {
1979     llvm::BitstreamEntry Entry =
1980         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1981     if (Entry.Kind != llvm::BitstreamEntry::Record) {
1982       Error("malformed block record in AST file");
1983       return;
1984     }
1985 
1986     Record.clear();
1987     switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1988     case PP_MACRO_DIRECTIVE_HISTORY:
1989       break;
1990 
1991     case PP_MODULE_MACRO: {
1992       ModuleMacros.push_back(ModuleMacroRecord());
1993       auto &Info = ModuleMacros.back();
1994       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1995       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1996       for (int I = 2, N = Record.size(); I != N; ++I)
1997         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1998       continue;
1999     }
2000 
2001     default:
2002       Error("malformed block record in AST file");
2003       return;
2004     }
2005 
2006     // We found the macro directive history; that's the last record
2007     // for this macro.
2008     break;
2009   }
2010 
2011   // Module macros are listed in reverse dependency order.
2012   {
2013     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2014     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2015     for (auto &MMR : ModuleMacros) {
2016       Overrides.clear();
2017       for (unsigned ModID : MMR.Overrides) {
2018         Module *Mod = getSubmodule(ModID);
2019         auto *Macro = PP.getModuleMacro(Mod, II);
2020         assert(Macro && "missing definition for overridden macro");
2021         Overrides.push_back(Macro);
2022       }
2023 
2024       bool Inserted = false;
2025       Module *Owner = getSubmodule(MMR.SubModID);
2026       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2027     }
2028   }
2029 
2030   // Don't read the directive history for a module; we don't have anywhere
2031   // to put it.
2032   if (M.isModule())
2033     return;
2034 
2035   // Deserialize the macro directives history in reverse source-order.
2036   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2037   unsigned Idx = 0, N = Record.size();
2038   while (Idx < N) {
2039     MacroDirective *MD = nullptr;
2040     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2041     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2042     switch (K) {
2043     case MacroDirective::MD_Define: {
2044       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2045       MD = PP.AllocateDefMacroDirective(MI, Loc);
2046       break;
2047     }
2048     case MacroDirective::MD_Undefine:
2049       MD = PP.AllocateUndefMacroDirective(Loc);
2050       break;
2051     case MacroDirective::MD_Visibility:
2052       bool isPublic = Record[Idx++];
2053       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2054       break;
2055     }
2056 
2057     if (!Latest)
2058       Latest = MD;
2059     if (Earliest)
2060       Earliest->setPrevious(MD);
2061     Earliest = MD;
2062   }
2063 
2064   if (Latest)
2065     PP.setLoadedMacroDirective(II, Earliest, Latest);
2066 }
2067 
2068 ASTReader::InputFileInfo
2069 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2070   // Go find this input file.
2071   BitstreamCursor &Cursor = F.InputFilesCursor;
2072   SavedStreamPosition SavedPosition(Cursor);
2073   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2074 
2075   unsigned Code = Cursor.ReadCode();
2076   RecordData Record;
2077   StringRef Blob;
2078 
2079   unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2080   assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2081          "invalid record type for input file");
2082   (void)Result;
2083 
2084   assert(Record[0] == ID && "Bogus stored ID or offset");
2085   InputFileInfo R;
2086   R.StoredSize = static_cast<off_t>(Record[1]);
2087   R.StoredTime = static_cast<time_t>(Record[2]);
2088   R.Overridden = static_cast<bool>(Record[3]);
2089   R.Transient = static_cast<bool>(Record[4]);
2090   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2091   R.Filename = Blob;
2092   ResolveImportedPath(F, R.Filename);
2093   return R;
2094 }
2095 
2096 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2097 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2098   // If this ID is bogus, just return an empty input file.
2099   if (ID == 0 || ID > F.InputFilesLoaded.size())
2100     return InputFile();
2101 
2102   // If we've already loaded this input file, return it.
2103   if (F.InputFilesLoaded[ID-1].getFile())
2104     return F.InputFilesLoaded[ID-1];
2105 
2106   if (F.InputFilesLoaded[ID-1].isNotFound())
2107     return InputFile();
2108 
2109   // Go find this input file.
2110   BitstreamCursor &Cursor = F.InputFilesCursor;
2111   SavedStreamPosition SavedPosition(Cursor);
2112   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2113 
2114   InputFileInfo FI = readInputFileInfo(F, ID);
2115   off_t StoredSize = FI.StoredSize;
2116   time_t StoredTime = FI.StoredTime;
2117   bool Overridden = FI.Overridden;
2118   bool Transient = FI.Transient;
2119   StringRef Filename = FI.Filename;
2120 
2121   const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
2122   // If we didn't find the file, resolve it relative to the
2123   // original directory from which this AST file was created.
2124   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2125       F.OriginalDir != F.BaseDirectory) {
2126     std::string Resolved = resolveFileRelativeToOriginalDir(
2127         Filename, F.OriginalDir, F.BaseDirectory);
2128     if (!Resolved.empty())
2129       File = FileMgr.getFile(Resolved);
2130   }
2131 
2132   // For an overridden file, create a virtual file with the stored
2133   // size/timestamp.
2134   if ((Overridden || Transient) && File == nullptr)
2135     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2136 
2137   if (File == nullptr) {
2138     if (Complain) {
2139       std::string ErrorStr = "could not find file '";
2140       ErrorStr += Filename;
2141       ErrorStr += "' referenced by AST file '";
2142       ErrorStr += F.FileName;
2143       ErrorStr += "'";
2144       Error(ErrorStr);
2145     }
2146     // Record that we didn't find the file.
2147     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2148     return InputFile();
2149   }
2150 
2151   // Check if there was a request to override the contents of the file
2152   // that was part of the precompiled header. Overriding such a file
2153   // can lead to problems when lexing using the source locations from the
2154   // PCH.
2155   SourceManager &SM = getSourceManager();
2156   // FIXME: Reject if the overrides are different.
2157   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2158     if (Complain)
2159       Error(diag::err_fe_pch_file_overridden, Filename);
2160     // After emitting the diagnostic, recover by disabling the override so
2161     // that the original file will be used.
2162     //
2163     // FIXME: This recovery is just as broken as the original state; there may
2164     // be another precompiled module that's using the overridden contents, or
2165     // we might be half way through parsing it. Instead, we should treat the
2166     // overridden contents as belonging to a separate FileEntry.
2167     SM.disableFileContentsOverride(File);
2168     // The FileEntry is a virtual file entry with the size of the contents
2169     // that would override the original contents. Set it to the original's
2170     // size/time.
2171     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2172                             StoredSize, StoredTime);
2173   }
2174 
2175   bool IsOutOfDate = false;
2176 
2177   // For an overridden file, there is nothing to validate.
2178   if (!Overridden && //
2179       (StoredSize != File->getSize() ||
2180        (StoredTime && StoredTime != File->getModificationTime() &&
2181         !DisableValidation)
2182        )) {
2183     if (Complain) {
2184       // Build a list of the PCH imports that got us here (in reverse).
2185       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2186       while (!ImportStack.back()->ImportedBy.empty())
2187         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2188 
2189       // The top-level PCH is stale.
2190       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2191       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2192       if (DiagnosticKind == 0)
2193         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2194       else if (DiagnosticKind == 1)
2195         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2196       else
2197         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2198 
2199       // Print the import stack.
2200       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2201         Diag(diag::note_pch_required_by)
2202           << Filename << ImportStack[0]->FileName;
2203         for (unsigned I = 1; I < ImportStack.size(); ++I)
2204           Diag(diag::note_pch_required_by)
2205             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2206       }
2207 
2208       if (!Diags.isDiagnosticInFlight())
2209         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2210     }
2211 
2212     IsOutOfDate = true;
2213   }
2214   // FIXME: If the file is overridden and we've already opened it,
2215   // issue an error (or split it into a separate FileEntry).
2216 
2217   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2218 
2219   // Note that we've loaded this input file.
2220   F.InputFilesLoaded[ID-1] = IF;
2221   return IF;
2222 }
2223 
2224 /// If we are loading a relocatable PCH or module file, and the filename
2225 /// is not an absolute path, add the system or module root to the beginning of
2226 /// the file name.
2227 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2228   // Resolve relative to the base directory, if we have one.
2229   if (!M.BaseDirectory.empty())
2230     return ResolveImportedPath(Filename, M.BaseDirectory);
2231 }
2232 
2233 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2234   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2235     return;
2236 
2237   SmallString<128> Buffer;
2238   llvm::sys::path::append(Buffer, Prefix, Filename);
2239   Filename.assign(Buffer.begin(), Buffer.end());
2240 }
2241 
2242 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2243   switch (ARR) {
2244   case ASTReader::Failure: return true;
2245   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2246   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2247   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2248   case ASTReader::ConfigurationMismatch:
2249     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2250   case ASTReader::HadErrors: return true;
2251   case ASTReader::Success: return false;
2252   }
2253 
2254   llvm_unreachable("unknown ASTReadResult");
2255 }
2256 
2257 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2258     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2259     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2260     std::string &SuggestedPredefines) {
2261   if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2262     return Failure;
2263 
2264   // Read all of the records in the options block.
2265   RecordData Record;
2266   ASTReadResult Result = Success;
2267   while (true) {
2268     llvm::BitstreamEntry Entry = Stream.advance();
2269 
2270     switch (Entry.Kind) {
2271     case llvm::BitstreamEntry::Error:
2272     case llvm::BitstreamEntry::SubBlock:
2273       return Failure;
2274 
2275     case llvm::BitstreamEntry::EndBlock:
2276       return Result;
2277 
2278     case llvm::BitstreamEntry::Record:
2279       // The interesting case.
2280       break;
2281     }
2282 
2283     // Read and process a record.
2284     Record.clear();
2285     switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2286     case LANGUAGE_OPTIONS: {
2287       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2288       if (ParseLanguageOptions(Record, Complain, Listener,
2289                                AllowCompatibleConfigurationMismatch))
2290         Result = ConfigurationMismatch;
2291       break;
2292     }
2293 
2294     case TARGET_OPTIONS: {
2295       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2296       if (ParseTargetOptions(Record, Complain, Listener,
2297                              AllowCompatibleConfigurationMismatch))
2298         Result = ConfigurationMismatch;
2299       break;
2300     }
2301 
2302     case FILE_SYSTEM_OPTIONS: {
2303       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2304       if (!AllowCompatibleConfigurationMismatch &&
2305           ParseFileSystemOptions(Record, Complain, Listener))
2306         Result = ConfigurationMismatch;
2307       break;
2308     }
2309 
2310     case HEADER_SEARCH_OPTIONS: {
2311       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2312       if (!AllowCompatibleConfigurationMismatch &&
2313           ParseHeaderSearchOptions(Record, Complain, Listener))
2314         Result = ConfigurationMismatch;
2315       break;
2316     }
2317 
2318     case PREPROCESSOR_OPTIONS:
2319       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2320       if (!AllowCompatibleConfigurationMismatch &&
2321           ParsePreprocessorOptions(Record, Complain, Listener,
2322                                    SuggestedPredefines))
2323         Result = ConfigurationMismatch;
2324       break;
2325     }
2326   }
2327 }
2328 
2329 ASTReader::ASTReadResult
2330 ASTReader::ReadControlBlock(ModuleFile &F,
2331                             SmallVectorImpl<ImportedModule> &Loaded,
2332                             const ModuleFile *ImportedBy,
2333                             unsigned ClientLoadCapabilities) {
2334   BitstreamCursor &Stream = F.Stream;
2335   ASTReadResult Result = Success;
2336 
2337   if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2338     Error("malformed block record in AST file");
2339     return Failure;
2340   }
2341 
2342   // Lambda to read the unhashed control block the first time it's called.
2343   //
2344   // For PCM files, the unhashed control block cannot be read until after the
2345   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2346   // need to look ahead before reading the IMPORTS record.  For consistency,
2347   // this block is always read somehow (see BitstreamEntry::EndBlock).
2348   bool HasReadUnhashedControlBlock = false;
2349   auto readUnhashedControlBlockOnce = [&]() {
2350     if (!HasReadUnhashedControlBlock) {
2351       HasReadUnhashedControlBlock = true;
2352       if (ASTReadResult Result =
2353               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2354         return Result;
2355     }
2356     return Success;
2357   };
2358 
2359   // Read all of the records and blocks in the control block.
2360   RecordData Record;
2361   unsigned NumInputs = 0;
2362   unsigned NumUserInputs = 0;
2363   StringRef BaseDirectoryAsWritten;
2364   while (true) {
2365     llvm::BitstreamEntry Entry = Stream.advance();
2366 
2367     switch (Entry.Kind) {
2368     case llvm::BitstreamEntry::Error:
2369       Error("malformed block record in AST file");
2370       return Failure;
2371     case llvm::BitstreamEntry::EndBlock: {
2372       // Validate the module before returning.  This call catches an AST with
2373       // no module name and no imports.
2374       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2375         return Result;
2376 
2377       // Validate input files.
2378       const HeaderSearchOptions &HSOpts =
2379           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2380 
2381       // All user input files reside at the index range [0, NumUserInputs), and
2382       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2383       // loaded module files, ignore missing inputs.
2384       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2385           F.Kind != MK_PrebuiltModule) {
2386         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2387 
2388         // If we are reading a module, we will create a verification timestamp,
2389         // so we verify all input files.  Otherwise, verify only user input
2390         // files.
2391 
2392         unsigned N = NumUserInputs;
2393         if (ValidateSystemInputs ||
2394             (HSOpts.ModulesValidateOncePerBuildSession &&
2395              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2396              F.Kind == MK_ImplicitModule))
2397           N = NumInputs;
2398 
2399         for (unsigned I = 0; I < N; ++I) {
2400           InputFile IF = getInputFile(F, I+1, Complain);
2401           if (!IF.getFile() || IF.isOutOfDate())
2402             return OutOfDate;
2403         }
2404       }
2405 
2406       if (Listener)
2407         Listener->visitModuleFile(F.FileName, F.Kind);
2408 
2409       if (Listener && Listener->needsInputFileVisitation()) {
2410         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2411                                                                 : NumUserInputs;
2412         for (unsigned I = 0; I < N; ++I) {
2413           bool IsSystem = I >= NumUserInputs;
2414           InputFileInfo FI = readInputFileInfo(F, I+1);
2415           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2416                                    F.Kind == MK_ExplicitModule ||
2417                                    F.Kind == MK_PrebuiltModule);
2418         }
2419       }
2420 
2421       return Result;
2422     }
2423 
2424     case llvm::BitstreamEntry::SubBlock:
2425       switch (Entry.ID) {
2426       case INPUT_FILES_BLOCK_ID:
2427         F.InputFilesCursor = Stream;
2428         if (Stream.SkipBlock() || // Skip with the main cursor
2429             // Read the abbreviations
2430             ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2431           Error("malformed block record in AST file");
2432           return Failure;
2433         }
2434         continue;
2435 
2436       case OPTIONS_BLOCK_ID:
2437         // If we're reading the first module for this group, check its options
2438         // are compatible with ours. For modules it imports, no further checking
2439         // is required, because we checked them when we built it.
2440         if (Listener && !ImportedBy) {
2441           // Should we allow the configuration of the module file to differ from
2442           // the configuration of the current translation unit in a compatible
2443           // way?
2444           //
2445           // FIXME: Allow this for files explicitly specified with -include-pch.
2446           bool AllowCompatibleConfigurationMismatch =
2447               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2448 
2449           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2450                                     AllowCompatibleConfigurationMismatch,
2451                                     *Listener, SuggestedPredefines);
2452           if (Result == Failure) {
2453             Error("malformed block record in AST file");
2454             return Result;
2455           }
2456 
2457           if (DisableValidation ||
2458               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2459             Result = Success;
2460 
2461           // If we can't load the module, exit early since we likely
2462           // will rebuild the module anyway. The stream may be in the
2463           // middle of a block.
2464           if (Result != Success)
2465             return Result;
2466         } else if (Stream.SkipBlock()) {
2467           Error("malformed block record in AST file");
2468           return Failure;
2469         }
2470         continue;
2471 
2472       default:
2473         if (Stream.SkipBlock()) {
2474           Error("malformed block record in AST file");
2475           return Failure;
2476         }
2477         continue;
2478       }
2479 
2480     case llvm::BitstreamEntry::Record:
2481       // The interesting case.
2482       break;
2483     }
2484 
2485     // Read and process a record.
2486     Record.clear();
2487     StringRef Blob;
2488     switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2489     case METADATA: {
2490       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2491         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2492           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2493                                         : diag::err_pch_version_too_new);
2494         return VersionMismatch;
2495       }
2496 
2497       bool hasErrors = Record[7];
2498       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2499         Diag(diag::err_pch_with_compiler_errors);
2500         return HadErrors;
2501       }
2502       if (hasErrors) {
2503         Diags.ErrorOccurred = true;
2504         Diags.UncompilableErrorOccurred = true;
2505         Diags.UnrecoverableErrorOccurred = true;
2506       }
2507 
2508       F.RelocatablePCH = Record[4];
2509       // Relative paths in a relocatable PCH are relative to our sysroot.
2510       if (F.RelocatablePCH)
2511         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2512 
2513       F.HasTimestamps = Record[5];
2514 
2515       F.PCHHasObjectFile = Record[6];
2516 
2517       const std::string &CurBranch = getClangFullRepositoryVersion();
2518       StringRef ASTBranch = Blob;
2519       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2520         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2521           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2522         return VersionMismatch;
2523       }
2524       break;
2525     }
2526 
2527     case IMPORTS: {
2528       // Validate the AST before processing any imports (otherwise, untangling
2529       // them can be error-prone and expensive).  A module will have a name and
2530       // will already have been validated, but this catches the PCH case.
2531       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2532         return Result;
2533 
2534       // Load each of the imported PCH files.
2535       unsigned Idx = 0, N = Record.size();
2536       while (Idx < N) {
2537         // Read information about the AST file.
2538         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2539         // The import location will be the local one for now; we will adjust
2540         // all import locations of module imports after the global source
2541         // location info are setup, in ReadAST.
2542         SourceLocation ImportLoc =
2543             ReadUntranslatedSourceLocation(Record[Idx++]);
2544         off_t StoredSize = (off_t)Record[Idx++];
2545         time_t StoredModTime = (time_t)Record[Idx++];
2546         ASTFileSignature StoredSignature = {
2547             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2548               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2549               (uint32_t)Record[Idx++]}}};
2550 
2551         std::string ImportedName = ReadString(Record, Idx);
2552         std::string ImportedFile;
2553 
2554         // For prebuilt and explicit modules first consult the file map for
2555         // an override. Note that here we don't search prebuilt module
2556         // directories, only the explicit name to file mappings. Also, we will
2557         // still verify the size/signature making sure it is essentially the
2558         // same file but perhaps in a different location.
2559         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2560           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2561             ImportedName, /*FileMapOnly*/ true);
2562 
2563         if (ImportedFile.empty())
2564           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2565           // ModuleCache as when writing.
2566           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2567         else
2568           SkipPath(Record, Idx);
2569 
2570         // If our client can't cope with us being out of date, we can't cope with
2571         // our dependency being missing.
2572         unsigned Capabilities = ClientLoadCapabilities;
2573         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2574           Capabilities &= ~ARR_Missing;
2575 
2576         // Load the AST file.
2577         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2578                                   Loaded, StoredSize, StoredModTime,
2579                                   StoredSignature, Capabilities);
2580 
2581         // If we diagnosed a problem, produce a backtrace.
2582         if (isDiagnosedResult(Result, Capabilities))
2583           Diag(diag::note_module_file_imported_by)
2584               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2585 
2586         switch (Result) {
2587         case Failure: return Failure;
2588           // If we have to ignore the dependency, we'll have to ignore this too.
2589         case Missing:
2590         case OutOfDate: return OutOfDate;
2591         case VersionMismatch: return VersionMismatch;
2592         case ConfigurationMismatch: return ConfigurationMismatch;
2593         case HadErrors: return HadErrors;
2594         case Success: break;
2595         }
2596       }
2597       break;
2598     }
2599 
2600     case ORIGINAL_FILE:
2601       F.OriginalSourceFileID = FileID::get(Record[0]);
2602       F.ActualOriginalSourceFileName = Blob;
2603       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2604       ResolveImportedPath(F, F.OriginalSourceFileName);
2605       break;
2606 
2607     case ORIGINAL_FILE_ID:
2608       F.OriginalSourceFileID = FileID::get(Record[0]);
2609       break;
2610 
2611     case ORIGINAL_PCH_DIR:
2612       F.OriginalDir = Blob;
2613       break;
2614 
2615     case MODULE_NAME:
2616       F.ModuleName = Blob;
2617       Diag(diag::remark_module_import)
2618           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2619           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2620       if (Listener)
2621         Listener->ReadModuleName(F.ModuleName);
2622 
2623       // Validate the AST as soon as we have a name so we can exit early on
2624       // failure.
2625       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2626         return Result;
2627 
2628       break;
2629 
2630     case MODULE_DIRECTORY: {
2631       // Save the BaseDirectory as written in the PCM for computing the module
2632       // filename for the ModuleCache.
2633       BaseDirectoryAsWritten = Blob;
2634       assert(!F.ModuleName.empty() &&
2635              "MODULE_DIRECTORY found before MODULE_NAME");
2636       // If we've already loaded a module map file covering this module, we may
2637       // have a better path for it (relative to the current build).
2638       Module *M = PP.getHeaderSearchInfo().lookupModule(
2639           F.ModuleName, /*AllowSearch*/ true,
2640           /*AllowExtraModuleMapSearch*/ true);
2641       if (M && M->Directory) {
2642         // If we're implicitly loading a module, the base directory can't
2643         // change between the build and use.
2644         // Don't emit module relocation error if we have -fno-validate-pch
2645         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2646             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2647           const DirectoryEntry *BuildDir =
2648               PP.getFileManager().getDirectory(Blob);
2649           if (!BuildDir || BuildDir != M->Directory) {
2650             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2651               Diag(diag::err_imported_module_relocated)
2652                   << F.ModuleName << Blob << M->Directory->getName();
2653             return OutOfDate;
2654           }
2655         }
2656         F.BaseDirectory = M->Directory->getName();
2657       } else {
2658         F.BaseDirectory = Blob;
2659       }
2660       break;
2661     }
2662 
2663     case MODULE_MAP_FILE:
2664       if (ASTReadResult Result =
2665               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2666         return Result;
2667       break;
2668 
2669     case INPUT_FILE_OFFSETS:
2670       NumInputs = Record[0];
2671       NumUserInputs = Record[1];
2672       F.InputFileOffsets =
2673           (const llvm::support::unaligned_uint64_t *)Blob.data();
2674       F.InputFilesLoaded.resize(NumInputs);
2675       F.NumUserInputFiles = NumUserInputs;
2676       break;
2677     }
2678   }
2679 }
2680 
2681 ASTReader::ASTReadResult
2682 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2683   BitstreamCursor &Stream = F.Stream;
2684 
2685   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2686     Error("malformed block record in AST file");
2687     return Failure;
2688   }
2689 
2690   // Read all of the records and blocks for the AST file.
2691   RecordData Record;
2692   while (true) {
2693     llvm::BitstreamEntry Entry = Stream.advance();
2694 
2695     switch (Entry.Kind) {
2696     case llvm::BitstreamEntry::Error:
2697       Error("error at end of module block in AST file");
2698       return Failure;
2699     case llvm::BitstreamEntry::EndBlock:
2700       // Outside of C++, we do not store a lookup map for the translation unit.
2701       // Instead, mark it as needing a lookup map to be built if this module
2702       // contains any declarations lexically within it (which it always does!).
2703       // This usually has no cost, since we very rarely need the lookup map for
2704       // the translation unit outside C++.
2705       if (ASTContext *Ctx = ContextObj) {
2706         DeclContext *DC = Ctx->getTranslationUnitDecl();
2707         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2708           DC->setMustBuildLookupTable();
2709       }
2710 
2711       return Success;
2712     case llvm::BitstreamEntry::SubBlock:
2713       switch (Entry.ID) {
2714       case DECLTYPES_BLOCK_ID:
2715         // We lazily load the decls block, but we want to set up the
2716         // DeclsCursor cursor to point into it.  Clone our current bitcode
2717         // cursor to it, enter the block and read the abbrevs in that block.
2718         // With the main cursor, we just skip over it.
2719         F.DeclsCursor = Stream;
2720         if (Stream.SkipBlock() ||  // Skip with the main cursor.
2721             // Read the abbrevs.
2722             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2723           Error("malformed block record in AST file");
2724           return Failure;
2725         }
2726         break;
2727 
2728       case PREPROCESSOR_BLOCK_ID:
2729         F.MacroCursor = Stream;
2730         if (!PP.getExternalSource())
2731           PP.setExternalSource(this);
2732 
2733         if (Stream.SkipBlock() ||
2734             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2735           Error("malformed block record in AST file");
2736           return Failure;
2737         }
2738         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2739         break;
2740 
2741       case PREPROCESSOR_DETAIL_BLOCK_ID:
2742         F.PreprocessorDetailCursor = Stream;
2743         if (Stream.SkipBlock() ||
2744             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2745                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2746               Error("malformed preprocessor detail record in AST file");
2747               return Failure;
2748             }
2749         F.PreprocessorDetailStartOffset
2750         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2751 
2752         if (!PP.getPreprocessingRecord())
2753           PP.createPreprocessingRecord();
2754         if (!PP.getPreprocessingRecord()->getExternalSource())
2755           PP.getPreprocessingRecord()->SetExternalSource(*this);
2756         break;
2757 
2758       case SOURCE_MANAGER_BLOCK_ID:
2759         if (ReadSourceManagerBlock(F))
2760           return Failure;
2761         break;
2762 
2763       case SUBMODULE_BLOCK_ID:
2764         if (ASTReadResult Result =
2765                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2766           return Result;
2767         break;
2768 
2769       case COMMENTS_BLOCK_ID: {
2770         BitstreamCursor C = Stream;
2771         if (Stream.SkipBlock() ||
2772             ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2773           Error("malformed comments block in AST file");
2774           return Failure;
2775         }
2776         CommentsCursors.push_back(std::make_pair(C, &F));
2777         break;
2778       }
2779 
2780       default:
2781         if (Stream.SkipBlock()) {
2782           Error("malformed block record in AST file");
2783           return Failure;
2784         }
2785         break;
2786       }
2787       continue;
2788 
2789     case llvm::BitstreamEntry::Record:
2790       // The interesting case.
2791       break;
2792     }
2793 
2794     // Read and process a record.
2795     Record.clear();
2796     StringRef Blob;
2797     auto RecordType =
2798         (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
2799 
2800     // If we're not loading an AST context, we don't care about most records.
2801     if (!ContextObj) {
2802       switch (RecordType) {
2803       case IDENTIFIER_TABLE:
2804       case IDENTIFIER_OFFSET:
2805       case INTERESTING_IDENTIFIERS:
2806       case STATISTICS:
2807       case PP_CONDITIONAL_STACK:
2808       case PP_COUNTER_VALUE:
2809       case SOURCE_LOCATION_OFFSETS:
2810       case MODULE_OFFSET_MAP:
2811       case SOURCE_MANAGER_LINE_TABLE:
2812       case SOURCE_LOCATION_PRELOADS:
2813       case PPD_ENTITIES_OFFSETS:
2814       case HEADER_SEARCH_TABLE:
2815       case IMPORTED_MODULES:
2816       case MACRO_OFFSET:
2817         break;
2818       default:
2819         continue;
2820       }
2821     }
2822 
2823     switch (RecordType) {
2824     default:  // Default behavior: ignore.
2825       break;
2826 
2827     case TYPE_OFFSET: {
2828       if (F.LocalNumTypes != 0) {
2829         Error("duplicate TYPE_OFFSET record in AST file");
2830         return Failure;
2831       }
2832       F.TypeOffsets = (const uint32_t *)Blob.data();
2833       F.LocalNumTypes = Record[0];
2834       unsigned LocalBaseTypeIndex = Record[1];
2835       F.BaseTypeIndex = getTotalNumTypes();
2836 
2837       if (F.LocalNumTypes > 0) {
2838         // Introduce the global -> local mapping for types within this module.
2839         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2840 
2841         // Introduce the local -> global mapping for types within this module.
2842         F.TypeRemap.insertOrReplace(
2843           std::make_pair(LocalBaseTypeIndex,
2844                          F.BaseTypeIndex - LocalBaseTypeIndex));
2845 
2846         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2847       }
2848       break;
2849     }
2850 
2851     case DECL_OFFSET: {
2852       if (F.LocalNumDecls != 0) {
2853         Error("duplicate DECL_OFFSET record in AST file");
2854         return Failure;
2855       }
2856       F.DeclOffsets = (const DeclOffset *)Blob.data();
2857       F.LocalNumDecls = Record[0];
2858       unsigned LocalBaseDeclID = Record[1];
2859       F.BaseDeclID = getTotalNumDecls();
2860 
2861       if (F.LocalNumDecls > 0) {
2862         // Introduce the global -> local mapping for declarations within this
2863         // module.
2864         GlobalDeclMap.insert(
2865           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2866 
2867         // Introduce the local -> global mapping for declarations within this
2868         // module.
2869         F.DeclRemap.insertOrReplace(
2870           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2871 
2872         // Introduce the global -> local mapping for declarations within this
2873         // module.
2874         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2875 
2876         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2877       }
2878       break;
2879     }
2880 
2881     case TU_UPDATE_LEXICAL: {
2882       DeclContext *TU = ContextObj->getTranslationUnitDecl();
2883       LexicalContents Contents(
2884           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2885               Blob.data()),
2886           static_cast<unsigned int>(Blob.size() / 4));
2887       TULexicalDecls.push_back(std::make_pair(&F, Contents));
2888       TU->setHasExternalLexicalStorage(true);
2889       break;
2890     }
2891 
2892     case UPDATE_VISIBLE: {
2893       unsigned Idx = 0;
2894       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2895       auto *Data = (const unsigned char*)Blob.data();
2896       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
2897       // If we've already loaded the decl, perform the updates when we finish
2898       // loading this block.
2899       if (Decl *D = GetExistingDecl(ID))
2900         PendingUpdateRecords.push_back(
2901             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
2902       break;
2903     }
2904 
2905     case IDENTIFIER_TABLE:
2906       F.IdentifierTableData = Blob.data();
2907       if (Record[0]) {
2908         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2909             (const unsigned char *)F.IdentifierTableData + Record[0],
2910             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2911             (const unsigned char *)F.IdentifierTableData,
2912             ASTIdentifierLookupTrait(*this, F));
2913 
2914         PP.getIdentifierTable().setExternalIdentifierLookup(this);
2915       }
2916       break;
2917 
2918     case IDENTIFIER_OFFSET: {
2919       if (F.LocalNumIdentifiers != 0) {
2920         Error("duplicate IDENTIFIER_OFFSET record in AST file");
2921         return Failure;
2922       }
2923       F.IdentifierOffsets = (const uint32_t *)Blob.data();
2924       F.LocalNumIdentifiers = Record[0];
2925       unsigned LocalBaseIdentifierID = Record[1];
2926       F.BaseIdentifierID = getTotalNumIdentifiers();
2927 
2928       if (F.LocalNumIdentifiers > 0) {
2929         // Introduce the global -> local mapping for identifiers within this
2930         // module.
2931         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2932                                                   &F));
2933 
2934         // Introduce the local -> global mapping for identifiers within this
2935         // module.
2936         F.IdentifierRemap.insertOrReplace(
2937           std::make_pair(LocalBaseIdentifierID,
2938                          F.BaseIdentifierID - LocalBaseIdentifierID));
2939 
2940         IdentifiersLoaded.resize(IdentifiersLoaded.size()
2941                                  + F.LocalNumIdentifiers);
2942       }
2943       break;
2944     }
2945 
2946     case INTERESTING_IDENTIFIERS:
2947       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2948       break;
2949 
2950     case EAGERLY_DESERIALIZED_DECLS:
2951       // FIXME: Skip reading this record if our ASTConsumer doesn't care
2952       // about "interesting" decls (for instance, if we're building a module).
2953       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2954         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2955       break;
2956 
2957     case MODULAR_CODEGEN_DECLS:
2958       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
2959       // them (ie: if we're not codegenerating this module).
2960       if (F.Kind == MK_MainFile)
2961         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2962           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2963       break;
2964 
2965     case SPECIAL_TYPES:
2966       if (SpecialTypes.empty()) {
2967         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2968           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2969         break;
2970       }
2971 
2972       if (SpecialTypes.size() != Record.size()) {
2973         Error("invalid special-types record");
2974         return Failure;
2975       }
2976 
2977       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2978         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2979         if (!SpecialTypes[I])
2980           SpecialTypes[I] = ID;
2981         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2982         // merge step?
2983       }
2984       break;
2985 
2986     case STATISTICS:
2987       TotalNumStatements += Record[0];
2988       TotalNumMacros += Record[1];
2989       TotalLexicalDeclContexts += Record[2];
2990       TotalVisibleDeclContexts += Record[3];
2991       break;
2992 
2993     case UNUSED_FILESCOPED_DECLS:
2994       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2995         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2996       break;
2997 
2998     case DELEGATING_CTORS:
2999       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3000         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3001       break;
3002 
3003     case WEAK_UNDECLARED_IDENTIFIERS:
3004       if (Record.size() % 4 != 0) {
3005         Error("invalid weak identifiers record");
3006         return Failure;
3007       }
3008 
3009       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3010       // files. This isn't the way to do it :)
3011       WeakUndeclaredIdentifiers.clear();
3012 
3013       // Translate the weak, undeclared identifiers into global IDs.
3014       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3015         WeakUndeclaredIdentifiers.push_back(
3016           getGlobalIdentifierID(F, Record[I++]));
3017         WeakUndeclaredIdentifiers.push_back(
3018           getGlobalIdentifierID(F, Record[I++]));
3019         WeakUndeclaredIdentifiers.push_back(
3020           ReadSourceLocation(F, Record, I).getRawEncoding());
3021         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3022       }
3023       break;
3024 
3025     case SELECTOR_OFFSETS: {
3026       F.SelectorOffsets = (const uint32_t *)Blob.data();
3027       F.LocalNumSelectors = Record[0];
3028       unsigned LocalBaseSelectorID = Record[1];
3029       F.BaseSelectorID = getTotalNumSelectors();
3030 
3031       if (F.LocalNumSelectors > 0) {
3032         // Introduce the global -> local mapping for selectors within this
3033         // module.
3034         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3035 
3036         // Introduce the local -> global mapping for selectors within this
3037         // module.
3038         F.SelectorRemap.insertOrReplace(
3039           std::make_pair(LocalBaseSelectorID,
3040                          F.BaseSelectorID - LocalBaseSelectorID));
3041 
3042         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3043       }
3044       break;
3045     }
3046 
3047     case METHOD_POOL:
3048       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3049       if (Record[0])
3050         F.SelectorLookupTable
3051           = ASTSelectorLookupTable::Create(
3052                         F.SelectorLookupTableData + Record[0],
3053                         F.SelectorLookupTableData,
3054                         ASTSelectorLookupTrait(*this, F));
3055       TotalNumMethodPoolEntries += Record[1];
3056       break;
3057 
3058     case REFERENCED_SELECTOR_POOL:
3059       if (!Record.empty()) {
3060         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3061           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3062                                                                 Record[Idx++]));
3063           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3064                                               getRawEncoding());
3065         }
3066       }
3067       break;
3068 
3069     case PP_CONDITIONAL_STACK:
3070       if (!Record.empty()) {
3071         unsigned Idx = 0, End = Record.size() - 1;
3072         bool ReachedEOFWhileSkipping = Record[Idx++];
3073         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3074         if (ReachedEOFWhileSkipping) {
3075           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3076           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3077           bool FoundNonSkipPortion = Record[Idx++];
3078           bool FoundElse = Record[Idx++];
3079           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3080           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3081                            FoundElse, ElseLoc);
3082         }
3083         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3084         while (Idx < End) {
3085           auto Loc = ReadSourceLocation(F, Record, Idx);
3086           bool WasSkipping = Record[Idx++];
3087           bool FoundNonSkip = Record[Idx++];
3088           bool FoundElse = Record[Idx++];
3089           ConditionalStack.push_back(
3090               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3091         }
3092         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3093       }
3094       break;
3095 
3096     case PP_COUNTER_VALUE:
3097       if (!Record.empty() && Listener)
3098         Listener->ReadCounter(F, Record[0]);
3099       break;
3100 
3101     case FILE_SORTED_DECLS:
3102       F.FileSortedDecls = (const DeclID *)Blob.data();
3103       F.NumFileSortedDecls = Record[0];
3104       break;
3105 
3106     case SOURCE_LOCATION_OFFSETS: {
3107       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3108       F.LocalNumSLocEntries = Record[0];
3109       unsigned SLocSpaceSize = Record[1];
3110       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3111           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3112                                               SLocSpaceSize);
3113       if (!F.SLocEntryBaseID) {
3114         Error("ran out of source locations");
3115         break;
3116       }
3117       // Make our entry in the range map. BaseID is negative and growing, so
3118       // we invert it. Because we invert it, though, we need the other end of
3119       // the range.
3120       unsigned RangeStart =
3121           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3122       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3123       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3124 
3125       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3126       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3127       GlobalSLocOffsetMap.insert(
3128           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3129                            - SLocSpaceSize,&F));
3130 
3131       // Initialize the remapping table.
3132       // Invalid stays invalid.
3133       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3134       // This module. Base was 2 when being compiled.
3135       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3136                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3137 
3138       TotalNumSLocEntries += F.LocalNumSLocEntries;
3139       break;
3140     }
3141 
3142     case MODULE_OFFSET_MAP:
3143       F.ModuleOffsetMap = Blob;
3144       break;
3145 
3146     case SOURCE_MANAGER_LINE_TABLE:
3147       if (ParseLineTable(F, Record))
3148         return Failure;
3149       break;
3150 
3151     case SOURCE_LOCATION_PRELOADS: {
3152       // Need to transform from the local view (1-based IDs) to the global view,
3153       // which is based off F.SLocEntryBaseID.
3154       if (!F.PreloadSLocEntries.empty()) {
3155         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3156         return Failure;
3157       }
3158 
3159       F.PreloadSLocEntries.swap(Record);
3160       break;
3161     }
3162 
3163     case EXT_VECTOR_DECLS:
3164       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3165         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3166       break;
3167 
3168     case VTABLE_USES:
3169       if (Record.size() % 3 != 0) {
3170         Error("Invalid VTABLE_USES record");
3171         return Failure;
3172       }
3173 
3174       // Later tables overwrite earlier ones.
3175       // FIXME: Modules will have some trouble with this. This is clearly not
3176       // the right way to do this.
3177       VTableUses.clear();
3178 
3179       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3180         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3181         VTableUses.push_back(
3182           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3183         VTableUses.push_back(Record[Idx++]);
3184       }
3185       break;
3186 
3187     case PENDING_IMPLICIT_INSTANTIATIONS:
3188       if (PendingInstantiations.size() % 2 != 0) {
3189         Error("Invalid existing PendingInstantiations");
3190         return Failure;
3191       }
3192 
3193       if (Record.size() % 2 != 0) {
3194         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3195         return Failure;
3196       }
3197 
3198       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3199         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3200         PendingInstantiations.push_back(
3201           ReadSourceLocation(F, Record, I).getRawEncoding());
3202       }
3203       break;
3204 
3205     case SEMA_DECL_REFS:
3206       if (Record.size() != 3) {
3207         Error("Invalid SEMA_DECL_REFS block");
3208         return Failure;
3209       }
3210       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3211         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3212       break;
3213 
3214     case PPD_ENTITIES_OFFSETS: {
3215       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3216       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3217       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3218 
3219       unsigned LocalBasePreprocessedEntityID = Record[0];
3220 
3221       unsigned StartingID;
3222       if (!PP.getPreprocessingRecord())
3223         PP.createPreprocessingRecord();
3224       if (!PP.getPreprocessingRecord()->getExternalSource())
3225         PP.getPreprocessingRecord()->SetExternalSource(*this);
3226       StartingID
3227         = PP.getPreprocessingRecord()
3228             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3229       F.BasePreprocessedEntityID = StartingID;
3230 
3231       if (F.NumPreprocessedEntities > 0) {
3232         // Introduce the global -> local mapping for preprocessed entities in
3233         // this module.
3234         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3235 
3236         // Introduce the local -> global mapping for preprocessed entities in
3237         // this module.
3238         F.PreprocessedEntityRemap.insertOrReplace(
3239           std::make_pair(LocalBasePreprocessedEntityID,
3240             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3241       }
3242 
3243       break;
3244     }
3245 
3246     case PPD_SKIPPED_RANGES: {
3247       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3248       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3249       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3250 
3251       if (!PP.getPreprocessingRecord())
3252         PP.createPreprocessingRecord();
3253       if (!PP.getPreprocessingRecord()->getExternalSource())
3254         PP.getPreprocessingRecord()->SetExternalSource(*this);
3255       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3256           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3257 
3258       if (F.NumPreprocessedSkippedRanges > 0)
3259         GlobalSkippedRangeMap.insert(
3260             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3261       break;
3262     }
3263 
3264     case DECL_UPDATE_OFFSETS:
3265       if (Record.size() % 2 != 0) {
3266         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3267         return Failure;
3268       }
3269       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3270         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3271         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3272 
3273         // If we've already loaded the decl, perform the updates when we finish
3274         // loading this block.
3275         if (Decl *D = GetExistingDecl(ID))
3276           PendingUpdateRecords.push_back(
3277               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3278       }
3279       break;
3280 
3281     case OBJC_CATEGORIES_MAP:
3282       if (F.LocalNumObjCCategoriesInMap != 0) {
3283         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3284         return Failure;
3285       }
3286 
3287       F.LocalNumObjCCategoriesInMap = Record[0];
3288       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3289       break;
3290 
3291     case OBJC_CATEGORIES:
3292       F.ObjCCategories.swap(Record);
3293       break;
3294 
3295     case CUDA_SPECIAL_DECL_REFS:
3296       // Later tables overwrite earlier ones.
3297       // FIXME: Modules will have trouble with this.
3298       CUDASpecialDeclRefs.clear();
3299       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3300         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3301       break;
3302 
3303     case HEADER_SEARCH_TABLE:
3304       F.HeaderFileInfoTableData = Blob.data();
3305       F.LocalNumHeaderFileInfos = Record[1];
3306       if (Record[0]) {
3307         F.HeaderFileInfoTable
3308           = HeaderFileInfoLookupTable::Create(
3309                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3310                    (const unsigned char *)F.HeaderFileInfoTableData,
3311                    HeaderFileInfoTrait(*this, F,
3312                                        &PP.getHeaderSearchInfo(),
3313                                        Blob.data() + Record[2]));
3314 
3315         PP.getHeaderSearchInfo().SetExternalSource(this);
3316         if (!PP.getHeaderSearchInfo().getExternalLookup())
3317           PP.getHeaderSearchInfo().SetExternalLookup(this);
3318       }
3319       break;
3320 
3321     case FP_PRAGMA_OPTIONS:
3322       // Later tables overwrite earlier ones.
3323       FPPragmaOptions.swap(Record);
3324       break;
3325 
3326     case OPENCL_EXTENSIONS:
3327       for (unsigned I = 0, E = Record.size(); I != E; ) {
3328         auto Name = ReadString(Record, I);
3329         auto &Opt = OpenCLExtensions.OptMap[Name];
3330         Opt.Supported = Record[I++] != 0;
3331         Opt.Enabled = Record[I++] != 0;
3332         Opt.Avail = Record[I++];
3333         Opt.Core = Record[I++];
3334       }
3335       break;
3336 
3337     case OPENCL_EXTENSION_TYPES:
3338       for (unsigned I = 0, E = Record.size(); I != E;) {
3339         auto TypeID = static_cast<::TypeID>(Record[I++]);
3340         auto *Type = GetType(TypeID).getTypePtr();
3341         auto NumExt = static_cast<unsigned>(Record[I++]);
3342         for (unsigned II = 0; II != NumExt; ++II) {
3343           auto Ext = ReadString(Record, I);
3344           OpenCLTypeExtMap[Type].insert(Ext);
3345         }
3346       }
3347       break;
3348 
3349     case OPENCL_EXTENSION_DECLS:
3350       for (unsigned I = 0, E = Record.size(); I != E;) {
3351         auto DeclID = static_cast<::DeclID>(Record[I++]);
3352         auto *Decl = GetDecl(DeclID);
3353         auto NumExt = static_cast<unsigned>(Record[I++]);
3354         for (unsigned II = 0; II != NumExt; ++II) {
3355           auto Ext = ReadString(Record, I);
3356           OpenCLDeclExtMap[Decl].insert(Ext);
3357         }
3358       }
3359       break;
3360 
3361     case TENTATIVE_DEFINITIONS:
3362       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3363         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3364       break;
3365 
3366     case KNOWN_NAMESPACES:
3367       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3368         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3369       break;
3370 
3371     case UNDEFINED_BUT_USED:
3372       if (UndefinedButUsed.size() % 2 != 0) {
3373         Error("Invalid existing UndefinedButUsed");
3374         return Failure;
3375       }
3376 
3377       if (Record.size() % 2 != 0) {
3378         Error("invalid undefined-but-used record");
3379         return Failure;
3380       }
3381       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3382         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3383         UndefinedButUsed.push_back(
3384             ReadSourceLocation(F, Record, I).getRawEncoding());
3385       }
3386       break;
3387 
3388     case DELETE_EXPRS_TO_ANALYZE:
3389       for (unsigned I = 0, N = Record.size(); I != N;) {
3390         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3391         const uint64_t Count = Record[I++];
3392         DelayedDeleteExprs.push_back(Count);
3393         for (uint64_t C = 0; C < Count; ++C) {
3394           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3395           bool IsArrayForm = Record[I++] == 1;
3396           DelayedDeleteExprs.push_back(IsArrayForm);
3397         }
3398       }
3399       break;
3400 
3401     case IMPORTED_MODULES:
3402       if (!F.isModule()) {
3403         // If we aren't loading a module (which has its own exports), make
3404         // all of the imported modules visible.
3405         // FIXME: Deal with macros-only imports.
3406         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3407           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3408           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3409           if (GlobalID) {
3410             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3411             if (DeserializationListener)
3412               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3413           }
3414         }
3415       }
3416       break;
3417 
3418     case MACRO_OFFSET: {
3419       if (F.LocalNumMacros != 0) {
3420         Error("duplicate MACRO_OFFSET record in AST file");
3421         return Failure;
3422       }
3423       F.MacroOffsets = (const uint32_t *)Blob.data();
3424       F.LocalNumMacros = Record[0];
3425       unsigned LocalBaseMacroID = Record[1];
3426       F.BaseMacroID = getTotalNumMacros();
3427 
3428       if (F.LocalNumMacros > 0) {
3429         // Introduce the global -> local mapping for macros within this module.
3430         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3431 
3432         // Introduce the local -> global mapping for macros within this module.
3433         F.MacroRemap.insertOrReplace(
3434           std::make_pair(LocalBaseMacroID,
3435                          F.BaseMacroID - LocalBaseMacroID));
3436 
3437         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3438       }
3439       break;
3440     }
3441 
3442     case LATE_PARSED_TEMPLATE:
3443       LateParsedTemplates.append(Record.begin(), Record.end());
3444       break;
3445 
3446     case OPTIMIZE_PRAGMA_OPTIONS:
3447       if (Record.size() != 1) {
3448         Error("invalid pragma optimize record");
3449         return Failure;
3450       }
3451       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3452       break;
3453 
3454     case MSSTRUCT_PRAGMA_OPTIONS:
3455       if (Record.size() != 1) {
3456         Error("invalid pragma ms_struct record");
3457         return Failure;
3458       }
3459       PragmaMSStructState = Record[0];
3460       break;
3461 
3462     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3463       if (Record.size() != 2) {
3464         Error("invalid pragma ms_struct record");
3465         return Failure;
3466       }
3467       PragmaMSPointersToMembersState = Record[0];
3468       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3469       break;
3470 
3471     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3472       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3473         UnusedLocalTypedefNameCandidates.push_back(
3474             getGlobalDeclID(F, Record[I]));
3475       break;
3476 
3477     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3478       if (Record.size() != 1) {
3479         Error("invalid cuda pragma options record");
3480         return Failure;
3481       }
3482       ForceCUDAHostDeviceDepth = Record[0];
3483       break;
3484 
3485     case PACK_PRAGMA_OPTIONS: {
3486       if (Record.size() < 3) {
3487         Error("invalid pragma pack record");
3488         return Failure;
3489       }
3490       PragmaPackCurrentValue = Record[0];
3491       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3492       unsigned NumStackEntries = Record[2];
3493       unsigned Idx = 3;
3494       // Reset the stack when importing a new module.
3495       PragmaPackStack.clear();
3496       for (unsigned I = 0; I < NumStackEntries; ++I) {
3497         PragmaPackStackEntry Entry;
3498         Entry.Value = Record[Idx++];
3499         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3500         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3501         PragmaPackStrings.push_back(ReadString(Record, Idx));
3502         Entry.SlotLabel = PragmaPackStrings.back();
3503         PragmaPackStack.push_back(Entry);
3504       }
3505       break;
3506     }
3507     }
3508   }
3509 }
3510 
3511 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3512   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3513 
3514   // Additional remapping information.
3515   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3516   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3517   F.ModuleOffsetMap = StringRef();
3518 
3519   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3520   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3521     F.SLocRemap.insert(std::make_pair(0U, 0));
3522     F.SLocRemap.insert(std::make_pair(2U, 1));
3523   }
3524 
3525   // Continuous range maps we may be updating in our module.
3526   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3527   RemapBuilder SLocRemap(F.SLocRemap);
3528   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3529   RemapBuilder MacroRemap(F.MacroRemap);
3530   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3531   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3532   RemapBuilder SelectorRemap(F.SelectorRemap);
3533   RemapBuilder DeclRemap(F.DeclRemap);
3534   RemapBuilder TypeRemap(F.TypeRemap);
3535 
3536   while (Data < DataEnd) {
3537     // FIXME: Looking up dependency modules by filename is horrible. Let's
3538     // start fixing this with prebuilt and explicit modules and see how it
3539     // goes...
3540     using namespace llvm::support;
3541     ModuleKind Kind = static_cast<ModuleKind>(
3542       endian::readNext<uint8_t, little, unaligned>(Data));
3543     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3544     StringRef Name = StringRef((const char*)Data, Len);
3545     Data += Len;
3546     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3547                       ? ModuleMgr.lookupByModuleName(Name)
3548                       : ModuleMgr.lookupByFileName(Name));
3549     if (!OM) {
3550       std::string Msg =
3551           "SourceLocation remap refers to unknown module, cannot find ";
3552       Msg.append(Name);
3553       Error(Msg);
3554       return;
3555     }
3556 
3557     uint32_t SLocOffset =
3558         endian::readNext<uint32_t, little, unaligned>(Data);
3559     uint32_t IdentifierIDOffset =
3560         endian::readNext<uint32_t, little, unaligned>(Data);
3561     uint32_t MacroIDOffset =
3562         endian::readNext<uint32_t, little, unaligned>(Data);
3563     uint32_t PreprocessedEntityIDOffset =
3564         endian::readNext<uint32_t, little, unaligned>(Data);
3565     uint32_t SubmoduleIDOffset =
3566         endian::readNext<uint32_t, little, unaligned>(Data);
3567     uint32_t SelectorIDOffset =
3568         endian::readNext<uint32_t, little, unaligned>(Data);
3569     uint32_t DeclIDOffset =
3570         endian::readNext<uint32_t, little, unaligned>(Data);
3571     uint32_t TypeIndexOffset =
3572         endian::readNext<uint32_t, little, unaligned>(Data);
3573 
3574     uint32_t None = std::numeric_limits<uint32_t>::max();
3575 
3576     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3577                          RemapBuilder &Remap) {
3578       if (Offset != None)
3579         Remap.insert(std::make_pair(Offset,
3580                                     static_cast<int>(BaseOffset - Offset)));
3581     };
3582     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3583     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3584     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3585     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3586               PreprocessedEntityRemap);
3587     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3588     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3589     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3590     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3591 
3592     // Global -> local mappings.
3593     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3594   }
3595 }
3596 
3597 ASTReader::ASTReadResult
3598 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3599                                   const ModuleFile *ImportedBy,
3600                                   unsigned ClientLoadCapabilities) {
3601   unsigned Idx = 0;
3602   F.ModuleMapPath = ReadPath(F, Record, Idx);
3603 
3604   // Try to resolve ModuleName in the current header search context and
3605   // verify that it is found in the same module map file as we saved. If the
3606   // top-level AST file is a main file, skip this check because there is no
3607   // usable header search context.
3608   assert(!F.ModuleName.empty() &&
3609          "MODULE_NAME should come before MODULE_MAP_FILE");
3610   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3611     // An implicitly-loaded module file should have its module listed in some
3612     // module map file that we've already loaded.
3613     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3614     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3615     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3616     // Don't emit module relocation error if we have -fno-validate-pch
3617     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3618       assert(ImportedBy && "top-level import should be verified");
3619       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3620         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3621           // This module was defined by an imported (explicit) module.
3622           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3623                                                << ASTFE->getName();
3624         } else {
3625           // This module was built with a different module map.
3626           Diag(diag::err_imported_module_not_found)
3627               << F.ModuleName << F.FileName << ImportedBy->FileName
3628               << F.ModuleMapPath;
3629           // In case it was imported by a PCH, there's a chance the user is
3630           // just missing to include the search path to the directory containing
3631           // the modulemap.
3632           if (ImportedBy->Kind == MK_PCH)
3633             Diag(diag::note_imported_by_pch_module_not_found)
3634                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3635         }
3636       }
3637       return OutOfDate;
3638     }
3639 
3640     assert(M->Name == F.ModuleName && "found module with different name");
3641 
3642     // Check the primary module map file.
3643     const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3644     if (StoredModMap == nullptr || StoredModMap != ModMap) {
3645       assert(ModMap && "found module is missing module map file");
3646       assert(ImportedBy && "top-level import should be verified");
3647       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3648         Diag(diag::err_imported_module_modmap_changed)
3649           << F.ModuleName << ImportedBy->FileName
3650           << ModMap->getName() << F.ModuleMapPath;
3651       return OutOfDate;
3652     }
3653 
3654     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3655     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3656       // FIXME: we should use input files rather than storing names.
3657       std::string Filename = ReadPath(F, Record, Idx);
3658       const FileEntry *F =
3659           FileMgr.getFile(Filename, false, false);
3660       if (F == nullptr) {
3661         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3662           Error("could not find file '" + Filename +"' referenced by AST file");
3663         return OutOfDate;
3664       }
3665       AdditionalStoredMaps.insert(F);
3666     }
3667 
3668     // Check any additional module map files (e.g. module.private.modulemap)
3669     // that are not in the pcm.
3670     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3671       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3672         // Remove files that match
3673         // Note: SmallPtrSet::erase is really remove
3674         if (!AdditionalStoredMaps.erase(ModMap)) {
3675           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3676             Diag(diag::err_module_different_modmap)
3677               << F.ModuleName << /*new*/0 << ModMap->getName();
3678           return OutOfDate;
3679         }
3680       }
3681     }
3682 
3683     // Check any additional module map files that are in the pcm, but not
3684     // found in header search. Cases that match are already removed.
3685     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3686       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3687         Diag(diag::err_module_different_modmap)
3688           << F.ModuleName << /*not new*/1 << ModMap->getName();
3689       return OutOfDate;
3690     }
3691   }
3692 
3693   if (Listener)
3694     Listener->ReadModuleMapFile(F.ModuleMapPath);
3695   return Success;
3696 }
3697 
3698 /// Move the given method to the back of the global list of methods.
3699 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3700   // Find the entry for this selector in the method pool.
3701   Sema::GlobalMethodPool::iterator Known
3702     = S.MethodPool.find(Method->getSelector());
3703   if (Known == S.MethodPool.end())
3704     return;
3705 
3706   // Retrieve the appropriate method list.
3707   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3708                                                     : Known->second.second;
3709   bool Found = false;
3710   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3711     if (!Found) {
3712       if (List->getMethod() == Method) {
3713         Found = true;
3714       } else {
3715         // Keep searching.
3716         continue;
3717       }
3718     }
3719 
3720     if (List->getNext())
3721       List->setMethod(List->getNext()->getMethod());
3722     else
3723       List->setMethod(Method);
3724   }
3725 }
3726 
3727 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3728   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3729   for (Decl *D : Names) {
3730     bool wasHidden = D->isHidden();
3731     D->setVisibleDespiteOwningModule();
3732 
3733     if (wasHidden && SemaObj) {
3734       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3735         moveMethodToBackOfGlobalList(*SemaObj, Method);
3736       }
3737     }
3738   }
3739 }
3740 
3741 void ASTReader::makeModuleVisible(Module *Mod,
3742                                   Module::NameVisibilityKind NameVisibility,
3743                                   SourceLocation ImportLoc) {
3744   llvm::SmallPtrSet<Module *, 4> Visited;
3745   SmallVector<Module *, 4> Stack;
3746   Stack.push_back(Mod);
3747   while (!Stack.empty()) {
3748     Mod = Stack.pop_back_val();
3749 
3750     if (NameVisibility <= Mod->NameVisibility) {
3751       // This module already has this level of visibility (or greater), so
3752       // there is nothing more to do.
3753       continue;
3754     }
3755 
3756     if (!Mod->isAvailable()) {
3757       // Modules that aren't available cannot be made visible.
3758       continue;
3759     }
3760 
3761     // Update the module's name visibility.
3762     Mod->NameVisibility = NameVisibility;
3763 
3764     // If we've already deserialized any names from this module,
3765     // mark them as visible.
3766     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3767     if (Hidden != HiddenNamesMap.end()) {
3768       auto HiddenNames = std::move(*Hidden);
3769       HiddenNamesMap.erase(Hidden);
3770       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3771       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3772              "making names visible added hidden names");
3773     }
3774 
3775     // Push any exported modules onto the stack to be marked as visible.
3776     SmallVector<Module *, 16> Exports;
3777     Mod->getExportedModules(Exports);
3778     for (SmallVectorImpl<Module *>::iterator
3779            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3780       Module *Exported = *I;
3781       if (Visited.insert(Exported).second)
3782         Stack.push_back(Exported);
3783     }
3784   }
3785 }
3786 
3787 /// We've merged the definition \p MergedDef into the existing definition
3788 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3789 /// visible.
3790 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3791                                           NamedDecl *MergedDef) {
3792   if (Def->isHidden()) {
3793     // If MergedDef is visible or becomes visible, make the definition visible.
3794     if (!MergedDef->isHidden())
3795       Def->setVisibleDespiteOwningModule();
3796     else {
3797       getContext().mergeDefinitionIntoModule(
3798           Def, MergedDef->getImportedOwningModule(),
3799           /*NotifyListeners*/ false);
3800       PendingMergedDefinitionsToDeduplicate.insert(Def);
3801     }
3802   }
3803 }
3804 
3805 bool ASTReader::loadGlobalIndex() {
3806   if (GlobalIndex)
3807     return false;
3808 
3809   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3810       !PP.getLangOpts().Modules)
3811     return true;
3812 
3813   // Try to load the global index.
3814   TriedLoadingGlobalIndex = true;
3815   StringRef ModuleCachePath
3816     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3817   std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3818     = GlobalModuleIndex::readIndex(ModuleCachePath);
3819   if (!Result.first)
3820     return true;
3821 
3822   GlobalIndex.reset(Result.first);
3823   ModuleMgr.setGlobalIndex(GlobalIndex.get());
3824   return false;
3825 }
3826 
3827 bool ASTReader::isGlobalIndexUnavailable() const {
3828   return PP.getLangOpts().Modules && UseGlobalIndex &&
3829          !hasGlobalIndex() && TriedLoadingGlobalIndex;
3830 }
3831 
3832 static void updateModuleTimestamp(ModuleFile &MF) {
3833   // Overwrite the timestamp file contents so that file's mtime changes.
3834   std::string TimestampFilename = MF.getTimestampFilename();
3835   std::error_code EC;
3836   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3837   if (EC)
3838     return;
3839   OS << "Timestamp file\n";
3840   OS.close();
3841   OS.clear_error(); // Avoid triggering a fatal error.
3842 }
3843 
3844 /// Given a cursor at the start of an AST file, scan ahead and drop the
3845 /// cursor into the start of the given block ID, returning false on success and
3846 /// true on failure.
3847 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3848   while (true) {
3849     llvm::BitstreamEntry Entry = Cursor.advance();
3850     switch (Entry.Kind) {
3851     case llvm::BitstreamEntry::Error:
3852     case llvm::BitstreamEntry::EndBlock:
3853       return true;
3854 
3855     case llvm::BitstreamEntry::Record:
3856       // Ignore top-level records.
3857       Cursor.skipRecord(Entry.ID);
3858       break;
3859 
3860     case llvm::BitstreamEntry::SubBlock:
3861       if (Entry.ID == BlockID) {
3862         if (Cursor.EnterSubBlock(BlockID))
3863           return true;
3864         // Found it!
3865         return false;
3866       }
3867 
3868       if (Cursor.SkipBlock())
3869         return true;
3870     }
3871   }
3872 }
3873 
3874 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
3875                                             ModuleKind Type,
3876                                             SourceLocation ImportLoc,
3877                                             unsigned ClientLoadCapabilities,
3878                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
3879   llvm::SaveAndRestore<SourceLocation>
3880     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3881 
3882   // Defer any pending actions until we get to the end of reading the AST file.
3883   Deserializing AnASTFile(this);
3884 
3885   // Bump the generation number.
3886   unsigned PreviousGeneration = 0;
3887   if (ContextObj)
3888     PreviousGeneration = incrementGeneration(*ContextObj);
3889 
3890   unsigned NumModules = ModuleMgr.size();
3891   SmallVector<ImportedModule, 4> Loaded;
3892   switch (ASTReadResult ReadResult =
3893               ReadASTCore(FileName, Type, ImportLoc,
3894                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
3895                           ASTFileSignature(), ClientLoadCapabilities)) {
3896   case Failure:
3897   case Missing:
3898   case OutOfDate:
3899   case VersionMismatch:
3900   case ConfigurationMismatch:
3901   case HadErrors: {
3902     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3903     for (const ImportedModule &IM : Loaded)
3904       LoadedSet.insert(IM.Mod);
3905 
3906     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
3907                             PP.getLangOpts().Modules
3908                                 ? &PP.getHeaderSearchInfo().getModuleMap()
3909                                 : nullptr);
3910 
3911     // If we find that any modules are unusable, the global index is going
3912     // to be out-of-date. Just remove it.
3913     GlobalIndex.reset();
3914     ModuleMgr.setGlobalIndex(nullptr);
3915     return ReadResult;
3916   }
3917   case Success:
3918     break;
3919   }
3920 
3921   // Here comes stuff that we only do once the entire chain is loaded.
3922 
3923   // Load the AST blocks of all of the modules that we loaded.
3924   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3925                                               MEnd = Loaded.end();
3926        M != MEnd; ++M) {
3927     ModuleFile &F = *M->Mod;
3928 
3929     // Read the AST block.
3930     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3931       return Result;
3932 
3933     // Read the extension blocks.
3934     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3935       if (ASTReadResult Result = ReadExtensionBlock(F))
3936         return Result;
3937     }
3938 
3939     // Once read, set the ModuleFile bit base offset and update the size in
3940     // bits of all files we've seen.
3941     F.GlobalBitOffset = TotalModulesSizeInBits;
3942     TotalModulesSizeInBits += F.SizeInBits;
3943     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3944 
3945     // Preload SLocEntries.
3946     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3947       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3948       // Load it through the SourceManager and don't call ReadSLocEntry()
3949       // directly because the entry may have already been loaded in which case
3950       // calling ReadSLocEntry() directly would trigger an assertion in
3951       // SourceManager.
3952       SourceMgr.getLoadedSLocEntryByID(Index);
3953     }
3954 
3955     // Map the original source file ID into the ID space of the current
3956     // compilation.
3957     if (F.OriginalSourceFileID.isValid()) {
3958       F.OriginalSourceFileID = FileID::get(
3959           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
3960     }
3961 
3962     // Preload all the pending interesting identifiers by marking them out of
3963     // date.
3964     for (auto Offset : F.PreloadIdentifierOffsets) {
3965       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3966           F.IdentifierTableData + Offset);
3967 
3968       ASTIdentifierLookupTrait Trait(*this, F);
3969       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3970       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
3971       auto &II = PP.getIdentifierTable().getOwn(Key);
3972       II.setOutOfDate(true);
3973 
3974       // Mark this identifier as being from an AST file so that we can track
3975       // whether we need to serialize it.
3976       markIdentifierFromAST(*this, II);
3977 
3978       // Associate the ID with the identifier so that the writer can reuse it.
3979       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3980       SetIdentifierInfo(ID, &II);
3981     }
3982   }
3983 
3984   // Setup the import locations and notify the module manager that we've
3985   // committed to these module files.
3986   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3987                                               MEnd = Loaded.end();
3988        M != MEnd; ++M) {
3989     ModuleFile &F = *M->Mod;
3990 
3991     ModuleMgr.moduleFileAccepted(&F);
3992 
3993     // Set the import location.
3994     F.DirectImportLoc = ImportLoc;
3995     // FIXME: We assume that locations from PCH / preamble do not need
3996     // any translation.
3997     if (!M->ImportedBy)
3998       F.ImportLoc = M->ImportLoc;
3999     else
4000       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4001   }
4002 
4003   if (!PP.getLangOpts().CPlusPlus ||
4004       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4005        Type != MK_PrebuiltModule)) {
4006     // Mark all of the identifiers in the identifier table as being out of date,
4007     // so that various accessors know to check the loaded modules when the
4008     // identifier is used.
4009     //
4010     // For C++ modules, we don't need information on many identifiers (just
4011     // those that provide macros or are poisoned), so we mark all of
4012     // the interesting ones via PreloadIdentifierOffsets.
4013     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4014                                 IdEnd = PP.getIdentifierTable().end();
4015          Id != IdEnd; ++Id)
4016       Id->second->setOutOfDate(true);
4017   }
4018   // Mark selectors as out of date.
4019   for (auto Sel : SelectorGeneration)
4020     SelectorOutOfDate[Sel.first] = true;
4021 
4022   // Resolve any unresolved module exports.
4023   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4024     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4025     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4026     Module *ResolvedMod = getSubmodule(GlobalID);
4027 
4028     switch (Unresolved.Kind) {
4029     case UnresolvedModuleRef::Conflict:
4030       if (ResolvedMod) {
4031         Module::Conflict Conflict;
4032         Conflict.Other = ResolvedMod;
4033         Conflict.Message = Unresolved.String.str();
4034         Unresolved.Mod->Conflicts.push_back(Conflict);
4035       }
4036       continue;
4037 
4038     case UnresolvedModuleRef::Import:
4039       if (ResolvedMod)
4040         Unresolved.Mod->Imports.insert(ResolvedMod);
4041       continue;
4042 
4043     case UnresolvedModuleRef::Export:
4044       if (ResolvedMod || Unresolved.IsWildcard)
4045         Unresolved.Mod->Exports.push_back(
4046           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4047       continue;
4048     }
4049   }
4050   UnresolvedModuleRefs.clear();
4051 
4052   if (Imported)
4053     Imported->append(ImportedModules.begin(),
4054                      ImportedModules.end());
4055 
4056   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4057   // Might be unnecessary as use declarations are only used to build the
4058   // module itself.
4059 
4060   if (ContextObj)
4061     InitializeContext();
4062 
4063   if (SemaObj)
4064     UpdateSema();
4065 
4066   if (DeserializationListener)
4067     DeserializationListener->ReaderInitialized(this);
4068 
4069   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4070   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4071     // If this AST file is a precompiled preamble, then set the
4072     // preamble file ID of the source manager to the file source file
4073     // from which the preamble was built.
4074     if (Type == MK_Preamble) {
4075       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4076     } else if (Type == MK_MainFile) {
4077       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4078     }
4079   }
4080 
4081   // For any Objective-C class definitions we have already loaded, make sure
4082   // that we load any additional categories.
4083   if (ContextObj) {
4084     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4085       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4086                          ObjCClassesLoaded[I],
4087                          PreviousGeneration);
4088     }
4089   }
4090 
4091   if (PP.getHeaderSearchInfo()
4092           .getHeaderSearchOpts()
4093           .ModulesValidateOncePerBuildSession) {
4094     // Now we are certain that the module and all modules it depends on are
4095     // up to date.  Create or update timestamp files for modules that are
4096     // located in the module cache (not for PCH files that could be anywhere
4097     // in the filesystem).
4098     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4099       ImportedModule &M = Loaded[I];
4100       if (M.Mod->Kind == MK_ImplicitModule) {
4101         updateModuleTimestamp(*M.Mod);
4102       }
4103     }
4104   }
4105 
4106   return Success;
4107 }
4108 
4109 static ASTFileSignature readASTFileSignature(StringRef PCH);
4110 
4111 /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
4112 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
4113   return Stream.canSkipToPos(4) &&
4114          Stream.Read(8) == 'C' &&
4115          Stream.Read(8) == 'P' &&
4116          Stream.Read(8) == 'C' &&
4117          Stream.Read(8) == 'H';
4118 }
4119 
4120 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4121   switch (Kind) {
4122   case MK_PCH:
4123     return 0; // PCH
4124   case MK_ImplicitModule:
4125   case MK_ExplicitModule:
4126   case MK_PrebuiltModule:
4127     return 1; // module
4128   case MK_MainFile:
4129   case MK_Preamble:
4130     return 2; // main source file
4131   }
4132   llvm_unreachable("unknown module kind");
4133 }
4134 
4135 ASTReader::ASTReadResult
4136 ASTReader::ReadASTCore(StringRef FileName,
4137                        ModuleKind Type,
4138                        SourceLocation ImportLoc,
4139                        ModuleFile *ImportedBy,
4140                        SmallVectorImpl<ImportedModule> &Loaded,
4141                        off_t ExpectedSize, time_t ExpectedModTime,
4142                        ASTFileSignature ExpectedSignature,
4143                        unsigned ClientLoadCapabilities) {
4144   ModuleFile *M;
4145   std::string ErrorStr;
4146   ModuleManager::AddModuleResult AddResult
4147     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4148                           getGeneration(), ExpectedSize, ExpectedModTime,
4149                           ExpectedSignature, readASTFileSignature,
4150                           M, ErrorStr);
4151 
4152   switch (AddResult) {
4153   case ModuleManager::AlreadyLoaded:
4154     Diag(diag::remark_module_import)
4155         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4156         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4157     return Success;
4158 
4159   case ModuleManager::NewlyLoaded:
4160     // Load module file below.
4161     break;
4162 
4163   case ModuleManager::Missing:
4164     // The module file was missing; if the client can handle that, return
4165     // it.
4166     if (ClientLoadCapabilities & ARR_Missing)
4167       return Missing;
4168 
4169     // Otherwise, return an error.
4170     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4171                                           << FileName << !ErrorStr.empty()
4172                                           << ErrorStr;
4173     return Failure;
4174 
4175   case ModuleManager::OutOfDate:
4176     // We couldn't load the module file because it is out-of-date. If the
4177     // client can handle out-of-date, return it.
4178     if (ClientLoadCapabilities & ARR_OutOfDate)
4179       return OutOfDate;
4180 
4181     // Otherwise, return an error.
4182     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4183                                             << FileName << !ErrorStr.empty()
4184                                             << ErrorStr;
4185     return Failure;
4186   }
4187 
4188   assert(M && "Missing module file");
4189 
4190   bool ShouldFinalizePCM = false;
4191   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4192     auto &MC = getModuleManager().getModuleCache();
4193     if (ShouldFinalizePCM)
4194       MC.finalizePCM(FileName);
4195     else
4196       MC.tryToDropPCM(FileName);
4197   });
4198   ModuleFile &F = *M;
4199   BitstreamCursor &Stream = F.Stream;
4200   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4201   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4202 
4203   // Sniff for the signature.
4204   if (!startsWithASTFileMagic(Stream)) {
4205     Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
4206                                         << FileName;
4207     return Failure;
4208   }
4209 
4210   // This is used for compatibility with older PCH formats.
4211   bool HaveReadControlBlock = false;
4212   while (true) {
4213     llvm::BitstreamEntry Entry = Stream.advance();
4214 
4215     switch (Entry.Kind) {
4216     case llvm::BitstreamEntry::Error:
4217     case llvm::BitstreamEntry::Record:
4218     case llvm::BitstreamEntry::EndBlock:
4219       Error("invalid record at top-level of AST file");
4220       return Failure;
4221 
4222     case llvm::BitstreamEntry::SubBlock:
4223       break;
4224     }
4225 
4226     switch (Entry.ID) {
4227     case CONTROL_BLOCK_ID:
4228       HaveReadControlBlock = true;
4229       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4230       case Success:
4231         // Check that we didn't try to load a non-module AST file as a module.
4232         //
4233         // FIXME: Should we also perform the converse check? Loading a module as
4234         // a PCH file sort of works, but it's a bit wonky.
4235         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4236              Type == MK_PrebuiltModule) &&
4237             F.ModuleName.empty()) {
4238           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4239           if (Result != OutOfDate ||
4240               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4241             Diag(diag::err_module_file_not_module) << FileName;
4242           return Result;
4243         }
4244         break;
4245 
4246       case Failure: return Failure;
4247       case Missing: return Missing;
4248       case OutOfDate: return OutOfDate;
4249       case VersionMismatch: return VersionMismatch;
4250       case ConfigurationMismatch: return ConfigurationMismatch;
4251       case HadErrors: return HadErrors;
4252       }
4253       break;
4254 
4255     case AST_BLOCK_ID:
4256       if (!HaveReadControlBlock) {
4257         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4258           Diag(diag::err_pch_version_too_old);
4259         return VersionMismatch;
4260       }
4261 
4262       // Record that we've loaded this module.
4263       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4264       ShouldFinalizePCM = true;
4265       return Success;
4266 
4267     case UNHASHED_CONTROL_BLOCK_ID:
4268       // This block is handled using look-ahead during ReadControlBlock.  We
4269       // shouldn't get here!
4270       Error("malformed block record in AST file");
4271       return Failure;
4272 
4273     default:
4274       if (Stream.SkipBlock()) {
4275         Error("malformed block record in AST file");
4276         return Failure;
4277       }
4278       break;
4279     }
4280   }
4281 
4282   llvm_unreachable("unexpected break; expected return");
4283 }
4284 
4285 ASTReader::ASTReadResult
4286 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4287                                     unsigned ClientLoadCapabilities) {
4288   const HeaderSearchOptions &HSOpts =
4289       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4290   bool AllowCompatibleConfigurationMismatch =
4291       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4292 
4293   ASTReadResult Result = readUnhashedControlBlockImpl(
4294       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4295       Listener.get(),
4296       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4297 
4298   // If F was directly imported by another module, it's implicitly validated by
4299   // the importing module.
4300   if (DisableValidation || WasImportedBy ||
4301       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4302     return Success;
4303 
4304   if (Result == Failure) {
4305     Error("malformed block record in AST file");
4306     return Failure;
4307   }
4308 
4309   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4310     // If this module has already been finalized in the ModuleCache, we're stuck
4311     // with it; we can only load a single version of each module.
4312     //
4313     // This can happen when a module is imported in two contexts: in one, as a
4314     // user module; in another, as a system module (due to an import from
4315     // another module marked with the [system] flag).  It usually indicates a
4316     // bug in the module map: this module should also be marked with [system].
4317     //
4318     // If -Wno-system-headers (the default), and the first import is as a
4319     // system module, then validation will fail during the as-user import,
4320     // since -Werror flags won't have been validated.  However, it's reasonable
4321     // to treat this consistently as a system module.
4322     //
4323     // If -Wsystem-headers, the PCM on disk was built with
4324     // -Wno-system-headers, and the first import is as a user module, then
4325     // validation will fail during the as-system import since the PCM on disk
4326     // doesn't guarantee that -Werror was respected.  However, the -Werror
4327     // flags were checked during the initial as-user import.
4328     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4329       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4330       return Success;
4331     }
4332   }
4333 
4334   return Result;
4335 }
4336 
4337 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4338     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4339     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4340     bool ValidateDiagnosticOptions) {
4341   // Initialize a stream.
4342   BitstreamCursor Stream(StreamData);
4343 
4344   // Sniff for the signature.
4345   if (!startsWithASTFileMagic(Stream))
4346     return Failure;
4347 
4348   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4349   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4350     return Failure;
4351 
4352   // Read all of the records in the options block.
4353   RecordData Record;
4354   ASTReadResult Result = Success;
4355   while (true) {
4356     llvm::BitstreamEntry Entry = Stream.advance();
4357 
4358     switch (Entry.Kind) {
4359     case llvm::BitstreamEntry::Error:
4360     case llvm::BitstreamEntry::SubBlock:
4361       return Failure;
4362 
4363     case llvm::BitstreamEntry::EndBlock:
4364       return Result;
4365 
4366     case llvm::BitstreamEntry::Record:
4367       // The interesting case.
4368       break;
4369     }
4370 
4371     // Read and process a record.
4372     Record.clear();
4373     switch (
4374         (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
4375     case SIGNATURE:
4376       if (F)
4377         std::copy(Record.begin(), Record.end(), F->Signature.data());
4378       break;
4379     case DIAGNOSTIC_OPTIONS: {
4380       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4381       if (Listener && ValidateDiagnosticOptions &&
4382           !AllowCompatibleConfigurationMismatch &&
4383           ParseDiagnosticOptions(Record, Complain, *Listener))
4384         Result = OutOfDate; // Don't return early.  Read the signature.
4385       break;
4386     }
4387     case DIAG_PRAGMA_MAPPINGS:
4388       if (!F)
4389         break;
4390       if (F->PragmaDiagMappings.empty())
4391         F->PragmaDiagMappings.swap(Record);
4392       else
4393         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4394                                      Record.begin(), Record.end());
4395       break;
4396     }
4397   }
4398 }
4399 
4400 /// Parse a record and blob containing module file extension metadata.
4401 static bool parseModuleFileExtensionMetadata(
4402               const SmallVectorImpl<uint64_t> &Record,
4403               StringRef Blob,
4404               ModuleFileExtensionMetadata &Metadata) {
4405   if (Record.size() < 4) return true;
4406 
4407   Metadata.MajorVersion = Record[0];
4408   Metadata.MinorVersion = Record[1];
4409 
4410   unsigned BlockNameLen = Record[2];
4411   unsigned UserInfoLen = Record[3];
4412 
4413   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4414 
4415   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4416   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4417                                   Blob.data() + BlockNameLen + UserInfoLen);
4418   return false;
4419 }
4420 
4421 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4422   BitstreamCursor &Stream = F.Stream;
4423 
4424   RecordData Record;
4425   while (true) {
4426     llvm::BitstreamEntry Entry = Stream.advance();
4427     switch (Entry.Kind) {
4428     case llvm::BitstreamEntry::SubBlock:
4429       if (Stream.SkipBlock())
4430         return Failure;
4431 
4432       continue;
4433 
4434     case llvm::BitstreamEntry::EndBlock:
4435       return Success;
4436 
4437     case llvm::BitstreamEntry::Error:
4438       return HadErrors;
4439 
4440     case llvm::BitstreamEntry::Record:
4441       break;
4442     }
4443 
4444     Record.clear();
4445     StringRef Blob;
4446     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4447     switch (RecCode) {
4448     case EXTENSION_METADATA: {
4449       ModuleFileExtensionMetadata Metadata;
4450       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4451         return Failure;
4452 
4453       // Find a module file extension with this block name.
4454       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4455       if (Known == ModuleFileExtensions.end()) break;
4456 
4457       // Form a reader.
4458       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4459                                                              F, Stream)) {
4460         F.ExtensionReaders.push_back(std::move(Reader));
4461       }
4462 
4463       break;
4464     }
4465     }
4466   }
4467 
4468   return Success;
4469 }
4470 
4471 void ASTReader::InitializeContext() {
4472   assert(ContextObj && "no context to initialize");
4473   ASTContext &Context = *ContextObj;
4474 
4475   // If there's a listener, notify them that we "read" the translation unit.
4476   if (DeserializationListener)
4477     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4478                                       Context.getTranslationUnitDecl());
4479 
4480   // FIXME: Find a better way to deal with collisions between these
4481   // built-in types. Right now, we just ignore the problem.
4482 
4483   // Load the special types.
4484   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4485     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4486       if (!Context.CFConstantStringTypeDecl)
4487         Context.setCFConstantStringType(GetType(String));
4488     }
4489 
4490     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4491       QualType FileType = GetType(File);
4492       if (FileType.isNull()) {
4493         Error("FILE type is NULL");
4494         return;
4495       }
4496 
4497       if (!Context.FILEDecl) {
4498         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4499           Context.setFILEDecl(Typedef->getDecl());
4500         else {
4501           const TagType *Tag = FileType->getAs<TagType>();
4502           if (!Tag) {
4503             Error("Invalid FILE type in AST file");
4504             return;
4505           }
4506           Context.setFILEDecl(Tag->getDecl());
4507         }
4508       }
4509     }
4510 
4511     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4512       QualType Jmp_bufType = GetType(Jmp_buf);
4513       if (Jmp_bufType.isNull()) {
4514         Error("jmp_buf type is NULL");
4515         return;
4516       }
4517 
4518       if (!Context.jmp_bufDecl) {
4519         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4520           Context.setjmp_bufDecl(Typedef->getDecl());
4521         else {
4522           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4523           if (!Tag) {
4524             Error("Invalid jmp_buf type in AST file");
4525             return;
4526           }
4527           Context.setjmp_bufDecl(Tag->getDecl());
4528         }
4529       }
4530     }
4531 
4532     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4533       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4534       if (Sigjmp_bufType.isNull()) {
4535         Error("sigjmp_buf type is NULL");
4536         return;
4537       }
4538 
4539       if (!Context.sigjmp_bufDecl) {
4540         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4541           Context.setsigjmp_bufDecl(Typedef->getDecl());
4542         else {
4543           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4544           assert(Tag && "Invalid sigjmp_buf type in AST file");
4545           Context.setsigjmp_bufDecl(Tag->getDecl());
4546         }
4547       }
4548     }
4549 
4550     if (unsigned ObjCIdRedef
4551           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4552       if (Context.ObjCIdRedefinitionType.isNull())
4553         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4554     }
4555 
4556     if (unsigned ObjCClassRedef
4557           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4558       if (Context.ObjCClassRedefinitionType.isNull())
4559         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4560     }
4561 
4562     if (unsigned ObjCSelRedef
4563           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4564       if (Context.ObjCSelRedefinitionType.isNull())
4565         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4566     }
4567 
4568     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4569       QualType Ucontext_tType = GetType(Ucontext_t);
4570       if (Ucontext_tType.isNull()) {
4571         Error("ucontext_t type is NULL");
4572         return;
4573       }
4574 
4575       if (!Context.ucontext_tDecl) {
4576         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4577           Context.setucontext_tDecl(Typedef->getDecl());
4578         else {
4579           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4580           assert(Tag && "Invalid ucontext_t type in AST file");
4581           Context.setucontext_tDecl(Tag->getDecl());
4582         }
4583       }
4584     }
4585   }
4586 
4587   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4588 
4589   // If there were any CUDA special declarations, deserialize them.
4590   if (!CUDASpecialDeclRefs.empty()) {
4591     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4592     Context.setcudaConfigureCallDecl(
4593                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4594   }
4595 
4596   // Re-export any modules that were imported by a non-module AST file.
4597   // FIXME: This does not make macro-only imports visible again.
4598   for (auto &Import : ImportedModules) {
4599     if (Module *Imported = getSubmodule(Import.ID)) {
4600       makeModuleVisible(Imported, Module::AllVisible,
4601                         /*ImportLoc=*/Import.ImportLoc);
4602       if (Import.ImportLoc.isValid())
4603         PP.makeModuleVisible(Imported, Import.ImportLoc);
4604       // FIXME: should we tell Sema to make the module visible too?
4605     }
4606   }
4607   ImportedModules.clear();
4608 }
4609 
4610 void ASTReader::finalizeForWriting() {
4611   // Nothing to do for now.
4612 }
4613 
4614 /// Reads and return the signature record from \p PCH's control block, or
4615 /// else returns 0.
4616 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4617   BitstreamCursor Stream(PCH);
4618   if (!startsWithASTFileMagic(Stream))
4619     return ASTFileSignature();
4620 
4621   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4622   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4623     return ASTFileSignature();
4624 
4625   // Scan for SIGNATURE inside the diagnostic options block.
4626   ASTReader::RecordData Record;
4627   while (true) {
4628     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4629     if (Entry.Kind != llvm::BitstreamEntry::Record)
4630       return ASTFileSignature();
4631 
4632     Record.clear();
4633     StringRef Blob;
4634     if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4635       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4636                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4637   }
4638 }
4639 
4640 /// Retrieve the name of the original source file name
4641 /// directly from the AST file, without actually loading the AST
4642 /// file.
4643 std::string ASTReader::getOriginalSourceFile(
4644     const std::string &ASTFileName, FileManager &FileMgr,
4645     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4646   // Open the AST file.
4647   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4648   if (!Buffer) {
4649     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4650         << ASTFileName << Buffer.getError().message();
4651     return std::string();
4652   }
4653 
4654   // Initialize the stream
4655   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4656 
4657   // Sniff for the signature.
4658   if (!startsWithASTFileMagic(Stream)) {
4659     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4660     return std::string();
4661   }
4662 
4663   // Scan for the CONTROL_BLOCK_ID block.
4664   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4665     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4666     return std::string();
4667   }
4668 
4669   // Scan for ORIGINAL_FILE inside the control block.
4670   RecordData Record;
4671   while (true) {
4672     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4673     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4674       return std::string();
4675 
4676     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4677       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4678       return std::string();
4679     }
4680 
4681     Record.clear();
4682     StringRef Blob;
4683     if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4684       return Blob.str();
4685   }
4686 }
4687 
4688 namespace {
4689 
4690   class SimplePCHValidator : public ASTReaderListener {
4691     const LangOptions &ExistingLangOpts;
4692     const TargetOptions &ExistingTargetOpts;
4693     const PreprocessorOptions &ExistingPPOpts;
4694     std::string ExistingModuleCachePath;
4695     FileManager &FileMgr;
4696 
4697   public:
4698     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4699                        const TargetOptions &ExistingTargetOpts,
4700                        const PreprocessorOptions &ExistingPPOpts,
4701                        StringRef ExistingModuleCachePath,
4702                        FileManager &FileMgr)
4703       : ExistingLangOpts(ExistingLangOpts),
4704         ExistingTargetOpts(ExistingTargetOpts),
4705         ExistingPPOpts(ExistingPPOpts),
4706         ExistingModuleCachePath(ExistingModuleCachePath),
4707         FileMgr(FileMgr) {}
4708 
4709     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4710                              bool AllowCompatibleDifferences) override {
4711       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4712                                   AllowCompatibleDifferences);
4713     }
4714 
4715     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4716                            bool AllowCompatibleDifferences) override {
4717       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4718                                 AllowCompatibleDifferences);
4719     }
4720 
4721     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4722                                  StringRef SpecificModuleCachePath,
4723                                  bool Complain) override {
4724       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4725                                       ExistingModuleCachePath,
4726                                       nullptr, ExistingLangOpts);
4727     }
4728 
4729     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4730                                  bool Complain,
4731                                  std::string &SuggestedPredefines) override {
4732       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4733                                       SuggestedPredefines, ExistingLangOpts);
4734     }
4735   };
4736 
4737 } // namespace
4738 
4739 bool ASTReader::readASTFileControlBlock(
4740     StringRef Filename, FileManager &FileMgr,
4741     const PCHContainerReader &PCHContainerRdr,
4742     bool FindModuleFileExtensions,
4743     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
4744   // Open the AST file.
4745   // FIXME: This allows use of the VFS; we do not allow use of the
4746   // VFS when actually loading a module.
4747   auto Buffer = FileMgr.getBufferForFile(Filename);
4748   if (!Buffer) {
4749     return true;
4750   }
4751 
4752   // Initialize the stream
4753   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
4754   BitstreamCursor Stream(Bytes);
4755 
4756   // Sniff for the signature.
4757   if (!startsWithASTFileMagic(Stream))
4758     return true;
4759 
4760   // Scan for the CONTROL_BLOCK_ID block.
4761   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4762     return true;
4763 
4764   bool NeedsInputFiles = Listener.needsInputFileVisitation();
4765   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4766   bool NeedsImports = Listener.needsImportVisitation();
4767   BitstreamCursor InputFilesCursor;
4768 
4769   RecordData Record;
4770   std::string ModuleDir;
4771   bool DoneWithControlBlock = false;
4772   while (!DoneWithControlBlock) {
4773     llvm::BitstreamEntry Entry = Stream.advance();
4774 
4775     switch (Entry.Kind) {
4776     case llvm::BitstreamEntry::SubBlock: {
4777       switch (Entry.ID) {
4778       case OPTIONS_BLOCK_ID: {
4779         std::string IgnoredSuggestedPredefines;
4780         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4781                              /*AllowCompatibleConfigurationMismatch*/ false,
4782                              Listener, IgnoredSuggestedPredefines) != Success)
4783           return true;
4784         break;
4785       }
4786 
4787       case INPUT_FILES_BLOCK_ID:
4788         InputFilesCursor = Stream;
4789         if (Stream.SkipBlock() ||
4790             (NeedsInputFiles &&
4791              ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4792           return true;
4793         break;
4794 
4795       default:
4796         if (Stream.SkipBlock())
4797           return true;
4798         break;
4799       }
4800 
4801       continue;
4802     }
4803 
4804     case llvm::BitstreamEntry::EndBlock:
4805       DoneWithControlBlock = true;
4806       break;
4807 
4808     case llvm::BitstreamEntry::Error:
4809       return true;
4810 
4811     case llvm::BitstreamEntry::Record:
4812       break;
4813     }
4814 
4815     if (DoneWithControlBlock) break;
4816 
4817     Record.clear();
4818     StringRef Blob;
4819     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4820     switch ((ControlRecordTypes)RecCode) {
4821     case METADATA:
4822       if (Record[0] != VERSION_MAJOR)
4823         return true;
4824       if (Listener.ReadFullVersionInformation(Blob))
4825         return true;
4826       break;
4827     case MODULE_NAME:
4828       Listener.ReadModuleName(Blob);
4829       break;
4830     case MODULE_DIRECTORY:
4831       ModuleDir = Blob;
4832       break;
4833     case MODULE_MAP_FILE: {
4834       unsigned Idx = 0;
4835       auto Path = ReadString(Record, Idx);
4836       ResolveImportedPath(Path, ModuleDir);
4837       Listener.ReadModuleMapFile(Path);
4838       break;
4839     }
4840     case INPUT_FILE_OFFSETS: {
4841       if (!NeedsInputFiles)
4842         break;
4843 
4844       unsigned NumInputFiles = Record[0];
4845       unsigned NumUserFiles = Record[1];
4846       const llvm::support::unaligned_uint64_t *InputFileOffs =
4847           (const llvm::support::unaligned_uint64_t *)Blob.data();
4848       for (unsigned I = 0; I != NumInputFiles; ++I) {
4849         // Go find this input file.
4850         bool isSystemFile = I >= NumUserFiles;
4851 
4852         if (isSystemFile && !NeedsSystemInputFiles)
4853           break; // the rest are system input files
4854 
4855         BitstreamCursor &Cursor = InputFilesCursor;
4856         SavedStreamPosition SavedPosition(Cursor);
4857         Cursor.JumpToBit(InputFileOffs[I]);
4858 
4859         unsigned Code = Cursor.ReadCode();
4860         RecordData Record;
4861         StringRef Blob;
4862         bool shouldContinue = false;
4863         switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4864         case INPUT_FILE:
4865           bool Overridden = static_cast<bool>(Record[3]);
4866           std::string Filename = Blob;
4867           ResolveImportedPath(Filename, ModuleDir);
4868           shouldContinue = Listener.visitInputFile(
4869               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
4870           break;
4871         }
4872         if (!shouldContinue)
4873           break;
4874       }
4875       break;
4876     }
4877 
4878     case IMPORTS: {
4879       if (!NeedsImports)
4880         break;
4881 
4882       unsigned Idx = 0, N = Record.size();
4883       while (Idx < N) {
4884         // Read information about the AST file.
4885         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
4886         std::string ModuleName = ReadString(Record, Idx);
4887         std::string Filename = ReadString(Record, Idx);
4888         ResolveImportedPath(Filename, ModuleDir);
4889         Listener.visitImport(ModuleName, Filename);
4890       }
4891       break;
4892     }
4893 
4894     default:
4895       // No other validation to perform.
4896       break;
4897     }
4898   }
4899 
4900   // Look for module file extension blocks, if requested.
4901   if (FindModuleFileExtensions) {
4902     BitstreamCursor SavedStream = Stream;
4903     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4904       bool DoneWithExtensionBlock = false;
4905       while (!DoneWithExtensionBlock) {
4906        llvm::BitstreamEntry Entry = Stream.advance();
4907 
4908        switch (Entry.Kind) {
4909        case llvm::BitstreamEntry::SubBlock:
4910          if (Stream.SkipBlock())
4911            return true;
4912 
4913          continue;
4914 
4915        case llvm::BitstreamEntry::EndBlock:
4916          DoneWithExtensionBlock = true;
4917          continue;
4918 
4919        case llvm::BitstreamEntry::Error:
4920          return true;
4921 
4922        case llvm::BitstreamEntry::Record:
4923          break;
4924        }
4925 
4926        Record.clear();
4927        StringRef Blob;
4928        unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4929        switch (RecCode) {
4930        case EXTENSION_METADATA: {
4931          ModuleFileExtensionMetadata Metadata;
4932          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4933            return true;
4934 
4935          Listener.readModuleFileExtension(Metadata);
4936          break;
4937        }
4938        }
4939       }
4940     }
4941     Stream = SavedStream;
4942   }
4943 
4944   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4945   if (readUnhashedControlBlockImpl(
4946           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
4947           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
4948           ValidateDiagnosticOptions) != Success)
4949     return true;
4950 
4951   return false;
4952 }
4953 
4954 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
4955                                     const PCHContainerReader &PCHContainerRdr,
4956                                     const LangOptions &LangOpts,
4957                                     const TargetOptions &TargetOpts,
4958                                     const PreprocessorOptions &PPOpts,
4959                                     StringRef ExistingModuleCachePath) {
4960   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4961                                ExistingModuleCachePath, FileMgr);
4962   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4963                                   /*FindModuleFileExtensions=*/false,
4964                                   validator,
4965                                   /*ValidateDiagnosticOptions=*/true);
4966 }
4967 
4968 ASTReader::ASTReadResult
4969 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4970   // Enter the submodule block.
4971   if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4972     Error("malformed submodule block record in AST file");
4973     return Failure;
4974   }
4975 
4976   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4977   bool First = true;
4978   Module *CurrentModule = nullptr;
4979   RecordData Record;
4980   while (true) {
4981     llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4982 
4983     switch (Entry.Kind) {
4984     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4985     case llvm::BitstreamEntry::Error:
4986       Error("malformed block record in AST file");
4987       return Failure;
4988     case llvm::BitstreamEntry::EndBlock:
4989       return Success;
4990     case llvm::BitstreamEntry::Record:
4991       // The interesting case.
4992       break;
4993     }
4994 
4995     // Read a record.
4996     StringRef Blob;
4997     Record.clear();
4998     auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4999 
5000     if ((Kind == SUBMODULE_METADATA) != First) {
5001       Error("submodule metadata record should be at beginning of block");
5002       return Failure;
5003     }
5004     First = false;
5005 
5006     // Submodule information is only valid if we have a current module.
5007     // FIXME: Should we error on these cases?
5008     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5009         Kind != SUBMODULE_DEFINITION)
5010       continue;
5011 
5012     switch (Kind) {
5013     default:  // Default behavior: ignore.
5014       break;
5015 
5016     case SUBMODULE_DEFINITION: {
5017       if (Record.size() < 12) {
5018         Error("malformed module definition");
5019         return Failure;
5020       }
5021 
5022       StringRef Name = Blob;
5023       unsigned Idx = 0;
5024       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5025       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5026       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5027       bool IsFramework = Record[Idx++];
5028       bool IsExplicit = Record[Idx++];
5029       bool IsSystem = Record[Idx++];
5030       bool IsExternC = Record[Idx++];
5031       bool InferSubmodules = Record[Idx++];
5032       bool InferExplicitSubmodules = Record[Idx++];
5033       bool InferExportWildcard = Record[Idx++];
5034       bool ConfigMacrosExhaustive = Record[Idx++];
5035       bool ModuleMapIsPrivate = Record[Idx++];
5036 
5037       Module *ParentModule = nullptr;
5038       if (Parent)
5039         ParentModule = getSubmodule(Parent);
5040 
5041       // Retrieve this (sub)module from the module map, creating it if
5042       // necessary.
5043       CurrentModule =
5044           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5045               .first;
5046 
5047       // FIXME: set the definition loc for CurrentModule, or call
5048       // ModMap.setInferredModuleAllowedBy()
5049 
5050       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5051       if (GlobalIndex >= SubmodulesLoaded.size() ||
5052           SubmodulesLoaded[GlobalIndex]) {
5053         Error("too many submodules");
5054         return Failure;
5055       }
5056 
5057       if (!ParentModule) {
5058         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5059           // Don't emit module relocation error if we have -fno-validate-pch
5060           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5061               CurFile != F.File) {
5062             if (!Diags.isDiagnosticInFlight()) {
5063               Diag(diag::err_module_file_conflict)
5064                 << CurrentModule->getTopLevelModuleName()
5065                 << CurFile->getName()
5066                 << F.File->getName();
5067             }
5068             return Failure;
5069           }
5070         }
5071 
5072         CurrentModule->setASTFile(F.File);
5073         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5074       }
5075 
5076       CurrentModule->Kind = Kind;
5077       CurrentModule->Signature = F.Signature;
5078       CurrentModule->IsFromModuleFile = true;
5079       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5080       CurrentModule->IsExternC = IsExternC;
5081       CurrentModule->InferSubmodules = InferSubmodules;
5082       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5083       CurrentModule->InferExportWildcard = InferExportWildcard;
5084       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5085       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5086       if (DeserializationListener)
5087         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5088 
5089       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5090 
5091       // Clear out data that will be replaced by what is in the module file.
5092       CurrentModule->LinkLibraries.clear();
5093       CurrentModule->ConfigMacros.clear();
5094       CurrentModule->UnresolvedConflicts.clear();
5095       CurrentModule->Conflicts.clear();
5096 
5097       // The module is available unless it's missing a requirement; relevant
5098       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5099       // Missing headers that were present when the module was built do not
5100       // make it unavailable -- if we got this far, this must be an explicitly
5101       // imported module file.
5102       CurrentModule->Requirements.clear();
5103       CurrentModule->MissingHeaders.clear();
5104       CurrentModule->IsMissingRequirement =
5105           ParentModule && ParentModule->IsMissingRequirement;
5106       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5107       break;
5108     }
5109 
5110     case SUBMODULE_UMBRELLA_HEADER: {
5111       std::string Filename = Blob;
5112       ResolveImportedPath(F, Filename);
5113       if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
5114         if (!CurrentModule->getUmbrellaHeader())
5115           ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5116         else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
5117           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5118             Error("mismatched umbrella headers in submodule");
5119           return OutOfDate;
5120         }
5121       }
5122       break;
5123     }
5124 
5125     case SUBMODULE_HEADER:
5126     case SUBMODULE_EXCLUDED_HEADER:
5127     case SUBMODULE_PRIVATE_HEADER:
5128       // We lazily associate headers with their modules via the HeaderInfo table.
5129       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5130       // of complete filenames or remove it entirely.
5131       break;
5132 
5133     case SUBMODULE_TEXTUAL_HEADER:
5134     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5135       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5136       // them here.
5137       break;
5138 
5139     case SUBMODULE_TOPHEADER:
5140       CurrentModule->addTopHeaderFilename(Blob);
5141       break;
5142 
5143     case SUBMODULE_UMBRELLA_DIR: {
5144       std::string Dirname = Blob;
5145       ResolveImportedPath(F, Dirname);
5146       if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5147         if (!CurrentModule->getUmbrellaDir())
5148           ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5149         else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
5150           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5151             Error("mismatched umbrella directories in submodule");
5152           return OutOfDate;
5153         }
5154       }
5155       break;
5156     }
5157 
5158     case SUBMODULE_METADATA: {
5159       F.BaseSubmoduleID = getTotalNumSubmodules();
5160       F.LocalNumSubmodules = Record[0];
5161       unsigned LocalBaseSubmoduleID = Record[1];
5162       if (F.LocalNumSubmodules > 0) {
5163         // Introduce the global -> local mapping for submodules within this
5164         // module.
5165         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5166 
5167         // Introduce the local -> global mapping for submodules within this
5168         // module.
5169         F.SubmoduleRemap.insertOrReplace(
5170           std::make_pair(LocalBaseSubmoduleID,
5171                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5172 
5173         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5174       }
5175       break;
5176     }
5177 
5178     case SUBMODULE_IMPORTS:
5179       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5180         UnresolvedModuleRef Unresolved;
5181         Unresolved.File = &F;
5182         Unresolved.Mod = CurrentModule;
5183         Unresolved.ID = Record[Idx];
5184         Unresolved.Kind = UnresolvedModuleRef::Import;
5185         Unresolved.IsWildcard = false;
5186         UnresolvedModuleRefs.push_back(Unresolved);
5187       }
5188       break;
5189 
5190     case SUBMODULE_EXPORTS:
5191       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5192         UnresolvedModuleRef Unresolved;
5193         Unresolved.File = &F;
5194         Unresolved.Mod = CurrentModule;
5195         Unresolved.ID = Record[Idx];
5196         Unresolved.Kind = UnresolvedModuleRef::Export;
5197         Unresolved.IsWildcard = Record[Idx + 1];
5198         UnresolvedModuleRefs.push_back(Unresolved);
5199       }
5200 
5201       // Once we've loaded the set of exports, there's no reason to keep
5202       // the parsed, unresolved exports around.
5203       CurrentModule->UnresolvedExports.clear();
5204       break;
5205 
5206     case SUBMODULE_REQUIRES:
5207       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5208                                     PP.getTargetInfo());
5209       break;
5210 
5211     case SUBMODULE_LINK_LIBRARY:
5212       ModMap.resolveLinkAsDependencies(CurrentModule);
5213       CurrentModule->LinkLibraries.push_back(
5214                                          Module::LinkLibrary(Blob, Record[0]));
5215       break;
5216 
5217     case SUBMODULE_CONFIG_MACRO:
5218       CurrentModule->ConfigMacros.push_back(Blob.str());
5219       break;
5220 
5221     case SUBMODULE_CONFLICT: {
5222       UnresolvedModuleRef Unresolved;
5223       Unresolved.File = &F;
5224       Unresolved.Mod = CurrentModule;
5225       Unresolved.ID = Record[0];
5226       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5227       Unresolved.IsWildcard = false;
5228       Unresolved.String = Blob;
5229       UnresolvedModuleRefs.push_back(Unresolved);
5230       break;
5231     }
5232 
5233     case SUBMODULE_INITIALIZERS: {
5234       if (!ContextObj)
5235         break;
5236       SmallVector<uint32_t, 16> Inits;
5237       for (auto &ID : Record)
5238         Inits.push_back(getGlobalDeclID(F, ID));
5239       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5240       break;
5241     }
5242 
5243     case SUBMODULE_EXPORT_AS:
5244       CurrentModule->ExportAsModule = Blob.str();
5245       ModMap.addLinkAsDependency(CurrentModule);
5246       break;
5247     }
5248   }
5249 }
5250 
5251 /// Parse the record that corresponds to a LangOptions data
5252 /// structure.
5253 ///
5254 /// This routine parses the language options from the AST file and then gives
5255 /// them to the AST listener if one is set.
5256 ///
5257 /// \returns true if the listener deems the file unacceptable, false otherwise.
5258 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5259                                      bool Complain,
5260                                      ASTReaderListener &Listener,
5261                                      bool AllowCompatibleDifferences) {
5262   LangOptions LangOpts;
5263   unsigned Idx = 0;
5264 #define LANGOPT(Name, Bits, Default, Description) \
5265   LangOpts.Name = Record[Idx++];
5266 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5267   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5268 #include "clang/Basic/LangOptions.def"
5269 #define SANITIZER(NAME, ID)                                                    \
5270   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5271 #include "clang/Basic/Sanitizers.def"
5272 
5273   for (unsigned N = Record[Idx++]; N; --N)
5274     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5275 
5276   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5277   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5278   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5279 
5280   LangOpts.CurrentModule = ReadString(Record, Idx);
5281 
5282   // Comment options.
5283   for (unsigned N = Record[Idx++]; N; --N) {
5284     LangOpts.CommentOpts.BlockCommandNames.push_back(
5285       ReadString(Record, Idx));
5286   }
5287   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5288 
5289   // OpenMP offloading options.
5290   for (unsigned N = Record[Idx++]; N; --N) {
5291     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5292   }
5293 
5294   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5295 
5296   return Listener.ReadLanguageOptions(LangOpts, Complain,
5297                                       AllowCompatibleDifferences);
5298 }
5299 
5300 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5301                                    ASTReaderListener &Listener,
5302                                    bool AllowCompatibleDifferences) {
5303   unsigned Idx = 0;
5304   TargetOptions TargetOpts;
5305   TargetOpts.Triple = ReadString(Record, Idx);
5306   TargetOpts.CPU = ReadString(Record, Idx);
5307   TargetOpts.ABI = ReadString(Record, Idx);
5308   for (unsigned N = Record[Idx++]; N; --N) {
5309     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5310   }
5311   for (unsigned N = Record[Idx++]; N; --N) {
5312     TargetOpts.Features.push_back(ReadString(Record, Idx));
5313   }
5314 
5315   return Listener.ReadTargetOptions(TargetOpts, Complain,
5316                                     AllowCompatibleDifferences);
5317 }
5318 
5319 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5320                                        ASTReaderListener &Listener) {
5321   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5322   unsigned Idx = 0;
5323 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5324 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5325   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5326 #include "clang/Basic/DiagnosticOptions.def"
5327 
5328   for (unsigned N = Record[Idx++]; N; --N)
5329     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5330   for (unsigned N = Record[Idx++]; N; --N)
5331     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5332 
5333   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5334 }
5335 
5336 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5337                                        ASTReaderListener &Listener) {
5338   FileSystemOptions FSOpts;
5339   unsigned Idx = 0;
5340   FSOpts.WorkingDir = ReadString(Record, Idx);
5341   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5342 }
5343 
5344 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5345                                          bool Complain,
5346                                          ASTReaderListener &Listener) {
5347   HeaderSearchOptions HSOpts;
5348   unsigned Idx = 0;
5349   HSOpts.Sysroot = ReadString(Record, Idx);
5350 
5351   // Include entries.
5352   for (unsigned N = Record[Idx++]; N; --N) {
5353     std::string Path = ReadString(Record, Idx);
5354     frontend::IncludeDirGroup Group
5355       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5356     bool IsFramework = Record[Idx++];
5357     bool IgnoreSysRoot = Record[Idx++];
5358     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5359                                     IgnoreSysRoot);
5360   }
5361 
5362   // System header prefixes.
5363   for (unsigned N = Record[Idx++]; N; --N) {
5364     std::string Prefix = ReadString(Record, Idx);
5365     bool IsSystemHeader = Record[Idx++];
5366     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5367   }
5368 
5369   HSOpts.ResourceDir = ReadString(Record, Idx);
5370   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5371   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5372   HSOpts.DisableModuleHash = Record[Idx++];
5373   HSOpts.ImplicitModuleMaps = Record[Idx++];
5374   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5375   HSOpts.UseBuiltinIncludes = Record[Idx++];
5376   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5377   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5378   HSOpts.UseLibcxx = Record[Idx++];
5379   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5380 
5381   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5382                                           Complain);
5383 }
5384 
5385 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5386                                          bool Complain,
5387                                          ASTReaderListener &Listener,
5388                                          std::string &SuggestedPredefines) {
5389   PreprocessorOptions PPOpts;
5390   unsigned Idx = 0;
5391 
5392   // Macro definitions/undefs
5393   for (unsigned N = Record[Idx++]; N; --N) {
5394     std::string Macro = ReadString(Record, Idx);
5395     bool IsUndef = Record[Idx++];
5396     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5397   }
5398 
5399   // Includes
5400   for (unsigned N = Record[Idx++]; N; --N) {
5401     PPOpts.Includes.push_back(ReadString(Record, Idx));
5402   }
5403 
5404   // Macro Includes
5405   for (unsigned N = Record[Idx++]; N; --N) {
5406     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5407   }
5408 
5409   PPOpts.UsePredefines = Record[Idx++];
5410   PPOpts.DetailedRecord = Record[Idx++];
5411   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5412   PPOpts.ObjCXXARCStandardLibrary =
5413     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5414   SuggestedPredefines.clear();
5415   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5416                                           SuggestedPredefines);
5417 }
5418 
5419 std::pair<ModuleFile *, unsigned>
5420 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5421   GlobalPreprocessedEntityMapType::iterator
5422   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5423   assert(I != GlobalPreprocessedEntityMap.end() &&
5424          "Corrupted global preprocessed entity map");
5425   ModuleFile *M = I->second;
5426   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5427   return std::make_pair(M, LocalIndex);
5428 }
5429 
5430 llvm::iterator_range<PreprocessingRecord::iterator>
5431 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5432   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5433     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5434                                              Mod.NumPreprocessedEntities);
5435 
5436   return llvm::make_range(PreprocessingRecord::iterator(),
5437                           PreprocessingRecord::iterator());
5438 }
5439 
5440 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5441 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5442   return llvm::make_range(
5443       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5444       ModuleDeclIterator(this, &Mod,
5445                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5446 }
5447 
5448 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5449   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5450   assert(I != GlobalSkippedRangeMap.end() &&
5451     "Corrupted global skipped range map");
5452   ModuleFile *M = I->second;
5453   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5454   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5455   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5456   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5457                     TranslateSourceLocation(*M, RawRange.getEnd()));
5458   assert(Range.isValid());
5459   return Range;
5460 }
5461 
5462 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5463   PreprocessedEntityID PPID = Index+1;
5464   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5465   ModuleFile &M = *PPInfo.first;
5466   unsigned LocalIndex = PPInfo.second;
5467   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5468 
5469   if (!PP.getPreprocessingRecord()) {
5470     Error("no preprocessing record");
5471     return nullptr;
5472   }
5473 
5474   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5475   M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
5476 
5477   llvm::BitstreamEntry Entry =
5478     M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5479   if (Entry.Kind != llvm::BitstreamEntry::Record)
5480     return nullptr;
5481 
5482   // Read the record.
5483   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5484                     TranslateSourceLocation(M, PPOffs.getEnd()));
5485   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5486   StringRef Blob;
5487   RecordData Record;
5488   PreprocessorDetailRecordTypes RecType =
5489     (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
5490                                           Entry.ID, Record, &Blob);
5491   switch (RecType) {
5492   case PPD_MACRO_EXPANSION: {
5493     bool isBuiltin = Record[0];
5494     IdentifierInfo *Name = nullptr;
5495     MacroDefinitionRecord *Def = nullptr;
5496     if (isBuiltin)
5497       Name = getLocalIdentifier(M, Record[1]);
5498     else {
5499       PreprocessedEntityID GlobalID =
5500           getGlobalPreprocessedEntityID(M, Record[1]);
5501       Def = cast<MacroDefinitionRecord>(
5502           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5503     }
5504 
5505     MacroExpansion *ME;
5506     if (isBuiltin)
5507       ME = new (PPRec) MacroExpansion(Name, Range);
5508     else
5509       ME = new (PPRec) MacroExpansion(Def, Range);
5510 
5511     return ME;
5512   }
5513 
5514   case PPD_MACRO_DEFINITION: {
5515     // Decode the identifier info and then check again; if the macro is
5516     // still defined and associated with the identifier,
5517     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5518     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5519 
5520     if (DeserializationListener)
5521       DeserializationListener->MacroDefinitionRead(PPID, MD);
5522 
5523     return MD;
5524   }
5525 
5526   case PPD_INCLUSION_DIRECTIVE: {
5527     const char *FullFileNameStart = Blob.data() + Record[0];
5528     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5529     const FileEntry *File = nullptr;
5530     if (!FullFileName.empty())
5531       File = PP.getFileManager().getFile(FullFileName);
5532 
5533     // FIXME: Stable encoding
5534     InclusionDirective::InclusionKind Kind
5535       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5536     InclusionDirective *ID
5537       = new (PPRec) InclusionDirective(PPRec, Kind,
5538                                        StringRef(Blob.data(), Record[0]),
5539                                        Record[1], Record[3],
5540                                        File,
5541                                        Range);
5542     return ID;
5543   }
5544   }
5545 
5546   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5547 }
5548 
5549 /// Find the next module that contains entities and return the ID
5550 /// of the first entry.
5551 ///
5552 /// \param SLocMapI points at a chunk of a module that contains no
5553 /// preprocessed entities or the entities it contains are not the ones we are
5554 /// looking for.
5555 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5556                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5557   ++SLocMapI;
5558   for (GlobalSLocOffsetMapType::const_iterator
5559          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5560     ModuleFile &M = *SLocMapI->second;
5561     if (M.NumPreprocessedEntities)
5562       return M.BasePreprocessedEntityID;
5563   }
5564 
5565   return getTotalNumPreprocessedEntities();
5566 }
5567 
5568 namespace {
5569 
5570 struct PPEntityComp {
5571   const ASTReader &Reader;
5572   ModuleFile &M;
5573 
5574   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5575 
5576   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5577     SourceLocation LHS = getLoc(L);
5578     SourceLocation RHS = getLoc(R);
5579     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5580   }
5581 
5582   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5583     SourceLocation LHS = getLoc(L);
5584     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5585   }
5586 
5587   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5588     SourceLocation RHS = getLoc(R);
5589     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5590   }
5591 
5592   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5593     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5594   }
5595 };
5596 
5597 } // namespace
5598 
5599 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5600                                                        bool EndsAfter) const {
5601   if (SourceMgr.isLocalSourceLocation(Loc))
5602     return getTotalNumPreprocessedEntities();
5603 
5604   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5605       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5606   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5607          "Corrupted global sloc offset map");
5608 
5609   if (SLocMapI->second->NumPreprocessedEntities == 0)
5610     return findNextPreprocessedEntity(SLocMapI);
5611 
5612   ModuleFile &M = *SLocMapI->second;
5613 
5614   using pp_iterator = const PPEntityOffset *;
5615 
5616   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5617   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5618 
5619   size_t Count = M.NumPreprocessedEntities;
5620   size_t Half;
5621   pp_iterator First = pp_begin;
5622   pp_iterator PPI;
5623 
5624   if (EndsAfter) {
5625     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5626                            PPEntityComp(*this, M));
5627   } else {
5628     // Do a binary search manually instead of using std::lower_bound because
5629     // The end locations of entities may be unordered (when a macro expansion
5630     // is inside another macro argument), but for this case it is not important
5631     // whether we get the first macro expansion or its containing macro.
5632     while (Count > 0) {
5633       Half = Count / 2;
5634       PPI = First;
5635       std::advance(PPI, Half);
5636       if (SourceMgr.isBeforeInTranslationUnit(
5637               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
5638         First = PPI;
5639         ++First;
5640         Count = Count - Half - 1;
5641       } else
5642         Count = Half;
5643     }
5644   }
5645 
5646   if (PPI == pp_end)
5647     return findNextPreprocessedEntity(SLocMapI);
5648 
5649   return M.BasePreprocessedEntityID + (PPI - pp_begin);
5650 }
5651 
5652 /// Returns a pair of [Begin, End) indices of preallocated
5653 /// preprocessed entities that \arg Range encompasses.
5654 std::pair<unsigned, unsigned>
5655     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5656   if (Range.isInvalid())
5657     return std::make_pair(0,0);
5658   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5659 
5660   PreprocessedEntityID BeginID =
5661       findPreprocessedEntity(Range.getBegin(), false);
5662   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5663   return std::make_pair(BeginID, EndID);
5664 }
5665 
5666 /// Optionally returns true or false if the preallocated preprocessed
5667 /// entity with index \arg Index came from file \arg FID.
5668 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5669                                                              FileID FID) {
5670   if (FID.isInvalid())
5671     return false;
5672 
5673   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5674   ModuleFile &M = *PPInfo.first;
5675   unsigned LocalIndex = PPInfo.second;
5676   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5677 
5678   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
5679   if (Loc.isInvalid())
5680     return false;
5681 
5682   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5683     return true;
5684   else
5685     return false;
5686 }
5687 
5688 namespace {
5689 
5690   /// Visitor used to search for information about a header file.
5691   class HeaderFileInfoVisitor {
5692     const FileEntry *FE;
5693     Optional<HeaderFileInfo> HFI;
5694 
5695   public:
5696     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
5697 
5698     bool operator()(ModuleFile &M) {
5699       HeaderFileInfoLookupTable *Table
5700         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5701       if (!Table)
5702         return false;
5703 
5704       // Look in the on-disk hash table for an entry for this file name.
5705       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
5706       if (Pos == Table->end())
5707         return false;
5708 
5709       HFI = *Pos;
5710       return true;
5711     }
5712 
5713     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5714   };
5715 
5716 } // namespace
5717 
5718 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5719   HeaderFileInfoVisitor Visitor(FE);
5720   ModuleMgr.visit(Visitor);
5721   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5722     return *HFI;
5723 
5724   return HeaderFileInfo();
5725 }
5726 
5727 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5728   using DiagState = DiagnosticsEngine::DiagState;
5729   SmallVector<DiagState *, 32> DiagStates;
5730 
5731   for (ModuleFile &F : ModuleMgr) {
5732     unsigned Idx = 0;
5733     auto &Record = F.PragmaDiagMappings;
5734     if (Record.empty())
5735       continue;
5736 
5737     DiagStates.clear();
5738 
5739     auto ReadDiagState =
5740         [&](const DiagState &BasedOn, SourceLocation Loc,
5741             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
5742       unsigned BackrefID = Record[Idx++];
5743       if (BackrefID != 0)
5744         return DiagStates[BackrefID - 1];
5745 
5746       // A new DiagState was created here.
5747       Diag.DiagStates.push_back(BasedOn);
5748       DiagState *NewState = &Diag.DiagStates.back();
5749       DiagStates.push_back(NewState);
5750       unsigned Size = Record[Idx++];
5751       assert(Idx + Size * 2 <= Record.size() &&
5752              "Invalid data, not enough diag/map pairs");
5753       while (Size--) {
5754         unsigned DiagID = Record[Idx++];
5755         DiagnosticMapping NewMapping =
5756             DiagnosticMapping::deserialize(Record[Idx++]);
5757         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
5758           continue;
5759 
5760         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
5761 
5762         // If this mapping was specified as a warning but the severity was
5763         // upgraded due to diagnostic settings, simulate the current diagnostic
5764         // settings (and use a warning).
5765         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
5766           NewMapping.setSeverity(diag::Severity::Warning);
5767           NewMapping.setUpgradedFromWarning(false);
5768         }
5769 
5770         Mapping = NewMapping;
5771       }
5772       return NewState;
5773     };
5774 
5775     // Read the first state.
5776     DiagState *FirstState;
5777     if (F.Kind == MK_ImplicitModule) {
5778       // Implicitly-built modules are reused with different diagnostic
5779       // settings.  Use the initial diagnostic state from Diag to simulate this
5780       // compilation's diagnostic settings.
5781       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
5782       DiagStates.push_back(FirstState);
5783 
5784       // Skip the initial diagnostic state from the serialized module.
5785       assert(Record[1] == 0 &&
5786              "Invalid data, unexpected backref in initial state");
5787       Idx = 3 + Record[2] * 2;
5788       assert(Idx < Record.size() &&
5789              "Invalid data, not enough state change pairs in initial state");
5790     } else if (F.isModule()) {
5791       // For an explicit module, preserve the flags from the module build
5792       // command line (-w, -Weverything, -Werror, ...) along with any explicit
5793       // -Wblah flags.
5794       unsigned Flags = Record[Idx++];
5795       DiagState Initial;
5796       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
5797       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
5798       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
5799       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
5800       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
5801       Initial.ExtBehavior = (diag::Severity)Flags;
5802       FirstState = ReadDiagState(Initial, SourceLocation(), true);
5803 
5804       assert(F.OriginalSourceFileID.isValid());
5805 
5806       // Set up the root buffer of the module to start with the initial
5807       // diagnostic state of the module itself, to cover files that contain no
5808       // explicit transitions (for which we did not serialize anything).
5809       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
5810           .StateTransitions.push_back({FirstState, 0});
5811     } else {
5812       // For prefix ASTs, start with whatever the user configured on the
5813       // command line.
5814       Idx++; // Skip flags.
5815       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
5816                                  SourceLocation(), false);
5817     }
5818 
5819     // Read the state transitions.
5820     unsigned NumLocations = Record[Idx++];
5821     while (NumLocations--) {
5822       assert(Idx < Record.size() &&
5823              "Invalid data, missing pragma diagnostic states");
5824       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
5825       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
5826       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
5827       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
5828       unsigned Transitions = Record[Idx++];
5829 
5830       // Note that we don't need to set up Parent/ParentOffset here, because
5831       // we won't be changing the diagnostic state within imported FileIDs
5832       // (other than perhaps appending to the main source file, which has no
5833       // parent).
5834       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
5835       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
5836       for (unsigned I = 0; I != Transitions; ++I) {
5837         unsigned Offset = Record[Idx++];
5838         auto *State =
5839             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
5840         F.StateTransitions.push_back({State, Offset});
5841       }
5842     }
5843 
5844     // Read the final state.
5845     assert(Idx < Record.size() &&
5846            "Invalid data, missing final pragma diagnostic state");
5847     SourceLocation CurStateLoc =
5848         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5849     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
5850 
5851     if (!F.isModule()) {
5852       Diag.DiagStatesByLoc.CurDiagState = CurState;
5853       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
5854 
5855       // Preserve the property that the imaginary root file describes the
5856       // current state.
5857       FileID NullFile;
5858       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
5859       if (T.empty())
5860         T.push_back({CurState, 0});
5861       else
5862         T[0].State = CurState;
5863     }
5864 
5865     // Don't try to read these mappings again.
5866     Record.clear();
5867   }
5868 }
5869 
5870 /// Get the correct cursor and offset for loading a type.
5871 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5872   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5873   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5874   ModuleFile *M = I->second;
5875   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5876 }
5877 
5878 /// Read and return the type with the given index..
5879 ///
5880 /// The index is the type ID, shifted and minus the number of predefs. This
5881 /// routine actually reads the record corresponding to the type at the given
5882 /// location. It is a helper routine for GetType, which deals with reading type
5883 /// IDs.
5884 QualType ASTReader::readTypeRecord(unsigned Index) {
5885   assert(ContextObj && "reading type with no AST context");
5886   ASTContext &Context = *ContextObj;
5887   RecordLocation Loc = TypeCursorForIndex(Index);
5888   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5889 
5890   // Keep track of where we are in the stream, then jump back there
5891   // after reading this type.
5892   SavedStreamPosition SavedPosition(DeclsCursor);
5893 
5894   ReadingKindTracker ReadingKind(Read_Type, *this);
5895 
5896   // Note that we are loading a type record.
5897   Deserializing AType(this);
5898 
5899   unsigned Idx = 0;
5900   DeclsCursor.JumpToBit(Loc.Offset);
5901   RecordData Record;
5902   unsigned Code = DeclsCursor.ReadCode();
5903   switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5904   case TYPE_EXT_QUAL: {
5905     if (Record.size() != 2) {
5906       Error("Incorrect encoding of extended qualifier type");
5907       return QualType();
5908     }
5909     QualType Base = readType(*Loc.F, Record, Idx);
5910     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5911     return Context.getQualifiedType(Base, Quals);
5912   }
5913 
5914   case TYPE_COMPLEX: {
5915     if (Record.size() != 1) {
5916       Error("Incorrect encoding of complex type");
5917       return QualType();
5918     }
5919     QualType ElemType = readType(*Loc.F, Record, Idx);
5920     return Context.getComplexType(ElemType);
5921   }
5922 
5923   case TYPE_POINTER: {
5924     if (Record.size() != 1) {
5925       Error("Incorrect encoding of pointer type");
5926       return QualType();
5927     }
5928     QualType PointeeType = readType(*Loc.F, Record, Idx);
5929     return Context.getPointerType(PointeeType);
5930   }
5931 
5932   case TYPE_DECAYED: {
5933     if (Record.size() != 1) {
5934       Error("Incorrect encoding of decayed type");
5935       return QualType();
5936     }
5937     QualType OriginalType = readType(*Loc.F, Record, Idx);
5938     QualType DT = Context.getAdjustedParameterType(OriginalType);
5939     if (!isa<DecayedType>(DT))
5940       Error("Decayed type does not decay");
5941     return DT;
5942   }
5943 
5944   case TYPE_ADJUSTED: {
5945     if (Record.size() != 2) {
5946       Error("Incorrect encoding of adjusted type");
5947       return QualType();
5948     }
5949     QualType OriginalTy = readType(*Loc.F, Record, Idx);
5950     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5951     return Context.getAdjustedType(OriginalTy, AdjustedTy);
5952   }
5953 
5954   case TYPE_BLOCK_POINTER: {
5955     if (Record.size() != 1) {
5956       Error("Incorrect encoding of block pointer type");
5957       return QualType();
5958     }
5959     QualType PointeeType = readType(*Loc.F, Record, Idx);
5960     return Context.getBlockPointerType(PointeeType);
5961   }
5962 
5963   case TYPE_LVALUE_REFERENCE: {
5964     if (Record.size() != 2) {
5965       Error("Incorrect encoding of lvalue reference type");
5966       return QualType();
5967     }
5968     QualType PointeeType = readType(*Loc.F, Record, Idx);
5969     return Context.getLValueReferenceType(PointeeType, Record[1]);
5970   }
5971 
5972   case TYPE_RVALUE_REFERENCE: {
5973     if (Record.size() != 1) {
5974       Error("Incorrect encoding of rvalue reference type");
5975       return QualType();
5976     }
5977     QualType PointeeType = readType(*Loc.F, Record, Idx);
5978     return Context.getRValueReferenceType(PointeeType);
5979   }
5980 
5981   case TYPE_MEMBER_POINTER: {
5982     if (Record.size() != 2) {
5983       Error("Incorrect encoding of member pointer type");
5984       return QualType();
5985     }
5986     QualType PointeeType = readType(*Loc.F, Record, Idx);
5987     QualType ClassType = readType(*Loc.F, Record, Idx);
5988     if (PointeeType.isNull() || ClassType.isNull())
5989       return QualType();
5990 
5991     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5992   }
5993 
5994   case TYPE_CONSTANT_ARRAY: {
5995     QualType ElementType = readType(*Loc.F, Record, Idx);
5996     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5997     unsigned IndexTypeQuals = Record[2];
5998     unsigned Idx = 3;
5999     llvm::APInt Size = ReadAPInt(Record, Idx);
6000     return Context.getConstantArrayType(ElementType, Size,
6001                                          ASM, IndexTypeQuals);
6002   }
6003 
6004   case TYPE_INCOMPLETE_ARRAY: {
6005     QualType ElementType = readType(*Loc.F, Record, Idx);
6006     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6007     unsigned IndexTypeQuals = Record[2];
6008     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6009   }
6010 
6011   case TYPE_VARIABLE_ARRAY: {
6012     QualType ElementType = readType(*Loc.F, Record, Idx);
6013     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6014     unsigned IndexTypeQuals = Record[2];
6015     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6016     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6017     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6018                                          ASM, IndexTypeQuals,
6019                                          SourceRange(LBLoc, RBLoc));
6020   }
6021 
6022   case TYPE_VECTOR: {
6023     if (Record.size() != 3) {
6024       Error("incorrect encoding of vector type in AST file");
6025       return QualType();
6026     }
6027 
6028     QualType ElementType = readType(*Loc.F, Record, Idx);
6029     unsigned NumElements = Record[1];
6030     unsigned VecKind = Record[2];
6031     return Context.getVectorType(ElementType, NumElements,
6032                                   (VectorType::VectorKind)VecKind);
6033   }
6034 
6035   case TYPE_EXT_VECTOR: {
6036     if (Record.size() != 3) {
6037       Error("incorrect encoding of extended vector type in AST file");
6038       return QualType();
6039     }
6040 
6041     QualType ElementType = readType(*Loc.F, Record, Idx);
6042     unsigned NumElements = Record[1];
6043     return Context.getExtVectorType(ElementType, NumElements);
6044   }
6045 
6046   case TYPE_FUNCTION_NO_PROTO: {
6047     if (Record.size() != 8) {
6048       Error("incorrect encoding of no-proto function type");
6049       return QualType();
6050     }
6051     QualType ResultType = readType(*Loc.F, Record, Idx);
6052     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6053                                (CallingConv)Record[4], Record[5], Record[6],
6054                                Record[7]);
6055     return Context.getFunctionNoProtoType(ResultType, Info);
6056   }
6057 
6058   case TYPE_FUNCTION_PROTO: {
6059     QualType ResultType = readType(*Loc.F, Record, Idx);
6060 
6061     FunctionProtoType::ExtProtoInfo EPI;
6062     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6063                                         /*hasregparm*/ Record[2],
6064                                         /*regparm*/ Record[3],
6065                                         static_cast<CallingConv>(Record[4]),
6066                                         /*produces*/ Record[5],
6067                                         /*nocallersavedregs*/ Record[6],
6068                                         /*nocfcheck*/ Record[7]);
6069 
6070     unsigned Idx = 8;
6071 
6072     EPI.Variadic = Record[Idx++];
6073     EPI.HasTrailingReturn = Record[Idx++];
6074     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6075     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6076     SmallVector<QualType, 8> ExceptionStorage;
6077     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6078 
6079     unsigned NumParams = Record[Idx++];
6080     SmallVector<QualType, 16> ParamTypes;
6081     for (unsigned I = 0; I != NumParams; ++I)
6082       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6083 
6084     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6085     if (Idx != Record.size()) {
6086       for (unsigned I = 0; I != NumParams; ++I)
6087         ExtParameterInfos.push_back(
6088           FunctionProtoType::ExtParameterInfo
6089                            ::getFromOpaqueValue(Record[Idx++]));
6090       EPI.ExtParameterInfos = ExtParameterInfos.data();
6091     }
6092 
6093     assert(Idx == Record.size());
6094 
6095     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6096   }
6097 
6098   case TYPE_UNRESOLVED_USING: {
6099     unsigned Idx = 0;
6100     return Context.getTypeDeclType(
6101                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6102   }
6103 
6104   case TYPE_TYPEDEF: {
6105     if (Record.size() != 2) {
6106       Error("incorrect encoding of typedef type");
6107       return QualType();
6108     }
6109     unsigned Idx = 0;
6110     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6111     QualType Canonical = readType(*Loc.F, Record, Idx);
6112     if (!Canonical.isNull())
6113       Canonical = Context.getCanonicalType(Canonical);
6114     return Context.getTypedefType(Decl, Canonical);
6115   }
6116 
6117   case TYPE_TYPEOF_EXPR:
6118     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6119 
6120   case TYPE_TYPEOF: {
6121     if (Record.size() != 1) {
6122       Error("incorrect encoding of typeof(type) in AST file");
6123       return QualType();
6124     }
6125     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6126     return Context.getTypeOfType(UnderlyingType);
6127   }
6128 
6129   case TYPE_DECLTYPE: {
6130     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6131     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6132   }
6133 
6134   case TYPE_UNARY_TRANSFORM: {
6135     QualType BaseType = readType(*Loc.F, Record, Idx);
6136     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6137     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6138     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6139   }
6140 
6141   case TYPE_AUTO: {
6142     QualType Deduced = readType(*Loc.F, Record, Idx);
6143     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6144     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6145     return Context.getAutoType(Deduced, Keyword, IsDependent);
6146   }
6147 
6148   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6149     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6150     QualType Deduced = readType(*Loc.F, Record, Idx);
6151     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6152     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6153                                                         IsDependent);
6154   }
6155 
6156   case TYPE_RECORD: {
6157     if (Record.size() != 2) {
6158       Error("incorrect encoding of record type");
6159       return QualType();
6160     }
6161     unsigned Idx = 0;
6162     bool IsDependent = Record[Idx++];
6163     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6164     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6165     QualType T = Context.getRecordType(RD);
6166     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6167     return T;
6168   }
6169 
6170   case TYPE_ENUM: {
6171     if (Record.size() != 2) {
6172       Error("incorrect encoding of enum type");
6173       return QualType();
6174     }
6175     unsigned Idx = 0;
6176     bool IsDependent = Record[Idx++];
6177     QualType T
6178       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6179     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6180     return T;
6181   }
6182 
6183   case TYPE_ATTRIBUTED: {
6184     if (Record.size() != 3) {
6185       Error("incorrect encoding of attributed type");
6186       return QualType();
6187     }
6188     QualType modifiedType = readType(*Loc.F, Record, Idx);
6189     QualType equivalentType = readType(*Loc.F, Record, Idx);
6190     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6191     return Context.getAttributedType(kind, modifiedType, equivalentType);
6192   }
6193 
6194   case TYPE_PAREN: {
6195     if (Record.size() != 1) {
6196       Error("incorrect encoding of paren type");
6197       return QualType();
6198     }
6199     QualType InnerType = readType(*Loc.F, Record, Idx);
6200     return Context.getParenType(InnerType);
6201   }
6202 
6203   case TYPE_MACRO_QUALIFIED: {
6204     if (Record.size() != 2) {
6205       Error("incorrect encoding of macro defined type");
6206       return QualType();
6207     }
6208     QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6209     IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6210     return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6211   }
6212 
6213   case TYPE_PACK_EXPANSION: {
6214     if (Record.size() != 2) {
6215       Error("incorrect encoding of pack expansion type");
6216       return QualType();
6217     }
6218     QualType Pattern = readType(*Loc.F, Record, Idx);
6219     if (Pattern.isNull())
6220       return QualType();
6221     Optional<unsigned> NumExpansions;
6222     if (Record[1])
6223       NumExpansions = Record[1] - 1;
6224     return Context.getPackExpansionType(Pattern, NumExpansions);
6225   }
6226 
6227   case TYPE_ELABORATED: {
6228     unsigned Idx = 0;
6229     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6230     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6231     QualType NamedType = readType(*Loc.F, Record, Idx);
6232     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6233     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6234   }
6235 
6236   case TYPE_OBJC_INTERFACE: {
6237     unsigned Idx = 0;
6238     ObjCInterfaceDecl *ItfD
6239       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6240     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6241   }
6242 
6243   case TYPE_OBJC_TYPE_PARAM: {
6244     unsigned Idx = 0;
6245     ObjCTypeParamDecl *Decl
6246       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6247     unsigned NumProtos = Record[Idx++];
6248     SmallVector<ObjCProtocolDecl*, 4> Protos;
6249     for (unsigned I = 0; I != NumProtos; ++I)
6250       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6251     return Context.getObjCTypeParamType(Decl, Protos);
6252   }
6253 
6254   case TYPE_OBJC_OBJECT: {
6255     unsigned Idx = 0;
6256     QualType Base = readType(*Loc.F, Record, Idx);
6257     unsigned NumTypeArgs = Record[Idx++];
6258     SmallVector<QualType, 4> TypeArgs;
6259     for (unsigned I = 0; I != NumTypeArgs; ++I)
6260       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6261     unsigned NumProtos = Record[Idx++];
6262     SmallVector<ObjCProtocolDecl*, 4> Protos;
6263     for (unsigned I = 0; I != NumProtos; ++I)
6264       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6265     bool IsKindOf = Record[Idx++];
6266     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6267   }
6268 
6269   case TYPE_OBJC_OBJECT_POINTER: {
6270     unsigned Idx = 0;
6271     QualType Pointee = readType(*Loc.F, Record, Idx);
6272     return Context.getObjCObjectPointerType(Pointee);
6273   }
6274 
6275   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6276     unsigned Idx = 0;
6277     QualType Parm = readType(*Loc.F, Record, Idx);
6278     QualType Replacement = readType(*Loc.F, Record, Idx);
6279     return Context.getSubstTemplateTypeParmType(
6280         cast<TemplateTypeParmType>(Parm),
6281         Context.getCanonicalType(Replacement));
6282   }
6283 
6284   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6285     unsigned Idx = 0;
6286     QualType Parm = readType(*Loc.F, Record, Idx);
6287     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6288     return Context.getSubstTemplateTypeParmPackType(
6289                                                cast<TemplateTypeParmType>(Parm),
6290                                                      ArgPack);
6291   }
6292 
6293   case TYPE_INJECTED_CLASS_NAME: {
6294     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6295     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6296     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6297     // for AST reading, too much interdependencies.
6298     const Type *T = nullptr;
6299     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6300       if (const Type *Existing = DI->getTypeForDecl()) {
6301         T = Existing;
6302         break;
6303       }
6304     }
6305     if (!T) {
6306       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6307       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6308         DI->setTypeForDecl(T);
6309     }
6310     return QualType(T, 0);
6311   }
6312 
6313   case TYPE_TEMPLATE_TYPE_PARM: {
6314     unsigned Idx = 0;
6315     unsigned Depth = Record[Idx++];
6316     unsigned Index = Record[Idx++];
6317     bool Pack = Record[Idx++];
6318     TemplateTypeParmDecl *D
6319       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6320     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6321   }
6322 
6323   case TYPE_DEPENDENT_NAME: {
6324     unsigned Idx = 0;
6325     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6326     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6327     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6328     QualType Canon = readType(*Loc.F, Record, Idx);
6329     if (!Canon.isNull())
6330       Canon = Context.getCanonicalType(Canon);
6331     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6332   }
6333 
6334   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6335     unsigned Idx = 0;
6336     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6337     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6338     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6339     unsigned NumArgs = Record[Idx++];
6340     SmallVector<TemplateArgument, 8> Args;
6341     Args.reserve(NumArgs);
6342     while (NumArgs--)
6343       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6344     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6345                                                           Args);
6346   }
6347 
6348   case TYPE_DEPENDENT_SIZED_ARRAY: {
6349     unsigned Idx = 0;
6350 
6351     // ArrayType
6352     QualType ElementType = readType(*Loc.F, Record, Idx);
6353     ArrayType::ArraySizeModifier ASM
6354       = (ArrayType::ArraySizeModifier)Record[Idx++];
6355     unsigned IndexTypeQuals = Record[Idx++];
6356 
6357     // DependentSizedArrayType
6358     Expr *NumElts = ReadExpr(*Loc.F);
6359     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6360 
6361     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6362                                                IndexTypeQuals, Brackets);
6363   }
6364 
6365   case TYPE_TEMPLATE_SPECIALIZATION: {
6366     unsigned Idx = 0;
6367     bool IsDependent = Record[Idx++];
6368     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6369     SmallVector<TemplateArgument, 8> Args;
6370     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6371     QualType Underlying = readType(*Loc.F, Record, Idx);
6372     QualType T;
6373     if (Underlying.isNull())
6374       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6375     else
6376       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6377     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6378     return T;
6379   }
6380 
6381   case TYPE_ATOMIC: {
6382     if (Record.size() != 1) {
6383       Error("Incorrect encoding of atomic type");
6384       return QualType();
6385     }
6386     QualType ValueType = readType(*Loc.F, Record, Idx);
6387     return Context.getAtomicType(ValueType);
6388   }
6389 
6390   case TYPE_PIPE: {
6391     if (Record.size() != 2) {
6392       Error("Incorrect encoding of pipe type");
6393       return QualType();
6394     }
6395 
6396     // Reading the pipe element type.
6397     QualType ElementType = readType(*Loc.F, Record, Idx);
6398     unsigned ReadOnly = Record[1];
6399     return Context.getPipeType(ElementType, ReadOnly);
6400   }
6401 
6402   case TYPE_DEPENDENT_SIZED_VECTOR: {
6403     unsigned Idx = 0;
6404     QualType ElementType = readType(*Loc.F, Record, Idx);
6405     Expr *SizeExpr = ReadExpr(*Loc.F);
6406     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6407     unsigned VecKind = Record[Idx];
6408 
6409     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6410                                                (VectorType::VectorKind)VecKind);
6411   }
6412 
6413   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6414     unsigned Idx = 0;
6415 
6416     // DependentSizedExtVectorType
6417     QualType ElementType = readType(*Loc.F, Record, Idx);
6418     Expr *SizeExpr = ReadExpr(*Loc.F);
6419     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6420 
6421     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6422                                                   AttrLoc);
6423   }
6424 
6425   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6426     unsigned Idx = 0;
6427 
6428     // DependentAddressSpaceType
6429     QualType PointeeType = readType(*Loc.F, Record, Idx);
6430     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6431     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6432 
6433     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6434                                                    AttrLoc);
6435   }
6436   }
6437   llvm_unreachable("Invalid TypeCode!");
6438 }
6439 
6440 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6441                                   SmallVectorImpl<QualType> &Exceptions,
6442                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6443                                   const RecordData &Record, unsigned &Idx) {
6444   ExceptionSpecificationType EST =
6445       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6446   ESI.Type = EST;
6447   if (EST == EST_Dynamic) {
6448     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6449       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6450     ESI.Exceptions = Exceptions;
6451   } else if (isComputedNoexcept(EST)) {
6452     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6453   } else if (EST == EST_Uninstantiated) {
6454     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6455     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6456   } else if (EST == EST_Unevaluated) {
6457     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6458   }
6459 }
6460 
6461 namespace clang {
6462 
6463 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6464   ModuleFile *F;
6465   ASTReader *Reader;
6466   const ASTReader::RecordData &Record;
6467   unsigned &Idx;
6468 
6469   SourceLocation ReadSourceLocation() {
6470     return Reader->ReadSourceLocation(*F, Record, Idx);
6471   }
6472 
6473   TypeSourceInfo *GetTypeSourceInfo() {
6474     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6475   }
6476 
6477   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6478     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6479   }
6480 
6481   Attr *ReadAttr() {
6482     return Reader->ReadAttr(*F, Record, Idx);
6483   }
6484 
6485 public:
6486   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6487                 const ASTReader::RecordData &Record, unsigned &Idx)
6488       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6489 
6490   // We want compile-time assurance that we've enumerated all of
6491   // these, so unfortunately we have to declare them first, then
6492   // define them out-of-line.
6493 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6494 #define TYPELOC(CLASS, PARENT) \
6495   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6496 #include "clang/AST/TypeLocNodes.def"
6497 
6498   void VisitFunctionTypeLoc(FunctionTypeLoc);
6499   void VisitArrayTypeLoc(ArrayTypeLoc);
6500 };
6501 
6502 } // namespace clang
6503 
6504 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6505   // nothing to do
6506 }
6507 
6508 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6509   TL.setBuiltinLoc(ReadSourceLocation());
6510   if (TL.needsExtraLocalData()) {
6511     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6512     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6513     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6514     TL.setModeAttr(Record[Idx++]);
6515   }
6516 }
6517 
6518 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6519   TL.setNameLoc(ReadSourceLocation());
6520 }
6521 
6522 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6523   TL.setStarLoc(ReadSourceLocation());
6524 }
6525 
6526 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6527   // nothing to do
6528 }
6529 
6530 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6531   // nothing to do
6532 }
6533 
6534 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6535   TL.setExpansionLoc(ReadSourceLocation());
6536 }
6537 
6538 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6539   TL.setCaretLoc(ReadSourceLocation());
6540 }
6541 
6542 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6543   TL.setAmpLoc(ReadSourceLocation());
6544 }
6545 
6546 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6547   TL.setAmpAmpLoc(ReadSourceLocation());
6548 }
6549 
6550 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6551   TL.setStarLoc(ReadSourceLocation());
6552   TL.setClassTInfo(GetTypeSourceInfo());
6553 }
6554 
6555 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6556   TL.setLBracketLoc(ReadSourceLocation());
6557   TL.setRBracketLoc(ReadSourceLocation());
6558   if (Record[Idx++])
6559     TL.setSizeExpr(Reader->ReadExpr(*F));
6560   else
6561     TL.setSizeExpr(nullptr);
6562 }
6563 
6564 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6565   VisitArrayTypeLoc(TL);
6566 }
6567 
6568 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6569   VisitArrayTypeLoc(TL);
6570 }
6571 
6572 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6573   VisitArrayTypeLoc(TL);
6574 }
6575 
6576 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6577                                             DependentSizedArrayTypeLoc TL) {
6578   VisitArrayTypeLoc(TL);
6579 }
6580 
6581 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6582     DependentAddressSpaceTypeLoc TL) {
6583 
6584     TL.setAttrNameLoc(ReadSourceLocation());
6585     SourceRange range;
6586     range.setBegin(ReadSourceLocation());
6587     range.setEnd(ReadSourceLocation());
6588     TL.setAttrOperandParensRange(range);
6589     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6590 }
6591 
6592 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6593                                         DependentSizedExtVectorTypeLoc TL) {
6594   TL.setNameLoc(ReadSourceLocation());
6595 }
6596 
6597 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6598   TL.setNameLoc(ReadSourceLocation());
6599 }
6600 
6601 void TypeLocReader::VisitDependentVectorTypeLoc(
6602     DependentVectorTypeLoc TL) {
6603   TL.setNameLoc(ReadSourceLocation());
6604 }
6605 
6606 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6607   TL.setNameLoc(ReadSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6611   TL.setLocalRangeBegin(ReadSourceLocation());
6612   TL.setLParenLoc(ReadSourceLocation());
6613   TL.setRParenLoc(ReadSourceLocation());
6614   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
6615                                        Reader->ReadSourceLocation(*F, Record, Idx)));
6616   TL.setLocalRangeEnd(ReadSourceLocation());
6617   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6618     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
6619   }
6620 }
6621 
6622 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6623   VisitFunctionTypeLoc(TL);
6624 }
6625 
6626 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6627   VisitFunctionTypeLoc(TL);
6628 }
6629 
6630 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6631   TL.setNameLoc(ReadSourceLocation());
6632 }
6633 
6634 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6635   TL.setNameLoc(ReadSourceLocation());
6636 }
6637 
6638 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6639   TL.setTypeofLoc(ReadSourceLocation());
6640   TL.setLParenLoc(ReadSourceLocation());
6641   TL.setRParenLoc(ReadSourceLocation());
6642 }
6643 
6644 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6645   TL.setTypeofLoc(ReadSourceLocation());
6646   TL.setLParenLoc(ReadSourceLocation());
6647   TL.setRParenLoc(ReadSourceLocation());
6648   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6649 }
6650 
6651 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6652   TL.setNameLoc(ReadSourceLocation());
6653 }
6654 
6655 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6656   TL.setKWLoc(ReadSourceLocation());
6657   TL.setLParenLoc(ReadSourceLocation());
6658   TL.setRParenLoc(ReadSourceLocation());
6659   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6660 }
6661 
6662 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6663   TL.setNameLoc(ReadSourceLocation());
6664 }
6665 
6666 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6667     DeducedTemplateSpecializationTypeLoc TL) {
6668   TL.setTemplateNameLoc(ReadSourceLocation());
6669 }
6670 
6671 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6672   TL.setNameLoc(ReadSourceLocation());
6673 }
6674 
6675 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6676   TL.setNameLoc(ReadSourceLocation());
6677 }
6678 
6679 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6680   TL.setAttr(ReadAttr());
6681 }
6682 
6683 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6684   TL.setNameLoc(ReadSourceLocation());
6685 }
6686 
6687 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6688                                             SubstTemplateTypeParmTypeLoc TL) {
6689   TL.setNameLoc(ReadSourceLocation());
6690 }
6691 
6692 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6693                                           SubstTemplateTypeParmPackTypeLoc TL) {
6694   TL.setNameLoc(ReadSourceLocation());
6695 }
6696 
6697 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6698                                            TemplateSpecializationTypeLoc TL) {
6699   TL.setTemplateKeywordLoc(ReadSourceLocation());
6700   TL.setTemplateNameLoc(ReadSourceLocation());
6701   TL.setLAngleLoc(ReadSourceLocation());
6702   TL.setRAngleLoc(ReadSourceLocation());
6703   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6704     TL.setArgLocInfo(
6705         i,
6706         Reader->GetTemplateArgumentLocInfo(
6707             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
6708 }
6709 
6710 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6711   TL.setLParenLoc(ReadSourceLocation());
6712   TL.setRParenLoc(ReadSourceLocation());
6713 }
6714 
6715 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6716   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6717   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6718 }
6719 
6720 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6721   TL.setNameLoc(ReadSourceLocation());
6722 }
6723 
6724 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6725   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6726   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6727   TL.setNameLoc(ReadSourceLocation());
6728 }
6729 
6730 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6731        DependentTemplateSpecializationTypeLoc TL) {
6732   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6733   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6734   TL.setTemplateKeywordLoc(ReadSourceLocation());
6735   TL.setTemplateNameLoc(ReadSourceLocation());
6736   TL.setLAngleLoc(ReadSourceLocation());
6737   TL.setRAngleLoc(ReadSourceLocation());
6738   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6739     TL.setArgLocInfo(
6740         I,
6741         Reader->GetTemplateArgumentLocInfo(
6742             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
6743 }
6744 
6745 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6746   TL.setEllipsisLoc(ReadSourceLocation());
6747 }
6748 
6749 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6750   TL.setNameLoc(ReadSourceLocation());
6751 }
6752 
6753 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6754   if (TL.getNumProtocols()) {
6755     TL.setProtocolLAngleLoc(ReadSourceLocation());
6756     TL.setProtocolRAngleLoc(ReadSourceLocation());
6757   }
6758   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6759     TL.setProtocolLoc(i, ReadSourceLocation());
6760 }
6761 
6762 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6763   TL.setHasBaseTypeAsWritten(Record[Idx++]);
6764   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
6765   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
6766   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6767     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6768   TL.setProtocolLAngleLoc(ReadSourceLocation());
6769   TL.setProtocolRAngleLoc(ReadSourceLocation());
6770   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6771     TL.setProtocolLoc(i, ReadSourceLocation());
6772 }
6773 
6774 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6775   TL.setStarLoc(ReadSourceLocation());
6776 }
6777 
6778 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6779   TL.setKWLoc(ReadSourceLocation());
6780   TL.setLParenLoc(ReadSourceLocation());
6781   TL.setRParenLoc(ReadSourceLocation());
6782 }
6783 
6784 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6785   TL.setKWLoc(ReadSourceLocation());
6786 }
6787 
6788 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
6789                             unsigned &Idx, TypeLoc TL) {
6790   TypeLocReader TLR(F, *this, Record, Idx);
6791   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6792     TLR.Visit(TL);
6793 }
6794 
6795 TypeSourceInfo *
6796 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
6797                              unsigned &Idx) {
6798   QualType InfoTy = readType(F, Record, Idx);
6799   if (InfoTy.isNull())
6800     return nullptr;
6801 
6802   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6803   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
6804   return TInfo;
6805 }
6806 
6807 QualType ASTReader::GetType(TypeID ID) {
6808   assert(ContextObj && "reading type with no AST context");
6809   ASTContext &Context = *ContextObj;
6810 
6811   unsigned FastQuals = ID & Qualifiers::FastMask;
6812   unsigned Index = ID >> Qualifiers::FastWidth;
6813 
6814   if (Index < NUM_PREDEF_TYPE_IDS) {
6815     QualType T;
6816     switch ((PredefinedTypeIDs)Index) {
6817     case PREDEF_TYPE_NULL_ID:
6818       return QualType();
6819     case PREDEF_TYPE_VOID_ID:
6820       T = Context.VoidTy;
6821       break;
6822     case PREDEF_TYPE_BOOL_ID:
6823       T = Context.BoolTy;
6824       break;
6825     case PREDEF_TYPE_CHAR_U_ID:
6826     case PREDEF_TYPE_CHAR_S_ID:
6827       // FIXME: Check that the signedness of CharTy is correct!
6828       T = Context.CharTy;
6829       break;
6830     case PREDEF_TYPE_UCHAR_ID:
6831       T = Context.UnsignedCharTy;
6832       break;
6833     case PREDEF_TYPE_USHORT_ID:
6834       T = Context.UnsignedShortTy;
6835       break;
6836     case PREDEF_TYPE_UINT_ID:
6837       T = Context.UnsignedIntTy;
6838       break;
6839     case PREDEF_TYPE_ULONG_ID:
6840       T = Context.UnsignedLongTy;
6841       break;
6842     case PREDEF_TYPE_ULONGLONG_ID:
6843       T = Context.UnsignedLongLongTy;
6844       break;
6845     case PREDEF_TYPE_UINT128_ID:
6846       T = Context.UnsignedInt128Ty;
6847       break;
6848     case PREDEF_TYPE_SCHAR_ID:
6849       T = Context.SignedCharTy;
6850       break;
6851     case PREDEF_TYPE_WCHAR_ID:
6852       T = Context.WCharTy;
6853       break;
6854     case PREDEF_TYPE_SHORT_ID:
6855       T = Context.ShortTy;
6856       break;
6857     case PREDEF_TYPE_INT_ID:
6858       T = Context.IntTy;
6859       break;
6860     case PREDEF_TYPE_LONG_ID:
6861       T = Context.LongTy;
6862       break;
6863     case PREDEF_TYPE_LONGLONG_ID:
6864       T = Context.LongLongTy;
6865       break;
6866     case PREDEF_TYPE_INT128_ID:
6867       T = Context.Int128Ty;
6868       break;
6869     case PREDEF_TYPE_HALF_ID:
6870       T = Context.HalfTy;
6871       break;
6872     case PREDEF_TYPE_FLOAT_ID:
6873       T = Context.FloatTy;
6874       break;
6875     case PREDEF_TYPE_DOUBLE_ID:
6876       T = Context.DoubleTy;
6877       break;
6878     case PREDEF_TYPE_LONGDOUBLE_ID:
6879       T = Context.LongDoubleTy;
6880       break;
6881     case PREDEF_TYPE_SHORT_ACCUM_ID:
6882       T = Context.ShortAccumTy;
6883       break;
6884     case PREDEF_TYPE_ACCUM_ID:
6885       T = Context.AccumTy;
6886       break;
6887     case PREDEF_TYPE_LONG_ACCUM_ID:
6888       T = Context.LongAccumTy;
6889       break;
6890     case PREDEF_TYPE_USHORT_ACCUM_ID:
6891       T = Context.UnsignedShortAccumTy;
6892       break;
6893     case PREDEF_TYPE_UACCUM_ID:
6894       T = Context.UnsignedAccumTy;
6895       break;
6896     case PREDEF_TYPE_ULONG_ACCUM_ID:
6897       T = Context.UnsignedLongAccumTy;
6898       break;
6899     case PREDEF_TYPE_SHORT_FRACT_ID:
6900       T = Context.ShortFractTy;
6901       break;
6902     case PREDEF_TYPE_FRACT_ID:
6903       T = Context.FractTy;
6904       break;
6905     case PREDEF_TYPE_LONG_FRACT_ID:
6906       T = Context.LongFractTy;
6907       break;
6908     case PREDEF_TYPE_USHORT_FRACT_ID:
6909       T = Context.UnsignedShortFractTy;
6910       break;
6911     case PREDEF_TYPE_UFRACT_ID:
6912       T = Context.UnsignedFractTy;
6913       break;
6914     case PREDEF_TYPE_ULONG_FRACT_ID:
6915       T = Context.UnsignedLongFractTy;
6916       break;
6917     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6918       T = Context.SatShortAccumTy;
6919       break;
6920     case PREDEF_TYPE_SAT_ACCUM_ID:
6921       T = Context.SatAccumTy;
6922       break;
6923     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6924       T = Context.SatLongAccumTy;
6925       break;
6926     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6927       T = Context.SatUnsignedShortAccumTy;
6928       break;
6929     case PREDEF_TYPE_SAT_UACCUM_ID:
6930       T = Context.SatUnsignedAccumTy;
6931       break;
6932     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6933       T = Context.SatUnsignedLongAccumTy;
6934       break;
6935     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6936       T = Context.SatShortFractTy;
6937       break;
6938     case PREDEF_TYPE_SAT_FRACT_ID:
6939       T = Context.SatFractTy;
6940       break;
6941     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6942       T = Context.SatLongFractTy;
6943       break;
6944     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6945       T = Context.SatUnsignedShortFractTy;
6946       break;
6947     case PREDEF_TYPE_SAT_UFRACT_ID:
6948       T = Context.SatUnsignedFractTy;
6949       break;
6950     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6951       T = Context.SatUnsignedLongFractTy;
6952       break;
6953     case PREDEF_TYPE_FLOAT16_ID:
6954       T = Context.Float16Ty;
6955       break;
6956     case PREDEF_TYPE_FLOAT128_ID:
6957       T = Context.Float128Ty;
6958       break;
6959     case PREDEF_TYPE_OVERLOAD_ID:
6960       T = Context.OverloadTy;
6961       break;
6962     case PREDEF_TYPE_BOUND_MEMBER:
6963       T = Context.BoundMemberTy;
6964       break;
6965     case PREDEF_TYPE_PSEUDO_OBJECT:
6966       T = Context.PseudoObjectTy;
6967       break;
6968     case PREDEF_TYPE_DEPENDENT_ID:
6969       T = Context.DependentTy;
6970       break;
6971     case PREDEF_TYPE_UNKNOWN_ANY:
6972       T = Context.UnknownAnyTy;
6973       break;
6974     case PREDEF_TYPE_NULLPTR_ID:
6975       T = Context.NullPtrTy;
6976       break;
6977     case PREDEF_TYPE_CHAR8_ID:
6978       T = Context.Char8Ty;
6979       break;
6980     case PREDEF_TYPE_CHAR16_ID:
6981       T = Context.Char16Ty;
6982       break;
6983     case PREDEF_TYPE_CHAR32_ID:
6984       T = Context.Char32Ty;
6985       break;
6986     case PREDEF_TYPE_OBJC_ID:
6987       T = Context.ObjCBuiltinIdTy;
6988       break;
6989     case PREDEF_TYPE_OBJC_CLASS:
6990       T = Context.ObjCBuiltinClassTy;
6991       break;
6992     case PREDEF_TYPE_OBJC_SEL:
6993       T = Context.ObjCBuiltinSelTy;
6994       break;
6995 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6996     case PREDEF_TYPE_##Id##_ID: \
6997       T = Context.SingletonId; \
6998       break;
6999 #include "clang/Basic/OpenCLImageTypes.def"
7000 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7001     case PREDEF_TYPE_##Id##_ID: \
7002       T = Context.Id##Ty; \
7003       break;
7004 #include "clang/Basic/OpenCLExtensionTypes.def"
7005     case PREDEF_TYPE_SAMPLER_ID:
7006       T = Context.OCLSamplerTy;
7007       break;
7008     case PREDEF_TYPE_EVENT_ID:
7009       T = Context.OCLEventTy;
7010       break;
7011     case PREDEF_TYPE_CLK_EVENT_ID:
7012       T = Context.OCLClkEventTy;
7013       break;
7014     case PREDEF_TYPE_QUEUE_ID:
7015       T = Context.OCLQueueTy;
7016       break;
7017     case PREDEF_TYPE_RESERVE_ID_ID:
7018       T = Context.OCLReserveIDTy;
7019       break;
7020     case PREDEF_TYPE_AUTO_DEDUCT:
7021       T = Context.getAutoDeductType();
7022       break;
7023     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7024       T = Context.getAutoRRefDeductType();
7025       break;
7026     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7027       T = Context.ARCUnbridgedCastTy;
7028       break;
7029     case PREDEF_TYPE_BUILTIN_FN:
7030       T = Context.BuiltinFnTy;
7031       break;
7032     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7033       T = Context.OMPArraySectionTy;
7034       break;
7035     }
7036 
7037     assert(!T.isNull() && "Unknown predefined type");
7038     return T.withFastQualifiers(FastQuals);
7039   }
7040 
7041   Index -= NUM_PREDEF_TYPE_IDS;
7042   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7043   if (TypesLoaded[Index].isNull()) {
7044     TypesLoaded[Index] = readTypeRecord(Index);
7045     if (TypesLoaded[Index].isNull())
7046       return QualType();
7047 
7048     TypesLoaded[Index]->setFromAST();
7049     if (DeserializationListener)
7050       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7051                                         TypesLoaded[Index]);
7052   }
7053 
7054   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7055 }
7056 
7057 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7058   return GetType(getGlobalTypeID(F, LocalID));
7059 }
7060 
7061 serialization::TypeID
7062 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7063   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7064   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7065 
7066   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7067     return LocalID;
7068 
7069   if (!F.ModuleOffsetMap.empty())
7070     ReadModuleOffsetMap(F);
7071 
7072   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7073     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7074   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7075 
7076   unsigned GlobalIndex = LocalIndex + I->second;
7077   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7078 }
7079 
7080 TemplateArgumentLocInfo
7081 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7082                                       TemplateArgument::ArgKind Kind,
7083                                       const RecordData &Record,
7084                                       unsigned &Index) {
7085   switch (Kind) {
7086   case TemplateArgument::Expression:
7087     return ReadExpr(F);
7088   case TemplateArgument::Type:
7089     return GetTypeSourceInfo(F, Record, Index);
7090   case TemplateArgument::Template: {
7091     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7092                                                                      Index);
7093     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7094     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7095                                    SourceLocation());
7096   }
7097   case TemplateArgument::TemplateExpansion: {
7098     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7099                                                                      Index);
7100     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7101     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7102     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7103                                    EllipsisLoc);
7104   }
7105   case TemplateArgument::Null:
7106   case TemplateArgument::Integral:
7107   case TemplateArgument::Declaration:
7108   case TemplateArgument::NullPtr:
7109   case TemplateArgument::Pack:
7110     // FIXME: Is this right?
7111     return TemplateArgumentLocInfo();
7112   }
7113   llvm_unreachable("unexpected template argument loc");
7114 }
7115 
7116 TemplateArgumentLoc
7117 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7118                                    const RecordData &Record, unsigned &Index) {
7119   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7120 
7121   if (Arg.getKind() == TemplateArgument::Expression) {
7122     if (Record[Index++]) // bool InfoHasSameExpr.
7123       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7124   }
7125   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7126                                                              Record, Index));
7127 }
7128 
7129 const ASTTemplateArgumentListInfo*
7130 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7131                                            const RecordData &Record,
7132                                            unsigned &Index) {
7133   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7134   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7135   unsigned NumArgsAsWritten = Record[Index++];
7136   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7137   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7138     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7139   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7140 }
7141 
7142 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7143   return GetDecl(ID);
7144 }
7145 
7146 void ASTReader::CompleteRedeclChain(const Decl *D) {
7147   if (NumCurrentElementsDeserializing) {
7148     // We arrange to not care about the complete redeclaration chain while we're
7149     // deserializing. Just remember that the AST has marked this one as complete
7150     // but that it's not actually complete yet, so we know we still need to
7151     // complete it later.
7152     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7153     return;
7154   }
7155 
7156   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7157 
7158   // If this is a named declaration, complete it by looking it up
7159   // within its context.
7160   //
7161   // FIXME: Merging a function definition should merge
7162   // all mergeable entities within it.
7163   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7164       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7165     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7166       if (!getContext().getLangOpts().CPlusPlus &&
7167           isa<TranslationUnitDecl>(DC)) {
7168         // Outside of C++, we don't have a lookup table for the TU, so update
7169         // the identifier instead. (For C++ modules, we don't store decls
7170         // in the serialized identifier table, so we do the lookup in the TU.)
7171         auto *II = Name.getAsIdentifierInfo();
7172         assert(II && "non-identifier name in C?");
7173         if (II->isOutOfDate())
7174           updateOutOfDateIdentifier(*II);
7175       } else
7176         DC->lookup(Name);
7177     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7178       // Find all declarations of this kind from the relevant context.
7179       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7180         auto *DC = cast<DeclContext>(DCDecl);
7181         SmallVector<Decl*, 8> Decls;
7182         FindExternalLexicalDecls(
7183             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7184       }
7185     }
7186   }
7187 
7188   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7189     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7190   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7191     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7192   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7193     if (auto *Template = FD->getPrimaryTemplate())
7194       Template->LoadLazySpecializations();
7195   }
7196 }
7197 
7198 CXXCtorInitializer **
7199 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7200   RecordLocation Loc = getLocalBitOffset(Offset);
7201   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7202   SavedStreamPosition SavedPosition(Cursor);
7203   Cursor.JumpToBit(Loc.Offset);
7204   ReadingKindTracker ReadingKind(Read_Decl, *this);
7205 
7206   RecordData Record;
7207   unsigned Code = Cursor.ReadCode();
7208   unsigned RecCode = Cursor.readRecord(Code, Record);
7209   if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
7210     Error("malformed AST file: missing C++ ctor initializers");
7211     return nullptr;
7212   }
7213 
7214   unsigned Idx = 0;
7215   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7216 }
7217 
7218 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7219   assert(ContextObj && "reading base specifiers with no AST context");
7220   ASTContext &Context = *ContextObj;
7221 
7222   RecordLocation Loc = getLocalBitOffset(Offset);
7223   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7224   SavedStreamPosition SavedPosition(Cursor);
7225   Cursor.JumpToBit(Loc.Offset);
7226   ReadingKindTracker ReadingKind(Read_Decl, *this);
7227   RecordData Record;
7228   unsigned Code = Cursor.ReadCode();
7229   unsigned RecCode = Cursor.readRecord(Code, Record);
7230   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7231     Error("malformed AST file: missing C++ base specifiers");
7232     return nullptr;
7233   }
7234 
7235   unsigned Idx = 0;
7236   unsigned NumBases = Record[Idx++];
7237   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7238   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7239   for (unsigned I = 0; I != NumBases; ++I)
7240     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7241   return Bases;
7242 }
7243 
7244 serialization::DeclID
7245 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7246   if (LocalID < NUM_PREDEF_DECL_IDS)
7247     return LocalID;
7248 
7249   if (!F.ModuleOffsetMap.empty())
7250     ReadModuleOffsetMap(F);
7251 
7252   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7253     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7254   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7255 
7256   return LocalID + I->second;
7257 }
7258 
7259 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7260                                    ModuleFile &M) const {
7261   // Predefined decls aren't from any module.
7262   if (ID < NUM_PREDEF_DECL_IDS)
7263     return false;
7264 
7265   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7266          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7267 }
7268 
7269 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7270   if (!D->isFromASTFile())
7271     return nullptr;
7272   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7273   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7274   return I->second;
7275 }
7276 
7277 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7278   if (ID < NUM_PREDEF_DECL_IDS)
7279     return SourceLocation();
7280 
7281   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7282 
7283   if (Index > DeclsLoaded.size()) {
7284     Error("declaration ID out-of-range for AST file");
7285     return SourceLocation();
7286   }
7287 
7288   if (Decl *D = DeclsLoaded[Index])
7289     return D->getLocation();
7290 
7291   SourceLocation Loc;
7292   DeclCursorForID(ID, Loc);
7293   return Loc;
7294 }
7295 
7296 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7297   switch (ID) {
7298   case PREDEF_DECL_NULL_ID:
7299     return nullptr;
7300 
7301   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7302     return Context.getTranslationUnitDecl();
7303 
7304   case PREDEF_DECL_OBJC_ID_ID:
7305     return Context.getObjCIdDecl();
7306 
7307   case PREDEF_DECL_OBJC_SEL_ID:
7308     return Context.getObjCSelDecl();
7309 
7310   case PREDEF_DECL_OBJC_CLASS_ID:
7311     return Context.getObjCClassDecl();
7312 
7313   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7314     return Context.getObjCProtocolDecl();
7315 
7316   case PREDEF_DECL_INT_128_ID:
7317     return Context.getInt128Decl();
7318 
7319   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7320     return Context.getUInt128Decl();
7321 
7322   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7323     return Context.getObjCInstanceTypeDecl();
7324 
7325   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7326     return Context.getBuiltinVaListDecl();
7327 
7328   case PREDEF_DECL_VA_LIST_TAG:
7329     return Context.getVaListTagDecl();
7330 
7331   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7332     return Context.getBuiltinMSVaListDecl();
7333 
7334   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7335     return Context.getExternCContextDecl();
7336 
7337   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7338     return Context.getMakeIntegerSeqDecl();
7339 
7340   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7341     return Context.getCFConstantStringDecl();
7342 
7343   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7344     return Context.getCFConstantStringTagDecl();
7345 
7346   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7347     return Context.getTypePackElementDecl();
7348   }
7349   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7350 }
7351 
7352 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7353   assert(ContextObj && "reading decl with no AST context");
7354   if (ID < NUM_PREDEF_DECL_IDS) {
7355     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7356     if (D) {
7357       // Track that we have merged the declaration with ID \p ID into the
7358       // pre-existing predefined declaration \p D.
7359       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7360       if (Merged.empty())
7361         Merged.push_back(ID);
7362     }
7363     return D;
7364   }
7365 
7366   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7367 
7368   if (Index >= DeclsLoaded.size()) {
7369     assert(0 && "declaration ID out-of-range for AST file");
7370     Error("declaration ID out-of-range for AST file");
7371     return nullptr;
7372   }
7373 
7374   return DeclsLoaded[Index];
7375 }
7376 
7377 Decl *ASTReader::GetDecl(DeclID ID) {
7378   if (ID < NUM_PREDEF_DECL_IDS)
7379     return GetExistingDecl(ID);
7380 
7381   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7382 
7383   if (Index >= DeclsLoaded.size()) {
7384     assert(0 && "declaration ID out-of-range for AST file");
7385     Error("declaration ID out-of-range for AST file");
7386     return nullptr;
7387   }
7388 
7389   if (!DeclsLoaded[Index]) {
7390     ReadDeclRecord(ID);
7391     if (DeserializationListener)
7392       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7393   }
7394 
7395   return DeclsLoaded[Index];
7396 }
7397 
7398 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7399                                                   DeclID GlobalID) {
7400   if (GlobalID < NUM_PREDEF_DECL_IDS)
7401     return GlobalID;
7402 
7403   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7404   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7405   ModuleFile *Owner = I->second;
7406 
7407   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7408     = M.GlobalToLocalDeclIDs.find(Owner);
7409   if (Pos == M.GlobalToLocalDeclIDs.end())
7410     return 0;
7411 
7412   return GlobalID - Owner->BaseDeclID + Pos->second;
7413 }
7414 
7415 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7416                                             const RecordData &Record,
7417                                             unsigned &Idx) {
7418   if (Idx >= Record.size()) {
7419     Error("Corrupted AST file");
7420     return 0;
7421   }
7422 
7423   return getGlobalDeclID(F, Record[Idx++]);
7424 }
7425 
7426 /// Resolve the offset of a statement into a statement.
7427 ///
7428 /// This operation will read a new statement from the external
7429 /// source each time it is called, and is meant to be used via a
7430 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7431 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7432   // Switch case IDs are per Decl.
7433   ClearSwitchCaseIDs();
7434 
7435   // Offset here is a global offset across the entire chain.
7436   RecordLocation Loc = getLocalBitOffset(Offset);
7437   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
7438   assert(NumCurrentElementsDeserializing == 0 &&
7439          "should not be called while already deserializing");
7440   Deserializing D(this);
7441   return ReadStmtFromStream(*Loc.F);
7442 }
7443 
7444 void ASTReader::FindExternalLexicalDecls(
7445     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7446     SmallVectorImpl<Decl *> &Decls) {
7447   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7448 
7449   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7450     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7451     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7452       auto K = (Decl::Kind)+LexicalDecls[I];
7453       if (!IsKindWeWant(K))
7454         continue;
7455 
7456       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7457 
7458       // Don't add predefined declarations to the lexical context more
7459       // than once.
7460       if (ID < NUM_PREDEF_DECL_IDS) {
7461         if (PredefsVisited[ID])
7462           continue;
7463 
7464         PredefsVisited[ID] = true;
7465       }
7466 
7467       if (Decl *D = GetLocalDecl(*M, ID)) {
7468         assert(D->getKind() == K && "wrong kind for lexical decl");
7469         if (!DC->isDeclInLexicalTraversal(D))
7470           Decls.push_back(D);
7471       }
7472     }
7473   };
7474 
7475   if (isa<TranslationUnitDecl>(DC)) {
7476     for (auto Lexical : TULexicalDecls)
7477       Visit(Lexical.first, Lexical.second);
7478   } else {
7479     auto I = LexicalDecls.find(DC);
7480     if (I != LexicalDecls.end())
7481       Visit(I->second.first, I->second.second);
7482   }
7483 
7484   ++NumLexicalDeclContextsRead;
7485 }
7486 
7487 namespace {
7488 
7489 class DeclIDComp {
7490   ASTReader &Reader;
7491   ModuleFile &Mod;
7492 
7493 public:
7494   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7495 
7496   bool operator()(LocalDeclID L, LocalDeclID R) const {
7497     SourceLocation LHS = getLocation(L);
7498     SourceLocation RHS = getLocation(R);
7499     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7500   }
7501 
7502   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7503     SourceLocation RHS = getLocation(R);
7504     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7505   }
7506 
7507   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7508     SourceLocation LHS = getLocation(L);
7509     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7510   }
7511 
7512   SourceLocation getLocation(LocalDeclID ID) const {
7513     return Reader.getSourceManager().getFileLoc(
7514             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7515   }
7516 };
7517 
7518 } // namespace
7519 
7520 void ASTReader::FindFileRegionDecls(FileID File,
7521                                     unsigned Offset, unsigned Length,
7522                                     SmallVectorImpl<Decl *> &Decls) {
7523   SourceManager &SM = getSourceManager();
7524 
7525   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7526   if (I == FileDeclIDs.end())
7527     return;
7528 
7529   FileDeclsInfo &DInfo = I->second;
7530   if (DInfo.Decls.empty())
7531     return;
7532 
7533   SourceLocation
7534     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7535   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7536 
7537   DeclIDComp DIDComp(*this, *DInfo.Mod);
7538   ArrayRef<serialization::LocalDeclID>::iterator
7539     BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7540                                BeginLoc, DIDComp);
7541   if (BeginIt != DInfo.Decls.begin())
7542     --BeginIt;
7543 
7544   // If we are pointing at a top-level decl inside an objc container, we need
7545   // to backtrack until we find it otherwise we will fail to report that the
7546   // region overlaps with an objc container.
7547   while (BeginIt != DInfo.Decls.begin() &&
7548          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7549              ->isTopLevelDeclInObjCContainer())
7550     --BeginIt;
7551 
7552   ArrayRef<serialization::LocalDeclID>::iterator
7553     EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7554                              EndLoc, DIDComp);
7555   if (EndIt != DInfo.Decls.end())
7556     ++EndIt;
7557 
7558   for (ArrayRef<serialization::LocalDeclID>::iterator
7559          DIt = BeginIt; DIt != EndIt; ++DIt)
7560     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7561 }
7562 
7563 bool
7564 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7565                                           DeclarationName Name) {
7566   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7567          "DeclContext has no visible decls in storage");
7568   if (!Name)
7569     return false;
7570 
7571   auto It = Lookups.find(DC);
7572   if (It == Lookups.end())
7573     return false;
7574 
7575   Deserializing LookupResults(this);
7576 
7577   // Load the list of declarations.
7578   SmallVector<NamedDecl *, 64> Decls;
7579   for (DeclID ID : It->second.Table.find(Name)) {
7580     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7581     if (ND->getDeclName() == Name)
7582       Decls.push_back(ND);
7583   }
7584 
7585   ++NumVisibleDeclContextsRead;
7586   SetExternalVisibleDeclsForName(DC, Name, Decls);
7587   return !Decls.empty();
7588 }
7589 
7590 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7591   if (!DC->hasExternalVisibleStorage())
7592     return;
7593 
7594   auto It = Lookups.find(DC);
7595   assert(It != Lookups.end() &&
7596          "have external visible storage but no lookup tables");
7597 
7598   DeclsMap Decls;
7599 
7600   for (DeclID ID : It->second.Table.findAll()) {
7601     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7602     Decls[ND->getDeclName()].push_back(ND);
7603   }
7604 
7605   ++NumVisibleDeclContextsRead;
7606 
7607   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7608     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7609   }
7610   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7611 }
7612 
7613 const serialization::reader::DeclContextLookupTable *
7614 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7615   auto I = Lookups.find(Primary);
7616   return I == Lookups.end() ? nullptr : &I->second;
7617 }
7618 
7619 /// Under non-PCH compilation the consumer receives the objc methods
7620 /// before receiving the implementation, and codegen depends on this.
7621 /// We simulate this by deserializing and passing to consumer the methods of the
7622 /// implementation before passing the deserialized implementation decl.
7623 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7624                                        ASTConsumer *Consumer) {
7625   assert(ImplD && Consumer);
7626 
7627   for (auto *I : ImplD->methods())
7628     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7629 
7630   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7631 }
7632 
7633 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7634   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7635     PassObjCImplDeclToConsumer(ImplD, Consumer);
7636   else
7637     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7638 }
7639 
7640 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7641   this->Consumer = Consumer;
7642 
7643   if (Consumer)
7644     PassInterestingDeclsToConsumer();
7645 
7646   if (DeserializationListener)
7647     DeserializationListener->ReaderInitialized(this);
7648 }
7649 
7650 void ASTReader::PrintStats() {
7651   std::fprintf(stderr, "*** AST File Statistics:\n");
7652 
7653   unsigned NumTypesLoaded
7654     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7655                                       QualType());
7656   unsigned NumDeclsLoaded
7657     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7658                                       (Decl *)nullptr);
7659   unsigned NumIdentifiersLoaded
7660     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7661                                             IdentifiersLoaded.end(),
7662                                             (IdentifierInfo *)nullptr);
7663   unsigned NumMacrosLoaded
7664     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7665                                        MacrosLoaded.end(),
7666                                        (MacroInfo *)nullptr);
7667   unsigned NumSelectorsLoaded
7668     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7669                                           SelectorsLoaded.end(),
7670                                           Selector());
7671 
7672   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7673     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7674                  NumSLocEntriesRead, TotalNumSLocEntries,
7675                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7676   if (!TypesLoaded.empty())
7677     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7678                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7679                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7680   if (!DeclsLoaded.empty())
7681     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7682                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7683                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7684   if (!IdentifiersLoaded.empty())
7685     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7686                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7687                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7688   if (!MacrosLoaded.empty())
7689     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7690                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7691                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7692   if (!SelectorsLoaded.empty())
7693     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7694                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7695                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7696   if (TotalNumStatements)
7697     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7698                  NumStatementsRead, TotalNumStatements,
7699                  ((float)NumStatementsRead/TotalNumStatements * 100));
7700   if (TotalNumMacros)
7701     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7702                  NumMacrosRead, TotalNumMacros,
7703                  ((float)NumMacrosRead/TotalNumMacros * 100));
7704   if (TotalLexicalDeclContexts)
7705     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7706                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7707                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7708                   * 100));
7709   if (TotalVisibleDeclContexts)
7710     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7711                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7712                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7713                   * 100));
7714   if (TotalNumMethodPoolEntries)
7715     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7716                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7717                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7718                   * 100));
7719   if (NumMethodPoolLookups)
7720     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7721                  NumMethodPoolHits, NumMethodPoolLookups,
7722                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7723   if (NumMethodPoolTableLookups)
7724     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7725                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7726                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7727                   * 100.0));
7728   if (NumIdentifierLookupHits)
7729     std::fprintf(stderr,
7730                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7731                  NumIdentifierLookupHits, NumIdentifierLookups,
7732                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7733 
7734   if (GlobalIndex) {
7735     std::fprintf(stderr, "\n");
7736     GlobalIndex->printStats();
7737   }
7738 
7739   std::fprintf(stderr, "\n");
7740   dump();
7741   std::fprintf(stderr, "\n");
7742 }
7743 
7744 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7745 LLVM_DUMP_METHOD static void
7746 dumpModuleIDMap(StringRef Name,
7747                 const ContinuousRangeMap<Key, ModuleFile *,
7748                                          InitialCapacity> &Map) {
7749   if (Map.begin() == Map.end())
7750     return;
7751 
7752   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7753 
7754   llvm::errs() << Name << ":\n";
7755   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7756        I != IEnd; ++I) {
7757     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7758       << "\n";
7759   }
7760 }
7761 
7762 LLVM_DUMP_METHOD void ASTReader::dump() {
7763   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7764   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7765   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7766   dumpModuleIDMap("Global type map", GlobalTypeMap);
7767   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7768   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7769   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7770   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7771   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7772   dumpModuleIDMap("Global preprocessed entity map",
7773                   GlobalPreprocessedEntityMap);
7774 
7775   llvm::errs() << "\n*** PCH/Modules Loaded:";
7776   for (ModuleFile &M : ModuleMgr)
7777     M.dump();
7778 }
7779 
7780 /// Return the amount of memory used by memory buffers, breaking down
7781 /// by heap-backed versus mmap'ed memory.
7782 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7783   for (ModuleFile &I : ModuleMgr) {
7784     if (llvm::MemoryBuffer *buf = I.Buffer) {
7785       size_t bytes = buf->getBufferSize();
7786       switch (buf->getBufferKind()) {
7787         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7788           sizes.malloc_bytes += bytes;
7789           break;
7790         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7791           sizes.mmap_bytes += bytes;
7792           break;
7793       }
7794     }
7795   }
7796 }
7797 
7798 void ASTReader::InitializeSema(Sema &S) {
7799   SemaObj = &S;
7800   S.addExternalSource(this);
7801 
7802   // Makes sure any declarations that were deserialized "too early"
7803   // still get added to the identifier's declaration chains.
7804   for (uint64_t ID : PreloadedDeclIDs) {
7805     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7806     pushExternalDeclIntoScope(D, D->getDeclName());
7807   }
7808   PreloadedDeclIDs.clear();
7809 
7810   // FIXME: What happens if these are changed by a module import?
7811   if (!FPPragmaOptions.empty()) {
7812     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7813     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7814   }
7815 
7816   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7817   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7818   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7819 
7820   UpdateSema();
7821 }
7822 
7823 void ASTReader::UpdateSema() {
7824   assert(SemaObj && "no Sema to update");
7825 
7826   // Load the offsets of the declarations that Sema references.
7827   // They will be lazily deserialized when needed.
7828   if (!SemaDeclRefs.empty()) {
7829     assert(SemaDeclRefs.size() % 3 == 0);
7830     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7831       if (!SemaObj->StdNamespace)
7832         SemaObj->StdNamespace = SemaDeclRefs[I];
7833       if (!SemaObj->StdBadAlloc)
7834         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7835       if (!SemaObj->StdAlignValT)
7836         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7837     }
7838     SemaDeclRefs.clear();
7839   }
7840 
7841   // Update the state of pragmas. Use the same API as if we had encountered the
7842   // pragma in the source.
7843   if(OptimizeOffPragmaLocation.isValid())
7844     SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
7845   if (PragmaMSStructState != -1)
7846     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7847   if (PointersToMembersPragmaLocation.isValid()) {
7848     SemaObj->ActOnPragmaMSPointersToMembers(
7849         (LangOptions::PragmaMSPointersToMembersKind)
7850             PragmaMSPointersToMembersState,
7851         PointersToMembersPragmaLocation);
7852   }
7853   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7854 
7855   if (PragmaPackCurrentValue) {
7856     // The bottom of the stack might have a default value. It must be adjusted
7857     // to the current value to ensure that the packing state is preserved after
7858     // popping entries that were included/imported from a PCH/module.
7859     bool DropFirst = false;
7860     if (!PragmaPackStack.empty() &&
7861         PragmaPackStack.front().Location.isInvalid()) {
7862       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7863              "Expected a default alignment value");
7864       SemaObj->PackStack.Stack.emplace_back(
7865           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7866           SemaObj->PackStack.CurrentPragmaLocation,
7867           PragmaPackStack.front().PushLocation);
7868       DropFirst = true;
7869     }
7870     for (const auto &Entry :
7871          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7872       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7873                                             Entry.Location, Entry.PushLocation);
7874     if (PragmaPackCurrentLocation.isInvalid()) {
7875       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7876              "Expected a default alignment value");
7877       // Keep the current values.
7878     } else {
7879       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7880       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7881     }
7882   }
7883 }
7884 
7885 IdentifierInfo *ASTReader::get(StringRef Name) {
7886   // Note that we are loading an identifier.
7887   Deserializing AnIdentifier(this);
7888 
7889   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7890                                   NumIdentifierLookups,
7891                                   NumIdentifierLookupHits);
7892 
7893   // We don't need to do identifier table lookups in C++ modules (we preload
7894   // all interesting declarations, and don't need to use the scope for name
7895   // lookups). Perform the lookup in PCH files, though, since we don't build
7896   // a complete initial identifier table if we're carrying on from a PCH.
7897   if (PP.getLangOpts().CPlusPlus) {
7898     for (auto F : ModuleMgr.pch_modules())
7899       if (Visitor(*F))
7900         break;
7901   } else {
7902     // If there is a global index, look there first to determine which modules
7903     // provably do not have any results for this identifier.
7904     GlobalModuleIndex::HitSet Hits;
7905     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7906     if (!loadGlobalIndex()) {
7907       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7908         HitsPtr = &Hits;
7909       }
7910     }
7911 
7912     ModuleMgr.visit(Visitor, HitsPtr);
7913   }
7914 
7915   IdentifierInfo *II = Visitor.getIdentifierInfo();
7916   markIdentifierUpToDate(II);
7917   return II;
7918 }
7919 
7920 namespace clang {
7921 
7922   /// An identifier-lookup iterator that enumerates all of the
7923   /// identifiers stored within a set of AST files.
7924   class ASTIdentifierIterator : public IdentifierIterator {
7925     /// The AST reader whose identifiers are being enumerated.
7926     const ASTReader &Reader;
7927 
7928     /// The current index into the chain of AST files stored in
7929     /// the AST reader.
7930     unsigned Index;
7931 
7932     /// The current position within the identifier lookup table
7933     /// of the current AST file.
7934     ASTIdentifierLookupTable::key_iterator Current;
7935 
7936     /// The end position within the identifier lookup table of
7937     /// the current AST file.
7938     ASTIdentifierLookupTable::key_iterator End;
7939 
7940     /// Whether to skip any modules in the ASTReader.
7941     bool SkipModules;
7942 
7943   public:
7944     explicit ASTIdentifierIterator(const ASTReader &Reader,
7945                                    bool SkipModules = false);
7946 
7947     StringRef Next() override;
7948   };
7949 
7950 } // namespace clang
7951 
7952 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7953                                              bool SkipModules)
7954     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7955 }
7956 
7957 StringRef ASTIdentifierIterator::Next() {
7958   while (Current == End) {
7959     // If we have exhausted all of our AST files, we're done.
7960     if (Index == 0)
7961       return StringRef();
7962 
7963     --Index;
7964     ModuleFile &F = Reader.ModuleMgr[Index];
7965     if (SkipModules && F.isModule())
7966       continue;
7967 
7968     ASTIdentifierLookupTable *IdTable =
7969         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7970     Current = IdTable->key_begin();
7971     End = IdTable->key_end();
7972   }
7973 
7974   // We have any identifiers remaining in the current AST file; return
7975   // the next one.
7976   StringRef Result = *Current;
7977   ++Current;
7978   return Result;
7979 }
7980 
7981 namespace {
7982 
7983 /// A utility for appending two IdentifierIterators.
7984 class ChainedIdentifierIterator : public IdentifierIterator {
7985   std::unique_ptr<IdentifierIterator> Current;
7986   std::unique_ptr<IdentifierIterator> Queued;
7987 
7988 public:
7989   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7990                             std::unique_ptr<IdentifierIterator> Second)
7991       : Current(std::move(First)), Queued(std::move(Second)) {}
7992 
7993   StringRef Next() override {
7994     if (!Current)
7995       return StringRef();
7996 
7997     StringRef result = Current->Next();
7998     if (!result.empty())
7999       return result;
8000 
8001     // Try the queued iterator, which may itself be empty.
8002     Current.reset();
8003     std::swap(Current, Queued);
8004     return Next();
8005   }
8006 };
8007 
8008 } // namespace
8009 
8010 IdentifierIterator *ASTReader::getIdentifiers() {
8011   if (!loadGlobalIndex()) {
8012     std::unique_ptr<IdentifierIterator> ReaderIter(
8013         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8014     std::unique_ptr<IdentifierIterator> ModulesIter(
8015         GlobalIndex->createIdentifierIterator());
8016     return new ChainedIdentifierIterator(std::move(ReaderIter),
8017                                          std::move(ModulesIter));
8018   }
8019 
8020   return new ASTIdentifierIterator(*this);
8021 }
8022 
8023 namespace clang {
8024 namespace serialization {
8025 
8026   class ReadMethodPoolVisitor {
8027     ASTReader &Reader;
8028     Selector Sel;
8029     unsigned PriorGeneration;
8030     unsigned InstanceBits = 0;
8031     unsigned FactoryBits = 0;
8032     bool InstanceHasMoreThanOneDecl = false;
8033     bool FactoryHasMoreThanOneDecl = false;
8034     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8035     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8036 
8037   public:
8038     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8039                           unsigned PriorGeneration)
8040         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8041 
8042     bool operator()(ModuleFile &M) {
8043       if (!M.SelectorLookupTable)
8044         return false;
8045 
8046       // If we've already searched this module file, skip it now.
8047       if (M.Generation <= PriorGeneration)
8048         return true;
8049 
8050       ++Reader.NumMethodPoolTableLookups;
8051       ASTSelectorLookupTable *PoolTable
8052         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8053       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8054       if (Pos == PoolTable->end())
8055         return false;
8056 
8057       ++Reader.NumMethodPoolTableHits;
8058       ++Reader.NumSelectorsRead;
8059       // FIXME: Not quite happy with the statistics here. We probably should
8060       // disable this tracking when called via LoadSelector.
8061       // Also, should entries without methods count as misses?
8062       ++Reader.NumMethodPoolEntriesRead;
8063       ASTSelectorLookupTrait::data_type Data = *Pos;
8064       if (Reader.DeserializationListener)
8065         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8066 
8067       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8068       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8069       InstanceBits = Data.InstanceBits;
8070       FactoryBits = Data.FactoryBits;
8071       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8072       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8073       return true;
8074     }
8075 
8076     /// Retrieve the instance methods found by this visitor.
8077     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8078       return InstanceMethods;
8079     }
8080 
8081     /// Retrieve the instance methods found by this visitor.
8082     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8083       return FactoryMethods;
8084     }
8085 
8086     unsigned getInstanceBits() const { return InstanceBits; }
8087     unsigned getFactoryBits() const { return FactoryBits; }
8088 
8089     bool instanceHasMoreThanOneDecl() const {
8090       return InstanceHasMoreThanOneDecl;
8091     }
8092 
8093     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8094   };
8095 
8096 } // namespace serialization
8097 } // namespace clang
8098 
8099 /// Add the given set of methods to the method list.
8100 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8101                              ObjCMethodList &List) {
8102   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8103     S.addMethodToGlobalList(&List, Methods[I]);
8104   }
8105 }
8106 
8107 void ASTReader::ReadMethodPool(Selector Sel) {
8108   // Get the selector generation and update it to the current generation.
8109   unsigned &Generation = SelectorGeneration[Sel];
8110   unsigned PriorGeneration = Generation;
8111   Generation = getGeneration();
8112   SelectorOutOfDate[Sel] = false;
8113 
8114   // Search for methods defined with this selector.
8115   ++NumMethodPoolLookups;
8116   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8117   ModuleMgr.visit(Visitor);
8118 
8119   if (Visitor.getInstanceMethods().empty() &&
8120       Visitor.getFactoryMethods().empty())
8121     return;
8122 
8123   ++NumMethodPoolHits;
8124 
8125   if (!getSema())
8126     return;
8127 
8128   Sema &S = *getSema();
8129   Sema::GlobalMethodPool::iterator Pos
8130     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8131 
8132   Pos->second.first.setBits(Visitor.getInstanceBits());
8133   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8134   Pos->second.second.setBits(Visitor.getFactoryBits());
8135   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8136 
8137   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8138   // when building a module we keep every method individually and may need to
8139   // update hasMoreThanOneDecl as we add the methods.
8140   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8141   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8142 }
8143 
8144 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8145   if (SelectorOutOfDate[Sel])
8146     ReadMethodPool(Sel);
8147 }
8148 
8149 void ASTReader::ReadKnownNamespaces(
8150                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8151   Namespaces.clear();
8152 
8153   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8154     if (NamespaceDecl *Namespace
8155                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8156       Namespaces.push_back(Namespace);
8157   }
8158 }
8159 
8160 void ASTReader::ReadUndefinedButUsed(
8161     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8162   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8163     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8164     SourceLocation Loc =
8165         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8166     Undefined.insert(std::make_pair(D, Loc));
8167   }
8168 }
8169 
8170 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8171     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8172                                                      Exprs) {
8173   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8174     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8175     uint64_t Count = DelayedDeleteExprs[Idx++];
8176     for (uint64_t C = 0; C < Count; ++C) {
8177       SourceLocation DeleteLoc =
8178           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8179       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8180       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8181     }
8182   }
8183 }
8184 
8185 void ASTReader::ReadTentativeDefinitions(
8186                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8187   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8188     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8189     if (Var)
8190       TentativeDefs.push_back(Var);
8191   }
8192   TentativeDefinitions.clear();
8193 }
8194 
8195 void ASTReader::ReadUnusedFileScopedDecls(
8196                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8197   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8198     DeclaratorDecl *D
8199       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8200     if (D)
8201       Decls.push_back(D);
8202   }
8203   UnusedFileScopedDecls.clear();
8204 }
8205 
8206 void ASTReader::ReadDelegatingConstructors(
8207                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8208   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8209     CXXConstructorDecl *D
8210       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8211     if (D)
8212       Decls.push_back(D);
8213   }
8214   DelegatingCtorDecls.clear();
8215 }
8216 
8217 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8218   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8219     TypedefNameDecl *D
8220       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8221     if (D)
8222       Decls.push_back(D);
8223   }
8224   ExtVectorDecls.clear();
8225 }
8226 
8227 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8228     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8229   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8230        ++I) {
8231     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8232         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8233     if (D)
8234       Decls.insert(D);
8235   }
8236   UnusedLocalTypedefNameCandidates.clear();
8237 }
8238 
8239 void ASTReader::ReadReferencedSelectors(
8240        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8241   if (ReferencedSelectorsData.empty())
8242     return;
8243 
8244   // If there are @selector references added them to its pool. This is for
8245   // implementation of -Wselector.
8246   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8247   unsigned I = 0;
8248   while (I < DataSize) {
8249     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8250     SourceLocation SelLoc
8251       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8252     Sels.push_back(std::make_pair(Sel, SelLoc));
8253   }
8254   ReferencedSelectorsData.clear();
8255 }
8256 
8257 void ASTReader::ReadWeakUndeclaredIdentifiers(
8258        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8259   if (WeakUndeclaredIdentifiers.empty())
8260     return;
8261 
8262   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8263     IdentifierInfo *WeakId
8264       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8265     IdentifierInfo *AliasId
8266       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8267     SourceLocation Loc
8268       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8269     bool Used = WeakUndeclaredIdentifiers[I++];
8270     WeakInfo WI(AliasId, Loc);
8271     WI.setUsed(Used);
8272     WeakIDs.push_back(std::make_pair(WeakId, WI));
8273   }
8274   WeakUndeclaredIdentifiers.clear();
8275 }
8276 
8277 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8278   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8279     ExternalVTableUse VT;
8280     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8281     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8282     VT.DefinitionRequired = VTableUses[Idx++];
8283     VTables.push_back(VT);
8284   }
8285 
8286   VTableUses.clear();
8287 }
8288 
8289 void ASTReader::ReadPendingInstantiations(
8290        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8291   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8292     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8293     SourceLocation Loc
8294       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8295 
8296     Pending.push_back(std::make_pair(D, Loc));
8297   }
8298   PendingInstantiations.clear();
8299 }
8300 
8301 void ASTReader::ReadLateParsedTemplates(
8302     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8303         &LPTMap) {
8304   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8305        /* In loop */) {
8306     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8307 
8308     auto LT = llvm::make_unique<LateParsedTemplate>();
8309     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8310 
8311     ModuleFile *F = getOwningModuleFile(LT->D);
8312     assert(F && "No module");
8313 
8314     unsigned TokN = LateParsedTemplates[Idx++];
8315     LT->Toks.reserve(TokN);
8316     for (unsigned T = 0; T < TokN; ++T)
8317       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8318 
8319     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8320   }
8321 
8322   LateParsedTemplates.clear();
8323 }
8324 
8325 void ASTReader::LoadSelector(Selector Sel) {
8326   // It would be complicated to avoid reading the methods anyway. So don't.
8327   ReadMethodPool(Sel);
8328 }
8329 
8330 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8331   assert(ID && "Non-zero identifier ID required");
8332   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8333   IdentifiersLoaded[ID - 1] = II;
8334   if (DeserializationListener)
8335     DeserializationListener->IdentifierRead(ID, II);
8336 }
8337 
8338 /// Set the globally-visible declarations associated with the given
8339 /// identifier.
8340 ///
8341 /// If the AST reader is currently in a state where the given declaration IDs
8342 /// cannot safely be resolved, they are queued until it is safe to resolve
8343 /// them.
8344 ///
8345 /// \param II an IdentifierInfo that refers to one or more globally-visible
8346 /// declarations.
8347 ///
8348 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8349 /// visible at global scope.
8350 ///
8351 /// \param Decls if non-null, this vector will be populated with the set of
8352 /// deserialized declarations. These declarations will not be pushed into
8353 /// scope.
8354 void
8355 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8356                               const SmallVectorImpl<uint32_t> &DeclIDs,
8357                                    SmallVectorImpl<Decl *> *Decls) {
8358   if (NumCurrentElementsDeserializing && !Decls) {
8359     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8360     return;
8361   }
8362 
8363   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8364     if (!SemaObj) {
8365       // Queue this declaration so that it will be added to the
8366       // translation unit scope and identifier's declaration chain
8367       // once a Sema object is known.
8368       PreloadedDeclIDs.push_back(DeclIDs[I]);
8369       continue;
8370     }
8371 
8372     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8373 
8374     // If we're simply supposed to record the declarations, do so now.
8375     if (Decls) {
8376       Decls->push_back(D);
8377       continue;
8378     }
8379 
8380     // Introduce this declaration into the translation-unit scope
8381     // and add it to the declaration chain for this identifier, so
8382     // that (unqualified) name lookup will find it.
8383     pushExternalDeclIntoScope(D, II);
8384   }
8385 }
8386 
8387 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8388   if (ID == 0)
8389     return nullptr;
8390 
8391   if (IdentifiersLoaded.empty()) {
8392     Error("no identifier table in AST file");
8393     return nullptr;
8394   }
8395 
8396   ID -= 1;
8397   if (!IdentifiersLoaded[ID]) {
8398     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8399     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8400     ModuleFile *M = I->second;
8401     unsigned Index = ID - M->BaseIdentifierID;
8402     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8403 
8404     // All of the strings in the AST file are preceded by a 16-bit length.
8405     // Extract that 16-bit length to avoid having to execute strlen().
8406     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8407     //  unsigned integers.  This is important to avoid integer overflow when
8408     //  we cast them to 'unsigned'.
8409     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8410     unsigned StrLen = (((unsigned) StrLenPtr[0])
8411                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8412     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8413     IdentifiersLoaded[ID] = &II;
8414     markIdentifierFromAST(*this,  II);
8415     if (DeserializationListener)
8416       DeserializationListener->IdentifierRead(ID + 1, &II);
8417   }
8418 
8419   return IdentifiersLoaded[ID];
8420 }
8421 
8422 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8423   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8424 }
8425 
8426 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8427   if (LocalID < NUM_PREDEF_IDENT_IDS)
8428     return LocalID;
8429 
8430   if (!M.ModuleOffsetMap.empty())
8431     ReadModuleOffsetMap(M);
8432 
8433   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8434     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8435   assert(I != M.IdentifierRemap.end()
8436          && "Invalid index into identifier index remap");
8437 
8438   return LocalID + I->second;
8439 }
8440 
8441 MacroInfo *ASTReader::getMacro(MacroID ID) {
8442   if (ID == 0)
8443     return nullptr;
8444 
8445   if (MacrosLoaded.empty()) {
8446     Error("no macro table in AST file");
8447     return nullptr;
8448   }
8449 
8450   ID -= NUM_PREDEF_MACRO_IDS;
8451   if (!MacrosLoaded[ID]) {
8452     GlobalMacroMapType::iterator I
8453       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8454     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8455     ModuleFile *M = I->second;
8456     unsigned Index = ID - M->BaseMacroID;
8457     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8458 
8459     if (DeserializationListener)
8460       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8461                                          MacrosLoaded[ID]);
8462   }
8463 
8464   return MacrosLoaded[ID];
8465 }
8466 
8467 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8468   if (LocalID < NUM_PREDEF_MACRO_IDS)
8469     return LocalID;
8470 
8471   if (!M.ModuleOffsetMap.empty())
8472     ReadModuleOffsetMap(M);
8473 
8474   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8475     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8476   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8477 
8478   return LocalID + I->second;
8479 }
8480 
8481 serialization::SubmoduleID
8482 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8483   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8484     return LocalID;
8485 
8486   if (!M.ModuleOffsetMap.empty())
8487     ReadModuleOffsetMap(M);
8488 
8489   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8490     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8491   assert(I != M.SubmoduleRemap.end()
8492          && "Invalid index into submodule index remap");
8493 
8494   return LocalID + I->second;
8495 }
8496 
8497 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8498   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8499     assert(GlobalID == 0 && "Unhandled global submodule ID");
8500     return nullptr;
8501   }
8502 
8503   if (GlobalID > SubmodulesLoaded.size()) {
8504     Error("submodule ID out of range in AST file");
8505     return nullptr;
8506   }
8507 
8508   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8509 }
8510 
8511 Module *ASTReader::getModule(unsigned ID) {
8512   return getSubmodule(ID);
8513 }
8514 
8515 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8516   ModuleFile *MF = getOwningModuleFile(D);
8517   return MF && MF->PCHHasObjectFile;
8518 }
8519 
8520 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8521   if (ID & 1) {
8522     // It's a module, look it up by submodule ID.
8523     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8524     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8525   } else {
8526     // It's a prefix (preamble, PCH, ...). Look it up by index.
8527     unsigned IndexFromEnd = ID >> 1;
8528     assert(IndexFromEnd && "got reference to unknown module file");
8529     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8530   }
8531 }
8532 
8533 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8534   if (!F)
8535     return 1;
8536 
8537   // For a file representing a module, use the submodule ID of the top-level
8538   // module as the file ID. For any other kind of file, the number of such
8539   // files loaded beforehand will be the same on reload.
8540   // FIXME: Is this true even if we have an explicit module file and a PCH?
8541   if (F->isModule())
8542     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8543 
8544   auto PCHModules = getModuleManager().pch_modules();
8545   auto I = llvm::find(PCHModules, F);
8546   assert(I != PCHModules.end() && "emitting reference to unknown file");
8547   return (I - PCHModules.end()) << 1;
8548 }
8549 
8550 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8551 ASTReader::getSourceDescriptor(unsigned ID) {
8552   if (const Module *M = getSubmodule(ID))
8553     return ExternalASTSource::ASTSourceDescriptor(*M);
8554 
8555   // If there is only a single PCH, return it instead.
8556   // Chained PCH are not supported.
8557   const auto &PCHChain = ModuleMgr.pch_modules();
8558   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8559     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8560     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8561     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8562     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8563                                           MF.Signature);
8564   }
8565   return None;
8566 }
8567 
8568 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8569   auto I = DefinitionSource.find(FD);
8570   if (I == DefinitionSource.end())
8571     return EK_ReplyHazy;
8572   return I->second ? EK_Never : EK_Always;
8573 }
8574 
8575 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8576   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8577 }
8578 
8579 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8580   if (ID == 0)
8581     return Selector();
8582 
8583   if (ID > SelectorsLoaded.size()) {
8584     Error("selector ID out of range in AST file");
8585     return Selector();
8586   }
8587 
8588   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8589     // Load this selector from the selector table.
8590     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8591     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8592     ModuleFile &M = *I->second;
8593     ASTSelectorLookupTrait Trait(*this, M);
8594     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8595     SelectorsLoaded[ID - 1] =
8596       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8597     if (DeserializationListener)
8598       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8599   }
8600 
8601   return SelectorsLoaded[ID - 1];
8602 }
8603 
8604 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8605   return DecodeSelector(ID);
8606 }
8607 
8608 uint32_t ASTReader::GetNumExternalSelectors() {
8609   // ID 0 (the null selector) is considered an external selector.
8610   return getTotalNumSelectors() + 1;
8611 }
8612 
8613 serialization::SelectorID
8614 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8615   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8616     return LocalID;
8617 
8618   if (!M.ModuleOffsetMap.empty())
8619     ReadModuleOffsetMap(M);
8620 
8621   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8622     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8623   assert(I != M.SelectorRemap.end()
8624          && "Invalid index into selector index remap");
8625 
8626   return LocalID + I->second;
8627 }
8628 
8629 DeclarationName
8630 ASTReader::ReadDeclarationName(ModuleFile &F,
8631                                const RecordData &Record, unsigned &Idx) {
8632   ASTContext &Context = getContext();
8633   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
8634   switch (Kind) {
8635   case DeclarationName::Identifier:
8636     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
8637 
8638   case DeclarationName::ObjCZeroArgSelector:
8639   case DeclarationName::ObjCOneArgSelector:
8640   case DeclarationName::ObjCMultiArgSelector:
8641     return DeclarationName(ReadSelector(F, Record, Idx));
8642 
8643   case DeclarationName::CXXConstructorName:
8644     return Context.DeclarationNames.getCXXConstructorName(
8645                           Context.getCanonicalType(readType(F, Record, Idx)));
8646 
8647   case DeclarationName::CXXDestructorName:
8648     return Context.DeclarationNames.getCXXDestructorName(
8649                           Context.getCanonicalType(readType(F, Record, Idx)));
8650 
8651   case DeclarationName::CXXDeductionGuideName:
8652     return Context.DeclarationNames.getCXXDeductionGuideName(
8653                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
8654 
8655   case DeclarationName::CXXConversionFunctionName:
8656     return Context.DeclarationNames.getCXXConversionFunctionName(
8657                           Context.getCanonicalType(readType(F, Record, Idx)));
8658 
8659   case DeclarationName::CXXOperatorName:
8660     return Context.DeclarationNames.getCXXOperatorName(
8661                                        (OverloadedOperatorKind)Record[Idx++]);
8662 
8663   case DeclarationName::CXXLiteralOperatorName:
8664     return Context.DeclarationNames.getCXXLiteralOperatorName(
8665                                        GetIdentifierInfo(F, Record, Idx));
8666 
8667   case DeclarationName::CXXUsingDirective:
8668     return DeclarationName::getUsingDirectiveName();
8669   }
8670 
8671   llvm_unreachable("Invalid NameKind!");
8672 }
8673 
8674 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
8675                                        DeclarationNameLoc &DNLoc,
8676                                        DeclarationName Name,
8677                                       const RecordData &Record, unsigned &Idx) {
8678   switch (Name.getNameKind()) {
8679   case DeclarationName::CXXConstructorName:
8680   case DeclarationName::CXXDestructorName:
8681   case DeclarationName::CXXConversionFunctionName:
8682     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
8683     break;
8684 
8685   case DeclarationName::CXXOperatorName:
8686     DNLoc.CXXOperatorName.BeginOpNameLoc
8687         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8688     DNLoc.CXXOperatorName.EndOpNameLoc
8689         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8690     break;
8691 
8692   case DeclarationName::CXXLiteralOperatorName:
8693     DNLoc.CXXLiteralOperatorName.OpNameLoc
8694         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8695     break;
8696 
8697   case DeclarationName::Identifier:
8698   case DeclarationName::ObjCZeroArgSelector:
8699   case DeclarationName::ObjCOneArgSelector:
8700   case DeclarationName::ObjCMultiArgSelector:
8701   case DeclarationName::CXXUsingDirective:
8702   case DeclarationName::CXXDeductionGuideName:
8703     break;
8704   }
8705 }
8706 
8707 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
8708                                         DeclarationNameInfo &NameInfo,
8709                                       const RecordData &Record, unsigned &Idx) {
8710   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
8711   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
8712   DeclarationNameLoc DNLoc;
8713   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
8714   NameInfo.setInfo(DNLoc);
8715 }
8716 
8717 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
8718                                   const RecordData &Record, unsigned &Idx) {
8719   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
8720   unsigned NumTPLists = Record[Idx++];
8721   Info.NumTemplParamLists = NumTPLists;
8722   if (NumTPLists) {
8723     Info.TemplParamLists =
8724         new (getContext()) TemplateParameterList *[NumTPLists];
8725     for (unsigned i = 0; i != NumTPLists; ++i)
8726       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
8727   }
8728 }
8729 
8730 TemplateName
8731 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
8732                             unsigned &Idx) {
8733   ASTContext &Context = getContext();
8734   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
8735   switch (Kind) {
8736   case TemplateName::Template:
8737       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
8738 
8739   case TemplateName::OverloadedTemplate: {
8740     unsigned size = Record[Idx++];
8741     UnresolvedSet<8> Decls;
8742     while (size--)
8743       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
8744 
8745     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
8746   }
8747 
8748   case TemplateName::AssumedTemplate: {
8749     DeclarationName Name = ReadDeclarationName(F, Record, Idx);
8750     return Context.getAssumedTemplateName(Name);
8751   }
8752 
8753   case TemplateName::QualifiedTemplate: {
8754     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8755     bool hasTemplKeyword = Record[Idx++];
8756     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
8757     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
8758   }
8759 
8760   case TemplateName::DependentTemplate: {
8761     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8762     if (Record[Idx++])  // isIdentifier
8763       return Context.getDependentTemplateName(NNS,
8764                                                GetIdentifierInfo(F, Record,
8765                                                                  Idx));
8766     return Context.getDependentTemplateName(NNS,
8767                                          (OverloadedOperatorKind)Record[Idx++]);
8768   }
8769 
8770   case TemplateName::SubstTemplateTemplateParm: {
8771     TemplateTemplateParmDecl *param
8772       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8773     if (!param) return TemplateName();
8774     TemplateName replacement = ReadTemplateName(F, Record, Idx);
8775     return Context.getSubstTemplateTemplateParm(param, replacement);
8776   }
8777 
8778   case TemplateName::SubstTemplateTemplateParmPack: {
8779     TemplateTemplateParmDecl *Param
8780       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8781     if (!Param)
8782       return TemplateName();
8783 
8784     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
8785     if (ArgPack.getKind() != TemplateArgument::Pack)
8786       return TemplateName();
8787 
8788     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
8789   }
8790   }
8791 
8792   llvm_unreachable("Unhandled template name kind!");
8793 }
8794 
8795 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
8796                                                  const RecordData &Record,
8797                                                  unsigned &Idx,
8798                                                  bool Canonicalize) {
8799   ASTContext &Context = getContext();
8800   if (Canonicalize) {
8801     // The caller wants a canonical template argument. Sometimes the AST only
8802     // wants template arguments in canonical form (particularly as the template
8803     // argument lists of template specializations) so ensure we preserve that
8804     // canonical form across serialization.
8805     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
8806     return Context.getCanonicalTemplateArgument(Arg);
8807   }
8808 
8809   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
8810   switch (Kind) {
8811   case TemplateArgument::Null:
8812     return TemplateArgument();
8813   case TemplateArgument::Type:
8814     return TemplateArgument(readType(F, Record, Idx));
8815   case TemplateArgument::Declaration: {
8816     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
8817     return TemplateArgument(D, readType(F, Record, Idx));
8818   }
8819   case TemplateArgument::NullPtr:
8820     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
8821   case TemplateArgument::Integral: {
8822     llvm::APSInt Value = ReadAPSInt(Record, Idx);
8823     QualType T = readType(F, Record, Idx);
8824     return TemplateArgument(Context, Value, T);
8825   }
8826   case TemplateArgument::Template:
8827     return TemplateArgument(ReadTemplateName(F, Record, Idx));
8828   case TemplateArgument::TemplateExpansion: {
8829     TemplateName Name = ReadTemplateName(F, Record, Idx);
8830     Optional<unsigned> NumTemplateExpansions;
8831     if (unsigned NumExpansions = Record[Idx++])
8832       NumTemplateExpansions = NumExpansions - 1;
8833     return TemplateArgument(Name, NumTemplateExpansions);
8834   }
8835   case TemplateArgument::Expression:
8836     return TemplateArgument(ReadExpr(F));
8837   case TemplateArgument::Pack: {
8838     unsigned NumArgs = Record[Idx++];
8839     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
8840     for (unsigned I = 0; I != NumArgs; ++I)
8841       Args[I] = ReadTemplateArgument(F, Record, Idx);
8842     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
8843   }
8844   }
8845 
8846   llvm_unreachable("Unhandled template argument kind!");
8847 }
8848 
8849 TemplateParameterList *
8850 ASTReader::ReadTemplateParameterList(ModuleFile &F,
8851                                      const RecordData &Record, unsigned &Idx) {
8852   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
8853   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
8854   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
8855 
8856   unsigned NumParams = Record[Idx++];
8857   SmallVector<NamedDecl *, 16> Params;
8858   Params.reserve(NumParams);
8859   while (NumParams--)
8860     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
8861 
8862   // TODO: Concepts
8863   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8864       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
8865   return TemplateParams;
8866 }
8867 
8868 void
8869 ASTReader::
8870 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
8871                          ModuleFile &F, const RecordData &Record,
8872                          unsigned &Idx, bool Canonicalize) {
8873   unsigned NumTemplateArgs = Record[Idx++];
8874   TemplArgs.reserve(NumTemplateArgs);
8875   while (NumTemplateArgs--)
8876     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
8877 }
8878 
8879 /// Read a UnresolvedSet structure.
8880 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
8881                                   const RecordData &Record, unsigned &Idx) {
8882   unsigned NumDecls = Record[Idx++];
8883   Set.reserve(getContext(), NumDecls);
8884   while (NumDecls--) {
8885     DeclID ID = ReadDeclID(F, Record, Idx);
8886     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
8887     Set.addLazyDecl(getContext(), ID, AS);
8888   }
8889 }
8890 
8891 CXXBaseSpecifier
8892 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
8893                                 const RecordData &Record, unsigned &Idx) {
8894   bool isVirtual = static_cast<bool>(Record[Idx++]);
8895   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
8896   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
8897   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
8898   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
8899   SourceRange Range = ReadSourceRange(F, Record, Idx);
8900   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
8901   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8902                           EllipsisLoc);
8903   Result.setInheritConstructors(inheritConstructors);
8904   return Result;
8905 }
8906 
8907 CXXCtorInitializer **
8908 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
8909                                    unsigned &Idx) {
8910   ASTContext &Context = getContext();
8911   unsigned NumInitializers = Record[Idx++];
8912   assert(NumInitializers && "wrote ctor initializers but have no inits");
8913   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8914   for (unsigned i = 0; i != NumInitializers; ++i) {
8915     TypeSourceInfo *TInfo = nullptr;
8916     bool IsBaseVirtual = false;
8917     FieldDecl *Member = nullptr;
8918     IndirectFieldDecl *IndirectMember = nullptr;
8919 
8920     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
8921     switch (Type) {
8922     case CTOR_INITIALIZER_BASE:
8923       TInfo = GetTypeSourceInfo(F, Record, Idx);
8924       IsBaseVirtual = Record[Idx++];
8925       break;
8926 
8927     case CTOR_INITIALIZER_DELEGATING:
8928       TInfo = GetTypeSourceInfo(F, Record, Idx);
8929       break;
8930 
8931      case CTOR_INITIALIZER_MEMBER:
8932       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
8933       break;
8934 
8935      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8936       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
8937       break;
8938     }
8939 
8940     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
8941     Expr *Init = ReadExpr(F);
8942     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
8943     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
8944 
8945     CXXCtorInitializer *BOMInit;
8946     if (Type == CTOR_INITIALIZER_BASE)
8947       BOMInit = new (Context)
8948           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8949                              RParenLoc, MemberOrEllipsisLoc);
8950     else if (Type == CTOR_INITIALIZER_DELEGATING)
8951       BOMInit = new (Context)
8952           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8953     else if (Member)
8954       BOMInit = new (Context)
8955           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8956                              Init, RParenLoc);
8957     else
8958       BOMInit = new (Context)
8959           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8960                              LParenLoc, Init, RParenLoc);
8961 
8962     if (/*IsWritten*/Record[Idx++]) {
8963       unsigned SourceOrder = Record[Idx++];
8964       BOMInit->setSourceOrder(SourceOrder);
8965     }
8966 
8967     CtorInitializers[i] = BOMInit;
8968   }
8969 
8970   return CtorInitializers;
8971 }
8972 
8973 NestedNameSpecifier *
8974 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8975                                    const RecordData &Record, unsigned &Idx) {
8976   ASTContext &Context = getContext();
8977   unsigned N = Record[Idx++];
8978   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
8979   for (unsigned I = 0; I != N; ++I) {
8980     NestedNameSpecifier::SpecifierKind Kind
8981       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8982     switch (Kind) {
8983     case NestedNameSpecifier::Identifier: {
8984       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8985       NNS = NestedNameSpecifier::Create(Context, Prev, II);
8986       break;
8987     }
8988 
8989     case NestedNameSpecifier::Namespace: {
8990       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8991       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8992       break;
8993     }
8994 
8995     case NestedNameSpecifier::NamespaceAlias: {
8996       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8997       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8998       break;
8999     }
9000 
9001     case NestedNameSpecifier::TypeSpec:
9002     case NestedNameSpecifier::TypeSpecWithTemplate: {
9003       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9004       if (!T)
9005         return nullptr;
9006 
9007       bool Template = Record[Idx++];
9008       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9009       break;
9010     }
9011 
9012     case NestedNameSpecifier::Global:
9013       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9014       // No associated value, and there can't be a prefix.
9015       break;
9016 
9017     case NestedNameSpecifier::Super: {
9018       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9019       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9020       break;
9021     }
9022     }
9023     Prev = NNS;
9024   }
9025   return NNS;
9026 }
9027 
9028 NestedNameSpecifierLoc
9029 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9030                                       unsigned &Idx) {
9031   ASTContext &Context = getContext();
9032   unsigned N = Record[Idx++];
9033   NestedNameSpecifierLocBuilder Builder;
9034   for (unsigned I = 0; I != N; ++I) {
9035     NestedNameSpecifier::SpecifierKind Kind
9036       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9037     switch (Kind) {
9038     case NestedNameSpecifier::Identifier: {
9039       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9040       SourceRange Range = ReadSourceRange(F, Record, Idx);
9041       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9042       break;
9043     }
9044 
9045     case NestedNameSpecifier::Namespace: {
9046       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9047       SourceRange Range = ReadSourceRange(F, Record, Idx);
9048       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9049       break;
9050     }
9051 
9052     case NestedNameSpecifier::NamespaceAlias: {
9053       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9054       SourceRange Range = ReadSourceRange(F, Record, Idx);
9055       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9056       break;
9057     }
9058 
9059     case NestedNameSpecifier::TypeSpec:
9060     case NestedNameSpecifier::TypeSpecWithTemplate: {
9061       bool Template = Record[Idx++];
9062       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9063       if (!T)
9064         return NestedNameSpecifierLoc();
9065       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9066 
9067       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9068       Builder.Extend(Context,
9069                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9070                      T->getTypeLoc(), ColonColonLoc);
9071       break;
9072     }
9073 
9074     case NestedNameSpecifier::Global: {
9075       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9076       Builder.MakeGlobal(Context, ColonColonLoc);
9077       break;
9078     }
9079 
9080     case NestedNameSpecifier::Super: {
9081       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9082       SourceRange Range = ReadSourceRange(F, Record, Idx);
9083       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9084       break;
9085     }
9086     }
9087   }
9088 
9089   return Builder.getWithLocInContext(Context);
9090 }
9091 
9092 SourceRange
9093 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9094                            unsigned &Idx) {
9095   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9096   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9097   return SourceRange(beg, end);
9098 }
9099 
9100 /// Read an integral value
9101 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9102   unsigned BitWidth = Record[Idx++];
9103   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9104   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9105   Idx += NumWords;
9106   return Result;
9107 }
9108 
9109 /// Read a signed integral value
9110 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9111   bool isUnsigned = Record[Idx++];
9112   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9113 }
9114 
9115 /// Read a floating-point value
9116 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9117                                      const llvm::fltSemantics &Sem,
9118                                      unsigned &Idx) {
9119   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9120 }
9121 
9122 // Read a string
9123 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9124   unsigned Len = Record[Idx++];
9125   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9126   Idx += Len;
9127   return Result;
9128 }
9129 
9130 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9131                                 unsigned &Idx) {
9132   std::string Filename = ReadString(Record, Idx);
9133   ResolveImportedPath(F, Filename);
9134   return Filename;
9135 }
9136 
9137 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9138                                 const RecordData &Record, unsigned &Idx) {
9139   std::string Filename = ReadString(Record, Idx);
9140   if (!BaseDirectory.empty())
9141     ResolveImportedPath(Filename, BaseDirectory);
9142   return Filename;
9143 }
9144 
9145 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9146                                          unsigned &Idx) {
9147   unsigned Major = Record[Idx++];
9148   unsigned Minor = Record[Idx++];
9149   unsigned Subminor = Record[Idx++];
9150   if (Minor == 0)
9151     return VersionTuple(Major);
9152   if (Subminor == 0)
9153     return VersionTuple(Major, Minor - 1);
9154   return VersionTuple(Major, Minor - 1, Subminor - 1);
9155 }
9156 
9157 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9158                                           const RecordData &Record,
9159                                           unsigned &Idx) {
9160   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9161   return CXXTemporary::Create(getContext(), Decl);
9162 }
9163 
9164 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9165   return Diag(CurrentImportLoc, DiagID);
9166 }
9167 
9168 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9169   return Diags.Report(Loc, DiagID);
9170 }
9171 
9172 /// Retrieve the identifier table associated with the
9173 /// preprocessor.
9174 IdentifierTable &ASTReader::getIdentifierTable() {
9175   return PP.getIdentifierTable();
9176 }
9177 
9178 /// Record that the given ID maps to the given switch-case
9179 /// statement.
9180 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9181   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9182          "Already have a SwitchCase with this ID");
9183   (*CurrSwitchCaseStmts)[ID] = SC;
9184 }
9185 
9186 /// Retrieve the switch-case statement with the given ID.
9187 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9188   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9189   return (*CurrSwitchCaseStmts)[ID];
9190 }
9191 
9192 void ASTReader::ClearSwitchCaseIDs() {
9193   CurrSwitchCaseStmts->clear();
9194 }
9195 
9196 void ASTReader::ReadComments() {
9197   ASTContext &Context = getContext();
9198   std::vector<RawComment *> Comments;
9199   for (SmallVectorImpl<std::pair<BitstreamCursor,
9200                                  serialization::ModuleFile *>>::iterator
9201        I = CommentsCursors.begin(),
9202        E = CommentsCursors.end();
9203        I != E; ++I) {
9204     Comments.clear();
9205     BitstreamCursor &Cursor = I->first;
9206     serialization::ModuleFile &F = *I->second;
9207     SavedStreamPosition SavedPosition(Cursor);
9208 
9209     RecordData Record;
9210     while (true) {
9211       llvm::BitstreamEntry Entry =
9212         Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
9213 
9214       switch (Entry.Kind) {
9215       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9216       case llvm::BitstreamEntry::Error:
9217         Error("malformed block record in AST file");
9218         return;
9219       case llvm::BitstreamEntry::EndBlock:
9220         goto NextCursor;
9221       case llvm::BitstreamEntry::Record:
9222         // The interesting case.
9223         break;
9224       }
9225 
9226       // Read a record.
9227       Record.clear();
9228       switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
9229       case COMMENTS_RAW_COMMENT: {
9230         unsigned Idx = 0;
9231         SourceRange SR = ReadSourceRange(F, Record, Idx);
9232         RawComment::CommentKind Kind =
9233             (RawComment::CommentKind) Record[Idx++];
9234         bool IsTrailingComment = Record[Idx++];
9235         bool IsAlmostTrailingComment = Record[Idx++];
9236         Comments.push_back(new (Context) RawComment(
9237             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9238         break;
9239       }
9240       }
9241     }
9242   NextCursor:
9243     // De-serialized SourceLocations get negative FileIDs for other modules,
9244     // potentially invalidating the original order. Sort it again.
9245     llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr));
9246     Context.Comments.addDeserializedComments(Comments);
9247   }
9248 }
9249 
9250 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9251                                 bool IncludeSystem, bool Complain,
9252                     llvm::function_ref<void(const serialization::InputFile &IF,
9253                                             bool isSystem)> Visitor) {
9254   unsigned NumUserInputs = MF.NumUserInputFiles;
9255   unsigned NumInputs = MF.InputFilesLoaded.size();
9256   assert(NumUserInputs <= NumInputs);
9257   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9258   for (unsigned I = 0; I < N; ++I) {
9259     bool IsSystem = I >= NumUserInputs;
9260     InputFile IF = getInputFile(MF, I+1, Complain);
9261     Visitor(IF, IsSystem);
9262   }
9263 }
9264 
9265 void ASTReader::visitTopLevelModuleMaps(
9266     serialization::ModuleFile &MF,
9267     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9268   unsigned NumInputs = MF.InputFilesLoaded.size();
9269   for (unsigned I = 0; I < NumInputs; ++I) {
9270     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9271     if (IFI.TopLevelModuleMap)
9272       // FIXME: This unnecessarily re-reads the InputFileInfo.
9273       if (auto *FE = getInputFile(MF, I + 1).getFile())
9274         Visitor(FE);
9275   }
9276 }
9277 
9278 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9279   // If we know the owning module, use it.
9280   if (Module *M = D->getImportedOwningModule())
9281     return M->getFullModuleName();
9282 
9283   // Otherwise, use the name of the top-level module the decl is within.
9284   if (ModuleFile *M = getOwningModuleFile(D))
9285     return M->ModuleName;
9286 
9287   // Not from a module.
9288   return {};
9289 }
9290 
9291 void ASTReader::finishPendingActions() {
9292   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9293          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9294          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9295          !PendingUpdateRecords.empty()) {
9296     // If any identifiers with corresponding top-level declarations have
9297     // been loaded, load those declarations now.
9298     using TopLevelDeclsMap =
9299         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9300     TopLevelDeclsMap TopLevelDecls;
9301 
9302     while (!PendingIdentifierInfos.empty()) {
9303       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9304       SmallVector<uint32_t, 4> DeclIDs =
9305           std::move(PendingIdentifierInfos.back().second);
9306       PendingIdentifierInfos.pop_back();
9307 
9308       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9309     }
9310 
9311     // Load each function type that we deferred loading because it was a
9312     // deduced type that might refer to a local type declared within itself.
9313     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9314       auto *FD = PendingFunctionTypes[I].first;
9315       FD->setType(GetType(PendingFunctionTypes[I].second));
9316 
9317       // If we gave a function a deduced return type, remember that we need to
9318       // propagate that along the redeclaration chain.
9319       auto *DT = FD->getReturnType()->getContainedDeducedType();
9320       if (DT && DT->isDeduced())
9321         PendingDeducedTypeUpdates.insert(
9322             {FD->getCanonicalDecl(), FD->getReturnType()});
9323     }
9324     PendingFunctionTypes.clear();
9325 
9326     // For each decl chain that we wanted to complete while deserializing, mark
9327     // it as "still needs to be completed".
9328     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9329       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9330     }
9331     PendingIncompleteDeclChains.clear();
9332 
9333     // Load pending declaration chains.
9334     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9335       loadPendingDeclChain(PendingDeclChains[I].first,
9336                            PendingDeclChains[I].second);
9337     PendingDeclChains.clear();
9338 
9339     // Make the most recent of the top-level declarations visible.
9340     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9341            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9342       IdentifierInfo *II = TLD->first;
9343       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9344         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9345       }
9346     }
9347 
9348     // Load any pending macro definitions.
9349     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9350       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9351       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9352       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9353       // Initialize the macro history from chained-PCHs ahead of module imports.
9354       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9355            ++IDIdx) {
9356         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9357         if (!Info.M->isModule())
9358           resolvePendingMacro(II, Info);
9359       }
9360       // Handle module imports.
9361       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9362            ++IDIdx) {
9363         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9364         if (Info.M->isModule())
9365           resolvePendingMacro(II, Info);
9366       }
9367     }
9368     PendingMacroIDs.clear();
9369 
9370     // Wire up the DeclContexts for Decls that we delayed setting until
9371     // recursive loading is completed.
9372     while (!PendingDeclContextInfos.empty()) {
9373       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9374       PendingDeclContextInfos.pop_front();
9375       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9376       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9377       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9378     }
9379 
9380     // Perform any pending declaration updates.
9381     while (!PendingUpdateRecords.empty()) {
9382       auto Update = PendingUpdateRecords.pop_back_val();
9383       ReadingKindTracker ReadingKind(Read_Decl, *this);
9384       loadDeclUpdateRecords(Update);
9385     }
9386   }
9387 
9388   // At this point, all update records for loaded decls are in place, so any
9389   // fake class definitions should have become real.
9390   assert(PendingFakeDefinitionData.empty() &&
9391          "faked up a class definition but never saw the real one");
9392 
9393   // If we deserialized any C++ or Objective-C class definitions, any
9394   // Objective-C protocol definitions, or any redeclarable templates, make sure
9395   // that all redeclarations point to the definitions. Note that this can only
9396   // happen now, after the redeclaration chains have been fully wired.
9397   for (Decl *D : PendingDefinitions) {
9398     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9399       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9400         // Make sure that the TagType points at the definition.
9401         const_cast<TagType*>(TagT)->decl = TD;
9402       }
9403 
9404       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9405         for (auto *R = getMostRecentExistingDecl(RD); R;
9406              R = R->getPreviousDecl()) {
9407           assert((R == D) ==
9408                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9409                  "declaration thinks it's the definition but it isn't");
9410           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9411         }
9412       }
9413 
9414       continue;
9415     }
9416 
9417     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9418       // Make sure that the ObjCInterfaceType points at the definition.
9419       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9420         ->Decl = ID;
9421 
9422       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9423         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9424 
9425       continue;
9426     }
9427 
9428     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9429       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9430         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9431 
9432       continue;
9433     }
9434 
9435     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9436     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9437       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9438   }
9439   PendingDefinitions.clear();
9440 
9441   // Load the bodies of any functions or methods we've encountered. We do
9442   // this now (delayed) so that we can be sure that the declaration chains
9443   // have been fully wired up (hasBody relies on this).
9444   // FIXME: We shouldn't require complete redeclaration chains here.
9445   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9446                                PBEnd = PendingBodies.end();
9447        PB != PBEnd; ++PB) {
9448     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9449       // For a function defined inline within a class template, force the
9450       // canonical definition to be the one inside the canonical definition of
9451       // the template. This ensures that we instantiate from a correct view
9452       // of the template.
9453       //
9454       // Sadly we can't do this more generally: we can't be sure that all
9455       // copies of an arbitrary class definition will have the same members
9456       // defined (eg, some member functions may not be instantiated, and some
9457       // special members may or may not have been implicitly defined).
9458       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9459         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9460           continue;
9461 
9462       // FIXME: Check for =delete/=default?
9463       // FIXME: Complain about ODR violations here?
9464       const FunctionDecl *Defn = nullptr;
9465       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9466         FD->setLazyBody(PB->second);
9467       } else {
9468         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9469         mergeDefinitionVisibility(NonConstDefn, FD);
9470 
9471         if (!FD->isLateTemplateParsed() &&
9472             !NonConstDefn->isLateTemplateParsed() &&
9473             FD->getODRHash() != NonConstDefn->getODRHash()) {
9474           if (!isa<CXXMethodDecl>(FD)) {
9475             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9476           } else if (FD->getLexicalParent()->isFileContext() &&
9477                      NonConstDefn->getLexicalParent()->isFileContext()) {
9478             // Only diagnose out-of-line method definitions.  If they are
9479             // in class definitions, then an error will be generated when
9480             // processing the class bodies.
9481             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9482           }
9483         }
9484       }
9485       continue;
9486     }
9487 
9488     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9489     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9490       MD->setLazyBody(PB->second);
9491   }
9492   PendingBodies.clear();
9493 
9494   // Do some cleanup.
9495   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9496     getContext().deduplicateMergedDefinitonsFor(ND);
9497   PendingMergedDefinitionsToDeduplicate.clear();
9498 }
9499 
9500 void ASTReader::diagnoseOdrViolations() {
9501   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9502       PendingFunctionOdrMergeFailures.empty() &&
9503       PendingEnumOdrMergeFailures.empty())
9504     return;
9505 
9506   // Trigger the import of the full definition of each class that had any
9507   // odr-merging problems, so we can produce better diagnostics for them.
9508   // These updates may in turn find and diagnose some ODR failures, so take
9509   // ownership of the set first.
9510   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9511   PendingOdrMergeFailures.clear();
9512   for (auto &Merge : OdrMergeFailures) {
9513     Merge.first->buildLookup();
9514     Merge.first->decls_begin();
9515     Merge.first->bases_begin();
9516     Merge.first->vbases_begin();
9517     for (auto &RecordPair : Merge.second) {
9518       auto *RD = RecordPair.first;
9519       RD->decls_begin();
9520       RD->bases_begin();
9521       RD->vbases_begin();
9522     }
9523   }
9524 
9525   // Trigger the import of functions.
9526   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9527   PendingFunctionOdrMergeFailures.clear();
9528   for (auto &Merge : FunctionOdrMergeFailures) {
9529     Merge.first->buildLookup();
9530     Merge.first->decls_begin();
9531     Merge.first->getBody();
9532     for (auto &FD : Merge.second) {
9533       FD->buildLookup();
9534       FD->decls_begin();
9535       FD->getBody();
9536     }
9537   }
9538 
9539   // Trigger the import of enums.
9540   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9541   PendingEnumOdrMergeFailures.clear();
9542   for (auto &Merge : EnumOdrMergeFailures) {
9543     Merge.first->decls_begin();
9544     for (auto &Enum : Merge.second) {
9545       Enum->decls_begin();
9546     }
9547   }
9548 
9549   // For each declaration from a merged context, check that the canonical
9550   // definition of that context also contains a declaration of the same
9551   // entity.
9552   //
9553   // Caution: this loop does things that might invalidate iterators into
9554   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9555   while (!PendingOdrMergeChecks.empty()) {
9556     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9557 
9558     // FIXME: Skip over implicit declarations for now. This matters for things
9559     // like implicitly-declared special member functions. This isn't entirely
9560     // correct; we can end up with multiple unmerged declarations of the same
9561     // implicit entity.
9562     if (D->isImplicit())
9563       continue;
9564 
9565     DeclContext *CanonDef = D->getDeclContext();
9566 
9567     bool Found = false;
9568     const Decl *DCanon = D->getCanonicalDecl();
9569 
9570     for (auto RI : D->redecls()) {
9571       if (RI->getLexicalDeclContext() == CanonDef) {
9572         Found = true;
9573         break;
9574       }
9575     }
9576     if (Found)
9577       continue;
9578 
9579     // Quick check failed, time to do the slow thing. Note, we can't just
9580     // look up the name of D in CanonDef here, because the member that is
9581     // in CanonDef might not be found by name lookup (it might have been
9582     // replaced by a more recent declaration in the lookup table), and we
9583     // can't necessarily find it in the redeclaration chain because it might
9584     // be merely mergeable, not redeclarable.
9585     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9586     for (auto *CanonMember : CanonDef->decls()) {
9587       if (CanonMember->getCanonicalDecl() == DCanon) {
9588         // This can happen if the declaration is merely mergeable and not
9589         // actually redeclarable (we looked for redeclarations earlier).
9590         //
9591         // FIXME: We should be able to detect this more efficiently, without
9592         // pulling in all of the members of CanonDef.
9593         Found = true;
9594         break;
9595       }
9596       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9597         if (ND->getDeclName() == D->getDeclName())
9598           Candidates.push_back(ND);
9599     }
9600 
9601     if (!Found) {
9602       // The AST doesn't like TagDecls becoming invalid after they've been
9603       // completed. We only really need to mark FieldDecls as invalid here.
9604       if (!isa<TagDecl>(D))
9605         D->setInvalidDecl();
9606 
9607       // Ensure we don't accidentally recursively enter deserialization while
9608       // we're producing our diagnostic.
9609       Deserializing RecursionGuard(this);
9610 
9611       std::string CanonDefModule =
9612           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9613       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9614         << D << getOwningModuleNameForDiagnostic(D)
9615         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9616 
9617       if (Candidates.empty())
9618         Diag(cast<Decl>(CanonDef)->getLocation(),
9619              diag::note_module_odr_violation_no_possible_decls) << D;
9620       else {
9621         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9622           Diag(Candidates[I]->getLocation(),
9623                diag::note_module_odr_violation_possible_decl)
9624             << Candidates[I];
9625       }
9626 
9627       DiagnosedOdrMergeFailures.insert(CanonDef);
9628     }
9629   }
9630 
9631   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9632       EnumOdrMergeFailures.empty())
9633     return;
9634 
9635   // Ensure we don't accidentally recursively enter deserialization while
9636   // we're producing our diagnostics.
9637   Deserializing RecursionGuard(this);
9638 
9639   // Common code for hashing helpers.
9640   ODRHash Hash;
9641   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9642     Hash.clear();
9643     Hash.AddQualType(Ty);
9644     return Hash.CalculateHash();
9645   };
9646 
9647   auto ComputeODRHash = [&Hash](const Stmt *S) {
9648     assert(S);
9649     Hash.clear();
9650     Hash.AddStmt(S);
9651     return Hash.CalculateHash();
9652   };
9653 
9654   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9655     assert(D);
9656     Hash.clear();
9657     Hash.AddSubDecl(D);
9658     return Hash.CalculateHash();
9659   };
9660 
9661   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9662     Hash.clear();
9663     Hash.AddTemplateArgument(TA);
9664     return Hash.CalculateHash();
9665   };
9666 
9667   auto ComputeTemplateParameterListODRHash =
9668       [&Hash](const TemplateParameterList *TPL) {
9669         assert(TPL);
9670         Hash.clear();
9671         Hash.AddTemplateParameterList(TPL);
9672         return Hash.CalculateHash();
9673       };
9674 
9675   // Issue any pending ODR-failure diagnostics.
9676   for (auto &Merge : OdrMergeFailures) {
9677     // If we've already pointed out a specific problem with this class, don't
9678     // bother issuing a general "something's different" diagnostic.
9679     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9680       continue;
9681 
9682     bool Diagnosed = false;
9683     CXXRecordDecl *FirstRecord = Merge.first;
9684     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9685     for (auto &RecordPair : Merge.second) {
9686       CXXRecordDecl *SecondRecord = RecordPair.first;
9687       // Multiple different declarations got merged together; tell the user
9688       // where they came from.
9689       if (FirstRecord == SecondRecord)
9690         continue;
9691 
9692       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9693 
9694       auto *FirstDD = FirstRecord->DefinitionData;
9695       auto *SecondDD = RecordPair.second;
9696 
9697       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9698 
9699       // Diagnostics from DefinitionData are emitted here.
9700       if (FirstDD != SecondDD) {
9701         enum ODRDefinitionDataDifference {
9702           NumBases,
9703           NumVBases,
9704           BaseType,
9705           BaseVirtual,
9706           BaseAccess,
9707         };
9708         auto ODRDiagError = [FirstRecord, &FirstModule,
9709                              this](SourceLocation Loc, SourceRange Range,
9710                                    ODRDefinitionDataDifference DiffType) {
9711           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9712                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9713                  << DiffType;
9714         };
9715         auto ODRDiagNote = [&SecondModule,
9716                             this](SourceLocation Loc, SourceRange Range,
9717                                   ODRDefinitionDataDifference DiffType) {
9718           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9719                  << SecondModule << Range << DiffType;
9720         };
9721 
9722         unsigned FirstNumBases = FirstDD->NumBases;
9723         unsigned FirstNumVBases = FirstDD->NumVBases;
9724         unsigned SecondNumBases = SecondDD->NumBases;
9725         unsigned SecondNumVBases = SecondDD->NumVBases;
9726 
9727         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9728           unsigned NumBases = DD->NumBases;
9729           if (NumBases == 0) return SourceRange();
9730           auto bases = DD->bases();
9731           return SourceRange(bases[0].getBeginLoc(),
9732                              bases[NumBases - 1].getEndLoc());
9733         };
9734 
9735         if (FirstNumBases != SecondNumBases) {
9736           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9737                        NumBases)
9738               << FirstNumBases;
9739           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9740                       NumBases)
9741               << SecondNumBases;
9742           Diagnosed = true;
9743           break;
9744         }
9745 
9746         if (FirstNumVBases != SecondNumVBases) {
9747           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9748                        NumVBases)
9749               << FirstNumVBases;
9750           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9751                       NumVBases)
9752               << SecondNumVBases;
9753           Diagnosed = true;
9754           break;
9755         }
9756 
9757         auto FirstBases = FirstDD->bases();
9758         auto SecondBases = SecondDD->bases();
9759         unsigned i = 0;
9760         for (i = 0; i < FirstNumBases; ++i) {
9761           auto FirstBase = FirstBases[i];
9762           auto SecondBase = SecondBases[i];
9763           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9764               ComputeQualTypeODRHash(SecondBase.getType())) {
9765             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9766                          BaseType)
9767                 << (i + 1) << FirstBase.getType();
9768             ODRDiagNote(SecondRecord->getLocation(),
9769                         SecondBase.getSourceRange(), BaseType)
9770                 << (i + 1) << SecondBase.getType();
9771             break;
9772           }
9773 
9774           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9775             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9776                          BaseVirtual)
9777                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9778             ODRDiagNote(SecondRecord->getLocation(),
9779                         SecondBase.getSourceRange(), BaseVirtual)
9780                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9781             break;
9782           }
9783 
9784           if (FirstBase.getAccessSpecifierAsWritten() !=
9785               SecondBase.getAccessSpecifierAsWritten()) {
9786             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9787                          BaseAccess)
9788                 << (i + 1) << FirstBase.getType()
9789                 << (int)FirstBase.getAccessSpecifierAsWritten();
9790             ODRDiagNote(SecondRecord->getLocation(),
9791                         SecondBase.getSourceRange(), BaseAccess)
9792                 << (i + 1) << SecondBase.getType()
9793                 << (int)SecondBase.getAccessSpecifierAsWritten();
9794             break;
9795           }
9796         }
9797 
9798         if (i != FirstNumBases) {
9799           Diagnosed = true;
9800           break;
9801         }
9802       }
9803 
9804       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9805 
9806       const ClassTemplateDecl *FirstTemplate =
9807           FirstRecord->getDescribedClassTemplate();
9808       const ClassTemplateDecl *SecondTemplate =
9809           SecondRecord->getDescribedClassTemplate();
9810 
9811       assert(!FirstTemplate == !SecondTemplate &&
9812              "Both pointers should be null or non-null");
9813 
9814       enum ODRTemplateDifference {
9815         ParamEmptyName,
9816         ParamName,
9817         ParamSingleDefaultArgument,
9818         ParamDifferentDefaultArgument,
9819       };
9820 
9821       if (FirstTemplate && SecondTemplate) {
9822         DeclHashes FirstTemplateHashes;
9823         DeclHashes SecondTemplateHashes;
9824 
9825         auto PopulateTemplateParameterHashs =
9826             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9827                                      const ClassTemplateDecl *TD) {
9828               for (auto *D : TD->getTemplateParameters()->asArray()) {
9829                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9830               }
9831             };
9832 
9833         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
9834         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
9835 
9836         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
9837                "Number of template parameters should be equal.");
9838 
9839         auto FirstIt = FirstTemplateHashes.begin();
9840         auto FirstEnd = FirstTemplateHashes.end();
9841         auto SecondIt = SecondTemplateHashes.begin();
9842         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
9843           if (FirstIt->second == SecondIt->second)
9844             continue;
9845 
9846           auto ODRDiagError = [FirstRecord, &FirstModule,
9847                                this](SourceLocation Loc, SourceRange Range,
9848                                      ODRTemplateDifference DiffType) {
9849             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
9850                    << FirstRecord << FirstModule.empty() << FirstModule << Range
9851                    << DiffType;
9852           };
9853           auto ODRDiagNote = [&SecondModule,
9854                               this](SourceLocation Loc, SourceRange Range,
9855                                     ODRTemplateDifference DiffType) {
9856             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
9857                    << SecondModule << Range << DiffType;
9858           };
9859 
9860           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
9861           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
9862 
9863           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
9864                  "Parameter Decl's should be the same kind.");
9865 
9866           DeclarationName FirstName = FirstDecl->getDeclName();
9867           DeclarationName SecondName = SecondDecl->getDeclName();
9868 
9869           if (FirstName != SecondName) {
9870             const bool FirstNameEmpty =
9871                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
9872             const bool SecondNameEmpty =
9873                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
9874             assert((!FirstNameEmpty || !SecondNameEmpty) &&
9875                    "Both template parameters cannot be unnamed.");
9876             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9877                          FirstNameEmpty ? ParamEmptyName : ParamName)
9878                 << FirstName;
9879             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9880                         SecondNameEmpty ? ParamEmptyName : ParamName)
9881                 << SecondName;
9882             break;
9883           }
9884 
9885           switch (FirstDecl->getKind()) {
9886           default:
9887             llvm_unreachable("Invalid template parameter type.");
9888           case Decl::TemplateTypeParm: {
9889             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
9890             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
9891             const bool HasFirstDefaultArgument =
9892                 FirstParam->hasDefaultArgument() &&
9893                 !FirstParam->defaultArgumentWasInherited();
9894             const bool HasSecondDefaultArgument =
9895                 SecondParam->hasDefaultArgument() &&
9896                 !SecondParam->defaultArgumentWasInherited();
9897 
9898             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9899               ODRDiagError(FirstDecl->getLocation(),
9900                            FirstDecl->getSourceRange(),
9901                            ParamSingleDefaultArgument)
9902                   << HasFirstDefaultArgument;
9903               ODRDiagNote(SecondDecl->getLocation(),
9904                           SecondDecl->getSourceRange(),
9905                           ParamSingleDefaultArgument)
9906                   << HasSecondDefaultArgument;
9907               break;
9908             }
9909 
9910             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9911                    "Expecting default arguments.");
9912 
9913             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9914                          ParamDifferentDefaultArgument);
9915             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9916                         ParamDifferentDefaultArgument);
9917 
9918             break;
9919           }
9920           case Decl::NonTypeTemplateParm: {
9921             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
9922             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
9923             const bool HasFirstDefaultArgument =
9924                 FirstParam->hasDefaultArgument() &&
9925                 !FirstParam->defaultArgumentWasInherited();
9926             const bool HasSecondDefaultArgument =
9927                 SecondParam->hasDefaultArgument() &&
9928                 !SecondParam->defaultArgumentWasInherited();
9929 
9930             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9931               ODRDiagError(FirstDecl->getLocation(),
9932                            FirstDecl->getSourceRange(),
9933                            ParamSingleDefaultArgument)
9934                   << HasFirstDefaultArgument;
9935               ODRDiagNote(SecondDecl->getLocation(),
9936                           SecondDecl->getSourceRange(),
9937                           ParamSingleDefaultArgument)
9938                   << HasSecondDefaultArgument;
9939               break;
9940             }
9941 
9942             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9943                    "Expecting default arguments.");
9944 
9945             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9946                          ParamDifferentDefaultArgument);
9947             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9948                         ParamDifferentDefaultArgument);
9949 
9950             break;
9951           }
9952           case Decl::TemplateTemplateParm: {
9953             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
9954             const auto *SecondParam =
9955                 cast<TemplateTemplateParmDecl>(SecondDecl);
9956             const bool HasFirstDefaultArgument =
9957                 FirstParam->hasDefaultArgument() &&
9958                 !FirstParam->defaultArgumentWasInherited();
9959             const bool HasSecondDefaultArgument =
9960                 SecondParam->hasDefaultArgument() &&
9961                 !SecondParam->defaultArgumentWasInherited();
9962 
9963             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9964               ODRDiagError(FirstDecl->getLocation(),
9965                            FirstDecl->getSourceRange(),
9966                            ParamSingleDefaultArgument)
9967                   << HasFirstDefaultArgument;
9968               ODRDiagNote(SecondDecl->getLocation(),
9969                           SecondDecl->getSourceRange(),
9970                           ParamSingleDefaultArgument)
9971                   << HasSecondDefaultArgument;
9972               break;
9973             }
9974 
9975             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9976                    "Expecting default arguments.");
9977 
9978             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9979                          ParamDifferentDefaultArgument);
9980             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9981                         ParamDifferentDefaultArgument);
9982 
9983             break;
9984           }
9985           }
9986 
9987           break;
9988         }
9989 
9990         if (FirstIt != FirstEnd) {
9991           Diagnosed = true;
9992           break;
9993         }
9994       }
9995 
9996       DeclHashes FirstHashes;
9997       DeclHashes SecondHashes;
9998 
9999       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10000                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
10001         for (auto *D : Record->decls()) {
10002           // Due to decl merging, the first CXXRecordDecl is the parent of
10003           // Decls in both records.
10004           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10005             continue;
10006           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10007         }
10008       };
10009       PopulateHashes(FirstHashes, FirstRecord);
10010       PopulateHashes(SecondHashes, SecondRecord);
10011 
10012       // Used with err_module_odr_violation_mismatch_decl and
10013       // note_module_odr_violation_mismatch_decl
10014       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10015       enum {
10016         EndOfClass,
10017         PublicSpecifer,
10018         PrivateSpecifer,
10019         ProtectedSpecifer,
10020         StaticAssert,
10021         Field,
10022         CXXMethod,
10023         TypeAlias,
10024         TypeDef,
10025         Var,
10026         Friend,
10027         FunctionTemplate,
10028         Other
10029       } FirstDiffType = Other,
10030         SecondDiffType = Other;
10031 
10032       auto DifferenceSelector = [](Decl *D) {
10033         assert(D && "valid Decl required");
10034         switch (D->getKind()) {
10035         default:
10036           return Other;
10037         case Decl::AccessSpec:
10038           switch (D->getAccess()) {
10039           case AS_public:
10040             return PublicSpecifer;
10041           case AS_private:
10042             return PrivateSpecifer;
10043           case AS_protected:
10044             return ProtectedSpecifer;
10045           case AS_none:
10046             break;
10047           }
10048           llvm_unreachable("Invalid access specifier");
10049         case Decl::StaticAssert:
10050           return StaticAssert;
10051         case Decl::Field:
10052           return Field;
10053         case Decl::CXXMethod:
10054         case Decl::CXXConstructor:
10055         case Decl::CXXDestructor:
10056           return CXXMethod;
10057         case Decl::TypeAlias:
10058           return TypeAlias;
10059         case Decl::Typedef:
10060           return TypeDef;
10061         case Decl::Var:
10062           return Var;
10063         case Decl::Friend:
10064           return Friend;
10065         case Decl::FunctionTemplate:
10066           return FunctionTemplate;
10067         }
10068       };
10069 
10070       Decl *FirstDecl = nullptr;
10071       Decl *SecondDecl = nullptr;
10072       auto FirstIt = FirstHashes.begin();
10073       auto SecondIt = SecondHashes.begin();
10074 
10075       // If there is a diagnoseable difference, FirstDiffType and
10076       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10077       // filled in if not EndOfClass.
10078       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10079         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10080             FirstIt->second == SecondIt->second) {
10081           ++FirstIt;
10082           ++SecondIt;
10083           continue;
10084         }
10085 
10086         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10087         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10088 
10089         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10090         SecondDiffType =
10091             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10092 
10093         break;
10094       }
10095 
10096       if (FirstDiffType == Other || SecondDiffType == Other) {
10097         // Reaching this point means an unexpected Decl was encountered
10098         // or no difference was detected.  This causes a generic error
10099         // message to be emitted.
10100         Diag(FirstRecord->getLocation(),
10101              diag::err_module_odr_violation_different_definitions)
10102             << FirstRecord << FirstModule.empty() << FirstModule;
10103 
10104         if (FirstDecl) {
10105           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10106               << FirstRecord << FirstDecl->getSourceRange();
10107         }
10108 
10109         Diag(SecondRecord->getLocation(),
10110              diag::note_module_odr_violation_different_definitions)
10111             << SecondModule;
10112 
10113         if (SecondDecl) {
10114           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10115               << SecondDecl->getSourceRange();
10116         }
10117 
10118         Diagnosed = true;
10119         break;
10120       }
10121 
10122       if (FirstDiffType != SecondDiffType) {
10123         SourceLocation FirstLoc;
10124         SourceRange FirstRange;
10125         if (FirstDiffType == EndOfClass) {
10126           FirstLoc = FirstRecord->getBraceRange().getEnd();
10127         } else {
10128           FirstLoc = FirstIt->first->getLocation();
10129           FirstRange = FirstIt->first->getSourceRange();
10130         }
10131         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10132             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10133             << FirstDiffType;
10134 
10135         SourceLocation SecondLoc;
10136         SourceRange SecondRange;
10137         if (SecondDiffType == EndOfClass) {
10138           SecondLoc = SecondRecord->getBraceRange().getEnd();
10139         } else {
10140           SecondLoc = SecondDecl->getLocation();
10141           SecondRange = SecondDecl->getSourceRange();
10142         }
10143         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10144             << SecondModule << SecondRange << SecondDiffType;
10145         Diagnosed = true;
10146         break;
10147       }
10148 
10149       assert(FirstDiffType == SecondDiffType);
10150 
10151       // Used with err_module_odr_violation_mismatch_decl_diff and
10152       // note_module_odr_violation_mismatch_decl_diff
10153       enum ODRDeclDifference {
10154         StaticAssertCondition,
10155         StaticAssertMessage,
10156         StaticAssertOnlyMessage,
10157         FieldName,
10158         FieldTypeName,
10159         FieldSingleBitField,
10160         FieldDifferentWidthBitField,
10161         FieldSingleMutable,
10162         FieldSingleInitializer,
10163         FieldDifferentInitializers,
10164         MethodName,
10165         MethodDeleted,
10166         MethodDefaulted,
10167         MethodVirtual,
10168         MethodStatic,
10169         MethodVolatile,
10170         MethodConst,
10171         MethodInline,
10172         MethodNumberParameters,
10173         MethodParameterType,
10174         MethodParameterName,
10175         MethodParameterSingleDefaultArgument,
10176         MethodParameterDifferentDefaultArgument,
10177         MethodNoTemplateArguments,
10178         MethodDifferentNumberTemplateArguments,
10179         MethodDifferentTemplateArgument,
10180         MethodSingleBody,
10181         MethodDifferentBody,
10182         TypedefName,
10183         TypedefType,
10184         VarName,
10185         VarType,
10186         VarSingleInitializer,
10187         VarDifferentInitializer,
10188         VarConstexpr,
10189         FriendTypeFunction,
10190         FriendType,
10191         FriendFunction,
10192         FunctionTemplateDifferentNumberParameters,
10193         FunctionTemplateParameterDifferentKind,
10194         FunctionTemplateParameterName,
10195         FunctionTemplateParameterSingleDefaultArgument,
10196         FunctionTemplateParameterDifferentDefaultArgument,
10197         FunctionTemplateParameterDifferentType,
10198         FunctionTemplatePackParameter,
10199       };
10200 
10201       // These lambdas have the common portions of the ODR diagnostics.  This
10202       // has the same return as Diag(), so addition parameters can be passed
10203       // in with operator<<
10204       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10205           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10206         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10207                << FirstRecord << FirstModule.empty() << FirstModule << Range
10208                << DiffType;
10209       };
10210       auto ODRDiagNote = [&SecondModule, this](
10211           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10212         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10213                << SecondModule << Range << DiffType;
10214       };
10215 
10216       switch (FirstDiffType) {
10217       case Other:
10218       case EndOfClass:
10219       case PublicSpecifer:
10220       case PrivateSpecifer:
10221       case ProtectedSpecifer:
10222         llvm_unreachable("Invalid diff type");
10223 
10224       case StaticAssert: {
10225         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10226         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10227 
10228         Expr *FirstExpr = FirstSA->getAssertExpr();
10229         Expr *SecondExpr = SecondSA->getAssertExpr();
10230         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10231         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10232         if (FirstODRHash != SecondODRHash) {
10233           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10234                        StaticAssertCondition);
10235           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10236                       StaticAssertCondition);
10237           Diagnosed = true;
10238           break;
10239         }
10240 
10241         StringLiteral *FirstStr = FirstSA->getMessage();
10242         StringLiteral *SecondStr = SecondSA->getMessage();
10243         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10244         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10245           SourceLocation FirstLoc, SecondLoc;
10246           SourceRange FirstRange, SecondRange;
10247           if (FirstStr) {
10248             FirstLoc = FirstStr->getBeginLoc();
10249             FirstRange = FirstStr->getSourceRange();
10250           } else {
10251             FirstLoc = FirstSA->getBeginLoc();
10252             FirstRange = FirstSA->getSourceRange();
10253           }
10254           if (SecondStr) {
10255             SecondLoc = SecondStr->getBeginLoc();
10256             SecondRange = SecondStr->getSourceRange();
10257           } else {
10258             SecondLoc = SecondSA->getBeginLoc();
10259             SecondRange = SecondSA->getSourceRange();
10260           }
10261           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10262               << (FirstStr == nullptr);
10263           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10264               << (SecondStr == nullptr);
10265           Diagnosed = true;
10266           break;
10267         }
10268 
10269         if (FirstStr && SecondStr &&
10270             FirstStr->getString() != SecondStr->getString()) {
10271           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10272                        StaticAssertMessage);
10273           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10274                       StaticAssertMessage);
10275           Diagnosed = true;
10276           break;
10277         }
10278         break;
10279       }
10280       case Field: {
10281         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10282         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10283         IdentifierInfo *FirstII = FirstField->getIdentifier();
10284         IdentifierInfo *SecondII = SecondField->getIdentifier();
10285         if (FirstII->getName() != SecondII->getName()) {
10286           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10287                        FieldName)
10288               << FirstII;
10289           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10290                       FieldName)
10291               << SecondII;
10292 
10293           Diagnosed = true;
10294           break;
10295         }
10296 
10297         assert(getContext().hasSameType(FirstField->getType(),
10298                                         SecondField->getType()));
10299 
10300         QualType FirstType = FirstField->getType();
10301         QualType SecondType = SecondField->getType();
10302         if (ComputeQualTypeODRHash(FirstType) !=
10303             ComputeQualTypeODRHash(SecondType)) {
10304           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10305                        FieldTypeName)
10306               << FirstII << FirstType;
10307           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10308                       FieldTypeName)
10309               << SecondII << SecondType;
10310 
10311           Diagnosed = true;
10312           break;
10313         }
10314 
10315         const bool IsFirstBitField = FirstField->isBitField();
10316         const bool IsSecondBitField = SecondField->isBitField();
10317         if (IsFirstBitField != IsSecondBitField) {
10318           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10319                        FieldSingleBitField)
10320               << FirstII << IsFirstBitField;
10321           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10322                       FieldSingleBitField)
10323               << SecondII << IsSecondBitField;
10324           Diagnosed = true;
10325           break;
10326         }
10327 
10328         if (IsFirstBitField && IsSecondBitField) {
10329           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10330                        FieldDifferentWidthBitField)
10331               << FirstII << FirstField->getBitWidth()->getSourceRange();
10332           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10333                       FieldDifferentWidthBitField)
10334               << SecondII << SecondField->getBitWidth()->getSourceRange();
10335           Diagnosed = true;
10336           break;
10337         }
10338 
10339         const bool IsFirstMutable = FirstField->isMutable();
10340         const bool IsSecondMutable = SecondField->isMutable();
10341         if (IsFirstMutable != IsSecondMutable) {
10342           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10343                        FieldSingleMutable)
10344               << FirstII << IsFirstMutable;
10345           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10346                       FieldSingleMutable)
10347               << SecondII << IsSecondMutable;
10348           Diagnosed = true;
10349           break;
10350         }
10351 
10352         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10353         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10354         if ((!FirstInitializer && SecondInitializer) ||
10355             (FirstInitializer && !SecondInitializer)) {
10356           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10357                        FieldSingleInitializer)
10358               << FirstII << (FirstInitializer != nullptr);
10359           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10360                       FieldSingleInitializer)
10361               << SecondII << (SecondInitializer != nullptr);
10362           Diagnosed = true;
10363           break;
10364         }
10365 
10366         if (FirstInitializer && SecondInitializer) {
10367           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10368           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10369           if (FirstInitHash != SecondInitHash) {
10370             ODRDiagError(FirstField->getLocation(),
10371                          FirstField->getSourceRange(),
10372                          FieldDifferentInitializers)
10373                 << FirstII << FirstInitializer->getSourceRange();
10374             ODRDiagNote(SecondField->getLocation(),
10375                         SecondField->getSourceRange(),
10376                         FieldDifferentInitializers)
10377                 << SecondII << SecondInitializer->getSourceRange();
10378             Diagnosed = true;
10379             break;
10380           }
10381         }
10382 
10383         break;
10384       }
10385       case CXXMethod: {
10386         enum {
10387           DiagMethod,
10388           DiagConstructor,
10389           DiagDestructor,
10390         } FirstMethodType,
10391             SecondMethodType;
10392         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10393           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10394           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10395           return DiagMethod;
10396         };
10397         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10398         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10399         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10400         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10401         auto FirstName = FirstMethod->getDeclName();
10402         auto SecondName = SecondMethod->getDeclName();
10403         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10404           ODRDiagError(FirstMethod->getLocation(),
10405                        FirstMethod->getSourceRange(), MethodName)
10406               << FirstMethodType << FirstName;
10407           ODRDiagNote(SecondMethod->getLocation(),
10408                       SecondMethod->getSourceRange(), MethodName)
10409               << SecondMethodType << SecondName;
10410 
10411           Diagnosed = true;
10412           break;
10413         }
10414 
10415         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10416         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10417         if (FirstDeleted != SecondDeleted) {
10418           ODRDiagError(FirstMethod->getLocation(),
10419                        FirstMethod->getSourceRange(), MethodDeleted)
10420               << FirstMethodType << FirstName << FirstDeleted;
10421 
10422           ODRDiagNote(SecondMethod->getLocation(),
10423                       SecondMethod->getSourceRange(), MethodDeleted)
10424               << SecondMethodType << SecondName << SecondDeleted;
10425           Diagnosed = true;
10426           break;
10427         }
10428 
10429         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10430         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10431         if (FirstDefaulted != SecondDefaulted) {
10432           ODRDiagError(FirstMethod->getLocation(),
10433                        FirstMethod->getSourceRange(), MethodDefaulted)
10434               << FirstMethodType << FirstName << FirstDefaulted;
10435 
10436           ODRDiagNote(SecondMethod->getLocation(),
10437                       SecondMethod->getSourceRange(), MethodDefaulted)
10438               << SecondMethodType << SecondName << SecondDefaulted;
10439           Diagnosed = true;
10440           break;
10441         }
10442 
10443         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10444         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10445         const bool FirstPure = FirstMethod->isPure();
10446         const bool SecondPure = SecondMethod->isPure();
10447         if ((FirstVirtual || SecondVirtual) &&
10448             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10449           ODRDiagError(FirstMethod->getLocation(),
10450                        FirstMethod->getSourceRange(), MethodVirtual)
10451               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10452           ODRDiagNote(SecondMethod->getLocation(),
10453                       SecondMethod->getSourceRange(), MethodVirtual)
10454               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10455           Diagnosed = true;
10456           break;
10457         }
10458 
10459         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10460         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10461         // class needs to be checked instead.
10462         const auto FirstStorage = FirstMethod->getStorageClass();
10463         const auto SecondStorage = SecondMethod->getStorageClass();
10464         const bool FirstStatic = FirstStorage == SC_Static;
10465         const bool SecondStatic = SecondStorage == SC_Static;
10466         if (FirstStatic != SecondStatic) {
10467           ODRDiagError(FirstMethod->getLocation(),
10468                        FirstMethod->getSourceRange(), MethodStatic)
10469               << FirstMethodType << FirstName << FirstStatic;
10470           ODRDiagNote(SecondMethod->getLocation(),
10471                       SecondMethod->getSourceRange(), MethodStatic)
10472               << SecondMethodType << SecondName << SecondStatic;
10473           Diagnosed = true;
10474           break;
10475         }
10476 
10477         const bool FirstVolatile = FirstMethod->isVolatile();
10478         const bool SecondVolatile = SecondMethod->isVolatile();
10479         if (FirstVolatile != SecondVolatile) {
10480           ODRDiagError(FirstMethod->getLocation(),
10481                        FirstMethod->getSourceRange(), MethodVolatile)
10482               << FirstMethodType << FirstName << FirstVolatile;
10483           ODRDiagNote(SecondMethod->getLocation(),
10484                       SecondMethod->getSourceRange(), MethodVolatile)
10485               << SecondMethodType << SecondName << SecondVolatile;
10486           Diagnosed = true;
10487           break;
10488         }
10489 
10490         const bool FirstConst = FirstMethod->isConst();
10491         const bool SecondConst = SecondMethod->isConst();
10492         if (FirstConst != SecondConst) {
10493           ODRDiagError(FirstMethod->getLocation(),
10494                        FirstMethod->getSourceRange(), MethodConst)
10495               << FirstMethodType << FirstName << FirstConst;
10496           ODRDiagNote(SecondMethod->getLocation(),
10497                       SecondMethod->getSourceRange(), MethodConst)
10498               << SecondMethodType << SecondName << SecondConst;
10499           Diagnosed = true;
10500           break;
10501         }
10502 
10503         const bool FirstInline = FirstMethod->isInlineSpecified();
10504         const bool SecondInline = SecondMethod->isInlineSpecified();
10505         if (FirstInline != SecondInline) {
10506           ODRDiagError(FirstMethod->getLocation(),
10507                        FirstMethod->getSourceRange(), MethodInline)
10508               << FirstMethodType << FirstName << FirstInline;
10509           ODRDiagNote(SecondMethod->getLocation(),
10510                       SecondMethod->getSourceRange(), MethodInline)
10511               << SecondMethodType << SecondName << SecondInline;
10512           Diagnosed = true;
10513           break;
10514         }
10515 
10516         const unsigned FirstNumParameters = FirstMethod->param_size();
10517         const unsigned SecondNumParameters = SecondMethod->param_size();
10518         if (FirstNumParameters != SecondNumParameters) {
10519           ODRDiagError(FirstMethod->getLocation(),
10520                        FirstMethod->getSourceRange(), MethodNumberParameters)
10521               << FirstMethodType << FirstName << FirstNumParameters;
10522           ODRDiagNote(SecondMethod->getLocation(),
10523                       SecondMethod->getSourceRange(), MethodNumberParameters)
10524               << SecondMethodType << SecondName << SecondNumParameters;
10525           Diagnosed = true;
10526           break;
10527         }
10528 
10529         // Need this status boolean to know when break out of the switch.
10530         bool ParameterMismatch = false;
10531         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10532           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10533           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10534 
10535           QualType FirstParamType = FirstParam->getType();
10536           QualType SecondParamType = SecondParam->getType();
10537           if (FirstParamType != SecondParamType &&
10538               ComputeQualTypeODRHash(FirstParamType) !=
10539                   ComputeQualTypeODRHash(SecondParamType)) {
10540             if (const DecayedType *ParamDecayedType =
10541                     FirstParamType->getAs<DecayedType>()) {
10542               ODRDiagError(FirstMethod->getLocation(),
10543                            FirstMethod->getSourceRange(), MethodParameterType)
10544                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10545                   << true << ParamDecayedType->getOriginalType();
10546             } else {
10547               ODRDiagError(FirstMethod->getLocation(),
10548                            FirstMethod->getSourceRange(), MethodParameterType)
10549                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10550                   << false;
10551             }
10552 
10553             if (const DecayedType *ParamDecayedType =
10554                     SecondParamType->getAs<DecayedType>()) {
10555               ODRDiagNote(SecondMethod->getLocation(),
10556                           SecondMethod->getSourceRange(), MethodParameterType)
10557                   << SecondMethodType << SecondName << (I + 1)
10558                   << SecondParamType << true
10559                   << ParamDecayedType->getOriginalType();
10560             } else {
10561               ODRDiagNote(SecondMethod->getLocation(),
10562                           SecondMethod->getSourceRange(), MethodParameterType)
10563                   << SecondMethodType << SecondName << (I + 1)
10564                   << SecondParamType << false;
10565             }
10566             ParameterMismatch = true;
10567             break;
10568           }
10569 
10570           DeclarationName FirstParamName = FirstParam->getDeclName();
10571           DeclarationName SecondParamName = SecondParam->getDeclName();
10572           if (FirstParamName != SecondParamName) {
10573             ODRDiagError(FirstMethod->getLocation(),
10574                          FirstMethod->getSourceRange(), MethodParameterName)
10575                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10576             ODRDiagNote(SecondMethod->getLocation(),
10577                         SecondMethod->getSourceRange(), MethodParameterName)
10578                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10579             ParameterMismatch = true;
10580             break;
10581           }
10582 
10583           const Expr *FirstInit = FirstParam->getInit();
10584           const Expr *SecondInit = SecondParam->getInit();
10585           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10586             ODRDiagError(FirstMethod->getLocation(),
10587                          FirstMethod->getSourceRange(),
10588                          MethodParameterSingleDefaultArgument)
10589                 << FirstMethodType << FirstName << (I + 1)
10590                 << (FirstInit == nullptr)
10591                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10592             ODRDiagNote(SecondMethod->getLocation(),
10593                         SecondMethod->getSourceRange(),
10594                         MethodParameterSingleDefaultArgument)
10595                 << SecondMethodType << SecondName << (I + 1)
10596                 << (SecondInit == nullptr)
10597                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10598             ParameterMismatch = true;
10599             break;
10600           }
10601 
10602           if (FirstInit && SecondInit &&
10603               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10604             ODRDiagError(FirstMethod->getLocation(),
10605                          FirstMethod->getSourceRange(),
10606                          MethodParameterDifferentDefaultArgument)
10607                 << FirstMethodType << FirstName << (I + 1)
10608                 << FirstInit->getSourceRange();
10609             ODRDiagNote(SecondMethod->getLocation(),
10610                         SecondMethod->getSourceRange(),
10611                         MethodParameterDifferentDefaultArgument)
10612                 << SecondMethodType << SecondName << (I + 1)
10613                 << SecondInit->getSourceRange();
10614             ParameterMismatch = true;
10615             break;
10616 
10617           }
10618         }
10619 
10620         if (ParameterMismatch) {
10621           Diagnosed = true;
10622           break;
10623         }
10624 
10625         const auto *FirstTemplateArgs =
10626             FirstMethod->getTemplateSpecializationArgs();
10627         const auto *SecondTemplateArgs =
10628             SecondMethod->getTemplateSpecializationArgs();
10629 
10630         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10631             (!FirstTemplateArgs && SecondTemplateArgs)) {
10632           ODRDiagError(FirstMethod->getLocation(),
10633                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
10634               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10635           ODRDiagNote(SecondMethod->getLocation(),
10636                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
10637               << SecondMethodType << SecondName
10638               << (SecondTemplateArgs != nullptr);
10639 
10640           Diagnosed = true;
10641           break;
10642         }
10643 
10644         if (FirstTemplateArgs && SecondTemplateArgs) {
10645           // Remove pack expansions from argument list.
10646           auto ExpandTemplateArgumentList =
10647               [](const TemplateArgumentList *TAL) {
10648                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10649                 for (const TemplateArgument &TA : TAL->asArray()) {
10650                   if (TA.getKind() != TemplateArgument::Pack) {
10651                     ExpandedList.push_back(&TA);
10652                     continue;
10653                   }
10654                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10655                     ExpandedList.push_back(&PackTA);
10656                   }
10657                 }
10658                 return ExpandedList;
10659               };
10660           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10661               ExpandTemplateArgumentList(FirstTemplateArgs);
10662           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10663               ExpandTemplateArgumentList(SecondTemplateArgs);
10664 
10665           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10666             ODRDiagError(FirstMethod->getLocation(),
10667                          FirstMethod->getSourceRange(),
10668                          MethodDifferentNumberTemplateArguments)
10669                 << FirstMethodType << FirstName
10670                 << (unsigned)FirstExpandedList.size();
10671             ODRDiagNote(SecondMethod->getLocation(),
10672                         SecondMethod->getSourceRange(),
10673                         MethodDifferentNumberTemplateArguments)
10674                 << SecondMethodType << SecondName
10675                 << (unsigned)SecondExpandedList.size();
10676 
10677             Diagnosed = true;
10678             break;
10679           }
10680 
10681           bool TemplateArgumentMismatch = false;
10682           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10683             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10684                                    &SecondTA = *SecondExpandedList[i];
10685             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10686                 ComputeTemplateArgumentODRHash(SecondTA)) {
10687               continue;
10688             }
10689 
10690             ODRDiagError(FirstMethod->getLocation(),
10691                          FirstMethod->getSourceRange(),
10692                          MethodDifferentTemplateArgument)
10693                 << FirstMethodType << FirstName << FirstTA << i + 1;
10694             ODRDiagNote(SecondMethod->getLocation(),
10695                         SecondMethod->getSourceRange(),
10696                         MethodDifferentTemplateArgument)
10697                 << SecondMethodType << SecondName << SecondTA << i + 1;
10698 
10699             TemplateArgumentMismatch = true;
10700             break;
10701           }
10702 
10703           if (TemplateArgumentMismatch) {
10704             Diagnosed = true;
10705             break;
10706           }
10707         }
10708 
10709         // Compute the hash of the method as if it has no body.
10710         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10711           Hash.clear();
10712           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10713           return Hash.CalculateHash();
10714         };
10715 
10716         // Compare the hash generated to the hash stored.  A difference means
10717         // that a body was present in the original source.  Due to merging,
10718         // the stardard way of detecting a body will not work.
10719         const bool HasFirstBody =
10720             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10721         const bool HasSecondBody =
10722             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10723 
10724         if (HasFirstBody != HasSecondBody) {
10725           ODRDiagError(FirstMethod->getLocation(),
10726                        FirstMethod->getSourceRange(), MethodSingleBody)
10727               << FirstMethodType << FirstName << HasFirstBody;
10728           ODRDiagNote(SecondMethod->getLocation(),
10729                       SecondMethod->getSourceRange(), MethodSingleBody)
10730               << SecondMethodType << SecondName << HasSecondBody;
10731           Diagnosed = true;
10732           break;
10733         }
10734 
10735         if (HasFirstBody && HasSecondBody) {
10736           ODRDiagError(FirstMethod->getLocation(),
10737                        FirstMethod->getSourceRange(), MethodDifferentBody)
10738               << FirstMethodType << FirstName;
10739           ODRDiagNote(SecondMethod->getLocation(),
10740                       SecondMethod->getSourceRange(), MethodDifferentBody)
10741               << SecondMethodType << SecondName;
10742           Diagnosed = true;
10743           break;
10744         }
10745 
10746         break;
10747       }
10748       case TypeAlias:
10749       case TypeDef: {
10750         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
10751         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
10752         auto FirstName = FirstTD->getDeclName();
10753         auto SecondName = SecondTD->getDeclName();
10754         if (FirstName != SecondName) {
10755           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10756                        TypedefName)
10757               << (FirstDiffType == TypeAlias) << FirstName;
10758           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10759                       TypedefName)
10760               << (FirstDiffType == TypeAlias) << SecondName;
10761           Diagnosed = true;
10762           break;
10763         }
10764 
10765         QualType FirstType = FirstTD->getUnderlyingType();
10766         QualType SecondType = SecondTD->getUnderlyingType();
10767         if (ComputeQualTypeODRHash(FirstType) !=
10768             ComputeQualTypeODRHash(SecondType)) {
10769           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10770                        TypedefType)
10771               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
10772           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10773                       TypedefType)
10774               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
10775           Diagnosed = true;
10776           break;
10777         }
10778         break;
10779       }
10780       case Var: {
10781         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
10782         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
10783         auto FirstName = FirstVD->getDeclName();
10784         auto SecondName = SecondVD->getDeclName();
10785         if (FirstName != SecondName) {
10786           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10787                        VarName)
10788               << FirstName;
10789           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10790                       VarName)
10791               << SecondName;
10792           Diagnosed = true;
10793           break;
10794         }
10795 
10796         QualType FirstType = FirstVD->getType();
10797         QualType SecondType = SecondVD->getType();
10798         if (ComputeQualTypeODRHash(FirstType) !=
10799                         ComputeQualTypeODRHash(SecondType)) {
10800           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10801                        VarType)
10802               << FirstName << FirstType;
10803           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10804                       VarType)
10805               << SecondName << SecondType;
10806           Diagnosed = true;
10807           break;
10808         }
10809 
10810         const Expr *FirstInit = FirstVD->getInit();
10811         const Expr *SecondInit = SecondVD->getInit();
10812         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10813           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10814                        VarSingleInitializer)
10815               << FirstName << (FirstInit == nullptr)
10816               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
10817           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10818                       VarSingleInitializer)
10819               << SecondName << (SecondInit == nullptr)
10820               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10821           Diagnosed = true;
10822           break;
10823         }
10824 
10825         if (FirstInit && SecondInit &&
10826             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10827           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10828                        VarDifferentInitializer)
10829               << FirstName << FirstInit->getSourceRange();
10830           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10831                       VarDifferentInitializer)
10832               << SecondName << SecondInit->getSourceRange();
10833           Diagnosed = true;
10834           break;
10835         }
10836 
10837         const bool FirstIsConstexpr = FirstVD->isConstexpr();
10838         const bool SecondIsConstexpr = SecondVD->isConstexpr();
10839         if (FirstIsConstexpr != SecondIsConstexpr) {
10840           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10841                        VarConstexpr)
10842               << FirstName << FirstIsConstexpr;
10843           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10844                       VarConstexpr)
10845               << SecondName << SecondIsConstexpr;
10846           Diagnosed = true;
10847           break;
10848         }
10849         break;
10850       }
10851       case Friend: {
10852         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10853         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10854 
10855         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10856         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10857 
10858         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10859         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10860 
10861         if (FirstND && SecondND) {
10862           ODRDiagError(FirstFriend->getFriendLoc(),
10863                        FirstFriend->getSourceRange(), FriendFunction)
10864               << FirstND;
10865           ODRDiagNote(SecondFriend->getFriendLoc(),
10866                       SecondFriend->getSourceRange(), FriendFunction)
10867               << SecondND;
10868 
10869           Diagnosed = true;
10870           break;
10871         }
10872 
10873         if (FirstTSI && SecondTSI) {
10874           QualType FirstFriendType = FirstTSI->getType();
10875           QualType SecondFriendType = SecondTSI->getType();
10876           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10877                  ComputeQualTypeODRHash(SecondFriendType));
10878           ODRDiagError(FirstFriend->getFriendLoc(),
10879                        FirstFriend->getSourceRange(), FriendType)
10880               << FirstFriendType;
10881           ODRDiagNote(SecondFriend->getFriendLoc(),
10882                       SecondFriend->getSourceRange(), FriendType)
10883               << SecondFriendType;
10884           Diagnosed = true;
10885           break;
10886         }
10887 
10888         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
10889                      FriendTypeFunction)
10890             << (FirstTSI == nullptr);
10891         ODRDiagNote(SecondFriend->getFriendLoc(),
10892                     SecondFriend->getSourceRange(), FriendTypeFunction)
10893             << (SecondTSI == nullptr);
10894 
10895         Diagnosed = true;
10896         break;
10897       }
10898       case FunctionTemplate: {
10899         FunctionTemplateDecl *FirstTemplate =
10900             cast<FunctionTemplateDecl>(FirstDecl);
10901         FunctionTemplateDecl *SecondTemplate =
10902             cast<FunctionTemplateDecl>(SecondDecl);
10903 
10904         TemplateParameterList *FirstTPL =
10905             FirstTemplate->getTemplateParameters();
10906         TemplateParameterList *SecondTPL =
10907             SecondTemplate->getTemplateParameters();
10908 
10909         if (FirstTPL->size() != SecondTPL->size()) {
10910           ODRDiagError(FirstTemplate->getLocation(),
10911                        FirstTemplate->getSourceRange(),
10912                        FunctionTemplateDifferentNumberParameters)
10913               << FirstTemplate << FirstTPL->size();
10914           ODRDiagNote(SecondTemplate->getLocation(),
10915                       SecondTemplate->getSourceRange(),
10916                       FunctionTemplateDifferentNumberParameters)
10917               << SecondTemplate  << SecondTPL->size();
10918 
10919           Diagnosed = true;
10920           break;
10921         }
10922 
10923         bool ParameterMismatch = false;
10924         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10925           NamedDecl *FirstParam = FirstTPL->getParam(i);
10926           NamedDecl *SecondParam = SecondTPL->getParam(i);
10927 
10928           if (FirstParam->getKind() != SecondParam->getKind()) {
10929             enum {
10930               TemplateTypeParameter,
10931               NonTypeTemplateParameter,
10932               TemplateTemplateParameter,
10933             };
10934             auto GetParamType = [](NamedDecl *D) {
10935               switch (D->getKind()) {
10936                 default:
10937                   llvm_unreachable("Unexpected template parameter type");
10938                 case Decl::TemplateTypeParm:
10939                   return TemplateTypeParameter;
10940                 case Decl::NonTypeTemplateParm:
10941                   return NonTypeTemplateParameter;
10942                 case Decl::TemplateTemplateParm:
10943                   return TemplateTemplateParameter;
10944               }
10945             };
10946 
10947             ODRDiagError(FirstTemplate->getLocation(),
10948                          FirstTemplate->getSourceRange(),
10949                          FunctionTemplateParameterDifferentKind)
10950                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10951             ODRDiagNote(SecondTemplate->getLocation(),
10952                         SecondTemplate->getSourceRange(),
10953                         FunctionTemplateParameterDifferentKind)
10954                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10955 
10956             ParameterMismatch = true;
10957             break;
10958           }
10959 
10960           if (FirstParam->getName() != SecondParam->getName()) {
10961             ODRDiagError(FirstTemplate->getLocation(),
10962                          FirstTemplate->getSourceRange(),
10963                          FunctionTemplateParameterName)
10964                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10965                 << FirstParam;
10966             ODRDiagNote(SecondTemplate->getLocation(),
10967                         SecondTemplate->getSourceRange(),
10968                         FunctionTemplateParameterName)
10969                 << SecondTemplate << (i + 1)
10970                 << (bool)SecondParam->getIdentifier() << SecondParam;
10971             ParameterMismatch = true;
10972             break;
10973           }
10974 
10975           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10976               isa<TemplateTypeParmDecl>(SecondParam)) {
10977             TemplateTypeParmDecl *FirstTTPD =
10978                 cast<TemplateTypeParmDecl>(FirstParam);
10979             TemplateTypeParmDecl *SecondTTPD =
10980                 cast<TemplateTypeParmDecl>(SecondParam);
10981             bool HasFirstDefaultArgument =
10982                 FirstTTPD->hasDefaultArgument() &&
10983                 !FirstTTPD->defaultArgumentWasInherited();
10984             bool HasSecondDefaultArgument =
10985                 SecondTTPD->hasDefaultArgument() &&
10986                 !SecondTTPD->defaultArgumentWasInherited();
10987             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10988               ODRDiagError(FirstTemplate->getLocation(),
10989                            FirstTemplate->getSourceRange(),
10990                            FunctionTemplateParameterSingleDefaultArgument)
10991                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10992               ODRDiagNote(SecondTemplate->getLocation(),
10993                           SecondTemplate->getSourceRange(),
10994                           FunctionTemplateParameterSingleDefaultArgument)
10995                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10996               ParameterMismatch = true;
10997               break;
10998             }
10999 
11000             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11001               QualType FirstType = FirstTTPD->getDefaultArgument();
11002               QualType SecondType = SecondTTPD->getDefaultArgument();
11003               if (ComputeQualTypeODRHash(FirstType) !=
11004                   ComputeQualTypeODRHash(SecondType)) {
11005                 ODRDiagError(FirstTemplate->getLocation(),
11006                              FirstTemplate->getSourceRange(),
11007                              FunctionTemplateParameterDifferentDefaultArgument)
11008                     << FirstTemplate << (i + 1) << FirstType;
11009                 ODRDiagNote(SecondTemplate->getLocation(),
11010                             SecondTemplate->getSourceRange(),
11011                             FunctionTemplateParameterDifferentDefaultArgument)
11012                     << SecondTemplate << (i + 1) << SecondType;
11013                 ParameterMismatch = true;
11014                 break;
11015               }
11016             }
11017 
11018             if (FirstTTPD->isParameterPack() !=
11019                 SecondTTPD->isParameterPack()) {
11020               ODRDiagError(FirstTemplate->getLocation(),
11021                            FirstTemplate->getSourceRange(),
11022                            FunctionTemplatePackParameter)
11023                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11024               ODRDiagNote(SecondTemplate->getLocation(),
11025                           SecondTemplate->getSourceRange(),
11026                           FunctionTemplatePackParameter)
11027                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11028               ParameterMismatch = true;
11029               break;
11030             }
11031           }
11032 
11033           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11034               isa<TemplateTemplateParmDecl>(SecondParam)) {
11035             TemplateTemplateParmDecl *FirstTTPD =
11036                 cast<TemplateTemplateParmDecl>(FirstParam);
11037             TemplateTemplateParmDecl *SecondTTPD =
11038                 cast<TemplateTemplateParmDecl>(SecondParam);
11039 
11040             TemplateParameterList *FirstTPL =
11041                 FirstTTPD->getTemplateParameters();
11042             TemplateParameterList *SecondTPL =
11043                 SecondTTPD->getTemplateParameters();
11044 
11045             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11046                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11047               ODRDiagError(FirstTemplate->getLocation(),
11048                            FirstTemplate->getSourceRange(),
11049                            FunctionTemplateParameterDifferentType)
11050                   << FirstTemplate << (i + 1);
11051               ODRDiagNote(SecondTemplate->getLocation(),
11052                           SecondTemplate->getSourceRange(),
11053                           FunctionTemplateParameterDifferentType)
11054                   << SecondTemplate << (i + 1);
11055               ParameterMismatch = true;
11056               break;
11057             }
11058 
11059             bool HasFirstDefaultArgument =
11060                 FirstTTPD->hasDefaultArgument() &&
11061                 !FirstTTPD->defaultArgumentWasInherited();
11062             bool HasSecondDefaultArgument =
11063                 SecondTTPD->hasDefaultArgument() &&
11064                 !SecondTTPD->defaultArgumentWasInherited();
11065             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11066               ODRDiagError(FirstTemplate->getLocation(),
11067                            FirstTemplate->getSourceRange(),
11068                            FunctionTemplateParameterSingleDefaultArgument)
11069                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11070               ODRDiagNote(SecondTemplate->getLocation(),
11071                           SecondTemplate->getSourceRange(),
11072                           FunctionTemplateParameterSingleDefaultArgument)
11073                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11074               ParameterMismatch = true;
11075               break;
11076             }
11077 
11078             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11079               TemplateArgument FirstTA =
11080                   FirstTTPD->getDefaultArgument().getArgument();
11081               TemplateArgument SecondTA =
11082                   SecondTTPD->getDefaultArgument().getArgument();
11083               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11084                   ComputeTemplateArgumentODRHash(SecondTA)) {
11085                 ODRDiagError(FirstTemplate->getLocation(),
11086                              FirstTemplate->getSourceRange(),
11087                              FunctionTemplateParameterDifferentDefaultArgument)
11088                     << FirstTemplate << (i + 1) << FirstTA;
11089                 ODRDiagNote(SecondTemplate->getLocation(),
11090                             SecondTemplate->getSourceRange(),
11091                             FunctionTemplateParameterDifferentDefaultArgument)
11092                     << SecondTemplate << (i + 1) << SecondTA;
11093                 ParameterMismatch = true;
11094                 break;
11095               }
11096             }
11097 
11098             if (FirstTTPD->isParameterPack() !=
11099                 SecondTTPD->isParameterPack()) {
11100               ODRDiagError(FirstTemplate->getLocation(),
11101                            FirstTemplate->getSourceRange(),
11102                            FunctionTemplatePackParameter)
11103                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11104               ODRDiagNote(SecondTemplate->getLocation(),
11105                           SecondTemplate->getSourceRange(),
11106                           FunctionTemplatePackParameter)
11107                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11108               ParameterMismatch = true;
11109               break;
11110             }
11111           }
11112 
11113           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11114               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11115             NonTypeTemplateParmDecl *FirstNTTPD =
11116                 cast<NonTypeTemplateParmDecl>(FirstParam);
11117             NonTypeTemplateParmDecl *SecondNTTPD =
11118                 cast<NonTypeTemplateParmDecl>(SecondParam);
11119 
11120             QualType FirstType = FirstNTTPD->getType();
11121             QualType SecondType = SecondNTTPD->getType();
11122             if (ComputeQualTypeODRHash(FirstType) !=
11123                 ComputeQualTypeODRHash(SecondType)) {
11124               ODRDiagError(FirstTemplate->getLocation(),
11125                            FirstTemplate->getSourceRange(),
11126                            FunctionTemplateParameterDifferentType)
11127                   << FirstTemplate << (i + 1);
11128               ODRDiagNote(SecondTemplate->getLocation(),
11129                           SecondTemplate->getSourceRange(),
11130                           FunctionTemplateParameterDifferentType)
11131                   << SecondTemplate << (i + 1);
11132               ParameterMismatch = true;
11133               break;
11134             }
11135 
11136             bool HasFirstDefaultArgument =
11137                 FirstNTTPD->hasDefaultArgument() &&
11138                 !FirstNTTPD->defaultArgumentWasInherited();
11139             bool HasSecondDefaultArgument =
11140                 SecondNTTPD->hasDefaultArgument() &&
11141                 !SecondNTTPD->defaultArgumentWasInherited();
11142             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11143               ODRDiagError(FirstTemplate->getLocation(),
11144                            FirstTemplate->getSourceRange(),
11145                            FunctionTemplateParameterSingleDefaultArgument)
11146                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11147               ODRDiagNote(SecondTemplate->getLocation(),
11148                           SecondTemplate->getSourceRange(),
11149                           FunctionTemplateParameterSingleDefaultArgument)
11150                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11151               ParameterMismatch = true;
11152               break;
11153             }
11154 
11155             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11156               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11157               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11158               if (ComputeODRHash(FirstDefaultArgument) !=
11159                   ComputeODRHash(SecondDefaultArgument)) {
11160                 ODRDiagError(FirstTemplate->getLocation(),
11161                              FirstTemplate->getSourceRange(),
11162                              FunctionTemplateParameterDifferentDefaultArgument)
11163                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11164                 ODRDiagNote(SecondTemplate->getLocation(),
11165                             SecondTemplate->getSourceRange(),
11166                             FunctionTemplateParameterDifferentDefaultArgument)
11167                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11168                 ParameterMismatch = true;
11169                 break;
11170               }
11171             }
11172 
11173             if (FirstNTTPD->isParameterPack() !=
11174                 SecondNTTPD->isParameterPack()) {
11175               ODRDiagError(FirstTemplate->getLocation(),
11176                            FirstTemplate->getSourceRange(),
11177                            FunctionTemplatePackParameter)
11178                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11179               ODRDiagNote(SecondTemplate->getLocation(),
11180                           SecondTemplate->getSourceRange(),
11181                           FunctionTemplatePackParameter)
11182                   << SecondTemplate << (i + 1)
11183                   << SecondNTTPD->isParameterPack();
11184               ParameterMismatch = true;
11185               break;
11186             }
11187           }
11188         }
11189 
11190         if (ParameterMismatch) {
11191           Diagnosed = true;
11192           break;
11193         }
11194 
11195         break;
11196       }
11197       }
11198 
11199       if (Diagnosed)
11200         continue;
11201 
11202       Diag(FirstDecl->getLocation(),
11203            diag::err_module_odr_violation_mismatch_decl_unknown)
11204           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11205           << FirstDecl->getSourceRange();
11206       Diag(SecondDecl->getLocation(),
11207            diag::note_module_odr_violation_mismatch_decl_unknown)
11208           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11209       Diagnosed = true;
11210     }
11211 
11212     if (!Diagnosed) {
11213       // All definitions are updates to the same declaration. This happens if a
11214       // module instantiates the declaration of a class template specialization
11215       // and two or more other modules instantiate its definition.
11216       //
11217       // FIXME: Indicate which modules had instantiations of this definition.
11218       // FIXME: How can this even happen?
11219       Diag(Merge.first->getLocation(),
11220            diag::err_module_odr_violation_different_instantiations)
11221         << Merge.first;
11222     }
11223   }
11224 
11225   // Issue ODR failures diagnostics for functions.
11226   for (auto &Merge : FunctionOdrMergeFailures) {
11227     enum ODRFunctionDifference {
11228       ReturnType,
11229       ParameterName,
11230       ParameterType,
11231       ParameterSingleDefaultArgument,
11232       ParameterDifferentDefaultArgument,
11233       FunctionBody,
11234     };
11235 
11236     FunctionDecl *FirstFunction = Merge.first;
11237     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11238 
11239     bool Diagnosed = false;
11240     for (auto &SecondFunction : Merge.second) {
11241 
11242       if (FirstFunction == SecondFunction)
11243         continue;
11244 
11245       std::string SecondModule =
11246           getOwningModuleNameForDiagnostic(SecondFunction);
11247 
11248       auto ODRDiagError = [FirstFunction, &FirstModule,
11249                            this](SourceLocation Loc, SourceRange Range,
11250                                  ODRFunctionDifference DiffType) {
11251         return Diag(Loc, diag::err_module_odr_violation_function)
11252                << FirstFunction << FirstModule.empty() << FirstModule << Range
11253                << DiffType;
11254       };
11255       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11256                                                SourceRange Range,
11257                                                ODRFunctionDifference DiffType) {
11258         return Diag(Loc, diag::note_module_odr_violation_function)
11259                << SecondModule << Range << DiffType;
11260       };
11261 
11262       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11263           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11264         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11265                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11266             << FirstFunction->getReturnType();
11267         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11268                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11269             << SecondFunction->getReturnType();
11270         Diagnosed = true;
11271         break;
11272       }
11273 
11274       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11275              "Merged functions with different number of parameters");
11276 
11277       auto ParamSize = FirstFunction->param_size();
11278       bool ParameterMismatch = false;
11279       for (unsigned I = 0; I < ParamSize; ++I) {
11280         auto *FirstParam = FirstFunction->getParamDecl(I);
11281         auto *SecondParam = SecondFunction->getParamDecl(I);
11282 
11283         assert(getContext().hasSameType(FirstParam->getType(),
11284                                       SecondParam->getType()) &&
11285                "Merged function has different parameter types.");
11286 
11287         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11288           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11289                        ParameterName)
11290               << I + 1 << FirstParam->getDeclName();
11291           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11292                       ParameterName)
11293               << I + 1 << SecondParam->getDeclName();
11294           ParameterMismatch = true;
11295           break;
11296         };
11297 
11298         QualType FirstParamType = FirstParam->getType();
11299         QualType SecondParamType = SecondParam->getType();
11300         if (FirstParamType != SecondParamType &&
11301             ComputeQualTypeODRHash(FirstParamType) !=
11302                 ComputeQualTypeODRHash(SecondParamType)) {
11303           if (const DecayedType *ParamDecayedType =
11304                   FirstParamType->getAs<DecayedType>()) {
11305             ODRDiagError(FirstParam->getLocation(),
11306                          FirstParam->getSourceRange(), ParameterType)
11307                 << (I + 1) << FirstParamType << true
11308                 << ParamDecayedType->getOriginalType();
11309           } else {
11310             ODRDiagError(FirstParam->getLocation(),
11311                          FirstParam->getSourceRange(), ParameterType)
11312                 << (I + 1) << FirstParamType << false;
11313           }
11314 
11315           if (const DecayedType *ParamDecayedType =
11316                   SecondParamType->getAs<DecayedType>()) {
11317             ODRDiagNote(SecondParam->getLocation(),
11318                         SecondParam->getSourceRange(), ParameterType)
11319                 << (I + 1) << SecondParamType << true
11320                 << ParamDecayedType->getOriginalType();
11321           } else {
11322             ODRDiagNote(SecondParam->getLocation(),
11323                         SecondParam->getSourceRange(), ParameterType)
11324                 << (I + 1) << SecondParamType << false;
11325           }
11326           ParameterMismatch = true;
11327           break;
11328         }
11329 
11330         const Expr *FirstInit = FirstParam->getInit();
11331         const Expr *SecondInit = SecondParam->getInit();
11332         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11333           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11334                        ParameterSingleDefaultArgument)
11335               << (I + 1) << (FirstInit == nullptr)
11336               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11337           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11338                       ParameterSingleDefaultArgument)
11339               << (I + 1) << (SecondInit == nullptr)
11340               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11341           ParameterMismatch = true;
11342           break;
11343         }
11344 
11345         if (FirstInit && SecondInit &&
11346             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11347           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11348                        ParameterDifferentDefaultArgument)
11349               << (I + 1) << FirstInit->getSourceRange();
11350           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11351                       ParameterDifferentDefaultArgument)
11352               << (I + 1) << SecondInit->getSourceRange();
11353           ParameterMismatch = true;
11354           break;
11355         }
11356 
11357         assert(ComputeSubDeclODRHash(FirstParam) ==
11358                    ComputeSubDeclODRHash(SecondParam) &&
11359                "Undiagnosed parameter difference.");
11360       }
11361 
11362       if (ParameterMismatch) {
11363         Diagnosed = true;
11364         break;
11365       }
11366 
11367       // If no error has been generated before now, assume the problem is in
11368       // the body and generate a message.
11369       ODRDiagError(FirstFunction->getLocation(),
11370                    FirstFunction->getSourceRange(), FunctionBody);
11371       ODRDiagNote(SecondFunction->getLocation(),
11372                   SecondFunction->getSourceRange(), FunctionBody);
11373       Diagnosed = true;
11374       break;
11375     }
11376     (void)Diagnosed;
11377     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11378   }
11379 
11380   // Issue ODR failures diagnostics for enums.
11381   for (auto &Merge : EnumOdrMergeFailures) {
11382     enum ODREnumDifference {
11383       SingleScopedEnum,
11384       EnumTagKeywordMismatch,
11385       SingleSpecifiedType,
11386       DifferentSpecifiedTypes,
11387       DifferentNumberEnumConstants,
11388       EnumConstantName,
11389       EnumConstantSingleInitilizer,
11390       EnumConstantDifferentInitilizer,
11391     };
11392 
11393     // If we've already pointed out a specific problem with this enum, don't
11394     // bother issuing a general "something's different" diagnostic.
11395     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11396       continue;
11397 
11398     EnumDecl *FirstEnum = Merge.first;
11399     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11400 
11401     using DeclHashes =
11402         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11403     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11404                               DeclHashes &Hashes, EnumDecl *Enum) {
11405       for (auto *D : Enum->decls()) {
11406         // Due to decl merging, the first EnumDecl is the parent of
11407         // Decls in both records.
11408         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11409           continue;
11410         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11411         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11412                             ComputeSubDeclODRHash(D));
11413       }
11414     };
11415     DeclHashes FirstHashes;
11416     PopulateHashes(FirstHashes, FirstEnum);
11417     bool Diagnosed = false;
11418     for (auto &SecondEnum : Merge.second) {
11419 
11420       if (FirstEnum == SecondEnum)
11421         continue;
11422 
11423       std::string SecondModule =
11424           getOwningModuleNameForDiagnostic(SecondEnum);
11425 
11426       auto ODRDiagError = [FirstEnum, &FirstModule,
11427                            this](SourceLocation Loc, SourceRange Range,
11428                                  ODREnumDifference DiffType) {
11429         return Diag(Loc, diag::err_module_odr_violation_enum)
11430                << FirstEnum << FirstModule.empty() << FirstModule << Range
11431                << DiffType;
11432       };
11433       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11434                                                SourceRange Range,
11435                                                ODREnumDifference DiffType) {
11436         return Diag(Loc, diag::note_module_odr_violation_enum)
11437                << SecondModule << Range << DiffType;
11438       };
11439 
11440       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11441         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11442                      SingleScopedEnum)
11443             << FirstEnum->isScoped();
11444         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11445                     SingleScopedEnum)
11446             << SecondEnum->isScoped();
11447         Diagnosed = true;
11448         continue;
11449       }
11450 
11451       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11452         if (FirstEnum->isScopedUsingClassTag() !=
11453             SecondEnum->isScopedUsingClassTag()) {
11454           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455                        EnumTagKeywordMismatch)
11456               << FirstEnum->isScopedUsingClassTag();
11457           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458                       EnumTagKeywordMismatch)
11459               << SecondEnum->isScopedUsingClassTag();
11460           Diagnosed = true;
11461           continue;
11462         }
11463       }
11464 
11465       QualType FirstUnderlyingType =
11466           FirstEnum->getIntegerTypeSourceInfo()
11467               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11468               : QualType();
11469       QualType SecondUnderlyingType =
11470           SecondEnum->getIntegerTypeSourceInfo()
11471               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11472               : QualType();
11473       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11474           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11475                        SingleSpecifiedType)
11476               << !FirstUnderlyingType.isNull();
11477           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11478                       SingleSpecifiedType)
11479               << !SecondUnderlyingType.isNull();
11480           Diagnosed = true;
11481           continue;
11482       }
11483 
11484       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11485         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11486             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11487           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11488                        DifferentSpecifiedTypes)
11489               << FirstUnderlyingType;
11490           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11491                       DifferentSpecifiedTypes)
11492               << SecondUnderlyingType;
11493           Diagnosed = true;
11494           continue;
11495         }
11496       }
11497 
11498       DeclHashes SecondHashes;
11499       PopulateHashes(SecondHashes, SecondEnum);
11500 
11501       if (FirstHashes.size() != SecondHashes.size()) {
11502         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11503                      DifferentNumberEnumConstants)
11504             << (int)FirstHashes.size();
11505         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11506                     DifferentNumberEnumConstants)
11507             << (int)SecondHashes.size();
11508         Diagnosed = true;
11509         continue;
11510       }
11511 
11512       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11513         if (FirstHashes[I].second == SecondHashes[I].second)
11514           continue;
11515         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11516         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11517 
11518         if (FirstEnumConstant->getDeclName() !=
11519             SecondEnumConstant->getDeclName()) {
11520 
11521           ODRDiagError(FirstEnumConstant->getLocation(),
11522                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11523               << I + 1 << FirstEnumConstant;
11524           ODRDiagNote(SecondEnumConstant->getLocation(),
11525                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11526               << I + 1 << SecondEnumConstant;
11527           Diagnosed = true;
11528           break;
11529         }
11530 
11531         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11532         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11533         if (!FirstInit && !SecondInit)
11534           continue;
11535 
11536         if (!FirstInit || !SecondInit) {
11537           ODRDiagError(FirstEnumConstant->getLocation(),
11538                        FirstEnumConstant->getSourceRange(),
11539                        EnumConstantSingleInitilizer)
11540               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11541           ODRDiagNote(SecondEnumConstant->getLocation(),
11542                       SecondEnumConstant->getSourceRange(),
11543                       EnumConstantSingleInitilizer)
11544               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11545           Diagnosed = true;
11546           break;
11547         }
11548 
11549         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11550           ODRDiagError(FirstEnumConstant->getLocation(),
11551                        FirstEnumConstant->getSourceRange(),
11552                        EnumConstantDifferentInitilizer)
11553               << I + 1 << FirstEnumConstant;
11554           ODRDiagNote(SecondEnumConstant->getLocation(),
11555                       SecondEnumConstant->getSourceRange(),
11556                       EnumConstantDifferentInitilizer)
11557               << I + 1 << SecondEnumConstant;
11558           Diagnosed = true;
11559           break;
11560         }
11561       }
11562     }
11563 
11564     (void)Diagnosed;
11565     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11566   }
11567 }
11568 
11569 void ASTReader::StartedDeserializing() {
11570   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11571     ReadTimer->startTimer();
11572 }
11573 
11574 void ASTReader::FinishedDeserializing() {
11575   assert(NumCurrentElementsDeserializing &&
11576          "FinishedDeserializing not paired with StartedDeserializing");
11577   if (NumCurrentElementsDeserializing == 1) {
11578     // We decrease NumCurrentElementsDeserializing only after pending actions
11579     // are finished, to avoid recursively re-calling finishPendingActions().
11580     finishPendingActions();
11581   }
11582   --NumCurrentElementsDeserializing;
11583 
11584   if (NumCurrentElementsDeserializing == 0) {
11585     // Propagate exception specification and deduced type updates along
11586     // redeclaration chains.
11587     //
11588     // We do this now rather than in finishPendingActions because we want to
11589     // be able to walk the complete redeclaration chains of the updated decls.
11590     while (!PendingExceptionSpecUpdates.empty() ||
11591            !PendingDeducedTypeUpdates.empty()) {
11592       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11593       PendingExceptionSpecUpdates.clear();
11594       for (auto Update : ESUpdates) {
11595         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11596         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11597         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11598         if (auto *Listener = getContext().getASTMutationListener())
11599           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11600         for (auto *Redecl : Update.second->redecls())
11601           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11602       }
11603 
11604       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11605       PendingDeducedTypeUpdates.clear();
11606       for (auto Update : DTUpdates) {
11607         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11608         // FIXME: If the return type is already deduced, check that it matches.
11609         getContext().adjustDeducedFunctionResultType(Update.first,
11610                                                      Update.second);
11611       }
11612     }
11613 
11614     if (ReadTimer)
11615       ReadTimer->stopTimer();
11616 
11617     diagnoseOdrViolations();
11618 
11619     // We are not in recursive loading, so it's safe to pass the "interesting"
11620     // decls to the consumer.
11621     if (Consumer)
11622       PassInterestingDeclsToConsumer();
11623   }
11624 }
11625 
11626 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11627   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11628     // Remove any fake results before adding any real ones.
11629     auto It = PendingFakeLookupResults.find(II);
11630     if (It != PendingFakeLookupResults.end()) {
11631       for (auto *ND : It->second)
11632         SemaObj->IdResolver.RemoveDecl(ND);
11633       // FIXME: this works around module+PCH performance issue.
11634       // Rather than erase the result from the map, which is O(n), just clear
11635       // the vector of NamedDecls.
11636       It->second.clear();
11637     }
11638   }
11639 
11640   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11641     SemaObj->TUScope->AddDecl(D);
11642   } else if (SemaObj->TUScope) {
11643     // Adding the decl to IdResolver may have failed because it was already in
11644     // (even though it was not added in scope). If it is already in, make sure
11645     // it gets in the scope as well.
11646     if (std::find(SemaObj->IdResolver.begin(Name),
11647                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11648       SemaObj->TUScope->AddDecl(D);
11649   }
11650 }
11651 
11652 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11653                      ASTContext *Context,
11654                      const PCHContainerReader &PCHContainerRdr,
11655                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11656                      StringRef isysroot, bool DisableValidation,
11657                      bool AllowASTWithCompilerErrors,
11658                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11659                      bool UseGlobalIndex,
11660                      std::unique_ptr<llvm::Timer> ReadTimer)
11661     : Listener(DisableValidation
11662                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11663                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11664       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11665       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11666       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11667                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11668       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11669       DisableValidation(DisableValidation),
11670       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11671       AllowConfigurationMismatch(AllowConfigurationMismatch),
11672       ValidateSystemInputs(ValidateSystemInputs),
11673       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11674   SourceMgr.setExternalSLocEntrySource(this);
11675 
11676   for (const auto &Ext : Extensions) {
11677     auto BlockName = Ext->getExtensionMetadata().BlockName;
11678     auto Known = ModuleFileExtensions.find(BlockName);
11679     if (Known != ModuleFileExtensions.end()) {
11680       Diags.Report(diag::warn_duplicate_module_file_extension)
11681         << BlockName;
11682       continue;
11683     }
11684 
11685     ModuleFileExtensions.insert({BlockName, Ext});
11686   }
11687 }
11688 
11689 ASTReader::~ASTReader() {
11690   if (OwnsDeserializationListener)
11691     delete DeserializationListener;
11692 }
11693 
11694 IdentifierResolver &ASTReader::getIdResolver() {
11695   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11696 }
11697 
11698 unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11699                                      unsigned AbbrevID) {
11700   Idx = 0;
11701   Record.clear();
11702   return Cursor.readRecord(AbbrevID, Record);
11703 }
11704 //===----------------------------------------------------------------------===//
11705 //// OMPClauseReader implementation
11706 ////===----------------------------------------------------------------------===//
11707 
11708 OMPClause *OMPClauseReader::readClause() {
11709   OMPClause *C;
11710   switch (Record.readInt()) {
11711   case OMPC_if:
11712     C = new (Context) OMPIfClause();
11713     break;
11714   case OMPC_final:
11715     C = new (Context) OMPFinalClause();
11716     break;
11717   case OMPC_num_threads:
11718     C = new (Context) OMPNumThreadsClause();
11719     break;
11720   case OMPC_safelen:
11721     C = new (Context) OMPSafelenClause();
11722     break;
11723   case OMPC_simdlen:
11724     C = new (Context) OMPSimdlenClause();
11725     break;
11726   case OMPC_allocator:
11727     C = new (Context) OMPAllocatorClause();
11728     break;
11729   case OMPC_collapse:
11730     C = new (Context) OMPCollapseClause();
11731     break;
11732   case OMPC_default:
11733     C = new (Context) OMPDefaultClause();
11734     break;
11735   case OMPC_proc_bind:
11736     C = new (Context) OMPProcBindClause();
11737     break;
11738   case OMPC_schedule:
11739     C = new (Context) OMPScheduleClause();
11740     break;
11741   case OMPC_ordered:
11742     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11743     break;
11744   case OMPC_nowait:
11745     C = new (Context) OMPNowaitClause();
11746     break;
11747   case OMPC_untied:
11748     C = new (Context) OMPUntiedClause();
11749     break;
11750   case OMPC_mergeable:
11751     C = new (Context) OMPMergeableClause();
11752     break;
11753   case OMPC_read:
11754     C = new (Context) OMPReadClause();
11755     break;
11756   case OMPC_write:
11757     C = new (Context) OMPWriteClause();
11758     break;
11759   case OMPC_update:
11760     C = new (Context) OMPUpdateClause();
11761     break;
11762   case OMPC_capture:
11763     C = new (Context) OMPCaptureClause();
11764     break;
11765   case OMPC_seq_cst:
11766     C = new (Context) OMPSeqCstClause();
11767     break;
11768   case OMPC_threads:
11769     C = new (Context) OMPThreadsClause();
11770     break;
11771   case OMPC_simd:
11772     C = new (Context) OMPSIMDClause();
11773     break;
11774   case OMPC_nogroup:
11775     C = new (Context) OMPNogroupClause();
11776     break;
11777   case OMPC_unified_address:
11778     C = new (Context) OMPUnifiedAddressClause();
11779     break;
11780   case OMPC_unified_shared_memory:
11781     C = new (Context) OMPUnifiedSharedMemoryClause();
11782     break;
11783   case OMPC_reverse_offload:
11784     C = new (Context) OMPReverseOffloadClause();
11785     break;
11786   case OMPC_dynamic_allocators:
11787     C = new (Context) OMPDynamicAllocatorsClause();
11788     break;
11789   case OMPC_atomic_default_mem_order:
11790     C = new (Context) OMPAtomicDefaultMemOrderClause();
11791     break;
11792  case OMPC_private:
11793     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11794     break;
11795   case OMPC_firstprivate:
11796     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11797     break;
11798   case OMPC_lastprivate:
11799     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11800     break;
11801   case OMPC_shared:
11802     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11803     break;
11804   case OMPC_reduction:
11805     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11806     break;
11807   case OMPC_task_reduction:
11808     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11809     break;
11810   case OMPC_in_reduction:
11811     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11812     break;
11813   case OMPC_linear:
11814     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11815     break;
11816   case OMPC_aligned:
11817     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11818     break;
11819   case OMPC_copyin:
11820     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11821     break;
11822   case OMPC_copyprivate:
11823     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11824     break;
11825   case OMPC_flush:
11826     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11827     break;
11828   case OMPC_depend: {
11829     unsigned NumVars = Record.readInt();
11830     unsigned NumLoops = Record.readInt();
11831     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11832     break;
11833   }
11834   case OMPC_device:
11835     C = new (Context) OMPDeviceClause();
11836     break;
11837   case OMPC_map: {
11838     OMPMappableExprListSizeTy Sizes;
11839     Sizes.NumVars = Record.readInt();
11840     Sizes.NumUniqueDeclarations = Record.readInt();
11841     Sizes.NumComponentLists = Record.readInt();
11842     Sizes.NumComponents = Record.readInt();
11843     C = OMPMapClause::CreateEmpty(Context, Sizes);
11844     break;
11845   }
11846   case OMPC_num_teams:
11847     C = new (Context) OMPNumTeamsClause();
11848     break;
11849   case OMPC_thread_limit:
11850     C = new (Context) OMPThreadLimitClause();
11851     break;
11852   case OMPC_priority:
11853     C = new (Context) OMPPriorityClause();
11854     break;
11855   case OMPC_grainsize:
11856     C = new (Context) OMPGrainsizeClause();
11857     break;
11858   case OMPC_num_tasks:
11859     C = new (Context) OMPNumTasksClause();
11860     break;
11861   case OMPC_hint:
11862     C = new (Context) OMPHintClause();
11863     break;
11864   case OMPC_dist_schedule:
11865     C = new (Context) OMPDistScheduleClause();
11866     break;
11867   case OMPC_defaultmap:
11868     C = new (Context) OMPDefaultmapClause();
11869     break;
11870   case OMPC_to: {
11871     OMPMappableExprListSizeTy Sizes;
11872     Sizes.NumVars = Record.readInt();
11873     Sizes.NumUniqueDeclarations = Record.readInt();
11874     Sizes.NumComponentLists = Record.readInt();
11875     Sizes.NumComponents = Record.readInt();
11876     C = OMPToClause::CreateEmpty(Context, Sizes);
11877     break;
11878   }
11879   case OMPC_from: {
11880     OMPMappableExprListSizeTy Sizes;
11881     Sizes.NumVars = Record.readInt();
11882     Sizes.NumUniqueDeclarations = Record.readInt();
11883     Sizes.NumComponentLists = Record.readInt();
11884     Sizes.NumComponents = Record.readInt();
11885     C = OMPFromClause::CreateEmpty(Context, Sizes);
11886     break;
11887   }
11888   case OMPC_use_device_ptr: {
11889     OMPMappableExprListSizeTy Sizes;
11890     Sizes.NumVars = Record.readInt();
11891     Sizes.NumUniqueDeclarations = Record.readInt();
11892     Sizes.NumComponentLists = Record.readInt();
11893     Sizes.NumComponents = Record.readInt();
11894     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11895     break;
11896   }
11897   case OMPC_is_device_ptr: {
11898     OMPMappableExprListSizeTy Sizes;
11899     Sizes.NumVars = Record.readInt();
11900     Sizes.NumUniqueDeclarations = Record.readInt();
11901     Sizes.NumComponentLists = Record.readInt();
11902     Sizes.NumComponents = Record.readInt();
11903     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11904     break;
11905   }
11906   case OMPC_allocate:
11907     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11908     break;
11909   }
11910   Visit(C);
11911   C->setLocStart(Record.readSourceLocation());
11912   C->setLocEnd(Record.readSourceLocation());
11913 
11914   return C;
11915 }
11916 
11917 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11918   C->setPreInitStmt(Record.readSubStmt(),
11919                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11920 }
11921 
11922 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11923   VisitOMPClauseWithPreInit(C);
11924   C->setPostUpdateExpr(Record.readSubExpr());
11925 }
11926 
11927 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11928   VisitOMPClauseWithPreInit(C);
11929   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11930   C->setNameModifierLoc(Record.readSourceLocation());
11931   C->setColonLoc(Record.readSourceLocation());
11932   C->setCondition(Record.readSubExpr());
11933   C->setLParenLoc(Record.readSourceLocation());
11934 }
11935 
11936 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11937   C->setCondition(Record.readSubExpr());
11938   C->setLParenLoc(Record.readSourceLocation());
11939 }
11940 
11941 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11942   VisitOMPClauseWithPreInit(C);
11943   C->setNumThreads(Record.readSubExpr());
11944   C->setLParenLoc(Record.readSourceLocation());
11945 }
11946 
11947 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11948   C->setSafelen(Record.readSubExpr());
11949   C->setLParenLoc(Record.readSourceLocation());
11950 }
11951 
11952 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11953   C->setSimdlen(Record.readSubExpr());
11954   C->setLParenLoc(Record.readSourceLocation());
11955 }
11956 
11957 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11958   C->setAllocator(Record.readExpr());
11959   C->setLParenLoc(Record.readSourceLocation());
11960 }
11961 
11962 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11963   C->setNumForLoops(Record.readSubExpr());
11964   C->setLParenLoc(Record.readSourceLocation());
11965 }
11966 
11967 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11968   C->setDefaultKind(
11969        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
11970   C->setLParenLoc(Record.readSourceLocation());
11971   C->setDefaultKindKwLoc(Record.readSourceLocation());
11972 }
11973 
11974 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11975   C->setProcBindKind(
11976        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
11977   C->setLParenLoc(Record.readSourceLocation());
11978   C->setProcBindKindKwLoc(Record.readSourceLocation());
11979 }
11980 
11981 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11982   VisitOMPClauseWithPreInit(C);
11983   C->setScheduleKind(
11984        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11985   C->setFirstScheduleModifier(
11986       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11987   C->setSecondScheduleModifier(
11988       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11989   C->setChunkSize(Record.readSubExpr());
11990   C->setLParenLoc(Record.readSourceLocation());
11991   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11992   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11993   C->setScheduleKindLoc(Record.readSourceLocation());
11994   C->setCommaLoc(Record.readSourceLocation());
11995 }
11996 
11997 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11998   C->setNumForLoops(Record.readSubExpr());
11999   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12000     C->setLoopNumIterations(I, Record.readSubExpr());
12001   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12002     C->setLoopCounter(I, Record.readSubExpr());
12003   C->setLParenLoc(Record.readSourceLocation());
12004 }
12005 
12006 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12007 
12008 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12009 
12010 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12011 
12012 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12013 
12014 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12015 
12016 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12017 
12018 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12019 
12020 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12021 
12022 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12023 
12024 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12025 
12026 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12027 
12028 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12029 
12030 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12031     OMPUnifiedSharedMemoryClause *) {}
12032 
12033 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12034 
12035 void
12036 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12037 }
12038 
12039 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12040     OMPAtomicDefaultMemOrderClause *C) {
12041   C->setAtomicDefaultMemOrderKind(
12042       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12043   C->setLParenLoc(Record.readSourceLocation());
12044   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12045 }
12046 
12047 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12048   C->setLParenLoc(Record.readSourceLocation());
12049   unsigned NumVars = C->varlist_size();
12050   SmallVector<Expr *, 16> Vars;
12051   Vars.reserve(NumVars);
12052   for (unsigned i = 0; i != NumVars; ++i)
12053     Vars.push_back(Record.readSubExpr());
12054   C->setVarRefs(Vars);
12055   Vars.clear();
12056   for (unsigned i = 0; i != NumVars; ++i)
12057     Vars.push_back(Record.readSubExpr());
12058   C->setPrivateCopies(Vars);
12059 }
12060 
12061 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12062   VisitOMPClauseWithPreInit(C);
12063   C->setLParenLoc(Record.readSourceLocation());
12064   unsigned NumVars = C->varlist_size();
12065   SmallVector<Expr *, 16> Vars;
12066   Vars.reserve(NumVars);
12067   for (unsigned i = 0; i != NumVars; ++i)
12068     Vars.push_back(Record.readSubExpr());
12069   C->setVarRefs(Vars);
12070   Vars.clear();
12071   for (unsigned i = 0; i != NumVars; ++i)
12072     Vars.push_back(Record.readSubExpr());
12073   C->setPrivateCopies(Vars);
12074   Vars.clear();
12075   for (unsigned i = 0; i != NumVars; ++i)
12076     Vars.push_back(Record.readSubExpr());
12077   C->setInits(Vars);
12078 }
12079 
12080 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12081   VisitOMPClauseWithPostUpdate(C);
12082   C->setLParenLoc(Record.readSourceLocation());
12083   unsigned NumVars = C->varlist_size();
12084   SmallVector<Expr *, 16> Vars;
12085   Vars.reserve(NumVars);
12086   for (unsigned i = 0; i != NumVars; ++i)
12087     Vars.push_back(Record.readSubExpr());
12088   C->setVarRefs(Vars);
12089   Vars.clear();
12090   for (unsigned i = 0; i != NumVars; ++i)
12091     Vars.push_back(Record.readSubExpr());
12092   C->setPrivateCopies(Vars);
12093   Vars.clear();
12094   for (unsigned i = 0; i != NumVars; ++i)
12095     Vars.push_back(Record.readSubExpr());
12096   C->setSourceExprs(Vars);
12097   Vars.clear();
12098   for (unsigned i = 0; i != NumVars; ++i)
12099     Vars.push_back(Record.readSubExpr());
12100   C->setDestinationExprs(Vars);
12101   Vars.clear();
12102   for (unsigned i = 0; i != NumVars; ++i)
12103     Vars.push_back(Record.readSubExpr());
12104   C->setAssignmentOps(Vars);
12105 }
12106 
12107 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12108   C->setLParenLoc(Record.readSourceLocation());
12109   unsigned NumVars = C->varlist_size();
12110   SmallVector<Expr *, 16> Vars;
12111   Vars.reserve(NumVars);
12112   for (unsigned i = 0; i != NumVars; ++i)
12113     Vars.push_back(Record.readSubExpr());
12114   C->setVarRefs(Vars);
12115 }
12116 
12117 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12118   VisitOMPClauseWithPostUpdate(C);
12119   C->setLParenLoc(Record.readSourceLocation());
12120   C->setColonLoc(Record.readSourceLocation());
12121   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12122   DeclarationNameInfo DNI;
12123   Record.readDeclarationNameInfo(DNI);
12124   C->setQualifierLoc(NNSL);
12125   C->setNameInfo(DNI);
12126 
12127   unsigned NumVars = C->varlist_size();
12128   SmallVector<Expr *, 16> Vars;
12129   Vars.reserve(NumVars);
12130   for (unsigned i = 0; i != NumVars; ++i)
12131     Vars.push_back(Record.readSubExpr());
12132   C->setVarRefs(Vars);
12133   Vars.clear();
12134   for (unsigned i = 0; i != NumVars; ++i)
12135     Vars.push_back(Record.readSubExpr());
12136   C->setPrivates(Vars);
12137   Vars.clear();
12138   for (unsigned i = 0; i != NumVars; ++i)
12139     Vars.push_back(Record.readSubExpr());
12140   C->setLHSExprs(Vars);
12141   Vars.clear();
12142   for (unsigned i = 0; i != NumVars; ++i)
12143     Vars.push_back(Record.readSubExpr());
12144   C->setRHSExprs(Vars);
12145   Vars.clear();
12146   for (unsigned i = 0; i != NumVars; ++i)
12147     Vars.push_back(Record.readSubExpr());
12148   C->setReductionOps(Vars);
12149 }
12150 
12151 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12152   VisitOMPClauseWithPostUpdate(C);
12153   C->setLParenLoc(Record.readSourceLocation());
12154   C->setColonLoc(Record.readSourceLocation());
12155   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12156   DeclarationNameInfo DNI;
12157   Record.readDeclarationNameInfo(DNI);
12158   C->setQualifierLoc(NNSL);
12159   C->setNameInfo(DNI);
12160 
12161   unsigned NumVars = C->varlist_size();
12162   SmallVector<Expr *, 16> Vars;
12163   Vars.reserve(NumVars);
12164   for (unsigned I = 0; I != NumVars; ++I)
12165     Vars.push_back(Record.readSubExpr());
12166   C->setVarRefs(Vars);
12167   Vars.clear();
12168   for (unsigned I = 0; I != NumVars; ++I)
12169     Vars.push_back(Record.readSubExpr());
12170   C->setPrivates(Vars);
12171   Vars.clear();
12172   for (unsigned I = 0; I != NumVars; ++I)
12173     Vars.push_back(Record.readSubExpr());
12174   C->setLHSExprs(Vars);
12175   Vars.clear();
12176   for (unsigned I = 0; I != NumVars; ++I)
12177     Vars.push_back(Record.readSubExpr());
12178   C->setRHSExprs(Vars);
12179   Vars.clear();
12180   for (unsigned I = 0; I != NumVars; ++I)
12181     Vars.push_back(Record.readSubExpr());
12182   C->setReductionOps(Vars);
12183 }
12184 
12185 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12186   VisitOMPClauseWithPostUpdate(C);
12187   C->setLParenLoc(Record.readSourceLocation());
12188   C->setColonLoc(Record.readSourceLocation());
12189   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12190   DeclarationNameInfo DNI;
12191   Record.readDeclarationNameInfo(DNI);
12192   C->setQualifierLoc(NNSL);
12193   C->setNameInfo(DNI);
12194 
12195   unsigned NumVars = C->varlist_size();
12196   SmallVector<Expr *, 16> Vars;
12197   Vars.reserve(NumVars);
12198   for (unsigned I = 0; I != NumVars; ++I)
12199     Vars.push_back(Record.readSubExpr());
12200   C->setVarRefs(Vars);
12201   Vars.clear();
12202   for (unsigned I = 0; I != NumVars; ++I)
12203     Vars.push_back(Record.readSubExpr());
12204   C->setPrivates(Vars);
12205   Vars.clear();
12206   for (unsigned I = 0; I != NumVars; ++I)
12207     Vars.push_back(Record.readSubExpr());
12208   C->setLHSExprs(Vars);
12209   Vars.clear();
12210   for (unsigned I = 0; I != NumVars; ++I)
12211     Vars.push_back(Record.readSubExpr());
12212   C->setRHSExprs(Vars);
12213   Vars.clear();
12214   for (unsigned I = 0; I != NumVars; ++I)
12215     Vars.push_back(Record.readSubExpr());
12216   C->setReductionOps(Vars);
12217   Vars.clear();
12218   for (unsigned I = 0; I != NumVars; ++I)
12219     Vars.push_back(Record.readSubExpr());
12220   C->setTaskgroupDescriptors(Vars);
12221 }
12222 
12223 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12224   VisitOMPClauseWithPostUpdate(C);
12225   C->setLParenLoc(Record.readSourceLocation());
12226   C->setColonLoc(Record.readSourceLocation());
12227   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12228   C->setModifierLoc(Record.readSourceLocation());
12229   unsigned NumVars = C->varlist_size();
12230   SmallVector<Expr *, 16> Vars;
12231   Vars.reserve(NumVars);
12232   for (unsigned i = 0; i != NumVars; ++i)
12233     Vars.push_back(Record.readSubExpr());
12234   C->setVarRefs(Vars);
12235   Vars.clear();
12236   for (unsigned i = 0; i != NumVars; ++i)
12237     Vars.push_back(Record.readSubExpr());
12238   C->setPrivates(Vars);
12239   Vars.clear();
12240   for (unsigned i = 0; i != NumVars; ++i)
12241     Vars.push_back(Record.readSubExpr());
12242   C->setInits(Vars);
12243   Vars.clear();
12244   for (unsigned i = 0; i != NumVars; ++i)
12245     Vars.push_back(Record.readSubExpr());
12246   C->setUpdates(Vars);
12247   Vars.clear();
12248   for (unsigned i = 0; i != NumVars; ++i)
12249     Vars.push_back(Record.readSubExpr());
12250   C->setFinals(Vars);
12251   C->setStep(Record.readSubExpr());
12252   C->setCalcStep(Record.readSubExpr());
12253 }
12254 
12255 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12256   C->setLParenLoc(Record.readSourceLocation());
12257   C->setColonLoc(Record.readSourceLocation());
12258   unsigned NumVars = C->varlist_size();
12259   SmallVector<Expr *, 16> Vars;
12260   Vars.reserve(NumVars);
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setVarRefs(Vars);
12264   C->setAlignment(Record.readSubExpr());
12265 }
12266 
12267 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12268   C->setLParenLoc(Record.readSourceLocation());
12269   unsigned NumVars = C->varlist_size();
12270   SmallVector<Expr *, 16> Exprs;
12271   Exprs.reserve(NumVars);
12272   for (unsigned i = 0; i != NumVars; ++i)
12273     Exprs.push_back(Record.readSubExpr());
12274   C->setVarRefs(Exprs);
12275   Exprs.clear();
12276   for (unsigned i = 0; i != NumVars; ++i)
12277     Exprs.push_back(Record.readSubExpr());
12278   C->setSourceExprs(Exprs);
12279   Exprs.clear();
12280   for (unsigned i = 0; i != NumVars; ++i)
12281     Exprs.push_back(Record.readSubExpr());
12282   C->setDestinationExprs(Exprs);
12283   Exprs.clear();
12284   for (unsigned i = 0; i != NumVars; ++i)
12285     Exprs.push_back(Record.readSubExpr());
12286   C->setAssignmentOps(Exprs);
12287 }
12288 
12289 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12290   C->setLParenLoc(Record.readSourceLocation());
12291   unsigned NumVars = C->varlist_size();
12292   SmallVector<Expr *, 16> Exprs;
12293   Exprs.reserve(NumVars);
12294   for (unsigned i = 0; i != NumVars; ++i)
12295     Exprs.push_back(Record.readSubExpr());
12296   C->setVarRefs(Exprs);
12297   Exprs.clear();
12298   for (unsigned i = 0; i != NumVars; ++i)
12299     Exprs.push_back(Record.readSubExpr());
12300   C->setSourceExprs(Exprs);
12301   Exprs.clear();
12302   for (unsigned i = 0; i != NumVars; ++i)
12303     Exprs.push_back(Record.readSubExpr());
12304   C->setDestinationExprs(Exprs);
12305   Exprs.clear();
12306   for (unsigned i = 0; i != NumVars; ++i)
12307     Exprs.push_back(Record.readSubExpr());
12308   C->setAssignmentOps(Exprs);
12309 }
12310 
12311 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12312   C->setLParenLoc(Record.readSourceLocation());
12313   unsigned NumVars = C->varlist_size();
12314   SmallVector<Expr *, 16> Vars;
12315   Vars.reserve(NumVars);
12316   for (unsigned i = 0; i != NumVars; ++i)
12317     Vars.push_back(Record.readSubExpr());
12318   C->setVarRefs(Vars);
12319 }
12320 
12321 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12322   C->setLParenLoc(Record.readSourceLocation());
12323   C->setDependencyKind(
12324       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12325   C->setDependencyLoc(Record.readSourceLocation());
12326   C->setColonLoc(Record.readSourceLocation());
12327   unsigned NumVars = C->varlist_size();
12328   SmallVector<Expr *, 16> Vars;
12329   Vars.reserve(NumVars);
12330   for (unsigned I = 0; I != NumVars; ++I)
12331     Vars.push_back(Record.readSubExpr());
12332   C->setVarRefs(Vars);
12333   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12334     C->setLoopData(I, Record.readSubExpr());
12335 }
12336 
12337 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12338   VisitOMPClauseWithPreInit(C);
12339   C->setDevice(Record.readSubExpr());
12340   C->setLParenLoc(Record.readSourceLocation());
12341 }
12342 
12343 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12344   C->setLParenLoc(Record.readSourceLocation());
12345   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12346     C->setMapTypeModifier(
12347         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12348     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12349   }
12350   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12351   DeclarationNameInfo DNI;
12352   Record.readDeclarationNameInfo(DNI);
12353   C->setMapperIdInfo(DNI);
12354   C->setMapType(
12355      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12356   C->setMapLoc(Record.readSourceLocation());
12357   C->setColonLoc(Record.readSourceLocation());
12358   auto NumVars = C->varlist_size();
12359   auto UniqueDecls = C->getUniqueDeclarationsNum();
12360   auto TotalLists = C->getTotalComponentListNum();
12361   auto TotalComponents = C->getTotalComponentsNum();
12362 
12363   SmallVector<Expr *, 16> Vars;
12364   Vars.reserve(NumVars);
12365   for (unsigned i = 0; i != NumVars; ++i)
12366     Vars.push_back(Record.readExpr());
12367   C->setVarRefs(Vars);
12368 
12369   SmallVector<Expr *, 16> UDMappers;
12370   UDMappers.reserve(NumVars);
12371   for (unsigned I = 0; I < NumVars; ++I)
12372     UDMappers.push_back(Record.readExpr());
12373   C->setUDMapperRefs(UDMappers);
12374 
12375   SmallVector<ValueDecl *, 16> Decls;
12376   Decls.reserve(UniqueDecls);
12377   for (unsigned i = 0; i < UniqueDecls; ++i)
12378     Decls.push_back(Record.readDeclAs<ValueDecl>());
12379   C->setUniqueDecls(Decls);
12380 
12381   SmallVector<unsigned, 16> ListsPerDecl;
12382   ListsPerDecl.reserve(UniqueDecls);
12383   for (unsigned i = 0; i < UniqueDecls; ++i)
12384     ListsPerDecl.push_back(Record.readInt());
12385   C->setDeclNumLists(ListsPerDecl);
12386 
12387   SmallVector<unsigned, 32> ListSizes;
12388   ListSizes.reserve(TotalLists);
12389   for (unsigned i = 0; i < TotalLists; ++i)
12390     ListSizes.push_back(Record.readInt());
12391   C->setComponentListSizes(ListSizes);
12392 
12393   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12394   Components.reserve(TotalComponents);
12395   for (unsigned i = 0; i < TotalComponents; ++i) {
12396     Expr *AssociatedExpr = Record.readExpr();
12397     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12398     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12399         AssociatedExpr, AssociatedDecl));
12400   }
12401   C->setComponents(Components, ListSizes);
12402 }
12403 
12404 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12405   C->setLParenLoc(Record.readSourceLocation());
12406   C->setColonLoc(Record.readSourceLocation());
12407   C->setAllocator(Record.readSubExpr());
12408   unsigned NumVars = C->varlist_size();
12409   SmallVector<Expr *, 16> Vars;
12410   Vars.reserve(NumVars);
12411   for (unsigned i = 0; i != NumVars; ++i)
12412     Vars.push_back(Record.readSubExpr());
12413   C->setVarRefs(Vars);
12414 }
12415 
12416 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12417   VisitOMPClauseWithPreInit(C);
12418   C->setNumTeams(Record.readSubExpr());
12419   C->setLParenLoc(Record.readSourceLocation());
12420 }
12421 
12422 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12423   VisitOMPClauseWithPreInit(C);
12424   C->setThreadLimit(Record.readSubExpr());
12425   C->setLParenLoc(Record.readSourceLocation());
12426 }
12427 
12428 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12429   C->setPriority(Record.readSubExpr());
12430   C->setLParenLoc(Record.readSourceLocation());
12431 }
12432 
12433 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12434   C->setGrainsize(Record.readSubExpr());
12435   C->setLParenLoc(Record.readSourceLocation());
12436 }
12437 
12438 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12439   C->setNumTasks(Record.readSubExpr());
12440   C->setLParenLoc(Record.readSourceLocation());
12441 }
12442 
12443 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12444   C->setHint(Record.readSubExpr());
12445   C->setLParenLoc(Record.readSourceLocation());
12446 }
12447 
12448 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12449   VisitOMPClauseWithPreInit(C);
12450   C->setDistScheduleKind(
12451       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12452   C->setChunkSize(Record.readSubExpr());
12453   C->setLParenLoc(Record.readSourceLocation());
12454   C->setDistScheduleKindLoc(Record.readSourceLocation());
12455   C->setCommaLoc(Record.readSourceLocation());
12456 }
12457 
12458 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12459   C->setDefaultmapKind(
12460        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12461   C->setDefaultmapModifier(
12462       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12463   C->setLParenLoc(Record.readSourceLocation());
12464   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12465   C->setDefaultmapKindLoc(Record.readSourceLocation());
12466 }
12467 
12468 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12469   C->setLParenLoc(Record.readSourceLocation());
12470   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12471   DeclarationNameInfo DNI;
12472   Record.readDeclarationNameInfo(DNI);
12473   C->setMapperIdInfo(DNI);
12474   auto NumVars = C->varlist_size();
12475   auto UniqueDecls = C->getUniqueDeclarationsNum();
12476   auto TotalLists = C->getTotalComponentListNum();
12477   auto TotalComponents = C->getTotalComponentsNum();
12478 
12479   SmallVector<Expr *, 16> Vars;
12480   Vars.reserve(NumVars);
12481   for (unsigned i = 0; i != NumVars; ++i)
12482     Vars.push_back(Record.readSubExpr());
12483   C->setVarRefs(Vars);
12484 
12485   SmallVector<Expr *, 16> UDMappers;
12486   UDMappers.reserve(NumVars);
12487   for (unsigned I = 0; I < NumVars; ++I)
12488     UDMappers.push_back(Record.readSubExpr());
12489   C->setUDMapperRefs(UDMappers);
12490 
12491   SmallVector<ValueDecl *, 16> Decls;
12492   Decls.reserve(UniqueDecls);
12493   for (unsigned i = 0; i < UniqueDecls; ++i)
12494     Decls.push_back(Record.readDeclAs<ValueDecl>());
12495   C->setUniqueDecls(Decls);
12496 
12497   SmallVector<unsigned, 16> ListsPerDecl;
12498   ListsPerDecl.reserve(UniqueDecls);
12499   for (unsigned i = 0; i < UniqueDecls; ++i)
12500     ListsPerDecl.push_back(Record.readInt());
12501   C->setDeclNumLists(ListsPerDecl);
12502 
12503   SmallVector<unsigned, 32> ListSizes;
12504   ListSizes.reserve(TotalLists);
12505   for (unsigned i = 0; i < TotalLists; ++i)
12506     ListSizes.push_back(Record.readInt());
12507   C->setComponentListSizes(ListSizes);
12508 
12509   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12510   Components.reserve(TotalComponents);
12511   for (unsigned i = 0; i < TotalComponents; ++i) {
12512     Expr *AssociatedExpr = Record.readSubExpr();
12513     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12514     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12515         AssociatedExpr, AssociatedDecl));
12516   }
12517   C->setComponents(Components, ListSizes);
12518 }
12519 
12520 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12521   C->setLParenLoc(Record.readSourceLocation());
12522   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12523   DeclarationNameInfo DNI;
12524   Record.readDeclarationNameInfo(DNI);
12525   C->setMapperIdInfo(DNI);
12526   auto NumVars = C->varlist_size();
12527   auto UniqueDecls = C->getUniqueDeclarationsNum();
12528   auto TotalLists = C->getTotalComponentListNum();
12529   auto TotalComponents = C->getTotalComponentsNum();
12530 
12531   SmallVector<Expr *, 16> Vars;
12532   Vars.reserve(NumVars);
12533   for (unsigned i = 0; i != NumVars; ++i)
12534     Vars.push_back(Record.readSubExpr());
12535   C->setVarRefs(Vars);
12536 
12537   SmallVector<Expr *, 16> UDMappers;
12538   UDMappers.reserve(NumVars);
12539   for (unsigned I = 0; I < NumVars; ++I)
12540     UDMappers.push_back(Record.readSubExpr());
12541   C->setUDMapperRefs(UDMappers);
12542 
12543   SmallVector<ValueDecl *, 16> Decls;
12544   Decls.reserve(UniqueDecls);
12545   for (unsigned i = 0; i < UniqueDecls; ++i)
12546     Decls.push_back(Record.readDeclAs<ValueDecl>());
12547   C->setUniqueDecls(Decls);
12548 
12549   SmallVector<unsigned, 16> ListsPerDecl;
12550   ListsPerDecl.reserve(UniqueDecls);
12551   for (unsigned i = 0; i < UniqueDecls; ++i)
12552     ListsPerDecl.push_back(Record.readInt());
12553   C->setDeclNumLists(ListsPerDecl);
12554 
12555   SmallVector<unsigned, 32> ListSizes;
12556   ListSizes.reserve(TotalLists);
12557   for (unsigned i = 0; i < TotalLists; ++i)
12558     ListSizes.push_back(Record.readInt());
12559   C->setComponentListSizes(ListSizes);
12560 
12561   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12562   Components.reserve(TotalComponents);
12563   for (unsigned i = 0; i < TotalComponents; ++i) {
12564     Expr *AssociatedExpr = Record.readSubExpr();
12565     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12566     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12567         AssociatedExpr, AssociatedDecl));
12568   }
12569   C->setComponents(Components, ListSizes);
12570 }
12571 
12572 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12573   C->setLParenLoc(Record.readSourceLocation());
12574   auto NumVars = C->varlist_size();
12575   auto UniqueDecls = C->getUniqueDeclarationsNum();
12576   auto TotalLists = C->getTotalComponentListNum();
12577   auto TotalComponents = C->getTotalComponentsNum();
12578 
12579   SmallVector<Expr *, 16> Vars;
12580   Vars.reserve(NumVars);
12581   for (unsigned i = 0; i != NumVars; ++i)
12582     Vars.push_back(Record.readSubExpr());
12583   C->setVarRefs(Vars);
12584   Vars.clear();
12585   for (unsigned i = 0; i != NumVars; ++i)
12586     Vars.push_back(Record.readSubExpr());
12587   C->setPrivateCopies(Vars);
12588   Vars.clear();
12589   for (unsigned i = 0; i != NumVars; ++i)
12590     Vars.push_back(Record.readSubExpr());
12591   C->setInits(Vars);
12592 
12593   SmallVector<ValueDecl *, 16> Decls;
12594   Decls.reserve(UniqueDecls);
12595   for (unsigned i = 0; i < UniqueDecls; ++i)
12596     Decls.push_back(Record.readDeclAs<ValueDecl>());
12597   C->setUniqueDecls(Decls);
12598 
12599   SmallVector<unsigned, 16> ListsPerDecl;
12600   ListsPerDecl.reserve(UniqueDecls);
12601   for (unsigned i = 0; i < UniqueDecls; ++i)
12602     ListsPerDecl.push_back(Record.readInt());
12603   C->setDeclNumLists(ListsPerDecl);
12604 
12605   SmallVector<unsigned, 32> ListSizes;
12606   ListSizes.reserve(TotalLists);
12607   for (unsigned i = 0; i < TotalLists; ++i)
12608     ListSizes.push_back(Record.readInt());
12609   C->setComponentListSizes(ListSizes);
12610 
12611   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12612   Components.reserve(TotalComponents);
12613   for (unsigned i = 0; i < TotalComponents; ++i) {
12614     Expr *AssociatedExpr = Record.readSubExpr();
12615     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12616     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12617         AssociatedExpr, AssociatedDecl));
12618   }
12619   C->setComponents(Components, ListSizes);
12620 }
12621 
12622 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12623   C->setLParenLoc(Record.readSourceLocation());
12624   auto NumVars = C->varlist_size();
12625   auto UniqueDecls = C->getUniqueDeclarationsNum();
12626   auto TotalLists = C->getTotalComponentListNum();
12627   auto TotalComponents = C->getTotalComponentsNum();
12628 
12629   SmallVector<Expr *, 16> Vars;
12630   Vars.reserve(NumVars);
12631   for (unsigned i = 0; i != NumVars; ++i)
12632     Vars.push_back(Record.readSubExpr());
12633   C->setVarRefs(Vars);
12634   Vars.clear();
12635 
12636   SmallVector<ValueDecl *, 16> Decls;
12637   Decls.reserve(UniqueDecls);
12638   for (unsigned i = 0; i < UniqueDecls; ++i)
12639     Decls.push_back(Record.readDeclAs<ValueDecl>());
12640   C->setUniqueDecls(Decls);
12641 
12642   SmallVector<unsigned, 16> ListsPerDecl;
12643   ListsPerDecl.reserve(UniqueDecls);
12644   for (unsigned i = 0; i < UniqueDecls; ++i)
12645     ListsPerDecl.push_back(Record.readInt());
12646   C->setDeclNumLists(ListsPerDecl);
12647 
12648   SmallVector<unsigned, 32> ListSizes;
12649   ListSizes.reserve(TotalLists);
12650   for (unsigned i = 0; i < TotalLists; ++i)
12651     ListSizes.push_back(Record.readInt());
12652   C->setComponentListSizes(ListSizes);
12653 
12654   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12655   Components.reserve(TotalComponents);
12656   for (unsigned i = 0; i < TotalComponents; ++i) {
12657     Expr *AssociatedExpr = Record.readSubExpr();
12658     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12659     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12660         AssociatedExpr, AssociatedDecl));
12661   }
12662   C->setComponents(Components, ListSizes);
12663 }
12664