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