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