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