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