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