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