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