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