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