1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Serialization/ASTReader.h"
14 #include "ASTCommon.h"
15 #include "ASTReaderInternals.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/ASTUnresolvedSet.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateBase.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/AST/UnresolvedSet.h"
40 #include "clang/Basic/CommentOptions.h"
41 #include "clang/Basic/Diagnostic.h"
42 #include "clang/Basic/DiagnosticOptions.h"
43 #include "clang/Basic/ExceptionSpecificationType.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/FileSystemOptions.h"
46 #include "clang/Basic/IdentifierTable.h"
47 #include "clang/Basic/LLVM.h"
48 #include "clang/Basic/LangOptions.h"
49 #include "clang/Basic/Module.h"
50 #include "clang/Basic/ObjCRuntime.h"
51 #include "clang/Basic/OperatorKinds.h"
52 #include "clang/Basic/PragmaKinds.h"
53 #include "clang/Basic/Sanitizers.h"
54 #include "clang/Basic/SourceLocation.h"
55 #include "clang/Basic/SourceManager.h"
56 #include "clang/Basic/SourceManagerInternals.h"
57 #include "clang/Basic/Specifiers.h"
58 #include "clang/Basic/TargetInfo.h"
59 #include "clang/Basic/TargetOptions.h"
60 #include "clang/Basic/TokenKinds.h"
61 #include "clang/Basic/Version.h"
62 #include "clang/Lex/HeaderSearch.h"
63 #include "clang/Lex/HeaderSearchOptions.h"
64 #include "clang/Lex/MacroInfo.h"
65 #include "clang/Lex/ModuleMap.h"
66 #include "clang/Lex/PreprocessingRecord.h"
67 #include "clang/Lex/Preprocessor.h"
68 #include "clang/Lex/PreprocessorOptions.h"
69 #include "clang/Lex/Token.h"
70 #include "clang/Sema/ObjCMethodList.h"
71 #include "clang/Sema/Scope.h"
72 #include "clang/Sema/Sema.h"
73 #include "clang/Sema/Weak.h"
74 #include "clang/Serialization/ASTBitCodes.h"
75 #include "clang/Serialization/ASTDeserializationListener.h"
76 #include "clang/Serialization/ContinuousRangeMap.h"
77 #include "clang/Serialization/GlobalModuleIndex.h"
78 #include "clang/Serialization/InMemoryModuleCache.h"
79 #include "clang/Serialization/Module.h"
80 #include "clang/Serialization/ModuleFileExtension.h"
81 #include "clang/Serialization/ModuleManager.h"
82 #include "clang/Serialization/PCHContainerOperations.h"
83 #include "clang/Serialization/SerializationDiagnostic.h"
84 #include "llvm/ADT/APFloat.h"
85 #include "llvm/ADT/APInt.h"
86 #include "llvm/ADT/APSInt.h"
87 #include "llvm/ADT/ArrayRef.h"
88 #include "llvm/ADT/DenseMap.h"
89 #include "llvm/ADT/FoldingSet.h"
90 #include "llvm/ADT/Hashing.h"
91 #include "llvm/ADT/IntrusiveRefCntPtr.h"
92 #include "llvm/ADT/None.h"
93 #include "llvm/ADT/Optional.h"
94 #include "llvm/ADT/STLExtras.h"
95 #include "llvm/ADT/ScopeExit.h"
96 #include "llvm/ADT/SmallPtrSet.h"
97 #include "llvm/ADT/SmallString.h"
98 #include "llvm/ADT/SmallVector.h"
99 #include "llvm/ADT/StringExtras.h"
100 #include "llvm/ADT/StringMap.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/ADT/iterator_range.h"
104 #include "llvm/Bitstream/BitstreamReader.h"
105 #include "llvm/Support/Casting.h"
106 #include "llvm/Support/Compiler.h"
107 #include "llvm/Support/Compression.h"
108 #include "llvm/Support/DJB.h"
109 #include "llvm/Support/Endian.h"
110 #include "llvm/Support/Error.h"
111 #include "llvm/Support/ErrorHandling.h"
112 #include "llvm/Support/FileSystem.h"
113 #include "llvm/Support/MemoryBuffer.h"
114 #include "llvm/Support/Path.h"
115 #include "llvm/Support/SaveAndRestore.h"
116 #include "llvm/Support/Timer.h"
117 #include "llvm/Support/VersionTuple.h"
118 #include "llvm/Support/raw_ostream.h"
119 #include <algorithm>
120 #include <cassert>
121 #include <cstddef>
122 #include <cstdint>
123 #include <cstdio>
124 #include <ctime>
125 #include <iterator>
126 #include <limits>
127 #include <map>
128 #include <memory>
129 #include <string>
130 #include <system_error>
131 #include <tuple>
132 #include <utility>
133 #include <vector>
134 
135 using namespace clang;
136 using namespace clang::serialization;
137 using namespace clang::serialization::reader;
138 using llvm::BitstreamCursor;
139 
140 //===----------------------------------------------------------------------===//
141 // ChainedASTReaderListener implementation
142 //===----------------------------------------------------------------------===//
143 
144 bool
145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146   return First->ReadFullVersionInformation(FullVersion) ||
147          Second->ReadFullVersionInformation(FullVersion);
148 }
149 
150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151   First->ReadModuleName(ModuleName);
152   Second->ReadModuleName(ModuleName);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156   First->ReadModuleMapFile(ModuleMapPath);
157   Second->ReadModuleMapFile(ModuleMapPath);
158 }
159 
160 bool
161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
162                                               bool Complain,
163                                               bool AllowCompatibleDifferences) {
164   return First->ReadLanguageOptions(LangOpts, Complain,
165                                     AllowCompatibleDifferences) ||
166          Second->ReadLanguageOptions(LangOpts, Complain,
167                                      AllowCompatibleDifferences);
168 }
169 
170 bool ChainedASTReaderListener::ReadTargetOptions(
171     const TargetOptions &TargetOpts, bool Complain,
172     bool AllowCompatibleDifferences) {
173   return First->ReadTargetOptions(TargetOpts, Complain,
174                                   AllowCompatibleDifferences) ||
175          Second->ReadTargetOptions(TargetOpts, Complain,
176                                    AllowCompatibleDifferences);
177 }
178 
179 bool ChainedASTReaderListener::ReadDiagnosticOptions(
180     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
181   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182          Second->ReadDiagnosticOptions(DiagOpts, Complain);
183 }
184 
185 bool
186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
187                                                 bool Complain) {
188   return First->ReadFileSystemOptions(FSOpts, Complain) ||
189          Second->ReadFileSystemOptions(FSOpts, Complain);
190 }
191 
192 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
193     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
194     bool Complain) {
195   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
196                                         Complain) ||
197          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198                                          Complain);
199 }
200 
201 bool ChainedASTReaderListener::ReadPreprocessorOptions(
202     const PreprocessorOptions &PPOpts, bool Complain,
203     std::string &SuggestedPredefines) {
204   return First->ReadPreprocessorOptions(PPOpts, Complain,
205                                         SuggestedPredefines) ||
206          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
207 }
208 
209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
210                                            unsigned Value) {
211   First->ReadCounter(M, Value);
212   Second->ReadCounter(M, Value);
213 }
214 
215 bool ChainedASTReaderListener::needsInputFileVisitation() {
216   return First->needsInputFileVisitation() ||
217          Second->needsInputFileVisitation();
218 }
219 
220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221   return First->needsSystemInputFileVisitation() ||
222   Second->needsSystemInputFileVisitation();
223 }
224 
225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
226                                                ModuleKind Kind) {
227   First->visitModuleFile(Filename, Kind);
228   Second->visitModuleFile(Filename, Kind);
229 }
230 
231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
232                                               bool isSystem,
233                                               bool isOverridden,
234                                               bool isExplicitModule) {
235   bool Continue = false;
236   if (First->needsInputFileVisitation() &&
237       (!isSystem || First->needsSystemInputFileVisitation()))
238     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
239                                       isExplicitModule);
240   if (Second->needsInputFileVisitation() &&
241       (!isSystem || Second->needsSystemInputFileVisitation()))
242     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
243                                        isExplicitModule);
244   return Continue;
245 }
246 
247 void ChainedASTReaderListener::readModuleFileExtension(
248        const ModuleFileExtensionMetadata &Metadata) {
249   First->readModuleFileExtension(Metadata);
250   Second->readModuleFileExtension(Metadata);
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // PCH validator implementation
255 //===----------------------------------------------------------------------===//
256 
257 ASTReaderListener::~ASTReaderListener() = default;
258 
259 /// Compare the given set of language options against an existing set of
260 /// language options.
261 ///
262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
263 /// \param AllowCompatibleDifferences If true, differences between compatible
264 ///        language options will be permitted.
265 ///
266 /// \returns true if the languagae options mis-match, false otherwise.
267 static bool checkLanguageOptions(const LangOptions &LangOpts,
268                                  const LangOptions &ExistingLangOpts,
269                                  DiagnosticsEngine *Diags,
270                                  bool AllowCompatibleDifferences = true) {
271 #define LANGOPT(Name, Bits, Default, Description)                 \
272   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
273     if (Diags)                                                    \
274       Diags->Report(diag::err_pch_langopt_mismatch)               \
275         << Description << LangOpts.Name << ExistingLangOpts.Name; \
276     return true;                                                  \
277   }
278 
279 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
280   if (ExistingLangOpts.Name != LangOpts.Name) {           \
281     if (Diags)                                            \
282       Diags->Report(diag::err_pch_langopt_value_mismatch) \
283         << Description;                                   \
284     return true;                                          \
285   }
286 
287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
288   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
289     if (Diags)                                                 \
290       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
291         << Description;                                        \
292     return true;                                               \
293   }
294 
295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
296   if (!AllowCompatibleDifferences)                            \
297     LANGOPT(Name, Bits, Default, Description)
298 
299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
300   if (!AllowCompatibleDifferences)                                 \
301     ENUM_LANGOPT(Name, Bits, Default, Description)
302 
303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304   if (!AllowCompatibleDifferences)                                 \
305     VALUE_LANGOPT(Name, Bits, Default, Description)
306 
307 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
310 #include "clang/Basic/LangOptions.def"
311 
312   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
313     if (Diags)
314       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
315     return true;
316   }
317 
318   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch)
321       << "target Objective-C runtime";
322     return true;
323   }
324 
325   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326       LangOpts.CommentOpts.BlockCommandNames) {
327     if (Diags)
328       Diags->Report(diag::err_pch_langopt_value_mismatch)
329         << "block command names";
330     return true;
331   }
332 
333   // Sanitizer feature mismatches are treated as compatible differences. If
334   // compatible differences aren't allowed, we still only want to check for
335   // mismatches of non-modular sanitizers (the only ones which can affect AST
336   // generation).
337   if (!AllowCompatibleDifferences) {
338     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341     ExistingSanitizers.clear(ModularSanitizers);
342     ImportedSanitizers.clear(ModularSanitizers);
343     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344       const std::string Flag = "-fsanitize=";
345       if (Diags) {
346 #define SANITIZER(NAME, ID)                                                    \
347   {                                                                            \
348     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
349     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
350     if (InExistingModule != InImportedModule)                                  \
351       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
352           << InExistingModule << (Flag + NAME);                                \
353   }
354 #include "clang/Basic/Sanitizers.def"
355       }
356       return true;
357     }
358   }
359 
360   return false;
361 }
362 
363 /// Compare the given set of target options against an existing set of
364 /// target options.
365 ///
366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
367 ///
368 /// \returns true if the target options mis-match, false otherwise.
369 static bool checkTargetOptions(const TargetOptions &TargetOpts,
370                                const TargetOptions &ExistingTargetOpts,
371                                DiagnosticsEngine *Diags,
372                                bool AllowCompatibleDifferences = true) {
373 #define CHECK_TARGET_OPT(Field, Name)                             \
374   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
375     if (Diags)                                                    \
376       Diags->Report(diag::err_pch_targetopt_mismatch)             \
377         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
378     return true;                                                  \
379   }
380 
381   // The triple and ABI must match exactly.
382   CHECK_TARGET_OPT(Triple, "target");
383   CHECK_TARGET_OPT(ABI, "target ABI");
384 
385   // We can tolerate different CPUs in many cases, notably when one CPU
386   // supports a strict superset of another. When allowing compatible
387   // differences skip this check.
388   if (!AllowCompatibleDifferences)
389     CHECK_TARGET_OPT(CPU, "target CPU");
390 
391 #undef CHECK_TARGET_OPT
392 
393   // Compare feature sets.
394   SmallVector<StringRef, 4> ExistingFeatures(
395                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
396                                              ExistingTargetOpts.FeaturesAsWritten.end());
397   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398                                          TargetOpts.FeaturesAsWritten.end());
399   llvm::sort(ExistingFeatures);
400   llvm::sort(ReadFeatures);
401 
402   // We compute the set difference in both directions explicitly so that we can
403   // diagnose the differences differently.
404   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
405   std::set_difference(
406       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409                       ExistingFeatures.begin(), ExistingFeatures.end(),
410                       std::back_inserter(UnmatchedReadFeatures));
411 
412   // If we are allowing compatible differences and the read feature set is
413   // a strict subset of the existing feature set, there is nothing to diagnose.
414   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
415     return false;
416 
417   if (Diags) {
418     for (StringRef Feature : UnmatchedReadFeatures)
419       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
420           << /* is-existing-feature */ false << Feature;
421     for (StringRef Feature : UnmatchedExistingFeatures)
422       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423           << /* is-existing-feature */ true << Feature;
424   }
425 
426   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
427 }
428 
429 bool
430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
431                                   bool Complain,
432                                   bool AllowCompatibleDifferences) {
433   const LangOptions &ExistingLangOpts = PP.getLangOpts();
434   return checkLanguageOptions(LangOpts, ExistingLangOpts,
435                               Complain ? &Reader.Diags : nullptr,
436                               AllowCompatibleDifferences);
437 }
438 
439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
440                                      bool Complain,
441                                      bool AllowCompatibleDifferences) {
442   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
444                             Complain ? &Reader.Diags : nullptr,
445                             AllowCompatibleDifferences);
446 }
447 
448 namespace {
449 
450 using MacroDefinitionsMap =
451     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
453 
454 } // namespace
455 
456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457                                          DiagnosticsEngine &Diags,
458                                          bool Complain) {
459   using Level = DiagnosticsEngine::Level;
460 
461   // Check current mappings for new -Werror mappings, and the stored mappings
462   // for cases that were explicitly mapped to *not* be errors that are now
463   // errors because of options like -Werror.
464   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
465 
466   for (DiagnosticsEngine *MappingSource : MappingSources) {
467     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468       diag::kind DiagID = DiagIDMappingPair.first;
469       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470       if (CurLevel < DiagnosticsEngine::Error)
471         continue; // not significant
472       Level StoredLevel =
473           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474       if (StoredLevel < DiagnosticsEngine::Error) {
475         if (Complain)
476           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
478         return true;
479       }
480     }
481   }
482 
483   return false;
484 }
485 
486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
489     return true;
490   return Ext >= diag::Severity::Error;
491 }
492 
493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494                                     DiagnosticsEngine &Diags,
495                                     bool IsSystem, bool Complain) {
496   // Top-level options
497   if (IsSystem) {
498     if (Diags.getSuppressSystemWarnings())
499       return false;
500     // If -Wsystem-headers was not enabled before, be conservative
501     if (StoredDiags.getSuppressSystemWarnings()) {
502       if (Complain)
503         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
504       return true;
505     }
506   }
507 
508   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
509     if (Complain)
510       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
511     return true;
512   }
513 
514   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515       !StoredDiags.getEnableAllWarnings()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
518     return true;
519   }
520 
521   if (isExtHandlingFromDiagsError(Diags) &&
522       !isExtHandlingFromDiagsError(StoredDiags)) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
525     return true;
526   }
527 
528   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
529 }
530 
531 /// Return the top import module if it is implicit, nullptr otherwise.
532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
533                                           Preprocessor &PP) {
534   // If the original import came from a file explicitly generated by the user,
535   // don't check the diagnostic mappings.
536   // FIXME: currently this is approximated by checking whether this is not a
537   // module import of an implicitly-loaded module file.
538   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539   // the transitive closure of its imports, since unrelated modules cannot be
540   // imported until after this module finishes validation.
541   ModuleFile *TopImport = &*ModuleMgr.rbegin();
542   while (!TopImport->ImportedBy.empty())
543     TopImport = TopImport->ImportedBy[0];
544   if (TopImport->Kind != MK_ImplicitModule)
545     return nullptr;
546 
547   StringRef ModuleName = TopImport->ModuleName;
548   assert(!ModuleName.empty() && "diagnostic options read before module name");
549 
550   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551   assert(M && "missing module");
552   return M;
553 }
554 
555 bool PCHValidator::ReadDiagnosticOptions(
556     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
560       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
561   // This should never fail, because we would have processed these options
562   // before writing them to an ASTFile.
563   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
564 
565   ModuleManager &ModuleMgr = Reader.getModuleManager();
566   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
567 
568   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
569   if (!TopM)
570     return false;
571 
572   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573   // contains the union of their flags.
574   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
575                                  Complain);
576 }
577 
578 /// Collect the macro definitions provided by the given preprocessor
579 /// options.
580 static void
581 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582                         MacroDefinitionsMap &Macros,
583                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
584   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585     StringRef Macro = PPOpts.Macros[I].first;
586     bool IsUndef = PPOpts.Macros[I].second;
587 
588     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589     StringRef MacroName = MacroPair.first;
590     StringRef MacroBody = MacroPair.second;
591 
592     // For an #undef'd macro, we only care about the name.
593     if (IsUndef) {
594       if (MacroNames && !Macros.count(MacroName))
595         MacroNames->push_back(MacroName);
596 
597       Macros[MacroName] = std::make_pair("", true);
598       continue;
599     }
600 
601     // For a #define'd macro, figure out the actual definition.
602     if (MacroName.size() == Macro.size())
603       MacroBody = "1";
604     else {
605       // Note: GCC drops anything following an end-of-line character.
606       StringRef::size_type End = MacroBody.find_first_of("\n\r");
607       MacroBody = MacroBody.substr(0, End);
608     }
609 
610     if (MacroNames && !Macros.count(MacroName))
611       MacroNames->push_back(MacroName);
612     Macros[MacroName] = std::make_pair(MacroBody, false);
613   }
614 }
615 
616 /// Check the preprocessor options deserialized from the control block
617 /// against the preprocessor options in an existing preprocessor.
618 ///
619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620 /// \param Validate If true, validate preprocessor options. If false, allow
621 ///        macros defined by \p ExistingPPOpts to override those defined by
622 ///        \p PPOpts in SuggestedPredefines.
623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624                                      const PreprocessorOptions &ExistingPPOpts,
625                                      DiagnosticsEngine *Diags,
626                                      FileManager &FileMgr,
627                                      std::string &SuggestedPredefines,
628                                      const LangOptions &LangOpts,
629                                      bool Validate = true) {
630   // Check macro definitions.
631   MacroDefinitionsMap ASTFileMacros;
632   collectMacroDefinitions(PPOpts, ASTFileMacros);
633   MacroDefinitionsMap ExistingMacros;
634   SmallVector<StringRef, 4> ExistingMacroNames;
635   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
636 
637   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
638     // Dig out the macro definition in the existing preprocessor options.
639     StringRef MacroName = ExistingMacroNames[I];
640     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
641 
642     // Check whether we know anything about this macro name or not.
643     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644         ASTFileMacros.find(MacroName);
645     if (!Validate || Known == ASTFileMacros.end()) {
646       // FIXME: Check whether this identifier was referenced anywhere in the
647       // AST file. If so, we should reject the AST file. Unfortunately, this
648       // information isn't in the control block. What shall we do about it?
649 
650       if (Existing.second) {
651         SuggestedPredefines += "#undef ";
652         SuggestedPredefines += MacroName.str();
653         SuggestedPredefines += '\n';
654       } else {
655         SuggestedPredefines += "#define ";
656         SuggestedPredefines += MacroName.str();
657         SuggestedPredefines += ' ';
658         SuggestedPredefines += Existing.first.str();
659         SuggestedPredefines += '\n';
660       }
661       continue;
662     }
663 
664     // If the macro was defined in one but undef'd in the other, we have a
665     // conflict.
666     if (Existing.second != Known->second.second) {
667       if (Diags) {
668         Diags->Report(diag::err_pch_macro_def_undef)
669           << MacroName << Known->second.second;
670       }
671       return true;
672     }
673 
674     // If the macro was #undef'd in both, or if the macro bodies are identical,
675     // it's fine.
676     if (Existing.second || Existing.first == Known->second.first)
677       continue;
678 
679     // The macro bodies differ; complain.
680     if (Diags) {
681       Diags->Report(diag::err_pch_macro_def_conflict)
682         << MacroName << Known->second.first << Existing.first;
683     }
684     return true;
685   }
686 
687   // Check whether we're using predefines.
688   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
689     if (Diags) {
690       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
691     }
692     return true;
693   }
694 
695   // Detailed record is important since it is used for the module cache hash.
696   if (LangOpts.Modules &&
697       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
700     }
701     return true;
702   }
703 
704   // Compute the #include and #include_macros lines we need.
705   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706     StringRef File = ExistingPPOpts.Includes[I];
707 
708     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
709         !ExistingPPOpts.PCHThroughHeader.empty()) {
710       // In case the through header is an include, we must add all the includes
711       // to the predefines so the start point can be determined.
712       SuggestedPredefines += "#include \"";
713       SuggestedPredefines += File;
714       SuggestedPredefines += "\"\n";
715       continue;
716     }
717 
718     if (File == ExistingPPOpts.ImplicitPCHInclude)
719       continue;
720 
721     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
722           != PPOpts.Includes.end())
723       continue;
724 
725     SuggestedPredefines += "#include \"";
726     SuggestedPredefines += File;
727     SuggestedPredefines += "\"\n";
728   }
729 
730   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
731     StringRef File = ExistingPPOpts.MacroIncludes[I];
732     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
733                   File)
734         != PPOpts.MacroIncludes.end())
735       continue;
736 
737     SuggestedPredefines += "#__include_macros \"";
738     SuggestedPredefines += File;
739     SuggestedPredefines += "\"\n##\n";
740   }
741 
742   return false;
743 }
744 
745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
746                                            bool Complain,
747                                            std::string &SuggestedPredefines) {
748   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
749 
750   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
751                                   Complain? &Reader.Diags : nullptr,
752                                   PP.getFileManager(),
753                                   SuggestedPredefines,
754                                   PP.getLangOpts());
755 }
756 
757 bool SimpleASTReaderListener::ReadPreprocessorOptions(
758                                   const PreprocessorOptions &PPOpts,
759                                   bool Complain,
760                                   std::string &SuggestedPredefines) {
761   return checkPreprocessorOptions(PPOpts,
762                                   PP.getPreprocessorOpts(),
763                                   nullptr,
764                                   PP.getFileManager(),
765                                   SuggestedPredefines,
766                                   PP.getLangOpts(),
767                                   false);
768 }
769 
770 /// Check the header search options deserialized from the control block
771 /// against the header search options in an existing preprocessor.
772 ///
773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
775                                      StringRef SpecificModuleCachePath,
776                                      StringRef ExistingModuleCachePath,
777                                      DiagnosticsEngine *Diags,
778                                      const LangOptions &LangOpts) {
779   if (LangOpts.Modules) {
780     if (SpecificModuleCachePath != ExistingModuleCachePath) {
781       if (Diags)
782         Diags->Report(diag::err_pch_modulecache_mismatch)
783           << SpecificModuleCachePath << ExistingModuleCachePath;
784       return true;
785     }
786   }
787 
788   return false;
789 }
790 
791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
792                                            StringRef SpecificModuleCachePath,
793                                            bool Complain) {
794   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
795                                   PP.getHeaderSearchInfo().getModuleCachePath(),
796                                   Complain ? &Reader.Diags : nullptr,
797                                   PP.getLangOpts());
798 }
799 
800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
801   PP.setCounterValue(Value);
802 }
803 
804 //===----------------------------------------------------------------------===//
805 // AST reader implementation
806 //===----------------------------------------------------------------------===//
807 
808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
809                                            bool TakeOwnership) {
810   DeserializationListener = Listener;
811   OwnsDeserializationListener = TakeOwnership;
812 }
813 
814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
815   return serialization::ComputeHash(Sel);
816 }
817 
818 std::pair<unsigned, unsigned>
819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
820   using namespace llvm::support;
821 
822   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
823   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
824   return std::make_pair(KeyLen, DataLen);
825 }
826 
827 ASTSelectorLookupTrait::internal_key_type
828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
829   using namespace llvm::support;
830 
831   SelectorTable &SelTable = Reader.getContext().Selectors;
832   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
833   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
834       F, endian::readNext<uint32_t, little, unaligned>(d));
835   if (N == 0)
836     return SelTable.getNullarySelector(FirstII);
837   else if (N == 1)
838     return SelTable.getUnarySelector(FirstII);
839 
840   SmallVector<IdentifierInfo *, 16> Args;
841   Args.push_back(FirstII);
842   for (unsigned I = 1; I != N; ++I)
843     Args.push_back(Reader.getLocalIdentifier(
844         F, endian::readNext<uint32_t, little, unaligned>(d)));
845 
846   return SelTable.getSelector(N, Args.data());
847 }
848 
849 ASTSelectorLookupTrait::data_type
850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
851                                  unsigned DataLen) {
852   using namespace llvm::support;
853 
854   data_type Result;
855 
856   Result.ID = Reader.getGlobalSelectorID(
857       F, endian::readNext<uint32_t, little, unaligned>(d));
858   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
859   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
860   Result.InstanceBits = FullInstanceBits & 0x3;
861   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
862   Result.FactoryBits = FullFactoryBits & 0x3;
863   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
864   unsigned NumInstanceMethods = FullInstanceBits >> 3;
865   unsigned NumFactoryMethods = FullFactoryBits >> 3;
866 
867   // Load instance methods
868   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
869     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
870             F, endian::readNext<uint32_t, little, unaligned>(d)))
871       Result.Instance.push_back(Method);
872   }
873 
874   // Load factory methods
875   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Factory.push_back(Method);
879   }
880 
881   return Result;
882 }
883 
884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
885   return llvm::djbHash(a);
886 }
887 
888 std::pair<unsigned, unsigned>
889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
890   using namespace llvm::support;
891 
892   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
893   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
894   return std::make_pair(KeyLen, DataLen);
895 }
896 
897 ASTIdentifierLookupTraitBase::internal_key_type
898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
899   assert(n >= 2 && d[n-1] == '\0');
900   return StringRef((const char*) d, n-1);
901 }
902 
903 /// Whether the given identifier is "interesting".
904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
905                                     bool IsModule) {
906   return II.hadMacroDefinition() ||
907          II.isPoisoned() ||
908          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
909          II.hasRevertedTokenIDToIdentifier() ||
910          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
911           II.getFETokenInfo());
912 }
913 
914 static bool readBit(unsigned &Bits) {
915   bool Value = Bits & 0x1;
916   Bits >>= 1;
917   return Value;
918 }
919 
920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
921   using namespace llvm::support;
922 
923   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
924   return Reader.getGlobalIdentifierID(F, RawID >> 1);
925 }
926 
927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
928   if (!II.isFromAST()) {
929     II.setIsFromAST();
930     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
931     if (isInterestingIdentifier(Reader, II, IsModule))
932       II.setChangedSinceDeserialization();
933   }
934 }
935 
936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
937                                                    const unsigned char* d,
938                                                    unsigned DataLen) {
939   using namespace llvm::support;
940 
941   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
942   bool IsInteresting = RawID & 0x01;
943 
944   // Wipe out the "is interesting" bit.
945   RawID = RawID >> 1;
946 
947   // Build the IdentifierInfo and link the identifier ID with it.
948   IdentifierInfo *II = KnownII;
949   if (!II) {
950     II = &Reader.getIdentifierTable().getOwn(k);
951     KnownII = II;
952   }
953   markIdentifierFromAST(Reader, *II);
954   Reader.markIdentifierUpToDate(II);
955 
956   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
957   if (!IsInteresting) {
958     // For uninteresting identifiers, there's nothing else to do. Just notify
959     // the reader that we've finished loading this identifier.
960     Reader.SetIdentifierInfo(ID, II);
961     return II;
962   }
963 
964   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
965   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
966   bool CPlusPlusOperatorKeyword = readBit(Bits);
967   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
968   bool HasRevertedBuiltin = readBit(Bits);
969   bool Poisoned = readBit(Bits);
970   bool ExtensionToken = readBit(Bits);
971   bool HadMacroDefinition = readBit(Bits);
972 
973   assert(Bits == 0 && "Extra bits in the identifier?");
974   DataLen -= 8;
975 
976   // Set or check the various bits in the IdentifierInfo structure.
977   // Token IDs are read-only.
978   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
979     II->revertTokenIDToIdentifier();
980   if (!F.isModule())
981     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
982   else if (HasRevertedBuiltin && II->getBuiltinID()) {
983     II->revertBuiltin();
984     assert((II->hasRevertedBuiltin() ||
985             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
986            "Incorrect ObjC keyword or builtin ID");
987   }
988   assert(II->isExtensionToken() == ExtensionToken &&
989          "Incorrect extension token flag");
990   (void)ExtensionToken;
991   if (Poisoned)
992     II->setIsPoisoned(true);
993   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
994          "Incorrect C++ operator keyword flag");
995   (void)CPlusPlusOperatorKeyword;
996 
997   // If this identifier is a macro, deserialize the macro
998   // definition.
999   if (HadMacroDefinition) {
1000     uint32_t MacroDirectivesOffset =
1001         endian::readNext<uint32_t, little, unaligned>(d);
1002     DataLen -= 4;
1003 
1004     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1005   }
1006 
1007   Reader.SetIdentifierInfo(ID, II);
1008 
1009   // Read all of the declarations visible at global scope with this
1010   // name.
1011   if (DataLen > 0) {
1012     SmallVector<uint32_t, 4> DeclIDs;
1013     for (; DataLen > 0; DataLen -= 4)
1014       DeclIDs.push_back(Reader.getGlobalDeclID(
1015           F, endian::readNext<uint32_t, little, unaligned>(d)));
1016     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1017   }
1018 
1019   return II;
1020 }
1021 
1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1023     : Kind(Name.getNameKind()) {
1024   switch (Kind) {
1025   case DeclarationName::Identifier:
1026     Data = (uint64_t)Name.getAsIdentifierInfo();
1027     break;
1028   case DeclarationName::ObjCZeroArgSelector:
1029   case DeclarationName::ObjCOneArgSelector:
1030   case DeclarationName::ObjCMultiArgSelector:
1031     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1032     break;
1033   case DeclarationName::CXXOperatorName:
1034     Data = Name.getCXXOverloadedOperator();
1035     break;
1036   case DeclarationName::CXXLiteralOperatorName:
1037     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1038     break;
1039   case DeclarationName::CXXDeductionGuideName:
1040     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1041                ->getDeclName().getAsIdentifierInfo();
1042     break;
1043   case DeclarationName::CXXConstructorName:
1044   case DeclarationName::CXXDestructorName:
1045   case DeclarationName::CXXConversionFunctionName:
1046   case DeclarationName::CXXUsingDirective:
1047     Data = 0;
1048     break;
1049   }
1050 }
1051 
1052 unsigned DeclarationNameKey::getHash() const {
1053   llvm::FoldingSetNodeID ID;
1054   ID.AddInteger(Kind);
1055 
1056   switch (Kind) {
1057   case DeclarationName::Identifier:
1058   case DeclarationName::CXXLiteralOperatorName:
1059   case DeclarationName::CXXDeductionGuideName:
1060     ID.AddString(((IdentifierInfo*)Data)->getName());
1061     break;
1062   case DeclarationName::ObjCZeroArgSelector:
1063   case DeclarationName::ObjCOneArgSelector:
1064   case DeclarationName::ObjCMultiArgSelector:
1065     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1066     break;
1067   case DeclarationName::CXXOperatorName:
1068     ID.AddInteger((OverloadedOperatorKind)Data);
1069     break;
1070   case DeclarationName::CXXConstructorName:
1071   case DeclarationName::CXXDestructorName:
1072   case DeclarationName::CXXConversionFunctionName:
1073   case DeclarationName::CXXUsingDirective:
1074     break;
1075   }
1076 
1077   return ID.ComputeHash();
1078 }
1079 
1080 ModuleFile *
1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1082   using namespace llvm::support;
1083 
1084   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1085   return Reader.getLocalModuleFile(F, ModuleFileID);
1086 }
1087 
1088 std::pair<unsigned, unsigned>
1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1090   using namespace llvm::support;
1091 
1092   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1093   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1094   return std::make_pair(KeyLen, DataLen);
1095 }
1096 
1097 ASTDeclContextNameLookupTrait::internal_key_type
1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1099   using namespace llvm::support;
1100 
1101   auto Kind = (DeclarationName::NameKind)*d++;
1102   uint64_t Data;
1103   switch (Kind) {
1104   case DeclarationName::Identifier:
1105   case DeclarationName::CXXLiteralOperatorName:
1106   case DeclarationName::CXXDeductionGuideName:
1107     Data = (uint64_t)Reader.getLocalIdentifier(
1108         F, endian::readNext<uint32_t, little, unaligned>(d));
1109     break;
1110   case DeclarationName::ObjCZeroArgSelector:
1111   case DeclarationName::ObjCOneArgSelector:
1112   case DeclarationName::ObjCMultiArgSelector:
1113     Data =
1114         (uint64_t)Reader.getLocalSelector(
1115                              F, endian::readNext<uint32_t, little, unaligned>(
1116                                     d)).getAsOpaquePtr();
1117     break;
1118   case DeclarationName::CXXOperatorName:
1119     Data = *d++; // OverloadedOperatorKind
1120     break;
1121   case DeclarationName::CXXConstructorName:
1122   case DeclarationName::CXXDestructorName:
1123   case DeclarationName::CXXConversionFunctionName:
1124   case DeclarationName::CXXUsingDirective:
1125     Data = 0;
1126     break;
1127   }
1128 
1129   return DeclarationNameKey(Kind, Data);
1130 }
1131 
1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1133                                                  const unsigned char *d,
1134                                                  unsigned DataLen,
1135                                                  data_type_builder &Val) {
1136   using namespace llvm::support;
1137 
1138   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1139     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1140     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1141   }
1142 }
1143 
1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1145                                               BitstreamCursor &Cursor,
1146                                               uint64_t Offset,
1147                                               DeclContext *DC) {
1148   assert(Offset != 0);
1149 
1150   SavedStreamPosition SavedPosition(Cursor);
1151   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1152     Error(std::move(Err));
1153     return true;
1154   }
1155 
1156   RecordData Record;
1157   StringRef Blob;
1158   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1159   if (!MaybeCode) {
1160     Error(MaybeCode.takeError());
1161     return true;
1162   }
1163   unsigned Code = MaybeCode.get();
1164 
1165   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1166   if (!MaybeRecCode) {
1167     Error(MaybeRecCode.takeError());
1168     return true;
1169   }
1170   unsigned RecCode = MaybeRecCode.get();
1171   if (RecCode != DECL_CONTEXT_LEXICAL) {
1172     Error("Expected lexical block");
1173     return true;
1174   }
1175 
1176   assert(!isa<TranslationUnitDecl>(DC) &&
1177          "expected a TU_UPDATE_LEXICAL record for TU");
1178   // If we are handling a C++ class template instantiation, we can see multiple
1179   // lexical updates for the same record. It's important that we select only one
1180   // of them, so that field numbering works properly. Just pick the first one we
1181   // see.
1182   auto &Lex = LexicalDecls[DC];
1183   if (!Lex.first) {
1184     Lex = std::make_pair(
1185         &M, llvm::makeArrayRef(
1186                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1187                     Blob.data()),
1188                 Blob.size() / 4));
1189   }
1190   DC->setHasExternalLexicalStorage(true);
1191   return false;
1192 }
1193 
1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1195                                               BitstreamCursor &Cursor,
1196                                               uint64_t Offset,
1197                                               DeclID ID) {
1198   assert(Offset != 0);
1199 
1200   SavedStreamPosition SavedPosition(Cursor);
1201   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1202     Error(std::move(Err));
1203     return true;
1204   }
1205 
1206   RecordData Record;
1207   StringRef Blob;
1208   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1209   if (!MaybeCode) {
1210     Error(MaybeCode.takeError());
1211     return true;
1212   }
1213   unsigned Code = MaybeCode.get();
1214 
1215   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1216   if (!MaybeRecCode) {
1217     Error(MaybeRecCode.takeError());
1218     return true;
1219   }
1220   unsigned RecCode = MaybeRecCode.get();
1221   if (RecCode != DECL_CONTEXT_VISIBLE) {
1222     Error("Expected visible lookup table block");
1223     return true;
1224   }
1225 
1226   // We can't safely determine the primary context yet, so delay attaching the
1227   // lookup table until we're done with recursive deserialization.
1228   auto *Data = (const unsigned char*)Blob.data();
1229   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1230   return false;
1231 }
1232 
1233 void ASTReader::Error(StringRef Msg) const {
1234   Error(diag::err_fe_pch_malformed, Msg);
1235   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1236       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1237     Diag(diag::note_module_cache_path)
1238       << PP.getHeaderSearchInfo().getModuleCachePath();
1239   }
1240 }
1241 
1242 void ASTReader::Error(unsigned DiagID,
1243                       StringRef Arg1, StringRef Arg2) const {
1244   if (Diags.isDiagnosticInFlight())
1245     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1246   else
1247     Diag(DiagID) << Arg1 << Arg2;
1248 }
1249 
1250 void ASTReader::Error(llvm::Error &&Err) const {
1251   Error(toString(std::move(Err)));
1252 }
1253 
1254 //===----------------------------------------------------------------------===//
1255 // Source Manager Deserialization
1256 //===----------------------------------------------------------------------===//
1257 
1258 /// Read the line table in the source manager block.
1259 /// \returns true if there was an error.
1260 bool ASTReader::ParseLineTable(ModuleFile &F,
1261                                const RecordData &Record) {
1262   unsigned Idx = 0;
1263   LineTableInfo &LineTable = SourceMgr.getLineTable();
1264 
1265   // Parse the file names
1266   std::map<int, int> FileIDs;
1267   FileIDs[-1] = -1; // For unspecified filenames.
1268   for (unsigned I = 0; Record[Idx]; ++I) {
1269     // Extract the file name
1270     auto Filename = ReadPath(F, Record, Idx);
1271     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1272   }
1273   ++Idx;
1274 
1275   // Parse the line entries
1276   std::vector<LineEntry> Entries;
1277   while (Idx < Record.size()) {
1278     int FID = Record[Idx++];
1279     assert(FID >= 0 && "Serialized line entries for non-local file.");
1280     // Remap FileID from 1-based old view.
1281     FID += F.SLocEntryBaseID - 1;
1282 
1283     // Extract the line entries
1284     unsigned NumEntries = Record[Idx++];
1285     assert(NumEntries && "no line entries for file ID");
1286     Entries.clear();
1287     Entries.reserve(NumEntries);
1288     for (unsigned I = 0; I != NumEntries; ++I) {
1289       unsigned FileOffset = Record[Idx++];
1290       unsigned LineNo = Record[Idx++];
1291       int FilenameID = FileIDs[Record[Idx++]];
1292       SrcMgr::CharacteristicKind FileKind
1293         = (SrcMgr::CharacteristicKind)Record[Idx++];
1294       unsigned IncludeOffset = Record[Idx++];
1295       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1296                                        FileKind, IncludeOffset));
1297     }
1298     LineTable.AddEntry(FileID::get(FID), Entries);
1299   }
1300 
1301   return false;
1302 }
1303 
1304 /// Read a source manager block
1305 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1306   using namespace SrcMgr;
1307 
1308   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1309 
1310   // Set the source-location entry cursor to the current position in
1311   // the stream. This cursor will be used to read the contents of the
1312   // source manager block initially, and then lazily read
1313   // source-location entries as needed.
1314   SLocEntryCursor = F.Stream;
1315 
1316   // The stream itself is going to skip over the source manager block.
1317   if (llvm::Error Err = F.Stream.SkipBlock()) {
1318     Error(std::move(Err));
1319     return true;
1320   }
1321 
1322   // Enter the source manager block.
1323   if (llvm::Error Err =
1324           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1325     Error(std::move(Err));
1326     return true;
1327   }
1328 
1329   RecordData Record;
1330   while (true) {
1331     Expected<llvm::BitstreamEntry> MaybeE =
1332         SLocEntryCursor.advanceSkippingSubblocks();
1333     if (!MaybeE) {
1334       Error(MaybeE.takeError());
1335       return true;
1336     }
1337     llvm::BitstreamEntry E = MaybeE.get();
1338 
1339     switch (E.Kind) {
1340     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1341     case llvm::BitstreamEntry::Error:
1342       Error("malformed block record in AST file");
1343       return true;
1344     case llvm::BitstreamEntry::EndBlock:
1345       return false;
1346     case llvm::BitstreamEntry::Record:
1347       // The interesting case.
1348       break;
1349     }
1350 
1351     // Read a record.
1352     Record.clear();
1353     StringRef Blob;
1354     Expected<unsigned> MaybeRecord =
1355         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1356     if (!MaybeRecord) {
1357       Error(MaybeRecord.takeError());
1358       return true;
1359     }
1360     switch (MaybeRecord.get()) {
1361     default:  // Default behavior: ignore.
1362       break;
1363 
1364     case SM_SLOC_FILE_ENTRY:
1365     case SM_SLOC_BUFFER_ENTRY:
1366     case SM_SLOC_EXPANSION_ENTRY:
1367       // Once we hit one of the source location entries, we're done.
1368       return false;
1369     }
1370   }
1371 }
1372 
1373 /// If a header file is not found at the path that we expect it to be
1374 /// and the PCH file was moved from its original location, try to resolve the
1375 /// file by assuming that header+PCH were moved together and the header is in
1376 /// the same place relative to the PCH.
1377 static std::string
1378 resolveFileRelativeToOriginalDir(const std::string &Filename,
1379                                  const std::string &OriginalDir,
1380                                  const std::string &CurrDir) {
1381   assert(OriginalDir != CurrDir &&
1382          "No point trying to resolve the file if the PCH dir didn't change");
1383 
1384   using namespace llvm::sys;
1385 
1386   SmallString<128> filePath(Filename);
1387   fs::make_absolute(filePath);
1388   assert(path::is_absolute(OriginalDir));
1389   SmallString<128> currPCHPath(CurrDir);
1390 
1391   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1392                        fileDirE = path::end(path::parent_path(filePath));
1393   path::const_iterator origDirI = path::begin(OriginalDir),
1394                        origDirE = path::end(OriginalDir);
1395   // Skip the common path components from filePath and OriginalDir.
1396   while (fileDirI != fileDirE && origDirI != origDirE &&
1397          *fileDirI == *origDirI) {
1398     ++fileDirI;
1399     ++origDirI;
1400   }
1401   for (; origDirI != origDirE; ++origDirI)
1402     path::append(currPCHPath, "..");
1403   path::append(currPCHPath, fileDirI, fileDirE);
1404   path::append(currPCHPath, path::filename(Filename));
1405   return currPCHPath.str();
1406 }
1407 
1408 bool ASTReader::ReadSLocEntry(int ID) {
1409   if (ID == 0)
1410     return false;
1411 
1412   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1413     Error("source location entry ID out-of-range for AST file");
1414     return true;
1415   }
1416 
1417   // Local helper to read the (possibly-compressed) buffer data following the
1418   // entry record.
1419   auto ReadBuffer = [this](
1420       BitstreamCursor &SLocEntryCursor,
1421       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1422     RecordData Record;
1423     StringRef Blob;
1424     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1425     if (!MaybeCode) {
1426       Error(MaybeCode.takeError());
1427       return nullptr;
1428     }
1429     unsigned Code = MaybeCode.get();
1430 
1431     Expected<unsigned> MaybeRecCode =
1432         SLocEntryCursor.readRecord(Code, Record, &Blob);
1433     if (!MaybeRecCode) {
1434       Error(MaybeRecCode.takeError());
1435       return nullptr;
1436     }
1437     unsigned RecCode = MaybeRecCode.get();
1438 
1439     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1440       if (!llvm::zlib::isAvailable()) {
1441         Error("zlib is not available");
1442         return nullptr;
1443       }
1444       SmallString<0> Uncompressed;
1445       if (llvm::Error E =
1446               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1447         Error("could not decompress embedded file contents: " +
1448               llvm::toString(std::move(E)));
1449         return nullptr;
1450       }
1451       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1452     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1453       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1454     } else {
1455       Error("AST record has invalid code");
1456       return nullptr;
1457     }
1458   };
1459 
1460   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1461   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1462           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1463     Error(std::move(Err));
1464     return true;
1465   }
1466 
1467   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1468   unsigned BaseOffset = F->SLocEntryBaseOffset;
1469 
1470   ++NumSLocEntriesRead;
1471   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1472   if (!MaybeEntry) {
1473     Error(MaybeEntry.takeError());
1474     return true;
1475   }
1476   llvm::BitstreamEntry Entry = MaybeEntry.get();
1477 
1478   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1479     Error("incorrectly-formatted source location entry in AST file");
1480     return true;
1481   }
1482 
1483   RecordData Record;
1484   StringRef Blob;
1485   Expected<unsigned> MaybeSLOC =
1486       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1487   if (!MaybeSLOC) {
1488     Error(MaybeSLOC.takeError());
1489     return true;
1490   }
1491   switch (MaybeSLOC.get()) {
1492   default:
1493     Error("incorrectly-formatted source location entry in AST file");
1494     return true;
1495 
1496   case SM_SLOC_FILE_ENTRY: {
1497     // We will detect whether a file changed and return 'Failure' for it, but
1498     // we will also try to fail gracefully by setting up the SLocEntry.
1499     unsigned InputID = Record[4];
1500     InputFile IF = getInputFile(*F, InputID);
1501     const FileEntry *File = IF.getFile();
1502     bool OverriddenBuffer = IF.isOverridden();
1503 
1504     // Note that we only check if a File was returned. If it was out-of-date
1505     // we have complained but we will continue creating a FileID to recover
1506     // gracefully.
1507     if (!File)
1508       return true;
1509 
1510     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1511     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1512       // This is the module's main file.
1513       IncludeLoc = getImportLocation(F);
1514     }
1515     SrcMgr::CharacteristicKind
1516       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1517     // FIXME: The FileID should be created from the FileEntryRef.
1518     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1519                                         ID, BaseOffset + Record[0]);
1520     SrcMgr::FileInfo &FileInfo =
1521           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1522     FileInfo.NumCreatedFIDs = Record[5];
1523     if (Record[3])
1524       FileInfo.setHasLineDirectives();
1525 
1526     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1527     unsigned NumFileDecls = Record[7];
1528     if (NumFileDecls && ContextObj) {
1529       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1530       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1531                                                              NumFileDecls));
1532     }
1533 
1534     const SrcMgr::ContentCache *ContentCache
1535       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1536     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1537         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1538         !ContentCache->getRawBuffer()) {
1539       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1540       if (!Buffer)
1541         return true;
1542       SourceMgr.overrideFileContents(File, std::move(Buffer));
1543     }
1544 
1545     break;
1546   }
1547 
1548   case SM_SLOC_BUFFER_ENTRY: {
1549     const char *Name = Blob.data();
1550     unsigned Offset = Record[0];
1551     SrcMgr::CharacteristicKind
1552       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1553     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1554     if (IncludeLoc.isInvalid() && F->isModule()) {
1555       IncludeLoc = getImportLocation(F);
1556     }
1557 
1558     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1559     if (!Buffer)
1560       return true;
1561     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1562                            BaseOffset + Offset, IncludeLoc);
1563     break;
1564   }
1565 
1566   case SM_SLOC_EXPANSION_ENTRY: {
1567     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1568     SourceMgr.createExpansionLoc(SpellingLoc,
1569                                      ReadSourceLocation(*F, Record[2]),
1570                                      ReadSourceLocation(*F, Record[3]),
1571                                      Record[5],
1572                                      Record[4],
1573                                      ID,
1574                                      BaseOffset + Record[0]);
1575     break;
1576   }
1577   }
1578 
1579   return false;
1580 }
1581 
1582 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1583   if (ID == 0)
1584     return std::make_pair(SourceLocation(), "");
1585 
1586   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1587     Error("source location entry ID out-of-range for AST file");
1588     return std::make_pair(SourceLocation(), "");
1589   }
1590 
1591   // Find which module file this entry lands in.
1592   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1593   if (!M->isModule())
1594     return std::make_pair(SourceLocation(), "");
1595 
1596   // FIXME: Can we map this down to a particular submodule? That would be
1597   // ideal.
1598   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1599 }
1600 
1601 /// Find the location where the module F is imported.
1602 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1603   if (F->ImportLoc.isValid())
1604     return F->ImportLoc;
1605 
1606   // Otherwise we have a PCH. It's considered to be "imported" at the first
1607   // location of its includer.
1608   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1609     // Main file is the importer.
1610     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1611     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1612   }
1613   return F->ImportedBy[0]->FirstLoc;
1614 }
1615 
1616 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1617 /// the abbreviations that are at the top of the block and then leave the cursor
1618 /// pointing into the block.
1619 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1620   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1621     // FIXME this drops errors on the floor.
1622     consumeError(std::move(Err));
1623     return true;
1624   }
1625 
1626   while (true) {
1627     uint64_t Offset = Cursor.GetCurrentBitNo();
1628     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1629     if (!MaybeCode) {
1630       // FIXME this drops errors on the floor.
1631       consumeError(MaybeCode.takeError());
1632       return true;
1633     }
1634     unsigned Code = MaybeCode.get();
1635 
1636     // We expect all abbrevs to be at the start of the block.
1637     if (Code != llvm::bitc::DEFINE_ABBREV) {
1638       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1639         // FIXME this drops errors on the floor.
1640         consumeError(std::move(Err));
1641         return true;
1642       }
1643       return false;
1644     }
1645     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1646       // FIXME this drops errors on the floor.
1647       consumeError(std::move(Err));
1648       return true;
1649     }
1650   }
1651 }
1652 
1653 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1654                            unsigned &Idx) {
1655   Token Tok;
1656   Tok.startToken();
1657   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1658   Tok.setLength(Record[Idx++]);
1659   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1660     Tok.setIdentifierInfo(II);
1661   Tok.setKind((tok::TokenKind)Record[Idx++]);
1662   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1663   return Tok;
1664 }
1665 
1666 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1667   BitstreamCursor &Stream = F.MacroCursor;
1668 
1669   // Keep track of where we are in the stream, then jump back there
1670   // after reading this macro.
1671   SavedStreamPosition SavedPosition(Stream);
1672 
1673   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1674     // FIXME this drops errors on the floor.
1675     consumeError(std::move(Err));
1676     return nullptr;
1677   }
1678   RecordData Record;
1679   SmallVector<IdentifierInfo*, 16> MacroParams;
1680   MacroInfo *Macro = nullptr;
1681 
1682   while (true) {
1683     // Advance to the next record, but if we get to the end of the block, don't
1684     // pop it (removing all the abbreviations from the cursor) since we want to
1685     // be able to reseek within the block and read entries.
1686     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1687     Expected<llvm::BitstreamEntry> MaybeEntry =
1688         Stream.advanceSkippingSubblocks(Flags);
1689     if (!MaybeEntry) {
1690       Error(MaybeEntry.takeError());
1691       return Macro;
1692     }
1693     llvm::BitstreamEntry Entry = MaybeEntry.get();
1694 
1695     switch (Entry.Kind) {
1696     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1697     case llvm::BitstreamEntry::Error:
1698       Error("malformed block record in AST file");
1699       return Macro;
1700     case llvm::BitstreamEntry::EndBlock:
1701       return Macro;
1702     case llvm::BitstreamEntry::Record:
1703       // The interesting case.
1704       break;
1705     }
1706 
1707     // Read a record.
1708     Record.clear();
1709     PreprocessorRecordTypes RecType;
1710     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1711       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1712     else {
1713       Error(MaybeRecType.takeError());
1714       return Macro;
1715     }
1716     switch (RecType) {
1717     case PP_MODULE_MACRO:
1718     case PP_MACRO_DIRECTIVE_HISTORY:
1719       return Macro;
1720 
1721     case PP_MACRO_OBJECT_LIKE:
1722     case PP_MACRO_FUNCTION_LIKE: {
1723       // If we already have a macro, that means that we've hit the end
1724       // of the definition of the macro we were looking for. We're
1725       // done.
1726       if (Macro)
1727         return Macro;
1728 
1729       unsigned NextIndex = 1; // Skip identifier ID.
1730       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1731       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1732       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1733       MI->setIsUsed(Record[NextIndex++]);
1734       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1735 
1736       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1737         // Decode function-like macro info.
1738         bool isC99VarArgs = Record[NextIndex++];
1739         bool isGNUVarArgs = Record[NextIndex++];
1740         bool hasCommaPasting = Record[NextIndex++];
1741         MacroParams.clear();
1742         unsigned NumArgs = Record[NextIndex++];
1743         for (unsigned i = 0; i != NumArgs; ++i)
1744           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1745 
1746         // Install function-like macro info.
1747         MI->setIsFunctionLike();
1748         if (isC99VarArgs) MI->setIsC99Varargs();
1749         if (isGNUVarArgs) MI->setIsGNUVarargs();
1750         if (hasCommaPasting) MI->setHasCommaPasting();
1751         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1752       }
1753 
1754       // Remember that we saw this macro last so that we add the tokens that
1755       // form its body to it.
1756       Macro = MI;
1757 
1758       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1759           Record[NextIndex]) {
1760         // We have a macro definition. Register the association
1761         PreprocessedEntityID
1762             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1763         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1764         PreprocessingRecord::PPEntityID PPID =
1765             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1766         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1767             PPRec.getPreprocessedEntity(PPID));
1768         if (PPDef)
1769           PPRec.RegisterMacroDefinition(Macro, PPDef);
1770       }
1771 
1772       ++NumMacrosRead;
1773       break;
1774     }
1775 
1776     case PP_TOKEN: {
1777       // If we see a TOKEN before a PP_MACRO_*, then the file is
1778       // erroneous, just pretend we didn't see this.
1779       if (!Macro) break;
1780 
1781       unsigned Idx = 0;
1782       Token Tok = ReadToken(F, Record, Idx);
1783       Macro->AddTokenToBody(Tok);
1784       break;
1785     }
1786     }
1787   }
1788 }
1789 
1790 PreprocessedEntityID
1791 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1792                                          unsigned LocalID) const {
1793   if (!M.ModuleOffsetMap.empty())
1794     ReadModuleOffsetMap(M);
1795 
1796   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1797     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1798   assert(I != M.PreprocessedEntityRemap.end()
1799          && "Invalid index into preprocessed entity index remap");
1800 
1801   return LocalID + I->second;
1802 }
1803 
1804 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1805   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1806 }
1807 
1808 HeaderFileInfoTrait::internal_key_type
1809 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1810   internal_key_type ikey = {FE->getSize(),
1811                             M.HasTimestamps ? FE->getModificationTime() : 0,
1812                             FE->getName(), /*Imported*/ false};
1813   return ikey;
1814 }
1815 
1816 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1817   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1818     return false;
1819 
1820   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1821     return true;
1822 
1823   // Determine whether the actual files are equivalent.
1824   FileManager &FileMgr = Reader.getFileManager();
1825   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1826     if (!Key.Imported) {
1827       if (auto File = FileMgr.getFile(Key.Filename))
1828         return *File;
1829       return nullptr;
1830     }
1831 
1832     std::string Resolved = Key.Filename;
1833     Reader.ResolveImportedPath(M, Resolved);
1834     if (auto File = FileMgr.getFile(Resolved))
1835       return *File;
1836     return nullptr;
1837   };
1838 
1839   const FileEntry *FEA = GetFile(a);
1840   const FileEntry *FEB = GetFile(b);
1841   return FEA && FEA == FEB;
1842 }
1843 
1844 std::pair<unsigned, unsigned>
1845 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1846   using namespace llvm::support;
1847 
1848   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1849   unsigned DataLen = (unsigned) *d++;
1850   return std::make_pair(KeyLen, DataLen);
1851 }
1852 
1853 HeaderFileInfoTrait::internal_key_type
1854 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1855   using namespace llvm::support;
1856 
1857   internal_key_type ikey;
1858   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1859   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1860   ikey.Filename = (const char *)d;
1861   ikey.Imported = true;
1862   return ikey;
1863 }
1864 
1865 HeaderFileInfoTrait::data_type
1866 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1867                               unsigned DataLen) {
1868   using namespace llvm::support;
1869 
1870   const unsigned char *End = d + DataLen;
1871   HeaderFileInfo HFI;
1872   unsigned Flags = *d++;
1873   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1874   HFI.isImport |= (Flags >> 5) & 0x01;
1875   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1876   HFI.DirInfo = (Flags >> 1) & 0x07;
1877   HFI.IndexHeaderMapHeader = Flags & 0x01;
1878   // FIXME: Find a better way to handle this. Maybe just store a
1879   // "has been included" flag?
1880   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1881                              HFI.NumIncludes);
1882   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1883       M, endian::readNext<uint32_t, little, unaligned>(d));
1884   if (unsigned FrameworkOffset =
1885           endian::readNext<uint32_t, little, unaligned>(d)) {
1886     // The framework offset is 1 greater than the actual offset,
1887     // since 0 is used as an indicator for "no framework name".
1888     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1889     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1890   }
1891 
1892   assert((End - d) % 4 == 0 &&
1893          "Wrong data length in HeaderFileInfo deserialization");
1894   while (d != End) {
1895     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1896     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1897     LocalSMID >>= 2;
1898 
1899     // This header is part of a module. Associate it with the module to enable
1900     // implicit module import.
1901     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1902     Module *Mod = Reader.getSubmodule(GlobalSMID);
1903     FileManager &FileMgr = Reader.getFileManager();
1904     ModuleMap &ModMap =
1905         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1906 
1907     std::string Filename = key.Filename;
1908     if (key.Imported)
1909       Reader.ResolveImportedPath(M, Filename);
1910     // FIXME: This is not always the right filename-as-written, but we're not
1911     // going to use this information to rebuild the module, so it doesn't make
1912     // a lot of difference.
1913     Module::Header H = { key.Filename, *FileMgr.getFile(Filename) };
1914     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1915     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1916   }
1917 
1918   // This HeaderFileInfo was externally loaded.
1919   HFI.External = true;
1920   HFI.IsValid = true;
1921   return HFI;
1922 }
1923 
1924 void ASTReader::addPendingMacro(IdentifierInfo *II,
1925                                 ModuleFile *M,
1926                                 uint64_t MacroDirectivesOffset) {
1927   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1928   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1929 }
1930 
1931 void ASTReader::ReadDefinedMacros() {
1932   // Note that we are loading defined macros.
1933   Deserializing Macros(this);
1934 
1935   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1936     BitstreamCursor &MacroCursor = I.MacroCursor;
1937 
1938     // If there was no preprocessor block, skip this file.
1939     if (MacroCursor.getBitcodeBytes().empty())
1940       continue;
1941 
1942     BitstreamCursor Cursor = MacroCursor;
1943     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1944       Error(std::move(Err));
1945       return;
1946     }
1947 
1948     RecordData Record;
1949     while (true) {
1950       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1951       if (!MaybeE) {
1952         Error(MaybeE.takeError());
1953         return;
1954       }
1955       llvm::BitstreamEntry E = MaybeE.get();
1956 
1957       switch (E.Kind) {
1958       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1959       case llvm::BitstreamEntry::Error:
1960         Error("malformed block record in AST file");
1961         return;
1962       case llvm::BitstreamEntry::EndBlock:
1963         goto NextCursor;
1964 
1965       case llvm::BitstreamEntry::Record: {
1966         Record.clear();
1967         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1968         if (!MaybeRecord) {
1969           Error(MaybeRecord.takeError());
1970           return;
1971         }
1972         switch (MaybeRecord.get()) {
1973         default:  // Default behavior: ignore.
1974           break;
1975 
1976         case PP_MACRO_OBJECT_LIKE:
1977         case PP_MACRO_FUNCTION_LIKE: {
1978           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1979           if (II->isOutOfDate())
1980             updateOutOfDateIdentifier(*II);
1981           break;
1982         }
1983 
1984         case PP_TOKEN:
1985           // Ignore tokens.
1986           break;
1987         }
1988         break;
1989       }
1990       }
1991     }
1992     NextCursor:  ;
1993   }
1994 }
1995 
1996 namespace {
1997 
1998   /// Visitor class used to look up identifirs in an AST file.
1999   class IdentifierLookupVisitor {
2000     StringRef Name;
2001     unsigned NameHash;
2002     unsigned PriorGeneration;
2003     unsigned &NumIdentifierLookups;
2004     unsigned &NumIdentifierLookupHits;
2005     IdentifierInfo *Found = nullptr;
2006 
2007   public:
2008     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2009                             unsigned &NumIdentifierLookups,
2010                             unsigned &NumIdentifierLookupHits)
2011       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2012         PriorGeneration(PriorGeneration),
2013         NumIdentifierLookups(NumIdentifierLookups),
2014         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2015 
2016     bool operator()(ModuleFile &M) {
2017       // If we've already searched this module file, skip it now.
2018       if (M.Generation <= PriorGeneration)
2019         return true;
2020 
2021       ASTIdentifierLookupTable *IdTable
2022         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2023       if (!IdTable)
2024         return false;
2025 
2026       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2027                                      Found);
2028       ++NumIdentifierLookups;
2029       ASTIdentifierLookupTable::iterator Pos =
2030           IdTable->find_hashed(Name, NameHash, &Trait);
2031       if (Pos == IdTable->end())
2032         return false;
2033 
2034       // Dereferencing the iterator has the effect of building the
2035       // IdentifierInfo node and populating it with the various
2036       // declarations it needs.
2037       ++NumIdentifierLookupHits;
2038       Found = *Pos;
2039       return true;
2040     }
2041 
2042     // Retrieve the identifier info found within the module
2043     // files.
2044     IdentifierInfo *getIdentifierInfo() const { return Found; }
2045   };
2046 
2047 } // namespace
2048 
2049 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2050   // Note that we are loading an identifier.
2051   Deserializing AnIdentifier(this);
2052 
2053   unsigned PriorGeneration = 0;
2054   if (getContext().getLangOpts().Modules)
2055     PriorGeneration = IdentifierGeneration[&II];
2056 
2057   // If there is a global index, look there first to determine which modules
2058   // provably do not have any results for this identifier.
2059   GlobalModuleIndex::HitSet Hits;
2060   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2061   if (!loadGlobalIndex()) {
2062     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2063       HitsPtr = &Hits;
2064     }
2065   }
2066 
2067   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2068                                   NumIdentifierLookups,
2069                                   NumIdentifierLookupHits);
2070   ModuleMgr.visit(Visitor, HitsPtr);
2071   markIdentifierUpToDate(&II);
2072 }
2073 
2074 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2075   if (!II)
2076     return;
2077 
2078   II->setOutOfDate(false);
2079 
2080   // Update the generation for this identifier.
2081   if (getContext().getLangOpts().Modules)
2082     IdentifierGeneration[II] = getGeneration();
2083 }
2084 
2085 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2086                                     const PendingMacroInfo &PMInfo) {
2087   ModuleFile &M = *PMInfo.M;
2088 
2089   BitstreamCursor &Cursor = M.MacroCursor;
2090   SavedStreamPosition SavedPosition(Cursor);
2091   if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2092     Error(std::move(Err));
2093     return;
2094   }
2095 
2096   struct ModuleMacroRecord {
2097     SubmoduleID SubModID;
2098     MacroInfo *MI;
2099     SmallVector<SubmoduleID, 8> Overrides;
2100   };
2101   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2102 
2103   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2104   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2105   // macro histroy.
2106   RecordData Record;
2107   while (true) {
2108     Expected<llvm::BitstreamEntry> MaybeEntry =
2109         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2110     if (!MaybeEntry) {
2111       Error(MaybeEntry.takeError());
2112       return;
2113     }
2114     llvm::BitstreamEntry Entry = MaybeEntry.get();
2115 
2116     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2117       Error("malformed block record in AST file");
2118       return;
2119     }
2120 
2121     Record.clear();
2122     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2123     if (!MaybePP) {
2124       Error(MaybePP.takeError());
2125       return;
2126     }
2127     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2128     case PP_MACRO_DIRECTIVE_HISTORY:
2129       break;
2130 
2131     case PP_MODULE_MACRO: {
2132       ModuleMacros.push_back(ModuleMacroRecord());
2133       auto &Info = ModuleMacros.back();
2134       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2135       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2136       for (int I = 2, N = Record.size(); I != N; ++I)
2137         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2138       continue;
2139     }
2140 
2141     default:
2142       Error("malformed block record in AST file");
2143       return;
2144     }
2145 
2146     // We found the macro directive history; that's the last record
2147     // for this macro.
2148     break;
2149   }
2150 
2151   // Module macros are listed in reverse dependency order.
2152   {
2153     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2154     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2155     for (auto &MMR : ModuleMacros) {
2156       Overrides.clear();
2157       for (unsigned ModID : MMR.Overrides) {
2158         Module *Mod = getSubmodule(ModID);
2159         auto *Macro = PP.getModuleMacro(Mod, II);
2160         assert(Macro && "missing definition for overridden macro");
2161         Overrides.push_back(Macro);
2162       }
2163 
2164       bool Inserted = false;
2165       Module *Owner = getSubmodule(MMR.SubModID);
2166       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2167     }
2168   }
2169 
2170   // Don't read the directive history for a module; we don't have anywhere
2171   // to put it.
2172   if (M.isModule())
2173     return;
2174 
2175   // Deserialize the macro directives history in reverse source-order.
2176   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2177   unsigned Idx = 0, N = Record.size();
2178   while (Idx < N) {
2179     MacroDirective *MD = nullptr;
2180     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2181     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2182     switch (K) {
2183     case MacroDirective::MD_Define: {
2184       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2185       MD = PP.AllocateDefMacroDirective(MI, Loc);
2186       break;
2187     }
2188     case MacroDirective::MD_Undefine:
2189       MD = PP.AllocateUndefMacroDirective(Loc);
2190       break;
2191     case MacroDirective::MD_Visibility:
2192       bool isPublic = Record[Idx++];
2193       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2194       break;
2195     }
2196 
2197     if (!Latest)
2198       Latest = MD;
2199     if (Earliest)
2200       Earliest->setPrevious(MD);
2201     Earliest = MD;
2202   }
2203 
2204   if (Latest)
2205     PP.setLoadedMacroDirective(II, Earliest, Latest);
2206 }
2207 
2208 ASTReader::InputFileInfo
2209 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2210   // Go find this input file.
2211   BitstreamCursor &Cursor = F.InputFilesCursor;
2212   SavedStreamPosition SavedPosition(Cursor);
2213   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2214     // FIXME this drops errors on the floor.
2215     consumeError(std::move(Err));
2216   }
2217 
2218   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2219   if (!MaybeCode) {
2220     // FIXME this drops errors on the floor.
2221     consumeError(MaybeCode.takeError());
2222   }
2223   unsigned Code = MaybeCode.get();
2224   RecordData Record;
2225   StringRef Blob;
2226 
2227   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2228     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2229            "invalid record type for input file");
2230   else {
2231     // FIXME this drops errors on the floor.
2232     consumeError(Maybe.takeError());
2233   }
2234 
2235   assert(Record[0] == ID && "Bogus stored ID or offset");
2236   InputFileInfo R;
2237   R.StoredSize = static_cast<off_t>(Record[1]);
2238   R.StoredTime = static_cast<time_t>(Record[2]);
2239   R.Overridden = static_cast<bool>(Record[3]);
2240   R.Transient = static_cast<bool>(Record[4]);
2241   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2242   R.Filename = Blob;
2243   ResolveImportedPath(F, R.Filename);
2244   return R;
2245 }
2246 
2247 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2248 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2249   // If this ID is bogus, just return an empty input file.
2250   if (ID == 0 || ID > F.InputFilesLoaded.size())
2251     return InputFile();
2252 
2253   // If we've already loaded this input file, return it.
2254   if (F.InputFilesLoaded[ID-1].getFile())
2255     return F.InputFilesLoaded[ID-1];
2256 
2257   if (F.InputFilesLoaded[ID-1].isNotFound())
2258     return InputFile();
2259 
2260   // Go find this input file.
2261   BitstreamCursor &Cursor = F.InputFilesCursor;
2262   SavedStreamPosition SavedPosition(Cursor);
2263   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2264     // FIXME this drops errors on the floor.
2265     consumeError(std::move(Err));
2266   }
2267 
2268   InputFileInfo FI = readInputFileInfo(F, ID);
2269   off_t StoredSize = FI.StoredSize;
2270   time_t StoredTime = FI.StoredTime;
2271   bool Overridden = FI.Overridden;
2272   bool Transient = FI.Transient;
2273   StringRef Filename = FI.Filename;
2274 
2275   const FileEntry *File = nullptr;
2276   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2277     File = *FE;
2278 
2279   // If we didn't find the file, resolve it relative to the
2280   // original directory from which this AST file was created.
2281   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2282       F.OriginalDir != F.BaseDirectory) {
2283     std::string Resolved = resolveFileRelativeToOriginalDir(
2284         Filename, F.OriginalDir, F.BaseDirectory);
2285     if (!Resolved.empty())
2286       if (auto FE = FileMgr.getFile(Resolved))
2287         File = *FE;
2288   }
2289 
2290   // For an overridden file, create a virtual file with the stored
2291   // size/timestamp.
2292   if ((Overridden || Transient) && File == nullptr)
2293     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2294 
2295   if (File == nullptr) {
2296     if (Complain) {
2297       std::string ErrorStr = "could not find file '";
2298       ErrorStr += Filename;
2299       ErrorStr += "' referenced by AST file '";
2300       ErrorStr += F.FileName;
2301       ErrorStr += "'";
2302       Error(ErrorStr);
2303     }
2304     // Record that we didn't find the file.
2305     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2306     return InputFile();
2307   }
2308 
2309   // Check if there was a request to override the contents of the file
2310   // that was part of the precompiled header. Overriding such a file
2311   // can lead to problems when lexing using the source locations from the
2312   // PCH.
2313   SourceManager &SM = getSourceManager();
2314   // FIXME: Reject if the overrides are different.
2315   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2316     if (Complain)
2317       Error(diag::err_fe_pch_file_overridden, Filename);
2318 
2319     // After emitting the diagnostic, bypass the overriding file to recover
2320     // (this creates a separate FileEntry).
2321     File = SM.bypassFileContentsOverride(*File);
2322     if (!File) {
2323       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2324       return InputFile();
2325     }
2326   }
2327 
2328   bool IsOutOfDate = false;
2329 
2330   // For an overridden file, there is nothing to validate.
2331   if (!Overridden && //
2332       (StoredSize != File->getSize() ||
2333        (StoredTime && StoredTime != File->getModificationTime() &&
2334         !DisableValidation)
2335        )) {
2336     if (Complain) {
2337       // Build a list of the PCH imports that got us here (in reverse).
2338       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2339       while (!ImportStack.back()->ImportedBy.empty())
2340         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2341 
2342       // The top-level PCH is stale.
2343       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2344       unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2345       if (DiagnosticKind == 0)
2346         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2347       else if (DiagnosticKind == 1)
2348         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2349       else
2350         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
2351 
2352       // Print the import stack.
2353       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2354         Diag(diag::note_pch_required_by)
2355           << Filename << ImportStack[0]->FileName;
2356         for (unsigned I = 1; I < ImportStack.size(); ++I)
2357           Diag(diag::note_pch_required_by)
2358             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2359       }
2360 
2361       if (!Diags.isDiagnosticInFlight())
2362         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2363     }
2364 
2365     IsOutOfDate = true;
2366   }
2367   // FIXME: If the file is overridden and we've already opened it,
2368   // issue an error (or split it into a separate FileEntry).
2369 
2370   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2371 
2372   // Note that we've loaded this input file.
2373   F.InputFilesLoaded[ID-1] = IF;
2374   return IF;
2375 }
2376 
2377 /// If we are loading a relocatable PCH or module file, and the filename
2378 /// is not an absolute path, add the system or module root to the beginning of
2379 /// the file name.
2380 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2381   // Resolve relative to the base directory, if we have one.
2382   if (!M.BaseDirectory.empty())
2383     return ResolveImportedPath(Filename, M.BaseDirectory);
2384 }
2385 
2386 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2387   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2388     return;
2389 
2390   SmallString<128> Buffer;
2391   llvm::sys::path::append(Buffer, Prefix, Filename);
2392   Filename.assign(Buffer.begin(), Buffer.end());
2393 }
2394 
2395 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2396   switch (ARR) {
2397   case ASTReader::Failure: return true;
2398   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2399   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2400   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2401   case ASTReader::ConfigurationMismatch:
2402     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2403   case ASTReader::HadErrors: return true;
2404   case ASTReader::Success: return false;
2405   }
2406 
2407   llvm_unreachable("unknown ASTReadResult");
2408 }
2409 
2410 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2411     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2412     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2413     std::string &SuggestedPredefines) {
2414   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2415     // FIXME this drops errors on the floor.
2416     consumeError(std::move(Err));
2417     return Failure;
2418   }
2419 
2420   // Read all of the records in the options block.
2421   RecordData Record;
2422   ASTReadResult Result = Success;
2423   while (true) {
2424     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2425     if (!MaybeEntry) {
2426       // FIXME this drops errors on the floor.
2427       consumeError(MaybeEntry.takeError());
2428       return Failure;
2429     }
2430     llvm::BitstreamEntry Entry = MaybeEntry.get();
2431 
2432     switch (Entry.Kind) {
2433     case llvm::BitstreamEntry::Error:
2434     case llvm::BitstreamEntry::SubBlock:
2435       return Failure;
2436 
2437     case llvm::BitstreamEntry::EndBlock:
2438       return Result;
2439 
2440     case llvm::BitstreamEntry::Record:
2441       // The interesting case.
2442       break;
2443     }
2444 
2445     // Read and process a record.
2446     Record.clear();
2447     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2448     if (!MaybeRecordType) {
2449       // FIXME this drops errors on the floor.
2450       consumeError(MaybeRecordType.takeError());
2451       return Failure;
2452     }
2453     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2454     case LANGUAGE_OPTIONS: {
2455       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2456       if (ParseLanguageOptions(Record, Complain, Listener,
2457                                AllowCompatibleConfigurationMismatch))
2458         Result = ConfigurationMismatch;
2459       break;
2460     }
2461 
2462     case TARGET_OPTIONS: {
2463       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2464       if (ParseTargetOptions(Record, Complain, Listener,
2465                              AllowCompatibleConfigurationMismatch))
2466         Result = ConfigurationMismatch;
2467       break;
2468     }
2469 
2470     case FILE_SYSTEM_OPTIONS: {
2471       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2472       if (!AllowCompatibleConfigurationMismatch &&
2473           ParseFileSystemOptions(Record, Complain, Listener))
2474         Result = ConfigurationMismatch;
2475       break;
2476     }
2477 
2478     case HEADER_SEARCH_OPTIONS: {
2479       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2480       if (!AllowCompatibleConfigurationMismatch &&
2481           ParseHeaderSearchOptions(Record, Complain, Listener))
2482         Result = ConfigurationMismatch;
2483       break;
2484     }
2485 
2486     case PREPROCESSOR_OPTIONS:
2487       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2488       if (!AllowCompatibleConfigurationMismatch &&
2489           ParsePreprocessorOptions(Record, Complain, Listener,
2490                                    SuggestedPredefines))
2491         Result = ConfigurationMismatch;
2492       break;
2493     }
2494   }
2495 }
2496 
2497 ASTReader::ASTReadResult
2498 ASTReader::ReadControlBlock(ModuleFile &F,
2499                             SmallVectorImpl<ImportedModule> &Loaded,
2500                             const ModuleFile *ImportedBy,
2501                             unsigned ClientLoadCapabilities) {
2502   BitstreamCursor &Stream = F.Stream;
2503   ASTReadResult Result = Success;
2504 
2505   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2506     Error(std::move(Err));
2507     return Failure;
2508   }
2509 
2510   // Lambda to read the unhashed control block the first time it's called.
2511   //
2512   // For PCM files, the unhashed control block cannot be read until after the
2513   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2514   // need to look ahead before reading the IMPORTS record.  For consistency,
2515   // this block is always read somehow (see BitstreamEntry::EndBlock).
2516   bool HasReadUnhashedControlBlock = false;
2517   auto readUnhashedControlBlockOnce = [&]() {
2518     if (!HasReadUnhashedControlBlock) {
2519       HasReadUnhashedControlBlock = true;
2520       if (ASTReadResult Result =
2521               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2522         return Result;
2523     }
2524     return Success;
2525   };
2526 
2527   // Read all of the records and blocks in the control block.
2528   RecordData Record;
2529   unsigned NumInputs = 0;
2530   unsigned NumUserInputs = 0;
2531   StringRef BaseDirectoryAsWritten;
2532   while (true) {
2533     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2534     if (!MaybeEntry) {
2535       Error(MaybeEntry.takeError());
2536       return Failure;
2537     }
2538     llvm::BitstreamEntry Entry = MaybeEntry.get();
2539 
2540     switch (Entry.Kind) {
2541     case llvm::BitstreamEntry::Error:
2542       Error("malformed block record in AST file");
2543       return Failure;
2544     case llvm::BitstreamEntry::EndBlock: {
2545       // Validate the module before returning.  This call catches an AST with
2546       // no module name and no imports.
2547       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2548         return Result;
2549 
2550       // Validate input files.
2551       const HeaderSearchOptions &HSOpts =
2552           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2553 
2554       // All user input files reside at the index range [0, NumUserInputs), and
2555       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2556       // loaded module files, ignore missing inputs.
2557       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2558           F.Kind != MK_PrebuiltModule) {
2559         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2560 
2561         // If we are reading a module, we will create a verification timestamp,
2562         // so we verify all input files.  Otherwise, verify only user input
2563         // files.
2564 
2565         unsigned N = NumUserInputs;
2566         if (ValidateSystemInputs ||
2567             (HSOpts.ModulesValidateOncePerBuildSession &&
2568              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2569              F.Kind == MK_ImplicitModule))
2570           N = NumInputs;
2571 
2572         for (unsigned I = 0; I < N; ++I) {
2573           InputFile IF = getInputFile(F, I+1, Complain);
2574           if (!IF.getFile() || IF.isOutOfDate())
2575             return OutOfDate;
2576         }
2577       }
2578 
2579       if (Listener)
2580         Listener->visitModuleFile(F.FileName, F.Kind);
2581 
2582       if (Listener && Listener->needsInputFileVisitation()) {
2583         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2584                                                                 : NumUserInputs;
2585         for (unsigned I = 0; I < N; ++I) {
2586           bool IsSystem = I >= NumUserInputs;
2587           InputFileInfo FI = readInputFileInfo(F, I+1);
2588           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2589                                    F.Kind == MK_ExplicitModule ||
2590                                    F.Kind == MK_PrebuiltModule);
2591         }
2592       }
2593 
2594       return Result;
2595     }
2596 
2597     case llvm::BitstreamEntry::SubBlock:
2598       switch (Entry.ID) {
2599       case INPUT_FILES_BLOCK_ID:
2600         F.InputFilesCursor = Stream;
2601         if (llvm::Error Err = Stream.SkipBlock()) {
2602           Error(std::move(Err));
2603           return Failure;
2604         }
2605         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2606           Error("malformed block record in AST file");
2607           return Failure;
2608         }
2609         continue;
2610 
2611       case OPTIONS_BLOCK_ID:
2612         // If we're reading the first module for this group, check its options
2613         // are compatible with ours. For modules it imports, no further checking
2614         // is required, because we checked them when we built it.
2615         if (Listener && !ImportedBy) {
2616           // Should we allow the configuration of the module file to differ from
2617           // the configuration of the current translation unit in a compatible
2618           // way?
2619           //
2620           // FIXME: Allow this for files explicitly specified with -include-pch.
2621           bool AllowCompatibleConfigurationMismatch =
2622               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2623 
2624           Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2625                                     AllowCompatibleConfigurationMismatch,
2626                                     *Listener, SuggestedPredefines);
2627           if (Result == Failure) {
2628             Error("malformed block record in AST file");
2629             return Result;
2630           }
2631 
2632           if (DisableValidation ||
2633               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2634             Result = Success;
2635 
2636           // If we can't load the module, exit early since we likely
2637           // will rebuild the module anyway. The stream may be in the
2638           // middle of a block.
2639           if (Result != Success)
2640             return Result;
2641         } else if (llvm::Error Err = Stream.SkipBlock()) {
2642           Error(std::move(Err));
2643           return Failure;
2644         }
2645         continue;
2646 
2647       default:
2648         if (llvm::Error Err = Stream.SkipBlock()) {
2649           Error(std::move(Err));
2650           return Failure;
2651         }
2652         continue;
2653       }
2654 
2655     case llvm::BitstreamEntry::Record:
2656       // The interesting case.
2657       break;
2658     }
2659 
2660     // Read and process a record.
2661     Record.clear();
2662     StringRef Blob;
2663     Expected<unsigned> MaybeRecordType =
2664         Stream.readRecord(Entry.ID, Record, &Blob);
2665     if (!MaybeRecordType) {
2666       Error(MaybeRecordType.takeError());
2667       return Failure;
2668     }
2669     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2670     case METADATA: {
2671       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2672         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2673           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2674                                         : diag::err_pch_version_too_new);
2675         return VersionMismatch;
2676       }
2677 
2678       bool hasErrors = Record[7];
2679       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2680         Diag(diag::err_pch_with_compiler_errors);
2681         return HadErrors;
2682       }
2683       if (hasErrors) {
2684         Diags.ErrorOccurred = true;
2685         Diags.UncompilableErrorOccurred = true;
2686         Diags.UnrecoverableErrorOccurred = true;
2687       }
2688 
2689       F.RelocatablePCH = Record[4];
2690       // Relative paths in a relocatable PCH are relative to our sysroot.
2691       if (F.RelocatablePCH)
2692         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2693 
2694       F.HasTimestamps = Record[5];
2695 
2696       F.PCHHasObjectFile = Record[6];
2697 
2698       const std::string &CurBranch = getClangFullRepositoryVersion();
2699       StringRef ASTBranch = Blob;
2700       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2701         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2702           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2703         return VersionMismatch;
2704       }
2705       break;
2706     }
2707 
2708     case IMPORTS: {
2709       // Validate the AST before processing any imports (otherwise, untangling
2710       // them can be error-prone and expensive).  A module will have a name and
2711       // will already have been validated, but this catches the PCH case.
2712       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2713         return Result;
2714 
2715       // Load each of the imported PCH files.
2716       unsigned Idx = 0, N = Record.size();
2717       while (Idx < N) {
2718         // Read information about the AST file.
2719         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2720         // The import location will be the local one for now; we will adjust
2721         // all import locations of module imports after the global source
2722         // location info are setup, in ReadAST.
2723         SourceLocation ImportLoc =
2724             ReadUntranslatedSourceLocation(Record[Idx++]);
2725         off_t StoredSize = (off_t)Record[Idx++];
2726         time_t StoredModTime = (time_t)Record[Idx++];
2727         ASTFileSignature StoredSignature = {
2728             {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2729               (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2730               (uint32_t)Record[Idx++]}}};
2731 
2732         std::string ImportedName = ReadString(Record, Idx);
2733         std::string ImportedFile;
2734 
2735         // For prebuilt and explicit modules first consult the file map for
2736         // an override. Note that here we don't search prebuilt module
2737         // directories, only the explicit name to file mappings. Also, we will
2738         // still verify the size/signature making sure it is essentially the
2739         // same file but perhaps in a different location.
2740         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2741           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2742             ImportedName, /*FileMapOnly*/ true);
2743 
2744         if (ImportedFile.empty())
2745           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2746           // ModuleCache as when writing.
2747           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2748         else
2749           SkipPath(Record, Idx);
2750 
2751         // If our client can't cope with us being out of date, we can't cope with
2752         // our dependency being missing.
2753         unsigned Capabilities = ClientLoadCapabilities;
2754         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2755           Capabilities &= ~ARR_Missing;
2756 
2757         // Load the AST file.
2758         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2759                                   Loaded, StoredSize, StoredModTime,
2760                                   StoredSignature, Capabilities);
2761 
2762         // If we diagnosed a problem, produce a backtrace.
2763         if (isDiagnosedResult(Result, Capabilities))
2764           Diag(diag::note_module_file_imported_by)
2765               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2766 
2767         switch (Result) {
2768         case Failure: return Failure;
2769           // If we have to ignore the dependency, we'll have to ignore this too.
2770         case Missing:
2771         case OutOfDate: return OutOfDate;
2772         case VersionMismatch: return VersionMismatch;
2773         case ConfigurationMismatch: return ConfigurationMismatch;
2774         case HadErrors: return HadErrors;
2775         case Success: break;
2776         }
2777       }
2778       break;
2779     }
2780 
2781     case ORIGINAL_FILE:
2782       F.OriginalSourceFileID = FileID::get(Record[0]);
2783       F.ActualOriginalSourceFileName = Blob;
2784       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2785       ResolveImportedPath(F, F.OriginalSourceFileName);
2786       break;
2787 
2788     case ORIGINAL_FILE_ID:
2789       F.OriginalSourceFileID = FileID::get(Record[0]);
2790       break;
2791 
2792     case ORIGINAL_PCH_DIR:
2793       F.OriginalDir = Blob;
2794       break;
2795 
2796     case MODULE_NAME:
2797       F.ModuleName = Blob;
2798       Diag(diag::remark_module_import)
2799           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2800           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2801       if (Listener)
2802         Listener->ReadModuleName(F.ModuleName);
2803 
2804       // Validate the AST as soon as we have a name so we can exit early on
2805       // failure.
2806       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2807         return Result;
2808 
2809       break;
2810 
2811     case MODULE_DIRECTORY: {
2812       // Save the BaseDirectory as written in the PCM for computing the module
2813       // filename for the ModuleCache.
2814       BaseDirectoryAsWritten = Blob;
2815       assert(!F.ModuleName.empty() &&
2816              "MODULE_DIRECTORY found before MODULE_NAME");
2817       // If we've already loaded a module map file covering this module, we may
2818       // have a better path for it (relative to the current build).
2819       Module *M = PP.getHeaderSearchInfo().lookupModule(
2820           F.ModuleName, /*AllowSearch*/ true,
2821           /*AllowExtraModuleMapSearch*/ true);
2822       if (M && M->Directory) {
2823         // If we're implicitly loading a module, the base directory can't
2824         // change between the build and use.
2825         // Don't emit module relocation error if we have -fno-validate-pch
2826         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2827             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2828           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2829           if (!BuildDir || *BuildDir != M->Directory) {
2830             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2831               Diag(diag::err_imported_module_relocated)
2832                   << F.ModuleName << Blob << M->Directory->getName();
2833             return OutOfDate;
2834           }
2835         }
2836         F.BaseDirectory = M->Directory->getName();
2837       } else {
2838         F.BaseDirectory = Blob;
2839       }
2840       break;
2841     }
2842 
2843     case MODULE_MAP_FILE:
2844       if (ASTReadResult Result =
2845               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2846         return Result;
2847       break;
2848 
2849     case INPUT_FILE_OFFSETS:
2850       NumInputs = Record[0];
2851       NumUserInputs = Record[1];
2852       F.InputFileOffsets =
2853           (const llvm::support::unaligned_uint64_t *)Blob.data();
2854       F.InputFilesLoaded.resize(NumInputs);
2855       F.NumUserInputFiles = NumUserInputs;
2856       break;
2857     }
2858   }
2859 }
2860 
2861 ASTReader::ASTReadResult
2862 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2863   BitstreamCursor &Stream = F.Stream;
2864 
2865   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2866     Error(std::move(Err));
2867     return Failure;
2868   }
2869 
2870   // Read all of the records and blocks for the AST file.
2871   RecordData Record;
2872   while (true) {
2873     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2874     if (!MaybeEntry) {
2875       Error(MaybeEntry.takeError());
2876       return Failure;
2877     }
2878     llvm::BitstreamEntry Entry = MaybeEntry.get();
2879 
2880     switch (Entry.Kind) {
2881     case llvm::BitstreamEntry::Error:
2882       Error("error at end of module block in AST file");
2883       return Failure;
2884     case llvm::BitstreamEntry::EndBlock:
2885       // Outside of C++, we do not store a lookup map for the translation unit.
2886       // Instead, mark it as needing a lookup map to be built if this module
2887       // contains any declarations lexically within it (which it always does!).
2888       // This usually has no cost, since we very rarely need the lookup map for
2889       // the translation unit outside C++.
2890       if (ASTContext *Ctx = ContextObj) {
2891         DeclContext *DC = Ctx->getTranslationUnitDecl();
2892         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2893           DC->setMustBuildLookupTable();
2894       }
2895 
2896       return Success;
2897     case llvm::BitstreamEntry::SubBlock:
2898       switch (Entry.ID) {
2899       case DECLTYPES_BLOCK_ID:
2900         // We lazily load the decls block, but we want to set up the
2901         // DeclsCursor cursor to point into it.  Clone our current bitcode
2902         // cursor to it, enter the block and read the abbrevs in that block.
2903         // With the main cursor, we just skip over it.
2904         F.DeclsCursor = Stream;
2905         if (llvm::Error Err = Stream.SkipBlock()) {
2906           Error(std::move(Err));
2907           return Failure;
2908         }
2909         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2910           Error("malformed block record in AST file");
2911           return Failure;
2912         }
2913         break;
2914 
2915       case PREPROCESSOR_BLOCK_ID:
2916         F.MacroCursor = Stream;
2917         if (!PP.getExternalSource())
2918           PP.setExternalSource(this);
2919 
2920         if (llvm::Error Err = Stream.SkipBlock()) {
2921           Error(std::move(Err));
2922           return Failure;
2923         }
2924         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2925           Error("malformed block record in AST file");
2926           return Failure;
2927         }
2928         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2929         break;
2930 
2931       case PREPROCESSOR_DETAIL_BLOCK_ID:
2932         F.PreprocessorDetailCursor = Stream;
2933 
2934         if (llvm::Error Err = Stream.SkipBlock()) {
2935           Error(std::move(Err));
2936           return Failure;
2937         }
2938         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2939                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2940           Error("malformed preprocessor detail record in AST file");
2941           return Failure;
2942         }
2943         F.PreprocessorDetailStartOffset
2944         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2945 
2946         if (!PP.getPreprocessingRecord())
2947           PP.createPreprocessingRecord();
2948         if (!PP.getPreprocessingRecord()->getExternalSource())
2949           PP.getPreprocessingRecord()->SetExternalSource(*this);
2950         break;
2951 
2952       case SOURCE_MANAGER_BLOCK_ID:
2953         if (ReadSourceManagerBlock(F))
2954           return Failure;
2955         break;
2956 
2957       case SUBMODULE_BLOCK_ID:
2958         if (ASTReadResult Result =
2959                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
2960           return Result;
2961         break;
2962 
2963       case COMMENTS_BLOCK_ID: {
2964         BitstreamCursor C = Stream;
2965 
2966         if (llvm::Error Err = Stream.SkipBlock()) {
2967           Error(std::move(Err));
2968           return Failure;
2969         }
2970         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2971           Error("malformed comments block in AST file");
2972           return Failure;
2973         }
2974         CommentsCursors.push_back(std::make_pair(C, &F));
2975         break;
2976       }
2977 
2978       default:
2979         if (llvm::Error Err = Stream.SkipBlock()) {
2980           Error(std::move(Err));
2981           return Failure;
2982         }
2983         break;
2984       }
2985       continue;
2986 
2987     case llvm::BitstreamEntry::Record:
2988       // The interesting case.
2989       break;
2990     }
2991 
2992     // Read and process a record.
2993     Record.clear();
2994     StringRef Blob;
2995     Expected<unsigned> MaybeRecordType =
2996         Stream.readRecord(Entry.ID, Record, &Blob);
2997     if (!MaybeRecordType) {
2998       Error(MaybeRecordType.takeError());
2999       return Failure;
3000     }
3001     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3002 
3003     // If we're not loading an AST context, we don't care about most records.
3004     if (!ContextObj) {
3005       switch (RecordType) {
3006       case IDENTIFIER_TABLE:
3007       case IDENTIFIER_OFFSET:
3008       case INTERESTING_IDENTIFIERS:
3009       case STATISTICS:
3010       case PP_CONDITIONAL_STACK:
3011       case PP_COUNTER_VALUE:
3012       case SOURCE_LOCATION_OFFSETS:
3013       case MODULE_OFFSET_MAP:
3014       case SOURCE_MANAGER_LINE_TABLE:
3015       case SOURCE_LOCATION_PRELOADS:
3016       case PPD_ENTITIES_OFFSETS:
3017       case HEADER_SEARCH_TABLE:
3018       case IMPORTED_MODULES:
3019       case MACRO_OFFSET:
3020         break;
3021       default:
3022         continue;
3023       }
3024     }
3025 
3026     switch (RecordType) {
3027     default:  // Default behavior: ignore.
3028       break;
3029 
3030     case TYPE_OFFSET: {
3031       if (F.LocalNumTypes != 0) {
3032         Error("duplicate TYPE_OFFSET record in AST file");
3033         return Failure;
3034       }
3035       F.TypeOffsets = (const uint32_t *)Blob.data();
3036       F.LocalNumTypes = Record[0];
3037       unsigned LocalBaseTypeIndex = Record[1];
3038       F.BaseTypeIndex = getTotalNumTypes();
3039 
3040       if (F.LocalNumTypes > 0) {
3041         // Introduce the global -> local mapping for types within this module.
3042         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3043 
3044         // Introduce the local -> global mapping for types within this module.
3045         F.TypeRemap.insertOrReplace(
3046           std::make_pair(LocalBaseTypeIndex,
3047                          F.BaseTypeIndex - LocalBaseTypeIndex));
3048 
3049         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3050       }
3051       break;
3052     }
3053 
3054     case DECL_OFFSET: {
3055       if (F.LocalNumDecls != 0) {
3056         Error("duplicate DECL_OFFSET record in AST file");
3057         return Failure;
3058       }
3059       F.DeclOffsets = (const DeclOffset *)Blob.data();
3060       F.LocalNumDecls = Record[0];
3061       unsigned LocalBaseDeclID = Record[1];
3062       F.BaseDeclID = getTotalNumDecls();
3063 
3064       if (F.LocalNumDecls > 0) {
3065         // Introduce the global -> local mapping for declarations within this
3066         // module.
3067         GlobalDeclMap.insert(
3068           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3069 
3070         // Introduce the local -> global mapping for declarations within this
3071         // module.
3072         F.DeclRemap.insertOrReplace(
3073           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3074 
3075         // Introduce the global -> local mapping for declarations within this
3076         // module.
3077         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3078 
3079         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3080       }
3081       break;
3082     }
3083 
3084     case TU_UPDATE_LEXICAL: {
3085       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3086       LexicalContents Contents(
3087           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3088               Blob.data()),
3089           static_cast<unsigned int>(Blob.size() / 4));
3090       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3091       TU->setHasExternalLexicalStorage(true);
3092       break;
3093     }
3094 
3095     case UPDATE_VISIBLE: {
3096       unsigned Idx = 0;
3097       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3098       auto *Data = (const unsigned char*)Blob.data();
3099       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3100       // If we've already loaded the decl, perform the updates when we finish
3101       // loading this block.
3102       if (Decl *D = GetExistingDecl(ID))
3103         PendingUpdateRecords.push_back(
3104             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3105       break;
3106     }
3107 
3108     case IDENTIFIER_TABLE:
3109       F.IdentifierTableData = Blob.data();
3110       if (Record[0]) {
3111         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3112             (const unsigned char *)F.IdentifierTableData + Record[0],
3113             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3114             (const unsigned char *)F.IdentifierTableData,
3115             ASTIdentifierLookupTrait(*this, F));
3116 
3117         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3118       }
3119       break;
3120 
3121     case IDENTIFIER_OFFSET: {
3122       if (F.LocalNumIdentifiers != 0) {
3123         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3124         return Failure;
3125       }
3126       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3127       F.LocalNumIdentifiers = Record[0];
3128       unsigned LocalBaseIdentifierID = Record[1];
3129       F.BaseIdentifierID = getTotalNumIdentifiers();
3130 
3131       if (F.LocalNumIdentifiers > 0) {
3132         // Introduce the global -> local mapping for identifiers within this
3133         // module.
3134         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3135                                                   &F));
3136 
3137         // Introduce the local -> global mapping for identifiers within this
3138         // module.
3139         F.IdentifierRemap.insertOrReplace(
3140           std::make_pair(LocalBaseIdentifierID,
3141                          F.BaseIdentifierID - LocalBaseIdentifierID));
3142 
3143         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3144                                  + F.LocalNumIdentifiers);
3145       }
3146       break;
3147     }
3148 
3149     case INTERESTING_IDENTIFIERS:
3150       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3151       break;
3152 
3153     case EAGERLY_DESERIALIZED_DECLS:
3154       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3155       // about "interesting" decls (for instance, if we're building a module).
3156       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3157         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3158       break;
3159 
3160     case MODULAR_CODEGEN_DECLS:
3161       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3162       // them (ie: if we're not codegenerating this module).
3163       if (F.Kind == MK_MainFile)
3164         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3165           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3166       break;
3167 
3168     case SPECIAL_TYPES:
3169       if (SpecialTypes.empty()) {
3170         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3171           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3172         break;
3173       }
3174 
3175       if (SpecialTypes.size() != Record.size()) {
3176         Error("invalid special-types record");
3177         return Failure;
3178       }
3179 
3180       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3181         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3182         if (!SpecialTypes[I])
3183           SpecialTypes[I] = ID;
3184         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3185         // merge step?
3186       }
3187       break;
3188 
3189     case STATISTICS:
3190       TotalNumStatements += Record[0];
3191       TotalNumMacros += Record[1];
3192       TotalLexicalDeclContexts += Record[2];
3193       TotalVisibleDeclContexts += Record[3];
3194       break;
3195 
3196     case UNUSED_FILESCOPED_DECLS:
3197       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3198         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3199       break;
3200 
3201     case DELEGATING_CTORS:
3202       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3203         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3204       break;
3205 
3206     case WEAK_UNDECLARED_IDENTIFIERS:
3207       if (Record.size() % 4 != 0) {
3208         Error("invalid weak identifiers record");
3209         return Failure;
3210       }
3211 
3212       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3213       // files. This isn't the way to do it :)
3214       WeakUndeclaredIdentifiers.clear();
3215 
3216       // Translate the weak, undeclared identifiers into global IDs.
3217       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3218         WeakUndeclaredIdentifiers.push_back(
3219           getGlobalIdentifierID(F, Record[I++]));
3220         WeakUndeclaredIdentifiers.push_back(
3221           getGlobalIdentifierID(F, Record[I++]));
3222         WeakUndeclaredIdentifiers.push_back(
3223           ReadSourceLocation(F, Record, I).getRawEncoding());
3224         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3225       }
3226       break;
3227 
3228     case SELECTOR_OFFSETS: {
3229       F.SelectorOffsets = (const uint32_t *)Blob.data();
3230       F.LocalNumSelectors = Record[0];
3231       unsigned LocalBaseSelectorID = Record[1];
3232       F.BaseSelectorID = getTotalNumSelectors();
3233 
3234       if (F.LocalNumSelectors > 0) {
3235         // Introduce the global -> local mapping for selectors within this
3236         // module.
3237         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3238 
3239         // Introduce the local -> global mapping for selectors within this
3240         // module.
3241         F.SelectorRemap.insertOrReplace(
3242           std::make_pair(LocalBaseSelectorID,
3243                          F.BaseSelectorID - LocalBaseSelectorID));
3244 
3245         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3246       }
3247       break;
3248     }
3249 
3250     case METHOD_POOL:
3251       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3252       if (Record[0])
3253         F.SelectorLookupTable
3254           = ASTSelectorLookupTable::Create(
3255                         F.SelectorLookupTableData + Record[0],
3256                         F.SelectorLookupTableData,
3257                         ASTSelectorLookupTrait(*this, F));
3258       TotalNumMethodPoolEntries += Record[1];
3259       break;
3260 
3261     case REFERENCED_SELECTOR_POOL:
3262       if (!Record.empty()) {
3263         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3264           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3265                                                                 Record[Idx++]));
3266           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3267                                               getRawEncoding());
3268         }
3269       }
3270       break;
3271 
3272     case PP_CONDITIONAL_STACK:
3273       if (!Record.empty()) {
3274         unsigned Idx = 0, End = Record.size() - 1;
3275         bool ReachedEOFWhileSkipping = Record[Idx++];
3276         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3277         if (ReachedEOFWhileSkipping) {
3278           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3279           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3280           bool FoundNonSkipPortion = Record[Idx++];
3281           bool FoundElse = Record[Idx++];
3282           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3283           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3284                            FoundElse, ElseLoc);
3285         }
3286         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3287         while (Idx < End) {
3288           auto Loc = ReadSourceLocation(F, Record, Idx);
3289           bool WasSkipping = Record[Idx++];
3290           bool FoundNonSkip = Record[Idx++];
3291           bool FoundElse = Record[Idx++];
3292           ConditionalStack.push_back(
3293               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3294         }
3295         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3296       }
3297       break;
3298 
3299     case PP_COUNTER_VALUE:
3300       if (!Record.empty() && Listener)
3301         Listener->ReadCounter(F, Record[0]);
3302       break;
3303 
3304     case FILE_SORTED_DECLS:
3305       F.FileSortedDecls = (const DeclID *)Blob.data();
3306       F.NumFileSortedDecls = Record[0];
3307       break;
3308 
3309     case SOURCE_LOCATION_OFFSETS: {
3310       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3311       F.LocalNumSLocEntries = Record[0];
3312       unsigned SLocSpaceSize = Record[1];
3313       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3314           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3315                                               SLocSpaceSize);
3316       if (!F.SLocEntryBaseID) {
3317         Error("ran out of source locations");
3318         break;
3319       }
3320       // Make our entry in the range map. BaseID is negative and growing, so
3321       // we invert it. Because we invert it, though, we need the other end of
3322       // the range.
3323       unsigned RangeStart =
3324           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3325       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3326       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3327 
3328       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3329       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3330       GlobalSLocOffsetMap.insert(
3331           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3332                            - SLocSpaceSize,&F));
3333 
3334       // Initialize the remapping table.
3335       // Invalid stays invalid.
3336       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3337       // This module. Base was 2 when being compiled.
3338       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3339                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3340 
3341       TotalNumSLocEntries += F.LocalNumSLocEntries;
3342       break;
3343     }
3344 
3345     case MODULE_OFFSET_MAP:
3346       F.ModuleOffsetMap = Blob;
3347       break;
3348 
3349     case SOURCE_MANAGER_LINE_TABLE:
3350       if (ParseLineTable(F, Record))
3351         return Failure;
3352       break;
3353 
3354     case SOURCE_LOCATION_PRELOADS: {
3355       // Need to transform from the local view (1-based IDs) to the global view,
3356       // which is based off F.SLocEntryBaseID.
3357       if (!F.PreloadSLocEntries.empty()) {
3358         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3359         return Failure;
3360       }
3361 
3362       F.PreloadSLocEntries.swap(Record);
3363       break;
3364     }
3365 
3366     case EXT_VECTOR_DECLS:
3367       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3368         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3369       break;
3370 
3371     case VTABLE_USES:
3372       if (Record.size() % 3 != 0) {
3373         Error("Invalid VTABLE_USES record");
3374         return Failure;
3375       }
3376 
3377       // Later tables overwrite earlier ones.
3378       // FIXME: Modules will have some trouble with this. This is clearly not
3379       // the right way to do this.
3380       VTableUses.clear();
3381 
3382       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3383         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3384         VTableUses.push_back(
3385           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3386         VTableUses.push_back(Record[Idx++]);
3387       }
3388       break;
3389 
3390     case PENDING_IMPLICIT_INSTANTIATIONS:
3391       if (PendingInstantiations.size() % 2 != 0) {
3392         Error("Invalid existing PendingInstantiations");
3393         return Failure;
3394       }
3395 
3396       if (Record.size() % 2 != 0) {
3397         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3398         return Failure;
3399       }
3400 
3401       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3402         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3403         PendingInstantiations.push_back(
3404           ReadSourceLocation(F, Record, I).getRawEncoding());
3405       }
3406       break;
3407 
3408     case SEMA_DECL_REFS:
3409       if (Record.size() != 3) {
3410         Error("Invalid SEMA_DECL_REFS block");
3411         return Failure;
3412       }
3413       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3414         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3415       break;
3416 
3417     case PPD_ENTITIES_OFFSETS: {
3418       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3419       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3420       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3421 
3422       unsigned LocalBasePreprocessedEntityID = Record[0];
3423 
3424       unsigned StartingID;
3425       if (!PP.getPreprocessingRecord())
3426         PP.createPreprocessingRecord();
3427       if (!PP.getPreprocessingRecord()->getExternalSource())
3428         PP.getPreprocessingRecord()->SetExternalSource(*this);
3429       StartingID
3430         = PP.getPreprocessingRecord()
3431             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3432       F.BasePreprocessedEntityID = StartingID;
3433 
3434       if (F.NumPreprocessedEntities > 0) {
3435         // Introduce the global -> local mapping for preprocessed entities in
3436         // this module.
3437         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3438 
3439         // Introduce the local -> global mapping for preprocessed entities in
3440         // this module.
3441         F.PreprocessedEntityRemap.insertOrReplace(
3442           std::make_pair(LocalBasePreprocessedEntityID,
3443             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3444       }
3445 
3446       break;
3447     }
3448 
3449     case PPD_SKIPPED_RANGES: {
3450       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3451       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3452       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3453 
3454       if (!PP.getPreprocessingRecord())
3455         PP.createPreprocessingRecord();
3456       if (!PP.getPreprocessingRecord()->getExternalSource())
3457         PP.getPreprocessingRecord()->SetExternalSource(*this);
3458       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3459           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3460 
3461       if (F.NumPreprocessedSkippedRanges > 0)
3462         GlobalSkippedRangeMap.insert(
3463             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3464       break;
3465     }
3466 
3467     case DECL_UPDATE_OFFSETS:
3468       if (Record.size() % 2 != 0) {
3469         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3470         return Failure;
3471       }
3472       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3473         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3474         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3475 
3476         // If we've already loaded the decl, perform the updates when we finish
3477         // loading this block.
3478         if (Decl *D = GetExistingDecl(ID))
3479           PendingUpdateRecords.push_back(
3480               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3481       }
3482       break;
3483 
3484     case OBJC_CATEGORIES_MAP:
3485       if (F.LocalNumObjCCategoriesInMap != 0) {
3486         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3487         return Failure;
3488       }
3489 
3490       F.LocalNumObjCCategoriesInMap = Record[0];
3491       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3492       break;
3493 
3494     case OBJC_CATEGORIES:
3495       F.ObjCCategories.swap(Record);
3496       break;
3497 
3498     case CUDA_SPECIAL_DECL_REFS:
3499       // Later tables overwrite earlier ones.
3500       // FIXME: Modules will have trouble with this.
3501       CUDASpecialDeclRefs.clear();
3502       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3503         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3504       break;
3505 
3506     case HEADER_SEARCH_TABLE:
3507       F.HeaderFileInfoTableData = Blob.data();
3508       F.LocalNumHeaderFileInfos = Record[1];
3509       if (Record[0]) {
3510         F.HeaderFileInfoTable
3511           = HeaderFileInfoLookupTable::Create(
3512                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3513                    (const unsigned char *)F.HeaderFileInfoTableData,
3514                    HeaderFileInfoTrait(*this, F,
3515                                        &PP.getHeaderSearchInfo(),
3516                                        Blob.data() + Record[2]));
3517 
3518         PP.getHeaderSearchInfo().SetExternalSource(this);
3519         if (!PP.getHeaderSearchInfo().getExternalLookup())
3520           PP.getHeaderSearchInfo().SetExternalLookup(this);
3521       }
3522       break;
3523 
3524     case FP_PRAGMA_OPTIONS:
3525       // Later tables overwrite earlier ones.
3526       FPPragmaOptions.swap(Record);
3527       break;
3528 
3529     case OPENCL_EXTENSIONS:
3530       for (unsigned I = 0, E = Record.size(); I != E; ) {
3531         auto Name = ReadString(Record, I);
3532         auto &Opt = OpenCLExtensions.OptMap[Name];
3533         Opt.Supported = Record[I++] != 0;
3534         Opt.Enabled = Record[I++] != 0;
3535         Opt.Avail = Record[I++];
3536         Opt.Core = Record[I++];
3537       }
3538       break;
3539 
3540     case OPENCL_EXTENSION_TYPES:
3541       for (unsigned I = 0, E = Record.size(); I != E;) {
3542         auto TypeID = static_cast<::TypeID>(Record[I++]);
3543         auto *Type = GetType(TypeID).getTypePtr();
3544         auto NumExt = static_cast<unsigned>(Record[I++]);
3545         for (unsigned II = 0; II != NumExt; ++II) {
3546           auto Ext = ReadString(Record, I);
3547           OpenCLTypeExtMap[Type].insert(Ext);
3548         }
3549       }
3550       break;
3551 
3552     case OPENCL_EXTENSION_DECLS:
3553       for (unsigned I = 0, E = Record.size(); I != E;) {
3554         auto DeclID = static_cast<::DeclID>(Record[I++]);
3555         auto *Decl = GetDecl(DeclID);
3556         auto NumExt = static_cast<unsigned>(Record[I++]);
3557         for (unsigned II = 0; II != NumExt; ++II) {
3558           auto Ext = ReadString(Record, I);
3559           OpenCLDeclExtMap[Decl].insert(Ext);
3560         }
3561       }
3562       break;
3563 
3564     case TENTATIVE_DEFINITIONS:
3565       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3566         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3567       break;
3568 
3569     case KNOWN_NAMESPACES:
3570       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3571         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3572       break;
3573 
3574     case UNDEFINED_BUT_USED:
3575       if (UndefinedButUsed.size() % 2 != 0) {
3576         Error("Invalid existing UndefinedButUsed");
3577         return Failure;
3578       }
3579 
3580       if (Record.size() % 2 != 0) {
3581         Error("invalid undefined-but-used record");
3582         return Failure;
3583       }
3584       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3585         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3586         UndefinedButUsed.push_back(
3587             ReadSourceLocation(F, Record, I).getRawEncoding());
3588       }
3589       break;
3590 
3591     case DELETE_EXPRS_TO_ANALYZE:
3592       for (unsigned I = 0, N = Record.size(); I != N;) {
3593         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3594         const uint64_t Count = Record[I++];
3595         DelayedDeleteExprs.push_back(Count);
3596         for (uint64_t C = 0; C < Count; ++C) {
3597           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3598           bool IsArrayForm = Record[I++] == 1;
3599           DelayedDeleteExprs.push_back(IsArrayForm);
3600         }
3601       }
3602       break;
3603 
3604     case IMPORTED_MODULES:
3605       if (!F.isModule()) {
3606         // If we aren't loading a module (which has its own exports), make
3607         // all of the imported modules visible.
3608         // FIXME: Deal with macros-only imports.
3609         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3610           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3611           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3612           if (GlobalID) {
3613             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3614             if (DeserializationListener)
3615               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3616           }
3617         }
3618       }
3619       break;
3620 
3621     case MACRO_OFFSET: {
3622       if (F.LocalNumMacros != 0) {
3623         Error("duplicate MACRO_OFFSET record in AST file");
3624         return Failure;
3625       }
3626       F.MacroOffsets = (const uint32_t *)Blob.data();
3627       F.LocalNumMacros = Record[0];
3628       unsigned LocalBaseMacroID = Record[1];
3629       F.BaseMacroID = getTotalNumMacros();
3630 
3631       if (F.LocalNumMacros > 0) {
3632         // Introduce the global -> local mapping for macros within this module.
3633         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3634 
3635         // Introduce the local -> global mapping for macros within this module.
3636         F.MacroRemap.insertOrReplace(
3637           std::make_pair(LocalBaseMacroID,
3638                          F.BaseMacroID - LocalBaseMacroID));
3639 
3640         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3641       }
3642       break;
3643     }
3644 
3645     case LATE_PARSED_TEMPLATE:
3646       LateParsedTemplates.append(Record.begin(), Record.end());
3647       break;
3648 
3649     case OPTIMIZE_PRAGMA_OPTIONS:
3650       if (Record.size() != 1) {
3651         Error("invalid pragma optimize record");
3652         return Failure;
3653       }
3654       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3655       break;
3656 
3657     case MSSTRUCT_PRAGMA_OPTIONS:
3658       if (Record.size() != 1) {
3659         Error("invalid pragma ms_struct record");
3660         return Failure;
3661       }
3662       PragmaMSStructState = Record[0];
3663       break;
3664 
3665     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3666       if (Record.size() != 2) {
3667         Error("invalid pragma ms_struct record");
3668         return Failure;
3669       }
3670       PragmaMSPointersToMembersState = Record[0];
3671       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3672       break;
3673 
3674     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3675       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3676         UnusedLocalTypedefNameCandidates.push_back(
3677             getGlobalDeclID(F, Record[I]));
3678       break;
3679 
3680     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3681       if (Record.size() != 1) {
3682         Error("invalid cuda pragma options record");
3683         return Failure;
3684       }
3685       ForceCUDAHostDeviceDepth = Record[0];
3686       break;
3687 
3688     case PACK_PRAGMA_OPTIONS: {
3689       if (Record.size() < 3) {
3690         Error("invalid pragma pack record");
3691         return Failure;
3692       }
3693       PragmaPackCurrentValue = Record[0];
3694       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3695       unsigned NumStackEntries = Record[2];
3696       unsigned Idx = 3;
3697       // Reset the stack when importing a new module.
3698       PragmaPackStack.clear();
3699       for (unsigned I = 0; I < NumStackEntries; ++I) {
3700         PragmaPackStackEntry Entry;
3701         Entry.Value = Record[Idx++];
3702         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3703         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3704         PragmaPackStrings.push_back(ReadString(Record, Idx));
3705         Entry.SlotLabel = PragmaPackStrings.back();
3706         PragmaPackStack.push_back(Entry);
3707       }
3708       break;
3709     }
3710     }
3711   }
3712 }
3713 
3714 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3715   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3716 
3717   // Additional remapping information.
3718   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3719   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3720   F.ModuleOffsetMap = StringRef();
3721 
3722   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3723   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3724     F.SLocRemap.insert(std::make_pair(0U, 0));
3725     F.SLocRemap.insert(std::make_pair(2U, 1));
3726   }
3727 
3728   // Continuous range maps we may be updating in our module.
3729   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3730   RemapBuilder SLocRemap(F.SLocRemap);
3731   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3732   RemapBuilder MacroRemap(F.MacroRemap);
3733   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3734   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3735   RemapBuilder SelectorRemap(F.SelectorRemap);
3736   RemapBuilder DeclRemap(F.DeclRemap);
3737   RemapBuilder TypeRemap(F.TypeRemap);
3738 
3739   while (Data < DataEnd) {
3740     // FIXME: Looking up dependency modules by filename is horrible. Let's
3741     // start fixing this with prebuilt and explicit modules and see how it
3742     // goes...
3743     using namespace llvm::support;
3744     ModuleKind Kind = static_cast<ModuleKind>(
3745       endian::readNext<uint8_t, little, unaligned>(Data));
3746     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3747     StringRef Name = StringRef((const char*)Data, Len);
3748     Data += Len;
3749     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3750                       ? ModuleMgr.lookupByModuleName(Name)
3751                       : ModuleMgr.lookupByFileName(Name));
3752     if (!OM) {
3753       std::string Msg =
3754           "SourceLocation remap refers to unknown module, cannot find ";
3755       Msg.append(Name);
3756       Error(Msg);
3757       return;
3758     }
3759 
3760     uint32_t SLocOffset =
3761         endian::readNext<uint32_t, little, unaligned>(Data);
3762     uint32_t IdentifierIDOffset =
3763         endian::readNext<uint32_t, little, unaligned>(Data);
3764     uint32_t MacroIDOffset =
3765         endian::readNext<uint32_t, little, unaligned>(Data);
3766     uint32_t PreprocessedEntityIDOffset =
3767         endian::readNext<uint32_t, little, unaligned>(Data);
3768     uint32_t SubmoduleIDOffset =
3769         endian::readNext<uint32_t, little, unaligned>(Data);
3770     uint32_t SelectorIDOffset =
3771         endian::readNext<uint32_t, little, unaligned>(Data);
3772     uint32_t DeclIDOffset =
3773         endian::readNext<uint32_t, little, unaligned>(Data);
3774     uint32_t TypeIndexOffset =
3775         endian::readNext<uint32_t, little, unaligned>(Data);
3776 
3777     uint32_t None = std::numeric_limits<uint32_t>::max();
3778 
3779     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3780                          RemapBuilder &Remap) {
3781       if (Offset != None)
3782         Remap.insert(std::make_pair(Offset,
3783                                     static_cast<int>(BaseOffset - Offset)));
3784     };
3785     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3786     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3787     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3788     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3789               PreprocessedEntityRemap);
3790     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3791     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3792     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3793     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3794 
3795     // Global -> local mappings.
3796     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3797   }
3798 }
3799 
3800 ASTReader::ASTReadResult
3801 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3802                                   const ModuleFile *ImportedBy,
3803                                   unsigned ClientLoadCapabilities) {
3804   unsigned Idx = 0;
3805   F.ModuleMapPath = ReadPath(F, Record, Idx);
3806 
3807   // Try to resolve ModuleName in the current header search context and
3808   // verify that it is found in the same module map file as we saved. If the
3809   // top-level AST file is a main file, skip this check because there is no
3810   // usable header search context.
3811   assert(!F.ModuleName.empty() &&
3812          "MODULE_NAME should come before MODULE_MAP_FILE");
3813   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3814     // An implicitly-loaded module file should have its module listed in some
3815     // module map file that we've already loaded.
3816     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3817     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3818     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3819     // Don't emit module relocation error if we have -fno-validate-pch
3820     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3821       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3822         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3823           // This module was defined by an imported (explicit) module.
3824           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3825                                                << ASTFE->getName();
3826         } else {
3827           // This module was built with a different module map.
3828           Diag(diag::err_imported_module_not_found)
3829               << F.ModuleName << F.FileName
3830               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3831               << !ImportedBy;
3832           // In case it was imported by a PCH, there's a chance the user is
3833           // just missing to include the search path to the directory containing
3834           // the modulemap.
3835           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3836             Diag(diag::note_imported_by_pch_module_not_found)
3837                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3838         }
3839       }
3840       return OutOfDate;
3841     }
3842 
3843     assert(M->Name == F.ModuleName && "found module with different name");
3844 
3845     // Check the primary module map file.
3846     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3847     if (!StoredModMap || *StoredModMap != ModMap) {
3848       assert(ModMap && "found module is missing module map file");
3849       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3850              "top-level import should be verified");
3851       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3852       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3853         Diag(diag::err_imported_module_modmap_changed)
3854             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3855             << ModMap->getName() << F.ModuleMapPath << NotImported;
3856       return OutOfDate;
3857     }
3858 
3859     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3860     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3861       // FIXME: we should use input files rather than storing names.
3862       std::string Filename = ReadPath(F, Record, Idx);
3863       auto F = FileMgr.getFile(Filename, false, false);
3864       if (!F) {
3865         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3866           Error("could not find file '" + Filename +"' referenced by AST file");
3867         return OutOfDate;
3868       }
3869       AdditionalStoredMaps.insert(*F);
3870     }
3871 
3872     // Check any additional module map files (e.g. module.private.modulemap)
3873     // that are not in the pcm.
3874     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3875       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3876         // Remove files that match
3877         // Note: SmallPtrSet::erase is really remove
3878         if (!AdditionalStoredMaps.erase(ModMap)) {
3879           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3880             Diag(diag::err_module_different_modmap)
3881               << F.ModuleName << /*new*/0 << ModMap->getName();
3882           return OutOfDate;
3883         }
3884       }
3885     }
3886 
3887     // Check any additional module map files that are in the pcm, but not
3888     // found in header search. Cases that match are already removed.
3889     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3890       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3891         Diag(diag::err_module_different_modmap)
3892           << F.ModuleName << /*not new*/1 << ModMap->getName();
3893       return OutOfDate;
3894     }
3895   }
3896 
3897   if (Listener)
3898     Listener->ReadModuleMapFile(F.ModuleMapPath);
3899   return Success;
3900 }
3901 
3902 /// Move the given method to the back of the global list of methods.
3903 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3904   // Find the entry for this selector in the method pool.
3905   Sema::GlobalMethodPool::iterator Known
3906     = S.MethodPool.find(Method->getSelector());
3907   if (Known == S.MethodPool.end())
3908     return;
3909 
3910   // Retrieve the appropriate method list.
3911   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3912                                                     : Known->second.second;
3913   bool Found = false;
3914   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
3915     if (!Found) {
3916       if (List->getMethod() == Method) {
3917         Found = true;
3918       } else {
3919         // Keep searching.
3920         continue;
3921       }
3922     }
3923 
3924     if (List->getNext())
3925       List->setMethod(List->getNext()->getMethod());
3926     else
3927       List->setMethod(Method);
3928   }
3929 }
3930 
3931 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
3932   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
3933   for (Decl *D : Names) {
3934     bool wasHidden = D->isHidden();
3935     D->setVisibleDespiteOwningModule();
3936 
3937     if (wasHidden && SemaObj) {
3938       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3939         moveMethodToBackOfGlobalList(*SemaObj, Method);
3940       }
3941     }
3942   }
3943 }
3944 
3945 void ASTReader::makeModuleVisible(Module *Mod,
3946                                   Module::NameVisibilityKind NameVisibility,
3947                                   SourceLocation ImportLoc) {
3948   llvm::SmallPtrSet<Module *, 4> Visited;
3949   SmallVector<Module *, 4> Stack;
3950   Stack.push_back(Mod);
3951   while (!Stack.empty()) {
3952     Mod = Stack.pop_back_val();
3953 
3954     if (NameVisibility <= Mod->NameVisibility) {
3955       // This module already has this level of visibility (or greater), so
3956       // there is nothing more to do.
3957       continue;
3958     }
3959 
3960     if (!Mod->isAvailable()) {
3961       // Modules that aren't available cannot be made visible.
3962       continue;
3963     }
3964 
3965     // Update the module's name visibility.
3966     Mod->NameVisibility = NameVisibility;
3967 
3968     // If we've already deserialized any names from this module,
3969     // mark them as visible.
3970     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3971     if (Hidden != HiddenNamesMap.end()) {
3972       auto HiddenNames = std::move(*Hidden);
3973       HiddenNamesMap.erase(Hidden);
3974       makeNamesVisible(HiddenNames.second, HiddenNames.first);
3975       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3976              "making names visible added hidden names");
3977     }
3978 
3979     // Push any exported modules onto the stack to be marked as visible.
3980     SmallVector<Module *, 16> Exports;
3981     Mod->getExportedModules(Exports);
3982     for (SmallVectorImpl<Module *>::iterator
3983            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3984       Module *Exported = *I;
3985       if (Visited.insert(Exported).second)
3986         Stack.push_back(Exported);
3987     }
3988   }
3989 }
3990 
3991 /// We've merged the definition \p MergedDef into the existing definition
3992 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3993 /// visible.
3994 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3995                                           NamedDecl *MergedDef) {
3996   if (Def->isHidden()) {
3997     // If MergedDef is visible or becomes visible, make the definition visible.
3998     if (!MergedDef->isHidden())
3999       Def->setVisibleDespiteOwningModule();
4000     else {
4001       getContext().mergeDefinitionIntoModule(
4002           Def, MergedDef->getImportedOwningModule(),
4003           /*NotifyListeners*/ false);
4004       PendingMergedDefinitionsToDeduplicate.insert(Def);
4005     }
4006   }
4007 }
4008 
4009 bool ASTReader::loadGlobalIndex() {
4010   if (GlobalIndex)
4011     return false;
4012 
4013   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4014       !PP.getLangOpts().Modules)
4015     return true;
4016 
4017   // Try to load the global index.
4018   TriedLoadingGlobalIndex = true;
4019   StringRef ModuleCachePath
4020     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4021   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4022       GlobalModuleIndex::readIndex(ModuleCachePath);
4023   if (llvm::Error Err = std::move(Result.second)) {
4024     assert(!Result.first);
4025     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4026     return true;
4027   }
4028 
4029   GlobalIndex.reset(Result.first);
4030   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4031   return false;
4032 }
4033 
4034 bool ASTReader::isGlobalIndexUnavailable() const {
4035   return PP.getLangOpts().Modules && UseGlobalIndex &&
4036          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4037 }
4038 
4039 static void updateModuleTimestamp(ModuleFile &MF) {
4040   // Overwrite the timestamp file contents so that file's mtime changes.
4041   std::string TimestampFilename = MF.getTimestampFilename();
4042   std::error_code EC;
4043   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4044   if (EC)
4045     return;
4046   OS << "Timestamp file\n";
4047   OS.close();
4048   OS.clear_error(); // Avoid triggering a fatal error.
4049 }
4050 
4051 /// Given a cursor at the start of an AST file, scan ahead and drop the
4052 /// cursor into the start of the given block ID, returning false on success and
4053 /// true on failure.
4054 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4055   while (true) {
4056     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4057     if (!MaybeEntry) {
4058       // FIXME this drops errors on the floor.
4059       consumeError(MaybeEntry.takeError());
4060       return true;
4061     }
4062     llvm::BitstreamEntry Entry = MaybeEntry.get();
4063 
4064     switch (Entry.Kind) {
4065     case llvm::BitstreamEntry::Error:
4066     case llvm::BitstreamEntry::EndBlock:
4067       return true;
4068 
4069     case llvm::BitstreamEntry::Record:
4070       // Ignore top-level records.
4071       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4072         break;
4073       else {
4074         // FIXME this drops errors on the floor.
4075         consumeError(Skipped.takeError());
4076         return true;
4077       }
4078 
4079     case llvm::BitstreamEntry::SubBlock:
4080       if (Entry.ID == BlockID) {
4081         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4082           // FIXME this drops the error on the floor.
4083           consumeError(std::move(Err));
4084           return true;
4085         }
4086         // Found it!
4087         return false;
4088       }
4089 
4090       if (llvm::Error Err = Cursor.SkipBlock()) {
4091         // FIXME this drops the error on the floor.
4092         consumeError(std::move(Err));
4093         return true;
4094       }
4095     }
4096   }
4097 }
4098 
4099 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4100                                             ModuleKind Type,
4101                                             SourceLocation ImportLoc,
4102                                             unsigned ClientLoadCapabilities,
4103                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4104   llvm::SaveAndRestore<SourceLocation>
4105     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4106 
4107   // Defer any pending actions until we get to the end of reading the AST file.
4108   Deserializing AnASTFile(this);
4109 
4110   // Bump the generation number.
4111   unsigned PreviousGeneration = 0;
4112   if (ContextObj)
4113     PreviousGeneration = incrementGeneration(*ContextObj);
4114 
4115   unsigned NumModules = ModuleMgr.size();
4116   SmallVector<ImportedModule, 4> Loaded;
4117   switch (ASTReadResult ReadResult =
4118               ReadASTCore(FileName, Type, ImportLoc,
4119                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4120                           ASTFileSignature(), ClientLoadCapabilities)) {
4121   case Failure:
4122   case Missing:
4123   case OutOfDate:
4124   case VersionMismatch:
4125   case ConfigurationMismatch:
4126   case HadErrors: {
4127     llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
4128     for (const ImportedModule &IM : Loaded)
4129       LoadedSet.insert(IM.Mod);
4130 
4131     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
4132                             PP.getLangOpts().Modules
4133                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4134                                 : nullptr);
4135 
4136     // If we find that any modules are unusable, the global index is going
4137     // to be out-of-date. Just remove it.
4138     GlobalIndex.reset();
4139     ModuleMgr.setGlobalIndex(nullptr);
4140     return ReadResult;
4141   }
4142   case Success:
4143     break;
4144   }
4145 
4146   // Here comes stuff that we only do once the entire chain is loaded.
4147 
4148   // Load the AST blocks of all of the modules that we loaded.
4149   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4150                                               MEnd = Loaded.end();
4151        M != MEnd; ++M) {
4152     ModuleFile &F = *M->Mod;
4153 
4154     // Read the AST block.
4155     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4156       return Result;
4157 
4158     // Read the extension blocks.
4159     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4160       if (ASTReadResult Result = ReadExtensionBlock(F))
4161         return Result;
4162     }
4163 
4164     // Once read, set the ModuleFile bit base offset and update the size in
4165     // bits of all files we've seen.
4166     F.GlobalBitOffset = TotalModulesSizeInBits;
4167     TotalModulesSizeInBits += F.SizeInBits;
4168     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4169 
4170     // Preload SLocEntries.
4171     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4172       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4173       // Load it through the SourceManager and don't call ReadSLocEntry()
4174       // directly because the entry may have already been loaded in which case
4175       // calling ReadSLocEntry() directly would trigger an assertion in
4176       // SourceManager.
4177       SourceMgr.getLoadedSLocEntryByID(Index);
4178     }
4179 
4180     // Map the original source file ID into the ID space of the current
4181     // compilation.
4182     if (F.OriginalSourceFileID.isValid()) {
4183       F.OriginalSourceFileID = FileID::get(
4184           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4185     }
4186 
4187     // Preload all the pending interesting identifiers by marking them out of
4188     // date.
4189     for (auto Offset : F.PreloadIdentifierOffsets) {
4190       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4191           F.IdentifierTableData + Offset);
4192 
4193       ASTIdentifierLookupTrait Trait(*this, F);
4194       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4195       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4196       auto &II = PP.getIdentifierTable().getOwn(Key);
4197       II.setOutOfDate(true);
4198 
4199       // Mark this identifier as being from an AST file so that we can track
4200       // whether we need to serialize it.
4201       markIdentifierFromAST(*this, II);
4202 
4203       // Associate the ID with the identifier so that the writer can reuse it.
4204       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4205       SetIdentifierInfo(ID, &II);
4206     }
4207   }
4208 
4209   // Setup the import locations and notify the module manager that we've
4210   // committed to these module files.
4211   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
4212                                               MEnd = Loaded.end();
4213        M != MEnd; ++M) {
4214     ModuleFile &F = *M->Mod;
4215 
4216     ModuleMgr.moduleFileAccepted(&F);
4217 
4218     // Set the import location.
4219     F.DirectImportLoc = ImportLoc;
4220     // FIXME: We assume that locations from PCH / preamble do not need
4221     // any translation.
4222     if (!M->ImportedBy)
4223       F.ImportLoc = M->ImportLoc;
4224     else
4225       F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
4226   }
4227 
4228   if (!PP.getLangOpts().CPlusPlus ||
4229       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4230        Type != MK_PrebuiltModule)) {
4231     // Mark all of the identifiers in the identifier table as being out of date,
4232     // so that various accessors know to check the loaded modules when the
4233     // identifier is used.
4234     //
4235     // For C++ modules, we don't need information on many identifiers (just
4236     // those that provide macros or are poisoned), so we mark all of
4237     // the interesting ones via PreloadIdentifierOffsets.
4238     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4239                                 IdEnd = PP.getIdentifierTable().end();
4240          Id != IdEnd; ++Id)
4241       Id->second->setOutOfDate(true);
4242   }
4243   // Mark selectors as out of date.
4244   for (auto Sel : SelectorGeneration)
4245     SelectorOutOfDate[Sel.first] = true;
4246 
4247   // Resolve any unresolved module exports.
4248   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4249     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4250     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4251     Module *ResolvedMod = getSubmodule(GlobalID);
4252 
4253     switch (Unresolved.Kind) {
4254     case UnresolvedModuleRef::Conflict:
4255       if (ResolvedMod) {
4256         Module::Conflict Conflict;
4257         Conflict.Other = ResolvedMod;
4258         Conflict.Message = Unresolved.String.str();
4259         Unresolved.Mod->Conflicts.push_back(Conflict);
4260       }
4261       continue;
4262 
4263     case UnresolvedModuleRef::Import:
4264       if (ResolvedMod)
4265         Unresolved.Mod->Imports.insert(ResolvedMod);
4266       continue;
4267 
4268     case UnresolvedModuleRef::Export:
4269       if (ResolvedMod || Unresolved.IsWildcard)
4270         Unresolved.Mod->Exports.push_back(
4271           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4272       continue;
4273     }
4274   }
4275   UnresolvedModuleRefs.clear();
4276 
4277   if (Imported)
4278     Imported->append(ImportedModules.begin(),
4279                      ImportedModules.end());
4280 
4281   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4282   // Might be unnecessary as use declarations are only used to build the
4283   // module itself.
4284 
4285   if (ContextObj)
4286     InitializeContext();
4287 
4288   if (SemaObj)
4289     UpdateSema();
4290 
4291   if (DeserializationListener)
4292     DeserializationListener->ReaderInitialized(this);
4293 
4294   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4295   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4296     // If this AST file is a precompiled preamble, then set the
4297     // preamble file ID of the source manager to the file source file
4298     // from which the preamble was built.
4299     if (Type == MK_Preamble) {
4300       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4301     } else if (Type == MK_MainFile) {
4302       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4303     }
4304   }
4305 
4306   // For any Objective-C class definitions we have already loaded, make sure
4307   // that we load any additional categories.
4308   if (ContextObj) {
4309     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4310       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4311                          ObjCClassesLoaded[I],
4312                          PreviousGeneration);
4313     }
4314   }
4315 
4316   if (PP.getHeaderSearchInfo()
4317           .getHeaderSearchOpts()
4318           .ModulesValidateOncePerBuildSession) {
4319     // Now we are certain that the module and all modules it depends on are
4320     // up to date.  Create or update timestamp files for modules that are
4321     // located in the module cache (not for PCH files that could be anywhere
4322     // in the filesystem).
4323     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4324       ImportedModule &M = Loaded[I];
4325       if (M.Mod->Kind == MK_ImplicitModule) {
4326         updateModuleTimestamp(*M.Mod);
4327       }
4328     }
4329   }
4330 
4331   return Success;
4332 }
4333 
4334 static ASTFileSignature readASTFileSignature(StringRef PCH);
4335 
4336 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4337 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4338   // FIXME checking magic headers is done in other places such as
4339   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4340   // always done the same. Unify it all with a helper.
4341   if (!Stream.canSkipToPos(4))
4342     return llvm::createStringError(std::errc::illegal_byte_sequence,
4343                                    "file too small to contain AST file magic");
4344   for (unsigned C : {'C', 'P', 'C', 'H'})
4345     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4346       if (Res.get() != C)
4347         return llvm::createStringError(
4348             std::errc::illegal_byte_sequence,
4349             "file doesn't start with AST file magic");
4350     } else
4351       return Res.takeError();
4352   return llvm::Error::success();
4353 }
4354 
4355 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4356   switch (Kind) {
4357   case MK_PCH:
4358     return 0; // PCH
4359   case MK_ImplicitModule:
4360   case MK_ExplicitModule:
4361   case MK_PrebuiltModule:
4362     return 1; // module
4363   case MK_MainFile:
4364   case MK_Preamble:
4365     return 2; // main source file
4366   }
4367   llvm_unreachable("unknown module kind");
4368 }
4369 
4370 ASTReader::ASTReadResult
4371 ASTReader::ReadASTCore(StringRef FileName,
4372                        ModuleKind Type,
4373                        SourceLocation ImportLoc,
4374                        ModuleFile *ImportedBy,
4375                        SmallVectorImpl<ImportedModule> &Loaded,
4376                        off_t ExpectedSize, time_t ExpectedModTime,
4377                        ASTFileSignature ExpectedSignature,
4378                        unsigned ClientLoadCapabilities) {
4379   ModuleFile *M;
4380   std::string ErrorStr;
4381   ModuleManager::AddModuleResult AddResult
4382     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4383                           getGeneration(), ExpectedSize, ExpectedModTime,
4384                           ExpectedSignature, readASTFileSignature,
4385                           M, ErrorStr);
4386 
4387   switch (AddResult) {
4388   case ModuleManager::AlreadyLoaded:
4389     Diag(diag::remark_module_import)
4390         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4391         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4392     return Success;
4393 
4394   case ModuleManager::NewlyLoaded:
4395     // Load module file below.
4396     break;
4397 
4398   case ModuleManager::Missing:
4399     // The module file was missing; if the client can handle that, return
4400     // it.
4401     if (ClientLoadCapabilities & ARR_Missing)
4402       return Missing;
4403 
4404     // Otherwise, return an error.
4405     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4406                                           << FileName << !ErrorStr.empty()
4407                                           << ErrorStr;
4408     return Failure;
4409 
4410   case ModuleManager::OutOfDate:
4411     // We couldn't load the module file because it is out-of-date. If the
4412     // client can handle out-of-date, return it.
4413     if (ClientLoadCapabilities & ARR_OutOfDate)
4414       return OutOfDate;
4415 
4416     // Otherwise, return an error.
4417     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4418                                             << FileName << !ErrorStr.empty()
4419                                             << ErrorStr;
4420     return Failure;
4421   }
4422 
4423   assert(M && "Missing module file");
4424 
4425   bool ShouldFinalizePCM = false;
4426   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4427     auto &MC = getModuleManager().getModuleCache();
4428     if (ShouldFinalizePCM)
4429       MC.finalizePCM(FileName);
4430     else
4431       MC.tryToDropPCM(FileName);
4432   });
4433   ModuleFile &F = *M;
4434   BitstreamCursor &Stream = F.Stream;
4435   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4436   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4437 
4438   // Sniff for the signature.
4439   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4440     Diag(diag::err_module_file_invalid)
4441         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4442     return Failure;
4443   }
4444 
4445   // This is used for compatibility with older PCH formats.
4446   bool HaveReadControlBlock = false;
4447   while (true) {
4448     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4449     if (!MaybeEntry) {
4450       Error(MaybeEntry.takeError());
4451       return Failure;
4452     }
4453     llvm::BitstreamEntry Entry = MaybeEntry.get();
4454 
4455     switch (Entry.Kind) {
4456     case llvm::BitstreamEntry::Error:
4457     case llvm::BitstreamEntry::Record:
4458     case llvm::BitstreamEntry::EndBlock:
4459       Error("invalid record at top-level of AST file");
4460       return Failure;
4461 
4462     case llvm::BitstreamEntry::SubBlock:
4463       break;
4464     }
4465 
4466     switch (Entry.ID) {
4467     case CONTROL_BLOCK_ID:
4468       HaveReadControlBlock = true;
4469       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4470       case Success:
4471         // Check that we didn't try to load a non-module AST file as a module.
4472         //
4473         // FIXME: Should we also perform the converse check? Loading a module as
4474         // a PCH file sort of works, but it's a bit wonky.
4475         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4476              Type == MK_PrebuiltModule) &&
4477             F.ModuleName.empty()) {
4478           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4479           if (Result != OutOfDate ||
4480               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4481             Diag(diag::err_module_file_not_module) << FileName;
4482           return Result;
4483         }
4484         break;
4485 
4486       case Failure: return Failure;
4487       case Missing: return Missing;
4488       case OutOfDate: return OutOfDate;
4489       case VersionMismatch: return VersionMismatch;
4490       case ConfigurationMismatch: return ConfigurationMismatch;
4491       case HadErrors: return HadErrors;
4492       }
4493       break;
4494 
4495     case AST_BLOCK_ID:
4496       if (!HaveReadControlBlock) {
4497         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4498           Diag(diag::err_pch_version_too_old);
4499         return VersionMismatch;
4500       }
4501 
4502       // Record that we've loaded this module.
4503       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4504       ShouldFinalizePCM = true;
4505       return Success;
4506 
4507     case UNHASHED_CONTROL_BLOCK_ID:
4508       // This block is handled using look-ahead during ReadControlBlock.  We
4509       // shouldn't get here!
4510       Error("malformed block record in AST file");
4511       return Failure;
4512 
4513     default:
4514       if (llvm::Error Err = Stream.SkipBlock()) {
4515         Error(std::move(Err));
4516         return Failure;
4517       }
4518       break;
4519     }
4520   }
4521 
4522   llvm_unreachable("unexpected break; expected return");
4523 }
4524 
4525 ASTReader::ASTReadResult
4526 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4527                                     unsigned ClientLoadCapabilities) {
4528   const HeaderSearchOptions &HSOpts =
4529       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4530   bool AllowCompatibleConfigurationMismatch =
4531       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4532 
4533   ASTReadResult Result = readUnhashedControlBlockImpl(
4534       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4535       Listener.get(),
4536       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4537 
4538   // If F was directly imported by another module, it's implicitly validated by
4539   // the importing module.
4540   if (DisableValidation || WasImportedBy ||
4541       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4542     return Success;
4543 
4544   if (Result == Failure) {
4545     Error("malformed block record in AST file");
4546     return Failure;
4547   }
4548 
4549   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4550     // If this module has already been finalized in the ModuleCache, we're stuck
4551     // with it; we can only load a single version of each module.
4552     //
4553     // This can happen when a module is imported in two contexts: in one, as a
4554     // user module; in another, as a system module (due to an import from
4555     // another module marked with the [system] flag).  It usually indicates a
4556     // bug in the module map: this module should also be marked with [system].
4557     //
4558     // If -Wno-system-headers (the default), and the first import is as a
4559     // system module, then validation will fail during the as-user import,
4560     // since -Werror flags won't have been validated.  However, it's reasonable
4561     // to treat this consistently as a system module.
4562     //
4563     // If -Wsystem-headers, the PCM on disk was built with
4564     // -Wno-system-headers, and the first import is as a user module, then
4565     // validation will fail during the as-system import since the PCM on disk
4566     // doesn't guarantee that -Werror was respected.  However, the -Werror
4567     // flags were checked during the initial as-user import.
4568     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4569       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4570       return Success;
4571     }
4572   }
4573 
4574   return Result;
4575 }
4576 
4577 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4578     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4579     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4580     bool ValidateDiagnosticOptions) {
4581   // Initialize a stream.
4582   BitstreamCursor Stream(StreamData);
4583 
4584   // Sniff for the signature.
4585   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4586     // FIXME this drops the error on the floor.
4587     consumeError(std::move(Err));
4588     return Failure;
4589   }
4590 
4591   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4592   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4593     return Failure;
4594 
4595   // Read all of the records in the options block.
4596   RecordData Record;
4597   ASTReadResult Result = Success;
4598   while (true) {
4599     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4600     if (!MaybeEntry) {
4601       // FIXME this drops the error on the floor.
4602       consumeError(MaybeEntry.takeError());
4603       return Failure;
4604     }
4605     llvm::BitstreamEntry Entry = MaybeEntry.get();
4606 
4607     switch (Entry.Kind) {
4608     case llvm::BitstreamEntry::Error:
4609     case llvm::BitstreamEntry::SubBlock:
4610       return Failure;
4611 
4612     case llvm::BitstreamEntry::EndBlock:
4613       return Result;
4614 
4615     case llvm::BitstreamEntry::Record:
4616       // The interesting case.
4617       break;
4618     }
4619 
4620     // Read and process a record.
4621     Record.clear();
4622     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4623     if (!MaybeRecordType) {
4624       // FIXME this drops the error.
4625       return Failure;
4626     }
4627     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4628     case SIGNATURE:
4629       if (F)
4630         std::copy(Record.begin(), Record.end(), F->Signature.data());
4631       break;
4632     case DIAGNOSTIC_OPTIONS: {
4633       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4634       if (Listener && ValidateDiagnosticOptions &&
4635           !AllowCompatibleConfigurationMismatch &&
4636           ParseDiagnosticOptions(Record, Complain, *Listener))
4637         Result = OutOfDate; // Don't return early.  Read the signature.
4638       break;
4639     }
4640     case DIAG_PRAGMA_MAPPINGS:
4641       if (!F)
4642         break;
4643       if (F->PragmaDiagMappings.empty())
4644         F->PragmaDiagMappings.swap(Record);
4645       else
4646         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4647                                      Record.begin(), Record.end());
4648       break;
4649     }
4650   }
4651 }
4652 
4653 /// Parse a record and blob containing module file extension metadata.
4654 static bool parseModuleFileExtensionMetadata(
4655               const SmallVectorImpl<uint64_t> &Record,
4656               StringRef Blob,
4657               ModuleFileExtensionMetadata &Metadata) {
4658   if (Record.size() < 4) return true;
4659 
4660   Metadata.MajorVersion = Record[0];
4661   Metadata.MinorVersion = Record[1];
4662 
4663   unsigned BlockNameLen = Record[2];
4664   unsigned UserInfoLen = Record[3];
4665 
4666   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4667 
4668   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4669   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4670                                   Blob.data() + BlockNameLen + UserInfoLen);
4671   return false;
4672 }
4673 
4674 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4675   BitstreamCursor &Stream = F.Stream;
4676 
4677   RecordData Record;
4678   while (true) {
4679     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4680     if (!MaybeEntry) {
4681       Error(MaybeEntry.takeError());
4682       return Failure;
4683     }
4684     llvm::BitstreamEntry Entry = MaybeEntry.get();
4685 
4686     switch (Entry.Kind) {
4687     case llvm::BitstreamEntry::SubBlock:
4688       if (llvm::Error Err = Stream.SkipBlock()) {
4689         Error(std::move(Err));
4690         return Failure;
4691       }
4692       continue;
4693 
4694     case llvm::BitstreamEntry::EndBlock:
4695       return Success;
4696 
4697     case llvm::BitstreamEntry::Error:
4698       return HadErrors;
4699 
4700     case llvm::BitstreamEntry::Record:
4701       break;
4702     }
4703 
4704     Record.clear();
4705     StringRef Blob;
4706     Expected<unsigned> MaybeRecCode =
4707         Stream.readRecord(Entry.ID, Record, &Blob);
4708     if (!MaybeRecCode) {
4709       Error(MaybeRecCode.takeError());
4710       return Failure;
4711     }
4712     switch (MaybeRecCode.get()) {
4713     case EXTENSION_METADATA: {
4714       ModuleFileExtensionMetadata Metadata;
4715       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4716         return Failure;
4717 
4718       // Find a module file extension with this block name.
4719       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4720       if (Known == ModuleFileExtensions.end()) break;
4721 
4722       // Form a reader.
4723       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4724                                                              F, Stream)) {
4725         F.ExtensionReaders.push_back(std::move(Reader));
4726       }
4727 
4728       break;
4729     }
4730     }
4731   }
4732 
4733   return Success;
4734 }
4735 
4736 void ASTReader::InitializeContext() {
4737   assert(ContextObj && "no context to initialize");
4738   ASTContext &Context = *ContextObj;
4739 
4740   // If there's a listener, notify them that we "read" the translation unit.
4741   if (DeserializationListener)
4742     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4743                                       Context.getTranslationUnitDecl());
4744 
4745   // FIXME: Find a better way to deal with collisions between these
4746   // built-in types. Right now, we just ignore the problem.
4747 
4748   // Load the special types.
4749   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4750     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4751       if (!Context.CFConstantStringTypeDecl)
4752         Context.setCFConstantStringType(GetType(String));
4753     }
4754 
4755     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4756       QualType FileType = GetType(File);
4757       if (FileType.isNull()) {
4758         Error("FILE type is NULL");
4759         return;
4760       }
4761 
4762       if (!Context.FILEDecl) {
4763         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4764           Context.setFILEDecl(Typedef->getDecl());
4765         else {
4766           const TagType *Tag = FileType->getAs<TagType>();
4767           if (!Tag) {
4768             Error("Invalid FILE type in AST file");
4769             return;
4770           }
4771           Context.setFILEDecl(Tag->getDecl());
4772         }
4773       }
4774     }
4775 
4776     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4777       QualType Jmp_bufType = GetType(Jmp_buf);
4778       if (Jmp_bufType.isNull()) {
4779         Error("jmp_buf type is NULL");
4780         return;
4781       }
4782 
4783       if (!Context.jmp_bufDecl) {
4784         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4785           Context.setjmp_bufDecl(Typedef->getDecl());
4786         else {
4787           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4788           if (!Tag) {
4789             Error("Invalid jmp_buf type in AST file");
4790             return;
4791           }
4792           Context.setjmp_bufDecl(Tag->getDecl());
4793         }
4794       }
4795     }
4796 
4797     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4798       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4799       if (Sigjmp_bufType.isNull()) {
4800         Error("sigjmp_buf type is NULL");
4801         return;
4802       }
4803 
4804       if (!Context.sigjmp_bufDecl) {
4805         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4806           Context.setsigjmp_bufDecl(Typedef->getDecl());
4807         else {
4808           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4809           assert(Tag && "Invalid sigjmp_buf type in AST file");
4810           Context.setsigjmp_bufDecl(Tag->getDecl());
4811         }
4812       }
4813     }
4814 
4815     if (unsigned ObjCIdRedef
4816           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4817       if (Context.ObjCIdRedefinitionType.isNull())
4818         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4819     }
4820 
4821     if (unsigned ObjCClassRedef
4822           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4823       if (Context.ObjCClassRedefinitionType.isNull())
4824         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4825     }
4826 
4827     if (unsigned ObjCSelRedef
4828           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4829       if (Context.ObjCSelRedefinitionType.isNull())
4830         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4831     }
4832 
4833     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4834       QualType Ucontext_tType = GetType(Ucontext_t);
4835       if (Ucontext_tType.isNull()) {
4836         Error("ucontext_t type is NULL");
4837         return;
4838       }
4839 
4840       if (!Context.ucontext_tDecl) {
4841         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4842           Context.setucontext_tDecl(Typedef->getDecl());
4843         else {
4844           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4845           assert(Tag && "Invalid ucontext_t type in AST file");
4846           Context.setucontext_tDecl(Tag->getDecl());
4847         }
4848       }
4849     }
4850   }
4851 
4852   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4853 
4854   // If there were any CUDA special declarations, deserialize them.
4855   if (!CUDASpecialDeclRefs.empty()) {
4856     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4857     Context.setcudaConfigureCallDecl(
4858                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4859   }
4860 
4861   // Re-export any modules that were imported by a non-module AST file.
4862   // FIXME: This does not make macro-only imports visible again.
4863   for (auto &Import : ImportedModules) {
4864     if (Module *Imported = getSubmodule(Import.ID)) {
4865       makeModuleVisible(Imported, Module::AllVisible,
4866                         /*ImportLoc=*/Import.ImportLoc);
4867       if (Import.ImportLoc.isValid())
4868         PP.makeModuleVisible(Imported, Import.ImportLoc);
4869       // FIXME: should we tell Sema to make the module visible too?
4870     }
4871   }
4872   ImportedModules.clear();
4873 }
4874 
4875 void ASTReader::finalizeForWriting() {
4876   // Nothing to do for now.
4877 }
4878 
4879 /// Reads and return the signature record from \p PCH's control block, or
4880 /// else returns 0.
4881 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4882   BitstreamCursor Stream(PCH);
4883   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4884     // FIXME this drops the error on the floor.
4885     consumeError(std::move(Err));
4886     return ASTFileSignature();
4887   }
4888 
4889   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4890   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4891     return ASTFileSignature();
4892 
4893   // Scan for SIGNATURE inside the diagnostic options block.
4894   ASTReader::RecordData Record;
4895   while (true) {
4896     Expected<llvm::BitstreamEntry> MaybeEntry =
4897         Stream.advanceSkippingSubblocks();
4898     if (!MaybeEntry) {
4899       // FIXME this drops the error on the floor.
4900       consumeError(MaybeEntry.takeError());
4901       return ASTFileSignature();
4902     }
4903     llvm::BitstreamEntry Entry = MaybeEntry.get();
4904 
4905     if (Entry.Kind != llvm::BitstreamEntry::Record)
4906       return ASTFileSignature();
4907 
4908     Record.clear();
4909     StringRef Blob;
4910     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4911     if (!MaybeRecord) {
4912       // FIXME this drops the error on the floor.
4913       consumeError(MaybeRecord.takeError());
4914       return ASTFileSignature();
4915     }
4916     if (SIGNATURE == MaybeRecord.get())
4917       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4918                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
4919   }
4920 }
4921 
4922 /// Retrieve the name of the original source file name
4923 /// directly from the AST file, without actually loading the AST
4924 /// file.
4925 std::string ASTReader::getOriginalSourceFile(
4926     const std::string &ASTFileName, FileManager &FileMgr,
4927     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
4928   // Open the AST file.
4929   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
4930   if (!Buffer) {
4931     Diags.Report(diag::err_fe_unable_to_read_pch_file)
4932         << ASTFileName << Buffer.getError().message();
4933     return std::string();
4934   }
4935 
4936   // Initialize the stream
4937   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
4938 
4939   // Sniff for the signature.
4940   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4941     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
4942     return std::string();
4943   }
4944 
4945   // Scan for the CONTROL_BLOCK_ID block.
4946   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
4947     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4948     return std::string();
4949   }
4950 
4951   // Scan for ORIGINAL_FILE inside the control block.
4952   RecordData Record;
4953   while (true) {
4954     Expected<llvm::BitstreamEntry> MaybeEntry =
4955         Stream.advanceSkippingSubblocks();
4956     if (!MaybeEntry) {
4957       // FIXME this drops errors on the floor.
4958       consumeError(MaybeEntry.takeError());
4959       return std::string();
4960     }
4961     llvm::BitstreamEntry Entry = MaybeEntry.get();
4962 
4963     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4964       return std::string();
4965 
4966     if (Entry.Kind != llvm::BitstreamEntry::Record) {
4967       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4968       return std::string();
4969     }
4970 
4971     Record.clear();
4972     StringRef Blob;
4973     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4974     if (!MaybeRecord) {
4975       // FIXME this drops the errors on the floor.
4976       consumeError(MaybeRecord.takeError());
4977       return std::string();
4978     }
4979     if (ORIGINAL_FILE == MaybeRecord.get())
4980       return Blob.str();
4981   }
4982 }
4983 
4984 namespace {
4985 
4986   class SimplePCHValidator : public ASTReaderListener {
4987     const LangOptions &ExistingLangOpts;
4988     const TargetOptions &ExistingTargetOpts;
4989     const PreprocessorOptions &ExistingPPOpts;
4990     std::string ExistingModuleCachePath;
4991     FileManager &FileMgr;
4992 
4993   public:
4994     SimplePCHValidator(const LangOptions &ExistingLangOpts,
4995                        const TargetOptions &ExistingTargetOpts,
4996                        const PreprocessorOptions &ExistingPPOpts,
4997                        StringRef ExistingModuleCachePath,
4998                        FileManager &FileMgr)
4999       : ExistingLangOpts(ExistingLangOpts),
5000         ExistingTargetOpts(ExistingTargetOpts),
5001         ExistingPPOpts(ExistingPPOpts),
5002         ExistingModuleCachePath(ExistingModuleCachePath),
5003         FileMgr(FileMgr) {}
5004 
5005     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5006                              bool AllowCompatibleDifferences) override {
5007       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5008                                   AllowCompatibleDifferences);
5009     }
5010 
5011     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5012                            bool AllowCompatibleDifferences) override {
5013       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5014                                 AllowCompatibleDifferences);
5015     }
5016 
5017     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5018                                  StringRef SpecificModuleCachePath,
5019                                  bool Complain) override {
5020       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5021                                       ExistingModuleCachePath,
5022                                       nullptr, ExistingLangOpts);
5023     }
5024 
5025     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5026                                  bool Complain,
5027                                  std::string &SuggestedPredefines) override {
5028       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5029                                       SuggestedPredefines, ExistingLangOpts);
5030     }
5031   };
5032 
5033 } // namespace
5034 
5035 bool ASTReader::readASTFileControlBlock(
5036     StringRef Filename, FileManager &FileMgr,
5037     const PCHContainerReader &PCHContainerRdr,
5038     bool FindModuleFileExtensions,
5039     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5040   // Open the AST file.
5041   // FIXME: This allows use of the VFS; we do not allow use of the
5042   // VFS when actually loading a module.
5043   auto Buffer = FileMgr.getBufferForFile(Filename);
5044   if (!Buffer) {
5045     return true;
5046   }
5047 
5048   // Initialize the stream
5049   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5050   BitstreamCursor Stream(Bytes);
5051 
5052   // Sniff for the signature.
5053   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5054     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5055     return true;
5056   }
5057 
5058   // Scan for the CONTROL_BLOCK_ID block.
5059   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5060     return true;
5061 
5062   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5063   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5064   bool NeedsImports = Listener.needsImportVisitation();
5065   BitstreamCursor InputFilesCursor;
5066 
5067   RecordData Record;
5068   std::string ModuleDir;
5069   bool DoneWithControlBlock = false;
5070   while (!DoneWithControlBlock) {
5071     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5072     if (!MaybeEntry) {
5073       // FIXME this drops the error on the floor.
5074       consumeError(MaybeEntry.takeError());
5075       return true;
5076     }
5077     llvm::BitstreamEntry Entry = MaybeEntry.get();
5078 
5079     switch (Entry.Kind) {
5080     case llvm::BitstreamEntry::SubBlock: {
5081       switch (Entry.ID) {
5082       case OPTIONS_BLOCK_ID: {
5083         std::string IgnoredSuggestedPredefines;
5084         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5085                              /*AllowCompatibleConfigurationMismatch*/ false,
5086                              Listener, IgnoredSuggestedPredefines) != Success)
5087           return true;
5088         break;
5089       }
5090 
5091       case INPUT_FILES_BLOCK_ID:
5092         InputFilesCursor = Stream;
5093         if (llvm::Error Err = Stream.SkipBlock()) {
5094           // FIXME this drops the error on the floor.
5095           consumeError(std::move(Err));
5096           return true;
5097         }
5098         if (NeedsInputFiles &&
5099             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5100           return true;
5101         break;
5102 
5103       default:
5104         if (llvm::Error Err = Stream.SkipBlock()) {
5105           // FIXME this drops the error on the floor.
5106           consumeError(std::move(Err));
5107           return true;
5108         }
5109         break;
5110       }
5111 
5112       continue;
5113     }
5114 
5115     case llvm::BitstreamEntry::EndBlock:
5116       DoneWithControlBlock = true;
5117       break;
5118 
5119     case llvm::BitstreamEntry::Error:
5120       return true;
5121 
5122     case llvm::BitstreamEntry::Record:
5123       break;
5124     }
5125 
5126     if (DoneWithControlBlock) break;
5127 
5128     Record.clear();
5129     StringRef Blob;
5130     Expected<unsigned> MaybeRecCode =
5131         Stream.readRecord(Entry.ID, Record, &Blob);
5132     if (!MaybeRecCode) {
5133       // FIXME this drops the error.
5134       return Failure;
5135     }
5136     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5137     case METADATA:
5138       if (Record[0] != VERSION_MAJOR)
5139         return true;
5140       if (Listener.ReadFullVersionInformation(Blob))
5141         return true;
5142       break;
5143     case MODULE_NAME:
5144       Listener.ReadModuleName(Blob);
5145       break;
5146     case MODULE_DIRECTORY:
5147       ModuleDir = Blob;
5148       break;
5149     case MODULE_MAP_FILE: {
5150       unsigned Idx = 0;
5151       auto Path = ReadString(Record, Idx);
5152       ResolveImportedPath(Path, ModuleDir);
5153       Listener.ReadModuleMapFile(Path);
5154       break;
5155     }
5156     case INPUT_FILE_OFFSETS: {
5157       if (!NeedsInputFiles)
5158         break;
5159 
5160       unsigned NumInputFiles = Record[0];
5161       unsigned NumUserFiles = Record[1];
5162       const llvm::support::unaligned_uint64_t *InputFileOffs =
5163           (const llvm::support::unaligned_uint64_t *)Blob.data();
5164       for (unsigned I = 0; I != NumInputFiles; ++I) {
5165         // Go find this input file.
5166         bool isSystemFile = I >= NumUserFiles;
5167 
5168         if (isSystemFile && !NeedsSystemInputFiles)
5169           break; // the rest are system input files
5170 
5171         BitstreamCursor &Cursor = InputFilesCursor;
5172         SavedStreamPosition SavedPosition(Cursor);
5173         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5174           // FIXME this drops errors on the floor.
5175           consumeError(std::move(Err));
5176         }
5177 
5178         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5179         if (!MaybeCode) {
5180           // FIXME this drops errors on the floor.
5181           consumeError(MaybeCode.takeError());
5182         }
5183         unsigned Code = MaybeCode.get();
5184 
5185         RecordData Record;
5186         StringRef Blob;
5187         bool shouldContinue = false;
5188         Expected<unsigned> MaybeRecordType =
5189             Cursor.readRecord(Code, Record, &Blob);
5190         if (!MaybeRecordType) {
5191           // FIXME this drops errors on the floor.
5192           consumeError(MaybeRecordType.takeError());
5193         }
5194         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5195         case INPUT_FILE:
5196           bool Overridden = static_cast<bool>(Record[3]);
5197           std::string Filename = Blob;
5198           ResolveImportedPath(Filename, ModuleDir);
5199           shouldContinue = Listener.visitInputFile(
5200               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5201           break;
5202         }
5203         if (!shouldContinue)
5204           break;
5205       }
5206       break;
5207     }
5208 
5209     case IMPORTS: {
5210       if (!NeedsImports)
5211         break;
5212 
5213       unsigned Idx = 0, N = Record.size();
5214       while (Idx < N) {
5215         // Read information about the AST file.
5216         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5217         std::string ModuleName = ReadString(Record, Idx);
5218         std::string Filename = ReadString(Record, Idx);
5219         ResolveImportedPath(Filename, ModuleDir);
5220         Listener.visitImport(ModuleName, Filename);
5221       }
5222       break;
5223     }
5224 
5225     default:
5226       // No other validation to perform.
5227       break;
5228     }
5229   }
5230 
5231   // Look for module file extension blocks, if requested.
5232   if (FindModuleFileExtensions) {
5233     BitstreamCursor SavedStream = Stream;
5234     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5235       bool DoneWithExtensionBlock = false;
5236       while (!DoneWithExtensionBlock) {
5237         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5238         if (!MaybeEntry) {
5239           // FIXME this drops the error.
5240           return true;
5241         }
5242         llvm::BitstreamEntry Entry = MaybeEntry.get();
5243 
5244         switch (Entry.Kind) {
5245         case llvm::BitstreamEntry::SubBlock:
5246           if (llvm::Error Err = Stream.SkipBlock()) {
5247             // FIXME this drops the error on the floor.
5248             consumeError(std::move(Err));
5249             return true;
5250           }
5251           continue;
5252 
5253         case llvm::BitstreamEntry::EndBlock:
5254           DoneWithExtensionBlock = true;
5255           continue;
5256 
5257         case llvm::BitstreamEntry::Error:
5258           return true;
5259 
5260         case llvm::BitstreamEntry::Record:
5261           break;
5262         }
5263 
5264        Record.clear();
5265        StringRef Blob;
5266        Expected<unsigned> MaybeRecCode =
5267            Stream.readRecord(Entry.ID, Record, &Blob);
5268        if (!MaybeRecCode) {
5269          // FIXME this drops the error.
5270          return true;
5271        }
5272        switch (MaybeRecCode.get()) {
5273        case EXTENSION_METADATA: {
5274          ModuleFileExtensionMetadata Metadata;
5275          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5276            return true;
5277 
5278          Listener.readModuleFileExtension(Metadata);
5279          break;
5280        }
5281        }
5282       }
5283     }
5284     Stream = SavedStream;
5285   }
5286 
5287   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5288   if (readUnhashedControlBlockImpl(
5289           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5290           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5291           ValidateDiagnosticOptions) != Success)
5292     return true;
5293 
5294   return false;
5295 }
5296 
5297 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5298                                     const PCHContainerReader &PCHContainerRdr,
5299                                     const LangOptions &LangOpts,
5300                                     const TargetOptions &TargetOpts,
5301                                     const PreprocessorOptions &PPOpts,
5302                                     StringRef ExistingModuleCachePath) {
5303   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5304                                ExistingModuleCachePath, FileMgr);
5305   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5306                                   /*FindModuleFileExtensions=*/false,
5307                                   validator,
5308                                   /*ValidateDiagnosticOptions=*/true);
5309 }
5310 
5311 ASTReader::ASTReadResult
5312 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5313   // Enter the submodule block.
5314   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5315     Error(std::move(Err));
5316     return Failure;
5317   }
5318 
5319   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5320   bool First = true;
5321   Module *CurrentModule = nullptr;
5322   RecordData Record;
5323   while (true) {
5324     Expected<llvm::BitstreamEntry> MaybeEntry =
5325         F.Stream.advanceSkippingSubblocks();
5326     if (!MaybeEntry) {
5327       Error(MaybeEntry.takeError());
5328       return Failure;
5329     }
5330     llvm::BitstreamEntry Entry = MaybeEntry.get();
5331 
5332     switch (Entry.Kind) {
5333     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5334     case llvm::BitstreamEntry::Error:
5335       Error("malformed block record in AST file");
5336       return Failure;
5337     case llvm::BitstreamEntry::EndBlock:
5338       return Success;
5339     case llvm::BitstreamEntry::Record:
5340       // The interesting case.
5341       break;
5342     }
5343 
5344     // Read a record.
5345     StringRef Blob;
5346     Record.clear();
5347     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5348     if (!MaybeKind) {
5349       Error(MaybeKind.takeError());
5350       return Failure;
5351     }
5352     unsigned Kind = MaybeKind.get();
5353 
5354     if ((Kind == SUBMODULE_METADATA) != First) {
5355       Error("submodule metadata record should be at beginning of block");
5356       return Failure;
5357     }
5358     First = false;
5359 
5360     // Submodule information is only valid if we have a current module.
5361     // FIXME: Should we error on these cases?
5362     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5363         Kind != SUBMODULE_DEFINITION)
5364       continue;
5365 
5366     switch (Kind) {
5367     default:  // Default behavior: ignore.
5368       break;
5369 
5370     case SUBMODULE_DEFINITION: {
5371       if (Record.size() < 12) {
5372         Error("malformed module definition");
5373         return Failure;
5374       }
5375 
5376       StringRef Name = Blob;
5377       unsigned Idx = 0;
5378       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5379       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5380       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5381       bool IsFramework = Record[Idx++];
5382       bool IsExplicit = Record[Idx++];
5383       bool IsSystem = Record[Idx++];
5384       bool IsExternC = Record[Idx++];
5385       bool InferSubmodules = Record[Idx++];
5386       bool InferExplicitSubmodules = Record[Idx++];
5387       bool InferExportWildcard = Record[Idx++];
5388       bool ConfigMacrosExhaustive = Record[Idx++];
5389       bool ModuleMapIsPrivate = Record[Idx++];
5390 
5391       Module *ParentModule = nullptr;
5392       if (Parent)
5393         ParentModule = getSubmodule(Parent);
5394 
5395       // Retrieve this (sub)module from the module map, creating it if
5396       // necessary.
5397       CurrentModule =
5398           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5399               .first;
5400 
5401       // FIXME: set the definition loc for CurrentModule, or call
5402       // ModMap.setInferredModuleAllowedBy()
5403 
5404       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5405       if (GlobalIndex >= SubmodulesLoaded.size() ||
5406           SubmodulesLoaded[GlobalIndex]) {
5407         Error("too many submodules");
5408         return Failure;
5409       }
5410 
5411       if (!ParentModule) {
5412         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5413           // Don't emit module relocation error if we have -fno-validate-pch
5414           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5415               CurFile != F.File) {
5416             if (!Diags.isDiagnosticInFlight()) {
5417               Diag(diag::err_module_file_conflict)
5418                 << CurrentModule->getTopLevelModuleName()
5419                 << CurFile->getName()
5420                 << F.File->getName();
5421             }
5422             return Failure;
5423           }
5424         }
5425 
5426         CurrentModule->setASTFile(F.File);
5427         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5428       }
5429 
5430       CurrentModule->Kind = Kind;
5431       CurrentModule->Signature = F.Signature;
5432       CurrentModule->IsFromModuleFile = true;
5433       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5434       CurrentModule->IsExternC = IsExternC;
5435       CurrentModule->InferSubmodules = InferSubmodules;
5436       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5437       CurrentModule->InferExportWildcard = InferExportWildcard;
5438       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5439       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5440       if (DeserializationListener)
5441         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5442 
5443       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5444 
5445       // Clear out data that will be replaced by what is in the module file.
5446       CurrentModule->LinkLibraries.clear();
5447       CurrentModule->ConfigMacros.clear();
5448       CurrentModule->UnresolvedConflicts.clear();
5449       CurrentModule->Conflicts.clear();
5450 
5451       // The module is available unless it's missing a requirement; relevant
5452       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5453       // Missing headers that were present when the module was built do not
5454       // make it unavailable -- if we got this far, this must be an explicitly
5455       // imported module file.
5456       CurrentModule->Requirements.clear();
5457       CurrentModule->MissingHeaders.clear();
5458       CurrentModule->IsMissingRequirement =
5459           ParentModule && ParentModule->IsMissingRequirement;
5460       CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
5461       break;
5462     }
5463 
5464     case SUBMODULE_UMBRELLA_HEADER: {
5465       std::string Filename = Blob;
5466       ResolveImportedPath(F, Filename);
5467       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5468         if (!CurrentModule->getUmbrellaHeader())
5469           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5470         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5471           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5472             Error("mismatched umbrella headers in submodule");
5473           return OutOfDate;
5474         }
5475       }
5476       break;
5477     }
5478 
5479     case SUBMODULE_HEADER:
5480     case SUBMODULE_EXCLUDED_HEADER:
5481     case SUBMODULE_PRIVATE_HEADER:
5482       // We lazily associate headers with their modules via the HeaderInfo table.
5483       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5484       // of complete filenames or remove it entirely.
5485       break;
5486 
5487     case SUBMODULE_TEXTUAL_HEADER:
5488     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5489       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5490       // them here.
5491       break;
5492 
5493     case SUBMODULE_TOPHEADER:
5494       CurrentModule->addTopHeaderFilename(Blob);
5495       break;
5496 
5497     case SUBMODULE_UMBRELLA_DIR: {
5498       std::string Dirname = Blob;
5499       ResolveImportedPath(F, Dirname);
5500       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5501         if (!CurrentModule->getUmbrellaDir())
5502           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5503         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5504           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5505             Error("mismatched umbrella directories in submodule");
5506           return OutOfDate;
5507         }
5508       }
5509       break;
5510     }
5511 
5512     case SUBMODULE_METADATA: {
5513       F.BaseSubmoduleID = getTotalNumSubmodules();
5514       F.LocalNumSubmodules = Record[0];
5515       unsigned LocalBaseSubmoduleID = Record[1];
5516       if (F.LocalNumSubmodules > 0) {
5517         // Introduce the global -> local mapping for submodules within this
5518         // module.
5519         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5520 
5521         // Introduce the local -> global mapping for submodules within this
5522         // module.
5523         F.SubmoduleRemap.insertOrReplace(
5524           std::make_pair(LocalBaseSubmoduleID,
5525                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5526 
5527         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5528       }
5529       break;
5530     }
5531 
5532     case SUBMODULE_IMPORTS:
5533       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5534         UnresolvedModuleRef Unresolved;
5535         Unresolved.File = &F;
5536         Unresolved.Mod = CurrentModule;
5537         Unresolved.ID = Record[Idx];
5538         Unresolved.Kind = UnresolvedModuleRef::Import;
5539         Unresolved.IsWildcard = false;
5540         UnresolvedModuleRefs.push_back(Unresolved);
5541       }
5542       break;
5543 
5544     case SUBMODULE_EXPORTS:
5545       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5546         UnresolvedModuleRef Unresolved;
5547         Unresolved.File = &F;
5548         Unresolved.Mod = CurrentModule;
5549         Unresolved.ID = Record[Idx];
5550         Unresolved.Kind = UnresolvedModuleRef::Export;
5551         Unresolved.IsWildcard = Record[Idx + 1];
5552         UnresolvedModuleRefs.push_back(Unresolved);
5553       }
5554 
5555       // Once we've loaded the set of exports, there's no reason to keep
5556       // the parsed, unresolved exports around.
5557       CurrentModule->UnresolvedExports.clear();
5558       break;
5559 
5560     case SUBMODULE_REQUIRES:
5561       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5562                                     PP.getTargetInfo());
5563       break;
5564 
5565     case SUBMODULE_LINK_LIBRARY:
5566       ModMap.resolveLinkAsDependencies(CurrentModule);
5567       CurrentModule->LinkLibraries.push_back(
5568                                          Module::LinkLibrary(Blob, Record[0]));
5569       break;
5570 
5571     case SUBMODULE_CONFIG_MACRO:
5572       CurrentModule->ConfigMacros.push_back(Blob.str());
5573       break;
5574 
5575     case SUBMODULE_CONFLICT: {
5576       UnresolvedModuleRef Unresolved;
5577       Unresolved.File = &F;
5578       Unresolved.Mod = CurrentModule;
5579       Unresolved.ID = Record[0];
5580       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5581       Unresolved.IsWildcard = false;
5582       Unresolved.String = Blob;
5583       UnresolvedModuleRefs.push_back(Unresolved);
5584       break;
5585     }
5586 
5587     case SUBMODULE_INITIALIZERS: {
5588       if (!ContextObj)
5589         break;
5590       SmallVector<uint32_t, 16> Inits;
5591       for (auto &ID : Record)
5592         Inits.push_back(getGlobalDeclID(F, ID));
5593       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5594       break;
5595     }
5596 
5597     case SUBMODULE_EXPORT_AS:
5598       CurrentModule->ExportAsModule = Blob.str();
5599       ModMap.addLinkAsDependency(CurrentModule);
5600       break;
5601     }
5602   }
5603 }
5604 
5605 /// Parse the record that corresponds to a LangOptions data
5606 /// structure.
5607 ///
5608 /// This routine parses the language options from the AST file and then gives
5609 /// them to the AST listener if one is set.
5610 ///
5611 /// \returns true if the listener deems the file unacceptable, false otherwise.
5612 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5613                                      bool Complain,
5614                                      ASTReaderListener &Listener,
5615                                      bool AllowCompatibleDifferences) {
5616   LangOptions LangOpts;
5617   unsigned Idx = 0;
5618 #define LANGOPT(Name, Bits, Default, Description) \
5619   LangOpts.Name = Record[Idx++];
5620 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5621   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5622 #include "clang/Basic/LangOptions.def"
5623 #define SANITIZER(NAME, ID)                                                    \
5624   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5625 #include "clang/Basic/Sanitizers.def"
5626 
5627   for (unsigned N = Record[Idx++]; N; --N)
5628     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5629 
5630   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5631   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5632   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5633 
5634   LangOpts.CurrentModule = ReadString(Record, Idx);
5635 
5636   // Comment options.
5637   for (unsigned N = Record[Idx++]; N; --N) {
5638     LangOpts.CommentOpts.BlockCommandNames.push_back(
5639       ReadString(Record, Idx));
5640   }
5641   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5642 
5643   // OpenMP offloading options.
5644   for (unsigned N = Record[Idx++]; N; --N) {
5645     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5646   }
5647 
5648   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5649 
5650   return Listener.ReadLanguageOptions(LangOpts, Complain,
5651                                       AllowCompatibleDifferences);
5652 }
5653 
5654 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5655                                    ASTReaderListener &Listener,
5656                                    bool AllowCompatibleDifferences) {
5657   unsigned Idx = 0;
5658   TargetOptions TargetOpts;
5659   TargetOpts.Triple = ReadString(Record, Idx);
5660   TargetOpts.CPU = ReadString(Record, Idx);
5661   TargetOpts.ABI = ReadString(Record, Idx);
5662   for (unsigned N = Record[Idx++]; N; --N) {
5663     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5664   }
5665   for (unsigned N = Record[Idx++]; N; --N) {
5666     TargetOpts.Features.push_back(ReadString(Record, Idx));
5667   }
5668 
5669   return Listener.ReadTargetOptions(TargetOpts, Complain,
5670                                     AllowCompatibleDifferences);
5671 }
5672 
5673 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5674                                        ASTReaderListener &Listener) {
5675   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5676   unsigned Idx = 0;
5677 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5678 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5679   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5680 #include "clang/Basic/DiagnosticOptions.def"
5681 
5682   for (unsigned N = Record[Idx++]; N; --N)
5683     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5684   for (unsigned N = Record[Idx++]; N; --N)
5685     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5686 
5687   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5688 }
5689 
5690 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5691                                        ASTReaderListener &Listener) {
5692   FileSystemOptions FSOpts;
5693   unsigned Idx = 0;
5694   FSOpts.WorkingDir = ReadString(Record, Idx);
5695   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5696 }
5697 
5698 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5699                                          bool Complain,
5700                                          ASTReaderListener &Listener) {
5701   HeaderSearchOptions HSOpts;
5702   unsigned Idx = 0;
5703   HSOpts.Sysroot = ReadString(Record, Idx);
5704 
5705   // Include entries.
5706   for (unsigned N = Record[Idx++]; N; --N) {
5707     std::string Path = ReadString(Record, Idx);
5708     frontend::IncludeDirGroup Group
5709       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5710     bool IsFramework = Record[Idx++];
5711     bool IgnoreSysRoot = Record[Idx++];
5712     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5713                                     IgnoreSysRoot);
5714   }
5715 
5716   // System header prefixes.
5717   for (unsigned N = Record[Idx++]; N; --N) {
5718     std::string Prefix = ReadString(Record, Idx);
5719     bool IsSystemHeader = Record[Idx++];
5720     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5721   }
5722 
5723   HSOpts.ResourceDir = ReadString(Record, Idx);
5724   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5725   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5726   HSOpts.DisableModuleHash = Record[Idx++];
5727   HSOpts.ImplicitModuleMaps = Record[Idx++];
5728   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5729   HSOpts.UseBuiltinIncludes = Record[Idx++];
5730   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5731   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5732   HSOpts.UseLibcxx = Record[Idx++];
5733   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5734 
5735   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5736                                           Complain);
5737 }
5738 
5739 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5740                                          bool Complain,
5741                                          ASTReaderListener &Listener,
5742                                          std::string &SuggestedPredefines) {
5743   PreprocessorOptions PPOpts;
5744   unsigned Idx = 0;
5745 
5746   // Macro definitions/undefs
5747   for (unsigned N = Record[Idx++]; N; --N) {
5748     std::string Macro = ReadString(Record, Idx);
5749     bool IsUndef = Record[Idx++];
5750     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5751   }
5752 
5753   // Includes
5754   for (unsigned N = Record[Idx++]; N; --N) {
5755     PPOpts.Includes.push_back(ReadString(Record, Idx));
5756   }
5757 
5758   // Macro Includes
5759   for (unsigned N = Record[Idx++]; N; --N) {
5760     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5761   }
5762 
5763   PPOpts.UsePredefines = Record[Idx++];
5764   PPOpts.DetailedRecord = Record[Idx++];
5765   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5766   PPOpts.ObjCXXARCStandardLibrary =
5767     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5768   SuggestedPredefines.clear();
5769   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5770                                           SuggestedPredefines);
5771 }
5772 
5773 std::pair<ModuleFile *, unsigned>
5774 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5775   GlobalPreprocessedEntityMapType::iterator
5776   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5777   assert(I != GlobalPreprocessedEntityMap.end() &&
5778          "Corrupted global preprocessed entity map");
5779   ModuleFile *M = I->second;
5780   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5781   return std::make_pair(M, LocalIndex);
5782 }
5783 
5784 llvm::iterator_range<PreprocessingRecord::iterator>
5785 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5786   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5787     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5788                                              Mod.NumPreprocessedEntities);
5789 
5790   return llvm::make_range(PreprocessingRecord::iterator(),
5791                           PreprocessingRecord::iterator());
5792 }
5793 
5794 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5795 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5796   return llvm::make_range(
5797       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5798       ModuleDeclIterator(this, &Mod,
5799                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5800 }
5801 
5802 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5803   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5804   assert(I != GlobalSkippedRangeMap.end() &&
5805     "Corrupted global skipped range map");
5806   ModuleFile *M = I->second;
5807   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5808   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5809   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5810   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5811                     TranslateSourceLocation(*M, RawRange.getEnd()));
5812   assert(Range.isValid());
5813   return Range;
5814 }
5815 
5816 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5817   PreprocessedEntityID PPID = Index+1;
5818   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5819   ModuleFile &M = *PPInfo.first;
5820   unsigned LocalIndex = PPInfo.second;
5821   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5822 
5823   if (!PP.getPreprocessingRecord()) {
5824     Error("no preprocessing record");
5825     return nullptr;
5826   }
5827 
5828   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5829   if (llvm::Error Err =
5830           M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5831     Error(std::move(Err));
5832     return nullptr;
5833   }
5834 
5835   Expected<llvm::BitstreamEntry> MaybeEntry =
5836       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5837   if (!MaybeEntry) {
5838     Error(MaybeEntry.takeError());
5839     return nullptr;
5840   }
5841   llvm::BitstreamEntry Entry = MaybeEntry.get();
5842 
5843   if (Entry.Kind != llvm::BitstreamEntry::Record)
5844     return nullptr;
5845 
5846   // Read the record.
5847   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5848                     TranslateSourceLocation(M, PPOffs.getEnd()));
5849   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5850   StringRef Blob;
5851   RecordData Record;
5852   Expected<unsigned> MaybeRecType =
5853       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5854   if (!MaybeRecType) {
5855     Error(MaybeRecType.takeError());
5856     return nullptr;
5857   }
5858   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5859   case PPD_MACRO_EXPANSION: {
5860     bool isBuiltin = Record[0];
5861     IdentifierInfo *Name = nullptr;
5862     MacroDefinitionRecord *Def = nullptr;
5863     if (isBuiltin)
5864       Name = getLocalIdentifier(M, Record[1]);
5865     else {
5866       PreprocessedEntityID GlobalID =
5867           getGlobalPreprocessedEntityID(M, Record[1]);
5868       Def = cast<MacroDefinitionRecord>(
5869           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5870     }
5871 
5872     MacroExpansion *ME;
5873     if (isBuiltin)
5874       ME = new (PPRec) MacroExpansion(Name, Range);
5875     else
5876       ME = new (PPRec) MacroExpansion(Def, Range);
5877 
5878     return ME;
5879   }
5880 
5881   case PPD_MACRO_DEFINITION: {
5882     // Decode the identifier info and then check again; if the macro is
5883     // still defined and associated with the identifier,
5884     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5885     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5886 
5887     if (DeserializationListener)
5888       DeserializationListener->MacroDefinitionRead(PPID, MD);
5889 
5890     return MD;
5891   }
5892 
5893   case PPD_INCLUSION_DIRECTIVE: {
5894     const char *FullFileNameStart = Blob.data() + Record[0];
5895     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
5896     const FileEntry *File = nullptr;
5897     if (!FullFileName.empty())
5898       if (auto FE = PP.getFileManager().getFile(FullFileName))
5899         File = *FE;
5900 
5901     // FIXME: Stable encoding
5902     InclusionDirective::InclusionKind Kind
5903       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5904     InclusionDirective *ID
5905       = new (PPRec) InclusionDirective(PPRec, Kind,
5906                                        StringRef(Blob.data(), Record[0]),
5907                                        Record[1], Record[3],
5908                                        File,
5909                                        Range);
5910     return ID;
5911   }
5912   }
5913 
5914   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5915 }
5916 
5917 /// Find the next module that contains entities and return the ID
5918 /// of the first entry.
5919 ///
5920 /// \param SLocMapI points at a chunk of a module that contains no
5921 /// preprocessed entities or the entities it contains are not the ones we are
5922 /// looking for.
5923 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5924                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5925   ++SLocMapI;
5926   for (GlobalSLocOffsetMapType::const_iterator
5927          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5928     ModuleFile &M = *SLocMapI->second;
5929     if (M.NumPreprocessedEntities)
5930       return M.BasePreprocessedEntityID;
5931   }
5932 
5933   return getTotalNumPreprocessedEntities();
5934 }
5935 
5936 namespace {
5937 
5938 struct PPEntityComp {
5939   const ASTReader &Reader;
5940   ModuleFile &M;
5941 
5942   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
5943 
5944   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5945     SourceLocation LHS = getLoc(L);
5946     SourceLocation RHS = getLoc(R);
5947     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5948   }
5949 
5950   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5951     SourceLocation LHS = getLoc(L);
5952     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5953   }
5954 
5955   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5956     SourceLocation RHS = getLoc(R);
5957     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5958   }
5959 
5960   SourceLocation getLoc(const PPEntityOffset &PPE) const {
5961     return Reader.TranslateSourceLocation(M, PPE.getBegin());
5962   }
5963 };
5964 
5965 } // namespace
5966 
5967 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5968                                                        bool EndsAfter) const {
5969   if (SourceMgr.isLocalSourceLocation(Loc))
5970     return getTotalNumPreprocessedEntities();
5971 
5972   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5973       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
5974   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5975          "Corrupted global sloc offset map");
5976 
5977   if (SLocMapI->second->NumPreprocessedEntities == 0)
5978     return findNextPreprocessedEntity(SLocMapI);
5979 
5980   ModuleFile &M = *SLocMapI->second;
5981 
5982   using pp_iterator = const PPEntityOffset *;
5983 
5984   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5985   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5986 
5987   size_t Count = M.NumPreprocessedEntities;
5988   size_t Half;
5989   pp_iterator First = pp_begin;
5990   pp_iterator PPI;
5991 
5992   if (EndsAfter) {
5993     PPI = std::upper_bound(pp_begin, pp_end, Loc,
5994                            PPEntityComp(*this, M));
5995   } else {
5996     // Do a binary search manually instead of using std::lower_bound because
5997     // The end locations of entities may be unordered (when a macro expansion
5998     // is inside another macro argument), but for this case it is not important
5999     // whether we get the first macro expansion or its containing macro.
6000     while (Count > 0) {
6001       Half = Count / 2;
6002       PPI = First;
6003       std::advance(PPI, Half);
6004       if (SourceMgr.isBeforeInTranslationUnit(
6005               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6006         First = PPI;
6007         ++First;
6008         Count = Count - Half - 1;
6009       } else
6010         Count = Half;
6011     }
6012   }
6013 
6014   if (PPI == pp_end)
6015     return findNextPreprocessedEntity(SLocMapI);
6016 
6017   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6018 }
6019 
6020 /// Returns a pair of [Begin, End) indices of preallocated
6021 /// preprocessed entities that \arg Range encompasses.
6022 std::pair<unsigned, unsigned>
6023     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6024   if (Range.isInvalid())
6025     return std::make_pair(0,0);
6026   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6027 
6028   PreprocessedEntityID BeginID =
6029       findPreprocessedEntity(Range.getBegin(), false);
6030   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6031   return std::make_pair(BeginID, EndID);
6032 }
6033 
6034 /// Optionally returns true or false if the preallocated preprocessed
6035 /// entity with index \arg Index came from file \arg FID.
6036 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6037                                                              FileID FID) {
6038   if (FID.isInvalid())
6039     return false;
6040 
6041   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6042   ModuleFile &M = *PPInfo.first;
6043   unsigned LocalIndex = PPInfo.second;
6044   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6045 
6046   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6047   if (Loc.isInvalid())
6048     return false;
6049 
6050   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6051     return true;
6052   else
6053     return false;
6054 }
6055 
6056 namespace {
6057 
6058   /// Visitor used to search for information about a header file.
6059   class HeaderFileInfoVisitor {
6060     const FileEntry *FE;
6061     Optional<HeaderFileInfo> HFI;
6062 
6063   public:
6064     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6065 
6066     bool operator()(ModuleFile &M) {
6067       HeaderFileInfoLookupTable *Table
6068         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6069       if (!Table)
6070         return false;
6071 
6072       // Look in the on-disk hash table for an entry for this file name.
6073       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6074       if (Pos == Table->end())
6075         return false;
6076 
6077       HFI = *Pos;
6078       return true;
6079     }
6080 
6081     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6082   };
6083 
6084 } // namespace
6085 
6086 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6087   HeaderFileInfoVisitor Visitor(FE);
6088   ModuleMgr.visit(Visitor);
6089   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6090     return *HFI;
6091 
6092   return HeaderFileInfo();
6093 }
6094 
6095 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6096   using DiagState = DiagnosticsEngine::DiagState;
6097   SmallVector<DiagState *, 32> DiagStates;
6098 
6099   for (ModuleFile &F : ModuleMgr) {
6100     unsigned Idx = 0;
6101     auto &Record = F.PragmaDiagMappings;
6102     if (Record.empty())
6103       continue;
6104 
6105     DiagStates.clear();
6106 
6107     auto ReadDiagState =
6108         [&](const DiagState &BasedOn, SourceLocation Loc,
6109             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6110       unsigned BackrefID = Record[Idx++];
6111       if (BackrefID != 0)
6112         return DiagStates[BackrefID - 1];
6113 
6114       // A new DiagState was created here.
6115       Diag.DiagStates.push_back(BasedOn);
6116       DiagState *NewState = &Diag.DiagStates.back();
6117       DiagStates.push_back(NewState);
6118       unsigned Size = Record[Idx++];
6119       assert(Idx + Size * 2 <= Record.size() &&
6120              "Invalid data, not enough diag/map pairs");
6121       while (Size--) {
6122         unsigned DiagID = Record[Idx++];
6123         DiagnosticMapping NewMapping =
6124             DiagnosticMapping::deserialize(Record[Idx++]);
6125         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6126           continue;
6127 
6128         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6129 
6130         // If this mapping was specified as a warning but the severity was
6131         // upgraded due to diagnostic settings, simulate the current diagnostic
6132         // settings (and use a warning).
6133         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6134           NewMapping.setSeverity(diag::Severity::Warning);
6135           NewMapping.setUpgradedFromWarning(false);
6136         }
6137 
6138         Mapping = NewMapping;
6139       }
6140       return NewState;
6141     };
6142 
6143     // Read the first state.
6144     DiagState *FirstState;
6145     if (F.Kind == MK_ImplicitModule) {
6146       // Implicitly-built modules are reused with different diagnostic
6147       // settings.  Use the initial diagnostic state from Diag to simulate this
6148       // compilation's diagnostic settings.
6149       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6150       DiagStates.push_back(FirstState);
6151 
6152       // Skip the initial diagnostic state from the serialized module.
6153       assert(Record[1] == 0 &&
6154              "Invalid data, unexpected backref in initial state");
6155       Idx = 3 + Record[2] * 2;
6156       assert(Idx < Record.size() &&
6157              "Invalid data, not enough state change pairs in initial state");
6158     } else if (F.isModule()) {
6159       // For an explicit module, preserve the flags from the module build
6160       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6161       // -Wblah flags.
6162       unsigned Flags = Record[Idx++];
6163       DiagState Initial;
6164       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6165       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6166       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6167       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6168       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6169       Initial.ExtBehavior = (diag::Severity)Flags;
6170       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6171 
6172       assert(F.OriginalSourceFileID.isValid());
6173 
6174       // Set up the root buffer of the module to start with the initial
6175       // diagnostic state of the module itself, to cover files that contain no
6176       // explicit transitions (for which we did not serialize anything).
6177       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6178           .StateTransitions.push_back({FirstState, 0});
6179     } else {
6180       // For prefix ASTs, start with whatever the user configured on the
6181       // command line.
6182       Idx++; // Skip flags.
6183       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6184                                  SourceLocation(), false);
6185     }
6186 
6187     // Read the state transitions.
6188     unsigned NumLocations = Record[Idx++];
6189     while (NumLocations--) {
6190       assert(Idx < Record.size() &&
6191              "Invalid data, missing pragma diagnostic states");
6192       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6193       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6194       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6195       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6196       unsigned Transitions = Record[Idx++];
6197 
6198       // Note that we don't need to set up Parent/ParentOffset here, because
6199       // we won't be changing the diagnostic state within imported FileIDs
6200       // (other than perhaps appending to the main source file, which has no
6201       // parent).
6202       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6203       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6204       for (unsigned I = 0; I != Transitions; ++I) {
6205         unsigned Offset = Record[Idx++];
6206         auto *State =
6207             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6208         F.StateTransitions.push_back({State, Offset});
6209       }
6210     }
6211 
6212     // Read the final state.
6213     assert(Idx < Record.size() &&
6214            "Invalid data, missing final pragma diagnostic state");
6215     SourceLocation CurStateLoc =
6216         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6217     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6218 
6219     if (!F.isModule()) {
6220       Diag.DiagStatesByLoc.CurDiagState = CurState;
6221       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6222 
6223       // Preserve the property that the imaginary root file describes the
6224       // current state.
6225       FileID NullFile;
6226       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6227       if (T.empty())
6228         T.push_back({CurState, 0});
6229       else
6230         T[0].State = CurState;
6231     }
6232 
6233     // Don't try to read these mappings again.
6234     Record.clear();
6235   }
6236 }
6237 
6238 /// Get the correct cursor and offset for loading a type.
6239 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6240   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6241   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6242   ModuleFile *M = I->second;
6243   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6244 }
6245 
6246 /// Read and return the type with the given index..
6247 ///
6248 /// The index is the type ID, shifted and minus the number of predefs. This
6249 /// routine actually reads the record corresponding to the type at the given
6250 /// location. It is a helper routine for GetType, which deals with reading type
6251 /// IDs.
6252 QualType ASTReader::readTypeRecord(unsigned Index) {
6253   assert(ContextObj && "reading type with no AST context");
6254   ASTContext &Context = *ContextObj;
6255   RecordLocation Loc = TypeCursorForIndex(Index);
6256   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6257 
6258   // Keep track of where we are in the stream, then jump back there
6259   // after reading this type.
6260   SavedStreamPosition SavedPosition(DeclsCursor);
6261 
6262   ReadingKindTracker ReadingKind(Read_Type, *this);
6263 
6264   // Note that we are loading a type record.
6265   Deserializing AType(this);
6266 
6267   unsigned Idx = 0;
6268   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6269     Error(std::move(Err));
6270     return QualType();
6271   }
6272   RecordData Record;
6273   Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
6274   if (!MaybeCode) {
6275     Error(MaybeCode.takeError());
6276     return QualType();
6277   }
6278   unsigned Code = MaybeCode.get();
6279 
6280   Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record);
6281   if (!MaybeTypeCode) {
6282     Error(MaybeTypeCode.takeError());
6283     return QualType();
6284   }
6285   switch ((TypeCode)MaybeTypeCode.get()) {
6286   case TYPE_EXT_QUAL: {
6287     if (Record.size() != 2) {
6288       Error("Incorrect encoding of extended qualifier type");
6289       return QualType();
6290     }
6291     QualType Base = readType(*Loc.F, Record, Idx);
6292     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6293     return Context.getQualifiedType(Base, Quals);
6294   }
6295 
6296   case TYPE_COMPLEX: {
6297     if (Record.size() != 1) {
6298       Error("Incorrect encoding of complex type");
6299       return QualType();
6300     }
6301     QualType ElemType = readType(*Loc.F, Record, Idx);
6302     return Context.getComplexType(ElemType);
6303   }
6304 
6305   case TYPE_POINTER: {
6306     if (Record.size() != 1) {
6307       Error("Incorrect encoding of pointer type");
6308       return QualType();
6309     }
6310     QualType PointeeType = readType(*Loc.F, Record, Idx);
6311     return Context.getPointerType(PointeeType);
6312   }
6313 
6314   case TYPE_DECAYED: {
6315     if (Record.size() != 1) {
6316       Error("Incorrect encoding of decayed type");
6317       return QualType();
6318     }
6319     QualType OriginalType = readType(*Loc.F, Record, Idx);
6320     QualType DT = Context.getAdjustedParameterType(OriginalType);
6321     if (!isa<DecayedType>(DT))
6322       Error("Decayed type does not decay");
6323     return DT;
6324   }
6325 
6326   case TYPE_ADJUSTED: {
6327     if (Record.size() != 2) {
6328       Error("Incorrect encoding of adjusted type");
6329       return QualType();
6330     }
6331     QualType OriginalTy = readType(*Loc.F, Record, Idx);
6332     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
6333     return Context.getAdjustedType(OriginalTy, AdjustedTy);
6334   }
6335 
6336   case TYPE_BLOCK_POINTER: {
6337     if (Record.size() != 1) {
6338       Error("Incorrect encoding of block pointer type");
6339       return QualType();
6340     }
6341     QualType PointeeType = readType(*Loc.F, Record, Idx);
6342     return Context.getBlockPointerType(PointeeType);
6343   }
6344 
6345   case TYPE_LVALUE_REFERENCE: {
6346     if (Record.size() != 2) {
6347       Error("Incorrect encoding of lvalue reference type");
6348       return QualType();
6349     }
6350     QualType PointeeType = readType(*Loc.F, Record, Idx);
6351     return Context.getLValueReferenceType(PointeeType, Record[1]);
6352   }
6353 
6354   case TYPE_RVALUE_REFERENCE: {
6355     if (Record.size() != 1) {
6356       Error("Incorrect encoding of rvalue reference type");
6357       return QualType();
6358     }
6359     QualType PointeeType = readType(*Loc.F, Record, Idx);
6360     return Context.getRValueReferenceType(PointeeType);
6361   }
6362 
6363   case TYPE_MEMBER_POINTER: {
6364     if (Record.size() != 2) {
6365       Error("Incorrect encoding of member pointer type");
6366       return QualType();
6367     }
6368     QualType PointeeType = readType(*Loc.F, Record, Idx);
6369     QualType ClassType = readType(*Loc.F, Record, Idx);
6370     if (PointeeType.isNull() || ClassType.isNull())
6371       return QualType();
6372 
6373     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
6374   }
6375 
6376   case TYPE_CONSTANT_ARRAY: {
6377     QualType ElementType = readType(*Loc.F, Record, Idx);
6378     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6379     unsigned IndexTypeQuals = Record[2];
6380     unsigned Idx = 3;
6381     llvm::APInt Size = ReadAPInt(Record, Idx);
6382     return Context.getConstantArrayType(ElementType, Size,
6383                                          ASM, IndexTypeQuals);
6384   }
6385 
6386   case TYPE_INCOMPLETE_ARRAY: {
6387     QualType ElementType = readType(*Loc.F, Record, Idx);
6388     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6389     unsigned IndexTypeQuals = Record[2];
6390     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
6391   }
6392 
6393   case TYPE_VARIABLE_ARRAY: {
6394     QualType ElementType = readType(*Loc.F, Record, Idx);
6395     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
6396     unsigned IndexTypeQuals = Record[2];
6397     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
6398     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
6399     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
6400                                          ASM, IndexTypeQuals,
6401                                          SourceRange(LBLoc, RBLoc));
6402   }
6403 
6404   case TYPE_VECTOR: {
6405     if (Record.size() != 3) {
6406       Error("incorrect encoding of vector type in AST file");
6407       return QualType();
6408     }
6409 
6410     QualType ElementType = readType(*Loc.F, Record, Idx);
6411     unsigned NumElements = Record[1];
6412     unsigned VecKind = Record[2];
6413     return Context.getVectorType(ElementType, NumElements,
6414                                   (VectorType::VectorKind)VecKind);
6415   }
6416 
6417   case TYPE_EXT_VECTOR: {
6418     if (Record.size() != 3) {
6419       Error("incorrect encoding of extended vector type in AST file");
6420       return QualType();
6421     }
6422 
6423     QualType ElementType = readType(*Loc.F, Record, Idx);
6424     unsigned NumElements = Record[1];
6425     return Context.getExtVectorType(ElementType, NumElements);
6426   }
6427 
6428   case TYPE_FUNCTION_NO_PROTO: {
6429     if (Record.size() != 8) {
6430       Error("incorrect encoding of no-proto function type");
6431       return QualType();
6432     }
6433     QualType ResultType = readType(*Loc.F, Record, Idx);
6434     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
6435                                (CallingConv)Record[4], Record[5], Record[6],
6436                                Record[7]);
6437     return Context.getFunctionNoProtoType(ResultType, Info);
6438   }
6439 
6440   case TYPE_FUNCTION_PROTO: {
6441     QualType ResultType = readType(*Loc.F, Record, Idx);
6442 
6443     FunctionProtoType::ExtProtoInfo EPI;
6444     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6445                                         /*hasregparm*/ Record[2],
6446                                         /*regparm*/ Record[3],
6447                                         static_cast<CallingConv>(Record[4]),
6448                                         /*produces*/ Record[5],
6449                                         /*nocallersavedregs*/ Record[6],
6450                                         /*nocfcheck*/ Record[7]);
6451 
6452     unsigned Idx = 8;
6453 
6454     EPI.Variadic = Record[Idx++];
6455     EPI.HasTrailingReturn = Record[Idx++];
6456     EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]);
6457     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
6458     SmallVector<QualType, 8> ExceptionStorage;
6459     readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
6460 
6461     unsigned NumParams = Record[Idx++];
6462     SmallVector<QualType, 16> ParamTypes;
6463     for (unsigned I = 0; I != NumParams; ++I)
6464       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6465 
6466     SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6467     if (Idx != Record.size()) {
6468       for (unsigned I = 0; I != NumParams; ++I)
6469         ExtParameterInfos.push_back(
6470           FunctionProtoType::ExtParameterInfo
6471                            ::getFromOpaqueValue(Record[Idx++]));
6472       EPI.ExtParameterInfos = ExtParameterInfos.data();
6473     }
6474 
6475     assert(Idx == Record.size());
6476 
6477     return Context.getFunctionType(ResultType, ParamTypes, EPI);
6478   }
6479 
6480   case TYPE_UNRESOLVED_USING: {
6481     unsigned Idx = 0;
6482     return Context.getTypeDeclType(
6483                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6484   }
6485 
6486   case TYPE_TYPEDEF: {
6487     if (Record.size() != 2) {
6488       Error("incorrect encoding of typedef type");
6489       return QualType();
6490     }
6491     unsigned Idx = 0;
6492     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
6493     QualType Canonical = readType(*Loc.F, Record, Idx);
6494     if (!Canonical.isNull())
6495       Canonical = Context.getCanonicalType(Canonical);
6496     return Context.getTypedefType(Decl, Canonical);
6497   }
6498 
6499   case TYPE_TYPEOF_EXPR:
6500     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6501 
6502   case TYPE_TYPEOF: {
6503     if (Record.size() != 1) {
6504       Error("incorrect encoding of typeof(type) in AST file");
6505       return QualType();
6506     }
6507     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6508     return Context.getTypeOfType(UnderlyingType);
6509   }
6510 
6511   case TYPE_DECLTYPE: {
6512     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6513     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6514   }
6515 
6516   case TYPE_UNARY_TRANSFORM: {
6517     QualType BaseType = readType(*Loc.F, Record, Idx);
6518     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6519     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
6520     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6521   }
6522 
6523   case TYPE_AUTO: {
6524     QualType Deduced = readType(*Loc.F, Record, Idx);
6525     AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
6526     bool IsDependent = false, IsPack = false;
6527     if (Deduced.isNull()) {
6528       IsDependent = Record[Idx] > 0;
6529       IsPack = Record[Idx] > 1;
6530       ++Idx;
6531     }
6532     return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack);
6533   }
6534 
6535   case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6536     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6537     QualType Deduced = readType(*Loc.F, Record, Idx);
6538     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6539     return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6540                                                         IsDependent);
6541   }
6542 
6543   case TYPE_RECORD: {
6544     if (Record.size() != 2) {
6545       Error("incorrect encoding of record type");
6546       return QualType();
6547     }
6548     unsigned Idx = 0;
6549     bool IsDependent = Record[Idx++];
6550     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
6551     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6552     QualType T = Context.getRecordType(RD);
6553     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6554     return T;
6555   }
6556 
6557   case TYPE_ENUM: {
6558     if (Record.size() != 2) {
6559       Error("incorrect encoding of enum type");
6560       return QualType();
6561     }
6562     unsigned Idx = 0;
6563     bool IsDependent = Record[Idx++];
6564     QualType T
6565       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6566     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6567     return T;
6568   }
6569 
6570   case TYPE_ATTRIBUTED: {
6571     if (Record.size() != 3) {
6572       Error("incorrect encoding of attributed type");
6573       return QualType();
6574     }
6575     QualType modifiedType = readType(*Loc.F, Record, Idx);
6576     QualType equivalentType = readType(*Loc.F, Record, Idx);
6577     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
6578     return Context.getAttributedType(kind, modifiedType, equivalentType);
6579   }
6580 
6581   case TYPE_PAREN: {
6582     if (Record.size() != 1) {
6583       Error("incorrect encoding of paren type");
6584       return QualType();
6585     }
6586     QualType InnerType = readType(*Loc.F, Record, Idx);
6587     return Context.getParenType(InnerType);
6588   }
6589 
6590   case TYPE_MACRO_QUALIFIED: {
6591     if (Record.size() != 2) {
6592       Error("incorrect encoding of macro defined type");
6593       return QualType();
6594     }
6595     QualType UnderlyingTy = readType(*Loc.F, Record, Idx);
6596     IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx);
6597     return Context.getMacroQualifiedType(UnderlyingTy, MacroII);
6598   }
6599 
6600   case TYPE_PACK_EXPANSION: {
6601     if (Record.size() != 2) {
6602       Error("incorrect encoding of pack expansion type");
6603       return QualType();
6604     }
6605     QualType Pattern = readType(*Loc.F, Record, Idx);
6606     if (Pattern.isNull())
6607       return QualType();
6608     Optional<unsigned> NumExpansions;
6609     if (Record[1])
6610       NumExpansions = Record[1] - 1;
6611     return Context.getPackExpansionType(Pattern, NumExpansions);
6612   }
6613 
6614   case TYPE_ELABORATED: {
6615     unsigned Idx = 0;
6616     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6617     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6618     QualType NamedType = readType(*Loc.F, Record, Idx);
6619     TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6620     return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
6621   }
6622 
6623   case TYPE_OBJC_INTERFACE: {
6624     unsigned Idx = 0;
6625     ObjCInterfaceDecl *ItfD
6626       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
6627     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6628   }
6629 
6630   case TYPE_OBJC_TYPE_PARAM: {
6631     unsigned Idx = 0;
6632     ObjCTypeParamDecl *Decl
6633       = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
6634     unsigned NumProtos = Record[Idx++];
6635     SmallVector<ObjCProtocolDecl*, 4> Protos;
6636     for (unsigned I = 0; I != NumProtos; ++I)
6637       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6638     return Context.getObjCTypeParamType(Decl, Protos);
6639   }
6640 
6641   case TYPE_OBJC_OBJECT: {
6642     unsigned Idx = 0;
6643     QualType Base = readType(*Loc.F, Record, Idx);
6644     unsigned NumTypeArgs = Record[Idx++];
6645     SmallVector<QualType, 4> TypeArgs;
6646     for (unsigned I = 0; I != NumTypeArgs; ++I)
6647       TypeArgs.push_back(readType(*Loc.F, Record, Idx));
6648     unsigned NumProtos = Record[Idx++];
6649     SmallVector<ObjCProtocolDecl*, 4> Protos;
6650     for (unsigned I = 0; I != NumProtos; ++I)
6651       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6652     bool IsKindOf = Record[Idx++];
6653     return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
6654   }
6655 
6656   case TYPE_OBJC_OBJECT_POINTER: {
6657     unsigned Idx = 0;
6658     QualType Pointee = readType(*Loc.F, Record, Idx);
6659     return Context.getObjCObjectPointerType(Pointee);
6660   }
6661 
6662   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6663     unsigned Idx = 0;
6664     QualType Parm = readType(*Loc.F, Record, Idx);
6665     QualType Replacement = readType(*Loc.F, Record, Idx);
6666     return Context.getSubstTemplateTypeParmType(
6667         cast<TemplateTypeParmType>(Parm),
6668         Context.getCanonicalType(Replacement));
6669   }
6670 
6671   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6672     unsigned Idx = 0;
6673     QualType Parm = readType(*Loc.F, Record, Idx);
6674     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6675     return Context.getSubstTemplateTypeParmPackType(
6676                                                cast<TemplateTypeParmType>(Parm),
6677                                                      ArgPack);
6678   }
6679 
6680   case TYPE_INJECTED_CLASS_NAME: {
6681     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
6682     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6683     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6684     // for AST reading, too much interdependencies.
6685     const Type *T = nullptr;
6686     for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6687       if (const Type *Existing = DI->getTypeForDecl()) {
6688         T = Existing;
6689         break;
6690       }
6691     }
6692     if (!T) {
6693       T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
6694       for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6695         DI->setTypeForDecl(T);
6696     }
6697     return QualType(T, 0);
6698   }
6699 
6700   case TYPE_TEMPLATE_TYPE_PARM: {
6701     unsigned Idx = 0;
6702     unsigned Depth = Record[Idx++];
6703     unsigned Index = Record[Idx++];
6704     bool Pack = Record[Idx++];
6705     TemplateTypeParmDecl *D
6706       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
6707     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6708   }
6709 
6710   case TYPE_DEPENDENT_NAME: {
6711     unsigned Idx = 0;
6712     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6713     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6714     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6715     QualType Canon = readType(*Loc.F, Record, Idx);
6716     if (!Canon.isNull())
6717       Canon = Context.getCanonicalType(Canon);
6718     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6719   }
6720 
6721   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6722     unsigned Idx = 0;
6723     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
6724     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6725     const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
6726     unsigned NumArgs = Record[Idx++];
6727     SmallVector<TemplateArgument, 8> Args;
6728     Args.reserve(NumArgs);
6729     while (NumArgs--)
6730       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6731     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
6732                                                           Args);
6733   }
6734 
6735   case TYPE_DEPENDENT_SIZED_ARRAY: {
6736     unsigned Idx = 0;
6737 
6738     // ArrayType
6739     QualType ElementType = readType(*Loc.F, Record, Idx);
6740     ArrayType::ArraySizeModifier ASM
6741       = (ArrayType::ArraySizeModifier)Record[Idx++];
6742     unsigned IndexTypeQuals = Record[Idx++];
6743 
6744     // DependentSizedArrayType
6745     Expr *NumElts = ReadExpr(*Loc.F);
6746     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6747 
6748     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6749                                                IndexTypeQuals, Brackets);
6750   }
6751 
6752   case TYPE_TEMPLATE_SPECIALIZATION: {
6753     unsigned Idx = 0;
6754     bool IsDependent = Record[Idx++];
6755     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6756     SmallVector<TemplateArgument, 8> Args;
6757     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6758     QualType Underlying = readType(*Loc.F, Record, Idx);
6759     QualType T;
6760     if (Underlying.isNull())
6761       T = Context.getCanonicalTemplateSpecializationType(Name, Args);
6762     else
6763       T = Context.getTemplateSpecializationType(Name, Args, Underlying);
6764     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6765     return T;
6766   }
6767 
6768   case TYPE_ATOMIC: {
6769     if (Record.size() != 1) {
6770       Error("Incorrect encoding of atomic type");
6771       return QualType();
6772     }
6773     QualType ValueType = readType(*Loc.F, Record, Idx);
6774     return Context.getAtomicType(ValueType);
6775   }
6776 
6777   case TYPE_PIPE: {
6778     if (Record.size() != 2) {
6779       Error("Incorrect encoding of pipe type");
6780       return QualType();
6781     }
6782 
6783     // Reading the pipe element type.
6784     QualType ElementType = readType(*Loc.F, Record, Idx);
6785     unsigned ReadOnly = Record[1];
6786     return Context.getPipeType(ElementType, ReadOnly);
6787   }
6788 
6789   case TYPE_DEPENDENT_SIZED_VECTOR: {
6790     unsigned Idx = 0;
6791     QualType ElementType = readType(*Loc.F, Record, Idx);
6792     Expr *SizeExpr = ReadExpr(*Loc.F);
6793     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6794     unsigned VecKind = Record[Idx];
6795 
6796     return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc,
6797                                                (VectorType::VectorKind)VecKind);
6798   }
6799 
6800   case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6801     unsigned Idx = 0;
6802 
6803     // DependentSizedExtVectorType
6804     QualType ElementType = readType(*Loc.F, Record, Idx);
6805     Expr *SizeExpr = ReadExpr(*Loc.F);
6806     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6807 
6808     return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6809                                                   AttrLoc);
6810   }
6811 
6812   case TYPE_DEPENDENT_ADDRESS_SPACE: {
6813     unsigned Idx = 0;
6814 
6815     // DependentAddressSpaceType
6816     QualType PointeeType = readType(*Loc.F, Record, Idx);
6817     Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6818     SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6819 
6820     return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6821                                                    AttrLoc);
6822   }
6823   }
6824   llvm_unreachable("Invalid TypeCode!");
6825 }
6826 
6827 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6828                                   SmallVectorImpl<QualType> &Exceptions,
6829                                   FunctionProtoType::ExceptionSpecInfo &ESI,
6830                                   const RecordData &Record, unsigned &Idx) {
6831   ExceptionSpecificationType EST =
6832       static_cast<ExceptionSpecificationType>(Record[Idx++]);
6833   ESI.Type = EST;
6834   if (EST == EST_Dynamic) {
6835     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
6836       Exceptions.push_back(readType(ModuleFile, Record, Idx));
6837     ESI.Exceptions = Exceptions;
6838   } else if (isComputedNoexcept(EST)) {
6839     ESI.NoexceptExpr = ReadExpr(ModuleFile);
6840   } else if (EST == EST_Uninstantiated) {
6841     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6842     ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6843   } else if (EST == EST_Unevaluated) {
6844     ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6845   }
6846 }
6847 
6848 namespace clang {
6849 
6850 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6851   ModuleFile *F;
6852   ASTReader *Reader;
6853   const ASTReader::RecordData &Record;
6854   unsigned &Idx;
6855 
6856   SourceLocation ReadSourceLocation() {
6857     return Reader->ReadSourceLocation(*F, Record, Idx);
6858   }
6859 
6860   TypeSourceInfo *GetTypeSourceInfo() {
6861     return Reader->GetTypeSourceInfo(*F, Record, Idx);
6862   }
6863 
6864   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6865     return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
6866   }
6867 
6868   Attr *ReadAttr() {
6869     return Reader->ReadAttr(*F, Record, Idx);
6870   }
6871 
6872 public:
6873   TypeLocReader(ModuleFile &F, ASTReader &Reader,
6874                 const ASTReader::RecordData &Record, unsigned &Idx)
6875       : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
6876 
6877   // We want compile-time assurance that we've enumerated all of
6878   // these, so unfortunately we have to declare them first, then
6879   // define them out-of-line.
6880 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6881 #define TYPELOC(CLASS, PARENT) \
6882   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6883 #include "clang/AST/TypeLocNodes.def"
6884 
6885   void VisitFunctionTypeLoc(FunctionTypeLoc);
6886   void VisitArrayTypeLoc(ArrayTypeLoc);
6887 };
6888 
6889 } // namespace clang
6890 
6891 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6892   // nothing to do
6893 }
6894 
6895 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6896   TL.setBuiltinLoc(ReadSourceLocation());
6897   if (TL.needsExtraLocalData()) {
6898     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6899     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6900     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6901     TL.setModeAttr(Record[Idx++]);
6902   }
6903 }
6904 
6905 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6906   TL.setNameLoc(ReadSourceLocation());
6907 }
6908 
6909 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6910   TL.setStarLoc(ReadSourceLocation());
6911 }
6912 
6913 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6914   // nothing to do
6915 }
6916 
6917 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6918   // nothing to do
6919 }
6920 
6921 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6922   TL.setExpansionLoc(ReadSourceLocation());
6923 }
6924 
6925 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6926   TL.setCaretLoc(ReadSourceLocation());
6927 }
6928 
6929 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6930   TL.setAmpLoc(ReadSourceLocation());
6931 }
6932 
6933 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6934   TL.setAmpAmpLoc(ReadSourceLocation());
6935 }
6936 
6937 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6938   TL.setStarLoc(ReadSourceLocation());
6939   TL.setClassTInfo(GetTypeSourceInfo());
6940 }
6941 
6942 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6943   TL.setLBracketLoc(ReadSourceLocation());
6944   TL.setRBracketLoc(ReadSourceLocation());
6945   if (Record[Idx++])
6946     TL.setSizeExpr(Reader->ReadExpr(*F));
6947   else
6948     TL.setSizeExpr(nullptr);
6949 }
6950 
6951 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6952   VisitArrayTypeLoc(TL);
6953 }
6954 
6955 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6956   VisitArrayTypeLoc(TL);
6957 }
6958 
6959 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6960   VisitArrayTypeLoc(TL);
6961 }
6962 
6963 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6964                                             DependentSizedArrayTypeLoc TL) {
6965   VisitArrayTypeLoc(TL);
6966 }
6967 
6968 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6969     DependentAddressSpaceTypeLoc TL) {
6970 
6971     TL.setAttrNameLoc(ReadSourceLocation());
6972     SourceRange range;
6973     range.setBegin(ReadSourceLocation());
6974     range.setEnd(ReadSourceLocation());
6975     TL.setAttrOperandParensRange(range);
6976     TL.setAttrExprOperand(Reader->ReadExpr(*F));
6977 }
6978 
6979 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6980                                         DependentSizedExtVectorTypeLoc TL) {
6981   TL.setNameLoc(ReadSourceLocation());
6982 }
6983 
6984 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6985   TL.setNameLoc(ReadSourceLocation());
6986 }
6987 
6988 void TypeLocReader::VisitDependentVectorTypeLoc(
6989     DependentVectorTypeLoc TL) {
6990   TL.setNameLoc(ReadSourceLocation());
6991 }
6992 
6993 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6994   TL.setNameLoc(ReadSourceLocation());
6995 }
6996 
6997 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6998   TL.setLocalRangeBegin(ReadSourceLocation());
6999   TL.setLParenLoc(ReadSourceLocation());
7000   TL.setRParenLoc(ReadSourceLocation());
7001   TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
7002                                        Reader->ReadSourceLocation(*F, Record, Idx)));
7003   TL.setLocalRangeEnd(ReadSourceLocation());
7004   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7005     TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
7006   }
7007 }
7008 
7009 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7010   VisitFunctionTypeLoc(TL);
7011 }
7012 
7013 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7014   VisitFunctionTypeLoc(TL);
7015 }
7016 
7017 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7018   TL.setNameLoc(ReadSourceLocation());
7019 }
7020 
7021 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7022   TL.setNameLoc(ReadSourceLocation());
7023 }
7024 
7025 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7026   TL.setTypeofLoc(ReadSourceLocation());
7027   TL.setLParenLoc(ReadSourceLocation());
7028   TL.setRParenLoc(ReadSourceLocation());
7029 }
7030 
7031 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7032   TL.setTypeofLoc(ReadSourceLocation());
7033   TL.setLParenLoc(ReadSourceLocation());
7034   TL.setRParenLoc(ReadSourceLocation());
7035   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7036 }
7037 
7038 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7039   TL.setNameLoc(ReadSourceLocation());
7040 }
7041 
7042 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7043   TL.setKWLoc(ReadSourceLocation());
7044   TL.setLParenLoc(ReadSourceLocation());
7045   TL.setRParenLoc(ReadSourceLocation());
7046   TL.setUnderlyingTInfo(GetTypeSourceInfo());
7047 }
7048 
7049 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7050   TL.setNameLoc(ReadSourceLocation());
7051 }
7052 
7053 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7054     DeducedTemplateSpecializationTypeLoc TL) {
7055   TL.setTemplateNameLoc(ReadSourceLocation());
7056 }
7057 
7058 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7059   TL.setNameLoc(ReadSourceLocation());
7060 }
7061 
7062 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7063   TL.setNameLoc(ReadSourceLocation());
7064 }
7065 
7066 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7067   TL.setAttr(ReadAttr());
7068 }
7069 
7070 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7071   TL.setNameLoc(ReadSourceLocation());
7072 }
7073 
7074 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7075                                             SubstTemplateTypeParmTypeLoc TL) {
7076   TL.setNameLoc(ReadSourceLocation());
7077 }
7078 
7079 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7080                                           SubstTemplateTypeParmPackTypeLoc TL) {
7081   TL.setNameLoc(ReadSourceLocation());
7082 }
7083 
7084 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7085                                            TemplateSpecializationTypeLoc TL) {
7086   TL.setTemplateKeywordLoc(ReadSourceLocation());
7087   TL.setTemplateNameLoc(ReadSourceLocation());
7088   TL.setLAngleLoc(ReadSourceLocation());
7089   TL.setRAngleLoc(ReadSourceLocation());
7090   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7091     TL.setArgLocInfo(
7092         i,
7093         Reader->GetTemplateArgumentLocInfo(
7094             *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
7095 }
7096 
7097 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7098   TL.setLParenLoc(ReadSourceLocation());
7099   TL.setRParenLoc(ReadSourceLocation());
7100 }
7101 
7102 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7103   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7104   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7105 }
7106 
7107 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7108   TL.setNameLoc(ReadSourceLocation());
7109 }
7110 
7111 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7112   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7113   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7114   TL.setNameLoc(ReadSourceLocation());
7115 }
7116 
7117 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7118        DependentTemplateSpecializationTypeLoc TL) {
7119   TL.setElaboratedKeywordLoc(ReadSourceLocation());
7120   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7121   TL.setTemplateKeywordLoc(ReadSourceLocation());
7122   TL.setTemplateNameLoc(ReadSourceLocation());
7123   TL.setLAngleLoc(ReadSourceLocation());
7124   TL.setRAngleLoc(ReadSourceLocation());
7125   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7126     TL.setArgLocInfo(
7127         I,
7128         Reader->GetTemplateArgumentLocInfo(
7129             *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
7130 }
7131 
7132 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7133   TL.setEllipsisLoc(ReadSourceLocation());
7134 }
7135 
7136 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7137   TL.setNameLoc(ReadSourceLocation());
7138 }
7139 
7140 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7141   if (TL.getNumProtocols()) {
7142     TL.setProtocolLAngleLoc(ReadSourceLocation());
7143     TL.setProtocolRAngleLoc(ReadSourceLocation());
7144   }
7145   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7146     TL.setProtocolLoc(i, ReadSourceLocation());
7147 }
7148 
7149 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7150   TL.setHasBaseTypeAsWritten(Record[Idx++]);
7151   TL.setTypeArgsLAngleLoc(ReadSourceLocation());
7152   TL.setTypeArgsRAngleLoc(ReadSourceLocation());
7153   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7154     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
7155   TL.setProtocolLAngleLoc(ReadSourceLocation());
7156   TL.setProtocolRAngleLoc(ReadSourceLocation());
7157   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7158     TL.setProtocolLoc(i, ReadSourceLocation());
7159 }
7160 
7161 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7162   TL.setStarLoc(ReadSourceLocation());
7163 }
7164 
7165 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7166   TL.setKWLoc(ReadSourceLocation());
7167   TL.setLParenLoc(ReadSourceLocation());
7168   TL.setRParenLoc(ReadSourceLocation());
7169 }
7170 
7171 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7172   TL.setKWLoc(ReadSourceLocation());
7173 }
7174 
7175 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record,
7176                             unsigned &Idx, TypeLoc TL) {
7177   TypeLocReader TLR(F, *this, Record, Idx);
7178   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7179     TLR.Visit(TL);
7180 }
7181 
7182 TypeSourceInfo *
7183 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
7184                              unsigned &Idx) {
7185   QualType InfoTy = readType(F, Record, Idx);
7186   if (InfoTy.isNull())
7187     return nullptr;
7188 
7189   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
7190   ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc());
7191   return TInfo;
7192 }
7193 
7194 QualType ASTReader::GetType(TypeID ID) {
7195   assert(ContextObj && "reading type with no AST context");
7196   ASTContext &Context = *ContextObj;
7197 
7198   unsigned FastQuals = ID & Qualifiers::FastMask;
7199   unsigned Index = ID >> Qualifiers::FastWidth;
7200 
7201   if (Index < NUM_PREDEF_TYPE_IDS) {
7202     QualType T;
7203     switch ((PredefinedTypeIDs)Index) {
7204     case PREDEF_TYPE_NULL_ID:
7205       return QualType();
7206     case PREDEF_TYPE_VOID_ID:
7207       T = Context.VoidTy;
7208       break;
7209     case PREDEF_TYPE_BOOL_ID:
7210       T = Context.BoolTy;
7211       break;
7212     case PREDEF_TYPE_CHAR_U_ID:
7213     case PREDEF_TYPE_CHAR_S_ID:
7214       // FIXME: Check that the signedness of CharTy is correct!
7215       T = Context.CharTy;
7216       break;
7217     case PREDEF_TYPE_UCHAR_ID:
7218       T = Context.UnsignedCharTy;
7219       break;
7220     case PREDEF_TYPE_USHORT_ID:
7221       T = Context.UnsignedShortTy;
7222       break;
7223     case PREDEF_TYPE_UINT_ID:
7224       T = Context.UnsignedIntTy;
7225       break;
7226     case PREDEF_TYPE_ULONG_ID:
7227       T = Context.UnsignedLongTy;
7228       break;
7229     case PREDEF_TYPE_ULONGLONG_ID:
7230       T = Context.UnsignedLongLongTy;
7231       break;
7232     case PREDEF_TYPE_UINT128_ID:
7233       T = Context.UnsignedInt128Ty;
7234       break;
7235     case PREDEF_TYPE_SCHAR_ID:
7236       T = Context.SignedCharTy;
7237       break;
7238     case PREDEF_TYPE_WCHAR_ID:
7239       T = Context.WCharTy;
7240       break;
7241     case PREDEF_TYPE_SHORT_ID:
7242       T = Context.ShortTy;
7243       break;
7244     case PREDEF_TYPE_INT_ID:
7245       T = Context.IntTy;
7246       break;
7247     case PREDEF_TYPE_LONG_ID:
7248       T = Context.LongTy;
7249       break;
7250     case PREDEF_TYPE_LONGLONG_ID:
7251       T = Context.LongLongTy;
7252       break;
7253     case PREDEF_TYPE_INT128_ID:
7254       T = Context.Int128Ty;
7255       break;
7256     case PREDEF_TYPE_HALF_ID:
7257       T = Context.HalfTy;
7258       break;
7259     case PREDEF_TYPE_FLOAT_ID:
7260       T = Context.FloatTy;
7261       break;
7262     case PREDEF_TYPE_DOUBLE_ID:
7263       T = Context.DoubleTy;
7264       break;
7265     case PREDEF_TYPE_LONGDOUBLE_ID:
7266       T = Context.LongDoubleTy;
7267       break;
7268     case PREDEF_TYPE_SHORT_ACCUM_ID:
7269       T = Context.ShortAccumTy;
7270       break;
7271     case PREDEF_TYPE_ACCUM_ID:
7272       T = Context.AccumTy;
7273       break;
7274     case PREDEF_TYPE_LONG_ACCUM_ID:
7275       T = Context.LongAccumTy;
7276       break;
7277     case PREDEF_TYPE_USHORT_ACCUM_ID:
7278       T = Context.UnsignedShortAccumTy;
7279       break;
7280     case PREDEF_TYPE_UACCUM_ID:
7281       T = Context.UnsignedAccumTy;
7282       break;
7283     case PREDEF_TYPE_ULONG_ACCUM_ID:
7284       T = Context.UnsignedLongAccumTy;
7285       break;
7286     case PREDEF_TYPE_SHORT_FRACT_ID:
7287       T = Context.ShortFractTy;
7288       break;
7289     case PREDEF_TYPE_FRACT_ID:
7290       T = Context.FractTy;
7291       break;
7292     case PREDEF_TYPE_LONG_FRACT_ID:
7293       T = Context.LongFractTy;
7294       break;
7295     case PREDEF_TYPE_USHORT_FRACT_ID:
7296       T = Context.UnsignedShortFractTy;
7297       break;
7298     case PREDEF_TYPE_UFRACT_ID:
7299       T = Context.UnsignedFractTy;
7300       break;
7301     case PREDEF_TYPE_ULONG_FRACT_ID:
7302       T = Context.UnsignedLongFractTy;
7303       break;
7304     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7305       T = Context.SatShortAccumTy;
7306       break;
7307     case PREDEF_TYPE_SAT_ACCUM_ID:
7308       T = Context.SatAccumTy;
7309       break;
7310     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7311       T = Context.SatLongAccumTy;
7312       break;
7313     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7314       T = Context.SatUnsignedShortAccumTy;
7315       break;
7316     case PREDEF_TYPE_SAT_UACCUM_ID:
7317       T = Context.SatUnsignedAccumTy;
7318       break;
7319     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7320       T = Context.SatUnsignedLongAccumTy;
7321       break;
7322     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7323       T = Context.SatShortFractTy;
7324       break;
7325     case PREDEF_TYPE_SAT_FRACT_ID:
7326       T = Context.SatFractTy;
7327       break;
7328     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7329       T = Context.SatLongFractTy;
7330       break;
7331     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7332       T = Context.SatUnsignedShortFractTy;
7333       break;
7334     case PREDEF_TYPE_SAT_UFRACT_ID:
7335       T = Context.SatUnsignedFractTy;
7336       break;
7337     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7338       T = Context.SatUnsignedLongFractTy;
7339       break;
7340     case PREDEF_TYPE_FLOAT16_ID:
7341       T = Context.Float16Ty;
7342       break;
7343     case PREDEF_TYPE_FLOAT128_ID:
7344       T = Context.Float128Ty;
7345       break;
7346     case PREDEF_TYPE_OVERLOAD_ID:
7347       T = Context.OverloadTy;
7348       break;
7349     case PREDEF_TYPE_BOUND_MEMBER:
7350       T = Context.BoundMemberTy;
7351       break;
7352     case PREDEF_TYPE_PSEUDO_OBJECT:
7353       T = Context.PseudoObjectTy;
7354       break;
7355     case PREDEF_TYPE_DEPENDENT_ID:
7356       T = Context.DependentTy;
7357       break;
7358     case PREDEF_TYPE_UNKNOWN_ANY:
7359       T = Context.UnknownAnyTy;
7360       break;
7361     case PREDEF_TYPE_NULLPTR_ID:
7362       T = Context.NullPtrTy;
7363       break;
7364     case PREDEF_TYPE_CHAR8_ID:
7365       T = Context.Char8Ty;
7366       break;
7367     case PREDEF_TYPE_CHAR16_ID:
7368       T = Context.Char16Ty;
7369       break;
7370     case PREDEF_TYPE_CHAR32_ID:
7371       T = Context.Char32Ty;
7372       break;
7373     case PREDEF_TYPE_OBJC_ID:
7374       T = Context.ObjCBuiltinIdTy;
7375       break;
7376     case PREDEF_TYPE_OBJC_CLASS:
7377       T = Context.ObjCBuiltinClassTy;
7378       break;
7379     case PREDEF_TYPE_OBJC_SEL:
7380       T = Context.ObjCBuiltinSelTy;
7381       break;
7382 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7383     case PREDEF_TYPE_##Id##_ID: \
7384       T = Context.SingletonId; \
7385       break;
7386 #include "clang/Basic/OpenCLImageTypes.def"
7387 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7388     case PREDEF_TYPE_##Id##_ID: \
7389       T = Context.Id##Ty; \
7390       break;
7391 #include "clang/Basic/OpenCLExtensionTypes.def"
7392     case PREDEF_TYPE_SAMPLER_ID:
7393       T = Context.OCLSamplerTy;
7394       break;
7395     case PREDEF_TYPE_EVENT_ID:
7396       T = Context.OCLEventTy;
7397       break;
7398     case PREDEF_TYPE_CLK_EVENT_ID:
7399       T = Context.OCLClkEventTy;
7400       break;
7401     case PREDEF_TYPE_QUEUE_ID:
7402       T = Context.OCLQueueTy;
7403       break;
7404     case PREDEF_TYPE_RESERVE_ID_ID:
7405       T = Context.OCLReserveIDTy;
7406       break;
7407     case PREDEF_TYPE_AUTO_DEDUCT:
7408       T = Context.getAutoDeductType();
7409       break;
7410     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7411       T = Context.getAutoRRefDeductType();
7412       break;
7413     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7414       T = Context.ARCUnbridgedCastTy;
7415       break;
7416     case PREDEF_TYPE_BUILTIN_FN:
7417       T = Context.BuiltinFnTy;
7418       break;
7419     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7420       T = Context.OMPArraySectionTy;
7421       break;
7422 #define SVE_TYPE(Name, Id, SingletonId) \
7423     case PREDEF_TYPE_##Id##_ID: \
7424       T = Context.SingletonId; \
7425       break;
7426 #include "clang/Basic/AArch64SVEACLETypes.def"
7427     }
7428 
7429     assert(!T.isNull() && "Unknown predefined type");
7430     return T.withFastQualifiers(FastQuals);
7431   }
7432 
7433   Index -= NUM_PREDEF_TYPE_IDS;
7434   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7435   if (TypesLoaded[Index].isNull()) {
7436     TypesLoaded[Index] = readTypeRecord(Index);
7437     if (TypesLoaded[Index].isNull())
7438       return QualType();
7439 
7440     TypesLoaded[Index]->setFromAST();
7441     if (DeserializationListener)
7442       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7443                                         TypesLoaded[Index]);
7444   }
7445 
7446   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7447 }
7448 
7449 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7450   return GetType(getGlobalTypeID(F, LocalID));
7451 }
7452 
7453 serialization::TypeID
7454 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7455   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7456   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7457 
7458   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7459     return LocalID;
7460 
7461   if (!F.ModuleOffsetMap.empty())
7462     ReadModuleOffsetMap(F);
7463 
7464   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7465     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7466   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7467 
7468   unsigned GlobalIndex = LocalIndex + I->second;
7469   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7470 }
7471 
7472 TemplateArgumentLocInfo
7473 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7474                                       TemplateArgument::ArgKind Kind,
7475                                       const RecordData &Record,
7476                                       unsigned &Index) {
7477   switch (Kind) {
7478   case TemplateArgument::Expression:
7479     return ReadExpr(F);
7480   case TemplateArgument::Type:
7481     return GetTypeSourceInfo(F, Record, Index);
7482   case TemplateArgument::Template: {
7483     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7484                                                                      Index);
7485     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7486     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7487                                    SourceLocation());
7488   }
7489   case TemplateArgument::TemplateExpansion: {
7490     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
7491                                                                      Index);
7492     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7493     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
7494     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7495                                    EllipsisLoc);
7496   }
7497   case TemplateArgument::Null:
7498   case TemplateArgument::Integral:
7499   case TemplateArgument::Declaration:
7500   case TemplateArgument::NullPtr:
7501   case TemplateArgument::Pack:
7502     // FIXME: Is this right?
7503     return TemplateArgumentLocInfo();
7504   }
7505   llvm_unreachable("unexpected template argument loc");
7506 }
7507 
7508 TemplateArgumentLoc
7509 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7510                                    const RecordData &Record, unsigned &Index) {
7511   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7512 
7513   if (Arg.getKind() == TemplateArgument::Expression) {
7514     if (Record[Index++]) // bool InfoHasSameExpr.
7515       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7516   }
7517   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7518                                                              Record, Index));
7519 }
7520 
7521 const ASTTemplateArgumentListInfo*
7522 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7523                                            const RecordData &Record,
7524                                            unsigned &Index) {
7525   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7526   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7527   unsigned NumArgsAsWritten = Record[Index++];
7528   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7529   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7530     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7531   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7532 }
7533 
7534 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7535   return GetDecl(ID);
7536 }
7537 
7538 void ASTReader::CompleteRedeclChain(const Decl *D) {
7539   if (NumCurrentElementsDeserializing) {
7540     // We arrange to not care about the complete redeclaration chain while we're
7541     // deserializing. Just remember that the AST has marked this one as complete
7542     // but that it's not actually complete yet, so we know we still need to
7543     // complete it later.
7544     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7545     return;
7546   }
7547 
7548   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7549 
7550   // If this is a named declaration, complete it by looking it up
7551   // within its context.
7552   //
7553   // FIXME: Merging a function definition should merge
7554   // all mergeable entities within it.
7555   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7556       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7557     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7558       if (!getContext().getLangOpts().CPlusPlus &&
7559           isa<TranslationUnitDecl>(DC)) {
7560         // Outside of C++, we don't have a lookup table for the TU, so update
7561         // the identifier instead. (For C++ modules, we don't store decls
7562         // in the serialized identifier table, so we do the lookup in the TU.)
7563         auto *II = Name.getAsIdentifierInfo();
7564         assert(II && "non-identifier name in C?");
7565         if (II->isOutOfDate())
7566           updateOutOfDateIdentifier(*II);
7567       } else
7568         DC->lookup(Name);
7569     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7570       // Find all declarations of this kind from the relevant context.
7571       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7572         auto *DC = cast<DeclContext>(DCDecl);
7573         SmallVector<Decl*, 8> Decls;
7574         FindExternalLexicalDecls(
7575             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7576       }
7577     }
7578   }
7579 
7580   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7581     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7582   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7583     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7584   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7585     if (auto *Template = FD->getPrimaryTemplate())
7586       Template->LoadLazySpecializations();
7587   }
7588 }
7589 
7590 CXXCtorInitializer **
7591 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7592   RecordLocation Loc = getLocalBitOffset(Offset);
7593   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7594   SavedStreamPosition SavedPosition(Cursor);
7595   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7596     Error(std::move(Err));
7597     return nullptr;
7598   }
7599   ReadingKindTracker ReadingKind(Read_Decl, *this);
7600 
7601   RecordData Record;
7602   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7603   if (!MaybeCode) {
7604     Error(MaybeCode.takeError());
7605     return nullptr;
7606   }
7607   unsigned Code = MaybeCode.get();
7608 
7609   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7610   if (!MaybeRecCode) {
7611     Error(MaybeRecCode.takeError());
7612     return nullptr;
7613   }
7614   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7615     Error("malformed AST file: missing C++ ctor initializers");
7616     return nullptr;
7617   }
7618 
7619   unsigned Idx = 0;
7620   return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7621 }
7622 
7623 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7624   assert(ContextObj && "reading base specifiers with no AST context");
7625   ASTContext &Context = *ContextObj;
7626 
7627   RecordLocation Loc = getLocalBitOffset(Offset);
7628   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7629   SavedStreamPosition SavedPosition(Cursor);
7630   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7631     Error(std::move(Err));
7632     return nullptr;
7633   }
7634   ReadingKindTracker ReadingKind(Read_Decl, *this);
7635   RecordData Record;
7636 
7637   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7638   if (!MaybeCode) {
7639     Error(MaybeCode.takeError());
7640     return nullptr;
7641   }
7642   unsigned Code = MaybeCode.get();
7643 
7644   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record);
7645   if (!MaybeRecCode) {
7646     Error(MaybeCode.takeError());
7647     return nullptr;
7648   }
7649   unsigned RecCode = MaybeRecCode.get();
7650 
7651   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7652     Error("malformed AST file: missing C++ base specifiers");
7653     return nullptr;
7654   }
7655 
7656   unsigned Idx = 0;
7657   unsigned NumBases = Record[Idx++];
7658   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7659   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7660   for (unsigned I = 0; I != NumBases; ++I)
7661     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7662   return Bases;
7663 }
7664 
7665 serialization::DeclID
7666 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7667   if (LocalID < NUM_PREDEF_DECL_IDS)
7668     return LocalID;
7669 
7670   if (!F.ModuleOffsetMap.empty())
7671     ReadModuleOffsetMap(F);
7672 
7673   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7674     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7675   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7676 
7677   return LocalID + I->second;
7678 }
7679 
7680 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7681                                    ModuleFile &M) const {
7682   // Predefined decls aren't from any module.
7683   if (ID < NUM_PREDEF_DECL_IDS)
7684     return false;
7685 
7686   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7687          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7688 }
7689 
7690 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7691   if (!D->isFromASTFile())
7692     return nullptr;
7693   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7694   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7695   return I->second;
7696 }
7697 
7698 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7699   if (ID < NUM_PREDEF_DECL_IDS)
7700     return SourceLocation();
7701 
7702   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7703 
7704   if (Index > DeclsLoaded.size()) {
7705     Error("declaration ID out-of-range for AST file");
7706     return SourceLocation();
7707   }
7708 
7709   if (Decl *D = DeclsLoaded[Index])
7710     return D->getLocation();
7711 
7712   SourceLocation Loc;
7713   DeclCursorForID(ID, Loc);
7714   return Loc;
7715 }
7716 
7717 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7718   switch (ID) {
7719   case PREDEF_DECL_NULL_ID:
7720     return nullptr;
7721 
7722   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7723     return Context.getTranslationUnitDecl();
7724 
7725   case PREDEF_DECL_OBJC_ID_ID:
7726     return Context.getObjCIdDecl();
7727 
7728   case PREDEF_DECL_OBJC_SEL_ID:
7729     return Context.getObjCSelDecl();
7730 
7731   case PREDEF_DECL_OBJC_CLASS_ID:
7732     return Context.getObjCClassDecl();
7733 
7734   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7735     return Context.getObjCProtocolDecl();
7736 
7737   case PREDEF_DECL_INT_128_ID:
7738     return Context.getInt128Decl();
7739 
7740   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7741     return Context.getUInt128Decl();
7742 
7743   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7744     return Context.getObjCInstanceTypeDecl();
7745 
7746   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7747     return Context.getBuiltinVaListDecl();
7748 
7749   case PREDEF_DECL_VA_LIST_TAG:
7750     return Context.getVaListTagDecl();
7751 
7752   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7753     return Context.getBuiltinMSVaListDecl();
7754 
7755   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7756     return Context.getExternCContextDecl();
7757 
7758   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7759     return Context.getMakeIntegerSeqDecl();
7760 
7761   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7762     return Context.getCFConstantStringDecl();
7763 
7764   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7765     return Context.getCFConstantStringTagDecl();
7766 
7767   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7768     return Context.getTypePackElementDecl();
7769   }
7770   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7771 }
7772 
7773 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7774   assert(ContextObj && "reading decl with no AST context");
7775   if (ID < NUM_PREDEF_DECL_IDS) {
7776     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7777     if (D) {
7778       // Track that we have merged the declaration with ID \p ID into the
7779       // pre-existing predefined declaration \p D.
7780       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7781       if (Merged.empty())
7782         Merged.push_back(ID);
7783     }
7784     return D;
7785   }
7786 
7787   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7788 
7789   if (Index >= DeclsLoaded.size()) {
7790     assert(0 && "declaration ID out-of-range for AST file");
7791     Error("declaration ID out-of-range for AST file");
7792     return nullptr;
7793   }
7794 
7795   return DeclsLoaded[Index];
7796 }
7797 
7798 Decl *ASTReader::GetDecl(DeclID ID) {
7799   if (ID < NUM_PREDEF_DECL_IDS)
7800     return GetExistingDecl(ID);
7801 
7802   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7803 
7804   if (Index >= DeclsLoaded.size()) {
7805     assert(0 && "declaration ID out-of-range for AST file");
7806     Error("declaration ID out-of-range for AST file");
7807     return nullptr;
7808   }
7809 
7810   if (!DeclsLoaded[Index]) {
7811     ReadDeclRecord(ID);
7812     if (DeserializationListener)
7813       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7814   }
7815 
7816   return DeclsLoaded[Index];
7817 }
7818 
7819 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7820                                                   DeclID GlobalID) {
7821   if (GlobalID < NUM_PREDEF_DECL_IDS)
7822     return GlobalID;
7823 
7824   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7825   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7826   ModuleFile *Owner = I->second;
7827 
7828   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7829     = M.GlobalToLocalDeclIDs.find(Owner);
7830   if (Pos == M.GlobalToLocalDeclIDs.end())
7831     return 0;
7832 
7833   return GlobalID - Owner->BaseDeclID + Pos->second;
7834 }
7835 
7836 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7837                                             const RecordData &Record,
7838                                             unsigned &Idx) {
7839   if (Idx >= Record.size()) {
7840     Error("Corrupted AST file");
7841     return 0;
7842   }
7843 
7844   return getGlobalDeclID(F, Record[Idx++]);
7845 }
7846 
7847 /// Resolve the offset of a statement into a statement.
7848 ///
7849 /// This operation will read a new statement from the external
7850 /// source each time it is called, and is meant to be used via a
7851 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7852 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7853   // Switch case IDs are per Decl.
7854   ClearSwitchCaseIDs();
7855 
7856   // Offset here is a global offset across the entire chain.
7857   RecordLocation Loc = getLocalBitOffset(Offset);
7858   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7859     Error(std::move(Err));
7860     return nullptr;
7861   }
7862   assert(NumCurrentElementsDeserializing == 0 &&
7863          "should not be called while already deserializing");
7864   Deserializing D(this);
7865   return ReadStmtFromStream(*Loc.F);
7866 }
7867 
7868 void ASTReader::FindExternalLexicalDecls(
7869     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7870     SmallVectorImpl<Decl *> &Decls) {
7871   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7872 
7873   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7874     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7875     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7876       auto K = (Decl::Kind)+LexicalDecls[I];
7877       if (!IsKindWeWant(K))
7878         continue;
7879 
7880       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7881 
7882       // Don't add predefined declarations to the lexical context more
7883       // than once.
7884       if (ID < NUM_PREDEF_DECL_IDS) {
7885         if (PredefsVisited[ID])
7886           continue;
7887 
7888         PredefsVisited[ID] = true;
7889       }
7890 
7891       if (Decl *D = GetLocalDecl(*M, ID)) {
7892         assert(D->getKind() == K && "wrong kind for lexical decl");
7893         if (!DC->isDeclInLexicalTraversal(D))
7894           Decls.push_back(D);
7895       }
7896     }
7897   };
7898 
7899   if (isa<TranslationUnitDecl>(DC)) {
7900     for (auto Lexical : TULexicalDecls)
7901       Visit(Lexical.first, Lexical.second);
7902   } else {
7903     auto I = LexicalDecls.find(DC);
7904     if (I != LexicalDecls.end())
7905       Visit(I->second.first, I->second.second);
7906   }
7907 
7908   ++NumLexicalDeclContextsRead;
7909 }
7910 
7911 namespace {
7912 
7913 class DeclIDComp {
7914   ASTReader &Reader;
7915   ModuleFile &Mod;
7916 
7917 public:
7918   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7919 
7920   bool operator()(LocalDeclID L, LocalDeclID R) const {
7921     SourceLocation LHS = getLocation(L);
7922     SourceLocation RHS = getLocation(R);
7923     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7924   }
7925 
7926   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7927     SourceLocation RHS = getLocation(R);
7928     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7929   }
7930 
7931   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7932     SourceLocation LHS = getLocation(L);
7933     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7934   }
7935 
7936   SourceLocation getLocation(LocalDeclID ID) const {
7937     return Reader.getSourceManager().getFileLoc(
7938             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7939   }
7940 };
7941 
7942 } // namespace
7943 
7944 void ASTReader::FindFileRegionDecls(FileID File,
7945                                     unsigned Offset, unsigned Length,
7946                                     SmallVectorImpl<Decl *> &Decls) {
7947   SourceManager &SM = getSourceManager();
7948 
7949   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7950   if (I == FileDeclIDs.end())
7951     return;
7952 
7953   FileDeclsInfo &DInfo = I->second;
7954   if (DInfo.Decls.empty())
7955     return;
7956 
7957   SourceLocation
7958     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7959   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7960 
7961   DeclIDComp DIDComp(*this, *DInfo.Mod);
7962   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7963       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7964   if (BeginIt != DInfo.Decls.begin())
7965     --BeginIt;
7966 
7967   // If we are pointing at a top-level decl inside an objc container, we need
7968   // to backtrack until we find it otherwise we will fail to report that the
7969   // region overlaps with an objc container.
7970   while (BeginIt != DInfo.Decls.begin() &&
7971          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7972              ->isTopLevelDeclInObjCContainer())
7973     --BeginIt;
7974 
7975   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7976       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7977   if (EndIt != DInfo.Decls.end())
7978     ++EndIt;
7979 
7980   for (ArrayRef<serialization::LocalDeclID>::iterator
7981          DIt = BeginIt; DIt != EndIt; ++DIt)
7982     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7983 }
7984 
7985 bool
7986 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7987                                           DeclarationName Name) {
7988   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7989          "DeclContext has no visible decls in storage");
7990   if (!Name)
7991     return false;
7992 
7993   auto It = Lookups.find(DC);
7994   if (It == Lookups.end())
7995     return false;
7996 
7997   Deserializing LookupResults(this);
7998 
7999   // Load the list of declarations.
8000   SmallVector<NamedDecl *, 64> Decls;
8001   for (DeclID ID : It->second.Table.find(Name)) {
8002     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8003     if (ND->getDeclName() == Name)
8004       Decls.push_back(ND);
8005   }
8006 
8007   ++NumVisibleDeclContextsRead;
8008   SetExternalVisibleDeclsForName(DC, Name, Decls);
8009   return !Decls.empty();
8010 }
8011 
8012 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8013   if (!DC->hasExternalVisibleStorage())
8014     return;
8015 
8016   auto It = Lookups.find(DC);
8017   assert(It != Lookups.end() &&
8018          "have external visible storage but no lookup tables");
8019 
8020   DeclsMap Decls;
8021 
8022   for (DeclID ID : It->second.Table.findAll()) {
8023     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
8024     Decls[ND->getDeclName()].push_back(ND);
8025   }
8026 
8027   ++NumVisibleDeclContextsRead;
8028 
8029   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8030     SetExternalVisibleDeclsForName(DC, I->first, I->second);
8031   }
8032   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8033 }
8034 
8035 const serialization::reader::DeclContextLookupTable *
8036 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8037   auto I = Lookups.find(Primary);
8038   return I == Lookups.end() ? nullptr : &I->second;
8039 }
8040 
8041 /// Under non-PCH compilation the consumer receives the objc methods
8042 /// before receiving the implementation, and codegen depends on this.
8043 /// We simulate this by deserializing and passing to consumer the methods of the
8044 /// implementation before passing the deserialized implementation decl.
8045 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8046                                        ASTConsumer *Consumer) {
8047   assert(ImplD && Consumer);
8048 
8049   for (auto *I : ImplD->methods())
8050     Consumer->HandleInterestingDecl(DeclGroupRef(I));
8051 
8052   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
8053 }
8054 
8055 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8056   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
8057     PassObjCImplDeclToConsumer(ImplD, Consumer);
8058   else
8059     Consumer->HandleInterestingDecl(DeclGroupRef(D));
8060 }
8061 
8062 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8063   this->Consumer = Consumer;
8064 
8065   if (Consumer)
8066     PassInterestingDeclsToConsumer();
8067 
8068   if (DeserializationListener)
8069     DeserializationListener->ReaderInitialized(this);
8070 }
8071 
8072 void ASTReader::PrintStats() {
8073   std::fprintf(stderr, "*** AST File Statistics:\n");
8074 
8075   unsigned NumTypesLoaded
8076     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
8077                                       QualType());
8078   unsigned NumDeclsLoaded
8079     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
8080                                       (Decl *)nullptr);
8081   unsigned NumIdentifiersLoaded
8082     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
8083                                             IdentifiersLoaded.end(),
8084                                             (IdentifierInfo *)nullptr);
8085   unsigned NumMacrosLoaded
8086     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
8087                                        MacrosLoaded.end(),
8088                                        (MacroInfo *)nullptr);
8089   unsigned NumSelectorsLoaded
8090     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
8091                                           SelectorsLoaded.end(),
8092                                           Selector());
8093 
8094   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8095     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
8096                  NumSLocEntriesRead, TotalNumSLocEntries,
8097                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8098   if (!TypesLoaded.empty())
8099     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
8100                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
8101                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8102   if (!DeclsLoaded.empty())
8103     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
8104                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8105                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8106   if (!IdentifiersLoaded.empty())
8107     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
8108                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8109                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8110   if (!MacrosLoaded.empty())
8111     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8112                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8113                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8114   if (!SelectorsLoaded.empty())
8115     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
8116                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8117                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8118   if (TotalNumStatements)
8119     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
8120                  NumStatementsRead, TotalNumStatements,
8121                  ((float)NumStatementsRead/TotalNumStatements * 100));
8122   if (TotalNumMacros)
8123     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
8124                  NumMacrosRead, TotalNumMacros,
8125                  ((float)NumMacrosRead/TotalNumMacros * 100));
8126   if (TotalLexicalDeclContexts)
8127     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
8128                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8129                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8130                   * 100));
8131   if (TotalVisibleDeclContexts)
8132     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
8133                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8134                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8135                   * 100));
8136   if (TotalNumMethodPoolEntries)
8137     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
8138                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8139                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8140                   * 100));
8141   if (NumMethodPoolLookups)
8142     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
8143                  NumMethodPoolHits, NumMethodPoolLookups,
8144                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8145   if (NumMethodPoolTableLookups)
8146     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
8147                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
8148                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8149                   * 100.0));
8150   if (NumIdentifierLookupHits)
8151     std::fprintf(stderr,
8152                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
8153                  NumIdentifierLookupHits, NumIdentifierLookups,
8154                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8155 
8156   if (GlobalIndex) {
8157     std::fprintf(stderr, "\n");
8158     GlobalIndex->printStats();
8159   }
8160 
8161   std::fprintf(stderr, "\n");
8162   dump();
8163   std::fprintf(stderr, "\n");
8164 }
8165 
8166 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8167 LLVM_DUMP_METHOD static void
8168 dumpModuleIDMap(StringRef Name,
8169                 const ContinuousRangeMap<Key, ModuleFile *,
8170                                          InitialCapacity> &Map) {
8171   if (Map.begin() == Map.end())
8172     return;
8173 
8174   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8175 
8176   llvm::errs() << Name << ":\n";
8177   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8178        I != IEnd; ++I) {
8179     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
8180       << "\n";
8181   }
8182 }
8183 
8184 LLVM_DUMP_METHOD void ASTReader::dump() {
8185   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8186   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
8187   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8188   dumpModuleIDMap("Global type map", GlobalTypeMap);
8189   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
8190   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
8191   dumpModuleIDMap("Global macro map", GlobalMacroMap);
8192   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
8193   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
8194   dumpModuleIDMap("Global preprocessed entity map",
8195                   GlobalPreprocessedEntityMap);
8196 
8197   llvm::errs() << "\n*** PCH/Modules Loaded:";
8198   for (ModuleFile &M : ModuleMgr)
8199     M.dump();
8200 }
8201 
8202 /// Return the amount of memory used by memory buffers, breaking down
8203 /// by heap-backed versus mmap'ed memory.
8204 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8205   for (ModuleFile &I : ModuleMgr) {
8206     if (llvm::MemoryBuffer *buf = I.Buffer) {
8207       size_t bytes = buf->getBufferSize();
8208       switch (buf->getBufferKind()) {
8209         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8210           sizes.malloc_bytes += bytes;
8211           break;
8212         case llvm::MemoryBuffer::MemoryBuffer_MMap:
8213           sizes.mmap_bytes += bytes;
8214           break;
8215       }
8216     }
8217   }
8218 }
8219 
8220 void ASTReader::InitializeSema(Sema &S) {
8221   SemaObj = &S;
8222   S.addExternalSource(this);
8223 
8224   // Makes sure any declarations that were deserialized "too early"
8225   // still get added to the identifier's declaration chains.
8226   for (uint64_t ID : PreloadedDeclIDs) {
8227     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
8228     pushExternalDeclIntoScope(D, D->getDeclName());
8229   }
8230   PreloadedDeclIDs.clear();
8231 
8232   // FIXME: What happens if these are changed by a module import?
8233   if (!FPPragmaOptions.empty()) {
8234     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8235     SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
8236   }
8237 
8238   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
8239   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
8240   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
8241 
8242   UpdateSema();
8243 }
8244 
8245 void ASTReader::UpdateSema() {
8246   assert(SemaObj && "no Sema to update");
8247 
8248   // Load the offsets of the declarations that Sema references.
8249   // They will be lazily deserialized when needed.
8250   if (!SemaDeclRefs.empty()) {
8251     assert(SemaDeclRefs.size() % 3 == 0);
8252     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8253       if (!SemaObj->StdNamespace)
8254         SemaObj->StdNamespace = SemaDeclRefs[I];
8255       if (!SemaObj->StdBadAlloc)
8256         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
8257       if (!SemaObj->StdAlignValT)
8258         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
8259     }
8260     SemaDeclRefs.clear();
8261   }
8262 
8263   // Update the state of pragmas. Use the same API as if we had encountered the
8264   // pragma in the source.
8265   if(OptimizeOffPragmaLocation.isValid())
8266     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
8267   if (PragmaMSStructState != -1)
8268     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
8269   if (PointersToMembersPragmaLocation.isValid()) {
8270     SemaObj->ActOnPragmaMSPointersToMembers(
8271         (LangOptions::PragmaMSPointersToMembersKind)
8272             PragmaMSPointersToMembersState,
8273         PointersToMembersPragmaLocation);
8274   }
8275   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
8276 
8277   if (PragmaPackCurrentValue) {
8278     // The bottom of the stack might have a default value. It must be adjusted
8279     // to the current value to ensure that the packing state is preserved after
8280     // popping entries that were included/imported from a PCH/module.
8281     bool DropFirst = false;
8282     if (!PragmaPackStack.empty() &&
8283         PragmaPackStack.front().Location.isInvalid()) {
8284       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
8285              "Expected a default alignment value");
8286       SemaObj->PackStack.Stack.emplace_back(
8287           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
8288           SemaObj->PackStack.CurrentPragmaLocation,
8289           PragmaPackStack.front().PushLocation);
8290       DropFirst = true;
8291     }
8292     for (const auto &Entry :
8293          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
8294       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
8295                                             Entry.Location, Entry.PushLocation);
8296     if (PragmaPackCurrentLocation.isInvalid()) {
8297       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
8298              "Expected a default alignment value");
8299       // Keep the current values.
8300     } else {
8301       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
8302       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
8303     }
8304   }
8305 }
8306 
8307 IdentifierInfo *ASTReader::get(StringRef Name) {
8308   // Note that we are loading an identifier.
8309   Deserializing AnIdentifier(this);
8310 
8311   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8312                                   NumIdentifierLookups,
8313                                   NumIdentifierLookupHits);
8314 
8315   // We don't need to do identifier table lookups in C++ modules (we preload
8316   // all interesting declarations, and don't need to use the scope for name
8317   // lookups). Perform the lookup in PCH files, though, since we don't build
8318   // a complete initial identifier table if we're carrying on from a PCH.
8319   if (PP.getLangOpts().CPlusPlus) {
8320     for (auto F : ModuleMgr.pch_modules())
8321       if (Visitor(*F))
8322         break;
8323   } else {
8324     // If there is a global index, look there first to determine which modules
8325     // provably do not have any results for this identifier.
8326     GlobalModuleIndex::HitSet Hits;
8327     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8328     if (!loadGlobalIndex()) {
8329       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8330         HitsPtr = &Hits;
8331       }
8332     }
8333 
8334     ModuleMgr.visit(Visitor, HitsPtr);
8335   }
8336 
8337   IdentifierInfo *II = Visitor.getIdentifierInfo();
8338   markIdentifierUpToDate(II);
8339   return II;
8340 }
8341 
8342 namespace clang {
8343 
8344   /// An identifier-lookup iterator that enumerates all of the
8345   /// identifiers stored within a set of AST files.
8346   class ASTIdentifierIterator : public IdentifierIterator {
8347     /// The AST reader whose identifiers are being enumerated.
8348     const ASTReader &Reader;
8349 
8350     /// The current index into the chain of AST files stored in
8351     /// the AST reader.
8352     unsigned Index;
8353 
8354     /// The current position within the identifier lookup table
8355     /// of the current AST file.
8356     ASTIdentifierLookupTable::key_iterator Current;
8357 
8358     /// The end position within the identifier lookup table of
8359     /// the current AST file.
8360     ASTIdentifierLookupTable::key_iterator End;
8361 
8362     /// Whether to skip any modules in the ASTReader.
8363     bool SkipModules;
8364 
8365   public:
8366     explicit ASTIdentifierIterator(const ASTReader &Reader,
8367                                    bool SkipModules = false);
8368 
8369     StringRef Next() override;
8370   };
8371 
8372 } // namespace clang
8373 
8374 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8375                                              bool SkipModules)
8376     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8377 }
8378 
8379 StringRef ASTIdentifierIterator::Next() {
8380   while (Current == End) {
8381     // If we have exhausted all of our AST files, we're done.
8382     if (Index == 0)
8383       return StringRef();
8384 
8385     --Index;
8386     ModuleFile &F = Reader.ModuleMgr[Index];
8387     if (SkipModules && F.isModule())
8388       continue;
8389 
8390     ASTIdentifierLookupTable *IdTable =
8391         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8392     Current = IdTable->key_begin();
8393     End = IdTable->key_end();
8394   }
8395 
8396   // We have any identifiers remaining in the current AST file; return
8397   // the next one.
8398   StringRef Result = *Current;
8399   ++Current;
8400   return Result;
8401 }
8402 
8403 namespace {
8404 
8405 /// A utility for appending two IdentifierIterators.
8406 class ChainedIdentifierIterator : public IdentifierIterator {
8407   std::unique_ptr<IdentifierIterator> Current;
8408   std::unique_ptr<IdentifierIterator> Queued;
8409 
8410 public:
8411   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8412                             std::unique_ptr<IdentifierIterator> Second)
8413       : Current(std::move(First)), Queued(std::move(Second)) {}
8414 
8415   StringRef Next() override {
8416     if (!Current)
8417       return StringRef();
8418 
8419     StringRef result = Current->Next();
8420     if (!result.empty())
8421       return result;
8422 
8423     // Try the queued iterator, which may itself be empty.
8424     Current.reset();
8425     std::swap(Current, Queued);
8426     return Next();
8427   }
8428 };
8429 
8430 } // namespace
8431 
8432 IdentifierIterator *ASTReader::getIdentifiers() {
8433   if (!loadGlobalIndex()) {
8434     std::unique_ptr<IdentifierIterator> ReaderIter(
8435         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8436     std::unique_ptr<IdentifierIterator> ModulesIter(
8437         GlobalIndex->createIdentifierIterator());
8438     return new ChainedIdentifierIterator(std::move(ReaderIter),
8439                                          std::move(ModulesIter));
8440   }
8441 
8442   return new ASTIdentifierIterator(*this);
8443 }
8444 
8445 namespace clang {
8446 namespace serialization {
8447 
8448   class ReadMethodPoolVisitor {
8449     ASTReader &Reader;
8450     Selector Sel;
8451     unsigned PriorGeneration;
8452     unsigned InstanceBits = 0;
8453     unsigned FactoryBits = 0;
8454     bool InstanceHasMoreThanOneDecl = false;
8455     bool FactoryHasMoreThanOneDecl = false;
8456     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8457     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8458 
8459   public:
8460     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8461                           unsigned PriorGeneration)
8462         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8463 
8464     bool operator()(ModuleFile &M) {
8465       if (!M.SelectorLookupTable)
8466         return false;
8467 
8468       // If we've already searched this module file, skip it now.
8469       if (M.Generation <= PriorGeneration)
8470         return true;
8471 
8472       ++Reader.NumMethodPoolTableLookups;
8473       ASTSelectorLookupTable *PoolTable
8474         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8475       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8476       if (Pos == PoolTable->end())
8477         return false;
8478 
8479       ++Reader.NumMethodPoolTableHits;
8480       ++Reader.NumSelectorsRead;
8481       // FIXME: Not quite happy with the statistics here. We probably should
8482       // disable this tracking when called via LoadSelector.
8483       // Also, should entries without methods count as misses?
8484       ++Reader.NumMethodPoolEntriesRead;
8485       ASTSelectorLookupTrait::data_type Data = *Pos;
8486       if (Reader.DeserializationListener)
8487         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8488 
8489       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8490       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8491       InstanceBits = Data.InstanceBits;
8492       FactoryBits = Data.FactoryBits;
8493       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8494       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8495       return true;
8496     }
8497 
8498     /// Retrieve the instance methods found by this visitor.
8499     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8500       return InstanceMethods;
8501     }
8502 
8503     /// Retrieve the instance methods found by this visitor.
8504     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8505       return FactoryMethods;
8506     }
8507 
8508     unsigned getInstanceBits() const { return InstanceBits; }
8509     unsigned getFactoryBits() const { return FactoryBits; }
8510 
8511     bool instanceHasMoreThanOneDecl() const {
8512       return InstanceHasMoreThanOneDecl;
8513     }
8514 
8515     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8516   };
8517 
8518 } // namespace serialization
8519 } // namespace clang
8520 
8521 /// Add the given set of methods to the method list.
8522 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8523                              ObjCMethodList &List) {
8524   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8525     S.addMethodToGlobalList(&List, Methods[I]);
8526   }
8527 }
8528 
8529 void ASTReader::ReadMethodPool(Selector Sel) {
8530   // Get the selector generation and update it to the current generation.
8531   unsigned &Generation = SelectorGeneration[Sel];
8532   unsigned PriorGeneration = Generation;
8533   Generation = getGeneration();
8534   SelectorOutOfDate[Sel] = false;
8535 
8536   // Search for methods defined with this selector.
8537   ++NumMethodPoolLookups;
8538   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8539   ModuleMgr.visit(Visitor);
8540 
8541   if (Visitor.getInstanceMethods().empty() &&
8542       Visitor.getFactoryMethods().empty())
8543     return;
8544 
8545   ++NumMethodPoolHits;
8546 
8547   if (!getSema())
8548     return;
8549 
8550   Sema &S = *getSema();
8551   Sema::GlobalMethodPool::iterator Pos
8552     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8553 
8554   Pos->second.first.setBits(Visitor.getInstanceBits());
8555   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8556   Pos->second.second.setBits(Visitor.getFactoryBits());
8557   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8558 
8559   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8560   // when building a module we keep every method individually and may need to
8561   // update hasMoreThanOneDecl as we add the methods.
8562   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8563   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8564 }
8565 
8566 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8567   if (SelectorOutOfDate[Sel])
8568     ReadMethodPool(Sel);
8569 }
8570 
8571 void ASTReader::ReadKnownNamespaces(
8572                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8573   Namespaces.clear();
8574 
8575   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8576     if (NamespaceDecl *Namespace
8577                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8578       Namespaces.push_back(Namespace);
8579   }
8580 }
8581 
8582 void ASTReader::ReadUndefinedButUsed(
8583     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8584   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8585     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8586     SourceLocation Loc =
8587         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8588     Undefined.insert(std::make_pair(D, Loc));
8589   }
8590 }
8591 
8592 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8593     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8594                                                      Exprs) {
8595   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8596     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8597     uint64_t Count = DelayedDeleteExprs[Idx++];
8598     for (uint64_t C = 0; C < Count; ++C) {
8599       SourceLocation DeleteLoc =
8600           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8601       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8602       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8603     }
8604   }
8605 }
8606 
8607 void ASTReader::ReadTentativeDefinitions(
8608                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8609   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8610     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8611     if (Var)
8612       TentativeDefs.push_back(Var);
8613   }
8614   TentativeDefinitions.clear();
8615 }
8616 
8617 void ASTReader::ReadUnusedFileScopedDecls(
8618                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8619   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8620     DeclaratorDecl *D
8621       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8622     if (D)
8623       Decls.push_back(D);
8624   }
8625   UnusedFileScopedDecls.clear();
8626 }
8627 
8628 void ASTReader::ReadDelegatingConstructors(
8629                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8630   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8631     CXXConstructorDecl *D
8632       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8633     if (D)
8634       Decls.push_back(D);
8635   }
8636   DelegatingCtorDecls.clear();
8637 }
8638 
8639 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8640   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8641     TypedefNameDecl *D
8642       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8643     if (D)
8644       Decls.push_back(D);
8645   }
8646   ExtVectorDecls.clear();
8647 }
8648 
8649 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8650     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8651   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8652        ++I) {
8653     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8654         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8655     if (D)
8656       Decls.insert(D);
8657   }
8658   UnusedLocalTypedefNameCandidates.clear();
8659 }
8660 
8661 void ASTReader::ReadReferencedSelectors(
8662        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8663   if (ReferencedSelectorsData.empty())
8664     return;
8665 
8666   // If there are @selector references added them to its pool. This is for
8667   // implementation of -Wselector.
8668   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8669   unsigned I = 0;
8670   while (I < DataSize) {
8671     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8672     SourceLocation SelLoc
8673       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8674     Sels.push_back(std::make_pair(Sel, SelLoc));
8675   }
8676   ReferencedSelectorsData.clear();
8677 }
8678 
8679 void ASTReader::ReadWeakUndeclaredIdentifiers(
8680        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8681   if (WeakUndeclaredIdentifiers.empty())
8682     return;
8683 
8684   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8685     IdentifierInfo *WeakId
8686       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8687     IdentifierInfo *AliasId
8688       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8689     SourceLocation Loc
8690       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8691     bool Used = WeakUndeclaredIdentifiers[I++];
8692     WeakInfo WI(AliasId, Loc);
8693     WI.setUsed(Used);
8694     WeakIDs.push_back(std::make_pair(WeakId, WI));
8695   }
8696   WeakUndeclaredIdentifiers.clear();
8697 }
8698 
8699 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8700   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8701     ExternalVTableUse VT;
8702     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8703     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8704     VT.DefinitionRequired = VTableUses[Idx++];
8705     VTables.push_back(VT);
8706   }
8707 
8708   VTableUses.clear();
8709 }
8710 
8711 void ASTReader::ReadPendingInstantiations(
8712        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8713   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8714     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8715     SourceLocation Loc
8716       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8717 
8718     Pending.push_back(std::make_pair(D, Loc));
8719   }
8720   PendingInstantiations.clear();
8721 }
8722 
8723 void ASTReader::ReadLateParsedTemplates(
8724     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8725         &LPTMap) {
8726   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8727        /* In loop */) {
8728     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8729 
8730     auto LT = std::make_unique<LateParsedTemplate>();
8731     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8732 
8733     ModuleFile *F = getOwningModuleFile(LT->D);
8734     assert(F && "No module");
8735 
8736     unsigned TokN = LateParsedTemplates[Idx++];
8737     LT->Toks.reserve(TokN);
8738     for (unsigned T = 0; T < TokN; ++T)
8739       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8740 
8741     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8742   }
8743 
8744   LateParsedTemplates.clear();
8745 }
8746 
8747 void ASTReader::LoadSelector(Selector Sel) {
8748   // It would be complicated to avoid reading the methods anyway. So don't.
8749   ReadMethodPool(Sel);
8750 }
8751 
8752 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8753   assert(ID && "Non-zero identifier ID required");
8754   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8755   IdentifiersLoaded[ID - 1] = II;
8756   if (DeserializationListener)
8757     DeserializationListener->IdentifierRead(ID, II);
8758 }
8759 
8760 /// Set the globally-visible declarations associated with the given
8761 /// identifier.
8762 ///
8763 /// If the AST reader is currently in a state where the given declaration IDs
8764 /// cannot safely be resolved, they are queued until it is safe to resolve
8765 /// them.
8766 ///
8767 /// \param II an IdentifierInfo that refers to one or more globally-visible
8768 /// declarations.
8769 ///
8770 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8771 /// visible at global scope.
8772 ///
8773 /// \param Decls if non-null, this vector will be populated with the set of
8774 /// deserialized declarations. These declarations will not be pushed into
8775 /// scope.
8776 void
8777 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8778                               const SmallVectorImpl<uint32_t> &DeclIDs,
8779                                    SmallVectorImpl<Decl *> *Decls) {
8780   if (NumCurrentElementsDeserializing && !Decls) {
8781     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8782     return;
8783   }
8784 
8785   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8786     if (!SemaObj) {
8787       // Queue this declaration so that it will be added to the
8788       // translation unit scope and identifier's declaration chain
8789       // once a Sema object is known.
8790       PreloadedDeclIDs.push_back(DeclIDs[I]);
8791       continue;
8792     }
8793 
8794     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8795 
8796     // If we're simply supposed to record the declarations, do so now.
8797     if (Decls) {
8798       Decls->push_back(D);
8799       continue;
8800     }
8801 
8802     // Introduce this declaration into the translation-unit scope
8803     // and add it to the declaration chain for this identifier, so
8804     // that (unqualified) name lookup will find it.
8805     pushExternalDeclIntoScope(D, II);
8806   }
8807 }
8808 
8809 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8810   if (ID == 0)
8811     return nullptr;
8812 
8813   if (IdentifiersLoaded.empty()) {
8814     Error("no identifier table in AST file");
8815     return nullptr;
8816   }
8817 
8818   ID -= 1;
8819   if (!IdentifiersLoaded[ID]) {
8820     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8821     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8822     ModuleFile *M = I->second;
8823     unsigned Index = ID - M->BaseIdentifierID;
8824     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8825 
8826     // All of the strings in the AST file are preceded by a 16-bit length.
8827     // Extract that 16-bit length to avoid having to execute strlen().
8828     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8829     //  unsigned integers.  This is important to avoid integer overflow when
8830     //  we cast them to 'unsigned'.
8831     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8832     unsigned StrLen = (((unsigned) StrLenPtr[0])
8833                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8834     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8835     IdentifiersLoaded[ID] = &II;
8836     markIdentifierFromAST(*this,  II);
8837     if (DeserializationListener)
8838       DeserializationListener->IdentifierRead(ID + 1, &II);
8839   }
8840 
8841   return IdentifiersLoaded[ID];
8842 }
8843 
8844 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8845   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8846 }
8847 
8848 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8849   if (LocalID < NUM_PREDEF_IDENT_IDS)
8850     return LocalID;
8851 
8852   if (!M.ModuleOffsetMap.empty())
8853     ReadModuleOffsetMap(M);
8854 
8855   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8856     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8857   assert(I != M.IdentifierRemap.end()
8858          && "Invalid index into identifier index remap");
8859 
8860   return LocalID + I->second;
8861 }
8862 
8863 MacroInfo *ASTReader::getMacro(MacroID ID) {
8864   if (ID == 0)
8865     return nullptr;
8866 
8867   if (MacrosLoaded.empty()) {
8868     Error("no macro table in AST file");
8869     return nullptr;
8870   }
8871 
8872   ID -= NUM_PREDEF_MACRO_IDS;
8873   if (!MacrosLoaded[ID]) {
8874     GlobalMacroMapType::iterator I
8875       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8876     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8877     ModuleFile *M = I->second;
8878     unsigned Index = ID - M->BaseMacroID;
8879     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
8880 
8881     if (DeserializationListener)
8882       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8883                                          MacrosLoaded[ID]);
8884   }
8885 
8886   return MacrosLoaded[ID];
8887 }
8888 
8889 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8890   if (LocalID < NUM_PREDEF_MACRO_IDS)
8891     return LocalID;
8892 
8893   if (!M.ModuleOffsetMap.empty())
8894     ReadModuleOffsetMap(M);
8895 
8896   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8897     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8898   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8899 
8900   return LocalID + I->second;
8901 }
8902 
8903 serialization::SubmoduleID
8904 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8905   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8906     return LocalID;
8907 
8908   if (!M.ModuleOffsetMap.empty())
8909     ReadModuleOffsetMap(M);
8910 
8911   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8912     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8913   assert(I != M.SubmoduleRemap.end()
8914          && "Invalid index into submodule index remap");
8915 
8916   return LocalID + I->second;
8917 }
8918 
8919 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8920   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8921     assert(GlobalID == 0 && "Unhandled global submodule ID");
8922     return nullptr;
8923   }
8924 
8925   if (GlobalID > SubmodulesLoaded.size()) {
8926     Error("submodule ID out of range in AST file");
8927     return nullptr;
8928   }
8929 
8930   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8931 }
8932 
8933 Module *ASTReader::getModule(unsigned ID) {
8934   return getSubmodule(ID);
8935 }
8936 
8937 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8938   ModuleFile *MF = getOwningModuleFile(D);
8939   return MF && MF->PCHHasObjectFile;
8940 }
8941 
8942 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8943   if (ID & 1) {
8944     // It's a module, look it up by submodule ID.
8945     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8946     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8947   } else {
8948     // It's a prefix (preamble, PCH, ...). Look it up by index.
8949     unsigned IndexFromEnd = ID >> 1;
8950     assert(IndexFromEnd && "got reference to unknown module file");
8951     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8952   }
8953 }
8954 
8955 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8956   if (!F)
8957     return 1;
8958 
8959   // For a file representing a module, use the submodule ID of the top-level
8960   // module as the file ID. For any other kind of file, the number of such
8961   // files loaded beforehand will be the same on reload.
8962   // FIXME: Is this true even if we have an explicit module file and a PCH?
8963   if (F->isModule())
8964     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8965 
8966   auto PCHModules = getModuleManager().pch_modules();
8967   auto I = llvm::find(PCHModules, F);
8968   assert(I != PCHModules.end() && "emitting reference to unknown file");
8969   return (I - PCHModules.end()) << 1;
8970 }
8971 
8972 llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8973 ASTReader::getSourceDescriptor(unsigned ID) {
8974   if (const Module *M = getSubmodule(ID))
8975     return ExternalASTSource::ASTSourceDescriptor(*M);
8976 
8977   // If there is only a single PCH, return it instead.
8978   // Chained PCH are not supported.
8979   const auto &PCHChain = ModuleMgr.pch_modules();
8980   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8981     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8982     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8983     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8984     return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8985                                           MF.Signature);
8986   }
8987   return None;
8988 }
8989 
8990 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8991   auto I = DefinitionSource.find(FD);
8992   if (I == DefinitionSource.end())
8993     return EK_ReplyHazy;
8994   return I->second ? EK_Never : EK_Always;
8995 }
8996 
8997 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8998   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8999 }
9000 
9001 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9002   if (ID == 0)
9003     return Selector();
9004 
9005   if (ID > SelectorsLoaded.size()) {
9006     Error("selector ID out of range in AST file");
9007     return Selector();
9008   }
9009 
9010   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9011     // Load this selector from the selector table.
9012     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
9013     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9014     ModuleFile &M = *I->second;
9015     ASTSelectorLookupTrait Trait(*this, M);
9016     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9017     SelectorsLoaded[ID - 1] =
9018       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9019     if (DeserializationListener)
9020       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
9021   }
9022 
9023   return SelectorsLoaded[ID - 1];
9024 }
9025 
9026 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9027   return DecodeSelector(ID);
9028 }
9029 
9030 uint32_t ASTReader::GetNumExternalSelectors() {
9031   // ID 0 (the null selector) is considered an external selector.
9032   return getTotalNumSelectors() + 1;
9033 }
9034 
9035 serialization::SelectorID
9036 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9037   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9038     return LocalID;
9039 
9040   if (!M.ModuleOffsetMap.empty())
9041     ReadModuleOffsetMap(M);
9042 
9043   ContinuousRangeMap<uint32_t, int, 2>::iterator I
9044     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
9045   assert(I != M.SelectorRemap.end()
9046          && "Invalid index into selector index remap");
9047 
9048   return LocalID + I->second;
9049 }
9050 
9051 DeclarationName
9052 ASTReader::ReadDeclarationName(ModuleFile &F,
9053                                const RecordData &Record, unsigned &Idx) {
9054   ASTContext &Context = getContext();
9055   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
9056   switch (Kind) {
9057   case DeclarationName::Identifier:
9058     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
9059 
9060   case DeclarationName::ObjCZeroArgSelector:
9061   case DeclarationName::ObjCOneArgSelector:
9062   case DeclarationName::ObjCMultiArgSelector:
9063     return DeclarationName(ReadSelector(F, Record, Idx));
9064 
9065   case DeclarationName::CXXConstructorName:
9066     return Context.DeclarationNames.getCXXConstructorName(
9067                           Context.getCanonicalType(readType(F, Record, Idx)));
9068 
9069   case DeclarationName::CXXDestructorName:
9070     return Context.DeclarationNames.getCXXDestructorName(
9071                           Context.getCanonicalType(readType(F, Record, Idx)));
9072 
9073   case DeclarationName::CXXDeductionGuideName:
9074     return Context.DeclarationNames.getCXXDeductionGuideName(
9075                           ReadDeclAs<TemplateDecl>(F, Record, Idx));
9076 
9077   case DeclarationName::CXXConversionFunctionName:
9078     return Context.DeclarationNames.getCXXConversionFunctionName(
9079                           Context.getCanonicalType(readType(F, Record, Idx)));
9080 
9081   case DeclarationName::CXXOperatorName:
9082     return Context.DeclarationNames.getCXXOperatorName(
9083                                        (OverloadedOperatorKind)Record[Idx++]);
9084 
9085   case DeclarationName::CXXLiteralOperatorName:
9086     return Context.DeclarationNames.getCXXLiteralOperatorName(
9087                                        GetIdentifierInfo(F, Record, Idx));
9088 
9089   case DeclarationName::CXXUsingDirective:
9090     return DeclarationName::getUsingDirectiveName();
9091   }
9092 
9093   llvm_unreachable("Invalid NameKind!");
9094 }
9095 
9096 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
9097                                        DeclarationNameLoc &DNLoc,
9098                                        DeclarationName Name,
9099                                       const RecordData &Record, unsigned &Idx) {
9100   switch (Name.getNameKind()) {
9101   case DeclarationName::CXXConstructorName:
9102   case DeclarationName::CXXDestructorName:
9103   case DeclarationName::CXXConversionFunctionName:
9104     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
9105     break;
9106 
9107   case DeclarationName::CXXOperatorName:
9108     DNLoc.CXXOperatorName.BeginOpNameLoc
9109         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9110     DNLoc.CXXOperatorName.EndOpNameLoc
9111         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9112     break;
9113 
9114   case DeclarationName::CXXLiteralOperatorName:
9115     DNLoc.CXXLiteralOperatorName.OpNameLoc
9116         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
9117     break;
9118 
9119   case DeclarationName::Identifier:
9120   case DeclarationName::ObjCZeroArgSelector:
9121   case DeclarationName::ObjCOneArgSelector:
9122   case DeclarationName::ObjCMultiArgSelector:
9123   case DeclarationName::CXXUsingDirective:
9124   case DeclarationName::CXXDeductionGuideName:
9125     break;
9126   }
9127 }
9128 
9129 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
9130                                         DeclarationNameInfo &NameInfo,
9131                                       const RecordData &Record, unsigned &Idx) {
9132   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
9133   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
9134   DeclarationNameLoc DNLoc;
9135   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
9136   NameInfo.setInfo(DNLoc);
9137 }
9138 
9139 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
9140                                   const RecordData &Record, unsigned &Idx) {
9141   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
9142   unsigned NumTPLists = Record[Idx++];
9143   Info.NumTemplParamLists = NumTPLists;
9144   if (NumTPLists) {
9145     Info.TemplParamLists =
9146         new (getContext()) TemplateParameterList *[NumTPLists];
9147     for (unsigned i = 0; i != NumTPLists; ++i)
9148       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
9149   }
9150 }
9151 
9152 TemplateName
9153 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
9154                             unsigned &Idx) {
9155   ASTContext &Context = getContext();
9156   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
9157   switch (Kind) {
9158   case TemplateName::Template:
9159       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
9160 
9161   case TemplateName::OverloadedTemplate: {
9162     unsigned size = Record[Idx++];
9163     UnresolvedSet<8> Decls;
9164     while (size--)
9165       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
9166 
9167     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
9168   }
9169 
9170   case TemplateName::AssumedTemplate: {
9171     DeclarationName Name = ReadDeclarationName(F, Record, Idx);
9172     return Context.getAssumedTemplateName(Name);
9173   }
9174 
9175   case TemplateName::QualifiedTemplate: {
9176     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9177     bool hasTemplKeyword = Record[Idx++];
9178     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
9179     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
9180   }
9181 
9182   case TemplateName::DependentTemplate: {
9183     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
9184     if (Record[Idx++])  // isIdentifier
9185       return Context.getDependentTemplateName(NNS,
9186                                                GetIdentifierInfo(F, Record,
9187                                                                  Idx));
9188     return Context.getDependentTemplateName(NNS,
9189                                          (OverloadedOperatorKind)Record[Idx++]);
9190   }
9191 
9192   case TemplateName::SubstTemplateTemplateParm: {
9193     TemplateTemplateParmDecl *param
9194       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9195     if (!param) return TemplateName();
9196     TemplateName replacement = ReadTemplateName(F, Record, Idx);
9197     return Context.getSubstTemplateTemplateParm(param, replacement);
9198   }
9199 
9200   case TemplateName::SubstTemplateTemplateParmPack: {
9201     TemplateTemplateParmDecl *Param
9202       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
9203     if (!Param)
9204       return TemplateName();
9205 
9206     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
9207     if (ArgPack.getKind() != TemplateArgument::Pack)
9208       return TemplateName();
9209 
9210     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
9211   }
9212   }
9213 
9214   llvm_unreachable("Unhandled template name kind!");
9215 }
9216 
9217 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
9218                                                  const RecordData &Record,
9219                                                  unsigned &Idx,
9220                                                  bool Canonicalize) {
9221   ASTContext &Context = getContext();
9222   if (Canonicalize) {
9223     // The caller wants a canonical template argument. Sometimes the AST only
9224     // wants template arguments in canonical form (particularly as the template
9225     // argument lists of template specializations) so ensure we preserve that
9226     // canonical form across serialization.
9227     TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
9228     return Context.getCanonicalTemplateArgument(Arg);
9229   }
9230 
9231   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
9232   switch (Kind) {
9233   case TemplateArgument::Null:
9234     return TemplateArgument();
9235   case TemplateArgument::Type:
9236     return TemplateArgument(readType(F, Record, Idx));
9237   case TemplateArgument::Declaration: {
9238     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
9239     return TemplateArgument(D, readType(F, Record, Idx));
9240   }
9241   case TemplateArgument::NullPtr:
9242     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
9243   case TemplateArgument::Integral: {
9244     llvm::APSInt Value = ReadAPSInt(Record, Idx);
9245     QualType T = readType(F, Record, Idx);
9246     return TemplateArgument(Context, Value, T);
9247   }
9248   case TemplateArgument::Template:
9249     return TemplateArgument(ReadTemplateName(F, Record, Idx));
9250   case TemplateArgument::TemplateExpansion: {
9251     TemplateName Name = ReadTemplateName(F, Record, Idx);
9252     Optional<unsigned> NumTemplateExpansions;
9253     if (unsigned NumExpansions = Record[Idx++])
9254       NumTemplateExpansions = NumExpansions - 1;
9255     return TemplateArgument(Name, NumTemplateExpansions);
9256   }
9257   case TemplateArgument::Expression:
9258     return TemplateArgument(ReadExpr(F));
9259   case TemplateArgument::Pack: {
9260     unsigned NumArgs = Record[Idx++];
9261     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
9262     for (unsigned I = 0; I != NumArgs; ++I)
9263       Args[I] = ReadTemplateArgument(F, Record, Idx);
9264     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
9265   }
9266   }
9267 
9268   llvm_unreachable("Unhandled template argument kind!");
9269 }
9270 
9271 TemplateParameterList *
9272 ASTReader::ReadTemplateParameterList(ModuleFile &F,
9273                                      const RecordData &Record, unsigned &Idx) {
9274   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
9275   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
9276   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
9277 
9278   unsigned NumParams = Record[Idx++];
9279   SmallVector<NamedDecl *, 16> Params;
9280   Params.reserve(NumParams);
9281   while (NumParams--)
9282     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
9283 
9284   // TODO: Concepts
9285   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9286       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
9287   return TemplateParams;
9288 }
9289 
9290 void
9291 ASTReader::
9292 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
9293                          ModuleFile &F, const RecordData &Record,
9294                          unsigned &Idx, bool Canonicalize) {
9295   unsigned NumTemplateArgs = Record[Idx++];
9296   TemplArgs.reserve(NumTemplateArgs);
9297   while (NumTemplateArgs--)
9298     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
9299 }
9300 
9301 /// Read a UnresolvedSet structure.
9302 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
9303                                   const RecordData &Record, unsigned &Idx) {
9304   unsigned NumDecls = Record[Idx++];
9305   Set.reserve(getContext(), NumDecls);
9306   while (NumDecls--) {
9307     DeclID ID = ReadDeclID(F, Record, Idx);
9308     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
9309     Set.addLazyDecl(getContext(), ID, AS);
9310   }
9311 }
9312 
9313 CXXBaseSpecifier
9314 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
9315                                 const RecordData &Record, unsigned &Idx) {
9316   bool isVirtual = static_cast<bool>(Record[Idx++]);
9317   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
9318   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
9319   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
9320   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
9321   SourceRange Range = ReadSourceRange(F, Record, Idx);
9322   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
9323   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9324                           EllipsisLoc);
9325   Result.setInheritConstructors(inheritConstructors);
9326   return Result;
9327 }
9328 
9329 CXXCtorInitializer **
9330 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
9331                                    unsigned &Idx) {
9332   ASTContext &Context = getContext();
9333   unsigned NumInitializers = Record[Idx++];
9334   assert(NumInitializers && "wrote ctor initializers but have no inits");
9335   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9336   for (unsigned i = 0; i != NumInitializers; ++i) {
9337     TypeSourceInfo *TInfo = nullptr;
9338     bool IsBaseVirtual = false;
9339     FieldDecl *Member = nullptr;
9340     IndirectFieldDecl *IndirectMember = nullptr;
9341 
9342     CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
9343     switch (Type) {
9344     case CTOR_INITIALIZER_BASE:
9345       TInfo = GetTypeSourceInfo(F, Record, Idx);
9346       IsBaseVirtual = Record[Idx++];
9347       break;
9348 
9349     case CTOR_INITIALIZER_DELEGATING:
9350       TInfo = GetTypeSourceInfo(F, Record, Idx);
9351       break;
9352 
9353      case CTOR_INITIALIZER_MEMBER:
9354       Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
9355       break;
9356 
9357      case CTOR_INITIALIZER_INDIRECT_MEMBER:
9358       IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
9359       break;
9360     }
9361 
9362     SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
9363     Expr *Init = ReadExpr(F);
9364     SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
9365     SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
9366 
9367     CXXCtorInitializer *BOMInit;
9368     if (Type == CTOR_INITIALIZER_BASE)
9369       BOMInit = new (Context)
9370           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9371                              RParenLoc, MemberOrEllipsisLoc);
9372     else if (Type == CTOR_INITIALIZER_DELEGATING)
9373       BOMInit = new (Context)
9374           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9375     else if (Member)
9376       BOMInit = new (Context)
9377           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9378                              Init, RParenLoc);
9379     else
9380       BOMInit = new (Context)
9381           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9382                              LParenLoc, Init, RParenLoc);
9383 
9384     if (/*IsWritten*/Record[Idx++]) {
9385       unsigned SourceOrder = Record[Idx++];
9386       BOMInit->setSourceOrder(SourceOrder);
9387     }
9388 
9389     CtorInitializers[i] = BOMInit;
9390   }
9391 
9392   return CtorInitializers;
9393 }
9394 
9395 NestedNameSpecifier *
9396 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
9397                                    const RecordData &Record, unsigned &Idx) {
9398   ASTContext &Context = getContext();
9399   unsigned N = Record[Idx++];
9400   NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
9401   for (unsigned I = 0; I != N; ++I) {
9402     NestedNameSpecifier::SpecifierKind Kind
9403       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9404     switch (Kind) {
9405     case NestedNameSpecifier::Identifier: {
9406       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9407       NNS = NestedNameSpecifier::Create(Context, Prev, II);
9408       break;
9409     }
9410 
9411     case NestedNameSpecifier::Namespace: {
9412       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9413       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
9414       break;
9415     }
9416 
9417     case NestedNameSpecifier::NamespaceAlias: {
9418       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9419       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
9420       break;
9421     }
9422 
9423     case NestedNameSpecifier::TypeSpec:
9424     case NestedNameSpecifier::TypeSpecWithTemplate: {
9425       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
9426       if (!T)
9427         return nullptr;
9428 
9429       bool Template = Record[Idx++];
9430       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
9431       break;
9432     }
9433 
9434     case NestedNameSpecifier::Global:
9435       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
9436       // No associated value, and there can't be a prefix.
9437       break;
9438 
9439     case NestedNameSpecifier::Super: {
9440       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9441       NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
9442       break;
9443     }
9444     }
9445     Prev = NNS;
9446   }
9447   return NNS;
9448 }
9449 
9450 NestedNameSpecifierLoc
9451 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
9452                                       unsigned &Idx) {
9453   ASTContext &Context = getContext();
9454   unsigned N = Record[Idx++];
9455   NestedNameSpecifierLocBuilder Builder;
9456   for (unsigned I = 0; I != N; ++I) {
9457     NestedNameSpecifier::SpecifierKind Kind
9458       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
9459     switch (Kind) {
9460     case NestedNameSpecifier::Identifier: {
9461       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
9462       SourceRange Range = ReadSourceRange(F, Record, Idx);
9463       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
9464       break;
9465     }
9466 
9467     case NestedNameSpecifier::Namespace: {
9468       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
9469       SourceRange Range = ReadSourceRange(F, Record, Idx);
9470       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
9471       break;
9472     }
9473 
9474     case NestedNameSpecifier::NamespaceAlias: {
9475       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
9476       SourceRange Range = ReadSourceRange(F, Record, Idx);
9477       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
9478       break;
9479     }
9480 
9481     case NestedNameSpecifier::TypeSpec:
9482     case NestedNameSpecifier::TypeSpecWithTemplate: {
9483       bool Template = Record[Idx++];
9484       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
9485       if (!T)
9486         return NestedNameSpecifierLoc();
9487       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9488 
9489       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
9490       Builder.Extend(Context,
9491                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9492                      T->getTypeLoc(), ColonColonLoc);
9493       break;
9494     }
9495 
9496     case NestedNameSpecifier::Global: {
9497       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9498       Builder.MakeGlobal(Context, ColonColonLoc);
9499       break;
9500     }
9501 
9502     case NestedNameSpecifier::Super: {
9503       CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
9504       SourceRange Range = ReadSourceRange(F, Record, Idx);
9505       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9506       break;
9507     }
9508     }
9509   }
9510 
9511   return Builder.getWithLocInContext(Context);
9512 }
9513 
9514 SourceRange
9515 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9516                            unsigned &Idx) {
9517   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9518   SourceLocation end = ReadSourceLocation(F, Record, Idx);
9519   return SourceRange(beg, end);
9520 }
9521 
9522 static FixedPointSemantics
9523 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
9524                         unsigned &Idx) {
9525   unsigned Width = Record[Idx++];
9526   unsigned Scale = Record[Idx++];
9527   uint64_t Tmp = Record[Idx++];
9528   bool IsSigned = Tmp & 0x1;
9529   bool IsSaturated = Tmp & 0x2;
9530   bool HasUnsignedPadding = Tmp & 0x4;
9531   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
9532                              HasUnsignedPadding);
9533 }
9534 
9535 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) {
9536   unsigned Kind = Record[Idx++];
9537   switch (Kind) {
9538   case APValue::None:
9539     return APValue();
9540   case APValue::Indeterminate:
9541     return APValue::IndeterminateValue();
9542   case APValue::Int:
9543     return APValue(ReadAPSInt(Record, Idx));
9544   case APValue::Float: {
9545     const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics(
9546         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9547     return APValue(ReadAPFloat(Record, FloatSema, Idx));
9548   }
9549   case APValue::FixedPoint: {
9550     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
9551     return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema));
9552   }
9553   case APValue::ComplexInt: {
9554     llvm::APSInt First = ReadAPSInt(Record, Idx);
9555     return APValue(std::move(First), ReadAPSInt(Record, Idx));
9556   }
9557   case APValue::ComplexFloat: {
9558     const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics(
9559         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9560     llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx);
9561     const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics(
9562         static_cast<llvm::APFloatBase::Semantics>(Record[Idx++]));
9563     return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx));
9564   }
9565   case APValue::LValue:
9566   case APValue::Vector:
9567   case APValue::Array:
9568   case APValue::Struct:
9569   case APValue::Union:
9570   case APValue::MemberPointer:
9571   case APValue::AddrLabelDiff:
9572     // TODO : Handle all these APValue::ValueKind.
9573     return APValue();
9574   }
9575   llvm_unreachable("Invalid APValue::ValueKind");
9576 }
9577 
9578 /// Read an integral value
9579 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9580   unsigned BitWidth = Record[Idx++];
9581   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9582   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9583   Idx += NumWords;
9584   return Result;
9585 }
9586 
9587 /// Read a signed integral value
9588 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9589   bool isUnsigned = Record[Idx++];
9590   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9591 }
9592 
9593 /// Read a floating-point value
9594 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9595                                      const llvm::fltSemantics &Sem,
9596                                      unsigned &Idx) {
9597   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
9598 }
9599 
9600 // Read a string
9601 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9602   unsigned Len = Record[Idx++];
9603   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9604   Idx += Len;
9605   return Result;
9606 }
9607 
9608 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9609                                 unsigned &Idx) {
9610   std::string Filename = ReadString(Record, Idx);
9611   ResolveImportedPath(F, Filename);
9612   return Filename;
9613 }
9614 
9615 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9616                                 const RecordData &Record, unsigned &Idx) {
9617   std::string Filename = ReadString(Record, Idx);
9618   if (!BaseDirectory.empty())
9619     ResolveImportedPath(Filename, BaseDirectory);
9620   return Filename;
9621 }
9622 
9623 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9624                                          unsigned &Idx) {
9625   unsigned Major = Record[Idx++];
9626   unsigned Minor = Record[Idx++];
9627   unsigned Subminor = Record[Idx++];
9628   if (Minor == 0)
9629     return VersionTuple(Major);
9630   if (Subminor == 0)
9631     return VersionTuple(Major, Minor - 1);
9632   return VersionTuple(Major, Minor - 1, Subminor - 1);
9633 }
9634 
9635 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9636                                           const RecordData &Record,
9637                                           unsigned &Idx) {
9638   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9639   return CXXTemporary::Create(getContext(), Decl);
9640 }
9641 
9642 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9643   return Diag(CurrentImportLoc, DiagID);
9644 }
9645 
9646 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9647   return Diags.Report(Loc, DiagID);
9648 }
9649 
9650 /// Retrieve the identifier table associated with the
9651 /// preprocessor.
9652 IdentifierTable &ASTReader::getIdentifierTable() {
9653   return PP.getIdentifierTable();
9654 }
9655 
9656 /// Record that the given ID maps to the given switch-case
9657 /// statement.
9658 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9659   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9660          "Already have a SwitchCase with this ID");
9661   (*CurrSwitchCaseStmts)[ID] = SC;
9662 }
9663 
9664 /// Retrieve the switch-case statement with the given ID.
9665 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9666   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9667   return (*CurrSwitchCaseStmts)[ID];
9668 }
9669 
9670 void ASTReader::ClearSwitchCaseIDs() {
9671   CurrSwitchCaseStmts->clear();
9672 }
9673 
9674 void ASTReader::ReadComments() {
9675   ASTContext &Context = getContext();
9676   std::vector<RawComment *> Comments;
9677   for (SmallVectorImpl<std::pair<BitstreamCursor,
9678                                  serialization::ModuleFile *>>::iterator
9679        I = CommentsCursors.begin(),
9680        E = CommentsCursors.end();
9681        I != E; ++I) {
9682     Comments.clear();
9683     BitstreamCursor &Cursor = I->first;
9684     serialization::ModuleFile &F = *I->second;
9685     SavedStreamPosition SavedPosition(Cursor);
9686 
9687     RecordData Record;
9688     while (true) {
9689       Expected<llvm::BitstreamEntry> MaybeEntry =
9690           Cursor.advanceSkippingSubblocks(
9691               BitstreamCursor::AF_DontPopBlockAtEnd);
9692       if (!MaybeEntry) {
9693         Error(MaybeEntry.takeError());
9694         return;
9695       }
9696       llvm::BitstreamEntry Entry = MaybeEntry.get();
9697 
9698       switch (Entry.Kind) {
9699       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9700       case llvm::BitstreamEntry::Error:
9701         Error("malformed block record in AST file");
9702         return;
9703       case llvm::BitstreamEntry::EndBlock:
9704         goto NextCursor;
9705       case llvm::BitstreamEntry::Record:
9706         // The interesting case.
9707         break;
9708       }
9709 
9710       // Read a record.
9711       Record.clear();
9712       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9713       if (!MaybeComment) {
9714         Error(MaybeComment.takeError());
9715         return;
9716       }
9717       switch ((CommentRecordTypes)MaybeComment.get()) {
9718       case COMMENTS_RAW_COMMENT: {
9719         unsigned Idx = 0;
9720         SourceRange SR = ReadSourceRange(F, Record, Idx);
9721         RawComment::CommentKind Kind =
9722             (RawComment::CommentKind) Record[Idx++];
9723         bool IsTrailingComment = Record[Idx++];
9724         bool IsAlmostTrailingComment = Record[Idx++];
9725         Comments.push_back(new (Context) RawComment(
9726             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9727         break;
9728       }
9729       }
9730     }
9731   NextCursor:
9732     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9733         FileToOffsetToComment;
9734     for (RawComment *C : Comments) {
9735       SourceLocation CommentLoc = C->getBeginLoc();
9736       if (CommentLoc.isValid()) {
9737         std::pair<FileID, unsigned> Loc =
9738             SourceMgr.getDecomposedLoc(CommentLoc);
9739         if (Loc.first.isValid())
9740           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9741       }
9742     }
9743   }
9744 }
9745 
9746 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9747                                 bool IncludeSystem, bool Complain,
9748                     llvm::function_ref<void(const serialization::InputFile &IF,
9749                                             bool isSystem)> Visitor) {
9750   unsigned NumUserInputs = MF.NumUserInputFiles;
9751   unsigned NumInputs = MF.InputFilesLoaded.size();
9752   assert(NumUserInputs <= NumInputs);
9753   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9754   for (unsigned I = 0; I < N; ++I) {
9755     bool IsSystem = I >= NumUserInputs;
9756     InputFile IF = getInputFile(MF, I+1, Complain);
9757     Visitor(IF, IsSystem);
9758   }
9759 }
9760 
9761 void ASTReader::visitTopLevelModuleMaps(
9762     serialization::ModuleFile &MF,
9763     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9764   unsigned NumInputs = MF.InputFilesLoaded.size();
9765   for (unsigned I = 0; I < NumInputs; ++I) {
9766     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9767     if (IFI.TopLevelModuleMap)
9768       // FIXME: This unnecessarily re-reads the InputFileInfo.
9769       if (auto *FE = getInputFile(MF, I + 1).getFile())
9770         Visitor(FE);
9771   }
9772 }
9773 
9774 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9775   // If we know the owning module, use it.
9776   if (Module *M = D->getImportedOwningModule())
9777     return M->getFullModuleName();
9778 
9779   // Otherwise, use the name of the top-level module the decl is within.
9780   if (ModuleFile *M = getOwningModuleFile(D))
9781     return M->ModuleName;
9782 
9783   // Not from a module.
9784   return {};
9785 }
9786 
9787 void ASTReader::finishPendingActions() {
9788   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9789          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9790          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9791          !PendingUpdateRecords.empty()) {
9792     // If any identifiers with corresponding top-level declarations have
9793     // been loaded, load those declarations now.
9794     using TopLevelDeclsMap =
9795         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9796     TopLevelDeclsMap TopLevelDecls;
9797 
9798     while (!PendingIdentifierInfos.empty()) {
9799       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9800       SmallVector<uint32_t, 4> DeclIDs =
9801           std::move(PendingIdentifierInfos.back().second);
9802       PendingIdentifierInfos.pop_back();
9803 
9804       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9805     }
9806 
9807     // Load each function type that we deferred loading because it was a
9808     // deduced type that might refer to a local type declared within itself.
9809     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9810       auto *FD = PendingFunctionTypes[I].first;
9811       FD->setType(GetType(PendingFunctionTypes[I].second));
9812 
9813       // If we gave a function a deduced return type, remember that we need to
9814       // propagate that along the redeclaration chain.
9815       auto *DT = FD->getReturnType()->getContainedDeducedType();
9816       if (DT && DT->isDeduced())
9817         PendingDeducedTypeUpdates.insert(
9818             {FD->getCanonicalDecl(), FD->getReturnType()});
9819     }
9820     PendingFunctionTypes.clear();
9821 
9822     // For each decl chain that we wanted to complete while deserializing, mark
9823     // it as "still needs to be completed".
9824     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9825       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9826     }
9827     PendingIncompleteDeclChains.clear();
9828 
9829     // Load pending declaration chains.
9830     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9831       loadPendingDeclChain(PendingDeclChains[I].first,
9832                            PendingDeclChains[I].second);
9833     PendingDeclChains.clear();
9834 
9835     // Make the most recent of the top-level declarations visible.
9836     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9837            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9838       IdentifierInfo *II = TLD->first;
9839       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9840         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9841       }
9842     }
9843 
9844     // Load any pending macro definitions.
9845     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9846       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9847       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9848       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9849       // Initialize the macro history from chained-PCHs ahead of module imports.
9850       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9851            ++IDIdx) {
9852         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9853         if (!Info.M->isModule())
9854           resolvePendingMacro(II, Info);
9855       }
9856       // Handle module imports.
9857       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9858            ++IDIdx) {
9859         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9860         if (Info.M->isModule())
9861           resolvePendingMacro(II, Info);
9862       }
9863     }
9864     PendingMacroIDs.clear();
9865 
9866     // Wire up the DeclContexts for Decls that we delayed setting until
9867     // recursive loading is completed.
9868     while (!PendingDeclContextInfos.empty()) {
9869       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9870       PendingDeclContextInfos.pop_front();
9871       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9872       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9873       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9874     }
9875 
9876     // Perform any pending declaration updates.
9877     while (!PendingUpdateRecords.empty()) {
9878       auto Update = PendingUpdateRecords.pop_back_val();
9879       ReadingKindTracker ReadingKind(Read_Decl, *this);
9880       loadDeclUpdateRecords(Update);
9881     }
9882   }
9883 
9884   // At this point, all update records for loaded decls are in place, so any
9885   // fake class definitions should have become real.
9886   assert(PendingFakeDefinitionData.empty() &&
9887          "faked up a class definition but never saw the real one");
9888 
9889   // If we deserialized any C++ or Objective-C class definitions, any
9890   // Objective-C protocol definitions, or any redeclarable templates, make sure
9891   // that all redeclarations point to the definitions. Note that this can only
9892   // happen now, after the redeclaration chains have been fully wired.
9893   for (Decl *D : PendingDefinitions) {
9894     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9895       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9896         // Make sure that the TagType points at the definition.
9897         const_cast<TagType*>(TagT)->decl = TD;
9898       }
9899 
9900       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9901         for (auto *R = getMostRecentExistingDecl(RD); R;
9902              R = R->getPreviousDecl()) {
9903           assert((R == D) ==
9904                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9905                  "declaration thinks it's the definition but it isn't");
9906           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9907         }
9908       }
9909 
9910       continue;
9911     }
9912 
9913     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9914       // Make sure that the ObjCInterfaceType points at the definition.
9915       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9916         ->Decl = ID;
9917 
9918       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9919         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9920 
9921       continue;
9922     }
9923 
9924     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9925       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9926         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9927 
9928       continue;
9929     }
9930 
9931     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9932     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9933       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9934   }
9935   PendingDefinitions.clear();
9936 
9937   // Load the bodies of any functions or methods we've encountered. We do
9938   // this now (delayed) so that we can be sure that the declaration chains
9939   // have been fully wired up (hasBody relies on this).
9940   // FIXME: We shouldn't require complete redeclaration chains here.
9941   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9942                                PBEnd = PendingBodies.end();
9943        PB != PBEnd; ++PB) {
9944     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9945       // For a function defined inline within a class template, force the
9946       // canonical definition to be the one inside the canonical definition of
9947       // the template. This ensures that we instantiate from a correct view
9948       // of the template.
9949       //
9950       // Sadly we can't do this more generally: we can't be sure that all
9951       // copies of an arbitrary class definition will have the same members
9952       // defined (eg, some member functions may not be instantiated, and some
9953       // special members may or may not have been implicitly defined).
9954       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9955         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9956           continue;
9957 
9958       // FIXME: Check for =delete/=default?
9959       // FIXME: Complain about ODR violations here?
9960       const FunctionDecl *Defn = nullptr;
9961       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9962         FD->setLazyBody(PB->second);
9963       } else {
9964         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9965         mergeDefinitionVisibility(NonConstDefn, FD);
9966 
9967         if (!FD->isLateTemplateParsed() &&
9968             !NonConstDefn->isLateTemplateParsed() &&
9969             FD->getODRHash() != NonConstDefn->getODRHash()) {
9970           if (!isa<CXXMethodDecl>(FD)) {
9971             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9972           } else if (FD->getLexicalParent()->isFileContext() &&
9973                      NonConstDefn->getLexicalParent()->isFileContext()) {
9974             // Only diagnose out-of-line method definitions.  If they are
9975             // in class definitions, then an error will be generated when
9976             // processing the class bodies.
9977             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9978           }
9979         }
9980       }
9981       continue;
9982     }
9983 
9984     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9985     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9986       MD->setLazyBody(PB->second);
9987   }
9988   PendingBodies.clear();
9989 
9990   // Do some cleanup.
9991   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9992     getContext().deduplicateMergedDefinitonsFor(ND);
9993   PendingMergedDefinitionsToDeduplicate.clear();
9994 }
9995 
9996 void ASTReader::diagnoseOdrViolations() {
9997   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9998       PendingFunctionOdrMergeFailures.empty() &&
9999       PendingEnumOdrMergeFailures.empty())
10000     return;
10001 
10002   // Trigger the import of the full definition of each class that had any
10003   // odr-merging problems, so we can produce better diagnostics for them.
10004   // These updates may in turn find and diagnose some ODR failures, so take
10005   // ownership of the set first.
10006   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10007   PendingOdrMergeFailures.clear();
10008   for (auto &Merge : OdrMergeFailures) {
10009     Merge.first->buildLookup();
10010     Merge.first->decls_begin();
10011     Merge.first->bases_begin();
10012     Merge.first->vbases_begin();
10013     for (auto &RecordPair : Merge.second) {
10014       auto *RD = RecordPair.first;
10015       RD->decls_begin();
10016       RD->bases_begin();
10017       RD->vbases_begin();
10018     }
10019   }
10020 
10021   // Trigger the import of functions.
10022   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10023   PendingFunctionOdrMergeFailures.clear();
10024   for (auto &Merge : FunctionOdrMergeFailures) {
10025     Merge.first->buildLookup();
10026     Merge.first->decls_begin();
10027     Merge.first->getBody();
10028     for (auto &FD : Merge.second) {
10029       FD->buildLookup();
10030       FD->decls_begin();
10031       FD->getBody();
10032     }
10033   }
10034 
10035   // Trigger the import of enums.
10036   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10037   PendingEnumOdrMergeFailures.clear();
10038   for (auto &Merge : EnumOdrMergeFailures) {
10039     Merge.first->decls_begin();
10040     for (auto &Enum : Merge.second) {
10041       Enum->decls_begin();
10042     }
10043   }
10044 
10045   // For each declaration from a merged context, check that the canonical
10046   // definition of that context also contains a declaration of the same
10047   // entity.
10048   //
10049   // Caution: this loop does things that might invalidate iterators into
10050   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10051   while (!PendingOdrMergeChecks.empty()) {
10052     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10053 
10054     // FIXME: Skip over implicit declarations for now. This matters for things
10055     // like implicitly-declared special member functions. This isn't entirely
10056     // correct; we can end up with multiple unmerged declarations of the same
10057     // implicit entity.
10058     if (D->isImplicit())
10059       continue;
10060 
10061     DeclContext *CanonDef = D->getDeclContext();
10062 
10063     bool Found = false;
10064     const Decl *DCanon = D->getCanonicalDecl();
10065 
10066     for (auto RI : D->redecls()) {
10067       if (RI->getLexicalDeclContext() == CanonDef) {
10068         Found = true;
10069         break;
10070       }
10071     }
10072     if (Found)
10073       continue;
10074 
10075     // Quick check failed, time to do the slow thing. Note, we can't just
10076     // look up the name of D in CanonDef here, because the member that is
10077     // in CanonDef might not be found by name lookup (it might have been
10078     // replaced by a more recent declaration in the lookup table), and we
10079     // can't necessarily find it in the redeclaration chain because it might
10080     // be merely mergeable, not redeclarable.
10081     llvm::SmallVector<const NamedDecl*, 4> Candidates;
10082     for (auto *CanonMember : CanonDef->decls()) {
10083       if (CanonMember->getCanonicalDecl() == DCanon) {
10084         // This can happen if the declaration is merely mergeable and not
10085         // actually redeclarable (we looked for redeclarations earlier).
10086         //
10087         // FIXME: We should be able to detect this more efficiently, without
10088         // pulling in all of the members of CanonDef.
10089         Found = true;
10090         break;
10091       }
10092       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
10093         if (ND->getDeclName() == D->getDeclName())
10094           Candidates.push_back(ND);
10095     }
10096 
10097     if (!Found) {
10098       // The AST doesn't like TagDecls becoming invalid after they've been
10099       // completed. We only really need to mark FieldDecls as invalid here.
10100       if (!isa<TagDecl>(D))
10101         D->setInvalidDecl();
10102 
10103       // Ensure we don't accidentally recursively enter deserialization while
10104       // we're producing our diagnostic.
10105       Deserializing RecursionGuard(this);
10106 
10107       std::string CanonDefModule =
10108           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
10109       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
10110         << D << getOwningModuleNameForDiagnostic(D)
10111         << CanonDef << CanonDefModule.empty() << CanonDefModule;
10112 
10113       if (Candidates.empty())
10114         Diag(cast<Decl>(CanonDef)->getLocation(),
10115              diag::note_module_odr_violation_no_possible_decls) << D;
10116       else {
10117         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10118           Diag(Candidates[I]->getLocation(),
10119                diag::note_module_odr_violation_possible_decl)
10120             << Candidates[I];
10121       }
10122 
10123       DiagnosedOdrMergeFailures.insert(CanonDef);
10124     }
10125   }
10126 
10127   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
10128       EnumOdrMergeFailures.empty())
10129     return;
10130 
10131   // Ensure we don't accidentally recursively enter deserialization while
10132   // we're producing our diagnostics.
10133   Deserializing RecursionGuard(this);
10134 
10135   // Common code for hashing helpers.
10136   ODRHash Hash;
10137   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
10138     Hash.clear();
10139     Hash.AddQualType(Ty);
10140     return Hash.CalculateHash();
10141   };
10142 
10143   auto ComputeODRHash = [&Hash](const Stmt *S) {
10144     assert(S);
10145     Hash.clear();
10146     Hash.AddStmt(S);
10147     return Hash.CalculateHash();
10148   };
10149 
10150   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
10151     assert(D);
10152     Hash.clear();
10153     Hash.AddSubDecl(D);
10154     return Hash.CalculateHash();
10155   };
10156 
10157   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
10158     Hash.clear();
10159     Hash.AddTemplateArgument(TA);
10160     return Hash.CalculateHash();
10161   };
10162 
10163   auto ComputeTemplateParameterListODRHash =
10164       [&Hash](const TemplateParameterList *TPL) {
10165         assert(TPL);
10166         Hash.clear();
10167         Hash.AddTemplateParameterList(TPL);
10168         return Hash.CalculateHash();
10169       };
10170 
10171   // Issue any pending ODR-failure diagnostics.
10172   for (auto &Merge : OdrMergeFailures) {
10173     // If we've already pointed out a specific problem with this class, don't
10174     // bother issuing a general "something's different" diagnostic.
10175     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10176       continue;
10177 
10178     bool Diagnosed = false;
10179     CXXRecordDecl *FirstRecord = Merge.first;
10180     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10181     for (auto &RecordPair : Merge.second) {
10182       CXXRecordDecl *SecondRecord = RecordPair.first;
10183       // Multiple different declarations got merged together; tell the user
10184       // where they came from.
10185       if (FirstRecord == SecondRecord)
10186         continue;
10187 
10188       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10189 
10190       auto *FirstDD = FirstRecord->DefinitionData;
10191       auto *SecondDD = RecordPair.second;
10192 
10193       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10194 
10195       // Diagnostics from DefinitionData are emitted here.
10196       if (FirstDD != SecondDD) {
10197         enum ODRDefinitionDataDifference {
10198           NumBases,
10199           NumVBases,
10200           BaseType,
10201           BaseVirtual,
10202           BaseAccess,
10203         };
10204         auto ODRDiagError = [FirstRecord, &FirstModule,
10205                              this](SourceLocation Loc, SourceRange Range,
10206                                    ODRDefinitionDataDifference DiffType) {
10207           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10208                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10209                  << DiffType;
10210         };
10211         auto ODRDiagNote = [&SecondModule,
10212                             this](SourceLocation Loc, SourceRange Range,
10213                                   ODRDefinitionDataDifference DiffType) {
10214           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10215                  << SecondModule << Range << DiffType;
10216         };
10217 
10218         unsigned FirstNumBases = FirstDD->NumBases;
10219         unsigned FirstNumVBases = FirstDD->NumVBases;
10220         unsigned SecondNumBases = SecondDD->NumBases;
10221         unsigned SecondNumVBases = SecondDD->NumVBases;
10222 
10223         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10224           unsigned NumBases = DD->NumBases;
10225           if (NumBases == 0) return SourceRange();
10226           auto bases = DD->bases();
10227           return SourceRange(bases[0].getBeginLoc(),
10228                              bases[NumBases - 1].getEndLoc());
10229         };
10230 
10231         if (FirstNumBases != SecondNumBases) {
10232           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10233                        NumBases)
10234               << FirstNumBases;
10235           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10236                       NumBases)
10237               << SecondNumBases;
10238           Diagnosed = true;
10239           break;
10240         }
10241 
10242         if (FirstNumVBases != SecondNumVBases) {
10243           ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10244                        NumVBases)
10245               << FirstNumVBases;
10246           ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10247                       NumVBases)
10248               << SecondNumVBases;
10249           Diagnosed = true;
10250           break;
10251         }
10252 
10253         auto FirstBases = FirstDD->bases();
10254         auto SecondBases = SecondDD->bases();
10255         unsigned i = 0;
10256         for (i = 0; i < FirstNumBases; ++i) {
10257           auto FirstBase = FirstBases[i];
10258           auto SecondBase = SecondBases[i];
10259           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10260               ComputeQualTypeODRHash(SecondBase.getType())) {
10261             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10262                          BaseType)
10263                 << (i + 1) << FirstBase.getType();
10264             ODRDiagNote(SecondRecord->getLocation(),
10265                         SecondBase.getSourceRange(), BaseType)
10266                 << (i + 1) << SecondBase.getType();
10267             break;
10268           }
10269 
10270           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10271             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10272                          BaseVirtual)
10273                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10274             ODRDiagNote(SecondRecord->getLocation(),
10275                         SecondBase.getSourceRange(), BaseVirtual)
10276                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10277             break;
10278           }
10279 
10280           if (FirstBase.getAccessSpecifierAsWritten() !=
10281               SecondBase.getAccessSpecifierAsWritten()) {
10282             ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
10283                          BaseAccess)
10284                 << (i + 1) << FirstBase.getType()
10285                 << (int)FirstBase.getAccessSpecifierAsWritten();
10286             ODRDiagNote(SecondRecord->getLocation(),
10287                         SecondBase.getSourceRange(), BaseAccess)
10288                 << (i + 1) << SecondBase.getType()
10289                 << (int)SecondBase.getAccessSpecifierAsWritten();
10290             break;
10291           }
10292         }
10293 
10294         if (i != FirstNumBases) {
10295           Diagnosed = true;
10296           break;
10297         }
10298       }
10299 
10300       using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
10301 
10302       const ClassTemplateDecl *FirstTemplate =
10303           FirstRecord->getDescribedClassTemplate();
10304       const ClassTemplateDecl *SecondTemplate =
10305           SecondRecord->getDescribedClassTemplate();
10306 
10307       assert(!FirstTemplate == !SecondTemplate &&
10308              "Both pointers should be null or non-null");
10309 
10310       enum ODRTemplateDifference {
10311         ParamEmptyName,
10312         ParamName,
10313         ParamSingleDefaultArgument,
10314         ParamDifferentDefaultArgument,
10315       };
10316 
10317       if (FirstTemplate && SecondTemplate) {
10318         DeclHashes FirstTemplateHashes;
10319         DeclHashes SecondTemplateHashes;
10320 
10321         auto PopulateTemplateParameterHashs =
10322             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10323                                      const ClassTemplateDecl *TD) {
10324               for (auto *D : TD->getTemplateParameters()->asArray()) {
10325                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10326               }
10327             };
10328 
10329         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10330         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10331 
10332         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10333                "Number of template parameters should be equal.");
10334 
10335         auto FirstIt = FirstTemplateHashes.begin();
10336         auto FirstEnd = FirstTemplateHashes.end();
10337         auto SecondIt = SecondTemplateHashes.begin();
10338         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10339           if (FirstIt->second == SecondIt->second)
10340             continue;
10341 
10342           auto ODRDiagError = [FirstRecord, &FirstModule,
10343                                this](SourceLocation Loc, SourceRange Range,
10344                                      ODRTemplateDifference DiffType) {
10345             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10346                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10347                    << DiffType;
10348           };
10349           auto ODRDiagNote = [&SecondModule,
10350                               this](SourceLocation Loc, SourceRange Range,
10351                                     ODRTemplateDifference DiffType) {
10352             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10353                    << SecondModule << Range << DiffType;
10354           };
10355 
10356           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10357           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10358 
10359           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10360                  "Parameter Decl's should be the same kind.");
10361 
10362           DeclarationName FirstName = FirstDecl->getDeclName();
10363           DeclarationName SecondName = SecondDecl->getDeclName();
10364 
10365           if (FirstName != SecondName) {
10366             const bool FirstNameEmpty =
10367                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10368             const bool SecondNameEmpty =
10369                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10370             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10371                    "Both template parameters cannot be unnamed.");
10372             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10373                          FirstNameEmpty ? ParamEmptyName : ParamName)
10374                 << FirstName;
10375             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10376                         SecondNameEmpty ? ParamEmptyName : ParamName)
10377                 << SecondName;
10378             break;
10379           }
10380 
10381           switch (FirstDecl->getKind()) {
10382           default:
10383             llvm_unreachable("Invalid template parameter type.");
10384           case Decl::TemplateTypeParm: {
10385             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10386             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10387             const bool HasFirstDefaultArgument =
10388                 FirstParam->hasDefaultArgument() &&
10389                 !FirstParam->defaultArgumentWasInherited();
10390             const bool HasSecondDefaultArgument =
10391                 SecondParam->hasDefaultArgument() &&
10392                 !SecondParam->defaultArgumentWasInherited();
10393 
10394             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10395               ODRDiagError(FirstDecl->getLocation(),
10396                            FirstDecl->getSourceRange(),
10397                            ParamSingleDefaultArgument)
10398                   << HasFirstDefaultArgument;
10399               ODRDiagNote(SecondDecl->getLocation(),
10400                           SecondDecl->getSourceRange(),
10401                           ParamSingleDefaultArgument)
10402                   << HasSecondDefaultArgument;
10403               break;
10404             }
10405 
10406             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10407                    "Expecting default arguments.");
10408 
10409             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10410                          ParamDifferentDefaultArgument);
10411             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10412                         ParamDifferentDefaultArgument);
10413 
10414             break;
10415           }
10416           case Decl::NonTypeTemplateParm: {
10417             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10418             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10419             const bool HasFirstDefaultArgument =
10420                 FirstParam->hasDefaultArgument() &&
10421                 !FirstParam->defaultArgumentWasInherited();
10422             const bool HasSecondDefaultArgument =
10423                 SecondParam->hasDefaultArgument() &&
10424                 !SecondParam->defaultArgumentWasInherited();
10425 
10426             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10427               ODRDiagError(FirstDecl->getLocation(),
10428                            FirstDecl->getSourceRange(),
10429                            ParamSingleDefaultArgument)
10430                   << HasFirstDefaultArgument;
10431               ODRDiagNote(SecondDecl->getLocation(),
10432                           SecondDecl->getSourceRange(),
10433                           ParamSingleDefaultArgument)
10434                   << HasSecondDefaultArgument;
10435               break;
10436             }
10437 
10438             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10439                    "Expecting default arguments.");
10440 
10441             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10442                          ParamDifferentDefaultArgument);
10443             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10444                         ParamDifferentDefaultArgument);
10445 
10446             break;
10447           }
10448           case Decl::TemplateTemplateParm: {
10449             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10450             const auto *SecondParam =
10451                 cast<TemplateTemplateParmDecl>(SecondDecl);
10452             const bool HasFirstDefaultArgument =
10453                 FirstParam->hasDefaultArgument() &&
10454                 !FirstParam->defaultArgumentWasInherited();
10455             const bool HasSecondDefaultArgument =
10456                 SecondParam->hasDefaultArgument() &&
10457                 !SecondParam->defaultArgumentWasInherited();
10458 
10459             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10460               ODRDiagError(FirstDecl->getLocation(),
10461                            FirstDecl->getSourceRange(),
10462                            ParamSingleDefaultArgument)
10463                   << HasFirstDefaultArgument;
10464               ODRDiagNote(SecondDecl->getLocation(),
10465                           SecondDecl->getSourceRange(),
10466                           ParamSingleDefaultArgument)
10467                   << HasSecondDefaultArgument;
10468               break;
10469             }
10470 
10471             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10472                    "Expecting default arguments.");
10473 
10474             ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
10475                          ParamDifferentDefaultArgument);
10476             ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
10477                         ParamDifferentDefaultArgument);
10478 
10479             break;
10480           }
10481           }
10482 
10483           break;
10484         }
10485 
10486         if (FirstIt != FirstEnd) {
10487           Diagnosed = true;
10488           break;
10489         }
10490       }
10491 
10492       DeclHashes FirstHashes;
10493       DeclHashes SecondHashes;
10494 
10495       auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
10496                                 DeclHashes &Hashes, CXXRecordDecl *Record) {
10497         for (auto *D : Record->decls()) {
10498           // Due to decl merging, the first CXXRecordDecl is the parent of
10499           // Decls in both records.
10500           if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
10501             continue;
10502           Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10503         }
10504       };
10505       PopulateHashes(FirstHashes, FirstRecord);
10506       PopulateHashes(SecondHashes, SecondRecord);
10507 
10508       // Used with err_module_odr_violation_mismatch_decl and
10509       // note_module_odr_violation_mismatch_decl
10510       // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
10511       enum {
10512         EndOfClass,
10513         PublicSpecifer,
10514         PrivateSpecifer,
10515         ProtectedSpecifer,
10516         StaticAssert,
10517         Field,
10518         CXXMethod,
10519         TypeAlias,
10520         TypeDef,
10521         Var,
10522         Friend,
10523         FunctionTemplate,
10524         Other
10525       } FirstDiffType = Other,
10526         SecondDiffType = Other;
10527 
10528       auto DifferenceSelector = [](Decl *D) {
10529         assert(D && "valid Decl required");
10530         switch (D->getKind()) {
10531         default:
10532           return Other;
10533         case Decl::AccessSpec:
10534           switch (D->getAccess()) {
10535           case AS_public:
10536             return PublicSpecifer;
10537           case AS_private:
10538             return PrivateSpecifer;
10539           case AS_protected:
10540             return ProtectedSpecifer;
10541           case AS_none:
10542             break;
10543           }
10544           llvm_unreachable("Invalid access specifier");
10545         case Decl::StaticAssert:
10546           return StaticAssert;
10547         case Decl::Field:
10548           return Field;
10549         case Decl::CXXMethod:
10550         case Decl::CXXConstructor:
10551         case Decl::CXXDestructor:
10552           return CXXMethod;
10553         case Decl::TypeAlias:
10554           return TypeAlias;
10555         case Decl::Typedef:
10556           return TypeDef;
10557         case Decl::Var:
10558           return Var;
10559         case Decl::Friend:
10560           return Friend;
10561         case Decl::FunctionTemplate:
10562           return FunctionTemplate;
10563         }
10564       };
10565 
10566       Decl *FirstDecl = nullptr;
10567       Decl *SecondDecl = nullptr;
10568       auto FirstIt = FirstHashes.begin();
10569       auto SecondIt = SecondHashes.begin();
10570 
10571       // If there is a diagnoseable difference, FirstDiffType and
10572       // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
10573       // filled in if not EndOfClass.
10574       while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
10575         if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
10576             FirstIt->second == SecondIt->second) {
10577           ++FirstIt;
10578           ++SecondIt;
10579           continue;
10580         }
10581 
10582         FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
10583         SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
10584 
10585         FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
10586         SecondDiffType =
10587             SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
10588 
10589         break;
10590       }
10591 
10592       if (FirstDiffType == Other || SecondDiffType == Other) {
10593         // Reaching this point means an unexpected Decl was encountered
10594         // or no difference was detected.  This causes a generic error
10595         // message to be emitted.
10596         Diag(FirstRecord->getLocation(),
10597              diag::err_module_odr_violation_different_definitions)
10598             << FirstRecord << FirstModule.empty() << FirstModule;
10599 
10600         if (FirstDecl) {
10601           Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
10602               << FirstRecord << FirstDecl->getSourceRange();
10603         }
10604 
10605         Diag(SecondRecord->getLocation(),
10606              diag::note_module_odr_violation_different_definitions)
10607             << SecondModule;
10608 
10609         if (SecondDecl) {
10610           Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
10611               << SecondDecl->getSourceRange();
10612         }
10613 
10614         Diagnosed = true;
10615         break;
10616       }
10617 
10618       if (FirstDiffType != SecondDiffType) {
10619         SourceLocation FirstLoc;
10620         SourceRange FirstRange;
10621         if (FirstDiffType == EndOfClass) {
10622           FirstLoc = FirstRecord->getBraceRange().getEnd();
10623         } else {
10624           FirstLoc = FirstIt->first->getLocation();
10625           FirstRange = FirstIt->first->getSourceRange();
10626         }
10627         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10628             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10629             << FirstDiffType;
10630 
10631         SourceLocation SecondLoc;
10632         SourceRange SecondRange;
10633         if (SecondDiffType == EndOfClass) {
10634           SecondLoc = SecondRecord->getBraceRange().getEnd();
10635         } else {
10636           SecondLoc = SecondDecl->getLocation();
10637           SecondRange = SecondDecl->getSourceRange();
10638         }
10639         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10640             << SecondModule << SecondRange << SecondDiffType;
10641         Diagnosed = true;
10642         break;
10643       }
10644 
10645       assert(FirstDiffType == SecondDiffType);
10646 
10647       // Used with err_module_odr_violation_mismatch_decl_diff and
10648       // note_module_odr_violation_mismatch_decl_diff
10649       enum ODRDeclDifference {
10650         StaticAssertCondition,
10651         StaticAssertMessage,
10652         StaticAssertOnlyMessage,
10653         FieldName,
10654         FieldTypeName,
10655         FieldSingleBitField,
10656         FieldDifferentWidthBitField,
10657         FieldSingleMutable,
10658         FieldSingleInitializer,
10659         FieldDifferentInitializers,
10660         MethodName,
10661         MethodDeleted,
10662         MethodDefaulted,
10663         MethodVirtual,
10664         MethodStatic,
10665         MethodVolatile,
10666         MethodConst,
10667         MethodInline,
10668         MethodNumberParameters,
10669         MethodParameterType,
10670         MethodParameterName,
10671         MethodParameterSingleDefaultArgument,
10672         MethodParameterDifferentDefaultArgument,
10673         MethodNoTemplateArguments,
10674         MethodDifferentNumberTemplateArguments,
10675         MethodDifferentTemplateArgument,
10676         MethodSingleBody,
10677         MethodDifferentBody,
10678         TypedefName,
10679         TypedefType,
10680         VarName,
10681         VarType,
10682         VarSingleInitializer,
10683         VarDifferentInitializer,
10684         VarConstexpr,
10685         FriendTypeFunction,
10686         FriendType,
10687         FriendFunction,
10688         FunctionTemplateDifferentNumberParameters,
10689         FunctionTemplateParameterDifferentKind,
10690         FunctionTemplateParameterName,
10691         FunctionTemplateParameterSingleDefaultArgument,
10692         FunctionTemplateParameterDifferentDefaultArgument,
10693         FunctionTemplateParameterDifferentType,
10694         FunctionTemplatePackParameter,
10695       };
10696 
10697       // These lambdas have the common portions of the ODR diagnostics.  This
10698       // has the same return as Diag(), so addition parameters can be passed
10699       // in with operator<<
10700       auto ODRDiagError = [FirstRecord, &FirstModule, this](
10701           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10702         return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10703                << FirstRecord << FirstModule.empty() << FirstModule << Range
10704                << DiffType;
10705       };
10706       auto ODRDiagNote = [&SecondModule, this](
10707           SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10708         return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10709                << SecondModule << Range << DiffType;
10710       };
10711 
10712       switch (FirstDiffType) {
10713       case Other:
10714       case EndOfClass:
10715       case PublicSpecifer:
10716       case PrivateSpecifer:
10717       case ProtectedSpecifer:
10718         llvm_unreachable("Invalid diff type");
10719 
10720       case StaticAssert: {
10721         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10722         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10723 
10724         Expr *FirstExpr = FirstSA->getAssertExpr();
10725         Expr *SecondExpr = SecondSA->getAssertExpr();
10726         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10727         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10728         if (FirstODRHash != SecondODRHash) {
10729           ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
10730                        StaticAssertCondition);
10731           ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
10732                       StaticAssertCondition);
10733           Diagnosed = true;
10734           break;
10735         }
10736 
10737         StringLiteral *FirstStr = FirstSA->getMessage();
10738         StringLiteral *SecondStr = SecondSA->getMessage();
10739         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10740         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10741           SourceLocation FirstLoc, SecondLoc;
10742           SourceRange FirstRange, SecondRange;
10743           if (FirstStr) {
10744             FirstLoc = FirstStr->getBeginLoc();
10745             FirstRange = FirstStr->getSourceRange();
10746           } else {
10747             FirstLoc = FirstSA->getBeginLoc();
10748             FirstRange = FirstSA->getSourceRange();
10749           }
10750           if (SecondStr) {
10751             SecondLoc = SecondStr->getBeginLoc();
10752             SecondRange = SecondStr->getSourceRange();
10753           } else {
10754             SecondLoc = SecondSA->getBeginLoc();
10755             SecondRange = SecondSA->getSourceRange();
10756           }
10757           ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10758               << (FirstStr == nullptr);
10759           ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10760               << (SecondStr == nullptr);
10761           Diagnosed = true;
10762           break;
10763         }
10764 
10765         if (FirstStr && SecondStr &&
10766             FirstStr->getString() != SecondStr->getString()) {
10767           ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
10768                        StaticAssertMessage);
10769           ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
10770                       StaticAssertMessage);
10771           Diagnosed = true;
10772           break;
10773         }
10774         break;
10775       }
10776       case Field: {
10777         FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10778         FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
10779         IdentifierInfo *FirstII = FirstField->getIdentifier();
10780         IdentifierInfo *SecondII = SecondField->getIdentifier();
10781         if (FirstII->getName() != SecondII->getName()) {
10782           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10783                        FieldName)
10784               << FirstII;
10785           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10786                       FieldName)
10787               << SecondII;
10788 
10789           Diagnosed = true;
10790           break;
10791         }
10792 
10793         assert(getContext().hasSameType(FirstField->getType(),
10794                                         SecondField->getType()));
10795 
10796         QualType FirstType = FirstField->getType();
10797         QualType SecondType = SecondField->getType();
10798         if (ComputeQualTypeODRHash(FirstType) !=
10799             ComputeQualTypeODRHash(SecondType)) {
10800           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10801                        FieldTypeName)
10802               << FirstII << FirstType;
10803           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10804                       FieldTypeName)
10805               << SecondII << SecondType;
10806 
10807           Diagnosed = true;
10808           break;
10809         }
10810 
10811         const bool IsFirstBitField = FirstField->isBitField();
10812         const bool IsSecondBitField = SecondField->isBitField();
10813         if (IsFirstBitField != IsSecondBitField) {
10814           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10815                        FieldSingleBitField)
10816               << FirstII << IsFirstBitField;
10817           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10818                       FieldSingleBitField)
10819               << SecondII << IsSecondBitField;
10820           Diagnosed = true;
10821           break;
10822         }
10823 
10824         if (IsFirstBitField && IsSecondBitField) {
10825           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10826                        FieldDifferentWidthBitField)
10827               << FirstII << FirstField->getBitWidth()->getSourceRange();
10828           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10829                       FieldDifferentWidthBitField)
10830               << SecondII << SecondField->getBitWidth()->getSourceRange();
10831           Diagnosed = true;
10832           break;
10833         }
10834 
10835         const bool IsFirstMutable = FirstField->isMutable();
10836         const bool IsSecondMutable = SecondField->isMutable();
10837         if (IsFirstMutable != IsSecondMutable) {
10838           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10839                        FieldSingleMutable)
10840               << FirstII << IsFirstMutable;
10841           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10842                       FieldSingleMutable)
10843               << SecondII << IsSecondMutable;
10844           Diagnosed = true;
10845           break;
10846         }
10847 
10848         const Expr *FirstInitializer = FirstField->getInClassInitializer();
10849         const Expr *SecondInitializer = SecondField->getInClassInitializer();
10850         if ((!FirstInitializer && SecondInitializer) ||
10851             (FirstInitializer && !SecondInitializer)) {
10852           ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10853                        FieldSingleInitializer)
10854               << FirstII << (FirstInitializer != nullptr);
10855           ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10856                       FieldSingleInitializer)
10857               << SecondII << (SecondInitializer != nullptr);
10858           Diagnosed = true;
10859           break;
10860         }
10861 
10862         if (FirstInitializer && SecondInitializer) {
10863           unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10864           unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10865           if (FirstInitHash != SecondInitHash) {
10866             ODRDiagError(FirstField->getLocation(),
10867                          FirstField->getSourceRange(),
10868                          FieldDifferentInitializers)
10869                 << FirstII << FirstInitializer->getSourceRange();
10870             ODRDiagNote(SecondField->getLocation(),
10871                         SecondField->getSourceRange(),
10872                         FieldDifferentInitializers)
10873                 << SecondII << SecondInitializer->getSourceRange();
10874             Diagnosed = true;
10875             break;
10876           }
10877         }
10878 
10879         break;
10880       }
10881       case CXXMethod: {
10882         enum {
10883           DiagMethod,
10884           DiagConstructor,
10885           DiagDestructor,
10886         } FirstMethodType,
10887             SecondMethodType;
10888         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10889           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10890           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10891           return DiagMethod;
10892         };
10893         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10894         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10895         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10896         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10897         auto FirstName = FirstMethod->getDeclName();
10898         auto SecondName = SecondMethod->getDeclName();
10899         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10900           ODRDiagError(FirstMethod->getLocation(),
10901                        FirstMethod->getSourceRange(), MethodName)
10902               << FirstMethodType << FirstName;
10903           ODRDiagNote(SecondMethod->getLocation(),
10904                       SecondMethod->getSourceRange(), MethodName)
10905               << SecondMethodType << SecondName;
10906 
10907           Diagnosed = true;
10908           break;
10909         }
10910 
10911         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10912         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10913         if (FirstDeleted != SecondDeleted) {
10914           ODRDiagError(FirstMethod->getLocation(),
10915                        FirstMethod->getSourceRange(), MethodDeleted)
10916               << FirstMethodType << FirstName << FirstDeleted;
10917 
10918           ODRDiagNote(SecondMethod->getLocation(),
10919                       SecondMethod->getSourceRange(), MethodDeleted)
10920               << SecondMethodType << SecondName << SecondDeleted;
10921           Diagnosed = true;
10922           break;
10923         }
10924 
10925         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10926         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10927         if (FirstDefaulted != SecondDefaulted) {
10928           ODRDiagError(FirstMethod->getLocation(),
10929                        FirstMethod->getSourceRange(), MethodDefaulted)
10930               << FirstMethodType << FirstName << FirstDefaulted;
10931 
10932           ODRDiagNote(SecondMethod->getLocation(),
10933                       SecondMethod->getSourceRange(), MethodDefaulted)
10934               << SecondMethodType << SecondName << SecondDefaulted;
10935           Diagnosed = true;
10936           break;
10937         }
10938 
10939         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10940         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10941         const bool FirstPure = FirstMethod->isPure();
10942         const bool SecondPure = SecondMethod->isPure();
10943         if ((FirstVirtual || SecondVirtual) &&
10944             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10945           ODRDiagError(FirstMethod->getLocation(),
10946                        FirstMethod->getSourceRange(), MethodVirtual)
10947               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10948           ODRDiagNote(SecondMethod->getLocation(),
10949                       SecondMethod->getSourceRange(), MethodVirtual)
10950               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10951           Diagnosed = true;
10952           break;
10953         }
10954 
10955         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10956         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10957         // class needs to be checked instead.
10958         const auto FirstStorage = FirstMethod->getStorageClass();
10959         const auto SecondStorage = SecondMethod->getStorageClass();
10960         const bool FirstStatic = FirstStorage == SC_Static;
10961         const bool SecondStatic = SecondStorage == SC_Static;
10962         if (FirstStatic != SecondStatic) {
10963           ODRDiagError(FirstMethod->getLocation(),
10964                        FirstMethod->getSourceRange(), MethodStatic)
10965               << FirstMethodType << FirstName << FirstStatic;
10966           ODRDiagNote(SecondMethod->getLocation(),
10967                       SecondMethod->getSourceRange(), MethodStatic)
10968               << SecondMethodType << SecondName << SecondStatic;
10969           Diagnosed = true;
10970           break;
10971         }
10972 
10973         const bool FirstVolatile = FirstMethod->isVolatile();
10974         const bool SecondVolatile = SecondMethod->isVolatile();
10975         if (FirstVolatile != SecondVolatile) {
10976           ODRDiagError(FirstMethod->getLocation(),
10977                        FirstMethod->getSourceRange(), MethodVolatile)
10978               << FirstMethodType << FirstName << FirstVolatile;
10979           ODRDiagNote(SecondMethod->getLocation(),
10980                       SecondMethod->getSourceRange(), MethodVolatile)
10981               << SecondMethodType << SecondName << SecondVolatile;
10982           Diagnosed = true;
10983           break;
10984         }
10985 
10986         const bool FirstConst = FirstMethod->isConst();
10987         const bool SecondConst = SecondMethod->isConst();
10988         if (FirstConst != SecondConst) {
10989           ODRDiagError(FirstMethod->getLocation(),
10990                        FirstMethod->getSourceRange(), MethodConst)
10991               << FirstMethodType << FirstName << FirstConst;
10992           ODRDiagNote(SecondMethod->getLocation(),
10993                       SecondMethod->getSourceRange(), MethodConst)
10994               << SecondMethodType << SecondName << SecondConst;
10995           Diagnosed = true;
10996           break;
10997         }
10998 
10999         const bool FirstInline = FirstMethod->isInlineSpecified();
11000         const bool SecondInline = SecondMethod->isInlineSpecified();
11001         if (FirstInline != SecondInline) {
11002           ODRDiagError(FirstMethod->getLocation(),
11003                        FirstMethod->getSourceRange(), MethodInline)
11004               << FirstMethodType << FirstName << FirstInline;
11005           ODRDiagNote(SecondMethod->getLocation(),
11006                       SecondMethod->getSourceRange(), MethodInline)
11007               << SecondMethodType << SecondName << SecondInline;
11008           Diagnosed = true;
11009           break;
11010         }
11011 
11012         const unsigned FirstNumParameters = FirstMethod->param_size();
11013         const unsigned SecondNumParameters = SecondMethod->param_size();
11014         if (FirstNumParameters != SecondNumParameters) {
11015           ODRDiagError(FirstMethod->getLocation(),
11016                        FirstMethod->getSourceRange(), MethodNumberParameters)
11017               << FirstMethodType << FirstName << FirstNumParameters;
11018           ODRDiagNote(SecondMethod->getLocation(),
11019                       SecondMethod->getSourceRange(), MethodNumberParameters)
11020               << SecondMethodType << SecondName << SecondNumParameters;
11021           Diagnosed = true;
11022           break;
11023         }
11024 
11025         // Need this status boolean to know when break out of the switch.
11026         bool ParameterMismatch = false;
11027         for (unsigned I = 0; I < FirstNumParameters; ++I) {
11028           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
11029           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
11030 
11031           QualType FirstParamType = FirstParam->getType();
11032           QualType SecondParamType = SecondParam->getType();
11033           if (FirstParamType != SecondParamType &&
11034               ComputeQualTypeODRHash(FirstParamType) !=
11035                   ComputeQualTypeODRHash(SecondParamType)) {
11036             if (const DecayedType *ParamDecayedType =
11037                     FirstParamType->getAs<DecayedType>()) {
11038               ODRDiagError(FirstMethod->getLocation(),
11039                            FirstMethod->getSourceRange(), MethodParameterType)
11040                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11041                   << true << ParamDecayedType->getOriginalType();
11042             } else {
11043               ODRDiagError(FirstMethod->getLocation(),
11044                            FirstMethod->getSourceRange(), MethodParameterType)
11045                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
11046                   << false;
11047             }
11048 
11049             if (const DecayedType *ParamDecayedType =
11050                     SecondParamType->getAs<DecayedType>()) {
11051               ODRDiagNote(SecondMethod->getLocation(),
11052                           SecondMethod->getSourceRange(), MethodParameterType)
11053                   << SecondMethodType << SecondName << (I + 1)
11054                   << SecondParamType << true
11055                   << ParamDecayedType->getOriginalType();
11056             } else {
11057               ODRDiagNote(SecondMethod->getLocation(),
11058                           SecondMethod->getSourceRange(), MethodParameterType)
11059                   << SecondMethodType << SecondName << (I + 1)
11060                   << SecondParamType << false;
11061             }
11062             ParameterMismatch = true;
11063             break;
11064           }
11065 
11066           DeclarationName FirstParamName = FirstParam->getDeclName();
11067           DeclarationName SecondParamName = SecondParam->getDeclName();
11068           if (FirstParamName != SecondParamName) {
11069             ODRDiagError(FirstMethod->getLocation(),
11070                          FirstMethod->getSourceRange(), MethodParameterName)
11071                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
11072             ODRDiagNote(SecondMethod->getLocation(),
11073                         SecondMethod->getSourceRange(), MethodParameterName)
11074                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
11075             ParameterMismatch = true;
11076             break;
11077           }
11078 
11079           const Expr *FirstInit = FirstParam->getInit();
11080           const Expr *SecondInit = SecondParam->getInit();
11081           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11082             ODRDiagError(FirstMethod->getLocation(),
11083                          FirstMethod->getSourceRange(),
11084                          MethodParameterSingleDefaultArgument)
11085                 << FirstMethodType << FirstName << (I + 1)
11086                 << (FirstInit == nullptr)
11087                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11088             ODRDiagNote(SecondMethod->getLocation(),
11089                         SecondMethod->getSourceRange(),
11090                         MethodParameterSingleDefaultArgument)
11091                 << SecondMethodType << SecondName << (I + 1)
11092                 << (SecondInit == nullptr)
11093                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11094             ParameterMismatch = true;
11095             break;
11096           }
11097 
11098           if (FirstInit && SecondInit &&
11099               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11100             ODRDiagError(FirstMethod->getLocation(),
11101                          FirstMethod->getSourceRange(),
11102                          MethodParameterDifferentDefaultArgument)
11103                 << FirstMethodType << FirstName << (I + 1)
11104                 << FirstInit->getSourceRange();
11105             ODRDiagNote(SecondMethod->getLocation(),
11106                         SecondMethod->getSourceRange(),
11107                         MethodParameterDifferentDefaultArgument)
11108                 << SecondMethodType << SecondName << (I + 1)
11109                 << SecondInit->getSourceRange();
11110             ParameterMismatch = true;
11111             break;
11112 
11113           }
11114         }
11115 
11116         if (ParameterMismatch) {
11117           Diagnosed = true;
11118           break;
11119         }
11120 
11121         const auto *FirstTemplateArgs =
11122             FirstMethod->getTemplateSpecializationArgs();
11123         const auto *SecondTemplateArgs =
11124             SecondMethod->getTemplateSpecializationArgs();
11125 
11126         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
11127             (!FirstTemplateArgs && SecondTemplateArgs)) {
11128           ODRDiagError(FirstMethod->getLocation(),
11129                        FirstMethod->getSourceRange(), MethodNoTemplateArguments)
11130               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
11131           ODRDiagNote(SecondMethod->getLocation(),
11132                       SecondMethod->getSourceRange(), MethodNoTemplateArguments)
11133               << SecondMethodType << SecondName
11134               << (SecondTemplateArgs != nullptr);
11135 
11136           Diagnosed = true;
11137           break;
11138         }
11139 
11140         if (FirstTemplateArgs && SecondTemplateArgs) {
11141           // Remove pack expansions from argument list.
11142           auto ExpandTemplateArgumentList =
11143               [](const TemplateArgumentList *TAL) {
11144                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
11145                 for (const TemplateArgument &TA : TAL->asArray()) {
11146                   if (TA.getKind() != TemplateArgument::Pack) {
11147                     ExpandedList.push_back(&TA);
11148                     continue;
11149                   }
11150                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
11151                     ExpandedList.push_back(&PackTA);
11152                   }
11153                 }
11154                 return ExpandedList;
11155               };
11156           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
11157               ExpandTemplateArgumentList(FirstTemplateArgs);
11158           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
11159               ExpandTemplateArgumentList(SecondTemplateArgs);
11160 
11161           if (FirstExpandedList.size() != SecondExpandedList.size()) {
11162             ODRDiagError(FirstMethod->getLocation(),
11163                          FirstMethod->getSourceRange(),
11164                          MethodDifferentNumberTemplateArguments)
11165                 << FirstMethodType << FirstName
11166                 << (unsigned)FirstExpandedList.size();
11167             ODRDiagNote(SecondMethod->getLocation(),
11168                         SecondMethod->getSourceRange(),
11169                         MethodDifferentNumberTemplateArguments)
11170                 << SecondMethodType << SecondName
11171                 << (unsigned)SecondExpandedList.size();
11172 
11173             Diagnosed = true;
11174             break;
11175           }
11176 
11177           bool TemplateArgumentMismatch = false;
11178           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
11179             const TemplateArgument &FirstTA = *FirstExpandedList[i],
11180                                    &SecondTA = *SecondExpandedList[i];
11181             if (ComputeTemplateArgumentODRHash(FirstTA) ==
11182                 ComputeTemplateArgumentODRHash(SecondTA)) {
11183               continue;
11184             }
11185 
11186             ODRDiagError(FirstMethod->getLocation(),
11187                          FirstMethod->getSourceRange(),
11188                          MethodDifferentTemplateArgument)
11189                 << FirstMethodType << FirstName << FirstTA << i + 1;
11190             ODRDiagNote(SecondMethod->getLocation(),
11191                         SecondMethod->getSourceRange(),
11192                         MethodDifferentTemplateArgument)
11193                 << SecondMethodType << SecondName << SecondTA << i + 1;
11194 
11195             TemplateArgumentMismatch = true;
11196             break;
11197           }
11198 
11199           if (TemplateArgumentMismatch) {
11200             Diagnosed = true;
11201             break;
11202           }
11203         }
11204 
11205         // Compute the hash of the method as if it has no body.
11206         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
11207           Hash.clear();
11208           Hash.AddFunctionDecl(D, true /*SkipBody*/);
11209           return Hash.CalculateHash();
11210         };
11211 
11212         // Compare the hash generated to the hash stored.  A difference means
11213         // that a body was present in the original source.  Due to merging,
11214         // the stardard way of detecting a body will not work.
11215         const bool HasFirstBody =
11216             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
11217         const bool HasSecondBody =
11218             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
11219 
11220         if (HasFirstBody != HasSecondBody) {
11221           ODRDiagError(FirstMethod->getLocation(),
11222                        FirstMethod->getSourceRange(), MethodSingleBody)
11223               << FirstMethodType << FirstName << HasFirstBody;
11224           ODRDiagNote(SecondMethod->getLocation(),
11225                       SecondMethod->getSourceRange(), MethodSingleBody)
11226               << SecondMethodType << SecondName << HasSecondBody;
11227           Diagnosed = true;
11228           break;
11229         }
11230 
11231         if (HasFirstBody && HasSecondBody) {
11232           ODRDiagError(FirstMethod->getLocation(),
11233                        FirstMethod->getSourceRange(), MethodDifferentBody)
11234               << FirstMethodType << FirstName;
11235           ODRDiagNote(SecondMethod->getLocation(),
11236                       SecondMethod->getSourceRange(), MethodDifferentBody)
11237               << SecondMethodType << SecondName;
11238           Diagnosed = true;
11239           break;
11240         }
11241 
11242         break;
11243       }
11244       case TypeAlias:
11245       case TypeDef: {
11246         TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
11247         TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
11248         auto FirstName = FirstTD->getDeclName();
11249         auto SecondName = SecondTD->getDeclName();
11250         if (FirstName != SecondName) {
11251           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11252                        TypedefName)
11253               << (FirstDiffType == TypeAlias) << FirstName;
11254           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11255                       TypedefName)
11256               << (FirstDiffType == TypeAlias) << SecondName;
11257           Diagnosed = true;
11258           break;
11259         }
11260 
11261         QualType FirstType = FirstTD->getUnderlyingType();
11262         QualType SecondType = SecondTD->getUnderlyingType();
11263         if (ComputeQualTypeODRHash(FirstType) !=
11264             ComputeQualTypeODRHash(SecondType)) {
11265           ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
11266                        TypedefType)
11267               << (FirstDiffType == TypeAlias) << FirstName << FirstType;
11268           ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
11269                       TypedefType)
11270               << (FirstDiffType == TypeAlias) << SecondName << SecondType;
11271           Diagnosed = true;
11272           break;
11273         }
11274         break;
11275       }
11276       case Var: {
11277         VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
11278         VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
11279         auto FirstName = FirstVD->getDeclName();
11280         auto SecondName = SecondVD->getDeclName();
11281         if (FirstName != SecondName) {
11282           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11283                        VarName)
11284               << FirstName;
11285           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11286                       VarName)
11287               << SecondName;
11288           Diagnosed = true;
11289           break;
11290         }
11291 
11292         QualType FirstType = FirstVD->getType();
11293         QualType SecondType = SecondVD->getType();
11294         if (ComputeQualTypeODRHash(FirstType) !=
11295                         ComputeQualTypeODRHash(SecondType)) {
11296           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11297                        VarType)
11298               << FirstName << FirstType;
11299           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11300                       VarType)
11301               << SecondName << SecondType;
11302           Diagnosed = true;
11303           break;
11304         }
11305 
11306         const Expr *FirstInit = FirstVD->getInit();
11307         const Expr *SecondInit = SecondVD->getInit();
11308         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11309           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11310                        VarSingleInitializer)
11311               << FirstName << (FirstInit == nullptr)
11312               << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
11313           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11314                       VarSingleInitializer)
11315               << SecondName << (SecondInit == nullptr)
11316               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11317           Diagnosed = true;
11318           break;
11319         }
11320 
11321         if (FirstInit && SecondInit &&
11322             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11323           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11324                        VarDifferentInitializer)
11325               << FirstName << FirstInit->getSourceRange();
11326           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11327                       VarDifferentInitializer)
11328               << SecondName << SecondInit->getSourceRange();
11329           Diagnosed = true;
11330           break;
11331         }
11332 
11333         const bool FirstIsConstexpr = FirstVD->isConstexpr();
11334         const bool SecondIsConstexpr = SecondVD->isConstexpr();
11335         if (FirstIsConstexpr != SecondIsConstexpr) {
11336           ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
11337                        VarConstexpr)
11338               << FirstName << FirstIsConstexpr;
11339           ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
11340                       VarConstexpr)
11341               << SecondName << SecondIsConstexpr;
11342           Diagnosed = true;
11343           break;
11344         }
11345         break;
11346       }
11347       case Friend: {
11348         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
11349         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
11350 
11351         NamedDecl *FirstND = FirstFriend->getFriendDecl();
11352         NamedDecl *SecondND = SecondFriend->getFriendDecl();
11353 
11354         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
11355         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
11356 
11357         if (FirstND && SecondND) {
11358           ODRDiagError(FirstFriend->getFriendLoc(),
11359                        FirstFriend->getSourceRange(), FriendFunction)
11360               << FirstND;
11361           ODRDiagNote(SecondFriend->getFriendLoc(),
11362                       SecondFriend->getSourceRange(), FriendFunction)
11363               << SecondND;
11364 
11365           Diagnosed = true;
11366           break;
11367         }
11368 
11369         if (FirstTSI && SecondTSI) {
11370           QualType FirstFriendType = FirstTSI->getType();
11371           QualType SecondFriendType = SecondTSI->getType();
11372           assert(ComputeQualTypeODRHash(FirstFriendType) !=
11373                  ComputeQualTypeODRHash(SecondFriendType));
11374           ODRDiagError(FirstFriend->getFriendLoc(),
11375                        FirstFriend->getSourceRange(), FriendType)
11376               << FirstFriendType;
11377           ODRDiagNote(SecondFriend->getFriendLoc(),
11378                       SecondFriend->getSourceRange(), FriendType)
11379               << SecondFriendType;
11380           Diagnosed = true;
11381           break;
11382         }
11383 
11384         ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
11385                      FriendTypeFunction)
11386             << (FirstTSI == nullptr);
11387         ODRDiagNote(SecondFriend->getFriendLoc(),
11388                     SecondFriend->getSourceRange(), FriendTypeFunction)
11389             << (SecondTSI == nullptr);
11390 
11391         Diagnosed = true;
11392         break;
11393       }
11394       case FunctionTemplate: {
11395         FunctionTemplateDecl *FirstTemplate =
11396             cast<FunctionTemplateDecl>(FirstDecl);
11397         FunctionTemplateDecl *SecondTemplate =
11398             cast<FunctionTemplateDecl>(SecondDecl);
11399 
11400         TemplateParameterList *FirstTPL =
11401             FirstTemplate->getTemplateParameters();
11402         TemplateParameterList *SecondTPL =
11403             SecondTemplate->getTemplateParameters();
11404 
11405         if (FirstTPL->size() != SecondTPL->size()) {
11406           ODRDiagError(FirstTemplate->getLocation(),
11407                        FirstTemplate->getSourceRange(),
11408                        FunctionTemplateDifferentNumberParameters)
11409               << FirstTemplate << FirstTPL->size();
11410           ODRDiagNote(SecondTemplate->getLocation(),
11411                       SecondTemplate->getSourceRange(),
11412                       FunctionTemplateDifferentNumberParameters)
11413               << SecondTemplate  << SecondTPL->size();
11414 
11415           Diagnosed = true;
11416           break;
11417         }
11418 
11419         bool ParameterMismatch = false;
11420         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
11421           NamedDecl *FirstParam = FirstTPL->getParam(i);
11422           NamedDecl *SecondParam = SecondTPL->getParam(i);
11423 
11424           if (FirstParam->getKind() != SecondParam->getKind()) {
11425             enum {
11426               TemplateTypeParameter,
11427               NonTypeTemplateParameter,
11428               TemplateTemplateParameter,
11429             };
11430             auto GetParamType = [](NamedDecl *D) {
11431               switch (D->getKind()) {
11432                 default:
11433                   llvm_unreachable("Unexpected template parameter type");
11434                 case Decl::TemplateTypeParm:
11435                   return TemplateTypeParameter;
11436                 case Decl::NonTypeTemplateParm:
11437                   return NonTypeTemplateParameter;
11438                 case Decl::TemplateTemplateParm:
11439                   return TemplateTemplateParameter;
11440               }
11441             };
11442 
11443             ODRDiagError(FirstTemplate->getLocation(),
11444                          FirstTemplate->getSourceRange(),
11445                          FunctionTemplateParameterDifferentKind)
11446                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
11447             ODRDiagNote(SecondTemplate->getLocation(),
11448                         SecondTemplate->getSourceRange(),
11449                         FunctionTemplateParameterDifferentKind)
11450                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
11451 
11452             ParameterMismatch = true;
11453             break;
11454           }
11455 
11456           if (FirstParam->getName() != SecondParam->getName()) {
11457             ODRDiagError(FirstTemplate->getLocation(),
11458                          FirstTemplate->getSourceRange(),
11459                          FunctionTemplateParameterName)
11460                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
11461                 << FirstParam;
11462             ODRDiagNote(SecondTemplate->getLocation(),
11463                         SecondTemplate->getSourceRange(),
11464                         FunctionTemplateParameterName)
11465                 << SecondTemplate << (i + 1)
11466                 << (bool)SecondParam->getIdentifier() << SecondParam;
11467             ParameterMismatch = true;
11468             break;
11469           }
11470 
11471           if (isa<TemplateTypeParmDecl>(FirstParam) &&
11472               isa<TemplateTypeParmDecl>(SecondParam)) {
11473             TemplateTypeParmDecl *FirstTTPD =
11474                 cast<TemplateTypeParmDecl>(FirstParam);
11475             TemplateTypeParmDecl *SecondTTPD =
11476                 cast<TemplateTypeParmDecl>(SecondParam);
11477             bool HasFirstDefaultArgument =
11478                 FirstTTPD->hasDefaultArgument() &&
11479                 !FirstTTPD->defaultArgumentWasInherited();
11480             bool HasSecondDefaultArgument =
11481                 SecondTTPD->hasDefaultArgument() &&
11482                 !SecondTTPD->defaultArgumentWasInherited();
11483             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11484               ODRDiagError(FirstTemplate->getLocation(),
11485                            FirstTemplate->getSourceRange(),
11486                            FunctionTemplateParameterSingleDefaultArgument)
11487                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11488               ODRDiagNote(SecondTemplate->getLocation(),
11489                           SecondTemplate->getSourceRange(),
11490                           FunctionTemplateParameterSingleDefaultArgument)
11491                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11492               ParameterMismatch = true;
11493               break;
11494             }
11495 
11496             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11497               QualType FirstType = FirstTTPD->getDefaultArgument();
11498               QualType SecondType = SecondTTPD->getDefaultArgument();
11499               if (ComputeQualTypeODRHash(FirstType) !=
11500                   ComputeQualTypeODRHash(SecondType)) {
11501                 ODRDiagError(FirstTemplate->getLocation(),
11502                              FirstTemplate->getSourceRange(),
11503                              FunctionTemplateParameterDifferentDefaultArgument)
11504                     << FirstTemplate << (i + 1) << FirstType;
11505                 ODRDiagNote(SecondTemplate->getLocation(),
11506                             SecondTemplate->getSourceRange(),
11507                             FunctionTemplateParameterDifferentDefaultArgument)
11508                     << SecondTemplate << (i + 1) << SecondType;
11509                 ParameterMismatch = true;
11510                 break;
11511               }
11512             }
11513 
11514             if (FirstTTPD->isParameterPack() !=
11515                 SecondTTPD->isParameterPack()) {
11516               ODRDiagError(FirstTemplate->getLocation(),
11517                            FirstTemplate->getSourceRange(),
11518                            FunctionTemplatePackParameter)
11519                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11520               ODRDiagNote(SecondTemplate->getLocation(),
11521                           SecondTemplate->getSourceRange(),
11522                           FunctionTemplatePackParameter)
11523                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11524               ParameterMismatch = true;
11525               break;
11526             }
11527           }
11528 
11529           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11530               isa<TemplateTemplateParmDecl>(SecondParam)) {
11531             TemplateTemplateParmDecl *FirstTTPD =
11532                 cast<TemplateTemplateParmDecl>(FirstParam);
11533             TemplateTemplateParmDecl *SecondTTPD =
11534                 cast<TemplateTemplateParmDecl>(SecondParam);
11535 
11536             TemplateParameterList *FirstTPL =
11537                 FirstTTPD->getTemplateParameters();
11538             TemplateParameterList *SecondTPL =
11539                 SecondTTPD->getTemplateParameters();
11540 
11541             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11542                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11543               ODRDiagError(FirstTemplate->getLocation(),
11544                            FirstTemplate->getSourceRange(),
11545                            FunctionTemplateParameterDifferentType)
11546                   << FirstTemplate << (i + 1);
11547               ODRDiagNote(SecondTemplate->getLocation(),
11548                           SecondTemplate->getSourceRange(),
11549                           FunctionTemplateParameterDifferentType)
11550                   << SecondTemplate << (i + 1);
11551               ParameterMismatch = true;
11552               break;
11553             }
11554 
11555             bool HasFirstDefaultArgument =
11556                 FirstTTPD->hasDefaultArgument() &&
11557                 !FirstTTPD->defaultArgumentWasInherited();
11558             bool HasSecondDefaultArgument =
11559                 SecondTTPD->hasDefaultArgument() &&
11560                 !SecondTTPD->defaultArgumentWasInherited();
11561             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11562               ODRDiagError(FirstTemplate->getLocation(),
11563                            FirstTemplate->getSourceRange(),
11564                            FunctionTemplateParameterSingleDefaultArgument)
11565                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11566               ODRDiagNote(SecondTemplate->getLocation(),
11567                           SecondTemplate->getSourceRange(),
11568                           FunctionTemplateParameterSingleDefaultArgument)
11569                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11570               ParameterMismatch = true;
11571               break;
11572             }
11573 
11574             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11575               TemplateArgument FirstTA =
11576                   FirstTTPD->getDefaultArgument().getArgument();
11577               TemplateArgument SecondTA =
11578                   SecondTTPD->getDefaultArgument().getArgument();
11579               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11580                   ComputeTemplateArgumentODRHash(SecondTA)) {
11581                 ODRDiagError(FirstTemplate->getLocation(),
11582                              FirstTemplate->getSourceRange(),
11583                              FunctionTemplateParameterDifferentDefaultArgument)
11584                     << FirstTemplate << (i + 1) << FirstTA;
11585                 ODRDiagNote(SecondTemplate->getLocation(),
11586                             SecondTemplate->getSourceRange(),
11587                             FunctionTemplateParameterDifferentDefaultArgument)
11588                     << SecondTemplate << (i + 1) << SecondTA;
11589                 ParameterMismatch = true;
11590                 break;
11591               }
11592             }
11593 
11594             if (FirstTTPD->isParameterPack() !=
11595                 SecondTTPD->isParameterPack()) {
11596               ODRDiagError(FirstTemplate->getLocation(),
11597                            FirstTemplate->getSourceRange(),
11598                            FunctionTemplatePackParameter)
11599                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11600               ODRDiagNote(SecondTemplate->getLocation(),
11601                           SecondTemplate->getSourceRange(),
11602                           FunctionTemplatePackParameter)
11603                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11604               ParameterMismatch = true;
11605               break;
11606             }
11607           }
11608 
11609           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11610               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11611             NonTypeTemplateParmDecl *FirstNTTPD =
11612                 cast<NonTypeTemplateParmDecl>(FirstParam);
11613             NonTypeTemplateParmDecl *SecondNTTPD =
11614                 cast<NonTypeTemplateParmDecl>(SecondParam);
11615 
11616             QualType FirstType = FirstNTTPD->getType();
11617             QualType SecondType = SecondNTTPD->getType();
11618             if (ComputeQualTypeODRHash(FirstType) !=
11619                 ComputeQualTypeODRHash(SecondType)) {
11620               ODRDiagError(FirstTemplate->getLocation(),
11621                            FirstTemplate->getSourceRange(),
11622                            FunctionTemplateParameterDifferentType)
11623                   << FirstTemplate << (i + 1);
11624               ODRDiagNote(SecondTemplate->getLocation(),
11625                           SecondTemplate->getSourceRange(),
11626                           FunctionTemplateParameterDifferentType)
11627                   << SecondTemplate << (i + 1);
11628               ParameterMismatch = true;
11629               break;
11630             }
11631 
11632             bool HasFirstDefaultArgument =
11633                 FirstNTTPD->hasDefaultArgument() &&
11634                 !FirstNTTPD->defaultArgumentWasInherited();
11635             bool HasSecondDefaultArgument =
11636                 SecondNTTPD->hasDefaultArgument() &&
11637                 !SecondNTTPD->defaultArgumentWasInherited();
11638             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11639               ODRDiagError(FirstTemplate->getLocation(),
11640                            FirstTemplate->getSourceRange(),
11641                            FunctionTemplateParameterSingleDefaultArgument)
11642                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11643               ODRDiagNote(SecondTemplate->getLocation(),
11644                           SecondTemplate->getSourceRange(),
11645                           FunctionTemplateParameterSingleDefaultArgument)
11646                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11647               ParameterMismatch = true;
11648               break;
11649             }
11650 
11651             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11652               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11653               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11654               if (ComputeODRHash(FirstDefaultArgument) !=
11655                   ComputeODRHash(SecondDefaultArgument)) {
11656                 ODRDiagError(FirstTemplate->getLocation(),
11657                              FirstTemplate->getSourceRange(),
11658                              FunctionTemplateParameterDifferentDefaultArgument)
11659                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11660                 ODRDiagNote(SecondTemplate->getLocation(),
11661                             SecondTemplate->getSourceRange(),
11662                             FunctionTemplateParameterDifferentDefaultArgument)
11663                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11664                 ParameterMismatch = true;
11665                 break;
11666               }
11667             }
11668 
11669             if (FirstNTTPD->isParameterPack() !=
11670                 SecondNTTPD->isParameterPack()) {
11671               ODRDiagError(FirstTemplate->getLocation(),
11672                            FirstTemplate->getSourceRange(),
11673                            FunctionTemplatePackParameter)
11674                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11675               ODRDiagNote(SecondTemplate->getLocation(),
11676                           SecondTemplate->getSourceRange(),
11677                           FunctionTemplatePackParameter)
11678                   << SecondTemplate << (i + 1)
11679                   << SecondNTTPD->isParameterPack();
11680               ParameterMismatch = true;
11681               break;
11682             }
11683           }
11684         }
11685 
11686         if (ParameterMismatch) {
11687           Diagnosed = true;
11688           break;
11689         }
11690 
11691         break;
11692       }
11693       }
11694 
11695       if (Diagnosed)
11696         continue;
11697 
11698       Diag(FirstDecl->getLocation(),
11699            diag::err_module_odr_violation_mismatch_decl_unknown)
11700           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11701           << FirstDecl->getSourceRange();
11702       Diag(SecondDecl->getLocation(),
11703            diag::note_module_odr_violation_mismatch_decl_unknown)
11704           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11705       Diagnosed = true;
11706     }
11707 
11708     if (!Diagnosed) {
11709       // All definitions are updates to the same declaration. This happens if a
11710       // module instantiates the declaration of a class template specialization
11711       // and two or more other modules instantiate its definition.
11712       //
11713       // FIXME: Indicate which modules had instantiations of this definition.
11714       // FIXME: How can this even happen?
11715       Diag(Merge.first->getLocation(),
11716            diag::err_module_odr_violation_different_instantiations)
11717         << Merge.first;
11718     }
11719   }
11720 
11721   // Issue ODR failures diagnostics for functions.
11722   for (auto &Merge : FunctionOdrMergeFailures) {
11723     enum ODRFunctionDifference {
11724       ReturnType,
11725       ParameterName,
11726       ParameterType,
11727       ParameterSingleDefaultArgument,
11728       ParameterDifferentDefaultArgument,
11729       FunctionBody,
11730     };
11731 
11732     FunctionDecl *FirstFunction = Merge.first;
11733     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11734 
11735     bool Diagnosed = false;
11736     for (auto &SecondFunction : Merge.second) {
11737 
11738       if (FirstFunction == SecondFunction)
11739         continue;
11740 
11741       std::string SecondModule =
11742           getOwningModuleNameForDiagnostic(SecondFunction);
11743 
11744       auto ODRDiagError = [FirstFunction, &FirstModule,
11745                            this](SourceLocation Loc, SourceRange Range,
11746                                  ODRFunctionDifference DiffType) {
11747         return Diag(Loc, diag::err_module_odr_violation_function)
11748                << FirstFunction << FirstModule.empty() << FirstModule << Range
11749                << DiffType;
11750       };
11751       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11752                                                SourceRange Range,
11753                                                ODRFunctionDifference DiffType) {
11754         return Diag(Loc, diag::note_module_odr_violation_function)
11755                << SecondModule << Range << DiffType;
11756       };
11757 
11758       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11759           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11760         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11761                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11762             << FirstFunction->getReturnType();
11763         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11764                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11765             << SecondFunction->getReturnType();
11766         Diagnosed = true;
11767         break;
11768       }
11769 
11770       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11771              "Merged functions with different number of parameters");
11772 
11773       auto ParamSize = FirstFunction->param_size();
11774       bool ParameterMismatch = false;
11775       for (unsigned I = 0; I < ParamSize; ++I) {
11776         auto *FirstParam = FirstFunction->getParamDecl(I);
11777         auto *SecondParam = SecondFunction->getParamDecl(I);
11778 
11779         assert(getContext().hasSameType(FirstParam->getType(),
11780                                       SecondParam->getType()) &&
11781                "Merged function has different parameter types.");
11782 
11783         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11784           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11785                        ParameterName)
11786               << I + 1 << FirstParam->getDeclName();
11787           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11788                       ParameterName)
11789               << I + 1 << SecondParam->getDeclName();
11790           ParameterMismatch = true;
11791           break;
11792         };
11793 
11794         QualType FirstParamType = FirstParam->getType();
11795         QualType SecondParamType = SecondParam->getType();
11796         if (FirstParamType != SecondParamType &&
11797             ComputeQualTypeODRHash(FirstParamType) !=
11798                 ComputeQualTypeODRHash(SecondParamType)) {
11799           if (const DecayedType *ParamDecayedType =
11800                   FirstParamType->getAs<DecayedType>()) {
11801             ODRDiagError(FirstParam->getLocation(),
11802                          FirstParam->getSourceRange(), ParameterType)
11803                 << (I + 1) << FirstParamType << true
11804                 << ParamDecayedType->getOriginalType();
11805           } else {
11806             ODRDiagError(FirstParam->getLocation(),
11807                          FirstParam->getSourceRange(), ParameterType)
11808                 << (I + 1) << FirstParamType << false;
11809           }
11810 
11811           if (const DecayedType *ParamDecayedType =
11812                   SecondParamType->getAs<DecayedType>()) {
11813             ODRDiagNote(SecondParam->getLocation(),
11814                         SecondParam->getSourceRange(), ParameterType)
11815                 << (I + 1) << SecondParamType << true
11816                 << ParamDecayedType->getOriginalType();
11817           } else {
11818             ODRDiagNote(SecondParam->getLocation(),
11819                         SecondParam->getSourceRange(), ParameterType)
11820                 << (I + 1) << SecondParamType << false;
11821           }
11822           ParameterMismatch = true;
11823           break;
11824         }
11825 
11826         const Expr *FirstInit = FirstParam->getInit();
11827         const Expr *SecondInit = SecondParam->getInit();
11828         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11829           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11830                        ParameterSingleDefaultArgument)
11831               << (I + 1) << (FirstInit == nullptr)
11832               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11833           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11834                       ParameterSingleDefaultArgument)
11835               << (I + 1) << (SecondInit == nullptr)
11836               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11837           ParameterMismatch = true;
11838           break;
11839         }
11840 
11841         if (FirstInit && SecondInit &&
11842             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11843           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11844                        ParameterDifferentDefaultArgument)
11845               << (I + 1) << FirstInit->getSourceRange();
11846           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11847                       ParameterDifferentDefaultArgument)
11848               << (I + 1) << SecondInit->getSourceRange();
11849           ParameterMismatch = true;
11850           break;
11851         }
11852 
11853         assert(ComputeSubDeclODRHash(FirstParam) ==
11854                    ComputeSubDeclODRHash(SecondParam) &&
11855                "Undiagnosed parameter difference.");
11856       }
11857 
11858       if (ParameterMismatch) {
11859         Diagnosed = true;
11860         break;
11861       }
11862 
11863       // If no error has been generated before now, assume the problem is in
11864       // the body and generate a message.
11865       ODRDiagError(FirstFunction->getLocation(),
11866                    FirstFunction->getSourceRange(), FunctionBody);
11867       ODRDiagNote(SecondFunction->getLocation(),
11868                   SecondFunction->getSourceRange(), FunctionBody);
11869       Diagnosed = true;
11870       break;
11871     }
11872     (void)Diagnosed;
11873     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11874   }
11875 
11876   // Issue ODR failures diagnostics for enums.
11877   for (auto &Merge : EnumOdrMergeFailures) {
11878     enum ODREnumDifference {
11879       SingleScopedEnum,
11880       EnumTagKeywordMismatch,
11881       SingleSpecifiedType,
11882       DifferentSpecifiedTypes,
11883       DifferentNumberEnumConstants,
11884       EnumConstantName,
11885       EnumConstantSingleInitilizer,
11886       EnumConstantDifferentInitilizer,
11887     };
11888 
11889     // If we've already pointed out a specific problem with this enum, don't
11890     // bother issuing a general "something's different" diagnostic.
11891     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11892       continue;
11893 
11894     EnumDecl *FirstEnum = Merge.first;
11895     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11896 
11897     using DeclHashes =
11898         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11899     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11900                               DeclHashes &Hashes, EnumDecl *Enum) {
11901       for (auto *D : Enum->decls()) {
11902         // Due to decl merging, the first EnumDecl is the parent of
11903         // Decls in both records.
11904         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11905           continue;
11906         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11907         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11908                             ComputeSubDeclODRHash(D));
11909       }
11910     };
11911     DeclHashes FirstHashes;
11912     PopulateHashes(FirstHashes, FirstEnum);
11913     bool Diagnosed = false;
11914     for (auto &SecondEnum : Merge.second) {
11915 
11916       if (FirstEnum == SecondEnum)
11917         continue;
11918 
11919       std::string SecondModule =
11920           getOwningModuleNameForDiagnostic(SecondEnum);
11921 
11922       auto ODRDiagError = [FirstEnum, &FirstModule,
11923                            this](SourceLocation Loc, SourceRange Range,
11924                                  ODREnumDifference DiffType) {
11925         return Diag(Loc, diag::err_module_odr_violation_enum)
11926                << FirstEnum << FirstModule.empty() << FirstModule << Range
11927                << DiffType;
11928       };
11929       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11930                                                SourceRange Range,
11931                                                ODREnumDifference DiffType) {
11932         return Diag(Loc, diag::note_module_odr_violation_enum)
11933                << SecondModule << Range << DiffType;
11934       };
11935 
11936       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11937         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11938                      SingleScopedEnum)
11939             << FirstEnum->isScoped();
11940         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11941                     SingleScopedEnum)
11942             << SecondEnum->isScoped();
11943         Diagnosed = true;
11944         continue;
11945       }
11946 
11947       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11948         if (FirstEnum->isScopedUsingClassTag() !=
11949             SecondEnum->isScopedUsingClassTag()) {
11950           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11951                        EnumTagKeywordMismatch)
11952               << FirstEnum->isScopedUsingClassTag();
11953           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11954                       EnumTagKeywordMismatch)
11955               << SecondEnum->isScopedUsingClassTag();
11956           Diagnosed = true;
11957           continue;
11958         }
11959       }
11960 
11961       QualType FirstUnderlyingType =
11962           FirstEnum->getIntegerTypeSourceInfo()
11963               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11964               : QualType();
11965       QualType SecondUnderlyingType =
11966           SecondEnum->getIntegerTypeSourceInfo()
11967               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11968               : QualType();
11969       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11970           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11971                        SingleSpecifiedType)
11972               << !FirstUnderlyingType.isNull();
11973           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11974                       SingleSpecifiedType)
11975               << !SecondUnderlyingType.isNull();
11976           Diagnosed = true;
11977           continue;
11978       }
11979 
11980       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11981         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11982             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11983           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11984                        DifferentSpecifiedTypes)
11985               << FirstUnderlyingType;
11986           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11987                       DifferentSpecifiedTypes)
11988               << SecondUnderlyingType;
11989           Diagnosed = true;
11990           continue;
11991         }
11992       }
11993 
11994       DeclHashes SecondHashes;
11995       PopulateHashes(SecondHashes, SecondEnum);
11996 
11997       if (FirstHashes.size() != SecondHashes.size()) {
11998         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11999                      DifferentNumberEnumConstants)
12000             << (int)FirstHashes.size();
12001         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
12002                     DifferentNumberEnumConstants)
12003             << (int)SecondHashes.size();
12004         Diagnosed = true;
12005         continue;
12006       }
12007 
12008       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
12009         if (FirstHashes[I].second == SecondHashes[I].second)
12010           continue;
12011         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
12012         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
12013 
12014         if (FirstEnumConstant->getDeclName() !=
12015             SecondEnumConstant->getDeclName()) {
12016 
12017           ODRDiagError(FirstEnumConstant->getLocation(),
12018                        FirstEnumConstant->getSourceRange(), EnumConstantName)
12019               << I + 1 << FirstEnumConstant;
12020           ODRDiagNote(SecondEnumConstant->getLocation(),
12021                       SecondEnumConstant->getSourceRange(), EnumConstantName)
12022               << I + 1 << SecondEnumConstant;
12023           Diagnosed = true;
12024           break;
12025         }
12026 
12027         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
12028         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
12029         if (!FirstInit && !SecondInit)
12030           continue;
12031 
12032         if (!FirstInit || !SecondInit) {
12033           ODRDiagError(FirstEnumConstant->getLocation(),
12034                        FirstEnumConstant->getSourceRange(),
12035                        EnumConstantSingleInitilizer)
12036               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
12037           ODRDiagNote(SecondEnumConstant->getLocation(),
12038                       SecondEnumConstant->getSourceRange(),
12039                       EnumConstantSingleInitilizer)
12040               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
12041           Diagnosed = true;
12042           break;
12043         }
12044 
12045         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
12046           ODRDiagError(FirstEnumConstant->getLocation(),
12047                        FirstEnumConstant->getSourceRange(),
12048                        EnumConstantDifferentInitilizer)
12049               << I + 1 << FirstEnumConstant;
12050           ODRDiagNote(SecondEnumConstant->getLocation(),
12051                       SecondEnumConstant->getSourceRange(),
12052                       EnumConstantDifferentInitilizer)
12053               << I + 1 << SecondEnumConstant;
12054           Diagnosed = true;
12055           break;
12056         }
12057       }
12058     }
12059 
12060     (void)Diagnosed;
12061     assert(Diagnosed && "Unable to emit ODR diagnostic.");
12062   }
12063 }
12064 
12065 void ASTReader::StartedDeserializing() {
12066   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
12067     ReadTimer->startTimer();
12068 }
12069 
12070 void ASTReader::FinishedDeserializing() {
12071   assert(NumCurrentElementsDeserializing &&
12072          "FinishedDeserializing not paired with StartedDeserializing");
12073   if (NumCurrentElementsDeserializing == 1) {
12074     // We decrease NumCurrentElementsDeserializing only after pending actions
12075     // are finished, to avoid recursively re-calling finishPendingActions().
12076     finishPendingActions();
12077   }
12078   --NumCurrentElementsDeserializing;
12079 
12080   if (NumCurrentElementsDeserializing == 0) {
12081     // Propagate exception specification and deduced type updates along
12082     // redeclaration chains.
12083     //
12084     // We do this now rather than in finishPendingActions because we want to
12085     // be able to walk the complete redeclaration chains of the updated decls.
12086     while (!PendingExceptionSpecUpdates.empty() ||
12087            !PendingDeducedTypeUpdates.empty()) {
12088       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
12089       PendingExceptionSpecUpdates.clear();
12090       for (auto Update : ESUpdates) {
12091         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12092         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
12093         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
12094         if (auto *Listener = getContext().getASTMutationListener())
12095           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
12096         for (auto *Redecl : Update.second->redecls())
12097           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
12098       }
12099 
12100       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
12101       PendingDeducedTypeUpdates.clear();
12102       for (auto Update : DTUpdates) {
12103         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
12104         // FIXME: If the return type is already deduced, check that it matches.
12105         getContext().adjustDeducedFunctionResultType(Update.first,
12106                                                      Update.second);
12107       }
12108     }
12109 
12110     if (ReadTimer)
12111       ReadTimer->stopTimer();
12112 
12113     diagnoseOdrViolations();
12114 
12115     // We are not in recursive loading, so it's safe to pass the "interesting"
12116     // decls to the consumer.
12117     if (Consumer)
12118       PassInterestingDeclsToConsumer();
12119   }
12120 }
12121 
12122 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
12123   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
12124     // Remove any fake results before adding any real ones.
12125     auto It = PendingFakeLookupResults.find(II);
12126     if (It != PendingFakeLookupResults.end()) {
12127       for (auto *ND : It->second)
12128         SemaObj->IdResolver.RemoveDecl(ND);
12129       // FIXME: this works around module+PCH performance issue.
12130       // Rather than erase the result from the map, which is O(n), just clear
12131       // the vector of NamedDecls.
12132       It->second.clear();
12133     }
12134   }
12135 
12136   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
12137     SemaObj->TUScope->AddDecl(D);
12138   } else if (SemaObj->TUScope) {
12139     // Adding the decl to IdResolver may have failed because it was already in
12140     // (even though it was not added in scope). If it is already in, make sure
12141     // it gets in the scope as well.
12142     if (std::find(SemaObj->IdResolver.begin(Name),
12143                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
12144       SemaObj->TUScope->AddDecl(D);
12145   }
12146 }
12147 
12148 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
12149                      ASTContext *Context,
12150                      const PCHContainerReader &PCHContainerRdr,
12151                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
12152                      StringRef isysroot, bool DisableValidation,
12153                      bool AllowASTWithCompilerErrors,
12154                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
12155                      bool UseGlobalIndex,
12156                      std::unique_ptr<llvm::Timer> ReadTimer)
12157     : Listener(DisableValidation
12158                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
12159                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
12160       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
12161       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
12162       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
12163                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
12164       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
12165       DisableValidation(DisableValidation),
12166       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
12167       AllowConfigurationMismatch(AllowConfigurationMismatch),
12168       ValidateSystemInputs(ValidateSystemInputs),
12169       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
12170   SourceMgr.setExternalSLocEntrySource(this);
12171 
12172   for (const auto &Ext : Extensions) {
12173     auto BlockName = Ext->getExtensionMetadata().BlockName;
12174     auto Known = ModuleFileExtensions.find(BlockName);
12175     if (Known != ModuleFileExtensions.end()) {
12176       Diags.Report(diag::warn_duplicate_module_file_extension)
12177         << BlockName;
12178       continue;
12179     }
12180 
12181     ModuleFileExtensions.insert({BlockName, Ext});
12182   }
12183 }
12184 
12185 ASTReader::~ASTReader() {
12186   if (OwnsDeserializationListener)
12187     delete DeserializationListener;
12188 }
12189 
12190 IdentifierResolver &ASTReader::getIdResolver() {
12191   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
12192 }
12193 
12194 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
12195                                                unsigned AbbrevID) {
12196   Idx = 0;
12197   Record.clear();
12198   return Cursor.readRecord(AbbrevID, Record);
12199 }
12200 //===----------------------------------------------------------------------===//
12201 //// OMPClauseReader implementation
12202 ////===----------------------------------------------------------------------===//
12203 
12204 OMPClause *OMPClauseReader::readClause() {
12205   OMPClause *C = nullptr;
12206   switch (Record.readInt()) {
12207   case OMPC_if:
12208     C = new (Context) OMPIfClause();
12209     break;
12210   case OMPC_final:
12211     C = new (Context) OMPFinalClause();
12212     break;
12213   case OMPC_num_threads:
12214     C = new (Context) OMPNumThreadsClause();
12215     break;
12216   case OMPC_safelen:
12217     C = new (Context) OMPSafelenClause();
12218     break;
12219   case OMPC_simdlen:
12220     C = new (Context) OMPSimdlenClause();
12221     break;
12222   case OMPC_allocator:
12223     C = new (Context) OMPAllocatorClause();
12224     break;
12225   case OMPC_collapse:
12226     C = new (Context) OMPCollapseClause();
12227     break;
12228   case OMPC_default:
12229     C = new (Context) OMPDefaultClause();
12230     break;
12231   case OMPC_proc_bind:
12232     C = new (Context) OMPProcBindClause();
12233     break;
12234   case OMPC_schedule:
12235     C = new (Context) OMPScheduleClause();
12236     break;
12237   case OMPC_ordered:
12238     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
12239     break;
12240   case OMPC_nowait:
12241     C = new (Context) OMPNowaitClause();
12242     break;
12243   case OMPC_untied:
12244     C = new (Context) OMPUntiedClause();
12245     break;
12246   case OMPC_mergeable:
12247     C = new (Context) OMPMergeableClause();
12248     break;
12249   case OMPC_read:
12250     C = new (Context) OMPReadClause();
12251     break;
12252   case OMPC_write:
12253     C = new (Context) OMPWriteClause();
12254     break;
12255   case OMPC_update:
12256     C = new (Context) OMPUpdateClause();
12257     break;
12258   case OMPC_capture:
12259     C = new (Context) OMPCaptureClause();
12260     break;
12261   case OMPC_seq_cst:
12262     C = new (Context) OMPSeqCstClause();
12263     break;
12264   case OMPC_threads:
12265     C = new (Context) OMPThreadsClause();
12266     break;
12267   case OMPC_simd:
12268     C = new (Context) OMPSIMDClause();
12269     break;
12270   case OMPC_nogroup:
12271     C = new (Context) OMPNogroupClause();
12272     break;
12273   case OMPC_unified_address:
12274     C = new (Context) OMPUnifiedAddressClause();
12275     break;
12276   case OMPC_unified_shared_memory:
12277     C = new (Context) OMPUnifiedSharedMemoryClause();
12278     break;
12279   case OMPC_reverse_offload:
12280     C = new (Context) OMPReverseOffloadClause();
12281     break;
12282   case OMPC_dynamic_allocators:
12283     C = new (Context) OMPDynamicAllocatorsClause();
12284     break;
12285   case OMPC_atomic_default_mem_order:
12286     C = new (Context) OMPAtomicDefaultMemOrderClause();
12287     break;
12288  case OMPC_private:
12289     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
12290     break;
12291   case OMPC_firstprivate:
12292     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
12293     break;
12294   case OMPC_lastprivate:
12295     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
12296     break;
12297   case OMPC_shared:
12298     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
12299     break;
12300   case OMPC_reduction:
12301     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
12302     break;
12303   case OMPC_task_reduction:
12304     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
12305     break;
12306   case OMPC_in_reduction:
12307     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
12308     break;
12309   case OMPC_linear:
12310     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
12311     break;
12312   case OMPC_aligned:
12313     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
12314     break;
12315   case OMPC_copyin:
12316     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
12317     break;
12318   case OMPC_copyprivate:
12319     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
12320     break;
12321   case OMPC_flush:
12322     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
12323     break;
12324   case OMPC_depend: {
12325     unsigned NumVars = Record.readInt();
12326     unsigned NumLoops = Record.readInt();
12327     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
12328     break;
12329   }
12330   case OMPC_device:
12331     C = new (Context) OMPDeviceClause();
12332     break;
12333   case OMPC_map: {
12334     OMPMappableExprListSizeTy Sizes;
12335     Sizes.NumVars = Record.readInt();
12336     Sizes.NumUniqueDeclarations = Record.readInt();
12337     Sizes.NumComponentLists = Record.readInt();
12338     Sizes.NumComponents = Record.readInt();
12339     C = OMPMapClause::CreateEmpty(Context, Sizes);
12340     break;
12341   }
12342   case OMPC_num_teams:
12343     C = new (Context) OMPNumTeamsClause();
12344     break;
12345   case OMPC_thread_limit:
12346     C = new (Context) OMPThreadLimitClause();
12347     break;
12348   case OMPC_priority:
12349     C = new (Context) OMPPriorityClause();
12350     break;
12351   case OMPC_grainsize:
12352     C = new (Context) OMPGrainsizeClause();
12353     break;
12354   case OMPC_num_tasks:
12355     C = new (Context) OMPNumTasksClause();
12356     break;
12357   case OMPC_hint:
12358     C = new (Context) OMPHintClause();
12359     break;
12360   case OMPC_dist_schedule:
12361     C = new (Context) OMPDistScheduleClause();
12362     break;
12363   case OMPC_defaultmap:
12364     C = new (Context) OMPDefaultmapClause();
12365     break;
12366   case OMPC_to: {
12367     OMPMappableExprListSizeTy Sizes;
12368     Sizes.NumVars = Record.readInt();
12369     Sizes.NumUniqueDeclarations = Record.readInt();
12370     Sizes.NumComponentLists = Record.readInt();
12371     Sizes.NumComponents = Record.readInt();
12372     C = OMPToClause::CreateEmpty(Context, Sizes);
12373     break;
12374   }
12375   case OMPC_from: {
12376     OMPMappableExprListSizeTy Sizes;
12377     Sizes.NumVars = Record.readInt();
12378     Sizes.NumUniqueDeclarations = Record.readInt();
12379     Sizes.NumComponentLists = Record.readInt();
12380     Sizes.NumComponents = Record.readInt();
12381     C = OMPFromClause::CreateEmpty(Context, Sizes);
12382     break;
12383   }
12384   case OMPC_use_device_ptr: {
12385     OMPMappableExprListSizeTy Sizes;
12386     Sizes.NumVars = Record.readInt();
12387     Sizes.NumUniqueDeclarations = Record.readInt();
12388     Sizes.NumComponentLists = Record.readInt();
12389     Sizes.NumComponents = Record.readInt();
12390     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
12391     break;
12392   }
12393   case OMPC_is_device_ptr: {
12394     OMPMappableExprListSizeTy Sizes;
12395     Sizes.NumVars = Record.readInt();
12396     Sizes.NumUniqueDeclarations = Record.readInt();
12397     Sizes.NumComponentLists = Record.readInt();
12398     Sizes.NumComponents = Record.readInt();
12399     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
12400     break;
12401   }
12402   case OMPC_allocate:
12403     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
12404     break;
12405   }
12406   assert(C && "Unknown OMPClause type");
12407 
12408   Visit(C);
12409   C->setLocStart(Record.readSourceLocation());
12410   C->setLocEnd(Record.readSourceLocation());
12411 
12412   return C;
12413 }
12414 
12415 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12416   C->setPreInitStmt(Record.readSubStmt(),
12417                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12418 }
12419 
12420 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12421   VisitOMPClauseWithPreInit(C);
12422   C->setPostUpdateExpr(Record.readSubExpr());
12423 }
12424 
12425 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12426   VisitOMPClauseWithPreInit(C);
12427   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12428   C->setNameModifierLoc(Record.readSourceLocation());
12429   C->setColonLoc(Record.readSourceLocation());
12430   C->setCondition(Record.readSubExpr());
12431   C->setLParenLoc(Record.readSourceLocation());
12432 }
12433 
12434 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12435   C->setCondition(Record.readSubExpr());
12436   C->setLParenLoc(Record.readSourceLocation());
12437 }
12438 
12439 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12440   VisitOMPClauseWithPreInit(C);
12441   C->setNumThreads(Record.readSubExpr());
12442   C->setLParenLoc(Record.readSourceLocation());
12443 }
12444 
12445 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12446   C->setSafelen(Record.readSubExpr());
12447   C->setLParenLoc(Record.readSourceLocation());
12448 }
12449 
12450 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12451   C->setSimdlen(Record.readSubExpr());
12452   C->setLParenLoc(Record.readSourceLocation());
12453 }
12454 
12455 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12456   C->setAllocator(Record.readExpr());
12457   C->setLParenLoc(Record.readSourceLocation());
12458 }
12459 
12460 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12461   C->setNumForLoops(Record.readSubExpr());
12462   C->setLParenLoc(Record.readSourceLocation());
12463 }
12464 
12465 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12466   C->setDefaultKind(
12467        static_cast<OpenMPDefaultClauseKind>(Record.readInt()));
12468   C->setLParenLoc(Record.readSourceLocation());
12469   C->setDefaultKindKwLoc(Record.readSourceLocation());
12470 }
12471 
12472 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12473   C->setProcBindKind(
12474        static_cast<OpenMPProcBindClauseKind>(Record.readInt()));
12475   C->setLParenLoc(Record.readSourceLocation());
12476   C->setProcBindKindKwLoc(Record.readSourceLocation());
12477 }
12478 
12479 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12480   VisitOMPClauseWithPreInit(C);
12481   C->setScheduleKind(
12482        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12483   C->setFirstScheduleModifier(
12484       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12485   C->setSecondScheduleModifier(
12486       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12487   C->setChunkSize(Record.readSubExpr());
12488   C->setLParenLoc(Record.readSourceLocation());
12489   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12490   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12491   C->setScheduleKindLoc(Record.readSourceLocation());
12492   C->setCommaLoc(Record.readSourceLocation());
12493 }
12494 
12495 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12496   C->setNumForLoops(Record.readSubExpr());
12497   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12498     C->setLoopNumIterations(I, Record.readSubExpr());
12499   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12500     C->setLoopCounter(I, Record.readSubExpr());
12501   C->setLParenLoc(Record.readSourceLocation());
12502 }
12503 
12504 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12505 
12506 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12507 
12508 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12509 
12510 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12511 
12512 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12513 
12514 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12515 
12516 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12517 
12518 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12519 
12520 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12521 
12522 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12523 
12524 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12525 
12526 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12527 
12528 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12529     OMPUnifiedSharedMemoryClause *) {}
12530 
12531 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12532 
12533 void
12534 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12535 }
12536 
12537 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12538     OMPAtomicDefaultMemOrderClause *C) {
12539   C->setAtomicDefaultMemOrderKind(
12540       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12541   C->setLParenLoc(Record.readSourceLocation());
12542   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12543 }
12544 
12545 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12546   C->setLParenLoc(Record.readSourceLocation());
12547   unsigned NumVars = C->varlist_size();
12548   SmallVector<Expr *, 16> Vars;
12549   Vars.reserve(NumVars);
12550   for (unsigned i = 0; i != NumVars; ++i)
12551     Vars.push_back(Record.readSubExpr());
12552   C->setVarRefs(Vars);
12553   Vars.clear();
12554   for (unsigned i = 0; i != NumVars; ++i)
12555     Vars.push_back(Record.readSubExpr());
12556   C->setPrivateCopies(Vars);
12557 }
12558 
12559 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12560   VisitOMPClauseWithPreInit(C);
12561   C->setLParenLoc(Record.readSourceLocation());
12562   unsigned NumVars = C->varlist_size();
12563   SmallVector<Expr *, 16> Vars;
12564   Vars.reserve(NumVars);
12565   for (unsigned i = 0; i != NumVars; ++i)
12566     Vars.push_back(Record.readSubExpr());
12567   C->setVarRefs(Vars);
12568   Vars.clear();
12569   for (unsigned i = 0; i != NumVars; ++i)
12570     Vars.push_back(Record.readSubExpr());
12571   C->setPrivateCopies(Vars);
12572   Vars.clear();
12573   for (unsigned i = 0; i != NumVars; ++i)
12574     Vars.push_back(Record.readSubExpr());
12575   C->setInits(Vars);
12576 }
12577 
12578 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12579   VisitOMPClauseWithPostUpdate(C);
12580   C->setLParenLoc(Record.readSourceLocation());
12581   unsigned NumVars = C->varlist_size();
12582   SmallVector<Expr *, 16> Vars;
12583   Vars.reserve(NumVars);
12584   for (unsigned i = 0; i != NumVars; ++i)
12585     Vars.push_back(Record.readSubExpr());
12586   C->setVarRefs(Vars);
12587   Vars.clear();
12588   for (unsigned i = 0; i != NumVars; ++i)
12589     Vars.push_back(Record.readSubExpr());
12590   C->setPrivateCopies(Vars);
12591   Vars.clear();
12592   for (unsigned i = 0; i != NumVars; ++i)
12593     Vars.push_back(Record.readSubExpr());
12594   C->setSourceExprs(Vars);
12595   Vars.clear();
12596   for (unsigned i = 0; i != NumVars; ++i)
12597     Vars.push_back(Record.readSubExpr());
12598   C->setDestinationExprs(Vars);
12599   Vars.clear();
12600   for (unsigned i = 0; i != NumVars; ++i)
12601     Vars.push_back(Record.readSubExpr());
12602   C->setAssignmentOps(Vars);
12603 }
12604 
12605 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12606   C->setLParenLoc(Record.readSourceLocation());
12607   unsigned NumVars = C->varlist_size();
12608   SmallVector<Expr *, 16> Vars;
12609   Vars.reserve(NumVars);
12610   for (unsigned i = 0; i != NumVars; ++i)
12611     Vars.push_back(Record.readSubExpr());
12612   C->setVarRefs(Vars);
12613 }
12614 
12615 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12616   VisitOMPClauseWithPostUpdate(C);
12617   C->setLParenLoc(Record.readSourceLocation());
12618   C->setColonLoc(Record.readSourceLocation());
12619   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12620   DeclarationNameInfo DNI;
12621   Record.readDeclarationNameInfo(DNI);
12622   C->setQualifierLoc(NNSL);
12623   C->setNameInfo(DNI);
12624 
12625   unsigned NumVars = C->varlist_size();
12626   SmallVector<Expr *, 16> Vars;
12627   Vars.reserve(NumVars);
12628   for (unsigned i = 0; i != NumVars; ++i)
12629     Vars.push_back(Record.readSubExpr());
12630   C->setVarRefs(Vars);
12631   Vars.clear();
12632   for (unsigned i = 0; i != NumVars; ++i)
12633     Vars.push_back(Record.readSubExpr());
12634   C->setPrivates(Vars);
12635   Vars.clear();
12636   for (unsigned i = 0; i != NumVars; ++i)
12637     Vars.push_back(Record.readSubExpr());
12638   C->setLHSExprs(Vars);
12639   Vars.clear();
12640   for (unsigned i = 0; i != NumVars; ++i)
12641     Vars.push_back(Record.readSubExpr());
12642   C->setRHSExprs(Vars);
12643   Vars.clear();
12644   for (unsigned i = 0; i != NumVars; ++i)
12645     Vars.push_back(Record.readSubExpr());
12646   C->setReductionOps(Vars);
12647 }
12648 
12649 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12650   VisitOMPClauseWithPostUpdate(C);
12651   C->setLParenLoc(Record.readSourceLocation());
12652   C->setColonLoc(Record.readSourceLocation());
12653   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12654   DeclarationNameInfo DNI;
12655   Record.readDeclarationNameInfo(DNI);
12656   C->setQualifierLoc(NNSL);
12657   C->setNameInfo(DNI);
12658 
12659   unsigned NumVars = C->varlist_size();
12660   SmallVector<Expr *, 16> Vars;
12661   Vars.reserve(NumVars);
12662   for (unsigned I = 0; I != NumVars; ++I)
12663     Vars.push_back(Record.readSubExpr());
12664   C->setVarRefs(Vars);
12665   Vars.clear();
12666   for (unsigned I = 0; I != NumVars; ++I)
12667     Vars.push_back(Record.readSubExpr());
12668   C->setPrivates(Vars);
12669   Vars.clear();
12670   for (unsigned I = 0; I != NumVars; ++I)
12671     Vars.push_back(Record.readSubExpr());
12672   C->setLHSExprs(Vars);
12673   Vars.clear();
12674   for (unsigned I = 0; I != NumVars; ++I)
12675     Vars.push_back(Record.readSubExpr());
12676   C->setRHSExprs(Vars);
12677   Vars.clear();
12678   for (unsigned I = 0; I != NumVars; ++I)
12679     Vars.push_back(Record.readSubExpr());
12680   C->setReductionOps(Vars);
12681 }
12682 
12683 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12684   VisitOMPClauseWithPostUpdate(C);
12685   C->setLParenLoc(Record.readSourceLocation());
12686   C->setColonLoc(Record.readSourceLocation());
12687   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12688   DeclarationNameInfo DNI;
12689   Record.readDeclarationNameInfo(DNI);
12690   C->setQualifierLoc(NNSL);
12691   C->setNameInfo(DNI);
12692 
12693   unsigned NumVars = C->varlist_size();
12694   SmallVector<Expr *, 16> Vars;
12695   Vars.reserve(NumVars);
12696   for (unsigned I = 0; I != NumVars; ++I)
12697     Vars.push_back(Record.readSubExpr());
12698   C->setVarRefs(Vars);
12699   Vars.clear();
12700   for (unsigned I = 0; I != NumVars; ++I)
12701     Vars.push_back(Record.readSubExpr());
12702   C->setPrivates(Vars);
12703   Vars.clear();
12704   for (unsigned I = 0; I != NumVars; ++I)
12705     Vars.push_back(Record.readSubExpr());
12706   C->setLHSExprs(Vars);
12707   Vars.clear();
12708   for (unsigned I = 0; I != NumVars; ++I)
12709     Vars.push_back(Record.readSubExpr());
12710   C->setRHSExprs(Vars);
12711   Vars.clear();
12712   for (unsigned I = 0; I != NumVars; ++I)
12713     Vars.push_back(Record.readSubExpr());
12714   C->setReductionOps(Vars);
12715   Vars.clear();
12716   for (unsigned I = 0; I != NumVars; ++I)
12717     Vars.push_back(Record.readSubExpr());
12718   C->setTaskgroupDescriptors(Vars);
12719 }
12720 
12721 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12722   VisitOMPClauseWithPostUpdate(C);
12723   C->setLParenLoc(Record.readSourceLocation());
12724   C->setColonLoc(Record.readSourceLocation());
12725   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12726   C->setModifierLoc(Record.readSourceLocation());
12727   unsigned NumVars = C->varlist_size();
12728   SmallVector<Expr *, 16> Vars;
12729   Vars.reserve(NumVars);
12730   for (unsigned i = 0; i != NumVars; ++i)
12731     Vars.push_back(Record.readSubExpr());
12732   C->setVarRefs(Vars);
12733   Vars.clear();
12734   for (unsigned i = 0; i != NumVars; ++i)
12735     Vars.push_back(Record.readSubExpr());
12736   C->setPrivates(Vars);
12737   Vars.clear();
12738   for (unsigned i = 0; i != NumVars; ++i)
12739     Vars.push_back(Record.readSubExpr());
12740   C->setInits(Vars);
12741   Vars.clear();
12742   for (unsigned i = 0; i != NumVars; ++i)
12743     Vars.push_back(Record.readSubExpr());
12744   C->setUpdates(Vars);
12745   Vars.clear();
12746   for (unsigned i = 0; i != NumVars; ++i)
12747     Vars.push_back(Record.readSubExpr());
12748   C->setFinals(Vars);
12749   C->setStep(Record.readSubExpr());
12750   C->setCalcStep(Record.readSubExpr());
12751   Vars.clear();
12752   for (unsigned I = 0; I != NumVars + 1; ++I)
12753     Vars.push_back(Record.readSubExpr());
12754   C->setUsedExprs(Vars);
12755 }
12756 
12757 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12758   C->setLParenLoc(Record.readSourceLocation());
12759   C->setColonLoc(Record.readSourceLocation());
12760   unsigned NumVars = C->varlist_size();
12761   SmallVector<Expr *, 16> Vars;
12762   Vars.reserve(NumVars);
12763   for (unsigned i = 0; i != NumVars; ++i)
12764     Vars.push_back(Record.readSubExpr());
12765   C->setVarRefs(Vars);
12766   C->setAlignment(Record.readSubExpr());
12767 }
12768 
12769 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12770   C->setLParenLoc(Record.readSourceLocation());
12771   unsigned NumVars = C->varlist_size();
12772   SmallVector<Expr *, 16> Exprs;
12773   Exprs.reserve(NumVars);
12774   for (unsigned i = 0; i != NumVars; ++i)
12775     Exprs.push_back(Record.readSubExpr());
12776   C->setVarRefs(Exprs);
12777   Exprs.clear();
12778   for (unsigned i = 0; i != NumVars; ++i)
12779     Exprs.push_back(Record.readSubExpr());
12780   C->setSourceExprs(Exprs);
12781   Exprs.clear();
12782   for (unsigned i = 0; i != NumVars; ++i)
12783     Exprs.push_back(Record.readSubExpr());
12784   C->setDestinationExprs(Exprs);
12785   Exprs.clear();
12786   for (unsigned i = 0; i != NumVars; ++i)
12787     Exprs.push_back(Record.readSubExpr());
12788   C->setAssignmentOps(Exprs);
12789 }
12790 
12791 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12792   C->setLParenLoc(Record.readSourceLocation());
12793   unsigned NumVars = C->varlist_size();
12794   SmallVector<Expr *, 16> Exprs;
12795   Exprs.reserve(NumVars);
12796   for (unsigned i = 0; i != NumVars; ++i)
12797     Exprs.push_back(Record.readSubExpr());
12798   C->setVarRefs(Exprs);
12799   Exprs.clear();
12800   for (unsigned i = 0; i != NumVars; ++i)
12801     Exprs.push_back(Record.readSubExpr());
12802   C->setSourceExprs(Exprs);
12803   Exprs.clear();
12804   for (unsigned i = 0; i != NumVars; ++i)
12805     Exprs.push_back(Record.readSubExpr());
12806   C->setDestinationExprs(Exprs);
12807   Exprs.clear();
12808   for (unsigned i = 0; i != NumVars; ++i)
12809     Exprs.push_back(Record.readSubExpr());
12810   C->setAssignmentOps(Exprs);
12811 }
12812 
12813 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12814   C->setLParenLoc(Record.readSourceLocation());
12815   unsigned NumVars = C->varlist_size();
12816   SmallVector<Expr *, 16> Vars;
12817   Vars.reserve(NumVars);
12818   for (unsigned i = 0; i != NumVars; ++i)
12819     Vars.push_back(Record.readSubExpr());
12820   C->setVarRefs(Vars);
12821 }
12822 
12823 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12824   C->setLParenLoc(Record.readSourceLocation());
12825   C->setDependencyKind(
12826       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12827   C->setDependencyLoc(Record.readSourceLocation());
12828   C->setColonLoc(Record.readSourceLocation());
12829   unsigned NumVars = C->varlist_size();
12830   SmallVector<Expr *, 16> Vars;
12831   Vars.reserve(NumVars);
12832   for (unsigned I = 0; I != NumVars; ++I)
12833     Vars.push_back(Record.readSubExpr());
12834   C->setVarRefs(Vars);
12835   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12836     C->setLoopData(I, Record.readSubExpr());
12837 }
12838 
12839 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12840   VisitOMPClauseWithPreInit(C);
12841   C->setDevice(Record.readSubExpr());
12842   C->setLParenLoc(Record.readSourceLocation());
12843 }
12844 
12845 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12846   C->setLParenLoc(Record.readSourceLocation());
12847   for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
12848     C->setMapTypeModifier(
12849         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12850     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12851   }
12852   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12853   DeclarationNameInfo DNI;
12854   Record.readDeclarationNameInfo(DNI);
12855   C->setMapperIdInfo(DNI);
12856   C->setMapType(
12857      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12858   C->setMapLoc(Record.readSourceLocation());
12859   C->setColonLoc(Record.readSourceLocation());
12860   auto NumVars = C->varlist_size();
12861   auto UniqueDecls = C->getUniqueDeclarationsNum();
12862   auto TotalLists = C->getTotalComponentListNum();
12863   auto TotalComponents = C->getTotalComponentsNum();
12864 
12865   SmallVector<Expr *, 16> Vars;
12866   Vars.reserve(NumVars);
12867   for (unsigned i = 0; i != NumVars; ++i)
12868     Vars.push_back(Record.readExpr());
12869   C->setVarRefs(Vars);
12870 
12871   SmallVector<Expr *, 16> UDMappers;
12872   UDMappers.reserve(NumVars);
12873   for (unsigned I = 0; I < NumVars; ++I)
12874     UDMappers.push_back(Record.readExpr());
12875   C->setUDMapperRefs(UDMappers);
12876 
12877   SmallVector<ValueDecl *, 16> Decls;
12878   Decls.reserve(UniqueDecls);
12879   for (unsigned i = 0; i < UniqueDecls; ++i)
12880     Decls.push_back(Record.readDeclAs<ValueDecl>());
12881   C->setUniqueDecls(Decls);
12882 
12883   SmallVector<unsigned, 16> ListsPerDecl;
12884   ListsPerDecl.reserve(UniqueDecls);
12885   for (unsigned i = 0; i < UniqueDecls; ++i)
12886     ListsPerDecl.push_back(Record.readInt());
12887   C->setDeclNumLists(ListsPerDecl);
12888 
12889   SmallVector<unsigned, 32> ListSizes;
12890   ListSizes.reserve(TotalLists);
12891   for (unsigned i = 0; i < TotalLists; ++i)
12892     ListSizes.push_back(Record.readInt());
12893   C->setComponentListSizes(ListSizes);
12894 
12895   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12896   Components.reserve(TotalComponents);
12897   for (unsigned i = 0; i < TotalComponents; ++i) {
12898     Expr *AssociatedExpr = Record.readExpr();
12899     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12900     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12901         AssociatedExpr, AssociatedDecl));
12902   }
12903   C->setComponents(Components, ListSizes);
12904 }
12905 
12906 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12907   C->setLParenLoc(Record.readSourceLocation());
12908   C->setColonLoc(Record.readSourceLocation());
12909   C->setAllocator(Record.readSubExpr());
12910   unsigned NumVars = C->varlist_size();
12911   SmallVector<Expr *, 16> Vars;
12912   Vars.reserve(NumVars);
12913   for (unsigned i = 0; i != NumVars; ++i)
12914     Vars.push_back(Record.readSubExpr());
12915   C->setVarRefs(Vars);
12916 }
12917 
12918 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12919   VisitOMPClauseWithPreInit(C);
12920   C->setNumTeams(Record.readSubExpr());
12921   C->setLParenLoc(Record.readSourceLocation());
12922 }
12923 
12924 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12925   VisitOMPClauseWithPreInit(C);
12926   C->setThreadLimit(Record.readSubExpr());
12927   C->setLParenLoc(Record.readSourceLocation());
12928 }
12929 
12930 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12931   C->setPriority(Record.readSubExpr());
12932   C->setLParenLoc(Record.readSourceLocation());
12933 }
12934 
12935 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12936   C->setGrainsize(Record.readSubExpr());
12937   C->setLParenLoc(Record.readSourceLocation());
12938 }
12939 
12940 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12941   C->setNumTasks(Record.readSubExpr());
12942   C->setLParenLoc(Record.readSourceLocation());
12943 }
12944 
12945 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12946   C->setHint(Record.readSubExpr());
12947   C->setLParenLoc(Record.readSourceLocation());
12948 }
12949 
12950 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12951   VisitOMPClauseWithPreInit(C);
12952   C->setDistScheduleKind(
12953       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12954   C->setChunkSize(Record.readSubExpr());
12955   C->setLParenLoc(Record.readSourceLocation());
12956   C->setDistScheduleKindLoc(Record.readSourceLocation());
12957   C->setCommaLoc(Record.readSourceLocation());
12958 }
12959 
12960 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12961   C->setDefaultmapKind(
12962        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12963   C->setDefaultmapModifier(
12964       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12965   C->setLParenLoc(Record.readSourceLocation());
12966   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12967   C->setDefaultmapKindLoc(Record.readSourceLocation());
12968 }
12969 
12970 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12971   C->setLParenLoc(Record.readSourceLocation());
12972   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12973   DeclarationNameInfo DNI;
12974   Record.readDeclarationNameInfo(DNI);
12975   C->setMapperIdInfo(DNI);
12976   auto NumVars = C->varlist_size();
12977   auto UniqueDecls = C->getUniqueDeclarationsNum();
12978   auto TotalLists = C->getTotalComponentListNum();
12979   auto TotalComponents = C->getTotalComponentsNum();
12980 
12981   SmallVector<Expr *, 16> Vars;
12982   Vars.reserve(NumVars);
12983   for (unsigned i = 0; i != NumVars; ++i)
12984     Vars.push_back(Record.readSubExpr());
12985   C->setVarRefs(Vars);
12986 
12987   SmallVector<Expr *, 16> UDMappers;
12988   UDMappers.reserve(NumVars);
12989   for (unsigned I = 0; I < NumVars; ++I)
12990     UDMappers.push_back(Record.readSubExpr());
12991   C->setUDMapperRefs(UDMappers);
12992 
12993   SmallVector<ValueDecl *, 16> Decls;
12994   Decls.reserve(UniqueDecls);
12995   for (unsigned i = 0; i < UniqueDecls; ++i)
12996     Decls.push_back(Record.readDeclAs<ValueDecl>());
12997   C->setUniqueDecls(Decls);
12998 
12999   SmallVector<unsigned, 16> ListsPerDecl;
13000   ListsPerDecl.reserve(UniqueDecls);
13001   for (unsigned i = 0; i < UniqueDecls; ++i)
13002     ListsPerDecl.push_back(Record.readInt());
13003   C->setDeclNumLists(ListsPerDecl);
13004 
13005   SmallVector<unsigned, 32> ListSizes;
13006   ListSizes.reserve(TotalLists);
13007   for (unsigned i = 0; i < TotalLists; ++i)
13008     ListSizes.push_back(Record.readInt());
13009   C->setComponentListSizes(ListSizes);
13010 
13011   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13012   Components.reserve(TotalComponents);
13013   for (unsigned i = 0; i < TotalComponents; ++i) {
13014     Expr *AssociatedExpr = Record.readSubExpr();
13015     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13016     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13017         AssociatedExpr, AssociatedDecl));
13018   }
13019   C->setComponents(Components, ListSizes);
13020 }
13021 
13022 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
13023   C->setLParenLoc(Record.readSourceLocation());
13024   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
13025   DeclarationNameInfo DNI;
13026   Record.readDeclarationNameInfo(DNI);
13027   C->setMapperIdInfo(DNI);
13028   auto NumVars = C->varlist_size();
13029   auto UniqueDecls = C->getUniqueDeclarationsNum();
13030   auto TotalLists = C->getTotalComponentListNum();
13031   auto TotalComponents = C->getTotalComponentsNum();
13032 
13033   SmallVector<Expr *, 16> Vars;
13034   Vars.reserve(NumVars);
13035   for (unsigned i = 0; i != NumVars; ++i)
13036     Vars.push_back(Record.readSubExpr());
13037   C->setVarRefs(Vars);
13038 
13039   SmallVector<Expr *, 16> UDMappers;
13040   UDMappers.reserve(NumVars);
13041   for (unsigned I = 0; I < NumVars; ++I)
13042     UDMappers.push_back(Record.readSubExpr());
13043   C->setUDMapperRefs(UDMappers);
13044 
13045   SmallVector<ValueDecl *, 16> Decls;
13046   Decls.reserve(UniqueDecls);
13047   for (unsigned i = 0; i < UniqueDecls; ++i)
13048     Decls.push_back(Record.readDeclAs<ValueDecl>());
13049   C->setUniqueDecls(Decls);
13050 
13051   SmallVector<unsigned, 16> ListsPerDecl;
13052   ListsPerDecl.reserve(UniqueDecls);
13053   for (unsigned i = 0; i < UniqueDecls; ++i)
13054     ListsPerDecl.push_back(Record.readInt());
13055   C->setDeclNumLists(ListsPerDecl);
13056 
13057   SmallVector<unsigned, 32> ListSizes;
13058   ListSizes.reserve(TotalLists);
13059   for (unsigned i = 0; i < TotalLists; ++i)
13060     ListSizes.push_back(Record.readInt());
13061   C->setComponentListSizes(ListSizes);
13062 
13063   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13064   Components.reserve(TotalComponents);
13065   for (unsigned i = 0; i < TotalComponents; ++i) {
13066     Expr *AssociatedExpr = Record.readSubExpr();
13067     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13068     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13069         AssociatedExpr, AssociatedDecl));
13070   }
13071   C->setComponents(Components, ListSizes);
13072 }
13073 
13074 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
13075   C->setLParenLoc(Record.readSourceLocation());
13076   auto NumVars = C->varlist_size();
13077   auto UniqueDecls = C->getUniqueDeclarationsNum();
13078   auto TotalLists = C->getTotalComponentListNum();
13079   auto TotalComponents = C->getTotalComponentsNum();
13080 
13081   SmallVector<Expr *, 16> Vars;
13082   Vars.reserve(NumVars);
13083   for (unsigned i = 0; i != NumVars; ++i)
13084     Vars.push_back(Record.readSubExpr());
13085   C->setVarRefs(Vars);
13086   Vars.clear();
13087   for (unsigned i = 0; i != NumVars; ++i)
13088     Vars.push_back(Record.readSubExpr());
13089   C->setPrivateCopies(Vars);
13090   Vars.clear();
13091   for (unsigned i = 0; i != NumVars; ++i)
13092     Vars.push_back(Record.readSubExpr());
13093   C->setInits(Vars);
13094 
13095   SmallVector<ValueDecl *, 16> Decls;
13096   Decls.reserve(UniqueDecls);
13097   for (unsigned i = 0; i < UniqueDecls; ++i)
13098     Decls.push_back(Record.readDeclAs<ValueDecl>());
13099   C->setUniqueDecls(Decls);
13100 
13101   SmallVector<unsigned, 16> ListsPerDecl;
13102   ListsPerDecl.reserve(UniqueDecls);
13103   for (unsigned i = 0; i < UniqueDecls; ++i)
13104     ListsPerDecl.push_back(Record.readInt());
13105   C->setDeclNumLists(ListsPerDecl);
13106 
13107   SmallVector<unsigned, 32> ListSizes;
13108   ListSizes.reserve(TotalLists);
13109   for (unsigned i = 0; i < TotalLists; ++i)
13110     ListSizes.push_back(Record.readInt());
13111   C->setComponentListSizes(ListSizes);
13112 
13113   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13114   Components.reserve(TotalComponents);
13115   for (unsigned i = 0; i < TotalComponents; ++i) {
13116     Expr *AssociatedExpr = Record.readSubExpr();
13117     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13118     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13119         AssociatedExpr, AssociatedDecl));
13120   }
13121   C->setComponents(Components, ListSizes);
13122 }
13123 
13124 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13125   C->setLParenLoc(Record.readSourceLocation());
13126   auto NumVars = C->varlist_size();
13127   auto UniqueDecls = C->getUniqueDeclarationsNum();
13128   auto TotalLists = C->getTotalComponentListNum();
13129   auto TotalComponents = C->getTotalComponentsNum();
13130 
13131   SmallVector<Expr *, 16> Vars;
13132   Vars.reserve(NumVars);
13133   for (unsigned i = 0; i != NumVars; ++i)
13134     Vars.push_back(Record.readSubExpr());
13135   C->setVarRefs(Vars);
13136   Vars.clear();
13137 
13138   SmallVector<ValueDecl *, 16> Decls;
13139   Decls.reserve(UniqueDecls);
13140   for (unsigned i = 0; i < UniqueDecls; ++i)
13141     Decls.push_back(Record.readDeclAs<ValueDecl>());
13142   C->setUniqueDecls(Decls);
13143 
13144   SmallVector<unsigned, 16> ListsPerDecl;
13145   ListsPerDecl.reserve(UniqueDecls);
13146   for (unsigned i = 0; i < UniqueDecls; ++i)
13147     ListsPerDecl.push_back(Record.readInt());
13148   C->setDeclNumLists(ListsPerDecl);
13149 
13150   SmallVector<unsigned, 32> ListSizes;
13151   ListSizes.reserve(TotalLists);
13152   for (unsigned i = 0; i < TotalLists; ++i)
13153     ListSizes.push_back(Record.readInt());
13154   C->setComponentListSizes(ListSizes);
13155 
13156   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13157   Components.reserve(TotalComponents);
13158   for (unsigned i = 0; i < TotalComponents; ++i) {
13159     Expr *AssociatedExpr = Record.readSubExpr();
13160     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13161     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
13162         AssociatedExpr, AssociatedDecl));
13163   }
13164   C->setComponents(Components, ListSizes);
13165 }
13166