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