ASTReader.cpp revision 7da99b05aa9482e76b2954223b5652469004aec2
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 "ASTCommon.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/Utils.h"
19#include "clang/Sema/Sema.h"
20#include "clang/Sema/Scope.h"
21#include "clang/AST/ASTConsumer.h"
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/ExprCXX.h"
26#include "clang/AST/Type.h"
27#include "clang/AST/TypeLocVisitor.h"
28#include "clang/Lex/MacroInfo.h"
29#include "clang/Lex/PreprocessingRecord.h"
30#include "clang/Lex/Preprocessor.h"
31#include "clang/Lex/HeaderSearch.h"
32#include "clang/Basic/OnDiskHashTable.h"
33#include "clang/Basic/SourceManager.h"
34#include "clang/Basic/SourceManagerInternals.h"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/FileSystemStatCache.h"
37#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/Version.h"
39#include "llvm/ADT/StringExtras.h"
40#include "llvm/Bitcode/BitstreamReader.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/Path.h"
45#include "llvm/Support/system_error.h"
46#include <algorithm>
47#include <iterator>
48#include <cstdio>
49#include <sys/stat.h>
50
51using namespace clang;
52using namespace clang::serialization;
53
54//===----------------------------------------------------------------------===//
55// PCH validator implementation
56//===----------------------------------------------------------------------===//
57
58ASTReaderListener::~ASTReaderListener() {}
59
60bool
61PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
62  const LangOptions &PPLangOpts = PP.getLangOptions();
63#define PARSE_LANGOPT_BENIGN(Option)
64#define PARSE_LANGOPT_IMPORTANT(Option, DiagID)                    \
65  if (PPLangOpts.Option != LangOpts.Option) {                      \
66    Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option;   \
67    return true;                                                   \
68  }
69
70  PARSE_LANGOPT_BENIGN(Trigraphs);
71  PARSE_LANGOPT_BENIGN(BCPLComment);
72  PARSE_LANGOPT_BENIGN(DollarIdents);
73  PARSE_LANGOPT_BENIGN(AsmPreprocessor);
74  PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
75  PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
76  PARSE_LANGOPT_BENIGN(ImplicitInt);
77  PARSE_LANGOPT_BENIGN(Digraphs);
78  PARSE_LANGOPT_BENIGN(HexFloats);
79  PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
80  PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
81  PARSE_LANGOPT_BENIGN(MSCVersion);
82  PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
83  PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
84  PARSE_LANGOPT_BENIGN(CXXOperatorName);
85  PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
86  PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
87  PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
88  PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
89  PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
90  PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
91                          diag::warn_pch_objc_auto_properties);
92  PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
93                          diag::warn_pch_no_constant_cfstrings);
94  PARSE_LANGOPT_BENIGN(PascalStrings);
95  PARSE_LANGOPT_BENIGN(WritableStrings);
96  PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
97                          diag::warn_pch_lax_vector_conversions);
98  PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
99  PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
100  PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
101  PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
102  PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
103  PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
104  PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
105  PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
106  PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
107  PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
108                          diag::warn_pch_thread_safe_statics);
109  PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
110  PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
111  PARSE_LANGOPT_BENIGN(EmitAllDecls);
112  PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
113  PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
114  PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
115                          diag::warn_pch_heinous_extensions);
116  // FIXME: Most of the options below are benign if the macro wasn't
117  // used. Unfortunately, this means that a PCH compiled without
118  // optimization can't be used with optimization turned on, even
119  // though the only thing that changes is whether __OPTIMIZE__ was
120  // defined... but if __OPTIMIZE__ never showed up in the header, it
121  // doesn't matter. We could consider making this some special kind
122  // of check.
123  PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
124  PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
125  PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
126  PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
127  PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
128  PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
129  PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
130  PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
131  PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
132  PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
133  if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
134    Reader.Diag(diag::warn_pch_gc_mode)
135      << LangOpts.getGCMode() << PPLangOpts.getGCMode();
136    return true;
137  }
138  PARSE_LANGOPT_BENIGN(getVisibilityMode());
139  PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
140                          diag::warn_pch_stack_protector);
141  PARSE_LANGOPT_BENIGN(InstantiationDepth);
142  PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
143  PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
144  PARSE_LANGOPT_BENIGN(CatchUndefined);
145  PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
146  PARSE_LANGOPT_BENIGN(SpellChecking);
147  PARSE_LANGOPT_BENIGN(DefaultFPContract);
148#undef PARSE_LANGOPT_IMPORTANT
149#undef PARSE_LANGOPT_BENIGN
150
151  return false;
152}
153
154bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
155  if (Triple == PP.getTargetInfo().getTriple().str())
156    return false;
157
158  Reader.Diag(diag::warn_pch_target_triple)
159    << Triple << PP.getTargetInfo().getTriple().str();
160  return true;
161}
162
163namespace {
164  struct EmptyStringRef {
165    bool operator ()(llvm::StringRef r) const { return r.empty(); }
166  };
167  struct EmptyBlock {
168    bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
169  };
170}
171
172static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
173                                PCHPredefinesBlocks R) {
174  // First, sum up the lengths.
175  unsigned LL = 0, RL = 0;
176  for (unsigned I = 0, N = L.size(); I != N; ++I) {
177    LL += L[I].size();
178  }
179  for (unsigned I = 0, N = R.size(); I != N; ++I) {
180    RL += R[I].Data.size();
181  }
182  if (LL != RL)
183    return false;
184  if (LL == 0 && RL == 0)
185    return true;
186
187  // Kick out empty parts, they confuse the algorithm below.
188  L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
189  R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
190
191  // Do it the hard way. At this point, both vectors must be non-empty.
192  llvm::StringRef LR = L[0], RR = R[0].Data;
193  unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
194  (void) RN;
195  for (;;) {
196    // Compare the current pieces.
197    if (LR.size() == RR.size()) {
198      // If they're the same length, it's pretty easy.
199      if (LR != RR)
200        return false;
201      // Both pieces are done, advance.
202      ++LI;
203      ++RI;
204      // If either string is done, they're both done, since they're the same
205      // length.
206      if (LI == LN) {
207        assert(RI == RN && "Strings not the same length after all?");
208        return true;
209      }
210      LR = L[LI];
211      RR = R[RI].Data;
212    } else if (LR.size() < RR.size()) {
213      // Right piece is longer.
214      if (!RR.startswith(LR))
215        return false;
216      ++LI;
217      assert(LI != LN && "Strings not the same length after all?");
218      RR = RR.substr(LR.size());
219      LR = L[LI];
220    } else {
221      // Left piece is longer.
222      if (!LR.startswith(RR))
223        return false;
224      ++RI;
225      assert(RI != RN && "Strings not the same length after all?");
226      LR = LR.substr(RR.size());
227      RR = R[RI].Data;
228    }
229  }
230}
231
232static std::pair<FileID, llvm::StringRef::size_type>
233FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
234  std::pair<FileID, llvm::StringRef::size_type> Res;
235  for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
236    Res.second = Buffers[I].Data.find(MacroDef);
237    if (Res.second != llvm::StringRef::npos) {
238      Res.first = Buffers[I].BufferID;
239      break;
240    }
241  }
242  return Res;
243}
244
245bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
246                                        llvm::StringRef OriginalFileName,
247                                        std::string &SuggestedPredefines) {
248  // We are in the context of an implicit include, so the predefines buffer will
249  // have a #include entry for the PCH file itself (as normalized by the
250  // preprocessor initialization). Find it and skip over it in the checking
251  // below.
252  llvm::SmallString<256> PCHInclude;
253  PCHInclude += "#include \"";
254  PCHInclude += NormalizeDashIncludePath(OriginalFileName);
255  PCHInclude += "\"\n";
256  std::pair<llvm::StringRef,llvm::StringRef> Split =
257    llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
258  llvm::StringRef Left =  Split.first, Right = Split.second;
259  if (Left == PP.getPredefines()) {
260    Error("Missing PCH include entry!");
261    return true;
262  }
263
264  // If the concatenation of all the PCH buffers is equal to the adjusted
265  // command line, we're done.
266  llvm::SmallVector<llvm::StringRef, 2> CommandLine;
267  CommandLine.push_back(Left);
268  CommandLine.push_back(Right);
269  if (EqualConcatenations(CommandLine, Buffers))
270    return false;
271
272  SourceManager &SourceMgr = PP.getSourceManager();
273
274  // The predefines buffers are different. Determine what the differences are,
275  // and whether they require us to reject the PCH file.
276  llvm::SmallVector<llvm::StringRef, 8> PCHLines;
277  for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
278    Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
279
280  llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
281  Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
282
283  // Pick out implicit #includes after the PCH and don't consider them for
284  // validation; we will insert them into SuggestedPredefines so that the
285  // preprocessor includes them.
286  std::string IncludesAfterPCH;
287  llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
288  Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
289  for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
290    if (AfterPCHLines[i].startswith("#include ")) {
291      IncludesAfterPCH += AfterPCHLines[i];
292      IncludesAfterPCH += '\n';
293    } else {
294      CmdLineLines.push_back(AfterPCHLines[i]);
295    }
296  }
297
298  // Make sure we add the includes last into SuggestedPredefines before we
299  // exit this function.
300  struct AddIncludesRAII {
301    std::string &SuggestedPredefines;
302    std::string &IncludesAfterPCH;
303
304    AddIncludesRAII(std::string &SuggestedPredefines,
305                    std::string &IncludesAfterPCH)
306      : SuggestedPredefines(SuggestedPredefines),
307        IncludesAfterPCH(IncludesAfterPCH) { }
308    ~AddIncludesRAII() {
309      SuggestedPredefines += IncludesAfterPCH;
310    }
311  } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
312
313  // Sort both sets of predefined buffer lines, since we allow some extra
314  // definitions and they may appear at any point in the output.
315  std::sort(CmdLineLines.begin(), CmdLineLines.end());
316  std::sort(PCHLines.begin(), PCHLines.end());
317
318  // Determine which predefines that were used to build the PCH file are missing
319  // from the command line.
320  std::vector<llvm::StringRef> MissingPredefines;
321  std::set_difference(PCHLines.begin(), PCHLines.end(),
322                      CmdLineLines.begin(), CmdLineLines.end(),
323                      std::back_inserter(MissingPredefines));
324
325  bool MissingDefines = false;
326  bool ConflictingDefines = false;
327  for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
328    llvm::StringRef Missing = MissingPredefines[I];
329    if (Missing.startswith("#include ")) {
330      // An -include was specified when generating the PCH; it is included in
331      // the PCH, just ignore it.
332      continue;
333    }
334    if (!Missing.startswith("#define ")) {
335      Reader.Diag(diag::warn_pch_compiler_options_mismatch);
336      return true;
337    }
338
339    // This is a macro definition. Determine the name of the macro we're
340    // defining.
341    std::string::size_type StartOfMacroName = strlen("#define ");
342    std::string::size_type EndOfMacroName
343      = Missing.find_first_of("( \n\r", StartOfMacroName);
344    assert(EndOfMacroName != std::string::npos &&
345           "Couldn't find the end of the macro name");
346    llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
347
348    // Determine whether this macro was given a different definition on the
349    // command line.
350    std::string MacroDefStart = "#define " + MacroName.str();
351    std::string::size_type MacroDefLen = MacroDefStart.size();
352    llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
353      = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
354                         MacroDefStart);
355    for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
356      if (!ConflictPos->startswith(MacroDefStart)) {
357        // Different macro; we're done.
358        ConflictPos = CmdLineLines.end();
359        break;
360      }
361
362      assert(ConflictPos->size() > MacroDefLen &&
363             "Invalid #define in predefines buffer?");
364      if ((*ConflictPos)[MacroDefLen] != ' ' &&
365          (*ConflictPos)[MacroDefLen] != '(')
366        continue; // Longer macro name; keep trying.
367
368      // We found a conflicting macro definition.
369      break;
370    }
371
372    if (ConflictPos != CmdLineLines.end()) {
373      Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
374          << MacroName;
375
376      // Show the definition of this macro within the PCH file.
377      std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
378          FindMacro(Buffers, Missing);
379      assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
380      SourceLocation PCHMissingLoc =
381          SourceMgr.getLocForStartOfFile(MacroLoc.first)
382            .getFileLocWithOffset(MacroLoc.second);
383      Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
384
385      ConflictingDefines = true;
386      continue;
387    }
388
389    // If the macro doesn't conflict, then we'll just pick up the macro
390    // definition from the PCH file. Warn the user that they made a mistake.
391    if (ConflictingDefines)
392      continue; // Don't complain if there are already conflicting defs
393
394    if (!MissingDefines) {
395      Reader.Diag(diag::warn_cmdline_missing_macro_defs);
396      MissingDefines = true;
397    }
398
399    // Show the definition of this macro within the PCH file.
400    std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
401        FindMacro(Buffers, Missing);
402    assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
403    SourceLocation PCHMissingLoc =
404        SourceMgr.getLocForStartOfFile(MacroLoc.first)
405          .getFileLocWithOffset(MacroLoc.second);
406    Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
407  }
408
409  if (ConflictingDefines)
410    return true;
411
412  // Determine what predefines were introduced based on command-line
413  // parameters that were not present when building the PCH
414  // file. Extra #defines are okay, so long as the identifiers being
415  // defined were not used within the precompiled header.
416  std::vector<llvm::StringRef> ExtraPredefines;
417  std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
418                      PCHLines.begin(), PCHLines.end(),
419                      std::back_inserter(ExtraPredefines));
420  for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
421    llvm::StringRef &Extra = ExtraPredefines[I];
422    if (!Extra.startswith("#define ")) {
423      Reader.Diag(diag::warn_pch_compiler_options_mismatch);
424      return true;
425    }
426
427    // This is an extra macro definition. Determine the name of the
428    // macro we're defining.
429    std::string::size_type StartOfMacroName = strlen("#define ");
430    std::string::size_type EndOfMacroName
431      = Extra.find_first_of("( \n\r", StartOfMacroName);
432    assert(EndOfMacroName != std::string::npos &&
433           "Couldn't find the end of the macro name");
434    llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
435
436    // Check whether this name was used somewhere in the PCH file. If
437    // so, defining it as a macro could change behavior, so we reject
438    // the PCH file.
439    if (IdentifierInfo *II = Reader.get(MacroName)) {
440      Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
441      return true;
442    }
443
444    // Add this definition to the suggested predefines buffer.
445    SuggestedPredefines += Extra;
446    SuggestedPredefines += '\n';
447  }
448
449  // If we get here, it's because the predefines buffer had compatible
450  // contents. Accept the PCH file.
451  return false;
452}
453
454void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
455                                      unsigned ID) {
456  PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
457  ++NumHeaderInfos;
458}
459
460void PCHValidator::ReadCounter(unsigned Value) {
461  PP.setCounterValue(Value);
462}
463
464//===----------------------------------------------------------------------===//
465// AST reader implementation
466//===----------------------------------------------------------------------===//
467
468void
469ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
470  DeserializationListener = Listener;
471}
472
473
474namespace {
475class ASTSelectorLookupTrait {
476  ASTReader &Reader;
477
478public:
479  struct data_type {
480    SelectorID ID;
481    ObjCMethodList Instance, Factory;
482  };
483
484  typedef Selector external_key_type;
485  typedef external_key_type internal_key_type;
486
487  explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
488
489  static bool EqualKey(const internal_key_type& a,
490                       const internal_key_type& b) {
491    return a == b;
492  }
493
494  static unsigned ComputeHash(Selector Sel) {
495    return serialization::ComputeHash(Sel);
496  }
497
498  // This hopefully will just get inlined and removed by the optimizer.
499  static const internal_key_type&
500  GetInternalKey(const external_key_type& x) { return x; }
501
502  static std::pair<unsigned, unsigned>
503  ReadKeyDataLength(const unsigned char*& d) {
504    using namespace clang::io;
505    unsigned KeyLen = ReadUnalignedLE16(d);
506    unsigned DataLen = ReadUnalignedLE16(d);
507    return std::make_pair(KeyLen, DataLen);
508  }
509
510  internal_key_type ReadKey(const unsigned char* d, unsigned) {
511    using namespace clang::io;
512    SelectorTable &SelTable = Reader.getContext()->Selectors;
513    unsigned N = ReadUnalignedLE16(d);
514    IdentifierInfo *FirstII
515      = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
516    if (N == 0)
517      return SelTable.getNullarySelector(FirstII);
518    else if (N == 1)
519      return SelTable.getUnarySelector(FirstII);
520
521    llvm::SmallVector<IdentifierInfo *, 16> Args;
522    Args.push_back(FirstII);
523    for (unsigned I = 1; I != N; ++I)
524      Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
525
526    return SelTable.getSelector(N, Args.data());
527  }
528
529  data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
530    using namespace clang::io;
531
532    data_type Result;
533
534    Result.ID = ReadUnalignedLE32(d);
535    unsigned NumInstanceMethods = ReadUnalignedLE16(d);
536    unsigned NumFactoryMethods = ReadUnalignedLE16(d);
537
538    // Load instance methods
539    ObjCMethodList *Prev = 0;
540    for (unsigned I = 0; I != NumInstanceMethods; ++I) {
541      ObjCMethodDecl *Method
542        = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
543      if (!Result.Instance.Method) {
544        // This is the first method, which is the easy case.
545        Result.Instance.Method = Method;
546        Prev = &Result.Instance;
547        continue;
548      }
549
550      ObjCMethodList *Mem =
551        Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
552      Prev->Next = new (Mem) ObjCMethodList(Method, 0);
553      Prev = Prev->Next;
554    }
555
556    // Load factory methods
557    Prev = 0;
558    for (unsigned I = 0; I != NumFactoryMethods; ++I) {
559      ObjCMethodDecl *Method
560        = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
561      if (!Result.Factory.Method) {
562        // This is the first method, which is the easy case.
563        Result.Factory.Method = Method;
564        Prev = &Result.Factory;
565        continue;
566      }
567
568      ObjCMethodList *Mem =
569        Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
570      Prev->Next = new (Mem) ObjCMethodList(Method, 0);
571      Prev = Prev->Next;
572    }
573
574    return Result;
575  }
576};
577
578} // end anonymous namespace
579
580/// \brief The on-disk hash table used for the global method pool.
581typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
582  ASTSelectorLookupTable;
583
584namespace clang {
585class ASTIdentifierLookupTrait {
586  ASTReader &Reader;
587  ASTReader::PerFileData &F;
588
589  // If we know the IdentifierInfo in advance, it is here and we will
590  // not build a new one. Used when deserializing information about an
591  // identifier that was constructed before the AST file was read.
592  IdentifierInfo *KnownII;
593
594public:
595  typedef IdentifierInfo * data_type;
596
597  typedef const std::pair<const char*, unsigned> external_key_type;
598
599  typedef external_key_type internal_key_type;
600
601  ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
602                           IdentifierInfo *II = 0)
603    : Reader(Reader), F(F), KnownII(II) { }
604
605  static bool EqualKey(const internal_key_type& a,
606                       const internal_key_type& b) {
607    return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
608                                  : false;
609  }
610
611  static unsigned ComputeHash(const internal_key_type& a) {
612    return llvm::HashString(llvm::StringRef(a.first, a.second));
613  }
614
615  // This hopefully will just get inlined and removed by the optimizer.
616  static const internal_key_type&
617  GetInternalKey(const external_key_type& x) { return x; }
618
619  // This hopefully will just get inlined and removed by the optimizer.
620  static const external_key_type&
621  GetExternalKey(const internal_key_type& x) { return x; }
622
623  static std::pair<unsigned, unsigned>
624  ReadKeyDataLength(const unsigned char*& d) {
625    using namespace clang::io;
626    unsigned DataLen = ReadUnalignedLE16(d);
627    unsigned KeyLen = ReadUnalignedLE16(d);
628    return std::make_pair(KeyLen, DataLen);
629  }
630
631  static std::pair<const char*, unsigned>
632  ReadKey(const unsigned char* d, unsigned n) {
633    assert(n >= 2 && d[n-1] == '\0');
634    return std::make_pair((const char*) d, n-1);
635  }
636
637  IdentifierInfo *ReadData(const internal_key_type& k,
638                           const unsigned char* d,
639                           unsigned DataLen) {
640    using namespace clang::io;
641    IdentID ID = ReadUnalignedLE32(d);
642    bool IsInteresting = ID & 0x01;
643
644    // Wipe out the "is interesting" bit.
645    ID = ID >> 1;
646
647    if (!IsInteresting) {
648      // For uninteresting identifiers, just build the IdentifierInfo
649      // and associate it with the persistent ID.
650      IdentifierInfo *II = KnownII;
651      if (!II)
652        II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
653      Reader.SetIdentifierInfo(ID, II);
654      II->setIsFromAST();
655      return II;
656    }
657
658    unsigned Bits = ReadUnalignedLE16(d);
659    bool CPlusPlusOperatorKeyword = Bits & 0x01;
660    Bits >>= 1;
661    bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
662    Bits >>= 1;
663    bool Poisoned = Bits & 0x01;
664    Bits >>= 1;
665    bool ExtensionToken = Bits & 0x01;
666    Bits >>= 1;
667    bool hasMacroDefinition = Bits & 0x01;
668    Bits >>= 1;
669    unsigned ObjCOrBuiltinID = Bits & 0x3FF;
670    Bits >>= 10;
671
672    assert(Bits == 0 && "Extra bits in the identifier?");
673    DataLen -= 6;
674
675    // Build the IdentifierInfo itself and link the identifier ID with
676    // the new IdentifierInfo.
677    IdentifierInfo *II = KnownII;
678    if (!II)
679      II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
680    Reader.SetIdentifierInfo(ID, II);
681
682    // Set or check the various bits in the IdentifierInfo structure.
683    // Token IDs are read-only.
684    if (HasRevertedTokenIDToIdentifier)
685      II->RevertTokenIDToIdentifier();
686    II->setObjCOrBuiltinID(ObjCOrBuiltinID);
687    assert(II->isExtensionToken() == ExtensionToken &&
688           "Incorrect extension token flag");
689    (void)ExtensionToken;
690    II->setIsPoisoned(Poisoned);
691    assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
692           "Incorrect C++ operator keyword flag");
693    (void)CPlusPlusOperatorKeyword;
694
695    // If this identifier is a macro, deserialize the macro
696    // definition.
697    if (hasMacroDefinition) {
698      uint32_t Offset = ReadUnalignedLE32(d);
699      Reader.SetIdentifierIsMacro(II, F, Offset);
700      DataLen -= 4;
701    }
702
703    // Read all of the declarations visible at global scope with this
704    // name.
705    if (Reader.getContext() == 0) return II;
706    if (DataLen > 0) {
707      llvm::SmallVector<uint32_t, 4> DeclIDs;
708      for (; DataLen > 0; DataLen -= 4)
709        DeclIDs.push_back(ReadUnalignedLE32(d));
710      Reader.SetGloballyVisibleDecls(II, DeclIDs);
711    }
712
713    II->setIsFromAST();
714    return II;
715  }
716};
717
718} // end anonymous namespace
719
720/// \brief The on-disk hash table used to contain information about
721/// all of the identifiers in the program.
722typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
723  ASTIdentifierLookupTable;
724
725namespace {
726class ASTDeclContextNameLookupTrait {
727  ASTReader &Reader;
728
729public:
730  /// \brief Pair of begin/end iterators for DeclIDs.
731  typedef std::pair<DeclID *, DeclID *> data_type;
732
733  /// \brief Special internal key for declaration names.
734  /// The hash table creates keys for comparison; we do not create
735  /// a DeclarationName for the internal key to avoid deserializing types.
736  struct DeclNameKey {
737    DeclarationName::NameKind Kind;
738    uint64_t Data;
739    DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
740  };
741
742  typedef DeclarationName external_key_type;
743  typedef DeclNameKey internal_key_type;
744
745  explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
746
747  static bool EqualKey(const internal_key_type& a,
748                       const internal_key_type& b) {
749    return a.Kind == b.Kind && a.Data == b.Data;
750  }
751
752  unsigned ComputeHash(const DeclNameKey &Key) const {
753    llvm::FoldingSetNodeID ID;
754    ID.AddInteger(Key.Kind);
755
756    switch (Key.Kind) {
757    case DeclarationName::Identifier:
758    case DeclarationName::CXXLiteralOperatorName:
759      ID.AddString(((IdentifierInfo*)Key.Data)->getName());
760      break;
761    case DeclarationName::ObjCZeroArgSelector:
762    case DeclarationName::ObjCOneArgSelector:
763    case DeclarationName::ObjCMultiArgSelector:
764      ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
765      break;
766    case DeclarationName::CXXConstructorName:
767    case DeclarationName::CXXDestructorName:
768    case DeclarationName::CXXConversionFunctionName:
769      ID.AddInteger((TypeID)Key.Data);
770      break;
771    case DeclarationName::CXXOperatorName:
772      ID.AddInteger((OverloadedOperatorKind)Key.Data);
773      break;
774    case DeclarationName::CXXUsingDirective:
775      break;
776    }
777
778    return ID.ComputeHash();
779  }
780
781  internal_key_type GetInternalKey(const external_key_type& Name) const {
782    DeclNameKey Key;
783    Key.Kind = Name.getNameKind();
784    switch (Name.getNameKind()) {
785    case DeclarationName::Identifier:
786      Key.Data = (uint64_t)Name.getAsIdentifierInfo();
787      break;
788    case DeclarationName::ObjCZeroArgSelector:
789    case DeclarationName::ObjCOneArgSelector:
790    case DeclarationName::ObjCMultiArgSelector:
791      Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
792      break;
793    case DeclarationName::CXXConstructorName:
794    case DeclarationName::CXXDestructorName:
795    case DeclarationName::CXXConversionFunctionName:
796      Key.Data = Reader.GetTypeID(Name.getCXXNameType());
797      break;
798    case DeclarationName::CXXOperatorName:
799      Key.Data = Name.getCXXOverloadedOperator();
800      break;
801    case DeclarationName::CXXLiteralOperatorName:
802      Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
803      break;
804    case DeclarationName::CXXUsingDirective:
805      break;
806    }
807
808    return Key;
809  }
810
811  external_key_type GetExternalKey(const internal_key_type& Key) const {
812    ASTContext *Context = Reader.getContext();
813    switch (Key.Kind) {
814    case DeclarationName::Identifier:
815      return DeclarationName((IdentifierInfo*)Key.Data);
816
817    case DeclarationName::ObjCZeroArgSelector:
818    case DeclarationName::ObjCOneArgSelector:
819    case DeclarationName::ObjCMultiArgSelector:
820      return DeclarationName(Selector(Key.Data));
821
822    case DeclarationName::CXXConstructorName:
823      return Context->DeclarationNames.getCXXConstructorName(
824                           Context->getCanonicalType(Reader.GetType(Key.Data)));
825
826    case DeclarationName::CXXDestructorName:
827      return Context->DeclarationNames.getCXXDestructorName(
828                           Context->getCanonicalType(Reader.GetType(Key.Data)));
829
830    case DeclarationName::CXXConversionFunctionName:
831      return Context->DeclarationNames.getCXXConversionFunctionName(
832                           Context->getCanonicalType(Reader.GetType(Key.Data)));
833
834    case DeclarationName::CXXOperatorName:
835      return Context->DeclarationNames.getCXXOperatorName(
836                                         (OverloadedOperatorKind)Key.Data);
837
838    case DeclarationName::CXXLiteralOperatorName:
839      return Context->DeclarationNames.getCXXLiteralOperatorName(
840                                                     (IdentifierInfo*)Key.Data);
841
842    case DeclarationName::CXXUsingDirective:
843      return DeclarationName::getUsingDirectiveName();
844    }
845
846    llvm_unreachable("Invalid Name Kind ?");
847  }
848
849  static std::pair<unsigned, unsigned>
850  ReadKeyDataLength(const unsigned char*& d) {
851    using namespace clang::io;
852    unsigned KeyLen = ReadUnalignedLE16(d);
853    unsigned DataLen = ReadUnalignedLE16(d);
854    return std::make_pair(KeyLen, DataLen);
855  }
856
857  internal_key_type ReadKey(const unsigned char* d, unsigned) {
858    using namespace clang::io;
859
860    DeclNameKey Key;
861    Key.Kind = (DeclarationName::NameKind)*d++;
862    switch (Key.Kind) {
863    case DeclarationName::Identifier:
864      Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
865      break;
866    case DeclarationName::ObjCZeroArgSelector:
867    case DeclarationName::ObjCOneArgSelector:
868    case DeclarationName::ObjCMultiArgSelector:
869      Key.Data =
870         (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
871      break;
872    case DeclarationName::CXXConstructorName:
873    case DeclarationName::CXXDestructorName:
874    case DeclarationName::CXXConversionFunctionName:
875      Key.Data = ReadUnalignedLE32(d); // TypeID
876      break;
877    case DeclarationName::CXXOperatorName:
878      Key.Data = *d++; // OverloadedOperatorKind
879      break;
880    case DeclarationName::CXXLiteralOperatorName:
881      Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
882      break;
883    case DeclarationName::CXXUsingDirective:
884      break;
885    }
886
887    return Key;
888  }
889
890  data_type ReadData(internal_key_type, const unsigned char* d,
891                     unsigned DataLen) {
892    using namespace clang::io;
893    unsigned NumDecls = ReadUnalignedLE16(d);
894    DeclID *Start = (DeclID *)d;
895    return std::make_pair(Start, Start + NumDecls);
896  }
897};
898
899} // end anonymous namespace
900
901/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
902typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
903  ASTDeclContextNameLookupTable;
904
905bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
906                                   const std::pair<uint64_t, uint64_t> &Offsets,
907                                       DeclContextInfo &Info) {
908  SavedStreamPosition SavedPosition(Cursor);
909  // First the lexical decls.
910  if (Offsets.first != 0) {
911    Cursor.JumpToBit(Offsets.first);
912
913    RecordData Record;
914    const char *Blob;
915    unsigned BlobLen;
916    unsigned Code = Cursor.ReadCode();
917    unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
918    if (RecCode != DECL_CONTEXT_LEXICAL) {
919      Error("Expected lexical block");
920      return true;
921    }
922
923    Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
924    Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
925  } else {
926    Info.LexicalDecls = 0;
927    Info.NumLexicalDecls = 0;
928  }
929
930  // Now the lookup table.
931  if (Offsets.second != 0) {
932    Cursor.JumpToBit(Offsets.second);
933
934    RecordData Record;
935    const char *Blob;
936    unsigned BlobLen;
937    unsigned Code = Cursor.ReadCode();
938    unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
939    if (RecCode != DECL_CONTEXT_VISIBLE) {
940      Error("Expected visible lookup table block");
941      return true;
942    }
943    Info.NameLookupTableData
944      = ASTDeclContextNameLookupTable::Create(
945                    (const unsigned char *)Blob + Record[0],
946                    (const unsigned char *)Blob,
947                    ASTDeclContextNameLookupTrait(*this));
948  } else {
949    Info.NameLookupTableData = 0;
950  }
951
952  return false;
953}
954
955void ASTReader::Error(const char *Msg) {
956  Diag(diag::err_fe_pch_malformed) << Msg;
957}
958
959/// \brief Tell the AST listener about the predefines buffers in the chain.
960bool ASTReader::CheckPredefinesBuffers() {
961  if (Listener)
962    return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
963                                          ActualOriginalFileName,
964                                          SuggestedPredefines);
965  return false;
966}
967
968//===----------------------------------------------------------------------===//
969// Source Manager Deserialization
970//===----------------------------------------------------------------------===//
971
972/// \brief Read the line table in the source manager block.
973/// \returns true if there was an error.
974bool ASTReader::ParseLineTable(PerFileData &F,
975                               llvm::SmallVectorImpl<uint64_t> &Record) {
976  unsigned Idx = 0;
977  LineTableInfo &LineTable = SourceMgr.getLineTable();
978
979  // Parse the file names
980  std::map<int, int> FileIDs;
981  for (int I = 0, N = Record[Idx++]; I != N; ++I) {
982    // Extract the file name
983    unsigned FilenameLen = Record[Idx++];
984    std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
985    Idx += FilenameLen;
986    MaybeAddSystemRootToFilename(Filename);
987    FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
988                                                  Filename.size());
989  }
990
991  // Parse the line entries
992  std::vector<LineEntry> Entries;
993  while (Idx < Record.size()) {
994    int FID = Record[Idx++];
995
996    // Extract the line entries
997    unsigned NumEntries = Record[Idx++];
998    assert(NumEntries && "Numentries is 00000");
999    Entries.clear();
1000    Entries.reserve(NumEntries);
1001    for (unsigned I = 0; I != NumEntries; ++I) {
1002      unsigned FileOffset = Record[Idx++];
1003      unsigned LineNo = Record[Idx++];
1004      int FilenameID = FileIDs[Record[Idx++]];
1005      SrcMgr::CharacteristicKind FileKind
1006        = (SrcMgr::CharacteristicKind)Record[Idx++];
1007      unsigned IncludeOffset = Record[Idx++];
1008      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1009                                       FileKind, IncludeOffset));
1010    }
1011    LineTable.AddEntry(FID, Entries);
1012  }
1013
1014  return false;
1015}
1016
1017namespace {
1018
1019class ASTStatData {
1020public:
1021  const ino_t ino;
1022  const dev_t dev;
1023  const mode_t mode;
1024  const time_t mtime;
1025  const off_t size;
1026
1027  ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
1028    : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1029};
1030
1031class ASTStatLookupTrait {
1032 public:
1033  typedef const char *external_key_type;
1034  typedef const char *internal_key_type;
1035
1036  typedef ASTStatData data_type;
1037
1038  static unsigned ComputeHash(const char *path) {
1039    return llvm::HashString(path);
1040  }
1041
1042  static internal_key_type GetInternalKey(const char *path) { return path; }
1043
1044  static bool EqualKey(internal_key_type a, internal_key_type b) {
1045    return strcmp(a, b) == 0;
1046  }
1047
1048  static std::pair<unsigned, unsigned>
1049  ReadKeyDataLength(const unsigned char*& d) {
1050    unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1051    unsigned DataLen = (unsigned) *d++;
1052    return std::make_pair(KeyLen + 1, DataLen);
1053  }
1054
1055  static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1056    return (const char *)d;
1057  }
1058
1059  static data_type ReadData(const internal_key_type, const unsigned char *d,
1060                            unsigned /*DataLen*/) {
1061    using namespace clang::io;
1062
1063    ino_t ino = (ino_t) ReadUnalignedLE32(d);
1064    dev_t dev = (dev_t) ReadUnalignedLE32(d);
1065    mode_t mode = (mode_t) ReadUnalignedLE16(d);
1066    time_t mtime = (time_t) ReadUnalignedLE64(d);
1067    off_t size = (off_t) ReadUnalignedLE64(d);
1068    return data_type(ino, dev, mode, mtime, size);
1069  }
1070};
1071
1072/// \brief stat() cache for precompiled headers.
1073///
1074/// This cache is very similar to the stat cache used by pretokenized
1075/// headers.
1076class ASTStatCache : public FileSystemStatCache {
1077  typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
1078  CacheTy *Cache;
1079
1080  unsigned &NumStatHits, &NumStatMisses;
1081public:
1082  ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1083               unsigned &NumStatHits, unsigned &NumStatMisses)
1084    : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1085    Cache = CacheTy::Create(Buckets, Base);
1086  }
1087
1088  ~ASTStatCache() { delete Cache; }
1089
1090  LookupResult getStat(const char *Path, struct stat &StatBuf,
1091                       int *FileDescriptor) {
1092    // Do the lookup for the file's data in the AST file.
1093    CacheTy::iterator I = Cache->find(Path);
1094
1095    // If we don't get a hit in the AST file just forward to 'stat'.
1096    if (I == Cache->end()) {
1097      ++NumStatMisses;
1098      return statChained(Path, StatBuf, FileDescriptor);
1099    }
1100
1101    ++NumStatHits;
1102    ASTStatData Data = *I;
1103
1104    StatBuf.st_ino = Data.ino;
1105    StatBuf.st_dev = Data.dev;
1106    StatBuf.st_mtime = Data.mtime;
1107    StatBuf.st_mode = Data.mode;
1108    StatBuf.st_size = Data.size;
1109    return CacheExists;
1110  }
1111};
1112} // end anonymous namespace
1113
1114
1115/// \brief Read a source manager block
1116ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
1117  using namespace SrcMgr;
1118
1119  llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1120
1121  // Set the source-location entry cursor to the current position in
1122  // the stream. This cursor will be used to read the contents of the
1123  // source manager block initially, and then lazily read
1124  // source-location entries as needed.
1125  SLocEntryCursor = F.Stream;
1126
1127  // The stream itself is going to skip over the source manager block.
1128  if (F.Stream.SkipBlock()) {
1129    Error("malformed block record in AST file");
1130    return Failure;
1131  }
1132
1133  // Enter the source manager block.
1134  if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1135    Error("malformed source manager block record in AST file");
1136    return Failure;
1137  }
1138
1139  RecordData Record;
1140  while (true) {
1141    unsigned Code = SLocEntryCursor.ReadCode();
1142    if (Code == llvm::bitc::END_BLOCK) {
1143      if (SLocEntryCursor.ReadBlockEnd()) {
1144        Error("error at end of Source Manager block in AST file");
1145        return Failure;
1146      }
1147      return Success;
1148    }
1149
1150    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1151      // No known subblocks, always skip them.
1152      SLocEntryCursor.ReadSubBlockID();
1153      if (SLocEntryCursor.SkipBlock()) {
1154        Error("malformed block record in AST file");
1155        return Failure;
1156      }
1157      continue;
1158    }
1159
1160    if (Code == llvm::bitc::DEFINE_ABBREV) {
1161      SLocEntryCursor.ReadAbbrevRecord();
1162      continue;
1163    }
1164
1165    // Read a record.
1166    const char *BlobStart;
1167    unsigned BlobLen;
1168    Record.clear();
1169    switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1170    default:  // Default behavior: ignore.
1171      break;
1172
1173    case SM_LINE_TABLE:
1174      if (ParseLineTable(F, Record))
1175        return Failure;
1176      break;
1177
1178    case SM_SLOC_FILE_ENTRY:
1179    case SM_SLOC_BUFFER_ENTRY:
1180    case SM_SLOC_INSTANTIATION_ENTRY:
1181      // Once we hit one of the source location entries, we're done.
1182      return Success;
1183    }
1184  }
1185}
1186
1187/// \brief If a header file is not found at the path that we expect it to be
1188/// and the PCH file was moved from its original location, try to resolve the
1189/// file by assuming that header+PCH were moved together and the header is in
1190/// the same place relative to the PCH.
1191static std::string
1192resolveFileRelativeToOriginalDir(const std::string &Filename,
1193                                 const std::string &OriginalDir,
1194                                 const std::string &CurrDir) {
1195  assert(OriginalDir != CurrDir &&
1196         "No point trying to resolve the file if the PCH dir didn't change");
1197  using namespace llvm::sys;
1198  llvm::SmallString<128> filePath(Filename);
1199  fs::make_absolute(filePath);
1200  assert(path::is_absolute(OriginalDir));
1201  llvm::SmallString<128> currPCHPath(CurrDir);
1202
1203  path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1204                       fileDirE = path::end(path::parent_path(filePath));
1205  path::const_iterator origDirI = path::begin(OriginalDir),
1206                       origDirE = path::end(OriginalDir);
1207  // Skip the common path components from filePath and OriginalDir.
1208  while (fileDirI != fileDirE && origDirI != origDirE &&
1209         *fileDirI == *origDirI) {
1210    ++fileDirI;
1211    ++origDirI;
1212  }
1213  for (; origDirI != origDirE; ++origDirI)
1214    path::append(currPCHPath, "..");
1215  path::append(currPCHPath, fileDirI, fileDirE);
1216  path::append(currPCHPath, path::filename(Filename));
1217  return currPCHPath.str();
1218}
1219
1220/// \brief Get a cursor that's correctly positioned for reading the source
1221/// location entry with the given ID.
1222ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
1223  assert(ID != 0 && ID <= TotalNumSLocEntries &&
1224         "SLocCursorForID should only be called for real IDs.");
1225
1226  ID -= 1;
1227  PerFileData *F = 0;
1228  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1229    F = Chain[N - I - 1];
1230    if (ID < F->LocalNumSLocEntries)
1231      break;
1232    ID -= F->LocalNumSLocEntries;
1233  }
1234  assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1235
1236  F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1237  return F;
1238}
1239
1240/// \brief Read in the source location entry with the given ID.
1241ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
1242  if (ID == 0)
1243    return Success;
1244
1245  if (ID > TotalNumSLocEntries) {
1246    Error("source location entry ID out-of-range for AST file");
1247    return Failure;
1248  }
1249
1250  PerFileData *F = SLocCursorForID(ID);
1251  llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1252
1253  ++NumSLocEntriesRead;
1254  unsigned Code = SLocEntryCursor.ReadCode();
1255  if (Code == llvm::bitc::END_BLOCK ||
1256      Code == llvm::bitc::ENTER_SUBBLOCK ||
1257      Code == llvm::bitc::DEFINE_ABBREV) {
1258    Error("incorrectly-formatted source location entry in AST file");
1259    return Failure;
1260  }
1261
1262  RecordData Record;
1263  const char *BlobStart;
1264  unsigned BlobLen;
1265  switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1266  default:
1267    Error("incorrectly-formatted source location entry in AST file");
1268    return Failure;
1269
1270  case SM_SLOC_FILE_ENTRY: {
1271    std::string Filename(BlobStart, BlobStart + BlobLen);
1272    MaybeAddSystemRootToFilename(Filename);
1273    const FileEntry *File = FileMgr.getFile(Filename);
1274    if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1275        OriginalDir != CurrentDir) {
1276      std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1277                                                              OriginalDir,
1278                                                              CurrentDir);
1279      if (!resolved.empty())
1280        File = FileMgr.getFile(resolved);
1281    }
1282    if (File == 0)
1283      File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1284                                    (time_t)Record[5]);
1285    if (File == 0) {
1286      std::string ErrorStr = "could not find file '";
1287      ErrorStr += Filename;
1288      ErrorStr += "' referenced by AST file";
1289      Error(ErrorStr.c_str());
1290      return Failure;
1291    }
1292
1293    if (Record.size() < 6) {
1294      Error("source location entry is incorrect");
1295      return Failure;
1296    }
1297
1298    if (!DisableValidation &&
1299        ((off_t)Record[4] != File->getSize()
1300#if !defined(LLVM_ON_WIN32)
1301        // In our regression testing, the Windows file system seems to
1302        // have inconsistent modification times that sometimes
1303        // erroneously trigger this error-handling path.
1304         || (time_t)Record[5] != File->getModificationTime()
1305#endif
1306        )) {
1307      Diag(diag::err_fe_pch_file_modified)
1308        << Filename;
1309      return Failure;
1310    }
1311
1312    FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1313                                        (SrcMgr::CharacteristicKind)Record[2],
1314                                        ID, Record[0]);
1315    if (Record[3])
1316      const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1317        .setHasLineDirectives();
1318
1319    break;
1320  }
1321
1322  case SM_SLOC_BUFFER_ENTRY: {
1323    const char *Name = BlobStart;
1324    unsigned Offset = Record[0];
1325    unsigned Code = SLocEntryCursor.ReadCode();
1326    Record.clear();
1327    unsigned RecCode
1328      = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1329
1330    if (RecCode != SM_SLOC_BUFFER_BLOB) {
1331      Error("AST record has invalid code");
1332      return Failure;
1333    }
1334
1335    llvm::MemoryBuffer *Buffer
1336    = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1337                                       Name);
1338    FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
1339
1340    if (strcmp(Name, "<built-in>") == 0) {
1341      PCHPredefinesBlock Block = {
1342        BufferID,
1343        llvm::StringRef(BlobStart, BlobLen - 1)
1344      };
1345      PCHPredefinesBuffers.push_back(Block);
1346    }
1347
1348    break;
1349  }
1350
1351  case SM_SLOC_INSTANTIATION_ENTRY: {
1352    SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1353    SourceMgr.createInstantiationLoc(SpellingLoc,
1354                                     ReadSourceLocation(*F, Record[2]),
1355                                     ReadSourceLocation(*F, Record[3]),
1356                                     Record[4],
1357                                     ID,
1358                                     Record[0]);
1359    break;
1360  }
1361  }
1362
1363  return Success;
1364}
1365
1366/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1367/// specified cursor.  Read the abbreviations that are at the top of the block
1368/// and then leave the cursor pointing into the block.
1369bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1370                                 unsigned BlockID) {
1371  if (Cursor.EnterSubBlock(BlockID)) {
1372    Error("malformed block record in AST file");
1373    return Failure;
1374  }
1375
1376  while (true) {
1377    uint64_t Offset = Cursor.GetCurrentBitNo();
1378    unsigned Code = Cursor.ReadCode();
1379
1380    // We expect all abbrevs to be at the start of the block.
1381    if (Code != llvm::bitc::DEFINE_ABBREV) {
1382      Cursor.JumpToBit(Offset);
1383      return false;
1384    }
1385    Cursor.ReadAbbrevRecord();
1386  }
1387}
1388
1389PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
1390  assert(PP && "Forgot to set Preprocessor ?");
1391  llvm::BitstreamCursor &Stream = F.MacroCursor;
1392
1393  // Keep track of where we are in the stream, then jump back there
1394  // after reading this macro.
1395  SavedStreamPosition SavedPosition(Stream);
1396
1397  Stream.JumpToBit(Offset);
1398  RecordData Record;
1399  llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1400  MacroInfo *Macro = 0;
1401
1402  while (true) {
1403    unsigned Code = Stream.ReadCode();
1404    switch (Code) {
1405    case llvm::bitc::END_BLOCK:
1406      return 0;
1407
1408    case llvm::bitc::ENTER_SUBBLOCK:
1409      // No known subblocks, always skip them.
1410      Stream.ReadSubBlockID();
1411      if (Stream.SkipBlock()) {
1412        Error("malformed block record in AST file");
1413        return 0;
1414      }
1415      continue;
1416
1417    case llvm::bitc::DEFINE_ABBREV:
1418      Stream.ReadAbbrevRecord();
1419      continue;
1420    default: break;
1421    }
1422
1423    // Read a record.
1424    const char *BlobStart = 0;
1425    unsigned BlobLen = 0;
1426    Record.clear();
1427    PreprocessorRecordTypes RecType =
1428      (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
1429                                                 BlobLen);
1430    switch (RecType) {
1431    case PP_MACRO_OBJECT_LIKE:
1432    case PP_MACRO_FUNCTION_LIKE: {
1433      // If we already have a macro, that means that we've hit the end
1434      // of the definition of the macro we were looking for. We're
1435      // done.
1436      if (Macro)
1437        return 0;
1438
1439      IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1440      if (II == 0) {
1441        Error("macro must have a name in AST file");
1442        return 0;
1443      }
1444      SourceLocation Loc = ReadSourceLocation(F, Record[1]);
1445      bool isUsed = Record[2];
1446
1447      MacroInfo *MI = PP->AllocateMacroInfo(Loc);
1448      MI->setIsUsed(isUsed);
1449      MI->setIsFromAST();
1450
1451      unsigned NextIndex = 3;
1452      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1453        // Decode function-like macro info.
1454        bool isC99VarArgs = Record[3];
1455        bool isGNUVarArgs = Record[4];
1456        MacroArgs.clear();
1457        unsigned NumArgs = Record[5];
1458        NextIndex = 6 + NumArgs;
1459        for (unsigned i = 0; i != NumArgs; ++i)
1460          MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1461
1462        // Install function-like macro info.
1463        MI->setIsFunctionLike();
1464        if (isC99VarArgs) MI->setIsC99Varargs();
1465        if (isGNUVarArgs) MI->setIsGNUVarargs();
1466        MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1467                            PP->getPreprocessorAllocator());
1468      }
1469
1470      // Finally, install the macro.
1471      PP->setMacroInfo(II, MI);
1472
1473      // Remember that we saw this macro last so that we add the tokens that
1474      // form its body to it.
1475      Macro = MI;
1476
1477      if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1478        // We have a macro definition. Load it now.
1479        PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1480                                        getMacroDefinition(Record[NextIndex]));
1481      }
1482
1483      ++NumMacrosRead;
1484      break;
1485    }
1486
1487    case PP_TOKEN: {
1488      // If we see a TOKEN before a PP_MACRO_*, then the file is
1489      // erroneous, just pretend we didn't see this.
1490      if (Macro == 0) break;
1491
1492      Token Tok;
1493      Tok.startToken();
1494      Tok.setLocation(ReadSourceLocation(F, Record[0]));
1495      Tok.setLength(Record[1]);
1496      if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1497        Tok.setIdentifierInfo(II);
1498      Tok.setKind((tok::TokenKind)Record[3]);
1499      Tok.setFlag((Token::TokenFlags)Record[4]);
1500      Macro->AddTokenToBody(Tok);
1501      break;
1502    }
1503  }
1504  }
1505
1506  return 0;
1507}
1508
1509PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1510  assert(PP && "Forgot to set Preprocessor ?");
1511  unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1512  switch (Code) {
1513  case llvm::bitc::END_BLOCK:
1514    return 0;
1515
1516  case llvm::bitc::ENTER_SUBBLOCK:
1517    Error("unexpected subblock record in preprocessor detail block");
1518    return 0;
1519
1520  case llvm::bitc::DEFINE_ABBREV:
1521    Error("unexpected abbrevation record in preprocessor detail block");
1522    return 0;
1523
1524  default:
1525    break;
1526  }
1527
1528  if (!PP->getPreprocessingRecord()) {
1529    Error("no preprocessing record");
1530    return 0;
1531  }
1532
1533  // Read the record.
1534  PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1535  const char *BlobStart = 0;
1536  unsigned BlobLen = 0;
1537  RecordData Record;
1538  PreprocessorDetailRecordTypes RecType =
1539    (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1540                                             Code, Record, BlobStart, BlobLen);
1541  switch (RecType) {
1542  case PPD_MACRO_INSTANTIATION: {
1543    if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1544      return PE;
1545
1546    MacroInstantiation *MI
1547      = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1548                                 SourceRange(ReadSourceLocation(F, Record[1]),
1549                                             ReadSourceLocation(F, Record[2])),
1550                                       getMacroDefinition(Record[4]));
1551    PPRec.SetPreallocatedEntity(Record[0], MI);
1552    return MI;
1553  }
1554
1555  case PPD_MACRO_DEFINITION: {
1556    if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1557      return PE;
1558
1559    if (Record[1] > MacroDefinitionsLoaded.size()) {
1560      Error("out-of-bounds macro definition record");
1561      return 0;
1562    }
1563
1564    // Decode the identifier info and then check again; if the macro is
1565    // still defined and associated with the identifier,
1566    IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1567    if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1568      MacroDefinition *MD
1569        = new (PPRec) MacroDefinition(II,
1570                                      ReadSourceLocation(F, Record[5]),
1571                                      SourceRange(
1572                                            ReadSourceLocation(F, Record[2]),
1573                                            ReadSourceLocation(F, Record[3])));
1574
1575      PPRec.SetPreallocatedEntity(Record[0], MD);
1576      MacroDefinitionsLoaded[Record[1] - 1] = MD;
1577
1578      if (DeserializationListener)
1579        DeserializationListener->MacroDefinitionRead(Record[1], MD);
1580    }
1581
1582    return MacroDefinitionsLoaded[Record[1] - 1];
1583  }
1584
1585  case PPD_INCLUSION_DIRECTIVE: {
1586    if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1587      return PE;
1588
1589    const char *FullFileNameStart = BlobStart + Record[3];
1590    const FileEntry *File
1591      = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1592                                                     BlobLen - Record[3]));
1593
1594    // FIXME: Stable encoding
1595    InclusionDirective::InclusionKind Kind
1596      = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1597    InclusionDirective *ID
1598      = new (PPRec) InclusionDirective(PPRec, Kind,
1599                                       llvm::StringRef(BlobStart, Record[3]),
1600                                       Record[4],
1601                                       File,
1602                                 SourceRange(ReadSourceLocation(F, Record[1]),
1603                                             ReadSourceLocation(F, Record[2])));
1604    PPRec.SetPreallocatedEntity(Record[0], ID);
1605    return ID;
1606  }
1607  }
1608
1609  Error("invalid offset in preprocessor detail block");
1610  return 0;
1611}
1612
1613namespace {
1614  /// \brief Trait class used to search the on-disk hash table containing all of
1615  /// the header search information.
1616  ///
1617  /// The on-disk hash table contains a mapping from each header path to
1618  /// information about that header (how many times it has been included, its
1619  /// controlling macro, etc.). Note that we actually hash based on the
1620  /// filename, and support "deep" comparisons of file names based on current
1621  /// inode numbers, so that the search can cope with non-normalized path names
1622  /// and symlinks.
1623  class HeaderFileInfoTrait {
1624    const char *SearchPath;
1625    struct stat SearchPathStatBuf;
1626    llvm::Optional<int> SearchPathStatResult;
1627
1628    int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1629      if (Path == SearchPath) {
1630        if (!SearchPathStatResult)
1631          SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1632
1633        *StatBuf = SearchPathStatBuf;
1634        return *SearchPathStatResult;
1635      }
1636
1637      return stat(Path, StatBuf);
1638    }
1639
1640  public:
1641    typedef const char *external_key_type;
1642    typedef const char *internal_key_type;
1643
1644    typedef HeaderFileInfo data_type;
1645
1646    HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1647
1648    static unsigned ComputeHash(const char *path) {
1649      return llvm::HashString(llvm::sys::path::filename(path));
1650    }
1651
1652    static internal_key_type GetInternalKey(const char *path) { return path; }
1653
1654    bool EqualKey(internal_key_type a, internal_key_type b) {
1655      if (strcmp(a, b) == 0)
1656        return true;
1657
1658      if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1659        return false;
1660
1661      // The file names match, but the path names don't. stat() the files to
1662      // see if they are the same.
1663      struct stat StatBufA, StatBufB;
1664      if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1665        return false;
1666
1667      return StatBufA.st_ino == StatBufB.st_ino;
1668    }
1669
1670    static std::pair<unsigned, unsigned>
1671    ReadKeyDataLength(const unsigned char*& d) {
1672      unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1673      unsigned DataLen = (unsigned) *d++;
1674      return std::make_pair(KeyLen + 1, DataLen);
1675    }
1676
1677    static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1678      return (const char *)d;
1679    }
1680
1681    static data_type ReadData(const internal_key_type, const unsigned char *d,
1682                              unsigned DataLen) {
1683      const unsigned char *End = d + DataLen;
1684      using namespace clang::io;
1685      HeaderFileInfo HFI;
1686      unsigned Flags = *d++;
1687      HFI.isImport = (Flags >> 3) & 0x01;
1688      HFI.DirInfo = (Flags >> 1) & 0x03;
1689      HFI.Resolved = Flags & 0x01;
1690      HFI.NumIncludes = ReadUnalignedLE16(d);
1691      HFI.ControllingMacroID = ReadUnalignedLE32(d);
1692      assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1693      (void)End;
1694
1695      // This HeaderFileInfo was externally loaded.
1696      HFI.External = true;
1697      return HFI;
1698    }
1699  };
1700}
1701
1702/// \brief The on-disk hash table used for the global method pool.
1703typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1704  HeaderFileInfoLookupTable;
1705
1706void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1707                                     uint64_t Offset) {
1708  // Note that this identifier has a macro definition.
1709  II->setHasMacroDefinition(true);
1710
1711  // Adjust the offset based on our position in the chain.
1712  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1713    if (Chain[I] == &F)
1714      break;
1715
1716    Offset += Chain[I]->SizeInBits;
1717  }
1718
1719  UnreadMacroRecordOffsets[II] = Offset;
1720}
1721
1722void ASTReader::ReadDefinedMacros() {
1723  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1724    PerFileData &F = *Chain[N - I - 1];
1725    llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
1726
1727    // If there was no preprocessor block, skip this file.
1728    if (!MacroCursor.getBitStreamReader())
1729      continue;
1730
1731    llvm::BitstreamCursor Cursor = MacroCursor;
1732    Cursor.JumpToBit(F.MacroStartOffset);
1733
1734    RecordData Record;
1735    while (true) {
1736      unsigned Code = Cursor.ReadCode();
1737      if (Code == llvm::bitc::END_BLOCK)
1738        break;
1739
1740      if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1741        // No known subblocks, always skip them.
1742        Cursor.ReadSubBlockID();
1743        if (Cursor.SkipBlock()) {
1744          Error("malformed block record in AST file");
1745          return;
1746        }
1747        continue;
1748      }
1749
1750      if (Code == llvm::bitc::DEFINE_ABBREV) {
1751        Cursor.ReadAbbrevRecord();
1752        continue;
1753      }
1754
1755      // Read a record.
1756      const char *BlobStart;
1757      unsigned BlobLen;
1758      Record.clear();
1759      switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1760      default:  // Default behavior: ignore.
1761        break;
1762
1763      case PP_MACRO_OBJECT_LIKE:
1764      case PP_MACRO_FUNCTION_LIKE:
1765        DecodeIdentifierInfo(Record[0]);
1766        break;
1767
1768      case PP_TOKEN:
1769        // Ignore tokens.
1770        break;
1771      }
1772    }
1773  }
1774
1775  // Drain the unread macro-record offsets map.
1776  while (!UnreadMacroRecordOffsets.empty())
1777    LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1778}
1779
1780void ASTReader::LoadMacroDefinition(
1781                     llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1782  assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1783  PerFileData *F = 0;
1784  uint64_t Offset = Pos->second;
1785  UnreadMacroRecordOffsets.erase(Pos);
1786
1787  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1788    if (Offset < Chain[I]->SizeInBits) {
1789      F = Chain[I];
1790      break;
1791    }
1792
1793    Offset -= Chain[I]->SizeInBits;
1794  }
1795  if (!F) {
1796    Error("Malformed macro record offset");
1797    return;
1798  }
1799
1800  ReadMacroRecord(*F, Offset);
1801}
1802
1803void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1804  llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1805    = UnreadMacroRecordOffsets.find(II);
1806  LoadMacroDefinition(Pos);
1807}
1808
1809MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
1810  if (ID == 0 || ID > MacroDefinitionsLoaded.size())
1811    return 0;
1812
1813  if (!MacroDefinitionsLoaded[ID - 1]) {
1814    unsigned Index = ID - 1;
1815    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1816      PerFileData &F = *Chain[N - I - 1];
1817      if (Index < F.LocalNumMacroDefinitions) {
1818        SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1819        F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1820        LoadPreprocessedEntity(F);
1821        break;
1822      }
1823      Index -= F.LocalNumMacroDefinitions;
1824    }
1825    assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
1826  }
1827
1828  return MacroDefinitionsLoaded[ID - 1];
1829}
1830
1831/// \brief If we are loading a relocatable PCH file, and the filename is
1832/// not an absolute path, add the system root to the beginning of the file
1833/// name.
1834void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1835  // If this is not a relocatable PCH file, there's nothing to do.
1836  if (!RelocatablePCH)
1837    return;
1838
1839  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1840    return;
1841
1842  if (isysroot == 0) {
1843    // If no system root was given, default to '/'
1844    Filename.insert(Filename.begin(), '/');
1845    return;
1846  }
1847
1848  unsigned Length = strlen(isysroot);
1849  if (isysroot[Length - 1] != '/')
1850    Filename.insert(Filename.begin(), '/');
1851
1852  Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1853}
1854
1855ASTReader::ASTReadResult
1856ASTReader::ReadASTBlock(PerFileData &F) {
1857  llvm::BitstreamCursor &Stream = F.Stream;
1858
1859  if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1860    Error("malformed block record in AST file");
1861    return Failure;
1862  }
1863
1864  // Read all of the records and blocks for the ASt file.
1865  RecordData Record;
1866  bool First = true;
1867  while (!Stream.AtEndOfStream()) {
1868    unsigned Code = Stream.ReadCode();
1869    if (Code == llvm::bitc::END_BLOCK) {
1870      if (Stream.ReadBlockEnd()) {
1871        Error("error at end of module block in AST file");
1872        return Failure;
1873      }
1874
1875      return Success;
1876    }
1877
1878    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1879      switch (Stream.ReadSubBlockID()) {
1880      case DECLTYPES_BLOCK_ID:
1881        // We lazily load the decls block, but we want to set up the
1882        // DeclsCursor cursor to point into it.  Clone our current bitcode
1883        // cursor to it, enter the block and read the abbrevs in that block.
1884        // With the main cursor, we just skip over it.
1885        F.DeclsCursor = Stream;
1886        if (Stream.SkipBlock() ||  // Skip with the main cursor.
1887            // Read the abbrevs.
1888            ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1889          Error("malformed block record in AST file");
1890          return Failure;
1891        }
1892        break;
1893
1894      case DECL_UPDATES_BLOCK_ID:
1895        if (Stream.SkipBlock()) {
1896          Error("malformed block record in AST file");
1897          return Failure;
1898        }
1899        break;
1900
1901      case PREPROCESSOR_BLOCK_ID:
1902        F.MacroCursor = Stream;
1903        if (PP)
1904          PP->setExternalSource(this);
1905
1906        if (Stream.SkipBlock() ||
1907            ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1908          Error("malformed block record in AST file");
1909          return Failure;
1910        }
1911        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1912        break;
1913
1914      case PREPROCESSOR_DETAIL_BLOCK_ID:
1915        F.PreprocessorDetailCursor = Stream;
1916        if (Stream.SkipBlock() ||
1917            ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1918                             PREPROCESSOR_DETAIL_BLOCK_ID)) {
1919          Error("malformed preprocessor detail record in AST file");
1920          return Failure;
1921        }
1922        F.PreprocessorDetailStartOffset
1923          = F.PreprocessorDetailCursor.GetCurrentBitNo();
1924        break;
1925
1926      case SOURCE_MANAGER_BLOCK_ID:
1927        switch (ReadSourceManagerBlock(F)) {
1928        case Success:
1929          break;
1930
1931        case Failure:
1932          Error("malformed source manager block in AST file");
1933          return Failure;
1934
1935        case IgnorePCH:
1936          return IgnorePCH;
1937        }
1938        break;
1939      }
1940      First = false;
1941      continue;
1942    }
1943
1944    if (Code == llvm::bitc::DEFINE_ABBREV) {
1945      Stream.ReadAbbrevRecord();
1946      continue;
1947    }
1948
1949    // Read and process a record.
1950    Record.clear();
1951    const char *BlobStart = 0;
1952    unsigned BlobLen = 0;
1953    switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1954                                              &BlobStart, &BlobLen)) {
1955    default:  // Default behavior: ignore.
1956      break;
1957
1958    case METADATA: {
1959      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1960        Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1961                                           : diag::warn_pch_version_too_new);
1962        return IgnorePCH;
1963      }
1964
1965      RelocatablePCH = Record[4];
1966      if (Listener) {
1967        std::string TargetTriple(BlobStart, BlobLen);
1968        if (Listener->ReadTargetTriple(TargetTriple))
1969          return IgnorePCH;
1970      }
1971      break;
1972    }
1973
1974    case CHAINED_METADATA: {
1975      if (!First) {
1976        Error("CHAINED_METADATA is not first record in block");
1977        return Failure;
1978      }
1979      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1980        Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1981                                           : diag::warn_pch_version_too_new);
1982        return IgnorePCH;
1983      }
1984
1985      // Load the chained file, which is always a PCH file.
1986      switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
1987      case Failure: return Failure;
1988        // If we have to ignore the dependency, we'll have to ignore this too.
1989      case IgnorePCH: return IgnorePCH;
1990      case Success: break;
1991      }
1992      break;
1993    }
1994
1995    case TYPE_OFFSET:
1996      if (F.LocalNumTypes != 0) {
1997        Error("duplicate TYPE_OFFSET record in AST file");
1998        return Failure;
1999      }
2000      F.TypeOffsets = (const uint32_t *)BlobStart;
2001      F.LocalNumTypes = Record[0];
2002      break;
2003
2004    case DECL_OFFSET:
2005      if (F.LocalNumDecls != 0) {
2006        Error("duplicate DECL_OFFSET record in AST file");
2007        return Failure;
2008      }
2009      F.DeclOffsets = (const uint32_t *)BlobStart;
2010      F.LocalNumDecls = Record[0];
2011      break;
2012
2013    case TU_UPDATE_LEXICAL: {
2014      DeclContextInfo Info = {
2015        /* No visible information */ 0,
2016        reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2017        BlobLen / sizeof(KindDeclIDPair)
2018      };
2019      DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2020        .push_back(Info);
2021      break;
2022    }
2023
2024    case UPDATE_VISIBLE: {
2025      serialization::DeclID ID = Record[0];
2026      void *Table = ASTDeclContextNameLookupTable::Create(
2027                        (const unsigned char *)BlobStart + Record[1],
2028                        (const unsigned char *)BlobStart,
2029                        ASTDeclContextNameLookupTrait(*this));
2030      if (ID == 1 && Context) { // Is it the TU?
2031        DeclContextInfo Info = {
2032          Table, /* No lexical inforamtion */ 0, 0
2033        };
2034        DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2035      } else
2036        PendingVisibleUpdates[ID].push_back(Table);
2037      break;
2038    }
2039
2040    case REDECLS_UPDATE_LATEST: {
2041      assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2042      for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
2043        DeclID First = Record[i], Latest = Record[i+1];
2044        assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2045                Latest > FirstLatestDeclIDs[First]) &&
2046               "The new latest is supposed to come after the previous latest");
2047        FirstLatestDeclIDs[First] = Latest;
2048      }
2049      break;
2050    }
2051
2052    case LANGUAGE_OPTIONS:
2053      if (ParseLanguageOptions(Record) && !DisableValidation)
2054        return IgnorePCH;
2055      break;
2056
2057    case IDENTIFIER_TABLE:
2058      F.IdentifierTableData = BlobStart;
2059      if (Record[0]) {
2060        F.IdentifierLookupTable
2061          = ASTIdentifierLookupTable::Create(
2062                       (const unsigned char *)F.IdentifierTableData + Record[0],
2063                       (const unsigned char *)F.IdentifierTableData,
2064                       ASTIdentifierLookupTrait(*this, F));
2065        if (PP)
2066          PP->getIdentifierTable().setExternalIdentifierLookup(this);
2067      }
2068      break;
2069
2070    case IDENTIFIER_OFFSET:
2071      if (F.LocalNumIdentifiers != 0) {
2072        Error("duplicate IDENTIFIER_OFFSET record in AST file");
2073        return Failure;
2074      }
2075      F.IdentifierOffsets = (const uint32_t *)BlobStart;
2076      F.LocalNumIdentifiers = Record[0];
2077      break;
2078
2079    case EXTERNAL_DEFINITIONS:
2080      // Optimization for the first block.
2081      if (ExternalDefinitions.empty())
2082        ExternalDefinitions.swap(Record);
2083      else
2084        ExternalDefinitions.insert(ExternalDefinitions.end(),
2085                                   Record.begin(), Record.end());
2086      break;
2087
2088    case SPECIAL_TYPES:
2089      // Optimization for the first block
2090      if (SpecialTypes.empty())
2091        SpecialTypes.swap(Record);
2092      else
2093        SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
2094      break;
2095
2096    case STATISTICS:
2097      TotalNumStatements += Record[0];
2098      TotalNumMacros += Record[1];
2099      TotalLexicalDeclContexts += Record[2];
2100      TotalVisibleDeclContexts += Record[3];
2101      break;
2102
2103    case TENTATIVE_DEFINITIONS:
2104      // Optimization for the first block.
2105      if (TentativeDefinitions.empty())
2106        TentativeDefinitions.swap(Record);
2107      else
2108        TentativeDefinitions.insert(TentativeDefinitions.end(),
2109                                    Record.begin(), Record.end());
2110      break;
2111
2112    case UNUSED_FILESCOPED_DECLS:
2113      // Optimization for the first block.
2114      if (UnusedFileScopedDecls.empty())
2115        UnusedFileScopedDecls.swap(Record);
2116      else
2117        UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2118                                     Record.begin(), Record.end());
2119      break;
2120
2121    case WEAK_UNDECLARED_IDENTIFIERS:
2122      // Later blocks overwrite earlier ones.
2123      WeakUndeclaredIdentifiers.swap(Record);
2124      break;
2125
2126    case LOCALLY_SCOPED_EXTERNAL_DECLS:
2127      // Optimization for the first block.
2128      if (LocallyScopedExternalDecls.empty())
2129        LocallyScopedExternalDecls.swap(Record);
2130      else
2131        LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2132                                          Record.begin(), Record.end());
2133      break;
2134
2135    case SELECTOR_OFFSETS:
2136      F.SelectorOffsets = (const uint32_t *)BlobStart;
2137      F.LocalNumSelectors = Record[0];
2138      break;
2139
2140    case METHOD_POOL:
2141      F.SelectorLookupTableData = (const unsigned char *)BlobStart;
2142      if (Record[0])
2143        F.SelectorLookupTable
2144          = ASTSelectorLookupTable::Create(
2145                        F.SelectorLookupTableData + Record[0],
2146                        F.SelectorLookupTableData,
2147                        ASTSelectorLookupTrait(*this));
2148      TotalNumMethodPoolEntries += Record[1];
2149      break;
2150
2151    case REFERENCED_SELECTOR_POOL:
2152      F.ReferencedSelectorsData.swap(Record);
2153      break;
2154
2155    case PP_COUNTER_VALUE:
2156      if (!Record.empty() && Listener)
2157        Listener->ReadCounter(Record[0]);
2158      break;
2159
2160    case SOURCE_LOCATION_OFFSETS:
2161      F.SLocOffsets = (const uint32_t *)BlobStart;
2162      F.LocalNumSLocEntries = Record[0];
2163      F.LocalSLocSize = Record[1];
2164      break;
2165
2166    case SOURCE_LOCATION_PRELOADS:
2167      if (PreloadSLocEntries.empty())
2168        PreloadSLocEntries.swap(Record);
2169      else
2170        PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2171            Record.begin(), Record.end());
2172      break;
2173
2174    case STAT_CACHE: {
2175      if (!DisableStatCache) {
2176        ASTStatCache *MyStatCache =
2177          new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2178                           (const unsigned char *)BlobStart,
2179                           NumStatHits, NumStatMisses);
2180        FileMgr.addStatCache(MyStatCache);
2181        F.StatCache = MyStatCache;
2182      }
2183      break;
2184    }
2185
2186    case EXT_VECTOR_DECLS:
2187      // Optimization for the first block.
2188      if (ExtVectorDecls.empty())
2189        ExtVectorDecls.swap(Record);
2190      else
2191        ExtVectorDecls.insert(ExtVectorDecls.end(),
2192                              Record.begin(), Record.end());
2193      break;
2194
2195    case VTABLE_USES:
2196      // Later tables overwrite earlier ones.
2197      VTableUses.swap(Record);
2198      break;
2199
2200    case DYNAMIC_CLASSES:
2201      // Optimization for the first block.
2202      if (DynamicClasses.empty())
2203        DynamicClasses.swap(Record);
2204      else
2205        DynamicClasses.insert(DynamicClasses.end(),
2206                              Record.begin(), Record.end());
2207      break;
2208
2209    case PENDING_IMPLICIT_INSTANTIATIONS:
2210      F.PendingInstantiations.swap(Record);
2211      break;
2212
2213    case SEMA_DECL_REFS:
2214      // Later tables overwrite earlier ones.
2215      SemaDeclRefs.swap(Record);
2216      break;
2217
2218    case ORIGINAL_FILE_NAME:
2219      // The primary AST will be the last to get here, so it will be the one
2220      // that's used.
2221      ActualOriginalFileName.assign(BlobStart, BlobLen);
2222      OriginalFileName = ActualOriginalFileName;
2223      MaybeAddSystemRootToFilename(OriginalFileName);
2224      break;
2225
2226    case ORIGINAL_PCH_DIR:
2227      // The primary AST will be the last to get here, so it will be the one
2228      // that's used.
2229      OriginalDir.assign(BlobStart, BlobLen);
2230      break;
2231
2232    case VERSION_CONTROL_BRANCH_REVISION: {
2233      const std::string &CurBranch = getClangFullRepositoryVersion();
2234      llvm::StringRef ASTBranch(BlobStart, BlobLen);
2235      if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2236        Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
2237        return IgnorePCH;
2238      }
2239      break;
2240    }
2241
2242    case MACRO_DEFINITION_OFFSETS:
2243      F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2244      F.NumPreallocatedPreprocessingEntities = Record[0];
2245      F.LocalNumMacroDefinitions = Record[1];
2246      break;
2247
2248    case DECL_UPDATE_OFFSETS: {
2249      if (Record.size() % 2 != 0) {
2250        Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2251        return Failure;
2252      }
2253      for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2254        DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2255            .push_back(std::make_pair(&F, Record[I+1]));
2256      break;
2257    }
2258
2259    case DECL_REPLACEMENTS: {
2260      if (Record.size() % 2 != 0) {
2261        Error("invalid DECL_REPLACEMENTS block in AST file");
2262        return Failure;
2263      }
2264      for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2265        ReplacedDecls[static_cast<DeclID>(Record[I])] =
2266            std::make_pair(&F, Record[I+1]);
2267      break;
2268    }
2269
2270    case CXX_BASE_SPECIFIER_OFFSETS: {
2271      if (F.LocalNumCXXBaseSpecifiers != 0) {
2272        Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2273        return Failure;
2274      }
2275
2276      F.LocalNumCXXBaseSpecifiers = Record[0];
2277      F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2278      break;
2279    }
2280
2281    case DIAG_PRAGMA_MAPPINGS:
2282      if (Record.size() % 2 != 0) {
2283        Error("invalid DIAG_USER_MAPPINGS block in AST file");
2284        return Failure;
2285      }
2286      if (PragmaDiagMappings.empty())
2287        PragmaDiagMappings.swap(Record);
2288      else
2289        PragmaDiagMappings.insert(PragmaDiagMappings.end(),
2290                                Record.begin(), Record.end());
2291      break;
2292
2293    case CUDA_SPECIAL_DECL_REFS:
2294      // Later tables overwrite earlier ones.
2295      CUDASpecialDeclRefs.swap(Record);
2296      break;
2297
2298    case HEADER_SEARCH_TABLE:
2299      F.HeaderFileInfoTableData = BlobStart;
2300      F.LocalNumHeaderFileInfos = Record[1];
2301      if (Record[0]) {
2302        F.HeaderFileInfoTable
2303          = HeaderFileInfoLookupTable::Create(
2304                   (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2305                   (const unsigned char *)F.HeaderFileInfoTableData);
2306        if (PP)
2307          PP->getHeaderSearchInfo().SetExternalSource(this);
2308      }
2309      break;
2310
2311    case FP_PRAGMA_OPTIONS:
2312      // Later tables overwrite earlier ones.
2313      FPPragmaOptions.swap(Record);
2314      break;
2315
2316    case OPENCL_EXTENSIONS:
2317      // Later tables overwrite earlier ones.
2318      OpenCLExtensions.swap(Record);
2319      break;
2320    }
2321    First = false;
2322  }
2323  Error("premature end of bitstream in AST file");
2324  return Failure;
2325}
2326
2327ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2328                                            ASTFileType Type) {
2329  switch(ReadASTCore(FileName, Type)) {
2330  case Failure: return Failure;
2331  case IgnorePCH: return IgnorePCH;
2332  case Success: break;
2333  }
2334
2335  // Here comes stuff that we only do once the entire chain is loaded.
2336
2337  // Allocate space for loaded slocentries, identifiers, decls and types.
2338  unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
2339           TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2340           TotalNumSelectors = 0;
2341  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2342    TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
2343    NextSLocOffset += Chain[I]->LocalSLocSize;
2344    TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
2345    TotalNumTypes += Chain[I]->LocalNumTypes;
2346    TotalNumDecls += Chain[I]->LocalNumDecls;
2347    TotalNumPreallocatedPreprocessingEntities +=
2348        Chain[I]->NumPreallocatedPreprocessingEntities;
2349    TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
2350    TotalNumSelectors += Chain[I]->LocalNumSelectors;
2351  }
2352  SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
2353  IdentifiersLoaded.resize(TotalNumIdentifiers);
2354  TypesLoaded.resize(TotalNumTypes);
2355  DeclsLoaded.resize(TotalNumDecls);
2356  MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2357  if (PP) {
2358    if (TotalNumIdentifiers > 0)
2359      PP->getHeaderSearchInfo().SetExternalLookup(this);
2360    if (TotalNumPreallocatedPreprocessingEntities > 0) {
2361      if (!PP->getPreprocessingRecord())
2362        PP->createPreprocessingRecord();
2363      PP->getPreprocessingRecord()->SetExternalSource(*this,
2364                                     TotalNumPreallocatedPreprocessingEntities);
2365    }
2366  }
2367  SelectorsLoaded.resize(TotalNumSelectors);
2368  // Preload SLocEntries.
2369  for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2370    ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2371    if (Result != Success)
2372      return Result;
2373  }
2374
2375  // Check the predefines buffers.
2376  if (!DisableValidation && CheckPredefinesBuffers())
2377    return IgnorePCH;
2378
2379  if (PP) {
2380    // Initialization of keywords and pragmas occurs before the
2381    // AST file is read, so there may be some identifiers that were
2382    // loaded into the IdentifierTable before we intercepted the
2383    // creation of identifiers. Iterate through the list of known
2384    // identifiers and determine whether we have to establish
2385    // preprocessor definitions or top-level identifier declaration
2386    // chains for those identifiers.
2387    //
2388    // We copy the IdentifierInfo pointers to a small vector first,
2389    // since de-serializing declarations or macro definitions can add
2390    // new entries into the identifier table, invalidating the
2391    // iterators.
2392    llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2393    for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2394                                IdEnd = PP->getIdentifierTable().end();
2395         Id != IdEnd; ++Id)
2396      Identifiers.push_back(Id->second);
2397    // We need to search the tables in all files.
2398    for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
2399      ASTIdentifierLookupTable *IdTable
2400        = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2401      // Not all AST files necessarily have identifier tables, only the useful
2402      // ones.
2403      if (!IdTable)
2404        continue;
2405      for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2406        IdentifierInfo *II = Identifiers[I];
2407        // Look in the on-disk hash tables for an entry for this identifier
2408        ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
2409        std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
2410        ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2411        if (Pos == IdTable->end())
2412          continue;
2413
2414        // Dereferencing the iterator has the effect of populating the
2415        // IdentifierInfo node with the various declarations it needs.
2416        (void)*Pos;
2417      }
2418    }
2419  }
2420
2421  if (Context)
2422    InitializeContext(*Context);
2423
2424  if (DeserializationListener)
2425    DeserializationListener->ReaderInitialized(this);
2426
2427  // If this AST file is a precompiled preamble, then set the main file ID of
2428  // the source manager to the file source file from which the preamble was
2429  // built. This is the only valid way to use a precompiled preamble.
2430  if (Type == Preamble) {
2431    SourceLocation Loc
2432      = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2433    if (Loc.isValid()) {
2434      std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2435      SourceMgr.SetPreambleFileID(Decomposed.first);
2436    }
2437  }
2438
2439  return Success;
2440}
2441
2442ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2443                                                ASTFileType Type) {
2444  PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
2445  Chain.push_back(new PerFileData(Type));
2446  PerFileData &F = *Chain.back();
2447  if (Prev)
2448    Prev->NextInSource = &F;
2449  else
2450    FirstInSource = &F;
2451  F.Loaders.push_back(Prev);
2452
2453  // Set the AST file name.
2454  F.FileName = FileName;
2455
2456  if (FileName != "-") {
2457    CurrentDir = llvm::sys::path::parent_path(FileName);
2458    if (CurrentDir.empty()) CurrentDir = ".";
2459  }
2460
2461  // Open the AST file.
2462  //
2463  // FIXME: This shouldn't be here, we should just take a raw_ostream.
2464  std::string ErrStr;
2465  llvm::error_code ec;
2466  if (FileName == "-") {
2467    ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2468    if (ec)
2469      ErrStr = ec.message();
2470  } else
2471    F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2472  if (!F.Buffer) {
2473    Error(ErrStr.c_str());
2474    return IgnorePCH;
2475  }
2476
2477  // Initialize the stream
2478  F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2479                    (const unsigned char *)F.Buffer->getBufferEnd());
2480  llvm::BitstreamCursor &Stream = F.Stream;
2481  Stream.init(F.StreamFile);
2482  F.SizeInBits = F.Buffer->getBufferSize() * 8;
2483
2484  // Sniff for the signature.
2485  if (Stream.Read(8) != 'C' ||
2486      Stream.Read(8) != 'P' ||
2487      Stream.Read(8) != 'C' ||
2488      Stream.Read(8) != 'H') {
2489    Diag(diag::err_not_a_pch_file) << FileName;
2490    return Failure;
2491  }
2492
2493  while (!Stream.AtEndOfStream()) {
2494    unsigned Code = Stream.ReadCode();
2495
2496    if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2497      Error("invalid record at top-level of AST file");
2498      return Failure;
2499    }
2500
2501    unsigned BlockID = Stream.ReadSubBlockID();
2502
2503    // We only know the AST subblock ID.
2504    switch (BlockID) {
2505    case llvm::bitc::BLOCKINFO_BLOCK_ID:
2506      if (Stream.ReadBlockInfoBlock()) {
2507        Error("malformed BlockInfoBlock in AST file");
2508        return Failure;
2509      }
2510      break;
2511    case AST_BLOCK_ID:
2512      switch (ReadASTBlock(F)) {
2513      case Success:
2514        break;
2515
2516      case Failure:
2517        return Failure;
2518
2519      case IgnorePCH:
2520        // FIXME: We could consider reading through to the end of this
2521        // AST block, skipping subblocks, to see if there are other
2522        // AST blocks elsewhere.
2523
2524        // Clear out any preallocated source location entries, so that
2525        // the source manager does not try to resolve them later.
2526        SourceMgr.ClearPreallocatedSLocEntries();
2527
2528        // Remove the stat cache.
2529        if (F.StatCache)
2530          FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
2531
2532        return IgnorePCH;
2533      }
2534      break;
2535    default:
2536      if (Stream.SkipBlock()) {
2537        Error("malformed block record in AST file");
2538        return Failure;
2539      }
2540      break;
2541    }
2542  }
2543
2544  return Success;
2545}
2546
2547void ASTReader::setPreprocessor(Preprocessor &pp) {
2548  PP = &pp;
2549
2550  unsigned TotalNum = 0;
2551  for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2552    TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2553  if (TotalNum) {
2554    if (!PP->getPreprocessingRecord())
2555      PP->createPreprocessingRecord();
2556    PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
2557  }
2558}
2559
2560void ASTReader::InitializeContext(ASTContext &Ctx) {
2561  Context = &Ctx;
2562  assert(Context && "Passed null context!");
2563
2564  assert(PP && "Forgot to set Preprocessor ?");
2565  PP->getIdentifierTable().setExternalIdentifierLookup(this);
2566  PP->getHeaderSearchInfo().SetExternalLookup(this);
2567  PP->setExternalSource(this);
2568  PP->getHeaderSearchInfo().SetExternalSource(this);
2569
2570  // If we have an update block for the TU waiting, we have to add it before
2571  // deserializing the decl.
2572  DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2573  if (DCU != DeclContextOffsets.end()) {
2574    // Insertion could invalidate map, so grab vector.
2575    DeclContextInfos T;
2576    T.swap(DCU->second);
2577    DeclContextOffsets.erase(DCU);
2578    DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2579  }
2580
2581  // Load the translation unit declaration
2582  GetTranslationUnitDecl();
2583
2584  // Load the special types.
2585  Context->setBuiltinVaListType(
2586    GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2587  if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
2588    Context->setObjCIdType(GetType(Id));
2589  if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
2590    Context->setObjCSelType(GetType(Sel));
2591  if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
2592    Context->setObjCProtoType(GetType(Proto));
2593  if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
2594    Context->setObjCClassType(GetType(Class));
2595
2596  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
2597    Context->setCFConstantStringType(GetType(String));
2598  if (unsigned FastEnum
2599        = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2600    Context->setObjCFastEnumerationStateType(GetType(FastEnum));
2601  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2602    QualType FileType = GetType(File);
2603    if (FileType.isNull()) {
2604      Error("FILE type is NULL");
2605      return;
2606    }
2607    if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2608      Context->setFILEDecl(Typedef->getDecl());
2609    else {
2610      const TagType *Tag = FileType->getAs<TagType>();
2611      if (!Tag) {
2612        Error("Invalid FILE type in AST file");
2613        return;
2614      }
2615      Context->setFILEDecl(Tag->getDecl());
2616    }
2617  }
2618  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2619    QualType Jmp_bufType = GetType(Jmp_buf);
2620    if (Jmp_bufType.isNull()) {
2621      Error("jmp_bug type is NULL");
2622      return;
2623    }
2624    if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2625      Context->setjmp_bufDecl(Typedef->getDecl());
2626    else {
2627      const TagType *Tag = Jmp_bufType->getAs<TagType>();
2628      if (!Tag) {
2629        Error("Invalid jmp_buf type in AST file");
2630        return;
2631      }
2632      Context->setjmp_bufDecl(Tag->getDecl());
2633    }
2634  }
2635  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2636    QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2637    if (Sigjmp_bufType.isNull()) {
2638      Error("sigjmp_buf type is NULL");
2639      return;
2640    }
2641    if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2642      Context->setsigjmp_bufDecl(Typedef->getDecl());
2643    else {
2644      const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2645      assert(Tag && "Invalid sigjmp_buf type in AST file");
2646      Context->setsigjmp_bufDecl(Tag->getDecl());
2647    }
2648  }
2649  if (unsigned ObjCIdRedef
2650        = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2651    Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2652  if (unsigned ObjCClassRedef
2653      = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2654    Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2655  if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2656    Context->setBlockDescriptorType(GetType(String));
2657  if (unsigned String
2658      = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2659    Context->setBlockDescriptorExtendedType(GetType(String));
2660  if (unsigned ObjCSelRedef
2661      = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2662    Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2663  if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
2664    Context->setNSConstantStringType(GetType(String));
2665
2666  if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
2667    Context->setInt128Installed();
2668
2669  ReadPragmaDiagnosticMappings(Context->getDiagnostics());
2670
2671  // If there were any CUDA special declarations, deserialize them.
2672  if (!CUDASpecialDeclRefs.empty()) {
2673    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2674    Context->setcudaConfigureCallDecl(
2675                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2676  }
2677}
2678
2679/// \brief Retrieve the name of the original source file name
2680/// directly from the AST file, without actually loading the AST
2681/// file.
2682std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2683                                             FileManager &FileMgr,
2684                                             Diagnostic &Diags) {
2685  // Open the AST file.
2686  std::string ErrStr;
2687  llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2688  Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
2689  if (!Buffer) {
2690    Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2691    return std::string();
2692  }
2693
2694  // Initialize the stream
2695  llvm::BitstreamReader StreamFile;
2696  llvm::BitstreamCursor Stream;
2697  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2698                  (const unsigned char *)Buffer->getBufferEnd());
2699  Stream.init(StreamFile);
2700
2701  // Sniff for the signature.
2702  if (Stream.Read(8) != 'C' ||
2703      Stream.Read(8) != 'P' ||
2704      Stream.Read(8) != 'C' ||
2705      Stream.Read(8) != 'H') {
2706    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2707    return std::string();
2708  }
2709
2710  RecordData Record;
2711  while (!Stream.AtEndOfStream()) {
2712    unsigned Code = Stream.ReadCode();
2713
2714    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2715      unsigned BlockID = Stream.ReadSubBlockID();
2716
2717      // We only know the AST subblock ID.
2718      switch (BlockID) {
2719      case AST_BLOCK_ID:
2720        if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2721          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2722          return std::string();
2723        }
2724        break;
2725
2726      default:
2727        if (Stream.SkipBlock()) {
2728          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2729          return std::string();
2730        }
2731        break;
2732      }
2733      continue;
2734    }
2735
2736    if (Code == llvm::bitc::END_BLOCK) {
2737      if (Stream.ReadBlockEnd()) {
2738        Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2739        return std::string();
2740      }
2741      continue;
2742    }
2743
2744    if (Code == llvm::bitc::DEFINE_ABBREV) {
2745      Stream.ReadAbbrevRecord();
2746      continue;
2747    }
2748
2749    Record.clear();
2750    const char *BlobStart = 0;
2751    unsigned BlobLen = 0;
2752    if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2753          == ORIGINAL_FILE_NAME)
2754      return std::string(BlobStart, BlobLen);
2755  }
2756
2757  return std::string();
2758}
2759
2760/// \brief Parse the record that corresponds to a LangOptions data
2761/// structure.
2762///
2763/// This routine parses the language options from the AST file and then gives
2764/// them to the AST listener if one is set.
2765///
2766/// \returns true if the listener deems the file unacceptable, false otherwise.
2767bool ASTReader::ParseLanguageOptions(
2768                             const llvm::SmallVectorImpl<uint64_t> &Record) {
2769  if (Listener) {
2770    LangOptions LangOpts;
2771
2772  #define PARSE_LANGOPT(Option)                  \
2773      LangOpts.Option = Record[Idx];             \
2774      ++Idx
2775
2776    unsigned Idx = 0;
2777    PARSE_LANGOPT(Trigraphs);
2778    PARSE_LANGOPT(BCPLComment);
2779    PARSE_LANGOPT(DollarIdents);
2780    PARSE_LANGOPT(AsmPreprocessor);
2781    PARSE_LANGOPT(GNUMode);
2782    PARSE_LANGOPT(GNUKeywords);
2783    PARSE_LANGOPT(ImplicitInt);
2784    PARSE_LANGOPT(Digraphs);
2785    PARSE_LANGOPT(HexFloats);
2786    PARSE_LANGOPT(C99);
2787    PARSE_LANGOPT(Microsoft);
2788    PARSE_LANGOPT(CPlusPlus);
2789    PARSE_LANGOPT(CPlusPlus0x);
2790    PARSE_LANGOPT(CXXOperatorNames);
2791    PARSE_LANGOPT(ObjC1);
2792    PARSE_LANGOPT(ObjC2);
2793    PARSE_LANGOPT(ObjCNonFragileABI);
2794    PARSE_LANGOPT(ObjCNonFragileABI2);
2795    PARSE_LANGOPT(AppleKext);
2796    PARSE_LANGOPT(ObjCDefaultSynthProperties);
2797    PARSE_LANGOPT(NoConstantCFStrings);
2798    PARSE_LANGOPT(PascalStrings);
2799    PARSE_LANGOPT(WritableStrings);
2800    PARSE_LANGOPT(LaxVectorConversions);
2801    PARSE_LANGOPT(AltiVec);
2802    PARSE_LANGOPT(Exceptions);
2803    PARSE_LANGOPT(ObjCExceptions);
2804    PARSE_LANGOPT(CXXExceptions);
2805    PARSE_LANGOPT(SjLjExceptions);
2806    PARSE_LANGOPT(MSBitfields);
2807    PARSE_LANGOPT(NeXTRuntime);
2808    PARSE_LANGOPT(Freestanding);
2809    PARSE_LANGOPT(NoBuiltin);
2810    PARSE_LANGOPT(ThreadsafeStatics);
2811    PARSE_LANGOPT(POSIXThreads);
2812    PARSE_LANGOPT(Blocks);
2813    PARSE_LANGOPT(EmitAllDecls);
2814    PARSE_LANGOPT(MathErrno);
2815    LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2816                                       Record[Idx++]);
2817    PARSE_LANGOPT(HeinousExtensions);
2818    PARSE_LANGOPT(Optimize);
2819    PARSE_LANGOPT(OptimizeSize);
2820    PARSE_LANGOPT(Static);
2821    PARSE_LANGOPT(PICLevel);
2822    PARSE_LANGOPT(GNUInline);
2823    PARSE_LANGOPT(NoInline);
2824    PARSE_LANGOPT(AccessControl);
2825    PARSE_LANGOPT(CharIsSigned);
2826    PARSE_LANGOPT(ShortWChar);
2827    PARSE_LANGOPT(ShortEnums);
2828    LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2829    LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
2830    LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
2831                                   Record[Idx++]);
2832    PARSE_LANGOPT(InstantiationDepth);
2833    PARSE_LANGOPT(OpenCL);
2834    PARSE_LANGOPT(CUDA);
2835    PARSE_LANGOPT(CatchUndefined);
2836    PARSE_LANGOPT(DefaultFPContract);
2837    // FIXME: Missing ElideConstructors?!
2838  #undef PARSE_LANGOPT
2839
2840    return Listener->ReadLanguageOptions(LangOpts);
2841  }
2842
2843  return false;
2844}
2845
2846void ASTReader::ReadPreprocessedEntities() {
2847  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2848    PerFileData &F = *Chain[I];
2849    if (!F.PreprocessorDetailCursor.getBitStreamReader())
2850      continue;
2851
2852    SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2853    F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2854    while (LoadPreprocessedEntity(F)) { }
2855  }
2856}
2857
2858PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
2859  PerFileData *F = 0;
2860  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2861    if (Offset < Chain[I]->SizeInBits) {
2862      F = Chain[I];
2863      break;
2864    }
2865
2866    Offset -= Chain[I]->SizeInBits;
2867  }
2868
2869  if (!F) {
2870    Error("Malformed preprocessed entity offset");
2871    return 0;
2872  }
2873
2874  // Keep track of where we are in the stream, then jump back there
2875  // after reading this entity.
2876  SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2877  F->PreprocessorDetailCursor.JumpToBit(Offset);
2878  return LoadPreprocessedEntity(*F);
2879}
2880
2881HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2882  HeaderFileInfoTrait Trait(FE->getName());
2883  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2884    PerFileData &F = *Chain[I];
2885    HeaderFileInfoLookupTable *Table
2886      = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2887    if (!Table)
2888      continue;
2889
2890    // Look in the on-disk hash table for an entry for this file name.
2891    HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2892                                                          &Trait);
2893    if (Pos == Table->end())
2894      continue;
2895
2896    HeaderFileInfo HFI = *Pos;
2897    if (Listener)
2898      Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2899
2900    return HFI;
2901  }
2902
2903  return HeaderFileInfo();
2904}
2905
2906void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
2907  unsigned Idx = 0;
2908  while (Idx < PragmaDiagMappings.size()) {
2909    SourceLocation
2910      Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2911    while (1) {
2912      assert(Idx < PragmaDiagMappings.size() &&
2913             "Invalid data, didn't find '-1' marking end of diag/map pairs");
2914      if (Idx >= PragmaDiagMappings.size())
2915        break; // Something is messed up but at least avoid infinite loop in
2916               // release build.
2917      unsigned DiagID = PragmaDiagMappings[Idx++];
2918      if (DiagID == (unsigned)-1)
2919        break; // no more diag/map pairs for this location.
2920      diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2921      Diag.setDiagnosticMapping(DiagID, Map, Loc);
2922    }
2923  }
2924}
2925
2926/// \brief Get the correct cursor and offset for loading a type.
2927ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
2928  PerFileData *F = 0;
2929  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2930    F = Chain[N - I - 1];
2931    if (Index < F->LocalNumTypes)
2932      break;
2933    Index -= F->LocalNumTypes;
2934  }
2935  assert(F && F->LocalNumTypes > Index && "Broken chain");
2936  return RecordLocation(F, F->TypeOffsets[Index]);
2937}
2938
2939/// \brief Read and return the type with the given index..
2940///
2941/// The index is the type ID, shifted and minus the number of predefs. This
2942/// routine actually reads the record corresponding to the type at the given
2943/// location. It is a helper routine for GetType, which deals with reading type
2944/// IDs.
2945QualType ASTReader::ReadTypeRecord(unsigned Index) {
2946  RecordLocation Loc = TypeCursorForIndex(Index);
2947  llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
2948
2949  // Keep track of where we are in the stream, then jump back there
2950  // after reading this type.
2951  SavedStreamPosition SavedPosition(DeclsCursor);
2952
2953  ReadingKindTracker ReadingKind(Read_Type, *this);
2954
2955  // Note that we are loading a type record.
2956  Deserializing AType(this);
2957
2958  DeclsCursor.JumpToBit(Loc.Offset);
2959  RecordData Record;
2960  unsigned Code = DeclsCursor.ReadCode();
2961  switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2962  case TYPE_EXT_QUAL: {
2963    if (Record.size() != 2) {
2964      Error("Incorrect encoding of extended qualifier type");
2965      return QualType();
2966    }
2967    QualType Base = GetType(Record[0]);
2968    Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2969    return Context->getQualifiedType(Base, Quals);
2970  }
2971
2972  case TYPE_COMPLEX: {
2973    if (Record.size() != 1) {
2974      Error("Incorrect encoding of complex type");
2975      return QualType();
2976    }
2977    QualType ElemType = GetType(Record[0]);
2978    return Context->getComplexType(ElemType);
2979  }
2980
2981  case TYPE_POINTER: {
2982    if (Record.size() != 1) {
2983      Error("Incorrect encoding of pointer type");
2984      return QualType();
2985    }
2986    QualType PointeeType = GetType(Record[0]);
2987    return Context->getPointerType(PointeeType);
2988  }
2989
2990  case TYPE_BLOCK_POINTER: {
2991    if (Record.size() != 1) {
2992      Error("Incorrect encoding of block pointer type");
2993      return QualType();
2994    }
2995    QualType PointeeType = GetType(Record[0]);
2996    return Context->getBlockPointerType(PointeeType);
2997  }
2998
2999  case TYPE_LVALUE_REFERENCE: {
3000    if (Record.size() != 1) {
3001      Error("Incorrect encoding of lvalue reference type");
3002      return QualType();
3003    }
3004    QualType PointeeType = GetType(Record[0]);
3005    return Context->getLValueReferenceType(PointeeType);
3006  }
3007
3008  case TYPE_RVALUE_REFERENCE: {
3009    if (Record.size() != 1) {
3010      Error("Incorrect encoding of rvalue reference type");
3011      return QualType();
3012    }
3013    QualType PointeeType = GetType(Record[0]);
3014    return Context->getRValueReferenceType(PointeeType);
3015  }
3016
3017  case TYPE_MEMBER_POINTER: {
3018    if (Record.size() != 2) {
3019      Error("Incorrect encoding of member pointer type");
3020      return QualType();
3021    }
3022    QualType PointeeType = GetType(Record[0]);
3023    QualType ClassType = GetType(Record[1]);
3024    if (PointeeType.isNull() || ClassType.isNull())
3025      return QualType();
3026
3027    return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
3028  }
3029
3030  case TYPE_CONSTANT_ARRAY: {
3031    QualType ElementType = GetType(Record[0]);
3032    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3033    unsigned IndexTypeQuals = Record[2];
3034    unsigned Idx = 3;
3035    llvm::APInt Size = ReadAPInt(Record, Idx);
3036    return Context->getConstantArrayType(ElementType, Size,
3037                                         ASM, IndexTypeQuals);
3038  }
3039
3040  case TYPE_INCOMPLETE_ARRAY: {
3041    QualType ElementType = GetType(Record[0]);
3042    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3043    unsigned IndexTypeQuals = Record[2];
3044    return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
3045  }
3046
3047  case TYPE_VARIABLE_ARRAY: {
3048    QualType ElementType = GetType(Record[0]);
3049    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3050    unsigned IndexTypeQuals = Record[2];
3051    SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3052    SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3053    return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
3054                                         ASM, IndexTypeQuals,
3055                                         SourceRange(LBLoc, RBLoc));
3056  }
3057
3058  case TYPE_VECTOR: {
3059    if (Record.size() != 3) {
3060      Error("incorrect encoding of vector type in AST file");
3061      return QualType();
3062    }
3063
3064    QualType ElementType = GetType(Record[0]);
3065    unsigned NumElements = Record[1];
3066    unsigned VecKind = Record[2];
3067    return Context->getVectorType(ElementType, NumElements,
3068                                  (VectorType::VectorKind)VecKind);
3069  }
3070
3071  case TYPE_EXT_VECTOR: {
3072    if (Record.size() != 3) {
3073      Error("incorrect encoding of extended vector type in AST file");
3074      return QualType();
3075    }
3076
3077    QualType ElementType = GetType(Record[0]);
3078    unsigned NumElements = Record[1];
3079    return Context->getExtVectorType(ElementType, NumElements);
3080  }
3081
3082  case TYPE_FUNCTION_NO_PROTO: {
3083    if (Record.size() != 4) {
3084      Error("incorrect encoding of no-proto function type");
3085      return QualType();
3086    }
3087    QualType ResultType = GetType(Record[0]);
3088    FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
3089    return Context->getFunctionNoProtoType(ResultType, Info);
3090  }
3091
3092  case TYPE_FUNCTION_PROTO: {
3093    QualType ResultType = GetType(Record[0]);
3094
3095    FunctionProtoType::ExtProtoInfo EPI;
3096    EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3097                                        /*regparm*/ Record[2],
3098                                        static_cast<CallingConv>(Record[3]));
3099
3100    unsigned Idx = 4;
3101    unsigned NumParams = Record[Idx++];
3102    llvm::SmallVector<QualType, 16> ParamTypes;
3103    for (unsigned I = 0; I != NumParams; ++I)
3104      ParamTypes.push_back(GetType(Record[Idx++]));
3105
3106    EPI.Variadic = Record[Idx++];
3107    EPI.TypeQuals = Record[Idx++];
3108    EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
3109    EPI.HasExceptionSpec = Record[Idx++];
3110    EPI.HasAnyExceptionSpec = Record[Idx++];
3111    EPI.NumExceptions = Record[Idx++];
3112    llvm::SmallVector<QualType, 2> Exceptions;
3113    for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3114      Exceptions.push_back(GetType(Record[Idx++]));
3115    EPI.Exceptions = Exceptions.data();
3116    return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
3117                                    EPI);
3118  }
3119
3120  case TYPE_UNRESOLVED_USING:
3121    return Context->getTypeDeclType(
3122             cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3123
3124  case TYPE_TYPEDEF: {
3125    if (Record.size() != 2) {
3126      Error("incorrect encoding of typedef type");
3127      return QualType();
3128    }
3129    TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3130    QualType Canonical = GetType(Record[1]);
3131    if (!Canonical.isNull())
3132      Canonical = Context->getCanonicalType(Canonical);
3133    return Context->getTypedefType(Decl, Canonical);
3134  }
3135
3136  case TYPE_TYPEOF_EXPR:
3137    return Context->getTypeOfExprType(ReadExpr(*Loc.F));
3138
3139  case TYPE_TYPEOF: {
3140    if (Record.size() != 1) {
3141      Error("incorrect encoding of typeof(type) in AST file");
3142      return QualType();
3143    }
3144    QualType UnderlyingType = GetType(Record[0]);
3145    return Context->getTypeOfType(UnderlyingType);
3146  }
3147
3148  case TYPE_DECLTYPE:
3149    return Context->getDecltypeType(ReadExpr(*Loc.F));
3150
3151  case TYPE_AUTO:
3152    return Context->getAutoType(GetType(Record[0]));
3153
3154  case TYPE_RECORD: {
3155    if (Record.size() != 2) {
3156      Error("incorrect encoding of record type");
3157      return QualType();
3158    }
3159    bool IsDependent = Record[0];
3160    QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
3161    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3162    return T;
3163  }
3164
3165  case TYPE_ENUM: {
3166    if (Record.size() != 2) {
3167      Error("incorrect encoding of enum type");
3168      return QualType();
3169    }
3170    bool IsDependent = Record[0];
3171    QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
3172    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3173    return T;
3174  }
3175
3176  case TYPE_ATTRIBUTED: {
3177    if (Record.size() != 3) {
3178      Error("incorrect encoding of attributed type");
3179      return QualType();
3180    }
3181    QualType modifiedType = GetType(Record[0]);
3182    QualType equivalentType = GetType(Record[1]);
3183    AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3184    return Context->getAttributedType(kind, modifiedType, equivalentType);
3185  }
3186
3187  case TYPE_PAREN: {
3188    if (Record.size() != 1) {
3189      Error("incorrect encoding of paren type");
3190      return QualType();
3191    }
3192    QualType InnerType = GetType(Record[0]);
3193    return Context->getParenType(InnerType);
3194  }
3195
3196  case TYPE_PACK_EXPANSION: {
3197    if (Record.size() != 2) {
3198      Error("incorrect encoding of pack expansion type");
3199      return QualType();
3200    }
3201    QualType Pattern = GetType(Record[0]);
3202    if (Pattern.isNull())
3203      return QualType();
3204    llvm::Optional<unsigned> NumExpansions;
3205    if (Record[1])
3206      NumExpansions = Record[1] - 1;
3207    return Context->getPackExpansionType(Pattern, NumExpansions);
3208  }
3209
3210  case TYPE_ELABORATED: {
3211    unsigned Idx = 0;
3212    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3213    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3214    QualType NamedType = GetType(Record[Idx++]);
3215    return Context->getElaboratedType(Keyword, NNS, NamedType);
3216  }
3217
3218  case TYPE_OBJC_INTERFACE: {
3219    unsigned Idx = 0;
3220    ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
3221    return Context->getObjCInterfaceType(ItfD);
3222  }
3223
3224  case TYPE_OBJC_OBJECT: {
3225    unsigned Idx = 0;
3226    QualType Base = GetType(Record[Idx++]);
3227    unsigned NumProtos = Record[Idx++];
3228    llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3229    for (unsigned I = 0; I != NumProtos; ++I)
3230      Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
3231    return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
3232  }
3233
3234  case TYPE_OBJC_OBJECT_POINTER: {
3235    unsigned Idx = 0;
3236    QualType Pointee = GetType(Record[Idx++]);
3237    return Context->getObjCObjectPointerType(Pointee);
3238  }
3239
3240  case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
3241    unsigned Idx = 0;
3242    QualType Parm = GetType(Record[Idx++]);
3243    QualType Replacement = GetType(Record[Idx++]);
3244    return
3245      Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3246                                            Replacement);
3247  }
3248
3249  case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3250    unsigned Idx = 0;
3251    QualType Parm = GetType(Record[Idx++]);
3252    TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3253    return Context->getSubstTemplateTypeParmPackType(
3254                                               cast<TemplateTypeParmType>(Parm),
3255                                                     ArgPack);
3256  }
3257
3258  case TYPE_INJECTED_CLASS_NAME: {
3259    CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3260    QualType TST = GetType(Record[1]); // probably derivable
3261    // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
3262    // for AST reading, too much interdependencies.
3263    return
3264      QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
3265  }
3266
3267  case TYPE_TEMPLATE_TYPE_PARM: {
3268    unsigned Idx = 0;
3269    unsigned Depth = Record[Idx++];
3270    unsigned Index = Record[Idx++];
3271    bool Pack = Record[Idx++];
3272    IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3273    return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3274  }
3275
3276  case TYPE_DEPENDENT_NAME: {
3277    unsigned Idx = 0;
3278    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3279    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3280    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3281    QualType Canon = GetType(Record[Idx++]);
3282    if (!Canon.isNull())
3283      Canon = Context->getCanonicalType(Canon);
3284    return Context->getDependentNameType(Keyword, NNS, Name, Canon);
3285  }
3286
3287  case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
3288    unsigned Idx = 0;
3289    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3290    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3291    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3292    unsigned NumArgs = Record[Idx++];
3293    llvm::SmallVector<TemplateArgument, 8> Args;
3294    Args.reserve(NumArgs);
3295    while (NumArgs--)
3296      Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
3297    return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3298                                                      Args.size(), Args.data());
3299  }
3300
3301  case TYPE_DEPENDENT_SIZED_ARRAY: {
3302    unsigned Idx = 0;
3303
3304    // ArrayType
3305    QualType ElementType = GetType(Record[Idx++]);
3306    ArrayType::ArraySizeModifier ASM
3307      = (ArrayType::ArraySizeModifier)Record[Idx++];
3308    unsigned IndexTypeQuals = Record[Idx++];
3309
3310    // DependentSizedArrayType
3311    Expr *NumElts = ReadExpr(*Loc.F);
3312    SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
3313
3314    return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3315                                               IndexTypeQuals, Brackets);
3316  }
3317
3318  case TYPE_TEMPLATE_SPECIALIZATION: {
3319    unsigned Idx = 0;
3320    bool IsDependent = Record[Idx++];
3321    TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
3322    llvm::SmallVector<TemplateArgument, 8> Args;
3323    ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
3324    QualType Canon = GetType(Record[Idx++]);
3325    QualType T;
3326    if (Canon.isNull())
3327      T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3328                                                          Args.size());
3329    else
3330      T = Context->getTemplateSpecializationType(Name, Args.data(),
3331                                                 Args.size(), Canon);
3332    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3333    return T;
3334  }
3335  }
3336  // Suppress a GCC warning
3337  return QualType();
3338}
3339
3340class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
3341  ASTReader &Reader;
3342  ASTReader::PerFileData &F;
3343  llvm::BitstreamCursor &DeclsCursor;
3344  const ASTReader::RecordData &Record;
3345  unsigned &Idx;
3346
3347  SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3348                                    unsigned &I) {
3349    return Reader.ReadSourceLocation(F, R, I);
3350  }
3351
3352public:
3353  TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
3354                const ASTReader::RecordData &Record, unsigned &Idx)
3355    : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3356  { }
3357
3358  // We want compile-time assurance that we've enumerated all of
3359  // these, so unfortunately we have to declare them first, then
3360  // define them out-of-line.
3361#define ABSTRACT_TYPELOC(CLASS, PARENT)
3362#define TYPELOC(CLASS, PARENT) \
3363  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
3364#include "clang/AST/TypeLocNodes.def"
3365
3366  void VisitFunctionTypeLoc(FunctionTypeLoc);
3367  void VisitArrayTypeLoc(ArrayTypeLoc);
3368};
3369
3370void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3371  // nothing to do
3372}
3373void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3374  TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
3375  if (TL.needsExtraLocalData()) {
3376    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3377    TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3378    TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3379    TL.setModeAttr(Record[Idx++]);
3380  }
3381}
3382void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
3383  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3384}
3385void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
3386  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3387}
3388void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3389  TL.setCaretLoc(ReadSourceLocation(Record, Idx));
3390}
3391void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3392  TL.setAmpLoc(ReadSourceLocation(Record, Idx));
3393}
3394void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3395  TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
3396}
3397void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3398  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3399}
3400void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
3401  TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3402  TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
3403  if (Record[Idx++])
3404    TL.setSizeExpr(Reader.ReadExpr(F));
3405  else
3406    TL.setSizeExpr(0);
3407}
3408void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3409  VisitArrayTypeLoc(TL);
3410}
3411void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3412  VisitArrayTypeLoc(TL);
3413}
3414void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3415  VisitArrayTypeLoc(TL);
3416}
3417void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3418                                            DependentSizedArrayTypeLoc TL) {
3419  VisitArrayTypeLoc(TL);
3420}
3421void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3422                                        DependentSizedExtVectorTypeLoc TL) {
3423  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3424}
3425void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
3426  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3427}
3428void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
3429  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3430}
3431void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3432  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3433  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3434  TL.setTrailingReturn(Record[Idx++]);
3435  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3436    TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
3437  }
3438}
3439void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3440  VisitFunctionTypeLoc(TL);
3441}
3442void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3443  VisitFunctionTypeLoc(TL);
3444}
3445void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
3446  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3447}
3448void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3449  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3450}
3451void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3452  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3453  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3454  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3455}
3456void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3457  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3458  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3459  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3460  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3461}
3462void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
3463  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3464}
3465void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3466  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3467}
3468void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
3469  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3470}
3471void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
3472  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3473}
3474void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3475  TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3476  if (TL.hasAttrOperand()) {
3477    SourceRange range;
3478    range.setBegin(ReadSourceLocation(Record, Idx));
3479    range.setEnd(ReadSourceLocation(Record, Idx));
3480    TL.setAttrOperandParensRange(range);
3481  }
3482  if (TL.hasAttrExprOperand()) {
3483    if (Record[Idx++])
3484      TL.setAttrExprOperand(Reader.ReadExpr(F));
3485    else
3486      TL.setAttrExprOperand(0);
3487  } else if (TL.hasAttrEnumOperand())
3488    TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3489}
3490void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
3491  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3492}
3493void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3494                                            SubstTemplateTypeParmTypeLoc TL) {
3495  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3496}
3497void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3498                                          SubstTemplateTypeParmPackTypeLoc TL) {
3499  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3500}
3501void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3502                                           TemplateSpecializationTypeLoc TL) {
3503  TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3504  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3505  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3506  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3507    TL.setArgLocInfo(i,
3508        Reader.GetTemplateArgumentLocInfo(F,
3509                                          TL.getTypePtr()->getArg(i).getKind(),
3510                                          Record, Idx));
3511}
3512void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3513  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3514  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3515}
3516void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3517  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3518  TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3519}
3520void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3521  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3522}
3523void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
3524  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3525  TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3526  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3527}
3528void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3529       DependentTemplateSpecializationTypeLoc TL) {
3530  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3531  TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3532  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3533  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3534  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3535  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3536    TL.setArgLocInfo(I,
3537        Reader.GetTemplateArgumentLocInfo(F,
3538                                          TL.getTypePtr()->getArg(I).getKind(),
3539                                          Record, Idx));
3540}
3541void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3542  TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3543}
3544void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3545  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3546}
3547void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3548  TL.setHasBaseTypeAsWritten(Record[Idx++]);
3549  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3550  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3551  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3552    TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
3553}
3554void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3555  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3556}
3557
3558TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
3559                                             const RecordData &Record,
3560                                             unsigned &Idx) {
3561  QualType InfoTy = GetType(Record[Idx++]);
3562  if (InfoTy.isNull())
3563    return 0;
3564
3565  TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
3566  TypeLocReader TLR(*this, F, Record, Idx);
3567  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
3568    TLR.Visit(TL);
3569  return TInfo;
3570}
3571
3572QualType ASTReader::GetType(TypeID ID) {
3573  unsigned FastQuals = ID & Qualifiers::FastMask;
3574  unsigned Index = ID >> Qualifiers::FastWidth;
3575
3576  if (Index < NUM_PREDEF_TYPE_IDS) {
3577    QualType T;
3578    switch ((PredefinedTypeIDs)Index) {
3579    case PREDEF_TYPE_NULL_ID: return QualType();
3580    case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3581    case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
3582
3583    case PREDEF_TYPE_CHAR_U_ID:
3584    case PREDEF_TYPE_CHAR_S_ID:
3585      // FIXME: Check that the signedness of CharTy is correct!
3586      T = Context->CharTy;
3587      break;
3588
3589    case PREDEF_TYPE_UCHAR_ID:      T = Context->UnsignedCharTy;     break;
3590    case PREDEF_TYPE_USHORT_ID:     T = Context->UnsignedShortTy;    break;
3591    case PREDEF_TYPE_UINT_ID:       T = Context->UnsignedIntTy;      break;
3592    case PREDEF_TYPE_ULONG_ID:      T = Context->UnsignedLongTy;     break;
3593    case PREDEF_TYPE_ULONGLONG_ID:  T = Context->UnsignedLongLongTy; break;
3594    case PREDEF_TYPE_UINT128_ID:    T = Context->UnsignedInt128Ty;   break;
3595    case PREDEF_TYPE_SCHAR_ID:      T = Context->SignedCharTy;       break;
3596    case PREDEF_TYPE_WCHAR_ID:      T = Context->WCharTy;            break;
3597    case PREDEF_TYPE_SHORT_ID:      T = Context->ShortTy;            break;
3598    case PREDEF_TYPE_INT_ID:        T = Context->IntTy;              break;
3599    case PREDEF_TYPE_LONG_ID:       T = Context->LongTy;             break;
3600    case PREDEF_TYPE_LONGLONG_ID:   T = Context->LongLongTy;         break;
3601    case PREDEF_TYPE_INT128_ID:     T = Context->Int128Ty;           break;
3602    case PREDEF_TYPE_FLOAT_ID:      T = Context->FloatTy;            break;
3603    case PREDEF_TYPE_DOUBLE_ID:     T = Context->DoubleTy;           break;
3604    case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy;       break;
3605    case PREDEF_TYPE_OVERLOAD_ID:   T = Context->OverloadTy;         break;
3606    case PREDEF_TYPE_DEPENDENT_ID:  T = Context->DependentTy;        break;
3607    case PREDEF_TYPE_NULLPTR_ID:    T = Context->NullPtrTy;          break;
3608    case PREDEF_TYPE_CHAR16_ID:     T = Context->Char16Ty;           break;
3609    case PREDEF_TYPE_CHAR32_ID:     T = Context->Char32Ty;           break;
3610    case PREDEF_TYPE_OBJC_ID:       T = Context->ObjCBuiltinIdTy;    break;
3611    case PREDEF_TYPE_OBJC_CLASS:    T = Context->ObjCBuiltinClassTy; break;
3612    case PREDEF_TYPE_OBJC_SEL:      T = Context->ObjCBuiltinSelTy;   break;
3613    }
3614
3615    assert(!T.isNull() && "Unknown predefined type");
3616    return T.withFastQualifiers(FastQuals);
3617  }
3618
3619  Index -= NUM_PREDEF_TYPE_IDS;
3620  assert(Index < TypesLoaded.size() && "Type index out-of-range");
3621  if (TypesLoaded[Index].isNull()) {
3622    TypesLoaded[Index] = ReadTypeRecord(Index);
3623    if (TypesLoaded[Index].isNull())
3624      return QualType();
3625
3626    TypesLoaded[Index]->setFromAST();
3627    TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
3628    if (DeserializationListener)
3629      DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
3630                                        TypesLoaded[Index]);
3631  }
3632
3633  return TypesLoaded[Index].withFastQualifiers(FastQuals);
3634}
3635
3636TypeID ASTReader::GetTypeID(QualType T) const {
3637  return MakeTypeID(T,
3638              std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3639}
3640
3641TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3642  if (T.isNull())
3643    return TypeIdx();
3644  assert(!T.getLocalFastQualifiers());
3645
3646  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3647  // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3648  // comparing keys of ASTDeclContextNameLookupTable.
3649  // If the type didn't come from the AST file use a specially marked index
3650  // so that any hash/key comparison fail since no such index is stored
3651  // in a AST file.
3652  if (I == TypeIdxs.end())
3653    return TypeIdx(-1);
3654  return I->second;
3655}
3656
3657unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3658  unsigned Result = 0;
3659  for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3660    Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3661
3662  return Result;
3663}
3664
3665TemplateArgumentLocInfo
3666ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3667                                      TemplateArgument::ArgKind Kind,
3668                                      const RecordData &Record,
3669                                      unsigned &Index) {
3670  switch (Kind) {
3671  case TemplateArgument::Expression:
3672    return ReadExpr(F);
3673  case TemplateArgument::Type:
3674    return GetTypeSourceInfo(F, Record, Index);
3675  case TemplateArgument::Template: {
3676    SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3677    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3678    return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3679                                   SourceLocation());
3680  }
3681  case TemplateArgument::TemplateExpansion: {
3682    SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3683    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3684    SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3685    return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3686                                   EllipsisLoc);
3687  }
3688  case TemplateArgument::Null:
3689  case TemplateArgument::Integral:
3690  case TemplateArgument::Declaration:
3691  case TemplateArgument::Pack:
3692    return TemplateArgumentLocInfo();
3693  }
3694  llvm_unreachable("unexpected template argument loc");
3695  return TemplateArgumentLocInfo();
3696}
3697
3698TemplateArgumentLoc
3699ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
3700                                   const RecordData &Record, unsigned &Index) {
3701  TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
3702
3703  if (Arg.getKind() == TemplateArgument::Expression) {
3704    if (Record[Index++]) // bool InfoHasSameExpr.
3705      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3706  }
3707  return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
3708                                                             Record, Index));
3709}
3710
3711Decl *ASTReader::GetExternalDecl(uint32_t ID) {
3712  return GetDecl(ID);
3713}
3714
3715uint64_t
3716ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3717  if (ID == 0)
3718    return 0;
3719
3720  --ID;
3721  uint64_t Offset = 0;
3722  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3723    if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3724      return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3725
3726    ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3727    Offset += Chain[I]->SizeInBits;
3728  }
3729
3730  assert(false && "CXXBaseSpecifiers not found");
3731  return 0;
3732}
3733
3734CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3735  // Figure out which AST file contains this offset.
3736  PerFileData *F = 0;
3737  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3738    if (Offset < Chain[I]->SizeInBits) {
3739      F = Chain[I];
3740      break;
3741    }
3742
3743    Offset -= Chain[I]->SizeInBits;
3744  }
3745
3746  if (!F) {
3747    Error("Malformed AST file: C++ base specifiers at impossible offset");
3748    return 0;
3749  }
3750
3751  llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3752  SavedStreamPosition SavedPosition(Cursor);
3753  Cursor.JumpToBit(Offset);
3754  ReadingKindTracker ReadingKind(Read_Decl, *this);
3755  RecordData Record;
3756  unsigned Code = Cursor.ReadCode();
3757  unsigned RecCode = Cursor.ReadRecord(Code, Record);
3758  if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3759    Error("Malformed AST file: missing C++ base specifiers");
3760    return 0;
3761  }
3762
3763  unsigned Idx = 0;
3764  unsigned NumBases = Record[Idx++];
3765  void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3766  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3767  for (unsigned I = 0; I != NumBases; ++I)
3768    Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3769  return Bases;
3770}
3771
3772TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
3773  if (!DeclsLoaded[0]) {
3774    ReadDeclRecord(0, 1);
3775    if (DeserializationListener)
3776      DeserializationListener->DeclRead(1, DeclsLoaded[0]);
3777  }
3778
3779  return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3780}
3781
3782Decl *ASTReader::GetDecl(DeclID ID) {
3783  if (ID == 0)
3784    return 0;
3785
3786  if (ID > DeclsLoaded.size()) {
3787    Error("declaration ID out-of-range for AST file");
3788    return 0;
3789  }
3790
3791  unsigned Index = ID - 1;
3792  if (!DeclsLoaded[Index]) {
3793    ReadDeclRecord(Index, ID);
3794    if (DeserializationListener)
3795      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3796  }
3797
3798  return DeclsLoaded[Index];
3799}
3800
3801/// \brief Resolve the offset of a statement into a statement.
3802///
3803/// This operation will read a new statement from the external
3804/// source each time it is called, and is meant to be used via a
3805/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
3806Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
3807  // Switch case IDs are per Decl.
3808  ClearSwitchCaseIDs();
3809
3810  // Offset here is a global offset across the entire chain.
3811  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3812    PerFileData &F = *Chain[N - I - 1];
3813    if (Offset < F.SizeInBits) {
3814      // Since we know that this statement is part of a decl, make sure to use
3815      // the decl cursor to read it.
3816      F.DeclsCursor.JumpToBit(Offset);
3817      return ReadStmtFromStream(F);
3818    }
3819    Offset -= F.SizeInBits;
3820  }
3821  llvm_unreachable("Broken chain");
3822}
3823
3824bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
3825                                         bool (*isKindWeWant)(Decl::Kind),
3826                                         llvm::SmallVectorImpl<Decl*> &Decls) {
3827  assert(DC->hasExternalLexicalStorage() &&
3828         "DeclContext has no lexical decls in storage");
3829
3830  // There might be lexical decls in multiple parts of the chain, for the TU
3831  // at least.
3832  // DeclContextOffsets might reallocate as we load additional decls below,
3833  // so make a copy of the vector.
3834  DeclContextInfos Infos = DeclContextOffsets[DC];
3835  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3836       I != E; ++I) {
3837    // IDs can be 0 if this context doesn't contain declarations.
3838    if (!I->LexicalDecls)
3839      continue;
3840
3841    // Load all of the declaration IDs
3842    for (const KindDeclIDPair *ID = I->LexicalDecls,
3843                              *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3844      if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3845        continue;
3846
3847      Decl *D = GetDecl(ID->second);
3848      assert(D && "Null decl in lexical decls");
3849      Decls.push_back(D);
3850    }
3851  }
3852
3853  ++NumLexicalDeclContextsRead;
3854  return false;
3855}
3856
3857DeclContext::lookup_result
3858ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
3859                                          DeclarationName Name) {
3860  assert(DC->hasExternalVisibleStorage() &&
3861         "DeclContext has no visible decls in storage");
3862  if (!Name)
3863    return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3864                                      DeclContext::lookup_iterator(0));
3865
3866  llvm::SmallVector<NamedDecl *, 64> Decls;
3867  // There might be visible decls in multiple parts of the chain, for the TU
3868  // and namespaces. For any given name, the last available results replace
3869  // all earlier ones. For this reason, we walk in reverse.
3870  DeclContextInfos &Infos = DeclContextOffsets[DC];
3871  for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
3872       I != E; ++I) {
3873    if (!I->NameLookupTableData)
3874      continue;
3875
3876    ASTDeclContextNameLookupTable *LookupTable =
3877        (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3878    ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3879    if (Pos == LookupTable->end())
3880      continue;
3881
3882    ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3883    for (; Data.first != Data.second; ++Data.first)
3884      Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3885    break;
3886  }
3887
3888  ++NumVisibleDeclContextsRead;
3889
3890  SetExternalVisibleDeclsForName(DC, Name, Decls);
3891  return const_cast<DeclContext*>(DC)->lookup(Name);
3892}
3893
3894void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3895  assert(DC->hasExternalVisibleStorage() &&
3896         "DeclContext has no visible decls in storage");
3897
3898  llvm::SmallVector<NamedDecl *, 64> Decls;
3899  // There might be visible decls in multiple parts of the chain, for the TU
3900  // and namespaces.
3901  DeclContextInfos &Infos = DeclContextOffsets[DC];
3902  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3903       I != E; ++I) {
3904    if (!I->NameLookupTableData)
3905      continue;
3906
3907    ASTDeclContextNameLookupTable *LookupTable =
3908        (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3909    for (ASTDeclContextNameLookupTable::item_iterator
3910           ItemI = LookupTable->item_begin(),
3911           ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3912      ASTDeclContextNameLookupTable::item_iterator::value_type Val
3913          = *ItemI;
3914      ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3915      Decls.clear();
3916      for (; Data.first != Data.second; ++Data.first)
3917        Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3918      MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3919    }
3920  }
3921}
3922
3923void ASTReader::PassInterestingDeclsToConsumer() {
3924  assert(Consumer);
3925  while (!InterestingDecls.empty()) {
3926    DeclGroupRef DG(InterestingDecls.front());
3927    InterestingDecls.pop_front();
3928    Consumer->HandleInterestingDecl(DG);
3929  }
3930}
3931
3932void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
3933  this->Consumer = Consumer;
3934
3935  if (!Consumer)
3936    return;
3937
3938  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
3939    // Force deserialization of this decl, which will cause it to be queued for
3940    // passing to the consumer.
3941    GetDecl(ExternalDefinitions[I]);
3942  }
3943
3944  PassInterestingDeclsToConsumer();
3945}
3946
3947void ASTReader::PrintStats() {
3948  std::fprintf(stderr, "*** AST File Statistics:\n");
3949
3950  unsigned NumTypesLoaded
3951    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
3952                                      QualType());
3953  unsigned NumDeclsLoaded
3954    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3955                                      (Decl *)0);
3956  unsigned NumIdentifiersLoaded
3957    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3958                                            IdentifiersLoaded.end(),
3959                                            (IdentifierInfo *)0);
3960  unsigned NumSelectorsLoaded
3961    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3962                                          SelectorsLoaded.end(),
3963                                          Selector());
3964
3965  std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
3966  std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
3967  if (TotalNumSLocEntries)
3968    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
3969                 NumSLocEntriesRead, TotalNumSLocEntries,
3970                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
3971  if (!TypesLoaded.empty())
3972    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
3973                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3974                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3975  if (!DeclsLoaded.empty())
3976    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
3977                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3978                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
3979  if (!IdentifiersLoaded.empty())
3980    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
3981                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3982                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
3983  if (!SelectorsLoaded.empty())
3984    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
3985                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3986                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
3987  if (TotalNumStatements)
3988    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
3989                 NumStatementsRead, TotalNumStatements,
3990                 ((float)NumStatementsRead/TotalNumStatements * 100));
3991  if (TotalNumMacros)
3992    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
3993                 NumMacrosRead, TotalNumMacros,
3994                 ((float)NumMacrosRead/TotalNumMacros * 100));
3995  if (TotalLexicalDeclContexts)
3996    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
3997                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3998                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3999                  * 100));
4000  if (TotalVisibleDeclContexts)
4001    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
4002                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4003                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4004                  * 100));
4005  if (TotalNumMethodPoolEntries) {
4006    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
4007                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4008                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4009                  * 100));
4010    std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
4011  }
4012  std::fprintf(stderr, "\n");
4013}
4014
4015void ASTReader::InitializeSema(Sema &S) {
4016  SemaObj = &S;
4017  S.ExternalSource = this;
4018
4019  // Makes sure any declarations that were deserialized "too early"
4020  // still get added to the identifier's declaration chains.
4021  for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4022    if (SemaObj->TUScope)
4023      SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4024
4025    SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4026  }
4027  PreloadedDecls.clear();
4028
4029  // If there were any tentative definitions, deserialize them and add
4030  // them to Sema's list of tentative definitions.
4031  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4032    VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
4033    SemaObj->TentativeDefinitions.push_back(Var);
4034  }
4035
4036  // If there were any unused file scoped decls, deserialize them and add to
4037  // Sema's list of unused file scoped decls.
4038  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4039    DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4040    SemaObj->UnusedFileScopedDecls.push_back(D);
4041  }
4042
4043  // If there were any locally-scoped external declarations,
4044  // deserialize them and add them to Sema's table of locally-scoped
4045  // external declarations.
4046  for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4047    NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4048    SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4049  }
4050
4051  // If there were any ext_vector type declarations, deserialize them
4052  // and add them to Sema's vector of such declarations.
4053  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4054    SemaObj->ExtVectorDecls.push_back(
4055                               cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
4056
4057  // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4058  // Can we cut them down before writing them ?
4059
4060  // If there were any dynamic classes declarations, deserialize them
4061  // and add them to Sema's vector of such declarations.
4062  for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4063    SemaObj->DynamicClasses.push_back(
4064                               cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
4065
4066  // Load the offsets of the declarations that Sema references.
4067  // They will be lazily deserialized when needed.
4068  if (!SemaDeclRefs.empty()) {
4069    assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4070    SemaObj->StdNamespace = SemaDeclRefs[0];
4071    SemaObj->StdBadAlloc = SemaDeclRefs[1];
4072  }
4073
4074  for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4075
4076    // If there are @selector references added them to its pool. This is for
4077    // implementation of -Wselector.
4078    if (!F->ReferencedSelectorsData.empty()) {
4079      unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4080      unsigned I = 0;
4081      while (I < DataSize) {
4082        Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4083        SourceLocation SelLoc = ReadSourceLocation(
4084                                    *F, F->ReferencedSelectorsData, I);
4085        SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4086      }
4087    }
4088
4089    // If there were any pending implicit instantiations, deserialize them
4090    // and add them to Sema's queue of such instantiations.
4091    assert(F->PendingInstantiations.size() % 2 == 0 &&
4092           "Expected pairs of entries");
4093    for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4094      ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4095      SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4096      SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4097    }
4098  }
4099
4100  // The two special data sets below always come from the most recent PCH,
4101  // which is at the front of the chain.
4102  PerFileData &F = *Chain.front();
4103
4104  // If there were any weak undeclared identifiers, deserialize them and add to
4105  // Sema's list of weak undeclared identifiers.
4106  if (!WeakUndeclaredIdentifiers.empty()) {
4107    unsigned Idx = 0;
4108    for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4109      IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4110      IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4111      SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4112      bool Used = WeakUndeclaredIdentifiers[Idx++];
4113      Sema::WeakInfo WI(AliasId, Loc);
4114      WI.setUsed(Used);
4115      SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4116    }
4117  }
4118
4119  // If there were any VTable uses, deserialize the information and add it
4120  // to Sema's vector and map of VTable uses.
4121  if (!VTableUses.empty()) {
4122    unsigned Idx = 0;
4123    for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4124      CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4125      SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4126      bool DefinitionRequired = VTableUses[Idx++];
4127      SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4128      SemaObj->VTablesUsed[Class] = DefinitionRequired;
4129    }
4130  }
4131
4132  if (!FPPragmaOptions.empty()) {
4133    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4134    SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4135  }
4136
4137  if (!OpenCLExtensions.empty()) {
4138    unsigned I = 0;
4139#define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4140#include "clang/Basic/OpenCLExtensions.def"
4141
4142    assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4143  }
4144}
4145
4146IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4147  // Try to find this name within our on-disk hash tables. We start with the
4148  // most recent one, since that one contains the most up-to-date info.
4149  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4150    ASTIdentifierLookupTable *IdTable
4151        = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
4152    if (!IdTable)
4153      continue;
4154    std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
4155    ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4156    if (Pos == IdTable->end())
4157      continue;
4158
4159    // Dereferencing the iterator has the effect of building the
4160    // IdentifierInfo node and populating it with the various
4161    // declarations it needs.
4162    return *Pos;
4163  }
4164  return 0;
4165}
4166
4167namespace clang {
4168  /// \brief An identifier-lookup iterator that enumerates all of the
4169  /// identifiers stored within a set of AST files.
4170  class ASTIdentifierIterator : public IdentifierIterator {
4171    /// \brief The AST reader whose identifiers are being enumerated.
4172    const ASTReader &Reader;
4173
4174    /// \brief The current index into the chain of AST files stored in
4175    /// the AST reader.
4176    unsigned Index;
4177
4178    /// \brief The current position within the identifier lookup table
4179    /// of the current AST file.
4180    ASTIdentifierLookupTable::key_iterator Current;
4181
4182    /// \brief The end position within the identifier lookup table of
4183    /// the current AST file.
4184    ASTIdentifierLookupTable::key_iterator End;
4185
4186  public:
4187    explicit ASTIdentifierIterator(const ASTReader &Reader);
4188
4189    virtual llvm::StringRef Next();
4190  };
4191}
4192
4193ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4194  : Reader(Reader), Index(Reader.Chain.size() - 1) {
4195  ASTIdentifierLookupTable *IdTable
4196    = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4197  Current = IdTable->key_begin();
4198  End = IdTable->key_end();
4199}
4200
4201llvm::StringRef ASTIdentifierIterator::Next() {
4202  while (Current == End) {
4203    // If we have exhausted all of our AST files, we're done.
4204    if (Index == 0)
4205      return llvm::StringRef();
4206
4207    --Index;
4208    ASTIdentifierLookupTable *IdTable
4209      = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4210    Current = IdTable->key_begin();
4211    End = IdTable->key_end();
4212  }
4213
4214  // We have any identifiers remaining in the current AST file; return
4215  // the next one.
4216  std::pair<const char*, unsigned> Key = *Current;
4217  ++Current;
4218  return llvm::StringRef(Key.first, Key.second);
4219}
4220
4221IdentifierIterator *ASTReader::getIdentifiers() const {
4222  return new ASTIdentifierIterator(*this);
4223}
4224
4225std::pair<ObjCMethodList, ObjCMethodList>
4226ASTReader::ReadMethodPool(Selector Sel) {
4227  // Find this selector in a hash table. We want to find the most recent entry.
4228  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4229    PerFileData &F = *Chain[I];
4230    if (!F.SelectorLookupTable)
4231      continue;
4232
4233    ASTSelectorLookupTable *PoolTable
4234      = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4235    ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
4236    if (Pos != PoolTable->end()) {
4237      ++NumSelectorsRead;
4238      // FIXME: Not quite happy with the statistics here. We probably should
4239      // disable this tracking when called via LoadSelector.
4240      // Also, should entries without methods count as misses?
4241      ++NumMethodPoolEntriesRead;
4242      ASTSelectorLookupTrait::data_type Data = *Pos;
4243      if (DeserializationListener)
4244        DeserializationListener->SelectorRead(Data.ID, Sel);
4245      return std::make_pair(Data.Instance, Data.Factory);
4246    }
4247  }
4248
4249  ++NumMethodPoolMisses;
4250  return std::pair<ObjCMethodList, ObjCMethodList>();
4251}
4252
4253void ASTReader::LoadSelector(Selector Sel) {
4254  // It would be complicated to avoid reading the methods anyway. So don't.
4255  ReadMethodPool(Sel);
4256}
4257
4258void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
4259  assert(ID && "Non-zero identifier ID required");
4260  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4261  IdentifiersLoaded[ID - 1] = II;
4262  if (DeserializationListener)
4263    DeserializationListener->IdentifierRead(ID, II);
4264}
4265
4266/// \brief Set the globally-visible declarations associated with the given
4267/// identifier.
4268///
4269/// If the AST reader is currently in a state where the given declaration IDs
4270/// cannot safely be resolved, they are queued until it is safe to resolve
4271/// them.
4272///
4273/// \param II an IdentifierInfo that refers to one or more globally-visible
4274/// declarations.
4275///
4276/// \param DeclIDs the set of declaration IDs with the name @p II that are
4277/// visible at global scope.
4278///
4279/// \param Nonrecursive should be true to indicate that the caller knows that
4280/// this call is non-recursive, and therefore the globally-visible declarations
4281/// will not be placed onto the pending queue.
4282void
4283ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4284                              const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4285                                   bool Nonrecursive) {
4286  if (NumCurrentElementsDeserializing && !Nonrecursive) {
4287    PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4288    PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4289    PII.II = II;
4290    PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4291    return;
4292  }
4293
4294  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4295    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4296    if (SemaObj) {
4297      if (SemaObj->TUScope) {
4298        // Introduce this declaration into the translation-unit scope
4299        // and add it to the declaration chain for this identifier, so
4300        // that (unqualified) name lookup will find it.
4301        SemaObj->TUScope->AddDecl(D);
4302      }
4303      SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4304    } else {
4305      // Queue this declaration so that it will be added to the
4306      // translation unit scope and identifier's declaration chain
4307      // once a Sema object is known.
4308      PreloadedDecls.push_back(D);
4309    }
4310  }
4311}
4312
4313IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
4314  if (ID == 0)
4315    return 0;
4316
4317  if (IdentifiersLoaded.empty()) {
4318    Error("no identifier table in AST file");
4319    return 0;
4320  }
4321
4322  assert(PP && "Forgot to set Preprocessor ?");
4323  ID -= 1;
4324  if (!IdentifiersLoaded[ID]) {
4325    unsigned Index = ID;
4326    const char *Str = 0;
4327    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4328      PerFileData *F = Chain[N - I - 1];
4329      if (Index < F->LocalNumIdentifiers) {
4330         uint32_t Offset = F->IdentifierOffsets[Index];
4331         Str = F->IdentifierTableData + Offset;
4332         break;
4333      }
4334      Index -= F->LocalNumIdentifiers;
4335    }
4336    assert(Str && "Broken Chain");
4337
4338    // All of the strings in the AST file are preceded by a 16-bit length.
4339    // Extract that 16-bit length to avoid having to execute strlen().
4340    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4341    //  unsigned integers.  This is important to avoid integer overflow when
4342    //  we cast them to 'unsigned'.
4343    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4344    unsigned StrLen = (((unsigned) StrLenPtr[0])
4345                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4346    IdentifiersLoaded[ID]
4347      = &PP->getIdentifierTable().get(Str, StrLen);
4348    if (DeserializationListener)
4349      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4350  }
4351
4352  return IdentifiersLoaded[ID];
4353}
4354
4355void ASTReader::ReadSLocEntry(unsigned ID) {
4356  ReadSLocEntryRecord(ID);
4357}
4358
4359Selector ASTReader::DecodeSelector(unsigned ID) {
4360  if (ID == 0)
4361    return Selector();
4362
4363  if (ID > SelectorsLoaded.size()) {
4364    Error("selector ID out of range in AST file");
4365    return Selector();
4366  }
4367
4368  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4369    // Load this selector from the selector table.
4370    unsigned Idx = ID - 1;
4371    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4372      PerFileData &F = *Chain[N - I - 1];
4373      if (Idx < F.LocalNumSelectors) {
4374        ASTSelectorLookupTrait Trait(*this);
4375        SelectorsLoaded[ID - 1] =
4376           Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4377        if (DeserializationListener)
4378          DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4379        break;
4380      }
4381      Idx -= F.LocalNumSelectors;
4382    }
4383  }
4384
4385  return SelectorsLoaded[ID - 1];
4386}
4387
4388Selector ASTReader::GetExternalSelector(uint32_t ID) {
4389  return DecodeSelector(ID);
4390}
4391
4392uint32_t ASTReader::GetNumExternalSelectors() {
4393  // ID 0 (the null selector) is considered an external selector.
4394  return getTotalNumSelectors() + 1;
4395}
4396
4397DeclarationName
4398ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
4399  DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4400  switch (Kind) {
4401  case DeclarationName::Identifier:
4402    return DeclarationName(GetIdentifierInfo(Record, Idx));
4403
4404  case DeclarationName::ObjCZeroArgSelector:
4405  case DeclarationName::ObjCOneArgSelector:
4406  case DeclarationName::ObjCMultiArgSelector:
4407    return DeclarationName(GetSelector(Record, Idx));
4408
4409  case DeclarationName::CXXConstructorName:
4410    return Context->DeclarationNames.getCXXConstructorName(
4411                          Context->getCanonicalType(GetType(Record[Idx++])));
4412
4413  case DeclarationName::CXXDestructorName:
4414    return Context->DeclarationNames.getCXXDestructorName(
4415                          Context->getCanonicalType(GetType(Record[Idx++])));
4416
4417  case DeclarationName::CXXConversionFunctionName:
4418    return Context->DeclarationNames.getCXXConversionFunctionName(
4419                          Context->getCanonicalType(GetType(Record[Idx++])));
4420
4421  case DeclarationName::CXXOperatorName:
4422    return Context->DeclarationNames.getCXXOperatorName(
4423                                       (OverloadedOperatorKind)Record[Idx++]);
4424
4425  case DeclarationName::CXXLiteralOperatorName:
4426    return Context->DeclarationNames.getCXXLiteralOperatorName(
4427                                       GetIdentifierInfo(Record, Idx));
4428
4429  case DeclarationName::CXXUsingDirective:
4430    return DeclarationName::getUsingDirectiveName();
4431  }
4432
4433  // Required to silence GCC warning
4434  return DeclarationName();
4435}
4436
4437void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4438                                       DeclarationNameLoc &DNLoc,
4439                                       DeclarationName Name,
4440                                      const RecordData &Record, unsigned &Idx) {
4441  switch (Name.getNameKind()) {
4442  case DeclarationName::CXXConstructorName:
4443  case DeclarationName::CXXDestructorName:
4444  case DeclarationName::CXXConversionFunctionName:
4445    DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4446    break;
4447
4448  case DeclarationName::CXXOperatorName:
4449    DNLoc.CXXOperatorName.BeginOpNameLoc
4450        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4451    DNLoc.CXXOperatorName.EndOpNameLoc
4452        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4453    break;
4454
4455  case DeclarationName::CXXLiteralOperatorName:
4456    DNLoc.CXXLiteralOperatorName.OpNameLoc
4457        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4458    break;
4459
4460  case DeclarationName::Identifier:
4461  case DeclarationName::ObjCZeroArgSelector:
4462  case DeclarationName::ObjCOneArgSelector:
4463  case DeclarationName::ObjCMultiArgSelector:
4464  case DeclarationName::CXXUsingDirective:
4465    break;
4466  }
4467}
4468
4469void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4470                                        DeclarationNameInfo &NameInfo,
4471                                      const RecordData &Record, unsigned &Idx) {
4472  NameInfo.setName(ReadDeclarationName(Record, Idx));
4473  NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4474  DeclarationNameLoc DNLoc;
4475  ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4476  NameInfo.setInfo(DNLoc);
4477}
4478
4479void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4480                                  const RecordData &Record, unsigned &Idx) {
4481  Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4482  Info.NNSRange = ReadSourceRange(F, Record, Idx);
4483  unsigned NumTPLists = Record[Idx++];
4484  Info.NumTemplParamLists = NumTPLists;
4485  if (NumTPLists) {
4486    Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4487    for (unsigned i=0; i != NumTPLists; ++i)
4488      Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4489  }
4490}
4491
4492TemplateName
4493ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4494                            unsigned &Idx) {
4495  TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
4496  switch (Kind) {
4497  case TemplateName::Template:
4498    return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4499
4500  case TemplateName::OverloadedTemplate: {
4501    unsigned size = Record[Idx++];
4502    UnresolvedSet<8> Decls;
4503    while (size--)
4504      Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4505
4506    return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4507  }
4508
4509  case TemplateName::QualifiedTemplate: {
4510    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4511    bool hasTemplKeyword = Record[Idx++];
4512    TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4513    return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4514  }
4515
4516  case TemplateName::DependentTemplate: {
4517    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4518    if (Record[Idx++])  // isIdentifier
4519      return Context->getDependentTemplateName(NNS,
4520                                               GetIdentifierInfo(Record, Idx));
4521    return Context->getDependentTemplateName(NNS,
4522                                         (OverloadedOperatorKind)Record[Idx++]);
4523  }
4524
4525  case TemplateName::SubstTemplateTemplateParmPack: {
4526    TemplateTemplateParmDecl *Param
4527      = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4528    if (!Param)
4529      return TemplateName();
4530
4531    TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4532    if (ArgPack.getKind() != TemplateArgument::Pack)
4533      return TemplateName();
4534
4535    return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4536  }
4537  }
4538
4539  assert(0 && "Unhandled template name kind!");
4540  return TemplateName();
4541}
4542
4543TemplateArgument
4544ASTReader::ReadTemplateArgument(PerFileData &F,
4545                                const RecordData &Record, unsigned &Idx) {
4546  TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4547  switch (Kind) {
4548  case TemplateArgument::Null:
4549    return TemplateArgument();
4550  case TemplateArgument::Type:
4551    return TemplateArgument(GetType(Record[Idx++]));
4552  case TemplateArgument::Declaration:
4553    return TemplateArgument(GetDecl(Record[Idx++]));
4554  case TemplateArgument::Integral: {
4555    llvm::APSInt Value = ReadAPSInt(Record, Idx);
4556    QualType T = GetType(Record[Idx++]);
4557    return TemplateArgument(Value, T);
4558  }
4559  case TemplateArgument::Template:
4560    return TemplateArgument(ReadTemplateName(F, Record, Idx));
4561  case TemplateArgument::TemplateExpansion: {
4562    TemplateName Name = ReadTemplateName(F, Record, Idx);
4563    llvm::Optional<unsigned> NumTemplateExpansions;
4564    if (unsigned NumExpansions = Record[Idx++])
4565      NumTemplateExpansions = NumExpansions - 1;
4566    return TemplateArgument(Name, NumTemplateExpansions);
4567  }
4568  case TemplateArgument::Expression:
4569    return TemplateArgument(ReadExpr(F));
4570  case TemplateArgument::Pack: {
4571    unsigned NumArgs = Record[Idx++];
4572    TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4573    for (unsigned I = 0; I != NumArgs; ++I)
4574      Args[I] = ReadTemplateArgument(F, Record, Idx);
4575    return TemplateArgument(Args, NumArgs);
4576  }
4577  }
4578
4579  assert(0 && "Unhandled template argument kind!");
4580  return TemplateArgument();
4581}
4582
4583TemplateParameterList *
4584ASTReader::ReadTemplateParameterList(PerFileData &F,
4585                                     const RecordData &Record, unsigned &Idx) {
4586  SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4587  SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4588  SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
4589
4590  unsigned NumParams = Record[Idx++];
4591  llvm::SmallVector<NamedDecl *, 16> Params;
4592  Params.reserve(NumParams);
4593  while (NumParams--)
4594    Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
4595
4596  TemplateParameterList* TemplateParams =
4597    TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4598                                  Params.data(), Params.size(), RAngleLoc);
4599  return TemplateParams;
4600}
4601
4602void
4603ASTReader::
4604ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
4605                         PerFileData &F, const RecordData &Record,
4606                         unsigned &Idx) {
4607  unsigned NumTemplateArgs = Record[Idx++];
4608  TemplArgs.reserve(NumTemplateArgs);
4609  while (NumTemplateArgs--)
4610    TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
4611}
4612
4613/// \brief Read a UnresolvedSet structure.
4614void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
4615                                  const RecordData &Record, unsigned &Idx) {
4616  unsigned NumDecls = Record[Idx++];
4617  while (NumDecls--) {
4618    NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4619    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4620    Set.addDecl(D, AS);
4621  }
4622}
4623
4624CXXBaseSpecifier
4625ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
4626                                const RecordData &Record, unsigned &Idx) {
4627  bool isVirtual = static_cast<bool>(Record[Idx++]);
4628  bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4629  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
4630  bool inheritConstructors = static_cast<bool>(Record[Idx++]);
4631  TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4632  SourceRange Range = ReadSourceRange(F, Record, Idx);
4633  SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4634  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
4635                          EllipsisLoc);
4636  Result.setInheritConstructors(inheritConstructors);
4637  return Result;
4638}
4639
4640std::pair<CXXCtorInitializer **, unsigned>
4641ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4642                                   unsigned &Idx) {
4643  CXXCtorInitializer **CtorInitializers = 0;
4644  unsigned NumInitializers = Record[Idx++];
4645  if (NumInitializers) {
4646    ASTContext &C = *getContext();
4647
4648    CtorInitializers
4649        = new (C) CXXCtorInitializer*[NumInitializers];
4650    for (unsigned i=0; i != NumInitializers; ++i) {
4651      TypeSourceInfo *BaseClassInfo = 0;
4652      bool IsBaseVirtual = false;
4653      FieldDecl *Member = 0;
4654      IndirectFieldDecl *IndirectMember = 0;
4655
4656      bool IsBaseInitializer = Record[Idx++];
4657      if (IsBaseInitializer) {
4658        BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
4659        IsBaseVirtual = Record[Idx++];
4660      } else {
4661        bool IsIndirectMemberInitializer = Record[Idx++];
4662        if (IsIndirectMemberInitializer)
4663          IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4664        else
4665          Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4666      }
4667      SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
4668      Expr *Init = ReadExpr(F);
4669      SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4670      SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
4671      bool IsWritten = Record[Idx++];
4672      unsigned SourceOrderOrNumArrayIndices;
4673      llvm::SmallVector<VarDecl *, 8> Indices;
4674      if (IsWritten) {
4675        SourceOrderOrNumArrayIndices = Record[Idx++];
4676      } else {
4677        SourceOrderOrNumArrayIndices = Record[Idx++];
4678        Indices.reserve(SourceOrderOrNumArrayIndices);
4679        for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4680          Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4681      }
4682
4683      CXXCtorInitializer *BOMInit;
4684      if (IsBaseInitializer) {
4685        BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4686                                             LParenLoc, Init, RParenLoc,
4687                                             MemberOrEllipsisLoc);
4688      } else if (IsWritten) {
4689        if (Member)
4690          BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4691                                               LParenLoc, Init, RParenLoc);
4692        else
4693          BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4694                                               MemberOrEllipsisLoc, LParenLoc,
4695                                               Init, RParenLoc);
4696      } else {
4697        BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4698                                             LParenLoc, Init, RParenLoc,
4699                                             Indices.data(), Indices.size());
4700      }
4701
4702      if (IsWritten)
4703        BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
4704      CtorInitializers[i] = BOMInit;
4705    }
4706  }
4707
4708  return std::make_pair(CtorInitializers, NumInitializers);
4709}
4710
4711NestedNameSpecifier *
4712ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
4713  unsigned N = Record[Idx++];
4714  NestedNameSpecifier *NNS = 0, *Prev = 0;
4715  for (unsigned I = 0; I != N; ++I) {
4716    NestedNameSpecifier::SpecifierKind Kind
4717      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4718    switch (Kind) {
4719    case NestedNameSpecifier::Identifier: {
4720      IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4721      NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4722      break;
4723    }
4724
4725    case NestedNameSpecifier::Namespace: {
4726      NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4727      NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4728      break;
4729    }
4730
4731    case NestedNameSpecifier::TypeSpec:
4732    case NestedNameSpecifier::TypeSpecWithTemplate: {
4733      const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
4734      if (!T)
4735        return 0;
4736
4737      bool Template = Record[Idx++];
4738      NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4739      break;
4740    }
4741
4742    case NestedNameSpecifier::Global: {
4743      NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4744      // No associated value, and there can't be a prefix.
4745      break;
4746    }
4747    }
4748    Prev = NNS;
4749  }
4750  return NNS;
4751}
4752
4753SourceRange
4754ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4755                           unsigned &Idx) {
4756  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4757  SourceLocation end = ReadSourceLocation(F, Record, Idx);
4758  return SourceRange(beg, end);
4759}
4760
4761/// \brief Read an integral value
4762llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
4763  unsigned BitWidth = Record[Idx++];
4764  unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4765  llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4766  Idx += NumWords;
4767  return Result;
4768}
4769
4770/// \brief Read a signed integral value
4771llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
4772  bool isUnsigned = Record[Idx++];
4773  return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4774}
4775
4776/// \brief Read a floating-point value
4777llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
4778  return llvm::APFloat(ReadAPInt(Record, Idx));
4779}
4780
4781// \brief Read a string
4782std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
4783  unsigned Len = Record[Idx++];
4784  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
4785  Idx += Len;
4786  return Result;
4787}
4788
4789CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
4790                                          unsigned &Idx) {
4791  CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4792  return CXXTemporary::Create(*Context, Decl);
4793}
4794
4795DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
4796  return Diag(SourceLocation(), DiagID);
4797}
4798
4799DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
4800  return Diags.Report(Loc, DiagID);
4801}
4802
4803/// \brief Retrieve the identifier table associated with the
4804/// preprocessor.
4805IdentifierTable &ASTReader::getIdentifierTable() {
4806  assert(PP && "Forgot to set Preprocessor ?");
4807  return PP->getIdentifierTable();
4808}
4809
4810/// \brief Record that the given ID maps to the given switch-case
4811/// statement.
4812void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
4813  assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4814  SwitchCaseStmts[ID] = SC;
4815}
4816
4817/// \brief Retrieve the switch-case statement with the given ID.
4818SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
4819  assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4820  return SwitchCaseStmts[ID];
4821}
4822
4823void ASTReader::ClearSwitchCaseIDs() {
4824  SwitchCaseStmts.clear();
4825}
4826
4827void ASTReader::FinishedDeserializing() {
4828  assert(NumCurrentElementsDeserializing &&
4829         "FinishedDeserializing not paired with StartedDeserializing");
4830  if (NumCurrentElementsDeserializing == 1) {
4831    // If any identifiers with corresponding top-level declarations have
4832    // been loaded, load those declarations now.
4833    while (!PendingIdentifierInfos.empty()) {
4834      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4835                              PendingIdentifierInfos.front().DeclIDs, true);
4836      PendingIdentifierInfos.pop_front();
4837    }
4838
4839    // Ready to load previous declarations of Decls that were delayed.
4840    while (!PendingPreviousDecls.empty()) {
4841      loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4842                                PendingPreviousDecls.front().second);
4843      PendingPreviousDecls.pop_front();
4844    }
4845
4846    // We are not in recursive loading, so it's safe to pass the "interesting"
4847    // decls to the consumer.
4848    if (Consumer)
4849      PassInterestingDeclsToConsumer();
4850
4851    assert(PendingForwardRefs.size() == 0 &&
4852           "Some forward refs did not get linked to the definition!");
4853  }
4854  --NumCurrentElementsDeserializing;
4855}
4856
4857ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4858                     const char *isysroot, bool DisableValidation,
4859                     bool DisableStatCache)
4860  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4861    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4862    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4863    Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4864    DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4865    NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4866    NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4867    TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4868    NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4869    NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4870    NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4871    NumCurrentElementsDeserializing(0)
4872{
4873  RelocatablePCH = false;
4874}
4875
4876ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4877                     Diagnostic &Diags, const char *isysroot,
4878                     bool DisableValidation, bool DisableStatCache)
4879  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4880    Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4881    isysroot(isysroot), DisableValidation(DisableValidation),
4882    DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4883    NumSLocEntriesRead(0), TotalNumSLocEntries(0),
4884    NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4885    NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4886    NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4887    TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4888    TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4889    TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4890  RelocatablePCH = false;
4891}
4892
4893ASTReader::~ASTReader() {
4894  for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4895    delete Chain[e - i - 1];
4896  // Delete all visible decl lookup tables
4897  for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4898                                       E = DeclContextOffsets.end();
4899       I != E; ++I) {
4900    for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4901         J != F; ++J) {
4902      if (J->NameLookupTableData)
4903        delete static_cast<ASTDeclContextNameLookupTable*>(
4904            J->NameLookupTableData);
4905    }
4906  }
4907  for (DeclContextVisibleUpdatesPending::iterator
4908           I = PendingVisibleUpdates.begin(),
4909           E = PendingVisibleUpdates.end();
4910       I != E; ++I) {
4911    for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4912                                             F = I->second.end();
4913         J != F; ++J)
4914      delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4915  }
4916}
4917
4918ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4919  : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
4920    LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4921    IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4922    MacroDefinitionOffsets(0),
4923    LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4924    HeaderFileInfoTable(0),
4925    LocalNumSelectors(0), SelectorOffsets(0),
4926    SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4927    DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4928    LocalNumTypes(0), TypeOffsets(0), StatCache(0),
4929    NumPreallocatedPreprocessingEntities(0), NextInSource(0)
4930{}
4931
4932ASTReader::PerFileData::~PerFileData() {
4933  delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4934  delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
4935  delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4936}
4937
4938