1 //===--- ASTReader.cpp - AST File Reader ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ASTReader class, which reads AST files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Serialization/ASTReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/Type.h"
24 #include "clang/AST/TypeLocVisitor.h"
25 #include "clang/Basic/FileManager.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "clang/Basic/SourceManagerInternals.h"
28 #include "clang/Basic/TargetInfo.h"
29 #include "clang/Basic/TargetOptions.h"
30 #include "clang/Basic/Version.h"
31 #include "clang/Basic/VersionTuple.h"
32 #include "clang/Lex/HeaderSearch.h"
33 #include "clang/Lex/HeaderSearchOptions.h"
34 #include "clang/Lex/MacroInfo.h"
35 #include "clang/Lex/PreprocessingRecord.h"
36 #include "clang/Lex/Preprocessor.h"
37 #include "clang/Lex/PreprocessorOptions.h"
38 #include "clang/Sema/Scope.h"
39 #include "clang/Sema/Sema.h"
40 #include "clang/Serialization/ASTDeserializationListener.h"
41 #include "clang/Serialization/GlobalModuleIndex.h"
42 #include "clang/Serialization/ModuleManager.h"
43 #include "clang/Serialization/SerializationDiagnostic.h"
44 #include "llvm/ADT/Hashing.h"
45 #include "llvm/ADT/StringExtras.h"
46 #include "llvm/Bitcode/BitstreamReader.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FileSystem.h"
49 #include "llvm/Support/MemoryBuffer.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/SaveAndRestore.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Support/system_error.h"
54 #include <algorithm>
55 #include <cstdio>
56 #include <iterator>
57 
58 using namespace clang;
59 using namespace clang::serialization;
60 using namespace clang::serialization::reader;
61 using llvm::BitstreamCursor;
62 
63 //===----------------------------------------------------------------------===//
64 // PCH validator implementation
65 //===----------------------------------------------------------------------===//
66 
67 ASTReaderListener::~ASTReaderListener() {}
68 
69 /// \brief Compare the given set of language options against an existing set of
70 /// language options.
71 ///
72 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
73 ///
74 /// \returns true if the languagae options mis-match, false otherwise.
75 static bool checkLanguageOptions(const LangOptions &LangOpts,
76                                  const LangOptions &ExistingLangOpts,
77                                  DiagnosticsEngine *Diags) {
78 #define LANGOPT(Name, Bits, Default, Description)                 \
79   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
80     if (Diags)                                                    \
81       Diags->Report(diag::err_pch_langopt_mismatch)               \
82         << Description << LangOpts.Name << ExistingLangOpts.Name; \
83     return true;                                                  \
84   }
85 
86 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
87   if (ExistingLangOpts.Name != LangOpts.Name) {           \
88     if (Diags)                                            \
89       Diags->Report(diag::err_pch_langopt_value_mismatch) \
90         << Description;                                   \
91     return true;                                          \
92   }
93 
94 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
95   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
96     if (Diags)                                                 \
97       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
98         << Description;                                        \
99     return true;                                               \
100   }
101 
102 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
103 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
104 #include "clang/Basic/LangOptions.def"
105 
106   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
107     if (Diags)
108       Diags->Report(diag::err_pch_langopt_value_mismatch)
109       << "target Objective-C runtime";
110     return true;
111   }
112 
113   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
114       LangOpts.CommentOpts.BlockCommandNames) {
115     if (Diags)
116       Diags->Report(diag::err_pch_langopt_value_mismatch)
117         << "block command names";
118     return true;
119   }
120 
121   return false;
122 }
123 
124 /// \brief Compare the given set of target options against an existing set of
125 /// target options.
126 ///
127 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
128 ///
129 /// \returns true if the target options mis-match, false otherwise.
130 static bool checkTargetOptions(const TargetOptions &TargetOpts,
131                                const TargetOptions &ExistingTargetOpts,
132                                DiagnosticsEngine *Diags) {
133 #define CHECK_TARGET_OPT(Field, Name)                             \
134   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
135     if (Diags)                                                    \
136       Diags->Report(diag::err_pch_targetopt_mismatch)             \
137         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
138     return true;                                                  \
139   }
140 
141   CHECK_TARGET_OPT(Triple, "target");
142   CHECK_TARGET_OPT(CPU, "target CPU");
143   CHECK_TARGET_OPT(ABI, "target ABI");
144   CHECK_TARGET_OPT(LinkerVersion, "target linker version");
145 #undef CHECK_TARGET_OPT
146 
147   // Compare feature sets.
148   SmallVector<StringRef, 4> ExistingFeatures(
149                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
150                                              ExistingTargetOpts.FeaturesAsWritten.end());
151   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
152                                          TargetOpts.FeaturesAsWritten.end());
153   std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
154   std::sort(ReadFeatures.begin(), ReadFeatures.end());
155 
156   unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
157   unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
158   while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
159     if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
160       ++ExistingIdx;
161       ++ReadIdx;
162       continue;
163     }
164 
165     if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
166       if (Diags)
167         Diags->Report(diag::err_pch_targetopt_feature_mismatch)
168           << false << ReadFeatures[ReadIdx];
169       return true;
170     }
171 
172     if (Diags)
173       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
174         << true << ExistingFeatures[ExistingIdx];
175     return true;
176   }
177 
178   if (ExistingIdx < ExistingN) {
179     if (Diags)
180       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
181         << true << ExistingFeatures[ExistingIdx];
182     return true;
183   }
184 
185   if (ReadIdx < ReadN) {
186     if (Diags)
187       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
188         << false << ReadFeatures[ReadIdx];
189     return true;
190   }
191 
192   return false;
193 }
194 
195 bool
196 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
197                                   bool Complain) {
198   const LangOptions &ExistingLangOpts = PP.getLangOpts();
199   return checkLanguageOptions(LangOpts, ExistingLangOpts,
200                               Complain? &Reader.Diags : 0);
201 }
202 
203 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
204                                      bool Complain) {
205   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
206   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
207                             Complain? &Reader.Diags : 0);
208 }
209 
210 namespace {
211   typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
212     MacroDefinitionsMap;
213   typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
214     DeclsMap;
215 }
216 
217 /// \brief Collect the macro definitions provided by the given preprocessor
218 /// options.
219 static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
220                                     MacroDefinitionsMap &Macros,
221                                     SmallVectorImpl<StringRef> *MacroNames = 0){
222   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
223     StringRef Macro = PPOpts.Macros[I].first;
224     bool IsUndef = PPOpts.Macros[I].second;
225 
226     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
227     StringRef MacroName = MacroPair.first;
228     StringRef MacroBody = MacroPair.second;
229 
230     // For an #undef'd macro, we only care about the name.
231     if (IsUndef) {
232       if (MacroNames && !Macros.count(MacroName))
233         MacroNames->push_back(MacroName);
234 
235       Macros[MacroName] = std::make_pair("", true);
236       continue;
237     }
238 
239     // For a #define'd macro, figure out the actual definition.
240     if (MacroName.size() == Macro.size())
241       MacroBody = "1";
242     else {
243       // Note: GCC drops anything following an end-of-line character.
244       StringRef::size_type End = MacroBody.find_first_of("\n\r");
245       MacroBody = MacroBody.substr(0, End);
246     }
247 
248     if (MacroNames && !Macros.count(MacroName))
249       MacroNames->push_back(MacroName);
250     Macros[MacroName] = std::make_pair(MacroBody, false);
251   }
252 }
253 
254 /// \brief Check the preprocessor options deserialized from the control block
255 /// against the preprocessor options in an existing preprocessor.
256 ///
257 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
258 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
259                                      const PreprocessorOptions &ExistingPPOpts,
260                                      DiagnosticsEngine *Diags,
261                                      FileManager &FileMgr,
262                                      std::string &SuggestedPredefines,
263                                      const LangOptions &LangOpts) {
264   // Check macro definitions.
265   MacroDefinitionsMap ASTFileMacros;
266   collectMacroDefinitions(PPOpts, ASTFileMacros);
267   MacroDefinitionsMap ExistingMacros;
268   SmallVector<StringRef, 4> ExistingMacroNames;
269   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
270 
271   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
272     // Dig out the macro definition in the existing preprocessor options.
273     StringRef MacroName = ExistingMacroNames[I];
274     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
275 
276     // Check whether we know anything about this macro name or not.
277     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
278       = ASTFileMacros.find(MacroName);
279     if (Known == ASTFileMacros.end()) {
280       // FIXME: Check whether this identifier was referenced anywhere in the
281       // AST file. If so, we should reject the AST file. Unfortunately, this
282       // information isn't in the control block. What shall we do about it?
283 
284       if (Existing.second) {
285         SuggestedPredefines += "#undef ";
286         SuggestedPredefines += MacroName.str();
287         SuggestedPredefines += '\n';
288       } else {
289         SuggestedPredefines += "#define ";
290         SuggestedPredefines += MacroName.str();
291         SuggestedPredefines += ' ';
292         SuggestedPredefines += Existing.first.str();
293         SuggestedPredefines += '\n';
294       }
295       continue;
296     }
297 
298     // If the macro was defined in one but undef'd in the other, we have a
299     // conflict.
300     if (Existing.second != Known->second.second) {
301       if (Diags) {
302         Diags->Report(diag::err_pch_macro_def_undef)
303           << MacroName << Known->second.second;
304       }
305       return true;
306     }
307 
308     // If the macro was #undef'd in both, or if the macro bodies are identical,
309     // it's fine.
310     if (Existing.second || Existing.first == Known->second.first)
311       continue;
312 
313     // The macro bodies differ; complain.
314     if (Diags) {
315       Diags->Report(diag::err_pch_macro_def_conflict)
316         << MacroName << Known->second.first << Existing.first;
317     }
318     return true;
319   }
320 
321   // Check whether we're using predefines.
322   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
323     if (Diags) {
324       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
325     }
326     return true;
327   }
328 
329   // Detailed record is important since it is used for the module cache hash.
330   if (LangOpts.Modules &&
331       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
332     if (Diags) {
333       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
334     }
335     return true;
336   }
337 
338   // Compute the #include and #include_macros lines we need.
339   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
340     StringRef File = ExistingPPOpts.Includes[I];
341     if (File == ExistingPPOpts.ImplicitPCHInclude)
342       continue;
343 
344     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
345           != PPOpts.Includes.end())
346       continue;
347 
348     SuggestedPredefines += "#include \"";
349     SuggestedPredefines +=
350       HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
351     SuggestedPredefines += "\"\n";
352   }
353 
354   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
355     StringRef File = ExistingPPOpts.MacroIncludes[I];
356     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
357                   File)
358         != PPOpts.MacroIncludes.end())
359       continue;
360 
361     SuggestedPredefines += "#__include_macros \"";
362     SuggestedPredefines +=
363       HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
364     SuggestedPredefines += "\"\n##\n";
365   }
366 
367   return false;
368 }
369 
370 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
371                                            bool Complain,
372                                            std::string &SuggestedPredefines) {
373   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
374 
375   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
376                                   Complain? &Reader.Diags : 0,
377                                   PP.getFileManager(),
378                                   SuggestedPredefines,
379                                   PP.getLangOpts());
380 }
381 
382 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
383   PP.setCounterValue(Value);
384 }
385 
386 //===----------------------------------------------------------------------===//
387 // AST reader implementation
388 //===----------------------------------------------------------------------===//
389 
390 void
391 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
392   DeserializationListener = Listener;
393 }
394 
395 
396 
397 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
398   return serialization::ComputeHash(Sel);
399 }
400 
401 
402 std::pair<unsigned, unsigned>
403 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
404   using namespace clang::io;
405   unsigned KeyLen = ReadUnalignedLE16(d);
406   unsigned DataLen = ReadUnalignedLE16(d);
407   return std::make_pair(KeyLen, DataLen);
408 }
409 
410 ASTSelectorLookupTrait::internal_key_type
411 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
412   using namespace clang::io;
413   SelectorTable &SelTable = Reader.getContext().Selectors;
414   unsigned N = ReadUnalignedLE16(d);
415   IdentifierInfo *FirstII
416     = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
417   if (N == 0)
418     return SelTable.getNullarySelector(FirstII);
419   else if (N == 1)
420     return SelTable.getUnarySelector(FirstII);
421 
422   SmallVector<IdentifierInfo *, 16> Args;
423   Args.push_back(FirstII);
424   for (unsigned I = 1; I != N; ++I)
425     Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
426 
427   return SelTable.getSelector(N, Args.data());
428 }
429 
430 ASTSelectorLookupTrait::data_type
431 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
432                                  unsigned DataLen) {
433   using namespace clang::io;
434 
435   data_type Result;
436 
437   Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
438   unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
439   unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
440   Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
441   Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
442   unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
443   unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
444 
445   // Load instance methods
446   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
447     if (ObjCMethodDecl *Method
448           = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
449       Result.Instance.push_back(Method);
450   }
451 
452   // Load factory methods
453   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
454     if (ObjCMethodDecl *Method
455           = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
456       Result.Factory.push_back(Method);
457   }
458 
459   return Result;
460 }
461 
462 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
463   return llvm::HashString(a);
464 }
465 
466 std::pair<unsigned, unsigned>
467 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
468   using namespace clang::io;
469   unsigned DataLen = ReadUnalignedLE16(d);
470   unsigned KeyLen = ReadUnalignedLE16(d);
471   return std::make_pair(KeyLen, DataLen);
472 }
473 
474 ASTIdentifierLookupTraitBase::internal_key_type
475 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
476   assert(n >= 2 && d[n-1] == '\0');
477   return StringRef((const char*) d, n-1);
478 }
479 
480 /// \brief Whether the given identifier is "interesting".
481 static bool isInterestingIdentifier(IdentifierInfo &II) {
482   return II.isPoisoned() ||
483          II.isExtensionToken() ||
484          II.getObjCOrBuiltinID() ||
485          II.hasRevertedTokenIDToIdentifier() ||
486          II.hadMacroDefinition() ||
487          II.getFETokenInfo<void>();
488 }
489 
490 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
491                                                    const unsigned char* d,
492                                                    unsigned DataLen) {
493   using namespace clang::io;
494   unsigned RawID = ReadUnalignedLE32(d);
495   bool IsInteresting = RawID & 0x01;
496 
497   // Wipe out the "is interesting" bit.
498   RawID = RawID >> 1;
499 
500   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
501   if (!IsInteresting) {
502     // For uninteresting identifiers, just build the IdentifierInfo
503     // and associate it with the persistent ID.
504     IdentifierInfo *II = KnownII;
505     if (!II) {
506       II = &Reader.getIdentifierTable().getOwn(k);
507       KnownII = II;
508     }
509     Reader.SetIdentifierInfo(ID, II);
510     if (!II->isFromAST()) {
511       bool WasInteresting = isInterestingIdentifier(*II);
512       II->setIsFromAST();
513       if (WasInteresting)
514         II->setChangedSinceDeserialization();
515     }
516     Reader.markIdentifierUpToDate(II);
517     return II;
518   }
519 
520   unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
521   unsigned Bits = ReadUnalignedLE16(d);
522   bool CPlusPlusOperatorKeyword = Bits & 0x01;
523   Bits >>= 1;
524   bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
525   Bits >>= 1;
526   bool Poisoned = Bits & 0x01;
527   Bits >>= 1;
528   bool ExtensionToken = Bits & 0x01;
529   Bits >>= 1;
530   bool hasSubmoduleMacros = Bits & 0x01;
531   Bits >>= 1;
532   bool hadMacroDefinition = Bits & 0x01;
533   Bits >>= 1;
534 
535   assert(Bits == 0 && "Extra bits in the identifier?");
536   DataLen -= 8;
537 
538   // Build the IdentifierInfo itself and link the identifier ID with
539   // the new IdentifierInfo.
540   IdentifierInfo *II = KnownII;
541   if (!II) {
542     II = &Reader.getIdentifierTable().getOwn(StringRef(k));
543     KnownII = II;
544   }
545   Reader.markIdentifierUpToDate(II);
546   if (!II->isFromAST()) {
547     bool WasInteresting = isInterestingIdentifier(*II);
548     II->setIsFromAST();
549     if (WasInteresting)
550       II->setChangedSinceDeserialization();
551   }
552 
553   // Set or check the various bits in the IdentifierInfo structure.
554   // Token IDs are read-only.
555   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
556     II->RevertTokenIDToIdentifier();
557   II->setObjCOrBuiltinID(ObjCOrBuiltinID);
558   assert(II->isExtensionToken() == ExtensionToken &&
559          "Incorrect extension token flag");
560   (void)ExtensionToken;
561   if (Poisoned)
562     II->setIsPoisoned(true);
563   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
564          "Incorrect C++ operator keyword flag");
565   (void)CPlusPlusOperatorKeyword;
566 
567   // If this identifier is a macro, deserialize the macro
568   // definition.
569   if (hadMacroDefinition) {
570     uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
571     DataLen -= 4;
572     SmallVector<uint32_t, 8> LocalMacroIDs;
573     if (hasSubmoduleMacros) {
574       while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
575         DataLen -= 4;
576         LocalMacroIDs.push_back(LocalMacroID);
577       }
578       DataLen -= 4;
579     }
580 
581     if (F.Kind == MK_Module) {
582       for (SmallVectorImpl<uint32_t>::iterator
583              I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
584         MacroID MacID = Reader.getGlobalMacroID(F, *I);
585         Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
586       }
587     } else {
588       Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
589     }
590   }
591 
592   Reader.SetIdentifierInfo(ID, II);
593 
594   // Read all of the declarations visible at global scope with this
595   // name.
596   if (DataLen > 0) {
597     SmallVector<uint32_t, 4> DeclIDs;
598     for (; DataLen > 0; DataLen -= 4)
599       DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
600     Reader.SetGloballyVisibleDecls(II, DeclIDs);
601   }
602 
603   return II;
604 }
605 
606 unsigned
607 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
608   llvm::FoldingSetNodeID ID;
609   ID.AddInteger(Key.Kind);
610 
611   switch (Key.Kind) {
612   case DeclarationName::Identifier:
613   case DeclarationName::CXXLiteralOperatorName:
614     ID.AddString(((IdentifierInfo*)Key.Data)->getName());
615     break;
616   case DeclarationName::ObjCZeroArgSelector:
617   case DeclarationName::ObjCOneArgSelector:
618   case DeclarationName::ObjCMultiArgSelector:
619     ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
620     break;
621   case DeclarationName::CXXOperatorName:
622     ID.AddInteger((OverloadedOperatorKind)Key.Data);
623     break;
624   case DeclarationName::CXXConstructorName:
625   case DeclarationName::CXXDestructorName:
626   case DeclarationName::CXXConversionFunctionName:
627   case DeclarationName::CXXUsingDirective:
628     break;
629   }
630 
631   return ID.ComputeHash();
632 }
633 
634 ASTDeclContextNameLookupTrait::internal_key_type
635 ASTDeclContextNameLookupTrait::GetInternalKey(
636                                           const external_key_type& Name) const {
637   DeclNameKey Key;
638   Key.Kind = Name.getNameKind();
639   switch (Name.getNameKind()) {
640   case DeclarationName::Identifier:
641     Key.Data = (uint64_t)Name.getAsIdentifierInfo();
642     break;
643   case DeclarationName::ObjCZeroArgSelector:
644   case DeclarationName::ObjCOneArgSelector:
645   case DeclarationName::ObjCMultiArgSelector:
646     Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
647     break;
648   case DeclarationName::CXXOperatorName:
649     Key.Data = Name.getCXXOverloadedOperator();
650     break;
651   case DeclarationName::CXXLiteralOperatorName:
652     Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
653     break;
654   case DeclarationName::CXXConstructorName:
655   case DeclarationName::CXXDestructorName:
656   case DeclarationName::CXXConversionFunctionName:
657   case DeclarationName::CXXUsingDirective:
658     Key.Data = 0;
659     break;
660   }
661 
662   return Key;
663 }
664 
665 std::pair<unsigned, unsigned>
666 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
667   using namespace clang::io;
668   unsigned KeyLen = ReadUnalignedLE16(d);
669   unsigned DataLen = ReadUnalignedLE16(d);
670   return std::make_pair(KeyLen, DataLen);
671 }
672 
673 ASTDeclContextNameLookupTrait::internal_key_type
674 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
675   using namespace clang::io;
676 
677   DeclNameKey Key;
678   Key.Kind = (DeclarationName::NameKind)*d++;
679   switch (Key.Kind) {
680   case DeclarationName::Identifier:
681     Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
682     break;
683   case DeclarationName::ObjCZeroArgSelector:
684   case DeclarationName::ObjCOneArgSelector:
685   case DeclarationName::ObjCMultiArgSelector:
686     Key.Data =
687        (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
688                    .getAsOpaquePtr();
689     break;
690   case DeclarationName::CXXOperatorName:
691     Key.Data = *d++; // OverloadedOperatorKind
692     break;
693   case DeclarationName::CXXLiteralOperatorName:
694     Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
695     break;
696   case DeclarationName::CXXConstructorName:
697   case DeclarationName::CXXDestructorName:
698   case DeclarationName::CXXConversionFunctionName:
699   case DeclarationName::CXXUsingDirective:
700     Key.Data = 0;
701     break;
702   }
703 
704   return Key;
705 }
706 
707 ASTDeclContextNameLookupTrait::data_type
708 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
709                                         const unsigned char* d,
710                                         unsigned DataLen) {
711   using namespace clang::io;
712   unsigned NumDecls = ReadUnalignedLE16(d);
713   LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
714                         const_cast<unsigned char *>(d));
715   return std::make_pair(Start, Start + NumDecls);
716 }
717 
718 bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
719                                        BitstreamCursor &Cursor,
720                                    const std::pair<uint64_t, uint64_t> &Offsets,
721                                        DeclContextInfo &Info) {
722   SavedStreamPosition SavedPosition(Cursor);
723   // First the lexical decls.
724   if (Offsets.first != 0) {
725     Cursor.JumpToBit(Offsets.first);
726 
727     RecordData Record;
728     StringRef Blob;
729     unsigned Code = Cursor.ReadCode();
730     unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
731     if (RecCode != DECL_CONTEXT_LEXICAL) {
732       Error("Expected lexical block");
733       return true;
734     }
735 
736     Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
737     Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
738   }
739 
740   // Now the lookup table.
741   if (Offsets.second != 0) {
742     Cursor.JumpToBit(Offsets.second);
743 
744     RecordData Record;
745     StringRef Blob;
746     unsigned Code = Cursor.ReadCode();
747     unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
748     if (RecCode != DECL_CONTEXT_VISIBLE) {
749       Error("Expected visible lookup table block");
750       return true;
751     }
752     Info.NameLookupTableData
753       = ASTDeclContextNameLookupTable::Create(
754                     (const unsigned char *)Blob.data() + Record[0],
755                     (const unsigned char *)Blob.data(),
756                     ASTDeclContextNameLookupTrait(*this, M));
757   }
758 
759   return false;
760 }
761 
762 void ASTReader::Error(StringRef Msg) {
763   Error(diag::err_fe_pch_malformed, Msg);
764   if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
765     Diag(diag::note_module_cache_path)
766       << PP.getHeaderSearchInfo().getModuleCachePath();
767   }
768 }
769 
770 void ASTReader::Error(unsigned DiagID,
771                       StringRef Arg1, StringRef Arg2) {
772   if (Diags.isDiagnosticInFlight())
773     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
774   else
775     Diag(DiagID) << Arg1 << Arg2;
776 }
777 
778 //===----------------------------------------------------------------------===//
779 // Source Manager Deserialization
780 //===----------------------------------------------------------------------===//
781 
782 /// \brief Read the line table in the source manager block.
783 /// \returns true if there was an error.
784 bool ASTReader::ParseLineTable(ModuleFile &F,
785                                SmallVectorImpl<uint64_t> &Record) {
786   unsigned Idx = 0;
787   LineTableInfo &LineTable = SourceMgr.getLineTable();
788 
789   // Parse the file names
790   std::map<int, int> FileIDs;
791   for (int I = 0, N = Record[Idx++]; I != N; ++I) {
792     // Extract the file name
793     unsigned FilenameLen = Record[Idx++];
794     std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
795     Idx += FilenameLen;
796     MaybeAddSystemRootToFilename(F, Filename);
797     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
798   }
799 
800   // Parse the line entries
801   std::vector<LineEntry> Entries;
802   while (Idx < Record.size()) {
803     int FID = Record[Idx++];
804     assert(FID >= 0 && "Serialized line entries for non-local file.");
805     // Remap FileID from 1-based old view.
806     FID += F.SLocEntryBaseID - 1;
807 
808     // Extract the line entries
809     unsigned NumEntries = Record[Idx++];
810     assert(NumEntries && "Numentries is 00000");
811     Entries.clear();
812     Entries.reserve(NumEntries);
813     for (unsigned I = 0; I != NumEntries; ++I) {
814       unsigned FileOffset = Record[Idx++];
815       unsigned LineNo = Record[Idx++];
816       int FilenameID = FileIDs[Record[Idx++]];
817       SrcMgr::CharacteristicKind FileKind
818         = (SrcMgr::CharacteristicKind)Record[Idx++];
819       unsigned IncludeOffset = Record[Idx++];
820       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
821                                        FileKind, IncludeOffset));
822     }
823     LineTable.AddEntry(FileID::get(FID), Entries);
824   }
825 
826   return false;
827 }
828 
829 /// \brief Read a source manager block
830 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
831   using namespace SrcMgr;
832 
833   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
834 
835   // Set the source-location entry cursor to the current position in
836   // the stream. This cursor will be used to read the contents of the
837   // source manager block initially, and then lazily read
838   // source-location entries as needed.
839   SLocEntryCursor = F.Stream;
840 
841   // The stream itself is going to skip over the source manager block.
842   if (F.Stream.SkipBlock()) {
843     Error("malformed block record in AST file");
844     return true;
845   }
846 
847   // Enter the source manager block.
848   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
849     Error("malformed source manager block record in AST file");
850     return true;
851   }
852 
853   RecordData Record;
854   while (true) {
855     llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
856 
857     switch (E.Kind) {
858     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
859     case llvm::BitstreamEntry::Error:
860       Error("malformed block record in AST file");
861       return true;
862     case llvm::BitstreamEntry::EndBlock:
863       return false;
864     case llvm::BitstreamEntry::Record:
865       // The interesting case.
866       break;
867     }
868 
869     // Read a record.
870     Record.clear();
871     StringRef Blob;
872     switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
873     default:  // Default behavior: ignore.
874       break;
875 
876     case SM_SLOC_FILE_ENTRY:
877     case SM_SLOC_BUFFER_ENTRY:
878     case SM_SLOC_EXPANSION_ENTRY:
879       // Once we hit one of the source location entries, we're done.
880       return false;
881     }
882   }
883 }
884 
885 /// \brief If a header file is not found at the path that we expect it to be
886 /// and the PCH file was moved from its original location, try to resolve the
887 /// file by assuming that header+PCH were moved together and the header is in
888 /// the same place relative to the PCH.
889 static std::string
890 resolveFileRelativeToOriginalDir(const std::string &Filename,
891                                  const std::string &OriginalDir,
892                                  const std::string &CurrDir) {
893   assert(OriginalDir != CurrDir &&
894          "No point trying to resolve the file if the PCH dir didn't change");
895   using namespace llvm::sys;
896   SmallString<128> filePath(Filename);
897   fs::make_absolute(filePath);
898   assert(path::is_absolute(OriginalDir));
899   SmallString<128> currPCHPath(CurrDir);
900 
901   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
902                        fileDirE = path::end(path::parent_path(filePath));
903   path::const_iterator origDirI = path::begin(OriginalDir),
904                        origDirE = path::end(OriginalDir);
905   // Skip the common path components from filePath and OriginalDir.
906   while (fileDirI != fileDirE && origDirI != origDirE &&
907          *fileDirI == *origDirI) {
908     ++fileDirI;
909     ++origDirI;
910   }
911   for (; origDirI != origDirE; ++origDirI)
912     path::append(currPCHPath, "..");
913   path::append(currPCHPath, fileDirI, fileDirE);
914   path::append(currPCHPath, path::filename(Filename));
915   return currPCHPath.str();
916 }
917 
918 bool ASTReader::ReadSLocEntry(int ID) {
919   if (ID == 0)
920     return false;
921 
922   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
923     Error("source location entry ID out-of-range for AST file");
924     return true;
925   }
926 
927   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
928   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
929   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
930   unsigned BaseOffset = F->SLocEntryBaseOffset;
931 
932   ++NumSLocEntriesRead;
933   llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
934   if (Entry.Kind != llvm::BitstreamEntry::Record) {
935     Error("incorrectly-formatted source location entry in AST file");
936     return true;
937   }
938 
939   RecordData Record;
940   StringRef Blob;
941   switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
942   default:
943     Error("incorrectly-formatted source location entry in AST file");
944     return true;
945 
946   case SM_SLOC_FILE_ENTRY: {
947     // We will detect whether a file changed and return 'Failure' for it, but
948     // we will also try to fail gracefully by setting up the SLocEntry.
949     unsigned InputID = Record[4];
950     InputFile IF = getInputFile(*F, InputID);
951     const FileEntry *File = IF.getFile();
952     bool OverriddenBuffer = IF.isOverridden();
953 
954     // Note that we only check if a File was returned. If it was out-of-date
955     // we have complained but we will continue creating a FileID to recover
956     // gracefully.
957     if (!File)
958       return true;
959 
960     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
961     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
962       // This is the module's main file.
963       IncludeLoc = getImportLocation(F);
964     }
965     SrcMgr::CharacteristicKind
966       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
967     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
968                                         ID, BaseOffset + Record[0]);
969     SrcMgr::FileInfo &FileInfo =
970           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
971     FileInfo.NumCreatedFIDs = Record[5];
972     if (Record[3])
973       FileInfo.setHasLineDirectives();
974 
975     const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
976     unsigned NumFileDecls = Record[7];
977     if (NumFileDecls) {
978       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
979       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
980                                                              NumFileDecls));
981     }
982 
983     const SrcMgr::ContentCache *ContentCache
984       = SourceMgr.getOrCreateContentCache(File,
985                               /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
986     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
987         ContentCache->ContentsEntry == ContentCache->OrigEntry) {
988       unsigned Code = SLocEntryCursor.ReadCode();
989       Record.clear();
990       unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
991 
992       if (RecCode != SM_SLOC_BUFFER_BLOB) {
993         Error("AST record has invalid code");
994         return true;
995       }
996 
997       llvm::MemoryBuffer *Buffer
998         = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
999       SourceMgr.overrideFileContents(File, Buffer);
1000     }
1001 
1002     break;
1003   }
1004 
1005   case SM_SLOC_BUFFER_ENTRY: {
1006     const char *Name = Blob.data();
1007     unsigned Offset = Record[0];
1008     SrcMgr::CharacteristicKind
1009       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1010     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1011     if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1012       IncludeLoc = getImportLocation(F);
1013     }
1014     unsigned Code = SLocEntryCursor.ReadCode();
1015     Record.clear();
1016     unsigned RecCode
1017       = SLocEntryCursor.readRecord(Code, Record, &Blob);
1018 
1019     if (RecCode != SM_SLOC_BUFFER_BLOB) {
1020       Error("AST record has invalid code");
1021       return true;
1022     }
1023 
1024     llvm::MemoryBuffer *Buffer
1025       = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
1026     SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1027                                        BaseOffset + Offset, IncludeLoc);
1028     break;
1029   }
1030 
1031   case SM_SLOC_EXPANSION_ENTRY: {
1032     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1033     SourceMgr.createExpansionLoc(SpellingLoc,
1034                                      ReadSourceLocation(*F, Record[2]),
1035                                      ReadSourceLocation(*F, Record[3]),
1036                                      Record[4],
1037                                      ID,
1038                                      BaseOffset + Record[0]);
1039     break;
1040   }
1041   }
1042 
1043   return false;
1044 }
1045 
1046 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1047   if (ID == 0)
1048     return std::make_pair(SourceLocation(), "");
1049 
1050   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1051     Error("source location entry ID out-of-range for AST file");
1052     return std::make_pair(SourceLocation(), "");
1053   }
1054 
1055   // Find which module file this entry lands in.
1056   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1057   if (M->Kind != MK_Module)
1058     return std::make_pair(SourceLocation(), "");
1059 
1060   // FIXME: Can we map this down to a particular submodule? That would be
1061   // ideal.
1062   return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1063 }
1064 
1065 /// \brief Find the location where the module F is imported.
1066 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1067   if (F->ImportLoc.isValid())
1068     return F->ImportLoc;
1069 
1070   // Otherwise we have a PCH. It's considered to be "imported" at the first
1071   // location of its includer.
1072   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1073     // Main file is the importer. We assume that it is the first entry in the
1074     // entry table. We can't ask the manager, because at the time of PCH loading
1075     // the main file entry doesn't exist yet.
1076     // The very first entry is the invalid instantiation loc, which takes up
1077     // offsets 0 and 1.
1078     return SourceLocation::getFromRawEncoding(2U);
1079   }
1080   //return F->Loaders[0]->FirstLoc;
1081   return F->ImportedBy[0]->FirstLoc;
1082 }
1083 
1084 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1085 /// specified cursor.  Read the abbreviations that are at the top of the block
1086 /// and then leave the cursor pointing into the block.
1087 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
1088   if (Cursor.EnterSubBlock(BlockID)) {
1089     Error("malformed block record in AST file");
1090     return Failure;
1091   }
1092 
1093   while (true) {
1094     uint64_t Offset = Cursor.GetCurrentBitNo();
1095     unsigned Code = Cursor.ReadCode();
1096 
1097     // We expect all abbrevs to be at the start of the block.
1098     if (Code != llvm::bitc::DEFINE_ABBREV) {
1099       Cursor.JumpToBit(Offset);
1100       return false;
1101     }
1102     Cursor.ReadAbbrevRecord();
1103   }
1104 }
1105 
1106 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1107                            unsigned &Idx) {
1108   Token Tok;
1109   Tok.startToken();
1110   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1111   Tok.setLength(Record[Idx++]);
1112   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1113     Tok.setIdentifierInfo(II);
1114   Tok.setKind((tok::TokenKind)Record[Idx++]);
1115   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1116   return Tok;
1117 }
1118 
1119 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1120   BitstreamCursor &Stream = F.MacroCursor;
1121 
1122   // Keep track of where we are in the stream, then jump back there
1123   // after reading this macro.
1124   SavedStreamPosition SavedPosition(Stream);
1125 
1126   Stream.JumpToBit(Offset);
1127   RecordData Record;
1128   SmallVector<IdentifierInfo*, 16> MacroArgs;
1129   MacroInfo *Macro = 0;
1130 
1131   while (true) {
1132     // Advance to the next record, but if we get to the end of the block, don't
1133     // pop it (removing all the abbreviations from the cursor) since we want to
1134     // be able to reseek within the block and read entries.
1135     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1136     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1137 
1138     switch (Entry.Kind) {
1139     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1140     case llvm::BitstreamEntry::Error:
1141       Error("malformed block record in AST file");
1142       return Macro;
1143     case llvm::BitstreamEntry::EndBlock:
1144       return Macro;
1145     case llvm::BitstreamEntry::Record:
1146       // The interesting case.
1147       break;
1148     }
1149 
1150     // Read a record.
1151     Record.clear();
1152     PreprocessorRecordTypes RecType =
1153       (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
1154     switch (RecType) {
1155     case PP_MACRO_DIRECTIVE_HISTORY:
1156       return Macro;
1157 
1158     case PP_MACRO_OBJECT_LIKE:
1159     case PP_MACRO_FUNCTION_LIKE: {
1160       // If we already have a macro, that means that we've hit the end
1161       // of the definition of the macro we were looking for. We're
1162       // done.
1163       if (Macro)
1164         return Macro;
1165 
1166       unsigned NextIndex = 1; // Skip identifier ID.
1167       SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
1168       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1169       MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
1170       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1171       MI->setIsUsed(Record[NextIndex++]);
1172 
1173       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1174         // Decode function-like macro info.
1175         bool isC99VarArgs = Record[NextIndex++];
1176         bool isGNUVarArgs = Record[NextIndex++];
1177         bool hasCommaPasting = Record[NextIndex++];
1178         MacroArgs.clear();
1179         unsigned NumArgs = Record[NextIndex++];
1180         for (unsigned i = 0; i != NumArgs; ++i)
1181           MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1182 
1183         // Install function-like macro info.
1184         MI->setIsFunctionLike();
1185         if (isC99VarArgs) MI->setIsC99Varargs();
1186         if (isGNUVarArgs) MI->setIsGNUVarargs();
1187         if (hasCommaPasting) MI->setHasCommaPasting();
1188         MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1189                             PP.getPreprocessorAllocator());
1190       }
1191 
1192       // Remember that we saw this macro last so that we add the tokens that
1193       // form its body to it.
1194       Macro = MI;
1195 
1196       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1197           Record[NextIndex]) {
1198         // We have a macro definition. Register the association
1199         PreprocessedEntityID
1200             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1201         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1202         PreprocessingRecord::PPEntityID
1203           PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1204         MacroDefinition *PPDef =
1205           cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1206         if (PPDef)
1207           PPRec.RegisterMacroDefinition(Macro, PPDef);
1208       }
1209 
1210       ++NumMacrosRead;
1211       break;
1212     }
1213 
1214     case PP_TOKEN: {
1215       // If we see a TOKEN before a PP_MACRO_*, then the file is
1216       // erroneous, just pretend we didn't see this.
1217       if (Macro == 0) break;
1218 
1219       unsigned Idx = 0;
1220       Token Tok = ReadToken(F, Record, Idx);
1221       Macro->AddTokenToBody(Tok);
1222       break;
1223     }
1224     }
1225   }
1226 }
1227 
1228 PreprocessedEntityID
1229 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1230   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1231     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1232   assert(I != M.PreprocessedEntityRemap.end()
1233          && "Invalid index into preprocessed entity index remap");
1234 
1235   return LocalID + I->second;
1236 }
1237 
1238 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1239   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1240 }
1241 
1242 HeaderFileInfoTrait::internal_key_type
1243 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1244   internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1245                              FE->getName() };
1246   return ikey;
1247 }
1248 
1249 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1250   if (a.Size != b.Size || a.ModTime != b.ModTime)
1251     return false;
1252 
1253   if (strcmp(a.Filename, b.Filename) == 0)
1254     return true;
1255 
1256   // Determine whether the actual files are equivalent.
1257   FileManager &FileMgr = Reader.getFileManager();
1258   const FileEntry *FEA = FileMgr.getFile(a.Filename);
1259   const FileEntry *FEB = FileMgr.getFile(b.Filename);
1260   return (FEA && FEA == FEB);
1261 }
1262 
1263 std::pair<unsigned, unsigned>
1264 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1265   unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1266   unsigned DataLen = (unsigned) *d++;
1267   return std::make_pair(KeyLen, DataLen);
1268 }
1269 
1270 HeaderFileInfoTrait::internal_key_type
1271 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1272   internal_key_type ikey;
1273   ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1274   ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1275   ikey.Filename = (const char *)d;
1276   return ikey;
1277 }
1278 
1279 HeaderFileInfoTrait::data_type
1280 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1281                               unsigned DataLen) {
1282   const unsigned char *End = d + DataLen;
1283   using namespace clang::io;
1284   HeaderFileInfo HFI;
1285   unsigned Flags = *d++;
1286   HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1287                    ((Flags >> 6) & 0x03);
1288   HFI.isImport = (Flags >> 5) & 0x01;
1289   HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1290   HFI.DirInfo = (Flags >> 2) & 0x03;
1291   HFI.Resolved = (Flags >> 1) & 0x01;
1292   HFI.IndexHeaderMapHeader = Flags & 0x01;
1293   HFI.NumIncludes = ReadUnalignedLE16(d);
1294   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1295                                                         ReadUnalignedLE32(d));
1296   if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1297     // The framework offset is 1 greater than the actual offset,
1298     // since 0 is used as an indicator for "no framework name".
1299     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1300     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1301   }
1302 
1303   if (d != End) {
1304     uint32_t LocalSMID = ReadUnalignedLE32(d);
1305     if (LocalSMID) {
1306       // This header is part of a module. Associate it with the module to enable
1307       // implicit module import.
1308       SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1309       Module *Mod = Reader.getSubmodule(GlobalSMID);
1310       HFI.isModuleHeader = true;
1311       FileManager &FileMgr = Reader.getFileManager();
1312       ModuleMap &ModMap =
1313           Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1314       ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
1315     }
1316   }
1317 
1318   assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1319   (void)End;
1320 
1321   // This HeaderFileInfo was externally loaded.
1322   HFI.External = true;
1323   return HFI;
1324 }
1325 
1326 void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1327                                           ModuleFile *M,
1328                                           GlobalMacroID GMacID,
1329                                           SourceLocation ImportLoc) {
1330   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1331   PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1332 }
1333 
1334 void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1335                                        ModuleFile *M,
1336                                        uint64_t MacroDirectivesOffset) {
1337   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1338   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1339 }
1340 
1341 void ASTReader::ReadDefinedMacros() {
1342   // Note that we are loading defined macros.
1343   Deserializing Macros(this);
1344 
1345   for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1346       E = ModuleMgr.rend(); I != E; ++I) {
1347     BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1348 
1349     // If there was no preprocessor block, skip this file.
1350     if (!MacroCursor.getBitStreamReader())
1351       continue;
1352 
1353     BitstreamCursor Cursor = MacroCursor;
1354     Cursor.JumpToBit((*I)->MacroStartOffset);
1355 
1356     RecordData Record;
1357     while (true) {
1358       llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1359 
1360       switch (E.Kind) {
1361       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1362       case llvm::BitstreamEntry::Error:
1363         Error("malformed block record in AST file");
1364         return;
1365       case llvm::BitstreamEntry::EndBlock:
1366         goto NextCursor;
1367 
1368       case llvm::BitstreamEntry::Record:
1369         Record.clear();
1370         switch (Cursor.readRecord(E.ID, Record)) {
1371         default:  // Default behavior: ignore.
1372           break;
1373 
1374         case PP_MACRO_OBJECT_LIKE:
1375         case PP_MACRO_FUNCTION_LIKE:
1376           getLocalIdentifier(**I, Record[0]);
1377           break;
1378 
1379         case PP_TOKEN:
1380           // Ignore tokens.
1381           break;
1382         }
1383         break;
1384       }
1385     }
1386     NextCursor:  ;
1387   }
1388 }
1389 
1390 namespace {
1391   /// \brief Visitor class used to look up identifirs in an AST file.
1392   class IdentifierLookupVisitor {
1393     StringRef Name;
1394     unsigned PriorGeneration;
1395     unsigned &NumIdentifierLookups;
1396     unsigned &NumIdentifierLookupHits;
1397     IdentifierInfo *Found;
1398 
1399   public:
1400     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1401                             unsigned &NumIdentifierLookups,
1402                             unsigned &NumIdentifierLookupHits)
1403       : Name(Name), PriorGeneration(PriorGeneration),
1404         NumIdentifierLookups(NumIdentifierLookups),
1405         NumIdentifierLookupHits(NumIdentifierLookupHits),
1406         Found()
1407     {
1408     }
1409 
1410     static bool visit(ModuleFile &M, void *UserData) {
1411       IdentifierLookupVisitor *This
1412         = static_cast<IdentifierLookupVisitor *>(UserData);
1413 
1414       // If we've already searched this module file, skip it now.
1415       if (M.Generation <= This->PriorGeneration)
1416         return true;
1417 
1418       ASTIdentifierLookupTable *IdTable
1419         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1420       if (!IdTable)
1421         return false;
1422 
1423       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1424                                      M, This->Found);
1425       ++This->NumIdentifierLookups;
1426       ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
1427       if (Pos == IdTable->end())
1428         return false;
1429 
1430       // Dereferencing the iterator has the effect of building the
1431       // IdentifierInfo node and populating it with the various
1432       // declarations it needs.
1433       ++This->NumIdentifierLookupHits;
1434       This->Found = *Pos;
1435       return true;
1436     }
1437 
1438     // \brief Retrieve the identifier info found within the module
1439     // files.
1440     IdentifierInfo *getIdentifierInfo() const { return Found; }
1441   };
1442 }
1443 
1444 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1445   // Note that we are loading an identifier.
1446   Deserializing AnIdentifier(this);
1447 
1448   unsigned PriorGeneration = 0;
1449   if (getContext().getLangOpts().Modules)
1450     PriorGeneration = IdentifierGeneration[&II];
1451 
1452   // If there is a global index, look there first to determine which modules
1453   // provably do not have any results for this identifier.
1454   GlobalModuleIndex::HitSet Hits;
1455   GlobalModuleIndex::HitSet *HitsPtr = 0;
1456   if (!loadGlobalIndex()) {
1457     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1458       HitsPtr = &Hits;
1459     }
1460   }
1461 
1462   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
1463                                   NumIdentifierLookups,
1464                                   NumIdentifierLookupHits);
1465   ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
1466   markIdentifierUpToDate(&II);
1467 }
1468 
1469 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1470   if (!II)
1471     return;
1472 
1473   II->setOutOfDate(false);
1474 
1475   // Update the generation for this identifier.
1476   if (getContext().getLangOpts().Modules)
1477     IdentifierGeneration[II] = CurrentGeneration;
1478 }
1479 
1480 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1481                                     const PendingMacroInfo &PMInfo) {
1482   assert(II);
1483 
1484   if (PMInfo.M->Kind != MK_Module) {
1485     installPCHMacroDirectives(II, *PMInfo.M,
1486                               PMInfo.PCHMacroData.MacroDirectivesOffset);
1487     return;
1488   }
1489 
1490   // Module Macro.
1491 
1492   GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1493   SourceLocation ImportLoc =
1494       SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1495 
1496   assert(GMacID);
1497   // If this macro has already been loaded, don't do so again.
1498   if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1499     return;
1500 
1501   MacroInfo *MI = getMacro(GMacID);
1502   SubmoduleID SubModID = MI->getOwningModuleID();
1503   MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1504                                                     /*isImported=*/true);
1505 
1506   // Determine whether this macro definition is visible.
1507   bool Hidden = false;
1508   Module *Owner = 0;
1509   if (SubModID) {
1510     if ((Owner = getSubmodule(SubModID))) {
1511       if (Owner->NameVisibility == Module::Hidden) {
1512         // The owning module is not visible, and this macro definition
1513         // should not be, either.
1514         Hidden = true;
1515 
1516         // Note that this macro definition was hidden because its owning
1517         // module is not yet visible.
1518         HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1519       }
1520     }
1521   }
1522 
1523   if (!Hidden)
1524     installImportedMacro(II, MD, Owner);
1525 }
1526 
1527 void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1528                                           ModuleFile &M, uint64_t Offset) {
1529   assert(M.Kind != MK_Module);
1530 
1531   BitstreamCursor &Cursor = M.MacroCursor;
1532   SavedStreamPosition SavedPosition(Cursor);
1533   Cursor.JumpToBit(Offset);
1534 
1535   llvm::BitstreamEntry Entry =
1536       Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1537   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1538     Error("malformed block record in AST file");
1539     return;
1540   }
1541 
1542   RecordData Record;
1543   PreprocessorRecordTypes RecType =
1544     (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1545   if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1546     Error("malformed block record in AST file");
1547     return;
1548   }
1549 
1550   // Deserialize the macro directives history in reverse source-order.
1551   MacroDirective *Latest = 0, *Earliest = 0;
1552   unsigned Idx = 0, N = Record.size();
1553   while (Idx < N) {
1554     MacroDirective *MD = 0;
1555     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
1556     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1557     switch (K) {
1558     case MacroDirective::MD_Define: {
1559       GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1560       MacroInfo *MI = getMacro(GMacID);
1561       bool isImported = Record[Idx++];
1562       bool isAmbiguous = Record[Idx++];
1563       DefMacroDirective *DefMD =
1564           PP.AllocateDefMacroDirective(MI, Loc, isImported);
1565       DefMD->setAmbiguous(isAmbiguous);
1566       MD = DefMD;
1567       break;
1568     }
1569     case MacroDirective::MD_Undefine:
1570       MD = PP.AllocateUndefMacroDirective(Loc);
1571       break;
1572     case MacroDirective::MD_Visibility: {
1573       bool isPublic = Record[Idx++];
1574       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1575       break;
1576     }
1577     }
1578 
1579     if (!Latest)
1580       Latest = MD;
1581     if (Earliest)
1582       Earliest->setPrevious(MD);
1583     Earliest = MD;
1584   }
1585 
1586   PP.setLoadedMacroDirective(II, Latest);
1587 }
1588 
1589 /// \brief For the given macro definitions, check if they are both in system
1590 /// modules.
1591 static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1592                                       Module *NewOwner, ASTReader &Reader) {
1593   assert(PrevMI && NewMI);
1594   Module *PrevOwner = 0;
1595   if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1596     PrevOwner = Reader.getSubmodule(PrevModID);
1597   SourceManager &SrcMgr = Reader.getSourceManager();
1598   bool PrevInSystem
1599     = PrevOwner? PrevOwner->IsSystem
1600                : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1601   bool NewInSystem
1602     = NewOwner? NewOwner->IsSystem
1603               : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1604   if (PrevOwner && PrevOwner == NewOwner)
1605     return false;
1606   return PrevInSystem && NewInSystem;
1607 }
1608 
1609 void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1610                                      Module *Owner) {
1611   assert(II && MD);
1612 
1613   DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
1614   MacroDirective *Prev = PP.getMacroDirective(II);
1615   if (Prev) {
1616     MacroDirective::DefInfo PrevDef = Prev->getDefinition();
1617     MacroInfo *PrevMI = PrevDef.getMacroInfo();
1618     MacroInfo *NewMI = DefMD->getInfo();
1619     if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1620                                                   /*Syntactically=*/true)) {
1621       // Before marking the macros as ambiguous, check if this is a case where
1622       // both macros are in system headers. If so, we trust that the system
1623       // did not get it wrong. This also handles cases where Clang's own
1624       // headers have a different spelling of certain system macros:
1625       //   #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1626       //   #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1627       if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
1628         PrevDef.getDirective()->setAmbiguous(true);
1629         DefMD->setAmbiguous(true);
1630       }
1631     }
1632   }
1633 
1634   PP.appendMacroDirective(II, MD);
1635 }
1636 
1637 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1638   // If this ID is bogus, just return an empty input file.
1639   if (ID == 0 || ID > F.InputFilesLoaded.size())
1640     return InputFile();
1641 
1642   // If we've already loaded this input file, return it.
1643   if (F.InputFilesLoaded[ID-1].getFile())
1644     return F.InputFilesLoaded[ID-1];
1645 
1646   if (F.InputFilesLoaded[ID-1].isNotFound())
1647     return InputFile();
1648 
1649   // Go find this input file.
1650   BitstreamCursor &Cursor = F.InputFilesCursor;
1651   SavedStreamPosition SavedPosition(Cursor);
1652   Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1653 
1654   unsigned Code = Cursor.ReadCode();
1655   RecordData Record;
1656   StringRef Blob;
1657   switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
1658   case INPUT_FILE: {
1659     unsigned StoredID = Record[0];
1660     assert(ID == StoredID && "Bogus stored ID or offset");
1661     (void)StoredID;
1662     off_t StoredSize = (off_t)Record[1];
1663     time_t StoredTime = (time_t)Record[2];
1664     bool Overridden = (bool)Record[3];
1665 
1666     // Get the file entry for this input file.
1667     StringRef OrigFilename = Blob;
1668     std::string Filename = OrigFilename;
1669     MaybeAddSystemRootToFilename(F, Filename);
1670     const FileEntry *File
1671       = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1672                   : FileMgr.getFile(Filename, /*OpenFile=*/false);
1673 
1674     // If we didn't find the file, resolve it relative to the
1675     // original directory from which this AST file was created.
1676     if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1677         F.OriginalDir != CurrentDir) {
1678       std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1679                                                               F.OriginalDir,
1680                                                               CurrentDir);
1681       if (!Resolved.empty())
1682         File = FileMgr.getFile(Resolved);
1683     }
1684 
1685     // For an overridden file, create a virtual file with the stored
1686     // size/timestamp.
1687     if (Overridden && File == 0) {
1688       File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1689     }
1690 
1691     if (File == 0) {
1692       if (Complain) {
1693         std::string ErrorStr = "could not find file '";
1694         ErrorStr += Filename;
1695         ErrorStr += "' referenced by AST file";
1696         Error(ErrorStr.c_str());
1697       }
1698       // Record that we didn't find the file.
1699       F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1700       return InputFile();
1701     }
1702 
1703     // Check if there was a request to override the contents of the file
1704     // that was part of the precompiled header. Overridding such a file
1705     // can lead to problems when lexing using the source locations from the
1706     // PCH.
1707     SourceManager &SM = getSourceManager();
1708     if (!Overridden && SM.isFileOverridden(File)) {
1709       if (Complain)
1710         Error(diag::err_fe_pch_file_overridden, Filename);
1711       // After emitting the diagnostic, recover by disabling the override so
1712       // that the original file will be used.
1713       SM.disableFileContentsOverride(File);
1714       // The FileEntry is a virtual file entry with the size of the contents
1715       // that would override the original contents. Set it to the original's
1716       // size/time.
1717       FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1718                               StoredSize, StoredTime);
1719     }
1720 
1721     bool IsOutOfDate = false;
1722 
1723     // For an overridden file, there is nothing to validate.
1724     if (!Overridden && (StoredSize != File->getSize()
1725 #if !defined(LLVM_ON_WIN32)
1726          // In our regression testing, the Windows file system seems to
1727          // have inconsistent modification times that sometimes
1728          // erroneously trigger this error-handling path.
1729          || StoredTime != File->getModificationTime()
1730 #endif
1731          )) {
1732       if (Complain) {
1733         // Build a list of the PCH imports that got us here (in reverse).
1734         SmallVector<ModuleFile *, 4> ImportStack(1, &F);
1735         while (ImportStack.back()->ImportedBy.size() > 0)
1736           ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
1737 
1738         // The top-level PCH is stale.
1739         StringRef TopLevelPCHName(ImportStack.back()->FileName);
1740         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
1741 
1742         // Print the import stack.
1743         if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
1744           Diag(diag::note_pch_required_by)
1745             << Filename << ImportStack[0]->FileName;
1746           for (unsigned I = 1; I < ImportStack.size(); ++I)
1747             Diag(diag::note_pch_required_by)
1748               << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
1749         }
1750 
1751         if (!Diags.isDiagnosticInFlight())
1752           Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
1753       }
1754 
1755       IsOutOfDate = true;
1756     }
1757 
1758     InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1759 
1760     // Note that we've loaded this input file.
1761     F.InputFilesLoaded[ID-1] = IF;
1762     return IF;
1763   }
1764   }
1765 
1766   return InputFile();
1767 }
1768 
1769 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1770   ModuleFile &M = ModuleMgr.getPrimaryModule();
1771   std::string Filename = filenameStrRef;
1772   MaybeAddSystemRootToFilename(M, Filename);
1773   const FileEntry *File = FileMgr.getFile(Filename);
1774   if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1775       M.OriginalDir != CurrentDir) {
1776     std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1777                                                             M.OriginalDir,
1778                                                             CurrentDir);
1779     if (!resolved.empty())
1780       File = FileMgr.getFile(resolved);
1781   }
1782 
1783   return File;
1784 }
1785 
1786 /// \brief If we are loading a relocatable PCH file, and the filename is
1787 /// not an absolute path, add the system root to the beginning of the file
1788 /// name.
1789 void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1790                                              std::string &Filename) {
1791   // If this is not a relocatable PCH file, there's nothing to do.
1792   if (!M.RelocatablePCH)
1793     return;
1794 
1795   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1796     return;
1797 
1798   if (isysroot.empty()) {
1799     // If no system root was given, default to '/'
1800     Filename.insert(Filename.begin(), '/');
1801     return;
1802   }
1803 
1804   unsigned Length = isysroot.size();
1805   if (isysroot[Length - 1] != '/')
1806     Filename.insert(Filename.begin(), '/');
1807 
1808   Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1809 }
1810 
1811 ASTReader::ASTReadResult
1812 ASTReader::ReadControlBlock(ModuleFile &F,
1813                             SmallVectorImpl<ImportedModule> &Loaded,
1814                             unsigned ClientLoadCapabilities) {
1815   BitstreamCursor &Stream = F.Stream;
1816 
1817   if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1818     Error("malformed block record in AST file");
1819     return Failure;
1820   }
1821 
1822   // Read all of the records and blocks in the control block.
1823   RecordData Record;
1824   while (1) {
1825     llvm::BitstreamEntry Entry = Stream.advance();
1826 
1827     switch (Entry.Kind) {
1828     case llvm::BitstreamEntry::Error:
1829       Error("malformed block record in AST file");
1830       return Failure;
1831     case llvm::BitstreamEntry::EndBlock: {
1832       // Validate input files.
1833       const HeaderSearchOptions &HSOpts =
1834           PP.getHeaderSearchInfo().getHeaderSearchOpts();
1835       if (!DisableValidation &&
1836           (!HSOpts.ModulesValidateOncePerBuildSession ||
1837            F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
1838         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
1839         // All user input files reside at the index range [0, Record[1]), and
1840         // system input files reside at [Record[1], Record[0]).
1841         // Record is the one from INPUT_FILE_OFFSETS.
1842         //
1843         // If we are reading a module, we will create a verification timestamp,
1844         // so we verify all input files.  Otherwise, verify only user input
1845         // files.
1846         unsigned NumInputs = Record[0];
1847         unsigned NumUserInputs = Record[1];
1848         unsigned N = ValidateSystemInputs ||
1849                              (HSOpts.ModulesValidateOncePerBuildSession &&
1850                               F.Kind == MK_Module)
1851                          ? NumInputs
1852                          : NumUserInputs;
1853         for (unsigned I = 0; I < N; ++I) {
1854           InputFile IF = getInputFile(F, I+1, Complain);
1855           if (!IF.getFile() || IF.isOutOfDate())
1856             return OutOfDate;
1857         }
1858       }
1859       return Success;
1860     }
1861 
1862     case llvm::BitstreamEntry::SubBlock:
1863       switch (Entry.ID) {
1864       case INPUT_FILES_BLOCK_ID:
1865         F.InputFilesCursor = Stream;
1866         if (Stream.SkipBlock() || // Skip with the main cursor
1867             // Read the abbreviations
1868             ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1869           Error("malformed block record in AST file");
1870           return Failure;
1871         }
1872         continue;
1873 
1874       default:
1875         if (Stream.SkipBlock()) {
1876           Error("malformed block record in AST file");
1877           return Failure;
1878         }
1879         continue;
1880       }
1881 
1882     case llvm::BitstreamEntry::Record:
1883       // The interesting case.
1884       break;
1885     }
1886 
1887     // Read and process a record.
1888     Record.clear();
1889     StringRef Blob;
1890     switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
1891     case METADATA: {
1892       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1893         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1894           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
1895                                         : diag::err_pch_version_too_new);
1896         return VersionMismatch;
1897       }
1898 
1899       bool hasErrors = Record[5];
1900       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1901         Diag(diag::err_pch_with_compiler_errors);
1902         return HadErrors;
1903       }
1904 
1905       F.RelocatablePCH = Record[4];
1906 
1907       const std::string &CurBranch = getClangFullRepositoryVersion();
1908       StringRef ASTBranch = Blob;
1909       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1910         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1911           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
1912         return VersionMismatch;
1913       }
1914       break;
1915     }
1916 
1917     case IMPORTS: {
1918       // Load each of the imported PCH files.
1919       unsigned Idx = 0, N = Record.size();
1920       while (Idx < N) {
1921         // Read information about the AST file.
1922         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1923         // The import location will be the local one for now; we will adjust
1924         // all import locations of module imports after the global source
1925         // location info are setup.
1926         SourceLocation ImportLoc =
1927             SourceLocation::getFromRawEncoding(Record[Idx++]);
1928         off_t StoredSize = (off_t)Record[Idx++];
1929         time_t StoredModTime = (time_t)Record[Idx++];
1930         unsigned Length = Record[Idx++];
1931         SmallString<128> ImportedFile(Record.begin() + Idx,
1932                                       Record.begin() + Idx + Length);
1933         Idx += Length;
1934 
1935         // Load the AST file.
1936         switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
1937                            StoredSize, StoredModTime,
1938                            ClientLoadCapabilities)) {
1939         case Failure: return Failure;
1940           // If we have to ignore the dependency, we'll have to ignore this too.
1941         case Missing:
1942         case OutOfDate: return OutOfDate;
1943         case VersionMismatch: return VersionMismatch;
1944         case ConfigurationMismatch: return ConfigurationMismatch;
1945         case HadErrors: return HadErrors;
1946         case Success: break;
1947         }
1948       }
1949       break;
1950     }
1951 
1952     case LANGUAGE_OPTIONS: {
1953       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1954       if (Listener && &F == *ModuleMgr.begin() &&
1955           ParseLanguageOptions(Record, Complain, *Listener) &&
1956           !DisableValidation && !AllowConfigurationMismatch)
1957         return ConfigurationMismatch;
1958       break;
1959     }
1960 
1961     case TARGET_OPTIONS: {
1962       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1963       if (Listener && &F == *ModuleMgr.begin() &&
1964           ParseTargetOptions(Record, Complain, *Listener) &&
1965           !DisableValidation && !AllowConfigurationMismatch)
1966         return ConfigurationMismatch;
1967       break;
1968     }
1969 
1970     case DIAGNOSTIC_OPTIONS: {
1971       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1972       if (Listener && &F == *ModuleMgr.begin() &&
1973           ParseDiagnosticOptions(Record, Complain, *Listener) &&
1974           !DisableValidation && !AllowConfigurationMismatch)
1975         return ConfigurationMismatch;
1976       break;
1977     }
1978 
1979     case FILE_SYSTEM_OPTIONS: {
1980       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1981       if (Listener && &F == *ModuleMgr.begin() &&
1982           ParseFileSystemOptions(Record, Complain, *Listener) &&
1983           !DisableValidation && !AllowConfigurationMismatch)
1984         return ConfigurationMismatch;
1985       break;
1986     }
1987 
1988     case HEADER_SEARCH_OPTIONS: {
1989       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1990       if (Listener && &F == *ModuleMgr.begin() &&
1991           ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1992           !DisableValidation && !AllowConfigurationMismatch)
1993         return ConfigurationMismatch;
1994       break;
1995     }
1996 
1997     case PREPROCESSOR_OPTIONS: {
1998       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1999       if (Listener && &F == *ModuleMgr.begin() &&
2000           ParsePreprocessorOptions(Record, Complain, *Listener,
2001                                    SuggestedPredefines) &&
2002           !DisableValidation && !AllowConfigurationMismatch)
2003         return ConfigurationMismatch;
2004       break;
2005     }
2006 
2007     case ORIGINAL_FILE:
2008       F.OriginalSourceFileID = FileID::get(Record[0]);
2009       F.ActualOriginalSourceFileName = Blob;
2010       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2011       MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2012       break;
2013 
2014     case ORIGINAL_FILE_ID:
2015       F.OriginalSourceFileID = FileID::get(Record[0]);
2016       break;
2017 
2018     case ORIGINAL_PCH_DIR:
2019       F.OriginalDir = Blob;
2020       break;
2021 
2022     case INPUT_FILE_OFFSETS:
2023       F.InputFileOffsets = (const uint32_t *)Blob.data();
2024       F.InputFilesLoaded.resize(Record[0]);
2025       break;
2026     }
2027   }
2028 }
2029 
2030 bool ASTReader::ReadASTBlock(ModuleFile &F) {
2031   BitstreamCursor &Stream = F.Stream;
2032 
2033   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2034     Error("malformed block record in AST file");
2035     return true;
2036   }
2037 
2038   // Read all of the records and blocks for the AST file.
2039   RecordData Record;
2040   while (1) {
2041     llvm::BitstreamEntry Entry = Stream.advance();
2042 
2043     switch (Entry.Kind) {
2044     case llvm::BitstreamEntry::Error:
2045       Error("error at end of module block in AST file");
2046       return true;
2047     case llvm::BitstreamEntry::EndBlock: {
2048       // Outside of C++, we do not store a lookup map for the translation unit.
2049       // Instead, mark it as needing a lookup map to be built if this module
2050       // contains any declarations lexically within it (which it always does!).
2051       // This usually has no cost, since we very rarely need the lookup map for
2052       // the translation unit outside C++.
2053       DeclContext *DC = Context.getTranslationUnitDecl();
2054       if (DC->hasExternalLexicalStorage() &&
2055           !getContext().getLangOpts().CPlusPlus)
2056         DC->setMustBuildLookupTable();
2057 
2058       return false;
2059     }
2060     case llvm::BitstreamEntry::SubBlock:
2061       switch (Entry.ID) {
2062       case DECLTYPES_BLOCK_ID:
2063         // We lazily load the decls block, but we want to set up the
2064         // DeclsCursor cursor to point into it.  Clone our current bitcode
2065         // cursor to it, enter the block and read the abbrevs in that block.
2066         // With the main cursor, we just skip over it.
2067         F.DeclsCursor = Stream;
2068         if (Stream.SkipBlock() ||  // Skip with the main cursor.
2069             // Read the abbrevs.
2070             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2071           Error("malformed block record in AST file");
2072           return true;
2073         }
2074         break;
2075 
2076       case DECL_UPDATES_BLOCK_ID:
2077         if (Stream.SkipBlock()) {
2078           Error("malformed block record in AST file");
2079           return true;
2080         }
2081         break;
2082 
2083       case PREPROCESSOR_BLOCK_ID:
2084         F.MacroCursor = Stream;
2085         if (!PP.getExternalSource())
2086           PP.setExternalSource(this);
2087 
2088         if (Stream.SkipBlock() ||
2089             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2090           Error("malformed block record in AST file");
2091           return true;
2092         }
2093         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2094         break;
2095 
2096       case PREPROCESSOR_DETAIL_BLOCK_ID:
2097         F.PreprocessorDetailCursor = Stream;
2098         if (Stream.SkipBlock() ||
2099             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2100                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
2101               Error("malformed preprocessor detail record in AST file");
2102               return true;
2103             }
2104         F.PreprocessorDetailStartOffset
2105         = F.PreprocessorDetailCursor.GetCurrentBitNo();
2106 
2107         if (!PP.getPreprocessingRecord())
2108           PP.createPreprocessingRecord();
2109         if (!PP.getPreprocessingRecord()->getExternalSource())
2110           PP.getPreprocessingRecord()->SetExternalSource(*this);
2111         break;
2112 
2113       case SOURCE_MANAGER_BLOCK_ID:
2114         if (ReadSourceManagerBlock(F))
2115           return true;
2116         break;
2117 
2118       case SUBMODULE_BLOCK_ID:
2119         if (ReadSubmoduleBlock(F))
2120           return true;
2121         break;
2122 
2123       case COMMENTS_BLOCK_ID: {
2124         BitstreamCursor C = Stream;
2125         if (Stream.SkipBlock() ||
2126             ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2127           Error("malformed comments block in AST file");
2128           return true;
2129         }
2130         CommentsCursors.push_back(std::make_pair(C, &F));
2131         break;
2132       }
2133 
2134       default:
2135         if (Stream.SkipBlock()) {
2136           Error("malformed block record in AST file");
2137           return true;
2138         }
2139         break;
2140       }
2141       continue;
2142 
2143     case llvm::BitstreamEntry::Record:
2144       // The interesting case.
2145       break;
2146     }
2147 
2148     // Read and process a record.
2149     Record.clear();
2150     StringRef Blob;
2151     switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
2152     default:  // Default behavior: ignore.
2153       break;
2154 
2155     case TYPE_OFFSET: {
2156       if (F.LocalNumTypes != 0) {
2157         Error("duplicate TYPE_OFFSET record in AST file");
2158         return true;
2159       }
2160       F.TypeOffsets = (const uint32_t *)Blob.data();
2161       F.LocalNumTypes = Record[0];
2162       unsigned LocalBaseTypeIndex = Record[1];
2163       F.BaseTypeIndex = getTotalNumTypes();
2164 
2165       if (F.LocalNumTypes > 0) {
2166         // Introduce the global -> local mapping for types within this module.
2167         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2168 
2169         // Introduce the local -> global mapping for types within this module.
2170         F.TypeRemap.insertOrReplace(
2171           std::make_pair(LocalBaseTypeIndex,
2172                          F.BaseTypeIndex - LocalBaseTypeIndex));
2173 
2174         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2175       }
2176       break;
2177     }
2178 
2179     case DECL_OFFSET: {
2180       if (F.LocalNumDecls != 0) {
2181         Error("duplicate DECL_OFFSET record in AST file");
2182         return true;
2183       }
2184       F.DeclOffsets = (const DeclOffset *)Blob.data();
2185       F.LocalNumDecls = Record[0];
2186       unsigned LocalBaseDeclID = Record[1];
2187       F.BaseDeclID = getTotalNumDecls();
2188 
2189       if (F.LocalNumDecls > 0) {
2190         // Introduce the global -> local mapping for declarations within this
2191         // module.
2192         GlobalDeclMap.insert(
2193           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2194 
2195         // Introduce the local -> global mapping for declarations within this
2196         // module.
2197         F.DeclRemap.insertOrReplace(
2198           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2199 
2200         // Introduce the global -> local mapping for declarations within this
2201         // module.
2202         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2203 
2204         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2205       }
2206       break;
2207     }
2208 
2209     case TU_UPDATE_LEXICAL: {
2210       DeclContext *TU = Context.getTranslationUnitDecl();
2211       DeclContextInfo &Info = F.DeclContextInfos[TU];
2212       Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
2213       Info.NumLexicalDecls
2214         = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
2215       TU->setHasExternalLexicalStorage(true);
2216       break;
2217     }
2218 
2219     case UPDATE_VISIBLE: {
2220       unsigned Idx = 0;
2221       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2222       ASTDeclContextNameLookupTable *Table =
2223         ASTDeclContextNameLookupTable::Create(
2224                         (const unsigned char *)Blob.data() + Record[Idx++],
2225                         (const unsigned char *)Blob.data(),
2226                         ASTDeclContextNameLookupTrait(*this, F));
2227       if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2228         DeclContext *TU = Context.getTranslationUnitDecl();
2229         F.DeclContextInfos[TU].NameLookupTableData = Table;
2230         TU->setHasExternalVisibleStorage(true);
2231       } else
2232         PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2233       break;
2234     }
2235 
2236     case IDENTIFIER_TABLE:
2237       F.IdentifierTableData = Blob.data();
2238       if (Record[0]) {
2239         F.IdentifierLookupTable
2240           = ASTIdentifierLookupTable::Create(
2241                        (const unsigned char *)F.IdentifierTableData + Record[0],
2242                        (const unsigned char *)F.IdentifierTableData,
2243                        ASTIdentifierLookupTrait(*this, F));
2244 
2245         PP.getIdentifierTable().setExternalIdentifierLookup(this);
2246       }
2247       break;
2248 
2249     case IDENTIFIER_OFFSET: {
2250       if (F.LocalNumIdentifiers != 0) {
2251         Error("duplicate IDENTIFIER_OFFSET record in AST file");
2252         return true;
2253       }
2254       F.IdentifierOffsets = (const uint32_t *)Blob.data();
2255       F.LocalNumIdentifiers = Record[0];
2256       unsigned LocalBaseIdentifierID = Record[1];
2257       F.BaseIdentifierID = getTotalNumIdentifiers();
2258 
2259       if (F.LocalNumIdentifiers > 0) {
2260         // Introduce the global -> local mapping for identifiers within this
2261         // module.
2262         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2263                                                   &F));
2264 
2265         // Introduce the local -> global mapping for identifiers within this
2266         // module.
2267         F.IdentifierRemap.insertOrReplace(
2268           std::make_pair(LocalBaseIdentifierID,
2269                          F.BaseIdentifierID - LocalBaseIdentifierID));
2270 
2271         IdentifiersLoaded.resize(IdentifiersLoaded.size()
2272                                  + F.LocalNumIdentifiers);
2273       }
2274       break;
2275     }
2276 
2277     case EAGERLY_DESERIALIZED_DECLS:
2278       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2279         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2280       break;
2281 
2282     case SPECIAL_TYPES:
2283       if (SpecialTypes.empty()) {
2284         for (unsigned I = 0, N = Record.size(); I != N; ++I)
2285           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2286         break;
2287       }
2288 
2289       if (SpecialTypes.size() != Record.size()) {
2290         Error("invalid special-types record");
2291         return true;
2292       }
2293 
2294       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2295         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2296         if (!SpecialTypes[I])
2297           SpecialTypes[I] = ID;
2298         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2299         // merge step?
2300       }
2301       break;
2302 
2303     case STATISTICS:
2304       TotalNumStatements += Record[0];
2305       TotalNumMacros += Record[1];
2306       TotalLexicalDeclContexts += Record[2];
2307       TotalVisibleDeclContexts += Record[3];
2308       break;
2309 
2310     case UNUSED_FILESCOPED_DECLS:
2311       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2312         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2313       break;
2314 
2315     case DELEGATING_CTORS:
2316       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2317         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2318       break;
2319 
2320     case WEAK_UNDECLARED_IDENTIFIERS:
2321       if (Record.size() % 4 != 0) {
2322         Error("invalid weak identifiers record");
2323         return true;
2324       }
2325 
2326       // FIXME: Ignore weak undeclared identifiers from non-original PCH
2327       // files. This isn't the way to do it :)
2328       WeakUndeclaredIdentifiers.clear();
2329 
2330       // Translate the weak, undeclared identifiers into global IDs.
2331       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2332         WeakUndeclaredIdentifiers.push_back(
2333           getGlobalIdentifierID(F, Record[I++]));
2334         WeakUndeclaredIdentifiers.push_back(
2335           getGlobalIdentifierID(F, Record[I++]));
2336         WeakUndeclaredIdentifiers.push_back(
2337           ReadSourceLocation(F, Record, I).getRawEncoding());
2338         WeakUndeclaredIdentifiers.push_back(Record[I++]);
2339       }
2340       break;
2341 
2342     case LOCALLY_SCOPED_EXTERN_C_DECLS:
2343       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2344         LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
2345       break;
2346 
2347     case SELECTOR_OFFSETS: {
2348       F.SelectorOffsets = (const uint32_t *)Blob.data();
2349       F.LocalNumSelectors = Record[0];
2350       unsigned LocalBaseSelectorID = Record[1];
2351       F.BaseSelectorID = getTotalNumSelectors();
2352 
2353       if (F.LocalNumSelectors > 0) {
2354         // Introduce the global -> local mapping for selectors within this
2355         // module.
2356         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2357 
2358         // Introduce the local -> global mapping for selectors within this
2359         // module.
2360         F.SelectorRemap.insertOrReplace(
2361           std::make_pair(LocalBaseSelectorID,
2362                          F.BaseSelectorID - LocalBaseSelectorID));
2363 
2364         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2365       }
2366       break;
2367     }
2368 
2369     case METHOD_POOL:
2370       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
2371       if (Record[0])
2372         F.SelectorLookupTable
2373           = ASTSelectorLookupTable::Create(
2374                         F.SelectorLookupTableData + Record[0],
2375                         F.SelectorLookupTableData,
2376                         ASTSelectorLookupTrait(*this, F));
2377       TotalNumMethodPoolEntries += Record[1];
2378       break;
2379 
2380     case REFERENCED_SELECTOR_POOL:
2381       if (!Record.empty()) {
2382         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2383           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2384                                                                 Record[Idx++]));
2385           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2386                                               getRawEncoding());
2387         }
2388       }
2389       break;
2390 
2391     case PP_COUNTER_VALUE:
2392       if (!Record.empty() && Listener)
2393         Listener->ReadCounter(F, Record[0]);
2394       break;
2395 
2396     case FILE_SORTED_DECLS:
2397       F.FileSortedDecls = (const DeclID *)Blob.data();
2398       F.NumFileSortedDecls = Record[0];
2399       break;
2400 
2401     case SOURCE_LOCATION_OFFSETS: {
2402       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
2403       F.LocalNumSLocEntries = Record[0];
2404       unsigned SLocSpaceSize = Record[1];
2405       llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2406           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2407                                               SLocSpaceSize);
2408       // Make our entry in the range map. BaseID is negative and growing, so
2409       // we invert it. Because we invert it, though, we need the other end of
2410       // the range.
2411       unsigned RangeStart =
2412           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2413       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2414       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2415 
2416       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2417       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2418       GlobalSLocOffsetMap.insert(
2419           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2420                            - SLocSpaceSize,&F));
2421 
2422       // Initialize the remapping table.
2423       // Invalid stays invalid.
2424       F.SLocRemap.insert(std::make_pair(0U, 0));
2425       // This module. Base was 2 when being compiled.
2426       F.SLocRemap.insert(std::make_pair(2U,
2427                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
2428 
2429       TotalNumSLocEntries += F.LocalNumSLocEntries;
2430       break;
2431     }
2432 
2433     case MODULE_OFFSET_MAP: {
2434       // Additional remapping information.
2435       const unsigned char *Data = (const unsigned char*)Blob.data();
2436       const unsigned char *DataEnd = Data + Blob.size();
2437 
2438       // Continuous range maps we may be updating in our module.
2439       ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2440       ContinuousRangeMap<uint32_t, int, 2>::Builder
2441         IdentifierRemap(F.IdentifierRemap);
2442       ContinuousRangeMap<uint32_t, int, 2>::Builder
2443         MacroRemap(F.MacroRemap);
2444       ContinuousRangeMap<uint32_t, int, 2>::Builder
2445         PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2446       ContinuousRangeMap<uint32_t, int, 2>::Builder
2447         SubmoduleRemap(F.SubmoduleRemap);
2448       ContinuousRangeMap<uint32_t, int, 2>::Builder
2449         SelectorRemap(F.SelectorRemap);
2450       ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2451       ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2452 
2453       while(Data < DataEnd) {
2454         uint16_t Len = io::ReadUnalignedLE16(Data);
2455         StringRef Name = StringRef((const char*)Data, Len);
2456         Data += Len;
2457         ModuleFile *OM = ModuleMgr.lookup(Name);
2458         if (!OM) {
2459           Error("SourceLocation remap refers to unknown module");
2460           return true;
2461         }
2462 
2463         uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2464         uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2465         uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2466         uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2467         uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2468         uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2469         uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2470         uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2471 
2472         // Source location offset is mapped to OM->SLocEntryBaseOffset.
2473         SLocRemap.insert(std::make_pair(SLocOffset,
2474           static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2475         IdentifierRemap.insert(
2476           std::make_pair(IdentifierIDOffset,
2477                          OM->BaseIdentifierID - IdentifierIDOffset));
2478         MacroRemap.insert(std::make_pair(MacroIDOffset,
2479                                          OM->BaseMacroID - MacroIDOffset));
2480         PreprocessedEntityRemap.insert(
2481           std::make_pair(PreprocessedEntityIDOffset,
2482             OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2483         SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2484                                       OM->BaseSubmoduleID - SubmoduleIDOffset));
2485         SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2486                                OM->BaseSelectorID - SelectorIDOffset));
2487         DeclRemap.insert(std::make_pair(DeclIDOffset,
2488                                         OM->BaseDeclID - DeclIDOffset));
2489 
2490         TypeRemap.insert(std::make_pair(TypeIndexOffset,
2491                                     OM->BaseTypeIndex - TypeIndexOffset));
2492 
2493         // Global -> local mappings.
2494         F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2495       }
2496       break;
2497     }
2498 
2499     case SOURCE_MANAGER_LINE_TABLE:
2500       if (ParseLineTable(F, Record))
2501         return true;
2502       break;
2503 
2504     case SOURCE_LOCATION_PRELOADS: {
2505       // Need to transform from the local view (1-based IDs) to the global view,
2506       // which is based off F.SLocEntryBaseID.
2507       if (!F.PreloadSLocEntries.empty()) {
2508         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2509         return true;
2510       }
2511 
2512       F.PreloadSLocEntries.swap(Record);
2513       break;
2514     }
2515 
2516     case EXT_VECTOR_DECLS:
2517       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2518         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2519       break;
2520 
2521     case VTABLE_USES:
2522       if (Record.size() % 3 != 0) {
2523         Error("Invalid VTABLE_USES record");
2524         return true;
2525       }
2526 
2527       // Later tables overwrite earlier ones.
2528       // FIXME: Modules will have some trouble with this. This is clearly not
2529       // the right way to do this.
2530       VTableUses.clear();
2531 
2532       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2533         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2534         VTableUses.push_back(
2535           ReadSourceLocation(F, Record, Idx).getRawEncoding());
2536         VTableUses.push_back(Record[Idx++]);
2537       }
2538       break;
2539 
2540     case DYNAMIC_CLASSES:
2541       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2542         DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2543       break;
2544 
2545     case PENDING_IMPLICIT_INSTANTIATIONS:
2546       if (PendingInstantiations.size() % 2 != 0) {
2547         Error("Invalid existing PendingInstantiations");
2548         return true;
2549       }
2550 
2551       if (Record.size() % 2 != 0) {
2552         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2553         return true;
2554       }
2555 
2556       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2557         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2558         PendingInstantiations.push_back(
2559           ReadSourceLocation(F, Record, I).getRawEncoding());
2560       }
2561       break;
2562 
2563     case SEMA_DECL_REFS:
2564       if (Record.size() != 2) {
2565         Error("Invalid SEMA_DECL_REFS block");
2566         return true;
2567       }
2568       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2569         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2570       break;
2571 
2572     case PPD_ENTITIES_OFFSETS: {
2573       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2574       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2575       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
2576 
2577       unsigned LocalBasePreprocessedEntityID = Record[0];
2578 
2579       unsigned StartingID;
2580       if (!PP.getPreprocessingRecord())
2581         PP.createPreprocessingRecord();
2582       if (!PP.getPreprocessingRecord()->getExternalSource())
2583         PP.getPreprocessingRecord()->SetExternalSource(*this);
2584       StartingID
2585         = PP.getPreprocessingRecord()
2586             ->allocateLoadedEntities(F.NumPreprocessedEntities);
2587       F.BasePreprocessedEntityID = StartingID;
2588 
2589       if (F.NumPreprocessedEntities > 0) {
2590         // Introduce the global -> local mapping for preprocessed entities in
2591         // this module.
2592         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2593 
2594         // Introduce the local -> global mapping for preprocessed entities in
2595         // this module.
2596         F.PreprocessedEntityRemap.insertOrReplace(
2597           std::make_pair(LocalBasePreprocessedEntityID,
2598             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2599       }
2600 
2601       break;
2602     }
2603 
2604     case DECL_UPDATE_OFFSETS: {
2605       if (Record.size() % 2 != 0) {
2606         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2607         return true;
2608       }
2609       for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2610         DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2611           .push_back(std::make_pair(&F, Record[I+1]));
2612       break;
2613     }
2614 
2615     case DECL_REPLACEMENTS: {
2616       if (Record.size() % 3 != 0) {
2617         Error("invalid DECL_REPLACEMENTS block in AST file");
2618         return true;
2619       }
2620       for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2621         ReplacedDecls[getGlobalDeclID(F, Record[I])]
2622           = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2623       break;
2624     }
2625 
2626     case OBJC_CATEGORIES_MAP: {
2627       if (F.LocalNumObjCCategoriesInMap != 0) {
2628         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2629         return true;
2630       }
2631 
2632       F.LocalNumObjCCategoriesInMap = Record[0];
2633       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
2634       break;
2635     }
2636 
2637     case OBJC_CATEGORIES:
2638       F.ObjCCategories.swap(Record);
2639       break;
2640 
2641     case CXX_BASE_SPECIFIER_OFFSETS: {
2642       if (F.LocalNumCXXBaseSpecifiers != 0) {
2643         Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2644         return true;
2645       }
2646 
2647       F.LocalNumCXXBaseSpecifiers = Record[0];
2648       F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
2649       NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2650       break;
2651     }
2652 
2653     case DIAG_PRAGMA_MAPPINGS:
2654       if (F.PragmaDiagMappings.empty())
2655         F.PragmaDiagMappings.swap(Record);
2656       else
2657         F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2658                                     Record.begin(), Record.end());
2659       break;
2660 
2661     case CUDA_SPECIAL_DECL_REFS:
2662       // Later tables overwrite earlier ones.
2663       // FIXME: Modules will have trouble with this.
2664       CUDASpecialDeclRefs.clear();
2665       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2666         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2667       break;
2668 
2669     case HEADER_SEARCH_TABLE: {
2670       F.HeaderFileInfoTableData = Blob.data();
2671       F.LocalNumHeaderFileInfos = Record[1];
2672       if (Record[0]) {
2673         F.HeaderFileInfoTable
2674           = HeaderFileInfoLookupTable::Create(
2675                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2676                    (const unsigned char *)F.HeaderFileInfoTableData,
2677                    HeaderFileInfoTrait(*this, F,
2678                                        &PP.getHeaderSearchInfo(),
2679                                        Blob.data() + Record[2]));
2680 
2681         PP.getHeaderSearchInfo().SetExternalSource(this);
2682         if (!PP.getHeaderSearchInfo().getExternalLookup())
2683           PP.getHeaderSearchInfo().SetExternalLookup(this);
2684       }
2685       break;
2686     }
2687 
2688     case FP_PRAGMA_OPTIONS:
2689       // Later tables overwrite earlier ones.
2690       FPPragmaOptions.swap(Record);
2691       break;
2692 
2693     case OPENCL_EXTENSIONS:
2694       // Later tables overwrite earlier ones.
2695       OpenCLExtensions.swap(Record);
2696       break;
2697 
2698     case TENTATIVE_DEFINITIONS:
2699       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2700         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2701       break;
2702 
2703     case KNOWN_NAMESPACES:
2704       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2705         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2706       break;
2707 
2708     case UNDEFINED_BUT_USED:
2709       if (UndefinedButUsed.size() % 2 != 0) {
2710         Error("Invalid existing UndefinedButUsed");
2711         return true;
2712       }
2713 
2714       if (Record.size() % 2 != 0) {
2715         Error("invalid undefined-but-used record");
2716         return true;
2717       }
2718       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2719         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2720         UndefinedButUsed.push_back(
2721             ReadSourceLocation(F, Record, I).getRawEncoding());
2722       }
2723       break;
2724 
2725     case IMPORTED_MODULES: {
2726       if (F.Kind != MK_Module) {
2727         // If we aren't loading a module (which has its own exports), make
2728         // all of the imported modules visible.
2729         // FIXME: Deal with macros-only imports.
2730         for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2731           if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2732             ImportedModules.push_back(GlobalID);
2733         }
2734       }
2735       break;
2736     }
2737 
2738     case LOCAL_REDECLARATIONS: {
2739       F.RedeclarationChains.swap(Record);
2740       break;
2741     }
2742 
2743     case LOCAL_REDECLARATIONS_MAP: {
2744       if (F.LocalNumRedeclarationsInMap != 0) {
2745         Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2746         return true;
2747       }
2748 
2749       F.LocalNumRedeclarationsInMap = Record[0];
2750       F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
2751       break;
2752     }
2753 
2754     case MERGED_DECLARATIONS: {
2755       for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2756         GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2757         SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2758         for (unsigned N = Record[Idx++]; N > 0; --N)
2759           Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2760       }
2761       break;
2762     }
2763 
2764     case MACRO_OFFSET: {
2765       if (F.LocalNumMacros != 0) {
2766         Error("duplicate MACRO_OFFSET record in AST file");
2767         return true;
2768       }
2769       F.MacroOffsets = (const uint32_t *)Blob.data();
2770       F.LocalNumMacros = Record[0];
2771       unsigned LocalBaseMacroID = Record[1];
2772       F.BaseMacroID = getTotalNumMacros();
2773 
2774       if (F.LocalNumMacros > 0) {
2775         // Introduce the global -> local mapping for macros within this module.
2776         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2777 
2778         // Introduce the local -> global mapping for macros within this module.
2779         F.MacroRemap.insertOrReplace(
2780           std::make_pair(LocalBaseMacroID,
2781                          F.BaseMacroID - LocalBaseMacroID));
2782 
2783         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2784       }
2785       break;
2786     }
2787 
2788     case MACRO_TABLE: {
2789       // FIXME: Not used yet.
2790       break;
2791     }
2792 
2793     case LATE_PARSED_TEMPLATE: {
2794       LateParsedTemplates.append(Record.begin(), Record.end());
2795       break;
2796     }
2797     }
2798   }
2799 }
2800 
2801 /// \brief Move the given method to the back of the global list of methods.
2802 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2803   // Find the entry for this selector in the method pool.
2804   Sema::GlobalMethodPool::iterator Known
2805     = S.MethodPool.find(Method->getSelector());
2806   if (Known == S.MethodPool.end())
2807     return;
2808 
2809   // Retrieve the appropriate method list.
2810   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2811                                                     : Known->second.second;
2812   bool Found = false;
2813   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
2814     if (!Found) {
2815       if (List->Method == Method) {
2816         Found = true;
2817       } else {
2818         // Keep searching.
2819         continue;
2820       }
2821     }
2822 
2823     if (List->getNext())
2824       List->Method = List->getNext()->Method;
2825     else
2826       List->Method = Method;
2827   }
2828 }
2829 
2830 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
2831   for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2832     switch (Names[I].getKind()) {
2833     case HiddenName::Declaration: {
2834       Decl *D = Names[I].getDecl();
2835       bool wasHidden = D->Hidden;
2836       D->Hidden = false;
2837 
2838       if (wasHidden && SemaObj) {
2839         if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2840           moveMethodToBackOfGlobalList(*SemaObj, Method);
2841         }
2842       }
2843       break;
2844     }
2845     case HiddenName::MacroVisibility: {
2846       std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
2847       installImportedMacro(Macro.first, Macro.second, Owner);
2848       break;
2849     }
2850     }
2851   }
2852 }
2853 
2854 void ASTReader::makeModuleVisible(Module *Mod,
2855                                   Module::NameVisibilityKind NameVisibility,
2856                                   SourceLocation ImportLoc,
2857                                   bool Complain) {
2858   llvm::SmallPtrSet<Module *, 4> Visited;
2859   SmallVector<Module *, 4> Stack;
2860   Stack.push_back(Mod);
2861   while (!Stack.empty()) {
2862     Mod = Stack.pop_back_val();
2863 
2864     if (NameVisibility <= Mod->NameVisibility) {
2865       // This module already has this level of visibility (or greater), so
2866       // there is nothing more to do.
2867       continue;
2868     }
2869 
2870     if (!Mod->isAvailable()) {
2871       // Modules that aren't available cannot be made visible.
2872       continue;
2873     }
2874 
2875     // Update the module's name visibility.
2876     Mod->NameVisibility = NameVisibility;
2877 
2878     // If we've already deserialized any names from this module,
2879     // mark them as visible.
2880     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2881     if (Hidden != HiddenNamesMap.end()) {
2882       makeNamesVisible(Hidden->second, Hidden->first);
2883       HiddenNamesMap.erase(Hidden);
2884     }
2885 
2886     // Push any exported modules onto the stack to be marked as visible.
2887     SmallVector<Module *, 16> Exports;
2888     Mod->getExportedModules(Exports);
2889     for (SmallVectorImpl<Module *>::iterator
2890            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2891       Module *Exported = *I;
2892       if (Visited.insert(Exported))
2893         Stack.push_back(Exported);
2894     }
2895 
2896     // Detect any conflicts.
2897     if (Complain) {
2898       assert(ImportLoc.isValid() && "Missing import location");
2899       for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2900         if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2901           Diag(ImportLoc, diag::warn_module_conflict)
2902             << Mod->getFullModuleName()
2903             << Mod->Conflicts[I].Other->getFullModuleName()
2904             << Mod->Conflicts[I].Message;
2905           // FIXME: Need note where the other module was imported.
2906         }
2907       }
2908     }
2909   }
2910 }
2911 
2912 bool ASTReader::loadGlobalIndex() {
2913   if (GlobalIndex)
2914     return false;
2915 
2916   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2917       !Context.getLangOpts().Modules)
2918     return true;
2919 
2920   // Try to load the global index.
2921   TriedLoadingGlobalIndex = true;
2922   StringRef ModuleCachePath
2923     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2924   std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
2925     = GlobalModuleIndex::readIndex(ModuleCachePath);
2926   if (!Result.first)
2927     return true;
2928 
2929   GlobalIndex.reset(Result.first);
2930   ModuleMgr.setGlobalIndex(GlobalIndex.get());
2931   return false;
2932 }
2933 
2934 bool ASTReader::isGlobalIndexUnavailable() const {
2935   return Context.getLangOpts().Modules && UseGlobalIndex &&
2936          !hasGlobalIndex() && TriedLoadingGlobalIndex;
2937 }
2938 
2939 static void updateModuleTimestamp(ModuleFile &MF) {
2940   // Overwrite the timestamp file contents so that file's mtime changes.
2941   std::string TimestampFilename = MF.getTimestampFilename();
2942   std::string ErrorInfo;
2943   llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo);
2944   if (!ErrorInfo.empty())
2945     return;
2946   OS << "Timestamp file\n";
2947 }
2948 
2949 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2950                                             ModuleKind Type,
2951                                             SourceLocation ImportLoc,
2952                                             unsigned ClientLoadCapabilities) {
2953   llvm::SaveAndRestore<SourceLocation>
2954     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
2955 
2956   // Bump the generation number.
2957   unsigned PreviousGeneration = CurrentGeneration++;
2958 
2959   unsigned NumModules = ModuleMgr.size();
2960   SmallVector<ImportedModule, 4> Loaded;
2961   switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2962                                                 /*ImportedBy=*/0, Loaded,
2963                                                 0, 0,
2964                                                 ClientLoadCapabilities)) {
2965   case Failure:
2966   case Missing:
2967   case OutOfDate:
2968   case VersionMismatch:
2969   case ConfigurationMismatch:
2970   case HadErrors:
2971     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2972                             Context.getLangOpts().Modules
2973                               ? &PP.getHeaderSearchInfo().getModuleMap()
2974                               : 0);
2975 
2976     // If we find that any modules are unusable, the global index is going
2977     // to be out-of-date. Just remove it.
2978     GlobalIndex.reset();
2979     ModuleMgr.setGlobalIndex(0);
2980     return ReadResult;
2981 
2982   case Success:
2983     break;
2984   }
2985 
2986   // Here comes stuff that we only do once the entire chain is loaded.
2987 
2988   // Load the AST blocks of all of the modules that we loaded.
2989   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2990                                               MEnd = Loaded.end();
2991        M != MEnd; ++M) {
2992     ModuleFile &F = *M->Mod;
2993 
2994     // Read the AST block.
2995     if (ReadASTBlock(F))
2996       return Failure;
2997 
2998     // Once read, set the ModuleFile bit base offset and update the size in
2999     // bits of all files we've seen.
3000     F.GlobalBitOffset = TotalModulesSizeInBits;
3001     TotalModulesSizeInBits += F.SizeInBits;
3002     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3003 
3004     // Preload SLocEntries.
3005     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3006       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3007       // Load it through the SourceManager and don't call ReadSLocEntry()
3008       // directly because the entry may have already been loaded in which case
3009       // calling ReadSLocEntry() directly would trigger an assertion in
3010       // SourceManager.
3011       SourceMgr.getLoadedSLocEntryByID(Index);
3012     }
3013   }
3014 
3015   // Setup the import locations and notify the module manager that we've
3016   // committed to these module files.
3017   for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3018                                               MEnd = Loaded.end();
3019        M != MEnd; ++M) {
3020     ModuleFile &F = *M->Mod;
3021 
3022     ModuleMgr.moduleFileAccepted(&F);
3023 
3024     // Set the import location.
3025     F.DirectImportLoc = ImportLoc;
3026     if (!M->ImportedBy)
3027       F.ImportLoc = M->ImportLoc;
3028     else
3029       F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3030                                        M->ImportLoc.getRawEncoding());
3031   }
3032 
3033   // Mark all of the identifiers in the identifier table as being out of date,
3034   // so that various accessors know to check the loaded modules when the
3035   // identifier is used.
3036   for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3037                               IdEnd = PP.getIdentifierTable().end();
3038        Id != IdEnd; ++Id)
3039     Id->second->setOutOfDate(true);
3040 
3041   // Resolve any unresolved module exports.
3042   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3043     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
3044     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3045     Module *ResolvedMod = getSubmodule(GlobalID);
3046 
3047     switch (Unresolved.Kind) {
3048     case UnresolvedModuleRef::Conflict:
3049       if (ResolvedMod) {
3050         Module::Conflict Conflict;
3051         Conflict.Other = ResolvedMod;
3052         Conflict.Message = Unresolved.String.str();
3053         Unresolved.Mod->Conflicts.push_back(Conflict);
3054       }
3055       continue;
3056 
3057     case UnresolvedModuleRef::Import:
3058       if (ResolvedMod)
3059         Unresolved.Mod->Imports.push_back(ResolvedMod);
3060       continue;
3061 
3062     case UnresolvedModuleRef::Export:
3063       if (ResolvedMod || Unresolved.IsWildcard)
3064         Unresolved.Mod->Exports.push_back(
3065           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3066       continue;
3067     }
3068   }
3069   UnresolvedModuleRefs.clear();
3070 
3071   // FIXME: How do we load the 'use'd modules? They may not be submodules.
3072   // Might be unnecessary as use declarations are only used to build the
3073   // module itself.
3074 
3075   InitializeContext();
3076 
3077   if (SemaObj)
3078     UpdateSema();
3079 
3080   if (DeserializationListener)
3081     DeserializationListener->ReaderInitialized(this);
3082 
3083   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3084   if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3085     PrimaryModule.OriginalSourceFileID
3086       = FileID::get(PrimaryModule.SLocEntryBaseID
3087                     + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3088 
3089     // If this AST file is a precompiled preamble, then set the
3090     // preamble file ID of the source manager to the file source file
3091     // from which the preamble was built.
3092     if (Type == MK_Preamble) {
3093       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3094     } else if (Type == MK_MainFile) {
3095       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3096     }
3097   }
3098 
3099   // For any Objective-C class definitions we have already loaded, make sure
3100   // that we load any additional categories.
3101   for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3102     loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3103                        ObjCClassesLoaded[I],
3104                        PreviousGeneration);
3105   }
3106 
3107   if (PP.getHeaderSearchInfo()
3108           .getHeaderSearchOpts()
3109           .ModulesValidateOncePerBuildSession) {
3110     // Now we are certain that the module and all modules it depends on are
3111     // up to date.  Create or update timestamp files for modules that are
3112     // located in the module cache (not for PCH files that could be anywhere
3113     // in the filesystem).
3114     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3115       ImportedModule &M = Loaded[I];
3116       if (M.Mod->Kind == MK_Module) {
3117         updateModuleTimestamp(*M.Mod);
3118       }
3119     }
3120   }
3121 
3122   return Success;
3123 }
3124 
3125 ASTReader::ASTReadResult
3126 ASTReader::ReadASTCore(StringRef FileName,
3127                        ModuleKind Type,
3128                        SourceLocation ImportLoc,
3129                        ModuleFile *ImportedBy,
3130                        SmallVectorImpl<ImportedModule> &Loaded,
3131                        off_t ExpectedSize, time_t ExpectedModTime,
3132                        unsigned ClientLoadCapabilities) {
3133   ModuleFile *M;
3134   std::string ErrorStr;
3135   ModuleManager::AddModuleResult AddResult
3136     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3137                           CurrentGeneration, ExpectedSize, ExpectedModTime,
3138                           M, ErrorStr);
3139 
3140   switch (AddResult) {
3141   case ModuleManager::AlreadyLoaded:
3142     return Success;
3143 
3144   case ModuleManager::NewlyLoaded:
3145     // Load module file below.
3146     break;
3147 
3148   case ModuleManager::Missing:
3149     // The module file was missing; if the client handle handle, that, return
3150     // it.
3151     if (ClientLoadCapabilities & ARR_Missing)
3152       return Missing;
3153 
3154     // Otherwise, return an error.
3155     {
3156       std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3157                       + ErrorStr;
3158       Error(Msg);
3159     }
3160     return Failure;
3161 
3162   case ModuleManager::OutOfDate:
3163     // We couldn't load the module file because it is out-of-date. If the
3164     // client can handle out-of-date, return it.
3165     if (ClientLoadCapabilities & ARR_OutOfDate)
3166       return OutOfDate;
3167 
3168     // Otherwise, return an error.
3169     {
3170       std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3171                       + ErrorStr;
3172       Error(Msg);
3173     }
3174     return Failure;
3175   }
3176 
3177   assert(M && "Missing module file");
3178 
3179   // FIXME: This seems rather a hack. Should CurrentDir be part of the
3180   // module?
3181   if (FileName != "-") {
3182     CurrentDir = llvm::sys::path::parent_path(FileName);
3183     if (CurrentDir.empty()) CurrentDir = ".";
3184   }
3185 
3186   ModuleFile &F = *M;
3187   BitstreamCursor &Stream = F.Stream;
3188   Stream.init(F.StreamFile);
3189   F.SizeInBits = F.Buffer->getBufferSize() * 8;
3190 
3191   // Sniff for the signature.
3192   if (Stream.Read(8) != 'C' ||
3193       Stream.Read(8) != 'P' ||
3194       Stream.Read(8) != 'C' ||
3195       Stream.Read(8) != 'H') {
3196     Diag(diag::err_not_a_pch_file) << FileName;
3197     return Failure;
3198   }
3199 
3200   // This is used for compatibility with older PCH formats.
3201   bool HaveReadControlBlock = false;
3202 
3203   while (1) {
3204     llvm::BitstreamEntry Entry = Stream.advance();
3205 
3206     switch (Entry.Kind) {
3207     case llvm::BitstreamEntry::Error:
3208     case llvm::BitstreamEntry::EndBlock:
3209     case llvm::BitstreamEntry::Record:
3210       Error("invalid record at top-level of AST file");
3211       return Failure;
3212 
3213     case llvm::BitstreamEntry::SubBlock:
3214       break;
3215     }
3216 
3217     // We only know the control subblock ID.
3218     switch (Entry.ID) {
3219     case llvm::bitc::BLOCKINFO_BLOCK_ID:
3220       if (Stream.ReadBlockInfoBlock()) {
3221         Error("malformed BlockInfoBlock in AST file");
3222         return Failure;
3223       }
3224       break;
3225     case CONTROL_BLOCK_ID:
3226       HaveReadControlBlock = true;
3227       switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3228       case Success:
3229         break;
3230 
3231       case Failure: return Failure;
3232       case Missing: return Missing;
3233       case OutOfDate: return OutOfDate;
3234       case VersionMismatch: return VersionMismatch;
3235       case ConfigurationMismatch: return ConfigurationMismatch;
3236       case HadErrors: return HadErrors;
3237       }
3238       break;
3239     case AST_BLOCK_ID:
3240       if (!HaveReadControlBlock) {
3241         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3242           Diag(diag::err_pch_version_too_old);
3243         return VersionMismatch;
3244       }
3245 
3246       // Record that we've loaded this module.
3247       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3248       return Success;
3249 
3250     default:
3251       if (Stream.SkipBlock()) {
3252         Error("malformed block record in AST file");
3253         return Failure;
3254       }
3255       break;
3256     }
3257   }
3258 
3259   return Success;
3260 }
3261 
3262 void ASTReader::InitializeContext() {
3263   // If there's a listener, notify them that we "read" the translation unit.
3264   if (DeserializationListener)
3265     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3266                                       Context.getTranslationUnitDecl());
3267 
3268   // Make sure we load the declaration update records for the translation unit,
3269   // if there are any.
3270   loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3271                         Context.getTranslationUnitDecl());
3272 
3273   // FIXME: Find a better way to deal with collisions between these
3274   // built-in types. Right now, we just ignore the problem.
3275 
3276   // Load the special types.
3277   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3278     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3279       if (!Context.CFConstantStringTypeDecl)
3280         Context.setCFConstantStringType(GetType(String));
3281     }
3282 
3283     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3284       QualType FileType = GetType(File);
3285       if (FileType.isNull()) {
3286         Error("FILE type is NULL");
3287         return;
3288       }
3289 
3290       if (!Context.FILEDecl) {
3291         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3292           Context.setFILEDecl(Typedef->getDecl());
3293         else {
3294           const TagType *Tag = FileType->getAs<TagType>();
3295           if (!Tag) {
3296             Error("Invalid FILE type in AST file");
3297             return;
3298           }
3299           Context.setFILEDecl(Tag->getDecl());
3300         }
3301       }
3302     }
3303 
3304     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3305       QualType Jmp_bufType = GetType(Jmp_buf);
3306       if (Jmp_bufType.isNull()) {
3307         Error("jmp_buf type is NULL");
3308         return;
3309       }
3310 
3311       if (!Context.jmp_bufDecl) {
3312         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3313           Context.setjmp_bufDecl(Typedef->getDecl());
3314         else {
3315           const TagType *Tag = Jmp_bufType->getAs<TagType>();
3316           if (!Tag) {
3317             Error("Invalid jmp_buf type in AST file");
3318             return;
3319           }
3320           Context.setjmp_bufDecl(Tag->getDecl());
3321         }
3322       }
3323     }
3324 
3325     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3326       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3327       if (Sigjmp_bufType.isNull()) {
3328         Error("sigjmp_buf type is NULL");
3329         return;
3330       }
3331 
3332       if (!Context.sigjmp_bufDecl) {
3333         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3334           Context.setsigjmp_bufDecl(Typedef->getDecl());
3335         else {
3336           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3337           assert(Tag && "Invalid sigjmp_buf type in AST file");
3338           Context.setsigjmp_bufDecl(Tag->getDecl());
3339         }
3340       }
3341     }
3342 
3343     if (unsigned ObjCIdRedef
3344           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3345       if (Context.ObjCIdRedefinitionType.isNull())
3346         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3347     }
3348 
3349     if (unsigned ObjCClassRedef
3350           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3351       if (Context.ObjCClassRedefinitionType.isNull())
3352         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3353     }
3354 
3355     if (unsigned ObjCSelRedef
3356           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3357       if (Context.ObjCSelRedefinitionType.isNull())
3358         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3359     }
3360 
3361     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3362       QualType Ucontext_tType = GetType(Ucontext_t);
3363       if (Ucontext_tType.isNull()) {
3364         Error("ucontext_t type is NULL");
3365         return;
3366       }
3367 
3368       if (!Context.ucontext_tDecl) {
3369         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3370           Context.setucontext_tDecl(Typedef->getDecl());
3371         else {
3372           const TagType *Tag = Ucontext_tType->getAs<TagType>();
3373           assert(Tag && "Invalid ucontext_t type in AST file");
3374           Context.setucontext_tDecl(Tag->getDecl());
3375         }
3376       }
3377     }
3378   }
3379 
3380   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3381 
3382   // If there were any CUDA special declarations, deserialize them.
3383   if (!CUDASpecialDeclRefs.empty()) {
3384     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3385     Context.setcudaConfigureCallDecl(
3386                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3387   }
3388 
3389   // Re-export any modules that were imported by a non-module AST file.
3390   for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3391     if (Module *Imported = getSubmodule(ImportedModules[I]))
3392       makeModuleVisible(Imported, Module::AllVisible,
3393                         /*ImportLoc=*/SourceLocation(),
3394                         /*Complain=*/false);
3395   }
3396   ImportedModules.clear();
3397 }
3398 
3399 void ASTReader::finalizeForWriting() {
3400   for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3401                                  HiddenEnd = HiddenNamesMap.end();
3402        Hidden != HiddenEnd; ++Hidden) {
3403     makeNamesVisible(Hidden->second, Hidden->first);
3404   }
3405   HiddenNamesMap.clear();
3406 }
3407 
3408 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3409 /// cursor into the start of the given block ID, returning false on success and
3410 /// true on failure.
3411 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3412   while (1) {
3413     llvm::BitstreamEntry Entry = Cursor.advance();
3414     switch (Entry.Kind) {
3415     case llvm::BitstreamEntry::Error:
3416     case llvm::BitstreamEntry::EndBlock:
3417       return true;
3418 
3419     case llvm::BitstreamEntry::Record:
3420       // Ignore top-level records.
3421       Cursor.skipRecord(Entry.ID);
3422       break;
3423 
3424     case llvm::BitstreamEntry::SubBlock:
3425       if (Entry.ID == BlockID) {
3426         if (Cursor.EnterSubBlock(BlockID))
3427           return true;
3428         // Found it!
3429         return false;
3430       }
3431 
3432       if (Cursor.SkipBlock())
3433         return true;
3434     }
3435   }
3436 }
3437 
3438 /// \brief Retrieve the name of the original source file name
3439 /// directly from the AST file, without actually loading the AST
3440 /// file.
3441 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3442                                              FileManager &FileMgr,
3443                                              DiagnosticsEngine &Diags) {
3444   // Open the AST file.
3445   std::string ErrStr;
3446   OwningPtr<llvm::MemoryBuffer> Buffer;
3447   Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3448   if (!Buffer) {
3449     Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3450     return std::string();
3451   }
3452 
3453   // Initialize the stream
3454   llvm::BitstreamReader StreamFile;
3455   BitstreamCursor Stream;
3456   StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3457                   (const unsigned char *)Buffer->getBufferEnd());
3458   Stream.init(StreamFile);
3459 
3460   // Sniff for the signature.
3461   if (Stream.Read(8) != 'C' ||
3462       Stream.Read(8) != 'P' ||
3463       Stream.Read(8) != 'C' ||
3464       Stream.Read(8) != 'H') {
3465     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3466     return std::string();
3467   }
3468 
3469   // Scan for the CONTROL_BLOCK_ID block.
3470   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
3471     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3472     return std::string();
3473   }
3474 
3475   // Scan for ORIGINAL_FILE inside the control block.
3476   RecordData Record;
3477   while (1) {
3478     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3479     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3480       return std::string();
3481 
3482     if (Entry.Kind != llvm::BitstreamEntry::Record) {
3483       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3484       return std::string();
3485     }
3486 
3487     Record.clear();
3488     StringRef Blob;
3489     if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3490       return Blob.str();
3491   }
3492 }
3493 
3494 namespace {
3495   class SimplePCHValidator : public ASTReaderListener {
3496     const LangOptions &ExistingLangOpts;
3497     const TargetOptions &ExistingTargetOpts;
3498     const PreprocessorOptions &ExistingPPOpts;
3499     FileManager &FileMgr;
3500 
3501   public:
3502     SimplePCHValidator(const LangOptions &ExistingLangOpts,
3503                        const TargetOptions &ExistingTargetOpts,
3504                        const PreprocessorOptions &ExistingPPOpts,
3505                        FileManager &FileMgr)
3506       : ExistingLangOpts(ExistingLangOpts),
3507         ExistingTargetOpts(ExistingTargetOpts),
3508         ExistingPPOpts(ExistingPPOpts),
3509         FileMgr(FileMgr)
3510     {
3511     }
3512 
3513     virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3514                                      bool Complain) {
3515       return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3516     }
3517     virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3518                                    bool Complain) {
3519       return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3520     }
3521     virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3522                                          bool Complain,
3523                                          std::string &SuggestedPredefines) {
3524       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3525                                       SuggestedPredefines, ExistingLangOpts);
3526     }
3527   };
3528 }
3529 
3530 bool ASTReader::readASTFileControlBlock(StringRef Filename,
3531                                         FileManager &FileMgr,
3532                                         ASTReaderListener &Listener) {
3533   // Open the AST file.
3534   std::string ErrStr;
3535   OwningPtr<llvm::MemoryBuffer> Buffer;
3536   Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3537   if (!Buffer) {
3538     return true;
3539   }
3540 
3541   // Initialize the stream
3542   llvm::BitstreamReader StreamFile;
3543   BitstreamCursor Stream;
3544   StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3545                   (const unsigned char *)Buffer->getBufferEnd());
3546   Stream.init(StreamFile);
3547 
3548   // Sniff for the signature.
3549   if (Stream.Read(8) != 'C' ||
3550       Stream.Read(8) != 'P' ||
3551       Stream.Read(8) != 'C' ||
3552       Stream.Read(8) != 'H') {
3553     return true;
3554   }
3555 
3556   // Scan for the CONTROL_BLOCK_ID block.
3557   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3558     return true;
3559 
3560   bool NeedsInputFiles = Listener.needsInputFileVisitation();
3561   BitstreamCursor InputFilesCursor;
3562   if (NeedsInputFiles) {
3563     InputFilesCursor = Stream;
3564     if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3565       return true;
3566 
3567     // Read the abbreviations
3568     while (true) {
3569       uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3570       unsigned Code = InputFilesCursor.ReadCode();
3571 
3572       // We expect all abbrevs to be at the start of the block.
3573       if (Code != llvm::bitc::DEFINE_ABBREV) {
3574         InputFilesCursor.JumpToBit(Offset);
3575         break;
3576       }
3577       InputFilesCursor.ReadAbbrevRecord();
3578     }
3579   }
3580 
3581   // Scan for ORIGINAL_FILE inside the control block.
3582   RecordData Record;
3583   while (1) {
3584     llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3585     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3586       return false;
3587 
3588     if (Entry.Kind != llvm::BitstreamEntry::Record)
3589       return true;
3590 
3591     Record.clear();
3592     StringRef Blob;
3593     unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3594     switch ((ControlRecordTypes)RecCode) {
3595     case METADATA: {
3596       if (Record[0] != VERSION_MAJOR)
3597         return true;
3598 
3599       if (Listener.ReadFullVersionInformation(Blob))
3600         return true;
3601 
3602       break;
3603     }
3604     case LANGUAGE_OPTIONS:
3605       if (ParseLanguageOptions(Record, false, Listener))
3606         return true;
3607       break;
3608 
3609     case TARGET_OPTIONS:
3610       if (ParseTargetOptions(Record, false, Listener))
3611         return true;
3612       break;
3613 
3614     case DIAGNOSTIC_OPTIONS:
3615       if (ParseDiagnosticOptions(Record, false, Listener))
3616         return true;
3617       break;
3618 
3619     case FILE_SYSTEM_OPTIONS:
3620       if (ParseFileSystemOptions(Record, false, Listener))
3621         return true;
3622       break;
3623 
3624     case HEADER_SEARCH_OPTIONS:
3625       if (ParseHeaderSearchOptions(Record, false, Listener))
3626         return true;
3627       break;
3628 
3629     case PREPROCESSOR_OPTIONS: {
3630       std::string IgnoredSuggestedPredefines;
3631       if (ParsePreprocessorOptions(Record, false, Listener,
3632                                    IgnoredSuggestedPredefines))
3633         return true;
3634       break;
3635     }
3636 
3637     case INPUT_FILE_OFFSETS: {
3638       if (!NeedsInputFiles)
3639         break;
3640 
3641       unsigned NumInputFiles = Record[0];
3642       unsigned NumUserFiles = Record[1];
3643       const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3644       for (unsigned I = 0; I != NumInputFiles; ++I) {
3645         // Go find this input file.
3646         bool isSystemFile = I >= NumUserFiles;
3647         BitstreamCursor &Cursor = InputFilesCursor;
3648         SavedStreamPosition SavedPosition(Cursor);
3649         Cursor.JumpToBit(InputFileOffs[I]);
3650 
3651         unsigned Code = Cursor.ReadCode();
3652         RecordData Record;
3653         StringRef Blob;
3654         bool shouldContinue = false;
3655         switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3656         case INPUT_FILE:
3657           shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3658           break;
3659         }
3660         if (!shouldContinue)
3661           break;
3662       }
3663       break;
3664     }
3665 
3666     default:
3667       // No other validation to perform.
3668       break;
3669     }
3670   }
3671 }
3672 
3673 
3674 bool ASTReader::isAcceptableASTFile(StringRef Filename,
3675                                     FileManager &FileMgr,
3676                                     const LangOptions &LangOpts,
3677                                     const TargetOptions &TargetOpts,
3678                                     const PreprocessorOptions &PPOpts) {
3679   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3680   return !readASTFileControlBlock(Filename, FileMgr, validator);
3681 }
3682 
3683 bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3684   // Enter the submodule block.
3685   if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3686     Error("malformed submodule block record in AST file");
3687     return true;
3688   }
3689 
3690   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3691   bool First = true;
3692   Module *CurrentModule = 0;
3693   RecordData Record;
3694   while (true) {
3695     llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3696 
3697     switch (Entry.Kind) {
3698     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3699     case llvm::BitstreamEntry::Error:
3700       Error("malformed block record in AST file");
3701       return true;
3702     case llvm::BitstreamEntry::EndBlock:
3703       return false;
3704     case llvm::BitstreamEntry::Record:
3705       // The interesting case.
3706       break;
3707     }
3708 
3709     // Read a record.
3710     StringRef Blob;
3711     Record.clear();
3712     switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
3713     default:  // Default behavior: ignore.
3714       break;
3715 
3716     case SUBMODULE_DEFINITION: {
3717       if (First) {
3718         Error("missing submodule metadata record at beginning of block");
3719         return true;
3720       }
3721 
3722       if (Record.size() < 8) {
3723         Error("malformed module definition");
3724         return true;
3725       }
3726 
3727       StringRef Name = Blob;
3728       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3729       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3730       bool IsFramework = Record[2];
3731       bool IsExplicit = Record[3];
3732       bool IsSystem = Record[4];
3733       bool InferSubmodules = Record[5];
3734       bool InferExplicitSubmodules = Record[6];
3735       bool InferExportWildcard = Record[7];
3736       bool ConfigMacrosExhaustive = Record[8];
3737 
3738       Module *ParentModule = 0;
3739       if (Parent)
3740         ParentModule = getSubmodule(Parent);
3741 
3742       // Retrieve this (sub)module from the module map, creating it if
3743       // necessary.
3744       CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3745                                                 IsFramework,
3746                                                 IsExplicit).first;
3747       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3748       if (GlobalIndex >= SubmodulesLoaded.size() ||
3749           SubmodulesLoaded[GlobalIndex]) {
3750         Error("too many submodules");
3751         return true;
3752       }
3753 
3754       if (!ParentModule) {
3755         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3756           if (CurFile != F.File) {
3757             if (!Diags.isDiagnosticInFlight()) {
3758               Diag(diag::err_module_file_conflict)
3759                 << CurrentModule->getTopLevelModuleName()
3760                 << CurFile->getName()
3761                 << F.File->getName();
3762             }
3763             return true;
3764           }
3765         }
3766 
3767         CurrentModule->setASTFile(F.File);
3768       }
3769 
3770       CurrentModule->IsFromModuleFile = true;
3771       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3772       CurrentModule->InferSubmodules = InferSubmodules;
3773       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3774       CurrentModule->InferExportWildcard = InferExportWildcard;
3775       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
3776       if (DeserializationListener)
3777         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3778 
3779       SubmodulesLoaded[GlobalIndex] = CurrentModule;
3780 
3781       // Clear out data that will be replaced by what is the module file.
3782       CurrentModule->LinkLibraries.clear();
3783       CurrentModule->ConfigMacros.clear();
3784       CurrentModule->UnresolvedConflicts.clear();
3785       CurrentModule->Conflicts.clear();
3786       break;
3787     }
3788 
3789     case SUBMODULE_UMBRELLA_HEADER: {
3790       if (First) {
3791         Error("missing submodule metadata record at beginning of block");
3792         return true;
3793       }
3794 
3795       if (!CurrentModule)
3796         break;
3797 
3798       if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
3799         if (!CurrentModule->getUmbrellaHeader())
3800           ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3801         else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3802           Error("mismatched umbrella headers in submodule");
3803           return true;
3804         }
3805       }
3806       break;
3807     }
3808 
3809     case SUBMODULE_HEADER: {
3810       if (First) {
3811         Error("missing submodule metadata record at beginning of block");
3812         return true;
3813       }
3814 
3815       if (!CurrentModule)
3816         break;
3817 
3818       // We lazily associate headers with their modules via the HeaderInfoTable.
3819       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3820       // of complete filenames or remove it entirely.
3821       break;
3822     }
3823 
3824     case SUBMODULE_EXCLUDED_HEADER: {
3825       if (First) {
3826         Error("missing submodule metadata record at beginning of block");
3827         return true;
3828       }
3829 
3830       if (!CurrentModule)
3831         break;
3832 
3833       // We lazily associate headers with their modules via the HeaderInfoTable.
3834       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3835       // of complete filenames or remove it entirely.
3836       break;
3837     }
3838 
3839     case SUBMODULE_PRIVATE_HEADER: {
3840       if (First) {
3841         Error("missing submodule metadata record at beginning of block");
3842         return true;
3843       }
3844 
3845       if (!CurrentModule)
3846         break;
3847 
3848       // We lazily associate headers with their modules via the HeaderInfoTable.
3849       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3850       // of complete filenames or remove it entirely.
3851       break;
3852     }
3853 
3854     case SUBMODULE_TOPHEADER: {
3855       if (First) {
3856         Error("missing submodule metadata record at beginning of block");
3857         return true;
3858       }
3859 
3860       if (!CurrentModule)
3861         break;
3862 
3863       CurrentModule->addTopHeaderFilename(Blob);
3864       break;
3865     }
3866 
3867     case SUBMODULE_UMBRELLA_DIR: {
3868       if (First) {
3869         Error("missing submodule metadata record at beginning of block");
3870         return true;
3871       }
3872 
3873       if (!CurrentModule)
3874         break;
3875 
3876       if (const DirectoryEntry *Umbrella
3877                                   = PP.getFileManager().getDirectory(Blob)) {
3878         if (!CurrentModule->getUmbrellaDir())
3879           ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3880         else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3881           Error("mismatched umbrella directories in submodule");
3882           return true;
3883         }
3884       }
3885       break;
3886     }
3887 
3888     case SUBMODULE_METADATA: {
3889       if (!First) {
3890         Error("submodule metadata record not at beginning of block");
3891         return true;
3892       }
3893       First = false;
3894 
3895       F.BaseSubmoduleID = getTotalNumSubmodules();
3896       F.LocalNumSubmodules = Record[0];
3897       unsigned LocalBaseSubmoduleID = Record[1];
3898       if (F.LocalNumSubmodules > 0) {
3899         // Introduce the global -> local mapping for submodules within this
3900         // module.
3901         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3902 
3903         // Introduce the local -> global mapping for submodules within this
3904         // module.
3905         F.SubmoduleRemap.insertOrReplace(
3906           std::make_pair(LocalBaseSubmoduleID,
3907                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
3908 
3909         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3910       }
3911       break;
3912     }
3913 
3914     case SUBMODULE_IMPORTS: {
3915       if (First) {
3916         Error("missing submodule metadata record at beginning of block");
3917         return true;
3918       }
3919 
3920       if (!CurrentModule)
3921         break;
3922 
3923       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3924         UnresolvedModuleRef Unresolved;
3925         Unresolved.File = &F;
3926         Unresolved.Mod = CurrentModule;
3927         Unresolved.ID = Record[Idx];
3928         Unresolved.Kind = UnresolvedModuleRef::Import;
3929         Unresolved.IsWildcard = false;
3930         UnresolvedModuleRefs.push_back(Unresolved);
3931       }
3932       break;
3933     }
3934 
3935     case SUBMODULE_EXPORTS: {
3936       if (First) {
3937         Error("missing submodule metadata record at beginning of block");
3938         return true;
3939       }
3940 
3941       if (!CurrentModule)
3942         break;
3943 
3944       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
3945         UnresolvedModuleRef Unresolved;
3946         Unresolved.File = &F;
3947         Unresolved.Mod = CurrentModule;
3948         Unresolved.ID = Record[Idx];
3949         Unresolved.Kind = UnresolvedModuleRef::Export;
3950         Unresolved.IsWildcard = Record[Idx + 1];
3951         UnresolvedModuleRefs.push_back(Unresolved);
3952       }
3953 
3954       // Once we've loaded the set of exports, there's no reason to keep
3955       // the parsed, unresolved exports around.
3956       CurrentModule->UnresolvedExports.clear();
3957       break;
3958     }
3959     case SUBMODULE_REQUIRES: {
3960       if (First) {
3961         Error("missing submodule metadata record at beginning of block");
3962         return true;
3963       }
3964 
3965       if (!CurrentModule)
3966         break;
3967 
3968       CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
3969                                     Context.getTargetInfo());
3970       break;
3971     }
3972 
3973     case SUBMODULE_LINK_LIBRARY:
3974       if (First) {
3975         Error("missing submodule metadata record at beginning of block");
3976         return true;
3977       }
3978 
3979       if (!CurrentModule)
3980         break;
3981 
3982       CurrentModule->LinkLibraries.push_back(
3983                                          Module::LinkLibrary(Blob, Record[0]));
3984       break;
3985 
3986     case SUBMODULE_CONFIG_MACRO:
3987       if (First) {
3988         Error("missing submodule metadata record at beginning of block");
3989         return true;
3990       }
3991 
3992       if (!CurrentModule)
3993         break;
3994 
3995       CurrentModule->ConfigMacros.push_back(Blob.str());
3996       break;
3997 
3998     case SUBMODULE_CONFLICT: {
3999       if (First) {
4000         Error("missing submodule metadata record at beginning of block");
4001         return true;
4002       }
4003 
4004       if (!CurrentModule)
4005         break;
4006 
4007       UnresolvedModuleRef Unresolved;
4008       Unresolved.File = &F;
4009       Unresolved.Mod = CurrentModule;
4010       Unresolved.ID = Record[0];
4011       Unresolved.Kind = UnresolvedModuleRef::Conflict;
4012       Unresolved.IsWildcard = false;
4013       Unresolved.String = Blob;
4014       UnresolvedModuleRefs.push_back(Unresolved);
4015       break;
4016     }
4017     }
4018   }
4019 }
4020 
4021 /// \brief Parse the record that corresponds to a LangOptions data
4022 /// structure.
4023 ///
4024 /// This routine parses the language options from the AST file and then gives
4025 /// them to the AST listener if one is set.
4026 ///
4027 /// \returns true if the listener deems the file unacceptable, false otherwise.
4028 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4029                                      bool Complain,
4030                                      ASTReaderListener &Listener) {
4031   LangOptions LangOpts;
4032   unsigned Idx = 0;
4033 #define LANGOPT(Name, Bits, Default, Description) \
4034   LangOpts.Name = Record[Idx++];
4035 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4036   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4037 #include "clang/Basic/LangOptions.def"
4038 #define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4039 #include "clang/Basic/Sanitizers.def"
4040 
4041   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4042   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4043   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4044 
4045   unsigned Length = Record[Idx++];
4046   LangOpts.CurrentModule.assign(Record.begin() + Idx,
4047                                 Record.begin() + Idx + Length);
4048 
4049   Idx += Length;
4050 
4051   // Comment options.
4052   for (unsigned N = Record[Idx++]; N; --N) {
4053     LangOpts.CommentOpts.BlockCommandNames.push_back(
4054       ReadString(Record, Idx));
4055   }
4056   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
4057 
4058   return Listener.ReadLanguageOptions(LangOpts, Complain);
4059 }
4060 
4061 bool ASTReader::ParseTargetOptions(const RecordData &Record,
4062                                    bool Complain,
4063                                    ASTReaderListener &Listener) {
4064   unsigned Idx = 0;
4065   TargetOptions TargetOpts;
4066   TargetOpts.Triple = ReadString(Record, Idx);
4067   TargetOpts.CPU = ReadString(Record, Idx);
4068   TargetOpts.ABI = ReadString(Record, Idx);
4069   TargetOpts.LinkerVersion = ReadString(Record, Idx);
4070   for (unsigned N = Record[Idx++]; N; --N) {
4071     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4072   }
4073   for (unsigned N = Record[Idx++]; N; --N) {
4074     TargetOpts.Features.push_back(ReadString(Record, Idx));
4075   }
4076 
4077   return Listener.ReadTargetOptions(TargetOpts, Complain);
4078 }
4079 
4080 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4081                                        ASTReaderListener &Listener) {
4082   DiagnosticOptions DiagOpts;
4083   unsigned Idx = 0;
4084 #define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4085 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4086   DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4087 #include "clang/Basic/DiagnosticOptions.def"
4088 
4089   for (unsigned N = Record[Idx++]; N; --N) {
4090     DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4091   }
4092 
4093   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4094 }
4095 
4096 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4097                                        ASTReaderListener &Listener) {
4098   FileSystemOptions FSOpts;
4099   unsigned Idx = 0;
4100   FSOpts.WorkingDir = ReadString(Record, Idx);
4101   return Listener.ReadFileSystemOptions(FSOpts, Complain);
4102 }
4103 
4104 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4105                                          bool Complain,
4106                                          ASTReaderListener &Listener) {
4107   HeaderSearchOptions HSOpts;
4108   unsigned Idx = 0;
4109   HSOpts.Sysroot = ReadString(Record, Idx);
4110 
4111   // Include entries.
4112   for (unsigned N = Record[Idx++]; N; --N) {
4113     std::string Path = ReadString(Record, Idx);
4114     frontend::IncludeDirGroup Group
4115       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4116     bool IsFramework = Record[Idx++];
4117     bool IgnoreSysRoot = Record[Idx++];
4118     HSOpts.UserEntries.push_back(
4119       HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
4120   }
4121 
4122   // System header prefixes.
4123   for (unsigned N = Record[Idx++]; N; --N) {
4124     std::string Prefix = ReadString(Record, Idx);
4125     bool IsSystemHeader = Record[Idx++];
4126     HSOpts.SystemHeaderPrefixes.push_back(
4127       HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4128   }
4129 
4130   HSOpts.ResourceDir = ReadString(Record, Idx);
4131   HSOpts.ModuleCachePath = ReadString(Record, Idx);
4132   HSOpts.DisableModuleHash = Record[Idx++];
4133   HSOpts.UseBuiltinIncludes = Record[Idx++];
4134   HSOpts.UseStandardSystemIncludes = Record[Idx++];
4135   HSOpts.UseStandardCXXIncludes = Record[Idx++];
4136   HSOpts.UseLibcxx = Record[Idx++];
4137 
4138   return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4139 }
4140 
4141 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4142                                          bool Complain,
4143                                          ASTReaderListener &Listener,
4144                                          std::string &SuggestedPredefines) {
4145   PreprocessorOptions PPOpts;
4146   unsigned Idx = 0;
4147 
4148   // Macro definitions/undefs
4149   for (unsigned N = Record[Idx++]; N; --N) {
4150     std::string Macro = ReadString(Record, Idx);
4151     bool IsUndef = Record[Idx++];
4152     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4153   }
4154 
4155   // Includes
4156   for (unsigned N = Record[Idx++]; N; --N) {
4157     PPOpts.Includes.push_back(ReadString(Record, Idx));
4158   }
4159 
4160   // Macro Includes
4161   for (unsigned N = Record[Idx++]; N; --N) {
4162     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4163   }
4164 
4165   PPOpts.UsePredefines = Record[Idx++];
4166   PPOpts.DetailedRecord = Record[Idx++];
4167   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4168   PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4169   PPOpts.ObjCXXARCStandardLibrary =
4170     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4171   SuggestedPredefines.clear();
4172   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4173                                           SuggestedPredefines);
4174 }
4175 
4176 std::pair<ModuleFile *, unsigned>
4177 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4178   GlobalPreprocessedEntityMapType::iterator
4179   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4180   assert(I != GlobalPreprocessedEntityMap.end() &&
4181          "Corrupted global preprocessed entity map");
4182   ModuleFile *M = I->second;
4183   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4184   return std::make_pair(M, LocalIndex);
4185 }
4186 
4187 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4188 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4189   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4190     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4191                                              Mod.NumPreprocessedEntities);
4192 
4193   return std::make_pair(PreprocessingRecord::iterator(),
4194                         PreprocessingRecord::iterator());
4195 }
4196 
4197 std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4198 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4199   return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4200                         ModuleDeclIterator(this, &Mod,
4201                                  Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4202 }
4203 
4204 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4205   PreprocessedEntityID PPID = Index+1;
4206   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4207   ModuleFile &M = *PPInfo.first;
4208   unsigned LocalIndex = PPInfo.second;
4209   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4210 
4211   if (!PP.getPreprocessingRecord()) {
4212     Error("no preprocessing record");
4213     return 0;
4214   }
4215 
4216   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4217   M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4218 
4219   llvm::BitstreamEntry Entry =
4220     M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4221   if (Entry.Kind != llvm::BitstreamEntry::Record)
4222     return 0;
4223 
4224   // Read the record.
4225   SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4226                     ReadSourceLocation(M, PPOffs.End));
4227   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4228   StringRef Blob;
4229   RecordData Record;
4230   PreprocessorDetailRecordTypes RecType =
4231     (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4232                                           Entry.ID, Record, &Blob);
4233   switch (RecType) {
4234   case PPD_MACRO_EXPANSION: {
4235     bool isBuiltin = Record[0];
4236     IdentifierInfo *Name = 0;
4237     MacroDefinition *Def = 0;
4238     if (isBuiltin)
4239       Name = getLocalIdentifier(M, Record[1]);
4240     else {
4241       PreprocessedEntityID
4242           GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4243       Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4244     }
4245 
4246     MacroExpansion *ME;
4247     if (isBuiltin)
4248       ME = new (PPRec) MacroExpansion(Name, Range);
4249     else
4250       ME = new (PPRec) MacroExpansion(Def, Range);
4251 
4252     return ME;
4253   }
4254 
4255   case PPD_MACRO_DEFINITION: {
4256     // Decode the identifier info and then check again; if the macro is
4257     // still defined and associated with the identifier,
4258     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4259     MacroDefinition *MD
4260       = new (PPRec) MacroDefinition(II, Range);
4261 
4262     if (DeserializationListener)
4263       DeserializationListener->MacroDefinitionRead(PPID, MD);
4264 
4265     return MD;
4266   }
4267 
4268   case PPD_INCLUSION_DIRECTIVE: {
4269     const char *FullFileNameStart = Blob.data() + Record[0];
4270     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
4271     const FileEntry *File = 0;
4272     if (!FullFileName.empty())
4273       File = PP.getFileManager().getFile(FullFileName);
4274 
4275     // FIXME: Stable encoding
4276     InclusionDirective::InclusionKind Kind
4277       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4278     InclusionDirective *ID
4279       = new (PPRec) InclusionDirective(PPRec, Kind,
4280                                        StringRef(Blob.data(), Record[0]),
4281                                        Record[1], Record[3],
4282                                        File,
4283                                        Range);
4284     return ID;
4285   }
4286   }
4287 
4288   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4289 }
4290 
4291 /// \brief \arg SLocMapI points at a chunk of a module that contains no
4292 /// preprocessed entities or the entities it contains are not the ones we are
4293 /// looking for. Find the next module that contains entities and return the ID
4294 /// of the first entry.
4295 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4296                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4297   ++SLocMapI;
4298   for (GlobalSLocOffsetMapType::const_iterator
4299          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4300     ModuleFile &M = *SLocMapI->second;
4301     if (M.NumPreprocessedEntities)
4302       return M.BasePreprocessedEntityID;
4303   }
4304 
4305   return getTotalNumPreprocessedEntities();
4306 }
4307 
4308 namespace {
4309 
4310 template <unsigned PPEntityOffset::*PPLoc>
4311 struct PPEntityComp {
4312   const ASTReader &Reader;
4313   ModuleFile &M;
4314 
4315   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4316 
4317   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4318     SourceLocation LHS = getLoc(L);
4319     SourceLocation RHS = getLoc(R);
4320     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4321   }
4322 
4323   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4324     SourceLocation LHS = getLoc(L);
4325     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4326   }
4327 
4328   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4329     SourceLocation RHS = getLoc(R);
4330     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4331   }
4332 
4333   SourceLocation getLoc(const PPEntityOffset &PPE) const {
4334     return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4335   }
4336 };
4337 
4338 }
4339 
4340 /// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4341 PreprocessedEntityID
4342 ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4343   if (SourceMgr.isLocalSourceLocation(BLoc))
4344     return getTotalNumPreprocessedEntities();
4345 
4346   GlobalSLocOffsetMapType::const_iterator
4347     SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4348                                         BLoc.getOffset() - 1);
4349   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4350          "Corrupted global sloc offset map");
4351 
4352   if (SLocMapI->second->NumPreprocessedEntities == 0)
4353     return findNextPreprocessedEntity(SLocMapI);
4354 
4355   ModuleFile &M = *SLocMapI->second;
4356   typedef const PPEntityOffset *pp_iterator;
4357   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4358   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4359 
4360   size_t Count = M.NumPreprocessedEntities;
4361   size_t Half;
4362   pp_iterator First = pp_begin;
4363   pp_iterator PPI;
4364 
4365   // Do a binary search manually instead of using std::lower_bound because
4366   // The end locations of entities may be unordered (when a macro expansion
4367   // is inside another macro argument), but for this case it is not important
4368   // whether we get the first macro expansion or its containing macro.
4369   while (Count > 0) {
4370     Half = Count/2;
4371     PPI = First;
4372     std::advance(PPI, Half);
4373     if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4374                                             BLoc)){
4375       First = PPI;
4376       ++First;
4377       Count = Count - Half - 1;
4378     } else
4379       Count = Half;
4380   }
4381 
4382   if (PPI == pp_end)
4383     return findNextPreprocessedEntity(SLocMapI);
4384 
4385   return M.BasePreprocessedEntityID + (PPI - pp_begin);
4386 }
4387 
4388 /// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4389 PreprocessedEntityID
4390 ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4391   if (SourceMgr.isLocalSourceLocation(ELoc))
4392     return getTotalNumPreprocessedEntities();
4393 
4394   GlobalSLocOffsetMapType::const_iterator
4395     SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4396                                         ELoc.getOffset() - 1);
4397   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4398          "Corrupted global sloc offset map");
4399 
4400   if (SLocMapI->second->NumPreprocessedEntities == 0)
4401     return findNextPreprocessedEntity(SLocMapI);
4402 
4403   ModuleFile &M = *SLocMapI->second;
4404   typedef const PPEntityOffset *pp_iterator;
4405   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4406   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4407   pp_iterator PPI =
4408       std::upper_bound(pp_begin, pp_end, ELoc,
4409                        PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4410 
4411   if (PPI == pp_end)
4412     return findNextPreprocessedEntity(SLocMapI);
4413 
4414   return M.BasePreprocessedEntityID + (PPI - pp_begin);
4415 }
4416 
4417 /// \brief Returns a pair of [Begin, End) indices of preallocated
4418 /// preprocessed entities that \arg Range encompasses.
4419 std::pair<unsigned, unsigned>
4420     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4421   if (Range.isInvalid())
4422     return std::make_pair(0,0);
4423   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4424 
4425   PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4426   PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4427   return std::make_pair(BeginID, EndID);
4428 }
4429 
4430 /// \brief Optionally returns true or false if the preallocated preprocessed
4431 /// entity with index \arg Index came from file \arg FID.
4432 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4433                                                              FileID FID) {
4434   if (FID.isInvalid())
4435     return false;
4436 
4437   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4438   ModuleFile &M = *PPInfo.first;
4439   unsigned LocalIndex = PPInfo.second;
4440   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4441 
4442   SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4443   if (Loc.isInvalid())
4444     return false;
4445 
4446   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4447     return true;
4448   else
4449     return false;
4450 }
4451 
4452 namespace {
4453   /// \brief Visitor used to search for information about a header file.
4454   class HeaderFileInfoVisitor {
4455     const FileEntry *FE;
4456 
4457     Optional<HeaderFileInfo> HFI;
4458 
4459   public:
4460     explicit HeaderFileInfoVisitor(const FileEntry *FE)
4461       : FE(FE) { }
4462 
4463     static bool visit(ModuleFile &M, void *UserData) {
4464       HeaderFileInfoVisitor *This
4465         = static_cast<HeaderFileInfoVisitor *>(UserData);
4466 
4467       HeaderFileInfoLookupTable *Table
4468         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4469       if (!Table)
4470         return false;
4471 
4472       // Look in the on-disk hash table for an entry for this file name.
4473       HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
4474       if (Pos == Table->end())
4475         return false;
4476 
4477       This->HFI = *Pos;
4478       return true;
4479     }
4480 
4481     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4482   };
4483 }
4484 
4485 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4486   HeaderFileInfoVisitor Visitor(FE);
4487   ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4488   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
4489     return *HFI;
4490 
4491   return HeaderFileInfo();
4492 }
4493 
4494 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4495   // FIXME: Make it work properly with modules.
4496   SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
4497   for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4498     ModuleFile &F = *(*I);
4499     unsigned Idx = 0;
4500     DiagStates.clear();
4501     assert(!Diag.DiagStates.empty());
4502     DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4503     while (Idx < F.PragmaDiagMappings.size()) {
4504       SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4505       unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4506       if (DiagStateID != 0) {
4507         Diag.DiagStatePoints.push_back(
4508                     DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4509                     FullSourceLoc(Loc, SourceMgr)));
4510         continue;
4511       }
4512 
4513       assert(DiagStateID == 0);
4514       // A new DiagState was created here.
4515       Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4516       DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4517       DiagStates.push_back(NewState);
4518       Diag.DiagStatePoints.push_back(
4519           DiagnosticsEngine::DiagStatePoint(NewState,
4520                                             FullSourceLoc(Loc, SourceMgr)));
4521       while (1) {
4522         assert(Idx < F.PragmaDiagMappings.size() &&
4523                "Invalid data, didn't find '-1' marking end of diag/map pairs");
4524         if (Idx >= F.PragmaDiagMappings.size()) {
4525           break; // Something is messed up but at least avoid infinite loop in
4526                  // release build.
4527         }
4528         unsigned DiagID = F.PragmaDiagMappings[Idx++];
4529         if (DiagID == (unsigned)-1) {
4530           break; // no more diag/map pairs for this location.
4531         }
4532         diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4533         DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4534         Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4535       }
4536     }
4537   }
4538 }
4539 
4540 /// \brief Get the correct cursor and offset for loading a type.
4541 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4542   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4543   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4544   ModuleFile *M = I->second;
4545   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4546 }
4547 
4548 /// \brief Read and return the type with the given index..
4549 ///
4550 /// The index is the type ID, shifted and minus the number of predefs. This
4551 /// routine actually reads the record corresponding to the type at the given
4552 /// location. It is a helper routine for GetType, which deals with reading type
4553 /// IDs.
4554 QualType ASTReader::readTypeRecord(unsigned Index) {
4555   RecordLocation Loc = TypeCursorForIndex(Index);
4556   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
4557 
4558   // Keep track of where we are in the stream, then jump back there
4559   // after reading this type.
4560   SavedStreamPosition SavedPosition(DeclsCursor);
4561 
4562   ReadingKindTracker ReadingKind(Read_Type, *this);
4563 
4564   // Note that we are loading a type record.
4565   Deserializing AType(this);
4566 
4567   unsigned Idx = 0;
4568   DeclsCursor.JumpToBit(Loc.Offset);
4569   RecordData Record;
4570   unsigned Code = DeclsCursor.ReadCode();
4571   switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
4572   case TYPE_EXT_QUAL: {
4573     if (Record.size() != 2) {
4574       Error("Incorrect encoding of extended qualifier type");
4575       return QualType();
4576     }
4577     QualType Base = readType(*Loc.F, Record, Idx);
4578     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4579     return Context.getQualifiedType(Base, Quals);
4580   }
4581 
4582   case TYPE_COMPLEX: {
4583     if (Record.size() != 1) {
4584       Error("Incorrect encoding of complex type");
4585       return QualType();
4586     }
4587     QualType ElemType = readType(*Loc.F, Record, Idx);
4588     return Context.getComplexType(ElemType);
4589   }
4590 
4591   case TYPE_POINTER: {
4592     if (Record.size() != 1) {
4593       Error("Incorrect encoding of pointer type");
4594       return QualType();
4595     }
4596     QualType PointeeType = readType(*Loc.F, Record, Idx);
4597     return Context.getPointerType(PointeeType);
4598   }
4599 
4600   case TYPE_DECAYED: {
4601     if (Record.size() != 1) {
4602       Error("Incorrect encoding of decayed type");
4603       return QualType();
4604     }
4605     QualType OriginalType = readType(*Loc.F, Record, Idx);
4606     QualType DT = Context.getAdjustedParameterType(OriginalType);
4607     if (!isa<DecayedType>(DT))
4608       Error("Decayed type does not decay");
4609     return DT;
4610   }
4611 
4612   case TYPE_ADJUSTED: {
4613     if (Record.size() != 2) {
4614       Error("Incorrect encoding of adjusted type");
4615       return QualType();
4616     }
4617     QualType OriginalTy = readType(*Loc.F, Record, Idx);
4618     QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4619     return Context.getAdjustedType(OriginalTy, AdjustedTy);
4620   }
4621 
4622   case TYPE_BLOCK_POINTER: {
4623     if (Record.size() != 1) {
4624       Error("Incorrect encoding of block pointer type");
4625       return QualType();
4626     }
4627     QualType PointeeType = readType(*Loc.F, Record, Idx);
4628     return Context.getBlockPointerType(PointeeType);
4629   }
4630 
4631   case TYPE_LVALUE_REFERENCE: {
4632     if (Record.size() != 2) {
4633       Error("Incorrect encoding of lvalue reference type");
4634       return QualType();
4635     }
4636     QualType PointeeType = readType(*Loc.F, Record, Idx);
4637     return Context.getLValueReferenceType(PointeeType, Record[1]);
4638   }
4639 
4640   case TYPE_RVALUE_REFERENCE: {
4641     if (Record.size() != 1) {
4642       Error("Incorrect encoding of rvalue reference type");
4643       return QualType();
4644     }
4645     QualType PointeeType = readType(*Loc.F, Record, Idx);
4646     return Context.getRValueReferenceType(PointeeType);
4647   }
4648 
4649   case TYPE_MEMBER_POINTER: {
4650     if (Record.size() != 2) {
4651       Error("Incorrect encoding of member pointer type");
4652       return QualType();
4653     }
4654     QualType PointeeType = readType(*Loc.F, Record, Idx);
4655     QualType ClassType = readType(*Loc.F, Record, Idx);
4656     if (PointeeType.isNull() || ClassType.isNull())
4657       return QualType();
4658 
4659     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4660   }
4661 
4662   case TYPE_CONSTANT_ARRAY: {
4663     QualType ElementType = readType(*Loc.F, Record, Idx);
4664     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4665     unsigned IndexTypeQuals = Record[2];
4666     unsigned Idx = 3;
4667     llvm::APInt Size = ReadAPInt(Record, Idx);
4668     return Context.getConstantArrayType(ElementType, Size,
4669                                          ASM, IndexTypeQuals);
4670   }
4671 
4672   case TYPE_INCOMPLETE_ARRAY: {
4673     QualType ElementType = readType(*Loc.F, Record, Idx);
4674     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4675     unsigned IndexTypeQuals = Record[2];
4676     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4677   }
4678 
4679   case TYPE_VARIABLE_ARRAY: {
4680     QualType ElementType = readType(*Loc.F, Record, Idx);
4681     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4682     unsigned IndexTypeQuals = Record[2];
4683     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4684     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4685     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4686                                          ASM, IndexTypeQuals,
4687                                          SourceRange(LBLoc, RBLoc));
4688   }
4689 
4690   case TYPE_VECTOR: {
4691     if (Record.size() != 3) {
4692       Error("incorrect encoding of vector type in AST file");
4693       return QualType();
4694     }
4695 
4696     QualType ElementType = readType(*Loc.F, Record, Idx);
4697     unsigned NumElements = Record[1];
4698     unsigned VecKind = Record[2];
4699     return Context.getVectorType(ElementType, NumElements,
4700                                   (VectorType::VectorKind)VecKind);
4701   }
4702 
4703   case TYPE_EXT_VECTOR: {
4704     if (Record.size() != 3) {
4705       Error("incorrect encoding of extended vector type in AST file");
4706       return QualType();
4707     }
4708 
4709     QualType ElementType = readType(*Loc.F, Record, Idx);
4710     unsigned NumElements = Record[1];
4711     return Context.getExtVectorType(ElementType, NumElements);
4712   }
4713 
4714   case TYPE_FUNCTION_NO_PROTO: {
4715     if (Record.size() != 6) {
4716       Error("incorrect encoding of no-proto function type");
4717       return QualType();
4718     }
4719     QualType ResultType = readType(*Loc.F, Record, Idx);
4720     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4721                                (CallingConv)Record[4], Record[5]);
4722     return Context.getFunctionNoProtoType(ResultType, Info);
4723   }
4724 
4725   case TYPE_FUNCTION_PROTO: {
4726     QualType ResultType = readType(*Loc.F, Record, Idx);
4727 
4728     FunctionProtoType::ExtProtoInfo EPI;
4729     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4730                                         /*hasregparm*/ Record[2],
4731                                         /*regparm*/ Record[3],
4732                                         static_cast<CallingConv>(Record[4]),
4733                                         /*produces*/ Record[5]);
4734 
4735     unsigned Idx = 6;
4736     unsigned NumParams = Record[Idx++];
4737     SmallVector<QualType, 16> ParamTypes;
4738     for (unsigned I = 0; I != NumParams; ++I)
4739       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4740 
4741     EPI.Variadic = Record[Idx++];
4742     EPI.HasTrailingReturn = Record[Idx++];
4743     EPI.TypeQuals = Record[Idx++];
4744     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4745     ExceptionSpecificationType EST =
4746         static_cast<ExceptionSpecificationType>(Record[Idx++]);
4747     EPI.ExceptionSpecType = EST;
4748     SmallVector<QualType, 2> Exceptions;
4749     if (EST == EST_Dynamic) {
4750       EPI.NumExceptions = Record[Idx++];
4751       for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4752         Exceptions.push_back(readType(*Loc.F, Record, Idx));
4753       EPI.Exceptions = Exceptions.data();
4754     } else if (EST == EST_ComputedNoexcept) {
4755       EPI.NoexceptExpr = ReadExpr(*Loc.F);
4756     } else if (EST == EST_Uninstantiated) {
4757       EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4758       EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4759     } else if (EST == EST_Unevaluated) {
4760       EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4761     }
4762     return Context.getFunctionType(ResultType, ParamTypes, EPI);
4763   }
4764 
4765   case TYPE_UNRESOLVED_USING: {
4766     unsigned Idx = 0;
4767     return Context.getTypeDeclType(
4768                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4769   }
4770 
4771   case TYPE_TYPEDEF: {
4772     if (Record.size() != 2) {
4773       Error("incorrect encoding of typedef type");
4774       return QualType();
4775     }
4776     unsigned Idx = 0;
4777     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4778     QualType Canonical = readType(*Loc.F, Record, Idx);
4779     if (!Canonical.isNull())
4780       Canonical = Context.getCanonicalType(Canonical);
4781     return Context.getTypedefType(Decl, Canonical);
4782   }
4783 
4784   case TYPE_TYPEOF_EXPR:
4785     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4786 
4787   case TYPE_TYPEOF: {
4788     if (Record.size() != 1) {
4789       Error("incorrect encoding of typeof(type) in AST file");
4790       return QualType();
4791     }
4792     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4793     return Context.getTypeOfType(UnderlyingType);
4794   }
4795 
4796   case TYPE_DECLTYPE: {
4797     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4798     return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4799   }
4800 
4801   case TYPE_UNARY_TRANSFORM: {
4802     QualType BaseType = readType(*Loc.F, Record, Idx);
4803     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4804     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4805     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4806   }
4807 
4808   case TYPE_AUTO: {
4809     QualType Deduced = readType(*Loc.F, Record, Idx);
4810     bool IsDecltypeAuto = Record[Idx++];
4811     bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4812     return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
4813   }
4814 
4815   case TYPE_RECORD: {
4816     if (Record.size() != 2) {
4817       Error("incorrect encoding of record type");
4818       return QualType();
4819     }
4820     unsigned Idx = 0;
4821     bool IsDependent = Record[Idx++];
4822     RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4823     RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4824     QualType T = Context.getRecordType(RD);
4825     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4826     return T;
4827   }
4828 
4829   case TYPE_ENUM: {
4830     if (Record.size() != 2) {
4831       Error("incorrect encoding of enum type");
4832       return QualType();
4833     }
4834     unsigned Idx = 0;
4835     bool IsDependent = Record[Idx++];
4836     QualType T
4837       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4838     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4839     return T;
4840   }
4841 
4842   case TYPE_ATTRIBUTED: {
4843     if (Record.size() != 3) {
4844       Error("incorrect encoding of attributed type");
4845       return QualType();
4846     }
4847     QualType modifiedType = readType(*Loc.F, Record, Idx);
4848     QualType equivalentType = readType(*Loc.F, Record, Idx);
4849     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4850     return Context.getAttributedType(kind, modifiedType, equivalentType);
4851   }
4852 
4853   case TYPE_PAREN: {
4854     if (Record.size() != 1) {
4855       Error("incorrect encoding of paren type");
4856       return QualType();
4857     }
4858     QualType InnerType = readType(*Loc.F, Record, Idx);
4859     return Context.getParenType(InnerType);
4860   }
4861 
4862   case TYPE_PACK_EXPANSION: {
4863     if (Record.size() != 2) {
4864       Error("incorrect encoding of pack expansion type");
4865       return QualType();
4866     }
4867     QualType Pattern = readType(*Loc.F, Record, Idx);
4868     if (Pattern.isNull())
4869       return QualType();
4870     Optional<unsigned> NumExpansions;
4871     if (Record[1])
4872       NumExpansions = Record[1] - 1;
4873     return Context.getPackExpansionType(Pattern, NumExpansions);
4874   }
4875 
4876   case TYPE_ELABORATED: {
4877     unsigned Idx = 0;
4878     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4879     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4880     QualType NamedType = readType(*Loc.F, Record, Idx);
4881     return Context.getElaboratedType(Keyword, NNS, NamedType);
4882   }
4883 
4884   case TYPE_OBJC_INTERFACE: {
4885     unsigned Idx = 0;
4886     ObjCInterfaceDecl *ItfD
4887       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4888     return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4889   }
4890 
4891   case TYPE_OBJC_OBJECT: {
4892     unsigned Idx = 0;
4893     QualType Base = readType(*Loc.F, Record, Idx);
4894     unsigned NumProtos = Record[Idx++];
4895     SmallVector<ObjCProtocolDecl*, 4> Protos;
4896     for (unsigned I = 0; I != NumProtos; ++I)
4897       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4898     return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4899   }
4900 
4901   case TYPE_OBJC_OBJECT_POINTER: {
4902     unsigned Idx = 0;
4903     QualType Pointee = readType(*Loc.F, Record, Idx);
4904     return Context.getObjCObjectPointerType(Pointee);
4905   }
4906 
4907   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4908     unsigned Idx = 0;
4909     QualType Parm = readType(*Loc.F, Record, Idx);
4910     QualType Replacement = readType(*Loc.F, Record, Idx);
4911     return
4912       Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4913                                             Replacement);
4914   }
4915 
4916   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4917     unsigned Idx = 0;
4918     QualType Parm = readType(*Loc.F, Record, Idx);
4919     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4920     return Context.getSubstTemplateTypeParmPackType(
4921                                                cast<TemplateTypeParmType>(Parm),
4922                                                      ArgPack);
4923   }
4924 
4925   case TYPE_INJECTED_CLASS_NAME: {
4926     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4927     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4928     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4929     // for AST reading, too much interdependencies.
4930     return
4931       QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4932   }
4933 
4934   case TYPE_TEMPLATE_TYPE_PARM: {
4935     unsigned Idx = 0;
4936     unsigned Depth = Record[Idx++];
4937     unsigned Index = Record[Idx++];
4938     bool Pack = Record[Idx++];
4939     TemplateTypeParmDecl *D
4940       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4941     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4942   }
4943 
4944   case TYPE_DEPENDENT_NAME: {
4945     unsigned Idx = 0;
4946     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4947     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4948     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4949     QualType Canon = readType(*Loc.F, Record, Idx);
4950     if (!Canon.isNull())
4951       Canon = Context.getCanonicalType(Canon);
4952     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4953   }
4954 
4955   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4956     unsigned Idx = 0;
4957     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4958     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4959     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4960     unsigned NumArgs = Record[Idx++];
4961     SmallVector<TemplateArgument, 8> Args;
4962     Args.reserve(NumArgs);
4963     while (NumArgs--)
4964       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4965     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4966                                                       Args.size(), Args.data());
4967   }
4968 
4969   case TYPE_DEPENDENT_SIZED_ARRAY: {
4970     unsigned Idx = 0;
4971 
4972     // ArrayType
4973     QualType ElementType = readType(*Loc.F, Record, Idx);
4974     ArrayType::ArraySizeModifier ASM
4975       = (ArrayType::ArraySizeModifier)Record[Idx++];
4976     unsigned IndexTypeQuals = Record[Idx++];
4977 
4978     // DependentSizedArrayType
4979     Expr *NumElts = ReadExpr(*Loc.F);
4980     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4981 
4982     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4983                                                IndexTypeQuals, Brackets);
4984   }
4985 
4986   case TYPE_TEMPLATE_SPECIALIZATION: {
4987     unsigned Idx = 0;
4988     bool IsDependent = Record[Idx++];
4989     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4990     SmallVector<TemplateArgument, 8> Args;
4991     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4992     QualType Underlying = readType(*Loc.F, Record, Idx);
4993     QualType T;
4994     if (Underlying.isNull())
4995       T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4996                                                           Args.size());
4997     else
4998       T = Context.getTemplateSpecializationType(Name, Args.data(),
4999                                                  Args.size(), Underlying);
5000     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5001     return T;
5002   }
5003 
5004   case TYPE_ATOMIC: {
5005     if (Record.size() != 1) {
5006       Error("Incorrect encoding of atomic type");
5007       return QualType();
5008     }
5009     QualType ValueType = readType(*Loc.F, Record, Idx);
5010     return Context.getAtomicType(ValueType);
5011   }
5012   }
5013   llvm_unreachable("Invalid TypeCode!");
5014 }
5015 
5016 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5017   ASTReader &Reader;
5018   ModuleFile &F;
5019   const ASTReader::RecordData &Record;
5020   unsigned &Idx;
5021 
5022   SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5023                                     unsigned &I) {
5024     return Reader.ReadSourceLocation(F, R, I);
5025   }
5026 
5027   template<typename T>
5028   T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5029     return Reader.ReadDeclAs<T>(F, Record, Idx);
5030   }
5031 
5032 public:
5033   TypeLocReader(ASTReader &Reader, ModuleFile &F,
5034                 const ASTReader::RecordData &Record, unsigned &Idx)
5035     : Reader(Reader), F(F), Record(Record), Idx(Idx)
5036   { }
5037 
5038   // We want compile-time assurance that we've enumerated all of
5039   // these, so unfortunately we have to declare them first, then
5040   // define them out-of-line.
5041 #define ABSTRACT_TYPELOC(CLASS, PARENT)
5042 #define TYPELOC(CLASS, PARENT) \
5043   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5044 #include "clang/AST/TypeLocNodes.def"
5045 
5046   void VisitFunctionTypeLoc(FunctionTypeLoc);
5047   void VisitArrayTypeLoc(ArrayTypeLoc);
5048 };
5049 
5050 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5051   // nothing to do
5052 }
5053 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5054   TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5055   if (TL.needsExtraLocalData()) {
5056     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5057     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5058     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5059     TL.setModeAttr(Record[Idx++]);
5060   }
5061 }
5062 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5063   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5064 }
5065 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5066   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5067 }
5068 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5069   // nothing to do
5070 }
5071 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5072   // nothing to do
5073 }
5074 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5075   TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5076 }
5077 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5078   TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5079 }
5080 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5081   TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5082 }
5083 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5084   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5085   TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5086 }
5087 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5088   TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5089   TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5090   if (Record[Idx++])
5091     TL.setSizeExpr(Reader.ReadExpr(F));
5092   else
5093     TL.setSizeExpr(0);
5094 }
5095 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5096   VisitArrayTypeLoc(TL);
5097 }
5098 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5099   VisitArrayTypeLoc(TL);
5100 }
5101 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5102   VisitArrayTypeLoc(TL);
5103 }
5104 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5105                                             DependentSizedArrayTypeLoc TL) {
5106   VisitArrayTypeLoc(TL);
5107 }
5108 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5109                                         DependentSizedExtVectorTypeLoc TL) {
5110   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5111 }
5112 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5113   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5114 }
5115 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5116   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5117 }
5118 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5119   TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5120   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5121   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5122   TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5123   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5124     TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5125   }
5126 }
5127 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5128   VisitFunctionTypeLoc(TL);
5129 }
5130 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5131   VisitFunctionTypeLoc(TL);
5132 }
5133 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5134   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5135 }
5136 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5137   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5138 }
5139 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5140   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5141   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5142   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5143 }
5144 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5145   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5146   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5147   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5148   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5149 }
5150 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5151   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5152 }
5153 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5154   TL.setKWLoc(ReadSourceLocation(Record, Idx));
5155   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5156   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5157   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5158 }
5159 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5160   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5161 }
5162 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5163   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5164 }
5165 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5166   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5167 }
5168 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5169   TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5170   if (TL.hasAttrOperand()) {
5171     SourceRange range;
5172     range.setBegin(ReadSourceLocation(Record, Idx));
5173     range.setEnd(ReadSourceLocation(Record, Idx));
5174     TL.setAttrOperandParensRange(range);
5175   }
5176   if (TL.hasAttrExprOperand()) {
5177     if (Record[Idx++])
5178       TL.setAttrExprOperand(Reader.ReadExpr(F));
5179     else
5180       TL.setAttrExprOperand(0);
5181   } else if (TL.hasAttrEnumOperand())
5182     TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5183 }
5184 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5185   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5186 }
5187 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5188                                             SubstTemplateTypeParmTypeLoc TL) {
5189   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5190 }
5191 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5192                                           SubstTemplateTypeParmPackTypeLoc TL) {
5193   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5194 }
5195 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5196                                            TemplateSpecializationTypeLoc TL) {
5197   TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5198   TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5199   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5200   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5201   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5202     TL.setArgLocInfo(i,
5203         Reader.GetTemplateArgumentLocInfo(F,
5204                                           TL.getTypePtr()->getArg(i).getKind(),
5205                                           Record, Idx));
5206 }
5207 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5208   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5209   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5210 }
5211 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5212   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5213   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5214 }
5215 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5216   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5217 }
5218 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5219   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5220   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5221   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5222 }
5223 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5224        DependentTemplateSpecializationTypeLoc TL) {
5225   TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5226   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5227   TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5228   TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5229   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5230   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5231   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5232     TL.setArgLocInfo(I,
5233         Reader.GetTemplateArgumentLocInfo(F,
5234                                           TL.getTypePtr()->getArg(I).getKind(),
5235                                           Record, Idx));
5236 }
5237 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5238   TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5239 }
5240 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5241   TL.setNameLoc(ReadSourceLocation(Record, Idx));
5242 }
5243 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5244   TL.setHasBaseTypeAsWritten(Record[Idx++]);
5245   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5246   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5247   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5248     TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5249 }
5250 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5251   TL.setStarLoc(ReadSourceLocation(Record, Idx));
5252 }
5253 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5254   TL.setKWLoc(ReadSourceLocation(Record, Idx));
5255   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5256   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5257 }
5258 
5259 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5260                                              const RecordData &Record,
5261                                              unsigned &Idx) {
5262   QualType InfoTy = readType(F, Record, Idx);
5263   if (InfoTy.isNull())
5264     return 0;
5265 
5266   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5267   TypeLocReader TLR(*this, F, Record, Idx);
5268   for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5269     TLR.Visit(TL);
5270   return TInfo;
5271 }
5272 
5273 QualType ASTReader::GetType(TypeID ID) {
5274   unsigned FastQuals = ID & Qualifiers::FastMask;
5275   unsigned Index = ID >> Qualifiers::FastWidth;
5276 
5277   if (Index < NUM_PREDEF_TYPE_IDS) {
5278     QualType T;
5279     switch ((PredefinedTypeIDs)Index) {
5280     case PREDEF_TYPE_NULL_ID: return QualType();
5281     case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5282     case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5283 
5284     case PREDEF_TYPE_CHAR_U_ID:
5285     case PREDEF_TYPE_CHAR_S_ID:
5286       // FIXME: Check that the signedness of CharTy is correct!
5287       T = Context.CharTy;
5288       break;
5289 
5290     case PREDEF_TYPE_UCHAR_ID:      T = Context.UnsignedCharTy;     break;
5291     case PREDEF_TYPE_USHORT_ID:     T = Context.UnsignedShortTy;    break;
5292     case PREDEF_TYPE_UINT_ID:       T = Context.UnsignedIntTy;      break;
5293     case PREDEF_TYPE_ULONG_ID:      T = Context.UnsignedLongTy;     break;
5294     case PREDEF_TYPE_ULONGLONG_ID:  T = Context.UnsignedLongLongTy; break;
5295     case PREDEF_TYPE_UINT128_ID:    T = Context.UnsignedInt128Ty;   break;
5296     case PREDEF_TYPE_SCHAR_ID:      T = Context.SignedCharTy;       break;
5297     case PREDEF_TYPE_WCHAR_ID:      T = Context.WCharTy;            break;
5298     case PREDEF_TYPE_SHORT_ID:      T = Context.ShortTy;            break;
5299     case PREDEF_TYPE_INT_ID:        T = Context.IntTy;              break;
5300     case PREDEF_TYPE_LONG_ID:       T = Context.LongTy;             break;
5301     case PREDEF_TYPE_LONGLONG_ID:   T = Context.LongLongTy;         break;
5302     case PREDEF_TYPE_INT128_ID:     T = Context.Int128Ty;           break;
5303     case PREDEF_TYPE_HALF_ID:       T = Context.HalfTy;             break;
5304     case PREDEF_TYPE_FLOAT_ID:      T = Context.FloatTy;            break;
5305     case PREDEF_TYPE_DOUBLE_ID:     T = Context.DoubleTy;           break;
5306     case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy;       break;
5307     case PREDEF_TYPE_OVERLOAD_ID:   T = Context.OverloadTy;         break;
5308     case PREDEF_TYPE_BOUND_MEMBER:  T = Context.BoundMemberTy;      break;
5309     case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy;     break;
5310     case PREDEF_TYPE_DEPENDENT_ID:  T = Context.DependentTy;        break;
5311     case PREDEF_TYPE_UNKNOWN_ANY:   T = Context.UnknownAnyTy;       break;
5312     case PREDEF_TYPE_NULLPTR_ID:    T = Context.NullPtrTy;          break;
5313     case PREDEF_TYPE_CHAR16_ID:     T = Context.Char16Ty;           break;
5314     case PREDEF_TYPE_CHAR32_ID:     T = Context.Char32Ty;           break;
5315     case PREDEF_TYPE_OBJC_ID:       T = Context.ObjCBuiltinIdTy;    break;
5316     case PREDEF_TYPE_OBJC_CLASS:    T = Context.ObjCBuiltinClassTy; break;
5317     case PREDEF_TYPE_OBJC_SEL:      T = Context.ObjCBuiltinSelTy;   break;
5318     case PREDEF_TYPE_IMAGE1D_ID:    T = Context.OCLImage1dTy;       break;
5319     case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5320     case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5321     case PREDEF_TYPE_IMAGE2D_ID:    T = Context.OCLImage2dTy;       break;
5322     case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5323     case PREDEF_TYPE_IMAGE3D_ID:    T = Context.OCLImage3dTy;       break;
5324     case PREDEF_TYPE_SAMPLER_ID:    T = Context.OCLSamplerTy;       break;
5325     case PREDEF_TYPE_EVENT_ID:      T = Context.OCLEventTy;         break;
5326     case PREDEF_TYPE_AUTO_DEDUCT:   T = Context.getAutoDeductType(); break;
5327 
5328     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5329       T = Context.getAutoRRefDeductType();
5330       break;
5331 
5332     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5333       T = Context.ARCUnbridgedCastTy;
5334       break;
5335 
5336     case PREDEF_TYPE_VA_LIST_TAG:
5337       T = Context.getVaListTagType();
5338       break;
5339 
5340     case PREDEF_TYPE_BUILTIN_FN:
5341       T = Context.BuiltinFnTy;
5342       break;
5343     }
5344 
5345     assert(!T.isNull() && "Unknown predefined type");
5346     return T.withFastQualifiers(FastQuals);
5347   }
5348 
5349   Index -= NUM_PREDEF_TYPE_IDS;
5350   assert(Index < TypesLoaded.size() && "Type index out-of-range");
5351   if (TypesLoaded[Index].isNull()) {
5352     TypesLoaded[Index] = readTypeRecord(Index);
5353     if (TypesLoaded[Index].isNull())
5354       return QualType();
5355 
5356     TypesLoaded[Index]->setFromAST();
5357     if (DeserializationListener)
5358       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5359                                         TypesLoaded[Index]);
5360   }
5361 
5362   return TypesLoaded[Index].withFastQualifiers(FastQuals);
5363 }
5364 
5365 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5366   return GetType(getGlobalTypeID(F, LocalID));
5367 }
5368 
5369 serialization::TypeID
5370 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5371   unsigned FastQuals = LocalID & Qualifiers::FastMask;
5372   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5373 
5374   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5375     return LocalID;
5376 
5377   ContinuousRangeMap<uint32_t, int, 2>::iterator I
5378     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5379   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5380 
5381   unsigned GlobalIndex = LocalIndex + I->second;
5382   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5383 }
5384 
5385 TemplateArgumentLocInfo
5386 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5387                                       TemplateArgument::ArgKind Kind,
5388                                       const RecordData &Record,
5389                                       unsigned &Index) {
5390   switch (Kind) {
5391   case TemplateArgument::Expression:
5392     return ReadExpr(F);
5393   case TemplateArgument::Type:
5394     return GetTypeSourceInfo(F, Record, Index);
5395   case TemplateArgument::Template: {
5396     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5397                                                                      Index);
5398     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5399     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5400                                    SourceLocation());
5401   }
5402   case TemplateArgument::TemplateExpansion: {
5403     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5404                                                                      Index);
5405     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5406     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5407     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5408                                    EllipsisLoc);
5409   }
5410   case TemplateArgument::Null:
5411   case TemplateArgument::Integral:
5412   case TemplateArgument::Declaration:
5413   case TemplateArgument::NullPtr:
5414   case TemplateArgument::Pack:
5415     // FIXME: Is this right?
5416     return TemplateArgumentLocInfo();
5417   }
5418   llvm_unreachable("unexpected template argument loc");
5419 }
5420 
5421 TemplateArgumentLoc
5422 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5423                                    const RecordData &Record, unsigned &Index) {
5424   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5425 
5426   if (Arg.getKind() == TemplateArgument::Expression) {
5427     if (Record[Index++]) // bool InfoHasSameExpr.
5428       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5429   }
5430   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5431                                                              Record, Index));
5432 }
5433 
5434 const ASTTemplateArgumentListInfo*
5435 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5436                                            const RecordData &Record,
5437                                            unsigned &Index) {
5438   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5439   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5440   unsigned NumArgsAsWritten = Record[Index++];
5441   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5442   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5443     TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5444   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5445 }
5446 
5447 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5448   return GetDecl(ID);
5449 }
5450 
5451 uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5452                                           unsigned &Idx){
5453   if (Idx >= Record.size())
5454     return 0;
5455 
5456   unsigned LocalID = Record[Idx++];
5457   return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5458 }
5459 
5460 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5461   RecordLocation Loc = getLocalBitOffset(Offset);
5462   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5463   SavedStreamPosition SavedPosition(Cursor);
5464   Cursor.JumpToBit(Loc.Offset);
5465   ReadingKindTracker ReadingKind(Read_Decl, *this);
5466   RecordData Record;
5467   unsigned Code = Cursor.ReadCode();
5468   unsigned RecCode = Cursor.readRecord(Code, Record);
5469   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5470     Error("Malformed AST file: missing C++ base specifiers");
5471     return 0;
5472   }
5473 
5474   unsigned Idx = 0;
5475   unsigned NumBases = Record[Idx++];
5476   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5477   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5478   for (unsigned I = 0; I != NumBases; ++I)
5479     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5480   return Bases;
5481 }
5482 
5483 serialization::DeclID
5484 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5485   if (LocalID < NUM_PREDEF_DECL_IDS)
5486     return LocalID;
5487 
5488   ContinuousRangeMap<uint32_t, int, 2>::iterator I
5489     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5490   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5491 
5492   return LocalID + I->second;
5493 }
5494 
5495 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5496                                    ModuleFile &M) const {
5497   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5498   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5499   return &M == I->second;
5500 }
5501 
5502 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
5503   if (!D->isFromASTFile())
5504     return 0;
5505   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5506   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5507   return I->second;
5508 }
5509 
5510 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5511   if (ID < NUM_PREDEF_DECL_IDS)
5512     return SourceLocation();
5513 
5514   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5515 
5516   if (Index > DeclsLoaded.size()) {
5517     Error("declaration ID out-of-range for AST file");
5518     return SourceLocation();
5519   }
5520 
5521   if (Decl *D = DeclsLoaded[Index])
5522     return D->getLocation();
5523 
5524   unsigned RawLocation = 0;
5525   RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5526   return ReadSourceLocation(*Rec.F, RawLocation);
5527 }
5528 
5529 Decl *ASTReader::GetDecl(DeclID ID) {
5530   if (ID < NUM_PREDEF_DECL_IDS) {
5531     switch ((PredefinedDeclIDs)ID) {
5532     case PREDEF_DECL_NULL_ID:
5533       return 0;
5534 
5535     case PREDEF_DECL_TRANSLATION_UNIT_ID:
5536       return Context.getTranslationUnitDecl();
5537 
5538     case PREDEF_DECL_OBJC_ID_ID:
5539       return Context.getObjCIdDecl();
5540 
5541     case PREDEF_DECL_OBJC_SEL_ID:
5542       return Context.getObjCSelDecl();
5543 
5544     case PREDEF_DECL_OBJC_CLASS_ID:
5545       return Context.getObjCClassDecl();
5546 
5547     case PREDEF_DECL_OBJC_PROTOCOL_ID:
5548       return Context.getObjCProtocolDecl();
5549 
5550     case PREDEF_DECL_INT_128_ID:
5551       return Context.getInt128Decl();
5552 
5553     case PREDEF_DECL_UNSIGNED_INT_128_ID:
5554       return Context.getUInt128Decl();
5555 
5556     case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5557       return Context.getObjCInstanceTypeDecl();
5558 
5559     case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5560       return Context.getBuiltinVaListDecl();
5561     }
5562   }
5563 
5564   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5565 
5566   if (Index >= DeclsLoaded.size()) {
5567     assert(0 && "declaration ID out-of-range for AST file");
5568     Error("declaration ID out-of-range for AST file");
5569     return 0;
5570   }
5571 
5572   if (!DeclsLoaded[Index]) {
5573     ReadDeclRecord(ID);
5574     if (DeserializationListener)
5575       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5576   }
5577 
5578   return DeclsLoaded[Index];
5579 }
5580 
5581 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5582                                                   DeclID GlobalID) {
5583   if (GlobalID < NUM_PREDEF_DECL_IDS)
5584     return GlobalID;
5585 
5586   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5587   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5588   ModuleFile *Owner = I->second;
5589 
5590   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5591     = M.GlobalToLocalDeclIDs.find(Owner);
5592   if (Pos == M.GlobalToLocalDeclIDs.end())
5593     return 0;
5594 
5595   return GlobalID - Owner->BaseDeclID + Pos->second;
5596 }
5597 
5598 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5599                                             const RecordData &Record,
5600                                             unsigned &Idx) {
5601   if (Idx >= Record.size()) {
5602     Error("Corrupted AST file");
5603     return 0;
5604   }
5605 
5606   return getGlobalDeclID(F, Record[Idx++]);
5607 }
5608 
5609 /// \brief Resolve the offset of a statement into a statement.
5610 ///
5611 /// This operation will read a new statement from the external
5612 /// source each time it is called, and is meant to be used via a
5613 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5614 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5615   // Switch case IDs are per Decl.
5616   ClearSwitchCaseIDs();
5617 
5618   // Offset here is a global offset across the entire chain.
5619   RecordLocation Loc = getLocalBitOffset(Offset);
5620   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5621   return ReadStmtFromStream(*Loc.F);
5622 }
5623 
5624 namespace {
5625   class FindExternalLexicalDeclsVisitor {
5626     ASTReader &Reader;
5627     const DeclContext *DC;
5628     bool (*isKindWeWant)(Decl::Kind);
5629 
5630     SmallVectorImpl<Decl*> &Decls;
5631     bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5632 
5633   public:
5634     FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5635                                     bool (*isKindWeWant)(Decl::Kind),
5636                                     SmallVectorImpl<Decl*> &Decls)
5637       : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5638     {
5639       for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5640         PredefsVisited[I] = false;
5641     }
5642 
5643     static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5644       if (Preorder)
5645         return false;
5646 
5647       FindExternalLexicalDeclsVisitor *This
5648         = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5649 
5650       ModuleFile::DeclContextInfosMap::iterator Info
5651         = M.DeclContextInfos.find(This->DC);
5652       if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5653         return false;
5654 
5655       // Load all of the declaration IDs
5656       for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5657                                *IDE = ID + Info->second.NumLexicalDecls;
5658            ID != IDE; ++ID) {
5659         if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5660           continue;
5661 
5662         // Don't add predefined declarations to the lexical context more
5663         // than once.
5664         if (ID->second < NUM_PREDEF_DECL_IDS) {
5665           if (This->PredefsVisited[ID->second])
5666             continue;
5667 
5668           This->PredefsVisited[ID->second] = true;
5669         }
5670 
5671         if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5672           if (!This->DC->isDeclInLexicalTraversal(D))
5673             This->Decls.push_back(D);
5674         }
5675       }
5676 
5677       return false;
5678     }
5679   };
5680 }
5681 
5682 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5683                                          bool (*isKindWeWant)(Decl::Kind),
5684                                          SmallVectorImpl<Decl*> &Decls) {
5685   // There might be lexical decls in multiple modules, for the TU at
5686   // least. Walk all of the modules in the order they were loaded.
5687   FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5688   ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5689   ++NumLexicalDeclContextsRead;
5690   return ELR_Success;
5691 }
5692 
5693 namespace {
5694 
5695 class DeclIDComp {
5696   ASTReader &Reader;
5697   ModuleFile &Mod;
5698 
5699 public:
5700   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5701 
5702   bool operator()(LocalDeclID L, LocalDeclID R) const {
5703     SourceLocation LHS = getLocation(L);
5704     SourceLocation RHS = getLocation(R);
5705     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5706   }
5707 
5708   bool operator()(SourceLocation LHS, LocalDeclID R) const {
5709     SourceLocation RHS = getLocation(R);
5710     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5711   }
5712 
5713   bool operator()(LocalDeclID L, SourceLocation RHS) const {
5714     SourceLocation LHS = getLocation(L);
5715     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5716   }
5717 
5718   SourceLocation getLocation(LocalDeclID ID) const {
5719     return Reader.getSourceManager().getFileLoc(
5720             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5721   }
5722 };
5723 
5724 }
5725 
5726 void ASTReader::FindFileRegionDecls(FileID File,
5727                                     unsigned Offset, unsigned Length,
5728                                     SmallVectorImpl<Decl *> &Decls) {
5729   SourceManager &SM = getSourceManager();
5730 
5731   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5732   if (I == FileDeclIDs.end())
5733     return;
5734 
5735   FileDeclsInfo &DInfo = I->second;
5736   if (DInfo.Decls.empty())
5737     return;
5738 
5739   SourceLocation
5740     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5741   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5742 
5743   DeclIDComp DIDComp(*this, *DInfo.Mod);
5744   ArrayRef<serialization::LocalDeclID>::iterator
5745     BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5746                                BeginLoc, DIDComp);
5747   if (BeginIt != DInfo.Decls.begin())
5748     --BeginIt;
5749 
5750   // If we are pointing at a top-level decl inside an objc container, we need
5751   // to backtrack until we find it otherwise we will fail to report that the
5752   // region overlaps with an objc container.
5753   while (BeginIt != DInfo.Decls.begin() &&
5754          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5755              ->isTopLevelDeclInObjCContainer())
5756     --BeginIt;
5757 
5758   ArrayRef<serialization::LocalDeclID>::iterator
5759     EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5760                              EndLoc, DIDComp);
5761   if (EndIt != DInfo.Decls.end())
5762     ++EndIt;
5763 
5764   for (ArrayRef<serialization::LocalDeclID>::iterator
5765          DIt = BeginIt; DIt != EndIt; ++DIt)
5766     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5767 }
5768 
5769 namespace {
5770   /// \brief ModuleFile visitor used to perform name lookup into a
5771   /// declaration context.
5772   class DeclContextNameLookupVisitor {
5773     ASTReader &Reader;
5774     SmallVectorImpl<const DeclContext *> &Contexts;
5775     DeclarationName Name;
5776     SmallVectorImpl<NamedDecl *> &Decls;
5777 
5778   public:
5779     DeclContextNameLookupVisitor(ASTReader &Reader,
5780                                  SmallVectorImpl<const DeclContext *> &Contexts,
5781                                  DeclarationName Name,
5782                                  SmallVectorImpl<NamedDecl *> &Decls)
5783       : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5784 
5785     static bool visit(ModuleFile &M, void *UserData) {
5786       DeclContextNameLookupVisitor *This
5787         = static_cast<DeclContextNameLookupVisitor *>(UserData);
5788 
5789       // Check whether we have any visible declaration information for
5790       // this context in this module.
5791       ModuleFile::DeclContextInfosMap::iterator Info;
5792       bool FoundInfo = false;
5793       for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5794         Info = M.DeclContextInfos.find(This->Contexts[I]);
5795         if (Info != M.DeclContextInfos.end() &&
5796             Info->second.NameLookupTableData) {
5797           FoundInfo = true;
5798           break;
5799         }
5800       }
5801 
5802       if (!FoundInfo)
5803         return false;
5804 
5805       // Look for this name within this module.
5806       ASTDeclContextNameLookupTable *LookupTable =
5807         Info->second.NameLookupTableData;
5808       ASTDeclContextNameLookupTable::iterator Pos
5809         = LookupTable->find(This->Name);
5810       if (Pos == LookupTable->end())
5811         return false;
5812 
5813       bool FoundAnything = false;
5814       ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5815       for (; Data.first != Data.second; ++Data.first) {
5816         NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5817         if (!ND)
5818           continue;
5819 
5820         if (ND->getDeclName() != This->Name) {
5821           // A name might be null because the decl's redeclarable part is
5822           // currently read before reading its name. The lookup is triggered by
5823           // building that decl (likely indirectly), and so it is later in the
5824           // sense of "already existing" and can be ignored here.
5825           continue;
5826         }
5827 
5828         // Record this declaration.
5829         FoundAnything = true;
5830         This->Decls.push_back(ND);
5831       }
5832 
5833       return FoundAnything;
5834     }
5835   };
5836 }
5837 
5838 /// \brief Retrieve the "definitive" module file for the definition of the
5839 /// given declaration context, if there is one.
5840 ///
5841 /// The "definitive" module file is the only place where we need to look to
5842 /// find information about the declarations within the given declaration
5843 /// context. For example, C++ and Objective-C classes, C structs/unions, and
5844 /// Objective-C protocols, categories, and extensions are all defined in a
5845 /// single place in the source code, so they have definitive module files
5846 /// associated with them. C++ namespaces, on the other hand, can have
5847 /// definitions in multiple different module files.
5848 ///
5849 /// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5850 /// NDEBUG checking.
5851 static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5852                                               ASTReader &Reader) {
5853   if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5854     return Reader.getOwningModuleFile(cast<Decl>(DefDC));
5855 
5856   return 0;
5857 }
5858 
5859 bool
5860 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5861                                           DeclarationName Name) {
5862   assert(DC->hasExternalVisibleStorage() &&
5863          "DeclContext has no visible decls in storage");
5864   if (!Name)
5865     return false;
5866 
5867   SmallVector<NamedDecl *, 64> Decls;
5868 
5869   // Compute the declaration contexts we need to look into. Multiple such
5870   // declaration contexts occur when two declaration contexts from disjoint
5871   // modules get merged, e.g., when two namespaces with the same name are
5872   // independently defined in separate modules.
5873   SmallVector<const DeclContext *, 2> Contexts;
5874   Contexts.push_back(DC);
5875 
5876   if (DC->isNamespace()) {
5877     MergedDeclsMap::iterator Merged
5878       = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5879     if (Merged != MergedDecls.end()) {
5880       for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5881         Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5882     }
5883   }
5884 
5885   DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
5886 
5887   // If we can definitively determine which module file to look into,
5888   // only look there. Otherwise, look in all module files.
5889   ModuleFile *Definitive;
5890   if (Contexts.size() == 1 &&
5891       (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5892     DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5893   } else {
5894     ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5895   }
5896   ++NumVisibleDeclContextsRead;
5897   SetExternalVisibleDeclsForName(DC, Name, Decls);
5898   return !Decls.empty();
5899 }
5900 
5901 namespace {
5902   /// \brief ModuleFile visitor used to retrieve all visible names in a
5903   /// declaration context.
5904   class DeclContextAllNamesVisitor {
5905     ASTReader &Reader;
5906     SmallVectorImpl<const DeclContext *> &Contexts;
5907     DeclsMap &Decls;
5908     bool VisitAll;
5909 
5910   public:
5911     DeclContextAllNamesVisitor(ASTReader &Reader,
5912                                SmallVectorImpl<const DeclContext *> &Contexts,
5913                                DeclsMap &Decls, bool VisitAll)
5914       : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
5915 
5916     static bool visit(ModuleFile &M, void *UserData) {
5917       DeclContextAllNamesVisitor *This
5918         = static_cast<DeclContextAllNamesVisitor *>(UserData);
5919 
5920       // Check whether we have any visible declaration information for
5921       // this context in this module.
5922       ModuleFile::DeclContextInfosMap::iterator Info;
5923       bool FoundInfo = false;
5924       for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5925         Info = M.DeclContextInfos.find(This->Contexts[I]);
5926         if (Info != M.DeclContextInfos.end() &&
5927             Info->second.NameLookupTableData) {
5928           FoundInfo = true;
5929           break;
5930         }
5931       }
5932 
5933       if (!FoundInfo)
5934         return false;
5935 
5936       ASTDeclContextNameLookupTable *LookupTable =
5937         Info->second.NameLookupTableData;
5938       bool FoundAnything = false;
5939       for (ASTDeclContextNameLookupTable::data_iterator
5940              I = LookupTable->data_begin(), E = LookupTable->data_end();
5941            I != E;
5942            ++I) {
5943         ASTDeclContextNameLookupTrait::data_type Data = *I;
5944         for (; Data.first != Data.second; ++Data.first) {
5945           NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5946                                                                  *Data.first);
5947           if (!ND)
5948             continue;
5949 
5950           // Record this declaration.
5951           FoundAnything = true;
5952           This->Decls[ND->getDeclName()].push_back(ND);
5953         }
5954       }
5955 
5956       return FoundAnything && !This->VisitAll;
5957     }
5958   };
5959 }
5960 
5961 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5962   if (!DC->hasExternalVisibleStorage())
5963     return;
5964   DeclsMap Decls;
5965 
5966   // Compute the declaration contexts we need to look into. Multiple such
5967   // declaration contexts occur when two declaration contexts from disjoint
5968   // modules get merged, e.g., when two namespaces with the same name are
5969   // independently defined in separate modules.
5970   SmallVector<const DeclContext *, 2> Contexts;
5971   Contexts.push_back(DC);
5972 
5973   if (DC->isNamespace()) {
5974     MergedDeclsMap::iterator Merged
5975       = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5976     if (Merged != MergedDecls.end()) {
5977       for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5978         Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5979     }
5980   }
5981 
5982   DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5983                                      /*VisitAll=*/DC->isFileContext());
5984   ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5985   ++NumVisibleDeclContextsRead;
5986 
5987   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5988     SetExternalVisibleDeclsForName(DC, I->first, I->second);
5989   }
5990   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5991 }
5992 
5993 /// \brief Under non-PCH compilation the consumer receives the objc methods
5994 /// before receiving the implementation, and codegen depends on this.
5995 /// We simulate this by deserializing and passing to consumer the methods of the
5996 /// implementation before passing the deserialized implementation decl.
5997 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5998                                        ASTConsumer *Consumer) {
5999   assert(ImplD && Consumer);
6000 
6001   for (ObjCImplDecl::method_iterator
6002          I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
6003     Consumer->HandleInterestingDecl(DeclGroupRef(*I));
6004 
6005   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6006 }
6007 
6008 void ASTReader::PassInterestingDeclsToConsumer() {
6009   assert(Consumer);
6010   while (!InterestingDecls.empty()) {
6011     Decl *D = InterestingDecls.front();
6012     InterestingDecls.pop_front();
6013 
6014     PassInterestingDeclToConsumer(D);
6015   }
6016 }
6017 
6018 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6019   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6020     PassObjCImplDeclToConsumer(ImplD, Consumer);
6021   else
6022     Consumer->HandleInterestingDecl(DeclGroupRef(D));
6023 }
6024 
6025 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6026   this->Consumer = Consumer;
6027 
6028   if (!Consumer)
6029     return;
6030 
6031   for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
6032     // Force deserialization of this decl, which will cause it to be queued for
6033     // passing to the consumer.
6034     GetDecl(EagerlyDeserializedDecls[I]);
6035   }
6036   EagerlyDeserializedDecls.clear();
6037 
6038   PassInterestingDeclsToConsumer();
6039 }
6040 
6041 void ASTReader::PrintStats() {
6042   std::fprintf(stderr, "*** AST File Statistics:\n");
6043 
6044   unsigned NumTypesLoaded
6045     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6046                                       QualType());
6047   unsigned NumDeclsLoaded
6048     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6049                                       (Decl *)0);
6050   unsigned NumIdentifiersLoaded
6051     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6052                                             IdentifiersLoaded.end(),
6053                                             (IdentifierInfo *)0);
6054   unsigned NumMacrosLoaded
6055     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6056                                        MacrosLoaded.end(),
6057                                        (MacroInfo *)0);
6058   unsigned NumSelectorsLoaded
6059     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6060                                           SelectorsLoaded.end(),
6061                                           Selector());
6062 
6063   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6064     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
6065                  NumSLocEntriesRead, TotalNumSLocEntries,
6066                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6067   if (!TypesLoaded.empty())
6068     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
6069                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
6070                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6071   if (!DeclsLoaded.empty())
6072     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
6073                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6074                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6075   if (!IdentifiersLoaded.empty())
6076     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
6077                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6078                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6079   if (!MacrosLoaded.empty())
6080     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
6081                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6082                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6083   if (!SelectorsLoaded.empty())
6084     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
6085                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6086                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6087   if (TotalNumStatements)
6088     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
6089                  NumStatementsRead, TotalNumStatements,
6090                  ((float)NumStatementsRead/TotalNumStatements * 100));
6091   if (TotalNumMacros)
6092     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
6093                  NumMacrosRead, TotalNumMacros,
6094                  ((float)NumMacrosRead/TotalNumMacros * 100));
6095   if (TotalLexicalDeclContexts)
6096     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
6097                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6098                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6099                   * 100));
6100   if (TotalVisibleDeclContexts)
6101     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
6102                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6103                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6104                   * 100));
6105   if (TotalNumMethodPoolEntries) {
6106     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
6107                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6108                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6109                   * 100));
6110   }
6111   if (NumMethodPoolLookups) {
6112     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
6113                  NumMethodPoolHits, NumMethodPoolLookups,
6114                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6115   }
6116   if (NumMethodPoolTableLookups) {
6117     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
6118                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
6119                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6120                   * 100.0));
6121   }
6122 
6123   if (NumIdentifierLookupHits) {
6124     std::fprintf(stderr,
6125                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
6126                  NumIdentifierLookupHits, NumIdentifierLookups,
6127                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6128   }
6129 
6130   if (GlobalIndex) {
6131     std::fprintf(stderr, "\n");
6132     GlobalIndex->printStats();
6133   }
6134 
6135   std::fprintf(stderr, "\n");
6136   dump();
6137   std::fprintf(stderr, "\n");
6138 }
6139 
6140 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6141 static void
6142 dumpModuleIDMap(StringRef Name,
6143                 const ContinuousRangeMap<Key, ModuleFile *,
6144                                          InitialCapacity> &Map) {
6145   if (Map.begin() == Map.end())
6146     return;
6147 
6148   typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6149   llvm::errs() << Name << ":\n";
6150   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6151        I != IEnd; ++I) {
6152     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
6153       << "\n";
6154   }
6155 }
6156 
6157 void ASTReader::dump() {
6158   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6159   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6160   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6161   dumpModuleIDMap("Global type map", GlobalTypeMap);
6162   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6163   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6164   dumpModuleIDMap("Global macro map", GlobalMacroMap);
6165   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6166   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6167   dumpModuleIDMap("Global preprocessed entity map",
6168                   GlobalPreprocessedEntityMap);
6169 
6170   llvm::errs() << "\n*** PCH/Modules Loaded:";
6171   for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6172                                        MEnd = ModuleMgr.end();
6173        M != MEnd; ++M)
6174     (*M)->dump();
6175 }
6176 
6177 /// Return the amount of memory used by memory buffers, breaking down
6178 /// by heap-backed versus mmap'ed memory.
6179 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6180   for (ModuleConstIterator I = ModuleMgr.begin(),
6181       E = ModuleMgr.end(); I != E; ++I) {
6182     if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6183       size_t bytes = buf->getBufferSize();
6184       switch (buf->getBufferKind()) {
6185         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6186           sizes.malloc_bytes += bytes;
6187           break;
6188         case llvm::MemoryBuffer::MemoryBuffer_MMap:
6189           sizes.mmap_bytes += bytes;
6190           break;
6191       }
6192     }
6193   }
6194 }
6195 
6196 void ASTReader::InitializeSema(Sema &S) {
6197   SemaObj = &S;
6198   S.addExternalSource(this);
6199 
6200   // Makes sure any declarations that were deserialized "too early"
6201   // still get added to the identifier's declaration chains.
6202   for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
6203     pushExternalDeclIntoScope(PreloadedDecls[I],
6204                               PreloadedDecls[I]->getDeclName());
6205   }
6206   PreloadedDecls.clear();
6207 
6208   // FIXME: What happens if these are changed by a module import?
6209   if (!FPPragmaOptions.empty()) {
6210     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6211     SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6212   }
6213 
6214   // FIXME: What happens if these are changed by a module import?
6215   if (!OpenCLExtensions.empty()) {
6216     unsigned I = 0;
6217 #define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6218 #include "clang/Basic/OpenCLExtensions.def"
6219 
6220     assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6221   }
6222 
6223   UpdateSema();
6224 }
6225 
6226 void ASTReader::UpdateSema() {
6227   assert(SemaObj && "no Sema to update");
6228 
6229   // Load the offsets of the declarations that Sema references.
6230   // They will be lazily deserialized when needed.
6231   if (!SemaDeclRefs.empty()) {
6232     assert(SemaDeclRefs.size() % 2 == 0);
6233     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6234       if (!SemaObj->StdNamespace)
6235         SemaObj->StdNamespace = SemaDeclRefs[I];
6236       if (!SemaObj->StdBadAlloc)
6237         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6238     }
6239     SemaDeclRefs.clear();
6240   }
6241 }
6242 
6243 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6244   // Note that we are loading an identifier.
6245   Deserializing AnIdentifier(this);
6246   StringRef Name(NameStart, NameEnd - NameStart);
6247 
6248   // If there is a global index, look there first to determine which modules
6249   // provably do not have any results for this identifier.
6250   GlobalModuleIndex::HitSet Hits;
6251   GlobalModuleIndex::HitSet *HitsPtr = 0;
6252   if (!loadGlobalIndex()) {
6253     if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6254       HitsPtr = &Hits;
6255     }
6256   }
6257   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
6258                                   NumIdentifierLookups,
6259                                   NumIdentifierLookupHits);
6260   ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
6261   IdentifierInfo *II = Visitor.getIdentifierInfo();
6262   markIdentifierUpToDate(II);
6263   return II;
6264 }
6265 
6266 namespace clang {
6267   /// \brief An identifier-lookup iterator that enumerates all of the
6268   /// identifiers stored within a set of AST files.
6269   class ASTIdentifierIterator : public IdentifierIterator {
6270     /// \brief The AST reader whose identifiers are being enumerated.
6271     const ASTReader &Reader;
6272 
6273     /// \brief The current index into the chain of AST files stored in
6274     /// the AST reader.
6275     unsigned Index;
6276 
6277     /// \brief The current position within the identifier lookup table
6278     /// of the current AST file.
6279     ASTIdentifierLookupTable::key_iterator Current;
6280 
6281     /// \brief The end position within the identifier lookup table of
6282     /// the current AST file.
6283     ASTIdentifierLookupTable::key_iterator End;
6284 
6285   public:
6286     explicit ASTIdentifierIterator(const ASTReader &Reader);
6287 
6288     virtual StringRef Next();
6289   };
6290 }
6291 
6292 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6293   : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6294   ASTIdentifierLookupTable *IdTable
6295     = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6296   Current = IdTable->key_begin();
6297   End = IdTable->key_end();
6298 }
6299 
6300 StringRef ASTIdentifierIterator::Next() {
6301   while (Current == End) {
6302     // If we have exhausted all of our AST files, we're done.
6303     if (Index == 0)
6304       return StringRef();
6305 
6306     --Index;
6307     ASTIdentifierLookupTable *IdTable
6308       = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6309         IdentifierLookupTable;
6310     Current = IdTable->key_begin();
6311     End = IdTable->key_end();
6312   }
6313 
6314   // We have any identifiers remaining in the current AST file; return
6315   // the next one.
6316   StringRef Result = *Current;
6317   ++Current;
6318   return Result;
6319 }
6320 
6321 IdentifierIterator *ASTReader::getIdentifiers() {
6322   if (!loadGlobalIndex())
6323     return GlobalIndex->createIdentifierIterator();
6324 
6325   return new ASTIdentifierIterator(*this);
6326 }
6327 
6328 namespace clang { namespace serialization {
6329   class ReadMethodPoolVisitor {
6330     ASTReader &Reader;
6331     Selector Sel;
6332     unsigned PriorGeneration;
6333     unsigned InstanceBits;
6334     unsigned FactoryBits;
6335     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6336     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
6337 
6338   public:
6339     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6340                           unsigned PriorGeneration)
6341       : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6342         InstanceBits(0), FactoryBits(0) { }
6343 
6344     static bool visit(ModuleFile &M, void *UserData) {
6345       ReadMethodPoolVisitor *This
6346         = static_cast<ReadMethodPoolVisitor *>(UserData);
6347 
6348       if (!M.SelectorLookupTable)
6349         return false;
6350 
6351       // If we've already searched this module file, skip it now.
6352       if (M.Generation <= This->PriorGeneration)
6353         return true;
6354 
6355       ++This->Reader.NumMethodPoolTableLookups;
6356       ASTSelectorLookupTable *PoolTable
6357         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6358       ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6359       if (Pos == PoolTable->end())
6360         return false;
6361 
6362       ++This->Reader.NumMethodPoolTableHits;
6363       ++This->Reader.NumSelectorsRead;
6364       // FIXME: Not quite happy with the statistics here. We probably should
6365       // disable this tracking when called via LoadSelector.
6366       // Also, should entries without methods count as misses?
6367       ++This->Reader.NumMethodPoolEntriesRead;
6368       ASTSelectorLookupTrait::data_type Data = *Pos;
6369       if (This->Reader.DeserializationListener)
6370         This->Reader.DeserializationListener->SelectorRead(Data.ID,
6371                                                            This->Sel);
6372 
6373       This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6374       This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6375       This->InstanceBits = Data.InstanceBits;
6376       This->FactoryBits = Data.FactoryBits;
6377       return true;
6378     }
6379 
6380     /// \brief Retrieve the instance methods found by this visitor.
6381     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6382       return InstanceMethods;
6383     }
6384 
6385     /// \brief Retrieve the instance methods found by this visitor.
6386     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6387       return FactoryMethods;
6388     }
6389 
6390     unsigned getInstanceBits() const { return InstanceBits; }
6391     unsigned getFactoryBits() const { return FactoryBits; }
6392   };
6393 } } // end namespace clang::serialization
6394 
6395 /// \brief Add the given set of methods to the method list.
6396 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6397                              ObjCMethodList &List) {
6398   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6399     S.addMethodToGlobalList(&List, Methods[I]);
6400   }
6401 }
6402 
6403 void ASTReader::ReadMethodPool(Selector Sel) {
6404   // Get the selector generation and update it to the current generation.
6405   unsigned &Generation = SelectorGeneration[Sel];
6406   unsigned PriorGeneration = Generation;
6407   Generation = CurrentGeneration;
6408 
6409   // Search for methods defined with this selector.
6410   ++NumMethodPoolLookups;
6411   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6412   ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6413 
6414   if (Visitor.getInstanceMethods().empty() &&
6415       Visitor.getFactoryMethods().empty())
6416     return;
6417 
6418   ++NumMethodPoolHits;
6419 
6420   if (!getSema())
6421     return;
6422 
6423   Sema &S = *getSema();
6424   Sema::GlobalMethodPool::iterator Pos
6425     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6426 
6427   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6428   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6429   Pos->second.first.setBits(Visitor.getInstanceBits());
6430   Pos->second.second.setBits(Visitor.getFactoryBits());
6431 }
6432 
6433 void ASTReader::ReadKnownNamespaces(
6434                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6435   Namespaces.clear();
6436 
6437   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6438     if (NamespaceDecl *Namespace
6439                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6440       Namespaces.push_back(Namespace);
6441   }
6442 }
6443 
6444 void ASTReader::ReadUndefinedButUsed(
6445                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
6446   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6447     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
6448     SourceLocation Loc =
6449         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
6450     Undefined.insert(std::make_pair(D, Loc));
6451   }
6452 }
6453 
6454 void ASTReader::ReadTentativeDefinitions(
6455                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
6456   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6457     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6458     if (Var)
6459       TentativeDefs.push_back(Var);
6460   }
6461   TentativeDefinitions.clear();
6462 }
6463 
6464 void ASTReader::ReadUnusedFileScopedDecls(
6465                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6466   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6467     DeclaratorDecl *D
6468       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6469     if (D)
6470       Decls.push_back(D);
6471   }
6472   UnusedFileScopedDecls.clear();
6473 }
6474 
6475 void ASTReader::ReadDelegatingConstructors(
6476                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6477   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6478     CXXConstructorDecl *D
6479       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6480     if (D)
6481       Decls.push_back(D);
6482   }
6483   DelegatingCtorDecls.clear();
6484 }
6485 
6486 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6487   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6488     TypedefNameDecl *D
6489       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6490     if (D)
6491       Decls.push_back(D);
6492   }
6493   ExtVectorDecls.clear();
6494 }
6495 
6496 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6497   for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6498     CXXRecordDecl *D
6499       = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6500     if (D)
6501       Decls.push_back(D);
6502   }
6503   DynamicClasses.clear();
6504 }
6505 
6506 void
6507 ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6508   for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6509     NamedDecl *D
6510       = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
6511     if (D)
6512       Decls.push_back(D);
6513   }
6514   LocallyScopedExternCDecls.clear();
6515 }
6516 
6517 void ASTReader::ReadReferencedSelectors(
6518        SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6519   if (ReferencedSelectorsData.empty())
6520     return;
6521 
6522   // If there are @selector references added them to its pool. This is for
6523   // implementation of -Wselector.
6524   unsigned int DataSize = ReferencedSelectorsData.size()-1;
6525   unsigned I = 0;
6526   while (I < DataSize) {
6527     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6528     SourceLocation SelLoc
6529       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6530     Sels.push_back(std::make_pair(Sel, SelLoc));
6531   }
6532   ReferencedSelectorsData.clear();
6533 }
6534 
6535 void ASTReader::ReadWeakUndeclaredIdentifiers(
6536        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6537   if (WeakUndeclaredIdentifiers.empty())
6538     return;
6539 
6540   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6541     IdentifierInfo *WeakId
6542       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6543     IdentifierInfo *AliasId
6544       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6545     SourceLocation Loc
6546       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6547     bool Used = WeakUndeclaredIdentifiers[I++];
6548     WeakInfo WI(AliasId, Loc);
6549     WI.setUsed(Used);
6550     WeakIDs.push_back(std::make_pair(WeakId, WI));
6551   }
6552   WeakUndeclaredIdentifiers.clear();
6553 }
6554 
6555 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6556   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6557     ExternalVTableUse VT;
6558     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6559     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6560     VT.DefinitionRequired = VTableUses[Idx++];
6561     VTables.push_back(VT);
6562   }
6563 
6564   VTableUses.clear();
6565 }
6566 
6567 void ASTReader::ReadPendingInstantiations(
6568        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6569   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6570     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6571     SourceLocation Loc
6572       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6573 
6574     Pending.push_back(std::make_pair(D, Loc));
6575   }
6576   PendingInstantiations.clear();
6577 }
6578 
6579 void ASTReader::ReadLateParsedTemplates(
6580     llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6581   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6582        /* In loop */) {
6583     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6584 
6585     LateParsedTemplate *LT = new LateParsedTemplate;
6586     LT->D = GetDecl(LateParsedTemplates[Idx++]);
6587 
6588     ModuleFile *F = getOwningModuleFile(LT->D);
6589     assert(F && "No module");
6590 
6591     unsigned TokN = LateParsedTemplates[Idx++];
6592     LT->Toks.reserve(TokN);
6593     for (unsigned T = 0; T < TokN; ++T)
6594       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6595 
6596     LPTMap[FD] = LT;
6597   }
6598 
6599   LateParsedTemplates.clear();
6600 }
6601 
6602 void ASTReader::LoadSelector(Selector Sel) {
6603   // It would be complicated to avoid reading the methods anyway. So don't.
6604   ReadMethodPool(Sel);
6605 }
6606 
6607 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6608   assert(ID && "Non-zero identifier ID required");
6609   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6610   IdentifiersLoaded[ID - 1] = II;
6611   if (DeserializationListener)
6612     DeserializationListener->IdentifierRead(ID, II);
6613 }
6614 
6615 /// \brief Set the globally-visible declarations associated with the given
6616 /// identifier.
6617 ///
6618 /// If the AST reader is currently in a state where the given declaration IDs
6619 /// cannot safely be resolved, they are queued until it is safe to resolve
6620 /// them.
6621 ///
6622 /// \param II an IdentifierInfo that refers to one or more globally-visible
6623 /// declarations.
6624 ///
6625 /// \param DeclIDs the set of declaration IDs with the name @p II that are
6626 /// visible at global scope.
6627 ///
6628 /// \param Decls if non-null, this vector will be populated with the set of
6629 /// deserialized declarations. These declarations will not be pushed into
6630 /// scope.
6631 void
6632 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6633                               const SmallVectorImpl<uint32_t> &DeclIDs,
6634                                    SmallVectorImpl<Decl *> *Decls) {
6635   if (NumCurrentElementsDeserializing && !Decls) {
6636     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
6637     return;
6638   }
6639 
6640   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6641     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6642     if (SemaObj) {
6643       // If we're simply supposed to record the declarations, do so now.
6644       if (Decls) {
6645         Decls->push_back(D);
6646         continue;
6647       }
6648 
6649       // Introduce this declaration into the translation-unit scope
6650       // and add it to the declaration chain for this identifier, so
6651       // that (unqualified) name lookup will find it.
6652       pushExternalDeclIntoScope(D, II);
6653     } else {
6654       // Queue this declaration so that it will be added to the
6655       // translation unit scope and identifier's declaration chain
6656       // once a Sema object is known.
6657       PreloadedDecls.push_back(D);
6658     }
6659   }
6660 }
6661 
6662 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
6663   if (ID == 0)
6664     return 0;
6665 
6666   if (IdentifiersLoaded.empty()) {
6667     Error("no identifier table in AST file");
6668     return 0;
6669   }
6670 
6671   ID -= 1;
6672   if (!IdentifiersLoaded[ID]) {
6673     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6674     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6675     ModuleFile *M = I->second;
6676     unsigned Index = ID - M->BaseIdentifierID;
6677     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6678 
6679     // All of the strings in the AST file are preceded by a 16-bit length.
6680     // Extract that 16-bit length to avoid having to execute strlen().
6681     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6682     //  unsigned integers.  This is important to avoid integer overflow when
6683     //  we cast them to 'unsigned'.
6684     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6685     unsigned StrLen = (((unsigned) StrLenPtr[0])
6686                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
6687     IdentifiersLoaded[ID]
6688       = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
6689     if (DeserializationListener)
6690       DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6691   }
6692 
6693   return IdentifiersLoaded[ID];
6694 }
6695 
6696 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6697   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6698 }
6699 
6700 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6701   if (LocalID < NUM_PREDEF_IDENT_IDS)
6702     return LocalID;
6703 
6704   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6705     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6706   assert(I != M.IdentifierRemap.end()
6707          && "Invalid index into identifier index remap");
6708 
6709   return LocalID + I->second;
6710 }
6711 
6712 MacroInfo *ASTReader::getMacro(MacroID ID) {
6713   if (ID == 0)
6714     return 0;
6715 
6716   if (MacrosLoaded.empty()) {
6717     Error("no macro table in AST file");
6718     return 0;
6719   }
6720 
6721   ID -= NUM_PREDEF_MACRO_IDS;
6722   if (!MacrosLoaded[ID]) {
6723     GlobalMacroMapType::iterator I
6724       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6725     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6726     ModuleFile *M = I->second;
6727     unsigned Index = ID - M->BaseMacroID;
6728     MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6729 
6730     if (DeserializationListener)
6731       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6732                                          MacrosLoaded[ID]);
6733   }
6734 
6735   return MacrosLoaded[ID];
6736 }
6737 
6738 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6739   if (LocalID < NUM_PREDEF_MACRO_IDS)
6740     return LocalID;
6741 
6742   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6743     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6744   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6745 
6746   return LocalID + I->second;
6747 }
6748 
6749 serialization::SubmoduleID
6750 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6751   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6752     return LocalID;
6753 
6754   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6755     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6756   assert(I != M.SubmoduleRemap.end()
6757          && "Invalid index into submodule index remap");
6758 
6759   return LocalID + I->second;
6760 }
6761 
6762 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6763   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6764     assert(GlobalID == 0 && "Unhandled global submodule ID");
6765     return 0;
6766   }
6767 
6768   if (GlobalID > SubmodulesLoaded.size()) {
6769     Error("submodule ID out of range in AST file");
6770     return 0;
6771   }
6772 
6773   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6774 }
6775 
6776 Module *ASTReader::getModule(unsigned ID) {
6777   return getSubmodule(ID);
6778 }
6779 
6780 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6781   return DecodeSelector(getGlobalSelectorID(M, LocalID));
6782 }
6783 
6784 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6785   if (ID == 0)
6786     return Selector();
6787 
6788   if (ID > SelectorsLoaded.size()) {
6789     Error("selector ID out of range in AST file");
6790     return Selector();
6791   }
6792 
6793   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6794     // Load this selector from the selector table.
6795     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6796     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6797     ModuleFile &M = *I->second;
6798     ASTSelectorLookupTrait Trait(*this, M);
6799     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6800     SelectorsLoaded[ID - 1] =
6801       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6802     if (DeserializationListener)
6803       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6804   }
6805 
6806   return SelectorsLoaded[ID - 1];
6807 }
6808 
6809 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6810   return DecodeSelector(ID);
6811 }
6812 
6813 uint32_t ASTReader::GetNumExternalSelectors() {
6814   // ID 0 (the null selector) is considered an external selector.
6815   return getTotalNumSelectors() + 1;
6816 }
6817 
6818 serialization::SelectorID
6819 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6820   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6821     return LocalID;
6822 
6823   ContinuousRangeMap<uint32_t, int, 2>::iterator I
6824     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6825   assert(I != M.SelectorRemap.end()
6826          && "Invalid index into selector index remap");
6827 
6828   return LocalID + I->second;
6829 }
6830 
6831 DeclarationName
6832 ASTReader::ReadDeclarationName(ModuleFile &F,
6833                                const RecordData &Record, unsigned &Idx) {
6834   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6835   switch (Kind) {
6836   case DeclarationName::Identifier:
6837     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
6838 
6839   case DeclarationName::ObjCZeroArgSelector:
6840   case DeclarationName::ObjCOneArgSelector:
6841   case DeclarationName::ObjCMultiArgSelector:
6842     return DeclarationName(ReadSelector(F, Record, Idx));
6843 
6844   case DeclarationName::CXXConstructorName:
6845     return Context.DeclarationNames.getCXXConstructorName(
6846                           Context.getCanonicalType(readType(F, Record, Idx)));
6847 
6848   case DeclarationName::CXXDestructorName:
6849     return Context.DeclarationNames.getCXXDestructorName(
6850                           Context.getCanonicalType(readType(F, Record, Idx)));
6851 
6852   case DeclarationName::CXXConversionFunctionName:
6853     return Context.DeclarationNames.getCXXConversionFunctionName(
6854                           Context.getCanonicalType(readType(F, Record, Idx)));
6855 
6856   case DeclarationName::CXXOperatorName:
6857     return Context.DeclarationNames.getCXXOperatorName(
6858                                        (OverloadedOperatorKind)Record[Idx++]);
6859 
6860   case DeclarationName::CXXLiteralOperatorName:
6861     return Context.DeclarationNames.getCXXLiteralOperatorName(
6862                                        GetIdentifierInfo(F, Record, Idx));
6863 
6864   case DeclarationName::CXXUsingDirective:
6865     return DeclarationName::getUsingDirectiveName();
6866   }
6867 
6868   llvm_unreachable("Invalid NameKind!");
6869 }
6870 
6871 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6872                                        DeclarationNameLoc &DNLoc,
6873                                        DeclarationName Name,
6874                                       const RecordData &Record, unsigned &Idx) {
6875   switch (Name.getNameKind()) {
6876   case DeclarationName::CXXConstructorName:
6877   case DeclarationName::CXXDestructorName:
6878   case DeclarationName::CXXConversionFunctionName:
6879     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6880     break;
6881 
6882   case DeclarationName::CXXOperatorName:
6883     DNLoc.CXXOperatorName.BeginOpNameLoc
6884         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6885     DNLoc.CXXOperatorName.EndOpNameLoc
6886         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6887     break;
6888 
6889   case DeclarationName::CXXLiteralOperatorName:
6890     DNLoc.CXXLiteralOperatorName.OpNameLoc
6891         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6892     break;
6893 
6894   case DeclarationName::Identifier:
6895   case DeclarationName::ObjCZeroArgSelector:
6896   case DeclarationName::ObjCOneArgSelector:
6897   case DeclarationName::ObjCMultiArgSelector:
6898   case DeclarationName::CXXUsingDirective:
6899     break;
6900   }
6901 }
6902 
6903 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6904                                         DeclarationNameInfo &NameInfo,
6905                                       const RecordData &Record, unsigned &Idx) {
6906   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6907   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6908   DeclarationNameLoc DNLoc;
6909   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6910   NameInfo.setInfo(DNLoc);
6911 }
6912 
6913 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6914                                   const RecordData &Record, unsigned &Idx) {
6915   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6916   unsigned NumTPLists = Record[Idx++];
6917   Info.NumTemplParamLists = NumTPLists;
6918   if (NumTPLists) {
6919     Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6920     for (unsigned i=0; i != NumTPLists; ++i)
6921       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6922   }
6923 }
6924 
6925 TemplateName
6926 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6927                             unsigned &Idx) {
6928   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6929   switch (Kind) {
6930   case TemplateName::Template:
6931       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6932 
6933   case TemplateName::OverloadedTemplate: {
6934     unsigned size = Record[Idx++];
6935     UnresolvedSet<8> Decls;
6936     while (size--)
6937       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6938 
6939     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6940   }
6941 
6942   case TemplateName::QualifiedTemplate: {
6943     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6944     bool hasTemplKeyword = Record[Idx++];
6945     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6946     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6947   }
6948 
6949   case TemplateName::DependentTemplate: {
6950     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6951     if (Record[Idx++])  // isIdentifier
6952       return Context.getDependentTemplateName(NNS,
6953                                                GetIdentifierInfo(F, Record,
6954                                                                  Idx));
6955     return Context.getDependentTemplateName(NNS,
6956                                          (OverloadedOperatorKind)Record[Idx++]);
6957   }
6958 
6959   case TemplateName::SubstTemplateTemplateParm: {
6960     TemplateTemplateParmDecl *param
6961       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6962     if (!param) return TemplateName();
6963     TemplateName replacement = ReadTemplateName(F, Record, Idx);
6964     return Context.getSubstTemplateTemplateParm(param, replacement);
6965   }
6966 
6967   case TemplateName::SubstTemplateTemplateParmPack: {
6968     TemplateTemplateParmDecl *Param
6969       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6970     if (!Param)
6971       return TemplateName();
6972 
6973     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6974     if (ArgPack.getKind() != TemplateArgument::Pack)
6975       return TemplateName();
6976 
6977     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6978   }
6979   }
6980 
6981   llvm_unreachable("Unhandled template name kind!");
6982 }
6983 
6984 TemplateArgument
6985 ASTReader::ReadTemplateArgument(ModuleFile &F,
6986                                 const RecordData &Record, unsigned &Idx) {
6987   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6988   switch (Kind) {
6989   case TemplateArgument::Null:
6990     return TemplateArgument();
6991   case TemplateArgument::Type:
6992     return TemplateArgument(readType(F, Record, Idx));
6993   case TemplateArgument::Declaration: {
6994     ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6995     bool ForReferenceParam = Record[Idx++];
6996     return TemplateArgument(D, ForReferenceParam);
6997   }
6998   case TemplateArgument::NullPtr:
6999     return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7000   case TemplateArgument::Integral: {
7001     llvm::APSInt Value = ReadAPSInt(Record, Idx);
7002     QualType T = readType(F, Record, Idx);
7003     return TemplateArgument(Context, Value, T);
7004   }
7005   case TemplateArgument::Template:
7006     return TemplateArgument(ReadTemplateName(F, Record, Idx));
7007   case TemplateArgument::TemplateExpansion: {
7008     TemplateName Name = ReadTemplateName(F, Record, Idx);
7009     Optional<unsigned> NumTemplateExpansions;
7010     if (unsigned NumExpansions = Record[Idx++])
7011       NumTemplateExpansions = NumExpansions - 1;
7012     return TemplateArgument(Name, NumTemplateExpansions);
7013   }
7014   case TemplateArgument::Expression:
7015     return TemplateArgument(ReadExpr(F));
7016   case TemplateArgument::Pack: {
7017     unsigned NumArgs = Record[Idx++];
7018     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7019     for (unsigned I = 0; I != NumArgs; ++I)
7020       Args[I] = ReadTemplateArgument(F, Record, Idx);
7021     return TemplateArgument(Args, NumArgs);
7022   }
7023   }
7024 
7025   llvm_unreachable("Unhandled template argument kind!");
7026 }
7027 
7028 TemplateParameterList *
7029 ASTReader::ReadTemplateParameterList(ModuleFile &F,
7030                                      const RecordData &Record, unsigned &Idx) {
7031   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7032   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7033   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7034 
7035   unsigned NumParams = Record[Idx++];
7036   SmallVector<NamedDecl *, 16> Params;
7037   Params.reserve(NumParams);
7038   while (NumParams--)
7039     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7040 
7041   TemplateParameterList* TemplateParams =
7042     TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7043                                   Params.data(), Params.size(), RAngleLoc);
7044   return TemplateParams;
7045 }
7046 
7047 void
7048 ASTReader::
7049 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
7050                          ModuleFile &F, const RecordData &Record,
7051                          unsigned &Idx) {
7052   unsigned NumTemplateArgs = Record[Idx++];
7053   TemplArgs.reserve(NumTemplateArgs);
7054   while (NumTemplateArgs--)
7055     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7056 }
7057 
7058 /// \brief Read a UnresolvedSet structure.
7059 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
7060                                   const RecordData &Record, unsigned &Idx) {
7061   unsigned NumDecls = Record[Idx++];
7062   Set.reserve(Context, NumDecls);
7063   while (NumDecls--) {
7064     DeclID ID = ReadDeclID(F, Record, Idx);
7065     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
7066     Set.addLazyDecl(Context, ID, AS);
7067   }
7068 }
7069 
7070 CXXBaseSpecifier
7071 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7072                                 const RecordData &Record, unsigned &Idx) {
7073   bool isVirtual = static_cast<bool>(Record[Idx++]);
7074   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7075   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7076   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7077   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7078   SourceRange Range = ReadSourceRange(F, Record, Idx);
7079   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7080   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7081                           EllipsisLoc);
7082   Result.setInheritConstructors(inheritConstructors);
7083   return Result;
7084 }
7085 
7086 std::pair<CXXCtorInitializer **, unsigned>
7087 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7088                                    unsigned &Idx) {
7089   CXXCtorInitializer **CtorInitializers = 0;
7090   unsigned NumInitializers = Record[Idx++];
7091   if (NumInitializers) {
7092     CtorInitializers
7093         = new (Context) CXXCtorInitializer*[NumInitializers];
7094     for (unsigned i=0; i != NumInitializers; ++i) {
7095       TypeSourceInfo *TInfo = 0;
7096       bool IsBaseVirtual = false;
7097       FieldDecl *Member = 0;
7098       IndirectFieldDecl *IndirectMember = 0;
7099 
7100       CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7101       switch (Type) {
7102       case CTOR_INITIALIZER_BASE:
7103         TInfo = GetTypeSourceInfo(F, Record, Idx);
7104         IsBaseVirtual = Record[Idx++];
7105         break;
7106 
7107       case CTOR_INITIALIZER_DELEGATING:
7108         TInfo = GetTypeSourceInfo(F, Record, Idx);
7109         break;
7110 
7111        case CTOR_INITIALIZER_MEMBER:
7112         Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7113         break;
7114 
7115        case CTOR_INITIALIZER_INDIRECT_MEMBER:
7116         IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7117         break;
7118       }
7119 
7120       SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7121       Expr *Init = ReadExpr(F);
7122       SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7123       SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7124       bool IsWritten = Record[Idx++];
7125       unsigned SourceOrderOrNumArrayIndices;
7126       SmallVector<VarDecl *, 8> Indices;
7127       if (IsWritten) {
7128         SourceOrderOrNumArrayIndices = Record[Idx++];
7129       } else {
7130         SourceOrderOrNumArrayIndices = Record[Idx++];
7131         Indices.reserve(SourceOrderOrNumArrayIndices);
7132         for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7133           Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7134       }
7135 
7136       CXXCtorInitializer *BOMInit;
7137       if (Type == CTOR_INITIALIZER_BASE) {
7138         BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7139                                              LParenLoc, Init, RParenLoc,
7140                                              MemberOrEllipsisLoc);
7141       } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7142         BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7143                                                    Init, RParenLoc);
7144       } else if (IsWritten) {
7145         if (Member)
7146           BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7147                                                LParenLoc, Init, RParenLoc);
7148         else
7149           BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7150                                                MemberOrEllipsisLoc, LParenLoc,
7151                                                Init, RParenLoc);
7152       } else {
7153         if (IndirectMember) {
7154           assert(Indices.empty() && "Indirect field improperly initialized");
7155           BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7156                                                      MemberOrEllipsisLoc, LParenLoc,
7157                                                      Init, RParenLoc);
7158         } else {
7159           BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7160                                                LParenLoc, Init, RParenLoc,
7161                                                Indices.data(), Indices.size());
7162         }
7163       }
7164 
7165       if (IsWritten)
7166         BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7167       CtorInitializers[i] = BOMInit;
7168     }
7169   }
7170 
7171   return std::make_pair(CtorInitializers, NumInitializers);
7172 }
7173 
7174 NestedNameSpecifier *
7175 ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7176                                    const RecordData &Record, unsigned &Idx) {
7177   unsigned N = Record[Idx++];
7178   NestedNameSpecifier *NNS = 0, *Prev = 0;
7179   for (unsigned I = 0; I != N; ++I) {
7180     NestedNameSpecifier::SpecifierKind Kind
7181       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7182     switch (Kind) {
7183     case NestedNameSpecifier::Identifier: {
7184       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7185       NNS = NestedNameSpecifier::Create(Context, Prev, II);
7186       break;
7187     }
7188 
7189     case NestedNameSpecifier::Namespace: {
7190       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7191       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7192       break;
7193     }
7194 
7195     case NestedNameSpecifier::NamespaceAlias: {
7196       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7197       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7198       break;
7199     }
7200 
7201     case NestedNameSpecifier::TypeSpec:
7202     case NestedNameSpecifier::TypeSpecWithTemplate: {
7203       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7204       if (!T)
7205         return 0;
7206 
7207       bool Template = Record[Idx++];
7208       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7209       break;
7210     }
7211 
7212     case NestedNameSpecifier::Global: {
7213       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7214       // No associated value, and there can't be a prefix.
7215       break;
7216     }
7217     }
7218     Prev = NNS;
7219   }
7220   return NNS;
7221 }
7222 
7223 NestedNameSpecifierLoc
7224 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7225                                       unsigned &Idx) {
7226   unsigned N = Record[Idx++];
7227   NestedNameSpecifierLocBuilder Builder;
7228   for (unsigned I = 0; I != N; ++I) {
7229     NestedNameSpecifier::SpecifierKind Kind
7230       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7231     switch (Kind) {
7232     case NestedNameSpecifier::Identifier: {
7233       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7234       SourceRange Range = ReadSourceRange(F, Record, Idx);
7235       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7236       break;
7237     }
7238 
7239     case NestedNameSpecifier::Namespace: {
7240       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7241       SourceRange Range = ReadSourceRange(F, Record, Idx);
7242       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7243       break;
7244     }
7245 
7246     case NestedNameSpecifier::NamespaceAlias: {
7247       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7248       SourceRange Range = ReadSourceRange(F, Record, Idx);
7249       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7250       break;
7251     }
7252 
7253     case NestedNameSpecifier::TypeSpec:
7254     case NestedNameSpecifier::TypeSpecWithTemplate: {
7255       bool Template = Record[Idx++];
7256       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7257       if (!T)
7258         return NestedNameSpecifierLoc();
7259       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7260 
7261       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7262       Builder.Extend(Context,
7263                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7264                      T->getTypeLoc(), ColonColonLoc);
7265       break;
7266     }
7267 
7268     case NestedNameSpecifier::Global: {
7269       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7270       Builder.MakeGlobal(Context, ColonColonLoc);
7271       break;
7272     }
7273     }
7274   }
7275 
7276   return Builder.getWithLocInContext(Context);
7277 }
7278 
7279 SourceRange
7280 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7281                            unsigned &Idx) {
7282   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7283   SourceLocation end = ReadSourceLocation(F, Record, Idx);
7284   return SourceRange(beg, end);
7285 }
7286 
7287 /// \brief Read an integral value
7288 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7289   unsigned BitWidth = Record[Idx++];
7290   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7291   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7292   Idx += NumWords;
7293   return Result;
7294 }
7295 
7296 /// \brief Read a signed integral value
7297 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7298   bool isUnsigned = Record[Idx++];
7299   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7300 }
7301 
7302 /// \brief Read a floating-point value
7303 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7304                                      const llvm::fltSemantics &Sem,
7305                                      unsigned &Idx) {
7306   return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
7307 }
7308 
7309 // \brief Read a string
7310 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7311   unsigned Len = Record[Idx++];
7312   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7313   Idx += Len;
7314   return Result;
7315 }
7316 
7317 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7318                                          unsigned &Idx) {
7319   unsigned Major = Record[Idx++];
7320   unsigned Minor = Record[Idx++];
7321   unsigned Subminor = Record[Idx++];
7322   if (Minor == 0)
7323     return VersionTuple(Major);
7324   if (Subminor == 0)
7325     return VersionTuple(Major, Minor - 1);
7326   return VersionTuple(Major, Minor - 1, Subminor - 1);
7327 }
7328 
7329 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7330                                           const RecordData &Record,
7331                                           unsigned &Idx) {
7332   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7333   return CXXTemporary::Create(Context, Decl);
7334 }
7335 
7336 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7337   return Diag(CurrentImportLoc, DiagID);
7338 }
7339 
7340 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7341   return Diags.Report(Loc, DiagID);
7342 }
7343 
7344 /// \brief Retrieve the identifier table associated with the
7345 /// preprocessor.
7346 IdentifierTable &ASTReader::getIdentifierTable() {
7347   return PP.getIdentifierTable();
7348 }
7349 
7350 /// \brief Record that the given ID maps to the given switch-case
7351 /// statement.
7352 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7353   assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7354          "Already have a SwitchCase with this ID");
7355   (*CurrSwitchCaseStmts)[ID] = SC;
7356 }
7357 
7358 /// \brief Retrieve the switch-case statement with the given ID.
7359 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7360   assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7361   return (*CurrSwitchCaseStmts)[ID];
7362 }
7363 
7364 void ASTReader::ClearSwitchCaseIDs() {
7365   CurrSwitchCaseStmts->clear();
7366 }
7367 
7368 void ASTReader::ReadComments() {
7369   std::vector<RawComment *> Comments;
7370   for (SmallVectorImpl<std::pair<BitstreamCursor,
7371                                  serialization::ModuleFile *> >::iterator
7372        I = CommentsCursors.begin(),
7373        E = CommentsCursors.end();
7374        I != E; ++I) {
7375     BitstreamCursor &Cursor = I->first;
7376     serialization::ModuleFile &F = *I->second;
7377     SavedStreamPosition SavedPosition(Cursor);
7378 
7379     RecordData Record;
7380     while (true) {
7381       llvm::BitstreamEntry Entry =
7382         Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7383 
7384       switch (Entry.Kind) {
7385       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7386       case llvm::BitstreamEntry::Error:
7387         Error("malformed block record in AST file");
7388         return;
7389       case llvm::BitstreamEntry::EndBlock:
7390         goto NextCursor;
7391       case llvm::BitstreamEntry::Record:
7392         // The interesting case.
7393         break;
7394       }
7395 
7396       // Read a record.
7397       Record.clear();
7398       switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
7399       case COMMENTS_RAW_COMMENT: {
7400         unsigned Idx = 0;
7401         SourceRange SR = ReadSourceRange(F, Record, Idx);
7402         RawComment::CommentKind Kind =
7403             (RawComment::CommentKind) Record[Idx++];
7404         bool IsTrailingComment = Record[Idx++];
7405         bool IsAlmostTrailingComment = Record[Idx++];
7406         Comments.push_back(new (Context) RawComment(
7407             SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7408             Context.getLangOpts().CommentOpts.ParseAllComments));
7409         break;
7410       }
7411       }
7412     }
7413   NextCursor:;
7414   }
7415   Context.Comments.addCommentsToFront(Comments);
7416 }
7417 
7418 void ASTReader::finishPendingActions() {
7419   while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
7420          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
7421          !PendingOdrMergeChecks.empty()) {
7422     // If any identifiers with corresponding top-level declarations have
7423     // been loaded, load those declarations now.
7424     typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7425       TopLevelDeclsMap;
7426     TopLevelDeclsMap TopLevelDecls;
7427 
7428     while (!PendingIdentifierInfos.empty()) {
7429       // FIXME: std::move
7430       IdentifierInfo *II = PendingIdentifierInfos.back().first;
7431       SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
7432       PendingIdentifierInfos.pop_back();
7433 
7434       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
7435     }
7436 
7437     // Load pending declaration chains.
7438     for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7439       loadPendingDeclChain(PendingDeclChains[I]);
7440       PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7441     }
7442     PendingDeclChains.clear();
7443 
7444     // Make the most recent of the top-level declarations visible.
7445     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7446            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
7447       IdentifierInfo *II = TLD->first;
7448       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7449         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
7450       }
7451     }
7452 
7453     // Load any pending macro definitions.
7454     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
7455       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7456       SmallVector<PendingMacroInfo, 2> GlobalIDs;
7457       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7458       // Initialize the macro history from chained-PCHs ahead of module imports.
7459       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx !=  NumIDs;
7460            ++IDIdx) {
7461         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7462         if (Info.M->Kind != MK_Module)
7463           resolvePendingMacro(II, Info);
7464       }
7465       // Handle module imports.
7466       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx !=  NumIDs;
7467            ++IDIdx) {
7468         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7469         if (Info.M->Kind == MK_Module)
7470           resolvePendingMacro(II, Info);
7471       }
7472     }
7473     PendingMacroIDs.clear();
7474 
7475     // Wire up the DeclContexts for Decls that we delayed setting until
7476     // recursive loading is completed.
7477     while (!PendingDeclContextInfos.empty()) {
7478       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7479       PendingDeclContextInfos.pop_front();
7480       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7481       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7482       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7483     }
7484 
7485     // For each declaration from a merged context, check that the canonical
7486     // definition of that context also contains a declaration of the same
7487     // entity.
7488     while (!PendingOdrMergeChecks.empty()) {
7489       NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
7490 
7491       // FIXME: Skip over implicit declarations for now. This matters for things
7492       // like implicitly-declared special member functions. This isn't entirely
7493       // correct; we can end up with multiple unmerged declarations of the same
7494       // implicit entity.
7495       if (D->isImplicit())
7496         continue;
7497 
7498       DeclContext *CanonDef = D->getDeclContext();
7499       DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
7500 
7501       bool Found = false;
7502       const Decl *DCanon = D->getCanonicalDecl();
7503 
7504       llvm::SmallVector<const NamedDecl*, 4> Candidates;
7505       for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
7506            !Found && I != E; ++I) {
7507         for (Decl::redecl_iterator RI = (*I)->redecls_begin(),
7508                                    RE = (*I)->redecls_end();
7509              RI != RE; ++RI) {
7510           if ((*RI)->getLexicalDeclContext() == CanonDef) {
7511             // This declaration is present in the canonical definition. If it's
7512             // in the same redecl chain, it's the one we're looking for.
7513             if ((*RI)->getCanonicalDecl() == DCanon)
7514               Found = true;
7515             else
7516               Candidates.push_back(cast<NamedDecl>(*RI));
7517             break;
7518           }
7519         }
7520       }
7521 
7522       if (!Found) {
7523         D->setInvalidDecl();
7524 
7525         Module *CanonDefModule = cast<Decl>(CanonDef)->getOwningModule();
7526         Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
7527           << D << D->getOwningModule()->getFullModuleName()
7528           << CanonDef << !CanonDefModule
7529           << (CanonDefModule ? CanonDefModule->getFullModuleName() : "");
7530 
7531         if (Candidates.empty())
7532           Diag(cast<Decl>(CanonDef)->getLocation(),
7533                diag::note_module_odr_violation_no_possible_decls) << D;
7534         else {
7535           for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
7536             Diag(Candidates[I]->getLocation(),
7537                  diag::note_module_odr_violation_possible_decl)
7538               << Candidates[I];
7539         }
7540       }
7541     }
7542   }
7543 
7544   // If we deserialized any C++ or Objective-C class definitions, any
7545   // Objective-C protocol definitions, or any redeclarable templates, make sure
7546   // that all redeclarations point to the definitions. Note that this can only
7547   // happen now, after the redeclaration chains have been fully wired.
7548   for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7549                                            DEnd = PendingDefinitions.end();
7550        D != DEnd; ++D) {
7551     if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7552       if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7553         // Make sure that the TagType points at the definition.
7554         const_cast<TagType*>(TagT)->decl = TD;
7555       }
7556 
7557       if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7558         for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7559                                          REnd = RD->redecls_end();
7560              R != REnd; ++R)
7561           cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7562 
7563       }
7564 
7565       continue;
7566     }
7567 
7568     if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7569       // Make sure that the ObjCInterfaceType points at the definition.
7570       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7571         ->Decl = ID;
7572 
7573       for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7574                                            REnd = ID->redecls_end();
7575            R != REnd; ++R)
7576         R->Data = ID->Data;
7577 
7578       continue;
7579     }
7580 
7581     if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7582       for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7583                                           REnd = PD->redecls_end();
7584            R != REnd; ++R)
7585         R->Data = PD->Data;
7586 
7587       continue;
7588     }
7589 
7590     RedeclarableTemplateDecl *RTD
7591       = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7592     for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7593                                                 REnd = RTD->redecls_end();
7594          R != REnd; ++R)
7595       R->Common = RTD->Common;
7596   }
7597   PendingDefinitions.clear();
7598 
7599   // Load the bodies of any functions or methods we've encountered. We do
7600   // this now (delayed) so that we can be sure that the declaration chains
7601   // have been fully wired up.
7602   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7603                                PBEnd = PendingBodies.end();
7604        PB != PBEnd; ++PB) {
7605     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7606       // FIXME: Check for =delete/=default?
7607       // FIXME: Complain about ODR violations here?
7608       if (!getContext().getLangOpts().Modules || !FD->hasBody())
7609         FD->setLazyBody(PB->second);
7610       continue;
7611     }
7612 
7613     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7614     if (!getContext().getLangOpts().Modules || !MD->hasBody())
7615       MD->setLazyBody(PB->second);
7616   }
7617   PendingBodies.clear();
7618 }
7619 
7620 void ASTReader::FinishedDeserializing() {
7621   assert(NumCurrentElementsDeserializing &&
7622          "FinishedDeserializing not paired with StartedDeserializing");
7623   if (NumCurrentElementsDeserializing == 1) {
7624     // We decrease NumCurrentElementsDeserializing only after pending actions
7625     // are finished, to avoid recursively re-calling finishPendingActions().
7626     finishPendingActions();
7627   }
7628   --NumCurrentElementsDeserializing;
7629 
7630   if (NumCurrentElementsDeserializing == 0 &&
7631       Consumer && !PassingDeclsToConsumer) {
7632     // Guard variable to avoid recursively redoing the process of passing
7633     // decls to consumer.
7634     SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7635                                                      true);
7636 
7637     while (!InterestingDecls.empty()) {
7638       // We are not in recursive loading, so it's safe to pass the "interesting"
7639       // decls to the consumer.
7640       Decl *D = InterestingDecls.front();
7641       InterestingDecls.pop_front();
7642       PassInterestingDeclToConsumer(D);
7643     }
7644   }
7645 }
7646 
7647 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7648   D = D->getMostRecentDecl();
7649 
7650   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7651     SemaObj->TUScope->AddDecl(D);
7652   } else if (SemaObj->TUScope) {
7653     // Adding the decl to IdResolver may have failed because it was already in
7654     // (even though it was not added in scope). If it is already in, make sure
7655     // it gets in the scope as well.
7656     if (std::find(SemaObj->IdResolver.begin(Name),
7657                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7658       SemaObj->TUScope->AddDecl(D);
7659   }
7660 }
7661 
7662 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7663                      StringRef isysroot, bool DisableValidation,
7664                      bool AllowASTWithCompilerErrors,
7665                      bool AllowConfigurationMismatch,
7666                      bool ValidateSystemInputs,
7667                      bool UseGlobalIndex)
7668   : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7669     SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7670     Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7671     Consumer(0), ModuleMgr(PP.getFileManager()),
7672     isysroot(isysroot), DisableValidation(DisableValidation),
7673     AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
7674     AllowConfigurationMismatch(AllowConfigurationMismatch),
7675     ValidateSystemInputs(ValidateSystemInputs),
7676     UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
7677     CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7678     NumSLocEntriesRead(0), TotalNumSLocEntries(0),
7679     NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7680     TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7681     NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7682     NumMethodPoolLookups(0), NumMethodPoolHits(0),
7683     NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7684     TotalNumMethodPoolEntries(0),
7685     NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7686     NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7687     TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7688     PassingDeclsToConsumer(false),
7689     NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
7690 {
7691   SourceMgr.setExternalSLocEntrySource(this);
7692 }
7693 
7694 ASTReader::~ASTReader() {
7695   for (DeclContextVisibleUpdatesPending::iterator
7696            I = PendingVisibleUpdates.begin(),
7697            E = PendingVisibleUpdates.end();
7698        I != E; ++I) {
7699     for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7700                                              F = I->second.end();
7701          J != F; ++J)
7702       delete J->first;
7703   }
7704 }
7705