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