ASTReader.cpp revision f249bf3b9f8bd3af711ffe9de411fc435871a4f6
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
2543ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2544                                            ModuleKind Type) {
2545  switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
2546  case Failure: return Failure;
2547  case IgnorePCH: return IgnorePCH;
2548  case Success: break;
2549  }
2550
2551  // Here comes stuff that we only do once the entire chain is loaded.
2552
2553  // Check the predefines buffers.
2554  if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
2555    return IgnorePCH;
2556
2557  if (PP) {
2558    // Initialization of keywords and pragmas occurs before the
2559    // AST file is read, so there may be some identifiers that were
2560    // loaded into the IdentifierTable before we intercepted the
2561    // creation of identifiers. Iterate through the list of known
2562    // identifiers and determine whether we have to establish
2563    // preprocessor definitions or top-level identifier declaration
2564    // chains for those identifiers.
2565    //
2566    // We copy the IdentifierInfo pointers to a small vector first,
2567    // since de-serializing declarations or macro definitions can add
2568    // new entries into the identifier table, invalidating the
2569    // iterators.
2570    //
2571    // FIXME: We need a lazier way to load this information, e.g., by marking
2572    // the identifier data as 'dirty', so that it will be looked up in the
2573    // AST file(s) if it is uttered in the source. This could save us some
2574    // module load time.
2575    SmallVector<IdentifierInfo *, 128> Identifiers;
2576    for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2577                                IdEnd = PP->getIdentifierTable().end();
2578         Id != IdEnd; ++Id)
2579      Identifiers.push_back(Id->second);
2580    // We need to search the tables in all files.
2581    for (ModuleIterator J = ModuleMgr.begin(),
2582        M = ModuleMgr.end(); J != M; ++J) {
2583      ASTIdentifierLookupTable *IdTable
2584        = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable;
2585      // Not all AST files necessarily have identifier tables, only the useful
2586      // ones.
2587      if (!IdTable)
2588        continue;
2589      for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2590        IdentifierInfo *II = Identifiers[I];
2591        // Look in the on-disk hash tables for an entry for this identifier
2592        ASTIdentifierLookupTrait Info(*this, *(*J), II);
2593        std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
2594        ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2595        if (Pos == IdTable->end())
2596          continue;
2597
2598        // Dereferencing the iterator has the effect of populating the
2599        // IdentifierInfo node with the various declarations it needs.
2600        (void)*Pos;
2601      }
2602    }
2603  }
2604
2605  if (Context)
2606    InitializeContext(*Context);
2607
2608  if (DeserializationListener)
2609    DeserializationListener->ReaderInitialized(this);
2610
2611  // If this AST file is a precompiled preamble, then set the main file ID of
2612  // the source manager to the file source file from which the preamble was
2613  // built. This is the only valid way to use a precompiled preamble.
2614  if (Type == MK_Preamble) {
2615    if (OriginalFileID.isInvalid()) {
2616      SourceLocation Loc
2617        = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2618      if (Loc.isValid())
2619        OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
2620    }
2621    else {
2622      OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2623                                        + OriginalFileID.getOpaqueValue() - 1);
2624    }
2625
2626    if (!OriginalFileID.isInvalid())
2627      SourceMgr.SetPreambleFileID(OriginalFileID);
2628  }
2629
2630  return Success;
2631}
2632
2633ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
2634                                                ModuleKind Type,
2635                                                Module *ImportedBy) {
2636  Module *M;
2637  bool NewModule;
2638  std::string ErrorStr;
2639  llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2640                                                ErrorStr);
2641
2642  if (!M) {
2643    // We couldn't load the module.
2644    std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2645      + ErrorStr;
2646    Error(Msg);
2647    return Failure;
2648  }
2649
2650  if (!NewModule) {
2651    // We've already loaded this module.
2652    return Success;
2653  }
2654
2655  // FIXME: This seems rather a hack. Should CurrentDir be part of the
2656  // module?
2657  if (FileName != "-") {
2658    CurrentDir = llvm::sys::path::parent_path(FileName);
2659    if (CurrentDir.empty()) CurrentDir = ".";
2660  }
2661
2662  Module &F = *M;
2663  llvm::BitstreamCursor &Stream = F.Stream;
2664  Stream.init(F.StreamFile);
2665  F.SizeInBits = F.Buffer->getBufferSize() * 8;
2666
2667  // Sniff for the signature.
2668  if (Stream.Read(8) != 'C' ||
2669      Stream.Read(8) != 'P' ||
2670      Stream.Read(8) != 'C' ||
2671      Stream.Read(8) != 'H') {
2672    Diag(diag::err_not_a_pch_file) << FileName;
2673    return Failure;
2674  }
2675
2676  while (!Stream.AtEndOfStream()) {
2677    unsigned Code = Stream.ReadCode();
2678
2679    if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2680      Error("invalid record at top-level of AST file");
2681      return Failure;
2682    }
2683
2684    unsigned BlockID = Stream.ReadSubBlockID();
2685
2686    // We only know the AST subblock ID.
2687    switch (BlockID) {
2688    case llvm::bitc::BLOCKINFO_BLOCK_ID:
2689      if (Stream.ReadBlockInfoBlock()) {
2690        Error("malformed BlockInfoBlock in AST file");
2691        return Failure;
2692      }
2693      break;
2694    case AST_BLOCK_ID:
2695      switch (ReadASTBlock(F)) {
2696      case Success:
2697        break;
2698
2699      case Failure:
2700        return Failure;
2701
2702      case IgnorePCH:
2703        // FIXME: We could consider reading through to the end of this
2704        // AST block, skipping subblocks, to see if there are other
2705        // AST blocks elsewhere.
2706
2707        // FIXME: We can't clear loaded slocentries anymore.
2708        //SourceMgr.ClearPreallocatedSLocEntries();
2709
2710        // Remove the stat cache.
2711        if (F.StatCache)
2712          FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
2713
2714        return IgnorePCH;
2715      }
2716      break;
2717    default:
2718      if (Stream.SkipBlock()) {
2719        Error("malformed block record in AST file");
2720        return Failure;
2721      }
2722      break;
2723    }
2724  }
2725
2726  // Once read, set the Module bit base offset and update the size in
2727  // bits of all files we've seen.
2728  F.GlobalBitOffset = TotalModulesSizeInBits;
2729  TotalModulesSizeInBits += F.SizeInBits;
2730  GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2731
2732  // Make sure that the files this module was built against are still available.
2733  if (!DisableValidation) {
2734    switch(validateFileEntries(*M)) {
2735    case Failure: return Failure;
2736    case IgnorePCH: return IgnorePCH;
2737    case Success: break;
2738    }
2739  }
2740
2741  // Preload SLocEntries.
2742  for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2743    int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2744    ASTReadResult Result = ReadSLocEntryRecord(Index);
2745    if (Result != Success)
2746      return Failure;
2747  }
2748
2749
2750  return Success;
2751}
2752
2753void ASTReader::setPreprocessor(Preprocessor &pp) {
2754  PP = &pp;
2755
2756  if (unsigned N = getTotalNumPreprocessedEntities()) {
2757    if (!PP->getPreprocessingRecord())
2758      PP->createPreprocessingRecord(true);
2759    PP->getPreprocessingRecord()->SetExternalSource(*this);
2760    PP->getPreprocessingRecord()->allocateLoadedEntities(N);
2761  }
2762
2763  PP->getHeaderSearchInfo().SetExternalLookup(this);
2764  PP->getHeaderSearchInfo().SetExternalSource(this);
2765}
2766
2767void ASTReader::InitializeContext(ASTContext &Ctx) {
2768  Context = &Ctx;
2769  assert(Context && "Passed null context!");
2770
2771  assert(PP && "Forgot to set Preprocessor ?");
2772  PP->getIdentifierTable().setExternalIdentifierLookup(this);
2773  PP->setExternalSource(this);
2774
2775  // If we have any update blocks for the TU waiting, we have to add
2776  // them before we deserialize anything.
2777  TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
2778  for (ModuleIterator M = ModuleMgr.begin(), MEnd = ModuleMgr.end();
2779       M != MEnd; ++M) {
2780    Module::DeclContextInfosMap::iterator DCU
2781      = (*M)->DeclContextInfos.find(0);
2782    if (DCU != (*M)->DeclContextInfos.end()) {
2783      // Insertion could invalidate map, so grab value first.
2784      DeclContextInfo Info = DCU->second;
2785      (*M)->DeclContextInfos.erase(DCU);
2786      (*M)->DeclContextInfos[TU] = Info;
2787    }
2788  }
2789
2790  // If there's a listener, notify them that we "read" the translation unit.
2791  if (DeserializationListener)
2792    DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2793
2794  // Make sure we load the declaration update records for the translation unit,
2795  // if there are any.
2796  loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2797
2798  // Note that the translation unit has external lexical and visible storage.
2799  TU->setHasExternalLexicalStorage(true);
2800  TU->setHasExternalVisibleStorage(true);
2801
2802  // FIXME: Find a better way to deal with collisions between these
2803  // built-in types. Right now, we just ignore the problem.
2804
2805  // Load the special types.
2806  if (Context->getBuiltinVaListType().isNull()) {
2807    Context->setBuiltinVaListType(
2808      GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2809  }
2810
2811  if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2812    if (Context->ObjCProtoType.isNull())
2813      Context->ObjCProtoType = GetType(Proto);
2814  }
2815
2816  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2817    if (!Context->CFConstantStringTypeDecl)
2818      Context->setCFConstantStringType(GetType(String));
2819  }
2820
2821  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2822    QualType FileType = GetType(File);
2823    if (FileType.isNull()) {
2824      Error("FILE type is NULL");
2825      return;
2826    }
2827
2828    if (!Context->FILEDecl) {
2829      if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2830        Context->setFILEDecl(Typedef->getDecl());
2831      else {
2832        const TagType *Tag = FileType->getAs<TagType>();
2833        if (!Tag) {
2834          Error("Invalid FILE type in AST file");
2835          return;
2836        }
2837        Context->setFILEDecl(Tag->getDecl());
2838      }
2839    }
2840  }
2841
2842  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2843    QualType Jmp_bufType = GetType(Jmp_buf);
2844    if (Jmp_bufType.isNull()) {
2845      Error("jmp_buf type is NULL");
2846      return;
2847    }
2848
2849    if (!Context->jmp_bufDecl) {
2850      if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2851        Context->setjmp_bufDecl(Typedef->getDecl());
2852      else {
2853        const TagType *Tag = Jmp_bufType->getAs<TagType>();
2854        if (!Tag) {
2855          Error("Invalid jmp_buf type in AST file");
2856          return;
2857        }
2858        Context->setjmp_bufDecl(Tag->getDecl());
2859      }
2860    }
2861  }
2862
2863  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2864    QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2865    if (Sigjmp_bufType.isNull()) {
2866      Error("sigjmp_buf type is NULL");
2867      return;
2868    }
2869
2870    if (!Context->sigjmp_bufDecl) {
2871      if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2872        Context->setsigjmp_bufDecl(Typedef->getDecl());
2873      else {
2874        const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2875        assert(Tag && "Invalid sigjmp_buf type in AST file");
2876        Context->setsigjmp_bufDecl(Tag->getDecl());
2877      }
2878    }
2879  }
2880
2881  if (unsigned ObjCIdRedef
2882        = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2883    if (Context->ObjCIdRedefinitionType.isNull())
2884      Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2885  }
2886
2887  if (unsigned ObjCClassRedef
2888        = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2889    if (Context->ObjCClassRedefinitionType.isNull())
2890      Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2891  }
2892
2893  if (unsigned ObjCSelRedef
2894        = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2895    if (Context->ObjCSelRedefinitionType.isNull())
2896      Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2897  }
2898
2899  ReadPragmaDiagnosticMappings(Context->getDiagnostics());
2900
2901  // If there were any CUDA special declarations, deserialize them.
2902  if (!CUDASpecialDeclRefs.empty()) {
2903    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2904    Context->setcudaConfigureCallDecl(
2905                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2906  }
2907}
2908
2909/// \brief Retrieve the name of the original source file name
2910/// directly from the AST file, without actually loading the AST
2911/// file.
2912std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2913                                             FileManager &FileMgr,
2914                                             Diagnostic &Diags) {
2915  // Open the AST file.
2916  std::string ErrStr;
2917  llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2918  Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
2919  if (!Buffer) {
2920    Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2921    return std::string();
2922  }
2923
2924  // Initialize the stream
2925  llvm::BitstreamReader StreamFile;
2926  llvm::BitstreamCursor Stream;
2927  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2928                  (const unsigned char *)Buffer->getBufferEnd());
2929  Stream.init(StreamFile);
2930
2931  // Sniff for the signature.
2932  if (Stream.Read(8) != 'C' ||
2933      Stream.Read(8) != 'P' ||
2934      Stream.Read(8) != 'C' ||
2935      Stream.Read(8) != 'H') {
2936    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2937    return std::string();
2938  }
2939
2940  RecordData Record;
2941  while (!Stream.AtEndOfStream()) {
2942    unsigned Code = Stream.ReadCode();
2943
2944    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2945      unsigned BlockID = Stream.ReadSubBlockID();
2946
2947      // We only know the AST subblock ID.
2948      switch (BlockID) {
2949      case AST_BLOCK_ID:
2950        if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2951          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2952          return std::string();
2953        }
2954        break;
2955
2956      default:
2957        if (Stream.SkipBlock()) {
2958          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2959          return std::string();
2960        }
2961        break;
2962      }
2963      continue;
2964    }
2965
2966    if (Code == llvm::bitc::END_BLOCK) {
2967      if (Stream.ReadBlockEnd()) {
2968        Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2969        return std::string();
2970      }
2971      continue;
2972    }
2973
2974    if (Code == llvm::bitc::DEFINE_ABBREV) {
2975      Stream.ReadAbbrevRecord();
2976      continue;
2977    }
2978
2979    Record.clear();
2980    const char *BlobStart = 0;
2981    unsigned BlobLen = 0;
2982    if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2983          == ORIGINAL_FILE_NAME)
2984      return std::string(BlobStart, BlobLen);
2985  }
2986
2987  return std::string();
2988}
2989
2990/// \brief Parse the record that corresponds to a LangOptions data
2991/// structure.
2992///
2993/// This routine parses the language options from the AST file and then gives
2994/// them to the AST listener if one is set.
2995///
2996/// \returns true if the listener deems the file unacceptable, false otherwise.
2997bool ASTReader::ParseLanguageOptions(
2998                             const SmallVectorImpl<uint64_t> &Record) {
2999  if (Listener) {
3000    LangOptions LangOpts;
3001
3002  #define PARSE_LANGOPT(Option)                  \
3003      LangOpts.Option = Record[Idx];             \
3004      ++Idx
3005
3006    unsigned Idx = 0;
3007    PARSE_LANGOPT(Trigraphs);
3008    PARSE_LANGOPT(BCPLComment);
3009    PARSE_LANGOPT(DollarIdents);
3010    PARSE_LANGOPT(AsmPreprocessor);
3011    PARSE_LANGOPT(GNUMode);
3012    PARSE_LANGOPT(GNUKeywords);
3013    PARSE_LANGOPT(ImplicitInt);
3014    PARSE_LANGOPT(Digraphs);
3015    PARSE_LANGOPT(HexFloats);
3016    PARSE_LANGOPT(C99);
3017    PARSE_LANGOPT(C1X);
3018    PARSE_LANGOPT(Microsoft);
3019    PARSE_LANGOPT(CPlusPlus);
3020    PARSE_LANGOPT(CPlusPlus0x);
3021    PARSE_LANGOPT(CXXOperatorNames);
3022    PARSE_LANGOPT(ObjC1);
3023    PARSE_LANGOPT(ObjC2);
3024    PARSE_LANGOPT(ObjCNonFragileABI);
3025    PARSE_LANGOPT(ObjCNonFragileABI2);
3026    PARSE_LANGOPT(AppleKext);
3027    PARSE_LANGOPT(ObjCDefaultSynthProperties);
3028    PARSE_LANGOPT(ObjCInferRelatedResultType);
3029    PARSE_LANGOPT(NoConstantCFStrings);
3030    PARSE_LANGOPT(PascalStrings);
3031    PARSE_LANGOPT(WritableStrings);
3032    PARSE_LANGOPT(LaxVectorConversions);
3033    PARSE_LANGOPT(AltiVec);
3034    PARSE_LANGOPT(Exceptions);
3035    PARSE_LANGOPT(ObjCExceptions);
3036    PARSE_LANGOPT(CXXExceptions);
3037    PARSE_LANGOPT(SjLjExceptions);
3038    PARSE_LANGOPT(MSBitfields);
3039    PARSE_LANGOPT(NeXTRuntime);
3040    PARSE_LANGOPT(Freestanding);
3041    PARSE_LANGOPT(NoBuiltin);
3042    PARSE_LANGOPT(ThreadsafeStatics);
3043    PARSE_LANGOPT(POSIXThreads);
3044    PARSE_LANGOPT(Blocks);
3045    PARSE_LANGOPT(EmitAllDecls);
3046    PARSE_LANGOPT(MathErrno);
3047    LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3048                                       Record[Idx++]);
3049    PARSE_LANGOPT(HeinousExtensions);
3050    PARSE_LANGOPT(Optimize);
3051    PARSE_LANGOPT(OptimizeSize);
3052    PARSE_LANGOPT(Static);
3053    PARSE_LANGOPT(PICLevel);
3054    PARSE_LANGOPT(GNUInline);
3055    PARSE_LANGOPT(NoInline);
3056    PARSE_LANGOPT(Deprecated);
3057    PARSE_LANGOPT(AccessControl);
3058    PARSE_LANGOPT(CharIsSigned);
3059    PARSE_LANGOPT(ShortWChar);
3060    PARSE_LANGOPT(ShortEnums);
3061    LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
3062    LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
3063    LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
3064                                   Record[Idx++]);
3065    PARSE_LANGOPT(InstantiationDepth);
3066    PARSE_LANGOPT(OpenCL);
3067    PARSE_LANGOPT(CUDA);
3068    PARSE_LANGOPT(CatchUndefined);
3069    PARSE_LANGOPT(DefaultFPContract);
3070    PARSE_LANGOPT(ElideConstructors);
3071    PARSE_LANGOPT(SpellChecking);
3072    PARSE_LANGOPT(MRTD);
3073    PARSE_LANGOPT(ObjCAutoRefCount);
3074    PARSE_LANGOPT(ObjCInferRelatedReturnType);
3075  #undef PARSE_LANGOPT
3076
3077    return Listener->ReadLanguageOptions(LangOpts);
3078  }
3079
3080  return false;
3081}
3082
3083namespace {
3084  /// \brief Visitor used by ASTReader::ReadPreprocessedEntities() to load
3085  /// all of the preprocessed entities within a module.
3086  class ReadPreprocessedEntitiesVisitor {
3087    ASTReader &Reader;
3088
3089  public:
3090    explicit ReadPreprocessedEntitiesVisitor(ASTReader &Reader)
3091      : Reader(Reader) { }
3092
3093    static bool visit(Module &M, bool Preorder, void *UserData) {
3094      if (Preorder)
3095        return false;
3096
3097      ReadPreprocessedEntitiesVisitor *This
3098        = static_cast<ReadPreprocessedEntitiesVisitor *>(UserData);
3099
3100      if (!M.PreprocessorDetailCursor.getBitStreamReader())
3101        return false;
3102
3103      SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3104      M.PreprocessorDetailCursor.JumpToBit(M.PreprocessorDetailStartOffset);
3105      while (This->Reader.LoadPreprocessedEntity(M)) { }
3106      return false;
3107    }
3108  };
3109}
3110
3111void ASTReader::ReadPreprocessedEntities() {
3112  ReadPreprocessedEntitiesVisitor Visitor(*this);
3113  ModuleMgr.visitDepthFirst(&ReadPreprocessedEntitiesVisitor::visit, &Visitor);
3114}
3115
3116PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
3117  RecordLocation Loc = getLocalBitOffset(Offset);
3118
3119  // Keep track of where we are in the stream, then jump back there
3120  // after reading this entity.
3121  SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3122  Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3123  return LoadPreprocessedEntity(*Loc.F);
3124}
3125
3126namespace {
3127  /// \brief Visitor used to search for information about a header file.
3128  class HeaderFileInfoVisitor {
3129    ASTReader &Reader;
3130    const FileEntry *FE;
3131
3132    llvm::Optional<HeaderFileInfo> HFI;
3133
3134  public:
3135    HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3136      : Reader(Reader), FE(FE) { }
3137
3138    static bool visit(Module &M, void *UserData) {
3139      HeaderFileInfoVisitor *This
3140        = static_cast<HeaderFileInfoVisitor *>(UserData);
3141
3142      HeaderFileInfoTrait Trait(This->Reader, M,
3143                                &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3144                                M.HeaderFileFrameworkStrings,
3145                                This->FE->getName());
3146
3147      HeaderFileInfoLookupTable *Table
3148        = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3149      if (!Table)
3150        return false;
3151
3152      // Look in the on-disk hash table for an entry for this file name.
3153      HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3154                                                            &Trait);
3155      if (Pos == Table->end())
3156        return false;
3157
3158      This->HFI = *Pos;
3159      return true;
3160    }
3161
3162    llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3163  };
3164}
3165
3166HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3167  HeaderFileInfoVisitor Visitor(*this, FE);
3168  ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3169  if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
3170    if (Listener)
3171      Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3172    return *HFI;
3173  }
3174
3175  return HeaderFileInfo();
3176}
3177
3178void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
3179  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3180    Module &F = *(*I);
3181    unsigned Idx = 0;
3182    while (Idx < F.PragmaDiagMappings.size()) {
3183      SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3184      while (1) {
3185        assert(Idx < F.PragmaDiagMappings.size() &&
3186               "Invalid data, didn't find '-1' marking end of diag/map pairs");
3187        if (Idx >= F.PragmaDiagMappings.size()) {
3188          break; // Something is messed up but at least avoid infinite loop in
3189                 // release build.
3190        }
3191        unsigned DiagID = F.PragmaDiagMappings[Idx++];
3192        if (DiagID == (unsigned)-1) {
3193          break; // no more diag/map pairs for this location.
3194        }
3195        diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3196        Diag.setDiagnosticMapping(DiagID, Map, Loc);
3197      }
3198    }
3199  }
3200}
3201
3202/// \brief Get the correct cursor and offset for loading a type.
3203ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
3204  GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
3205  assert(I != GlobalTypeMap.end() && "Corrupted global type map");
3206  Module *M = I->second;
3207  return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
3208}
3209
3210/// \brief Read and return the type with the given index..
3211///
3212/// The index is the type ID, shifted and minus the number of predefs. This
3213/// routine actually reads the record corresponding to the type at the given
3214/// location. It is a helper routine for GetType, which deals with reading type
3215/// IDs.
3216QualType ASTReader::readTypeRecord(unsigned Index) {
3217  RecordLocation Loc = TypeCursorForIndex(Index);
3218  llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
3219
3220  // Keep track of where we are in the stream, then jump back there
3221  // after reading this type.
3222  SavedStreamPosition SavedPosition(DeclsCursor);
3223
3224  ReadingKindTracker ReadingKind(Read_Type, *this);
3225
3226  // Note that we are loading a type record.
3227  Deserializing AType(this);
3228
3229  unsigned Idx = 0;
3230  DeclsCursor.JumpToBit(Loc.Offset);
3231  RecordData Record;
3232  unsigned Code = DeclsCursor.ReadCode();
3233  switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3234  case TYPE_EXT_QUAL: {
3235    if (Record.size() != 2) {
3236      Error("Incorrect encoding of extended qualifier type");
3237      return QualType();
3238    }
3239    QualType Base = readType(*Loc.F, Record, Idx);
3240    Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
3241    return Context->getQualifiedType(Base, Quals);
3242  }
3243
3244  case TYPE_COMPLEX: {
3245    if (Record.size() != 1) {
3246      Error("Incorrect encoding of complex type");
3247      return QualType();
3248    }
3249    QualType ElemType = readType(*Loc.F, Record, Idx);
3250    return Context->getComplexType(ElemType);
3251  }
3252
3253  case TYPE_POINTER: {
3254    if (Record.size() != 1) {
3255      Error("Incorrect encoding of pointer type");
3256      return QualType();
3257    }
3258    QualType PointeeType = readType(*Loc.F, Record, Idx);
3259    return Context->getPointerType(PointeeType);
3260  }
3261
3262  case TYPE_BLOCK_POINTER: {
3263    if (Record.size() != 1) {
3264      Error("Incorrect encoding of block pointer type");
3265      return QualType();
3266    }
3267    QualType PointeeType = readType(*Loc.F, Record, Idx);
3268    return Context->getBlockPointerType(PointeeType);
3269  }
3270
3271  case TYPE_LVALUE_REFERENCE: {
3272    if (Record.size() != 2) {
3273      Error("Incorrect encoding of lvalue reference type");
3274      return QualType();
3275    }
3276    QualType PointeeType = readType(*Loc.F, Record, Idx);
3277    return Context->getLValueReferenceType(PointeeType, Record[1]);
3278  }
3279
3280  case TYPE_RVALUE_REFERENCE: {
3281    if (Record.size() != 1) {
3282      Error("Incorrect encoding of rvalue reference type");
3283      return QualType();
3284    }
3285    QualType PointeeType = readType(*Loc.F, Record, Idx);
3286    return Context->getRValueReferenceType(PointeeType);
3287  }
3288
3289  case TYPE_MEMBER_POINTER: {
3290    if (Record.size() != 2) {
3291      Error("Incorrect encoding of member pointer type");
3292      return QualType();
3293    }
3294    QualType PointeeType = readType(*Loc.F, Record, Idx);
3295    QualType ClassType = readType(*Loc.F, Record, Idx);
3296    if (PointeeType.isNull() || ClassType.isNull())
3297      return QualType();
3298
3299    return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
3300  }
3301
3302  case TYPE_CONSTANT_ARRAY: {
3303    QualType ElementType = readType(*Loc.F, Record, Idx);
3304    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3305    unsigned IndexTypeQuals = Record[2];
3306    unsigned Idx = 3;
3307    llvm::APInt Size = ReadAPInt(Record, Idx);
3308    return Context->getConstantArrayType(ElementType, Size,
3309                                         ASM, IndexTypeQuals);
3310  }
3311
3312  case TYPE_INCOMPLETE_ARRAY: {
3313    QualType ElementType = readType(*Loc.F, Record, Idx);
3314    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3315    unsigned IndexTypeQuals = Record[2];
3316    return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
3317  }
3318
3319  case TYPE_VARIABLE_ARRAY: {
3320    QualType ElementType = readType(*Loc.F, Record, Idx);
3321    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3322    unsigned IndexTypeQuals = Record[2];
3323    SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3324    SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3325    return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
3326                                         ASM, IndexTypeQuals,
3327                                         SourceRange(LBLoc, RBLoc));
3328  }
3329
3330  case TYPE_VECTOR: {
3331    if (Record.size() != 3) {
3332      Error("incorrect encoding of vector type in AST file");
3333      return QualType();
3334    }
3335
3336    QualType ElementType = readType(*Loc.F, Record, Idx);
3337    unsigned NumElements = Record[1];
3338    unsigned VecKind = Record[2];
3339    return Context->getVectorType(ElementType, NumElements,
3340                                  (VectorType::VectorKind)VecKind);
3341  }
3342
3343  case TYPE_EXT_VECTOR: {
3344    if (Record.size() != 3) {
3345      Error("incorrect encoding of extended vector type in AST file");
3346      return QualType();
3347    }
3348
3349    QualType ElementType = readType(*Loc.F, Record, Idx);
3350    unsigned NumElements = Record[1];
3351    return Context->getExtVectorType(ElementType, NumElements);
3352  }
3353
3354  case TYPE_FUNCTION_NO_PROTO: {
3355    if (Record.size() != 6) {
3356      Error("incorrect encoding of no-proto function type");
3357      return QualType();
3358    }
3359    QualType ResultType = readType(*Loc.F, Record, Idx);
3360    FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3361                               (CallingConv)Record[4], Record[5]);
3362    return Context->getFunctionNoProtoType(ResultType, Info);
3363  }
3364
3365  case TYPE_FUNCTION_PROTO: {
3366    QualType ResultType = readType(*Loc.F, Record, Idx);
3367
3368    FunctionProtoType::ExtProtoInfo EPI;
3369    EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3370                                        /*hasregparm*/ Record[2],
3371                                        /*regparm*/ Record[3],
3372                                        static_cast<CallingConv>(Record[4]),
3373                                        /*produces*/ Record[5]);
3374
3375    unsigned Idx = 6;
3376    unsigned NumParams = Record[Idx++];
3377    SmallVector<QualType, 16> ParamTypes;
3378    for (unsigned I = 0; I != NumParams; ++I)
3379      ParamTypes.push_back(readType(*Loc.F, Record, Idx));
3380
3381    EPI.Variadic = Record[Idx++];
3382    EPI.TypeQuals = Record[Idx++];
3383    EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
3384    ExceptionSpecificationType EST =
3385        static_cast<ExceptionSpecificationType>(Record[Idx++]);
3386    EPI.ExceptionSpecType = EST;
3387    if (EST == EST_Dynamic) {
3388      EPI.NumExceptions = Record[Idx++];
3389      SmallVector<QualType, 2> Exceptions;
3390      for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3391        Exceptions.push_back(readType(*Loc.F, Record, Idx));
3392      EPI.Exceptions = Exceptions.data();
3393    } else if (EST == EST_ComputedNoexcept) {
3394      EPI.NoexceptExpr = ReadExpr(*Loc.F);
3395    }
3396    return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
3397                                    EPI);
3398  }
3399
3400  case TYPE_UNRESOLVED_USING: {
3401    unsigned Idx = 0;
3402    return Context->getTypeDeclType(
3403                  ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3404  }
3405
3406  case TYPE_TYPEDEF: {
3407    if (Record.size() != 2) {
3408      Error("incorrect encoding of typedef type");
3409      return QualType();
3410    }
3411    unsigned Idx = 0;
3412    TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
3413    QualType Canonical = readType(*Loc.F, Record, Idx);
3414    if (!Canonical.isNull())
3415      Canonical = Context->getCanonicalType(Canonical);
3416    return Context->getTypedefType(Decl, Canonical);
3417  }
3418
3419  case TYPE_TYPEOF_EXPR:
3420    return Context->getTypeOfExprType(ReadExpr(*Loc.F));
3421
3422  case TYPE_TYPEOF: {
3423    if (Record.size() != 1) {
3424      Error("incorrect encoding of typeof(type) in AST file");
3425      return QualType();
3426    }
3427    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3428    return Context->getTypeOfType(UnderlyingType);
3429  }
3430
3431  case TYPE_DECLTYPE:
3432    return Context->getDecltypeType(ReadExpr(*Loc.F));
3433
3434  case TYPE_UNARY_TRANSFORM: {
3435    QualType BaseType = readType(*Loc.F, Record, Idx);
3436    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3437    UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3438    return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3439  }
3440
3441  case TYPE_AUTO:
3442    return Context->getAutoType(readType(*Loc.F, Record, Idx));
3443
3444  case TYPE_RECORD: {
3445    if (Record.size() != 2) {
3446      Error("incorrect encoding of record type");
3447      return QualType();
3448    }
3449    unsigned Idx = 0;
3450    bool IsDependent = Record[Idx++];
3451    QualType T
3452      = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
3453    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3454    return T;
3455  }
3456
3457  case TYPE_ENUM: {
3458    if (Record.size() != 2) {
3459      Error("incorrect encoding of enum type");
3460      return QualType();
3461    }
3462    unsigned Idx = 0;
3463    bool IsDependent = Record[Idx++];
3464    QualType T
3465      = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
3466    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3467    return T;
3468  }
3469
3470  case TYPE_ATTRIBUTED: {
3471    if (Record.size() != 3) {
3472      Error("incorrect encoding of attributed type");
3473      return QualType();
3474    }
3475    QualType modifiedType = readType(*Loc.F, Record, Idx);
3476    QualType equivalentType = readType(*Loc.F, Record, Idx);
3477    AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3478    return Context->getAttributedType(kind, modifiedType, equivalentType);
3479  }
3480
3481  case TYPE_PAREN: {
3482    if (Record.size() != 1) {
3483      Error("incorrect encoding of paren type");
3484      return QualType();
3485    }
3486    QualType InnerType = readType(*Loc.F, Record, Idx);
3487    return Context->getParenType(InnerType);
3488  }
3489
3490  case TYPE_PACK_EXPANSION: {
3491    if (Record.size() != 2) {
3492      Error("incorrect encoding of pack expansion type");
3493      return QualType();
3494    }
3495    QualType Pattern = readType(*Loc.F, Record, Idx);
3496    if (Pattern.isNull())
3497      return QualType();
3498    llvm::Optional<unsigned> NumExpansions;
3499    if (Record[1])
3500      NumExpansions = Record[1] - 1;
3501    return Context->getPackExpansionType(Pattern, NumExpansions);
3502  }
3503
3504  case TYPE_ELABORATED: {
3505    unsigned Idx = 0;
3506    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3507    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3508    QualType NamedType = readType(*Loc.F, Record, Idx);
3509    return Context->getElaboratedType(Keyword, NNS, NamedType);
3510  }
3511
3512  case TYPE_OBJC_INTERFACE: {
3513    unsigned Idx = 0;
3514    ObjCInterfaceDecl *ItfD
3515      = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
3516    return Context->getObjCInterfaceType(ItfD);
3517  }
3518
3519  case TYPE_OBJC_OBJECT: {
3520    unsigned Idx = 0;
3521    QualType Base = readType(*Loc.F, Record, Idx);
3522    unsigned NumProtos = Record[Idx++];
3523    SmallVector<ObjCProtocolDecl*, 4> Protos;
3524    for (unsigned I = 0; I != NumProtos; ++I)
3525      Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
3526    return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
3527  }
3528
3529  case TYPE_OBJC_OBJECT_POINTER: {
3530    unsigned Idx = 0;
3531    QualType Pointee = readType(*Loc.F, Record, Idx);
3532    return Context->getObjCObjectPointerType(Pointee);
3533  }
3534
3535  case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
3536    unsigned Idx = 0;
3537    QualType Parm = readType(*Loc.F, Record, Idx);
3538    QualType Replacement = readType(*Loc.F, Record, Idx);
3539    return
3540      Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3541                                            Replacement);
3542  }
3543
3544  case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3545    unsigned Idx = 0;
3546    QualType Parm = readType(*Loc.F, Record, Idx);
3547    TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3548    return Context->getSubstTemplateTypeParmPackType(
3549                                               cast<TemplateTypeParmType>(Parm),
3550                                                     ArgPack);
3551  }
3552
3553  case TYPE_INJECTED_CLASS_NAME: {
3554    CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
3555    QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
3556    // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
3557    // for AST reading, too much interdependencies.
3558    return
3559      QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
3560  }
3561
3562  case TYPE_TEMPLATE_TYPE_PARM: {
3563    unsigned Idx = 0;
3564    unsigned Depth = Record[Idx++];
3565    unsigned Index = Record[Idx++];
3566    bool Pack = Record[Idx++];
3567    TemplateTypeParmDecl *D
3568      = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
3569    return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
3570  }
3571
3572  case TYPE_DEPENDENT_NAME: {
3573    unsigned Idx = 0;
3574    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3575    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3576    const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
3577    QualType Canon = readType(*Loc.F, Record, Idx);
3578    if (!Canon.isNull())
3579      Canon = Context->getCanonicalType(Canon);
3580    return Context->getDependentNameType(Keyword, NNS, Name, Canon);
3581  }
3582
3583  case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
3584    unsigned Idx = 0;
3585    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3586    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3587    const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
3588    unsigned NumArgs = Record[Idx++];
3589    SmallVector<TemplateArgument, 8> Args;
3590    Args.reserve(NumArgs);
3591    while (NumArgs--)
3592      Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
3593    return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3594                                                      Args.size(), Args.data());
3595  }
3596
3597  case TYPE_DEPENDENT_SIZED_ARRAY: {
3598    unsigned Idx = 0;
3599
3600    // ArrayType
3601    QualType ElementType = readType(*Loc.F, Record, Idx);
3602    ArrayType::ArraySizeModifier ASM
3603      = (ArrayType::ArraySizeModifier)Record[Idx++];
3604    unsigned IndexTypeQuals = Record[Idx++];
3605
3606    // DependentSizedArrayType
3607    Expr *NumElts = ReadExpr(*Loc.F);
3608    SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
3609
3610    return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3611                                               IndexTypeQuals, Brackets);
3612  }
3613
3614  case TYPE_TEMPLATE_SPECIALIZATION: {
3615    unsigned Idx = 0;
3616    bool IsDependent = Record[Idx++];
3617    TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
3618    SmallVector<TemplateArgument, 8> Args;
3619    ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
3620    QualType Underlying = readType(*Loc.F, Record, Idx);
3621    QualType T;
3622    if (Underlying.isNull())
3623      T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3624                                                          Args.size());
3625    else
3626      T = Context->getTemplateSpecializationType(Name, Args.data(),
3627                                                 Args.size(), Underlying);
3628    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3629    return T;
3630  }
3631  }
3632  // Suppress a GCC warning
3633  return QualType();
3634}
3635
3636class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
3637  ASTReader &Reader;
3638  Module &F;
3639  llvm::BitstreamCursor &DeclsCursor;
3640  const ASTReader::RecordData &Record;
3641  unsigned &Idx;
3642
3643  SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3644                                    unsigned &I) {
3645    return Reader.ReadSourceLocation(F, R, I);
3646  }
3647
3648  template<typename T>
3649  T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3650    return Reader.ReadDeclAs<T>(F, Record, Idx);
3651  }
3652
3653public:
3654  TypeLocReader(ASTReader &Reader, Module &F,
3655                const ASTReader::RecordData &Record, unsigned &Idx)
3656    : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3657  { }
3658
3659  // We want compile-time assurance that we've enumerated all of
3660  // these, so unfortunately we have to declare them first, then
3661  // define them out-of-line.
3662#define ABSTRACT_TYPELOC(CLASS, PARENT)
3663#define TYPELOC(CLASS, PARENT) \
3664  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
3665#include "clang/AST/TypeLocNodes.def"
3666
3667  void VisitFunctionTypeLoc(FunctionTypeLoc);
3668  void VisitArrayTypeLoc(ArrayTypeLoc);
3669};
3670
3671void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3672  // nothing to do
3673}
3674void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3675  TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
3676  if (TL.needsExtraLocalData()) {
3677    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3678    TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3679    TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3680    TL.setModeAttr(Record[Idx++]);
3681  }
3682}
3683void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
3684  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3685}
3686void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
3687  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3688}
3689void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3690  TL.setCaretLoc(ReadSourceLocation(Record, Idx));
3691}
3692void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3693  TL.setAmpLoc(ReadSourceLocation(Record, Idx));
3694}
3695void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3696  TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
3697}
3698void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3699  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3700  TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3701}
3702void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
3703  TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3704  TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
3705  if (Record[Idx++])
3706    TL.setSizeExpr(Reader.ReadExpr(F));
3707  else
3708    TL.setSizeExpr(0);
3709}
3710void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3711  VisitArrayTypeLoc(TL);
3712}
3713void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3714  VisitArrayTypeLoc(TL);
3715}
3716void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3717  VisitArrayTypeLoc(TL);
3718}
3719void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3720                                            DependentSizedArrayTypeLoc TL) {
3721  VisitArrayTypeLoc(TL);
3722}
3723void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3724                                        DependentSizedExtVectorTypeLoc TL) {
3725  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3726}
3727void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
3728  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3729}
3730void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
3731  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3732}
3733void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3734  TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3735  TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
3736  TL.setTrailingReturn(Record[Idx++]);
3737  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3738    TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
3739  }
3740}
3741void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3742  VisitFunctionTypeLoc(TL);
3743}
3744void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3745  VisitFunctionTypeLoc(TL);
3746}
3747void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
3748  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3749}
3750void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3751  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3752}
3753void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3754  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3755  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3756  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3757}
3758void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3759  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3760  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3761  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3762  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3763}
3764void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
3765  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3766}
3767void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3768  TL.setKWLoc(ReadSourceLocation(Record, Idx));
3769  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3770  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3771  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3772}
3773void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3774  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3775}
3776void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
3777  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3778}
3779void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
3780  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3781}
3782void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3783  TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3784  if (TL.hasAttrOperand()) {
3785    SourceRange range;
3786    range.setBegin(ReadSourceLocation(Record, Idx));
3787    range.setEnd(ReadSourceLocation(Record, Idx));
3788    TL.setAttrOperandParensRange(range);
3789  }
3790  if (TL.hasAttrExprOperand()) {
3791    if (Record[Idx++])
3792      TL.setAttrExprOperand(Reader.ReadExpr(F));
3793    else
3794      TL.setAttrExprOperand(0);
3795  } else if (TL.hasAttrEnumOperand())
3796    TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3797}
3798void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
3799  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3800}
3801void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3802                                            SubstTemplateTypeParmTypeLoc TL) {
3803  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3804}
3805void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3806                                          SubstTemplateTypeParmPackTypeLoc TL) {
3807  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3808}
3809void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3810                                           TemplateSpecializationTypeLoc TL) {
3811  TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3812  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3813  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3814  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3815    TL.setArgLocInfo(i,
3816        Reader.GetTemplateArgumentLocInfo(F,
3817                                          TL.getTypePtr()->getArg(i).getKind(),
3818                                          Record, Idx));
3819}
3820void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3821  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3822  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3823}
3824void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3825  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3826  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3827}
3828void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3829  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3830}
3831void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
3832  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3833  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3834  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3835}
3836void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3837       DependentTemplateSpecializationTypeLoc TL) {
3838  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3839  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3840  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3841  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3842  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3843  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3844    TL.setArgLocInfo(I,
3845        Reader.GetTemplateArgumentLocInfo(F,
3846                                          TL.getTypePtr()->getArg(I).getKind(),
3847                                          Record, Idx));
3848}
3849void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3850  TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3851}
3852void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3853  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3854}
3855void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3856  TL.setHasBaseTypeAsWritten(Record[Idx++]);
3857  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3858  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3859  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3860    TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
3861}
3862void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3863  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3864}
3865
3866TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
3867                                             const RecordData &Record,
3868                                             unsigned &Idx) {
3869  QualType InfoTy = readType(F, Record, Idx);
3870  if (InfoTy.isNull())
3871    return 0;
3872
3873  TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
3874  TypeLocReader TLR(*this, F, Record, Idx);
3875  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
3876    TLR.Visit(TL);
3877  return TInfo;
3878}
3879
3880QualType ASTReader::GetType(TypeID ID) {
3881  unsigned FastQuals = ID & Qualifiers::FastMask;
3882  unsigned Index = ID >> Qualifiers::FastWidth;
3883
3884  if (Index < NUM_PREDEF_TYPE_IDS) {
3885    QualType T;
3886    switch ((PredefinedTypeIDs)Index) {
3887    case PREDEF_TYPE_NULL_ID: return QualType();
3888    case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3889    case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
3890
3891    case PREDEF_TYPE_CHAR_U_ID:
3892    case PREDEF_TYPE_CHAR_S_ID:
3893      // FIXME: Check that the signedness of CharTy is correct!
3894      T = Context->CharTy;
3895      break;
3896
3897    case PREDEF_TYPE_UCHAR_ID:      T = Context->UnsignedCharTy;     break;
3898    case PREDEF_TYPE_USHORT_ID:     T = Context->UnsignedShortTy;    break;
3899    case PREDEF_TYPE_UINT_ID:       T = Context->UnsignedIntTy;      break;
3900    case PREDEF_TYPE_ULONG_ID:      T = Context->UnsignedLongTy;     break;
3901    case PREDEF_TYPE_ULONGLONG_ID:  T = Context->UnsignedLongLongTy; break;
3902    case PREDEF_TYPE_UINT128_ID:    T = Context->UnsignedInt128Ty;   break;
3903    case PREDEF_TYPE_SCHAR_ID:      T = Context->SignedCharTy;       break;
3904    case PREDEF_TYPE_WCHAR_ID:      T = Context->WCharTy;            break;
3905    case PREDEF_TYPE_SHORT_ID:      T = Context->ShortTy;            break;
3906    case PREDEF_TYPE_INT_ID:        T = Context->IntTy;              break;
3907    case PREDEF_TYPE_LONG_ID:       T = Context->LongTy;             break;
3908    case PREDEF_TYPE_LONGLONG_ID:   T = Context->LongLongTy;         break;
3909    case PREDEF_TYPE_INT128_ID:     T = Context->Int128Ty;           break;
3910    case PREDEF_TYPE_FLOAT_ID:      T = Context->FloatTy;            break;
3911    case PREDEF_TYPE_DOUBLE_ID:     T = Context->DoubleTy;           break;
3912    case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy;       break;
3913    case PREDEF_TYPE_OVERLOAD_ID:   T = Context->OverloadTy;         break;
3914    case PREDEF_TYPE_BOUND_MEMBER:  T = Context->BoundMemberTy;      break;
3915    case PREDEF_TYPE_DEPENDENT_ID:  T = Context->DependentTy;        break;
3916    case PREDEF_TYPE_UNKNOWN_ANY:   T = Context->UnknownAnyTy;       break;
3917    case PREDEF_TYPE_NULLPTR_ID:    T = Context->NullPtrTy;          break;
3918    case PREDEF_TYPE_CHAR16_ID:     T = Context->Char16Ty;           break;
3919    case PREDEF_TYPE_CHAR32_ID:     T = Context->Char32Ty;           break;
3920    case PREDEF_TYPE_OBJC_ID:       T = Context->ObjCBuiltinIdTy;    break;
3921    case PREDEF_TYPE_OBJC_CLASS:    T = Context->ObjCBuiltinClassTy; break;
3922    case PREDEF_TYPE_OBJC_SEL:      T = Context->ObjCBuiltinSelTy;   break;
3923    case PREDEF_TYPE_AUTO_DEDUCT:   T = Context->getAutoDeductType(); break;
3924
3925    case PREDEF_TYPE_AUTO_RREF_DEDUCT:
3926      T = Context->getAutoRRefDeductType();
3927      break;
3928    }
3929
3930    assert(!T.isNull() && "Unknown predefined type");
3931    return T.withFastQualifiers(FastQuals);
3932  }
3933
3934  Index -= NUM_PREDEF_TYPE_IDS;
3935  assert(Index < TypesLoaded.size() && "Type index out-of-range");
3936  if (TypesLoaded[Index].isNull()) {
3937    TypesLoaded[Index] = readTypeRecord(Index);
3938    if (TypesLoaded[Index].isNull())
3939      return QualType();
3940
3941    TypesLoaded[Index]->setFromAST();
3942    if (DeserializationListener)
3943      DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
3944                                        TypesLoaded[Index]);
3945  }
3946
3947  return TypesLoaded[Index].withFastQualifiers(FastQuals);
3948}
3949
3950QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
3951  return GetType(getGlobalTypeID(F, LocalID));
3952}
3953
3954serialization::TypeID
3955ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
3956  unsigned FastQuals = LocalID & Qualifiers::FastMask;
3957  unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3958
3959  if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3960    return LocalID;
3961
3962  ContinuousRangeMap<uint32_t, int, 2>::iterator I
3963    = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3964  assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3965
3966  unsigned GlobalIndex = LocalIndex + I->second;
3967  return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3968}
3969
3970TemplateArgumentLocInfo
3971ASTReader::GetTemplateArgumentLocInfo(Module &F,
3972                                      TemplateArgument::ArgKind Kind,
3973                                      const RecordData &Record,
3974                                      unsigned &Index) {
3975  switch (Kind) {
3976  case TemplateArgument::Expression:
3977    return ReadExpr(F);
3978  case TemplateArgument::Type:
3979    return GetTypeSourceInfo(F, Record, Index);
3980  case TemplateArgument::Template: {
3981    NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3982                                                                     Index);
3983    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3984    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3985                                   SourceLocation());
3986  }
3987  case TemplateArgument::TemplateExpansion: {
3988    NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3989                                                                     Index);
3990    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3991    SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3992    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3993                                   EllipsisLoc);
3994  }
3995  case TemplateArgument::Null:
3996  case TemplateArgument::Integral:
3997  case TemplateArgument::Declaration:
3998  case TemplateArgument::Pack:
3999    return TemplateArgumentLocInfo();
4000  }
4001  llvm_unreachable("unexpected template argument loc");
4002  return TemplateArgumentLocInfo();
4003}
4004
4005TemplateArgumentLoc
4006ASTReader::ReadTemplateArgumentLoc(Module &F,
4007                                   const RecordData &Record, unsigned &Index) {
4008  TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
4009
4010  if (Arg.getKind() == TemplateArgument::Expression) {
4011    if (Record[Index++]) // bool InfoHasSameExpr.
4012      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4013  }
4014  return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
4015                                                             Record, Index));
4016}
4017
4018Decl *ASTReader::GetExternalDecl(uint32_t ID) {
4019  return GetDecl(ID);
4020}
4021
4022uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4023                                          unsigned &Idx){
4024  if (Idx >= Record.size())
4025    return 0;
4026
4027  unsigned LocalID = Record[Idx++];
4028  return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
4029}
4030
4031CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
4032  RecordLocation Loc = getLocalBitOffset(Offset);
4033  llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
4034  SavedStreamPosition SavedPosition(Cursor);
4035  Cursor.JumpToBit(Loc.Offset);
4036  ReadingKindTracker ReadingKind(Read_Decl, *this);
4037  RecordData Record;
4038  unsigned Code = Cursor.ReadCode();
4039  unsigned RecCode = Cursor.ReadRecord(Code, Record);
4040  if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4041    Error("Malformed AST file: missing C++ base specifiers");
4042    return 0;
4043  }
4044
4045  unsigned Idx = 0;
4046  unsigned NumBases = Record[Idx++];
4047  void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4048  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4049  for (unsigned I = 0; I != NumBases; ++I)
4050    Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
4051  return Bases;
4052}
4053
4054serialization::DeclID
4055ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
4056  if (LocalID < NUM_PREDEF_DECL_IDS)
4057    return LocalID;
4058
4059  ContinuousRangeMap<uint32_t, int, 2>::iterator I
4060    = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
4061  assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4062
4063  return LocalID + I->second;
4064}
4065
4066Decl *ASTReader::GetDecl(DeclID ID) {
4067  if (ID < NUM_PREDEF_DECL_IDS) {
4068    switch ((PredefinedDeclIDs)ID) {
4069    case PREDEF_DECL_NULL_ID:
4070      return 0;
4071
4072    case PREDEF_DECL_TRANSLATION_UNIT_ID:
4073      assert(Context && "No context available?");
4074      return Context->getTranslationUnitDecl();
4075
4076    case PREDEF_DECL_OBJC_ID_ID:
4077      assert(Context && "No context available?");
4078      return Context->getObjCIdDecl();
4079
4080    case PREDEF_DECL_OBJC_SEL_ID:
4081      assert(Context && "No context available?");
4082      return Context->getObjCSelDecl();
4083
4084    case PREDEF_DECL_OBJC_CLASS_ID:
4085      assert(Context && "No context available?");
4086      return Context->getObjCClassDecl();
4087
4088    case PREDEF_DECL_INT_128_ID:
4089      assert(Context && "No context available?");
4090      return Context->getInt128Decl();
4091
4092    case PREDEF_DECL_UNSIGNED_INT_128_ID:
4093      assert(Context && "No context available?");
4094      return Context->getUInt128Decl();
4095    }
4096
4097    return 0;
4098  }
4099
4100  unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4101
4102  if (Index > DeclsLoaded.size()) {
4103    Error("declaration ID out-of-range for AST file");
4104    return 0;
4105  }
4106
4107if (!DeclsLoaded[Index]) {
4108    ReadDeclRecord(ID);
4109    if (DeserializationListener)
4110      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4111  }
4112
4113  return DeclsLoaded[Index];
4114}
4115
4116serialization::DeclID ASTReader::ReadDeclID(Module &F,
4117                                            const RecordData &Record,
4118                                            unsigned &Idx) {
4119  if (Idx >= Record.size()) {
4120    Error("Corrupted AST file");
4121    return 0;
4122  }
4123
4124  return getGlobalDeclID(F, Record[Idx++]);
4125}
4126
4127/// \brief Resolve the offset of a statement into a statement.
4128///
4129/// This operation will read a new statement from the external
4130/// source each time it is called, and is meant to be used via a
4131/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
4132Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
4133  // Switch case IDs are per Decl.
4134  ClearSwitchCaseIDs();
4135
4136  // Offset here is a global offset across the entire chain.
4137  RecordLocation Loc = getLocalBitOffset(Offset);
4138  Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4139  return ReadStmtFromStream(*Loc.F);
4140}
4141
4142namespace {
4143  class FindExternalLexicalDeclsVisitor {
4144    ASTReader &Reader;
4145    const DeclContext *DC;
4146    bool (*isKindWeWant)(Decl::Kind);
4147    SmallVectorImpl<Decl*> &Decls;
4148    bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4149
4150  public:
4151    FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4152                                    bool (*isKindWeWant)(Decl::Kind),
4153                                    SmallVectorImpl<Decl*> &Decls)
4154      : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4155    {
4156      for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4157        PredefsVisited[I] = false;
4158    }
4159
4160    static bool visit(Module &M, bool Preorder, void *UserData) {
4161      if (Preorder)
4162        return false;
4163
4164      FindExternalLexicalDeclsVisitor *This
4165        = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4166
4167      Module::DeclContextInfosMap::iterator Info
4168        = M.DeclContextInfos.find(This->DC);
4169      if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4170        return false;
4171
4172      // Load all of the declaration IDs
4173      for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4174                               *IDE = ID + Info->second.NumLexicalDecls;
4175           ID != IDE; ++ID) {
4176        if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4177          continue;
4178
4179        // Don't add predefined declarations to the lexical context more
4180        // than once.
4181        if (ID->second < NUM_PREDEF_DECL_IDS) {
4182          if (This->PredefsVisited[ID->second])
4183            continue;
4184
4185          This->PredefsVisited[ID->second] = true;
4186        }
4187
4188        Decl *D = This->Reader.GetLocalDecl(M, ID->second);
4189        assert(D && "Null decl in lexical decls");
4190        This->Decls.push_back(D);
4191      }
4192
4193      return false;
4194    }
4195  };
4196}
4197
4198ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
4199                                         bool (*isKindWeWant)(Decl::Kind),
4200                                         SmallVectorImpl<Decl*> &Decls) {
4201  // There might be lexical decls in multiple modules, for the TU at
4202  // least. Walk all of the modules in the order they were loaded.
4203  FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4204  ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
4205  ++NumLexicalDeclContextsRead;
4206  return ELR_Success;
4207}
4208
4209namespace {
4210  /// \brief Module visitor used to perform name lookup into a
4211  /// declaration context.
4212  class DeclContextNameLookupVisitor {
4213    ASTReader &Reader;
4214    const DeclContext *DC;
4215    DeclarationName Name;
4216    SmallVectorImpl<NamedDecl *> &Decls;
4217
4218  public:
4219    DeclContextNameLookupVisitor(ASTReader &Reader,
4220                                 const DeclContext *DC, DeclarationName Name,
4221                                 SmallVectorImpl<NamedDecl *> &Decls)
4222      : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4223
4224    static bool visit(Module &M, void *UserData) {
4225      DeclContextNameLookupVisitor *This
4226        = static_cast<DeclContextNameLookupVisitor *>(UserData);
4227
4228      // Check whether we have any visible declaration information for
4229      // this context in this module.
4230      Module::DeclContextInfosMap::iterator Info
4231        = M.DeclContextInfos.find(This->DC);
4232      if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4233        return false;
4234
4235      // Look for this name within this module.
4236      ASTDeclContextNameLookupTable *LookupTable =
4237        (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4238      ASTDeclContextNameLookupTable::iterator Pos
4239        = LookupTable->find(This->Name);
4240      if (Pos == LookupTable->end())
4241        return false;
4242
4243      bool FoundAnything = false;
4244      ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4245      for (; Data.first != Data.second; ++Data.first) {
4246        NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4247        if (!ND)
4248          continue;
4249
4250        if (ND->getDeclName() != This->Name) {
4251          assert(!This->Name.getCXXNameType().isNull() &&
4252                 "Name mismatch without a type");
4253          continue;
4254        }
4255
4256        // Record this declaration.
4257        FoundAnything = true;
4258        This->Decls.push_back(ND);
4259      }
4260
4261      return FoundAnything;
4262    }
4263  };
4264}
4265
4266DeclContext::lookup_result
4267ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
4268                                          DeclarationName Name) {
4269  assert(DC->hasExternalVisibleStorage() &&
4270         "DeclContext has no visible decls in storage");
4271  if (!Name)
4272    return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4273                                      DeclContext::lookup_iterator(0));
4274
4275  SmallVector<NamedDecl *, 64> Decls;
4276  DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4277  ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
4278  ++NumVisibleDeclContextsRead;
4279  SetExternalVisibleDeclsForName(DC, Name, Decls);
4280  return const_cast<DeclContext*>(DC)->lookup(Name);
4281}
4282
4283void ASTReader::PassInterestingDeclsToConsumer() {
4284  assert(Consumer);
4285  while (!InterestingDecls.empty()) {
4286    DeclGroupRef DG(InterestingDecls.front());
4287    InterestingDecls.pop_front();
4288    Consumer->HandleInterestingDecl(DG);
4289  }
4290}
4291
4292void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
4293  this->Consumer = Consumer;
4294
4295  if (!Consumer)
4296    return;
4297
4298  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
4299    // Force deserialization of this decl, which will cause it to be queued for
4300    // passing to the consumer.
4301    GetDecl(ExternalDefinitions[I]);
4302  }
4303
4304  PassInterestingDeclsToConsumer();
4305}
4306
4307void ASTReader::PrintStats() {
4308  std::fprintf(stderr, "*** AST File Statistics:\n");
4309
4310  unsigned NumTypesLoaded
4311    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
4312                                      QualType());
4313  unsigned NumDeclsLoaded
4314    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4315                                      (Decl *)0);
4316  unsigned NumIdentifiersLoaded
4317    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4318                                            IdentifiersLoaded.end(),
4319                                            (IdentifierInfo *)0);
4320  unsigned NumSelectorsLoaded
4321    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4322                                          SelectorsLoaded.end(),
4323                                          Selector());
4324
4325  std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
4326  std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
4327  if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
4328    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
4329                 NumSLocEntriesRead, TotalNumSLocEntries,
4330                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
4331  if (!TypesLoaded.empty())
4332    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
4333                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4334                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4335  if (!DeclsLoaded.empty())
4336    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
4337                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4338                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
4339  if (!IdentifiersLoaded.empty())
4340    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
4341                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4342                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
4343  if (!SelectorsLoaded.empty())
4344    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
4345                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4346                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
4347  if (TotalNumStatements)
4348    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
4349                 NumStatementsRead, TotalNumStatements,
4350                 ((float)NumStatementsRead/TotalNumStatements * 100));
4351  if (TotalNumMacros)
4352    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
4353                 NumMacrosRead, TotalNumMacros,
4354                 ((float)NumMacrosRead/TotalNumMacros * 100));
4355  if (TotalLexicalDeclContexts)
4356    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
4357                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4358                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4359                  * 100));
4360  if (TotalVisibleDeclContexts)
4361    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
4362                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4363                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4364                  * 100));
4365  if (TotalNumMethodPoolEntries) {
4366    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
4367                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4368                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4369                  * 100));
4370    std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
4371  }
4372  std::fprintf(stderr, "\n");
4373  dump();
4374  std::fprintf(stderr, "\n");
4375}
4376
4377template<typename Key, typename Module, unsigned InitialCapacity>
4378static void
4379dumpModuleIDMap(StringRef Name,
4380                const ContinuousRangeMap<Key, Module *,
4381                                         InitialCapacity> &Map) {
4382  if (Map.begin() == Map.end())
4383    return;
4384
4385  typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
4386  llvm::errs() << Name << ":\n";
4387  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4388       I != IEnd; ++I) {
4389    llvm::errs() << "  " << I->first << " -> " << I->second->FileName
4390      << "\n";
4391  }
4392}
4393
4394void ASTReader::dump() {
4395  llvm::errs() << "*** PCH/Module Remappings:\n";
4396  dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
4397  dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
4398  dumpModuleIDMap("Global type map", GlobalTypeMap);
4399  dumpModuleIDMap("Global declaration map", GlobalDeclMap);
4400  dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4401  dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4402  dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4403  dumpModuleIDMap("Global preprocessed entity map",
4404                  GlobalPreprocessedEntityMap);
4405
4406  llvm::errs() << "\n*** PCH/Modules Loaded:";
4407  for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4408                                       MEnd = ModuleMgr.end();
4409       M != MEnd; ++M)
4410    (*M)->dump();
4411}
4412
4413/// Return the amount of memory used by memory buffers, breaking down
4414/// by heap-backed versus mmap'ed memory.
4415void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4416  for (ModuleConstIterator I = ModuleMgr.begin(),
4417      E = ModuleMgr.end(); I != E; ++I) {
4418    if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
4419      size_t bytes = buf->getBufferSize();
4420      switch (buf->getBufferKind()) {
4421        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4422          sizes.malloc_bytes += bytes;
4423          break;
4424        case llvm::MemoryBuffer::MemoryBuffer_MMap:
4425          sizes.mmap_bytes += bytes;
4426          break;
4427      }
4428    }
4429  }
4430}
4431
4432void ASTReader::InitializeSema(Sema &S) {
4433  SemaObj = &S;
4434  S.ExternalSource = this;
4435
4436  // Makes sure any declarations that were deserialized "too early"
4437  // still get added to the identifier's declaration chains.
4438  for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4439    if (SemaObj->TUScope)
4440      SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4441
4442    SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4443  }
4444  PreloadedDecls.clear();
4445
4446  // Load the offsets of the declarations that Sema references.
4447  // They will be lazily deserialized when needed.
4448  if (!SemaDeclRefs.empty()) {
4449    assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4450    if (!SemaObj->StdNamespace)
4451      SemaObj->StdNamespace = SemaDeclRefs[0];
4452    if (!SemaObj->StdBadAlloc)
4453      SemaObj->StdBadAlloc = SemaDeclRefs[1];
4454  }
4455
4456  if (!FPPragmaOptions.empty()) {
4457    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4458    SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4459  }
4460
4461  if (!OpenCLExtensions.empty()) {
4462    unsigned I = 0;
4463#define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4464#include "clang/Basic/OpenCLExtensions.def"
4465
4466    assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4467  }
4468}
4469
4470namespace {
4471  /// \brief Visitor class used to look up identifirs in
4472  class IdentifierLookupVisitor {
4473    StringRef Name;
4474    IdentifierInfo *Found;
4475  public:
4476    explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
4477
4478    static bool visit(Module &M, void *UserData) {
4479      IdentifierLookupVisitor *This
4480        = static_cast<IdentifierLookupVisitor *>(UserData);
4481
4482      ASTIdentifierLookupTable *IdTable
4483        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4484      if (!IdTable)
4485        return false;
4486
4487      std::pair<const char*, unsigned> Key(This->Name.begin(),
4488                                           This->Name.size());
4489      ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4490      if (Pos == IdTable->end())
4491        return false;
4492
4493      // Dereferencing the iterator has the effect of building the
4494      // IdentifierInfo node and populating it with the various
4495      // declarations it needs.
4496      This->Found = *Pos;
4497      return true;
4498    }
4499
4500    // \brief Retrieve the identifier info found within the module
4501    // files.
4502    IdentifierInfo *getIdentifierInfo() const { return Found; }
4503  };
4504}
4505
4506IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4507  IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4508  ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4509  return Visitor.getIdentifierInfo();
4510}
4511
4512namespace clang {
4513  /// \brief An identifier-lookup iterator that enumerates all of the
4514  /// identifiers stored within a set of AST files.
4515  class ASTIdentifierIterator : public IdentifierIterator {
4516    /// \brief The AST reader whose identifiers are being enumerated.
4517    const ASTReader &Reader;
4518
4519    /// \brief The current index into the chain of AST files stored in
4520    /// the AST reader.
4521    unsigned Index;
4522
4523    /// \brief The current position within the identifier lookup table
4524    /// of the current AST file.
4525    ASTIdentifierLookupTable::key_iterator Current;
4526
4527    /// \brief The end position within the identifier lookup table of
4528    /// the current AST file.
4529    ASTIdentifierLookupTable::key_iterator End;
4530
4531  public:
4532    explicit ASTIdentifierIterator(const ASTReader &Reader);
4533
4534    virtual StringRef Next();
4535  };
4536}
4537
4538ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4539  : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
4540  ASTIdentifierLookupTable *IdTable
4541    = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
4542  Current = IdTable->key_begin();
4543  End = IdTable->key_end();
4544}
4545
4546StringRef ASTIdentifierIterator::Next() {
4547  while (Current == End) {
4548    // If we have exhausted all of our AST files, we're done.
4549    if (Index == 0)
4550      return StringRef();
4551
4552    --Index;
4553    ASTIdentifierLookupTable *IdTable
4554      = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4555        IdentifierLookupTable;
4556    Current = IdTable->key_begin();
4557    End = IdTable->key_end();
4558  }
4559
4560  // We have any identifiers remaining in the current AST file; return
4561  // the next one.
4562  std::pair<const char*, unsigned> Key = *Current;
4563  ++Current;
4564  return StringRef(Key.first, Key.second);
4565}
4566
4567IdentifierIterator *ASTReader::getIdentifiers() const {
4568  return new ASTIdentifierIterator(*this);
4569}
4570
4571namespace clang { namespace serialization {
4572  class ReadMethodPoolVisitor {
4573    ASTReader &Reader;
4574    Selector Sel;
4575    llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4576    llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
4577
4578    /// \brief Build an ObjCMethodList from a vector of Objective-C method
4579    /// declarations.
4580    ObjCMethodList
4581    buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4582    {
4583      ObjCMethodList List;
4584      ObjCMethodList *Prev = 0;
4585      for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4586        if (!List.Method) {
4587          // This is the first method, which is the easy case.
4588          List.Method = Vec[I];
4589          Prev = &List;
4590          continue;
4591        }
4592
4593        ObjCMethodList *Mem =
4594          Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4595        Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4596        Prev = Prev->Next;
4597      }
4598
4599      return List;
4600    }
4601
4602  public:
4603    ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4604      : Reader(Reader), Sel(Sel) { }
4605
4606    static bool visit(Module &M, void *UserData) {
4607      ReadMethodPoolVisitor *This
4608        = static_cast<ReadMethodPoolVisitor *>(UserData);
4609
4610      if (!M.SelectorLookupTable)
4611        return false;
4612
4613      ASTSelectorLookupTable *PoolTable
4614        = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4615      ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4616      if (Pos == PoolTable->end())
4617        return false;
4618
4619      ++This->Reader.NumSelectorsRead;
4620      // FIXME: Not quite happy with the statistics here. We probably should
4621      // disable this tracking when called via LoadSelector.
4622      // Also, should entries without methods count as misses?
4623      ++This->Reader.NumMethodPoolEntriesRead;
4624      ASTSelectorLookupTrait::data_type Data = *Pos;
4625      if (This->Reader.DeserializationListener)
4626        This->Reader.DeserializationListener->SelectorRead(Data.ID,
4627                                                           This->Sel);
4628
4629      This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4630      This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4631      return true;
4632    }
4633
4634    /// \brief Retrieve the instance methods found by this visitor.
4635    ObjCMethodList getInstanceMethods() const {
4636      return buildObjCMethodList(InstanceMethods);
4637    }
4638
4639    /// \brief Retrieve the instance methods found by this visitor.
4640    ObjCMethodList getFactoryMethods() const {
4641      return buildObjCMethodList(FactoryMethods);
4642    }
4643  };
4644} } // end namespace clang::serialization
4645
4646std::pair<ObjCMethodList, ObjCMethodList>
4647ASTReader::ReadMethodPool(Selector Sel) {
4648  ReadMethodPoolVisitor Visitor(*this, Sel);
4649  ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4650  std::pair<ObjCMethodList, ObjCMethodList> Result;
4651  Result.first = Visitor.getInstanceMethods();
4652  Result.second = Visitor.getFactoryMethods();
4653
4654  if (!Result.first.Method && !Result.second.Method)
4655    ++NumMethodPoolMisses;
4656  return Result;
4657}
4658
4659void ASTReader::ReadKnownNamespaces(
4660                          SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4661  Namespaces.clear();
4662
4663  for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4664    if (NamespaceDecl *Namespace
4665                = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4666      Namespaces.push_back(Namespace);
4667  }
4668}
4669
4670void ASTReader::ReadTentativeDefinitions(
4671                  SmallVectorImpl<VarDecl *> &TentativeDefs) {
4672  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4673    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4674    if (Var)
4675      TentativeDefs.push_back(Var);
4676  }
4677  TentativeDefinitions.clear();
4678}
4679
4680void ASTReader::ReadUnusedFileScopedDecls(
4681                               SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4682  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4683    DeclaratorDecl *D
4684      = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4685    if (D)
4686      Decls.push_back(D);
4687  }
4688  UnusedFileScopedDecls.clear();
4689}
4690
4691void ASTReader::ReadDelegatingConstructors(
4692                                 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4693  for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4694    CXXConstructorDecl *D
4695      = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4696    if (D)
4697      Decls.push_back(D);
4698  }
4699  DelegatingCtorDecls.clear();
4700}
4701
4702void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4703  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4704    TypedefNameDecl *D
4705      = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4706    if (D)
4707      Decls.push_back(D);
4708  }
4709  ExtVectorDecls.clear();
4710}
4711
4712void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4713  for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4714    CXXRecordDecl *D
4715      = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4716    if (D)
4717      Decls.push_back(D);
4718  }
4719  DynamicClasses.clear();
4720}
4721
4722void
4723ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4724  for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4725    NamedDecl *D
4726      = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4727    if (D)
4728      Decls.push_back(D);
4729  }
4730  LocallyScopedExternalDecls.clear();
4731}
4732
4733void ASTReader::ReadReferencedSelectors(
4734       SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4735  if (ReferencedSelectorsData.empty())
4736    return;
4737
4738  // If there are @selector references added them to its pool. This is for
4739  // implementation of -Wselector.
4740  unsigned int DataSize = ReferencedSelectorsData.size()-1;
4741  unsigned I = 0;
4742  while (I < DataSize) {
4743    Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4744    SourceLocation SelLoc
4745      = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4746    Sels.push_back(std::make_pair(Sel, SelLoc));
4747  }
4748  ReferencedSelectorsData.clear();
4749}
4750
4751void ASTReader::ReadWeakUndeclaredIdentifiers(
4752       SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4753  if (WeakUndeclaredIdentifiers.empty())
4754    return;
4755
4756  for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4757    IdentifierInfo *WeakId
4758      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4759    IdentifierInfo *AliasId
4760      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4761    SourceLocation Loc
4762      = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4763    bool Used = WeakUndeclaredIdentifiers[I++];
4764    WeakInfo WI(AliasId, Loc);
4765    WI.setUsed(Used);
4766    WeakIDs.push_back(std::make_pair(WeakId, WI));
4767  }
4768  WeakUndeclaredIdentifiers.clear();
4769}
4770
4771void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4772  for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4773    ExternalVTableUse VT;
4774    VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4775    VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4776    VT.DefinitionRequired = VTableUses[Idx++];
4777    VTables.push_back(VT);
4778  }
4779
4780  VTableUses.clear();
4781}
4782
4783void ASTReader::ReadPendingInstantiations(
4784       SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4785  for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4786    ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4787    SourceLocation Loc
4788      = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4789    Pending.push_back(std::make_pair(D, Loc));
4790  }
4791  PendingInstantiations.clear();
4792}
4793
4794void ASTReader::LoadSelector(Selector Sel) {
4795  // It would be complicated to avoid reading the methods anyway. So don't.
4796  ReadMethodPool(Sel);
4797}
4798
4799void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
4800  assert(ID && "Non-zero identifier ID required");
4801  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4802  IdentifiersLoaded[ID - 1] = II;
4803  if (DeserializationListener)
4804    DeserializationListener->IdentifierRead(ID, II);
4805}
4806
4807/// \brief Set the globally-visible declarations associated with the given
4808/// identifier.
4809///
4810/// If the AST reader is currently in a state where the given declaration IDs
4811/// cannot safely be resolved, they are queued until it is safe to resolve
4812/// them.
4813///
4814/// \param II an IdentifierInfo that refers to one or more globally-visible
4815/// declarations.
4816///
4817/// \param DeclIDs the set of declaration IDs with the name @p II that are
4818/// visible at global scope.
4819///
4820/// \param Nonrecursive should be true to indicate that the caller knows that
4821/// this call is non-recursive, and therefore the globally-visible declarations
4822/// will not be placed onto the pending queue.
4823void
4824ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4825                              const SmallVectorImpl<uint32_t> &DeclIDs,
4826                                   bool Nonrecursive) {
4827  if (NumCurrentElementsDeserializing && !Nonrecursive) {
4828    PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4829    PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4830    PII.II = II;
4831    PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4832    return;
4833  }
4834
4835  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4836    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4837    if (SemaObj) {
4838      if (SemaObj->TUScope) {
4839        // Introduce this declaration into the translation-unit scope
4840        // and add it to the declaration chain for this identifier, so
4841        // that (unqualified) name lookup will find it.
4842        SemaObj->TUScope->AddDecl(D);
4843      }
4844      SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4845    } else {
4846      // Queue this declaration so that it will be added to the
4847      // translation unit scope and identifier's declaration chain
4848      // once a Sema object is known.
4849      PreloadedDecls.push_back(D);
4850    }
4851  }
4852}
4853
4854IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
4855  if (ID == 0)
4856    return 0;
4857
4858  if (IdentifiersLoaded.empty()) {
4859    Error("no identifier table in AST file");
4860    return 0;
4861  }
4862
4863  assert(PP && "Forgot to set Preprocessor ?");
4864  ID -= 1;
4865  if (!IdentifiersLoaded[ID]) {
4866    GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4867    assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
4868    Module *M = I->second;
4869    unsigned Index = ID - M->BaseIdentifierID;
4870    const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
4871
4872    // All of the strings in the AST file are preceded by a 16-bit length.
4873    // Extract that 16-bit length to avoid having to execute strlen().
4874    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4875    //  unsigned integers.  This is important to avoid integer overflow when
4876    //  we cast them to 'unsigned'.
4877    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4878    unsigned StrLen = (((unsigned) StrLenPtr[0])
4879                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4880    IdentifiersLoaded[ID]
4881      = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
4882    if (DeserializationListener)
4883      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4884  }
4885
4886  return IdentifiersLoaded[ID];
4887}
4888
4889IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4890  return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4891}
4892
4893IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
4894  if (LocalID < NUM_PREDEF_IDENT_IDS)
4895    return LocalID;
4896
4897  ContinuousRangeMap<uint32_t, int, 2>::iterator I
4898    = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4899  assert(I != M.IdentifierRemap.end()
4900         && "Invalid index into identifier index remap");
4901
4902  return LocalID + I->second;
4903}
4904
4905bool ASTReader::ReadSLocEntry(int ID) {
4906  return ReadSLocEntryRecord(ID) != Success;
4907}
4908
4909Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4910  return DecodeSelector(getGlobalSelectorID(M, LocalID));
4911}
4912
4913Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
4914  if (ID == 0)
4915    return Selector();
4916
4917  if (ID > SelectorsLoaded.size()) {
4918    Error("selector ID out of range in AST file");
4919    return Selector();
4920  }
4921
4922  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4923    // Load this selector from the selector table.
4924    GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4925    assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
4926    Module &M = *I->second;
4927    ASTSelectorLookupTrait Trait(*this, M);
4928    unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
4929    SelectorsLoaded[ID - 1] =
4930      Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
4931    if (DeserializationListener)
4932      DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4933  }
4934
4935  return SelectorsLoaded[ID - 1];
4936}
4937
4938Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
4939  return DecodeSelector(ID);
4940}
4941
4942uint32_t ASTReader::GetNumExternalSelectors() {
4943  // ID 0 (the null selector) is considered an external selector.
4944  return getTotalNumSelectors() + 1;
4945}
4946
4947serialization::SelectorID
4948ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4949  if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4950    return LocalID;
4951
4952  ContinuousRangeMap<uint32_t, int, 2>::iterator I
4953    = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4954  assert(I != M.SelectorRemap.end()
4955         && "Invalid index into identifier index remap");
4956
4957  return LocalID + I->second;
4958}
4959
4960DeclarationName
4961ASTReader::ReadDeclarationName(Module &F,
4962                               const RecordData &Record, unsigned &Idx) {
4963  DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4964  switch (Kind) {
4965  case DeclarationName::Identifier:
4966    return DeclarationName(GetIdentifierInfo(F, Record, Idx));
4967
4968  case DeclarationName::ObjCZeroArgSelector:
4969  case DeclarationName::ObjCOneArgSelector:
4970  case DeclarationName::ObjCMultiArgSelector:
4971    return DeclarationName(ReadSelector(F, Record, Idx));
4972
4973  case DeclarationName::CXXConstructorName:
4974    return Context->DeclarationNames.getCXXConstructorName(
4975                          Context->getCanonicalType(readType(F, Record, Idx)));
4976
4977  case DeclarationName::CXXDestructorName:
4978    return Context->DeclarationNames.getCXXDestructorName(
4979                          Context->getCanonicalType(readType(F, Record, Idx)));
4980
4981  case DeclarationName::CXXConversionFunctionName:
4982    return Context->DeclarationNames.getCXXConversionFunctionName(
4983                          Context->getCanonicalType(readType(F, Record, Idx)));
4984
4985  case DeclarationName::CXXOperatorName:
4986    return Context->DeclarationNames.getCXXOperatorName(
4987                                       (OverloadedOperatorKind)Record[Idx++]);
4988
4989  case DeclarationName::CXXLiteralOperatorName:
4990    return Context->DeclarationNames.getCXXLiteralOperatorName(
4991                                       GetIdentifierInfo(F, Record, Idx));
4992
4993  case DeclarationName::CXXUsingDirective:
4994    return DeclarationName::getUsingDirectiveName();
4995  }
4996
4997  // Required to silence GCC warning
4998  return DeclarationName();
4999}
5000
5001void ASTReader::ReadDeclarationNameLoc(Module &F,
5002                                       DeclarationNameLoc &DNLoc,
5003                                       DeclarationName Name,
5004                                      const RecordData &Record, unsigned &Idx) {
5005  switch (Name.getNameKind()) {
5006  case DeclarationName::CXXConstructorName:
5007  case DeclarationName::CXXDestructorName:
5008  case DeclarationName::CXXConversionFunctionName:
5009    DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5010    break;
5011
5012  case DeclarationName::CXXOperatorName:
5013    DNLoc.CXXOperatorName.BeginOpNameLoc
5014        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5015    DNLoc.CXXOperatorName.EndOpNameLoc
5016        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5017    break;
5018
5019  case DeclarationName::CXXLiteralOperatorName:
5020    DNLoc.CXXLiteralOperatorName.OpNameLoc
5021        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5022    break;
5023
5024  case DeclarationName::Identifier:
5025  case DeclarationName::ObjCZeroArgSelector:
5026  case DeclarationName::ObjCOneArgSelector:
5027  case DeclarationName::ObjCMultiArgSelector:
5028  case DeclarationName::CXXUsingDirective:
5029    break;
5030  }
5031}
5032
5033void ASTReader::ReadDeclarationNameInfo(Module &F,
5034                                        DeclarationNameInfo &NameInfo,
5035                                      const RecordData &Record, unsigned &Idx) {
5036  NameInfo.setName(ReadDeclarationName(F, Record, Idx));
5037  NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5038  DeclarationNameLoc DNLoc;
5039  ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5040  NameInfo.setInfo(DNLoc);
5041}
5042
5043void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
5044                                  const RecordData &Record, unsigned &Idx) {
5045  Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
5046  unsigned NumTPLists = Record[Idx++];
5047  Info.NumTemplParamLists = NumTPLists;
5048  if (NumTPLists) {
5049    Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5050    for (unsigned i=0; i != NumTPLists; ++i)
5051      Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5052  }
5053}
5054
5055TemplateName
5056ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
5057                            unsigned &Idx) {
5058  TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
5059  switch (Kind) {
5060  case TemplateName::Template:
5061      return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
5062
5063  case TemplateName::OverloadedTemplate: {
5064    unsigned size = Record[Idx++];
5065    UnresolvedSet<8> Decls;
5066    while (size--)
5067      Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
5068
5069    return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5070  }
5071
5072  case TemplateName::QualifiedTemplate: {
5073    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
5074    bool hasTemplKeyword = Record[Idx++];
5075    TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
5076    return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5077  }
5078
5079  case TemplateName::DependentTemplate: {
5080    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
5081    if (Record[Idx++])  // isIdentifier
5082      return Context->getDependentTemplateName(NNS,
5083                                               GetIdentifierInfo(F, Record,
5084                                                                 Idx));
5085    return Context->getDependentTemplateName(NNS,
5086                                         (OverloadedOperatorKind)Record[Idx++]);
5087  }
5088
5089  case TemplateName::SubstTemplateTemplateParm: {
5090    TemplateTemplateParmDecl *param
5091      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
5092    if (!param) return TemplateName();
5093    TemplateName replacement = ReadTemplateName(F, Record, Idx);
5094    return Context->getSubstTemplateTemplateParm(param, replacement);
5095  }
5096
5097  case TemplateName::SubstTemplateTemplateParmPack: {
5098    TemplateTemplateParmDecl *Param
5099      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
5100    if (!Param)
5101      return TemplateName();
5102
5103    TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5104    if (ArgPack.getKind() != TemplateArgument::Pack)
5105      return TemplateName();
5106
5107    return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5108  }
5109  }
5110
5111  assert(0 && "Unhandled template name kind!");
5112  return TemplateName();
5113}
5114
5115TemplateArgument
5116ASTReader::ReadTemplateArgument(Module &F,
5117                                const RecordData &Record, unsigned &Idx) {
5118  TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5119  switch (Kind) {
5120  case TemplateArgument::Null:
5121    return TemplateArgument();
5122  case TemplateArgument::Type:
5123    return TemplateArgument(readType(F, Record, Idx));
5124  case TemplateArgument::Declaration:
5125    return TemplateArgument(ReadDecl(F, Record, Idx));
5126  case TemplateArgument::Integral: {
5127    llvm::APSInt Value = ReadAPSInt(Record, Idx);
5128    QualType T = readType(F, Record, Idx);
5129    return TemplateArgument(Value, T);
5130  }
5131  case TemplateArgument::Template:
5132    return TemplateArgument(ReadTemplateName(F, Record, Idx));
5133  case TemplateArgument::TemplateExpansion: {
5134    TemplateName Name = ReadTemplateName(F, Record, Idx);
5135    llvm::Optional<unsigned> NumTemplateExpansions;
5136    if (unsigned NumExpansions = Record[Idx++])
5137      NumTemplateExpansions = NumExpansions - 1;
5138    return TemplateArgument(Name, NumTemplateExpansions);
5139  }
5140  case TemplateArgument::Expression:
5141    return TemplateArgument(ReadExpr(F));
5142  case TemplateArgument::Pack: {
5143    unsigned NumArgs = Record[Idx++];
5144    TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5145    for (unsigned I = 0; I != NumArgs; ++I)
5146      Args[I] = ReadTemplateArgument(F, Record, Idx);
5147    return TemplateArgument(Args, NumArgs);
5148  }
5149  }
5150
5151  assert(0 && "Unhandled template argument kind!");
5152  return TemplateArgument();
5153}
5154
5155TemplateParameterList *
5156ASTReader::ReadTemplateParameterList(Module &F,
5157                                     const RecordData &Record, unsigned &Idx) {
5158  SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5159  SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5160  SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
5161
5162  unsigned NumParams = Record[Idx++];
5163  SmallVector<NamedDecl *, 16> Params;
5164  Params.reserve(NumParams);
5165  while (NumParams--)
5166    Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
5167
5168  TemplateParameterList* TemplateParams =
5169    TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5170                                  Params.data(), Params.size(), RAngleLoc);
5171  return TemplateParams;
5172}
5173
5174void
5175ASTReader::
5176ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
5177                         Module &F, const RecordData &Record,
5178                         unsigned &Idx) {
5179  unsigned NumTemplateArgs = Record[Idx++];
5180  TemplArgs.reserve(NumTemplateArgs);
5181  while (NumTemplateArgs--)
5182    TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
5183}
5184
5185/// \brief Read a UnresolvedSet structure.
5186void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
5187                                  const RecordData &Record, unsigned &Idx) {
5188  unsigned NumDecls = Record[Idx++];
5189  while (NumDecls--) {
5190    NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
5191    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5192    Set.addDecl(D, AS);
5193  }
5194}
5195
5196CXXBaseSpecifier
5197ASTReader::ReadCXXBaseSpecifier(Module &F,
5198                                const RecordData &Record, unsigned &Idx) {
5199  bool isVirtual = static_cast<bool>(Record[Idx++]);
5200  bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5201  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
5202  bool inheritConstructors = static_cast<bool>(Record[Idx++]);
5203  TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5204  SourceRange Range = ReadSourceRange(F, Record, Idx);
5205  SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
5206  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
5207                          EllipsisLoc);
5208  Result.setInheritConstructors(inheritConstructors);
5209  return Result;
5210}
5211
5212std::pair<CXXCtorInitializer **, unsigned>
5213ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
5214                                   unsigned &Idx) {
5215  CXXCtorInitializer **CtorInitializers = 0;
5216  unsigned NumInitializers = Record[Idx++];
5217  if (NumInitializers) {
5218    ASTContext &C = *getContext();
5219
5220    CtorInitializers
5221        = new (C) CXXCtorInitializer*[NumInitializers];
5222    for (unsigned i=0; i != NumInitializers; ++i) {
5223      TypeSourceInfo *BaseClassInfo = 0;
5224      bool IsBaseVirtual = false;
5225      FieldDecl *Member = 0;
5226      IndirectFieldDecl *IndirectMember = 0;
5227      CXXConstructorDecl *Target = 0;
5228
5229      CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5230      switch (Type) {
5231       case CTOR_INITIALIZER_BASE:
5232        BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
5233        IsBaseVirtual = Record[Idx++];
5234        break;
5235
5236       case CTOR_INITIALIZER_DELEGATING:
5237        Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
5238        break;
5239
5240       case CTOR_INITIALIZER_MEMBER:
5241        Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
5242        break;
5243
5244       case CTOR_INITIALIZER_INDIRECT_MEMBER:
5245        IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
5246        break;
5247      }
5248
5249      SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
5250      Expr *Init = ReadExpr(F);
5251      SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5252      SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
5253      bool IsWritten = Record[Idx++];
5254      unsigned SourceOrderOrNumArrayIndices;
5255      SmallVector<VarDecl *, 8> Indices;
5256      if (IsWritten) {
5257        SourceOrderOrNumArrayIndices = Record[Idx++];
5258      } else {
5259        SourceOrderOrNumArrayIndices = Record[Idx++];
5260        Indices.reserve(SourceOrderOrNumArrayIndices);
5261        for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
5262          Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
5263      }
5264
5265      CXXCtorInitializer *BOMInit;
5266      if (Type == CTOR_INITIALIZER_BASE) {
5267        BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5268                                             LParenLoc, Init, RParenLoc,
5269                                             MemberOrEllipsisLoc);
5270      } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5271        BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5272                                             Target, Init, RParenLoc);
5273      } else if (IsWritten) {
5274        if (Member)
5275          BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5276                                               LParenLoc, Init, RParenLoc);
5277        else
5278          BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5279                                               MemberOrEllipsisLoc, LParenLoc,
5280                                               Init, RParenLoc);
5281      } else {
5282        BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5283                                             LParenLoc, Init, RParenLoc,
5284                                             Indices.data(), Indices.size());
5285      }
5286
5287      if (IsWritten)
5288        BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
5289      CtorInitializers[i] = BOMInit;
5290    }
5291  }
5292
5293  return std::make_pair(CtorInitializers, NumInitializers);
5294}
5295
5296NestedNameSpecifier *
5297ASTReader::ReadNestedNameSpecifier(Module &F,
5298                                   const RecordData &Record, unsigned &Idx) {
5299  unsigned N = Record[Idx++];
5300  NestedNameSpecifier *NNS = 0, *Prev = 0;
5301  for (unsigned I = 0; I != N; ++I) {
5302    NestedNameSpecifier::SpecifierKind Kind
5303      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5304    switch (Kind) {
5305    case NestedNameSpecifier::Identifier: {
5306      IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5307      NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5308      break;
5309    }
5310
5311    case NestedNameSpecifier::Namespace: {
5312      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5313      NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5314      break;
5315    }
5316
5317    case NestedNameSpecifier::NamespaceAlias: {
5318      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5319      NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5320      break;
5321    }
5322
5323    case NestedNameSpecifier::TypeSpec:
5324    case NestedNameSpecifier::TypeSpecWithTemplate: {
5325      const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
5326      if (!T)
5327        return 0;
5328
5329      bool Template = Record[Idx++];
5330      NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5331      break;
5332    }
5333
5334    case NestedNameSpecifier::Global: {
5335      NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5336      // No associated value, and there can't be a prefix.
5337      break;
5338    }
5339    }
5340    Prev = NNS;
5341  }
5342  return NNS;
5343}
5344
5345NestedNameSpecifierLoc
5346ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
5347                                      unsigned &Idx) {
5348  unsigned N = Record[Idx++];
5349  NestedNameSpecifierLocBuilder Builder;
5350  for (unsigned I = 0; I != N; ++I) {
5351    NestedNameSpecifier::SpecifierKind Kind
5352      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5353    switch (Kind) {
5354    case NestedNameSpecifier::Identifier: {
5355      IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5356      SourceRange Range = ReadSourceRange(F, Record, Idx);
5357      Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
5358      break;
5359    }
5360
5361    case NestedNameSpecifier::Namespace: {
5362      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5363      SourceRange Range = ReadSourceRange(F, Record, Idx);
5364      Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
5365      break;
5366    }
5367
5368    case NestedNameSpecifier::NamespaceAlias: {
5369      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5370      SourceRange Range = ReadSourceRange(F, Record, Idx);
5371      Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
5372      break;
5373    }
5374
5375    case NestedNameSpecifier::TypeSpec:
5376    case NestedNameSpecifier::TypeSpecWithTemplate: {
5377      bool Template = Record[Idx++];
5378      TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5379      if (!T)
5380        return NestedNameSpecifierLoc();
5381      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5382
5383      // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5384      Builder.Extend(*Context,
5385                     Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5386                     T->getTypeLoc(), ColonColonLoc);
5387      break;
5388    }
5389
5390    case NestedNameSpecifier::Global: {
5391      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5392      Builder.MakeGlobal(*Context, ColonColonLoc);
5393      break;
5394    }
5395    }
5396  }
5397
5398  return Builder.getWithLocInContext(*Context);
5399}
5400
5401SourceRange
5402ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
5403                           unsigned &Idx) {
5404  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5405  SourceLocation end = ReadSourceLocation(F, Record, Idx);
5406  return SourceRange(beg, end);
5407}
5408
5409/// \brief Read an integral value
5410llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
5411  unsigned BitWidth = Record[Idx++];
5412  unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5413  llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5414  Idx += NumWords;
5415  return Result;
5416}
5417
5418/// \brief Read a signed integral value
5419llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
5420  bool isUnsigned = Record[Idx++];
5421  return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5422}
5423
5424/// \brief Read a floating-point value
5425llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
5426  return llvm::APFloat(ReadAPInt(Record, Idx));
5427}
5428
5429// \brief Read a string
5430std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
5431  unsigned Len = Record[Idx++];
5432  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
5433  Idx += Len;
5434  return Result;
5435}
5436
5437VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5438                                         unsigned &Idx) {
5439  unsigned Major = Record[Idx++];
5440  unsigned Minor = Record[Idx++];
5441  unsigned Subminor = Record[Idx++];
5442  if (Minor == 0)
5443    return VersionTuple(Major);
5444  if (Subminor == 0)
5445    return VersionTuple(Major, Minor - 1);
5446  return VersionTuple(Major, Minor - 1, Subminor - 1);
5447}
5448
5449CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
5450                                          const RecordData &Record,
5451                                          unsigned &Idx) {
5452  CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
5453  return CXXTemporary::Create(*Context, Decl);
5454}
5455
5456DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
5457  return Diag(SourceLocation(), DiagID);
5458}
5459
5460DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
5461  return Diags.Report(Loc, DiagID);
5462}
5463
5464/// \brief Retrieve the identifier table associated with the
5465/// preprocessor.
5466IdentifierTable &ASTReader::getIdentifierTable() {
5467  assert(PP && "Forgot to set Preprocessor ?");
5468  return PP->getIdentifierTable();
5469}
5470
5471/// \brief Record that the given ID maps to the given switch-case
5472/// statement.
5473void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
5474  assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5475  SwitchCaseStmts[ID] = SC;
5476}
5477
5478/// \brief Retrieve the switch-case statement with the given ID.
5479SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
5480  assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5481  return SwitchCaseStmts[ID];
5482}
5483
5484void ASTReader::ClearSwitchCaseIDs() {
5485  SwitchCaseStmts.clear();
5486}
5487
5488void ASTReader::FinishedDeserializing() {
5489  assert(NumCurrentElementsDeserializing &&
5490         "FinishedDeserializing not paired with StartedDeserializing");
5491  if (NumCurrentElementsDeserializing == 1) {
5492    // If any identifiers with corresponding top-level declarations have
5493    // been loaded, load those declarations now.
5494    while (!PendingIdentifierInfos.empty()) {
5495      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5496                              PendingIdentifierInfos.front().DeclIDs, true);
5497      PendingIdentifierInfos.pop_front();
5498    }
5499
5500    // Ready to load previous declarations of Decls that were delayed.
5501    while (!PendingPreviousDecls.empty()) {
5502      loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5503                                PendingPreviousDecls.front().second);
5504      PendingPreviousDecls.pop_front();
5505    }
5506
5507    // We are not in recursive loading, so it's safe to pass the "interesting"
5508    // decls to the consumer.
5509    if (Consumer)
5510      PassInterestingDeclsToConsumer();
5511
5512    assert(PendingForwardRefs.size() == 0 &&
5513           "Some forward refs did not get linked to the definition!");
5514  }
5515  --NumCurrentElementsDeserializing;
5516}
5517
5518ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
5519                     StringRef isysroot, bool DisableValidation,
5520                     bool DisableStatCache)
5521  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5522    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5523    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5524    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5525    RelocatablePCH(false), isysroot(isysroot),
5526    DisableValidation(DisableValidation),
5527    DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5528    NumSLocEntriesRead(0), TotalNumSLocEntries(0),
5529    NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5530    TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5531    NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5532    NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5533    NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5534    TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5535    NumCXXBaseSpecifiersLoaded(0)
5536{
5537  SourceMgr.setExternalSLocEntrySource(this);
5538}
5539
5540ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5541                     Diagnostic &Diags, StringRef isysroot,
5542                     bool DisableValidation, bool DisableStatCache)
5543  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5544    Diags(Diags), SemaObj(0), PP(0), Context(0),
5545    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5546    RelocatablePCH(false), isysroot(isysroot),
5547    DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5548    NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5549    TotalNumSLocEntries(0), NumStatementsRead(0),
5550    TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5551    NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5552    TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5553    TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5554    TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5555    NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
5556{
5557  SourceMgr.setExternalSLocEntrySource(this);
5558}
5559
5560ASTReader::~ASTReader() {
5561  for (DeclContextVisibleUpdatesPending::iterator
5562           I = PendingVisibleUpdates.begin(),
5563           E = PendingVisibleUpdates.end();
5564       I != E; ++I) {
5565    for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5566                                             F = I->second.end();
5567         J != F; ++J)
5568      delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
5569  }
5570}
5571