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