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