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