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/MemoryBufferCache.h"
50 #include "clang/Basic/Module.h"
51 #include "clang/Basic/ObjCRuntime.h"
52 #include "clang/Basic/OperatorKinds.h"
53 #include "clang/Basic/PragmaKinds.h"
54 #include "clang/Basic/Sanitizers.h"
55 #include "clang/Basic/SourceLocation.h"
56 #include "clang/Basic/SourceManager.h"
57 #include "clang/Basic/SourceManagerInternals.h"
58 #include "clang/Basic/Specifiers.h"
59 #include "clang/Basic/TargetInfo.h"
60 #include "clang/Basic/TargetOptions.h"
61 #include "clang/Basic/TokenKinds.h"
62 #include "clang/Basic/Version.h"
63 #include "clang/Lex/HeaderSearch.h"
64 #include "clang/Lex/HeaderSearchOptions.h"
65 #include "clang/Lex/MacroInfo.h"
66 #include "clang/Lex/ModuleMap.h"
67 #include "clang/Lex/PreprocessingRecord.h"
68 #include "clang/Lex/Preprocessor.h"
69 #include "clang/Lex/PreprocessorOptions.h"
70 #include "clang/Lex/Token.h"
71 #include "clang/Sema/ObjCMethodList.h"
72 #include "clang/Sema/Scope.h"
73 #include "clang/Sema/Sema.h"
74 #include "clang/Sema/Weak.h"
75 #include "clang/Serialization/ASTBitCodes.h"
76 #include "clang/Serialization/ASTDeserializationListener.h"
77 #include "clang/Serialization/ContinuousRangeMap.h"
78 #include "clang/Serialization/GlobalModuleIndex.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/SmallPtrSet.h"
96 #include "llvm/ADT/SmallString.h"
97 #include "llvm/ADT/SmallVector.h"
98 #include "llvm/ADT/StringExtras.h"
99 #include "llvm/ADT/StringMap.h"
100 #include "llvm/ADT/StringRef.h"
101 #include "llvm/ADT/Triple.h"
102 #include "llvm/ADT/iterator_range.h"
103 #include "llvm/Bitcode/BitstreamReader.h"
104 #include "llvm/Support/Casting.h"
105 #include "llvm/Support/Compiler.h"
106 #include "llvm/Support/Compression.h"
107 #include "llvm/Support/DJB.h"
108 #include "llvm/Support/Endian.h"
109 #include "llvm/Support/Error.h"
110 #include "llvm/Support/ErrorHandling.h"
111 #include "llvm/Support/FileSystem.h"
112 #include "llvm/Support/MemoryBuffer.h"
113 #include "llvm/Support/Path.h"
114 #include "llvm/Support/SaveAndRestore.h"
115 #include "llvm/Support/Timer.h"
116 #include "llvm/Support/VersionTuple.h"
117 #include "llvm/Support/raw_ostream.h"
118 #include <algorithm>
119 #include <cassert>
120 #include <cstddef>
121 #include <cstdint>
122 #include <cstdio>
123 #include <ctime>
124 #include <iterator>
125 #include <limits>
126 #include <map>
127 #include <memory>
128 #include <string>
129 #include <system_error>
130 #include <tuple>
131 #include <utility>
132 #include <vector>
133 
134 using namespace clang;
135 using namespace clang::serialization;
136 using namespace clang::serialization::reader;
137 using llvm::BitstreamCursor;
138 
139 //===----------------------------------------------------------------------===//
140 // ChainedASTReaderListener implementation
141 //===----------------------------------------------------------------------===//
142 
143 bool
144 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
145   return First->ReadFullVersionInformation(FullVersion) ||
146          Second->ReadFullVersionInformation(FullVersion);
147 }
148 
149 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
150   First->ReadModuleName(ModuleName);
151   Second->ReadModuleName(ModuleName);
152 }
153 
154 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
155   First->ReadModuleMapFile(ModuleMapPath);
156   Second->ReadModuleMapFile(ModuleMapPath);
157 }
158 
159 bool
160 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
161                                               bool Complain,
162                                               bool AllowCompatibleDifferences) {
163   return First->ReadLanguageOptions(LangOpts, Complain,
164                                     AllowCompatibleDifferences) ||
165          Second->ReadLanguageOptions(LangOpts, Complain,
166                                      AllowCompatibleDifferences);
167 }
168 
169 bool ChainedASTReaderListener::ReadTargetOptions(
170     const TargetOptions &TargetOpts, bool Complain,
171     bool AllowCompatibleDifferences) {
172   return First->ReadTargetOptions(TargetOpts, Complain,
173                                   AllowCompatibleDifferences) ||
174          Second->ReadTargetOptions(TargetOpts, Complain,
175                                    AllowCompatibleDifferences);
176 }
177 
178 bool ChainedASTReaderListener::ReadDiagnosticOptions(
179     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
180   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
181          Second->ReadDiagnosticOptions(DiagOpts, Complain);
182 }
183 
184 bool
185 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
186                                                 bool Complain) {
187   return First->ReadFileSystemOptions(FSOpts, Complain) ||
188          Second->ReadFileSystemOptions(FSOpts, Complain);
189 }
190 
191 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
192     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
193     bool Complain) {
194   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
195                                         Complain) ||
196          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
197                                          Complain);
198 }
199 
200 bool ChainedASTReaderListener::ReadPreprocessorOptions(
201     const PreprocessorOptions &PPOpts, bool Complain,
202     std::string &SuggestedPredefines) {
203   return First->ReadPreprocessorOptions(PPOpts, Complain,
204                                         SuggestedPredefines) ||
205          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
206 }
207 
208 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
209                                            unsigned Value) {
210   First->ReadCounter(M, Value);
211   Second->ReadCounter(M, Value);
212 }
213 
214 bool ChainedASTReaderListener::needsInputFileVisitation() {
215   return First->needsInputFileVisitation() ||
216          Second->needsInputFileVisitation();
217 }
218 
219 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
220   return First->needsSystemInputFileVisitation() ||
221   Second->needsSystemInputFileVisitation();
222 }
223 
224 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
225                                                ModuleKind Kind) {
226   First->visitModuleFile(Filename, Kind);
227   Second->visitModuleFile(Filename, Kind);
228 }
229 
230 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
231                                               bool isSystem,
232                                               bool isOverridden,
233                                               bool isExplicitModule) {
234   bool Continue = false;
235   if (First->needsInputFileVisitation() &&
236       (!isSystem || First->needsSystemInputFileVisitation()))
237     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
238                                       isExplicitModule);
239   if (Second->needsInputFileVisitation() &&
240       (!isSystem || Second->needsSystemInputFileVisitation()))
241     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
242                                        isExplicitModule);
243   return Continue;
244 }
245 
246 void ChainedASTReaderListener::readModuleFileExtension(
247        const ModuleFileExtensionMetadata &Metadata) {
248   First->readModuleFileExtension(Metadata);
249   Second->readModuleFileExtension(Metadata);
250 }
251 
252 //===----------------------------------------------------------------------===//
253 // PCH validator implementation
254 //===----------------------------------------------------------------------===//
255 
256 ASTReaderListener::~ASTReaderListener() = default;
257 
258 /// Compare the given set of language options against an existing set of
259 /// language options.
260 ///
261 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
262 /// \param AllowCompatibleDifferences If true, differences between compatible
263 ///        language options will be permitted.
264 ///
265 /// \returns true if the languagae options mis-match, false otherwise.
266 static bool checkLanguageOptions(const LangOptions &LangOpts,
267                                  const LangOptions &ExistingLangOpts,
268                                  DiagnosticsEngine *Diags,
269                                  bool AllowCompatibleDifferences = true) {
270 #define LANGOPT(Name, Bits, Default, Description)                 \
271   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
272     if (Diags)                                                    \
273       Diags->Report(diag::err_pch_langopt_mismatch)               \
274         << Description << LangOpts.Name << ExistingLangOpts.Name; \
275     return true;                                                  \
276   }
277 
278 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
279   if (ExistingLangOpts.Name != LangOpts.Name) {           \
280     if (Diags)                                            \
281       Diags->Report(diag::err_pch_langopt_value_mismatch) \
282         << Description;                                   \
283     return true;                                          \
284   }
285 
286 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
287   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
288     if (Diags)                                                 \
289       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
290         << Description;                                        \
291     return true;                                               \
292   }
293 
294 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
295   if (!AllowCompatibleDifferences)                            \
296     LANGOPT(Name, Bits, Default, Description)
297 
298 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
299   if (!AllowCompatibleDifferences)                                 \
300     ENUM_LANGOPT(Name, Bits, Default, Description)
301 
302 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
303   if (!AllowCompatibleDifferences)                                 \
304     VALUE_LANGOPT(Name, Bits, Default, Description)
305 
306 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
307 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
308 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
309 #include "clang/Basic/LangOptions.def"
310 
311   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
312     if (Diags)
313       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
314     return true;
315   }
316 
317   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
318     if (Diags)
319       Diags->Report(diag::err_pch_langopt_value_mismatch)
320       << "target Objective-C runtime";
321     return true;
322   }
323 
324   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
325       LangOpts.CommentOpts.BlockCommandNames) {
326     if (Diags)
327       Diags->Report(diag::err_pch_langopt_value_mismatch)
328         << "block command names";
329     return true;
330   }
331 
332   // Sanitizer feature mismatches are treated as compatible differences. If
333   // compatible differences aren't allowed, we still only want to check for
334   // mismatches of non-modular sanitizers (the only ones which can affect AST
335   // generation).
336   if (!AllowCompatibleDifferences) {
337     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
338     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
339     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
340     ExistingSanitizers.clear(ModularSanitizers);
341     ImportedSanitizers.clear(ModularSanitizers);
342     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
343       const std::string Flag = "-fsanitize=";
344       if (Diags) {
345 #define SANITIZER(NAME, ID)                                                    \
346   {                                                                            \
347     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
348     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
349     if (InExistingModule != InImportedModule)                                  \
350       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
351           << InExistingModule << (Flag + NAME);                                \
352   }
353 #include "clang/Basic/Sanitizers.def"
354       }
355       return true;
356     }
357   }
358 
359   return false;
360 }
361 
362 /// Compare the given set of target options against an existing set of
363 /// target options.
364 ///
365 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
366 ///
367 /// \returns true if the target options mis-match, false otherwise.
368 static bool checkTargetOptions(const TargetOptions &TargetOpts,
369                                const TargetOptions &ExistingTargetOpts,
370                                DiagnosticsEngine *Diags,
371                                bool AllowCompatibleDifferences = true) {
372 #define CHECK_TARGET_OPT(Field, Name)                             \
373   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
374     if (Diags)                                                    \
375       Diags->Report(diag::err_pch_targetopt_mismatch)             \
376         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
377     return true;                                                  \
378   }
379 
380   // The triple and ABI must match exactly.
381   CHECK_TARGET_OPT(Triple, "target");
382   CHECK_TARGET_OPT(ABI, "target ABI");
383 
384   // We can tolerate different CPUs in many cases, notably when one CPU
385   // supports a strict superset of another. When allowing compatible
386   // differences skip this check.
387   if (!AllowCompatibleDifferences)
388     CHECK_TARGET_OPT(CPU, "target CPU");
389 
390 #undef CHECK_TARGET_OPT
391 
392   // Compare feature sets.
393   SmallVector<StringRef, 4> ExistingFeatures(
394                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
395                                              ExistingTargetOpts.FeaturesAsWritten.end());
396   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
397                                          TargetOpts.FeaturesAsWritten.end());
398   llvm::sort(ExistingFeatures);
399   llvm::sort(ReadFeatures);
400 
401   // We compute the set difference in both directions explicitly so that we can
402   // diagnose the differences differently.
403   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
404   std::set_difference(
405       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
406       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
407   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
408                       ExistingFeatures.begin(), ExistingFeatures.end(),
409                       std::back_inserter(UnmatchedReadFeatures));
410 
411   // If we are allowing compatible differences and the read feature set is
412   // a strict subset of the existing feature set, there is nothing to diagnose.
413   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
414     return false;
415 
416   if (Diags) {
417     for (StringRef Feature : UnmatchedReadFeatures)
418       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
419           << /* is-existing-feature */ false << Feature;
420     for (StringRef Feature : UnmatchedExistingFeatures)
421       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
422           << /* is-existing-feature */ true << Feature;
423   }
424 
425   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
426 }
427 
428 bool
429 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
430                                   bool Complain,
431                                   bool AllowCompatibleDifferences) {
432   const LangOptions &ExistingLangOpts = PP.getLangOpts();
433   return checkLanguageOptions(LangOpts, ExistingLangOpts,
434                               Complain ? &Reader.Diags : nullptr,
435                               AllowCompatibleDifferences);
436 }
437 
438 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
439                                      bool Complain,
440                                      bool AllowCompatibleDifferences) {
441   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
442   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
443                             Complain ? &Reader.Diags : nullptr,
444                             AllowCompatibleDifferences);
445 }
446 
447 namespace {
448 
449 using MacroDefinitionsMap =
450     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
451 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
452 
453 } // namespace
454 
455 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
456                                          DiagnosticsEngine &Diags,
457                                          bool Complain) {
458   using Level = DiagnosticsEngine::Level;
459 
460   // Check current mappings for new -Werror mappings, and the stored mappings
461   // for cases that were explicitly mapped to *not* be errors that are now
462   // errors because of options like -Werror.
463   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
464 
465   for (DiagnosticsEngine *MappingSource : MappingSources) {
466     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
467       diag::kind DiagID = DiagIDMappingPair.first;
468       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
469       if (CurLevel < DiagnosticsEngine::Error)
470         continue; // not significant
471       Level StoredLevel =
472           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
473       if (StoredLevel < DiagnosticsEngine::Error) {
474         if (Complain)
475           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
476               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
477         return true;
478       }
479     }
480   }
481 
482   return false;
483 }
484 
485 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
486   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
487   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
488     return true;
489   return Ext >= diag::Severity::Error;
490 }
491 
492 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
493                                     DiagnosticsEngine &Diags,
494                                     bool IsSystem, bool Complain) {
495   // Top-level options
496   if (IsSystem) {
497     if (Diags.getSuppressSystemWarnings())
498       return false;
499     // If -Wsystem-headers was not enabled before, be conservative
500     if (StoredDiags.getSuppressSystemWarnings()) {
501       if (Complain)
502         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
503       return true;
504     }
505   }
506 
507   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
508     if (Complain)
509       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
510     return true;
511   }
512 
513   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
514       !StoredDiags.getEnableAllWarnings()) {
515     if (Complain)
516       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
517     return true;
518   }
519 
520   if (isExtHandlingFromDiagsError(Diags) &&
521       !isExtHandlingFromDiagsError(StoredDiags)) {
522     if (Complain)
523       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
524     return true;
525   }
526 
527   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
528 }
529 
530 /// Return the top import module if it is implicit, nullptr otherwise.
531 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
532                                           Preprocessor &PP) {
533   // If the original import came from a file explicitly generated by the user,
534   // don't check the diagnostic mappings.
535   // FIXME: currently this is approximated by checking whether this is not a
536   // module import of an implicitly-loaded module file.
537   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
538   // the transitive closure of its imports, since unrelated modules cannot be
539   // imported until after this module finishes validation.
540   ModuleFile *TopImport = &*ModuleMgr.rbegin();
541   while (!TopImport->ImportedBy.empty())
542     TopImport = TopImport->ImportedBy[0];
543   if (TopImport->Kind != MK_ImplicitModule)
544     return nullptr;
545 
546   StringRef ModuleName = TopImport->ModuleName;
547   assert(!ModuleName.empty() && "diagnostic options read before module name");
548 
549   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
550   assert(M && "missing module");
551   return M;
552 }
553 
554 bool PCHValidator::ReadDiagnosticOptions(
555     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
556   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
557   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
558   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
559       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
560   // This should never fail, because we would have processed these options
561   // before writing them to an ASTFile.
562   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
563 
564   ModuleManager &ModuleMgr = Reader.getModuleManager();
565   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
566 
567   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
568   if (!TopM)
569     return false;
570 
571   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
572   // contains the union of their flags.
573   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
574                                  Complain);
575 }
576 
577 /// Collect the macro definitions provided by the given preprocessor
578 /// options.
579 static void
580 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
581                         MacroDefinitionsMap &Macros,
582                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
583   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
584     StringRef Macro = PPOpts.Macros[I].first;
585     bool IsUndef = PPOpts.Macros[I].second;
586 
587     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
588     StringRef MacroName = MacroPair.first;
589     StringRef MacroBody = MacroPair.second;
590 
591     // For an #undef'd macro, we only care about the name.
592     if (IsUndef) {
593       if (MacroNames && !Macros.count(MacroName))
594         MacroNames->push_back(MacroName);
595 
596       Macros[MacroName] = std::make_pair("", true);
597       continue;
598     }
599 
600     // For a #define'd macro, figure out the actual definition.
601     if (MacroName.size() == Macro.size())
602       MacroBody = "1";
603     else {
604       // Note: GCC drops anything following an end-of-line character.
605       StringRef::size_type End = MacroBody.find_first_of("\n\r");
606       MacroBody = MacroBody.substr(0, End);
607     }
608 
609     if (MacroNames && !Macros.count(MacroName))
610       MacroNames->push_back(MacroName);
611     Macros[MacroName] = std::make_pair(MacroBody, false);
612   }
613 }
614 
615 /// Check the preprocessor options deserialized from the control block
616 /// against the preprocessor options in an existing preprocessor.
617 ///
618 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
619 /// \param Validate If true, validate preprocessor options. If false, allow
620 ///        macros defined by \p ExistingPPOpts to override those defined by
621 ///        \p PPOpts in SuggestedPredefines.
622 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
623                                      const PreprocessorOptions &ExistingPPOpts,
624                                      DiagnosticsEngine *Diags,
625                                      FileManager &FileMgr,
626                                      std::string &SuggestedPredefines,
627                                      const LangOptions &LangOpts,
628                                      bool Validate = true) {
629   // Check macro definitions.
630   MacroDefinitionsMap ASTFileMacros;
631   collectMacroDefinitions(PPOpts, ASTFileMacros);
632   MacroDefinitionsMap ExistingMacros;
633   SmallVector<StringRef, 4> ExistingMacroNames;
634   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
635 
636   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
637     // Dig out the macro definition in the existing preprocessor options.
638     StringRef MacroName = ExistingMacroNames[I];
639     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
640 
641     // Check whether we know anything about this macro name or not.
642     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
643         ASTFileMacros.find(MacroName);
644     if (!Validate || Known == ASTFileMacros.end()) {
645       // FIXME: Check whether this identifier was referenced anywhere in the
646       // AST file. If so, we should reject the AST file. Unfortunately, this
647       // information isn't in the control block. What shall we do about it?
648 
649       if (Existing.second) {
650         SuggestedPredefines += "#undef ";
651         SuggestedPredefines += MacroName.str();
652         SuggestedPredefines += '\n';
653       } else {
654         SuggestedPredefines += "#define ";
655         SuggestedPredefines += MacroName.str();
656         SuggestedPredefines += ' ';
657         SuggestedPredefines += Existing.first.str();
658         SuggestedPredefines += '\n';
659       }
660       continue;
661     }
662 
663     // If the macro was defined in one but undef'd in the other, we have a
664     // conflict.
665     if (Existing.second != Known->second.second) {
666       if (Diags) {
667         Diags->Report(diag::err_pch_macro_def_undef)
668           << MacroName << Known->second.second;
669       }
670       return true;
671     }
672 
673     // If the macro was #undef'd in both, or if the macro bodies are identical,
674     // it's fine.
675     if (Existing.second || Existing.first == Known->second.first)
676       continue;
677 
678     // The macro bodies differ; complain.
679     if (Diags) {
680       Diags->Report(diag::err_pch_macro_def_conflict)
681         << MacroName << Known->second.first << Existing.first;
682     }
683     return true;
684   }
685 
686   // Check whether we're using predefines.
687   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
688     if (Diags) {
689       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
690     }
691     return true;
692   }
693 
694   // Detailed record is important since it is used for the module cache hash.
695   if (LangOpts.Modules &&
696       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
697     if (Diags) {
698       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
699     }
700     return true;
701   }
702 
703   // Compute the #include and #include_macros lines we need.
704   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
705     StringRef File = ExistingPPOpts.Includes[I];
706 
707     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
708         !ExistingPPOpts.PCHThroughHeader.empty()) {
709       // In case the through header is an include, we must add all the includes
710       // to the predefines so the start point can be determined.
711       SuggestedPredefines += "#include \"";
712       SuggestedPredefines += File;
713       SuggestedPredefines += "\"\n";
714       continue;
715     }
716 
717     if (File == ExistingPPOpts.ImplicitPCHInclude)
718       continue;
719 
720     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
721           != PPOpts.Includes.end())
722       continue;
723 
724     SuggestedPredefines += "#include \"";
725     SuggestedPredefines += File;
726     SuggestedPredefines += "\"\n";
727   }
728 
729   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
730     StringRef File = ExistingPPOpts.MacroIncludes[I];
731     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
732                   File)
733         != PPOpts.MacroIncludes.end())
734       continue;
735 
736     SuggestedPredefines += "#__include_macros \"";
737     SuggestedPredefines += File;
738     SuggestedPredefines += "\"\n##\n";
739   }
740 
741   return false;
742 }
743 
744 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
745                                            bool Complain,
746                                            std::string &SuggestedPredefines) {
747   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
748 
749   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
750                                   Complain? &Reader.Diags : nullptr,
751                                   PP.getFileManager(),
752                                   SuggestedPredefines,
753                                   PP.getLangOpts());
754 }
755 
756 bool SimpleASTReaderListener::ReadPreprocessorOptions(
757                                   const PreprocessorOptions &PPOpts,
758                                   bool Complain,
759                                   std::string &SuggestedPredefines) {
760   return checkPreprocessorOptions(PPOpts,
761                                   PP.getPreprocessorOpts(),
762                                   nullptr,
763                                   PP.getFileManager(),
764                                   SuggestedPredefines,
765                                   PP.getLangOpts(),
766                                   false);
767 }
768 
769 /// Check the header search options deserialized from the control block
770 /// against the header search options in an existing preprocessor.
771 ///
772 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
773 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
774                                      StringRef SpecificModuleCachePath,
775                                      StringRef ExistingModuleCachePath,
776                                      DiagnosticsEngine *Diags,
777                                      const LangOptions &LangOpts) {
778   if (LangOpts.Modules) {
779     if (SpecificModuleCachePath != ExistingModuleCachePath) {
780       if (Diags)
781         Diags->Report(diag::err_pch_modulecache_mismatch)
782           << SpecificModuleCachePath << ExistingModuleCachePath;
783       return true;
784     }
785   }
786 
787   return false;
788 }
789 
790 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
791                                            StringRef SpecificModuleCachePath,
792                                            bool Complain) {
793   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
794                                   PP.getHeaderSearchInfo().getModuleCachePath(),
795                                   Complain ? &Reader.Diags : nullptr,
796                                   PP.getLangOpts());
797 }
798 
799 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
800   PP.setCounterValue(Value);
801 }
802 
803 //===----------------------------------------------------------------------===//
804 // AST reader implementation
805 //===----------------------------------------------------------------------===//
806 
807 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
808                                            bool TakeOwnership) {
809   DeserializationListener = Listener;
810   OwnsDeserializationListener = TakeOwnership;
811 }
812 
813 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
814   return serialization::ComputeHash(Sel);
815 }
816 
817 std::pair<unsigned, unsigned>
818 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
819   using namespace llvm::support;
820 
821   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
822   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
823   return std::make_pair(KeyLen, DataLen);
824 }
825 
826 ASTSelectorLookupTrait::internal_key_type
827 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
828   using namespace llvm::support;
829 
830   SelectorTable &SelTable = Reader.getContext().Selectors;
831   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
832   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
833       F, endian::readNext<uint32_t, little, unaligned>(d));
834   if (N == 0)
835     return SelTable.getNullarySelector(FirstII);
836   else if (N == 1)
837     return SelTable.getUnarySelector(FirstII);
838 
839   SmallVector<IdentifierInfo *, 16> Args;
840   Args.push_back(FirstII);
841   for (unsigned I = 1; I != N; ++I)
842     Args.push_back(Reader.getLocalIdentifier(
843         F, endian::readNext<uint32_t, little, unaligned>(d)));
844 
845   return SelTable.getSelector(N, Args.data());
846 }
847 
848 ASTSelectorLookupTrait::data_type
849 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
850                                  unsigned DataLen) {
851   using namespace llvm::support;
852 
853   data_type Result;
854 
855   Result.ID = Reader.getGlobalSelectorID(
856       F, endian::readNext<uint32_t, little, unaligned>(d));
857   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
858   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
859   Result.InstanceBits = FullInstanceBits & 0x3;
860   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
861   Result.FactoryBits = FullFactoryBits & 0x3;
862   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
863   unsigned NumInstanceMethods = FullInstanceBits >> 3;
864   unsigned NumFactoryMethods = FullFactoryBits >> 3;
865 
866   // Load instance methods
867   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
868     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
869             F, endian::readNext<uint32_t, little, unaligned>(d)))
870       Result.Instance.push_back(Method);
871   }
872 
873   // Load factory methods
874   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
875     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
876             F, endian::readNext<uint32_t, little, unaligned>(d)))
877       Result.Factory.push_back(Method);
878   }
879 
880   return Result;
881 }
882 
883 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
884   return llvm::djbHash(a);
885 }
886 
887 std::pair<unsigned, unsigned>
888 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
889   using namespace llvm::support;
890 
891   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
892   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
893   return std::make_pair(KeyLen, DataLen);
894 }
895 
896 ASTIdentifierLookupTraitBase::internal_key_type
897 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
898   assert(n >= 2 && d[n-1] == '\0');
899   return StringRef((const char*) d, n-1);
900 }
901 
902 /// Whether the given identifier is "interesting".
903 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
904                                     bool IsModule) {
905   return II.hadMacroDefinition() ||
906          II.isPoisoned() ||
907          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
908          II.hasRevertedTokenIDToIdentifier() ||
909          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
910           II.getFETokenInfo());
911 }
912 
913 static bool readBit(unsigned &Bits) {
914   bool Value = Bits & 0x1;
915   Bits >>= 1;
916   return Value;
917 }
918 
919 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
920   using namespace llvm::support;
921 
922   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
923   return Reader.getGlobalIdentifierID(F, RawID >> 1);
924 }
925 
926 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
927   if (!II.isFromAST()) {
928     II.setIsFromAST();
929     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
930     if (isInterestingIdentifier(Reader, II, IsModule))
931       II.setChangedSinceDeserialization();
932   }
933 }
934 
935 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
936                                                    const unsigned char* d,
937                                                    unsigned DataLen) {
938   using namespace llvm::support;
939 
940   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
941   bool IsInteresting = RawID & 0x01;
942 
943   // Wipe out the "is interesting" bit.
944   RawID = RawID >> 1;
945 
946   // Build the IdentifierInfo and link the identifier ID with it.
947   IdentifierInfo *II = KnownII;
948   if (!II) {
949     II = &Reader.getIdentifierTable().getOwn(k);
950     KnownII = II;
951   }
952   markIdentifierFromAST(Reader, *II);
953   Reader.markIdentifierUpToDate(II);
954 
955   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
956   if (!IsInteresting) {
957     // For uninteresting identifiers, there's nothing else to do. Just notify
958     // the reader that we've finished loading this identifier.
959     Reader.SetIdentifierInfo(ID, II);
960     return II;
961   }
962 
963   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
964   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
965   bool CPlusPlusOperatorKeyword = readBit(Bits);
966   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
967   bool HasRevertedBuiltin = readBit(Bits);
968   bool Poisoned = readBit(Bits);
969   bool ExtensionToken = readBit(Bits);
970   bool HadMacroDefinition = readBit(Bits);
971 
972   assert(Bits == 0 && "Extra bits in the identifier?");
973   DataLen -= 8;
974 
975   // Set or check the various bits in the IdentifierInfo structure.
976   // Token IDs are read-only.
977   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
978     II->revertTokenIDToIdentifier();
979   if (!F.isModule())
980     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
981   else if (HasRevertedBuiltin && II->getBuiltinID()) {
982     II->revertBuiltin();
983     assert((II->hasRevertedBuiltin() ||
984             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
985            "Incorrect ObjC keyword or builtin ID");
986   }
987   assert(II->isExtensionToken() == ExtensionToken &&
988          "Incorrect extension token flag");
989   (void)ExtensionToken;
990   if (Poisoned)
991     II->setIsPoisoned(true);
992   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
993          "Incorrect C++ operator keyword flag");
994   (void)CPlusPlusOperatorKeyword;
995 
996   // If this identifier is a macro, deserialize the macro
997   // definition.
998   if (HadMacroDefinition) {
999     uint32_t MacroDirectivesOffset =
1000         endian::readNext<uint32_t, little, unaligned>(d);
1001     DataLen -= 4;
1002 
1003     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1004   }
1005 
1006   Reader.SetIdentifierInfo(ID, II);
1007 
1008   // Read all of the declarations visible at global scope with this
1009   // name.
1010   if (DataLen > 0) {
1011     SmallVector<uint32_t, 4> DeclIDs;
1012     for (; DataLen > 0; DataLen -= 4)
1013       DeclIDs.push_back(Reader.getGlobalDeclID(
1014           F, endian::readNext<uint32_t, little, unaligned>(d)));
1015     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1016   }
1017 
1018   return II;
1019 }
1020 
1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1022     : Kind(Name.getNameKind()) {
1023   switch (Kind) {
1024   case DeclarationName::Identifier:
1025     Data = (uint64_t)Name.getAsIdentifierInfo();
1026     break;
1027   case DeclarationName::ObjCZeroArgSelector:
1028   case DeclarationName::ObjCOneArgSelector:
1029   case DeclarationName::ObjCMultiArgSelector:
1030     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1031     break;
1032   case DeclarationName::CXXOperatorName:
1033     Data = Name.getCXXOverloadedOperator();
1034     break;
1035   case DeclarationName::CXXLiteralOperatorName:
1036     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1037     break;
1038   case DeclarationName::CXXDeductionGuideName:
1039     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1040                ->getDeclName().getAsIdentifierInfo();
1041     break;
1042   case DeclarationName::CXXConstructorName:
1043   case DeclarationName::CXXDestructorName:
1044   case DeclarationName::CXXConversionFunctionName:
1045   case DeclarationName::CXXUsingDirective:
1046     Data = 0;
1047     break;
1048   }
1049 }
1050 
1051 unsigned DeclarationNameKey::getHash() const {
1052   llvm::FoldingSetNodeID ID;
1053   ID.AddInteger(Kind);
1054 
1055   switch (Kind) {
1056   case DeclarationName::Identifier:
1057   case DeclarationName::CXXLiteralOperatorName:
1058   case DeclarationName::CXXDeductionGuideName:
1059     ID.AddString(((IdentifierInfo*)Data)->getName());
1060     break;
1061   case DeclarationName::ObjCZeroArgSelector:
1062   case DeclarationName::ObjCOneArgSelector:
1063   case DeclarationName::ObjCMultiArgSelector:
1064     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1065     break;
1066   case DeclarationName::CXXOperatorName:
1067     ID.AddInteger((OverloadedOperatorKind)Data);
1068     break;
1069   case DeclarationName::CXXConstructorName:
1070   case DeclarationName::CXXDestructorName:
1071   case DeclarationName::CXXConversionFunctionName:
1072   case DeclarationName::CXXUsingDirective:
1073     break;
1074   }
1075 
1076   return ID.ComputeHash();
1077 }
1078 
1079 ModuleFile *
1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1081   using namespace llvm::support;
1082 
1083   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1084   return Reader.getLocalModuleFile(F, ModuleFileID);
1085 }
1086 
1087 std::pair<unsigned, unsigned>
1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1089   using namespace llvm::support;
1090 
1091   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1092   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   return std::make_pair(KeyLen, DataLen);
1094 }
1095 
1096 ASTDeclContextNameLookupTrait::internal_key_type
1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1098   using namespace llvm::support;
1099 
1100   auto Kind = (DeclarationName::NameKind)*d++;
1101   uint64_t Data;
1102   switch (Kind) {
1103   case DeclarationName::Identifier:
1104   case DeclarationName::CXXLiteralOperatorName:
1105   case DeclarationName::CXXDeductionGuideName:
1106     Data = (uint64_t)Reader.getLocalIdentifier(
1107         F, endian::readNext<uint32_t, little, unaligned>(d));
1108     break;
1109   case DeclarationName::ObjCZeroArgSelector:
1110   case DeclarationName::ObjCOneArgSelector:
1111   case DeclarationName::ObjCMultiArgSelector:
1112     Data =
1113         (uint64_t)Reader.getLocalSelector(
1114                              F, endian::readNext<uint32_t, little, unaligned>(
1115                                     d)).getAsOpaquePtr();
1116     break;
1117   case DeclarationName::CXXOperatorName:
1118     Data = *d++; // OverloadedOperatorKind
1119     break;
1120   case DeclarationName::CXXConstructorName:
1121   case DeclarationName::CXXDestructorName:
1122   case DeclarationName::CXXConversionFunctionName:
1123   case DeclarationName::CXXUsingDirective:
1124     Data = 0;
1125     break;
1126   }
1127 
1128   return DeclarationNameKey(Kind, Data);
1129 }
1130 
1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1132                                                  const unsigned char *d,
1133                                                  unsigned DataLen,
1134                                                  data_type_builder &Val) {
1135   using namespace llvm::support;
1136 
1137   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1138     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1139     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1140   }
1141 }
1142 
1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1144                                               BitstreamCursor &Cursor,
1145                                               uint64_t Offset,
1146                                               DeclContext *DC) {
1147   assert(Offset != 0);
1148 
1149   SavedStreamPosition SavedPosition(Cursor);
1150   Cursor.JumpToBit(Offset);
1151 
1152   RecordData Record;
1153   StringRef Blob;
1154   unsigned Code = Cursor.ReadCode();
1155   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1156   if (RecCode != DECL_CONTEXT_LEXICAL) {
1157     Error("Expected lexical block");
1158     return true;
1159   }
1160 
1161   assert(!isa<TranslationUnitDecl>(DC) &&
1162          "expected a TU_UPDATE_LEXICAL record for TU");
1163   // If we are handling a C++ class template instantiation, we can see multiple
1164   // lexical updates for the same record. It's important that we select only one
1165   // of them, so that field numbering works properly. Just pick the first one we
1166   // see.
1167   auto &Lex = LexicalDecls[DC];
1168   if (!Lex.first) {
1169     Lex = std::make_pair(
1170         &M, llvm::makeArrayRef(
1171                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1172                     Blob.data()),
1173                 Blob.size() / 4));
1174   }
1175   DC->setHasExternalLexicalStorage(true);
1176   return false;
1177 }
1178 
1179 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1180                                               BitstreamCursor &Cursor,
1181                                               uint64_t Offset,
1182                                               DeclID ID) {
1183   assert(Offset != 0);
1184 
1185   SavedStreamPosition SavedPosition(Cursor);
1186   Cursor.JumpToBit(Offset);
1187 
1188   RecordData Record;
1189   StringRef Blob;
1190   unsigned Code = Cursor.ReadCode();
1191   unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1192   if (RecCode != DECL_CONTEXT_VISIBLE) {
1193     Error("Expected visible lookup table block");
1194     return true;
1195   }
1196 
1197   // We can't safely determine the primary context yet, so delay attaching the
1198   // lookup table until we're done with recursive deserialization.
1199   auto *Data = (const unsigned char*)Blob.data();
1200   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1201   return false;
1202 }
1203 
1204 void ASTReader::Error(StringRef Msg) const {
1205   Error(diag::err_fe_pch_malformed, Msg);
1206   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1207       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1208     Diag(diag::note_module_cache_path)
1209       << PP.getHeaderSearchInfo().getModuleCachePath();
1210   }
1211 }
1212 
1213 void ASTReader::Error(unsigned DiagID,
1214                       StringRef Arg1, StringRef Arg2) const {
1215   if (Diags.isDiagnosticInFlight())
1216     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1217   else
1218     Diag(DiagID) << Arg1 << Arg2;
1219 }
1220 
1221 //===----------------------------------------------------------------------===//
1222 // Source Manager Deserialization
1223 //===----------------------------------------------------------------------===//
1224 
1225 /// Read the line table in the source manager block.
1226 /// \returns true if there was an error.
1227 bool ASTReader::ParseLineTable(ModuleFile &F,
1228                                const RecordData &Record) {
1229   unsigned Idx = 0;
1230   LineTableInfo &LineTable = SourceMgr.getLineTable();
1231 
1232   // Parse the file names
1233   std::map<int, int> FileIDs;
1234   FileIDs[-1] = -1; // For unspecified filenames.
1235   for (unsigned I = 0; Record[Idx]; ++I) {
1236     // Extract the file name
1237     auto Filename = ReadPath(F, Record, Idx);
1238     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1239   }
1240   ++Idx;
1241 
1242   // Parse the line entries
1243   std::vector<LineEntry> Entries;
1244   while (Idx < Record.size()) {
1245     int FID = Record[Idx++];
1246     assert(FID >= 0 && "Serialized line entries for non-local file.");
1247     // Remap FileID from 1-based old view.
1248     FID += F.SLocEntryBaseID - 1;
1249 
1250     // Extract the line entries
1251     unsigned NumEntries = Record[Idx++];
1252     assert(NumEntries && "no line entries for file ID");
1253     Entries.clear();
1254     Entries.reserve(NumEntries);
1255     for (unsigned I = 0; I != NumEntries; ++I) {
1256       unsigned FileOffset = Record[Idx++];
1257       unsigned LineNo = Record[Idx++];
1258       int FilenameID = FileIDs[Record[Idx++]];
1259       SrcMgr::CharacteristicKind FileKind
1260         = (SrcMgr::CharacteristicKind)Record[Idx++];
1261       unsigned IncludeOffset = Record[Idx++];
1262       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1263                                        FileKind, IncludeOffset));
1264     }
1265     LineTable.AddEntry(FileID::get(FID), Entries);
1266   }
1267 
1268   return false;
1269 }
1270 
1271 /// Read a source manager block
1272 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1273   using namespace SrcMgr;
1274 
1275   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1276 
1277   // Set the source-location entry cursor to the current position in
1278   // the stream. This cursor will be used to read the contents of the
1279   // source manager block initially, and then lazily read
1280   // source-location entries as needed.
1281   SLocEntryCursor = F.Stream;
1282 
1283   // The stream itself is going to skip over the source manager block.
1284   if (F.Stream.SkipBlock()) {
1285     Error("malformed block record in AST file");
1286     return true;
1287   }
1288 
1289   // Enter the source manager block.
1290   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1291     Error("malformed source manager block record in AST file");
1292     return true;
1293   }
1294 
1295   RecordData Record;
1296   while (true) {
1297     llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1298 
1299     switch (E.Kind) {
1300     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1301     case llvm::BitstreamEntry::Error:
1302       Error("malformed block record in AST file");
1303       return true;
1304     case llvm::BitstreamEntry::EndBlock:
1305       return false;
1306     case llvm::BitstreamEntry::Record:
1307       // The interesting case.
1308       break;
1309     }
1310 
1311     // Read a record.
1312     Record.clear();
1313     StringRef Blob;
1314     switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
1315     default:  // Default behavior: ignore.
1316       break;
1317 
1318     case SM_SLOC_FILE_ENTRY:
1319     case SM_SLOC_BUFFER_ENTRY:
1320     case SM_SLOC_EXPANSION_ENTRY:
1321       // Once we hit one of the source location entries, we're done.
1322       return false;
1323     }
1324   }
1325 }
1326 
1327 /// If a header file is not found at the path that we expect it to be
1328 /// and the PCH file was moved from its original location, try to resolve the
1329 /// file by assuming that header+PCH were moved together and the header is in
1330 /// the same place relative to the PCH.
1331 static std::string
1332 resolveFileRelativeToOriginalDir(const std::string &Filename,
1333                                  const std::string &OriginalDir,
1334                                  const std::string &CurrDir) {
1335   assert(OriginalDir != CurrDir &&
1336          "No point trying to resolve the file if the PCH dir didn't change");
1337 
1338   using namespace llvm::sys;
1339 
1340   SmallString<128> filePath(Filename);
1341   fs::make_absolute(filePath);
1342   assert(path::is_absolute(OriginalDir));
1343   SmallString<128> currPCHPath(CurrDir);
1344 
1345   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1346                        fileDirE = path::end(path::parent_path(filePath));
1347   path::const_iterator origDirI = path::begin(OriginalDir),
1348                        origDirE = path::end(OriginalDir);
1349   // Skip the common path components from filePath and OriginalDir.
1350   while (fileDirI != fileDirE && origDirI != origDirE &&
1351          *fileDirI == *origDirI) {
1352     ++fileDirI;
1353     ++origDirI;
1354   }
1355   for (; origDirI != origDirE; ++origDirI)
1356     path::append(currPCHPath, "..");
1357   path::append(currPCHPath, fileDirI, fileDirE);
1358   path::append(currPCHPath, path::filename(Filename));
1359   return currPCHPath.str();
1360 }
1361 
1362 bool ASTReader::ReadSLocEntry(int ID) {
1363   if (ID == 0)
1364     return false;
1365 
1366   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1367     Error("source location entry ID out-of-range for AST file");
1368     return true;
1369   }
1370 
1371   // Local helper to read the (possibly-compressed) buffer data following the
1372   // entry record.
1373   auto ReadBuffer = [this](
1374       BitstreamCursor &SLocEntryCursor,
1375       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1376     RecordData Record;
1377     StringRef Blob;
1378     unsigned Code = SLocEntryCursor.ReadCode();
1379     unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1380 
1381     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1382       if (!llvm::zlib::isAvailable()) {
1383         Error("zlib is not available");
1384         return nullptr;
1385       }
1386       SmallString<0> Uncompressed;
1387       if (llvm::Error E =
1388               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1389         Error("could not decompress embedded file contents: " +
1390               llvm::toString(std::move(E)));
1391         return nullptr;
1392       }
1393       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1394     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1395       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1396     } else {
1397       Error("AST record has invalid code");
1398       return nullptr;
1399     }
1400   };
1401 
1402   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1403   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1404   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1405   unsigned BaseOffset = F->SLocEntryBaseOffset;
1406 
1407   ++NumSLocEntriesRead;
1408   llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1409   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1410     Error("incorrectly-formatted source location entry in AST file");
1411     return true;
1412   }
1413 
1414   RecordData Record;
1415   StringRef Blob;
1416   switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
1417   default:
1418     Error("incorrectly-formatted source location entry in AST file");
1419     return true;
1420 
1421   case SM_SLOC_FILE_ENTRY: {
1422     // We will detect whether a file changed and return 'Failure' for it, but
1423     // we will also try to fail gracefully by setting up the SLocEntry.
1424     unsigned InputID = Record[4];
1425     InputFile IF = getInputFile(*F, InputID);
1426     const FileEntry *File = IF.getFile();
1427     bool OverriddenBuffer = IF.isOverridden();
1428 
1429     // Note that we only check if a File was returned. If it was out-of-date
1430     // we have complained but we will continue creating a FileID to recover
1431     // gracefully.
1432     if (!File)
1433       return true;
1434 
1435     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1436     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1437       // This is the module's main file.
1438       IncludeLoc = getImportLocation(F);
1439     }
1440     SrcMgr::CharacteristicKind
1441       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1442     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1443                                         ID, BaseOffset + Record[0]);
1444     SrcMgr::FileInfo &FileInfo =
1445           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1446     FileInfo.NumCreatedFIDs = Record[5];
1447     if (Record[3])
1448       FileInfo.setHasLineDirectives();
1449 
1450     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1451     unsigned NumFileDecls = Record[7];
1452     if (NumFileDecls && ContextObj) {
1453       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1454       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1455                                                              NumFileDecls));
1456     }
1457 
1458     const SrcMgr::ContentCache *ContentCache
1459       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1460     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1461         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1462         !ContentCache->getRawBuffer()) {
1463       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1464       if (!Buffer)
1465         return true;
1466       SourceMgr.overrideFileContents(File, std::move(Buffer));
1467     }
1468 
1469     break;
1470   }
1471 
1472   case SM_SLOC_BUFFER_ENTRY: {
1473     const char *Name = Blob.data();
1474     unsigned Offset = Record[0];
1475     SrcMgr::CharacteristicKind
1476       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1477     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1478     if (IncludeLoc.isInvalid() && F->isModule()) {
1479       IncludeLoc = getImportLocation(F);
1480     }
1481 
1482     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1483     if (!Buffer)
1484       return true;
1485     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1486                            BaseOffset + Offset, IncludeLoc);
1487     break;
1488   }
1489 
1490   case SM_SLOC_EXPANSION_ENTRY: {
1491     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1492     SourceMgr.createExpansionLoc(SpellingLoc,
1493                                      ReadSourceLocation(*F, Record[2]),
1494                                      ReadSourceLocation(*F, Record[3]),
1495                                      Record[5],
1496                                      Record[4],
1497                                      ID,
1498                                      BaseOffset + Record[0]);
1499     break;
1500   }
1501   }
1502 
1503   return false;
1504 }
1505 
1506 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1507   if (ID == 0)
1508     return std::make_pair(SourceLocation(), "");
1509 
1510   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1511     Error("source location entry ID out-of-range for AST file");
1512     return std::make_pair(SourceLocation(), "");
1513   }
1514 
1515   // Find which module file this entry lands in.
1516   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1517   if (!M->isModule())
1518     return std::make_pair(SourceLocation(), "");
1519 
1520   // FIXME: Can we map this down to a particular submodule? That would be
1521   // ideal.
1522   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1523 }
1524 
1525 /// Find the location where the module F is imported.
1526 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1527   if (F->ImportLoc.isValid())
1528     return F->ImportLoc;
1529 
1530   // Otherwise we have a PCH. It's considered to be "imported" at the first
1531   // location of its includer.
1532   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1533     // Main file is the importer.
1534     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1535     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1536   }
1537   return F->ImportedBy[0]->FirstLoc;
1538 }
1539 
1540 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1541 /// specified cursor.  Read the abbreviations that are at the top of the block
1542 /// and then leave the cursor pointing into the block.
1543 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1544   if (Cursor.EnterSubBlock(BlockID))
1545     return true;
1546 
1547   while (true) {
1548     uint64_t Offset = Cursor.GetCurrentBitNo();
1549     unsigned Code = Cursor.ReadCode();
1550 
1551     // We expect all abbrevs to be at the start of the block.
1552     if (Code != llvm::bitc::DEFINE_ABBREV) {
1553       Cursor.JumpToBit(Offset);
1554       return false;
1555     }
1556     Cursor.ReadAbbrevRecord();
1557   }
1558 }
1559 
1560 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1561                            unsigned &Idx) {
1562   Token Tok;
1563   Tok.startToken();
1564   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1565   Tok.setLength(Record[Idx++]);
1566   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1567     Tok.setIdentifierInfo(II);
1568   Tok.setKind((tok::TokenKind)Record[Idx++]);
1569   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1570   return Tok;
1571 }
1572 
1573 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1574   BitstreamCursor &Stream = F.MacroCursor;
1575 
1576   // Keep track of where we are in the stream, then jump back there
1577   // after reading this macro.
1578   SavedStreamPosition SavedPosition(Stream);
1579 
1580   Stream.JumpToBit(Offset);
1581   RecordData Record;
1582   SmallVector<IdentifierInfo*, 16> MacroParams;
1583   MacroInfo *Macro = nullptr;
1584 
1585   while (true) {
1586     // Advance to the next record, but if we get to the end of the block, don't
1587     // pop it (removing all the abbreviations from the cursor) since we want to
1588     // be able to reseek within the block and read entries.
1589     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1590     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1591 
1592     switch (Entry.Kind) {
1593     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1594     case llvm::BitstreamEntry::Error:
1595       Error("malformed block record in AST file");
1596       return Macro;
1597     case llvm::BitstreamEntry::EndBlock:
1598       return Macro;
1599     case llvm::BitstreamEntry::Record:
1600       // The interesting case.
1601       break;
1602     }
1603 
1604     // Read a record.
1605     Record.clear();
1606     PreprocessorRecordTypes RecType =
1607       (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1608     switch (RecType) {
1609     case PP_MODULE_MACRO:
1610     case PP_MACRO_DIRECTIVE_HISTORY:
1611       return Macro;
1612 
1613     case PP_MACRO_OBJECT_LIKE:
1614     case PP_MACRO_FUNCTION_LIKE: {
1615       // If we already have a macro, that means that we've hit the end
1616       // of the definition of the macro we were looking for. We're
1617       // done.
1618       if (Macro)
1619         return Macro;
1620 
1621       unsigned NextIndex = 1; // Skip identifier ID.
1622       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1623       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1624       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1625       MI->setIsUsed(Record[NextIndex++]);
1626       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1627 
1628       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1629         // Decode function-like macro info.
1630         bool isC99VarArgs = Record[NextIndex++];
1631         bool isGNUVarArgs = Record[NextIndex++];
1632         bool hasCommaPasting = Record[NextIndex++];
1633         MacroParams.clear();
1634         unsigned NumArgs = Record[NextIndex++];
1635         for (unsigned i = 0; i != NumArgs; ++i)
1636           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1637 
1638         // Install function-like macro info.
1639         MI->setIsFunctionLike();
1640         if (isC99VarArgs) MI->setIsC99Varargs();
1641         if (isGNUVarArgs) MI->setIsGNUVarargs();
1642         if (hasCommaPasting) MI->setHasCommaPasting();
1643         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1644       }
1645 
1646       // Remember that we saw this macro last so that we add the tokens that
1647       // form its body to it.
1648       Macro = MI;
1649 
1650       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1651           Record[NextIndex]) {
1652         // We have a macro definition. Register the association
1653         PreprocessedEntityID
1654             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1655         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1656         PreprocessingRecord::PPEntityID PPID =
1657             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1658         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1659             PPRec.getPreprocessedEntity(PPID));
1660         if (PPDef)
1661           PPRec.RegisterMacroDefinition(Macro, PPDef);
1662       }
1663 
1664       ++NumMacrosRead;
1665       break;
1666     }
1667 
1668     case PP_TOKEN: {
1669       // If we see a TOKEN before a PP_MACRO_*, then the file is
1670       // erroneous, just pretend we didn't see this.
1671       if (!Macro) break;
1672 
1673       unsigned Idx = 0;
1674       Token Tok = ReadToken(F, Record, Idx);
1675       Macro->AddTokenToBody(Tok);
1676       break;
1677     }
1678     }
1679   }
1680 }
1681 
1682 PreprocessedEntityID
1683 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1684                                          unsigned LocalID) const {
1685   if (!M.ModuleOffsetMap.empty())
1686     ReadModuleOffsetMap(M);
1687 
1688   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1689     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1690   assert(I != M.PreprocessedEntityRemap.end()
1691          && "Invalid index into preprocessed entity index remap");
1692 
1693   return LocalID + I->second;
1694 }
1695 
1696 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1697   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1698 }
1699 
1700 HeaderFileInfoTrait::internal_key_type
1701 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1702   internal_key_type ikey = {FE->getSize(),
1703                             M.HasTimestamps ? FE->getModificationTime() : 0,
1704                             FE->getName(), /*Imported*/ false};
1705   return ikey;
1706 }
1707 
1708 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1709   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1710     return false;
1711 
1712   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1713     return true;
1714 
1715   // Determine whether the actual files are equivalent.
1716   FileManager &FileMgr = Reader.getFileManager();
1717   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1718     if (!Key.Imported)
1719       return FileMgr.getFile(Key.Filename);
1720 
1721     std::string Resolved = Key.Filename;
1722     Reader.ResolveImportedPath(M, Resolved);
1723     return FileMgr.getFile(Resolved);
1724   };
1725 
1726   const FileEntry *FEA = GetFile(a);
1727   const FileEntry *FEB = GetFile(b);
1728   return FEA && FEA == FEB;
1729 }
1730 
1731 std::pair<unsigned, unsigned>
1732 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1733   using namespace llvm::support;
1734 
1735   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1736   unsigned DataLen = (unsigned) *d++;
1737   return std::make_pair(KeyLen, DataLen);
1738 }
1739 
1740 HeaderFileInfoTrait::internal_key_type
1741 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1742   using namespace llvm::support;
1743 
1744   internal_key_type ikey;
1745   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1746   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1747   ikey.Filename = (const char *)d;
1748   ikey.Imported = true;
1749   return ikey;
1750 }
1751 
1752 HeaderFileInfoTrait::data_type
1753 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1754                               unsigned DataLen) {
1755   using namespace llvm::support;
1756 
1757   const unsigned char *End = d + DataLen;
1758   HeaderFileInfo HFI;
1759   unsigned Flags = *d++;
1760   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1761   HFI.isImport |= (Flags >> 5) & 0x01;
1762   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1763   HFI.DirInfo = (Flags >> 1) & 0x07;
1764   HFI.IndexHeaderMapHeader = Flags & 0x01;
1765   // FIXME: Find a better way to handle this. Maybe just store a
1766   // "has been included" flag?
1767   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1768                              HFI.NumIncludes);
1769   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1770       M, endian::readNext<uint32_t, little, unaligned>(d));
1771   if (unsigned FrameworkOffset =
1772           endian::readNext<uint32_t, little, unaligned>(d)) {
1773     // The framework offset is 1 greater than the actual offset,
1774     // since 0 is used as an indicator for "no framework name".
1775     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1776     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1777   }
1778 
1779   assert((End - d) % 4 == 0 &&
1780          "Wrong data length in HeaderFileInfo deserialization");
1781   while (d != End) {
1782     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1783     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1784     LocalSMID >>= 2;
1785 
1786     // This header is part of a module. Associate it with the module to enable
1787     // implicit module import.
1788     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1789     Module *Mod = Reader.getSubmodule(GlobalSMID);
1790     FileManager &FileMgr = Reader.getFileManager();
1791     ModuleMap &ModMap =
1792         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1793 
1794     std::string Filename = key.Filename;
1795     if (key.Imported)
1796       Reader.ResolveImportedPath(M, Filename);
1797     // FIXME: This is not always the right filename-as-written, but we're not
1798     // going to use this information to rebuild the module, so it doesn't make
1799     // a lot of difference.
1800     Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
1801     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1802     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1803   }
1804 
1805   // This HeaderFileInfo was externally loaded.
1806   HFI.External = true;
1807   HFI.IsValid = true;
1808   return HFI;
1809 }
1810 
1811 void ASTReader::addPendingMacro(IdentifierInfo *II,
1812                                 ModuleFile *M,
1813                                 uint64_t MacroDirectivesOffset) {
1814   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1815   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1816 }
1817 
1818 void ASTReader::ReadDefinedMacros() {
1819   // Note that we are loading defined macros.
1820   Deserializing Macros(this);
1821 
1822   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1823     BitstreamCursor &MacroCursor = I.MacroCursor;
1824 
1825     // If there was no preprocessor block, skip this file.
1826     if (MacroCursor.getBitcodeBytes().empty())
1827       continue;
1828 
1829     BitstreamCursor Cursor = MacroCursor;
1830     Cursor.JumpToBit(I.MacroStartOffset);
1831 
1832     RecordData Record;
1833     while (true) {
1834       llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1835 
1836       switch (E.Kind) {
1837       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1838       case llvm::BitstreamEntry::Error:
1839         Error("malformed block record in AST file");
1840         return;
1841       case llvm::BitstreamEntry::EndBlock:
1842         goto NextCursor;
1843 
1844       case llvm::BitstreamEntry::Record:
1845         Record.clear();
1846         switch (Cursor.readRecord(E.ID, Record)) {
1847         default:  // Default behavior: ignore.
1848           break;
1849 
1850         case PP_MACRO_OBJECT_LIKE:
1851         case PP_MACRO_FUNCTION_LIKE: {
1852           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1853           if (II->isOutOfDate())
1854             updateOutOfDateIdentifier(*II);
1855           break;
1856         }
1857 
1858         case PP_TOKEN:
1859           // Ignore tokens.
1860           break;
1861         }
1862         break;
1863       }
1864     }
1865     NextCursor:  ;
1866   }
1867 }
1868 
1869 namespace {
1870 
1871   /// Visitor class used to look up identifirs in an AST file.
1872   class IdentifierLookupVisitor {
1873     StringRef Name;
1874     unsigned NameHash;
1875     unsigned PriorGeneration;
1876     unsigned &NumIdentifierLookups;
1877     unsigned &NumIdentifierLookupHits;
1878     IdentifierInfo *Found = nullptr;
1879 
1880   public:
1881     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1882                             unsigned &NumIdentifierLookups,
1883                             unsigned &NumIdentifierLookupHits)
1884       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1885         PriorGeneration(PriorGeneration),
1886         NumIdentifierLookups(NumIdentifierLookups),
1887         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
1888 
1889     bool operator()(ModuleFile &M) {
1890       // If we've already searched this module file, skip it now.
1891       if (M.Generation <= PriorGeneration)
1892         return true;
1893 
1894       ASTIdentifierLookupTable *IdTable
1895         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1896       if (!IdTable)
1897         return false;
1898 
1899       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
1900                                      Found);
1901       ++NumIdentifierLookups;
1902       ASTIdentifierLookupTable::iterator Pos =
1903           IdTable->find_hashed(Name, NameHash, &Trait);
1904       if (Pos == IdTable->end())
1905         return false;
1906 
1907       // Dereferencing the iterator has the effect of building the
1908       // IdentifierInfo node and populating it with the various
1909       // declarations it needs.
1910       ++NumIdentifierLookupHits;
1911       Found = *Pos;
1912       return true;
1913     }
1914 
1915     // Retrieve the identifier info found within the module
1916     // files.
1917     IdentifierInfo *getIdentifierInfo() const { return Found; }
1918   };
1919 
1920 } // namespace
1921 
1922 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1923   // Note that we are loading an identifier.
1924   Deserializing AnIdentifier(this);
1925 
1926   unsigned PriorGeneration = 0;
1927   if (getContext().getLangOpts().Modules)
1928     PriorGeneration = IdentifierGeneration[&II];
1929 
1930   // If there is a global index, look there first to determine which modules
1931   // provably do not have any results for this identifier.
1932   GlobalModuleIndex::HitSet Hits;
1933   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
1934   if (!loadGlobalIndex()) {
1935     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1936       HitsPtr = &Hits;
1937     }
1938   }
1939 
1940   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1941                                   NumIdentifierLookups,
1942                                   NumIdentifierLookupHits);
1943   ModuleMgr.visit(Visitor, HitsPtr);
1944   markIdentifierUpToDate(&II);
1945 }
1946 
1947 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1948   if (!II)
1949     return;
1950 
1951   II->setOutOfDate(false);
1952 
1953   // Update the generation for this identifier.
1954   if (getContext().getLangOpts().Modules)
1955     IdentifierGeneration[II] = getGeneration();
1956 }
1957 
1958 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1959                                     const PendingMacroInfo &PMInfo) {
1960   ModuleFile &M = *PMInfo.M;
1961 
1962   BitstreamCursor &Cursor = M.MacroCursor;
1963   SavedStreamPosition SavedPosition(Cursor);
1964   Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
1965 
1966   struct ModuleMacroRecord {
1967     SubmoduleID SubModID;
1968     MacroInfo *MI;
1969     SmallVector<SubmoduleID, 8> Overrides;
1970   };
1971   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
1972 
1973   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1974   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1975   // macro histroy.
1976   RecordData Record;
1977   while (true) {
1978     llvm::BitstreamEntry Entry =
1979         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1980     if (Entry.Kind != llvm::BitstreamEntry::Record) {
1981       Error("malformed block record in AST file");
1982       return;
1983     }
1984 
1985     Record.clear();
1986     switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
1987     case PP_MACRO_DIRECTIVE_HISTORY:
1988       break;
1989 
1990     case PP_MODULE_MACRO: {
1991       ModuleMacros.push_back(ModuleMacroRecord());
1992       auto &Info = ModuleMacros.back();
1993       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1994       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
1995       for (int I = 2, N = Record.size(); I != N; ++I)
1996         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
1997       continue;
1998     }
1999 
2000     default:
2001       Error("malformed block record in AST file");
2002       return;
2003     }
2004 
2005     // We found the macro directive history; that's the last record
2006     // for this macro.
2007     break;
2008   }
2009 
2010   // Module macros are listed in reverse dependency order.
2011   {
2012     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2013     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2014     for (auto &MMR : ModuleMacros) {
2015       Overrides.clear();
2016       for (unsigned ModID : MMR.Overrides) {
2017         Module *Mod = getSubmodule(ModID);
2018         auto *Macro = PP.getModuleMacro(Mod, II);
2019         assert(Macro && "missing definition for overridden macro");
2020         Overrides.push_back(Macro);
2021       }
2022 
2023       bool Inserted = false;
2024       Module *Owner = getSubmodule(MMR.SubModID);
2025       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2026     }
2027   }
2028 
2029   // Don't read the directive history for a module; we don't have anywhere
2030   // to put it.
2031   if (M.isModule())
2032     return;
2033 
2034   // Deserialize the macro directives history in reverse source-order.
2035   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2036   unsigned Idx = 0, N = Record.size();
2037   while (Idx < N) {
2038     MacroDirective *MD = nullptr;
2039     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2040     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2041     switch (K) {
2042     case MacroDirective::MD_Define: {
2043       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2044       MD = PP.AllocateDefMacroDirective(MI, Loc);
2045       break;
2046     }
2047     case MacroDirective::MD_Undefine:
2048       MD = PP.AllocateUndefMacroDirective(Loc);
2049       break;
2050     case MacroDirective::MD_Visibility:
2051       bool isPublic = Record[Idx++];
2052       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2053       break;
2054     }
2055 
2056     if (!Latest)
2057       Latest = MD;
2058     if (Earliest)
2059       Earliest->setPrevious(MD);
2060     Earliest = MD;
2061   }
2062 
2063   if (Latest)
2064     PP.setLoadedMacroDirective(II, Earliest, Latest);
2065 }
2066 
2067 ASTReader::InputFileInfo
2068 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2069   // Go find this input file.
2070   BitstreamCursor &Cursor = F.InputFilesCursor;
2071   SavedStreamPosition SavedPosition(Cursor);
2072   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2073 
2074   unsigned Code = Cursor.ReadCode();
2075   RecordData Record;
2076   StringRef Blob;
2077 
2078   unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2079   assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2080          "invalid record type for input file");
2081   (void)Result;
2082 
2083   assert(Record[0] == ID && "Bogus stored ID or offset");
2084   InputFileInfo R;
2085   R.StoredSize = static_cast<off_t>(Record[1]);
2086   R.StoredTime = static_cast<time_t>(Record[2]);
2087   R.Overridden = static_cast<bool>(Record[3]);
2088   R.Transient = static_cast<bool>(Record[4]);
2089   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2090   R.Filename = Blob;
2091   ResolveImportedPath(F, R.Filename);
2092   return R;
2093 }
2094 
2095 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2096 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2097   // If this ID is bogus, just return an empty input file.
2098   if (ID == 0 || ID > F.InputFilesLoaded.size())
2099     return InputFile();
2100 
2101   // If we've already loaded this input file, return it.
2102   if (F.InputFilesLoaded[ID-1].getFile())
2103     return F.InputFilesLoaded[ID-1];
2104 
2105   if (F.InputFilesLoaded[ID-1].isNotFound())
2106     return InputFile();
2107 
2108   // Go find this input file.
2109   BitstreamCursor &Cursor = F.InputFilesCursor;
2110   SavedStreamPosition SavedPosition(Cursor);
2111   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2112 
2113   InputFileInfo FI = readInputFileInfo(F, ID);
2114   off_t StoredSize = FI.StoredSize;
2115   time_t StoredTime = FI.StoredTime;
2116   bool Overridden = FI.Overridden;
2117   bool Transient = FI.Transient;
2118   StringRef Filename = FI.Filename;
2119 
2120   const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
2121   // If we didn't find the file, resolve it relative to the
2122   // original directory from which this AST file was created.
2123   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2124       F.OriginalDir != F.BaseDirectory) {
2125     std::string Resolved = resolveFileRelativeToOriginalDir(
2126         Filename, F.OriginalDir, F.BaseDirectory);
2127     if (!Resolved.empty())
2128       File = FileMgr.getFile(Resolved);
2129   }
2130 
2131   // For an overridden file, create a virtual file with the stored
2132   // size/timestamp.
2133   if ((Overridden || Transient) && File == nullptr)
2134     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2135 
2136   if (File == nullptr) {
2137     if (Complain) {
2138       std::string ErrorStr = "could not find file '";
2139       ErrorStr += Filename;
2140       ErrorStr += "' referenced by AST file '";
2141       ErrorStr += F.FileName;
2142       ErrorStr += "'";
2143       Error(ErrorStr);
2144     }
2145     // Record that we didn't find the file.
2146     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2147     return InputFile();
2148   }
2149 
2150   // Check if there was a request to override the contents of the file
2151   // that was part of the precompiled header. Overriding such a file
2152   // can lead to problems when lexing using the source locations from the
2153   // PCH.
2154   SourceManager &SM = getSourceManager();
2155   // FIXME: Reject if the overrides are different.
2156   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2157     if (Complain)
2158       Error(diag::err_fe_pch_file_overridden, Filename);
2159     // After emitting the diagnostic, recover by disabling the override so
2160     // that the original file will be used.
2161     //
2162     // FIXME: This recovery is just as broken as the original state; there may
2163     // be another precompiled module that's using the overridden contents, or
2164     // we might be half way through parsing it. Instead, we should treat the
2165     // overridden contents as belonging to a separate FileEntry.
2166     SM.disableFileContentsOverride(File);
2167     // The FileEntry is a virtual file entry with the size of the contents
2168     // that would override the original contents. Set it to the original's
2169     // size/time.
2170     FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2171                             StoredSize, StoredTime);
2172   }
2173 
2174   bool IsOutOfDate = false;
2175 
2176   // For an overridden file, there is nothing to validate.
2177   if (!Overridden && //
2178       (StoredSize != File->getSize() ||
2179        (StoredTime && StoredTime != File->getModificationTime() &&
2180         !DisableValidation)
2181        )) {
2182     if (Complain) {
2183       // Build a list of the PCH imports that got us here (in reverse).
2184       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2185       while (!ImportStack.back()->ImportedBy.empty())
2186         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2187 
2188       // The top-level PCH is stale.
2189       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2190       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2191       if (DiagnosticKind == 0)
2192         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2193       else if (DiagnosticKind == 1)
2194         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2195       else
2196         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2197 
2198       // Print the import stack.
2199       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2200         Diag(diag::note_pch_required_by)
2201           << Filename << ImportStack[0]->FileName;
2202         for (unsigned I = 1; I < ImportStack.size(); ++I)
2203           Diag(diag::note_pch_required_by)
2204             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2205       }
2206 
2207       if (!Diags.isDiagnosticInFlight())
2208         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2209     }
2210 
2211     IsOutOfDate = true;
2212   }
2213   // FIXME: If the file is overridden and we've already opened it,
2214   // issue an error (or split it into a separate FileEntry).
2215 
2216   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2217 
2218   // Note that we've loaded this input file.
2219   F.InputFilesLoaded[ID-1] = IF;
2220   return IF;
2221 }
2222 
2223 /// If we are loading a relocatable PCH or module file, and the filename
2224 /// is not an absolute path, add the system or module root to the beginning of
2225 /// the file name.
2226 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2227   // Resolve relative to the base directory, if we have one.
2228   if (!M.BaseDirectory.empty())
2229     return ResolveImportedPath(Filename, M.BaseDirectory);
2230 }
2231 
2232 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2233   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2234     return;
2235 
2236   SmallString<128> Buffer;
2237   llvm::sys::path::append(Buffer, Prefix, Filename);
2238   Filename.assign(Buffer.begin(), Buffer.end());
2239 }
2240 
2241 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2242   switch (ARR) {
2243   case ASTReader::Failure: return true;
2244   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2245   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2246   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2247   case ASTReader::ConfigurationMismatch:
2248     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2249   case ASTReader::HadErrors: return true;
2250   case ASTReader::Success: return false;
2251   }
2252 
2253   llvm_unreachable("unknown ASTReadResult");
2254 }
2255 
2256 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2257     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2258     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2259     std::string &SuggestedPredefines) {
2260   if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2261     return Failure;
2262 
2263   // Read all of the records in the options block.
2264   RecordData Record;
2265   ASTReadResult Result = Success;
2266   while (true) {
2267     llvm::BitstreamEntry Entry = Stream.advance();
2268 
2269     switch (Entry.Kind) {
2270     case llvm::BitstreamEntry::Error:
2271     case llvm::BitstreamEntry::SubBlock:
2272       return Failure;
2273 
2274     case llvm::BitstreamEntry::EndBlock:
2275       return Result;
2276 
2277     case llvm::BitstreamEntry::Record:
2278       // The interesting case.
2279       break;
2280     }
2281 
2282     // Read and process a record.
2283     Record.clear();
2284     switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2285     case LANGUAGE_OPTIONS: {
2286       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2287       if (ParseLanguageOptions(Record, Complain, Listener,
2288                                AllowCompatibleConfigurationMismatch))
2289         Result = ConfigurationMismatch;
2290       break;
2291     }
2292 
2293     case TARGET_OPTIONS: {
2294       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2295       if (ParseTargetOptions(Record, Complain, Listener,
2296                              AllowCompatibleConfigurationMismatch))
2297         Result = ConfigurationMismatch;
2298       break;
2299     }
2300 
2301     case FILE_SYSTEM_OPTIONS: {
2302       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2303       if (!AllowCompatibleConfigurationMismatch &&
2304           ParseFileSystemOptions(Record, Complain, Listener))
2305         Result = ConfigurationMismatch;
2306       break;
2307     }
2308 
2309     case HEADER_SEARCH_OPTIONS: {
2310       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2311       if (!AllowCompatibleConfigurationMismatch &&
2312           ParseHeaderSearchOptions(Record, Complain, Listener))
2313         Result = ConfigurationMismatch;
2314       break;
2315     }
2316 
2317     case PREPROCESSOR_OPTIONS:
2318       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2319       if (!AllowCompatibleConfigurationMismatch &&
2320           ParsePreprocessorOptions(Record, Complain, Listener,
2321                                    SuggestedPredefines))
2322         Result = ConfigurationMismatch;
2323       break;
2324     }
2325   }
2326 }
2327 
2328 ASTReader::ASTReadResult
2329 ASTReader::ReadControlBlock(ModuleFile &F,
2330                             SmallVectorImpl<ImportedModule> &Loaded,
2331                             const ModuleFile *ImportedBy,
2332                             unsigned ClientLoadCapabilities) {
2333   BitstreamCursor &Stream = F.Stream;
2334   ASTReadResult Result = Success;
2335 
2336   if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2337     Error("malformed block record in AST file");
2338     return Failure;
2339   }
2340 
2341   // Lambda to read the unhashed control block the first time it's called.
2342   //
2343   // For PCM files, the unhashed control block cannot be read until after the
2344   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2345   // need to look ahead before reading the IMPORTS record.  For consistency,
2346   // this block is always read somehow (see BitstreamEntry::EndBlock).
2347   bool HasReadUnhashedControlBlock = false;
2348   auto readUnhashedControlBlockOnce = [&]() {
2349     if (!HasReadUnhashedControlBlock) {
2350       HasReadUnhashedControlBlock = true;
2351       if (ASTReadResult Result =
2352               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2353         return Result;
2354     }
2355     return Success;
2356   };
2357 
2358   // Read all of the records and blocks in the control block.
2359   RecordData Record;
2360   unsigned NumInputs = 0;
2361   unsigned NumUserInputs = 0;
2362   while (true) {
2363     llvm::BitstreamEntry Entry = Stream.advance();
2364 
2365     switch (Entry.Kind) {
2366     case llvm::BitstreamEntry::Error:
2367       Error("malformed block record in AST file");
2368       return Failure;
2369     case llvm::BitstreamEntry::EndBlock: {
2370       // Validate the module before returning.  This call catches an AST with
2371       // no module name and no imports.
2372       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2373         return Result;
2374 
2375       // Validate input files.
2376       const HeaderSearchOptions &HSOpts =
2377           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2378 
2379       // All user input files reside at the index range [0, NumUserInputs), and
2380       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2381       // loaded module files, ignore missing inputs.
2382       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2383           F.Kind != MK_PrebuiltModule) {
2384         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2385 
2386         // If we are reading a module, we will create a verification timestamp,
2387         // so we verify all input files.  Otherwise, verify only user input
2388         // files.
2389 
2390         unsigned N = NumUserInputs;
2391         if (ValidateSystemInputs ||
2392             (HSOpts.ModulesValidateOncePerBuildSession &&
2393              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2394              F.Kind == MK_ImplicitModule))
2395           N = NumInputs;
2396 
2397         for (unsigned I = 0; I < N; ++I) {
2398           InputFile IF = getInputFile(F, I+1, Complain);
2399           if (!IF.getFile() || IF.isOutOfDate())
2400             return OutOfDate;
2401         }
2402       }
2403 
2404       if (Listener)
2405         Listener->visitModuleFile(F.FileName, F.Kind);
2406 
2407       if (Listener && Listener->needsInputFileVisitation()) {
2408         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2409                                                                 : NumUserInputs;
2410         for (unsigned I = 0; I < N; ++I) {
2411           bool IsSystem = I >= NumUserInputs;
2412           InputFileInfo FI = readInputFileInfo(F, I+1);
2413           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2414                                    F.Kind == MK_ExplicitModule ||
2415                                    F.Kind == MK_PrebuiltModule);
2416         }
2417       }
2418 
2419       return Result;
2420     }
2421 
2422     case llvm::BitstreamEntry::SubBlock:
2423       switch (Entry.ID) {
2424       case INPUT_FILES_BLOCK_ID:
2425         F.InputFilesCursor = Stream;
2426         if (Stream.SkipBlock() || // Skip with the main cursor
2427             // Read the abbreviations
2428             ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2429           Error("malformed block record in AST file");
2430           return Failure;
2431         }
2432         continue;
2433 
2434       case OPTIONS_BLOCK_ID:
2435         // If we're reading the first module for this group, check its options
2436         // are compatible with ours. For modules it imports, no further checking
2437         // is required, because we checked them when we built it.
2438         if (Listener && !ImportedBy) {
2439           // Should we allow the configuration of the module file to differ from
2440           // the configuration of the current translation unit in a compatible
2441           // way?
2442           //
2443           // FIXME: Allow this for files explicitly specified with -include-pch.
2444           bool AllowCompatibleConfigurationMismatch =
2445               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2446 
2447           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2448                                     AllowCompatibleConfigurationMismatch,
2449                                     *Listener, SuggestedPredefines);
2450           if (Result == Failure) {
2451             Error("malformed block record in AST file");
2452             return Result;
2453           }
2454 
2455           if (DisableValidation ||
2456               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2457             Result = Success;
2458 
2459           // If we can't load the module, exit early since we likely
2460           // will rebuild the module anyway. The stream may be in the
2461           // middle of a block.
2462           if (Result != Success)
2463             return Result;
2464         } else if (Stream.SkipBlock()) {
2465           Error("malformed block record in AST file");
2466           return Failure;
2467         }
2468         continue;
2469 
2470       default:
2471         if (Stream.SkipBlock()) {
2472           Error("malformed block record in AST file");
2473           return Failure;
2474         }
2475         continue;
2476       }
2477 
2478     case llvm::BitstreamEntry::Record:
2479       // The interesting case.
2480       break;
2481     }
2482 
2483     // Read and process a record.
2484     Record.clear();
2485     StringRef Blob;
2486     switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2487     case METADATA: {
2488       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2489         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2490           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2491                                         : diag::err_pch_version_too_new);
2492         return VersionMismatch;
2493       }
2494 
2495       bool hasErrors = Record[7];
2496       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2497         Diag(diag::err_pch_with_compiler_errors);
2498         return HadErrors;
2499       }
2500       if (hasErrors) {
2501         Diags.ErrorOccurred = true;
2502         Diags.UncompilableErrorOccurred = true;
2503         Diags.UnrecoverableErrorOccurred = true;
2504       }
2505 
2506       F.RelocatablePCH = Record[4];
2507       // Relative paths in a relocatable PCH are relative to our sysroot.
2508       if (F.RelocatablePCH)
2509         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2510 
2511       F.HasTimestamps = Record[5];
2512 
2513       F.PCHHasObjectFile = Record[6];
2514 
2515       const std::string &CurBranch = getClangFullRepositoryVersion();
2516       StringRef ASTBranch = Blob;
2517       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2518         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2519           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2520         return VersionMismatch;
2521       }
2522       break;
2523     }
2524 
2525     case IMPORTS: {
2526       // Validate the AST before processing any imports (otherwise, untangling
2527       // them can be error-prone and expensive).  A module will have a name and
2528       // will already have been validated, but this catches the PCH case.
2529       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2530         return Result;
2531 
2532       // Load each of the imported PCH files.
2533       unsigned Idx = 0, N = Record.size();
2534       while (Idx < N) {
2535         // Read information about the AST file.
2536         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2537         // The import location will be the local one for now; we will adjust
2538         // all import locations of module imports after the global source
2539         // location info are setup, in ReadAST.
2540         SourceLocation ImportLoc =
2541             ReadUntranslatedSourceLocation(Record[Idx++]);
2542         off_t StoredSize = (off_t)Record[Idx++];
2543         time_t StoredModTime = (time_t)Record[Idx++];
2544         ASTFileSignature StoredSignature = {
2545             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2546               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2547               (uint32_t)Record[Idx++]}}};
2548 
2549         std::string ImportedName = ReadString(Record, Idx);
2550         std::string ImportedFile;
2551 
2552         // For prebuilt and explicit modules first consult the file map for
2553         // an override. Note that here we don't search prebuilt module
2554         // directories, only the explicit name to file mappings. Also, we will
2555         // still verify the size/signature making sure it is essentially the
2556         // same file but perhaps in a different location.
2557         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2558           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2559             ImportedName, /*FileMapOnly*/ true);
2560 
2561         if (ImportedFile.empty())
2562           ImportedFile = ReadPath(F, Record, Idx);
2563         else
2564           SkipPath(Record, Idx);
2565 
2566         // If our client can't cope with us being out of date, we can't cope with
2567         // our dependency being missing.
2568         unsigned Capabilities = ClientLoadCapabilities;
2569         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2570           Capabilities &= ~ARR_Missing;
2571 
2572         // Load the AST file.
2573         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2574                                   Loaded, StoredSize, StoredModTime,
2575                                   StoredSignature, Capabilities);
2576 
2577         // If we diagnosed a problem, produce a backtrace.
2578         if (isDiagnosedResult(Result, Capabilities))
2579           Diag(diag::note_module_file_imported_by)
2580               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2581 
2582         switch (Result) {
2583         case Failure: return Failure;
2584           // If we have to ignore the dependency, we'll have to ignore this too.
2585         case Missing:
2586         case OutOfDate: return OutOfDate;
2587         case VersionMismatch: return VersionMismatch;
2588         case ConfigurationMismatch: return ConfigurationMismatch;
2589         case HadErrors: return HadErrors;
2590         case Success: break;
2591         }
2592       }
2593       break;
2594     }
2595 
2596     case ORIGINAL_FILE:
2597       F.OriginalSourceFileID = FileID::get(Record[0]);
2598       F.ActualOriginalSourceFileName = Blob;
2599       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2600       ResolveImportedPath(F, F.OriginalSourceFileName);
2601       break;
2602 
2603     case ORIGINAL_FILE_ID:
2604       F.OriginalSourceFileID = FileID::get(Record[0]);
2605       break;
2606 
2607     case ORIGINAL_PCH_DIR:
2608       F.OriginalDir = Blob;
2609       break;
2610 
2611     case MODULE_NAME:
2612       F.ModuleName = Blob;
2613       Diag(diag::remark_module_import)
2614           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2615           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2616       if (Listener)
2617         Listener->ReadModuleName(F.ModuleName);
2618 
2619       // Validate the AST as soon as we have a name so we can exit early on
2620       // failure.
2621       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2622         return Result;
2623 
2624       break;
2625 
2626     case MODULE_DIRECTORY: {
2627       assert(!F.ModuleName.empty() &&
2628              "MODULE_DIRECTORY found before MODULE_NAME");
2629       // If we've already loaded a module map file covering this module, we may
2630       // have a better path for it (relative to the current build).
2631       Module *M = PP.getHeaderSearchInfo().lookupModule(
2632           F.ModuleName, /*AllowSearch*/ true,
2633           /*AllowExtraModuleMapSearch*/ true);
2634       if (M && M->Directory) {
2635         // If we're implicitly loading a module, the base directory can't
2636         // change between the build and use.
2637         // Don't emit module relocation error if we have -fno-validate-pch
2638         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2639             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2640           const DirectoryEntry *BuildDir =
2641               PP.getFileManager().getDirectory(Blob);
2642           if (!BuildDir || BuildDir != M->Directory) {
2643             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2644               Diag(diag::err_imported_module_relocated)
2645                   << F.ModuleName << Blob << M->Directory->getName();
2646             return OutOfDate;
2647           }
2648         }
2649         F.BaseDirectory = M->Directory->getName();
2650       } else {
2651         F.BaseDirectory = Blob;
2652       }
2653       break;
2654     }
2655 
2656     case MODULE_MAP_FILE:
2657       if (ASTReadResult Result =
2658               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2659         return Result;
2660       break;
2661 
2662     case INPUT_FILE_OFFSETS:
2663       NumInputs = Record[0];
2664       NumUserInputs = Record[1];
2665       F.InputFileOffsets =
2666           (const llvm::support::unaligned_uint64_t *)Blob.data();
2667       F.InputFilesLoaded.resize(NumInputs);
2668       F.NumUserInputFiles = NumUserInputs;
2669       break;
2670     }
2671   }
2672 }
2673 
2674 ASTReader::ASTReadResult
2675 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2676   BitstreamCursor &Stream = F.Stream;
2677 
2678   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2679     Error("malformed block record in AST file");
2680     return Failure;
2681   }
2682 
2683   // Read all of the records and blocks for the AST file.
2684   RecordData Record;
2685   while (true) {
2686     llvm::BitstreamEntry Entry = Stream.advance();
2687 
2688     switch (Entry.Kind) {
2689     case llvm::BitstreamEntry::Error:
2690       Error("error at end of module block in AST file");
2691       return Failure;
2692     case llvm::BitstreamEntry::EndBlock:
2693       // Outside of C++, we do not store a lookup map for the translation unit.
2694       // Instead, mark it as needing a lookup map to be built if this module
2695       // contains any declarations lexically within it (which it always does!).
2696       // This usually has no cost, since we very rarely need the lookup map for
2697       // the translation unit outside C++.
2698       if (ASTContext *Ctx = ContextObj) {
2699         DeclContext *DC = Ctx->getTranslationUnitDecl();
2700         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2701           DC->setMustBuildLookupTable();
2702       }
2703 
2704       return Success;
2705     case llvm::BitstreamEntry::SubBlock:
2706       switch (Entry.ID) {
2707       case DECLTYPES_BLOCK_ID:
2708         // We lazily load the decls block, but we want to set up the
2709         // DeclsCursor cursor to point into it.  Clone our current bitcode
2710         // cursor to it, enter the block and read the abbrevs in that block.
2711         // With the main cursor, we just skip over it.
2712         F.DeclsCursor = Stream;
2713         if (Stream.SkipBlock() ||  // Skip with the main cursor.
2714             // Read the abbrevs.
2715             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2716           Error("malformed block record in AST file");
2717           return Failure;
2718         }
2719         break;
2720 
2721       case PREPROCESSOR_BLOCK_ID:
2722         F.MacroCursor = Stream;
2723         if (!PP.getExternalSource())
2724           PP.setExternalSource(this);
2725 
2726         if (Stream.SkipBlock() ||
2727             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2728           Error("malformed block record in AST file");
2729           return Failure;
2730         }
2731         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2732         break;
2733 
2734       case PREPROCESSOR_DETAIL_BLOCK_ID:
2735         F.PreprocessorDetailCursor = Stream;
2736         if (Stream.SkipBlock() ||
2737             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2738                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2739               Error("malformed preprocessor detail record in AST file");
2740               return Failure;
2741             }
2742         F.PreprocessorDetailStartOffset
2743         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2744 
2745         if (!PP.getPreprocessingRecord())
2746           PP.createPreprocessingRecord();
2747         if (!PP.getPreprocessingRecord()->getExternalSource())
2748           PP.getPreprocessingRecord()->SetExternalSource(*this);
2749         break;
2750 
2751       case SOURCE_MANAGER_BLOCK_ID:
2752         if (ReadSourceManagerBlock(F))
2753           return Failure;
2754         break;
2755 
2756       case SUBMODULE_BLOCK_ID:
2757         if (ASTReadResult Result =
2758                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2759           return Result;
2760         break;
2761 
2762       case COMMENTS_BLOCK_ID: {
2763         BitstreamCursor C = Stream;
2764         if (Stream.SkipBlock() ||
2765             ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2766           Error("malformed comments block in AST file");
2767           return Failure;
2768         }
2769         CommentsCursors.push_back(std::make_pair(C, &F));
2770         break;
2771       }
2772 
2773       default:
2774         if (Stream.SkipBlock()) {
2775           Error("malformed block record in AST file");
2776           return Failure;
2777         }
2778         break;
2779       }
2780       continue;
2781 
2782     case llvm::BitstreamEntry::Record:
2783       // The interesting case.
2784       break;
2785     }
2786 
2787     // Read and process a record.
2788     Record.clear();
2789     StringRef Blob;
2790     auto RecordType =
2791         (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
2792 
2793     // If we're not loading an AST context, we don't care about most records.
2794     if (!ContextObj) {
2795       switch (RecordType) {
2796       case IDENTIFIER_TABLE:
2797       case IDENTIFIER_OFFSET:
2798       case INTERESTING_IDENTIFIERS:
2799       case STATISTICS:
2800       case PP_CONDITIONAL_STACK:
2801       case PP_COUNTER_VALUE:
2802       case SOURCE_LOCATION_OFFSETS:
2803       case MODULE_OFFSET_MAP:
2804       case SOURCE_MANAGER_LINE_TABLE:
2805       case SOURCE_LOCATION_PRELOADS:
2806       case PPD_ENTITIES_OFFSETS:
2807       case HEADER_SEARCH_TABLE:
2808       case IMPORTED_MODULES:
2809       case MACRO_OFFSET:
2810         break;
2811       default:
2812         continue;
2813       }
2814     }
2815 
2816     switch (RecordType) {
2817     default:  // Default behavior: ignore.
2818       break;
2819 
2820     case TYPE_OFFSET: {
2821       if (F.LocalNumTypes != 0) {
2822         Error("duplicate TYPE_OFFSET record in AST file");
2823         return Failure;
2824       }
2825       F.TypeOffsets = (const uint32_t *)Blob.data();
2826       F.LocalNumTypes = Record[0];
2827       unsigned LocalBaseTypeIndex = Record[1];
2828       F.BaseTypeIndex = getTotalNumTypes();
2829 
2830       if (F.LocalNumTypes > 0) {
2831         // Introduce the global -> local mapping for types within this module.
2832         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2833 
2834         // Introduce the local -> global mapping for types within this module.
2835         F.TypeRemap.insertOrReplace(
2836           std::make_pair(LocalBaseTypeIndex,
2837                          F.BaseTypeIndex - LocalBaseTypeIndex));
2838 
2839         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2840       }
2841       break;
2842     }
2843 
2844     case DECL_OFFSET: {
2845       if (F.LocalNumDecls != 0) {
2846         Error("duplicate DECL_OFFSET record in AST file");
2847         return Failure;
2848       }
2849       F.DeclOffsets = (const DeclOffset *)Blob.data();
2850       F.LocalNumDecls = Record[0];
2851       unsigned LocalBaseDeclID = Record[1];
2852       F.BaseDeclID = getTotalNumDecls();
2853 
2854       if (F.LocalNumDecls > 0) {
2855         // Introduce the global -> local mapping for declarations within this
2856         // module.
2857         GlobalDeclMap.insert(
2858           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2859 
2860         // Introduce the local -> global mapping for declarations within this
2861         // module.
2862         F.DeclRemap.insertOrReplace(
2863           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2864 
2865         // Introduce the global -> local mapping for declarations within this
2866         // module.
2867         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2868 
2869         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2870       }
2871       break;
2872     }
2873 
2874     case TU_UPDATE_LEXICAL: {
2875       DeclContext *TU = ContextObj->getTranslationUnitDecl();
2876       LexicalContents Contents(
2877           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2878               Blob.data()),
2879           static_cast<unsigned int>(Blob.size() / 4));
2880       TULexicalDecls.push_back(std::make_pair(&F, Contents));
2881       TU->setHasExternalLexicalStorage(true);
2882       break;
2883     }
2884 
2885     case UPDATE_VISIBLE: {
2886       unsigned Idx = 0;
2887       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2888       auto *Data = (const unsigned char*)Blob.data();
2889       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
2890       // If we've already loaded the decl, perform the updates when we finish
2891       // loading this block.
2892       if (Decl *D = GetExistingDecl(ID))
2893         PendingUpdateRecords.push_back(
2894             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
2895       break;
2896     }
2897 
2898     case IDENTIFIER_TABLE:
2899       F.IdentifierTableData = Blob.data();
2900       if (Record[0]) {
2901         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2902             (const unsigned char *)F.IdentifierTableData + Record[0],
2903             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2904             (const unsigned char *)F.IdentifierTableData,
2905             ASTIdentifierLookupTrait(*this, F));
2906 
2907         PP.getIdentifierTable().setExternalIdentifierLookup(this);
2908       }
2909       break;
2910 
2911     case IDENTIFIER_OFFSET: {
2912       if (F.LocalNumIdentifiers != 0) {
2913         Error("duplicate IDENTIFIER_OFFSET record in AST file");
2914         return Failure;
2915       }
2916       F.IdentifierOffsets = (const uint32_t *)Blob.data();
2917       F.LocalNumIdentifiers = Record[0];
2918       unsigned LocalBaseIdentifierID = Record[1];
2919       F.BaseIdentifierID = getTotalNumIdentifiers();
2920 
2921       if (F.LocalNumIdentifiers > 0) {
2922         // Introduce the global -> local mapping for identifiers within this
2923         // module.
2924         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2925                                                   &F));
2926 
2927         // Introduce the local -> global mapping for identifiers within this
2928         // module.
2929         F.IdentifierRemap.insertOrReplace(
2930           std::make_pair(LocalBaseIdentifierID,
2931                          F.BaseIdentifierID - LocalBaseIdentifierID));
2932 
2933         IdentifiersLoaded.resize(IdentifiersLoaded.size()
2934                                  + F.LocalNumIdentifiers);
2935       }
2936       break;
2937     }
2938 
2939     case INTERESTING_IDENTIFIERS:
2940       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2941       break;
2942 
2943     case EAGERLY_DESERIALIZED_DECLS:
2944       // FIXME: Skip reading this record if our ASTConsumer doesn't care
2945       // about "interesting" decls (for instance, if we're building a module).
2946       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2947         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2948       break;
2949 
2950     case MODULAR_CODEGEN_DECLS:
2951       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
2952       // them (ie: if we're not codegenerating this module).
2953       if (F.Kind == MK_MainFile)
2954         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2955           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2956       break;
2957 
2958     case SPECIAL_TYPES:
2959       if (SpecialTypes.empty()) {
2960         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2961           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2962         break;
2963       }
2964 
2965       if (SpecialTypes.size() != Record.size()) {
2966         Error("invalid special-types record");
2967         return Failure;
2968       }
2969 
2970       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2971         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2972         if (!SpecialTypes[I])
2973           SpecialTypes[I] = ID;
2974         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2975         // merge step?
2976       }
2977       break;
2978 
2979     case STATISTICS:
2980       TotalNumStatements += Record[0];
2981       TotalNumMacros += Record[1];
2982       TotalLexicalDeclContexts += Record[2];
2983       TotalVisibleDeclContexts += Record[3];
2984       break;
2985 
2986     case UNUSED_FILESCOPED_DECLS:
2987       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2988         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2989       break;
2990 
2991     case DELEGATING_CTORS:
2992       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2993         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2994       break;
2995 
2996     case WEAK_UNDECLARED_IDENTIFIERS:
2997       if (Record.size() % 4 != 0) {
2998         Error("invalid weak identifiers record");
2999         return Failure;
3000       }
3001 
3002       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3003       // files. This isn't the way to do it :)
3004       WeakUndeclaredIdentifiers.clear();
3005 
3006       // Translate the weak, undeclared identifiers into global IDs.
3007       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3008         WeakUndeclaredIdentifiers.push_back(
3009           getGlobalIdentifierID(F, Record[I++]));
3010         WeakUndeclaredIdentifiers.push_back(
3011           getGlobalIdentifierID(F, Record[I++]));
3012         WeakUndeclaredIdentifiers.push_back(
3013           ReadSourceLocation(F, Record, I).getRawEncoding());
3014         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3015       }
3016       break;
3017 
3018     case SELECTOR_OFFSETS: {
3019       F.SelectorOffsets = (const uint32_t *)Blob.data();
3020       F.LocalNumSelectors = Record[0];
3021       unsigned LocalBaseSelectorID = Record[1];
3022       F.BaseSelectorID = getTotalNumSelectors();
3023 
3024       if (F.LocalNumSelectors > 0) {
3025         // Introduce the global -> local mapping for selectors within this
3026         // module.
3027         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3028 
3029         // Introduce the local -> global mapping for selectors within this
3030         // module.
3031         F.SelectorRemap.insertOrReplace(
3032           std::make_pair(LocalBaseSelectorID,
3033                          F.BaseSelectorID - LocalBaseSelectorID));
3034 
3035         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3036       }
3037       break;
3038     }
3039 
3040     case METHOD_POOL:
3041       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3042       if (Record[0])
3043         F.SelectorLookupTable
3044           = ASTSelectorLookupTable::Create(
3045                         F.SelectorLookupTableData + Record[0],
3046                         F.SelectorLookupTableData,
3047                         ASTSelectorLookupTrait(*this, F));
3048       TotalNumMethodPoolEntries += Record[1];
3049       break;
3050 
3051     case REFERENCED_SELECTOR_POOL:
3052       if (!Record.empty()) {
3053         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3054           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3055                                                                 Record[Idx++]));
3056           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3057                                               getRawEncoding());
3058         }
3059       }
3060       break;
3061 
3062     case PP_CONDITIONAL_STACK:
3063       if (!Record.empty()) {
3064         unsigned Idx = 0, End = Record.size() - 1;
3065         bool ReachedEOFWhileSkipping = Record[Idx++];
3066         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3067         if (ReachedEOFWhileSkipping) {
3068           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3069           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3070           bool FoundNonSkipPortion = Record[Idx++];
3071           bool FoundElse = Record[Idx++];
3072           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3073           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3074                            FoundElse, ElseLoc);
3075         }
3076         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3077         while (Idx < End) {
3078           auto Loc = ReadSourceLocation(F, Record, Idx);
3079           bool WasSkipping = Record[Idx++];
3080           bool FoundNonSkip = Record[Idx++];
3081           bool FoundElse = Record[Idx++];
3082           ConditionalStack.push_back(
3083               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3084         }
3085         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3086       }
3087       break;
3088 
3089     case PP_COUNTER_VALUE:
3090       if (!Record.empty() && Listener)
3091         Listener->ReadCounter(F, Record[0]);
3092       break;
3093 
3094     case FILE_SORTED_DECLS:
3095       F.FileSortedDecls = (const DeclID *)Blob.data();
3096       F.NumFileSortedDecls = Record[0];
3097       break;
3098 
3099     case SOURCE_LOCATION_OFFSETS: {
3100       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3101       F.LocalNumSLocEntries = Record[0];
3102       unsigned SLocSpaceSize = Record[1];
3103       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3104           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3105                                               SLocSpaceSize);
3106       if (!F.SLocEntryBaseID) {
3107         Error("ran out of source locations");
3108         break;
3109       }
3110       // Make our entry in the range map. BaseID is negative and growing, so
3111       // we invert it. Because we invert it, though, we need the other end of
3112       // the range.
3113       unsigned RangeStart =
3114           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3115       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3116       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3117 
3118       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3119       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3120       GlobalSLocOffsetMap.insert(
3121           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3122                            - SLocSpaceSize,&F));
3123 
3124       // Initialize the remapping table.
3125       // Invalid stays invalid.
3126       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3127       // This module. Base was 2 when being compiled.
3128       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3129                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3130 
3131       TotalNumSLocEntries += F.LocalNumSLocEntries;
3132       break;
3133     }
3134 
3135     case MODULE_OFFSET_MAP:
3136       F.ModuleOffsetMap = Blob;
3137       break;
3138 
3139     case SOURCE_MANAGER_LINE_TABLE:
3140       if (ParseLineTable(F, Record))
3141         return Failure;
3142       break;
3143 
3144     case SOURCE_LOCATION_PRELOADS: {
3145       // Need to transform from the local view (1-based IDs) to the global view,
3146       // which is based off F.SLocEntryBaseID.
3147       if (!F.PreloadSLocEntries.empty()) {
3148         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3149         return Failure;
3150       }
3151 
3152       F.PreloadSLocEntries.swap(Record);
3153       break;
3154     }
3155 
3156     case EXT_VECTOR_DECLS:
3157       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3158         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3159       break;
3160 
3161     case VTABLE_USES:
3162       if (Record.size() % 3 != 0) {
3163         Error("Invalid VTABLE_USES record");
3164         return Failure;
3165       }
3166 
3167       // Later tables overwrite earlier ones.
3168       // FIXME: Modules will have some trouble with this. This is clearly not
3169       // the right way to do this.
3170       VTableUses.clear();
3171 
3172       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3173         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3174         VTableUses.push_back(
3175           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3176         VTableUses.push_back(Record[Idx++]);
3177       }
3178       break;
3179 
3180     case PENDING_IMPLICIT_INSTANTIATIONS:
3181       if (PendingInstantiations.size() % 2 != 0) {
3182         Error("Invalid existing PendingInstantiations");
3183         return Failure;
3184       }
3185 
3186       if (Record.size() % 2 != 0) {
3187         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3188         return Failure;
3189       }
3190 
3191       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3192         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3193         PendingInstantiations.push_back(
3194           ReadSourceLocation(F, Record, I).getRawEncoding());
3195       }
3196       break;
3197 
3198     case SEMA_DECL_REFS:
3199       if (Record.size() != 3) {
3200         Error("Invalid SEMA_DECL_REFS block");
3201         return Failure;
3202       }
3203       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3204         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3205       break;
3206 
3207     case PPD_ENTITIES_OFFSETS: {
3208       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3209       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3210       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3211 
3212       unsigned LocalBasePreprocessedEntityID = Record[0];
3213 
3214       unsigned StartingID;
3215       if (!PP.getPreprocessingRecord())
3216         PP.createPreprocessingRecord();
3217       if (!PP.getPreprocessingRecord()->getExternalSource())
3218         PP.getPreprocessingRecord()->SetExternalSource(*this);
3219       StartingID
3220         = PP.getPreprocessingRecord()
3221             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3222       F.BasePreprocessedEntityID = StartingID;
3223 
3224       if (F.NumPreprocessedEntities > 0) {
3225         // Introduce the global -> local mapping for preprocessed entities in
3226         // this module.
3227         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3228 
3229         // Introduce the local -> global mapping for preprocessed entities in
3230         // this module.
3231         F.PreprocessedEntityRemap.insertOrReplace(
3232           std::make_pair(LocalBasePreprocessedEntityID,
3233             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3234       }
3235 
3236       break;
3237     }
3238 
3239     case PPD_SKIPPED_RANGES: {
3240       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3241       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3242       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3243 
3244       if (!PP.getPreprocessingRecord())
3245         PP.createPreprocessingRecord();
3246       if (!PP.getPreprocessingRecord()->getExternalSource())
3247         PP.getPreprocessingRecord()->SetExternalSource(*this);
3248       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3249           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3250 
3251       if (F.NumPreprocessedSkippedRanges > 0)
3252         GlobalSkippedRangeMap.insert(
3253             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3254       break;
3255     }
3256 
3257     case DECL_UPDATE_OFFSETS:
3258       if (Record.size() % 2 != 0) {
3259         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3260         return Failure;
3261       }
3262       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3263         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3264         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3265 
3266         // If we've already loaded the decl, perform the updates when we finish
3267         // loading this block.
3268         if (Decl *D = GetExistingDecl(ID))
3269           PendingUpdateRecords.push_back(
3270               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3271       }
3272       break;
3273 
3274     case OBJC_CATEGORIES_MAP:
3275       if (F.LocalNumObjCCategoriesInMap != 0) {
3276         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3277         return Failure;
3278       }
3279 
3280       F.LocalNumObjCCategoriesInMap = Record[0];
3281       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3282       break;
3283 
3284     case OBJC_CATEGORIES:
3285       F.ObjCCategories.swap(Record);
3286       break;
3287 
3288     case CUDA_SPECIAL_DECL_REFS:
3289       // Later tables overwrite earlier ones.
3290       // FIXME: Modules will have trouble with this.
3291       CUDASpecialDeclRefs.clear();
3292       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3293         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3294       break;
3295 
3296     case HEADER_SEARCH_TABLE:
3297       F.HeaderFileInfoTableData = Blob.data();
3298       F.LocalNumHeaderFileInfos = Record[1];
3299       if (Record[0]) {
3300         F.HeaderFileInfoTable
3301           = HeaderFileInfoLookupTable::Create(
3302                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3303                    (const unsigned char *)F.HeaderFileInfoTableData,
3304                    HeaderFileInfoTrait(*this, F,
3305                                        &PP.getHeaderSearchInfo(),
3306                                        Blob.data() + Record[2]));
3307 
3308         PP.getHeaderSearchInfo().SetExternalSource(this);
3309         if (!PP.getHeaderSearchInfo().getExternalLookup())
3310           PP.getHeaderSearchInfo().SetExternalLookup(this);
3311       }
3312       break;
3313 
3314     case FP_PRAGMA_OPTIONS:
3315       // Later tables overwrite earlier ones.
3316       FPPragmaOptions.swap(Record);
3317       break;
3318 
3319     case OPENCL_EXTENSIONS:
3320       for (unsigned I = 0, E = Record.size(); I != E; ) {
3321         auto Name = ReadString(Record, I);
3322         auto &Opt = OpenCLExtensions.OptMap[Name];
3323         Opt.Supported = Record[I++] != 0;
3324         Opt.Enabled = Record[I++] != 0;
3325         Opt.Avail = Record[I++];
3326         Opt.Core = Record[I++];
3327       }
3328       break;
3329 
3330     case OPENCL_EXTENSION_TYPES:
3331       for (unsigned I = 0, E = Record.size(); I != E;) {
3332         auto TypeID = static_cast<::TypeID>(Record[I++]);
3333         auto *Type = GetType(TypeID).getTypePtr();
3334         auto NumExt = static_cast<unsigned>(Record[I++]);
3335         for (unsigned II = 0; II != NumExt; ++II) {
3336           auto Ext = ReadString(Record, I);
3337           OpenCLTypeExtMap[Type].insert(Ext);
3338         }
3339       }
3340       break;
3341 
3342     case OPENCL_EXTENSION_DECLS:
3343       for (unsigned I = 0, E = Record.size(); I != E;) {
3344         auto DeclID = static_cast<::DeclID>(Record[I++]);
3345         auto *Decl = GetDecl(DeclID);
3346         auto NumExt = static_cast<unsigned>(Record[I++]);
3347         for (unsigned II = 0; II != NumExt; ++II) {
3348           auto Ext = ReadString(Record, I);
3349           OpenCLDeclExtMap[Decl].insert(Ext);
3350         }
3351       }
3352       break;
3353 
3354     case TENTATIVE_DEFINITIONS:
3355       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3356         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3357       break;
3358 
3359     case KNOWN_NAMESPACES:
3360       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3361         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3362       break;
3363 
3364     case UNDEFINED_BUT_USED:
3365       if (UndefinedButUsed.size() % 2 != 0) {
3366         Error("Invalid existing UndefinedButUsed");
3367         return Failure;
3368       }
3369 
3370       if (Record.size() % 2 != 0) {
3371         Error("invalid undefined-but-used record");
3372         return Failure;
3373       }
3374       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3375         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3376         UndefinedButUsed.push_back(
3377             ReadSourceLocation(F, Record, I).getRawEncoding());
3378       }
3379       break;
3380 
3381     case DELETE_EXPRS_TO_ANALYZE:
3382       for (unsigned I = 0, N = Record.size(); I != N;) {
3383         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3384         const uint64_t Count = Record[I++];
3385         DelayedDeleteExprs.push_back(Count);
3386         for (uint64_t C = 0; C < Count; ++C) {
3387           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3388           bool IsArrayForm = Record[I++] == 1;
3389           DelayedDeleteExprs.push_back(IsArrayForm);
3390         }
3391       }
3392       break;
3393 
3394     case IMPORTED_MODULES:
3395       if (!F.isModule()) {
3396         // If we aren't loading a module (which has its own exports), make
3397         // all of the imported modules visible.
3398         // FIXME: Deal with macros-only imports.
3399         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3400           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3401           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3402           if (GlobalID) {
3403             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3404             if (DeserializationListener)
3405               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3406           }
3407         }
3408       }
3409       break;
3410 
3411     case MACRO_OFFSET: {
3412       if (F.LocalNumMacros != 0) {
3413         Error("duplicate MACRO_OFFSET record in AST file");
3414         return Failure;
3415       }
3416       F.MacroOffsets = (const uint32_t *)Blob.data();
3417       F.LocalNumMacros = Record[0];
3418       unsigned LocalBaseMacroID = Record[1];
3419       F.BaseMacroID = getTotalNumMacros();
3420 
3421       if (F.LocalNumMacros > 0) {
3422         // Introduce the global -> local mapping for macros within this module.
3423         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3424 
3425         // Introduce the local -> global mapping for macros within this module.
3426         F.MacroRemap.insertOrReplace(
3427           std::make_pair(LocalBaseMacroID,
3428                          F.BaseMacroID - LocalBaseMacroID));
3429 
3430         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3431       }
3432       break;
3433     }
3434 
3435     case LATE_PARSED_TEMPLATE:
3436       LateParsedTemplates.append(Record.begin(), Record.end());
3437       break;
3438 
3439     case OPTIMIZE_PRAGMA_OPTIONS:
3440       if (Record.size() != 1) {
3441         Error("invalid pragma optimize record");
3442         return Failure;
3443       }
3444       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3445       break;
3446 
3447     case MSSTRUCT_PRAGMA_OPTIONS:
3448       if (Record.size() != 1) {
3449         Error("invalid pragma ms_struct record");
3450         return Failure;
3451       }
3452       PragmaMSStructState = Record[0];
3453       break;
3454 
3455     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3456       if (Record.size() != 2) {
3457         Error("invalid pragma ms_struct record");
3458         return Failure;
3459       }
3460       PragmaMSPointersToMembersState = Record[0];
3461       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3462       break;
3463 
3464     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3465       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3466         UnusedLocalTypedefNameCandidates.push_back(
3467             getGlobalDeclID(F, Record[I]));
3468       break;
3469 
3470     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3471       if (Record.size() != 1) {
3472         Error("invalid cuda pragma options record");
3473         return Failure;
3474       }
3475       ForceCUDAHostDeviceDepth = Record[0];
3476       break;
3477 
3478     case PACK_PRAGMA_OPTIONS: {
3479       if (Record.size() < 3) {
3480         Error("invalid pragma pack record");
3481         return Failure;
3482       }
3483       PragmaPackCurrentValue = Record[0];
3484       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3485       unsigned NumStackEntries = Record[2];
3486       unsigned Idx = 3;
3487       // Reset the stack when importing a new module.
3488       PragmaPackStack.clear();
3489       for (unsigned I = 0; I < NumStackEntries; ++I) {
3490         PragmaPackStackEntry Entry;
3491         Entry.Value = Record[Idx++];
3492         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3493         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3494         PragmaPackStrings.push_back(ReadString(Record, Idx));
3495         Entry.SlotLabel = PragmaPackStrings.back();
3496         PragmaPackStack.push_back(Entry);
3497       }
3498       break;
3499     }
3500     }
3501   }
3502 }
3503 
3504 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3505   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3506 
3507   // Additional remapping information.
3508   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3509   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3510   F.ModuleOffsetMap = StringRef();
3511 
3512   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3513   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3514     F.SLocRemap.insert(std::make_pair(0U, 0));
3515     F.SLocRemap.insert(std::make_pair(2U, 1));
3516   }
3517 
3518   // Continuous range maps we may be updating in our module.
3519   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3520   RemapBuilder SLocRemap(F.SLocRemap);
3521   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3522   RemapBuilder MacroRemap(F.MacroRemap);
3523   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3524   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3525   RemapBuilder SelectorRemap(F.SelectorRemap);
3526   RemapBuilder DeclRemap(F.DeclRemap);
3527   RemapBuilder TypeRemap(F.TypeRemap);
3528 
3529   while (Data < DataEnd) {
3530     // FIXME: Looking up dependency modules by filename is horrible. Let's
3531     // start fixing this with prebuilt and explicit modules and see how it
3532     // goes...
3533     using namespace llvm::support;
3534     ModuleKind Kind = static_cast<ModuleKind>(
3535       endian::readNext<uint8_t, little, unaligned>(Data));
3536     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3537     StringRef Name = StringRef((const char*)Data, Len);
3538     Data += Len;
3539     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3540                       ? ModuleMgr.lookupByModuleName(Name)
3541                       : ModuleMgr.lookupByFileName(Name));
3542     if (!OM) {
3543       std::string Msg =
3544           "SourceLocation remap refers to unknown module, cannot find ";
3545       Msg.append(Name);
3546       Error(Msg);
3547       return;
3548     }
3549 
3550     uint32_t SLocOffset =
3551         endian::readNext<uint32_t, little, unaligned>(Data);
3552     uint32_t IdentifierIDOffset =
3553         endian::readNext<uint32_t, little, unaligned>(Data);
3554     uint32_t MacroIDOffset =
3555         endian::readNext<uint32_t, little, unaligned>(Data);
3556     uint32_t PreprocessedEntityIDOffset =
3557         endian::readNext<uint32_t, little, unaligned>(Data);
3558     uint32_t SubmoduleIDOffset =
3559         endian::readNext<uint32_t, little, unaligned>(Data);
3560     uint32_t SelectorIDOffset =
3561         endian::readNext<uint32_t, little, unaligned>(Data);
3562     uint32_t DeclIDOffset =
3563         endian::readNext<uint32_t, little, unaligned>(Data);
3564     uint32_t TypeIndexOffset =
3565         endian::readNext<uint32_t, little, unaligned>(Data);
3566 
3567     uint32_t None = std::numeric_limits<uint32_t>::max();
3568 
3569     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3570                          RemapBuilder &Remap) {
3571       if (Offset != None)
3572         Remap.insert(std::make_pair(Offset,
3573                                     static_cast<int>(BaseOffset - Offset)));
3574     };
3575     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3576     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3577     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3578     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3579               PreprocessedEntityRemap);
3580     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3581     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3582     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3583     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3584 
3585     // Global -> local mappings.
3586     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3587   }
3588 }
3589 
3590 ASTReader::ASTReadResult
3591 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3592                                   const ModuleFile *ImportedBy,
3593                                   unsigned ClientLoadCapabilities) {
3594   unsigned Idx = 0;
3595   F.ModuleMapPath = ReadPath(F, Record, Idx);
3596 
3597   // Try to resolve ModuleName in the current header search context and
3598   // verify that it is found in the same module map file as we saved. If the
3599   // top-level AST file is a main file, skip this check because there is no
3600   // usable header search context.
3601   assert(!F.ModuleName.empty() &&
3602          "MODULE_NAME should come before MODULE_MAP_FILE");
3603   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3604     // An implicitly-loaded module file should have its module listed in some
3605     // module map file that we've already loaded.
3606     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3607     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3608     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3609     // Don't emit module relocation error if we have -fno-validate-pch
3610     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3611       assert(ImportedBy && "top-level import should be verified");
3612       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3613         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3614           // This module was defined by an imported (explicit) module.
3615           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3616                                                << ASTFE->getName();
3617         } else {
3618           // This module was built with a different module map.
3619           Diag(diag::err_imported_module_not_found)
3620               << F.ModuleName << F.FileName << ImportedBy->FileName
3621               << F.ModuleMapPath;
3622           // In case it was imported by a PCH, there's a chance the user is
3623           // just missing to include the search path to the directory containing
3624           // the modulemap.
3625           if (ImportedBy->Kind == MK_PCH)
3626             Diag(diag::note_imported_by_pch_module_not_found)
3627                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3628         }
3629       }
3630       return OutOfDate;
3631     }
3632 
3633     assert(M->Name == F.ModuleName && "found module with different name");
3634 
3635     // Check the primary module map file.
3636     const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3637     if (StoredModMap == nullptr || StoredModMap != ModMap) {
3638       assert(ModMap && "found module is missing module map file");
3639       assert(ImportedBy && "top-level import should be verified");
3640       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3641         Diag(diag::err_imported_module_modmap_changed)
3642           << F.ModuleName << ImportedBy->FileName
3643           << ModMap->getName() << F.ModuleMapPath;
3644       return OutOfDate;
3645     }
3646 
3647     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3648     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3649       // FIXME: we should use input files rather than storing names.
3650       std::string Filename = ReadPath(F, Record, Idx);
3651       const FileEntry *F =
3652           FileMgr.getFile(Filename, false, false);
3653       if (F == nullptr) {
3654         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3655           Error("could not find file '" + Filename +"' referenced by AST file");
3656         return OutOfDate;
3657       }
3658       AdditionalStoredMaps.insert(F);
3659     }
3660 
3661     // Check any additional module map files (e.g. module.private.modulemap)
3662     // that are not in the pcm.
3663     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3664       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3665         // Remove files that match
3666         // Note: SmallPtrSet::erase is really remove
3667         if (!AdditionalStoredMaps.erase(ModMap)) {
3668           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3669             Diag(diag::err_module_different_modmap)
3670               << F.ModuleName << /*new*/0 << ModMap->getName();
3671           return OutOfDate;
3672         }
3673       }
3674     }
3675 
3676     // Check any additional module map files that are in the pcm, but not
3677     // found in header search. Cases that match are already removed.
3678     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3679       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3680         Diag(diag::err_module_different_modmap)
3681           << F.ModuleName << /*not new*/1 << ModMap->getName();
3682       return OutOfDate;
3683     }
3684   }
3685 
3686   if (Listener)
3687     Listener->ReadModuleMapFile(F.ModuleMapPath);
3688   return Success;
3689 }
3690 
3691 /// Move the given method to the back of the global list of methods.
3692 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3693   // Find the entry for this selector in the method pool.
3694   Sema::GlobalMethodPool::iterator Known
3695     = S.MethodPool.find(Method->getSelector());
3696   if (Known == S.MethodPool.end())
3697     return;
3698 
3699   // Retrieve the appropriate method list.
3700   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3701                                                     : Known->second.second;
3702   bool Found = false;
3703   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3704     if (!Found) {
3705       if (List->getMethod() == Method) {
3706         Found = true;
3707       } else {
3708         // Keep searching.
3709         continue;
3710       }
3711     }
3712 
3713     if (List->getNext())
3714       List->setMethod(List->getNext()->getMethod());
3715     else
3716       List->setMethod(Method);
3717   }
3718 }
3719 
3720 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3721   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3722   for (Decl *D : Names) {
3723     bool wasHidden = D->isHidden();
3724     D->setVisibleDespiteOwningModule();
3725 
3726     if (wasHidden && SemaObj) {
3727       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3728         moveMethodToBackOfGlobalList(*SemaObj, Method);
3729       }
3730     }
3731   }
3732 }
3733 
3734 void ASTReader::makeModuleVisible(Module *Mod,
3735                                   Module::NameVisibilityKind NameVisibility,
3736                                   SourceLocation ImportLoc) {
3737   llvm::SmallPtrSet<Module *, 4> Visited;
3738   SmallVector<Module *, 4> Stack;
3739   Stack.push_back(Mod);
3740   while (!Stack.empty()) {
3741     Mod = Stack.pop_back_val();
3742 
3743     if (NameVisibility <= Mod->NameVisibility) {
3744       // This module already has this level of visibility (or greater), so
3745       // there is nothing more to do.
3746       continue;
3747     }
3748 
3749     if (!Mod->isAvailable()) {
3750       // Modules that aren't available cannot be made visible.
3751       continue;
3752     }
3753 
3754     // Update the module's name visibility.
3755     Mod->NameVisibility = NameVisibility;
3756 
3757     // If we've already deserialized any names from this module,
3758     // mark them as visible.
3759     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3760     if (Hidden != HiddenNamesMap.end()) {
3761       auto HiddenNames = std::move(*Hidden);
3762       HiddenNamesMap.erase(Hidden);
3763       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3764       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3765              "making names visible added hidden names");
3766     }
3767 
3768     // Push any exported modules onto the stack to be marked as visible.
3769     SmallVector<Module *, 16> Exports;
3770     Mod->getExportedModules(Exports);
3771     for (SmallVectorImpl<Module *>::iterator
3772            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3773       Module *Exported = *I;
3774       if (Visited.insert(Exported).second)
3775         Stack.push_back(Exported);
3776     }
3777   }
3778 }
3779 
3780 /// We've merged the definition \p MergedDef into the existing definition
3781 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3782 /// visible.
3783 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3784                                           NamedDecl *MergedDef) {
3785   if (Def->isHidden()) {
3786     // If MergedDef is visible or becomes visible, make the definition visible.
3787     if (!MergedDef->isHidden())
3788       Def->setVisibleDespiteOwningModule();
3789     else {
3790       getContext().mergeDefinitionIntoModule(
3791           Def, MergedDef->getImportedOwningModule(),
3792           /*NotifyListeners*/ false);
3793       PendingMergedDefinitionsToDeduplicate.insert(Def);
3794     }
3795   }
3796 }
3797 
3798 bool ASTReader::loadGlobalIndex() {
3799   if (GlobalIndex)
3800     return false;
3801 
3802   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3803       !PP.getLangOpts().Modules)
3804     return true;
3805 
3806   // Try to load the global index.
3807   TriedLoadingGlobalIndex = true;
3808   StringRef ModuleCachePath
3809     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3810   std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
3811     = GlobalModuleIndex::readIndex(ModuleCachePath);
3812   if (!Result.first)
3813     return true;
3814 
3815   GlobalIndex.reset(Result.first);
3816   ModuleMgr.setGlobalIndex(GlobalIndex.get());
3817   return false;
3818 }
3819 
3820 bool ASTReader::isGlobalIndexUnavailable() const {
3821   return PP.getLangOpts().Modules && UseGlobalIndex &&
3822          !hasGlobalIndex() && TriedLoadingGlobalIndex;
3823 }
3824 
3825 static void updateModuleTimestamp(ModuleFile &MF) {
3826   // Overwrite the timestamp file contents so that file's mtime changes.
3827   std::string TimestampFilename = MF.getTimestampFilename();
3828   std::error_code EC;
3829   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3830   if (EC)
3831     return;
3832   OS << "Timestamp file\n";
3833   OS.close();
3834   OS.clear_error(); // Avoid triggering a fatal error.
3835 }
3836 
3837 /// Given a cursor at the start of an AST file, scan ahead and drop the
3838 /// cursor into the start of the given block ID, returning false on success and
3839 /// true on failure.
3840 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3841   while (true) {
3842     llvm::BitstreamEntry Entry = Cursor.advance();
3843     switch (Entry.Kind) {
3844     case llvm::BitstreamEntry::Error:
3845     case llvm::BitstreamEntry::EndBlock:
3846       return true;
3847 
3848     case llvm::BitstreamEntry::Record:
3849       // Ignore top-level records.
3850       Cursor.skipRecord(Entry.ID);
3851       break;
3852 
3853     case llvm::BitstreamEntry::SubBlock:
3854       if (Entry.ID == BlockID) {
3855         if (Cursor.EnterSubBlock(BlockID))
3856           return true;
3857         // Found it!
3858         return false;
3859       }
3860 
3861       if (Cursor.SkipBlock())
3862         return true;
3863     }
3864   }
3865 }
3866 
3867 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
3868                                             ModuleKind Type,
3869                                             SourceLocation ImportLoc,
3870                                             unsigned ClientLoadCapabilities,
3871                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
3872   llvm::SaveAndRestore<SourceLocation>
3873     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3874 
3875   // Defer any pending actions until we get to the end of reading the AST file.
3876   Deserializing AnASTFile(this);
3877 
3878   // Bump the generation number.
3879   unsigned PreviousGeneration = 0;
3880   if (ContextObj)
3881     PreviousGeneration = incrementGeneration(*ContextObj);
3882 
3883   unsigned NumModules = ModuleMgr.size();
3884   SmallVector<ImportedModule, 4> Loaded;
3885   switch (ASTReadResult ReadResult =
3886               ReadASTCore(FileName, Type, ImportLoc,
3887                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
3888                           ASTFileSignature(), ClientLoadCapabilities)) {
3889   case Failure:
3890   case Missing:
3891   case OutOfDate:
3892   case VersionMismatch:
3893   case ConfigurationMismatch:
3894   case HadErrors: {
3895     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3896     for (const ImportedModule &IM : Loaded)
3897       LoadedSet.insert(IM.Mod);
3898 
3899     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
3900                             PP.getLangOpts().Modules
3901                                 ? &PP.getHeaderSearchInfo().getModuleMap()
3902                                 : nullptr);
3903 
3904     // If we find that any modules are unusable, the global index is going
3905     // to be out-of-date. Just remove it.
3906     GlobalIndex.reset();
3907     ModuleMgr.setGlobalIndex(nullptr);
3908     return ReadResult;
3909   }
3910   case Success:
3911     break;
3912   }
3913 
3914   // Here comes stuff that we only do once the entire chain is loaded.
3915 
3916   // Load the AST blocks of all of the modules that we loaded.
3917   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3918                                               MEnd = Loaded.end();
3919        M != MEnd; ++M) {
3920     ModuleFile &F = *M->Mod;
3921 
3922     // Read the AST block.
3923     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3924       return Result;
3925 
3926     // Read the extension blocks.
3927     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3928       if (ASTReadResult Result = ReadExtensionBlock(F))
3929         return Result;
3930     }
3931 
3932     // Once read, set the ModuleFile bit base offset and update the size in
3933     // bits of all files we've seen.
3934     F.GlobalBitOffset = TotalModulesSizeInBits;
3935     TotalModulesSizeInBits += F.SizeInBits;
3936     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3937 
3938     // Preload SLocEntries.
3939     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3940       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3941       // Load it through the SourceManager and don't call ReadSLocEntry()
3942       // directly because the entry may have already been loaded in which case
3943       // calling ReadSLocEntry() directly would trigger an assertion in
3944       // SourceManager.
3945       SourceMgr.getLoadedSLocEntryByID(Index);
3946     }
3947 
3948     // Map the original source file ID into the ID space of the current
3949     // compilation.
3950     if (F.OriginalSourceFileID.isValid()) {
3951       F.OriginalSourceFileID = FileID::get(
3952           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
3953     }
3954 
3955     // Preload all the pending interesting identifiers by marking them out of
3956     // date.
3957     for (auto Offset : F.PreloadIdentifierOffsets) {
3958       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3959           F.IdentifierTableData + Offset);
3960 
3961       ASTIdentifierLookupTrait Trait(*this, F);
3962       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3963       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
3964       auto &II = PP.getIdentifierTable().getOwn(Key);
3965       II.setOutOfDate(true);
3966 
3967       // Mark this identifier as being from an AST file so that we can track
3968       // whether we need to serialize it.
3969       markIdentifierFromAST(*this, II);
3970 
3971       // Associate the ID with the identifier so that the writer can reuse it.
3972       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3973       SetIdentifierInfo(ID, &II);
3974     }
3975   }
3976 
3977   // Setup the import locations and notify the module manager that we've
3978   // committed to these module files.
3979   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3980                                               MEnd = Loaded.end();
3981        M != MEnd; ++M) {
3982     ModuleFile &F = *M->Mod;
3983 
3984     ModuleMgr.moduleFileAccepted(&F);
3985 
3986     // Set the import location.
3987     F.DirectImportLoc = ImportLoc;
3988     // FIXME: We assume that locations from PCH / preamble do not need
3989     // any translation.
3990     if (!M->ImportedBy)
3991       F.ImportLoc = M->ImportLoc;
3992     else
3993       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
3994   }
3995 
3996   if (!PP.getLangOpts().CPlusPlus ||
3997       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
3998        Type != MK_PrebuiltModule)) {
3999     // Mark all of the identifiers in the identifier table as being out of date,
4000     // so that various accessors know to check the loaded modules when the
4001     // identifier is used.
4002     //
4003     // For C++ modules, we don't need information on many identifiers (just
4004     // those that provide macros or are poisoned), so we mark all of
4005     // the interesting ones via PreloadIdentifierOffsets.
4006     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4007                                 IdEnd = PP.getIdentifierTable().end();
4008          Id != IdEnd; ++Id)
4009       Id->second->setOutOfDate(true);
4010   }
4011   // Mark selectors as out of date.
4012   for (auto Sel : SelectorGeneration)
4013     SelectorOutOfDate[Sel.first] = true;
4014 
4015   // Resolve any unresolved module exports.
4016   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4017     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4018     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4019     Module *ResolvedMod = getSubmodule(GlobalID);
4020 
4021     switch (Unresolved.Kind) {
4022     case UnresolvedModuleRef::Conflict:
4023       if (ResolvedMod) {
4024         Module::Conflict Conflict;
4025         Conflict.Other = ResolvedMod;
4026         Conflict.Message = Unresolved.String.str();
4027         Unresolved.Mod->Conflicts.push_back(Conflict);
4028       }
4029       continue;
4030 
4031     case UnresolvedModuleRef::Import:
4032       if (ResolvedMod)
4033         Unresolved.Mod->Imports.insert(ResolvedMod);
4034       continue;
4035 
4036     case UnresolvedModuleRef::Export:
4037       if (ResolvedMod || Unresolved.IsWildcard)
4038         Unresolved.Mod->Exports.push_back(
4039           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4040       continue;
4041     }
4042   }
4043   UnresolvedModuleRefs.clear();
4044 
4045   if (Imported)
4046     Imported->append(ImportedModules.begin(),
4047                      ImportedModules.end());
4048 
4049   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4050   // Might be unnecessary as use declarations are only used to build the
4051   // module itself.
4052 
4053   if (ContextObj)
4054     InitializeContext();
4055 
4056   if (SemaObj)
4057     UpdateSema();
4058 
4059   if (DeserializationListener)
4060     DeserializationListener->ReaderInitialized(this);
4061 
4062   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4063   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4064     // If this AST file is a precompiled preamble, then set the
4065     // preamble file ID of the source manager to the file source file
4066     // from which the preamble was built.
4067     if (Type == MK_Preamble) {
4068       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4069     } else if (Type == MK_MainFile) {
4070       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4071     }
4072   }
4073 
4074   // For any Objective-C class definitions we have already loaded, make sure
4075   // that we load any additional categories.
4076   if (ContextObj) {
4077     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4078       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4079                          ObjCClassesLoaded[I],
4080                          PreviousGeneration);
4081     }
4082   }
4083 
4084   if (PP.getHeaderSearchInfo()
4085           .getHeaderSearchOpts()
4086           .ModulesValidateOncePerBuildSession) {
4087     // Now we are certain that the module and all modules it depends on are
4088     // up to date.  Create or update timestamp files for modules that are
4089     // located in the module cache (not for PCH files that could be anywhere
4090     // in the filesystem).
4091     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4092       ImportedModule &M = Loaded[I];
4093       if (M.Mod->Kind == MK_ImplicitModule) {
4094         updateModuleTimestamp(*M.Mod);
4095       }
4096     }
4097   }
4098 
4099   return Success;
4100 }
4101 
4102 static ASTFileSignature readASTFileSignature(StringRef PCH);
4103 
4104 /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
4105 static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
4106   return Stream.canSkipToPos(4) &&
4107          Stream.Read(8) == 'C' &&
4108          Stream.Read(8) == 'P' &&
4109          Stream.Read(8) == 'C' &&
4110          Stream.Read(8) == 'H';
4111 }
4112 
4113 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4114   switch (Kind) {
4115   case MK_PCH:
4116     return 0; // PCH
4117   case MK_ImplicitModule:
4118   case MK_ExplicitModule:
4119   case MK_PrebuiltModule:
4120     return 1; // module
4121   case MK_MainFile:
4122   case MK_Preamble:
4123     return 2; // main source file
4124   }
4125   llvm_unreachable("unknown module kind");
4126 }
4127 
4128 ASTReader::ASTReadResult
4129 ASTReader::ReadASTCore(StringRef FileName,
4130                        ModuleKind Type,
4131                        SourceLocation ImportLoc,
4132                        ModuleFile *ImportedBy,
4133                        SmallVectorImpl<ImportedModule> &Loaded,
4134                        off_t ExpectedSize, time_t ExpectedModTime,
4135                        ASTFileSignature ExpectedSignature,
4136                        unsigned ClientLoadCapabilities) {
4137   ModuleFile *M;
4138   std::string ErrorStr;
4139   ModuleManager::AddModuleResult AddResult
4140     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4141                           getGeneration(), ExpectedSize, ExpectedModTime,
4142                           ExpectedSignature, readASTFileSignature,
4143                           M, ErrorStr);
4144 
4145   switch (AddResult) {
4146   case ModuleManager::AlreadyLoaded:
4147     Diag(diag::remark_module_import)
4148         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4149         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4150     return Success;
4151 
4152   case ModuleManager::NewlyLoaded:
4153     // Load module file below.
4154     break;
4155 
4156   case ModuleManager::Missing:
4157     // The module file was missing; if the client can handle that, return
4158     // it.
4159     if (ClientLoadCapabilities & ARR_Missing)
4160       return Missing;
4161 
4162     // Otherwise, return an error.
4163     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4164                                           << FileName << !ErrorStr.empty()
4165                                           << ErrorStr;
4166     return Failure;
4167 
4168   case ModuleManager::OutOfDate:
4169     // We couldn't load the module file because it is out-of-date. If the
4170     // client can handle out-of-date, return it.
4171     if (ClientLoadCapabilities & ARR_OutOfDate)
4172       return OutOfDate;
4173 
4174     // Otherwise, return an error.
4175     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4176                                             << FileName << !ErrorStr.empty()
4177                                             << ErrorStr;
4178     return Failure;
4179   }
4180 
4181   assert(M && "Missing module file");
4182 
4183   ModuleFile &F = *M;
4184   BitstreamCursor &Stream = F.Stream;
4185   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4186   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4187 
4188   // Sniff for the signature.
4189   if (!startsWithASTFileMagic(Stream)) {
4190     Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
4191                                         << FileName;
4192     return Failure;
4193   }
4194 
4195   // This is used for compatibility with older PCH formats.
4196   bool HaveReadControlBlock = false;
4197   while (true) {
4198     llvm::BitstreamEntry Entry = Stream.advance();
4199 
4200     switch (Entry.Kind) {
4201     case llvm::BitstreamEntry::Error:
4202     case llvm::BitstreamEntry::Record:
4203     case llvm::BitstreamEntry::EndBlock:
4204       Error("invalid record at top-level of AST file");
4205       return Failure;
4206 
4207     case llvm::BitstreamEntry::SubBlock:
4208       break;
4209     }
4210 
4211     switch (Entry.ID) {
4212     case CONTROL_BLOCK_ID:
4213       HaveReadControlBlock = true;
4214       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4215       case Success:
4216         // Check that we didn't try to load a non-module AST file as a module.
4217         //
4218         // FIXME: Should we also perform the converse check? Loading a module as
4219         // a PCH file sort of works, but it's a bit wonky.
4220         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4221              Type == MK_PrebuiltModule) &&
4222             F.ModuleName.empty()) {
4223           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4224           if (Result != OutOfDate ||
4225               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4226             Diag(diag::err_module_file_not_module) << FileName;
4227           return Result;
4228         }
4229         break;
4230 
4231       case Failure: return Failure;
4232       case Missing: return Missing;
4233       case OutOfDate: return OutOfDate;
4234       case VersionMismatch: return VersionMismatch;
4235       case ConfigurationMismatch: return ConfigurationMismatch;
4236       case HadErrors: return HadErrors;
4237       }
4238       break;
4239 
4240     case AST_BLOCK_ID:
4241       if (!HaveReadControlBlock) {
4242         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4243           Diag(diag::err_pch_version_too_old);
4244         return VersionMismatch;
4245       }
4246 
4247       // Record that we've loaded this module.
4248       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4249       return Success;
4250 
4251     case UNHASHED_CONTROL_BLOCK_ID:
4252       // This block is handled using look-ahead during ReadControlBlock.  We
4253       // shouldn't get here!
4254       Error("malformed block record in AST file");
4255       return Failure;
4256 
4257     default:
4258       if (Stream.SkipBlock()) {
4259         Error("malformed block record in AST file");
4260         return Failure;
4261       }
4262       break;
4263     }
4264   }
4265 
4266   llvm_unreachable("unexpected break; expected return");
4267 }
4268 
4269 ASTReader::ASTReadResult
4270 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4271                                     unsigned ClientLoadCapabilities) {
4272   const HeaderSearchOptions &HSOpts =
4273       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4274   bool AllowCompatibleConfigurationMismatch =
4275       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4276 
4277   ASTReadResult Result = readUnhashedControlBlockImpl(
4278       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4279       Listener.get(),
4280       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4281 
4282   // If F was directly imported by another module, it's implicitly validated by
4283   // the importing module.
4284   if (DisableValidation || WasImportedBy ||
4285       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4286     return Success;
4287 
4288   if (Result == Failure) {
4289     Error("malformed block record in AST file");
4290     return Failure;
4291   }
4292 
4293   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4294     // If this module has already been finalized in the PCMCache, we're stuck
4295     // with it; we can only load a single version of each module.
4296     //
4297     // This can happen when a module is imported in two contexts: in one, as a
4298     // user module; in another, as a system module (due to an import from
4299     // another module marked with the [system] flag).  It usually indicates a
4300     // bug in the module map: this module should also be marked with [system].
4301     //
4302     // If -Wno-system-headers (the default), and the first import is as a
4303     // system module, then validation will fail during the as-user import,
4304     // since -Werror flags won't have been validated.  However, it's reasonable
4305     // to treat this consistently as a system module.
4306     //
4307     // If -Wsystem-headers, the PCM on disk was built with
4308     // -Wno-system-headers, and the first import is as a user module, then
4309     // validation will fail during the as-system import since the PCM on disk
4310     // doesn't guarantee that -Werror was respected.  However, the -Werror
4311     // flags were checked during the initial as-user import.
4312     if (PCMCache.isBufferFinal(F.FileName)) {
4313       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4314       return Success;
4315     }
4316   }
4317 
4318   return Result;
4319 }
4320 
4321 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4322     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4323     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4324     bool ValidateDiagnosticOptions) {
4325   // Initialize a stream.
4326   BitstreamCursor Stream(StreamData);
4327 
4328   // Sniff for the signature.
4329   if (!startsWithASTFileMagic(Stream))
4330     return Failure;
4331 
4332   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4333   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4334     return Failure;
4335 
4336   // Read all of the records in the options block.
4337   RecordData Record;
4338   ASTReadResult Result = Success;
4339   while (true) {
4340     llvm::BitstreamEntry Entry = Stream.advance();
4341 
4342     switch (Entry.Kind) {
4343     case llvm::BitstreamEntry::Error:
4344     case llvm::BitstreamEntry::SubBlock:
4345       return Failure;
4346 
4347     case llvm::BitstreamEntry::EndBlock:
4348       return Result;
4349 
4350     case llvm::BitstreamEntry::Record:
4351       // The interesting case.
4352       break;
4353     }
4354 
4355     // Read and process a record.
4356     Record.clear();
4357     switch (
4358         (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
4359     case SIGNATURE:
4360       if (F)
4361         std::copy(Record.begin(), Record.end(), F->Signature.data());
4362       break;
4363     case DIAGNOSTIC_OPTIONS: {
4364       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4365       if (Listener && ValidateDiagnosticOptions &&
4366           !AllowCompatibleConfigurationMismatch &&
4367           ParseDiagnosticOptions(Record, Complain, *Listener))
4368         Result = OutOfDate; // Don't return early.  Read the signature.
4369       break;
4370     }
4371     case DIAG_PRAGMA_MAPPINGS:
4372       if (!F)
4373         break;
4374       if (F->PragmaDiagMappings.empty())
4375         F->PragmaDiagMappings.swap(Record);
4376       else
4377         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4378                                      Record.begin(), Record.end());
4379       break;
4380     }
4381   }
4382 }
4383 
4384 /// Parse a record and blob containing module file extension metadata.
4385 static bool parseModuleFileExtensionMetadata(
4386               const SmallVectorImpl<uint64_t> &Record,
4387               StringRef Blob,
4388               ModuleFileExtensionMetadata &Metadata) {
4389   if (Record.size() < 4) return true;
4390 
4391   Metadata.MajorVersion = Record[0];
4392   Metadata.MinorVersion = Record[1];
4393 
4394   unsigned BlockNameLen = Record[2];
4395   unsigned UserInfoLen = Record[3];
4396 
4397   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4398 
4399   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4400   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4401                                   Blob.data() + BlockNameLen + UserInfoLen);
4402   return false;
4403 }
4404 
4405 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4406   BitstreamCursor &Stream = F.Stream;
4407 
4408   RecordData Record;
4409   while (true) {
4410     llvm::BitstreamEntry Entry = Stream.advance();
4411     switch (Entry.Kind) {
4412     case llvm::BitstreamEntry::SubBlock:
4413       if (Stream.SkipBlock())
4414         return Failure;
4415 
4416       continue;
4417 
4418     case llvm::BitstreamEntry::EndBlock:
4419       return Success;
4420 
4421     case llvm::BitstreamEntry::Error:
4422       return HadErrors;
4423 
4424     case llvm::BitstreamEntry::Record:
4425       break;
4426     }
4427 
4428     Record.clear();
4429     StringRef Blob;
4430     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4431     switch (RecCode) {
4432     case EXTENSION_METADATA: {
4433       ModuleFileExtensionMetadata Metadata;
4434       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4435         return Failure;
4436 
4437       // Find a module file extension with this block name.
4438       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4439       if (Known == ModuleFileExtensions.end()) break;
4440 
4441       // Form a reader.
4442       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4443                                                              F, Stream)) {
4444         F.ExtensionReaders.push_back(std::move(Reader));
4445       }
4446 
4447       break;
4448     }
4449     }
4450   }
4451 
4452   return Success;
4453 }
4454 
4455 void ASTReader::InitializeContext() {
4456   assert(ContextObj && "no context to initialize");
4457   ASTContext &Context = *ContextObj;
4458 
4459   // If there's a listener, notify them that we "read" the translation unit.
4460   if (DeserializationListener)
4461     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4462                                       Context.getTranslationUnitDecl());
4463 
4464   // FIXME: Find a better way to deal with collisions between these
4465   // built-in types. Right now, we just ignore the problem.
4466 
4467   // Load the special types.
4468   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4469     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4470       if (!Context.CFConstantStringTypeDecl)
4471         Context.setCFConstantStringType(GetType(String));
4472     }
4473 
4474     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4475       QualType FileType = GetType(File);
4476       if (FileType.isNull()) {
4477         Error("FILE type is NULL");
4478         return;
4479       }
4480 
4481       if (!Context.FILEDecl) {
4482         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4483           Context.setFILEDecl(Typedef->getDecl());
4484         else {
4485           const TagType *Tag = FileType->getAs<TagType>();
4486           if (!Tag) {
4487             Error("Invalid FILE type in AST file");
4488             return;
4489           }
4490           Context.setFILEDecl(Tag->getDecl());
4491         }
4492       }
4493     }
4494 
4495     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4496       QualType Jmp_bufType = GetType(Jmp_buf);
4497       if (Jmp_bufType.isNull()) {
4498         Error("jmp_buf type is NULL");
4499         return;
4500       }
4501 
4502       if (!Context.jmp_bufDecl) {
4503         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4504           Context.setjmp_bufDecl(Typedef->getDecl());
4505         else {
4506           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4507           if (!Tag) {
4508             Error("Invalid jmp_buf type in AST file");
4509             return;
4510           }
4511           Context.setjmp_bufDecl(Tag->getDecl());
4512         }
4513       }
4514     }
4515 
4516     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4517       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4518       if (Sigjmp_bufType.isNull()) {
4519         Error("sigjmp_buf type is NULL");
4520         return;
4521       }
4522 
4523       if (!Context.sigjmp_bufDecl) {
4524         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4525           Context.setsigjmp_bufDecl(Typedef->getDecl());
4526         else {
4527           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4528           assert(Tag && "Invalid sigjmp_buf type in AST file");
4529           Context.setsigjmp_bufDecl(Tag->getDecl());
4530         }
4531       }
4532     }
4533 
4534     if (unsigned ObjCIdRedef
4535           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4536       if (Context.ObjCIdRedefinitionType.isNull())
4537         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4538     }
4539 
4540     if (unsigned ObjCClassRedef
4541           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4542       if (Context.ObjCClassRedefinitionType.isNull())
4543         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4544     }
4545 
4546     if (unsigned ObjCSelRedef
4547           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4548       if (Context.ObjCSelRedefinitionType.isNull())
4549         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4550     }
4551 
4552     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4553       QualType Ucontext_tType = GetType(Ucontext_t);
4554       if (Ucontext_tType.isNull()) {
4555         Error("ucontext_t type is NULL");
4556         return;
4557       }
4558 
4559       if (!Context.ucontext_tDecl) {
4560         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4561           Context.setucontext_tDecl(Typedef->getDecl());
4562         else {
4563           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4564           assert(Tag && "Invalid ucontext_t type in AST file");
4565           Context.setucontext_tDecl(Tag->getDecl());
4566         }
4567       }
4568     }
4569   }
4570 
4571   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4572 
4573   // If there were any CUDA special declarations, deserialize them.
4574   if (!CUDASpecialDeclRefs.empty()) {
4575     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4576     Context.setcudaConfigureCallDecl(
4577                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4578   }
4579 
4580   // Re-export any modules that were imported by a non-module AST file.
4581   // FIXME: This does not make macro-only imports visible again.
4582   for (auto &Import : ImportedModules) {
4583     if (Module *Imported = getSubmodule(Import.ID)) {
4584       makeModuleVisible(Imported, Module::AllVisible,
4585                         /*ImportLoc=*/Import.ImportLoc);
4586       if (Import.ImportLoc.isValid())
4587         PP.makeModuleVisible(Imported, Import.ImportLoc);
4588       // FIXME: should we tell Sema to make the module visible too?
4589     }
4590   }
4591   ImportedModules.clear();
4592 }
4593 
4594 void ASTReader::finalizeForWriting() {
4595   // Nothing to do for now.
4596 }
4597 
4598 /// Reads and return the signature record from \p PCH's control block, or
4599 /// else returns 0.
4600 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4601   BitstreamCursor Stream(PCH);
4602   if (!startsWithASTFileMagic(Stream))
4603     return ASTFileSignature();
4604 
4605   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4606   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4607     return ASTFileSignature();
4608 
4609   // Scan for SIGNATURE inside the diagnostic options block.
4610   ASTReader::RecordData Record;
4611   while (true) {
4612     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4613     if (Entry.Kind != llvm::BitstreamEntry::Record)
4614       return ASTFileSignature();
4615 
4616     Record.clear();
4617     StringRef Blob;
4618     if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4619       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4620                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4621   }
4622 }
4623 
4624 /// Retrieve the name of the original source file name
4625 /// directly from the AST file, without actually loading the AST
4626 /// file.
4627 std::string ASTReader::getOriginalSourceFile(
4628     const std::string &ASTFileName, FileManager &FileMgr,
4629     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4630   // Open the AST file.
4631   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4632   if (!Buffer) {
4633     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4634         << ASTFileName << Buffer.getError().message();
4635     return std::string();
4636   }
4637 
4638   // Initialize the stream
4639   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4640 
4641   // Sniff for the signature.
4642   if (!startsWithASTFileMagic(Stream)) {
4643     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4644     return std::string();
4645   }
4646 
4647   // Scan for the CONTROL_BLOCK_ID block.
4648   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4649     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4650     return std::string();
4651   }
4652 
4653   // Scan for ORIGINAL_FILE inside the control block.
4654   RecordData Record;
4655   while (true) {
4656     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4657     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4658       return std::string();
4659 
4660     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4661       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4662       return std::string();
4663     }
4664 
4665     Record.clear();
4666     StringRef Blob;
4667     if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4668       return Blob.str();
4669   }
4670 }
4671 
4672 namespace {
4673 
4674   class SimplePCHValidator : public ASTReaderListener {
4675     const LangOptions &ExistingLangOpts;
4676     const TargetOptions &ExistingTargetOpts;
4677     const PreprocessorOptions &ExistingPPOpts;
4678     std::string ExistingModuleCachePath;
4679     FileManager &FileMgr;
4680 
4681   public:
4682     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4683                        const TargetOptions &ExistingTargetOpts,
4684                        const PreprocessorOptions &ExistingPPOpts,
4685                        StringRef ExistingModuleCachePath,
4686                        FileManager &FileMgr)
4687       : ExistingLangOpts(ExistingLangOpts),
4688         ExistingTargetOpts(ExistingTargetOpts),
4689         ExistingPPOpts(ExistingPPOpts),
4690         ExistingModuleCachePath(ExistingModuleCachePath),
4691         FileMgr(FileMgr) {}
4692 
4693     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4694                              bool AllowCompatibleDifferences) override {
4695       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4696                                   AllowCompatibleDifferences);
4697     }
4698 
4699     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4700                            bool AllowCompatibleDifferences) override {
4701       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4702                                 AllowCompatibleDifferences);
4703     }
4704 
4705     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4706                                  StringRef SpecificModuleCachePath,
4707                                  bool Complain) override {
4708       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4709                                       ExistingModuleCachePath,
4710                                       nullptr, ExistingLangOpts);
4711     }
4712 
4713     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4714                                  bool Complain,
4715                                  std::string &SuggestedPredefines) override {
4716       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
4717                                       SuggestedPredefines, ExistingLangOpts);
4718     }
4719   };
4720 
4721 } // namespace
4722 
4723 bool ASTReader::readASTFileControlBlock(
4724     StringRef Filename, FileManager &FileMgr,
4725     const PCHContainerReader &PCHContainerRdr,
4726     bool FindModuleFileExtensions,
4727     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
4728   // Open the AST file.
4729   // FIXME: This allows use of the VFS; we do not allow use of the
4730   // VFS when actually loading a module.
4731   auto Buffer = FileMgr.getBufferForFile(Filename);
4732   if (!Buffer) {
4733     return true;
4734   }
4735 
4736   // Initialize the stream
4737   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
4738   BitstreamCursor Stream(Bytes);
4739 
4740   // Sniff for the signature.
4741   if (!startsWithASTFileMagic(Stream))
4742     return true;
4743 
4744   // Scan for the CONTROL_BLOCK_ID block.
4745   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4746     return true;
4747 
4748   bool NeedsInputFiles = Listener.needsInputFileVisitation();
4749   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
4750   bool NeedsImports = Listener.needsImportVisitation();
4751   BitstreamCursor InputFilesCursor;
4752 
4753   RecordData Record;
4754   std::string ModuleDir;
4755   bool DoneWithControlBlock = false;
4756   while (!DoneWithControlBlock) {
4757     llvm::BitstreamEntry Entry = Stream.advance();
4758 
4759     switch (Entry.Kind) {
4760     case llvm::BitstreamEntry::SubBlock: {
4761       switch (Entry.ID) {
4762       case OPTIONS_BLOCK_ID: {
4763         std::string IgnoredSuggestedPredefines;
4764         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4765                              /*AllowCompatibleConfigurationMismatch*/ false,
4766                              Listener, IgnoredSuggestedPredefines) != Success)
4767           return true;
4768         break;
4769       }
4770 
4771       case INPUT_FILES_BLOCK_ID:
4772         InputFilesCursor = Stream;
4773         if (Stream.SkipBlock() ||
4774             (NeedsInputFiles &&
4775              ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4776           return true;
4777         break;
4778 
4779       default:
4780         if (Stream.SkipBlock())
4781           return true;
4782         break;
4783       }
4784 
4785       continue;
4786     }
4787 
4788     case llvm::BitstreamEntry::EndBlock:
4789       DoneWithControlBlock = true;
4790       break;
4791 
4792     case llvm::BitstreamEntry::Error:
4793       return true;
4794 
4795     case llvm::BitstreamEntry::Record:
4796       break;
4797     }
4798 
4799     if (DoneWithControlBlock) break;
4800 
4801     Record.clear();
4802     StringRef Blob;
4803     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4804     switch ((ControlRecordTypes)RecCode) {
4805     case METADATA:
4806       if (Record[0] != VERSION_MAJOR)
4807         return true;
4808       if (Listener.ReadFullVersionInformation(Blob))
4809         return true;
4810       break;
4811     case MODULE_NAME:
4812       Listener.ReadModuleName(Blob);
4813       break;
4814     case MODULE_DIRECTORY:
4815       ModuleDir = Blob;
4816       break;
4817     case MODULE_MAP_FILE: {
4818       unsigned Idx = 0;
4819       auto Path = ReadString(Record, Idx);
4820       ResolveImportedPath(Path, ModuleDir);
4821       Listener.ReadModuleMapFile(Path);
4822       break;
4823     }
4824     case INPUT_FILE_OFFSETS: {
4825       if (!NeedsInputFiles)
4826         break;
4827 
4828       unsigned NumInputFiles = Record[0];
4829       unsigned NumUserFiles = Record[1];
4830       const llvm::support::unaligned_uint64_t *InputFileOffs =
4831           (const llvm::support::unaligned_uint64_t *)Blob.data();
4832       for (unsigned I = 0; I != NumInputFiles; ++I) {
4833         // Go find this input file.
4834         bool isSystemFile = I >= NumUserFiles;
4835 
4836         if (isSystemFile && !NeedsSystemInputFiles)
4837           break; // the rest are system input files
4838 
4839         BitstreamCursor &Cursor = InputFilesCursor;
4840         SavedStreamPosition SavedPosition(Cursor);
4841         Cursor.JumpToBit(InputFileOffs[I]);
4842 
4843         unsigned Code = Cursor.ReadCode();
4844         RecordData Record;
4845         StringRef Blob;
4846         bool shouldContinue = false;
4847         switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4848         case INPUT_FILE:
4849           bool Overridden = static_cast<bool>(Record[3]);
4850           std::string Filename = Blob;
4851           ResolveImportedPath(Filename, ModuleDir);
4852           shouldContinue = Listener.visitInputFile(
4853               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
4854           break;
4855         }
4856         if (!shouldContinue)
4857           break;
4858       }
4859       break;
4860     }
4861 
4862     case IMPORTS: {
4863       if (!NeedsImports)
4864         break;
4865 
4866       unsigned Idx = 0, N = Record.size();
4867       while (Idx < N) {
4868         // Read information about the AST file.
4869         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
4870         std::string ModuleName = ReadString(Record, Idx);
4871         std::string Filename = ReadString(Record, Idx);
4872         ResolveImportedPath(Filename, ModuleDir);
4873         Listener.visitImport(ModuleName, Filename);
4874       }
4875       break;
4876     }
4877 
4878     default:
4879       // No other validation to perform.
4880       break;
4881     }
4882   }
4883 
4884   // Look for module file extension blocks, if requested.
4885   if (FindModuleFileExtensions) {
4886     BitstreamCursor SavedStream = Stream;
4887     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4888       bool DoneWithExtensionBlock = false;
4889       while (!DoneWithExtensionBlock) {
4890        llvm::BitstreamEntry Entry = Stream.advance();
4891 
4892        switch (Entry.Kind) {
4893        case llvm::BitstreamEntry::SubBlock:
4894          if (Stream.SkipBlock())
4895            return true;
4896 
4897          continue;
4898 
4899        case llvm::BitstreamEntry::EndBlock:
4900          DoneWithExtensionBlock = true;
4901          continue;
4902 
4903        case llvm::BitstreamEntry::Error:
4904          return true;
4905 
4906        case llvm::BitstreamEntry::Record:
4907          break;
4908        }
4909 
4910        Record.clear();
4911        StringRef Blob;
4912        unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4913        switch (RecCode) {
4914        case EXTENSION_METADATA: {
4915          ModuleFileExtensionMetadata Metadata;
4916          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4917            return true;
4918 
4919          Listener.readModuleFileExtension(Metadata);
4920          break;
4921        }
4922        }
4923       }
4924     }
4925     Stream = SavedStream;
4926   }
4927 
4928   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4929   if (readUnhashedControlBlockImpl(
4930           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
4931           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
4932           ValidateDiagnosticOptions) != Success)
4933     return true;
4934 
4935   return false;
4936 }
4937 
4938 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
4939                                     const PCHContainerReader &PCHContainerRdr,
4940                                     const LangOptions &LangOpts,
4941                                     const TargetOptions &TargetOpts,
4942                                     const PreprocessorOptions &PPOpts,
4943                                     StringRef ExistingModuleCachePath) {
4944   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4945                                ExistingModuleCachePath, FileMgr);
4946   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
4947                                   /*FindModuleFileExtensions=*/false,
4948                                   validator,
4949                                   /*ValidateDiagnosticOptions=*/true);
4950 }
4951 
4952 ASTReader::ASTReadResult
4953 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
4954   // Enter the submodule block.
4955   if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4956     Error("malformed submodule block record in AST file");
4957     return Failure;
4958   }
4959 
4960   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4961   bool First = true;
4962   Module *CurrentModule = nullptr;
4963   RecordData Record;
4964   while (true) {
4965     llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4966 
4967     switch (Entry.Kind) {
4968     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4969     case llvm::BitstreamEntry::Error:
4970       Error("malformed block record in AST file");
4971       return Failure;
4972     case llvm::BitstreamEntry::EndBlock:
4973       return Success;
4974     case llvm::BitstreamEntry::Record:
4975       // The interesting case.
4976       break;
4977     }
4978 
4979     // Read a record.
4980     StringRef Blob;
4981     Record.clear();
4982     auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4983 
4984     if ((Kind == SUBMODULE_METADATA) != First) {
4985       Error("submodule metadata record should be at beginning of block");
4986       return Failure;
4987     }
4988     First = false;
4989 
4990     // Submodule information is only valid if we have a current module.
4991     // FIXME: Should we error on these cases?
4992     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4993         Kind != SUBMODULE_DEFINITION)
4994       continue;
4995 
4996     switch (Kind) {
4997     default:  // Default behavior: ignore.
4998       break;
4999 
5000     case SUBMODULE_DEFINITION: {
5001       if (Record.size() < 12) {
5002         Error("malformed module definition");
5003         return Failure;
5004       }
5005 
5006       StringRef Name = Blob;
5007       unsigned Idx = 0;
5008       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5009       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5010       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5011       bool IsFramework = Record[Idx++];
5012       bool IsExplicit = Record[Idx++];
5013       bool IsSystem = Record[Idx++];
5014       bool IsExternC = Record[Idx++];
5015       bool InferSubmodules = Record[Idx++];
5016       bool InferExplicitSubmodules = Record[Idx++];
5017       bool InferExportWildcard = Record[Idx++];
5018       bool ConfigMacrosExhaustive = Record[Idx++];
5019       bool ModuleMapIsPrivate = Record[Idx++];
5020 
5021       Module *ParentModule = nullptr;
5022       if (Parent)
5023         ParentModule = getSubmodule(Parent);
5024 
5025       // Retrieve this (sub)module from the module map, creating it if
5026       // necessary.
5027       CurrentModule =
5028           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5029               .first;
5030 
5031       // FIXME: set the definition loc for CurrentModule, or call
5032       // ModMap.setInferredModuleAllowedBy()
5033 
5034       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5035       if (GlobalIndex >= SubmodulesLoaded.size() ||
5036           SubmodulesLoaded[GlobalIndex]) {
5037         Error("too many submodules");
5038         return Failure;
5039       }
5040 
5041       if (!ParentModule) {
5042         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5043           // Don't emit module relocation error if we have -fno-validate-pch
5044           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5045               CurFile != F.File) {
5046             if (!Diags.isDiagnosticInFlight()) {
5047               Diag(diag::err_module_file_conflict)
5048                 << CurrentModule->getTopLevelModuleName()
5049                 << CurFile->getName()
5050                 << F.File->getName();
5051             }
5052             return Failure;
5053           }
5054         }
5055 
5056         CurrentModule->setASTFile(F.File);
5057         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5058       }
5059 
5060       CurrentModule->Kind = Kind;
5061       CurrentModule->Signature = F.Signature;
5062       CurrentModule->IsFromModuleFile = true;
5063       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5064       CurrentModule->IsExternC = IsExternC;
5065       CurrentModule->InferSubmodules = InferSubmodules;
5066       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5067       CurrentModule->InferExportWildcard = InferExportWildcard;
5068       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5069       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5070       if (DeserializationListener)
5071         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5072 
5073       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5074 
5075       // Clear out data that will be replaced by what is in the module file.
5076       CurrentModule->LinkLibraries.clear();
5077       CurrentModule->ConfigMacros.clear();
5078       CurrentModule->UnresolvedConflicts.clear();
5079       CurrentModule->Conflicts.clear();
5080 
5081       // The module is available unless it's missing a requirement; relevant
5082       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5083       // Missing headers that were present when the module was built do not
5084       // make it unavailable -- if we got this far, this must be an explicitly
5085       // imported module file.
5086       CurrentModule->Requirements.clear();
5087       CurrentModule->MissingHeaders.clear();
5088       CurrentModule->IsMissingRequirement =
5089           ParentModule && ParentModule->IsMissingRequirement;
5090       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5091       break;
5092     }
5093 
5094     case SUBMODULE_UMBRELLA_HEADER: {
5095       std::string Filename = Blob;
5096       ResolveImportedPath(F, Filename);
5097       if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
5098         if (!CurrentModule->getUmbrellaHeader())
5099           ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5100         else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
5101           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5102             Error("mismatched umbrella headers in submodule");
5103           return OutOfDate;
5104         }
5105       }
5106       break;
5107     }
5108 
5109     case SUBMODULE_HEADER:
5110     case SUBMODULE_EXCLUDED_HEADER:
5111     case SUBMODULE_PRIVATE_HEADER:
5112       // We lazily associate headers with their modules via the HeaderInfo table.
5113       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5114       // of complete filenames or remove it entirely.
5115       break;
5116 
5117     case SUBMODULE_TEXTUAL_HEADER:
5118     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5119       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5120       // them here.
5121       break;
5122 
5123     case SUBMODULE_TOPHEADER:
5124       CurrentModule->addTopHeaderFilename(Blob);
5125       break;
5126 
5127     case SUBMODULE_UMBRELLA_DIR: {
5128       std::string Dirname = Blob;
5129       ResolveImportedPath(F, Dirname);
5130       if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5131         if (!CurrentModule->getUmbrellaDir())
5132           ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5133         else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
5134           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5135             Error("mismatched umbrella directories in submodule");
5136           return OutOfDate;
5137         }
5138       }
5139       break;
5140     }
5141 
5142     case SUBMODULE_METADATA: {
5143       F.BaseSubmoduleID = getTotalNumSubmodules();
5144       F.LocalNumSubmodules = Record[0];
5145       unsigned LocalBaseSubmoduleID = Record[1];
5146       if (F.LocalNumSubmodules > 0) {
5147         // Introduce the global -> local mapping for submodules within this
5148         // module.
5149         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5150 
5151         // Introduce the local -> global mapping for submodules within this
5152         // module.
5153         F.SubmoduleRemap.insertOrReplace(
5154           std::make_pair(LocalBaseSubmoduleID,
5155                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5156 
5157         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5158       }
5159       break;
5160     }
5161 
5162     case SUBMODULE_IMPORTS:
5163       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5164         UnresolvedModuleRef Unresolved;
5165         Unresolved.File = &F;
5166         Unresolved.Mod = CurrentModule;
5167         Unresolved.ID = Record[Idx];
5168         Unresolved.Kind = UnresolvedModuleRef::Import;
5169         Unresolved.IsWildcard = false;
5170         UnresolvedModuleRefs.push_back(Unresolved);
5171       }
5172       break;
5173 
5174     case SUBMODULE_EXPORTS:
5175       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5176         UnresolvedModuleRef Unresolved;
5177         Unresolved.File = &F;
5178         Unresolved.Mod = CurrentModule;
5179         Unresolved.ID = Record[Idx];
5180         Unresolved.Kind = UnresolvedModuleRef::Export;
5181         Unresolved.IsWildcard = Record[Idx + 1];
5182         UnresolvedModuleRefs.push_back(Unresolved);
5183       }
5184 
5185       // Once we've loaded the set of exports, there's no reason to keep
5186       // the parsed, unresolved exports around.
5187       CurrentModule->UnresolvedExports.clear();
5188       break;
5189 
5190     case SUBMODULE_REQUIRES:
5191       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5192                                     PP.getTargetInfo());
5193       break;
5194 
5195     case SUBMODULE_LINK_LIBRARY:
5196       ModMap.resolveLinkAsDependencies(CurrentModule);
5197       CurrentModule->LinkLibraries.push_back(
5198                                          Module::LinkLibrary(Blob, Record[0]));
5199       break;
5200 
5201     case SUBMODULE_CONFIG_MACRO:
5202       CurrentModule->ConfigMacros.push_back(Blob.str());
5203       break;
5204 
5205     case SUBMODULE_CONFLICT: {
5206       UnresolvedModuleRef Unresolved;
5207       Unresolved.File = &F;
5208       Unresolved.Mod = CurrentModule;
5209       Unresolved.ID = Record[0];
5210       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5211       Unresolved.IsWildcard = false;
5212       Unresolved.String = Blob;
5213       UnresolvedModuleRefs.push_back(Unresolved);
5214       break;
5215     }
5216 
5217     case SUBMODULE_INITIALIZERS: {
5218       if (!ContextObj)
5219         break;
5220       SmallVector<uint32_t, 16> Inits;
5221       for (auto &ID : Record)
5222         Inits.push_back(getGlobalDeclID(F, ID));
5223       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5224       break;
5225     }
5226 
5227     case SUBMODULE_EXPORT_AS:
5228       CurrentModule->ExportAsModule = Blob.str();
5229       ModMap.addLinkAsDependency(CurrentModule);
5230       break;
5231     }
5232   }
5233 }
5234 
5235 /// Parse the record that corresponds to a LangOptions data
5236 /// structure.
5237 ///
5238 /// This routine parses the language options from the AST file and then gives
5239 /// them to the AST listener if one is set.
5240 ///
5241 /// \returns true if the listener deems the file unacceptable, false otherwise.
5242 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5243                                      bool Complain,
5244                                      ASTReaderListener &Listener,
5245                                      bool AllowCompatibleDifferences) {
5246   LangOptions LangOpts;
5247   unsigned Idx = 0;
5248 #define LANGOPT(Name, Bits, Default, Description) \
5249   LangOpts.Name = Record[Idx++];
5250 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5251   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5252 #include "clang/Basic/LangOptions.def"
5253 #define SANITIZER(NAME, ID)                                                    \
5254   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5255 #include "clang/Basic/Sanitizers.def"
5256 
5257   for (unsigned N = Record[Idx++]; N; --N)
5258     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5259 
5260   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5261   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5262   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5263 
5264   LangOpts.CurrentModule = ReadString(Record, Idx);
5265 
5266   // Comment options.
5267   for (unsigned N = Record[Idx++]; N; --N) {
5268     LangOpts.CommentOpts.BlockCommandNames.push_back(
5269       ReadString(Record, Idx));
5270   }
5271   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5272 
5273   // OpenMP offloading options.
5274   for (unsigned N = Record[Idx++]; N; --N) {
5275     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5276   }
5277 
5278   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5279 
5280   return Listener.ReadLanguageOptions(LangOpts, Complain,
5281                                       AllowCompatibleDifferences);
5282 }
5283 
5284 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5285                                    ASTReaderListener &Listener,
5286                                    bool AllowCompatibleDifferences) {
5287   unsigned Idx = 0;
5288   TargetOptions TargetOpts;
5289   TargetOpts.Triple = ReadString(Record, Idx);
5290   TargetOpts.CPU = ReadString(Record, Idx);
5291   TargetOpts.ABI = ReadString(Record, Idx);
5292   for (unsigned N = Record[Idx++]; N; --N) {
5293     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5294   }
5295   for (unsigned N = Record[Idx++]; N; --N) {
5296     TargetOpts.Features.push_back(ReadString(Record, Idx));
5297   }
5298 
5299   return Listener.ReadTargetOptions(TargetOpts, Complain,
5300                                     AllowCompatibleDifferences);
5301 }
5302 
5303 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5304                                        ASTReaderListener &Listener) {
5305   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5306   unsigned Idx = 0;
5307 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5308 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5309   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5310 #include "clang/Basic/DiagnosticOptions.def"
5311 
5312   for (unsigned N = Record[Idx++]; N; --N)
5313     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5314   for (unsigned N = Record[Idx++]; N; --N)
5315     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5316 
5317   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5318 }
5319 
5320 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5321                                        ASTReaderListener &Listener) {
5322   FileSystemOptions FSOpts;
5323   unsigned Idx = 0;
5324   FSOpts.WorkingDir = ReadString(Record, Idx);
5325   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5326 }
5327 
5328 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5329                                          bool Complain,
5330                                          ASTReaderListener &Listener) {
5331   HeaderSearchOptions HSOpts;
5332   unsigned Idx = 0;
5333   HSOpts.Sysroot = ReadString(Record, Idx);
5334 
5335   // Include entries.
5336   for (unsigned N = Record[Idx++]; N; --N) {
5337     std::string Path = ReadString(Record, Idx);
5338     frontend::IncludeDirGroup Group
5339       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5340     bool IsFramework = Record[Idx++];
5341     bool IgnoreSysRoot = Record[Idx++];
5342     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5343                                     IgnoreSysRoot);
5344   }
5345 
5346   // System header prefixes.
5347   for (unsigned N = Record[Idx++]; N; --N) {
5348     std::string Prefix = ReadString(Record, Idx);
5349     bool IsSystemHeader = Record[Idx++];
5350     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5351   }
5352 
5353   HSOpts.ResourceDir = ReadString(Record, Idx);
5354   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5355   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5356   HSOpts.DisableModuleHash = Record[Idx++];
5357   HSOpts.ImplicitModuleMaps = Record[Idx++];
5358   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5359   HSOpts.UseBuiltinIncludes = Record[Idx++];
5360   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5361   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5362   HSOpts.UseLibcxx = Record[Idx++];
5363   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5364 
5365   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5366                                           Complain);
5367 }
5368 
5369 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5370                                          bool Complain,
5371                                          ASTReaderListener &Listener,
5372                                          std::string &SuggestedPredefines) {
5373   PreprocessorOptions PPOpts;
5374   unsigned Idx = 0;
5375 
5376   // Macro definitions/undefs
5377   for (unsigned N = Record[Idx++]; N; --N) {
5378     std::string Macro = ReadString(Record, Idx);
5379     bool IsUndef = Record[Idx++];
5380     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5381   }
5382 
5383   // Includes
5384   for (unsigned N = Record[Idx++]; N; --N) {
5385     PPOpts.Includes.push_back(ReadString(Record, Idx));
5386   }
5387 
5388   // Macro Includes
5389   for (unsigned N = Record[Idx++]; N; --N) {
5390     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5391   }
5392 
5393   PPOpts.UsePredefines = Record[Idx++];
5394   PPOpts.DetailedRecord = Record[Idx++];
5395   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5396   PPOpts.ObjCXXARCStandardLibrary =
5397     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5398   SuggestedPredefines.clear();
5399   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5400                                           SuggestedPredefines);
5401 }
5402 
5403 std::pair<ModuleFile *, unsigned>
5404 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5405   GlobalPreprocessedEntityMapType::iterator
5406   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5407   assert(I != GlobalPreprocessedEntityMap.end() &&
5408          "Corrupted global preprocessed entity map");
5409   ModuleFile *M = I->second;
5410   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5411   return std::make_pair(M, LocalIndex);
5412 }
5413 
5414 llvm::iterator_range<PreprocessingRecord::iterator>
5415 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5416   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5417     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5418                                              Mod.NumPreprocessedEntities);
5419 
5420   return llvm::make_range(PreprocessingRecord::iterator(),
5421                           PreprocessingRecord::iterator());
5422 }
5423 
5424 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5425 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5426   return llvm::make_range(
5427       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5428       ModuleDeclIterator(this, &Mod,
5429                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5430 }
5431 
5432 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5433   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5434   assert(I != GlobalSkippedRangeMap.end() &&
5435     "Corrupted global skipped range map");
5436   ModuleFile *M = I->second;
5437   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5438   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5439   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5440   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5441                     TranslateSourceLocation(*M, RawRange.getEnd()));
5442   assert(Range.isValid());
5443   return Range;
5444 }
5445 
5446 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5447   PreprocessedEntityID PPID = Index+1;
5448   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5449   ModuleFile &M = *PPInfo.first;
5450   unsigned LocalIndex = PPInfo.second;
5451   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5452 
5453   if (!PP.getPreprocessingRecord()) {
5454     Error("no preprocessing record");
5455     return nullptr;
5456   }
5457 
5458   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5459   M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
5460 
5461   llvm::BitstreamEntry Entry =
5462     M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5463   if (Entry.Kind != llvm::BitstreamEntry::Record)
5464     return nullptr;
5465 
5466   // Read the record.
5467   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5468                     TranslateSourceLocation(M, PPOffs.getEnd()));
5469   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5470   StringRef Blob;
5471   RecordData Record;
5472   PreprocessorDetailRecordTypes RecType =
5473     (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
5474                                           Entry.ID, Record, &Blob);
5475   switch (RecType) {
5476   case PPD_MACRO_EXPANSION: {
5477     bool isBuiltin = Record[0];
5478     IdentifierInfo *Name = nullptr;
5479     MacroDefinitionRecord *Def = nullptr;
5480     if (isBuiltin)
5481       Name = getLocalIdentifier(M, Record[1]);
5482     else {
5483       PreprocessedEntityID GlobalID =
5484           getGlobalPreprocessedEntityID(M, Record[1]);
5485       Def = cast<MacroDefinitionRecord>(
5486           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5487     }
5488 
5489     MacroExpansion *ME;
5490     if (isBuiltin)
5491       ME = new (PPRec) MacroExpansion(Name, Range);
5492     else
5493       ME = new (PPRec) MacroExpansion(Def, Range);
5494 
5495     return ME;
5496   }
5497 
5498   case PPD_MACRO_DEFINITION: {
5499     // Decode the identifier info and then check again; if the macro is
5500     // still defined and associated with the identifier,
5501     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5502     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5503 
5504     if (DeserializationListener)
5505       DeserializationListener->MacroDefinitionRead(PPID, MD);
5506 
5507     return MD;
5508   }
5509 
5510   case PPD_INCLUSION_DIRECTIVE: {
5511     const char *FullFileNameStart = Blob.data() + Record[0];
5512     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5513     const FileEntry *File = nullptr;
5514     if (!FullFileName.empty())
5515       File = PP.getFileManager().getFile(FullFileName);
5516 
5517     // FIXME: Stable encoding
5518     InclusionDirective::InclusionKind Kind
5519       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5520     InclusionDirective *ID
5521       = new (PPRec) InclusionDirective(PPRec, Kind,
5522                                        StringRef(Blob.data(), Record[0]),
5523                                        Record[1], Record[3],
5524                                        File,
5525                                        Range);
5526     return ID;
5527   }
5528   }
5529 
5530   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5531 }
5532 
5533 /// Find the next module that contains entities and return the ID
5534 /// of the first entry.
5535 ///
5536 /// \param SLocMapI points at a chunk of a module that contains no
5537 /// preprocessed entities or the entities it contains are not the ones we are
5538 /// looking for.
5539 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5540                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5541   ++SLocMapI;
5542   for (GlobalSLocOffsetMapType::const_iterator
5543          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5544     ModuleFile &M = *SLocMapI->second;
5545     if (M.NumPreprocessedEntities)
5546       return M.BasePreprocessedEntityID;
5547   }
5548 
5549   return getTotalNumPreprocessedEntities();
5550 }
5551 
5552 namespace {
5553 
5554 struct PPEntityComp {
5555   const ASTReader &Reader;
5556   ModuleFile &M;
5557 
5558   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5559 
5560   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5561     SourceLocation LHS = getLoc(L);
5562     SourceLocation RHS = getLoc(R);
5563     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5564   }
5565 
5566   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5567     SourceLocation LHS = getLoc(L);
5568     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5569   }
5570 
5571   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5572     SourceLocation RHS = getLoc(R);
5573     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5574   }
5575 
5576   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5577     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5578   }
5579 };
5580 
5581 } // namespace
5582 
5583 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5584                                                        bool EndsAfter) const {
5585   if (SourceMgr.isLocalSourceLocation(Loc))
5586     return getTotalNumPreprocessedEntities();
5587 
5588   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5589       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5590   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5591          "Corrupted global sloc offset map");
5592 
5593   if (SLocMapI->second->NumPreprocessedEntities == 0)
5594     return findNextPreprocessedEntity(SLocMapI);
5595 
5596   ModuleFile &M = *SLocMapI->second;
5597 
5598   using pp_iterator = const PPEntityOffset *;
5599 
5600   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5601   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5602 
5603   size_t Count = M.NumPreprocessedEntities;
5604   size_t Half;
5605   pp_iterator First = pp_begin;
5606   pp_iterator PPI;
5607 
5608   if (EndsAfter) {
5609     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5610                            PPEntityComp(*this, M));
5611   } else {
5612     // Do a binary search manually instead of using std::lower_bound because
5613     // The end locations of entities may be unordered (when a macro expansion
5614     // is inside another macro argument), but for this case it is not important
5615     // whether we get the first macro expansion or its containing macro.
5616     while (Count > 0) {
5617       Half = Count / 2;
5618       PPI = First;
5619       std::advance(PPI, Half);
5620       if (SourceMgr.isBeforeInTranslationUnit(
5621               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
5622         First = PPI;
5623         ++First;
5624         Count = Count - Half - 1;
5625       } else
5626         Count = Half;
5627     }
5628   }
5629 
5630   if (PPI == pp_end)
5631     return findNextPreprocessedEntity(SLocMapI);
5632 
5633   return M.BasePreprocessedEntityID + (PPI - pp_begin);
5634 }
5635 
5636 /// Returns a pair of [Begin, End) indices of preallocated
5637 /// preprocessed entities that \arg Range encompasses.
5638 std::pair<unsigned, unsigned>
5639     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5640   if (Range.isInvalid())
5641     return std::make_pair(0,0);
5642   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5643 
5644   PreprocessedEntityID BeginID =
5645       findPreprocessedEntity(Range.getBegin(), false);
5646   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
5647   return std::make_pair(BeginID, EndID);
5648 }
5649 
5650 /// Optionally returns true or false if the preallocated preprocessed
5651 /// entity with index \arg Index came from file \arg FID.
5652 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
5653                                                              FileID FID) {
5654   if (FID.isInvalid())
5655     return false;
5656 
5657   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5658   ModuleFile &M = *PPInfo.first;
5659   unsigned LocalIndex = PPInfo.second;
5660   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5661 
5662   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
5663   if (Loc.isInvalid())
5664     return false;
5665 
5666   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5667     return true;
5668   else
5669     return false;
5670 }
5671 
5672 namespace {
5673 
5674   /// Visitor used to search for information about a header file.
5675   class HeaderFileInfoVisitor {
5676     const FileEntry *FE;
5677     Optional<HeaderFileInfo> HFI;
5678 
5679   public:
5680     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
5681 
5682     bool operator()(ModuleFile &M) {
5683       HeaderFileInfoLookupTable *Table
5684         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5685       if (!Table)
5686         return false;
5687 
5688       // Look in the on-disk hash table for an entry for this file name.
5689       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
5690       if (Pos == Table->end())
5691         return false;
5692 
5693       HFI = *Pos;
5694       return true;
5695     }
5696 
5697     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
5698   };
5699 
5700 } // namespace
5701 
5702 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
5703   HeaderFileInfoVisitor Visitor(FE);
5704   ModuleMgr.visit(Visitor);
5705   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
5706     return *HFI;
5707 
5708   return HeaderFileInfo();
5709 }
5710 
5711 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5712   using DiagState = DiagnosticsEngine::DiagState;
5713   SmallVector<DiagState *, 32> DiagStates;
5714 
5715   for (ModuleFile &F : ModuleMgr) {
5716     unsigned Idx = 0;
5717     auto &Record = F.PragmaDiagMappings;
5718     if (Record.empty())
5719       continue;
5720 
5721     DiagStates.clear();
5722 
5723     auto ReadDiagState =
5724         [&](const DiagState &BasedOn, SourceLocation Loc,
5725             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
5726       unsigned BackrefID = Record[Idx++];
5727       if (BackrefID != 0)
5728         return DiagStates[BackrefID - 1];
5729 
5730       // A new DiagState was created here.
5731       Diag.DiagStates.push_back(BasedOn);
5732       DiagState *NewState = &Diag.DiagStates.back();
5733       DiagStates.push_back(NewState);
5734       unsigned Size = Record[Idx++];
5735       assert(Idx + Size * 2 <= Record.size() &&
5736              "Invalid data, not enough diag/map pairs");
5737       while (Size--) {
5738         unsigned DiagID = Record[Idx++];
5739         DiagnosticMapping NewMapping =
5740             DiagnosticMapping::deserialize(Record[Idx++]);
5741         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
5742           continue;
5743 
5744         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
5745 
5746         // If this mapping was specified as a warning but the severity was
5747         // upgraded due to diagnostic settings, simulate the current diagnostic
5748         // settings (and use a warning).
5749         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
5750           NewMapping.setSeverity(diag::Severity::Warning);
5751           NewMapping.setUpgradedFromWarning(false);
5752         }
5753 
5754         Mapping = NewMapping;
5755       }
5756       return NewState;
5757     };
5758 
5759     // Read the first state.
5760     DiagState *FirstState;
5761     if (F.Kind == MK_ImplicitModule) {
5762       // Implicitly-built modules are reused with different diagnostic
5763       // settings.  Use the initial diagnostic state from Diag to simulate this
5764       // compilation's diagnostic settings.
5765       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
5766       DiagStates.push_back(FirstState);
5767 
5768       // Skip the initial diagnostic state from the serialized module.
5769       assert(Record[1] == 0 &&
5770              "Invalid data, unexpected backref in initial state");
5771       Idx = 3 + Record[2] * 2;
5772       assert(Idx < Record.size() &&
5773              "Invalid data, not enough state change pairs in initial state");
5774     } else if (F.isModule()) {
5775       // For an explicit module, preserve the flags from the module build
5776       // command line (-w, -Weverything, -Werror, ...) along with any explicit
5777       // -Wblah flags.
5778       unsigned Flags = Record[Idx++];
5779       DiagState Initial;
5780       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
5781       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
5782       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
5783       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
5784       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
5785       Initial.ExtBehavior = (diag::Severity)Flags;
5786       FirstState = ReadDiagState(Initial, SourceLocation(), true);
5787 
5788       assert(F.OriginalSourceFileID.isValid());
5789 
5790       // Set up the root buffer of the module to start with the initial
5791       // diagnostic state of the module itself, to cover files that contain no
5792       // explicit transitions (for which we did not serialize anything).
5793       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
5794           .StateTransitions.push_back({FirstState, 0});
5795     } else {
5796       // For prefix ASTs, start with whatever the user configured on the
5797       // command line.
5798       Idx++; // Skip flags.
5799       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
5800                                  SourceLocation(), false);
5801     }
5802 
5803     // Read the state transitions.
5804     unsigned NumLocations = Record[Idx++];
5805     while (NumLocations--) {
5806       assert(Idx < Record.size() &&
5807              "Invalid data, missing pragma diagnostic states");
5808       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
5809       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
5810       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
5811       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
5812       unsigned Transitions = Record[Idx++];
5813 
5814       // Note that we don't need to set up Parent/ParentOffset here, because
5815       // we won't be changing the diagnostic state within imported FileIDs
5816       // (other than perhaps appending to the main source file, which has no
5817       // parent).
5818       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
5819       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
5820       for (unsigned I = 0; I != Transitions; ++I) {
5821         unsigned Offset = Record[Idx++];
5822         auto *State =
5823             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
5824         F.StateTransitions.push_back({State, Offset});
5825       }
5826     }
5827 
5828     // Read the final state.
5829     assert(Idx < Record.size() &&
5830            "Invalid data, missing final pragma diagnostic state");
5831     SourceLocation CurStateLoc =
5832         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5833     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
5834 
5835     if (!F.isModule()) {
5836       Diag.DiagStatesByLoc.CurDiagState = CurState;
5837       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
5838 
5839       // Preserve the property that the imaginary root file describes the
5840       // current state.
5841       FileID NullFile;
5842       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
5843       if (T.empty())
5844         T.push_back({CurState, 0});
5845       else
5846         T[0].State = CurState;
5847     }
5848 
5849     // Don't try to read these mappings again.
5850     Record.clear();
5851   }
5852 }
5853 
5854 /// Get the correct cursor and offset for loading a type.
5855 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5856   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5857   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5858   ModuleFile *M = I->second;
5859   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5860 }
5861 
5862 /// Read and return the type with the given index..
5863 ///
5864 /// The index is the type ID, shifted and minus the number of predefs. This
5865 /// routine actually reads the record corresponding to the type at the given
5866 /// location. It is a helper routine for GetType, which deals with reading type
5867 /// IDs.
5868 QualType ASTReader::readTypeRecord(unsigned Index) {
5869   assert(ContextObj && "reading type with no AST context");
5870   ASTContext &Context = *ContextObj;
5871   RecordLocation Loc = TypeCursorForIndex(Index);
5872   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
5873 
5874   // Keep track of where we are in the stream, then jump back there
5875   // after reading this type.
5876   SavedStreamPosition SavedPosition(DeclsCursor);
5877 
5878   ReadingKindTracker ReadingKind(Read_Type, *this);
5879 
5880   // Note that we are loading a type record.
5881   Deserializing AType(this);
5882 
5883   unsigned Idx = 0;
5884   DeclsCursor.JumpToBit(Loc.Offset);
5885   RecordData Record;
5886   unsigned Code = DeclsCursor.ReadCode();
5887   switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
5888   case TYPE_EXT_QUAL: {
5889     if (Record.size() != 2) {
5890       Error("Incorrect encoding of extended qualifier type");
5891       return QualType();
5892     }
5893     QualType Base = readType(*Loc.F, Record, Idx);
5894     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5895     return Context.getQualifiedType(Base, Quals);
5896   }
5897 
5898   case TYPE_COMPLEX: {
5899     if (Record.size() != 1) {
5900       Error("Incorrect encoding of complex type");
5901       return QualType();
5902     }
5903     QualType ElemType = readType(*Loc.F, Record, Idx);
5904     return Context.getComplexType(ElemType);
5905   }
5906 
5907   case TYPE_POINTER: {
5908     if (Record.size() != 1) {
5909       Error("Incorrect encoding of pointer type");
5910       return QualType();
5911     }
5912     QualType PointeeType = readType(*Loc.F, Record, Idx);
5913     return Context.getPointerType(PointeeType);
5914   }
5915 
5916   case TYPE_DECAYED: {
5917     if (Record.size() != 1) {
5918       Error("Incorrect encoding of decayed type");
5919       return QualType();
5920     }
5921     QualType OriginalType = readType(*Loc.F, Record, Idx);
5922     QualType DT = Context.getAdjustedParameterType(OriginalType);
5923     if (!isa<DecayedType>(DT))
5924       Error("Decayed type does not decay");
5925     return DT;
5926   }
5927 
5928   case TYPE_ADJUSTED: {
5929     if (Record.size() != 2) {
5930       Error("Incorrect encoding of adjusted type");
5931       return QualType();
5932     }
5933     QualType OriginalTy = readType(*Loc.F, Record, Idx);
5934     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5935     return Context.getAdjustedType(OriginalTy, AdjustedTy);
5936   }
5937 
5938   case TYPE_BLOCK_POINTER: {
5939     if (Record.size() != 1) {
5940       Error("Incorrect encoding of block pointer type");
5941       return QualType();
5942     }
5943     QualType PointeeType = readType(*Loc.F, Record, Idx);
5944     return Context.getBlockPointerType(PointeeType);
5945   }
5946 
5947   case TYPE_LVALUE_REFERENCE: {
5948     if (Record.size() != 2) {
5949       Error("Incorrect encoding of lvalue reference type");
5950       return QualType();
5951     }
5952     QualType PointeeType = readType(*Loc.F, Record, Idx);
5953     return Context.getLValueReferenceType(PointeeType, Record[1]);
5954   }
5955 
5956   case TYPE_RVALUE_REFERENCE: {
5957     if (Record.size() != 1) {
5958       Error("Incorrect encoding of rvalue reference type");
5959       return QualType();
5960     }
5961     QualType PointeeType = readType(*Loc.F, Record, Idx);
5962     return Context.getRValueReferenceType(PointeeType);
5963   }
5964 
5965   case TYPE_MEMBER_POINTER: {
5966     if (Record.size() != 2) {
5967       Error("Incorrect encoding of member pointer type");
5968       return QualType();
5969     }
5970     QualType PointeeType = readType(*Loc.F, Record, Idx);
5971     QualType ClassType = readType(*Loc.F, Record, Idx);
5972     if (PointeeType.isNull() || ClassType.isNull())
5973       return QualType();
5974 
5975     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5976   }
5977 
5978   case TYPE_CONSTANT_ARRAY: {
5979     QualType ElementType = readType(*Loc.F, Record, Idx);
5980     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5981     unsigned IndexTypeQuals = Record[2];
5982     unsigned Idx = 3;
5983     llvm::APInt Size = ReadAPInt(Record, Idx);
5984     return Context.getConstantArrayType(ElementType, Size,
5985                                          ASM, IndexTypeQuals);
5986   }
5987 
5988   case TYPE_INCOMPLETE_ARRAY: {
5989     QualType ElementType = readType(*Loc.F, Record, Idx);
5990     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5991     unsigned IndexTypeQuals = Record[2];
5992     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5993   }
5994 
5995   case TYPE_VARIABLE_ARRAY: {
5996     QualType ElementType = readType(*Loc.F, Record, Idx);
5997     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5998     unsigned IndexTypeQuals = Record[2];
5999     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6000     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6001     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6002                                          ASM, IndexTypeQuals,
6003                                          SourceRange(LBLoc, RBLoc));
6004   }
6005 
6006   case TYPE_VECTOR: {
6007     if (Record.size() != 3) {
6008       Error("incorrect encoding of vector type in AST file");
6009       return QualType();
6010     }
6011 
6012     QualType ElementType = readType(*Loc.F, Record, Idx);
6013     unsigned NumElements = Record[1];
6014     unsigned VecKind = Record[2];
6015     return Context.getVectorType(ElementType, NumElements,
6016                                   (VectorType::VectorKind)VecKind);
6017   }
6018 
6019   case TYPE_EXT_VECTOR: {
6020     if (Record.size() != 3) {
6021       Error("incorrect encoding of extended vector type in AST file");
6022       return QualType();
6023     }
6024 
6025     QualType ElementType = readType(*Loc.F, Record, Idx);
6026     unsigned NumElements = Record[1];
6027     return Context.getExtVectorType(ElementType, NumElements);
6028   }
6029 
6030   case TYPE_FUNCTION_NO_PROTO: {
6031     if (Record.size() != 8) {
6032       Error("incorrect encoding of no-proto function type");
6033       return QualType();
6034     }
6035     QualType ResultType = readType(*Loc.F, Record, Idx);
6036     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6037                                (CallingConv)Record[4], Record[5], Record[6],
6038                                Record[7]);
6039     return Context.getFunctionNoProtoType(ResultType, Info);
6040   }
6041 
6042   case TYPE_FUNCTION_PROTO: {
6043     QualType ResultType = readType(*Loc.F, Record, Idx);
6044 
6045     FunctionProtoType::ExtProtoInfo EPI;
6046     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6047                                         /*hasregparm*/ Record[2],
6048                                         /*regparm*/ Record[3],
6049                                         static_cast<CallingConv>(Record[4]),
6050                                         /*produces*/ Record[5],
6051                                         /*nocallersavedregs*/ Record[6],
6052                                         /*nocfcheck*/ Record[7]);
6053 
6054     unsigned Idx = 8;
6055 
6056     EPI.Variadic = Record[Idx++];
6057     EPI.HasTrailingReturn = Record[Idx++];
6058     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6059     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6060     SmallVector<QualType, 8> ExceptionStorage;
6061     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6062 
6063     unsigned NumParams = Record[Idx++];
6064     SmallVector<QualType, 16> ParamTypes;
6065     for (unsigned I = 0; I != NumParams; ++I)
6066       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6067 
6068     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6069     if (Idx != Record.size()) {
6070       for (unsigned I = 0; I != NumParams; ++I)
6071         ExtParameterInfos.push_back(
6072           FunctionProtoType::ExtParameterInfo
6073                            ::getFromOpaqueValue(Record[Idx++]));
6074       EPI.ExtParameterInfos = ExtParameterInfos.data();
6075     }
6076 
6077     assert(Idx == Record.size());
6078 
6079     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6080   }
6081 
6082   case TYPE_UNRESOLVED_USING: {
6083     unsigned Idx = 0;
6084     return Context.getTypeDeclType(
6085                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6086   }
6087 
6088   case TYPE_TYPEDEF: {
6089     if (Record.size() != 2) {
6090       Error("incorrect encoding of typedef type");
6091       return QualType();
6092     }
6093     unsigned Idx = 0;
6094     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6095     QualType Canonical = readType(*Loc.F, Record, Idx);
6096     if (!Canonical.isNull())
6097       Canonical = Context.getCanonicalType(Canonical);
6098     return Context.getTypedefType(Decl, Canonical);
6099   }
6100 
6101   case TYPE_TYPEOF_EXPR:
6102     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6103 
6104   case TYPE_TYPEOF: {
6105     if (Record.size() != 1) {
6106       Error("incorrect encoding of typeof(type) in AST file");
6107       return QualType();
6108     }
6109     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6110     return Context.getTypeOfType(UnderlyingType);
6111   }
6112 
6113   case TYPE_DECLTYPE: {
6114     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6115     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6116   }
6117 
6118   case TYPE_UNARY_TRANSFORM: {
6119     QualType BaseType = readType(*Loc.F, Record, Idx);
6120     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6121     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6122     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6123   }
6124 
6125   case TYPE_AUTO: {
6126     QualType Deduced = readType(*Loc.F, Record, Idx);
6127     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6128     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6129     return Context.getAutoType(Deduced, Keyword, IsDependent);
6130   }
6131 
6132   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6133     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6134     QualType Deduced = readType(*Loc.F, Record, Idx);
6135     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6136     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6137                                                         IsDependent);
6138   }
6139 
6140   case TYPE_RECORD: {
6141     if (Record.size() != 2) {
6142       Error("incorrect encoding of record type");
6143       return QualType();
6144     }
6145     unsigned Idx = 0;
6146     bool IsDependent = Record[Idx++];
6147     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6148     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6149     QualType T = Context.getRecordType(RD);
6150     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6151     return T;
6152   }
6153 
6154   case TYPE_ENUM: {
6155     if (Record.size() != 2) {
6156       Error("incorrect encoding of enum type");
6157       return QualType();
6158     }
6159     unsigned Idx = 0;
6160     bool IsDependent = Record[Idx++];
6161     QualType T
6162       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6163     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6164     return T;
6165   }
6166 
6167   case TYPE_ATTRIBUTED: {
6168     if (Record.size() != 3) {
6169       Error("incorrect encoding of attributed type");
6170       return QualType();
6171     }
6172     QualType modifiedType = readType(*Loc.F, Record, Idx);
6173     QualType equivalentType = readType(*Loc.F, Record, Idx);
6174     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6175     return Context.getAttributedType(kind, modifiedType, equivalentType);
6176   }
6177 
6178   case TYPE_PAREN: {
6179     if (Record.size() != 1) {
6180       Error("incorrect encoding of paren type");
6181       return QualType();
6182     }
6183     QualType InnerType = readType(*Loc.F, Record, Idx);
6184     return Context.getParenType(InnerType);
6185   }
6186 
6187   case TYPE_PACK_EXPANSION: {
6188     if (Record.size() != 2) {
6189       Error("incorrect encoding of pack expansion type");
6190       return QualType();
6191     }
6192     QualType Pattern = readType(*Loc.F, Record, Idx);
6193     if (Pattern.isNull())
6194       return QualType();
6195     Optional<unsigned> NumExpansions;
6196     if (Record[1])
6197       NumExpansions = Record[1] - 1;
6198     return Context.getPackExpansionType(Pattern, NumExpansions);
6199   }
6200 
6201   case TYPE_ELABORATED: {
6202     unsigned Idx = 0;
6203     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6204     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6205     QualType NamedType = readType(*Loc.F, Record, Idx);
6206     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6207     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6208   }
6209 
6210   case TYPE_OBJC_INTERFACE: {
6211     unsigned Idx = 0;
6212     ObjCInterfaceDecl *ItfD
6213       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6214     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6215   }
6216 
6217   case TYPE_OBJC_TYPE_PARAM: {
6218     unsigned Idx = 0;
6219     ObjCTypeParamDecl *Decl
6220       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6221     unsigned NumProtos = Record[Idx++];
6222     SmallVector<ObjCProtocolDecl*, 4> Protos;
6223     for (unsigned I = 0; I != NumProtos; ++I)
6224       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6225     return Context.getObjCTypeParamType(Decl, Protos);
6226   }
6227 
6228   case TYPE_OBJC_OBJECT: {
6229     unsigned Idx = 0;
6230     QualType Base = readType(*Loc.F, Record, Idx);
6231     unsigned NumTypeArgs = Record[Idx++];
6232     SmallVector<QualType, 4> TypeArgs;
6233     for (unsigned I = 0; I != NumTypeArgs; ++I)
6234       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6235     unsigned NumProtos = Record[Idx++];
6236     SmallVector<ObjCProtocolDecl*, 4> Protos;
6237     for (unsigned I = 0; I != NumProtos; ++I)
6238       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6239     bool IsKindOf = Record[Idx++];
6240     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6241   }
6242 
6243   case TYPE_OBJC_OBJECT_POINTER: {
6244     unsigned Idx = 0;
6245     QualType Pointee = readType(*Loc.F, Record, Idx);
6246     return Context.getObjCObjectPointerType(Pointee);
6247   }
6248 
6249   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6250     unsigned Idx = 0;
6251     QualType Parm = readType(*Loc.F, Record, Idx);
6252     QualType Replacement = readType(*Loc.F, Record, Idx);
6253     return Context.getSubstTemplateTypeParmType(
6254         cast<TemplateTypeParmType>(Parm),
6255         Context.getCanonicalType(Replacement));
6256   }
6257 
6258   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6259     unsigned Idx = 0;
6260     QualType Parm = readType(*Loc.F, Record, Idx);
6261     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6262     return Context.getSubstTemplateTypeParmPackType(
6263                                                cast<TemplateTypeParmType>(Parm),
6264                                                      ArgPack);
6265   }
6266 
6267   case TYPE_INJECTED_CLASS_NAME: {
6268     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6269     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6270     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6271     // for AST reading, too much interdependencies.
6272     const Type *T = nullptr;
6273     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6274       if (const Type *Existing = DI->getTypeForDecl()) {
6275         T = Existing;
6276         break;
6277       }
6278     }
6279     if (!T) {
6280       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6281       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6282         DI->setTypeForDecl(T);
6283     }
6284     return QualType(T, 0);
6285   }
6286 
6287   case TYPE_TEMPLATE_TYPE_PARM: {
6288     unsigned Idx = 0;
6289     unsigned Depth = Record[Idx++];
6290     unsigned Index = Record[Idx++];
6291     bool Pack = Record[Idx++];
6292     TemplateTypeParmDecl *D
6293       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6294     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6295   }
6296 
6297   case TYPE_DEPENDENT_NAME: {
6298     unsigned Idx = 0;
6299     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6300     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6301     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6302     QualType Canon = readType(*Loc.F, Record, Idx);
6303     if (!Canon.isNull())
6304       Canon = Context.getCanonicalType(Canon);
6305     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6306   }
6307 
6308   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6309     unsigned Idx = 0;
6310     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6311     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6312     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6313     unsigned NumArgs = Record[Idx++];
6314     SmallVector<TemplateArgument, 8> Args;
6315     Args.reserve(NumArgs);
6316     while (NumArgs--)
6317       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6318     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6319                                                           Args);
6320   }
6321 
6322   case TYPE_DEPENDENT_SIZED_ARRAY: {
6323     unsigned Idx = 0;
6324 
6325     // ArrayType
6326     QualType ElementType = readType(*Loc.F, Record, Idx);
6327     ArrayType::ArraySizeModifier ASM
6328       = (ArrayType::ArraySizeModifier)Record[Idx++];
6329     unsigned IndexTypeQuals = Record[Idx++];
6330 
6331     // DependentSizedArrayType
6332     Expr *NumElts = ReadExpr(*Loc.F);
6333     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6334 
6335     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6336                                                IndexTypeQuals, Brackets);
6337   }
6338 
6339   case TYPE_TEMPLATE_SPECIALIZATION: {
6340     unsigned Idx = 0;
6341     bool IsDependent = Record[Idx++];
6342     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6343     SmallVector<TemplateArgument, 8> Args;
6344     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6345     QualType Underlying = readType(*Loc.F, Record, Idx);
6346     QualType T;
6347     if (Underlying.isNull())
6348       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6349     else
6350       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6351     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6352     return T;
6353   }
6354 
6355   case TYPE_ATOMIC: {
6356     if (Record.size() != 1) {
6357       Error("Incorrect encoding of atomic type");
6358       return QualType();
6359     }
6360     QualType ValueType = readType(*Loc.F, Record, Idx);
6361     return Context.getAtomicType(ValueType);
6362   }
6363 
6364   case TYPE_PIPE: {
6365     if (Record.size() != 2) {
6366       Error("Incorrect encoding of pipe type");
6367       return QualType();
6368     }
6369 
6370     // Reading the pipe element type.
6371     QualType ElementType = readType(*Loc.F, Record, Idx);
6372     unsigned ReadOnly = Record[1];
6373     return Context.getPipeType(ElementType, ReadOnly);
6374   }
6375 
6376   case TYPE_DEPENDENT_SIZED_VECTOR: {
6377     unsigned Idx = 0;
6378     QualType ElementType = readType(*Loc.F, Record, Idx);
6379     Expr *SizeExpr = ReadExpr(*Loc.F);
6380     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6381     unsigned VecKind = Record[Idx];
6382 
6383     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6384                                                (VectorType::VectorKind)VecKind);
6385   }
6386 
6387   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6388     unsigned Idx = 0;
6389 
6390     // DependentSizedExtVectorType
6391     QualType ElementType = readType(*Loc.F, Record, Idx);
6392     Expr *SizeExpr = ReadExpr(*Loc.F);
6393     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6394 
6395     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6396                                                   AttrLoc);
6397   }
6398 
6399   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6400     unsigned Idx = 0;
6401 
6402     // DependentAddressSpaceType
6403     QualType PointeeType = readType(*Loc.F, Record, Idx);
6404     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6405     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6406 
6407     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6408                                                    AttrLoc);
6409   }
6410   }
6411   llvm_unreachable("Invalid TypeCode!");
6412 }
6413 
6414 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6415                                   SmallVectorImpl<QualType> &Exceptions,
6416                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6417                                   const RecordData &Record, unsigned &Idx) {
6418   ExceptionSpecificationType EST =
6419       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6420   ESI.Type = EST;
6421   if (EST == EST_Dynamic) {
6422     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6423       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6424     ESI.Exceptions = Exceptions;
6425   } else if (isComputedNoexcept(EST)) {
6426     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6427   } else if (EST == EST_Uninstantiated) {
6428     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6429     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6430   } else if (EST == EST_Unevaluated) {
6431     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6432   }
6433 }
6434 
6435 namespace clang {
6436 
6437 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6438   ModuleFile *F;
6439   ASTReader *Reader;
6440   const ASTReader::RecordData &Record;
6441   unsigned &Idx;
6442 
6443   SourceLocation ReadSourceLocation() {
6444     return Reader->ReadSourceLocation(*F, Record, Idx);
6445   }
6446 
6447   TypeSourceInfo *GetTypeSourceInfo() {
6448     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6449   }
6450 
6451   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6452     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6453   }
6454 
6455   Attr *ReadAttr() {
6456     return Reader->ReadAttr(*F, Record, Idx);
6457   }
6458 
6459 public:
6460   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6461                 const ASTReader::RecordData &Record, unsigned &Idx)
6462       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6463 
6464   // We want compile-time assurance that we've enumerated all of
6465   // these, so unfortunately we have to declare them first, then
6466   // define them out-of-line.
6467 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6468 #define TYPELOC(CLASS, PARENT) \
6469   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6470 #include "clang/AST/TypeLocNodes.def"
6471 
6472   void VisitFunctionTypeLoc(FunctionTypeLoc);
6473   void VisitArrayTypeLoc(ArrayTypeLoc);
6474 };
6475 
6476 } // namespace clang
6477 
6478 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6479   // nothing to do
6480 }
6481 
6482 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6483   TL.setBuiltinLoc(ReadSourceLocation());
6484   if (TL.needsExtraLocalData()) {
6485     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6486     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6487     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6488     TL.setModeAttr(Record[Idx++]);
6489   }
6490 }
6491 
6492 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6493   TL.setNameLoc(ReadSourceLocation());
6494 }
6495 
6496 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6497   TL.setStarLoc(ReadSourceLocation());
6498 }
6499 
6500 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6501   // nothing to do
6502 }
6503 
6504 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6505   // nothing to do
6506 }
6507 
6508 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6509   TL.setCaretLoc(ReadSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6513   TL.setAmpLoc(ReadSourceLocation());
6514 }
6515 
6516 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6517   TL.setAmpAmpLoc(ReadSourceLocation());
6518 }
6519 
6520 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6521   TL.setStarLoc(ReadSourceLocation());
6522   TL.setClassTInfo(GetTypeSourceInfo());
6523 }
6524 
6525 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6526   TL.setLBracketLoc(ReadSourceLocation());
6527   TL.setRBracketLoc(ReadSourceLocation());
6528   if (Record[Idx++])
6529     TL.setSizeExpr(Reader->ReadExpr(*F));
6530   else
6531     TL.setSizeExpr(nullptr);
6532 }
6533 
6534 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6535   VisitArrayTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6539   VisitArrayTypeLoc(TL);
6540 }
6541 
6542 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6543   VisitArrayTypeLoc(TL);
6544 }
6545 
6546 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6547                                             DependentSizedArrayTypeLoc TL) {
6548   VisitArrayTypeLoc(TL);
6549 }
6550 
6551 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6552     DependentAddressSpaceTypeLoc TL) {
6553 
6554     TL.setAttrNameLoc(ReadSourceLocation());
6555     SourceRange range;
6556     range.setBegin(ReadSourceLocation());
6557     range.setEnd(ReadSourceLocation());
6558     TL.setAttrOperandParensRange(range);
6559     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6560 }
6561 
6562 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6563                                         DependentSizedExtVectorTypeLoc TL) {
6564   TL.setNameLoc(ReadSourceLocation());
6565 }
6566 
6567 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6568   TL.setNameLoc(ReadSourceLocation());
6569 }
6570 
6571 void TypeLocReader::VisitDependentVectorTypeLoc(
6572     DependentVectorTypeLoc TL) {
6573   TL.setNameLoc(ReadSourceLocation());
6574 }
6575 
6576 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6577   TL.setNameLoc(ReadSourceLocation());
6578 }
6579 
6580 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6581   TL.setLocalRangeBegin(ReadSourceLocation());
6582   TL.setLParenLoc(ReadSourceLocation());
6583   TL.setRParenLoc(ReadSourceLocation());
6584   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
6585                                        Reader->ReadSourceLocation(*F, Record, Idx)));
6586   TL.setLocalRangeEnd(ReadSourceLocation());
6587   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6588     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
6589   }
6590 }
6591 
6592 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6593   VisitFunctionTypeLoc(TL);
6594 }
6595 
6596 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6597   VisitFunctionTypeLoc(TL);
6598 }
6599 
6600 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6601   TL.setNameLoc(ReadSourceLocation());
6602 }
6603 
6604 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6605   TL.setNameLoc(ReadSourceLocation());
6606 }
6607 
6608 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6609   TL.setTypeofLoc(ReadSourceLocation());
6610   TL.setLParenLoc(ReadSourceLocation());
6611   TL.setRParenLoc(ReadSourceLocation());
6612 }
6613 
6614 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6615   TL.setTypeofLoc(ReadSourceLocation());
6616   TL.setLParenLoc(ReadSourceLocation());
6617   TL.setRParenLoc(ReadSourceLocation());
6618   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6619 }
6620 
6621 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6622   TL.setNameLoc(ReadSourceLocation());
6623 }
6624 
6625 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6626   TL.setKWLoc(ReadSourceLocation());
6627   TL.setLParenLoc(ReadSourceLocation());
6628   TL.setRParenLoc(ReadSourceLocation());
6629   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6630 }
6631 
6632 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6633   TL.setNameLoc(ReadSourceLocation());
6634 }
6635 
6636 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6637     DeducedTemplateSpecializationTypeLoc TL) {
6638   TL.setTemplateNameLoc(ReadSourceLocation());
6639 }
6640 
6641 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6642   TL.setNameLoc(ReadSourceLocation());
6643 }
6644 
6645 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6646   TL.setNameLoc(ReadSourceLocation());
6647 }
6648 
6649 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6650   TL.setAttr(ReadAttr());
6651 }
6652 
6653 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6654   TL.setNameLoc(ReadSourceLocation());
6655 }
6656 
6657 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6658                                             SubstTemplateTypeParmTypeLoc TL) {
6659   TL.setNameLoc(ReadSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6663                                           SubstTemplateTypeParmPackTypeLoc TL) {
6664   TL.setNameLoc(ReadSourceLocation());
6665 }
6666 
6667 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6668                                            TemplateSpecializationTypeLoc TL) {
6669   TL.setTemplateKeywordLoc(ReadSourceLocation());
6670   TL.setTemplateNameLoc(ReadSourceLocation());
6671   TL.setLAngleLoc(ReadSourceLocation());
6672   TL.setRAngleLoc(ReadSourceLocation());
6673   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6674     TL.setArgLocInfo(
6675         i,
6676         Reader->GetTemplateArgumentLocInfo(
6677             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
6678 }
6679 
6680 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6681   TL.setLParenLoc(ReadSourceLocation());
6682   TL.setRParenLoc(ReadSourceLocation());
6683 }
6684 
6685 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6686   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6687   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6688 }
6689 
6690 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6691   TL.setNameLoc(ReadSourceLocation());
6692 }
6693 
6694 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6695   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6696   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6697   TL.setNameLoc(ReadSourceLocation());
6698 }
6699 
6700 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6701        DependentTemplateSpecializationTypeLoc TL) {
6702   TL.setElaboratedKeywordLoc(ReadSourceLocation());
6703   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6704   TL.setTemplateKeywordLoc(ReadSourceLocation());
6705   TL.setTemplateNameLoc(ReadSourceLocation());
6706   TL.setLAngleLoc(ReadSourceLocation());
6707   TL.setRAngleLoc(ReadSourceLocation());
6708   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6709     TL.setArgLocInfo(
6710         I,
6711         Reader->GetTemplateArgumentLocInfo(
6712             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
6713 }
6714 
6715 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6716   TL.setEllipsisLoc(ReadSourceLocation());
6717 }
6718 
6719 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6720   TL.setNameLoc(ReadSourceLocation());
6721 }
6722 
6723 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6724   if (TL.getNumProtocols()) {
6725     TL.setProtocolLAngleLoc(ReadSourceLocation());
6726     TL.setProtocolRAngleLoc(ReadSourceLocation());
6727   }
6728   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6729     TL.setProtocolLoc(i, ReadSourceLocation());
6730 }
6731 
6732 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6733   TL.setHasBaseTypeAsWritten(Record[Idx++]);
6734   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
6735   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
6736   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6737     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6738   TL.setProtocolLAngleLoc(ReadSourceLocation());
6739   TL.setProtocolRAngleLoc(ReadSourceLocation());
6740   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6741     TL.setProtocolLoc(i, ReadSourceLocation());
6742 }
6743 
6744 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6745   TL.setStarLoc(ReadSourceLocation());
6746 }
6747 
6748 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6749   TL.setKWLoc(ReadSourceLocation());
6750   TL.setLParenLoc(ReadSourceLocation());
6751   TL.setRParenLoc(ReadSourceLocation());
6752 }
6753 
6754 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6755   TL.setKWLoc(ReadSourceLocation());
6756 }
6757 
6758 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
6759                             unsigned &Idx, TypeLoc TL) {
6760   TypeLocReader TLR(F, *this, Record, Idx);
6761   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6762     TLR.Visit(TL);
6763 }
6764 
6765 TypeSourceInfo *
6766 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
6767                              unsigned &Idx) {
6768   QualType InfoTy = readType(F, Record, Idx);
6769   if (InfoTy.isNull())
6770     return nullptr;
6771 
6772   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6773   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
6774   return TInfo;
6775 }
6776 
6777 QualType ASTReader::GetType(TypeID ID) {
6778   assert(ContextObj && "reading type with no AST context");
6779   ASTContext &Context = *ContextObj;
6780 
6781   unsigned FastQuals = ID & Qualifiers::FastMask;
6782   unsigned Index = ID >> Qualifiers::FastWidth;
6783 
6784   if (Index < NUM_PREDEF_TYPE_IDS) {
6785     QualType T;
6786     switch ((PredefinedTypeIDs)Index) {
6787     case PREDEF_TYPE_NULL_ID:
6788       return QualType();
6789     case PREDEF_TYPE_VOID_ID:
6790       T = Context.VoidTy;
6791       break;
6792     case PREDEF_TYPE_BOOL_ID:
6793       T = Context.BoolTy;
6794       break;
6795     case PREDEF_TYPE_CHAR_U_ID:
6796     case PREDEF_TYPE_CHAR_S_ID:
6797       // FIXME: Check that the signedness of CharTy is correct!
6798       T = Context.CharTy;
6799       break;
6800     case PREDEF_TYPE_UCHAR_ID:
6801       T = Context.UnsignedCharTy;
6802       break;
6803     case PREDEF_TYPE_USHORT_ID:
6804       T = Context.UnsignedShortTy;
6805       break;
6806     case PREDEF_TYPE_UINT_ID:
6807       T = Context.UnsignedIntTy;
6808       break;
6809     case PREDEF_TYPE_ULONG_ID:
6810       T = Context.UnsignedLongTy;
6811       break;
6812     case PREDEF_TYPE_ULONGLONG_ID:
6813       T = Context.UnsignedLongLongTy;
6814       break;
6815     case PREDEF_TYPE_UINT128_ID:
6816       T = Context.UnsignedInt128Ty;
6817       break;
6818     case PREDEF_TYPE_SCHAR_ID:
6819       T = Context.SignedCharTy;
6820       break;
6821     case PREDEF_TYPE_WCHAR_ID:
6822       T = Context.WCharTy;
6823       break;
6824     case PREDEF_TYPE_SHORT_ID:
6825       T = Context.ShortTy;
6826       break;
6827     case PREDEF_TYPE_INT_ID:
6828       T = Context.IntTy;
6829       break;
6830     case PREDEF_TYPE_LONG_ID:
6831       T = Context.LongTy;
6832       break;
6833     case PREDEF_TYPE_LONGLONG_ID:
6834       T = Context.LongLongTy;
6835       break;
6836     case PREDEF_TYPE_INT128_ID:
6837       T = Context.Int128Ty;
6838       break;
6839     case PREDEF_TYPE_HALF_ID:
6840       T = Context.HalfTy;
6841       break;
6842     case PREDEF_TYPE_FLOAT_ID:
6843       T = Context.FloatTy;
6844       break;
6845     case PREDEF_TYPE_DOUBLE_ID:
6846       T = Context.DoubleTy;
6847       break;
6848     case PREDEF_TYPE_LONGDOUBLE_ID:
6849       T = Context.LongDoubleTy;
6850       break;
6851     case PREDEF_TYPE_SHORT_ACCUM_ID:
6852       T = Context.ShortAccumTy;
6853       break;
6854     case PREDEF_TYPE_ACCUM_ID:
6855       T = Context.AccumTy;
6856       break;
6857     case PREDEF_TYPE_LONG_ACCUM_ID:
6858       T = Context.LongAccumTy;
6859       break;
6860     case PREDEF_TYPE_USHORT_ACCUM_ID:
6861       T = Context.UnsignedShortAccumTy;
6862       break;
6863     case PREDEF_TYPE_UACCUM_ID:
6864       T = Context.UnsignedAccumTy;
6865       break;
6866     case PREDEF_TYPE_ULONG_ACCUM_ID:
6867       T = Context.UnsignedLongAccumTy;
6868       break;
6869     case PREDEF_TYPE_SHORT_FRACT_ID:
6870       T = Context.ShortFractTy;
6871       break;
6872     case PREDEF_TYPE_FRACT_ID:
6873       T = Context.FractTy;
6874       break;
6875     case PREDEF_TYPE_LONG_FRACT_ID:
6876       T = Context.LongFractTy;
6877       break;
6878     case PREDEF_TYPE_USHORT_FRACT_ID:
6879       T = Context.UnsignedShortFractTy;
6880       break;
6881     case PREDEF_TYPE_UFRACT_ID:
6882       T = Context.UnsignedFractTy;
6883       break;
6884     case PREDEF_TYPE_ULONG_FRACT_ID:
6885       T = Context.UnsignedLongFractTy;
6886       break;
6887     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6888       T = Context.SatShortAccumTy;
6889       break;
6890     case PREDEF_TYPE_SAT_ACCUM_ID:
6891       T = Context.SatAccumTy;
6892       break;
6893     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6894       T = Context.SatLongAccumTy;
6895       break;
6896     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6897       T = Context.SatUnsignedShortAccumTy;
6898       break;
6899     case PREDEF_TYPE_SAT_UACCUM_ID:
6900       T = Context.SatUnsignedAccumTy;
6901       break;
6902     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6903       T = Context.SatUnsignedLongAccumTy;
6904       break;
6905     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6906       T = Context.SatShortFractTy;
6907       break;
6908     case PREDEF_TYPE_SAT_FRACT_ID:
6909       T = Context.SatFractTy;
6910       break;
6911     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6912       T = Context.SatLongFractTy;
6913       break;
6914     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6915       T = Context.SatUnsignedShortFractTy;
6916       break;
6917     case PREDEF_TYPE_SAT_UFRACT_ID:
6918       T = Context.SatUnsignedFractTy;
6919       break;
6920     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6921       T = Context.SatUnsignedLongFractTy;
6922       break;
6923     case PREDEF_TYPE_FLOAT16_ID:
6924       T = Context.Float16Ty;
6925       break;
6926     case PREDEF_TYPE_FLOAT128_ID:
6927       T = Context.Float128Ty;
6928       break;
6929     case PREDEF_TYPE_OVERLOAD_ID:
6930       T = Context.OverloadTy;
6931       break;
6932     case PREDEF_TYPE_BOUND_MEMBER:
6933       T = Context.BoundMemberTy;
6934       break;
6935     case PREDEF_TYPE_PSEUDO_OBJECT:
6936       T = Context.PseudoObjectTy;
6937       break;
6938     case PREDEF_TYPE_DEPENDENT_ID:
6939       T = Context.DependentTy;
6940       break;
6941     case PREDEF_TYPE_UNKNOWN_ANY:
6942       T = Context.UnknownAnyTy;
6943       break;
6944     case PREDEF_TYPE_NULLPTR_ID:
6945       T = Context.NullPtrTy;
6946       break;
6947     case PREDEF_TYPE_CHAR8_ID:
6948       T = Context.Char8Ty;
6949       break;
6950     case PREDEF_TYPE_CHAR16_ID:
6951       T = Context.Char16Ty;
6952       break;
6953     case PREDEF_TYPE_CHAR32_ID:
6954       T = Context.Char32Ty;
6955       break;
6956     case PREDEF_TYPE_OBJC_ID:
6957       T = Context.ObjCBuiltinIdTy;
6958       break;
6959     case PREDEF_TYPE_OBJC_CLASS:
6960       T = Context.ObjCBuiltinClassTy;
6961       break;
6962     case PREDEF_TYPE_OBJC_SEL:
6963       T = Context.ObjCBuiltinSelTy;
6964       break;
6965 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6966     case PREDEF_TYPE_##Id##_ID: \
6967       T = Context.SingletonId; \
6968       break;
6969 #include "clang/Basic/OpenCLImageTypes.def"
6970 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6971     case PREDEF_TYPE_##Id##_ID: \
6972       T = Context.Id##Ty; \
6973       break;
6974 #include "clang/Basic/OpenCLExtensionTypes.def"
6975     case PREDEF_TYPE_SAMPLER_ID:
6976       T = Context.OCLSamplerTy;
6977       break;
6978     case PREDEF_TYPE_EVENT_ID:
6979       T = Context.OCLEventTy;
6980       break;
6981     case PREDEF_TYPE_CLK_EVENT_ID:
6982       T = Context.OCLClkEventTy;
6983       break;
6984     case PREDEF_TYPE_QUEUE_ID:
6985       T = Context.OCLQueueTy;
6986       break;
6987     case PREDEF_TYPE_RESERVE_ID_ID:
6988       T = Context.OCLReserveIDTy;
6989       break;
6990     case PREDEF_TYPE_AUTO_DEDUCT:
6991       T = Context.getAutoDeductType();
6992       break;
6993     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6994       T = Context.getAutoRRefDeductType();
6995       break;
6996     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6997       T = Context.ARCUnbridgedCastTy;
6998       break;
6999     case PREDEF_TYPE_BUILTIN_FN:
7000       T = Context.BuiltinFnTy;
7001       break;
7002     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7003       T = Context.OMPArraySectionTy;
7004       break;
7005     }
7006 
7007     assert(!T.isNull() && "Unknown predefined type");
7008     return T.withFastQualifiers(FastQuals);
7009   }
7010 
7011   Index -= NUM_PREDEF_TYPE_IDS;
7012   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7013   if (TypesLoaded[Index].isNull()) {
7014     TypesLoaded[Index] = readTypeRecord(Index);
7015     if (TypesLoaded[Index].isNull())
7016       return QualType();
7017 
7018     TypesLoaded[Index]->setFromAST();
7019     if (DeserializationListener)
7020       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7021                                         TypesLoaded[Index]);
7022   }
7023 
7024   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7025 }
7026 
7027 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7028   return GetType(getGlobalTypeID(F, LocalID));
7029 }
7030 
7031 serialization::TypeID
7032 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7033   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7034   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7035 
7036   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7037     return LocalID;
7038 
7039   if (!F.ModuleOffsetMap.empty())
7040     ReadModuleOffsetMap(F);
7041 
7042   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7043     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7044   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7045 
7046   unsigned GlobalIndex = LocalIndex + I->second;
7047   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7048 }
7049 
7050 TemplateArgumentLocInfo
7051 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7052                                       TemplateArgument::ArgKind Kind,
7053                                       const RecordData &Record,
7054                                       unsigned &Index) {
7055   switch (Kind) {
7056   case TemplateArgument::Expression:
7057     return ReadExpr(F);
7058   case TemplateArgument::Type:
7059     return GetTypeSourceInfo(F, Record, Index);
7060   case TemplateArgument::Template: {
7061     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7062                                                                      Index);
7063     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7064     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7065                                    SourceLocation());
7066   }
7067   case TemplateArgument::TemplateExpansion: {
7068     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7069                                                                      Index);
7070     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7071     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7072     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7073                                    EllipsisLoc);
7074   }
7075   case TemplateArgument::Null:
7076   case TemplateArgument::Integral:
7077   case TemplateArgument::Declaration:
7078   case TemplateArgument::NullPtr:
7079   case TemplateArgument::Pack:
7080     // FIXME: Is this right?
7081     return TemplateArgumentLocInfo();
7082   }
7083   llvm_unreachable("unexpected template argument loc");
7084 }
7085 
7086 TemplateArgumentLoc
7087 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7088                                    const RecordData &Record, unsigned &Index) {
7089   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7090 
7091   if (Arg.getKind() == TemplateArgument::Expression) {
7092     if (Record[Index++]) // bool InfoHasSameExpr.
7093       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7094   }
7095   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7096                                                              Record, Index));
7097 }
7098 
7099 const ASTTemplateArgumentListInfo*
7100 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7101                                            const RecordData &Record,
7102                                            unsigned &Index) {
7103   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7104   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7105   unsigned NumArgsAsWritten = Record[Index++];
7106   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7107   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7108     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7109   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7110 }
7111 
7112 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7113   return GetDecl(ID);
7114 }
7115 
7116 void ASTReader::CompleteRedeclChain(const Decl *D) {
7117   if (NumCurrentElementsDeserializing) {
7118     // We arrange to not care about the complete redeclaration chain while we're
7119     // deserializing. Just remember that the AST has marked this one as complete
7120     // but that it's not actually complete yet, so we know we still need to
7121     // complete it later.
7122     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7123     return;
7124   }
7125 
7126   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7127 
7128   // If this is a named declaration, complete it by looking it up
7129   // within its context.
7130   //
7131   // FIXME: Merging a function definition should merge
7132   // all mergeable entities within it.
7133   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7134       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7135     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7136       if (!getContext().getLangOpts().CPlusPlus &&
7137           isa<TranslationUnitDecl>(DC)) {
7138         // Outside of C++, we don't have a lookup table for the TU, so update
7139         // the identifier instead. (For C++ modules, we don't store decls
7140         // in the serialized identifier table, so we do the lookup in the TU.)
7141         auto *II = Name.getAsIdentifierInfo();
7142         assert(II && "non-identifier name in C?");
7143         if (II->isOutOfDate())
7144           updateOutOfDateIdentifier(*II);
7145       } else
7146         DC->lookup(Name);
7147     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7148       // Find all declarations of this kind from the relevant context.
7149       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7150         auto *DC = cast<DeclContext>(DCDecl);
7151         SmallVector<Decl*, 8> Decls;
7152         FindExternalLexicalDecls(
7153             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7154       }
7155     }
7156   }
7157 
7158   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7159     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7160   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7161     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7162   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7163     if (auto *Template = FD->getPrimaryTemplate())
7164       Template->LoadLazySpecializations();
7165   }
7166 }
7167 
7168 CXXCtorInitializer **
7169 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7170   RecordLocation Loc = getLocalBitOffset(Offset);
7171   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7172   SavedStreamPosition SavedPosition(Cursor);
7173   Cursor.JumpToBit(Loc.Offset);
7174   ReadingKindTracker ReadingKind(Read_Decl, *this);
7175 
7176   RecordData Record;
7177   unsigned Code = Cursor.ReadCode();
7178   unsigned RecCode = Cursor.readRecord(Code, Record);
7179   if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
7180     Error("malformed AST file: missing C++ ctor initializers");
7181     return nullptr;
7182   }
7183 
7184   unsigned Idx = 0;
7185   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7186 }
7187 
7188 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7189   assert(ContextObj && "reading base specifiers with no AST context");
7190   ASTContext &Context = *ContextObj;
7191 
7192   RecordLocation Loc = getLocalBitOffset(Offset);
7193   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7194   SavedStreamPosition SavedPosition(Cursor);
7195   Cursor.JumpToBit(Loc.Offset);
7196   ReadingKindTracker ReadingKind(Read_Decl, *this);
7197   RecordData Record;
7198   unsigned Code = Cursor.ReadCode();
7199   unsigned RecCode = Cursor.readRecord(Code, Record);
7200   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7201     Error("malformed AST file: missing C++ base specifiers");
7202     return nullptr;
7203   }
7204 
7205   unsigned Idx = 0;
7206   unsigned NumBases = Record[Idx++];
7207   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7208   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7209   for (unsigned I = 0; I != NumBases; ++I)
7210     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7211   return Bases;
7212 }
7213 
7214 serialization::DeclID
7215 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7216   if (LocalID < NUM_PREDEF_DECL_IDS)
7217     return LocalID;
7218 
7219   if (!F.ModuleOffsetMap.empty())
7220     ReadModuleOffsetMap(F);
7221 
7222   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7223     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7224   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7225 
7226   return LocalID + I->second;
7227 }
7228 
7229 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7230                                    ModuleFile &M) const {
7231   // Predefined decls aren't from any module.
7232   if (ID < NUM_PREDEF_DECL_IDS)
7233     return false;
7234 
7235   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7236          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7237 }
7238 
7239 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7240   if (!D->isFromASTFile())
7241     return nullptr;
7242   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7243   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7244   return I->second;
7245 }
7246 
7247 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7248   if (ID < NUM_PREDEF_DECL_IDS)
7249     return SourceLocation();
7250 
7251   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7252 
7253   if (Index > DeclsLoaded.size()) {
7254     Error("declaration ID out-of-range for AST file");
7255     return SourceLocation();
7256   }
7257 
7258   if (Decl *D = DeclsLoaded[Index])
7259     return D->getLocation();
7260 
7261   SourceLocation Loc;
7262   DeclCursorForID(ID, Loc);
7263   return Loc;
7264 }
7265 
7266 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7267   switch (ID) {
7268   case PREDEF_DECL_NULL_ID:
7269     return nullptr;
7270 
7271   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7272     return Context.getTranslationUnitDecl();
7273 
7274   case PREDEF_DECL_OBJC_ID_ID:
7275     return Context.getObjCIdDecl();
7276 
7277   case PREDEF_DECL_OBJC_SEL_ID:
7278     return Context.getObjCSelDecl();
7279 
7280   case PREDEF_DECL_OBJC_CLASS_ID:
7281     return Context.getObjCClassDecl();
7282 
7283   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7284     return Context.getObjCProtocolDecl();
7285 
7286   case PREDEF_DECL_INT_128_ID:
7287     return Context.getInt128Decl();
7288 
7289   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7290     return Context.getUInt128Decl();
7291 
7292   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7293     return Context.getObjCInstanceTypeDecl();
7294 
7295   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7296     return Context.getBuiltinVaListDecl();
7297 
7298   case PREDEF_DECL_VA_LIST_TAG:
7299     return Context.getVaListTagDecl();
7300 
7301   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7302     return Context.getBuiltinMSVaListDecl();
7303 
7304   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7305     return Context.getExternCContextDecl();
7306 
7307   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7308     return Context.getMakeIntegerSeqDecl();
7309 
7310   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7311     return Context.getCFConstantStringDecl();
7312 
7313   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7314     return Context.getCFConstantStringTagDecl();
7315 
7316   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7317     return Context.getTypePackElementDecl();
7318   }
7319   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7320 }
7321 
7322 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7323   assert(ContextObj && "reading decl with no AST context");
7324   if (ID < NUM_PREDEF_DECL_IDS) {
7325     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7326     if (D) {
7327       // Track that we have merged the declaration with ID \p ID into the
7328       // pre-existing predefined declaration \p D.
7329       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7330       if (Merged.empty())
7331         Merged.push_back(ID);
7332     }
7333     return D;
7334   }
7335 
7336   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7337 
7338   if (Index >= DeclsLoaded.size()) {
7339     assert(0 && "declaration ID out-of-range for AST file");
7340     Error("declaration ID out-of-range for AST file");
7341     return nullptr;
7342   }
7343 
7344   return DeclsLoaded[Index];
7345 }
7346 
7347 Decl *ASTReader::GetDecl(DeclID ID) {
7348   if (ID < NUM_PREDEF_DECL_IDS)
7349     return GetExistingDecl(ID);
7350 
7351   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7352 
7353   if (Index >= DeclsLoaded.size()) {
7354     assert(0 && "declaration ID out-of-range for AST file");
7355     Error("declaration ID out-of-range for AST file");
7356     return nullptr;
7357   }
7358 
7359   if (!DeclsLoaded[Index]) {
7360     ReadDeclRecord(ID);
7361     if (DeserializationListener)
7362       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7363   }
7364 
7365   return DeclsLoaded[Index];
7366 }
7367 
7368 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7369                                                   DeclID GlobalID) {
7370   if (GlobalID < NUM_PREDEF_DECL_IDS)
7371     return GlobalID;
7372 
7373   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7374   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7375   ModuleFile *Owner = I->second;
7376 
7377   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7378     = M.GlobalToLocalDeclIDs.find(Owner);
7379   if (Pos == M.GlobalToLocalDeclIDs.end())
7380     return 0;
7381 
7382   return GlobalID - Owner->BaseDeclID + Pos->second;
7383 }
7384 
7385 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7386                                             const RecordData &Record,
7387                                             unsigned &Idx) {
7388   if (Idx >= Record.size()) {
7389     Error("Corrupted AST file");
7390     return 0;
7391   }
7392 
7393   return getGlobalDeclID(F, Record[Idx++]);
7394 }
7395 
7396 /// Resolve the offset of a statement into a statement.
7397 ///
7398 /// This operation will read a new statement from the external
7399 /// source each time it is called, and is meant to be used via a
7400 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7401 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7402   // Switch case IDs are per Decl.
7403   ClearSwitchCaseIDs();
7404 
7405   // Offset here is a global offset across the entire chain.
7406   RecordLocation Loc = getLocalBitOffset(Offset);
7407   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
7408   assert(NumCurrentElementsDeserializing == 0 &&
7409          "should not be called while already deserializing");
7410   Deserializing D(this);
7411   return ReadStmtFromStream(*Loc.F);
7412 }
7413 
7414 void ASTReader::FindExternalLexicalDecls(
7415     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7416     SmallVectorImpl<Decl *> &Decls) {
7417   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7418 
7419   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7420     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7421     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7422       auto K = (Decl::Kind)+LexicalDecls[I];
7423       if (!IsKindWeWant(K))
7424         continue;
7425 
7426       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7427 
7428       // Don't add predefined declarations to the lexical context more
7429       // than once.
7430       if (ID < NUM_PREDEF_DECL_IDS) {
7431         if (PredefsVisited[ID])
7432           continue;
7433 
7434         PredefsVisited[ID] = true;
7435       }
7436 
7437       if (Decl *D = GetLocalDecl(*M, ID)) {
7438         assert(D->getKind() == K && "wrong kind for lexical decl");
7439         if (!DC->isDeclInLexicalTraversal(D))
7440           Decls.push_back(D);
7441       }
7442     }
7443   };
7444 
7445   if (isa<TranslationUnitDecl>(DC)) {
7446     for (auto Lexical : TULexicalDecls)
7447       Visit(Lexical.first, Lexical.second);
7448   } else {
7449     auto I = LexicalDecls.find(DC);
7450     if (I != LexicalDecls.end())
7451       Visit(I->second.first, I->second.second);
7452   }
7453 
7454   ++NumLexicalDeclContextsRead;
7455 }
7456 
7457 namespace {
7458 
7459 class DeclIDComp {
7460   ASTReader &Reader;
7461   ModuleFile &Mod;
7462 
7463 public:
7464   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7465 
7466   bool operator()(LocalDeclID L, LocalDeclID R) const {
7467     SourceLocation LHS = getLocation(L);
7468     SourceLocation RHS = getLocation(R);
7469     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7470   }
7471 
7472   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7473     SourceLocation RHS = getLocation(R);
7474     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7475   }
7476 
7477   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7478     SourceLocation LHS = getLocation(L);
7479     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7480   }
7481 
7482   SourceLocation getLocation(LocalDeclID ID) const {
7483     return Reader.getSourceManager().getFileLoc(
7484             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7485   }
7486 };
7487 
7488 } // namespace
7489 
7490 void ASTReader::FindFileRegionDecls(FileID File,
7491                                     unsigned Offset, unsigned Length,
7492                                     SmallVectorImpl<Decl *> &Decls) {
7493   SourceManager &SM = getSourceManager();
7494 
7495   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7496   if (I == FileDeclIDs.end())
7497     return;
7498 
7499   FileDeclsInfo &DInfo = I->second;
7500   if (DInfo.Decls.empty())
7501     return;
7502 
7503   SourceLocation
7504     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7505   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7506 
7507   DeclIDComp DIDComp(*this, *DInfo.Mod);
7508   ArrayRef<serialization::LocalDeclID>::iterator
7509     BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7510                                BeginLoc, DIDComp);
7511   if (BeginIt != DInfo.Decls.begin())
7512     --BeginIt;
7513 
7514   // If we are pointing at a top-level decl inside an objc container, we need
7515   // to backtrack until we find it otherwise we will fail to report that the
7516   // region overlaps with an objc container.
7517   while (BeginIt != DInfo.Decls.begin() &&
7518          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7519              ->isTopLevelDeclInObjCContainer())
7520     --BeginIt;
7521 
7522   ArrayRef<serialization::LocalDeclID>::iterator
7523     EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7524                              EndLoc, DIDComp);
7525   if (EndIt != DInfo.Decls.end())
7526     ++EndIt;
7527 
7528   for (ArrayRef<serialization::LocalDeclID>::iterator
7529          DIt = BeginIt; DIt != EndIt; ++DIt)
7530     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7531 }
7532 
7533 bool
7534 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7535                                           DeclarationName Name) {
7536   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7537          "DeclContext has no visible decls in storage");
7538   if (!Name)
7539     return false;
7540 
7541   auto It = Lookups.find(DC);
7542   if (It == Lookups.end())
7543     return false;
7544 
7545   Deserializing LookupResults(this);
7546 
7547   // Load the list of declarations.
7548   SmallVector<NamedDecl *, 64> Decls;
7549   for (DeclID ID : It->second.Table.find(Name)) {
7550     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7551     if (ND->getDeclName() == Name)
7552       Decls.push_back(ND);
7553   }
7554 
7555   ++NumVisibleDeclContextsRead;
7556   SetExternalVisibleDeclsForName(DC, Name, Decls);
7557   return !Decls.empty();
7558 }
7559 
7560 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7561   if (!DC->hasExternalVisibleStorage())
7562     return;
7563 
7564   auto It = Lookups.find(DC);
7565   assert(It != Lookups.end() &&
7566          "have external visible storage but no lookup tables");
7567 
7568   DeclsMap Decls;
7569 
7570   for (DeclID ID : It->second.Table.findAll()) {
7571     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7572     Decls[ND->getDeclName()].push_back(ND);
7573   }
7574 
7575   ++NumVisibleDeclContextsRead;
7576 
7577   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7578     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7579   }
7580   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7581 }
7582 
7583 const serialization::reader::DeclContextLookupTable *
7584 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7585   auto I = Lookups.find(Primary);
7586   return I == Lookups.end() ? nullptr : &I->second;
7587 }
7588 
7589 /// Under non-PCH compilation the consumer receives the objc methods
7590 /// before receiving the implementation, and codegen depends on this.
7591 /// We simulate this by deserializing and passing to consumer the methods of the
7592 /// implementation before passing the deserialized implementation decl.
7593 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7594                                        ASTConsumer *Consumer) {
7595   assert(ImplD && Consumer);
7596 
7597   for (auto *I : ImplD->methods())
7598     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7599 
7600   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7601 }
7602 
7603 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7604   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7605     PassObjCImplDeclToConsumer(ImplD, Consumer);
7606   else
7607     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7608 }
7609 
7610 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7611   this->Consumer = Consumer;
7612 
7613   if (Consumer)
7614     PassInterestingDeclsToConsumer();
7615 
7616   if (DeserializationListener)
7617     DeserializationListener->ReaderInitialized(this);
7618 }
7619 
7620 void ASTReader::PrintStats() {
7621   std::fprintf(stderr, "*** AST File Statistics:\n");
7622 
7623   unsigned NumTypesLoaded
7624     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7625                                       QualType());
7626   unsigned NumDeclsLoaded
7627     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7628                                       (Decl *)nullptr);
7629   unsigned NumIdentifiersLoaded
7630     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7631                                             IdentifiersLoaded.end(),
7632                                             (IdentifierInfo *)nullptr);
7633   unsigned NumMacrosLoaded
7634     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7635                                        MacrosLoaded.end(),
7636                                        (MacroInfo *)nullptr);
7637   unsigned NumSelectorsLoaded
7638     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7639                                           SelectorsLoaded.end(),
7640                                           Selector());
7641 
7642   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7643     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7644                  NumSLocEntriesRead, TotalNumSLocEntries,
7645                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7646   if (!TypesLoaded.empty())
7647     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7648                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7649                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7650   if (!DeclsLoaded.empty())
7651     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7652                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7653                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7654   if (!IdentifiersLoaded.empty())
7655     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7656                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7657                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7658   if (!MacrosLoaded.empty())
7659     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7660                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7661                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7662   if (!SelectorsLoaded.empty())
7663     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7664                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7665                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7666   if (TotalNumStatements)
7667     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7668                  NumStatementsRead, TotalNumStatements,
7669                  ((float)NumStatementsRead/TotalNumStatements * 100));
7670   if (TotalNumMacros)
7671     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7672                  NumMacrosRead, TotalNumMacros,
7673                  ((float)NumMacrosRead/TotalNumMacros * 100));
7674   if (TotalLexicalDeclContexts)
7675     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7676                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7677                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7678                   * 100));
7679   if (TotalVisibleDeclContexts)
7680     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7681                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7682                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7683                   * 100));
7684   if (TotalNumMethodPoolEntries)
7685     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7686                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7687                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7688                   * 100));
7689   if (NumMethodPoolLookups)
7690     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7691                  NumMethodPoolHits, NumMethodPoolLookups,
7692                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7693   if (NumMethodPoolTableLookups)
7694     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7695                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7696                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7697                   * 100.0));
7698   if (NumIdentifierLookupHits)
7699     std::fprintf(stderr,
7700                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7701                  NumIdentifierLookupHits, NumIdentifierLookups,
7702                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7703 
7704   if (GlobalIndex) {
7705     std::fprintf(stderr, "\n");
7706     GlobalIndex->printStats();
7707   }
7708 
7709   std::fprintf(stderr, "\n");
7710   dump();
7711   std::fprintf(stderr, "\n");
7712 }
7713 
7714 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7715 LLVM_DUMP_METHOD static void
7716 dumpModuleIDMap(StringRef Name,
7717                 const ContinuousRangeMap<Key, ModuleFile *,
7718                                          InitialCapacity> &Map) {
7719   if (Map.begin() == Map.end())
7720     return;
7721 
7722   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7723 
7724   llvm::errs() << Name << ":\n";
7725   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7726        I != IEnd; ++I) {
7727     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7728       << "\n";
7729   }
7730 }
7731 
7732 LLVM_DUMP_METHOD void ASTReader::dump() {
7733   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7734   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7735   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7736   dumpModuleIDMap("Global type map", GlobalTypeMap);
7737   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7738   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7739   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7740   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7741   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7742   dumpModuleIDMap("Global preprocessed entity map",
7743                   GlobalPreprocessedEntityMap);
7744 
7745   llvm::errs() << "\n*** PCH/Modules Loaded:";
7746   for (ModuleFile &M : ModuleMgr)
7747     M.dump();
7748 }
7749 
7750 /// Return the amount of memory used by memory buffers, breaking down
7751 /// by heap-backed versus mmap'ed memory.
7752 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7753   for (ModuleFile &I : ModuleMgr) {
7754     if (llvm::MemoryBuffer *buf = I.Buffer) {
7755       size_t bytes = buf->getBufferSize();
7756       switch (buf->getBufferKind()) {
7757         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7758           sizes.malloc_bytes += bytes;
7759           break;
7760         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7761           sizes.mmap_bytes += bytes;
7762           break;
7763       }
7764     }
7765   }
7766 }
7767 
7768 void ASTReader::InitializeSema(Sema &S) {
7769   SemaObj = &S;
7770   S.addExternalSource(this);
7771 
7772   // Makes sure any declarations that were deserialized "too early"
7773   // still get added to the identifier's declaration chains.
7774   for (uint64_t ID : PreloadedDeclIDs) {
7775     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7776     pushExternalDeclIntoScope(D, D->getDeclName());
7777   }
7778   PreloadedDeclIDs.clear();
7779 
7780   // FIXME: What happens if these are changed by a module import?
7781   if (!FPPragmaOptions.empty()) {
7782     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7783     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
7784   }
7785 
7786   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7787   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7788   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7789 
7790   UpdateSema();
7791 }
7792 
7793 void ASTReader::UpdateSema() {
7794   assert(SemaObj && "no Sema to update");
7795 
7796   // Load the offsets of the declarations that Sema references.
7797   // They will be lazily deserialized when needed.
7798   if (!SemaDeclRefs.empty()) {
7799     assert(SemaDeclRefs.size() % 3 == 0);
7800     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7801       if (!SemaObj->StdNamespace)
7802         SemaObj->StdNamespace = SemaDeclRefs[I];
7803       if (!SemaObj->StdBadAlloc)
7804         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7805       if (!SemaObj->StdAlignValT)
7806         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7807     }
7808     SemaDeclRefs.clear();
7809   }
7810 
7811   // Update the state of pragmas. Use the same API as if we had encountered the
7812   // pragma in the source.
7813   if(OptimizeOffPragmaLocation.isValid())
7814     SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
7815   if (PragmaMSStructState != -1)
7816     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7817   if (PointersToMembersPragmaLocation.isValid()) {
7818     SemaObj->ActOnPragmaMSPointersToMembers(
7819         (LangOptions::PragmaMSPointersToMembersKind)
7820             PragmaMSPointersToMembersState,
7821         PointersToMembersPragmaLocation);
7822   }
7823   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7824 
7825   if (PragmaPackCurrentValue) {
7826     // The bottom of the stack might have a default value. It must be adjusted
7827     // to the current value to ensure that the packing state is preserved after
7828     // popping entries that were included/imported from a PCH/module.
7829     bool DropFirst = false;
7830     if (!PragmaPackStack.empty() &&
7831         PragmaPackStack.front().Location.isInvalid()) {
7832       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7833              "Expected a default alignment value");
7834       SemaObj->PackStack.Stack.emplace_back(
7835           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7836           SemaObj->PackStack.CurrentPragmaLocation,
7837           PragmaPackStack.front().PushLocation);
7838       DropFirst = true;
7839     }
7840     for (const auto &Entry :
7841          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7842       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7843                                             Entry.Location, Entry.PushLocation);
7844     if (PragmaPackCurrentLocation.isInvalid()) {
7845       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7846              "Expected a default alignment value");
7847       // Keep the current values.
7848     } else {
7849       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7850       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7851     }
7852   }
7853 }
7854 
7855 IdentifierInfo *ASTReader::get(StringRef Name) {
7856   // Note that we are loading an identifier.
7857   Deserializing AnIdentifier(this);
7858 
7859   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7860                                   NumIdentifierLookups,
7861                                   NumIdentifierLookupHits);
7862 
7863   // We don't need to do identifier table lookups in C++ modules (we preload
7864   // all interesting declarations, and don't need to use the scope for name
7865   // lookups). Perform the lookup in PCH files, though, since we don't build
7866   // a complete initial identifier table if we're carrying on from a PCH.
7867   if (PP.getLangOpts().CPlusPlus) {
7868     for (auto F : ModuleMgr.pch_modules())
7869       if (Visitor(*F))
7870         break;
7871   } else {
7872     // If there is a global index, look there first to determine which modules
7873     // provably do not have any results for this identifier.
7874     GlobalModuleIndex::HitSet Hits;
7875     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7876     if (!loadGlobalIndex()) {
7877       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7878         HitsPtr = &Hits;
7879       }
7880     }
7881 
7882     ModuleMgr.visit(Visitor, HitsPtr);
7883   }
7884 
7885   IdentifierInfo *II = Visitor.getIdentifierInfo();
7886   markIdentifierUpToDate(II);
7887   return II;
7888 }
7889 
7890 namespace clang {
7891 
7892   /// An identifier-lookup iterator that enumerates all of the
7893   /// identifiers stored within a set of AST files.
7894   class ASTIdentifierIterator : public IdentifierIterator {
7895     /// The AST reader whose identifiers are being enumerated.
7896     const ASTReader &Reader;
7897 
7898     /// The current index into the chain of AST files stored in
7899     /// the AST reader.
7900     unsigned Index;
7901 
7902     /// The current position within the identifier lookup table
7903     /// of the current AST file.
7904     ASTIdentifierLookupTable::key_iterator Current;
7905 
7906     /// The end position within the identifier lookup table of
7907     /// the current AST file.
7908     ASTIdentifierLookupTable::key_iterator End;
7909 
7910     /// Whether to skip any modules in the ASTReader.
7911     bool SkipModules;
7912 
7913   public:
7914     explicit ASTIdentifierIterator(const ASTReader &Reader,
7915                                    bool SkipModules = false);
7916 
7917     StringRef Next() override;
7918   };
7919 
7920 } // namespace clang
7921 
7922 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7923                                              bool SkipModules)
7924     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7925 }
7926 
7927 StringRef ASTIdentifierIterator::Next() {
7928   while (Current == End) {
7929     // If we have exhausted all of our AST files, we're done.
7930     if (Index == 0)
7931       return StringRef();
7932 
7933     --Index;
7934     ModuleFile &F = Reader.ModuleMgr[Index];
7935     if (SkipModules && F.isModule())
7936       continue;
7937 
7938     ASTIdentifierLookupTable *IdTable =
7939         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
7940     Current = IdTable->key_begin();
7941     End = IdTable->key_end();
7942   }
7943 
7944   // We have any identifiers remaining in the current AST file; return
7945   // the next one.
7946   StringRef Result = *Current;
7947   ++Current;
7948   return Result;
7949 }
7950 
7951 namespace {
7952 
7953 /// A utility for appending two IdentifierIterators.
7954 class ChainedIdentifierIterator : public IdentifierIterator {
7955   std::unique_ptr<IdentifierIterator> Current;
7956   std::unique_ptr<IdentifierIterator> Queued;
7957 
7958 public:
7959   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7960                             std::unique_ptr<IdentifierIterator> Second)
7961       : Current(std::move(First)), Queued(std::move(Second)) {}
7962 
7963   StringRef Next() override {
7964     if (!Current)
7965       return StringRef();
7966 
7967     StringRef result = Current->Next();
7968     if (!result.empty())
7969       return result;
7970 
7971     // Try the queued iterator, which may itself be empty.
7972     Current.reset();
7973     std::swap(Current, Queued);
7974     return Next();
7975   }
7976 };
7977 
7978 } // namespace
7979 
7980 IdentifierIterator *ASTReader::getIdentifiers() {
7981   if (!loadGlobalIndex()) {
7982     std::unique_ptr<IdentifierIterator> ReaderIter(
7983         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7984     std::unique_ptr<IdentifierIterator> ModulesIter(
7985         GlobalIndex->createIdentifierIterator());
7986     return new ChainedIdentifierIterator(std::move(ReaderIter),
7987                                          std::move(ModulesIter));
7988   }
7989 
7990   return new ASTIdentifierIterator(*this);
7991 }
7992 
7993 namespace clang {
7994 namespace serialization {
7995 
7996   class ReadMethodPoolVisitor {
7997     ASTReader &Reader;
7998     Selector Sel;
7999     unsigned PriorGeneration;
8000     unsigned InstanceBits = 0;
8001     unsigned FactoryBits = 0;
8002     bool InstanceHasMoreThanOneDecl = false;
8003     bool FactoryHasMoreThanOneDecl = false;
8004     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8005     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8006 
8007   public:
8008     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8009                           unsigned PriorGeneration)
8010         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8011 
8012     bool operator()(ModuleFile &M) {
8013       if (!M.SelectorLookupTable)
8014         return false;
8015 
8016       // If we've already searched this module file, skip it now.
8017       if (M.Generation <= PriorGeneration)
8018         return true;
8019 
8020       ++Reader.NumMethodPoolTableLookups;
8021       ASTSelectorLookupTable *PoolTable
8022         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8023       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8024       if (Pos == PoolTable->end())
8025         return false;
8026 
8027       ++Reader.NumMethodPoolTableHits;
8028       ++Reader.NumSelectorsRead;
8029       // FIXME: Not quite happy with the statistics here. We probably should
8030       // disable this tracking when called via LoadSelector.
8031       // Also, should entries without methods count as misses?
8032       ++Reader.NumMethodPoolEntriesRead;
8033       ASTSelectorLookupTrait::data_type Data = *Pos;
8034       if (Reader.DeserializationListener)
8035         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8036 
8037       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8038       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8039       InstanceBits = Data.InstanceBits;
8040       FactoryBits = Data.FactoryBits;
8041       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8042       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8043       return true;
8044     }
8045 
8046     /// Retrieve the instance methods found by this visitor.
8047     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8048       return InstanceMethods;
8049     }
8050 
8051     /// Retrieve the instance methods found by this visitor.
8052     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8053       return FactoryMethods;
8054     }
8055 
8056     unsigned getInstanceBits() const { return InstanceBits; }
8057     unsigned getFactoryBits() const { return FactoryBits; }
8058 
8059     bool instanceHasMoreThanOneDecl() const {
8060       return InstanceHasMoreThanOneDecl;
8061     }
8062 
8063     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8064   };
8065 
8066 } // namespace serialization
8067 } // namespace clang
8068 
8069 /// Add the given set of methods to the method list.
8070 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8071                              ObjCMethodList &List) {
8072   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8073     S.addMethodToGlobalList(&List, Methods[I]);
8074   }
8075 }
8076 
8077 void ASTReader::ReadMethodPool(Selector Sel) {
8078   // Get the selector generation and update it to the current generation.
8079   unsigned &Generation = SelectorGeneration[Sel];
8080   unsigned PriorGeneration = Generation;
8081   Generation = getGeneration();
8082   SelectorOutOfDate[Sel] = false;
8083 
8084   // Search for methods defined with this selector.
8085   ++NumMethodPoolLookups;
8086   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8087   ModuleMgr.visit(Visitor);
8088 
8089   if (Visitor.getInstanceMethods().empty() &&
8090       Visitor.getFactoryMethods().empty())
8091     return;
8092 
8093   ++NumMethodPoolHits;
8094 
8095   if (!getSema())
8096     return;
8097 
8098   Sema &S = *getSema();
8099   Sema::GlobalMethodPool::iterator Pos
8100     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8101 
8102   Pos->second.first.setBits(Visitor.getInstanceBits());
8103   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8104   Pos->second.second.setBits(Visitor.getFactoryBits());
8105   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8106 
8107   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8108   // when building a module we keep every method individually and may need to
8109   // update hasMoreThanOneDecl as we add the methods.
8110   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8111   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8112 }
8113 
8114 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8115   if (SelectorOutOfDate[Sel])
8116     ReadMethodPool(Sel);
8117 }
8118 
8119 void ASTReader::ReadKnownNamespaces(
8120                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8121   Namespaces.clear();
8122 
8123   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8124     if (NamespaceDecl *Namespace
8125                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8126       Namespaces.push_back(Namespace);
8127   }
8128 }
8129 
8130 void ASTReader::ReadUndefinedButUsed(
8131     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8132   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8133     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8134     SourceLocation Loc =
8135         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8136     Undefined.insert(std::make_pair(D, Loc));
8137   }
8138 }
8139 
8140 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8141     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8142                                                      Exprs) {
8143   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8144     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8145     uint64_t Count = DelayedDeleteExprs[Idx++];
8146     for (uint64_t C = 0; C < Count; ++C) {
8147       SourceLocation DeleteLoc =
8148           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8149       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8150       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8151     }
8152   }
8153 }
8154 
8155 void ASTReader::ReadTentativeDefinitions(
8156                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8157   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8158     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8159     if (Var)
8160       TentativeDefs.push_back(Var);
8161   }
8162   TentativeDefinitions.clear();
8163 }
8164 
8165 void ASTReader::ReadUnusedFileScopedDecls(
8166                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8167   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8168     DeclaratorDecl *D
8169       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8170     if (D)
8171       Decls.push_back(D);
8172   }
8173   UnusedFileScopedDecls.clear();
8174 }
8175 
8176 void ASTReader::ReadDelegatingConstructors(
8177                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8178   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8179     CXXConstructorDecl *D
8180       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8181     if (D)
8182       Decls.push_back(D);
8183   }
8184   DelegatingCtorDecls.clear();
8185 }
8186 
8187 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8188   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8189     TypedefNameDecl *D
8190       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8191     if (D)
8192       Decls.push_back(D);
8193   }
8194   ExtVectorDecls.clear();
8195 }
8196 
8197 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8198     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8199   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8200        ++I) {
8201     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8202         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8203     if (D)
8204       Decls.insert(D);
8205   }
8206   UnusedLocalTypedefNameCandidates.clear();
8207 }
8208 
8209 void ASTReader::ReadReferencedSelectors(
8210        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8211   if (ReferencedSelectorsData.empty())
8212     return;
8213 
8214   // If there are @selector references added them to its pool. This is for
8215   // implementation of -Wselector.
8216   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8217   unsigned I = 0;
8218   while (I < DataSize) {
8219     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8220     SourceLocation SelLoc
8221       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8222     Sels.push_back(std::make_pair(Sel, SelLoc));
8223   }
8224   ReferencedSelectorsData.clear();
8225 }
8226 
8227 void ASTReader::ReadWeakUndeclaredIdentifiers(
8228        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8229   if (WeakUndeclaredIdentifiers.empty())
8230     return;
8231 
8232   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8233     IdentifierInfo *WeakId
8234       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8235     IdentifierInfo *AliasId
8236       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8237     SourceLocation Loc
8238       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8239     bool Used = WeakUndeclaredIdentifiers[I++];
8240     WeakInfo WI(AliasId, Loc);
8241     WI.setUsed(Used);
8242     WeakIDs.push_back(std::make_pair(WeakId, WI));
8243   }
8244   WeakUndeclaredIdentifiers.clear();
8245 }
8246 
8247 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8248   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8249     ExternalVTableUse VT;
8250     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8251     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8252     VT.DefinitionRequired = VTableUses[Idx++];
8253     VTables.push_back(VT);
8254   }
8255 
8256   VTableUses.clear();
8257 }
8258 
8259 void ASTReader::ReadPendingInstantiations(
8260        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8261   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8262     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8263     SourceLocation Loc
8264       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8265 
8266     Pending.push_back(std::make_pair(D, Loc));
8267   }
8268   PendingInstantiations.clear();
8269 }
8270 
8271 void ASTReader::ReadLateParsedTemplates(
8272     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8273         &LPTMap) {
8274   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8275        /* In loop */) {
8276     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8277 
8278     auto LT = llvm::make_unique<LateParsedTemplate>();
8279     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8280 
8281     ModuleFile *F = getOwningModuleFile(LT->D);
8282     assert(F && "No module");
8283 
8284     unsigned TokN = LateParsedTemplates[Idx++];
8285     LT->Toks.reserve(TokN);
8286     for (unsigned T = 0; T < TokN; ++T)
8287       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8288 
8289     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8290   }
8291 
8292   LateParsedTemplates.clear();
8293 }
8294 
8295 void ASTReader::LoadSelector(Selector Sel) {
8296   // It would be complicated to avoid reading the methods anyway. So don't.
8297   ReadMethodPool(Sel);
8298 }
8299 
8300 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8301   assert(ID && "Non-zero identifier ID required");
8302   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8303   IdentifiersLoaded[ID - 1] = II;
8304   if (DeserializationListener)
8305     DeserializationListener->IdentifierRead(ID, II);
8306 }
8307 
8308 /// Set the globally-visible declarations associated with the given
8309 /// identifier.
8310 ///
8311 /// If the AST reader is currently in a state where the given declaration IDs
8312 /// cannot safely be resolved, they are queued until it is safe to resolve
8313 /// them.
8314 ///
8315 /// \param II an IdentifierInfo that refers to one or more globally-visible
8316 /// declarations.
8317 ///
8318 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8319 /// visible at global scope.
8320 ///
8321 /// \param Decls if non-null, this vector will be populated with the set of
8322 /// deserialized declarations. These declarations will not be pushed into
8323 /// scope.
8324 void
8325 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8326                               const SmallVectorImpl<uint32_t> &DeclIDs,
8327                                    SmallVectorImpl<Decl *> *Decls) {
8328   if (NumCurrentElementsDeserializing && !Decls) {
8329     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8330     return;
8331   }
8332 
8333   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8334     if (!SemaObj) {
8335       // Queue this declaration so that it will be added to the
8336       // translation unit scope and identifier's declaration chain
8337       // once a Sema object is known.
8338       PreloadedDeclIDs.push_back(DeclIDs[I]);
8339       continue;
8340     }
8341 
8342     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8343 
8344     // If we're simply supposed to record the declarations, do so now.
8345     if (Decls) {
8346       Decls->push_back(D);
8347       continue;
8348     }
8349 
8350     // Introduce this declaration into the translation-unit scope
8351     // and add it to the declaration chain for this identifier, so
8352     // that (unqualified) name lookup will find it.
8353     pushExternalDeclIntoScope(D, II);
8354   }
8355 }
8356 
8357 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8358   if (ID == 0)
8359     return nullptr;
8360 
8361   if (IdentifiersLoaded.empty()) {
8362     Error("no identifier table in AST file");
8363     return nullptr;
8364   }
8365 
8366   ID -= 1;
8367   if (!IdentifiersLoaded[ID]) {
8368     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8369     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8370     ModuleFile *M = I->second;
8371     unsigned Index = ID - M->BaseIdentifierID;
8372     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8373 
8374     // All of the strings in the AST file are preceded by a 16-bit length.
8375     // Extract that 16-bit length to avoid having to execute strlen().
8376     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8377     //  unsigned integers.  This is important to avoid integer overflow when
8378     //  we cast them to 'unsigned'.
8379     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8380     unsigned StrLen = (((unsigned) StrLenPtr[0])
8381                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8382     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8383     IdentifiersLoaded[ID] = &II;
8384     markIdentifierFromAST(*this,  II);
8385     if (DeserializationListener)
8386       DeserializationListener->IdentifierRead(ID + 1, &II);
8387   }
8388 
8389   return IdentifiersLoaded[ID];
8390 }
8391 
8392 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8393   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8394 }
8395 
8396 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8397   if (LocalID < NUM_PREDEF_IDENT_IDS)
8398     return LocalID;
8399 
8400   if (!M.ModuleOffsetMap.empty())
8401     ReadModuleOffsetMap(M);
8402 
8403   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8404     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8405   assert(I != M.IdentifierRemap.end()
8406          && "Invalid index into identifier index remap");
8407 
8408   return LocalID + I->second;
8409 }
8410 
8411 MacroInfo *ASTReader::getMacro(MacroID ID) {
8412   if (ID == 0)
8413     return nullptr;
8414 
8415   if (MacrosLoaded.empty()) {
8416     Error("no macro table in AST file");
8417     return nullptr;
8418   }
8419 
8420   ID -= NUM_PREDEF_MACRO_IDS;
8421   if (!MacrosLoaded[ID]) {
8422     GlobalMacroMapType::iterator I
8423       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8424     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8425     ModuleFile *M = I->second;
8426     unsigned Index = ID - M->BaseMacroID;
8427     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8428 
8429     if (DeserializationListener)
8430       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8431                                          MacrosLoaded[ID]);
8432   }
8433 
8434   return MacrosLoaded[ID];
8435 }
8436 
8437 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8438   if (LocalID < NUM_PREDEF_MACRO_IDS)
8439     return LocalID;
8440 
8441   if (!M.ModuleOffsetMap.empty())
8442     ReadModuleOffsetMap(M);
8443 
8444   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8445     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8446   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8447 
8448   return LocalID + I->second;
8449 }
8450 
8451 serialization::SubmoduleID
8452 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8453   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8454     return LocalID;
8455 
8456   if (!M.ModuleOffsetMap.empty())
8457     ReadModuleOffsetMap(M);
8458 
8459   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8460     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8461   assert(I != M.SubmoduleRemap.end()
8462          && "Invalid index into submodule index remap");
8463 
8464   return LocalID + I->second;
8465 }
8466 
8467 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8468   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8469     assert(GlobalID == 0 && "Unhandled global submodule ID");
8470     return nullptr;
8471   }
8472 
8473   if (GlobalID > SubmodulesLoaded.size()) {
8474     Error("submodule ID out of range in AST file");
8475     return nullptr;
8476   }
8477 
8478   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8479 }
8480 
8481 Module *ASTReader::getModule(unsigned ID) {
8482   return getSubmodule(ID);
8483 }
8484 
8485 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8486   ModuleFile *MF = getOwningModuleFile(D);
8487   return MF && MF->PCHHasObjectFile;
8488 }
8489 
8490 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8491   if (ID & 1) {
8492     // It's a module, look it up by submodule ID.
8493     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8494     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8495   } else {
8496     // It's a prefix (preamble, PCH, ...). Look it up by index.
8497     unsigned IndexFromEnd = ID >> 1;
8498     assert(IndexFromEnd && "got reference to unknown module file");
8499     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8500   }
8501 }
8502 
8503 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8504   if (!F)
8505     return 1;
8506 
8507   // For a file representing a module, use the submodule ID of the top-level
8508   // module as the file ID. For any other kind of file, the number of such
8509   // files loaded beforehand will be the same on reload.
8510   // FIXME: Is this true even if we have an explicit module file and a PCH?
8511   if (F->isModule())
8512     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8513 
8514   auto PCHModules = getModuleManager().pch_modules();
8515   auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
8516   assert(I != PCHModules.end() && "emitting reference to unknown file");
8517   return (I - PCHModules.end()) << 1;
8518 }
8519 
8520 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8521 ASTReader::getSourceDescriptor(unsigned ID) {
8522   if (const Module *M = getSubmodule(ID))
8523     return ExternalASTSource::ASTSourceDescriptor(*M);
8524 
8525   // If there is only a single PCH, return it instead.
8526   // Chained PCH are not supported.
8527   const auto &PCHChain = ModuleMgr.pch_modules();
8528   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8529     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8530     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8531     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8532     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8533                                           MF.Signature);
8534   }
8535   return None;
8536 }
8537 
8538 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8539   auto I = DefinitionSource.find(FD);
8540   if (I == DefinitionSource.end())
8541     return EK_ReplyHazy;
8542   return I->second ? EK_Never : EK_Always;
8543 }
8544 
8545 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8546   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8547 }
8548 
8549 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8550   if (ID == 0)
8551     return Selector();
8552 
8553   if (ID > SelectorsLoaded.size()) {
8554     Error("selector ID out of range in AST file");
8555     return Selector();
8556   }
8557 
8558   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8559     // Load this selector from the selector table.
8560     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8561     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8562     ModuleFile &M = *I->second;
8563     ASTSelectorLookupTrait Trait(*this, M);
8564     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8565     SelectorsLoaded[ID - 1] =
8566       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8567     if (DeserializationListener)
8568       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8569   }
8570 
8571   return SelectorsLoaded[ID - 1];
8572 }
8573 
8574 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8575   return DecodeSelector(ID);
8576 }
8577 
8578 uint32_t ASTReader::GetNumExternalSelectors() {
8579   // ID 0 (the null selector) is considered an external selector.
8580   return getTotalNumSelectors() + 1;
8581 }
8582 
8583 serialization::SelectorID
8584 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8585   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8586     return LocalID;
8587 
8588   if (!M.ModuleOffsetMap.empty())
8589     ReadModuleOffsetMap(M);
8590 
8591   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8592     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8593   assert(I != M.SelectorRemap.end()
8594          && "Invalid index into selector index remap");
8595 
8596   return LocalID + I->second;
8597 }
8598 
8599 DeclarationName
8600 ASTReader::ReadDeclarationName(ModuleFile &F,
8601                                const RecordData &Record, unsigned &Idx) {
8602   ASTContext &Context = getContext();
8603   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
8604   switch (Kind) {
8605   case DeclarationName::Identifier:
8606     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
8607 
8608   case DeclarationName::ObjCZeroArgSelector:
8609   case DeclarationName::ObjCOneArgSelector:
8610   case DeclarationName::ObjCMultiArgSelector:
8611     return DeclarationName(ReadSelector(F, Record, Idx));
8612 
8613   case DeclarationName::CXXConstructorName:
8614     return Context.DeclarationNames.getCXXConstructorName(
8615                           Context.getCanonicalType(readType(F, Record, Idx)));
8616 
8617   case DeclarationName::CXXDestructorName:
8618     return Context.DeclarationNames.getCXXDestructorName(
8619                           Context.getCanonicalType(readType(F, Record, Idx)));
8620 
8621   case DeclarationName::CXXDeductionGuideName:
8622     return Context.DeclarationNames.getCXXDeductionGuideName(
8623                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
8624 
8625   case DeclarationName::CXXConversionFunctionName:
8626     return Context.DeclarationNames.getCXXConversionFunctionName(
8627                           Context.getCanonicalType(readType(F, Record, Idx)));
8628 
8629   case DeclarationName::CXXOperatorName:
8630     return Context.DeclarationNames.getCXXOperatorName(
8631                                        (OverloadedOperatorKind)Record[Idx++]);
8632 
8633   case DeclarationName::CXXLiteralOperatorName:
8634     return Context.DeclarationNames.getCXXLiteralOperatorName(
8635                                        GetIdentifierInfo(F, Record, Idx));
8636 
8637   case DeclarationName::CXXUsingDirective:
8638     return DeclarationName::getUsingDirectiveName();
8639   }
8640 
8641   llvm_unreachable("Invalid NameKind!");
8642 }
8643 
8644 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
8645                                        DeclarationNameLoc &DNLoc,
8646                                        DeclarationName Name,
8647                                       const RecordData &Record, unsigned &Idx) {
8648   switch (Name.getNameKind()) {
8649   case DeclarationName::CXXConstructorName:
8650   case DeclarationName::CXXDestructorName:
8651   case DeclarationName::CXXConversionFunctionName:
8652     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
8653     break;
8654 
8655   case DeclarationName::CXXOperatorName:
8656     DNLoc.CXXOperatorName.BeginOpNameLoc
8657         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8658     DNLoc.CXXOperatorName.EndOpNameLoc
8659         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8660     break;
8661 
8662   case DeclarationName::CXXLiteralOperatorName:
8663     DNLoc.CXXLiteralOperatorName.OpNameLoc
8664         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8665     break;
8666 
8667   case DeclarationName::Identifier:
8668   case DeclarationName::ObjCZeroArgSelector:
8669   case DeclarationName::ObjCOneArgSelector:
8670   case DeclarationName::ObjCMultiArgSelector:
8671   case DeclarationName::CXXUsingDirective:
8672   case DeclarationName::CXXDeductionGuideName:
8673     break;
8674   }
8675 }
8676 
8677 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
8678                                         DeclarationNameInfo &NameInfo,
8679                                       const RecordData &Record, unsigned &Idx) {
8680   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
8681   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
8682   DeclarationNameLoc DNLoc;
8683   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
8684   NameInfo.setInfo(DNLoc);
8685 }
8686 
8687 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
8688                                   const RecordData &Record, unsigned &Idx) {
8689   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
8690   unsigned NumTPLists = Record[Idx++];
8691   Info.NumTemplParamLists = NumTPLists;
8692   if (NumTPLists) {
8693     Info.TemplParamLists =
8694         new (getContext()) TemplateParameterList *[NumTPLists];
8695     for (unsigned i = 0; i != NumTPLists; ++i)
8696       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
8697   }
8698 }
8699 
8700 TemplateName
8701 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
8702                             unsigned &Idx) {
8703   ASTContext &Context = getContext();
8704   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
8705   switch (Kind) {
8706   case TemplateName::Template:
8707       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
8708 
8709   case TemplateName::OverloadedTemplate: {
8710     unsigned size = Record[Idx++];
8711     UnresolvedSet<8> Decls;
8712     while (size--)
8713       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
8714 
8715     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
8716   }
8717 
8718   case TemplateName::QualifiedTemplate: {
8719     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8720     bool hasTemplKeyword = Record[Idx++];
8721     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
8722     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
8723   }
8724 
8725   case TemplateName::DependentTemplate: {
8726     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8727     if (Record[Idx++])  // isIdentifier
8728       return Context.getDependentTemplateName(NNS,
8729                                                GetIdentifierInfo(F, Record,
8730                                                                  Idx));
8731     return Context.getDependentTemplateName(NNS,
8732                                          (OverloadedOperatorKind)Record[Idx++]);
8733   }
8734 
8735   case TemplateName::SubstTemplateTemplateParm: {
8736     TemplateTemplateParmDecl *param
8737       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8738     if (!param) return TemplateName();
8739     TemplateName replacement = ReadTemplateName(F, Record, Idx);
8740     return Context.getSubstTemplateTemplateParm(param, replacement);
8741   }
8742 
8743   case TemplateName::SubstTemplateTemplateParmPack: {
8744     TemplateTemplateParmDecl *Param
8745       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8746     if (!Param)
8747       return TemplateName();
8748 
8749     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
8750     if (ArgPack.getKind() != TemplateArgument::Pack)
8751       return TemplateName();
8752 
8753     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
8754   }
8755   }
8756 
8757   llvm_unreachable("Unhandled template name kind!");
8758 }
8759 
8760 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
8761                                                  const RecordData &Record,
8762                                                  unsigned &Idx,
8763                                                  bool Canonicalize) {
8764   ASTContext &Context = getContext();
8765   if (Canonicalize) {
8766     // The caller wants a canonical template argument. Sometimes the AST only
8767     // wants template arguments in canonical form (particularly as the template
8768     // argument lists of template specializations) so ensure we preserve that
8769     // canonical form across serialization.
8770     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
8771     return Context.getCanonicalTemplateArgument(Arg);
8772   }
8773 
8774   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
8775   switch (Kind) {
8776   case TemplateArgument::Null:
8777     return TemplateArgument();
8778   case TemplateArgument::Type:
8779     return TemplateArgument(readType(F, Record, Idx));
8780   case TemplateArgument::Declaration: {
8781     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
8782     return TemplateArgument(D, readType(F, Record, Idx));
8783   }
8784   case TemplateArgument::NullPtr:
8785     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
8786   case TemplateArgument::Integral: {
8787     llvm::APSInt Value = ReadAPSInt(Record, Idx);
8788     QualType T = readType(F, Record, Idx);
8789     return TemplateArgument(Context, Value, T);
8790   }
8791   case TemplateArgument::Template:
8792     return TemplateArgument(ReadTemplateName(F, Record, Idx));
8793   case TemplateArgument::TemplateExpansion: {
8794     TemplateName Name = ReadTemplateName(F, Record, Idx);
8795     Optional<unsigned> NumTemplateExpansions;
8796     if (unsigned NumExpansions = Record[Idx++])
8797       NumTemplateExpansions = NumExpansions - 1;
8798     return TemplateArgument(Name, NumTemplateExpansions);
8799   }
8800   case TemplateArgument::Expression:
8801     return TemplateArgument(ReadExpr(F));
8802   case TemplateArgument::Pack: {
8803     unsigned NumArgs = Record[Idx++];
8804     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
8805     for (unsigned I = 0; I != NumArgs; ++I)
8806       Args[I] = ReadTemplateArgument(F, Record, Idx);
8807     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
8808   }
8809   }
8810 
8811   llvm_unreachable("Unhandled template argument kind!");
8812 }
8813 
8814 TemplateParameterList *
8815 ASTReader::ReadTemplateParameterList(ModuleFile &F,
8816                                      const RecordData &Record, unsigned &Idx) {
8817   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
8818   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
8819   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
8820 
8821   unsigned NumParams = Record[Idx++];
8822   SmallVector<NamedDecl *, 16> Params;
8823   Params.reserve(NumParams);
8824   while (NumParams--)
8825     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
8826 
8827   // TODO: Concepts
8828   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8829       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
8830   return TemplateParams;
8831 }
8832 
8833 void
8834 ASTReader::
8835 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
8836                          ModuleFile &F, const RecordData &Record,
8837                          unsigned &Idx, bool Canonicalize) {
8838   unsigned NumTemplateArgs = Record[Idx++];
8839   TemplArgs.reserve(NumTemplateArgs);
8840   while (NumTemplateArgs--)
8841     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
8842 }
8843 
8844 /// Read a UnresolvedSet structure.
8845 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
8846                                   const RecordData &Record, unsigned &Idx) {
8847   unsigned NumDecls = Record[Idx++];
8848   Set.reserve(getContext(), NumDecls);
8849   while (NumDecls--) {
8850     DeclID ID = ReadDeclID(F, Record, Idx);
8851     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
8852     Set.addLazyDecl(getContext(), ID, AS);
8853   }
8854 }
8855 
8856 CXXBaseSpecifier
8857 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
8858                                 const RecordData &Record, unsigned &Idx) {
8859   bool isVirtual = static_cast<bool>(Record[Idx++]);
8860   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
8861   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
8862   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
8863   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
8864   SourceRange Range = ReadSourceRange(F, Record, Idx);
8865   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
8866   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8867                           EllipsisLoc);
8868   Result.setInheritConstructors(inheritConstructors);
8869   return Result;
8870 }
8871 
8872 CXXCtorInitializer **
8873 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
8874                                    unsigned &Idx) {
8875   ASTContext &Context = getContext();
8876   unsigned NumInitializers = Record[Idx++];
8877   assert(NumInitializers && "wrote ctor initializers but have no inits");
8878   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8879   for (unsigned i = 0; i != NumInitializers; ++i) {
8880     TypeSourceInfo *TInfo = nullptr;
8881     bool IsBaseVirtual = false;
8882     FieldDecl *Member = nullptr;
8883     IndirectFieldDecl *IndirectMember = nullptr;
8884 
8885     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
8886     switch (Type) {
8887     case CTOR_INITIALIZER_BASE:
8888       TInfo = GetTypeSourceInfo(F, Record, Idx);
8889       IsBaseVirtual = Record[Idx++];
8890       break;
8891 
8892     case CTOR_INITIALIZER_DELEGATING:
8893       TInfo = GetTypeSourceInfo(F, Record, Idx);
8894       break;
8895 
8896      case CTOR_INITIALIZER_MEMBER:
8897       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
8898       break;
8899 
8900      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8901       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
8902       break;
8903     }
8904 
8905     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
8906     Expr *Init = ReadExpr(F);
8907     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
8908     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
8909 
8910     CXXCtorInitializer *BOMInit;
8911     if (Type == CTOR_INITIALIZER_BASE)
8912       BOMInit = new (Context)
8913           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8914                              RParenLoc, MemberOrEllipsisLoc);
8915     else if (Type == CTOR_INITIALIZER_DELEGATING)
8916       BOMInit = new (Context)
8917           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8918     else if (Member)
8919       BOMInit = new (Context)
8920           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8921                              Init, RParenLoc);
8922     else
8923       BOMInit = new (Context)
8924           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8925                              LParenLoc, Init, RParenLoc);
8926 
8927     if (/*IsWritten*/Record[Idx++]) {
8928       unsigned SourceOrder = Record[Idx++];
8929       BOMInit->setSourceOrder(SourceOrder);
8930     }
8931 
8932     CtorInitializers[i] = BOMInit;
8933   }
8934 
8935   return CtorInitializers;
8936 }
8937 
8938 NestedNameSpecifier *
8939 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8940                                    const RecordData &Record, unsigned &Idx) {
8941   ASTContext &Context = getContext();
8942   unsigned N = Record[Idx++];
8943   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
8944   for (unsigned I = 0; I != N; ++I) {
8945     NestedNameSpecifier::SpecifierKind Kind
8946       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8947     switch (Kind) {
8948     case NestedNameSpecifier::Identifier: {
8949       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8950       NNS = NestedNameSpecifier::Create(Context, Prev, II);
8951       break;
8952     }
8953 
8954     case NestedNameSpecifier::Namespace: {
8955       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8956       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8957       break;
8958     }
8959 
8960     case NestedNameSpecifier::NamespaceAlias: {
8961       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8962       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8963       break;
8964     }
8965 
8966     case NestedNameSpecifier::TypeSpec:
8967     case NestedNameSpecifier::TypeSpecWithTemplate: {
8968       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8969       if (!T)
8970         return nullptr;
8971 
8972       bool Template = Record[Idx++];
8973       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8974       break;
8975     }
8976 
8977     case NestedNameSpecifier::Global:
8978       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8979       // No associated value, and there can't be a prefix.
8980       break;
8981 
8982     case NestedNameSpecifier::Super: {
8983       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8984       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8985       break;
8986     }
8987     }
8988     Prev = NNS;
8989   }
8990   return NNS;
8991 }
8992 
8993 NestedNameSpecifierLoc
8994 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8995                                       unsigned &Idx) {
8996   ASTContext &Context = getContext();
8997   unsigned N = Record[Idx++];
8998   NestedNameSpecifierLocBuilder Builder;
8999   for (unsigned I = 0; I != N; ++I) {
9000     NestedNameSpecifier::SpecifierKind Kind
9001       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9002     switch (Kind) {
9003     case NestedNameSpecifier::Identifier: {
9004       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9005       SourceRange Range = ReadSourceRange(F, Record, Idx);
9006       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9007       break;
9008     }
9009 
9010     case NestedNameSpecifier::Namespace: {
9011       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9012       SourceRange Range = ReadSourceRange(F, Record, Idx);
9013       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9014       break;
9015     }
9016 
9017     case NestedNameSpecifier::NamespaceAlias: {
9018       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9019       SourceRange Range = ReadSourceRange(F, Record, Idx);
9020       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9021       break;
9022     }
9023 
9024     case NestedNameSpecifier::TypeSpec:
9025     case NestedNameSpecifier::TypeSpecWithTemplate: {
9026       bool Template = Record[Idx++];
9027       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9028       if (!T)
9029         return NestedNameSpecifierLoc();
9030       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9031 
9032       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9033       Builder.Extend(Context,
9034                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9035                      T->getTypeLoc(), ColonColonLoc);
9036       break;
9037     }
9038 
9039     case NestedNameSpecifier::Global: {
9040       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9041       Builder.MakeGlobal(Context, ColonColonLoc);
9042       break;
9043     }
9044 
9045     case NestedNameSpecifier::Super: {
9046       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9047       SourceRange Range = ReadSourceRange(F, Record, Idx);
9048       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9049       break;
9050     }
9051     }
9052   }
9053 
9054   return Builder.getWithLocInContext(Context);
9055 }
9056 
9057 SourceRange
9058 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9059                            unsigned &Idx) {
9060   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9061   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9062   return SourceRange(beg, end);
9063 }
9064 
9065 /// Read an integral value
9066 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9067   unsigned BitWidth = Record[Idx++];
9068   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9069   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9070   Idx += NumWords;
9071   return Result;
9072 }
9073 
9074 /// Read a signed integral value
9075 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9076   bool isUnsigned = Record[Idx++];
9077   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9078 }
9079 
9080 /// Read a floating-point value
9081 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9082                                      const llvm::fltSemantics &Sem,
9083                                      unsigned &Idx) {
9084   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9085 }
9086 
9087 // Read a string
9088 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9089   unsigned Len = Record[Idx++];
9090   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9091   Idx += Len;
9092   return Result;
9093 }
9094 
9095 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9096                                 unsigned &Idx) {
9097   std::string Filename = ReadString(Record, Idx);
9098   ResolveImportedPath(F, Filename);
9099   return Filename;
9100 }
9101 
9102 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9103                                          unsigned &Idx) {
9104   unsigned Major = Record[Idx++];
9105   unsigned Minor = Record[Idx++];
9106   unsigned Subminor = Record[Idx++];
9107   if (Minor == 0)
9108     return VersionTuple(Major);
9109   if (Subminor == 0)
9110     return VersionTuple(Major, Minor - 1);
9111   return VersionTuple(Major, Minor - 1, Subminor - 1);
9112 }
9113 
9114 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9115                                           const RecordData &Record,
9116                                           unsigned &Idx) {
9117   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9118   return CXXTemporary::Create(getContext(), Decl);
9119 }
9120 
9121 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9122   return Diag(CurrentImportLoc, DiagID);
9123 }
9124 
9125 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9126   return Diags.Report(Loc, DiagID);
9127 }
9128 
9129 /// Retrieve the identifier table associated with the
9130 /// preprocessor.
9131 IdentifierTable &ASTReader::getIdentifierTable() {
9132   return PP.getIdentifierTable();
9133 }
9134 
9135 /// Record that the given ID maps to the given switch-case
9136 /// statement.
9137 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9138   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9139          "Already have a SwitchCase with this ID");
9140   (*CurrSwitchCaseStmts)[ID] = SC;
9141 }
9142 
9143 /// Retrieve the switch-case statement with the given ID.
9144 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9145   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9146   return (*CurrSwitchCaseStmts)[ID];
9147 }
9148 
9149 void ASTReader::ClearSwitchCaseIDs() {
9150   CurrSwitchCaseStmts->clear();
9151 }
9152 
9153 void ASTReader::ReadComments() {
9154   ASTContext &Context = getContext();
9155   std::vector<RawComment *> Comments;
9156   for (SmallVectorImpl<std::pair<BitstreamCursor,
9157                                  serialization::ModuleFile *>>::iterator
9158        I = CommentsCursors.begin(),
9159        E = CommentsCursors.end();
9160        I != E; ++I) {
9161     Comments.clear();
9162     BitstreamCursor &Cursor = I->first;
9163     serialization::ModuleFile &F = *I->second;
9164     SavedStreamPosition SavedPosition(Cursor);
9165 
9166     RecordData Record;
9167     while (true) {
9168       llvm::BitstreamEntry Entry =
9169         Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
9170 
9171       switch (Entry.Kind) {
9172       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9173       case llvm::BitstreamEntry::Error:
9174         Error("malformed block record in AST file");
9175         return;
9176       case llvm::BitstreamEntry::EndBlock:
9177         goto NextCursor;
9178       case llvm::BitstreamEntry::Record:
9179         // The interesting case.
9180         break;
9181       }
9182 
9183       // Read a record.
9184       Record.clear();
9185       switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
9186       case COMMENTS_RAW_COMMENT: {
9187         unsigned Idx = 0;
9188         SourceRange SR = ReadSourceRange(F, Record, Idx);
9189         RawComment::CommentKind Kind =
9190             (RawComment::CommentKind) Record[Idx++];
9191         bool IsTrailingComment = Record[Idx++];
9192         bool IsAlmostTrailingComment = Record[Idx++];
9193         Comments.push_back(new (Context) RawComment(
9194             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9195         break;
9196       }
9197       }
9198     }
9199   NextCursor:
9200     // De-serialized SourceLocations get negative FileIDs for other modules,
9201     // potentially invalidating the original order. Sort it again.
9202     llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr));
9203     Context.Comments.addDeserializedComments(Comments);
9204   }
9205 }
9206 
9207 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9208                                 bool IncludeSystem, bool Complain,
9209                     llvm::function_ref<void(const serialization::InputFile &IF,
9210                                             bool isSystem)> Visitor) {
9211   unsigned NumUserInputs = MF.NumUserInputFiles;
9212   unsigned NumInputs = MF.InputFilesLoaded.size();
9213   assert(NumUserInputs <= NumInputs);
9214   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9215   for (unsigned I = 0; I < N; ++I) {
9216     bool IsSystem = I >= NumUserInputs;
9217     InputFile IF = getInputFile(MF, I+1, Complain);
9218     Visitor(IF, IsSystem);
9219   }
9220 }
9221 
9222 void ASTReader::visitTopLevelModuleMaps(
9223     serialization::ModuleFile &MF,
9224     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9225   unsigned NumInputs = MF.InputFilesLoaded.size();
9226   for (unsigned I = 0; I < NumInputs; ++I) {
9227     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9228     if (IFI.TopLevelModuleMap)
9229       // FIXME: This unnecessarily re-reads the InputFileInfo.
9230       if (auto *FE = getInputFile(MF, I + 1).getFile())
9231         Visitor(FE);
9232   }
9233 }
9234 
9235 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9236   // If we know the owning module, use it.
9237   if (Module *M = D->getImportedOwningModule())
9238     return M->getFullModuleName();
9239 
9240   // Otherwise, use the name of the top-level module the decl is within.
9241   if (ModuleFile *M = getOwningModuleFile(D))
9242     return M->ModuleName;
9243 
9244   // Not from a module.
9245   return {};
9246 }
9247 
9248 void ASTReader::finishPendingActions() {
9249   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9250          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9251          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9252          !PendingUpdateRecords.empty()) {
9253     // If any identifiers with corresponding top-level declarations have
9254     // been loaded, load those declarations now.
9255     using TopLevelDeclsMap =
9256         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9257     TopLevelDeclsMap TopLevelDecls;
9258 
9259     while (!PendingIdentifierInfos.empty()) {
9260       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9261       SmallVector<uint32_t, 4> DeclIDs =
9262           std::move(PendingIdentifierInfos.back().second);
9263       PendingIdentifierInfos.pop_back();
9264 
9265       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9266     }
9267 
9268     // Load each function type that we deferred loading because it was a
9269     // deduced type that might refer to a local type declared within itself.
9270     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9271       auto *FD = PendingFunctionTypes[I].first;
9272       FD->setType(GetType(PendingFunctionTypes[I].second));
9273 
9274       // If we gave a function a deduced return type, remember that we need to
9275       // propagate that along the redeclaration chain.
9276       auto *DT = FD->getReturnType()->getContainedDeducedType();
9277       if (DT && DT->isDeduced())
9278         PendingDeducedTypeUpdates.insert(
9279             {FD->getCanonicalDecl(), FD->getReturnType()});
9280     }
9281     PendingFunctionTypes.clear();
9282 
9283     // For each decl chain that we wanted to complete while deserializing, mark
9284     // it as "still needs to be completed".
9285     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9286       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9287     }
9288     PendingIncompleteDeclChains.clear();
9289 
9290     // Load pending declaration chains.
9291     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9292       loadPendingDeclChain(PendingDeclChains[I].first,
9293                            PendingDeclChains[I].second);
9294     PendingDeclChains.clear();
9295 
9296     // Make the most recent of the top-level declarations visible.
9297     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9298            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9299       IdentifierInfo *II = TLD->first;
9300       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9301         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9302       }
9303     }
9304 
9305     // Load any pending macro definitions.
9306     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9307       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9308       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9309       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9310       // Initialize the macro history from chained-PCHs ahead of module imports.
9311       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9312            ++IDIdx) {
9313         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9314         if (!Info.M->isModule())
9315           resolvePendingMacro(II, Info);
9316       }
9317       // Handle module imports.
9318       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9319            ++IDIdx) {
9320         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9321         if (Info.M->isModule())
9322           resolvePendingMacro(II, Info);
9323       }
9324     }
9325     PendingMacroIDs.clear();
9326 
9327     // Wire up the DeclContexts for Decls that we delayed setting until
9328     // recursive loading is completed.
9329     while (!PendingDeclContextInfos.empty()) {
9330       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9331       PendingDeclContextInfos.pop_front();
9332       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9333       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9334       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9335     }
9336 
9337     // Perform any pending declaration updates.
9338     while (!PendingUpdateRecords.empty()) {
9339       auto Update = PendingUpdateRecords.pop_back_val();
9340       ReadingKindTracker ReadingKind(Read_Decl, *this);
9341       loadDeclUpdateRecords(Update);
9342     }
9343   }
9344 
9345   // At this point, all update records for loaded decls are in place, so any
9346   // fake class definitions should have become real.
9347   assert(PendingFakeDefinitionData.empty() &&
9348          "faked up a class definition but never saw the real one");
9349 
9350   // If we deserialized any C++ or Objective-C class definitions, any
9351   // Objective-C protocol definitions, or any redeclarable templates, make sure
9352   // that all redeclarations point to the definitions. Note that this can only
9353   // happen now, after the redeclaration chains have been fully wired.
9354   for (Decl *D : PendingDefinitions) {
9355     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9356       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9357         // Make sure that the TagType points at the definition.
9358         const_cast<TagType*>(TagT)->decl = TD;
9359       }
9360 
9361       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9362         for (auto *R = getMostRecentExistingDecl(RD); R;
9363              R = R->getPreviousDecl()) {
9364           assert((R == D) ==
9365                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9366                  "declaration thinks it's the definition but it isn't");
9367           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9368         }
9369       }
9370 
9371       continue;
9372     }
9373 
9374     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9375       // Make sure that the ObjCInterfaceType points at the definition.
9376       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9377         ->Decl = ID;
9378 
9379       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9380         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9381 
9382       continue;
9383     }
9384 
9385     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9386       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9387         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9388 
9389       continue;
9390     }
9391 
9392     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9393     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9394       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9395   }
9396   PendingDefinitions.clear();
9397 
9398   // Load the bodies of any functions or methods we've encountered. We do
9399   // this now (delayed) so that we can be sure that the declaration chains
9400   // have been fully wired up (hasBody relies on this).
9401   // FIXME: We shouldn't require complete redeclaration chains here.
9402   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9403                                PBEnd = PendingBodies.end();
9404        PB != PBEnd; ++PB) {
9405     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9406       // For a function defined inline within a class template, force the
9407       // canonical definition to be the one inside the canonical definition of
9408       // the template. This ensures that we instantiate from a correct view
9409       // of the template.
9410       //
9411       // Sadly we can't do this more generally: we can't be sure that all
9412       // copies of an arbitrary class definition will have the same members
9413       // defined (eg, some member functions may not be instantiated, and some
9414       // special members may or may not have been implicitly defined).
9415       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9416         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9417           continue;
9418 
9419       // FIXME: Check for =delete/=default?
9420       // FIXME: Complain about ODR violations here?
9421       const FunctionDecl *Defn = nullptr;
9422       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9423         FD->setLazyBody(PB->second);
9424       } else {
9425         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9426         mergeDefinitionVisibility(NonConstDefn, FD);
9427 
9428         if (!FD->isLateTemplateParsed() &&
9429             !NonConstDefn->isLateTemplateParsed() &&
9430             FD->getODRHash() != NonConstDefn->getODRHash()) {
9431           if (!isa<CXXMethodDecl>(FD)) {
9432             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9433           } else if (FD->getLexicalParent()->isFileContext() &&
9434                      NonConstDefn->getLexicalParent()->isFileContext()) {
9435             // Only diagnose out-of-line method definitions.  If they are
9436             // in class definitions, then an error will be generated when
9437             // processing the class bodies.
9438             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9439           }
9440         }
9441       }
9442       continue;
9443     }
9444 
9445     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9446     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9447       MD->setLazyBody(PB->second);
9448   }
9449   PendingBodies.clear();
9450 
9451   // Do some cleanup.
9452   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9453     getContext().deduplicateMergedDefinitonsFor(ND);
9454   PendingMergedDefinitionsToDeduplicate.clear();
9455 }
9456 
9457 void ASTReader::diagnoseOdrViolations() {
9458   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9459       PendingFunctionOdrMergeFailures.empty() &&
9460       PendingEnumOdrMergeFailures.empty())
9461     return;
9462 
9463   // Trigger the import of the full definition of each class that had any
9464   // odr-merging problems, so we can produce better diagnostics for them.
9465   // These updates may in turn find and diagnose some ODR failures, so take
9466   // ownership of the set first.
9467   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9468   PendingOdrMergeFailures.clear();
9469   for (auto &Merge : OdrMergeFailures) {
9470     Merge.first->buildLookup();
9471     Merge.first->decls_begin();
9472     Merge.first->bases_begin();
9473     Merge.first->vbases_begin();
9474     for (auto &RecordPair : Merge.second) {
9475       auto *RD = RecordPair.first;
9476       RD->decls_begin();
9477       RD->bases_begin();
9478       RD->vbases_begin();
9479     }
9480   }
9481 
9482   // Trigger the import of functions.
9483   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9484   PendingFunctionOdrMergeFailures.clear();
9485   for (auto &Merge : FunctionOdrMergeFailures) {
9486     Merge.first->buildLookup();
9487     Merge.first->decls_begin();
9488     Merge.first->getBody();
9489     for (auto &FD : Merge.second) {
9490       FD->buildLookup();
9491       FD->decls_begin();
9492       FD->getBody();
9493     }
9494   }
9495 
9496   // Trigger the import of enums.
9497   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9498   PendingEnumOdrMergeFailures.clear();
9499   for (auto &Merge : EnumOdrMergeFailures) {
9500     Merge.first->decls_begin();
9501     for (auto &Enum : Merge.second) {
9502       Enum->decls_begin();
9503     }
9504   }
9505 
9506   // For each declaration from a merged context, check that the canonical
9507   // definition of that context also contains a declaration of the same
9508   // entity.
9509   //
9510   // Caution: this loop does things that might invalidate iterators into
9511   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9512   while (!PendingOdrMergeChecks.empty()) {
9513     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9514 
9515     // FIXME: Skip over implicit declarations for now. This matters for things
9516     // like implicitly-declared special member functions. This isn't entirely
9517     // correct; we can end up with multiple unmerged declarations of the same
9518     // implicit entity.
9519     if (D->isImplicit())
9520       continue;
9521 
9522     DeclContext *CanonDef = D->getDeclContext();
9523 
9524     bool Found = false;
9525     const Decl *DCanon = D->getCanonicalDecl();
9526 
9527     for (auto RI : D->redecls()) {
9528       if (RI->getLexicalDeclContext() == CanonDef) {
9529         Found = true;
9530         break;
9531       }
9532     }
9533     if (Found)
9534       continue;
9535 
9536     // Quick check failed, time to do the slow thing. Note, we can't just
9537     // look up the name of D in CanonDef here, because the member that is
9538     // in CanonDef might not be found by name lookup (it might have been
9539     // replaced by a more recent declaration in the lookup table), and we
9540     // can't necessarily find it in the redeclaration chain because it might
9541     // be merely mergeable, not redeclarable.
9542     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9543     for (auto *CanonMember : CanonDef->decls()) {
9544       if (CanonMember->getCanonicalDecl() == DCanon) {
9545         // This can happen if the declaration is merely mergeable and not
9546         // actually redeclarable (we looked for redeclarations earlier).
9547         //
9548         // FIXME: We should be able to detect this more efficiently, without
9549         // pulling in all of the members of CanonDef.
9550         Found = true;
9551         break;
9552       }
9553       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9554         if (ND->getDeclName() == D->getDeclName())
9555           Candidates.push_back(ND);
9556     }
9557 
9558     if (!Found) {
9559       // The AST doesn't like TagDecls becoming invalid after they've been
9560       // completed. We only really need to mark FieldDecls as invalid here.
9561       if (!isa<TagDecl>(D))
9562         D->setInvalidDecl();
9563 
9564       // Ensure we don't accidentally recursively enter deserialization while
9565       // we're producing our diagnostic.
9566       Deserializing RecursionGuard(this);
9567 
9568       std::string CanonDefModule =
9569           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9570       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9571         << D << getOwningModuleNameForDiagnostic(D)
9572         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9573 
9574       if (Candidates.empty())
9575         Diag(cast<Decl>(CanonDef)->getLocation(),
9576              diag::note_module_odr_violation_no_possible_decls) << D;
9577       else {
9578         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9579           Diag(Candidates[I]->getLocation(),
9580                diag::note_module_odr_violation_possible_decl)
9581             << Candidates[I];
9582       }
9583 
9584       DiagnosedOdrMergeFailures.insert(CanonDef);
9585     }
9586   }
9587 
9588   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9589       EnumOdrMergeFailures.empty())
9590     return;
9591 
9592   // Ensure we don't accidentally recursively enter deserialization while
9593   // we're producing our diagnostics.
9594   Deserializing RecursionGuard(this);
9595 
9596   // Common code for hashing helpers.
9597   ODRHash Hash;
9598   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9599     Hash.clear();
9600     Hash.AddQualType(Ty);
9601     return Hash.CalculateHash();
9602   };
9603 
9604   auto ComputeODRHash = [&Hash](const Stmt *S) {
9605     assert(S);
9606     Hash.clear();
9607     Hash.AddStmt(S);
9608     return Hash.CalculateHash();
9609   };
9610 
9611   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9612     assert(D);
9613     Hash.clear();
9614     Hash.AddSubDecl(D);
9615     return Hash.CalculateHash();
9616   };
9617 
9618   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9619     Hash.clear();
9620     Hash.AddTemplateArgument(TA);
9621     return Hash.CalculateHash();
9622   };
9623 
9624   auto ComputeTemplateParameterListODRHash =
9625       [&Hash](const TemplateParameterList *TPL) {
9626         assert(TPL);
9627         Hash.clear();
9628         Hash.AddTemplateParameterList(TPL);
9629         return Hash.CalculateHash();
9630       };
9631 
9632   // Issue any pending ODR-failure diagnostics.
9633   for (auto &Merge : OdrMergeFailures) {
9634     // If we've already pointed out a specific problem with this class, don't
9635     // bother issuing a general "something's different" diagnostic.
9636     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9637       continue;
9638 
9639     bool Diagnosed = false;
9640     CXXRecordDecl *FirstRecord = Merge.first;
9641     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9642     for (auto &RecordPair : Merge.second) {
9643       CXXRecordDecl *SecondRecord = RecordPair.first;
9644       // Multiple different declarations got merged together; tell the user
9645       // where they came from.
9646       if (FirstRecord == SecondRecord)
9647         continue;
9648 
9649       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9650 
9651       auto *FirstDD = FirstRecord->DefinitionData;
9652       auto *SecondDD = RecordPair.second;
9653 
9654       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9655 
9656       // Diagnostics from DefinitionData are emitted here.
9657       if (FirstDD != SecondDD) {
9658         enum ODRDefinitionDataDifference {
9659           NumBases,
9660           NumVBases,
9661           BaseType,
9662           BaseVirtual,
9663           BaseAccess,
9664         };
9665         auto ODRDiagError = [FirstRecord, &FirstModule,
9666                              this](SourceLocation Loc, SourceRange Range,
9667                                    ODRDefinitionDataDifference DiffType) {
9668           return Diag(Loc, diag::err_module_odr_violation_definition_data)
9669                  << FirstRecord << FirstModule.empty() << FirstModule << Range
9670                  << DiffType;
9671         };
9672         auto ODRDiagNote = [&SecondModule,
9673                             this](SourceLocation Loc, SourceRange Range,
9674                                   ODRDefinitionDataDifference DiffType) {
9675           return Diag(Loc, diag::note_module_odr_violation_definition_data)
9676                  << SecondModule << Range << DiffType;
9677         };
9678 
9679         unsigned FirstNumBases = FirstDD->NumBases;
9680         unsigned FirstNumVBases = FirstDD->NumVBases;
9681         unsigned SecondNumBases = SecondDD->NumBases;
9682         unsigned SecondNumVBases = SecondDD->NumVBases;
9683 
9684         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9685           unsigned NumBases = DD->NumBases;
9686           if (NumBases == 0) return SourceRange();
9687           auto bases = DD->bases();
9688           return SourceRange(bases[0].getBeginLoc(),
9689                              bases[NumBases - 1].getEndLoc());
9690         };
9691 
9692         if (FirstNumBases != SecondNumBases) {
9693           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9694                        NumBases)
9695               << FirstNumBases;
9696           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9697                       NumBases)
9698               << SecondNumBases;
9699           Diagnosed = true;
9700           break;
9701         }
9702 
9703         if (FirstNumVBases != SecondNumVBases) {
9704           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9705                        NumVBases)
9706               << FirstNumVBases;
9707           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9708                       NumVBases)
9709               << SecondNumVBases;
9710           Diagnosed = true;
9711           break;
9712         }
9713 
9714         auto FirstBases = FirstDD->bases();
9715         auto SecondBases = SecondDD->bases();
9716         unsigned i = 0;
9717         for (i = 0; i < FirstNumBases; ++i) {
9718           auto FirstBase = FirstBases[i];
9719           auto SecondBase = SecondBases[i];
9720           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9721               ComputeQualTypeODRHash(SecondBase.getType())) {
9722             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9723                          BaseType)
9724                 << (i + 1) << FirstBase.getType();
9725             ODRDiagNote(SecondRecord->getLocation(),
9726                         SecondBase.getSourceRange(), BaseType)
9727                 << (i + 1) << SecondBase.getType();
9728             break;
9729           }
9730 
9731           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9732             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9733                          BaseVirtual)
9734                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9735             ODRDiagNote(SecondRecord->getLocation(),
9736                         SecondBase.getSourceRange(), BaseVirtual)
9737                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9738             break;
9739           }
9740 
9741           if (FirstBase.getAccessSpecifierAsWritten() !=
9742               SecondBase.getAccessSpecifierAsWritten()) {
9743             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9744                          BaseAccess)
9745                 << (i + 1) << FirstBase.getType()
9746                 << (int)FirstBase.getAccessSpecifierAsWritten();
9747             ODRDiagNote(SecondRecord->getLocation(),
9748                         SecondBase.getSourceRange(), BaseAccess)
9749                 << (i + 1) << SecondBase.getType()
9750                 << (int)SecondBase.getAccessSpecifierAsWritten();
9751             break;
9752           }
9753         }
9754 
9755         if (i != FirstNumBases) {
9756           Diagnosed = true;
9757           break;
9758         }
9759       }
9760 
9761       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9762 
9763       const ClassTemplateDecl *FirstTemplate =
9764           FirstRecord->getDescribedClassTemplate();
9765       const ClassTemplateDecl *SecondTemplate =
9766           SecondRecord->getDescribedClassTemplate();
9767 
9768       assert(!FirstTemplate == !SecondTemplate &&
9769              "Both pointers should be null or non-null");
9770 
9771       enum ODRTemplateDifference {
9772         ParamEmptyName,
9773         ParamName,
9774         ParamSingleDefaultArgument,
9775         ParamDifferentDefaultArgument,
9776       };
9777 
9778       if (FirstTemplate && SecondTemplate) {
9779         DeclHashes FirstTemplateHashes;
9780         DeclHashes SecondTemplateHashes;
9781 
9782         auto PopulateTemplateParameterHashs =
9783             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9784                                      const ClassTemplateDecl *TD) {
9785               for (auto *D : TD->getTemplateParameters()->asArray()) {
9786                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9787               }
9788             };
9789 
9790         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
9791         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
9792 
9793         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
9794                "Number of template parameters should be equal.");
9795 
9796         auto FirstIt = FirstTemplateHashes.begin();
9797         auto FirstEnd = FirstTemplateHashes.end();
9798         auto SecondIt = SecondTemplateHashes.begin();
9799         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
9800           if (FirstIt->second == SecondIt->second)
9801             continue;
9802 
9803           auto ODRDiagError = [FirstRecord, &FirstModule,
9804                                this](SourceLocation Loc, SourceRange Range,
9805                                      ODRTemplateDifference DiffType) {
9806             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
9807                    << FirstRecord << FirstModule.empty() << FirstModule << Range
9808                    << DiffType;
9809           };
9810           auto ODRDiagNote = [&SecondModule,
9811                               this](SourceLocation Loc, SourceRange Range,
9812                                     ODRTemplateDifference DiffType) {
9813             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
9814                    << SecondModule << Range << DiffType;
9815           };
9816 
9817           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
9818           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
9819 
9820           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
9821                  "Parameter Decl's should be the same kind.");
9822 
9823           DeclarationName FirstName = FirstDecl->getDeclName();
9824           DeclarationName SecondName = SecondDecl->getDeclName();
9825 
9826           if (FirstName != SecondName) {
9827             const bool FirstNameEmpty =
9828                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
9829             const bool SecondNameEmpty =
9830                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
9831             assert((!FirstNameEmpty || !SecondNameEmpty) &&
9832                    "Both template parameters cannot be unnamed.");
9833             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9834                          FirstNameEmpty ? ParamEmptyName : ParamName)
9835                 << FirstName;
9836             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9837                         SecondNameEmpty ? ParamEmptyName : ParamName)
9838                 << SecondName;
9839             break;
9840           }
9841 
9842           switch (FirstDecl->getKind()) {
9843           default:
9844             llvm_unreachable("Invalid template parameter type.");
9845           case Decl::TemplateTypeParm: {
9846             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
9847             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
9848             const bool HasFirstDefaultArgument =
9849                 FirstParam->hasDefaultArgument() &&
9850                 !FirstParam->defaultArgumentWasInherited();
9851             const bool HasSecondDefaultArgument =
9852                 SecondParam->hasDefaultArgument() &&
9853                 !SecondParam->defaultArgumentWasInherited();
9854 
9855             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9856               ODRDiagError(FirstDecl->getLocation(),
9857                            FirstDecl->getSourceRange(),
9858                            ParamSingleDefaultArgument)
9859                   << HasFirstDefaultArgument;
9860               ODRDiagNote(SecondDecl->getLocation(),
9861                           SecondDecl->getSourceRange(),
9862                           ParamSingleDefaultArgument)
9863                   << HasSecondDefaultArgument;
9864               break;
9865             }
9866 
9867             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9868                    "Expecting default arguments.");
9869 
9870             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9871                          ParamDifferentDefaultArgument);
9872             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9873                         ParamDifferentDefaultArgument);
9874 
9875             break;
9876           }
9877           case Decl::NonTypeTemplateParm: {
9878             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
9879             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
9880             const bool HasFirstDefaultArgument =
9881                 FirstParam->hasDefaultArgument() &&
9882                 !FirstParam->defaultArgumentWasInherited();
9883             const bool HasSecondDefaultArgument =
9884                 SecondParam->hasDefaultArgument() &&
9885                 !SecondParam->defaultArgumentWasInherited();
9886 
9887             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9888               ODRDiagError(FirstDecl->getLocation(),
9889                            FirstDecl->getSourceRange(),
9890                            ParamSingleDefaultArgument)
9891                   << HasFirstDefaultArgument;
9892               ODRDiagNote(SecondDecl->getLocation(),
9893                           SecondDecl->getSourceRange(),
9894                           ParamSingleDefaultArgument)
9895                   << HasSecondDefaultArgument;
9896               break;
9897             }
9898 
9899             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9900                    "Expecting default arguments.");
9901 
9902             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9903                          ParamDifferentDefaultArgument);
9904             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9905                         ParamDifferentDefaultArgument);
9906 
9907             break;
9908           }
9909           case Decl::TemplateTemplateParm: {
9910             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
9911             const auto *SecondParam =
9912                 cast<TemplateTemplateParmDecl>(SecondDecl);
9913             const bool HasFirstDefaultArgument =
9914                 FirstParam->hasDefaultArgument() &&
9915                 !FirstParam->defaultArgumentWasInherited();
9916             const bool HasSecondDefaultArgument =
9917                 SecondParam->hasDefaultArgument() &&
9918                 !SecondParam->defaultArgumentWasInherited();
9919 
9920             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9921               ODRDiagError(FirstDecl->getLocation(),
9922                            FirstDecl->getSourceRange(),
9923                            ParamSingleDefaultArgument)
9924                   << HasFirstDefaultArgument;
9925               ODRDiagNote(SecondDecl->getLocation(),
9926                           SecondDecl->getSourceRange(),
9927                           ParamSingleDefaultArgument)
9928                   << HasSecondDefaultArgument;
9929               break;
9930             }
9931 
9932             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9933                    "Expecting default arguments.");
9934 
9935             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9936                          ParamDifferentDefaultArgument);
9937             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9938                         ParamDifferentDefaultArgument);
9939 
9940             break;
9941           }
9942           }
9943 
9944           break;
9945         }
9946 
9947         if (FirstIt != FirstEnd) {
9948           Diagnosed = true;
9949           break;
9950         }
9951       }
9952 
9953       DeclHashes FirstHashes;
9954       DeclHashes SecondHashes;
9955 
9956       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
9957                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
9958         for (auto *D : Record->decls()) {
9959           // Due to decl merging, the first CXXRecordDecl is the parent of
9960           // Decls in both records.
9961           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
9962             continue;
9963           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9964         }
9965       };
9966       PopulateHashes(FirstHashes, FirstRecord);
9967       PopulateHashes(SecondHashes, SecondRecord);
9968 
9969       // Used with err_module_odr_violation_mismatch_decl and
9970       // note_module_odr_violation_mismatch_decl
9971       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9972       enum {
9973         EndOfClass,
9974         PublicSpecifer,
9975         PrivateSpecifer,
9976         ProtectedSpecifer,
9977         StaticAssert,
9978         Field,
9979         CXXMethod,
9980         TypeAlias,
9981         TypeDef,
9982         Var,
9983         Friend,
9984         FunctionTemplate,
9985         Other
9986       } FirstDiffType = Other,
9987         SecondDiffType = Other;
9988 
9989       auto DifferenceSelector = [](Decl *D) {
9990         assert(D && "valid Decl required");
9991         switch (D->getKind()) {
9992         default:
9993           return Other;
9994         case Decl::AccessSpec:
9995           switch (D->getAccess()) {
9996           case AS_public:
9997             return PublicSpecifer;
9998           case AS_private:
9999             return PrivateSpecifer;
10000           case AS_protected:
10001             return ProtectedSpecifer;
10002           case AS_none:
10003             break;
10004           }
10005           llvm_unreachable("Invalid access specifier");
10006         case Decl::StaticAssert:
10007           return StaticAssert;
10008         case Decl::Field:
10009           return Field;
10010         case Decl::CXXMethod:
10011         case Decl::CXXConstructor:
10012         case Decl::CXXDestructor:
10013           return CXXMethod;
10014         case Decl::TypeAlias:
10015           return TypeAlias;
10016         case Decl::Typedef:
10017           return TypeDef;
10018         case Decl::Var:
10019           return Var;
10020         case Decl::Friend:
10021           return Friend;
10022         case Decl::FunctionTemplate:
10023           return FunctionTemplate;
10024         }
10025       };
10026 
10027       Decl *FirstDecl = nullptr;
10028       Decl *SecondDecl = nullptr;
10029       auto FirstIt = FirstHashes.begin();
10030       auto SecondIt = SecondHashes.begin();
10031 
10032       // If there is a diagnoseable difference, FirstDiffType and
10033       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10034       // filled in if not EndOfClass.
10035       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10036         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10037             FirstIt->second == SecondIt->second) {
10038           ++FirstIt;
10039           ++SecondIt;
10040           continue;
10041         }
10042 
10043         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10044         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10045 
10046         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10047         SecondDiffType =
10048             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10049 
10050         break;
10051       }
10052 
10053       if (FirstDiffType == Other || SecondDiffType == Other) {
10054         // Reaching this point means an unexpected Decl was encountered
10055         // or no difference was detected.  This causes a generic error
10056         // message to be emitted.
10057         Diag(FirstRecord->getLocation(),
10058              diag::err_module_odr_violation_different_definitions)
10059             << FirstRecord << FirstModule.empty() << FirstModule;
10060 
10061         if (FirstDecl) {
10062           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10063               << FirstRecord << FirstDecl->getSourceRange();
10064         }
10065 
10066         Diag(SecondRecord->getLocation(),
10067              diag::note_module_odr_violation_different_definitions)
10068             << SecondModule;
10069 
10070         if (SecondDecl) {
10071           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10072               << SecondDecl->getSourceRange();
10073         }
10074 
10075         Diagnosed = true;
10076         break;
10077       }
10078 
10079       if (FirstDiffType != SecondDiffType) {
10080         SourceLocation FirstLoc;
10081         SourceRange FirstRange;
10082         if (FirstDiffType == EndOfClass) {
10083           FirstLoc = FirstRecord->getBraceRange().getEnd();
10084         } else {
10085           FirstLoc = FirstIt->first->getLocation();
10086           FirstRange = FirstIt->first->getSourceRange();
10087         }
10088         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10089             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10090             << FirstDiffType;
10091 
10092         SourceLocation SecondLoc;
10093         SourceRange SecondRange;
10094         if (SecondDiffType == EndOfClass) {
10095           SecondLoc = SecondRecord->getBraceRange().getEnd();
10096         } else {
10097           SecondLoc = SecondDecl->getLocation();
10098           SecondRange = SecondDecl->getSourceRange();
10099         }
10100         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10101             << SecondModule << SecondRange << SecondDiffType;
10102         Diagnosed = true;
10103         break;
10104       }
10105 
10106       assert(FirstDiffType == SecondDiffType);
10107 
10108       // Used with err_module_odr_violation_mismatch_decl_diff and
10109       // note_module_odr_violation_mismatch_decl_diff
10110       enum ODRDeclDifference {
10111         StaticAssertCondition,
10112         StaticAssertMessage,
10113         StaticAssertOnlyMessage,
10114         FieldName,
10115         FieldTypeName,
10116         FieldSingleBitField,
10117         FieldDifferentWidthBitField,
10118         FieldSingleMutable,
10119         FieldSingleInitializer,
10120         FieldDifferentInitializers,
10121         MethodName,
10122         MethodDeleted,
10123         MethodDefaulted,
10124         MethodVirtual,
10125         MethodStatic,
10126         MethodVolatile,
10127         MethodConst,
10128         MethodInline,
10129         MethodNumberParameters,
10130         MethodParameterType,
10131         MethodParameterName,
10132         MethodParameterSingleDefaultArgument,
10133         MethodParameterDifferentDefaultArgument,
10134         MethodNoTemplateArguments,
10135         MethodDifferentNumberTemplateArguments,
10136         MethodDifferentTemplateArgument,
10137         MethodSingleBody,
10138         MethodDifferentBody,
10139         TypedefName,
10140         TypedefType,
10141         VarName,
10142         VarType,
10143         VarSingleInitializer,
10144         VarDifferentInitializer,
10145         VarConstexpr,
10146         FriendTypeFunction,
10147         FriendType,
10148         FriendFunction,
10149         FunctionTemplateDifferentNumberParameters,
10150         FunctionTemplateParameterDifferentKind,
10151         FunctionTemplateParameterName,
10152         FunctionTemplateParameterSingleDefaultArgument,
10153         FunctionTemplateParameterDifferentDefaultArgument,
10154         FunctionTemplateParameterDifferentType,
10155         FunctionTemplatePackParameter,
10156       };
10157 
10158       // These lambdas have the common portions of the ODR diagnostics.  This
10159       // has the same return as Diag(), so addition parameters can be passed
10160       // in with operator<<
10161       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10162           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10163         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10164                << FirstRecord << FirstModule.empty() << FirstModule << Range
10165                << DiffType;
10166       };
10167       auto ODRDiagNote = [&SecondModule, this](
10168           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10169         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10170                << SecondModule << Range << DiffType;
10171       };
10172 
10173       switch (FirstDiffType) {
10174       case Other:
10175       case EndOfClass:
10176       case PublicSpecifer:
10177       case PrivateSpecifer:
10178       case ProtectedSpecifer:
10179         llvm_unreachable("Invalid diff type");
10180 
10181       case StaticAssert: {
10182         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10183         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10184 
10185         Expr *FirstExpr = FirstSA->getAssertExpr();
10186         Expr *SecondExpr = SecondSA->getAssertExpr();
10187         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10188         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10189         if (FirstODRHash != SecondODRHash) {
10190           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10191                        StaticAssertCondition);
10192           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10193                       StaticAssertCondition);
10194           Diagnosed = true;
10195           break;
10196         }
10197 
10198         StringLiteral *FirstStr = FirstSA->getMessage();
10199         StringLiteral *SecondStr = SecondSA->getMessage();
10200         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10201         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10202           SourceLocation FirstLoc, SecondLoc;
10203           SourceRange FirstRange, SecondRange;
10204           if (FirstStr) {
10205             FirstLoc = FirstStr->getBeginLoc();
10206             FirstRange = FirstStr->getSourceRange();
10207           } else {
10208             FirstLoc = FirstSA->getBeginLoc();
10209             FirstRange = FirstSA->getSourceRange();
10210           }
10211           if (SecondStr) {
10212             SecondLoc = SecondStr->getBeginLoc();
10213             SecondRange = SecondStr->getSourceRange();
10214           } else {
10215             SecondLoc = SecondSA->getBeginLoc();
10216             SecondRange = SecondSA->getSourceRange();
10217           }
10218           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10219               << (FirstStr == nullptr);
10220           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10221               << (SecondStr == nullptr);
10222           Diagnosed = true;
10223           break;
10224         }
10225 
10226         if (FirstStr && SecondStr &&
10227             FirstStr->getString() != SecondStr->getString()) {
10228           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10229                        StaticAssertMessage);
10230           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10231                       StaticAssertMessage);
10232           Diagnosed = true;
10233           break;
10234         }
10235         break;
10236       }
10237       case Field: {
10238         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10239         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10240         IdentifierInfo *FirstII = FirstField->getIdentifier();
10241         IdentifierInfo *SecondII = SecondField->getIdentifier();
10242         if (FirstII->getName() != SecondII->getName()) {
10243           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10244                        FieldName)
10245               << FirstII;
10246           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10247                       FieldName)
10248               << SecondII;
10249 
10250           Diagnosed = true;
10251           break;
10252         }
10253 
10254         assert(getContext().hasSameType(FirstField->getType(),
10255                                         SecondField->getType()));
10256 
10257         QualType FirstType = FirstField->getType();
10258         QualType SecondType = SecondField->getType();
10259         if (ComputeQualTypeODRHash(FirstType) !=
10260             ComputeQualTypeODRHash(SecondType)) {
10261           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10262                        FieldTypeName)
10263               << FirstII << FirstType;
10264           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10265                       FieldTypeName)
10266               << SecondII << SecondType;
10267 
10268           Diagnosed = true;
10269           break;
10270         }
10271 
10272         const bool IsFirstBitField = FirstField->isBitField();
10273         const bool IsSecondBitField = SecondField->isBitField();
10274         if (IsFirstBitField != IsSecondBitField) {
10275           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10276                        FieldSingleBitField)
10277               << FirstII << IsFirstBitField;
10278           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10279                       FieldSingleBitField)
10280               << SecondII << IsSecondBitField;
10281           Diagnosed = true;
10282           break;
10283         }
10284 
10285         if (IsFirstBitField && IsSecondBitField) {
10286           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10287                        FieldDifferentWidthBitField)
10288               << FirstII << FirstField->getBitWidth()->getSourceRange();
10289           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10290                       FieldDifferentWidthBitField)
10291               << SecondII << SecondField->getBitWidth()->getSourceRange();
10292           Diagnosed = true;
10293           break;
10294         }
10295 
10296         const bool IsFirstMutable = FirstField->isMutable();
10297         const bool IsSecondMutable = SecondField->isMutable();
10298         if (IsFirstMutable != IsSecondMutable) {
10299           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10300                        FieldSingleMutable)
10301               << FirstII << IsFirstMutable;
10302           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10303                       FieldSingleMutable)
10304               << SecondII << IsSecondMutable;
10305           Diagnosed = true;
10306           break;
10307         }
10308 
10309         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10310         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10311         if ((!FirstInitializer && SecondInitializer) ||
10312             (FirstInitializer && !SecondInitializer)) {
10313           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10314                        FieldSingleInitializer)
10315               << FirstII << (FirstInitializer != nullptr);
10316           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10317                       FieldSingleInitializer)
10318               << SecondII << (SecondInitializer != nullptr);
10319           Diagnosed = true;
10320           break;
10321         }
10322 
10323         if (FirstInitializer && SecondInitializer) {
10324           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10325           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10326           if (FirstInitHash != SecondInitHash) {
10327             ODRDiagError(FirstField->getLocation(),
10328                          FirstField->getSourceRange(),
10329                          FieldDifferentInitializers)
10330                 << FirstII << FirstInitializer->getSourceRange();
10331             ODRDiagNote(SecondField->getLocation(),
10332                         SecondField->getSourceRange(),
10333                         FieldDifferentInitializers)
10334                 << SecondII << SecondInitializer->getSourceRange();
10335             Diagnosed = true;
10336             break;
10337           }
10338         }
10339 
10340         break;
10341       }
10342       case CXXMethod: {
10343         enum {
10344           DiagMethod,
10345           DiagConstructor,
10346           DiagDestructor,
10347         } FirstMethodType,
10348             SecondMethodType;
10349         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10350           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10351           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10352           return DiagMethod;
10353         };
10354         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10355         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10356         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10357         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10358         auto FirstName = FirstMethod->getDeclName();
10359         auto SecondName = SecondMethod->getDeclName();
10360         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10361           ODRDiagError(FirstMethod->getLocation(),
10362                        FirstMethod->getSourceRange(), MethodName)
10363               << FirstMethodType << FirstName;
10364           ODRDiagNote(SecondMethod->getLocation(),
10365                       SecondMethod->getSourceRange(), MethodName)
10366               << SecondMethodType << SecondName;
10367 
10368           Diagnosed = true;
10369           break;
10370         }
10371 
10372         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10373         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10374         if (FirstDeleted != SecondDeleted) {
10375           ODRDiagError(FirstMethod->getLocation(),
10376                        FirstMethod->getSourceRange(), MethodDeleted)
10377               << FirstMethodType << FirstName << FirstDeleted;
10378 
10379           ODRDiagNote(SecondMethod->getLocation(),
10380                       SecondMethod->getSourceRange(), MethodDeleted)
10381               << SecondMethodType << SecondName << SecondDeleted;
10382           Diagnosed = true;
10383           break;
10384         }
10385 
10386         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10387         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10388         if (FirstDefaulted != SecondDefaulted) {
10389           ODRDiagError(FirstMethod->getLocation(),
10390                        FirstMethod->getSourceRange(), MethodDefaulted)
10391               << FirstMethodType << FirstName << FirstDefaulted;
10392 
10393           ODRDiagNote(SecondMethod->getLocation(),
10394                       SecondMethod->getSourceRange(), MethodDefaulted)
10395               << SecondMethodType << SecondName << SecondDefaulted;
10396           Diagnosed = true;
10397           break;
10398         }
10399 
10400         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10401         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10402         const bool FirstPure = FirstMethod->isPure();
10403         const bool SecondPure = SecondMethod->isPure();
10404         if ((FirstVirtual || SecondVirtual) &&
10405             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10406           ODRDiagError(FirstMethod->getLocation(),
10407                        FirstMethod->getSourceRange(), MethodVirtual)
10408               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10409           ODRDiagNote(SecondMethod->getLocation(),
10410                       SecondMethod->getSourceRange(), MethodVirtual)
10411               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10412           Diagnosed = true;
10413           break;
10414         }
10415 
10416         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10417         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10418         // class needs to be checked instead.
10419         const auto FirstStorage = FirstMethod->getStorageClass();
10420         const auto SecondStorage = SecondMethod->getStorageClass();
10421         const bool FirstStatic = FirstStorage == SC_Static;
10422         const bool SecondStatic = SecondStorage == SC_Static;
10423         if (FirstStatic != SecondStatic) {
10424           ODRDiagError(FirstMethod->getLocation(),
10425                        FirstMethod->getSourceRange(), MethodStatic)
10426               << FirstMethodType << FirstName << FirstStatic;
10427           ODRDiagNote(SecondMethod->getLocation(),
10428                       SecondMethod->getSourceRange(), MethodStatic)
10429               << SecondMethodType << SecondName << SecondStatic;
10430           Diagnosed = true;
10431           break;
10432         }
10433 
10434         const bool FirstVolatile = FirstMethod->isVolatile();
10435         const bool SecondVolatile = SecondMethod->isVolatile();
10436         if (FirstVolatile != SecondVolatile) {
10437           ODRDiagError(FirstMethod->getLocation(),
10438                        FirstMethod->getSourceRange(), MethodVolatile)
10439               << FirstMethodType << FirstName << FirstVolatile;
10440           ODRDiagNote(SecondMethod->getLocation(),
10441                       SecondMethod->getSourceRange(), MethodVolatile)
10442               << SecondMethodType << SecondName << SecondVolatile;
10443           Diagnosed = true;
10444           break;
10445         }
10446 
10447         const bool FirstConst = FirstMethod->isConst();
10448         const bool SecondConst = SecondMethod->isConst();
10449         if (FirstConst != SecondConst) {
10450           ODRDiagError(FirstMethod->getLocation(),
10451                        FirstMethod->getSourceRange(), MethodConst)
10452               << FirstMethodType << FirstName << FirstConst;
10453           ODRDiagNote(SecondMethod->getLocation(),
10454                       SecondMethod->getSourceRange(), MethodConst)
10455               << SecondMethodType << SecondName << SecondConst;
10456           Diagnosed = true;
10457           break;
10458         }
10459 
10460         const bool FirstInline = FirstMethod->isInlineSpecified();
10461         const bool SecondInline = SecondMethod->isInlineSpecified();
10462         if (FirstInline != SecondInline) {
10463           ODRDiagError(FirstMethod->getLocation(),
10464                        FirstMethod->getSourceRange(), MethodInline)
10465               << FirstMethodType << FirstName << FirstInline;
10466           ODRDiagNote(SecondMethod->getLocation(),
10467                       SecondMethod->getSourceRange(), MethodInline)
10468               << SecondMethodType << SecondName << SecondInline;
10469           Diagnosed = true;
10470           break;
10471         }
10472 
10473         const unsigned FirstNumParameters = FirstMethod->param_size();
10474         const unsigned SecondNumParameters = SecondMethod->param_size();
10475         if (FirstNumParameters != SecondNumParameters) {
10476           ODRDiagError(FirstMethod->getLocation(),
10477                        FirstMethod->getSourceRange(), MethodNumberParameters)
10478               << FirstMethodType << FirstName << FirstNumParameters;
10479           ODRDiagNote(SecondMethod->getLocation(),
10480                       SecondMethod->getSourceRange(), MethodNumberParameters)
10481               << SecondMethodType << SecondName << SecondNumParameters;
10482           Diagnosed = true;
10483           break;
10484         }
10485 
10486         // Need this status boolean to know when break out of the switch.
10487         bool ParameterMismatch = false;
10488         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10489           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10490           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10491 
10492           QualType FirstParamType = FirstParam->getType();
10493           QualType SecondParamType = SecondParam->getType();
10494           if (FirstParamType != SecondParamType &&
10495               ComputeQualTypeODRHash(FirstParamType) !=
10496                   ComputeQualTypeODRHash(SecondParamType)) {
10497             if (const DecayedType *ParamDecayedType =
10498                     FirstParamType->getAs<DecayedType>()) {
10499               ODRDiagError(FirstMethod->getLocation(),
10500                            FirstMethod->getSourceRange(), MethodParameterType)
10501                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10502                   << true << ParamDecayedType->getOriginalType();
10503             } else {
10504               ODRDiagError(FirstMethod->getLocation(),
10505                            FirstMethod->getSourceRange(), MethodParameterType)
10506                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10507                   << false;
10508             }
10509 
10510             if (const DecayedType *ParamDecayedType =
10511                     SecondParamType->getAs<DecayedType>()) {
10512               ODRDiagNote(SecondMethod->getLocation(),
10513                           SecondMethod->getSourceRange(), MethodParameterType)
10514                   << SecondMethodType << SecondName << (I + 1)
10515                   << SecondParamType << true
10516                   << ParamDecayedType->getOriginalType();
10517             } else {
10518               ODRDiagNote(SecondMethod->getLocation(),
10519                           SecondMethod->getSourceRange(), MethodParameterType)
10520                   << SecondMethodType << SecondName << (I + 1)
10521                   << SecondParamType << false;
10522             }
10523             ParameterMismatch = true;
10524             break;
10525           }
10526 
10527           DeclarationName FirstParamName = FirstParam->getDeclName();
10528           DeclarationName SecondParamName = SecondParam->getDeclName();
10529           if (FirstParamName != SecondParamName) {
10530             ODRDiagError(FirstMethod->getLocation(),
10531                          FirstMethod->getSourceRange(), MethodParameterName)
10532                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10533             ODRDiagNote(SecondMethod->getLocation(),
10534                         SecondMethod->getSourceRange(), MethodParameterName)
10535                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10536             ParameterMismatch = true;
10537             break;
10538           }
10539 
10540           const Expr *FirstInit = FirstParam->getInit();
10541           const Expr *SecondInit = SecondParam->getInit();
10542           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10543             ODRDiagError(FirstMethod->getLocation(),
10544                          FirstMethod->getSourceRange(),
10545                          MethodParameterSingleDefaultArgument)
10546                 << FirstMethodType << FirstName << (I + 1)
10547                 << (FirstInit == nullptr)
10548                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10549             ODRDiagNote(SecondMethod->getLocation(),
10550                         SecondMethod->getSourceRange(),
10551                         MethodParameterSingleDefaultArgument)
10552                 << SecondMethodType << SecondName << (I + 1)
10553                 << (SecondInit == nullptr)
10554                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10555             ParameterMismatch = true;
10556             break;
10557           }
10558 
10559           if (FirstInit && SecondInit &&
10560               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10561             ODRDiagError(FirstMethod->getLocation(),
10562                          FirstMethod->getSourceRange(),
10563                          MethodParameterDifferentDefaultArgument)
10564                 << FirstMethodType << FirstName << (I + 1)
10565                 << FirstInit->getSourceRange();
10566             ODRDiagNote(SecondMethod->getLocation(),
10567                         SecondMethod->getSourceRange(),
10568                         MethodParameterDifferentDefaultArgument)
10569                 << SecondMethodType << SecondName << (I + 1)
10570                 << SecondInit->getSourceRange();
10571             ParameterMismatch = true;
10572             break;
10573 
10574           }
10575         }
10576 
10577         if (ParameterMismatch) {
10578           Diagnosed = true;
10579           break;
10580         }
10581 
10582         const auto *FirstTemplateArgs =
10583             FirstMethod->getTemplateSpecializationArgs();
10584         const auto *SecondTemplateArgs =
10585             SecondMethod->getTemplateSpecializationArgs();
10586 
10587         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10588             (!FirstTemplateArgs && SecondTemplateArgs)) {
10589           ODRDiagError(FirstMethod->getLocation(),
10590                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
10591               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10592           ODRDiagNote(SecondMethod->getLocation(),
10593                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
10594               << SecondMethodType << SecondName
10595               << (SecondTemplateArgs != nullptr);
10596 
10597           Diagnosed = true;
10598           break;
10599         }
10600 
10601         if (FirstTemplateArgs && SecondTemplateArgs) {
10602           // Remove pack expansions from argument list.
10603           auto ExpandTemplateArgumentList =
10604               [](const TemplateArgumentList *TAL) {
10605                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10606                 for (const TemplateArgument &TA : TAL->asArray()) {
10607                   if (TA.getKind() != TemplateArgument::Pack) {
10608                     ExpandedList.push_back(&TA);
10609                     continue;
10610                   }
10611                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10612                     ExpandedList.push_back(&PackTA);
10613                   }
10614                 }
10615                 return ExpandedList;
10616               };
10617           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10618               ExpandTemplateArgumentList(FirstTemplateArgs);
10619           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10620               ExpandTemplateArgumentList(SecondTemplateArgs);
10621 
10622           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10623             ODRDiagError(FirstMethod->getLocation(),
10624                          FirstMethod->getSourceRange(),
10625                          MethodDifferentNumberTemplateArguments)
10626                 << FirstMethodType << FirstName
10627                 << (unsigned)FirstExpandedList.size();
10628             ODRDiagNote(SecondMethod->getLocation(),
10629                         SecondMethod->getSourceRange(),
10630                         MethodDifferentNumberTemplateArguments)
10631                 << SecondMethodType << SecondName
10632                 << (unsigned)SecondExpandedList.size();
10633 
10634             Diagnosed = true;
10635             break;
10636           }
10637 
10638           bool TemplateArgumentMismatch = false;
10639           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10640             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10641                                    &SecondTA = *SecondExpandedList[i];
10642             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10643                 ComputeTemplateArgumentODRHash(SecondTA)) {
10644               continue;
10645             }
10646 
10647             ODRDiagError(FirstMethod->getLocation(),
10648                          FirstMethod->getSourceRange(),
10649                          MethodDifferentTemplateArgument)
10650                 << FirstMethodType << FirstName << FirstTA << i + 1;
10651             ODRDiagNote(SecondMethod->getLocation(),
10652                         SecondMethod->getSourceRange(),
10653                         MethodDifferentTemplateArgument)
10654                 << SecondMethodType << SecondName << SecondTA << i + 1;
10655 
10656             TemplateArgumentMismatch = true;
10657             break;
10658           }
10659 
10660           if (TemplateArgumentMismatch) {
10661             Diagnosed = true;
10662             break;
10663           }
10664         }
10665 
10666         // Compute the hash of the method as if it has no body.
10667         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10668           Hash.clear();
10669           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10670           return Hash.CalculateHash();
10671         };
10672 
10673         // Compare the hash generated to the hash stored.  A difference means
10674         // that a body was present in the original source.  Due to merging,
10675         // the stardard way of detecting a body will not work.
10676         const bool HasFirstBody =
10677             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10678         const bool HasSecondBody =
10679             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10680 
10681         if (HasFirstBody != HasSecondBody) {
10682           ODRDiagError(FirstMethod->getLocation(),
10683                        FirstMethod->getSourceRange(), MethodSingleBody)
10684               << FirstMethodType << FirstName << HasFirstBody;
10685           ODRDiagNote(SecondMethod->getLocation(),
10686                       SecondMethod->getSourceRange(), MethodSingleBody)
10687               << SecondMethodType << SecondName << HasSecondBody;
10688           Diagnosed = true;
10689           break;
10690         }
10691 
10692         if (HasFirstBody && HasSecondBody) {
10693           ODRDiagError(FirstMethod->getLocation(),
10694                        FirstMethod->getSourceRange(), MethodDifferentBody)
10695               << FirstMethodType << FirstName;
10696           ODRDiagNote(SecondMethod->getLocation(),
10697                       SecondMethod->getSourceRange(), MethodDifferentBody)
10698               << SecondMethodType << SecondName;
10699           Diagnosed = true;
10700           break;
10701         }
10702 
10703         break;
10704       }
10705       case TypeAlias:
10706       case TypeDef: {
10707         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
10708         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
10709         auto FirstName = FirstTD->getDeclName();
10710         auto SecondName = SecondTD->getDeclName();
10711         if (FirstName != SecondName) {
10712           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10713                        TypedefName)
10714               << (FirstDiffType == TypeAlias) << FirstName;
10715           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10716                       TypedefName)
10717               << (FirstDiffType == TypeAlias) << SecondName;
10718           Diagnosed = true;
10719           break;
10720         }
10721 
10722         QualType FirstType = FirstTD->getUnderlyingType();
10723         QualType SecondType = SecondTD->getUnderlyingType();
10724         if (ComputeQualTypeODRHash(FirstType) !=
10725             ComputeQualTypeODRHash(SecondType)) {
10726           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10727                        TypedefType)
10728               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
10729           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10730                       TypedefType)
10731               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
10732           Diagnosed = true;
10733           break;
10734         }
10735         break;
10736       }
10737       case Var: {
10738         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
10739         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
10740         auto FirstName = FirstVD->getDeclName();
10741         auto SecondName = SecondVD->getDeclName();
10742         if (FirstName != SecondName) {
10743           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10744                        VarName)
10745               << FirstName;
10746           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10747                       VarName)
10748               << SecondName;
10749           Diagnosed = true;
10750           break;
10751         }
10752 
10753         QualType FirstType = FirstVD->getType();
10754         QualType SecondType = SecondVD->getType();
10755         if (ComputeQualTypeODRHash(FirstType) !=
10756                         ComputeQualTypeODRHash(SecondType)) {
10757           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10758                        VarType)
10759               << FirstName << FirstType;
10760           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10761                       VarType)
10762               << SecondName << SecondType;
10763           Diagnosed = true;
10764           break;
10765         }
10766 
10767         const Expr *FirstInit = FirstVD->getInit();
10768         const Expr *SecondInit = SecondVD->getInit();
10769         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10770           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10771                        VarSingleInitializer)
10772               << FirstName << (FirstInit == nullptr)
10773               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
10774           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10775                       VarSingleInitializer)
10776               << SecondName << (SecondInit == nullptr)
10777               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10778           Diagnosed = true;
10779           break;
10780         }
10781 
10782         if (FirstInit && SecondInit &&
10783             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10784           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10785                        VarDifferentInitializer)
10786               << FirstName << FirstInit->getSourceRange();
10787           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10788                       VarDifferentInitializer)
10789               << SecondName << SecondInit->getSourceRange();
10790           Diagnosed = true;
10791           break;
10792         }
10793 
10794         const bool FirstIsConstexpr = FirstVD->isConstexpr();
10795         const bool SecondIsConstexpr = SecondVD->isConstexpr();
10796         if (FirstIsConstexpr != SecondIsConstexpr) {
10797           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10798                        VarConstexpr)
10799               << FirstName << FirstIsConstexpr;
10800           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10801                       VarConstexpr)
10802               << SecondName << SecondIsConstexpr;
10803           Diagnosed = true;
10804           break;
10805         }
10806         break;
10807       }
10808       case Friend: {
10809         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10810         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10811 
10812         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10813         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10814 
10815         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10816         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10817 
10818         if (FirstND && SecondND) {
10819           ODRDiagError(FirstFriend->getFriendLoc(),
10820                        FirstFriend->getSourceRange(), FriendFunction)
10821               << FirstND;
10822           ODRDiagNote(SecondFriend->getFriendLoc(),
10823                       SecondFriend->getSourceRange(), FriendFunction)
10824               << SecondND;
10825 
10826           Diagnosed = true;
10827           break;
10828         }
10829 
10830         if (FirstTSI && SecondTSI) {
10831           QualType FirstFriendType = FirstTSI->getType();
10832           QualType SecondFriendType = SecondTSI->getType();
10833           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10834                  ComputeQualTypeODRHash(SecondFriendType));
10835           ODRDiagError(FirstFriend->getFriendLoc(),
10836                        FirstFriend->getSourceRange(), FriendType)
10837               << FirstFriendType;
10838           ODRDiagNote(SecondFriend->getFriendLoc(),
10839                       SecondFriend->getSourceRange(), FriendType)
10840               << SecondFriendType;
10841           Diagnosed = true;
10842           break;
10843         }
10844 
10845         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
10846                      FriendTypeFunction)
10847             << (FirstTSI == nullptr);
10848         ODRDiagNote(SecondFriend->getFriendLoc(),
10849                     SecondFriend->getSourceRange(), FriendTypeFunction)
10850             << (SecondTSI == nullptr);
10851 
10852         Diagnosed = true;
10853         break;
10854       }
10855       case FunctionTemplate: {
10856         FunctionTemplateDecl *FirstTemplate =
10857             cast<FunctionTemplateDecl>(FirstDecl);
10858         FunctionTemplateDecl *SecondTemplate =
10859             cast<FunctionTemplateDecl>(SecondDecl);
10860 
10861         TemplateParameterList *FirstTPL =
10862             FirstTemplate->getTemplateParameters();
10863         TemplateParameterList *SecondTPL =
10864             SecondTemplate->getTemplateParameters();
10865 
10866         if (FirstTPL->size() != SecondTPL->size()) {
10867           ODRDiagError(FirstTemplate->getLocation(),
10868                        FirstTemplate->getSourceRange(),
10869                        FunctionTemplateDifferentNumberParameters)
10870               << FirstTemplate << FirstTPL->size();
10871           ODRDiagNote(SecondTemplate->getLocation(),
10872                       SecondTemplate->getSourceRange(),
10873                       FunctionTemplateDifferentNumberParameters)
10874               << SecondTemplate  << SecondTPL->size();
10875 
10876           Diagnosed = true;
10877           break;
10878         }
10879 
10880         bool ParameterMismatch = false;
10881         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10882           NamedDecl *FirstParam = FirstTPL->getParam(i);
10883           NamedDecl *SecondParam = SecondTPL->getParam(i);
10884 
10885           if (FirstParam->getKind() != SecondParam->getKind()) {
10886             enum {
10887               TemplateTypeParameter,
10888               NonTypeTemplateParameter,
10889               TemplateTemplateParameter,
10890             };
10891             auto GetParamType = [](NamedDecl *D) {
10892               switch (D->getKind()) {
10893                 default:
10894                   llvm_unreachable("Unexpected template parameter type");
10895                 case Decl::TemplateTypeParm:
10896                   return TemplateTypeParameter;
10897                 case Decl::NonTypeTemplateParm:
10898                   return NonTypeTemplateParameter;
10899                 case Decl::TemplateTemplateParm:
10900                   return TemplateTemplateParameter;
10901               }
10902             };
10903 
10904             ODRDiagError(FirstTemplate->getLocation(),
10905                          FirstTemplate->getSourceRange(),
10906                          FunctionTemplateParameterDifferentKind)
10907                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10908             ODRDiagNote(SecondTemplate->getLocation(),
10909                         SecondTemplate->getSourceRange(),
10910                         FunctionTemplateParameterDifferentKind)
10911                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10912 
10913             ParameterMismatch = true;
10914             break;
10915           }
10916 
10917           if (FirstParam->getName() != SecondParam->getName()) {
10918             ODRDiagError(FirstTemplate->getLocation(),
10919                          FirstTemplate->getSourceRange(),
10920                          FunctionTemplateParameterName)
10921                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10922                 << FirstParam;
10923             ODRDiagNote(SecondTemplate->getLocation(),
10924                         SecondTemplate->getSourceRange(),
10925                         FunctionTemplateParameterName)
10926                 << SecondTemplate << (i + 1)
10927                 << (bool)SecondParam->getIdentifier() << SecondParam;
10928             ParameterMismatch = true;
10929             break;
10930           }
10931 
10932           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10933               isa<TemplateTypeParmDecl>(SecondParam)) {
10934             TemplateTypeParmDecl *FirstTTPD =
10935                 cast<TemplateTypeParmDecl>(FirstParam);
10936             TemplateTypeParmDecl *SecondTTPD =
10937                 cast<TemplateTypeParmDecl>(SecondParam);
10938             bool HasFirstDefaultArgument =
10939                 FirstTTPD->hasDefaultArgument() &&
10940                 !FirstTTPD->defaultArgumentWasInherited();
10941             bool HasSecondDefaultArgument =
10942                 SecondTTPD->hasDefaultArgument() &&
10943                 !SecondTTPD->defaultArgumentWasInherited();
10944             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10945               ODRDiagError(FirstTemplate->getLocation(),
10946                            FirstTemplate->getSourceRange(),
10947                            FunctionTemplateParameterSingleDefaultArgument)
10948                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10949               ODRDiagNote(SecondTemplate->getLocation(),
10950                           SecondTemplate->getSourceRange(),
10951                           FunctionTemplateParameterSingleDefaultArgument)
10952                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10953               ParameterMismatch = true;
10954               break;
10955             }
10956 
10957             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10958               QualType FirstType = FirstTTPD->getDefaultArgument();
10959               QualType SecondType = SecondTTPD->getDefaultArgument();
10960               if (ComputeQualTypeODRHash(FirstType) !=
10961                   ComputeQualTypeODRHash(SecondType)) {
10962                 ODRDiagError(FirstTemplate->getLocation(),
10963                              FirstTemplate->getSourceRange(),
10964                              FunctionTemplateParameterDifferentDefaultArgument)
10965                     << FirstTemplate << (i + 1) << FirstType;
10966                 ODRDiagNote(SecondTemplate->getLocation(),
10967                             SecondTemplate->getSourceRange(),
10968                             FunctionTemplateParameterDifferentDefaultArgument)
10969                     << SecondTemplate << (i + 1) << SecondType;
10970                 ParameterMismatch = true;
10971                 break;
10972               }
10973             }
10974 
10975             if (FirstTTPD->isParameterPack() !=
10976                 SecondTTPD->isParameterPack()) {
10977               ODRDiagError(FirstTemplate->getLocation(),
10978                            FirstTemplate->getSourceRange(),
10979                            FunctionTemplatePackParameter)
10980                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10981               ODRDiagNote(SecondTemplate->getLocation(),
10982                           SecondTemplate->getSourceRange(),
10983                           FunctionTemplatePackParameter)
10984                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10985               ParameterMismatch = true;
10986               break;
10987             }
10988           }
10989 
10990           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10991               isa<TemplateTemplateParmDecl>(SecondParam)) {
10992             TemplateTemplateParmDecl *FirstTTPD =
10993                 cast<TemplateTemplateParmDecl>(FirstParam);
10994             TemplateTemplateParmDecl *SecondTTPD =
10995                 cast<TemplateTemplateParmDecl>(SecondParam);
10996 
10997             TemplateParameterList *FirstTPL =
10998                 FirstTTPD->getTemplateParameters();
10999             TemplateParameterList *SecondTPL =
11000                 SecondTTPD->getTemplateParameters();
11001 
11002             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11003                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11004               ODRDiagError(FirstTemplate->getLocation(),
11005                            FirstTemplate->getSourceRange(),
11006                            FunctionTemplateParameterDifferentType)
11007                   << FirstTemplate << (i + 1);
11008               ODRDiagNote(SecondTemplate->getLocation(),
11009                           SecondTemplate->getSourceRange(),
11010                           FunctionTemplateParameterDifferentType)
11011                   << SecondTemplate << (i + 1);
11012               ParameterMismatch = true;
11013               break;
11014             }
11015 
11016             bool HasFirstDefaultArgument =
11017                 FirstTTPD->hasDefaultArgument() &&
11018                 !FirstTTPD->defaultArgumentWasInherited();
11019             bool HasSecondDefaultArgument =
11020                 SecondTTPD->hasDefaultArgument() &&
11021                 !SecondTTPD->defaultArgumentWasInherited();
11022             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11023               ODRDiagError(FirstTemplate->getLocation(),
11024                            FirstTemplate->getSourceRange(),
11025                            FunctionTemplateParameterSingleDefaultArgument)
11026                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11027               ODRDiagNote(SecondTemplate->getLocation(),
11028                           SecondTemplate->getSourceRange(),
11029                           FunctionTemplateParameterSingleDefaultArgument)
11030                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11031               ParameterMismatch = true;
11032               break;
11033             }
11034 
11035             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11036               TemplateArgument FirstTA =
11037                   FirstTTPD->getDefaultArgument().getArgument();
11038               TemplateArgument SecondTA =
11039                   SecondTTPD->getDefaultArgument().getArgument();
11040               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11041                   ComputeTemplateArgumentODRHash(SecondTA)) {
11042                 ODRDiagError(FirstTemplate->getLocation(),
11043                              FirstTemplate->getSourceRange(),
11044                              FunctionTemplateParameterDifferentDefaultArgument)
11045                     << FirstTemplate << (i + 1) << FirstTA;
11046                 ODRDiagNote(SecondTemplate->getLocation(),
11047                             SecondTemplate->getSourceRange(),
11048                             FunctionTemplateParameterDifferentDefaultArgument)
11049                     << SecondTemplate << (i + 1) << SecondTA;
11050                 ParameterMismatch = true;
11051                 break;
11052               }
11053             }
11054 
11055             if (FirstTTPD->isParameterPack() !=
11056                 SecondTTPD->isParameterPack()) {
11057               ODRDiagError(FirstTemplate->getLocation(),
11058                            FirstTemplate->getSourceRange(),
11059                            FunctionTemplatePackParameter)
11060                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11061               ODRDiagNote(SecondTemplate->getLocation(),
11062                           SecondTemplate->getSourceRange(),
11063                           FunctionTemplatePackParameter)
11064                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11065               ParameterMismatch = true;
11066               break;
11067             }
11068           }
11069 
11070           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11071               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11072             NonTypeTemplateParmDecl *FirstNTTPD =
11073                 cast<NonTypeTemplateParmDecl>(FirstParam);
11074             NonTypeTemplateParmDecl *SecondNTTPD =
11075                 cast<NonTypeTemplateParmDecl>(SecondParam);
11076 
11077             QualType FirstType = FirstNTTPD->getType();
11078             QualType SecondType = SecondNTTPD->getType();
11079             if (ComputeQualTypeODRHash(FirstType) !=
11080                 ComputeQualTypeODRHash(SecondType)) {
11081               ODRDiagError(FirstTemplate->getLocation(),
11082                            FirstTemplate->getSourceRange(),
11083                            FunctionTemplateParameterDifferentType)
11084                   << FirstTemplate << (i + 1);
11085               ODRDiagNote(SecondTemplate->getLocation(),
11086                           SecondTemplate->getSourceRange(),
11087                           FunctionTemplateParameterDifferentType)
11088                   << SecondTemplate << (i + 1);
11089               ParameterMismatch = true;
11090               break;
11091             }
11092 
11093             bool HasFirstDefaultArgument =
11094                 FirstNTTPD->hasDefaultArgument() &&
11095                 !FirstNTTPD->defaultArgumentWasInherited();
11096             bool HasSecondDefaultArgument =
11097                 SecondNTTPD->hasDefaultArgument() &&
11098                 !SecondNTTPD->defaultArgumentWasInherited();
11099             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11100               ODRDiagError(FirstTemplate->getLocation(),
11101                            FirstTemplate->getSourceRange(),
11102                            FunctionTemplateParameterSingleDefaultArgument)
11103                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11104               ODRDiagNote(SecondTemplate->getLocation(),
11105                           SecondTemplate->getSourceRange(),
11106                           FunctionTemplateParameterSingleDefaultArgument)
11107                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11108               ParameterMismatch = true;
11109               break;
11110             }
11111 
11112             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11113               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11114               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11115               if (ComputeODRHash(FirstDefaultArgument) !=
11116                   ComputeODRHash(SecondDefaultArgument)) {
11117                 ODRDiagError(FirstTemplate->getLocation(),
11118                              FirstTemplate->getSourceRange(),
11119                              FunctionTemplateParameterDifferentDefaultArgument)
11120                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11121                 ODRDiagNote(SecondTemplate->getLocation(),
11122                             SecondTemplate->getSourceRange(),
11123                             FunctionTemplateParameterDifferentDefaultArgument)
11124                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11125                 ParameterMismatch = true;
11126                 break;
11127               }
11128             }
11129 
11130             if (FirstNTTPD->isParameterPack() !=
11131                 SecondNTTPD->isParameterPack()) {
11132               ODRDiagError(FirstTemplate->getLocation(),
11133                            FirstTemplate->getSourceRange(),
11134                            FunctionTemplatePackParameter)
11135                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11136               ODRDiagNote(SecondTemplate->getLocation(),
11137                           SecondTemplate->getSourceRange(),
11138                           FunctionTemplatePackParameter)
11139                   << SecondTemplate << (i + 1)
11140                   << SecondNTTPD->isParameterPack();
11141               ParameterMismatch = true;
11142               break;
11143             }
11144           }
11145         }
11146 
11147         if (ParameterMismatch) {
11148           Diagnosed = true;
11149           break;
11150         }
11151 
11152         break;
11153       }
11154       }
11155 
11156       if (Diagnosed)
11157         continue;
11158 
11159       Diag(FirstDecl->getLocation(),
11160            diag::err_module_odr_violation_mismatch_decl_unknown)
11161           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11162           << FirstDecl->getSourceRange();
11163       Diag(SecondDecl->getLocation(),
11164            diag::note_module_odr_violation_mismatch_decl_unknown)
11165           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11166       Diagnosed = true;
11167     }
11168 
11169     if (!Diagnosed) {
11170       // All definitions are updates to the same declaration. This happens if a
11171       // module instantiates the declaration of a class template specialization
11172       // and two or more other modules instantiate its definition.
11173       //
11174       // FIXME: Indicate which modules had instantiations of this definition.
11175       // FIXME: How can this even happen?
11176       Diag(Merge.first->getLocation(),
11177            diag::err_module_odr_violation_different_instantiations)
11178         << Merge.first;
11179     }
11180   }
11181 
11182   // Issue ODR failures diagnostics for functions.
11183   for (auto &Merge : FunctionOdrMergeFailures) {
11184     enum ODRFunctionDifference {
11185       ReturnType,
11186       ParameterName,
11187       ParameterType,
11188       ParameterSingleDefaultArgument,
11189       ParameterDifferentDefaultArgument,
11190       FunctionBody,
11191     };
11192 
11193     FunctionDecl *FirstFunction = Merge.first;
11194     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11195 
11196     bool Diagnosed = false;
11197     for (auto &SecondFunction : Merge.second) {
11198 
11199       if (FirstFunction == SecondFunction)
11200         continue;
11201 
11202       std::string SecondModule =
11203           getOwningModuleNameForDiagnostic(SecondFunction);
11204 
11205       auto ODRDiagError = [FirstFunction, &FirstModule,
11206                            this](SourceLocation Loc, SourceRange Range,
11207                                  ODRFunctionDifference DiffType) {
11208         return Diag(Loc, diag::err_module_odr_violation_function)
11209                << FirstFunction << FirstModule.empty() << FirstModule << Range
11210                << DiffType;
11211       };
11212       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11213                                                SourceRange Range,
11214                                                ODRFunctionDifference DiffType) {
11215         return Diag(Loc, diag::note_module_odr_violation_function)
11216                << SecondModule << Range << DiffType;
11217       };
11218 
11219       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11220           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11221         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11222                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11223             << FirstFunction->getReturnType();
11224         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11225                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11226             << SecondFunction->getReturnType();
11227         Diagnosed = true;
11228         break;
11229       }
11230 
11231       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11232              "Merged functions with different number of parameters");
11233 
11234       auto ParamSize = FirstFunction->param_size();
11235       bool ParameterMismatch = false;
11236       for (unsigned I = 0; I < ParamSize; ++I) {
11237         auto *FirstParam = FirstFunction->getParamDecl(I);
11238         auto *SecondParam = SecondFunction->getParamDecl(I);
11239 
11240         assert(getContext().hasSameType(FirstParam->getType(),
11241                                       SecondParam->getType()) &&
11242                "Merged function has different parameter types.");
11243 
11244         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11245           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11246                        ParameterName)
11247               << I + 1 << FirstParam->getDeclName();
11248           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11249                       ParameterName)
11250               << I + 1 << SecondParam->getDeclName();
11251           ParameterMismatch = true;
11252           break;
11253         };
11254 
11255         QualType FirstParamType = FirstParam->getType();
11256         QualType SecondParamType = SecondParam->getType();
11257         if (FirstParamType != SecondParamType &&
11258             ComputeQualTypeODRHash(FirstParamType) !=
11259                 ComputeQualTypeODRHash(SecondParamType)) {
11260           if (const DecayedType *ParamDecayedType =
11261                   FirstParamType->getAs<DecayedType>()) {
11262             ODRDiagError(FirstParam->getLocation(),
11263                          FirstParam->getSourceRange(), ParameterType)
11264                 << (I + 1) << FirstParamType << true
11265                 << ParamDecayedType->getOriginalType();
11266           } else {
11267             ODRDiagError(FirstParam->getLocation(),
11268                          FirstParam->getSourceRange(), ParameterType)
11269                 << (I + 1) << FirstParamType << false;
11270           }
11271 
11272           if (const DecayedType *ParamDecayedType =
11273                   SecondParamType->getAs<DecayedType>()) {
11274             ODRDiagNote(SecondParam->getLocation(),
11275                         SecondParam->getSourceRange(), ParameterType)
11276                 << (I + 1) << SecondParamType << true
11277                 << ParamDecayedType->getOriginalType();
11278           } else {
11279             ODRDiagNote(SecondParam->getLocation(),
11280                         SecondParam->getSourceRange(), ParameterType)
11281                 << (I + 1) << SecondParamType << false;
11282           }
11283           ParameterMismatch = true;
11284           break;
11285         }
11286 
11287         const Expr *FirstInit = FirstParam->getInit();
11288         const Expr *SecondInit = SecondParam->getInit();
11289         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11290           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11291                        ParameterSingleDefaultArgument)
11292               << (I + 1) << (FirstInit == nullptr)
11293               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11294           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11295                       ParameterSingleDefaultArgument)
11296               << (I + 1) << (SecondInit == nullptr)
11297               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11298           ParameterMismatch = true;
11299           break;
11300         }
11301 
11302         if (FirstInit && SecondInit &&
11303             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11304           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11305                        ParameterDifferentDefaultArgument)
11306               << (I + 1) << FirstInit->getSourceRange();
11307           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11308                       ParameterDifferentDefaultArgument)
11309               << (I + 1) << SecondInit->getSourceRange();
11310           ParameterMismatch = true;
11311           break;
11312         }
11313 
11314         assert(ComputeSubDeclODRHash(FirstParam) ==
11315                    ComputeSubDeclODRHash(SecondParam) &&
11316                "Undiagnosed parameter difference.");
11317       }
11318 
11319       if (ParameterMismatch) {
11320         Diagnosed = true;
11321         break;
11322       }
11323 
11324       // If no error has been generated before now, assume the problem is in
11325       // the body and generate a message.
11326       ODRDiagError(FirstFunction->getLocation(),
11327                    FirstFunction->getSourceRange(), FunctionBody);
11328       ODRDiagNote(SecondFunction->getLocation(),
11329                   SecondFunction->getSourceRange(), FunctionBody);
11330       Diagnosed = true;
11331       break;
11332     }
11333     (void)Diagnosed;
11334     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11335   }
11336 
11337   // Issue ODR failures diagnostics for enums.
11338   for (auto &Merge : EnumOdrMergeFailures) {
11339     enum ODREnumDifference {
11340       SingleScopedEnum,
11341       EnumTagKeywordMismatch,
11342       SingleSpecifiedType,
11343       DifferentSpecifiedTypes,
11344       DifferentNumberEnumConstants,
11345       EnumConstantName,
11346       EnumConstantSingleInitilizer,
11347       EnumConstantDifferentInitilizer,
11348     };
11349 
11350     // If we've already pointed out a specific problem with this enum, don't
11351     // bother issuing a general "something's different" diagnostic.
11352     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11353       continue;
11354 
11355     EnumDecl *FirstEnum = Merge.first;
11356     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11357 
11358     using DeclHashes =
11359         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11360     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11361                               DeclHashes &Hashes, EnumDecl *Enum) {
11362       for (auto *D : Enum->decls()) {
11363         // Due to decl merging, the first EnumDecl is the parent of
11364         // Decls in both records.
11365         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11366           continue;
11367         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11368         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11369                             ComputeSubDeclODRHash(D));
11370       }
11371     };
11372     DeclHashes FirstHashes;
11373     PopulateHashes(FirstHashes, FirstEnum);
11374     bool Diagnosed = false;
11375     for (auto &SecondEnum : Merge.second) {
11376 
11377       if (FirstEnum == SecondEnum)
11378         continue;
11379 
11380       std::string SecondModule =
11381           getOwningModuleNameForDiagnostic(SecondEnum);
11382 
11383       auto ODRDiagError = [FirstEnum, &FirstModule,
11384                            this](SourceLocation Loc, SourceRange Range,
11385                                  ODREnumDifference DiffType) {
11386         return Diag(Loc, diag::err_module_odr_violation_enum)
11387                << FirstEnum << FirstModule.empty() << FirstModule << Range
11388                << DiffType;
11389       };
11390       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11391                                                SourceRange Range,
11392                                                ODREnumDifference DiffType) {
11393         return Diag(Loc, diag::note_module_odr_violation_enum)
11394                << SecondModule << Range << DiffType;
11395       };
11396 
11397       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11398         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11399                      SingleScopedEnum)
11400             << FirstEnum->isScoped();
11401         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11402                     SingleScopedEnum)
11403             << SecondEnum->isScoped();
11404         Diagnosed = true;
11405         continue;
11406       }
11407 
11408       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11409         if (FirstEnum->isScopedUsingClassTag() !=
11410             SecondEnum->isScopedUsingClassTag()) {
11411           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11412                        EnumTagKeywordMismatch)
11413               << FirstEnum->isScopedUsingClassTag();
11414           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11415                       EnumTagKeywordMismatch)
11416               << SecondEnum->isScopedUsingClassTag();
11417           Diagnosed = true;
11418           continue;
11419         }
11420       }
11421 
11422       QualType FirstUnderlyingType =
11423           FirstEnum->getIntegerTypeSourceInfo()
11424               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11425               : QualType();
11426       QualType SecondUnderlyingType =
11427           SecondEnum->getIntegerTypeSourceInfo()
11428               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11429               : QualType();
11430       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11431           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11432                        SingleSpecifiedType)
11433               << !FirstUnderlyingType.isNull();
11434           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11435                       SingleSpecifiedType)
11436               << !SecondUnderlyingType.isNull();
11437           Diagnosed = true;
11438           continue;
11439       }
11440 
11441       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11442         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11443             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11444           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11445                        DifferentSpecifiedTypes)
11446               << FirstUnderlyingType;
11447           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11448                       DifferentSpecifiedTypes)
11449               << SecondUnderlyingType;
11450           Diagnosed = true;
11451           continue;
11452         }
11453       }
11454 
11455       DeclHashes SecondHashes;
11456       PopulateHashes(SecondHashes, SecondEnum);
11457 
11458       if (FirstHashes.size() != SecondHashes.size()) {
11459         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11460                      DifferentNumberEnumConstants)
11461             << (int)FirstHashes.size();
11462         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11463                     DifferentNumberEnumConstants)
11464             << (int)SecondHashes.size();
11465         Diagnosed = true;
11466         continue;
11467       }
11468 
11469       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11470         if (FirstHashes[I].second == SecondHashes[I].second)
11471           continue;
11472         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11473         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11474 
11475         if (FirstEnumConstant->getDeclName() !=
11476             SecondEnumConstant->getDeclName()) {
11477 
11478           ODRDiagError(FirstEnumConstant->getLocation(),
11479                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11480               << I + 1 << FirstEnumConstant;
11481           ODRDiagNote(SecondEnumConstant->getLocation(),
11482                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11483               << I + 1 << SecondEnumConstant;
11484           Diagnosed = true;
11485           break;
11486         }
11487 
11488         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11489         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11490         if (!FirstInit && !SecondInit)
11491           continue;
11492 
11493         if (!FirstInit || !SecondInit) {
11494           ODRDiagError(FirstEnumConstant->getLocation(),
11495                        FirstEnumConstant->getSourceRange(),
11496                        EnumConstantSingleInitilizer)
11497               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11498           ODRDiagNote(SecondEnumConstant->getLocation(),
11499                       SecondEnumConstant->getSourceRange(),
11500                       EnumConstantSingleInitilizer)
11501               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11502           Diagnosed = true;
11503           break;
11504         }
11505 
11506         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11507           ODRDiagError(FirstEnumConstant->getLocation(),
11508                        FirstEnumConstant->getSourceRange(),
11509                        EnumConstantDifferentInitilizer)
11510               << I + 1 << FirstEnumConstant;
11511           ODRDiagNote(SecondEnumConstant->getLocation(),
11512                       SecondEnumConstant->getSourceRange(),
11513                       EnumConstantDifferentInitilizer)
11514               << I + 1 << SecondEnumConstant;
11515           Diagnosed = true;
11516           break;
11517         }
11518       }
11519     }
11520 
11521     (void)Diagnosed;
11522     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11523   }
11524 }
11525 
11526 void ASTReader::StartedDeserializing() {
11527   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11528     ReadTimer->startTimer();
11529 }
11530 
11531 void ASTReader::FinishedDeserializing() {
11532   assert(NumCurrentElementsDeserializing &&
11533          "FinishedDeserializing not paired with StartedDeserializing");
11534   if (NumCurrentElementsDeserializing == 1) {
11535     // We decrease NumCurrentElementsDeserializing only after pending actions
11536     // are finished, to avoid recursively re-calling finishPendingActions().
11537     finishPendingActions();
11538   }
11539   --NumCurrentElementsDeserializing;
11540 
11541   if (NumCurrentElementsDeserializing == 0) {
11542     // Propagate exception specification and deduced type updates along
11543     // redeclaration chains.
11544     //
11545     // We do this now rather than in finishPendingActions because we want to
11546     // be able to walk the complete redeclaration chains of the updated decls.
11547     while (!PendingExceptionSpecUpdates.empty() ||
11548            !PendingDeducedTypeUpdates.empty()) {
11549       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11550       PendingExceptionSpecUpdates.clear();
11551       for (auto Update : ESUpdates) {
11552         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11553         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11554         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11555         if (auto *Listener = getContext().getASTMutationListener())
11556           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11557         for (auto *Redecl : Update.second->redecls())
11558           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11559       }
11560 
11561       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11562       PendingDeducedTypeUpdates.clear();
11563       for (auto Update : DTUpdates) {
11564         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11565         // FIXME: If the return type is already deduced, check that it matches.
11566         getContext().adjustDeducedFunctionResultType(Update.first,
11567                                                      Update.second);
11568       }
11569     }
11570 
11571     if (ReadTimer)
11572       ReadTimer->stopTimer();
11573 
11574     diagnoseOdrViolations();
11575 
11576     // We are not in recursive loading, so it's safe to pass the "interesting"
11577     // decls to the consumer.
11578     if (Consumer)
11579       PassInterestingDeclsToConsumer();
11580   }
11581 }
11582 
11583 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11584   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11585     // Remove any fake results before adding any real ones.
11586     auto It = PendingFakeLookupResults.find(II);
11587     if (It != PendingFakeLookupResults.end()) {
11588       for (auto *ND : It->second)
11589         SemaObj->IdResolver.RemoveDecl(ND);
11590       // FIXME: this works around module+PCH performance issue.
11591       // Rather than erase the result from the map, which is O(n), just clear
11592       // the vector of NamedDecls.
11593       It->second.clear();
11594     }
11595   }
11596 
11597   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11598     SemaObj->TUScope->AddDecl(D);
11599   } else if (SemaObj->TUScope) {
11600     // Adding the decl to IdResolver may have failed because it was already in
11601     // (even though it was not added in scope). If it is already in, make sure
11602     // it gets in the scope as well.
11603     if (std::find(SemaObj->IdResolver.begin(Name),
11604                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11605       SemaObj->TUScope->AddDecl(D);
11606   }
11607 }
11608 
11609 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
11610                      const PCHContainerReader &PCHContainerRdr,
11611                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11612                      StringRef isysroot, bool DisableValidation,
11613                      bool AllowASTWithCompilerErrors,
11614                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11615                      bool UseGlobalIndex,
11616                      std::unique_ptr<llvm::Timer> ReadTimer)
11617     : Listener(DisableValidation
11618                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11619                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11620       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11621       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11622       ContextObj(Context),
11623       ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr,
11624                 PP.getHeaderSearchInfo()),
11625       PCMCache(PP.getPCMCache()), DummyIdResolver(PP),
11626       ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11627       DisableValidation(DisableValidation),
11628       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11629       AllowConfigurationMismatch(AllowConfigurationMismatch),
11630       ValidateSystemInputs(ValidateSystemInputs),
11631       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11632   SourceMgr.setExternalSLocEntrySource(this);
11633 
11634   for (const auto &Ext : Extensions) {
11635     auto BlockName = Ext->getExtensionMetadata().BlockName;
11636     auto Known = ModuleFileExtensions.find(BlockName);
11637     if (Known != ModuleFileExtensions.end()) {
11638       Diags.Report(diag::warn_duplicate_module_file_extension)
11639         << BlockName;
11640       continue;
11641     }
11642 
11643     ModuleFileExtensions.insert({BlockName, Ext});
11644   }
11645 }
11646 
11647 ASTReader::~ASTReader() {
11648   if (OwnsDeserializationListener)
11649     delete DeserializationListener;
11650 }
11651 
11652 IdentifierResolver &ASTReader::getIdResolver() {
11653   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11654 }
11655 
11656 unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11657                                      unsigned AbbrevID) {
11658   Idx = 0;
11659   Record.clear();
11660   return Cursor.readRecord(AbbrevID, Record);
11661 }
11662 //===----------------------------------------------------------------------===//
11663 //// OMPClauseReader implementation
11664 ////===----------------------------------------------------------------------===//
11665 
11666 OMPClause *OMPClauseReader::readClause() {
11667   OMPClause *C;
11668   switch (Record.readInt()) {
11669   case OMPC_if:
11670     C = new (Context) OMPIfClause();
11671     break;
11672   case OMPC_final:
11673     C = new (Context) OMPFinalClause();
11674     break;
11675   case OMPC_num_threads:
11676     C = new (Context) OMPNumThreadsClause();
11677     break;
11678   case OMPC_safelen:
11679     C = new (Context) OMPSafelenClause();
11680     break;
11681   case OMPC_simdlen:
11682     C = new (Context) OMPSimdlenClause();
11683     break;
11684   case OMPC_collapse:
11685     C = new (Context) OMPCollapseClause();
11686     break;
11687   case OMPC_default:
11688     C = new (Context) OMPDefaultClause();
11689     break;
11690   case OMPC_proc_bind:
11691     C = new (Context) OMPProcBindClause();
11692     break;
11693   case OMPC_schedule:
11694     C = new (Context) OMPScheduleClause();
11695     break;
11696   case OMPC_ordered:
11697     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11698     break;
11699   case OMPC_nowait:
11700     C = new (Context) OMPNowaitClause();
11701     break;
11702   case OMPC_untied:
11703     C = new (Context) OMPUntiedClause();
11704     break;
11705   case OMPC_mergeable:
11706     C = new (Context) OMPMergeableClause();
11707     break;
11708   case OMPC_read:
11709     C = new (Context) OMPReadClause();
11710     break;
11711   case OMPC_write:
11712     C = new (Context) OMPWriteClause();
11713     break;
11714   case OMPC_update:
11715     C = new (Context) OMPUpdateClause();
11716     break;
11717   case OMPC_capture:
11718     C = new (Context) OMPCaptureClause();
11719     break;
11720   case OMPC_seq_cst:
11721     C = new (Context) OMPSeqCstClause();
11722     break;
11723   case OMPC_threads:
11724     C = new (Context) OMPThreadsClause();
11725     break;
11726   case OMPC_simd:
11727     C = new (Context) OMPSIMDClause();
11728     break;
11729   case OMPC_nogroup:
11730     C = new (Context) OMPNogroupClause();
11731     break;
11732   case OMPC_unified_address:
11733     C = new (Context) OMPUnifiedAddressClause();
11734     break;
11735   case OMPC_unified_shared_memory:
11736     C = new (Context) OMPUnifiedSharedMemoryClause();
11737     break;
11738   case OMPC_reverse_offload:
11739     C = new (Context) OMPReverseOffloadClause();
11740     break;
11741   case OMPC_dynamic_allocators:
11742     C = new (Context) OMPDynamicAllocatorsClause();
11743     break;
11744   case OMPC_atomic_default_mem_order:
11745     C = new (Context) OMPAtomicDefaultMemOrderClause();
11746     break;
11747  case OMPC_private:
11748     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11749     break;
11750   case OMPC_firstprivate:
11751     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11752     break;
11753   case OMPC_lastprivate:
11754     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11755     break;
11756   case OMPC_shared:
11757     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11758     break;
11759   case OMPC_reduction:
11760     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11761     break;
11762   case OMPC_task_reduction:
11763     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11764     break;
11765   case OMPC_in_reduction:
11766     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11767     break;
11768   case OMPC_linear:
11769     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11770     break;
11771   case OMPC_aligned:
11772     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11773     break;
11774   case OMPC_copyin:
11775     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11776     break;
11777   case OMPC_copyprivate:
11778     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11779     break;
11780   case OMPC_flush:
11781     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11782     break;
11783   case OMPC_depend: {
11784     unsigned NumVars = Record.readInt();
11785     unsigned NumLoops = Record.readInt();
11786     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11787     break;
11788   }
11789   case OMPC_device:
11790     C = new (Context) OMPDeviceClause();
11791     break;
11792   case OMPC_map: {
11793     OMPMappableExprListSizeTy Sizes;
11794     Sizes.NumVars = Record.readInt();
11795     Sizes.NumUniqueDeclarations = Record.readInt();
11796     Sizes.NumComponentLists = Record.readInt();
11797     Sizes.NumComponents = Record.readInt();
11798     C = OMPMapClause::CreateEmpty(Context, Sizes);
11799     break;
11800   }
11801   case OMPC_num_teams:
11802     C = new (Context) OMPNumTeamsClause();
11803     break;
11804   case OMPC_thread_limit:
11805     C = new (Context) OMPThreadLimitClause();
11806     break;
11807   case OMPC_priority:
11808     C = new (Context) OMPPriorityClause();
11809     break;
11810   case OMPC_grainsize:
11811     C = new (Context) OMPGrainsizeClause();
11812     break;
11813   case OMPC_num_tasks:
11814     C = new (Context) OMPNumTasksClause();
11815     break;
11816   case OMPC_hint:
11817     C = new (Context) OMPHintClause();
11818     break;
11819   case OMPC_dist_schedule:
11820     C = new (Context) OMPDistScheduleClause();
11821     break;
11822   case OMPC_defaultmap:
11823     C = new (Context) OMPDefaultmapClause();
11824     break;
11825   case OMPC_to: {
11826     OMPMappableExprListSizeTy Sizes;
11827     Sizes.NumVars = Record.readInt();
11828     Sizes.NumUniqueDeclarations = Record.readInt();
11829     Sizes.NumComponentLists = Record.readInt();
11830     Sizes.NumComponents = Record.readInt();
11831     C = OMPToClause::CreateEmpty(Context, Sizes);
11832     break;
11833   }
11834   case OMPC_from: {
11835     OMPMappableExprListSizeTy Sizes;
11836     Sizes.NumVars = Record.readInt();
11837     Sizes.NumUniqueDeclarations = Record.readInt();
11838     Sizes.NumComponentLists = Record.readInt();
11839     Sizes.NumComponents = Record.readInt();
11840     C = OMPFromClause::CreateEmpty(Context, Sizes);
11841     break;
11842   }
11843   case OMPC_use_device_ptr: {
11844     OMPMappableExprListSizeTy Sizes;
11845     Sizes.NumVars = Record.readInt();
11846     Sizes.NumUniqueDeclarations = Record.readInt();
11847     Sizes.NumComponentLists = Record.readInt();
11848     Sizes.NumComponents = Record.readInt();
11849     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11850     break;
11851   }
11852   case OMPC_is_device_ptr: {
11853     OMPMappableExprListSizeTy Sizes;
11854     Sizes.NumVars = Record.readInt();
11855     Sizes.NumUniqueDeclarations = Record.readInt();
11856     Sizes.NumComponentLists = Record.readInt();
11857     Sizes.NumComponents = Record.readInt();
11858     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11859     break;
11860   }
11861   }
11862   Visit(C);
11863   C->setLocStart(Record.readSourceLocation());
11864   C->setLocEnd(Record.readSourceLocation());
11865 
11866   return C;
11867 }
11868 
11869 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11870   C->setPreInitStmt(Record.readSubStmt(),
11871                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11872 }
11873 
11874 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11875   VisitOMPClauseWithPreInit(C);
11876   C->setPostUpdateExpr(Record.readSubExpr());
11877 }
11878 
11879 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11880   VisitOMPClauseWithPreInit(C);
11881   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11882   C->setNameModifierLoc(Record.readSourceLocation());
11883   C->setColonLoc(Record.readSourceLocation());
11884   C->setCondition(Record.readSubExpr());
11885   C->setLParenLoc(Record.readSourceLocation());
11886 }
11887 
11888 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11889   C->setCondition(Record.readSubExpr());
11890   C->setLParenLoc(Record.readSourceLocation());
11891 }
11892 
11893 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11894   VisitOMPClauseWithPreInit(C);
11895   C->setNumThreads(Record.readSubExpr());
11896   C->setLParenLoc(Record.readSourceLocation());
11897 }
11898 
11899 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11900   C->setSafelen(Record.readSubExpr());
11901   C->setLParenLoc(Record.readSourceLocation());
11902 }
11903 
11904 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11905   C->setSimdlen(Record.readSubExpr());
11906   C->setLParenLoc(Record.readSourceLocation());
11907 }
11908 
11909 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11910   C->setNumForLoops(Record.readSubExpr());
11911   C->setLParenLoc(Record.readSourceLocation());
11912 }
11913 
11914 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11915   C->setDefaultKind(
11916        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
11917   C->setLParenLoc(Record.readSourceLocation());
11918   C->setDefaultKindKwLoc(Record.readSourceLocation());
11919 }
11920 
11921 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11922   C->setProcBindKind(
11923        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
11924   C->setLParenLoc(Record.readSourceLocation());
11925   C->setProcBindKindKwLoc(Record.readSourceLocation());
11926 }
11927 
11928 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11929   VisitOMPClauseWithPreInit(C);
11930   C->setScheduleKind(
11931        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11932   C->setFirstScheduleModifier(
11933       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11934   C->setSecondScheduleModifier(
11935       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11936   C->setChunkSize(Record.readSubExpr());
11937   C->setLParenLoc(Record.readSourceLocation());
11938   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11939   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11940   C->setScheduleKindLoc(Record.readSourceLocation());
11941   C->setCommaLoc(Record.readSourceLocation());
11942 }
11943 
11944 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11945   C->setNumForLoops(Record.readSubExpr());
11946   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11947     C->setLoopNumIterations(I, Record.readSubExpr());
11948   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11949     C->setLoopCounter(I, Record.readSubExpr());
11950   C->setLParenLoc(Record.readSourceLocation());
11951 }
11952 
11953 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11954 
11955 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11956 
11957 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11958 
11959 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11960 
11961 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11962 
11963 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
11964 
11965 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11966 
11967 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11968 
11969 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11970 
11971 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11972 
11973 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11974 
11975 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11976 
11977 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11978     OMPUnifiedSharedMemoryClause *) {}
11979 
11980 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11981 
11982 void
11983 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11984 }
11985 
11986 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11987     OMPAtomicDefaultMemOrderClause *C) {
11988   C->setAtomicDefaultMemOrderKind(
11989       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11990   C->setLParenLoc(Record.readSourceLocation());
11991   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11992 }
11993 
11994 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11995   C->setLParenLoc(Record.readSourceLocation());
11996   unsigned NumVars = C->varlist_size();
11997   SmallVector<Expr *, 16> Vars;
11998   Vars.reserve(NumVars);
11999   for (unsigned i = 0; i != NumVars; ++i)
12000     Vars.push_back(Record.readSubExpr());
12001   C->setVarRefs(Vars);
12002   Vars.clear();
12003   for (unsigned i = 0; i != NumVars; ++i)
12004     Vars.push_back(Record.readSubExpr());
12005   C->setPrivateCopies(Vars);
12006 }
12007 
12008 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12009   VisitOMPClauseWithPreInit(C);
12010   C->setLParenLoc(Record.readSourceLocation());
12011   unsigned NumVars = C->varlist_size();
12012   SmallVector<Expr *, 16> Vars;
12013   Vars.reserve(NumVars);
12014   for (unsigned i = 0; i != NumVars; ++i)
12015     Vars.push_back(Record.readSubExpr());
12016   C->setVarRefs(Vars);
12017   Vars.clear();
12018   for (unsigned i = 0; i != NumVars; ++i)
12019     Vars.push_back(Record.readSubExpr());
12020   C->setPrivateCopies(Vars);
12021   Vars.clear();
12022   for (unsigned i = 0; i != NumVars; ++i)
12023     Vars.push_back(Record.readSubExpr());
12024   C->setInits(Vars);
12025 }
12026 
12027 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12028   VisitOMPClauseWithPostUpdate(C);
12029   C->setLParenLoc(Record.readSourceLocation());
12030   unsigned NumVars = C->varlist_size();
12031   SmallVector<Expr *, 16> Vars;
12032   Vars.reserve(NumVars);
12033   for (unsigned i = 0; i != NumVars; ++i)
12034     Vars.push_back(Record.readSubExpr());
12035   C->setVarRefs(Vars);
12036   Vars.clear();
12037   for (unsigned i = 0; i != NumVars; ++i)
12038     Vars.push_back(Record.readSubExpr());
12039   C->setPrivateCopies(Vars);
12040   Vars.clear();
12041   for (unsigned i = 0; i != NumVars; ++i)
12042     Vars.push_back(Record.readSubExpr());
12043   C->setSourceExprs(Vars);
12044   Vars.clear();
12045   for (unsigned i = 0; i != NumVars; ++i)
12046     Vars.push_back(Record.readSubExpr());
12047   C->setDestinationExprs(Vars);
12048   Vars.clear();
12049   for (unsigned i = 0; i != NumVars; ++i)
12050     Vars.push_back(Record.readSubExpr());
12051   C->setAssignmentOps(Vars);
12052 }
12053 
12054 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12055   C->setLParenLoc(Record.readSourceLocation());
12056   unsigned NumVars = C->varlist_size();
12057   SmallVector<Expr *, 16> Vars;
12058   Vars.reserve(NumVars);
12059   for (unsigned i = 0; i != NumVars; ++i)
12060     Vars.push_back(Record.readSubExpr());
12061   C->setVarRefs(Vars);
12062 }
12063 
12064 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12065   VisitOMPClauseWithPostUpdate(C);
12066   C->setLParenLoc(Record.readSourceLocation());
12067   C->setColonLoc(Record.readSourceLocation());
12068   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12069   DeclarationNameInfo DNI;
12070   Record.readDeclarationNameInfo(DNI);
12071   C->setQualifierLoc(NNSL);
12072   C->setNameInfo(DNI);
12073 
12074   unsigned NumVars = C->varlist_size();
12075   SmallVector<Expr *, 16> Vars;
12076   Vars.reserve(NumVars);
12077   for (unsigned i = 0; i != NumVars; ++i)
12078     Vars.push_back(Record.readSubExpr());
12079   C->setVarRefs(Vars);
12080   Vars.clear();
12081   for (unsigned i = 0; i != NumVars; ++i)
12082     Vars.push_back(Record.readSubExpr());
12083   C->setPrivates(Vars);
12084   Vars.clear();
12085   for (unsigned i = 0; i != NumVars; ++i)
12086     Vars.push_back(Record.readSubExpr());
12087   C->setLHSExprs(Vars);
12088   Vars.clear();
12089   for (unsigned i = 0; i != NumVars; ++i)
12090     Vars.push_back(Record.readSubExpr());
12091   C->setRHSExprs(Vars);
12092   Vars.clear();
12093   for (unsigned i = 0; i != NumVars; ++i)
12094     Vars.push_back(Record.readSubExpr());
12095   C->setReductionOps(Vars);
12096 }
12097 
12098 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12099   VisitOMPClauseWithPostUpdate(C);
12100   C->setLParenLoc(Record.readSourceLocation());
12101   C->setColonLoc(Record.readSourceLocation());
12102   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12103   DeclarationNameInfo DNI;
12104   Record.readDeclarationNameInfo(DNI);
12105   C->setQualifierLoc(NNSL);
12106   C->setNameInfo(DNI);
12107 
12108   unsigned NumVars = C->varlist_size();
12109   SmallVector<Expr *, 16> Vars;
12110   Vars.reserve(NumVars);
12111   for (unsigned I = 0; I != NumVars; ++I)
12112     Vars.push_back(Record.readSubExpr());
12113   C->setVarRefs(Vars);
12114   Vars.clear();
12115   for (unsigned I = 0; I != NumVars; ++I)
12116     Vars.push_back(Record.readSubExpr());
12117   C->setPrivates(Vars);
12118   Vars.clear();
12119   for (unsigned I = 0; I != NumVars; ++I)
12120     Vars.push_back(Record.readSubExpr());
12121   C->setLHSExprs(Vars);
12122   Vars.clear();
12123   for (unsigned I = 0; I != NumVars; ++I)
12124     Vars.push_back(Record.readSubExpr());
12125   C->setRHSExprs(Vars);
12126   Vars.clear();
12127   for (unsigned I = 0; I != NumVars; ++I)
12128     Vars.push_back(Record.readSubExpr());
12129   C->setReductionOps(Vars);
12130 }
12131 
12132 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12133   VisitOMPClauseWithPostUpdate(C);
12134   C->setLParenLoc(Record.readSourceLocation());
12135   C->setColonLoc(Record.readSourceLocation());
12136   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12137   DeclarationNameInfo DNI;
12138   Record.readDeclarationNameInfo(DNI);
12139   C->setQualifierLoc(NNSL);
12140   C->setNameInfo(DNI);
12141 
12142   unsigned NumVars = C->varlist_size();
12143   SmallVector<Expr *, 16> Vars;
12144   Vars.reserve(NumVars);
12145   for (unsigned I = 0; I != NumVars; ++I)
12146     Vars.push_back(Record.readSubExpr());
12147   C->setVarRefs(Vars);
12148   Vars.clear();
12149   for (unsigned I = 0; I != NumVars; ++I)
12150     Vars.push_back(Record.readSubExpr());
12151   C->setPrivates(Vars);
12152   Vars.clear();
12153   for (unsigned I = 0; I != NumVars; ++I)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setLHSExprs(Vars);
12156   Vars.clear();
12157   for (unsigned I = 0; I != NumVars; ++I)
12158     Vars.push_back(Record.readSubExpr());
12159   C->setRHSExprs(Vars);
12160   Vars.clear();
12161   for (unsigned I = 0; I != NumVars; ++I)
12162     Vars.push_back(Record.readSubExpr());
12163   C->setReductionOps(Vars);
12164   Vars.clear();
12165   for (unsigned I = 0; I != NumVars; ++I)
12166     Vars.push_back(Record.readSubExpr());
12167   C->setTaskgroupDescriptors(Vars);
12168 }
12169 
12170 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12171   VisitOMPClauseWithPostUpdate(C);
12172   C->setLParenLoc(Record.readSourceLocation());
12173   C->setColonLoc(Record.readSourceLocation());
12174   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12175   C->setModifierLoc(Record.readSourceLocation());
12176   unsigned NumVars = C->varlist_size();
12177   SmallVector<Expr *, 16> Vars;
12178   Vars.reserve(NumVars);
12179   for (unsigned i = 0; i != NumVars; ++i)
12180     Vars.push_back(Record.readSubExpr());
12181   C->setVarRefs(Vars);
12182   Vars.clear();
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setPrivates(Vars);
12186   Vars.clear();
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setInits(Vars);
12190   Vars.clear();
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setUpdates(Vars);
12194   Vars.clear();
12195   for (unsigned i = 0; i != NumVars; ++i)
12196     Vars.push_back(Record.readSubExpr());
12197   C->setFinals(Vars);
12198   C->setStep(Record.readSubExpr());
12199   C->setCalcStep(Record.readSubExpr());
12200 }
12201 
12202 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12203   C->setLParenLoc(Record.readSourceLocation());
12204   C->setColonLoc(Record.readSourceLocation());
12205   unsigned NumVars = C->varlist_size();
12206   SmallVector<Expr *, 16> Vars;
12207   Vars.reserve(NumVars);
12208   for (unsigned i = 0; i != NumVars; ++i)
12209     Vars.push_back(Record.readSubExpr());
12210   C->setVarRefs(Vars);
12211   C->setAlignment(Record.readSubExpr());
12212 }
12213 
12214 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12215   C->setLParenLoc(Record.readSourceLocation());
12216   unsigned NumVars = C->varlist_size();
12217   SmallVector<Expr *, 16> Exprs;
12218   Exprs.reserve(NumVars);
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Exprs.push_back(Record.readSubExpr());
12221   C->setVarRefs(Exprs);
12222   Exprs.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Exprs.push_back(Record.readSubExpr());
12225   C->setSourceExprs(Exprs);
12226   Exprs.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Exprs.push_back(Record.readSubExpr());
12229   C->setDestinationExprs(Exprs);
12230   Exprs.clear();
12231   for (unsigned i = 0; i != NumVars; ++i)
12232     Exprs.push_back(Record.readSubExpr());
12233   C->setAssignmentOps(Exprs);
12234 }
12235 
12236 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12237   C->setLParenLoc(Record.readSourceLocation());
12238   unsigned NumVars = C->varlist_size();
12239   SmallVector<Expr *, 16> Exprs;
12240   Exprs.reserve(NumVars);
12241   for (unsigned i = 0; i != NumVars; ++i)
12242     Exprs.push_back(Record.readSubExpr());
12243   C->setVarRefs(Exprs);
12244   Exprs.clear();
12245   for (unsigned i = 0; i != NumVars; ++i)
12246     Exprs.push_back(Record.readSubExpr());
12247   C->setSourceExprs(Exprs);
12248   Exprs.clear();
12249   for (unsigned i = 0; i != NumVars; ++i)
12250     Exprs.push_back(Record.readSubExpr());
12251   C->setDestinationExprs(Exprs);
12252   Exprs.clear();
12253   for (unsigned i = 0; i != NumVars; ++i)
12254     Exprs.push_back(Record.readSubExpr());
12255   C->setAssignmentOps(Exprs);
12256 }
12257 
12258 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12259   C->setLParenLoc(Record.readSourceLocation());
12260   unsigned NumVars = C->varlist_size();
12261   SmallVector<Expr *, 16> Vars;
12262   Vars.reserve(NumVars);
12263   for (unsigned i = 0; i != NumVars; ++i)
12264     Vars.push_back(Record.readSubExpr());
12265   C->setVarRefs(Vars);
12266 }
12267 
12268 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12269   C->setLParenLoc(Record.readSourceLocation());
12270   C->setDependencyKind(
12271       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12272   C->setDependencyLoc(Record.readSourceLocation());
12273   C->setColonLoc(Record.readSourceLocation());
12274   unsigned NumVars = C->varlist_size();
12275   SmallVector<Expr *, 16> Vars;
12276   Vars.reserve(NumVars);
12277   for (unsigned I = 0; I != NumVars; ++I)
12278     Vars.push_back(Record.readSubExpr());
12279   C->setVarRefs(Vars);
12280   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12281     C->setLoopData(I, Record.readSubExpr());
12282 }
12283 
12284 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12285   VisitOMPClauseWithPreInit(C);
12286   C->setDevice(Record.readSubExpr());
12287   C->setLParenLoc(Record.readSourceLocation());
12288 }
12289 
12290 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12291   C->setLParenLoc(Record.readSourceLocation());
12292   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12293     C->setMapTypeModifier(
12294         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12295     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12296   }
12297   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12298   DeclarationNameInfo DNI;
12299   Record.readDeclarationNameInfo(DNI);
12300   C->setMapperIdInfo(DNI);
12301   C->setMapType(
12302      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12303   C->setMapLoc(Record.readSourceLocation());
12304   C->setColonLoc(Record.readSourceLocation());
12305   auto NumVars = C->varlist_size();
12306   auto UniqueDecls = C->getUniqueDeclarationsNum();
12307   auto TotalLists = C->getTotalComponentListNum();
12308   auto TotalComponents = C->getTotalComponentsNum();
12309 
12310   SmallVector<Expr *, 16> Vars;
12311   Vars.reserve(NumVars);
12312   for (unsigned i = 0; i != NumVars; ++i)
12313     Vars.push_back(Record.readExpr());
12314   C->setVarRefs(Vars);
12315 
12316   SmallVector<Expr *, 16> UDMappers;
12317   UDMappers.reserve(NumVars);
12318   for (unsigned I = 0; I < NumVars; ++I)
12319     UDMappers.push_back(Record.readExpr());
12320   C->setUDMapperRefs(UDMappers);
12321 
12322   SmallVector<ValueDecl *, 16> Decls;
12323   Decls.reserve(UniqueDecls);
12324   for (unsigned i = 0; i < UniqueDecls; ++i)
12325     Decls.push_back(Record.readDeclAs<ValueDecl>());
12326   C->setUniqueDecls(Decls);
12327 
12328   SmallVector<unsigned, 16> ListsPerDecl;
12329   ListsPerDecl.reserve(UniqueDecls);
12330   for (unsigned i = 0; i < UniqueDecls; ++i)
12331     ListsPerDecl.push_back(Record.readInt());
12332   C->setDeclNumLists(ListsPerDecl);
12333 
12334   SmallVector<unsigned, 32> ListSizes;
12335   ListSizes.reserve(TotalLists);
12336   for (unsigned i = 0; i < TotalLists; ++i)
12337     ListSizes.push_back(Record.readInt());
12338   C->setComponentListSizes(ListSizes);
12339 
12340   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12341   Components.reserve(TotalComponents);
12342   for (unsigned i = 0; i < TotalComponents; ++i) {
12343     Expr *AssociatedExpr = Record.readExpr();
12344     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12345     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12346         AssociatedExpr, AssociatedDecl));
12347   }
12348   C->setComponents(Components, ListSizes);
12349 }
12350 
12351 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12352   VisitOMPClauseWithPreInit(C);
12353   C->setNumTeams(Record.readSubExpr());
12354   C->setLParenLoc(Record.readSourceLocation());
12355 }
12356 
12357 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12358   VisitOMPClauseWithPreInit(C);
12359   C->setThreadLimit(Record.readSubExpr());
12360   C->setLParenLoc(Record.readSourceLocation());
12361 }
12362 
12363 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12364   C->setPriority(Record.readSubExpr());
12365   C->setLParenLoc(Record.readSourceLocation());
12366 }
12367 
12368 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12369   C->setGrainsize(Record.readSubExpr());
12370   C->setLParenLoc(Record.readSourceLocation());
12371 }
12372 
12373 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12374   C->setNumTasks(Record.readSubExpr());
12375   C->setLParenLoc(Record.readSourceLocation());
12376 }
12377 
12378 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12379   C->setHint(Record.readSubExpr());
12380   C->setLParenLoc(Record.readSourceLocation());
12381 }
12382 
12383 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12384   VisitOMPClauseWithPreInit(C);
12385   C->setDistScheduleKind(
12386       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12387   C->setChunkSize(Record.readSubExpr());
12388   C->setLParenLoc(Record.readSourceLocation());
12389   C->setDistScheduleKindLoc(Record.readSourceLocation());
12390   C->setCommaLoc(Record.readSourceLocation());
12391 }
12392 
12393 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12394   C->setDefaultmapKind(
12395        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12396   C->setDefaultmapModifier(
12397       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12398   C->setLParenLoc(Record.readSourceLocation());
12399   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12400   C->setDefaultmapKindLoc(Record.readSourceLocation());
12401 }
12402 
12403 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12404   C->setLParenLoc(Record.readSourceLocation());
12405   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12406   DeclarationNameInfo DNI;
12407   Record.readDeclarationNameInfo(DNI);
12408   C->setMapperIdInfo(DNI);
12409   auto NumVars = C->varlist_size();
12410   auto UniqueDecls = C->getUniqueDeclarationsNum();
12411   auto TotalLists = C->getTotalComponentListNum();
12412   auto TotalComponents = C->getTotalComponentsNum();
12413 
12414   SmallVector<Expr *, 16> Vars;
12415   Vars.reserve(NumVars);
12416   for (unsigned i = 0; i != NumVars; ++i)
12417     Vars.push_back(Record.readSubExpr());
12418   C->setVarRefs(Vars);
12419 
12420   SmallVector<Expr *, 16> UDMappers;
12421   UDMappers.reserve(NumVars);
12422   for (unsigned I = 0; I < NumVars; ++I)
12423     UDMappers.push_back(Record.readSubExpr());
12424   C->setUDMapperRefs(UDMappers);
12425 
12426   SmallVector<ValueDecl *, 16> Decls;
12427   Decls.reserve(UniqueDecls);
12428   for (unsigned i = 0; i < UniqueDecls; ++i)
12429     Decls.push_back(Record.readDeclAs<ValueDecl>());
12430   C->setUniqueDecls(Decls);
12431 
12432   SmallVector<unsigned, 16> ListsPerDecl;
12433   ListsPerDecl.reserve(UniqueDecls);
12434   for (unsigned i = 0; i < UniqueDecls; ++i)
12435     ListsPerDecl.push_back(Record.readInt());
12436   C->setDeclNumLists(ListsPerDecl);
12437 
12438   SmallVector<unsigned, 32> ListSizes;
12439   ListSizes.reserve(TotalLists);
12440   for (unsigned i = 0; i < TotalLists; ++i)
12441     ListSizes.push_back(Record.readInt());
12442   C->setComponentListSizes(ListSizes);
12443 
12444   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12445   Components.reserve(TotalComponents);
12446   for (unsigned i = 0; i < TotalComponents; ++i) {
12447     Expr *AssociatedExpr = Record.readSubExpr();
12448     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12449     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12450         AssociatedExpr, AssociatedDecl));
12451   }
12452   C->setComponents(Components, ListSizes);
12453 }
12454 
12455 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12456   C->setLParenLoc(Record.readSourceLocation());
12457   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12458   DeclarationNameInfo DNI;
12459   Record.readDeclarationNameInfo(DNI);
12460   C->setMapperIdInfo(DNI);
12461   auto NumVars = C->varlist_size();
12462   auto UniqueDecls = C->getUniqueDeclarationsNum();
12463   auto TotalLists = C->getTotalComponentListNum();
12464   auto TotalComponents = C->getTotalComponentsNum();
12465 
12466   SmallVector<Expr *, 16> Vars;
12467   Vars.reserve(NumVars);
12468   for (unsigned i = 0; i != NumVars; ++i)
12469     Vars.push_back(Record.readSubExpr());
12470   C->setVarRefs(Vars);
12471 
12472   SmallVector<Expr *, 16> UDMappers;
12473   UDMappers.reserve(NumVars);
12474   for (unsigned I = 0; I < NumVars; ++I)
12475     UDMappers.push_back(Record.readSubExpr());
12476   C->setUDMapperRefs(UDMappers);
12477 
12478   SmallVector<ValueDecl *, 16> Decls;
12479   Decls.reserve(UniqueDecls);
12480   for (unsigned i = 0; i < UniqueDecls; ++i)
12481     Decls.push_back(Record.readDeclAs<ValueDecl>());
12482   C->setUniqueDecls(Decls);
12483 
12484   SmallVector<unsigned, 16> ListsPerDecl;
12485   ListsPerDecl.reserve(UniqueDecls);
12486   for (unsigned i = 0; i < UniqueDecls; ++i)
12487     ListsPerDecl.push_back(Record.readInt());
12488   C->setDeclNumLists(ListsPerDecl);
12489 
12490   SmallVector<unsigned, 32> ListSizes;
12491   ListSizes.reserve(TotalLists);
12492   for (unsigned i = 0; i < TotalLists; ++i)
12493     ListSizes.push_back(Record.readInt());
12494   C->setComponentListSizes(ListSizes);
12495 
12496   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12497   Components.reserve(TotalComponents);
12498   for (unsigned i = 0; i < TotalComponents; ++i) {
12499     Expr *AssociatedExpr = Record.readSubExpr();
12500     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12501     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12502         AssociatedExpr, AssociatedDecl));
12503   }
12504   C->setComponents(Components, ListSizes);
12505 }
12506 
12507 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12508   C->setLParenLoc(Record.readSourceLocation());
12509   auto NumVars = C->varlist_size();
12510   auto UniqueDecls = C->getUniqueDeclarationsNum();
12511   auto TotalLists = C->getTotalComponentListNum();
12512   auto TotalComponents = C->getTotalComponentsNum();
12513 
12514   SmallVector<Expr *, 16> Vars;
12515   Vars.reserve(NumVars);
12516   for (unsigned i = 0; i != NumVars; ++i)
12517     Vars.push_back(Record.readSubExpr());
12518   C->setVarRefs(Vars);
12519   Vars.clear();
12520   for (unsigned i = 0; i != NumVars; ++i)
12521     Vars.push_back(Record.readSubExpr());
12522   C->setPrivateCopies(Vars);
12523   Vars.clear();
12524   for (unsigned i = 0; i != NumVars; ++i)
12525     Vars.push_back(Record.readSubExpr());
12526   C->setInits(Vars);
12527 
12528   SmallVector<ValueDecl *, 16> Decls;
12529   Decls.reserve(UniqueDecls);
12530   for (unsigned i = 0; i < UniqueDecls; ++i)
12531     Decls.push_back(Record.readDeclAs<ValueDecl>());
12532   C->setUniqueDecls(Decls);
12533 
12534   SmallVector<unsigned, 16> ListsPerDecl;
12535   ListsPerDecl.reserve(UniqueDecls);
12536   for (unsigned i = 0; i < UniqueDecls; ++i)
12537     ListsPerDecl.push_back(Record.readInt());
12538   C->setDeclNumLists(ListsPerDecl);
12539 
12540   SmallVector<unsigned, 32> ListSizes;
12541   ListSizes.reserve(TotalLists);
12542   for (unsigned i = 0; i < TotalLists; ++i)
12543     ListSizes.push_back(Record.readInt());
12544   C->setComponentListSizes(ListSizes);
12545 
12546   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12547   Components.reserve(TotalComponents);
12548   for (unsigned i = 0; i < TotalComponents; ++i) {
12549     Expr *AssociatedExpr = Record.readSubExpr();
12550     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12551     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12552         AssociatedExpr, AssociatedDecl));
12553   }
12554   C->setComponents(Components, ListSizes);
12555 }
12556 
12557 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12558   C->setLParenLoc(Record.readSourceLocation());
12559   auto NumVars = C->varlist_size();
12560   auto UniqueDecls = C->getUniqueDeclarationsNum();
12561   auto TotalLists = C->getTotalComponentListNum();
12562   auto TotalComponents = C->getTotalComponentsNum();
12563 
12564   SmallVector<Expr *, 16> Vars;
12565   Vars.reserve(NumVars);
12566   for (unsigned i = 0; i != NumVars; ++i)
12567     Vars.push_back(Record.readSubExpr());
12568   C->setVarRefs(Vars);
12569   Vars.clear();
12570 
12571   SmallVector<ValueDecl *, 16> Decls;
12572   Decls.reserve(UniqueDecls);
12573   for (unsigned i = 0; i < UniqueDecls; ++i)
12574     Decls.push_back(Record.readDeclAs<ValueDecl>());
12575   C->setUniqueDecls(Decls);
12576 
12577   SmallVector<unsigned, 16> ListsPerDecl;
12578   ListsPerDecl.reserve(UniqueDecls);
12579   for (unsigned i = 0; i < UniqueDecls; ++i)
12580     ListsPerDecl.push_back(Record.readInt());
12581   C->setDeclNumLists(ListsPerDecl);
12582 
12583   SmallVector<unsigned, 32> ListSizes;
12584   ListSizes.reserve(TotalLists);
12585   for (unsigned i = 0; i < TotalLists; ++i)
12586     ListSizes.push_back(Record.readInt());
12587   C->setComponentListSizes(ListSizes);
12588 
12589   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12590   Components.reserve(TotalComponents);
12591   for (unsigned i = 0; i < TotalComponents; ++i) {
12592     Expr *AssociatedExpr = Record.readSubExpr();
12593     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12594     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12595         AssociatedExpr, AssociatedDecl));
12596   }
12597   C->setComponents(Components, ListSizes);
12598 }
12599