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