1 //===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ASTReader class, which reads AST files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Serialization/ASTReader.h"
15 #include "clang/Serialization/ASTDeserializationListener.h"
16 #include "clang/Serialization/ModuleManager.h"
17 #include "ASTCommon.h"
18 #include "ASTReaderInternals.h"
19 #include "clang/Frontend/FrontendDiagnostic.h"
20 #include "clang/Frontend/Utils.h"
21 #include "clang/Sema/Sema.h"
22 #include "clang/Sema/Scope.h"
23 #include "clang/AST/ASTConsumer.h"
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/DeclTemplate.h"
26 #include "clang/AST/Expr.h"
27 #include "clang/AST/ExprCXX.h"
28 #include "clang/AST/NestedNameSpecifier.h"
29 #include "clang/AST/Type.h"
30 #include "clang/AST/TypeLocVisitor.h"
31 #include "clang/Lex/MacroInfo.h"
32 #include "clang/Lex/PreprocessingRecord.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Basic/OnDiskHashTable.h"
36 #include "clang/Basic/SourceManager.h"
37 #include "clang/Basic/SourceManagerInternals.h"
38 #include "clang/Basic/FileManager.h"
39 #include "clang/Basic/FileSystemStatCache.h"
40 #include "clang/Basic/TargetInfo.h"
41 #include "clang/Basic/Version.h"
42 #include "clang/Basic/VersionTuple.h"
43 #include "llvm/ADT/StringExtras.h"
44 #include "llvm/Bitcode/BitstreamReader.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/Path.h"
49 #include "llvm/Support/system_error.h"
50 #include <algorithm>
51 #include <iterator>
52 #include <cstdio>
53 #include <sys/stat.h>
54 
55 using namespace clang;
56 using namespace clang::serialization;
57 using namespace clang::serialization::reader;
58 
59 //===----------------------------------------------------------------------===//
60 // PCH validator implementation
61 //===----------------------------------------------------------------------===//
62 
63 ASTReaderListener::~ASTReaderListener() {}
64 
65 bool
66 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
67   const LangOptions &PPLangOpts = PP.getLangOptions();
68 
69 #define LANGOPT(Name, Bits, Default, Description)         \
70   if (PPLangOpts.Name != LangOpts.Name) {                 \
71     Reader.Diag(diag::err_pch_langopt_mismatch)           \
72       << Description << LangOpts.Name << PPLangOpts.Name; \
73     return true;                                          \
74   }
75 
76 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
77   if (PPLangOpts.Name != LangOpts.Name) {               \
78     Reader.Diag(diag::err_pch_langopt_value_mismatch)   \
79       << Description;                                   \
80   return true;                                          \
81 }
82 
83 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
84   if (PPLangOpts.get##Name() != LangOpts.get##Name()) {      \
85     Reader.Diag(diag::err_pch_langopt_value_mismatch)        \
86       << Description;                                        \
87     return true;                                             \
88   }
89 
90 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
91 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
92 #include "clang/Basic/LangOptions.def"
93 
94   return false;
95 }
96 
97 bool PCHValidator::ReadTargetTriple(StringRef Triple) {
98   if (Triple == PP.getTargetInfo().getTriple().str())
99     return false;
100 
101   Reader.Diag(diag::warn_pch_target_triple)
102     << Triple << PP.getTargetInfo().getTriple().str();
103   return true;
104 }
105 
106 namespace {
107   struct EmptyStringRef {
108     bool operator ()(StringRef r) const { return r.empty(); }
109   };
110   struct EmptyBlock {
111     bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
112   };
113 }
114 
115 static bool EqualConcatenations(SmallVector<StringRef, 2> L,
116                                 PCHPredefinesBlocks R) {
117   // First, sum up the lengths.
118   unsigned LL = 0, RL = 0;
119   for (unsigned I = 0, N = L.size(); I != N; ++I) {
120     LL += L[I].size();
121   }
122   for (unsigned I = 0, N = R.size(); I != N; ++I) {
123     RL += R[I].Data.size();
124   }
125   if (LL != RL)
126     return false;
127   if (LL == 0 && RL == 0)
128     return true;
129 
130   // Kick out empty parts, they confuse the algorithm below.
131   L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
132   R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
133 
134   // Do it the hard way. At this point, both vectors must be non-empty.
135   StringRef LR = L[0], RR = R[0].Data;
136   unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
137   (void) RN;
138   for (;;) {
139     // Compare the current pieces.
140     if (LR.size() == RR.size()) {
141       // If they're the same length, it's pretty easy.
142       if (LR != RR)
143         return false;
144       // Both pieces are done, advance.
145       ++LI;
146       ++RI;
147       // If either string is done, they're both done, since they're the same
148       // length.
149       if (LI == LN) {
150         assert(RI == RN && "Strings not the same length after all?");
151         return true;
152       }
153       LR = L[LI];
154       RR = R[RI].Data;
155     } else if (LR.size() < RR.size()) {
156       // Right piece is longer.
157       if (!RR.startswith(LR))
158         return false;
159       ++LI;
160       assert(LI != LN && "Strings not the same length after all?");
161       RR = RR.substr(LR.size());
162       LR = L[LI];
163     } else {
164       // Left piece is longer.
165       if (!LR.startswith(RR))
166         return false;
167       ++RI;
168       assert(RI != RN && "Strings not the same length after all?");
169       LR = LR.substr(RR.size());
170       RR = R[RI].Data;
171     }
172   }
173 }
174 
175 static std::pair<FileID, StringRef::size_type>
176 FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
177   std::pair<FileID, StringRef::size_type> Res;
178   for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
179     Res.second = Buffers[I].Data.find(MacroDef);
180     if (Res.second != StringRef::npos) {
181       Res.first = Buffers[I].BufferID;
182       break;
183     }
184   }
185   return Res;
186 }
187 
188 bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
189                                         StringRef OriginalFileName,
190                                         std::string &SuggestedPredefines,
191                                         FileManager &FileMgr) {
192   // We are in the context of an implicit include, so the predefines buffer will
193   // have a #include entry for the PCH file itself (as normalized by the
194   // preprocessor initialization). Find it and skip over it in the checking
195   // below.
196   llvm::SmallString<256> PCHInclude;
197   PCHInclude += "#include \"";
198   PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
199   PCHInclude += "\"\n";
200   std::pair<StringRef,StringRef> Split =
201     StringRef(PP.getPredefines()).split(PCHInclude.str());
202   StringRef Left =  Split.first, Right = Split.second;
203   if (Left == PP.getPredefines()) {
204     Error("Missing PCH include entry!");
205     return true;
206   }
207 
208   // If the concatenation of all the PCH buffers is equal to the adjusted
209   // command line, we're done.
210   SmallVector<StringRef, 2> CommandLine;
211   CommandLine.push_back(Left);
212   CommandLine.push_back(Right);
213   if (EqualConcatenations(CommandLine, Buffers))
214     return false;
215 
216   SourceManager &SourceMgr = PP.getSourceManager();
217 
218   // The predefines buffers are different. Determine what the differences are,
219   // and whether they require us to reject the PCH file.
220   SmallVector<StringRef, 8> PCHLines;
221   for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
222     Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
223 
224   SmallVector<StringRef, 8> CmdLineLines;
225   Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
226 
227   // Pick out implicit #includes after the PCH and don't consider them for
228   // validation; we will insert them into SuggestedPredefines so that the
229   // preprocessor includes them.
230   std::string IncludesAfterPCH;
231   SmallVector<StringRef, 8> AfterPCHLines;
232   Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
233   for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
234     if (AfterPCHLines[i].startswith("#include ")) {
235       IncludesAfterPCH += AfterPCHLines[i];
236       IncludesAfterPCH += '\n';
237     } else {
238       CmdLineLines.push_back(AfterPCHLines[i]);
239     }
240   }
241 
242   // Make sure we add the includes last into SuggestedPredefines before we
243   // exit this function.
244   struct AddIncludesRAII {
245     std::string &SuggestedPredefines;
246     std::string &IncludesAfterPCH;
247 
248     AddIncludesRAII(std::string &SuggestedPredefines,
249                     std::string &IncludesAfterPCH)
250       : SuggestedPredefines(SuggestedPredefines),
251         IncludesAfterPCH(IncludesAfterPCH) { }
252     ~AddIncludesRAII() {
253       SuggestedPredefines += IncludesAfterPCH;
254     }
255   } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
256 
257   // Sort both sets of predefined buffer lines, since we allow some extra
258   // definitions and they may appear at any point in the output.
259   std::sort(CmdLineLines.begin(), CmdLineLines.end());
260   std::sort(PCHLines.begin(), PCHLines.end());
261 
262   // Determine which predefines that were used to build the PCH file are missing
263   // from the command line.
264   std::vector<StringRef> MissingPredefines;
265   std::set_difference(PCHLines.begin(), PCHLines.end(),
266                       CmdLineLines.begin(), CmdLineLines.end(),
267                       std::back_inserter(MissingPredefines));
268 
269   bool MissingDefines = false;
270   bool ConflictingDefines = false;
271   for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
272     StringRef Missing = MissingPredefines[I];
273     if (Missing.startswith("#include ")) {
274       // An -include was specified when generating the PCH; it is included in
275       // the PCH, just ignore it.
276       continue;
277     }
278     if (!Missing.startswith("#define ")) {
279       Reader.Diag(diag::warn_pch_compiler_options_mismatch);
280       return true;
281     }
282 
283     // This is a macro definition. Determine the name of the macro we're
284     // defining.
285     std::string::size_type StartOfMacroName = strlen("#define ");
286     std::string::size_type EndOfMacroName
287       = Missing.find_first_of("( \n\r", StartOfMacroName);
288     assert(EndOfMacroName != std::string::npos &&
289            "Couldn't find the end of the macro name");
290     StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
291 
292     // Determine whether this macro was given a different definition on the
293     // command line.
294     std::string MacroDefStart = "#define " + MacroName.str();
295     std::string::size_type MacroDefLen = MacroDefStart.size();
296     SmallVector<StringRef, 8>::iterator ConflictPos
297       = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
298                          MacroDefStart);
299     for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
300       if (!ConflictPos->startswith(MacroDefStart)) {
301         // Different macro; we're done.
302         ConflictPos = CmdLineLines.end();
303         break;
304       }
305 
306       assert(ConflictPos->size() > MacroDefLen &&
307              "Invalid #define in predefines buffer?");
308       if ((*ConflictPos)[MacroDefLen] != ' ' &&
309           (*ConflictPos)[MacroDefLen] != '(')
310         continue; // Longer macro name; keep trying.
311 
312       // We found a conflicting macro definition.
313       break;
314     }
315 
316     if (ConflictPos != CmdLineLines.end()) {
317       Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
318           << MacroName;
319 
320       // Show the definition of this macro within the PCH file.
321       std::pair<FileID, StringRef::size_type> MacroLoc =
322           FindMacro(Buffers, Missing);
323       assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
324       SourceLocation PCHMissingLoc =
325           SourceMgr.getLocForStartOfFile(MacroLoc.first)
326             .getFileLocWithOffset(MacroLoc.second);
327       Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
328 
329       ConflictingDefines = true;
330       continue;
331     }
332 
333     // If the macro doesn't conflict, then we'll just pick up the macro
334     // definition from the PCH file. Warn the user that they made a mistake.
335     if (ConflictingDefines)
336       continue; // Don't complain if there are already conflicting defs
337 
338     if (!MissingDefines) {
339       Reader.Diag(diag::warn_cmdline_missing_macro_defs);
340       MissingDefines = true;
341     }
342 
343     // Show the definition of this macro within the PCH file.
344     std::pair<FileID, StringRef::size_type> MacroLoc =
345         FindMacro(Buffers, Missing);
346     assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
347     SourceLocation PCHMissingLoc =
348         SourceMgr.getLocForStartOfFile(MacroLoc.first)
349           .getFileLocWithOffset(MacroLoc.second);
350     Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
351   }
352 
353   if (ConflictingDefines)
354     return true;
355 
356   // Determine what predefines were introduced based on command-line
357   // parameters that were not present when building the PCH
358   // file. Extra #defines are okay, so long as the identifiers being
359   // defined were not used within the precompiled header.
360   std::vector<StringRef> ExtraPredefines;
361   std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
362                       PCHLines.begin(), PCHLines.end(),
363                       std::back_inserter(ExtraPredefines));
364   for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
365     StringRef &Extra = ExtraPredefines[I];
366     if (!Extra.startswith("#define ")) {
367       Reader.Diag(diag::warn_pch_compiler_options_mismatch);
368       return true;
369     }
370 
371     // This is an extra macro definition. Determine the name of the
372     // macro we're defining.
373     std::string::size_type StartOfMacroName = strlen("#define ");
374     std::string::size_type EndOfMacroName
375       = Extra.find_first_of("( \n\r", StartOfMacroName);
376     assert(EndOfMacroName != std::string::npos &&
377            "Couldn't find the end of the macro name");
378     StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
379 
380     // Check whether this name was used somewhere in the PCH file. If
381     // so, defining it as a macro could change behavior, so we reject
382     // the PCH file.
383     if (IdentifierInfo *II = Reader.get(MacroName)) {
384       Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
385       return true;
386     }
387 
388     // Add this definition to the suggested predefines buffer.
389     SuggestedPredefines += Extra;
390     SuggestedPredefines += '\n';
391   }
392 
393   // If we get here, it's because the predefines buffer had compatible
394   // contents. Accept the PCH file.
395   return false;
396 }
397 
398 void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
399                                       unsigned ID) {
400   PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
401   ++NumHeaderInfos;
402 }
403 
404 void PCHValidator::ReadCounter(unsigned Value) {
405   PP.setCounterValue(Value);
406 }
407 
408 //===----------------------------------------------------------------------===//
409 // AST reader implementation
410 //===----------------------------------------------------------------------===//
411 
412 void
413 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
414   DeserializationListener = Listener;
415 }
416 
417 
418 
419 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
420   return serialization::ComputeHash(Sel);
421 }
422 
423 
424 std::pair<unsigned, unsigned>
425 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
426   using namespace clang::io;
427   unsigned KeyLen = ReadUnalignedLE16(d);
428   unsigned DataLen = ReadUnalignedLE16(d);
429   return std::make_pair(KeyLen, DataLen);
430 }
431 
432 ASTSelectorLookupTrait::internal_key_type
433 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
434   using namespace clang::io;
435   SelectorTable &SelTable = Reader.getContext().Selectors;
436   unsigned N = ReadUnalignedLE16(d);
437   IdentifierInfo *FirstII
438     = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
439   if (N == 0)
440     return SelTable.getNullarySelector(FirstII);
441   else if (N == 1)
442     return SelTable.getUnarySelector(FirstII);
443 
444   SmallVector<IdentifierInfo *, 16> Args;
445   Args.push_back(FirstII);
446   for (unsigned I = 1; I != N; ++I)
447     Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
448 
449   return SelTable.getSelector(N, Args.data());
450 }
451 
452 ASTSelectorLookupTrait::data_type
453 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
454                                  unsigned DataLen) {
455   using namespace clang::io;
456 
457   data_type Result;
458 
459   Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
460   unsigned NumInstanceMethods = ReadUnalignedLE16(d);
461   unsigned NumFactoryMethods = ReadUnalignedLE16(d);
462 
463   // Load instance methods
464   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
465     if (ObjCMethodDecl *Method
466           = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
467       Result.Instance.push_back(Method);
468   }
469 
470   // Load factory methods
471   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
472     if (ObjCMethodDecl *Method
473           = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
474       Result.Factory.push_back(Method);
475   }
476 
477   return Result;
478 }
479 
480 unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
481   return llvm::HashString(StringRef(a.first, a.second));
482 }
483 
484 std::pair<unsigned, unsigned>
485 ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
486   using namespace clang::io;
487   unsigned DataLen = ReadUnalignedLE16(d);
488   unsigned KeyLen = ReadUnalignedLE16(d);
489   return std::make_pair(KeyLen, DataLen);
490 }
491 
492 std::pair<const char*, unsigned>
493 ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
494   assert(n >= 2 && d[n-1] == '\0');
495   return std::make_pair((const char*) d, n-1);
496 }
497 
498 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
499                                                    const unsigned char* d,
500                                                    unsigned DataLen) {
501   using namespace clang::io;
502   unsigned RawID = ReadUnalignedLE32(d);
503   bool IsInteresting = RawID & 0x01;
504 
505   // Wipe out the "is interesting" bit.
506   RawID = RawID >> 1;
507 
508   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
509   if (!IsInteresting) {
510     // For uninteresting identifiers, just build the IdentifierInfo
511     // and associate it with the persistent ID.
512     IdentifierInfo *II = KnownII;
513     if (!II)
514       II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
515     Reader.SetIdentifierInfo(ID, II);
516     II->setIsFromAST();
517     return II;
518   }
519 
520   unsigned Bits = ReadUnalignedLE16(d);
521   bool CPlusPlusOperatorKeyword = Bits & 0x01;
522   Bits >>= 1;
523   bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
524   Bits >>= 1;
525   bool Poisoned = Bits & 0x01;
526   Bits >>= 1;
527   bool ExtensionToken = Bits & 0x01;
528   Bits >>= 1;
529   bool hasMacroDefinition = Bits & 0x01;
530   Bits >>= 1;
531   unsigned ObjCOrBuiltinID = Bits & 0x3FF;
532   Bits >>= 10;
533 
534   assert(Bits == 0 && "Extra bits in the identifier?");
535   DataLen -= 6;
536 
537   // Build the IdentifierInfo itself and link the identifier ID with
538   // the new IdentifierInfo.
539   IdentifierInfo *II = KnownII;
540   if (!II)
541     II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
542   Reader.SetIdentifierInfo(ID, II);
543 
544   // Set or check the various bits in the IdentifierInfo structure.
545   // Token IDs are read-only.
546   if (HasRevertedTokenIDToIdentifier)
547     II->RevertTokenIDToIdentifier();
548   II->setObjCOrBuiltinID(ObjCOrBuiltinID);
549   assert(II->isExtensionToken() == ExtensionToken &&
550          "Incorrect extension token flag");
551   (void)ExtensionToken;
552   if (Poisoned)
553     II->setIsPoisoned(true);
554   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
555          "Incorrect C++ operator keyword flag");
556   (void)CPlusPlusOperatorKeyword;
557 
558   // If this identifier is a macro, deserialize the macro
559   // definition.
560   if (hasMacroDefinition) {
561     // FIXME: Check for conflicts?
562     uint32_t Offset = ReadUnalignedLE32(d);
563     Reader.SetIdentifierIsMacro(II, F, Offset);
564     DataLen -= 4;
565   }
566 
567   // Read all of the declarations visible at global scope with this
568   // name.
569   if (DataLen > 0) {
570     SmallVector<uint32_t, 4> DeclIDs;
571     for (; DataLen > 0; DataLen -= 4)
572       DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
573     Reader.SetGloballyVisibleDecls(II, DeclIDs);
574   }
575 
576   II->setIsFromAST();
577   return II;
578 }
579 
580 unsigned
581 ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
582   llvm::FoldingSetNodeID ID;
583   ID.AddInteger(Key.Kind);
584 
585   switch (Key.Kind) {
586   case DeclarationName::Identifier:
587   case DeclarationName::CXXLiteralOperatorName:
588     ID.AddString(((IdentifierInfo*)Key.Data)->getName());
589     break;
590   case DeclarationName::ObjCZeroArgSelector:
591   case DeclarationName::ObjCOneArgSelector:
592   case DeclarationName::ObjCMultiArgSelector:
593     ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
594     break;
595   case DeclarationName::CXXOperatorName:
596     ID.AddInteger((OverloadedOperatorKind)Key.Data);
597     break;
598   case DeclarationName::CXXConstructorName:
599   case DeclarationName::CXXDestructorName:
600   case DeclarationName::CXXConversionFunctionName:
601   case DeclarationName::CXXUsingDirective:
602     break;
603   }
604 
605   return ID.ComputeHash();
606 }
607 
608 ASTDeclContextNameLookupTrait::internal_key_type
609 ASTDeclContextNameLookupTrait::GetInternalKey(
610                                           const external_key_type& Name) const {
611   DeclNameKey Key;
612   Key.Kind = Name.getNameKind();
613   switch (Name.getNameKind()) {
614   case DeclarationName::Identifier:
615     Key.Data = (uint64_t)Name.getAsIdentifierInfo();
616     break;
617   case DeclarationName::ObjCZeroArgSelector:
618   case DeclarationName::ObjCOneArgSelector:
619   case DeclarationName::ObjCMultiArgSelector:
620     Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
621     break;
622   case DeclarationName::CXXOperatorName:
623     Key.Data = Name.getCXXOverloadedOperator();
624     break;
625   case DeclarationName::CXXLiteralOperatorName:
626     Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
627     break;
628   case DeclarationName::CXXConstructorName:
629   case DeclarationName::CXXDestructorName:
630   case DeclarationName::CXXConversionFunctionName:
631   case DeclarationName::CXXUsingDirective:
632     Key.Data = 0;
633     break;
634   }
635 
636   return Key;
637 }
638 
639 ASTDeclContextNameLookupTrait::external_key_type
640 ASTDeclContextNameLookupTrait::GetExternalKey(
641                                           const internal_key_type& Key) const {
642   ASTContext &Context = Reader.getContext();
643   switch (Key.Kind) {
644   case DeclarationName::Identifier:
645     return DeclarationName((IdentifierInfo*)Key.Data);
646 
647   case DeclarationName::ObjCZeroArgSelector:
648   case DeclarationName::ObjCOneArgSelector:
649   case DeclarationName::ObjCMultiArgSelector:
650     return DeclarationName(Selector(Key.Data));
651 
652   case DeclarationName::CXXConstructorName:
653     return Context.DeclarationNames.getCXXConstructorName(
654              Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
655 
656   case DeclarationName::CXXDestructorName:
657     return Context.DeclarationNames.getCXXDestructorName(
658              Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
659 
660   case DeclarationName::CXXConversionFunctionName:
661     return Context.DeclarationNames.getCXXConversionFunctionName(
662              Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
663 
664   case DeclarationName::CXXOperatorName:
665     return Context.DeclarationNames.getCXXOperatorName(
666                                        (OverloadedOperatorKind)Key.Data);
667 
668   case DeclarationName::CXXLiteralOperatorName:
669     return Context.DeclarationNames.getCXXLiteralOperatorName(
670                                                    (IdentifierInfo*)Key.Data);
671 
672   case DeclarationName::CXXUsingDirective:
673     return DeclarationName::getUsingDirectiveName();
674   }
675 
676   llvm_unreachable("Invalid Name Kind ?");
677 }
678 
679 std::pair<unsigned, unsigned>
680 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
681   using namespace clang::io;
682   unsigned KeyLen = ReadUnalignedLE16(d);
683   unsigned DataLen = ReadUnalignedLE16(d);
684   return std::make_pair(KeyLen, DataLen);
685 }
686 
687 ASTDeclContextNameLookupTrait::internal_key_type
688 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
689   using namespace clang::io;
690 
691   DeclNameKey Key;
692   Key.Kind = (DeclarationName::NameKind)*d++;
693   switch (Key.Kind) {
694   case DeclarationName::Identifier:
695     Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
696     break;
697   case DeclarationName::ObjCZeroArgSelector:
698   case DeclarationName::ObjCOneArgSelector:
699   case DeclarationName::ObjCMultiArgSelector:
700     Key.Data =
701        (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
702                    .getAsOpaquePtr();
703     break;
704   case DeclarationName::CXXOperatorName:
705     Key.Data = *d++; // OverloadedOperatorKind
706     break;
707   case DeclarationName::CXXLiteralOperatorName:
708     Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
709     break;
710   case DeclarationName::CXXConstructorName:
711   case DeclarationName::CXXDestructorName:
712   case DeclarationName::CXXConversionFunctionName:
713   case DeclarationName::CXXUsingDirective:
714     Key.Data = 0;
715     break;
716   }
717 
718   return Key;
719 }
720 
721 ASTDeclContextNameLookupTrait::data_type
722 ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
723                                         const unsigned char* d,
724                                       unsigned DataLen) {
725   using namespace clang::io;
726   unsigned NumDecls = ReadUnalignedLE16(d);
727   DeclID *Start = (DeclID *)d;
728   return std::make_pair(Start, Start + NumDecls);
729 }
730 
731 bool ASTReader::ReadDeclContextStorage(Module &M,
732                                        llvm::BitstreamCursor &Cursor,
733                                    const std::pair<uint64_t, uint64_t> &Offsets,
734                                        DeclContextInfo &Info) {
735   SavedStreamPosition SavedPosition(Cursor);
736   // First the lexical decls.
737   if (Offsets.first != 0) {
738     Cursor.JumpToBit(Offsets.first);
739 
740     RecordData Record;
741     const char *Blob;
742     unsigned BlobLen;
743     unsigned Code = Cursor.ReadCode();
744     unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
745     if (RecCode != DECL_CONTEXT_LEXICAL) {
746       Error("Expected lexical block");
747       return true;
748     }
749 
750     Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
751     Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
752   }
753 
754   // Now the lookup table.
755   if (Offsets.second != 0) {
756     Cursor.JumpToBit(Offsets.second);
757 
758     RecordData Record;
759     const char *Blob;
760     unsigned BlobLen;
761     unsigned Code = Cursor.ReadCode();
762     unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
763     if (RecCode != DECL_CONTEXT_VISIBLE) {
764       Error("Expected visible lookup table block");
765       return true;
766     }
767     Info.NameLookupTableData
768       = ASTDeclContextNameLookupTable::Create(
769                     (const unsigned char *)Blob + Record[0],
770                     (const unsigned char *)Blob,
771                     ASTDeclContextNameLookupTrait(*this, M));
772   }
773 
774   return false;
775 }
776 
777 void ASTReader::Error(StringRef Msg) {
778   Error(diag::err_fe_pch_malformed, Msg);
779 }
780 
781 void ASTReader::Error(unsigned DiagID,
782                       StringRef Arg1, StringRef Arg2) {
783   if (Diags.isDiagnosticInFlight())
784     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
785   else
786     Diag(DiagID) << Arg1 << Arg2;
787 }
788 
789 /// \brief Tell the AST listener about the predefines buffers in the chain.
790 bool ASTReader::CheckPredefinesBuffers() {
791   if (Listener)
792     return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
793                                           ActualOriginalFileName,
794                                           SuggestedPredefines,
795                                           FileMgr);
796   return false;
797 }
798 
799 //===----------------------------------------------------------------------===//
800 // Source Manager Deserialization
801 //===----------------------------------------------------------------------===//
802 
803 /// \brief Read the line table in the source manager block.
804 /// \returns true if there was an error.
805 bool ASTReader::ParseLineTable(Module &F,
806                                SmallVectorImpl<uint64_t> &Record) {
807   unsigned Idx = 0;
808   LineTableInfo &LineTable = SourceMgr.getLineTable();
809 
810   // Parse the file names
811   std::map<int, int> FileIDs;
812   for (int I = 0, N = Record[Idx++]; I != N; ++I) {
813     // Extract the file name
814     unsigned FilenameLen = Record[Idx++];
815     std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
816     Idx += FilenameLen;
817     MaybeAddSystemRootToFilename(Filename);
818     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
819   }
820 
821   // Parse the line entries
822   std::vector<LineEntry> Entries;
823   while (Idx < Record.size()) {
824     int FID = Record[Idx++];
825     assert(FID >= 0 && "Serialized line entries for non-local file.");
826     // Remap FileID from 1-based old view.
827     FID += F.SLocEntryBaseID - 1;
828 
829     // Extract the line entries
830     unsigned NumEntries = Record[Idx++];
831     assert(NumEntries && "Numentries is 00000");
832     Entries.clear();
833     Entries.reserve(NumEntries);
834     for (unsigned I = 0; I != NumEntries; ++I) {
835       unsigned FileOffset = Record[Idx++];
836       unsigned LineNo = Record[Idx++];
837       int FilenameID = FileIDs[Record[Idx++]];
838       SrcMgr::CharacteristicKind FileKind
839         = (SrcMgr::CharacteristicKind)Record[Idx++];
840       unsigned IncludeOffset = Record[Idx++];
841       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
842                                        FileKind, IncludeOffset));
843     }
844     LineTable.AddEntry(FID, Entries);
845   }
846 
847   return false;
848 }
849 
850 namespace {
851 
852 class ASTStatData {
853 public:
854   const ino_t ino;
855   const dev_t dev;
856   const mode_t mode;
857   const time_t mtime;
858   const off_t size;
859 
860   ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
861     : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
862 };
863 
864 class ASTStatLookupTrait {
865  public:
866   typedef const char *external_key_type;
867   typedef const char *internal_key_type;
868 
869   typedef ASTStatData data_type;
870 
871   static unsigned ComputeHash(const char *path) {
872     return llvm::HashString(path);
873   }
874 
875   static internal_key_type GetInternalKey(const char *path) { return path; }
876 
877   static bool EqualKey(internal_key_type a, internal_key_type b) {
878     return strcmp(a, b) == 0;
879   }
880 
881   static std::pair<unsigned, unsigned>
882   ReadKeyDataLength(const unsigned char*& d) {
883     unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
884     unsigned DataLen = (unsigned) *d++;
885     return std::make_pair(KeyLen + 1, DataLen);
886   }
887 
888   static internal_key_type ReadKey(const unsigned char *d, unsigned) {
889     return (const char *)d;
890   }
891 
892   static data_type ReadData(const internal_key_type, const unsigned char *d,
893                             unsigned /*DataLen*/) {
894     using namespace clang::io;
895 
896     ino_t ino = (ino_t) ReadUnalignedLE32(d);
897     dev_t dev = (dev_t) ReadUnalignedLE32(d);
898     mode_t mode = (mode_t) ReadUnalignedLE16(d);
899     time_t mtime = (time_t) ReadUnalignedLE64(d);
900     off_t size = (off_t) ReadUnalignedLE64(d);
901     return data_type(ino, dev, mode, mtime, size);
902   }
903 };
904 
905 /// \brief stat() cache for precompiled headers.
906 ///
907 /// This cache is very similar to the stat cache used by pretokenized
908 /// headers.
909 class ASTStatCache : public FileSystemStatCache {
910   typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
911   CacheTy *Cache;
912 
913   unsigned &NumStatHits, &NumStatMisses;
914 public:
915   ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
916                unsigned &NumStatHits, unsigned &NumStatMisses)
917     : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
918     Cache = CacheTy::Create(Buckets, Base);
919   }
920 
921   ~ASTStatCache() { delete Cache; }
922 
923   LookupResult getStat(const char *Path, struct stat &StatBuf,
924                        int *FileDescriptor) {
925     // Do the lookup for the file's data in the AST file.
926     CacheTy::iterator I = Cache->find(Path);
927 
928     // If we don't get a hit in the AST file just forward to 'stat'.
929     if (I == Cache->end()) {
930       ++NumStatMisses;
931       return statChained(Path, StatBuf, FileDescriptor);
932     }
933 
934     ++NumStatHits;
935     ASTStatData Data = *I;
936 
937     StatBuf.st_ino = Data.ino;
938     StatBuf.st_dev = Data.dev;
939     StatBuf.st_mtime = Data.mtime;
940     StatBuf.st_mode = Data.mode;
941     StatBuf.st_size = Data.size;
942     return CacheExists;
943   }
944 };
945 } // end anonymous namespace
946 
947 
948 /// \brief Read a source manager block
949 ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
950   using namespace SrcMgr;
951 
952   llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
953 
954   // Set the source-location entry cursor to the current position in
955   // the stream. This cursor will be used to read the contents of the
956   // source manager block initially, and then lazily read
957   // source-location entries as needed.
958   SLocEntryCursor = F.Stream;
959 
960   // The stream itself is going to skip over the source manager block.
961   if (F.Stream.SkipBlock()) {
962     Error("malformed block record in AST file");
963     return Failure;
964   }
965 
966   // Enter the source manager block.
967   if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
968     Error("malformed source manager block record in AST file");
969     return Failure;
970   }
971 
972   RecordData Record;
973   while (true) {
974     unsigned Code = SLocEntryCursor.ReadCode();
975     if (Code == llvm::bitc::END_BLOCK) {
976       if (SLocEntryCursor.ReadBlockEnd()) {
977         Error("error at end of Source Manager block in AST file");
978         return Failure;
979       }
980       return Success;
981     }
982 
983     if (Code == llvm::bitc::ENTER_SUBBLOCK) {
984       // No known subblocks, always skip them.
985       SLocEntryCursor.ReadSubBlockID();
986       if (SLocEntryCursor.SkipBlock()) {
987         Error("malformed block record in AST file");
988         return Failure;
989       }
990       continue;
991     }
992 
993     if (Code == llvm::bitc::DEFINE_ABBREV) {
994       SLocEntryCursor.ReadAbbrevRecord();
995       continue;
996     }
997 
998     // Read a record.
999     const char *BlobStart;
1000     unsigned BlobLen;
1001     Record.clear();
1002     switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1003     default:  // Default behavior: ignore.
1004       break;
1005 
1006     case SM_SLOC_FILE_ENTRY:
1007     case SM_SLOC_BUFFER_ENTRY:
1008     case SM_SLOC_EXPANSION_ENTRY:
1009       // Once we hit one of the source location entries, we're done.
1010       return Success;
1011     }
1012   }
1013 }
1014 
1015 /// \brief If a header file is not found at the path that we expect it to be
1016 /// and the PCH file was moved from its original location, try to resolve the
1017 /// file by assuming that header+PCH were moved together and the header is in
1018 /// the same place relative to the PCH.
1019 static std::string
1020 resolveFileRelativeToOriginalDir(const std::string &Filename,
1021                                  const std::string &OriginalDir,
1022                                  const std::string &CurrDir) {
1023   assert(OriginalDir != CurrDir &&
1024          "No point trying to resolve the file if the PCH dir didn't change");
1025   using namespace llvm::sys;
1026   llvm::SmallString<128> filePath(Filename);
1027   fs::make_absolute(filePath);
1028   assert(path::is_absolute(OriginalDir));
1029   llvm::SmallString<128> currPCHPath(CurrDir);
1030 
1031   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1032                        fileDirE = path::end(path::parent_path(filePath));
1033   path::const_iterator origDirI = path::begin(OriginalDir),
1034                        origDirE = path::end(OriginalDir);
1035   // Skip the common path components from filePath and OriginalDir.
1036   while (fileDirI != fileDirE && origDirI != origDirE &&
1037          *fileDirI == *origDirI) {
1038     ++fileDirI;
1039     ++origDirI;
1040   }
1041   for (; origDirI != origDirE; ++origDirI)
1042     path::append(currPCHPath, "..");
1043   path::append(currPCHPath, fileDirI, fileDirE);
1044   path::append(currPCHPath, path::filename(Filename));
1045   return currPCHPath.str();
1046 }
1047 
1048 /// \brief Read in the source location entry with the given ID.
1049 ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
1050   if (ID == 0)
1051     return Success;
1052 
1053   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1054     Error("source location entry ID out-of-range for AST file");
1055     return Failure;
1056   }
1057 
1058   Module *F = GlobalSLocEntryMap.find(-ID)->second;
1059   F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
1060   llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1061   unsigned BaseOffset = F->SLocEntryBaseOffset;
1062 
1063   ++NumSLocEntriesRead;
1064   unsigned Code = SLocEntryCursor.ReadCode();
1065   if (Code == llvm::bitc::END_BLOCK ||
1066       Code == llvm::bitc::ENTER_SUBBLOCK ||
1067       Code == llvm::bitc::DEFINE_ABBREV) {
1068     Error("incorrectly-formatted source location entry in AST file");
1069     return Failure;
1070   }
1071 
1072   RecordData Record;
1073   const char *BlobStart;
1074   unsigned BlobLen;
1075   switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1076   default:
1077     Error("incorrectly-formatted source location entry in AST file");
1078     return Failure;
1079 
1080   case SM_SLOC_FILE_ENTRY: {
1081     std::string Filename(BlobStart, BlobStart + BlobLen);
1082     MaybeAddSystemRootToFilename(Filename);
1083     const FileEntry *File = FileMgr.getFile(Filename);
1084     if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1085         OriginalDir != CurrentDir) {
1086       std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1087                                                               OriginalDir,
1088                                                               CurrentDir);
1089       if (!resolved.empty())
1090         File = FileMgr.getFile(resolved);
1091     }
1092     if (File == 0)
1093       File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1094                                     (time_t)Record[5]);
1095     if (File == 0) {
1096       std::string ErrorStr = "could not find file '";
1097       ErrorStr += Filename;
1098       ErrorStr += "' referenced by AST file";
1099       Error(ErrorStr.c_str());
1100       return Failure;
1101     }
1102 
1103     if (Record.size() < 6) {
1104       Error("source location entry is incorrect");
1105       return Failure;
1106     }
1107 
1108     if (!DisableValidation &&
1109         ((off_t)Record[4] != File->getSize()
1110 #if !defined(LLVM_ON_WIN32)
1111         // In our regression testing, the Windows file system seems to
1112         // have inconsistent modification times that sometimes
1113         // erroneously trigger this error-handling path.
1114          || (time_t)Record[5] != File->getModificationTime()
1115 #endif
1116         )) {
1117       Error(diag::err_fe_pch_file_modified, Filename);
1118       return Failure;
1119     }
1120 
1121     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1122     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1123       // This is the module's main file.
1124       IncludeLoc = getImportLocation(F);
1125     }
1126     FileID FID = SourceMgr.createFileID(File, IncludeLoc,
1127                                         (SrcMgr::CharacteristicKind)Record[2],
1128                                         ID, BaseOffset + Record[0]);
1129     SrcMgr::FileInfo &FileInfo =
1130           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1131     FileInfo.NumCreatedFIDs = Record[6];
1132     if (Record[3])
1133       FileInfo.setHasLineDirectives();
1134 
1135     break;
1136   }
1137 
1138   case SM_SLOC_BUFFER_ENTRY: {
1139     const char *Name = BlobStart;
1140     unsigned Offset = Record[0];
1141     unsigned Code = SLocEntryCursor.ReadCode();
1142     Record.clear();
1143     unsigned RecCode
1144       = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1145 
1146     if (RecCode != SM_SLOC_BUFFER_BLOB) {
1147       Error("AST record has invalid code");
1148       return Failure;
1149     }
1150 
1151     llvm::MemoryBuffer *Buffer
1152     = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1153                                        Name);
1154     FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1155                                                          BaseOffset + Offset);
1156 
1157     if (strcmp(Name, "<built-in>") == 0) {
1158       PCHPredefinesBlock Block = {
1159         BufferID,
1160         StringRef(BlobStart, BlobLen - 1)
1161       };
1162       PCHPredefinesBuffers.push_back(Block);
1163     }
1164 
1165     break;
1166   }
1167 
1168   case SM_SLOC_EXPANSION_ENTRY: {
1169     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1170     SourceMgr.createExpansionLoc(SpellingLoc,
1171                                      ReadSourceLocation(*F, Record[2]),
1172                                      ReadSourceLocation(*F, Record[3]),
1173                                      Record[4],
1174                                      ID,
1175                                      BaseOffset + Record[0]);
1176     break;
1177   }
1178   }
1179 
1180   return Success;
1181 }
1182 
1183 /// \brief Find the location where the module F is imported.
1184 SourceLocation ASTReader::getImportLocation(Module *F) {
1185   if (F->ImportLoc.isValid())
1186     return F->ImportLoc;
1187 
1188   // Otherwise we have a PCH. It's considered to be "imported" at the first
1189   // location of its includer.
1190   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1191     // Main file is the importer. We assume that it is the first entry in the
1192     // entry table. We can't ask the manager, because at the time of PCH loading
1193     // the main file entry doesn't exist yet.
1194     // The very first entry is the invalid instantiation loc, which takes up
1195     // offsets 0 and 1.
1196     return SourceLocation::getFromRawEncoding(2U);
1197   }
1198   //return F->Loaders[0]->FirstLoc;
1199   return F->ImportedBy[0]->FirstLoc;
1200 }
1201 
1202 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1203 /// specified cursor.  Read the abbreviations that are at the top of the block
1204 /// and then leave the cursor pointing into the block.
1205 bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1206                                  unsigned BlockID) {
1207   if (Cursor.EnterSubBlock(BlockID)) {
1208     Error("malformed block record in AST file");
1209     return Failure;
1210   }
1211 
1212   while (true) {
1213     uint64_t Offset = Cursor.GetCurrentBitNo();
1214     unsigned Code = Cursor.ReadCode();
1215 
1216     // We expect all abbrevs to be at the start of the block.
1217     if (Code != llvm::bitc::DEFINE_ABBREV) {
1218       Cursor.JumpToBit(Offset);
1219       return false;
1220     }
1221     Cursor.ReadAbbrevRecord();
1222   }
1223 }
1224 
1225 void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
1226   llvm::BitstreamCursor &Stream = F.MacroCursor;
1227 
1228   // Keep track of where we are in the stream, then jump back there
1229   // after reading this macro.
1230   SavedStreamPosition SavedPosition(Stream);
1231 
1232   Stream.JumpToBit(Offset);
1233   RecordData Record;
1234   SmallVector<IdentifierInfo*, 16> MacroArgs;
1235   MacroInfo *Macro = 0;
1236 
1237   while (true) {
1238     unsigned Code = Stream.ReadCode();
1239     switch (Code) {
1240     case llvm::bitc::END_BLOCK:
1241       return;
1242 
1243     case llvm::bitc::ENTER_SUBBLOCK:
1244       // No known subblocks, always skip them.
1245       Stream.ReadSubBlockID();
1246       if (Stream.SkipBlock()) {
1247         Error("malformed block record in AST file");
1248         return;
1249       }
1250       continue;
1251 
1252     case llvm::bitc::DEFINE_ABBREV:
1253       Stream.ReadAbbrevRecord();
1254       continue;
1255     default: break;
1256     }
1257 
1258     // Read a record.
1259     const char *BlobStart = 0;
1260     unsigned BlobLen = 0;
1261     Record.clear();
1262     PreprocessorRecordTypes RecType =
1263       (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
1264                                                  BlobLen);
1265     switch (RecType) {
1266     case PP_MACRO_OBJECT_LIKE:
1267     case PP_MACRO_FUNCTION_LIKE: {
1268       // If we already have a macro, that means that we've hit the end
1269       // of the definition of the macro we were looking for. We're
1270       // done.
1271       if (Macro)
1272         return;
1273 
1274       IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
1275       if (II == 0) {
1276         Error("macro must have a name in AST file");
1277         return;
1278       }
1279       SourceLocation Loc = ReadSourceLocation(F, Record[1]);
1280       bool isUsed = Record[2];
1281 
1282       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1283       MI->setIsUsed(isUsed);
1284       MI->setIsFromAST();
1285 
1286       unsigned NextIndex = 3;
1287       MI->setExportLocation(ReadSourceLocation(F, Record, NextIndex));
1288 
1289       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1290         // Decode function-like macro info.
1291         bool isC99VarArgs = Record[NextIndex++];
1292         bool isGNUVarArgs = Record[NextIndex++];
1293         MacroArgs.clear();
1294         unsigned NumArgs = Record[NextIndex++];
1295         for (unsigned i = 0; i != NumArgs; ++i)
1296           MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1297 
1298         // Install function-like macro info.
1299         MI->setIsFunctionLike();
1300         if (isC99VarArgs) MI->setIsC99Varargs();
1301         if (isGNUVarArgs) MI->setIsGNUVarargs();
1302         MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1303                             PP.getPreprocessorAllocator());
1304       }
1305 
1306       // Finally, install the macro.
1307       PP.setMacroInfo(II, MI);
1308 
1309       // Remember that we saw this macro last so that we add the tokens that
1310       // form its body to it.
1311       Macro = MI;
1312 
1313       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1314           Record[NextIndex]) {
1315         // We have a macro definition. Register the association
1316         PreprocessedEntityID
1317             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1318         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1319         PPRec.RegisterMacroDefinition(Macro,
1320                             PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
1321       }
1322 
1323       ++NumMacrosRead;
1324       break;
1325     }
1326 
1327     case PP_TOKEN: {
1328       // If we see a TOKEN before a PP_MACRO_*, then the file is
1329       // erroneous, just pretend we didn't see this.
1330       if (Macro == 0) break;
1331 
1332       Token Tok;
1333       Tok.startToken();
1334       Tok.setLocation(ReadSourceLocation(F, Record[0]));
1335       Tok.setLength(Record[1]);
1336       if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
1337         Tok.setIdentifierInfo(II);
1338       Tok.setKind((tok::TokenKind)Record[3]);
1339       Tok.setFlag((Token::TokenFlags)Record[4]);
1340       Macro->AddTokenToBody(Tok);
1341       break;
1342     }
1343   }
1344   }
1345 
1346   return;
1347 }
1348 
1349 PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
1350   unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1351   switch (Code) {
1352   case llvm::bitc::END_BLOCK:
1353     return 0;
1354 
1355   case llvm::bitc::ENTER_SUBBLOCK:
1356     Error("unexpected subblock record in preprocessor detail block");
1357     return 0;
1358 
1359   case llvm::bitc::DEFINE_ABBREV:
1360     Error("unexpected abbrevation record in preprocessor detail block");
1361     return 0;
1362 
1363   default:
1364     break;
1365   }
1366 
1367   if (!PP.getPreprocessingRecord()) {
1368     Error("no preprocessing record");
1369     return 0;
1370   }
1371 
1372   // Read the record.
1373   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1374   const char *BlobStart = 0;
1375   unsigned BlobLen = 0;
1376   RecordData Record;
1377   PreprocessorDetailRecordTypes RecType =
1378     (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1379                                              Code, Record, BlobStart, BlobLen);
1380   switch (RecType) {
1381   case PPD_MACRO_EXPANSION: {
1382     bool isBuiltin = Record[3];
1383     MacroExpansion *ME;
1384     if (isBuiltin) {
1385       ME = new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[4]),
1386                                  SourceRange(ReadSourceLocation(F, Record[1]),
1387                                              ReadSourceLocation(F, Record[2])));
1388     } else {
1389       PreprocessedEntityID
1390           GlobalID = getGlobalPreprocessedEntityID(F, Record[4]);
1391       ME = new (PPRec) MacroExpansion(
1392            cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1)),
1393                                  SourceRange(ReadSourceLocation(F, Record[1]),
1394                                              ReadSourceLocation(F, Record[2])));
1395     }
1396     return ME;
1397   }
1398 
1399   case PPD_MACRO_DEFINITION: {
1400     PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
1401 
1402     // Decode the identifier info and then check again; if the macro is
1403     // still defined and associated with the identifier,
1404     IdentifierInfo *II = getLocalIdentifier(F, Record[3]);
1405     MacroDefinition *MD
1406       = new (PPRec) MacroDefinition(II,
1407                                     ReadSourceLocation(F, Record[4]),
1408                                     SourceRange(
1409                                           ReadSourceLocation(F, Record[1]),
1410                                           ReadSourceLocation(F, Record[2])));
1411 
1412     if (DeserializationListener)
1413       DeserializationListener->MacroDefinitionRead(GlobalID, MD);
1414 
1415     return MD;
1416   }
1417 
1418   case PPD_INCLUSION_DIRECTIVE: {
1419     const char *FullFileNameStart = BlobStart + Record[3];
1420     const FileEntry *File
1421       = PP.getFileManager().getFile(StringRef(FullFileNameStart,
1422                                                BlobLen - Record[3]));
1423 
1424     // FIXME: Stable encoding
1425     InclusionDirective::InclusionKind Kind
1426       = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1427     InclusionDirective *ID
1428       = new (PPRec) InclusionDirective(PPRec, Kind,
1429                                        StringRef(BlobStart, Record[3]),
1430                                        Record[4],
1431                                        File,
1432                                  SourceRange(ReadSourceLocation(F, Record[1]),
1433                                              ReadSourceLocation(F, Record[2])));
1434     return ID;
1435   }
1436   }
1437 
1438   Error("invalid offset in preprocessor detail block");
1439   return 0;
1440 }
1441 
1442 PreprocessedEntityID
1443 ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
1444   ContinuousRangeMap<uint32_t, int, 2>::iterator
1445     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1446   assert(I != M.PreprocessedEntityRemap.end()
1447          && "Invalid index into preprocessed entity index remap");
1448 
1449   return LocalID + I->second;
1450 }
1451 
1452 unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1453   return llvm::HashString(llvm::sys::path::filename(path));
1454 }
1455 
1456 HeaderFileInfoTrait::internal_key_type
1457 HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1458 
1459 bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1460   if (strcmp(a, b) == 0)
1461     return true;
1462 
1463   if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1464     return false;
1465 
1466   // The file names match, but the path names don't. stat() the files to
1467   // see if they are the same.
1468   struct stat StatBufA, StatBufB;
1469   if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1470     return false;
1471 
1472   return StatBufA.st_ino == StatBufB.st_ino;
1473 }
1474 
1475 std::pair<unsigned, unsigned>
1476 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1477   unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1478   unsigned DataLen = (unsigned) *d++;
1479   return std::make_pair(KeyLen + 1, DataLen);
1480 }
1481 
1482 HeaderFileInfoTrait::data_type
1483 HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1484                               unsigned DataLen) {
1485   const unsigned char *End = d + DataLen;
1486   using namespace clang::io;
1487   HeaderFileInfo HFI;
1488   unsigned Flags = *d++;
1489   HFI.isImport = (Flags >> 5) & 0x01;
1490   HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1491   HFI.DirInfo = (Flags >> 2) & 0x03;
1492   HFI.Resolved = (Flags >> 1) & 0x01;
1493   HFI.IndexHeaderMapHeader = Flags & 0x01;
1494   HFI.NumIncludes = ReadUnalignedLE16(d);
1495   HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1496   if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1497     // The framework offset is 1 greater than the actual offset,
1498     // since 0 is used as an indicator for "no framework name".
1499     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1500     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1501   }
1502 
1503   assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1504   (void)End;
1505 
1506   // This HeaderFileInfo was externally loaded.
1507   HFI.External = true;
1508   return HFI;
1509 }
1510 
1511 void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
1512                                      uint64_t LocalOffset) {
1513   // Note that this identifier has a macro definition.
1514   II->setHasMacroDefinition(true);
1515 
1516   // Adjust the offset to a global offset.
1517   UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
1518 }
1519 
1520 void ASTReader::ReadDefinedMacros() {
1521   for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1522       E = ModuleMgr.rend(); I != E; ++I) {
1523     llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1524 
1525     // If there was no preprocessor block, skip this file.
1526     if (!MacroCursor.getBitStreamReader())
1527       continue;
1528 
1529     llvm::BitstreamCursor Cursor = MacroCursor;
1530     Cursor.JumpToBit((*I)->MacroStartOffset);
1531 
1532     RecordData Record;
1533     while (true) {
1534       unsigned Code = Cursor.ReadCode();
1535       if (Code == llvm::bitc::END_BLOCK)
1536         break;
1537 
1538       if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1539         // No known subblocks, always skip them.
1540         Cursor.ReadSubBlockID();
1541         if (Cursor.SkipBlock()) {
1542           Error("malformed block record in AST file");
1543           return;
1544         }
1545         continue;
1546       }
1547 
1548       if (Code == llvm::bitc::DEFINE_ABBREV) {
1549         Cursor.ReadAbbrevRecord();
1550         continue;
1551       }
1552 
1553       // Read a record.
1554       const char *BlobStart;
1555       unsigned BlobLen;
1556       Record.clear();
1557       switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1558       default:  // Default behavior: ignore.
1559         break;
1560 
1561       case PP_MACRO_OBJECT_LIKE:
1562       case PP_MACRO_FUNCTION_LIKE:
1563         getLocalIdentifier(**I, Record[0]);
1564         break;
1565 
1566       case PP_TOKEN:
1567         // Ignore tokens.
1568         break;
1569       }
1570     }
1571   }
1572 
1573   // Drain the unread macro-record offsets map.
1574   while (!UnreadMacroRecordOffsets.empty())
1575     LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1576 }
1577 
1578 void ASTReader::LoadMacroDefinition(
1579                      llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1580   assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1581   uint64_t Offset = Pos->second;
1582   UnreadMacroRecordOffsets.erase(Pos);
1583 
1584   RecordLocation Loc = getLocalBitOffset(Offset);
1585   ReadMacroRecord(*Loc.F, Loc.Offset);
1586 }
1587 
1588 void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1589   llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1590     = UnreadMacroRecordOffsets.find(II);
1591   LoadMacroDefinition(Pos);
1592 }
1593 
1594 const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1595   std::string Filename = filenameStrRef;
1596   MaybeAddSystemRootToFilename(Filename);
1597   const FileEntry *File = FileMgr.getFile(Filename);
1598   if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1599       OriginalDir != CurrentDir) {
1600     std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1601                                                             OriginalDir,
1602                                                             CurrentDir);
1603     if (!resolved.empty())
1604       File = FileMgr.getFile(resolved);
1605   }
1606 
1607   return File;
1608 }
1609 
1610 /// \brief If we are loading a relocatable PCH file, and the filename is
1611 /// not an absolute path, add the system root to the beginning of the file
1612 /// name.
1613 void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1614   // If this is not a relocatable PCH file, there's nothing to do.
1615   if (!RelocatablePCH)
1616     return;
1617 
1618   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1619     return;
1620 
1621   if (isysroot.empty()) {
1622     // If no system root was given, default to '/'
1623     Filename.insert(Filename.begin(), '/');
1624     return;
1625   }
1626 
1627   unsigned Length = isysroot.size();
1628   if (isysroot[Length - 1] != '/')
1629     Filename.insert(Filename.begin(), '/');
1630 
1631   Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1632 }
1633 
1634 ASTReader::ASTReadResult
1635 ASTReader::ReadASTBlock(Module &F) {
1636   llvm::BitstreamCursor &Stream = F.Stream;
1637 
1638   if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1639     Error("malformed block record in AST file");
1640     return Failure;
1641   }
1642 
1643   // Read all of the records and blocks for the ASt file.
1644   RecordData Record;
1645   while (!Stream.AtEndOfStream()) {
1646     unsigned Code = Stream.ReadCode();
1647     if (Code == llvm::bitc::END_BLOCK) {
1648       if (Stream.ReadBlockEnd()) {
1649         Error("error at end of module block in AST file");
1650         return Failure;
1651       }
1652 
1653       return Success;
1654     }
1655 
1656     if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1657       switch (Stream.ReadSubBlockID()) {
1658       case DECLTYPES_BLOCK_ID:
1659         // We lazily load the decls block, but we want to set up the
1660         // DeclsCursor cursor to point into it.  Clone our current bitcode
1661         // cursor to it, enter the block and read the abbrevs in that block.
1662         // With the main cursor, we just skip over it.
1663         F.DeclsCursor = Stream;
1664         if (Stream.SkipBlock() ||  // Skip with the main cursor.
1665             // Read the abbrevs.
1666             ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1667           Error("malformed block record in AST file");
1668           return Failure;
1669         }
1670         break;
1671 
1672       case DECL_UPDATES_BLOCK_ID:
1673         if (Stream.SkipBlock()) {
1674           Error("malformed block record in AST file");
1675           return Failure;
1676         }
1677         break;
1678 
1679       case PREPROCESSOR_BLOCK_ID:
1680         F.MacroCursor = Stream;
1681         if (!PP.getExternalSource())
1682           PP.setExternalSource(this);
1683 
1684         if (Stream.SkipBlock() ||
1685             ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1686           Error("malformed block record in AST file");
1687           return Failure;
1688         }
1689         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1690         break;
1691 
1692       case PREPROCESSOR_DETAIL_BLOCK_ID:
1693         F.PreprocessorDetailCursor = Stream;
1694         if (Stream.SkipBlock() ||
1695             ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1696                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
1697           Error("malformed preprocessor detail record in AST file");
1698           return Failure;
1699         }
1700         F.PreprocessorDetailStartOffset
1701           = F.PreprocessorDetailCursor.GetCurrentBitNo();
1702 
1703         if (!PP.getPreprocessingRecord())
1704           PP.createPreprocessingRecord(true);
1705         if (!PP.getPreprocessingRecord()->getExternalSource())
1706           PP.getPreprocessingRecord()->SetExternalSource(*this);
1707         break;
1708 
1709       case SOURCE_MANAGER_BLOCK_ID:
1710         switch (ReadSourceManagerBlock(F)) {
1711         case Success:
1712           break;
1713 
1714         case Failure:
1715           Error("malformed source manager block in AST file");
1716           return Failure;
1717 
1718         case IgnorePCH:
1719           return IgnorePCH;
1720         }
1721         break;
1722       }
1723       continue;
1724     }
1725 
1726     if (Code == llvm::bitc::DEFINE_ABBREV) {
1727       Stream.ReadAbbrevRecord();
1728       continue;
1729     }
1730 
1731     // Read and process a record.
1732     Record.clear();
1733     const char *BlobStart = 0;
1734     unsigned BlobLen = 0;
1735     switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1736                                               &BlobStart, &BlobLen)) {
1737     default:  // Default behavior: ignore.
1738       break;
1739 
1740     case METADATA: {
1741       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1742         Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1743                                            : diag::warn_pch_version_too_new);
1744         return IgnorePCH;
1745       }
1746 
1747       RelocatablePCH = Record[4];
1748       if (Listener) {
1749         std::string TargetTriple(BlobStart, BlobLen);
1750         if (Listener->ReadTargetTriple(TargetTriple))
1751           return IgnorePCH;
1752       }
1753       break;
1754     }
1755 
1756     case IMPORTS: {
1757       // Load each of the imported PCH files.
1758       unsigned Idx = 0, N = Record.size();
1759       while (Idx < N) {
1760         // Read information about the AST file.
1761         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1762         unsigned Length = Record[Idx++];
1763         llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1764                                             Record.begin() + Idx + Length);
1765         Idx += Length;
1766 
1767         // Load the AST file.
1768         switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
1769         case Failure: return Failure;
1770           // If we have to ignore the dependency, we'll have to ignore this too.
1771         case IgnorePCH: return IgnorePCH;
1772         case Success: break;
1773         }
1774       }
1775       break;
1776     }
1777 
1778     case TYPE_OFFSET: {
1779       if (F.LocalNumTypes != 0) {
1780         Error("duplicate TYPE_OFFSET record in AST file");
1781         return Failure;
1782       }
1783       F.TypeOffsets = (const uint32_t *)BlobStart;
1784       F.LocalNumTypes = Record[0];
1785       unsigned LocalBaseTypeIndex = Record[1];
1786       F.BaseTypeIndex = getTotalNumTypes();
1787 
1788       if (F.LocalNumTypes > 0) {
1789         // Introduce the global -> local mapping for types within this module.
1790         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1791 
1792         // Introduce the local -> global mapping for types within this module.
1793         F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1794                              F.BaseTypeIndex - LocalBaseTypeIndex));
1795 
1796         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1797       }
1798       break;
1799     }
1800 
1801     case DECL_OFFSET: {
1802       if (F.LocalNumDecls != 0) {
1803         Error("duplicate DECL_OFFSET record in AST file");
1804         return Failure;
1805       }
1806       F.DeclOffsets = (const uint32_t *)BlobStart;
1807       F.LocalNumDecls = Record[0];
1808       unsigned LocalBaseDeclID = Record[1];
1809       F.BaseDeclID = getTotalNumDecls();
1810 
1811       if (F.LocalNumDecls > 0) {
1812         // Introduce the global -> local mapping for declarations within this
1813         // module.
1814         GlobalDeclMap.insert(
1815           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
1816 
1817         // Introduce the local -> global mapping for declarations within this
1818         // module.
1819         F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1820                                           F.BaseDeclID - LocalBaseDeclID));
1821 
1822         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1823       }
1824       break;
1825     }
1826 
1827     case TU_UPDATE_LEXICAL: {
1828       DeclContext *TU = Context.getTranslationUnitDecl();
1829       DeclContextInfo &Info = F.DeclContextInfos[TU];
1830       Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1831       Info.NumLexicalDecls
1832         = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
1833       TU->setHasExternalLexicalStorage(true);
1834       break;
1835     }
1836 
1837     case UPDATE_VISIBLE: {
1838       unsigned Idx = 0;
1839       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
1840       void *Table = ASTDeclContextNameLookupTable::Create(
1841                         (const unsigned char *)BlobStart + Record[Idx++],
1842                         (const unsigned char *)BlobStart,
1843                         ASTDeclContextNameLookupTrait(*this, F));
1844       if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1845         DeclContext *TU = Context.getTranslationUnitDecl();
1846         F.DeclContextInfos[TU].NameLookupTableData = Table;
1847         TU->setHasExternalVisibleStorage(true);
1848       } else
1849         PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
1850       break;
1851     }
1852 
1853     case REDECLS_UPDATE_LATEST: {
1854       assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1855       for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1856         DeclID First = ReadDeclID(F, Record, i);
1857         DeclID Latest = ReadDeclID(F, Record, i);
1858         FirstLatestDeclIDs[First] = Latest;
1859       }
1860       break;
1861     }
1862 
1863     case LANGUAGE_OPTIONS:
1864       if (ParseLanguageOptions(Record) && !DisableValidation)
1865         return IgnorePCH;
1866       break;
1867 
1868     case IDENTIFIER_TABLE:
1869       F.IdentifierTableData = BlobStart;
1870       if (Record[0]) {
1871         F.IdentifierLookupTable
1872           = ASTIdentifierLookupTable::Create(
1873                        (const unsigned char *)F.IdentifierTableData + Record[0],
1874                        (const unsigned char *)F.IdentifierTableData,
1875                        ASTIdentifierLookupTrait(*this, F));
1876 
1877         PP.getIdentifierTable().setExternalIdentifierLookup(this);
1878       }
1879       break;
1880 
1881     case IDENTIFIER_OFFSET: {
1882       if (F.LocalNumIdentifiers != 0) {
1883         Error("duplicate IDENTIFIER_OFFSET record in AST file");
1884         return Failure;
1885       }
1886       F.IdentifierOffsets = (const uint32_t *)BlobStart;
1887       F.LocalNumIdentifiers = Record[0];
1888       unsigned LocalBaseIdentifierID = Record[1];
1889       F.BaseIdentifierID = getTotalNumIdentifiers();
1890 
1891       if (F.LocalNumIdentifiers > 0) {
1892         // Introduce the global -> local mapping for identifiers within this
1893         // module.
1894         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1895                                                   &F));
1896 
1897         // Introduce the local -> global mapping for identifiers within this
1898         // module.
1899         F.IdentifierRemap.insert(
1900                             std::make_pair(LocalBaseIdentifierID,
1901                               F.BaseIdentifierID - LocalBaseIdentifierID));
1902 
1903         IdentifiersLoaded.resize(IdentifiersLoaded.size()
1904                                  + F.LocalNumIdentifiers);
1905       }
1906       break;
1907     }
1908 
1909     case EXTERNAL_DEFINITIONS:
1910       for (unsigned I = 0, N = Record.size(); I != N; ++I)
1911         ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
1912       break;
1913 
1914     case SPECIAL_TYPES:
1915       for (unsigned I = 0, N = Record.size(); I != N; ++I)
1916         SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
1917       break;
1918 
1919     case STATISTICS:
1920       TotalNumStatements += Record[0];
1921       TotalNumMacros += Record[1];
1922       TotalLexicalDeclContexts += Record[2];
1923       TotalVisibleDeclContexts += Record[3];
1924       break;
1925 
1926     case UNUSED_FILESCOPED_DECLS:
1927       for (unsigned I = 0, N = Record.size(); I != N; ++I)
1928         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
1929       break;
1930 
1931     case DELEGATING_CTORS:
1932       for (unsigned I = 0, N = Record.size(); I != N; ++I)
1933         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
1934       break;
1935 
1936     case WEAK_UNDECLARED_IDENTIFIERS:
1937       if (Record.size() % 4 != 0) {
1938         Error("invalid weak identifiers record");
1939         return Failure;
1940       }
1941 
1942       // FIXME: Ignore weak undeclared identifiers from non-original PCH
1943       // files. This isn't the way to do it :)
1944       WeakUndeclaredIdentifiers.clear();
1945 
1946       // Translate the weak, undeclared identifiers into global IDs.
1947       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1948         WeakUndeclaredIdentifiers.push_back(
1949           getGlobalIdentifierID(F, Record[I++]));
1950         WeakUndeclaredIdentifiers.push_back(
1951           getGlobalIdentifierID(F, Record[I++]));
1952         WeakUndeclaredIdentifiers.push_back(
1953           ReadSourceLocation(F, Record, I).getRawEncoding());
1954         WeakUndeclaredIdentifiers.push_back(Record[I++]);
1955       }
1956       break;
1957 
1958     case LOCALLY_SCOPED_EXTERNAL_DECLS:
1959       for (unsigned I = 0, N = Record.size(); I != N; ++I)
1960         LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
1961       break;
1962 
1963     case SELECTOR_OFFSETS: {
1964       F.SelectorOffsets = (const uint32_t *)BlobStart;
1965       F.LocalNumSelectors = Record[0];
1966       unsigned LocalBaseSelectorID = Record[1];
1967       F.BaseSelectorID = getTotalNumSelectors();
1968 
1969       if (F.LocalNumSelectors > 0) {
1970         // Introduce the global -> local mapping for selectors within this
1971         // module.
1972         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
1973 
1974         // Introduce the local -> global mapping for selectors within this
1975         // module.
1976         F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
1977                                  F.BaseSelectorID - LocalBaseSelectorID));
1978 
1979         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
1980       }
1981       break;
1982     }
1983 
1984     case METHOD_POOL:
1985       F.SelectorLookupTableData = (const unsigned char *)BlobStart;
1986       if (Record[0])
1987         F.SelectorLookupTable
1988           = ASTSelectorLookupTable::Create(
1989                         F.SelectorLookupTableData + Record[0],
1990                         F.SelectorLookupTableData,
1991                         ASTSelectorLookupTrait(*this, F));
1992       TotalNumMethodPoolEntries += Record[1];
1993       break;
1994 
1995     case REFERENCED_SELECTOR_POOL:
1996       if (!Record.empty()) {
1997         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
1998           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
1999                                                                 Record[Idx++]));
2000           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2001                                               getRawEncoding());
2002         }
2003       }
2004       break;
2005 
2006     case PP_COUNTER_VALUE:
2007       if (!Record.empty() && Listener)
2008         Listener->ReadCounter(Record[0]);
2009       break;
2010 
2011     case SOURCE_LOCATION_OFFSETS: {
2012       F.SLocEntryOffsets = (const uint32_t *)BlobStart;
2013       F.LocalNumSLocEntries = Record[0];
2014       llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2015           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2016       // Make our entry in the range map. BaseID is negative and growing, so
2017       // we invert it. Because we invert it, though, we need the other end of
2018       // the range.
2019       unsigned RangeStart =
2020           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2021       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2022       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2023 
2024       // Initialize the remapping table.
2025       // Invalid stays invalid.
2026       F.SLocRemap.insert(std::make_pair(0U, 0));
2027       // This module. Base was 2 when being compiled.
2028       F.SLocRemap.insert(std::make_pair(2U,
2029                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
2030 
2031       TotalNumSLocEntries += F.LocalNumSLocEntries;
2032       break;
2033     }
2034 
2035     case MODULE_OFFSET_MAP: {
2036       // Additional remapping information.
2037       const unsigned char *Data = (const unsigned char*)BlobStart;
2038       const unsigned char *DataEnd = Data + BlobLen;
2039 
2040       // Continuous range maps we may be updating in our module.
2041       ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2042       ContinuousRangeMap<uint32_t, int, 2>::Builder
2043         IdentifierRemap(F.IdentifierRemap);
2044       ContinuousRangeMap<uint32_t, int, 2>::Builder
2045         PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2046       ContinuousRangeMap<uint32_t, int, 2>::Builder
2047         SelectorRemap(F.SelectorRemap);
2048       ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2049       ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2050 
2051       while(Data < DataEnd) {
2052         uint16_t Len = io::ReadUnalignedLE16(Data);
2053         StringRef Name = StringRef((const char*)Data, Len);
2054         Data += Len;
2055         Module *OM = ModuleMgr.lookup(Name);
2056         if (!OM) {
2057           Error("SourceLocation remap refers to unknown module");
2058           return Failure;
2059         }
2060 
2061         uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2062         uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2063         uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2064         uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2065         uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2066         uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2067 
2068         // Source location offset is mapped to OM->SLocEntryBaseOffset.
2069         SLocRemap.insert(std::make_pair(SLocOffset,
2070           static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2071         IdentifierRemap.insert(
2072           std::make_pair(IdentifierIDOffset,
2073                          OM->BaseIdentifierID - IdentifierIDOffset));
2074         PreprocessedEntityRemap.insert(
2075           std::make_pair(PreprocessedEntityIDOffset,
2076             OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2077         SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2078                                OM->BaseSelectorID - SelectorIDOffset));
2079         DeclRemap.insert(std::make_pair(DeclIDOffset,
2080                                         OM->BaseDeclID - DeclIDOffset));
2081 
2082         TypeRemap.insert(std::make_pair(TypeIndexOffset,
2083                                     OM->BaseTypeIndex - TypeIndexOffset));
2084       }
2085       break;
2086     }
2087 
2088     case SOURCE_MANAGER_LINE_TABLE:
2089       if (ParseLineTable(F, Record))
2090         return Failure;
2091       break;
2092 
2093     case FILE_SOURCE_LOCATION_OFFSETS:
2094       F.SLocFileOffsets = (const uint32_t *)BlobStart;
2095       F.LocalNumSLocFileEntries = Record[0];
2096       break;
2097 
2098     case SOURCE_LOCATION_PRELOADS: {
2099       // Need to transform from the local view (1-based IDs) to the global view,
2100       // which is based off F.SLocEntryBaseID.
2101       if (!F.PreloadSLocEntries.empty()) {
2102         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2103         return Failure;
2104       }
2105 
2106       F.PreloadSLocEntries.swap(Record);
2107       break;
2108     }
2109 
2110     case STAT_CACHE: {
2111       if (!DisableStatCache) {
2112         ASTStatCache *MyStatCache =
2113           new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2114                            (const unsigned char *)BlobStart,
2115                            NumStatHits, NumStatMisses);
2116         FileMgr.addStatCache(MyStatCache);
2117         F.StatCache = MyStatCache;
2118       }
2119       break;
2120     }
2121 
2122     case EXT_VECTOR_DECLS:
2123       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2124         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2125       break;
2126 
2127     case VTABLE_USES:
2128       if (Record.size() % 3 != 0) {
2129         Error("Invalid VTABLE_USES record");
2130         return Failure;
2131       }
2132 
2133       // Later tables overwrite earlier ones.
2134       // FIXME: Modules will have some trouble with this. This is clearly not
2135       // the right way to do this.
2136       VTableUses.clear();
2137 
2138       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2139         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2140         VTableUses.push_back(
2141           ReadSourceLocation(F, Record, Idx).getRawEncoding());
2142         VTableUses.push_back(Record[Idx++]);
2143       }
2144       break;
2145 
2146     case DYNAMIC_CLASSES:
2147       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2148         DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2149       break;
2150 
2151     case PENDING_IMPLICIT_INSTANTIATIONS:
2152       if (PendingInstantiations.size() % 2 != 0) {
2153         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2154         return Failure;
2155       }
2156 
2157       // Later lists of pending instantiations overwrite earlier ones.
2158       // FIXME: This is most certainly wrong for modules.
2159       PendingInstantiations.clear();
2160       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2161         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2162         PendingInstantiations.push_back(
2163           ReadSourceLocation(F, Record, I).getRawEncoding());
2164       }
2165       break;
2166 
2167     case SEMA_DECL_REFS:
2168       // Later tables overwrite earlier ones.
2169       // FIXME: Modules will have some trouble with this.
2170       SemaDeclRefs.clear();
2171       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2172         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2173       break;
2174 
2175     case ORIGINAL_FILE_NAME:
2176       // The primary AST will be the last to get here, so it will be the one
2177       // that's used.
2178       ActualOriginalFileName.assign(BlobStart, BlobLen);
2179       OriginalFileName = ActualOriginalFileName;
2180       MaybeAddSystemRootToFilename(OriginalFileName);
2181       break;
2182 
2183     case ORIGINAL_FILE_ID:
2184       OriginalFileID = FileID::get(Record[0]);
2185       break;
2186 
2187     case ORIGINAL_PCH_DIR:
2188       // The primary AST will be the last to get here, so it will be the one
2189       // that's used.
2190       OriginalDir.assign(BlobStart, BlobLen);
2191       break;
2192 
2193     case VERSION_CONTROL_BRANCH_REVISION: {
2194       const std::string &CurBranch = getClangFullRepositoryVersion();
2195       StringRef ASTBranch(BlobStart, BlobLen);
2196       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2197         Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
2198         return IgnorePCH;
2199       }
2200       break;
2201     }
2202 
2203     case PPD_ENTITIES_OFFSETS: {
2204       F.PreprocessedEntityOffsets = (const uint32_t *)BlobStart;
2205       assert(BlobLen % sizeof(uint32_t) == 0);
2206       F.NumPreprocessedEntities = BlobLen / sizeof(uint32_t);
2207 
2208       unsigned LocalBasePreprocessedEntityID = Record[0];
2209 
2210       unsigned StartingID;
2211       if (!PP.getPreprocessingRecord())
2212         PP.createPreprocessingRecord(true);
2213       if (!PP.getPreprocessingRecord()->getExternalSource())
2214         PP.getPreprocessingRecord()->SetExternalSource(*this);
2215       StartingID
2216         = PP.getPreprocessingRecord()
2217             ->allocateLoadedEntities(F.NumPreprocessedEntities);
2218       F.BasePreprocessedEntityID = StartingID;
2219 
2220       if (F.NumPreprocessedEntities > 0) {
2221         // Introduce the global -> local mapping for preprocessed entities in
2222         // this module.
2223         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2224 
2225         // Introduce the local -> global mapping for preprocessed entities in
2226         // this module.
2227         F.PreprocessedEntityRemap.insert(
2228           std::make_pair(LocalBasePreprocessedEntityID,
2229             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2230       }
2231 
2232       break;
2233     }
2234 
2235     case DECL_UPDATE_OFFSETS: {
2236       if (Record.size() % 2 != 0) {
2237         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2238         return Failure;
2239       }
2240       for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2241         DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2242           .push_back(std::make_pair(&F, Record[I+1]));
2243       break;
2244     }
2245 
2246     case DECL_REPLACEMENTS: {
2247       if (Record.size() % 2 != 0) {
2248         Error("invalid DECL_REPLACEMENTS block in AST file");
2249         return Failure;
2250       }
2251       for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2252         ReplacedDecls[getGlobalDeclID(F, Record[I])]
2253           = std::make_pair(&F, Record[I+1]);
2254       break;
2255     }
2256 
2257     case OBJC_CHAINED_CATEGORIES: {
2258       if (Record.size() % 3 != 0) {
2259         Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2260         return Failure;
2261       }
2262       for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2263         serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2264         F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2265                                                          Record[I+2]);
2266         ObjCChainedCategoriesInterfaces.insert(GlobID);
2267       }
2268       break;
2269     }
2270 
2271     case CXX_BASE_SPECIFIER_OFFSETS: {
2272       if (F.LocalNumCXXBaseSpecifiers != 0) {
2273         Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2274         return Failure;
2275       }
2276 
2277       F.LocalNumCXXBaseSpecifiers = Record[0];
2278       F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2279       NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2280       break;
2281     }
2282 
2283     case DIAG_PRAGMA_MAPPINGS:
2284       if (Record.size() % 2 != 0) {
2285         Error("invalid DIAG_USER_MAPPINGS block in AST file");
2286         return Failure;
2287       }
2288 
2289       if (F.PragmaDiagMappings.empty())
2290         F.PragmaDiagMappings.swap(Record);
2291       else
2292         F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2293                                     Record.begin(), Record.end());
2294       break;
2295 
2296     case CUDA_SPECIAL_DECL_REFS:
2297       // Later tables overwrite earlier ones.
2298       // FIXME: Modules will have trouble with this.
2299       CUDASpecialDeclRefs.clear();
2300       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2301         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2302       break;
2303 
2304     case HEADER_SEARCH_TABLE: {
2305       F.HeaderFileInfoTableData = BlobStart;
2306       F.LocalNumHeaderFileInfos = Record[1];
2307       F.HeaderFileFrameworkStrings = BlobStart + Record[2];
2308       if (Record[0]) {
2309         F.HeaderFileInfoTable
2310           = HeaderFileInfoLookupTable::Create(
2311                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2312                    (const unsigned char *)F.HeaderFileInfoTableData,
2313                    HeaderFileInfoTrait(*this, F,
2314                                        &PP.getHeaderSearchInfo(),
2315                                        BlobStart + Record[2]));
2316 
2317         PP.getHeaderSearchInfo().SetExternalSource(this);
2318         if (!PP.getHeaderSearchInfo().getExternalLookup())
2319           PP.getHeaderSearchInfo().SetExternalLookup(this);
2320       }
2321       break;
2322     }
2323 
2324     case FP_PRAGMA_OPTIONS:
2325       // Later tables overwrite earlier ones.
2326       FPPragmaOptions.swap(Record);
2327       break;
2328 
2329     case OPENCL_EXTENSIONS:
2330       // Later tables overwrite earlier ones.
2331       OpenCLExtensions.swap(Record);
2332       break;
2333 
2334     case TENTATIVE_DEFINITIONS:
2335       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2336         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2337       break;
2338 
2339     case KNOWN_NAMESPACES:
2340       for (unsigned I = 0, N = Record.size(); I != N; ++I)
2341         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2342       break;
2343     }
2344   }
2345   Error("premature end of bitstream in AST file");
2346   return Failure;
2347 }
2348 
2349 ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2350   llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
2351 
2352   for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2353     SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2354     unsigned Code = SLocEntryCursor.ReadCode();
2355     if (Code == llvm::bitc::END_BLOCK ||
2356         Code == llvm::bitc::ENTER_SUBBLOCK ||
2357         Code == llvm::bitc::DEFINE_ABBREV) {
2358       Error("incorrectly-formatted source location entry in AST file");
2359       return Failure;
2360     }
2361 
2362     RecordData Record;
2363     const char *BlobStart;
2364     unsigned BlobLen;
2365     switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2366     default:
2367       Error("incorrectly-formatted source location entry in AST file");
2368       return Failure;
2369 
2370     case SM_SLOC_FILE_ENTRY: {
2371       StringRef Filename(BlobStart, BlobLen);
2372       const FileEntry *File = getFileEntry(Filename);
2373 
2374       if (File == 0) {
2375         std::string ErrorStr = "could not find file '";
2376         ErrorStr += Filename;
2377         ErrorStr += "' referenced by AST file";
2378         Error(ErrorStr.c_str());
2379         return IgnorePCH;
2380       }
2381 
2382       if (Record.size() < 6) {
2383         Error("source location entry is incorrect");
2384         return Failure;
2385       }
2386 
2387       // The stat info from the FileEntry came from the cached stat
2388       // info of the PCH, so we cannot trust it.
2389       struct stat StatBuf;
2390       if (::stat(File->getName(), &StatBuf) != 0) {
2391         StatBuf.st_size = File->getSize();
2392         StatBuf.st_mtime = File->getModificationTime();
2393       }
2394 
2395       if (((off_t)Record[4] != StatBuf.st_size
2396 #if !defined(LLVM_ON_WIN32)
2397           // In our regression testing, the Windows file system seems to
2398           // have inconsistent modification times that sometimes
2399           // erroneously trigger this error-handling path.
2400            || (time_t)Record[5] != StatBuf.st_mtime
2401 #endif
2402           )) {
2403         Error(diag::err_fe_pch_file_modified, Filename);
2404         return IgnorePCH;
2405       }
2406 
2407       break;
2408     }
2409     }
2410   }
2411 
2412   return Success;
2413 }
2414 
2415 namespace {
2416   /// \brief Visitor class used to look up identifirs in an AST file.
2417   class IdentifierLookupVisitor {
2418     StringRef Name;
2419     IdentifierInfo *Found;
2420   public:
2421     explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
2422 
2423     static bool visit(Module &M, void *UserData) {
2424       IdentifierLookupVisitor *This
2425       = static_cast<IdentifierLookupVisitor *>(UserData);
2426 
2427       ASTIdentifierLookupTable *IdTable
2428         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2429       if (!IdTable)
2430         return false;
2431 
2432       std::pair<const char*, unsigned> Key(This->Name.begin(),
2433                                            This->Name.size());
2434       ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2435       if (Pos == IdTable->end())
2436         return false;
2437 
2438       // Dereferencing the iterator has the effect of building the
2439       // IdentifierInfo node and populating it with the various
2440       // declarations it needs.
2441       This->Found = *Pos;
2442       return true;
2443     }
2444 
2445     // \brief Retrieve the identifier info found within the module
2446     // files.
2447     IdentifierInfo *getIdentifierInfo() const { return Found; }
2448   };
2449 }
2450 
2451 
2452 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2453                                             ModuleKind Type) {
2454   switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
2455   case Failure: return Failure;
2456   case IgnorePCH: return IgnorePCH;
2457   case Success: break;
2458   }
2459 
2460   // Here comes stuff that we only do once the entire chain is loaded.
2461 
2462   // Check the predefines buffers.
2463   if (!DisableValidation && Type != MK_Module && Type != MK_Preamble &&
2464       // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2465       // if DisableValidation is true, defines that were set on command-line
2466       // but not in the PCH file will not be added to SuggestedPredefines.
2467       CheckPredefinesBuffers())
2468     return IgnorePCH;
2469 
2470   // Initialization of keywords and pragmas occurs before the
2471   // AST file is read, so there may be some identifiers that were
2472   // loaded into the IdentifierTable before we intercepted the
2473   // creation of identifiers. Iterate through the list of known
2474   // identifiers and determine whether we have to establish
2475   // preprocessor definitions or top-level identifier declaration
2476   // chains for those identifiers.
2477   //
2478   // We copy the IdentifierInfo pointers to a small vector first,
2479   // since de-serializing declarations or macro definitions can add
2480   // new entries into the identifier table, invalidating the
2481   // iterators.
2482   //
2483   // FIXME: We need a lazier way to load this information, e.g., by marking
2484   // the identifier data as 'dirty', so that it will be looked up in the
2485   // AST file(s) if it is uttered in the source. This could save us some
2486   // module load time.
2487   SmallVector<IdentifierInfo *, 128> Identifiers;
2488   for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2489                               IdEnd = PP.getIdentifierTable().end();
2490        Id != IdEnd; ++Id)
2491     Identifiers.push_back(Id->second);
2492 
2493   for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2494     IdentifierLookupVisitor Visitor(Identifiers[I]->getName());
2495     ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
2496   }
2497 
2498   InitializeContext();
2499 
2500   if (DeserializationListener)
2501     DeserializationListener->ReaderInitialized(this);
2502 
2503   // If this AST file is a precompiled preamble, then set the main file ID of
2504   // the source manager to the file source file from which the preamble was
2505   // built. This is the only valid way to use a precompiled preamble.
2506   if (Type == MK_Preamble) {
2507     if (OriginalFileID.isInvalid()) {
2508       SourceLocation Loc
2509         = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2510       if (Loc.isValid())
2511         OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
2512     }
2513     else {
2514       OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2515                                         + OriginalFileID.getOpaqueValue() - 1);
2516     }
2517 
2518     if (!OriginalFileID.isInvalid())
2519       SourceMgr.SetPreambleFileID(OriginalFileID);
2520   }
2521 
2522   return Success;
2523 }
2524 
2525 ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
2526                                                 ModuleKind Type,
2527                                                 Module *ImportedBy) {
2528   Module *M;
2529   bool NewModule;
2530   std::string ErrorStr;
2531   llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2532                                                 ErrorStr);
2533 
2534   if (!M) {
2535     // We couldn't load the module.
2536     std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2537       + ErrorStr;
2538     Error(Msg);
2539     return Failure;
2540   }
2541 
2542   if (!NewModule) {
2543     // We've already loaded this module.
2544     return Success;
2545   }
2546 
2547   // FIXME: This seems rather a hack. Should CurrentDir be part of the
2548   // module?
2549   if (FileName != "-") {
2550     CurrentDir = llvm::sys::path::parent_path(FileName);
2551     if (CurrentDir.empty()) CurrentDir = ".";
2552   }
2553 
2554   Module &F = *M;
2555   llvm::BitstreamCursor &Stream = F.Stream;
2556   Stream.init(F.StreamFile);
2557   F.SizeInBits = F.Buffer->getBufferSize() * 8;
2558 
2559   // Sniff for the signature.
2560   if (Stream.Read(8) != 'C' ||
2561       Stream.Read(8) != 'P' ||
2562       Stream.Read(8) != 'C' ||
2563       Stream.Read(8) != 'H') {
2564     Diag(diag::err_not_a_pch_file) << FileName;
2565     return Failure;
2566   }
2567 
2568   while (!Stream.AtEndOfStream()) {
2569     unsigned Code = Stream.ReadCode();
2570 
2571     if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2572       Error("invalid record at top-level of AST file");
2573       return Failure;
2574     }
2575 
2576     unsigned BlockID = Stream.ReadSubBlockID();
2577 
2578     // We only know the AST subblock ID.
2579     switch (BlockID) {
2580     case llvm::bitc::BLOCKINFO_BLOCK_ID:
2581       if (Stream.ReadBlockInfoBlock()) {
2582         Error("malformed BlockInfoBlock in AST file");
2583         return Failure;
2584       }
2585       break;
2586     case AST_BLOCK_ID:
2587       switch (ReadASTBlock(F)) {
2588       case Success:
2589         break;
2590 
2591       case Failure:
2592         return Failure;
2593 
2594       case IgnorePCH:
2595         // FIXME: We could consider reading through to the end of this
2596         // AST block, skipping subblocks, to see if there are other
2597         // AST blocks elsewhere.
2598 
2599         // FIXME: We can't clear loaded slocentries anymore.
2600         //SourceMgr.ClearPreallocatedSLocEntries();
2601 
2602         // Remove the stat cache.
2603         if (F.StatCache)
2604           FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
2605 
2606         return IgnorePCH;
2607       }
2608       break;
2609     default:
2610       if (Stream.SkipBlock()) {
2611         Error("malformed block record in AST file");
2612         return Failure;
2613       }
2614       break;
2615     }
2616   }
2617 
2618   // Once read, set the Module bit base offset and update the size in
2619   // bits of all files we've seen.
2620   F.GlobalBitOffset = TotalModulesSizeInBits;
2621   TotalModulesSizeInBits += F.SizeInBits;
2622   GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2623 
2624   // Make sure that the files this module was built against are still available.
2625   if (!DisableValidation) {
2626     switch(validateFileEntries(*M)) {
2627     case Failure: return Failure;
2628     case IgnorePCH: return IgnorePCH;
2629     case Success: break;
2630     }
2631   }
2632 
2633   // Preload SLocEntries.
2634   for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2635     int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2636     ASTReadResult Result = ReadSLocEntryRecord(Index);
2637     if (Result != Success)
2638       return Failure;
2639   }
2640 
2641 
2642   return Success;
2643 }
2644 
2645 void ASTReader::InitializeContext() {
2646   // If there's a listener, notify them that we "read" the translation unit.
2647   if (DeserializationListener)
2648     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2649                                       Context.getTranslationUnitDecl());
2650 
2651   // Make sure we load the declaration update records for the translation unit,
2652   // if there are any.
2653   loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2654                         Context.getTranslationUnitDecl());
2655 
2656   // FIXME: Find a better way to deal with collisions between these
2657   // built-in types. Right now, we just ignore the problem.
2658 
2659   // Load the special types.
2660   if (SpecialTypes.size() > NumSpecialTypeIDs) {
2661     if (Context.getBuiltinVaListType().isNull()) {
2662       Context.setBuiltinVaListType(
2663         GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2664     }
2665 
2666     if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2667       if (Context.ObjCProtoType.isNull())
2668         Context.ObjCProtoType = GetType(Proto);
2669     }
2670 
2671     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2672       if (!Context.CFConstantStringTypeDecl)
2673         Context.setCFConstantStringType(GetType(String));
2674     }
2675 
2676     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2677       QualType FileType = GetType(File);
2678       if (FileType.isNull()) {
2679         Error("FILE type is NULL");
2680         return;
2681       }
2682 
2683       if (!Context.FILEDecl) {
2684         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2685           Context.setFILEDecl(Typedef->getDecl());
2686         else {
2687           const TagType *Tag = FileType->getAs<TagType>();
2688           if (!Tag) {
2689             Error("Invalid FILE type in AST file");
2690             return;
2691           }
2692           Context.setFILEDecl(Tag->getDecl());
2693         }
2694       }
2695     }
2696 
2697     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2698       QualType Jmp_bufType = GetType(Jmp_buf);
2699       if (Jmp_bufType.isNull()) {
2700         Error("jmp_buf type is NULL");
2701         return;
2702       }
2703 
2704       if (!Context.jmp_bufDecl) {
2705         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2706           Context.setjmp_bufDecl(Typedef->getDecl());
2707         else {
2708           const TagType *Tag = Jmp_bufType->getAs<TagType>();
2709           if (!Tag) {
2710             Error("Invalid jmp_buf type in AST file");
2711             return;
2712           }
2713           Context.setjmp_bufDecl(Tag->getDecl());
2714         }
2715       }
2716     }
2717 
2718     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2719       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2720       if (Sigjmp_bufType.isNull()) {
2721         Error("sigjmp_buf type is NULL");
2722         return;
2723       }
2724 
2725       if (!Context.sigjmp_bufDecl) {
2726         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2727           Context.setsigjmp_bufDecl(Typedef->getDecl());
2728         else {
2729           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2730           assert(Tag && "Invalid sigjmp_buf type in AST file");
2731           Context.setsigjmp_bufDecl(Tag->getDecl());
2732         }
2733       }
2734     }
2735 
2736     if (unsigned ObjCIdRedef
2737           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2738       if (Context.ObjCIdRedefinitionType.isNull())
2739         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2740     }
2741 
2742     if (unsigned ObjCClassRedef
2743           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2744       if (Context.ObjCClassRedefinitionType.isNull())
2745         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2746     }
2747 
2748     if (unsigned ObjCSelRedef
2749           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2750       if (Context.ObjCSelRedefinitionType.isNull())
2751         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2752     }
2753   }
2754 
2755   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
2756 
2757   // If there were any CUDA special declarations, deserialize them.
2758   if (!CUDASpecialDeclRefs.empty()) {
2759     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2760     Context.setcudaConfigureCallDecl(
2761                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2762   }
2763 }
2764 
2765 /// \brief Retrieve the name of the original source file name
2766 /// directly from the AST file, without actually loading the AST
2767 /// file.
2768 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2769                                              FileManager &FileMgr,
2770                                              Diagnostic &Diags) {
2771   // Open the AST file.
2772   std::string ErrStr;
2773   llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2774   Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
2775   if (!Buffer) {
2776     Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2777     return std::string();
2778   }
2779 
2780   // Initialize the stream
2781   llvm::BitstreamReader StreamFile;
2782   llvm::BitstreamCursor Stream;
2783   StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2784                   (const unsigned char *)Buffer->getBufferEnd());
2785   Stream.init(StreamFile);
2786 
2787   // Sniff for the signature.
2788   if (Stream.Read(8) != 'C' ||
2789       Stream.Read(8) != 'P' ||
2790       Stream.Read(8) != 'C' ||
2791       Stream.Read(8) != 'H') {
2792     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2793     return std::string();
2794   }
2795 
2796   RecordData Record;
2797   while (!Stream.AtEndOfStream()) {
2798     unsigned Code = Stream.ReadCode();
2799 
2800     if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2801       unsigned BlockID = Stream.ReadSubBlockID();
2802 
2803       // We only know the AST subblock ID.
2804       switch (BlockID) {
2805       case AST_BLOCK_ID:
2806         if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2807           Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2808           return std::string();
2809         }
2810         break;
2811 
2812       default:
2813         if (Stream.SkipBlock()) {
2814           Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2815           return std::string();
2816         }
2817         break;
2818       }
2819       continue;
2820     }
2821 
2822     if (Code == llvm::bitc::END_BLOCK) {
2823       if (Stream.ReadBlockEnd()) {
2824         Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2825         return std::string();
2826       }
2827       continue;
2828     }
2829 
2830     if (Code == llvm::bitc::DEFINE_ABBREV) {
2831       Stream.ReadAbbrevRecord();
2832       continue;
2833     }
2834 
2835     Record.clear();
2836     const char *BlobStart = 0;
2837     unsigned BlobLen = 0;
2838     if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2839           == ORIGINAL_FILE_NAME)
2840       return std::string(BlobStart, BlobLen);
2841   }
2842 
2843   return std::string();
2844 }
2845 
2846 /// \brief Parse the record that corresponds to a LangOptions data
2847 /// structure.
2848 ///
2849 /// This routine parses the language options from the AST file and then gives
2850 /// them to the AST listener if one is set.
2851 ///
2852 /// \returns true if the listener deems the file unacceptable, false otherwise.
2853 bool ASTReader::ParseLanguageOptions(
2854                              const SmallVectorImpl<uint64_t> &Record) {
2855   if (Listener) {
2856     LangOptions LangOpts;
2857     unsigned Idx = 0;
2858 #define LANGOPT(Name, Bits, Default, Description) \
2859   LangOpts.Name = Record[Idx++];
2860 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
2861   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
2862 #include "clang/Basic/LangOptions.def"
2863 
2864     return Listener->ReadLanguageOptions(LangOpts);
2865   }
2866 
2867   return false;
2868 }
2869 
2870 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
2871   GlobalPreprocessedEntityMapType::iterator
2872     I = GlobalPreprocessedEntityMap.find(Index);
2873   assert(I != GlobalPreprocessedEntityMap.end() &&
2874          "Corrupted global preprocessed entity map");
2875   Module &M = *I->second;
2876   unsigned LocalIndex = Index - M.BasePreprocessedEntityID;
2877 
2878   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
2879   M.PreprocessorDetailCursor.JumpToBit(M.PreprocessedEntityOffsets[LocalIndex]);
2880   return LoadPreprocessedEntity(M);
2881 }
2882 
2883 PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
2884   RecordLocation Loc = getLocalBitOffset(Offset);
2885 
2886   // Keep track of where we are in the stream, then jump back there
2887   // after reading this entity.
2888   SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
2889   Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
2890   return LoadPreprocessedEntity(*Loc.F);
2891 }
2892 
2893 namespace {
2894   /// \brief Visitor used to search for information about a header file.
2895   class HeaderFileInfoVisitor {
2896     ASTReader &Reader;
2897     const FileEntry *FE;
2898 
2899     llvm::Optional<HeaderFileInfo> HFI;
2900 
2901   public:
2902     HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
2903       : Reader(Reader), FE(FE) { }
2904 
2905     static bool visit(Module &M, void *UserData) {
2906       HeaderFileInfoVisitor *This
2907         = static_cast<HeaderFileInfoVisitor *>(UserData);
2908 
2909       HeaderFileInfoTrait Trait(This->Reader, M,
2910                                 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
2911                                 M.HeaderFileFrameworkStrings,
2912                                 This->FE->getName());
2913 
2914       HeaderFileInfoLookupTable *Table
2915         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
2916       if (!Table)
2917         return false;
2918 
2919       // Look in the on-disk hash table for an entry for this file name.
2920       HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
2921                                                             &Trait);
2922       if (Pos == Table->end())
2923         return false;
2924 
2925       This->HFI = *Pos;
2926       return true;
2927     }
2928 
2929     llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
2930   };
2931 }
2932 
2933 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2934   HeaderFileInfoVisitor Visitor(*this, FE);
2935   ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
2936   if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
2937     if (Listener)
2938       Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
2939     return *HFI;
2940   }
2941 
2942   return HeaderFileInfo();
2943 }
2944 
2945 void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
2946   for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
2947     Module &F = *(*I);
2948     unsigned Idx = 0;
2949     while (Idx < F.PragmaDiagMappings.size()) {
2950       SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
2951       while (1) {
2952         assert(Idx < F.PragmaDiagMappings.size() &&
2953                "Invalid data, didn't find '-1' marking end of diag/map pairs");
2954         if (Idx >= F.PragmaDiagMappings.size()) {
2955           break; // Something is messed up but at least avoid infinite loop in
2956                  // release build.
2957         }
2958         unsigned DiagID = F.PragmaDiagMappings[Idx++];
2959         if (DiagID == (unsigned)-1) {
2960           break; // no more diag/map pairs for this location.
2961         }
2962         diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
2963         Diag.setDiagnosticMapping(DiagID, Map, Loc);
2964       }
2965     }
2966   }
2967 }
2968 
2969 /// \brief Get the correct cursor and offset for loading a type.
2970 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
2971   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
2972   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
2973   Module *M = I->second;
2974   return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
2975 }
2976 
2977 /// \brief Read and return the type with the given index..
2978 ///
2979 /// The index is the type ID, shifted and minus the number of predefs. This
2980 /// routine actually reads the record corresponding to the type at the given
2981 /// location. It is a helper routine for GetType, which deals with reading type
2982 /// IDs.
2983 QualType ASTReader::readTypeRecord(unsigned Index) {
2984   RecordLocation Loc = TypeCursorForIndex(Index);
2985   llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
2986 
2987   // Keep track of where we are in the stream, then jump back there
2988   // after reading this type.
2989   SavedStreamPosition SavedPosition(DeclsCursor);
2990 
2991   ReadingKindTracker ReadingKind(Read_Type, *this);
2992 
2993   // Note that we are loading a type record.
2994   Deserializing AType(this);
2995 
2996   unsigned Idx = 0;
2997   DeclsCursor.JumpToBit(Loc.Offset);
2998   RecordData Record;
2999   unsigned Code = DeclsCursor.ReadCode();
3000   switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3001   case TYPE_EXT_QUAL: {
3002     if (Record.size() != 2) {
3003       Error("Incorrect encoding of extended qualifier type");
3004       return QualType();
3005     }
3006     QualType Base = readType(*Loc.F, Record, Idx);
3007     Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
3008     return Context.getQualifiedType(Base, Quals);
3009   }
3010 
3011   case TYPE_COMPLEX: {
3012     if (Record.size() != 1) {
3013       Error("Incorrect encoding of complex type");
3014       return QualType();
3015     }
3016     QualType ElemType = readType(*Loc.F, Record, Idx);
3017     return Context.getComplexType(ElemType);
3018   }
3019 
3020   case TYPE_POINTER: {
3021     if (Record.size() != 1) {
3022       Error("Incorrect encoding of pointer type");
3023       return QualType();
3024     }
3025     QualType PointeeType = readType(*Loc.F, Record, Idx);
3026     return Context.getPointerType(PointeeType);
3027   }
3028 
3029   case TYPE_BLOCK_POINTER: {
3030     if (Record.size() != 1) {
3031       Error("Incorrect encoding of block pointer type");
3032       return QualType();
3033     }
3034     QualType PointeeType = readType(*Loc.F, Record, Idx);
3035     return Context.getBlockPointerType(PointeeType);
3036   }
3037 
3038   case TYPE_LVALUE_REFERENCE: {
3039     if (Record.size() != 2) {
3040       Error("Incorrect encoding of lvalue reference type");
3041       return QualType();
3042     }
3043     QualType PointeeType = readType(*Loc.F, Record, Idx);
3044     return Context.getLValueReferenceType(PointeeType, Record[1]);
3045   }
3046 
3047   case TYPE_RVALUE_REFERENCE: {
3048     if (Record.size() != 1) {
3049       Error("Incorrect encoding of rvalue reference type");
3050       return QualType();
3051     }
3052     QualType PointeeType = readType(*Loc.F, Record, Idx);
3053     return Context.getRValueReferenceType(PointeeType);
3054   }
3055 
3056   case TYPE_MEMBER_POINTER: {
3057     if (Record.size() != 2) {
3058       Error("Incorrect encoding of member pointer type");
3059       return QualType();
3060     }
3061     QualType PointeeType = readType(*Loc.F, Record, Idx);
3062     QualType ClassType = readType(*Loc.F, Record, Idx);
3063     if (PointeeType.isNull() || ClassType.isNull())
3064       return QualType();
3065 
3066     return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
3067   }
3068 
3069   case TYPE_CONSTANT_ARRAY: {
3070     QualType ElementType = readType(*Loc.F, Record, Idx);
3071     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3072     unsigned IndexTypeQuals = Record[2];
3073     unsigned Idx = 3;
3074     llvm::APInt Size = ReadAPInt(Record, Idx);
3075     return Context.getConstantArrayType(ElementType, Size,
3076                                          ASM, IndexTypeQuals);
3077   }
3078 
3079   case TYPE_INCOMPLETE_ARRAY: {
3080     QualType ElementType = readType(*Loc.F, Record, Idx);
3081     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3082     unsigned IndexTypeQuals = Record[2];
3083     return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
3084   }
3085 
3086   case TYPE_VARIABLE_ARRAY: {
3087     QualType ElementType = readType(*Loc.F, Record, Idx);
3088     ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3089     unsigned IndexTypeQuals = Record[2];
3090     SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3091     SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3092     return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
3093                                          ASM, IndexTypeQuals,
3094                                          SourceRange(LBLoc, RBLoc));
3095   }
3096 
3097   case TYPE_VECTOR: {
3098     if (Record.size() != 3) {
3099       Error("incorrect encoding of vector type in AST file");
3100       return QualType();
3101     }
3102 
3103     QualType ElementType = readType(*Loc.F, Record, Idx);
3104     unsigned NumElements = Record[1];
3105     unsigned VecKind = Record[2];
3106     return Context.getVectorType(ElementType, NumElements,
3107                                   (VectorType::VectorKind)VecKind);
3108   }
3109 
3110   case TYPE_EXT_VECTOR: {
3111     if (Record.size() != 3) {
3112       Error("incorrect encoding of extended vector type in AST file");
3113       return QualType();
3114     }
3115 
3116     QualType ElementType = readType(*Loc.F, Record, Idx);
3117     unsigned NumElements = Record[1];
3118     return Context.getExtVectorType(ElementType, NumElements);
3119   }
3120 
3121   case TYPE_FUNCTION_NO_PROTO: {
3122     if (Record.size() != 6) {
3123       Error("incorrect encoding of no-proto function type");
3124       return QualType();
3125     }
3126     QualType ResultType = readType(*Loc.F, Record, Idx);
3127     FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3128                                (CallingConv)Record[4], Record[5]);
3129     return Context.getFunctionNoProtoType(ResultType, Info);
3130   }
3131 
3132   case TYPE_FUNCTION_PROTO: {
3133     QualType ResultType = readType(*Loc.F, Record, Idx);
3134 
3135     FunctionProtoType::ExtProtoInfo EPI;
3136     EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3137                                         /*hasregparm*/ Record[2],
3138                                         /*regparm*/ Record[3],
3139                                         static_cast<CallingConv>(Record[4]),
3140                                         /*produces*/ Record[5]);
3141 
3142     unsigned Idx = 6;
3143     unsigned NumParams = Record[Idx++];
3144     SmallVector<QualType, 16> ParamTypes;
3145     for (unsigned I = 0; I != NumParams; ++I)
3146       ParamTypes.push_back(readType(*Loc.F, Record, Idx));
3147 
3148     EPI.Variadic = Record[Idx++];
3149     EPI.TypeQuals = Record[Idx++];
3150     EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
3151     ExceptionSpecificationType EST =
3152         static_cast<ExceptionSpecificationType>(Record[Idx++]);
3153     EPI.ExceptionSpecType = EST;
3154     if (EST == EST_Dynamic) {
3155       EPI.NumExceptions = Record[Idx++];
3156       SmallVector<QualType, 2> Exceptions;
3157       for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3158         Exceptions.push_back(readType(*Loc.F, Record, Idx));
3159       EPI.Exceptions = Exceptions.data();
3160     } else if (EST == EST_ComputedNoexcept) {
3161       EPI.NoexceptExpr = ReadExpr(*Loc.F);
3162     }
3163     return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
3164                                     EPI);
3165   }
3166 
3167   case TYPE_UNRESOLVED_USING: {
3168     unsigned Idx = 0;
3169     return Context.getTypeDeclType(
3170                   ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3171   }
3172 
3173   case TYPE_TYPEDEF: {
3174     if (Record.size() != 2) {
3175       Error("incorrect encoding of typedef type");
3176       return QualType();
3177     }
3178     unsigned Idx = 0;
3179     TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
3180     QualType Canonical = readType(*Loc.F, Record, Idx);
3181     if (!Canonical.isNull())
3182       Canonical = Context.getCanonicalType(Canonical);
3183     return Context.getTypedefType(Decl, Canonical);
3184   }
3185 
3186   case TYPE_TYPEOF_EXPR:
3187     return Context.getTypeOfExprType(ReadExpr(*Loc.F));
3188 
3189   case TYPE_TYPEOF: {
3190     if (Record.size() != 1) {
3191       Error("incorrect encoding of typeof(type) in AST file");
3192       return QualType();
3193     }
3194     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3195     return Context.getTypeOfType(UnderlyingType);
3196   }
3197 
3198   case TYPE_DECLTYPE:
3199     return Context.getDecltypeType(ReadExpr(*Loc.F));
3200 
3201   case TYPE_UNARY_TRANSFORM: {
3202     QualType BaseType = readType(*Loc.F, Record, Idx);
3203     QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3204     UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3205     return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
3206   }
3207 
3208   case TYPE_AUTO:
3209     return Context.getAutoType(readType(*Loc.F, Record, Idx));
3210 
3211   case TYPE_RECORD: {
3212     if (Record.size() != 2) {
3213       Error("incorrect encoding of record type");
3214       return QualType();
3215     }
3216     unsigned Idx = 0;
3217     bool IsDependent = Record[Idx++];
3218     QualType T
3219       = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
3220     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3221     return T;
3222   }
3223 
3224   case TYPE_ENUM: {
3225     if (Record.size() != 2) {
3226       Error("incorrect encoding of enum type");
3227       return QualType();
3228     }
3229     unsigned Idx = 0;
3230     bool IsDependent = Record[Idx++];
3231     QualType T
3232       = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
3233     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3234     return T;
3235   }
3236 
3237   case TYPE_ATTRIBUTED: {
3238     if (Record.size() != 3) {
3239       Error("incorrect encoding of attributed type");
3240       return QualType();
3241     }
3242     QualType modifiedType = readType(*Loc.F, Record, Idx);
3243     QualType equivalentType = readType(*Loc.F, Record, Idx);
3244     AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3245     return Context.getAttributedType(kind, modifiedType, equivalentType);
3246   }
3247 
3248   case TYPE_PAREN: {
3249     if (Record.size() != 1) {
3250       Error("incorrect encoding of paren type");
3251       return QualType();
3252     }
3253     QualType InnerType = readType(*Loc.F, Record, Idx);
3254     return Context.getParenType(InnerType);
3255   }
3256 
3257   case TYPE_PACK_EXPANSION: {
3258     if (Record.size() != 2) {
3259       Error("incorrect encoding of pack expansion type");
3260       return QualType();
3261     }
3262     QualType Pattern = readType(*Loc.F, Record, Idx);
3263     if (Pattern.isNull())
3264       return QualType();
3265     llvm::Optional<unsigned> NumExpansions;
3266     if (Record[1])
3267       NumExpansions = Record[1] - 1;
3268     return Context.getPackExpansionType(Pattern, NumExpansions);
3269   }
3270 
3271   case TYPE_ELABORATED: {
3272     unsigned Idx = 0;
3273     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3274     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3275     QualType NamedType = readType(*Loc.F, Record, Idx);
3276     return Context.getElaboratedType(Keyword, NNS, NamedType);
3277   }
3278 
3279   case TYPE_OBJC_INTERFACE: {
3280     unsigned Idx = 0;
3281     ObjCInterfaceDecl *ItfD
3282       = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
3283     return Context.getObjCInterfaceType(ItfD);
3284   }
3285 
3286   case TYPE_OBJC_OBJECT: {
3287     unsigned Idx = 0;
3288     QualType Base = readType(*Loc.F, Record, Idx);
3289     unsigned NumProtos = Record[Idx++];
3290     SmallVector<ObjCProtocolDecl*, 4> Protos;
3291     for (unsigned I = 0; I != NumProtos; ++I)
3292       Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
3293     return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
3294   }
3295 
3296   case TYPE_OBJC_OBJECT_POINTER: {
3297     unsigned Idx = 0;
3298     QualType Pointee = readType(*Loc.F, Record, Idx);
3299     return Context.getObjCObjectPointerType(Pointee);
3300   }
3301 
3302   case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
3303     unsigned Idx = 0;
3304     QualType Parm = readType(*Loc.F, Record, Idx);
3305     QualType Replacement = readType(*Loc.F, Record, Idx);
3306     return
3307       Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3308                                             Replacement);
3309   }
3310 
3311   case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3312     unsigned Idx = 0;
3313     QualType Parm = readType(*Loc.F, Record, Idx);
3314     TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3315     return Context.getSubstTemplateTypeParmPackType(
3316                                                cast<TemplateTypeParmType>(Parm),
3317                                                      ArgPack);
3318   }
3319 
3320   case TYPE_INJECTED_CLASS_NAME: {
3321     CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
3322     QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
3323     // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
3324     // for AST reading, too much interdependencies.
3325     return
3326       QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
3327   }
3328 
3329   case TYPE_TEMPLATE_TYPE_PARM: {
3330     unsigned Idx = 0;
3331     unsigned Depth = Record[Idx++];
3332     unsigned Index = Record[Idx++];
3333     bool Pack = Record[Idx++];
3334     TemplateTypeParmDecl *D
3335       = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
3336     return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
3337   }
3338 
3339   case TYPE_DEPENDENT_NAME: {
3340     unsigned Idx = 0;
3341     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3342     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3343     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
3344     QualType Canon = readType(*Loc.F, Record, Idx);
3345     if (!Canon.isNull())
3346       Canon = Context.getCanonicalType(Canon);
3347     return Context.getDependentNameType(Keyword, NNS, Name, Canon);
3348   }
3349 
3350   case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
3351     unsigned Idx = 0;
3352     ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3353     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3354     const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
3355     unsigned NumArgs = Record[Idx++];
3356     SmallVector<TemplateArgument, 8> Args;
3357     Args.reserve(NumArgs);
3358     while (NumArgs--)
3359       Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
3360     return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
3361                                                       Args.size(), Args.data());
3362   }
3363 
3364   case TYPE_DEPENDENT_SIZED_ARRAY: {
3365     unsigned Idx = 0;
3366 
3367     // ArrayType
3368     QualType ElementType = readType(*Loc.F, Record, Idx);
3369     ArrayType::ArraySizeModifier ASM
3370       = (ArrayType::ArraySizeModifier)Record[Idx++];
3371     unsigned IndexTypeQuals = Record[Idx++];
3372 
3373     // DependentSizedArrayType
3374     Expr *NumElts = ReadExpr(*Loc.F);
3375     SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
3376 
3377     return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
3378                                                IndexTypeQuals, Brackets);
3379   }
3380 
3381   case TYPE_TEMPLATE_SPECIALIZATION: {
3382     unsigned Idx = 0;
3383     bool IsDependent = Record[Idx++];
3384     TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
3385     SmallVector<TemplateArgument, 8> Args;
3386     ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
3387     QualType Underlying = readType(*Loc.F, Record, Idx);
3388     QualType T;
3389     if (Underlying.isNull())
3390       T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
3391                                                           Args.size());
3392     else
3393       T = Context.getTemplateSpecializationType(Name, Args.data(),
3394                                                  Args.size(), Underlying);
3395     const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3396     return T;
3397   }
3398   }
3399   // Suppress a GCC warning
3400   return QualType();
3401 }
3402 
3403 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
3404   ASTReader &Reader;
3405   Module &F;
3406   llvm::BitstreamCursor &DeclsCursor;
3407   const ASTReader::RecordData &Record;
3408   unsigned &Idx;
3409 
3410   SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3411                                     unsigned &I) {
3412     return Reader.ReadSourceLocation(F, R, I);
3413   }
3414 
3415   template<typename T>
3416   T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3417     return Reader.ReadDeclAs<T>(F, Record, Idx);
3418   }
3419 
3420 public:
3421   TypeLocReader(ASTReader &Reader, Module &F,
3422                 const ASTReader::RecordData &Record, unsigned &Idx)
3423     : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3424   { }
3425 
3426   // We want compile-time assurance that we've enumerated all of
3427   // these, so unfortunately we have to declare them first, then
3428   // define them out-of-line.
3429 #define ABSTRACT_TYPELOC(CLASS, PARENT)
3430 #define TYPELOC(CLASS, PARENT) \
3431   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
3432 #include "clang/AST/TypeLocNodes.def"
3433 
3434   void VisitFunctionTypeLoc(FunctionTypeLoc);
3435   void VisitArrayTypeLoc(ArrayTypeLoc);
3436 };
3437 
3438 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3439   // nothing to do
3440 }
3441 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3442   TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
3443   if (TL.needsExtraLocalData()) {
3444     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3445     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3446     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3447     TL.setModeAttr(Record[Idx++]);
3448   }
3449 }
3450 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
3451   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3452 }
3453 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
3454   TL.setStarLoc(ReadSourceLocation(Record, Idx));
3455 }
3456 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3457   TL.setCaretLoc(ReadSourceLocation(Record, Idx));
3458 }
3459 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3460   TL.setAmpLoc(ReadSourceLocation(Record, Idx));
3461 }
3462 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3463   TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
3464 }
3465 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3466   TL.setStarLoc(ReadSourceLocation(Record, Idx));
3467   TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3468 }
3469 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
3470   TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3471   TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
3472   if (Record[Idx++])
3473     TL.setSizeExpr(Reader.ReadExpr(F));
3474   else
3475     TL.setSizeExpr(0);
3476 }
3477 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3478   VisitArrayTypeLoc(TL);
3479 }
3480 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3481   VisitArrayTypeLoc(TL);
3482 }
3483 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3484   VisitArrayTypeLoc(TL);
3485 }
3486 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3487                                             DependentSizedArrayTypeLoc TL) {
3488   VisitArrayTypeLoc(TL);
3489 }
3490 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3491                                         DependentSizedExtVectorTypeLoc TL) {
3492   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3493 }
3494 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
3495   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3496 }
3497 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
3498   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3499 }
3500 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3501   TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3502   TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
3503   TL.setTrailingReturn(Record[Idx++]);
3504   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3505     TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
3506   }
3507 }
3508 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3509   VisitFunctionTypeLoc(TL);
3510 }
3511 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3512   VisitFunctionTypeLoc(TL);
3513 }
3514 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
3515   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3516 }
3517 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3518   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3519 }
3520 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3521   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3522   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3523   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3524 }
3525 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3526   TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3527   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3528   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3529   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3530 }
3531 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
3532   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3533 }
3534 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3535   TL.setKWLoc(ReadSourceLocation(Record, Idx));
3536   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3537   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3538   TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3539 }
3540 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3541   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3542 }
3543 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
3544   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3545 }
3546 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
3547   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3548 }
3549 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3550   TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3551   if (TL.hasAttrOperand()) {
3552     SourceRange range;
3553     range.setBegin(ReadSourceLocation(Record, Idx));
3554     range.setEnd(ReadSourceLocation(Record, Idx));
3555     TL.setAttrOperandParensRange(range);
3556   }
3557   if (TL.hasAttrExprOperand()) {
3558     if (Record[Idx++])
3559       TL.setAttrExprOperand(Reader.ReadExpr(F));
3560     else
3561       TL.setAttrExprOperand(0);
3562   } else if (TL.hasAttrEnumOperand())
3563     TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3564 }
3565 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
3566   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3567 }
3568 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3569                                             SubstTemplateTypeParmTypeLoc TL) {
3570   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3571 }
3572 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3573                                           SubstTemplateTypeParmPackTypeLoc TL) {
3574   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3575 }
3576 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3577                                            TemplateSpecializationTypeLoc TL) {
3578   TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3579   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3580   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3581   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3582     TL.setArgLocInfo(i,
3583         Reader.GetTemplateArgumentLocInfo(F,
3584                                           TL.getTypePtr()->getArg(i).getKind(),
3585                                           Record, Idx));
3586 }
3587 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3588   TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3589   TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3590 }
3591 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3592   TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3593   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3594 }
3595 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3596   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3597 }
3598 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
3599   TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3600   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3601   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3602 }
3603 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3604        DependentTemplateSpecializationTypeLoc TL) {
3605   TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3606   TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3607   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3608   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3609   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3610   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3611     TL.setArgLocInfo(I,
3612         Reader.GetTemplateArgumentLocInfo(F,
3613                                           TL.getTypePtr()->getArg(I).getKind(),
3614                                           Record, Idx));
3615 }
3616 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3617   TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3618 }
3619 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3620   TL.setNameLoc(ReadSourceLocation(Record, Idx));
3621 }
3622 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3623   TL.setHasBaseTypeAsWritten(Record[Idx++]);
3624   TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3625   TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3626   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3627     TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
3628 }
3629 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3630   TL.setStarLoc(ReadSourceLocation(Record, Idx));
3631 }
3632 
3633 TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
3634                                              const RecordData &Record,
3635                                              unsigned &Idx) {
3636   QualType InfoTy = readType(F, Record, Idx);
3637   if (InfoTy.isNull())
3638     return 0;
3639 
3640   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
3641   TypeLocReader TLR(*this, F, Record, Idx);
3642   for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
3643     TLR.Visit(TL);
3644   return TInfo;
3645 }
3646 
3647 QualType ASTReader::GetType(TypeID ID) {
3648   unsigned FastQuals = ID & Qualifiers::FastMask;
3649   unsigned Index = ID >> Qualifiers::FastWidth;
3650 
3651   if (Index < NUM_PREDEF_TYPE_IDS) {
3652     QualType T;
3653     switch ((PredefinedTypeIDs)Index) {
3654     case PREDEF_TYPE_NULL_ID: return QualType();
3655     case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
3656     case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
3657 
3658     case PREDEF_TYPE_CHAR_U_ID:
3659     case PREDEF_TYPE_CHAR_S_ID:
3660       // FIXME: Check that the signedness of CharTy is correct!
3661       T = Context.CharTy;
3662       break;
3663 
3664     case PREDEF_TYPE_UCHAR_ID:      T = Context.UnsignedCharTy;     break;
3665     case PREDEF_TYPE_USHORT_ID:     T = Context.UnsignedShortTy;    break;
3666     case PREDEF_TYPE_UINT_ID:       T = Context.UnsignedIntTy;      break;
3667     case PREDEF_TYPE_ULONG_ID:      T = Context.UnsignedLongTy;     break;
3668     case PREDEF_TYPE_ULONGLONG_ID:  T = Context.UnsignedLongLongTy; break;
3669     case PREDEF_TYPE_UINT128_ID:    T = Context.UnsignedInt128Ty;   break;
3670     case PREDEF_TYPE_SCHAR_ID:      T = Context.SignedCharTy;       break;
3671     case PREDEF_TYPE_WCHAR_ID:      T = Context.WCharTy;            break;
3672     case PREDEF_TYPE_SHORT_ID:      T = Context.ShortTy;            break;
3673     case PREDEF_TYPE_INT_ID:        T = Context.IntTy;              break;
3674     case PREDEF_TYPE_LONG_ID:       T = Context.LongTy;             break;
3675     case PREDEF_TYPE_LONGLONG_ID:   T = Context.LongLongTy;         break;
3676     case PREDEF_TYPE_INT128_ID:     T = Context.Int128Ty;           break;
3677     case PREDEF_TYPE_FLOAT_ID:      T = Context.FloatTy;            break;
3678     case PREDEF_TYPE_DOUBLE_ID:     T = Context.DoubleTy;           break;
3679     case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy;       break;
3680     case PREDEF_TYPE_OVERLOAD_ID:   T = Context.OverloadTy;         break;
3681     case PREDEF_TYPE_BOUND_MEMBER:  T = Context.BoundMemberTy;      break;
3682     case PREDEF_TYPE_DEPENDENT_ID:  T = Context.DependentTy;        break;
3683     case PREDEF_TYPE_UNKNOWN_ANY:   T = Context.UnknownAnyTy;       break;
3684     case PREDEF_TYPE_NULLPTR_ID:    T = Context.NullPtrTy;          break;
3685     case PREDEF_TYPE_CHAR16_ID:     T = Context.Char16Ty;           break;
3686     case PREDEF_TYPE_CHAR32_ID:     T = Context.Char32Ty;           break;
3687     case PREDEF_TYPE_OBJC_ID:       T = Context.ObjCBuiltinIdTy;    break;
3688     case PREDEF_TYPE_OBJC_CLASS:    T = Context.ObjCBuiltinClassTy; break;
3689     case PREDEF_TYPE_OBJC_SEL:      T = Context.ObjCBuiltinSelTy;   break;
3690     case PREDEF_TYPE_AUTO_DEDUCT:   T = Context.getAutoDeductType(); break;
3691 
3692     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
3693       T = Context.getAutoRRefDeductType();
3694       break;
3695     }
3696 
3697     assert(!T.isNull() && "Unknown predefined type");
3698     return T.withFastQualifiers(FastQuals);
3699   }
3700 
3701   Index -= NUM_PREDEF_TYPE_IDS;
3702   assert(Index < TypesLoaded.size() && "Type index out-of-range");
3703   if (TypesLoaded[Index].isNull()) {
3704     TypesLoaded[Index] = readTypeRecord(Index);
3705     if (TypesLoaded[Index].isNull())
3706       return QualType();
3707 
3708     TypesLoaded[Index]->setFromAST();
3709     if (DeserializationListener)
3710       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
3711                                         TypesLoaded[Index]);
3712   }
3713 
3714   return TypesLoaded[Index].withFastQualifiers(FastQuals);
3715 }
3716 
3717 QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
3718   return GetType(getGlobalTypeID(F, LocalID));
3719 }
3720 
3721 serialization::TypeID
3722 ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
3723   unsigned FastQuals = LocalID & Qualifiers::FastMask;
3724   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3725 
3726   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3727     return LocalID;
3728 
3729   ContinuousRangeMap<uint32_t, int, 2>::iterator I
3730     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3731   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3732 
3733   unsigned GlobalIndex = LocalIndex + I->second;
3734   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3735 }
3736 
3737 TemplateArgumentLocInfo
3738 ASTReader::GetTemplateArgumentLocInfo(Module &F,
3739                                       TemplateArgument::ArgKind Kind,
3740                                       const RecordData &Record,
3741                                       unsigned &Index) {
3742   switch (Kind) {
3743   case TemplateArgument::Expression:
3744     return ReadExpr(F);
3745   case TemplateArgument::Type:
3746     return GetTypeSourceInfo(F, Record, Index);
3747   case TemplateArgument::Template: {
3748     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3749                                                                      Index);
3750     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3751     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3752                                    SourceLocation());
3753   }
3754   case TemplateArgument::TemplateExpansion: {
3755     NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3756                                                                      Index);
3757     SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3758     SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3759     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3760                                    EllipsisLoc);
3761   }
3762   case TemplateArgument::Null:
3763   case TemplateArgument::Integral:
3764   case TemplateArgument::Declaration:
3765   case TemplateArgument::Pack:
3766     return TemplateArgumentLocInfo();
3767   }
3768   llvm_unreachable("unexpected template argument loc");
3769   return TemplateArgumentLocInfo();
3770 }
3771 
3772 TemplateArgumentLoc
3773 ASTReader::ReadTemplateArgumentLoc(Module &F,
3774                                    const RecordData &Record, unsigned &Index) {
3775   TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
3776 
3777   if (Arg.getKind() == TemplateArgument::Expression) {
3778     if (Record[Index++]) // bool InfoHasSameExpr.
3779       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3780   }
3781   return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
3782                                                              Record, Index));
3783 }
3784 
3785 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
3786   return GetDecl(ID);
3787 }
3788 
3789 uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
3790                                           unsigned &Idx){
3791   if (Idx >= Record.size())
3792     return 0;
3793 
3794   unsigned LocalID = Record[Idx++];
3795   return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
3796 }
3797 
3798 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3799   RecordLocation Loc = getLocalBitOffset(Offset);
3800   llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
3801   SavedStreamPosition SavedPosition(Cursor);
3802   Cursor.JumpToBit(Loc.Offset);
3803   ReadingKindTracker ReadingKind(Read_Decl, *this);
3804   RecordData Record;
3805   unsigned Code = Cursor.ReadCode();
3806   unsigned RecCode = Cursor.ReadRecord(Code, Record);
3807   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3808     Error("Malformed AST file: missing C++ base specifiers");
3809     return 0;
3810   }
3811 
3812   unsigned Idx = 0;
3813   unsigned NumBases = Record[Idx++];
3814   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3815   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3816   for (unsigned I = 0; I != NumBases; ++I)
3817     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
3818   return Bases;
3819 }
3820 
3821 serialization::DeclID
3822 ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
3823   if (LocalID < NUM_PREDEF_DECL_IDS)
3824     return LocalID;
3825 
3826   ContinuousRangeMap<uint32_t, int, 2>::iterator I
3827     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
3828   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
3829 
3830   return LocalID + I->second;
3831 }
3832 
3833 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
3834                                    Module &M) const {
3835   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
3836   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
3837   return &M == I->second;
3838 }
3839 
3840 Decl *ASTReader::GetDecl(DeclID ID) {
3841   if (ID < NUM_PREDEF_DECL_IDS) {
3842     switch ((PredefinedDeclIDs)ID) {
3843     case PREDEF_DECL_NULL_ID:
3844       return 0;
3845 
3846     case PREDEF_DECL_TRANSLATION_UNIT_ID:
3847       return Context.getTranslationUnitDecl();
3848 
3849     case PREDEF_DECL_OBJC_ID_ID:
3850       return Context.getObjCIdDecl();
3851 
3852     case PREDEF_DECL_OBJC_SEL_ID:
3853       return Context.getObjCSelDecl();
3854 
3855     case PREDEF_DECL_OBJC_CLASS_ID:
3856       return Context.getObjCClassDecl();
3857 
3858     case PREDEF_DECL_INT_128_ID:
3859       return Context.getInt128Decl();
3860 
3861     case PREDEF_DECL_UNSIGNED_INT_128_ID:
3862       return Context.getUInt128Decl();
3863 
3864     case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
3865       return Context.getObjCInstanceTypeDecl();
3866     }
3867 
3868     return 0;
3869   }
3870 
3871   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
3872 
3873   if (Index > DeclsLoaded.size()) {
3874     Error("declaration ID out-of-range for AST file");
3875     return 0;
3876   }
3877 
3878 if (!DeclsLoaded[Index]) {
3879     ReadDeclRecord(ID);
3880     if (DeserializationListener)
3881       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3882   }
3883 
3884   return DeclsLoaded[Index];
3885 }
3886 
3887 serialization::DeclID ASTReader::ReadDeclID(Module &F,
3888                                             const RecordData &Record,
3889                                             unsigned &Idx) {
3890   if (Idx >= Record.size()) {
3891     Error("Corrupted AST file");
3892     return 0;
3893   }
3894 
3895   return getGlobalDeclID(F, Record[Idx++]);
3896 }
3897 
3898 /// \brief Resolve the offset of a statement into a statement.
3899 ///
3900 /// This operation will read a new statement from the external
3901 /// source each time it is called, and is meant to be used via a
3902 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
3903 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
3904   // Switch case IDs are per Decl.
3905   ClearSwitchCaseIDs();
3906 
3907   // Offset here is a global offset across the entire chain.
3908   RecordLocation Loc = getLocalBitOffset(Offset);
3909   Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
3910   return ReadStmtFromStream(*Loc.F);
3911 }
3912 
3913 namespace {
3914   class FindExternalLexicalDeclsVisitor {
3915     ASTReader &Reader;
3916     const DeclContext *DC;
3917     bool (*isKindWeWant)(Decl::Kind);
3918 
3919     SmallVectorImpl<Decl*> &Decls;
3920     bool PredefsVisited[NUM_PREDEF_DECL_IDS];
3921 
3922   public:
3923     FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
3924                                     bool (*isKindWeWant)(Decl::Kind),
3925                                     SmallVectorImpl<Decl*> &Decls)
3926       : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
3927     {
3928       for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
3929         PredefsVisited[I] = false;
3930     }
3931 
3932     static bool visit(Module &M, bool Preorder, void *UserData) {
3933       if (Preorder)
3934         return false;
3935 
3936       FindExternalLexicalDeclsVisitor *This
3937         = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
3938 
3939       Module::DeclContextInfosMap::iterator Info
3940         = M.DeclContextInfos.find(This->DC);
3941       if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
3942         return false;
3943 
3944       // Load all of the declaration IDs
3945       for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
3946                                *IDE = ID + Info->second.NumLexicalDecls;
3947            ID != IDE; ++ID) {
3948         if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
3949           continue;
3950 
3951         // Don't add predefined declarations to the lexical context more
3952         // than once.
3953         if (ID->second < NUM_PREDEF_DECL_IDS) {
3954           if (This->PredefsVisited[ID->second])
3955             continue;
3956 
3957           This->PredefsVisited[ID->second] = true;
3958         }
3959 
3960         if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
3961           if (!This->DC->isDeclInLexicalTraversal(D))
3962             This->Decls.push_back(D);
3963         }
3964       }
3965 
3966       return false;
3967     }
3968   };
3969 }
3970 
3971 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
3972                                          bool (*isKindWeWant)(Decl::Kind),
3973                                          SmallVectorImpl<Decl*> &Decls) {
3974   // There might be lexical decls in multiple modules, for the TU at
3975   // least. Walk all of the modules in the order they were loaded.
3976   FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
3977   ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
3978   ++NumLexicalDeclContextsRead;
3979   return ELR_Success;
3980 }
3981 
3982 namespace {
3983   /// \brief Module visitor used to perform name lookup into a
3984   /// declaration context.
3985   class DeclContextNameLookupVisitor {
3986     ASTReader &Reader;
3987     const DeclContext *DC;
3988     DeclarationName Name;
3989     SmallVectorImpl<NamedDecl *> &Decls;
3990 
3991   public:
3992     DeclContextNameLookupVisitor(ASTReader &Reader,
3993                                  const DeclContext *DC, DeclarationName Name,
3994                                  SmallVectorImpl<NamedDecl *> &Decls)
3995       : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
3996 
3997     static bool visit(Module &M, void *UserData) {
3998       DeclContextNameLookupVisitor *This
3999         = static_cast<DeclContextNameLookupVisitor *>(UserData);
4000 
4001       // Check whether we have any visible declaration information for
4002       // this context in this module.
4003       Module::DeclContextInfosMap::iterator Info
4004         = M.DeclContextInfos.find(This->DC);
4005       if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4006         return false;
4007 
4008       // Look for this name within this module.
4009       ASTDeclContextNameLookupTable *LookupTable =
4010         (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4011       ASTDeclContextNameLookupTable::iterator Pos
4012         = LookupTable->find(This->Name);
4013       if (Pos == LookupTable->end())
4014         return false;
4015 
4016       bool FoundAnything = false;
4017       ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4018       for (; Data.first != Data.second; ++Data.first) {
4019         NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4020         if (!ND)
4021           continue;
4022 
4023         if (ND->getDeclName() != This->Name) {
4024           assert(!This->Name.getCXXNameType().isNull() &&
4025                  "Name mismatch without a type");
4026           continue;
4027         }
4028 
4029         // Record this declaration.
4030         FoundAnything = true;
4031         This->Decls.push_back(ND);
4032       }
4033 
4034       return FoundAnything;
4035     }
4036   };
4037 }
4038 
4039 DeclContext::lookup_result
4040 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
4041                                           DeclarationName Name) {
4042   assert(DC->hasExternalVisibleStorage() &&
4043          "DeclContext has no visible decls in storage");
4044   if (!Name)
4045     return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4046                                       DeclContext::lookup_iterator(0));
4047 
4048   SmallVector<NamedDecl *, 64> Decls;
4049   DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4050   ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
4051   ++NumVisibleDeclContextsRead;
4052   SetExternalVisibleDeclsForName(DC, Name, Decls);
4053   return const_cast<DeclContext*>(DC)->lookup(Name);
4054 }
4055 
4056 /// \brief Under non-PCH compilation the consumer receives the objc methods
4057 /// before receiving the implementation, and codegen depends on this.
4058 /// We simulate this by deserializing and passing to consumer the methods of the
4059 /// implementation before passing the deserialized implementation decl.
4060 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
4061                                        ASTConsumer *Consumer) {
4062   assert(ImplD && Consumer);
4063 
4064   for (ObjCImplDecl::method_iterator
4065          I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
4066     Consumer->HandleInterestingDecl(DeclGroupRef(*I));
4067 
4068   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
4069 }
4070 
4071 void ASTReader::PassInterestingDeclsToConsumer() {
4072   assert(Consumer);
4073   while (!InterestingDecls.empty()) {
4074     Decl *D = InterestingDecls.front();
4075     InterestingDecls.pop_front();
4076 
4077     if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4078       PassObjCImplDeclToConsumer(ImplD, Consumer);
4079     else
4080       Consumer->HandleInterestingDecl(DeclGroupRef(D));
4081   }
4082 }
4083 
4084 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
4085   this->Consumer = Consumer;
4086 
4087   if (!Consumer)
4088     return;
4089 
4090   for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
4091     // Force deserialization of this decl, which will cause it to be queued for
4092     // passing to the consumer.
4093     GetDecl(ExternalDefinitions[I]);
4094   }
4095   ExternalDefinitions.clear();
4096 
4097   PassInterestingDeclsToConsumer();
4098 }
4099 
4100 void ASTReader::PrintStats() {
4101   std::fprintf(stderr, "*** AST File Statistics:\n");
4102 
4103   unsigned NumTypesLoaded
4104     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
4105                                       QualType());
4106   unsigned NumDeclsLoaded
4107     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4108                                       (Decl *)0);
4109   unsigned NumIdentifiersLoaded
4110     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4111                                             IdentifiersLoaded.end(),
4112                                             (IdentifierInfo *)0);
4113   unsigned NumSelectorsLoaded
4114     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4115                                           SelectorsLoaded.end(),
4116                                           Selector());
4117 
4118   std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
4119   std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
4120   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
4121     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
4122                  NumSLocEntriesRead, TotalNumSLocEntries,
4123                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
4124   if (!TypesLoaded.empty())
4125     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
4126                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
4127                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4128   if (!DeclsLoaded.empty())
4129     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
4130                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4131                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
4132   if (!IdentifiersLoaded.empty())
4133     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
4134                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4135                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
4136   if (!SelectorsLoaded.empty())
4137     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
4138                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4139                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
4140   if (TotalNumStatements)
4141     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
4142                  NumStatementsRead, TotalNumStatements,
4143                  ((float)NumStatementsRead/TotalNumStatements * 100));
4144   if (TotalNumMacros)
4145     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
4146                  NumMacrosRead, TotalNumMacros,
4147                  ((float)NumMacrosRead/TotalNumMacros * 100));
4148   if (TotalLexicalDeclContexts)
4149     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
4150                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4151                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4152                   * 100));
4153   if (TotalVisibleDeclContexts)
4154     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
4155                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4156                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4157                   * 100));
4158   if (TotalNumMethodPoolEntries) {
4159     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
4160                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4161                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4162                   * 100));
4163     std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
4164   }
4165   std::fprintf(stderr, "\n");
4166   dump();
4167   std::fprintf(stderr, "\n");
4168 }
4169 
4170 template<typename Key, typename Module, unsigned InitialCapacity>
4171 static void
4172 dumpModuleIDMap(StringRef Name,
4173                 const ContinuousRangeMap<Key, Module *,
4174                                          InitialCapacity> &Map) {
4175   if (Map.begin() == Map.end())
4176     return;
4177 
4178   typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
4179   llvm::errs() << Name << ":\n";
4180   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4181        I != IEnd; ++I) {
4182     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
4183       << "\n";
4184   }
4185 }
4186 
4187 void ASTReader::dump() {
4188   llvm::errs() << "*** PCH/Module Remappings:\n";
4189   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
4190   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
4191   dumpModuleIDMap("Global type map", GlobalTypeMap);
4192   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
4193   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4194   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4195   dumpModuleIDMap("Global preprocessed entity map",
4196                   GlobalPreprocessedEntityMap);
4197 
4198   llvm::errs() << "\n*** PCH/Modules Loaded:";
4199   for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4200                                        MEnd = ModuleMgr.end();
4201        M != MEnd; ++M)
4202     (*M)->dump();
4203 }
4204 
4205 /// Return the amount of memory used by memory buffers, breaking down
4206 /// by heap-backed versus mmap'ed memory.
4207 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4208   for (ModuleConstIterator I = ModuleMgr.begin(),
4209       E = ModuleMgr.end(); I != E; ++I) {
4210     if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
4211       size_t bytes = buf->getBufferSize();
4212       switch (buf->getBufferKind()) {
4213         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4214           sizes.malloc_bytes += bytes;
4215           break;
4216         case llvm::MemoryBuffer::MemoryBuffer_MMap:
4217           sizes.mmap_bytes += bytes;
4218           break;
4219       }
4220     }
4221   }
4222 }
4223 
4224 void ASTReader::InitializeSema(Sema &S) {
4225   SemaObj = &S;
4226   S.ExternalSource = this;
4227 
4228   // Makes sure any declarations that were deserialized "too early"
4229   // still get added to the identifier's declaration chains.
4230   for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4231     if (SemaObj->TUScope)
4232       SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4233 
4234     SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4235   }
4236   PreloadedDecls.clear();
4237 
4238   // Load the offsets of the declarations that Sema references.
4239   // They will be lazily deserialized when needed.
4240   if (!SemaDeclRefs.empty()) {
4241     assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4242     if (!SemaObj->StdNamespace)
4243       SemaObj->StdNamespace = SemaDeclRefs[0];
4244     if (!SemaObj->StdBadAlloc)
4245       SemaObj->StdBadAlloc = SemaDeclRefs[1];
4246   }
4247 
4248   if (!FPPragmaOptions.empty()) {
4249     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4250     SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4251   }
4252 
4253   if (!OpenCLExtensions.empty()) {
4254     unsigned I = 0;
4255 #define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4256 #include "clang/Basic/OpenCLExtensions.def"
4257 
4258     assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4259   }
4260 }
4261 
4262 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4263   IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4264   ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4265   return Visitor.getIdentifierInfo();
4266 }
4267 
4268 namespace clang {
4269   /// \brief An identifier-lookup iterator that enumerates all of the
4270   /// identifiers stored within a set of AST files.
4271   class ASTIdentifierIterator : public IdentifierIterator {
4272     /// \brief The AST reader whose identifiers are being enumerated.
4273     const ASTReader &Reader;
4274 
4275     /// \brief The current index into the chain of AST files stored in
4276     /// the AST reader.
4277     unsigned Index;
4278 
4279     /// \brief The current position within the identifier lookup table
4280     /// of the current AST file.
4281     ASTIdentifierLookupTable::key_iterator Current;
4282 
4283     /// \brief The end position within the identifier lookup table of
4284     /// the current AST file.
4285     ASTIdentifierLookupTable::key_iterator End;
4286 
4287   public:
4288     explicit ASTIdentifierIterator(const ASTReader &Reader);
4289 
4290     virtual StringRef Next();
4291   };
4292 }
4293 
4294 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4295   : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
4296   ASTIdentifierLookupTable *IdTable
4297     = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
4298   Current = IdTable->key_begin();
4299   End = IdTable->key_end();
4300 }
4301 
4302 StringRef ASTIdentifierIterator::Next() {
4303   while (Current == End) {
4304     // If we have exhausted all of our AST files, we're done.
4305     if (Index == 0)
4306       return StringRef();
4307 
4308     --Index;
4309     ASTIdentifierLookupTable *IdTable
4310       = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4311         IdentifierLookupTable;
4312     Current = IdTable->key_begin();
4313     End = IdTable->key_end();
4314   }
4315 
4316   // We have any identifiers remaining in the current AST file; return
4317   // the next one.
4318   std::pair<const char*, unsigned> Key = *Current;
4319   ++Current;
4320   return StringRef(Key.first, Key.second);
4321 }
4322 
4323 IdentifierIterator *ASTReader::getIdentifiers() const {
4324   return new ASTIdentifierIterator(*this);
4325 }
4326 
4327 namespace clang { namespace serialization {
4328   class ReadMethodPoolVisitor {
4329     ASTReader &Reader;
4330     Selector Sel;
4331     llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4332     llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
4333 
4334     /// \brief Build an ObjCMethodList from a vector of Objective-C method
4335     /// declarations.
4336     ObjCMethodList
4337     buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4338     {
4339       ObjCMethodList List;
4340       ObjCMethodList *Prev = 0;
4341       for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4342         if (!List.Method) {
4343           // This is the first method, which is the easy case.
4344           List.Method = Vec[I];
4345           Prev = &List;
4346           continue;
4347         }
4348 
4349         ObjCMethodList *Mem =
4350           Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4351         Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4352         Prev = Prev->Next;
4353       }
4354 
4355       return List;
4356     }
4357 
4358   public:
4359     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4360       : Reader(Reader), Sel(Sel) { }
4361 
4362     static bool visit(Module &M, void *UserData) {
4363       ReadMethodPoolVisitor *This
4364         = static_cast<ReadMethodPoolVisitor *>(UserData);
4365 
4366       if (!M.SelectorLookupTable)
4367         return false;
4368 
4369       ASTSelectorLookupTable *PoolTable
4370         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4371       ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4372       if (Pos == PoolTable->end())
4373         return false;
4374 
4375       ++This->Reader.NumSelectorsRead;
4376       // FIXME: Not quite happy with the statistics here. We probably should
4377       // disable this tracking when called via LoadSelector.
4378       // Also, should entries without methods count as misses?
4379       ++This->Reader.NumMethodPoolEntriesRead;
4380       ASTSelectorLookupTrait::data_type Data = *Pos;
4381       if (This->Reader.DeserializationListener)
4382         This->Reader.DeserializationListener->SelectorRead(Data.ID,
4383                                                            This->Sel);
4384 
4385       This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4386       This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4387       return true;
4388     }
4389 
4390     /// \brief Retrieve the instance methods found by this visitor.
4391     ObjCMethodList getInstanceMethods() const {
4392       return buildObjCMethodList(InstanceMethods);
4393     }
4394 
4395     /// \brief Retrieve the instance methods found by this visitor.
4396     ObjCMethodList getFactoryMethods() const {
4397       return buildObjCMethodList(FactoryMethods);
4398     }
4399   };
4400 } } // end namespace clang::serialization
4401 
4402 std::pair<ObjCMethodList, ObjCMethodList>
4403 ASTReader::ReadMethodPool(Selector Sel) {
4404   ReadMethodPoolVisitor Visitor(*this, Sel);
4405   ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4406   std::pair<ObjCMethodList, ObjCMethodList> Result;
4407   Result.first = Visitor.getInstanceMethods();
4408   Result.second = Visitor.getFactoryMethods();
4409 
4410   if (!Result.first.Method && !Result.second.Method)
4411     ++NumMethodPoolMisses;
4412   return Result;
4413 }
4414 
4415 void ASTReader::ReadKnownNamespaces(
4416                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4417   Namespaces.clear();
4418 
4419   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4420     if (NamespaceDecl *Namespace
4421                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4422       Namespaces.push_back(Namespace);
4423   }
4424 }
4425 
4426 void ASTReader::ReadTentativeDefinitions(
4427                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
4428   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4429     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4430     if (Var)
4431       TentativeDefs.push_back(Var);
4432   }
4433   TentativeDefinitions.clear();
4434 }
4435 
4436 void ASTReader::ReadUnusedFileScopedDecls(
4437                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4438   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4439     DeclaratorDecl *D
4440       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4441     if (D)
4442       Decls.push_back(D);
4443   }
4444   UnusedFileScopedDecls.clear();
4445 }
4446 
4447 void ASTReader::ReadDelegatingConstructors(
4448                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4449   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4450     CXXConstructorDecl *D
4451       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4452     if (D)
4453       Decls.push_back(D);
4454   }
4455   DelegatingCtorDecls.clear();
4456 }
4457 
4458 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4459   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4460     TypedefNameDecl *D
4461       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4462     if (D)
4463       Decls.push_back(D);
4464   }
4465   ExtVectorDecls.clear();
4466 }
4467 
4468 void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4469   for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4470     CXXRecordDecl *D
4471       = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4472     if (D)
4473       Decls.push_back(D);
4474   }
4475   DynamicClasses.clear();
4476 }
4477 
4478 void
4479 ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4480   for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4481     NamedDecl *D
4482       = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4483     if (D)
4484       Decls.push_back(D);
4485   }
4486   LocallyScopedExternalDecls.clear();
4487 }
4488 
4489 void ASTReader::ReadReferencedSelectors(
4490        SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4491   if (ReferencedSelectorsData.empty())
4492     return;
4493 
4494   // If there are @selector references added them to its pool. This is for
4495   // implementation of -Wselector.
4496   unsigned int DataSize = ReferencedSelectorsData.size()-1;
4497   unsigned I = 0;
4498   while (I < DataSize) {
4499     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4500     SourceLocation SelLoc
4501       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4502     Sels.push_back(std::make_pair(Sel, SelLoc));
4503   }
4504   ReferencedSelectorsData.clear();
4505 }
4506 
4507 void ASTReader::ReadWeakUndeclaredIdentifiers(
4508        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4509   if (WeakUndeclaredIdentifiers.empty())
4510     return;
4511 
4512   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4513     IdentifierInfo *WeakId
4514       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4515     IdentifierInfo *AliasId
4516       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4517     SourceLocation Loc
4518       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4519     bool Used = WeakUndeclaredIdentifiers[I++];
4520     WeakInfo WI(AliasId, Loc);
4521     WI.setUsed(Used);
4522     WeakIDs.push_back(std::make_pair(WeakId, WI));
4523   }
4524   WeakUndeclaredIdentifiers.clear();
4525 }
4526 
4527 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4528   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4529     ExternalVTableUse VT;
4530     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4531     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4532     VT.DefinitionRequired = VTableUses[Idx++];
4533     VTables.push_back(VT);
4534   }
4535 
4536   VTableUses.clear();
4537 }
4538 
4539 void ASTReader::ReadPendingInstantiations(
4540        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4541   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4542     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4543     SourceLocation Loc
4544       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4545     Pending.push_back(std::make_pair(D, Loc));
4546   }
4547   PendingInstantiations.clear();
4548 }
4549 
4550 void ASTReader::LoadSelector(Selector Sel) {
4551   // It would be complicated to avoid reading the methods anyway. So don't.
4552   ReadMethodPool(Sel);
4553 }
4554 
4555 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
4556   assert(ID && "Non-zero identifier ID required");
4557   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4558   IdentifiersLoaded[ID - 1] = II;
4559   if (DeserializationListener)
4560     DeserializationListener->IdentifierRead(ID, II);
4561 }
4562 
4563 /// \brief Set the globally-visible declarations associated with the given
4564 /// identifier.
4565 ///
4566 /// If the AST reader is currently in a state where the given declaration IDs
4567 /// cannot safely be resolved, they are queued until it is safe to resolve
4568 /// them.
4569 ///
4570 /// \param II an IdentifierInfo that refers to one or more globally-visible
4571 /// declarations.
4572 ///
4573 /// \param DeclIDs the set of declaration IDs with the name @p II that are
4574 /// visible at global scope.
4575 ///
4576 /// \param Nonrecursive should be true to indicate that the caller knows that
4577 /// this call is non-recursive, and therefore the globally-visible declarations
4578 /// will not be placed onto the pending queue.
4579 void
4580 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4581                               const SmallVectorImpl<uint32_t> &DeclIDs,
4582                                    bool Nonrecursive) {
4583   if (NumCurrentElementsDeserializing && !Nonrecursive) {
4584     PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4585     PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4586     PII.II = II;
4587     PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4588     return;
4589   }
4590 
4591   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4592     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4593     if (SemaObj) {
4594       if (SemaObj->TUScope) {
4595         // Introduce this declaration into the translation-unit scope
4596         // and add it to the declaration chain for this identifier, so
4597         // that (unqualified) name lookup will find it.
4598         SemaObj->TUScope->AddDecl(D);
4599       }
4600       SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4601     } else {
4602       // Queue this declaration so that it will be added to the
4603       // translation unit scope and identifier's declaration chain
4604       // once a Sema object is known.
4605       PreloadedDecls.push_back(D);
4606     }
4607   }
4608 }
4609 
4610 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
4611   if (ID == 0)
4612     return 0;
4613 
4614   if (IdentifiersLoaded.empty()) {
4615     Error("no identifier table in AST file");
4616     return 0;
4617   }
4618 
4619   ID -= 1;
4620   if (!IdentifiersLoaded[ID]) {
4621     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4622     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
4623     Module *M = I->second;
4624     unsigned Index = ID - M->BaseIdentifierID;
4625     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
4626 
4627     // All of the strings in the AST file are preceded by a 16-bit length.
4628     // Extract that 16-bit length to avoid having to execute strlen().
4629     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4630     //  unsigned integers.  This is important to avoid integer overflow when
4631     //  we cast them to 'unsigned'.
4632     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4633     unsigned StrLen = (((unsigned) StrLenPtr[0])
4634                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4635     IdentifiersLoaded[ID]
4636       = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
4637     if (DeserializationListener)
4638       DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4639   }
4640 
4641   return IdentifiersLoaded[ID];
4642 }
4643 
4644 IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4645   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4646 }
4647 
4648 IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
4649   if (LocalID < NUM_PREDEF_IDENT_IDS)
4650     return LocalID;
4651 
4652   ContinuousRangeMap<uint32_t, int, 2>::iterator I
4653     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4654   assert(I != M.IdentifierRemap.end()
4655          && "Invalid index into identifier index remap");
4656 
4657   return LocalID + I->second;
4658 }
4659 
4660 bool ASTReader::ReadSLocEntry(int ID) {
4661   return ReadSLocEntryRecord(ID) != Success;
4662 }
4663 
4664 Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4665   return DecodeSelector(getGlobalSelectorID(M, LocalID));
4666 }
4667 
4668 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
4669   if (ID == 0)
4670     return Selector();
4671 
4672   if (ID > SelectorsLoaded.size()) {
4673     Error("selector ID out of range in AST file");
4674     return Selector();
4675   }
4676 
4677   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4678     // Load this selector from the selector table.
4679     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4680     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
4681     Module &M = *I->second;
4682     ASTSelectorLookupTrait Trait(*this, M);
4683     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
4684     SelectorsLoaded[ID - 1] =
4685       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
4686     if (DeserializationListener)
4687       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4688   }
4689 
4690   return SelectorsLoaded[ID - 1];
4691 }
4692 
4693 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
4694   return DecodeSelector(ID);
4695 }
4696 
4697 uint32_t ASTReader::GetNumExternalSelectors() {
4698   // ID 0 (the null selector) is considered an external selector.
4699   return getTotalNumSelectors() + 1;
4700 }
4701 
4702 serialization::SelectorID
4703 ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4704   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4705     return LocalID;
4706 
4707   ContinuousRangeMap<uint32_t, int, 2>::iterator I
4708     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4709   assert(I != M.SelectorRemap.end()
4710          && "Invalid index into identifier index remap");
4711 
4712   return LocalID + I->second;
4713 }
4714 
4715 DeclarationName
4716 ASTReader::ReadDeclarationName(Module &F,
4717                                const RecordData &Record, unsigned &Idx) {
4718   DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4719   switch (Kind) {
4720   case DeclarationName::Identifier:
4721     return DeclarationName(GetIdentifierInfo(F, Record, Idx));
4722 
4723   case DeclarationName::ObjCZeroArgSelector:
4724   case DeclarationName::ObjCOneArgSelector:
4725   case DeclarationName::ObjCMultiArgSelector:
4726     return DeclarationName(ReadSelector(F, Record, Idx));
4727 
4728   case DeclarationName::CXXConstructorName:
4729     return Context.DeclarationNames.getCXXConstructorName(
4730                           Context.getCanonicalType(readType(F, Record, Idx)));
4731 
4732   case DeclarationName::CXXDestructorName:
4733     return Context.DeclarationNames.getCXXDestructorName(
4734                           Context.getCanonicalType(readType(F, Record, Idx)));
4735 
4736   case DeclarationName::CXXConversionFunctionName:
4737     return Context.DeclarationNames.getCXXConversionFunctionName(
4738                           Context.getCanonicalType(readType(F, Record, Idx)));
4739 
4740   case DeclarationName::CXXOperatorName:
4741     return Context.DeclarationNames.getCXXOperatorName(
4742                                        (OverloadedOperatorKind)Record[Idx++]);
4743 
4744   case DeclarationName::CXXLiteralOperatorName:
4745     return Context.DeclarationNames.getCXXLiteralOperatorName(
4746                                        GetIdentifierInfo(F, Record, Idx));
4747 
4748   case DeclarationName::CXXUsingDirective:
4749     return DeclarationName::getUsingDirectiveName();
4750   }
4751 
4752   // Required to silence GCC warning
4753   return DeclarationName();
4754 }
4755 
4756 void ASTReader::ReadDeclarationNameLoc(Module &F,
4757                                        DeclarationNameLoc &DNLoc,
4758                                        DeclarationName Name,
4759                                       const RecordData &Record, unsigned &Idx) {
4760   switch (Name.getNameKind()) {
4761   case DeclarationName::CXXConstructorName:
4762   case DeclarationName::CXXDestructorName:
4763   case DeclarationName::CXXConversionFunctionName:
4764     DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4765     break;
4766 
4767   case DeclarationName::CXXOperatorName:
4768     DNLoc.CXXOperatorName.BeginOpNameLoc
4769         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4770     DNLoc.CXXOperatorName.EndOpNameLoc
4771         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4772     break;
4773 
4774   case DeclarationName::CXXLiteralOperatorName:
4775     DNLoc.CXXLiteralOperatorName.OpNameLoc
4776         = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4777     break;
4778 
4779   case DeclarationName::Identifier:
4780   case DeclarationName::ObjCZeroArgSelector:
4781   case DeclarationName::ObjCOneArgSelector:
4782   case DeclarationName::ObjCMultiArgSelector:
4783   case DeclarationName::CXXUsingDirective:
4784     break;
4785   }
4786 }
4787 
4788 void ASTReader::ReadDeclarationNameInfo(Module &F,
4789                                         DeclarationNameInfo &NameInfo,
4790                                       const RecordData &Record, unsigned &Idx) {
4791   NameInfo.setName(ReadDeclarationName(F, Record, Idx));
4792   NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4793   DeclarationNameLoc DNLoc;
4794   ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4795   NameInfo.setInfo(DNLoc);
4796 }
4797 
4798 void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
4799                                   const RecordData &Record, unsigned &Idx) {
4800   Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
4801   unsigned NumTPLists = Record[Idx++];
4802   Info.NumTemplParamLists = NumTPLists;
4803   if (NumTPLists) {
4804     Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
4805     for (unsigned i=0; i != NumTPLists; ++i)
4806       Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4807   }
4808 }
4809 
4810 TemplateName
4811 ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
4812                             unsigned &Idx) {
4813   TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
4814   switch (Kind) {
4815   case TemplateName::Template:
4816       return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
4817 
4818   case TemplateName::OverloadedTemplate: {
4819     unsigned size = Record[Idx++];
4820     UnresolvedSet<8> Decls;
4821     while (size--)
4822       Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
4823 
4824     return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
4825   }
4826 
4827   case TemplateName::QualifiedTemplate: {
4828     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
4829     bool hasTemplKeyword = Record[Idx++];
4830     TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
4831     return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4832   }
4833 
4834   case TemplateName::DependentTemplate: {
4835     NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
4836     if (Record[Idx++])  // isIdentifier
4837       return Context.getDependentTemplateName(NNS,
4838                                                GetIdentifierInfo(F, Record,
4839                                                                  Idx));
4840     return Context.getDependentTemplateName(NNS,
4841                                          (OverloadedOperatorKind)Record[Idx++]);
4842   }
4843 
4844   case TemplateName::SubstTemplateTemplateParm: {
4845     TemplateTemplateParmDecl *param
4846       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
4847     if (!param) return TemplateName();
4848     TemplateName replacement = ReadTemplateName(F, Record, Idx);
4849     return Context.getSubstTemplateTemplateParm(param, replacement);
4850   }
4851 
4852   case TemplateName::SubstTemplateTemplateParmPack: {
4853     TemplateTemplateParmDecl *Param
4854       = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
4855     if (!Param)
4856       return TemplateName();
4857 
4858     TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4859     if (ArgPack.getKind() != TemplateArgument::Pack)
4860       return TemplateName();
4861 
4862     return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
4863   }
4864   }
4865 
4866   assert(0 && "Unhandled template name kind!");
4867   return TemplateName();
4868 }
4869 
4870 TemplateArgument
4871 ASTReader::ReadTemplateArgument(Module &F,
4872                                 const RecordData &Record, unsigned &Idx) {
4873   TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4874   switch (Kind) {
4875   case TemplateArgument::Null:
4876     return TemplateArgument();
4877   case TemplateArgument::Type:
4878     return TemplateArgument(readType(F, Record, Idx));
4879   case TemplateArgument::Declaration:
4880     return TemplateArgument(ReadDecl(F, Record, Idx));
4881   case TemplateArgument::Integral: {
4882     llvm::APSInt Value = ReadAPSInt(Record, Idx);
4883     QualType T = readType(F, Record, Idx);
4884     return TemplateArgument(Value, T);
4885   }
4886   case TemplateArgument::Template:
4887     return TemplateArgument(ReadTemplateName(F, Record, Idx));
4888   case TemplateArgument::TemplateExpansion: {
4889     TemplateName Name = ReadTemplateName(F, Record, Idx);
4890     llvm::Optional<unsigned> NumTemplateExpansions;
4891     if (unsigned NumExpansions = Record[Idx++])
4892       NumTemplateExpansions = NumExpansions - 1;
4893     return TemplateArgument(Name, NumTemplateExpansions);
4894   }
4895   case TemplateArgument::Expression:
4896     return TemplateArgument(ReadExpr(F));
4897   case TemplateArgument::Pack: {
4898     unsigned NumArgs = Record[Idx++];
4899     TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
4900     for (unsigned I = 0; I != NumArgs; ++I)
4901       Args[I] = ReadTemplateArgument(F, Record, Idx);
4902     return TemplateArgument(Args, NumArgs);
4903   }
4904   }
4905 
4906   assert(0 && "Unhandled template argument kind!");
4907   return TemplateArgument();
4908 }
4909 
4910 TemplateParameterList *
4911 ASTReader::ReadTemplateParameterList(Module &F,
4912                                      const RecordData &Record, unsigned &Idx) {
4913   SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4914   SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4915   SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
4916 
4917   unsigned NumParams = Record[Idx++];
4918   SmallVector<NamedDecl *, 16> Params;
4919   Params.reserve(NumParams);
4920   while (NumParams--)
4921     Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
4922 
4923   TemplateParameterList* TemplateParams =
4924     TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
4925                                   Params.data(), Params.size(), RAngleLoc);
4926   return TemplateParams;
4927 }
4928 
4929 void
4930 ASTReader::
4931 ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
4932                          Module &F, const RecordData &Record,
4933                          unsigned &Idx) {
4934   unsigned NumTemplateArgs = Record[Idx++];
4935   TemplArgs.reserve(NumTemplateArgs);
4936   while (NumTemplateArgs--)
4937     TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
4938 }
4939 
4940 /// \brief Read a UnresolvedSet structure.
4941 void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
4942                                   const RecordData &Record, unsigned &Idx) {
4943   unsigned NumDecls = Record[Idx++];
4944   while (NumDecls--) {
4945     NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
4946     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4947     Set.addDecl(D, AS);
4948   }
4949 }
4950 
4951 CXXBaseSpecifier
4952 ASTReader::ReadCXXBaseSpecifier(Module &F,
4953                                 const RecordData &Record, unsigned &Idx) {
4954   bool isVirtual = static_cast<bool>(Record[Idx++]);
4955   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4956   AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
4957   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
4958   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4959   SourceRange Range = ReadSourceRange(F, Record, Idx);
4960   SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4961   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
4962                           EllipsisLoc);
4963   Result.setInheritConstructors(inheritConstructors);
4964   return Result;
4965 }
4966 
4967 std::pair<CXXCtorInitializer **, unsigned>
4968 ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
4969                                    unsigned &Idx) {
4970   CXXCtorInitializer **CtorInitializers = 0;
4971   unsigned NumInitializers = Record[Idx++];
4972   if (NumInitializers) {
4973     CtorInitializers
4974         = new (Context) CXXCtorInitializer*[NumInitializers];
4975     for (unsigned i=0; i != NumInitializers; ++i) {
4976       TypeSourceInfo *BaseClassInfo = 0;
4977       bool IsBaseVirtual = false;
4978       FieldDecl *Member = 0;
4979       IndirectFieldDecl *IndirectMember = 0;
4980       CXXConstructorDecl *Target = 0;
4981 
4982       CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4983       switch (Type) {
4984        case CTOR_INITIALIZER_BASE:
4985         BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
4986         IsBaseVirtual = Record[Idx++];
4987         break;
4988 
4989        case CTOR_INITIALIZER_DELEGATING:
4990         Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
4991         break;
4992 
4993        case CTOR_INITIALIZER_MEMBER:
4994         Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
4995         break;
4996 
4997        case CTOR_INITIALIZER_INDIRECT_MEMBER:
4998         IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
4999         break;
5000       }
5001 
5002       SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
5003       Expr *Init = ReadExpr(F);
5004       SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5005       SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
5006       bool IsWritten = Record[Idx++];
5007       unsigned SourceOrderOrNumArrayIndices;
5008       SmallVector<VarDecl *, 8> Indices;
5009       if (IsWritten) {
5010         SourceOrderOrNumArrayIndices = Record[Idx++];
5011       } else {
5012         SourceOrderOrNumArrayIndices = Record[Idx++];
5013         Indices.reserve(SourceOrderOrNumArrayIndices);
5014         for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
5015           Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
5016       }
5017 
5018       CXXCtorInitializer *BOMInit;
5019       if (Type == CTOR_INITIALIZER_BASE) {
5020         BOMInit = new (Context) CXXCtorInitializer(Context, BaseClassInfo, IsBaseVirtual,
5021                                              LParenLoc, Init, RParenLoc,
5022                                              MemberOrEllipsisLoc);
5023       } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5024         BOMInit = new (Context) CXXCtorInitializer(Context, MemberOrEllipsisLoc, LParenLoc,
5025                                              Target, Init, RParenLoc);
5026       } else if (IsWritten) {
5027         if (Member)
5028           BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
5029                                                LParenLoc, Init, RParenLoc);
5030         else
5031           BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
5032                                                MemberOrEllipsisLoc, LParenLoc,
5033                                                Init, RParenLoc);
5034       } else {
5035         BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
5036                                              LParenLoc, Init, RParenLoc,
5037                                              Indices.data(), Indices.size());
5038       }
5039 
5040       if (IsWritten)
5041         BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
5042       CtorInitializers[i] = BOMInit;
5043     }
5044   }
5045 
5046   return std::make_pair(CtorInitializers, NumInitializers);
5047 }
5048 
5049 NestedNameSpecifier *
5050 ASTReader::ReadNestedNameSpecifier(Module &F,
5051                                    const RecordData &Record, unsigned &Idx) {
5052   unsigned N = Record[Idx++];
5053   NestedNameSpecifier *NNS = 0, *Prev = 0;
5054   for (unsigned I = 0; I != N; ++I) {
5055     NestedNameSpecifier::SpecifierKind Kind
5056       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5057     switch (Kind) {
5058     case NestedNameSpecifier::Identifier: {
5059       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5060       NNS = NestedNameSpecifier::Create(Context, Prev, II);
5061       break;
5062     }
5063 
5064     case NestedNameSpecifier::Namespace: {
5065       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5066       NNS = NestedNameSpecifier::Create(Context, Prev, NS);
5067       break;
5068     }
5069 
5070     case NestedNameSpecifier::NamespaceAlias: {
5071       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5072       NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
5073       break;
5074     }
5075 
5076     case NestedNameSpecifier::TypeSpec:
5077     case NestedNameSpecifier::TypeSpecWithTemplate: {
5078       const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
5079       if (!T)
5080         return 0;
5081 
5082       bool Template = Record[Idx++];
5083       NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
5084       break;
5085     }
5086 
5087     case NestedNameSpecifier::Global: {
5088       NNS = NestedNameSpecifier::GlobalSpecifier(Context);
5089       // No associated value, and there can't be a prefix.
5090       break;
5091     }
5092     }
5093     Prev = NNS;
5094   }
5095   return NNS;
5096 }
5097 
5098 NestedNameSpecifierLoc
5099 ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
5100                                       unsigned &Idx) {
5101   unsigned N = Record[Idx++];
5102   NestedNameSpecifierLocBuilder Builder;
5103   for (unsigned I = 0; I != N; ++I) {
5104     NestedNameSpecifier::SpecifierKind Kind
5105       = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5106     switch (Kind) {
5107     case NestedNameSpecifier::Identifier: {
5108       IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5109       SourceRange Range = ReadSourceRange(F, Record, Idx);
5110       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
5111       break;
5112     }
5113 
5114     case NestedNameSpecifier::Namespace: {
5115       NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5116       SourceRange Range = ReadSourceRange(F, Record, Idx);
5117       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
5118       break;
5119     }
5120 
5121     case NestedNameSpecifier::NamespaceAlias: {
5122       NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5123       SourceRange Range = ReadSourceRange(F, Record, Idx);
5124       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
5125       break;
5126     }
5127 
5128     case NestedNameSpecifier::TypeSpec:
5129     case NestedNameSpecifier::TypeSpecWithTemplate: {
5130       bool Template = Record[Idx++];
5131       TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5132       if (!T)
5133         return NestedNameSpecifierLoc();
5134       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5135 
5136       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5137       Builder.Extend(Context,
5138                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5139                      T->getTypeLoc(), ColonColonLoc);
5140       break;
5141     }
5142 
5143     case NestedNameSpecifier::Global: {
5144       SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5145       Builder.MakeGlobal(Context, ColonColonLoc);
5146       break;
5147     }
5148     }
5149   }
5150 
5151   return Builder.getWithLocInContext(Context);
5152 }
5153 
5154 SourceRange
5155 ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
5156                            unsigned &Idx) {
5157   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5158   SourceLocation end = ReadSourceLocation(F, Record, Idx);
5159   return SourceRange(beg, end);
5160 }
5161 
5162 /// \brief Read an integral value
5163 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
5164   unsigned BitWidth = Record[Idx++];
5165   unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5166   llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5167   Idx += NumWords;
5168   return Result;
5169 }
5170 
5171 /// \brief Read a signed integral value
5172 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
5173   bool isUnsigned = Record[Idx++];
5174   return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5175 }
5176 
5177 /// \brief Read a floating-point value
5178 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
5179   return llvm::APFloat(ReadAPInt(Record, Idx));
5180 }
5181 
5182 // \brief Read a string
5183 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
5184   unsigned Len = Record[Idx++];
5185   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
5186   Idx += Len;
5187   return Result;
5188 }
5189 
5190 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5191                                          unsigned &Idx) {
5192   unsigned Major = Record[Idx++];
5193   unsigned Minor = Record[Idx++];
5194   unsigned Subminor = Record[Idx++];
5195   if (Minor == 0)
5196     return VersionTuple(Major);
5197   if (Subminor == 0)
5198     return VersionTuple(Major, Minor - 1);
5199   return VersionTuple(Major, Minor - 1, Subminor - 1);
5200 }
5201 
5202 CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
5203                                           const RecordData &Record,
5204                                           unsigned &Idx) {
5205   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
5206   return CXXTemporary::Create(Context, Decl);
5207 }
5208 
5209 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
5210   return Diag(SourceLocation(), DiagID);
5211 }
5212 
5213 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
5214   return Diags.Report(Loc, DiagID);
5215 }
5216 
5217 /// \brief Retrieve the identifier table associated with the
5218 /// preprocessor.
5219 IdentifierTable &ASTReader::getIdentifierTable() {
5220   return PP.getIdentifierTable();
5221 }
5222 
5223 /// \brief Record that the given ID maps to the given switch-case
5224 /// statement.
5225 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
5226   assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5227   SwitchCaseStmts[ID] = SC;
5228 }
5229 
5230 /// \brief Retrieve the switch-case statement with the given ID.
5231 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
5232   assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5233   return SwitchCaseStmts[ID];
5234 }
5235 
5236 void ASTReader::ClearSwitchCaseIDs() {
5237   SwitchCaseStmts.clear();
5238 }
5239 
5240 void ASTReader::FinishedDeserializing() {
5241   assert(NumCurrentElementsDeserializing &&
5242          "FinishedDeserializing not paired with StartedDeserializing");
5243   if (NumCurrentElementsDeserializing == 1) {
5244     // If any identifiers with corresponding top-level declarations have
5245     // been loaded, load those declarations now.
5246     while (!PendingIdentifierInfos.empty()) {
5247       SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5248                               PendingIdentifierInfos.front().DeclIDs, true);
5249       PendingIdentifierInfos.pop_front();
5250     }
5251 
5252     // Ready to load previous declarations of Decls that were delayed.
5253     while (!PendingPreviousDecls.empty()) {
5254       loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5255                                 PendingPreviousDecls.front().second);
5256       PendingPreviousDecls.pop_front();
5257     }
5258 
5259     // We are not in recursive loading, so it's safe to pass the "interesting"
5260     // decls to the consumer.
5261     if (Consumer)
5262       PassInterestingDeclsToConsumer();
5263 
5264     assert(PendingForwardRefs.size() == 0 &&
5265            "Some forward refs did not get linked to the definition!");
5266   }
5267   --NumCurrentElementsDeserializing;
5268 }
5269 
5270 ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
5271                      StringRef isysroot, bool DisableValidation,
5272                      bool DisableStatCache)
5273   : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5274     SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5275     Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
5276     Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5277     RelocatablePCH(false), isysroot(isysroot),
5278     DisableValidation(DisableValidation),
5279     DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5280     NumSLocEntriesRead(0), TotalNumSLocEntries(0),
5281     NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5282     TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5283     NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5284     NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5285     NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5286     TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5287     NumCXXBaseSpecifiersLoaded(0)
5288 {
5289   SourceMgr.setExternalSLocEntrySource(this);
5290 }
5291 
5292 ASTReader::~ASTReader() {
5293   for (DeclContextVisibleUpdatesPending::iterator
5294            I = PendingVisibleUpdates.begin(),
5295            E = PendingVisibleUpdates.end();
5296        I != E; ++I) {
5297     for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5298                                              F = I->second.end();
5299          J != F; ++J)
5300       delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
5301   }
5302 }
5303