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