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