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