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