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