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