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