ASTReader.cpp revision c266de97e269c3747ffc2d122fe53185b541cb37
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
4267namespace {
4268  class FindExternalLexicalDeclsVisitor {
4269    ASTReader &Reader;
4270    const DeclContext *DC;
4271    bool (*isKindWeWant)(Decl::Kind);
4272    SmallVectorImpl<Decl*> &Decls;
4273    bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4274
4275  public:
4276    FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4277                                    bool (*isKindWeWant)(Decl::Kind),
4278                                    SmallVectorImpl<Decl*> &Decls)
4279      : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4280    {
4281      for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4282        PredefsVisited[I] = false;
4283    }
4284
4285    static bool visit(Module &M, bool Preorder, void *UserData) {
4286      if (Preorder)
4287        return false;
4288
4289      FindExternalLexicalDeclsVisitor *This
4290        = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4291
4292      Module::DeclContextInfosMap::iterator Info
4293        = M.DeclContextInfos.find(This->DC);
4294      if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4295        return false;
4296
4297      // Load all of the declaration IDs
4298      for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4299                               *IDE = ID + Info->second.NumLexicalDecls;
4300           ID != IDE; ++ID) {
4301        if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4302          continue;
4303
4304        // Don't add predefined declarations to the lexical context more
4305        // than once.
4306        if (ID->second < NUM_PREDEF_DECL_IDS) {
4307          if (This->PredefsVisited[ID->second])
4308            continue;
4309
4310          This->PredefsVisited[ID->second] = true;
4311        }
4312
4313        Decl *D = This->Reader.GetLocalDecl(M, ID->second);
4314        assert(D && "Null decl in lexical decls");
4315        This->Decls.push_back(D);
4316      }
4317
4318      return false;
4319    }
4320  };
4321}
4322
4323ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
4324                                         bool (*isKindWeWant)(Decl::Kind),
4325                                         SmallVectorImpl<Decl*> &Decls) {
4326  // There might be lexical decls in multiple modules, for the TU at
4327  // least. Walk all of the modules in the order they were loaded.
4328  FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4329  ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
4330  ++NumLexicalDeclContextsRead;
4331  return ELR_Success;
4332}
4333
4334namespace {
4335  /// \brief Module visitor used to perform name lookup into a
4336  /// declaration context.
4337  class DeclContextNameLookupVisitor {
4338    ASTReader &Reader;
4339    const DeclContext *DC;
4340    DeclarationName Name;
4341    SmallVectorImpl<NamedDecl *> &Decls;
4342
4343  public:
4344    DeclContextNameLookupVisitor(ASTReader &Reader,
4345                                 const DeclContext *DC, DeclarationName Name,
4346                                 SmallVectorImpl<NamedDecl *> &Decls)
4347      : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4348
4349    static bool visit(Module &M, void *UserData) {
4350      DeclContextNameLookupVisitor *This
4351        = static_cast<DeclContextNameLookupVisitor *>(UserData);
4352
4353      // Check whether we have any visible declaration information for
4354      // this context in this module.
4355      Module::DeclContextInfosMap::iterator Info
4356        = M.DeclContextInfos.find(This->DC);
4357      if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4358        return false;
4359
4360      // Look for this name within this module.
4361      ASTDeclContextNameLookupTable *LookupTable =
4362        (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4363      ASTDeclContextNameLookupTable::iterator Pos
4364        = LookupTable->find(This->Name);
4365      if (Pos == LookupTable->end())
4366        return false;
4367
4368      bool FoundAnything = false;
4369      ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4370      for (; Data.first != Data.second; ++Data.first) {
4371        NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4372        if (!ND)
4373          continue;
4374
4375        if (ND->getDeclName() != This->Name) {
4376          assert(!This->Name.getCXXNameType().isNull() &&
4377                 "Name mismatch without a type");
4378          continue;
4379        }
4380
4381        // Record this declaration.
4382        FoundAnything = true;
4383        This->Decls.push_back(ND);
4384      }
4385
4386      return FoundAnything;
4387    }
4388  };
4389}
4390
4391DeclContext::lookup_result
4392ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
4393                                          DeclarationName Name) {
4394  assert(DC->hasExternalVisibleStorage() &&
4395         "DeclContext has no visible decls in storage");
4396  if (!Name)
4397    return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4398                                      DeclContext::lookup_iterator(0));
4399
4400  SmallVector<NamedDecl *, 64> Decls;
4401  DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4402  ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
4403  ++NumVisibleDeclContextsRead;
4404  SetExternalVisibleDeclsForName(DC, Name, Decls);
4405  return const_cast<DeclContext*>(DC)->lookup(Name);
4406}
4407
4408void ASTReader::PassInterestingDeclsToConsumer() {
4409  assert(Consumer);
4410  while (!InterestingDecls.empty()) {
4411    DeclGroupRef DG(InterestingDecls.front());
4412    InterestingDecls.pop_front();
4413    Consumer->HandleInterestingDecl(DG);
4414  }
4415}
4416
4417void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
4418  this->Consumer = Consumer;
4419
4420  if (!Consumer)
4421    return;
4422
4423  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
4424    // Force deserialization of this decl, which will cause it to be queued for
4425    // passing to the consumer.
4426    GetDecl(ExternalDefinitions[I]);
4427  }
4428
4429  PassInterestingDeclsToConsumer();
4430}
4431
4432void ASTReader::PrintStats() {
4433  std::fprintf(stderr, "*** AST File Statistics:\n");
4434
4435  unsigned NumTypesLoaded
4436    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
4437                                      QualType());
4438  unsigned NumDeclsLoaded
4439    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4440                                      (Decl *)0);
4441  unsigned NumIdentifiersLoaded
4442    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4443                                            IdentifiersLoaded.end(),
4444                                            (IdentifierInfo *)0);
4445  unsigned NumSelectorsLoaded
4446    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4447                                          SelectorsLoaded.end(),
4448                                          Selector());
4449
4450  std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
4451  std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
4452  if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
4453    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
4454                 NumSLocEntriesRead, TotalNumSLocEntries,
4455                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
4456  if (!TypesLoaded.empty())
4457    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
4458                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4459                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4460  if (!DeclsLoaded.empty())
4461    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
4462                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4463                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
4464  if (!IdentifiersLoaded.empty())
4465    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
4466                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4467                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
4468  if (!SelectorsLoaded.empty())
4469    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
4470                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4471                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
4472  if (TotalNumStatements)
4473    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
4474                 NumStatementsRead, TotalNumStatements,
4475                 ((float)NumStatementsRead/TotalNumStatements * 100));
4476  if (TotalNumMacros)
4477    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
4478                 NumMacrosRead, TotalNumMacros,
4479                 ((float)NumMacrosRead/TotalNumMacros * 100));
4480  if (TotalLexicalDeclContexts)
4481    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
4482                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4483                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4484                  * 100));
4485  if (TotalVisibleDeclContexts)
4486    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
4487                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4488                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4489                  * 100));
4490  if (TotalNumMethodPoolEntries) {
4491    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
4492                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4493                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4494                  * 100));
4495    std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
4496  }
4497  std::fprintf(stderr, "\n");
4498  dump();
4499  std::fprintf(stderr, "\n");
4500}
4501
4502template<typename Key, typename Module, unsigned InitialCapacity>
4503static void
4504dumpModuleIDMap(StringRef Name,
4505                const ContinuousRangeMap<Key, Module *,
4506                                         InitialCapacity> &Map) {
4507  if (Map.begin() == Map.end())
4508    return;
4509
4510  typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
4511  llvm::errs() << Name << ":\n";
4512  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4513       I != IEnd; ++I) {
4514    llvm::errs() << "  " << I->first << " -> " << I->second->FileName
4515      << "\n";
4516  }
4517}
4518
4519void ASTReader::dump() {
4520  llvm::errs() << "*** PCH/Module Remappings:\n";
4521  dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
4522  dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
4523  dumpModuleIDMap("Global type map", GlobalTypeMap);
4524  dumpModuleIDMap("Global declaration map", GlobalDeclMap);
4525  dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4526  dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4527  dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4528  dumpModuleIDMap("Global preprocessed entity map",
4529                  GlobalPreprocessedEntityMap);
4530
4531  llvm::errs() << "\n*** PCH/Modules Loaded:";
4532  for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4533                                       MEnd = ModuleMgr.end();
4534       M != MEnd; ++M)
4535    (*M)->dump();
4536}
4537
4538/// Return the amount of memory used by memory buffers, breaking down
4539/// by heap-backed versus mmap'ed memory.
4540void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4541  for (ModuleConstIterator I = ModuleMgr.begin(),
4542      E = ModuleMgr.end(); I != E; ++I) {
4543    if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
4544      size_t bytes = buf->getBufferSize();
4545      switch (buf->getBufferKind()) {
4546        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4547          sizes.malloc_bytes += bytes;
4548          break;
4549        case llvm::MemoryBuffer::MemoryBuffer_MMap:
4550          sizes.mmap_bytes += bytes;
4551          break;
4552      }
4553    }
4554  }
4555}
4556
4557void ASTReader::InitializeSema(Sema &S) {
4558  SemaObj = &S;
4559  S.ExternalSource = this;
4560
4561  // Makes sure any declarations that were deserialized "too early"
4562  // still get added to the identifier's declaration chains.
4563  for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4564    if (SemaObj->TUScope)
4565      SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4566
4567    SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4568  }
4569  PreloadedDecls.clear();
4570
4571  // Load the offsets of the declarations that Sema references.
4572  // They will be lazily deserialized when needed.
4573  if (!SemaDeclRefs.empty()) {
4574    assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4575    if (!SemaObj->StdNamespace)
4576      SemaObj->StdNamespace = SemaDeclRefs[0];
4577    if (!SemaObj->StdBadAlloc)
4578      SemaObj->StdBadAlloc = SemaDeclRefs[1];
4579  }
4580
4581  if (!FPPragmaOptions.empty()) {
4582    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4583    SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4584  }
4585
4586  if (!OpenCLExtensions.empty()) {
4587    unsigned I = 0;
4588#define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4589#include "clang/Basic/OpenCLExtensions.def"
4590
4591    assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4592  }
4593}
4594
4595namespace {
4596  /// \brief Visitor class used to look up identifirs in
4597  class IdentifierLookupVisitor {
4598    StringRef Name;
4599    IdentifierInfo *Found;
4600  public:
4601    explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
4602
4603    static bool visit(Module &M, void *UserData) {
4604      IdentifierLookupVisitor *This
4605        = static_cast<IdentifierLookupVisitor *>(UserData);
4606
4607      ASTIdentifierLookupTable *IdTable
4608        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4609      if (!IdTable)
4610        return false;
4611
4612      std::pair<const char*, unsigned> Key(This->Name.begin(),
4613                                           This->Name.size());
4614      ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4615      if (Pos == IdTable->end())
4616        return false;
4617
4618      // Dereferencing the iterator has the effect of building the
4619      // IdentifierInfo node and populating it with the various
4620      // declarations it needs.
4621      This->Found = *Pos;
4622      return true;
4623    }
4624
4625    // \brief Retrieve the identifier info found within the module
4626    // files.
4627    IdentifierInfo *getIdentifierInfo() const { return Found; }
4628  };
4629}
4630
4631IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4632  IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4633  ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4634  return Visitor.getIdentifierInfo();
4635}
4636
4637namespace clang {
4638  /// \brief An identifier-lookup iterator that enumerates all of the
4639  /// identifiers stored within a set of AST files.
4640  class ASTIdentifierIterator : public IdentifierIterator {
4641    /// \brief The AST reader whose identifiers are being enumerated.
4642    const ASTReader &Reader;
4643
4644    /// \brief The current index into the chain of AST files stored in
4645    /// the AST reader.
4646    unsigned Index;
4647
4648    /// \brief The current position within the identifier lookup table
4649    /// of the current AST file.
4650    ASTIdentifierLookupTable::key_iterator Current;
4651
4652    /// \brief The end position within the identifier lookup table of
4653    /// the current AST file.
4654    ASTIdentifierLookupTable::key_iterator End;
4655
4656  public:
4657    explicit ASTIdentifierIterator(const ASTReader &Reader);
4658
4659    virtual StringRef Next();
4660  };
4661}
4662
4663ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4664  : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
4665  ASTIdentifierLookupTable *IdTable
4666    = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
4667  Current = IdTable->key_begin();
4668  End = IdTable->key_end();
4669}
4670
4671StringRef ASTIdentifierIterator::Next() {
4672  while (Current == End) {
4673    // If we have exhausted all of our AST files, we're done.
4674    if (Index == 0)
4675      return StringRef();
4676
4677    --Index;
4678    ASTIdentifierLookupTable *IdTable
4679      = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4680        IdentifierLookupTable;
4681    Current = IdTable->key_begin();
4682    End = IdTable->key_end();
4683  }
4684
4685  // We have any identifiers remaining in the current AST file; return
4686  // the next one.
4687  std::pair<const char*, unsigned> Key = *Current;
4688  ++Current;
4689  return StringRef(Key.first, Key.second);
4690}
4691
4692IdentifierIterator *ASTReader::getIdentifiers() const {
4693  return new ASTIdentifierIterator(*this);
4694}
4695
4696std::pair<ObjCMethodList, ObjCMethodList>
4697ASTReader::ReadMethodPool(Selector Sel) {
4698  // Find this selector in a hash table. We want to find the most recent entry.
4699  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4700    Module &F = *(*I);
4701    if (!F.SelectorLookupTable)
4702      continue;
4703
4704    ASTSelectorLookupTable *PoolTable
4705      = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4706    ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
4707    if (Pos != PoolTable->end()) {
4708      ++NumSelectorsRead;
4709      // FIXME: Not quite happy with the statistics here. We probably should
4710      // disable this tracking when called via LoadSelector.
4711      // Also, should entries without methods count as misses?
4712      ++NumMethodPoolEntriesRead;
4713      ASTSelectorLookupTrait::data_type Data = *Pos;
4714      if (DeserializationListener)
4715        DeserializationListener->SelectorRead(Data.ID, Sel);
4716      return std::make_pair(Data.Instance, Data.Factory);
4717    }
4718  }
4719
4720  ++NumMethodPoolMisses;
4721  return std::pair<ObjCMethodList, ObjCMethodList>();
4722}
4723
4724void ASTReader::ReadKnownNamespaces(
4725                          SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4726  Namespaces.clear();
4727
4728  for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4729    if (NamespaceDecl *Namespace
4730                = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4731      Namespaces.push_back(Namespace);
4732  }
4733}
4734
4735void ASTReader::ReadTentativeDefinitions(
4736                  SmallVectorImpl<VarDecl *> &TentativeDefs) {
4737  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4738    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4739    if (Var)
4740      TentativeDefs.push_back(Var);
4741  }
4742  TentativeDefinitions.clear();
4743}
4744
4745void ASTReader::ReadUnusedFileScopedDecls(
4746                               SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4747  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4748    DeclaratorDecl *D
4749      = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4750    if (D)
4751      Decls.push_back(D);
4752  }
4753  UnusedFileScopedDecls.clear();
4754}
4755
4756void ASTReader::ReadDelegatingConstructors(
4757                                 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4758  for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4759    CXXConstructorDecl *D
4760      = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4761    if (D)
4762      Decls.push_back(D);
4763  }
4764  DelegatingCtorDecls.clear();
4765}
4766
4767void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4768  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4769    TypedefNameDecl *D
4770      = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4771    if (D)
4772      Decls.push_back(D);
4773  }
4774  ExtVectorDecls.clear();
4775}
4776
4777void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4778  for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4779    CXXRecordDecl *D
4780      = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4781    if (D)
4782      Decls.push_back(D);
4783  }
4784  DynamicClasses.clear();
4785}
4786
4787void
4788ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4789  for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4790    NamedDecl *D
4791      = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4792    if (D)
4793      Decls.push_back(D);
4794  }
4795  LocallyScopedExternalDecls.clear();
4796}
4797
4798void ASTReader::ReadReferencedSelectors(
4799       SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4800  if (ReferencedSelectorsData.empty())
4801    return;
4802
4803  // If there are @selector references added them to its pool. This is for
4804  // implementation of -Wselector.
4805  unsigned int DataSize = ReferencedSelectorsData.size()-1;
4806  unsigned I = 0;
4807  while (I < DataSize) {
4808    Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4809    SourceLocation SelLoc
4810      = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4811    Sels.push_back(std::make_pair(Sel, SelLoc));
4812  }
4813  ReferencedSelectorsData.clear();
4814}
4815
4816void ASTReader::ReadWeakUndeclaredIdentifiers(
4817       SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4818  if (WeakUndeclaredIdentifiers.empty())
4819    return;
4820
4821  for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4822    IdentifierInfo *WeakId
4823      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4824    IdentifierInfo *AliasId
4825      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4826    SourceLocation Loc
4827      = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4828    bool Used = WeakUndeclaredIdentifiers[I++];
4829    WeakInfo WI(AliasId, Loc);
4830    WI.setUsed(Used);
4831    WeakIDs.push_back(std::make_pair(WeakId, WI));
4832  }
4833  WeakUndeclaredIdentifiers.clear();
4834}
4835
4836void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4837  for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4838    ExternalVTableUse VT;
4839    VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4840    VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4841    VT.DefinitionRequired = VTableUses[Idx++];
4842    VTables.push_back(VT);
4843  }
4844
4845  VTableUses.clear();
4846}
4847
4848void ASTReader::ReadPendingInstantiations(
4849       SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4850  for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4851    ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4852    SourceLocation Loc
4853      = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4854    Pending.push_back(std::make_pair(D, Loc));
4855  }
4856  PendingInstantiations.clear();
4857}
4858
4859void ASTReader::LoadSelector(Selector Sel) {
4860  // It would be complicated to avoid reading the methods anyway. So don't.
4861  ReadMethodPool(Sel);
4862}
4863
4864void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
4865  assert(ID && "Non-zero identifier ID required");
4866  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4867  IdentifiersLoaded[ID - 1] = II;
4868  if (DeserializationListener)
4869    DeserializationListener->IdentifierRead(ID, II);
4870}
4871
4872/// \brief Set the globally-visible declarations associated with the given
4873/// identifier.
4874///
4875/// If the AST reader is currently in a state where the given declaration IDs
4876/// cannot safely be resolved, they are queued until it is safe to resolve
4877/// them.
4878///
4879/// \param II an IdentifierInfo that refers to one or more globally-visible
4880/// declarations.
4881///
4882/// \param DeclIDs the set of declaration IDs with the name @p II that are
4883/// visible at global scope.
4884///
4885/// \param Nonrecursive should be true to indicate that the caller knows that
4886/// this call is non-recursive, and therefore the globally-visible declarations
4887/// will not be placed onto the pending queue.
4888void
4889ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4890                              const SmallVectorImpl<uint32_t> &DeclIDs,
4891                                   bool Nonrecursive) {
4892  if (NumCurrentElementsDeserializing && !Nonrecursive) {
4893    PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4894    PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4895    PII.II = II;
4896    PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4897    return;
4898  }
4899
4900  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4901    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4902    if (SemaObj) {
4903      if (SemaObj->TUScope) {
4904        // Introduce this declaration into the translation-unit scope
4905        // and add it to the declaration chain for this identifier, so
4906        // that (unqualified) name lookup will find it.
4907        SemaObj->TUScope->AddDecl(D);
4908      }
4909      SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4910    } else {
4911      // Queue this declaration so that it will be added to the
4912      // translation unit scope and identifier's declaration chain
4913      // once a Sema object is known.
4914      PreloadedDecls.push_back(D);
4915    }
4916  }
4917}
4918
4919IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
4920  if (ID == 0)
4921    return 0;
4922
4923  if (IdentifiersLoaded.empty()) {
4924    Error("no identifier table in AST file");
4925    return 0;
4926  }
4927
4928  assert(PP && "Forgot to set Preprocessor ?");
4929  ID -= 1;
4930  if (!IdentifiersLoaded[ID]) {
4931    GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4932    assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
4933    Module *M = I->second;
4934    unsigned Index = ID - M->BaseIdentifierID;
4935    const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
4936
4937    // All of the strings in the AST file are preceded by a 16-bit length.
4938    // Extract that 16-bit length to avoid having to execute strlen().
4939    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4940    //  unsigned integers.  This is important to avoid integer overflow when
4941    //  we cast them to 'unsigned'.
4942    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4943    unsigned StrLen = (((unsigned) StrLenPtr[0])
4944                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4945    IdentifiersLoaded[ID]
4946      = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
4947    if (DeserializationListener)
4948      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4949  }
4950
4951  return IdentifiersLoaded[ID];
4952}
4953
4954IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4955  return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4956}
4957
4958IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
4959  if (LocalID < NUM_PREDEF_IDENT_IDS)
4960    return LocalID;
4961
4962  ContinuousRangeMap<uint32_t, int, 2>::iterator I
4963    = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4964  assert(I != M.IdentifierRemap.end()
4965         && "Invalid index into identifier index remap");
4966
4967  return LocalID + I->second;
4968}
4969
4970bool ASTReader::ReadSLocEntry(int ID) {
4971  return ReadSLocEntryRecord(ID) != Success;
4972}
4973
4974Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4975  return DecodeSelector(getGlobalSelectorID(M, LocalID));
4976}
4977
4978Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
4979  if (ID == 0)
4980    return Selector();
4981
4982  if (ID > SelectorsLoaded.size()) {
4983    Error("selector ID out of range in AST file");
4984    return Selector();
4985  }
4986
4987  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4988    // Load this selector from the selector table.
4989    GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4990    assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
4991    Module &M = *I->second;
4992    ASTSelectorLookupTrait Trait(*this, M);
4993    unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
4994    SelectorsLoaded[ID - 1] =
4995      Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
4996    if (DeserializationListener)
4997      DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4998  }
4999
5000  return SelectorsLoaded[ID - 1];
5001}
5002
5003Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
5004  return DecodeSelector(ID);
5005}
5006
5007uint32_t ASTReader::GetNumExternalSelectors() {
5008  // ID 0 (the null selector) is considered an external selector.
5009  return getTotalNumSelectors() + 1;
5010}
5011
5012serialization::SelectorID
5013ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
5014  if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5015    return LocalID;
5016
5017  ContinuousRangeMap<uint32_t, int, 2>::iterator I
5018    = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5019  assert(I != M.SelectorRemap.end()
5020         && "Invalid index into identifier index remap");
5021
5022  return LocalID + I->second;
5023}
5024
5025DeclarationName
5026ASTReader::ReadDeclarationName(Module &F,
5027                               const RecordData &Record, unsigned &Idx) {
5028  DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5029  switch (Kind) {
5030  case DeclarationName::Identifier:
5031    return DeclarationName(GetIdentifierInfo(F, Record, Idx));
5032
5033  case DeclarationName::ObjCZeroArgSelector:
5034  case DeclarationName::ObjCOneArgSelector:
5035  case DeclarationName::ObjCMultiArgSelector:
5036    return DeclarationName(ReadSelector(F, Record, Idx));
5037
5038  case DeclarationName::CXXConstructorName:
5039    return Context->DeclarationNames.getCXXConstructorName(
5040                          Context->getCanonicalType(readType(F, Record, Idx)));
5041
5042  case DeclarationName::CXXDestructorName:
5043    return Context->DeclarationNames.getCXXDestructorName(
5044                          Context->getCanonicalType(readType(F, Record, Idx)));
5045
5046  case DeclarationName::CXXConversionFunctionName:
5047    return Context->DeclarationNames.getCXXConversionFunctionName(
5048                          Context->getCanonicalType(readType(F, Record, Idx)));
5049
5050  case DeclarationName::CXXOperatorName:
5051    return Context->DeclarationNames.getCXXOperatorName(
5052                                       (OverloadedOperatorKind)Record[Idx++]);
5053
5054  case DeclarationName::CXXLiteralOperatorName:
5055    return Context->DeclarationNames.getCXXLiteralOperatorName(
5056                                       GetIdentifierInfo(F, Record, Idx));
5057
5058  case DeclarationName::CXXUsingDirective:
5059    return DeclarationName::getUsingDirectiveName();
5060  }
5061
5062  // Required to silence GCC warning
5063  return DeclarationName();
5064}
5065
5066void ASTReader::ReadDeclarationNameLoc(Module &F,
5067                                       DeclarationNameLoc &DNLoc,
5068                                       DeclarationName Name,
5069                                      const RecordData &Record, unsigned &Idx) {
5070  switch (Name.getNameKind()) {
5071  case DeclarationName::CXXConstructorName:
5072  case DeclarationName::CXXDestructorName:
5073  case DeclarationName::CXXConversionFunctionName:
5074    DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5075    break;
5076
5077  case DeclarationName::CXXOperatorName:
5078    DNLoc.CXXOperatorName.BeginOpNameLoc
5079        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5080    DNLoc.CXXOperatorName.EndOpNameLoc
5081        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5082    break;
5083
5084  case DeclarationName::CXXLiteralOperatorName:
5085    DNLoc.CXXLiteralOperatorName.OpNameLoc
5086        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5087    break;
5088
5089  case DeclarationName::Identifier:
5090  case DeclarationName::ObjCZeroArgSelector:
5091  case DeclarationName::ObjCOneArgSelector:
5092  case DeclarationName::ObjCMultiArgSelector:
5093  case DeclarationName::CXXUsingDirective:
5094    break;
5095  }
5096}
5097
5098void ASTReader::ReadDeclarationNameInfo(Module &F,
5099                                        DeclarationNameInfo &NameInfo,
5100                                      const RecordData &Record, unsigned &Idx) {
5101  NameInfo.setName(ReadDeclarationName(F, Record, Idx));
5102  NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5103  DeclarationNameLoc DNLoc;
5104  ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5105  NameInfo.setInfo(DNLoc);
5106}
5107
5108void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
5109                                  const RecordData &Record, unsigned &Idx) {
5110  Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
5111  unsigned NumTPLists = Record[Idx++];
5112  Info.NumTemplParamLists = NumTPLists;
5113  if (NumTPLists) {
5114    Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5115    for (unsigned i=0; i != NumTPLists; ++i)
5116      Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5117  }
5118}
5119
5120TemplateName
5121ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
5122                            unsigned &Idx) {
5123  TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
5124  switch (Kind) {
5125  case TemplateName::Template:
5126      return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
5127
5128  case TemplateName::OverloadedTemplate: {
5129    unsigned size = Record[Idx++];
5130    UnresolvedSet<8> Decls;
5131    while (size--)
5132      Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
5133
5134    return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5135  }
5136
5137  case TemplateName::QualifiedTemplate: {
5138    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
5139    bool hasTemplKeyword = Record[Idx++];
5140    TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
5141    return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5142  }
5143
5144  case TemplateName::DependentTemplate: {
5145    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
5146    if (Record[Idx++])  // isIdentifier
5147      return Context->getDependentTemplateName(NNS,
5148                                               GetIdentifierInfo(F, Record,
5149                                                                 Idx));
5150    return Context->getDependentTemplateName(NNS,
5151                                         (OverloadedOperatorKind)Record[Idx++]);
5152  }
5153
5154  case TemplateName::SubstTemplateTemplateParm: {
5155    TemplateTemplateParmDecl *param
5156      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
5157    if (!param) return TemplateName();
5158    TemplateName replacement = ReadTemplateName(F, Record, Idx);
5159    return Context->getSubstTemplateTemplateParm(param, replacement);
5160  }
5161
5162  case TemplateName::SubstTemplateTemplateParmPack: {
5163    TemplateTemplateParmDecl *Param
5164      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
5165    if (!Param)
5166      return TemplateName();
5167
5168    TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5169    if (ArgPack.getKind() != TemplateArgument::Pack)
5170      return TemplateName();
5171
5172    return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5173  }
5174  }
5175
5176  assert(0 && "Unhandled template name kind!");
5177  return TemplateName();
5178}
5179
5180TemplateArgument
5181ASTReader::ReadTemplateArgument(Module &F,
5182                                const RecordData &Record, unsigned &Idx) {
5183  TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5184  switch (Kind) {
5185  case TemplateArgument::Null:
5186    return TemplateArgument();
5187  case TemplateArgument::Type:
5188    return TemplateArgument(readType(F, Record, Idx));
5189  case TemplateArgument::Declaration:
5190    return TemplateArgument(ReadDecl(F, Record, Idx));
5191  case TemplateArgument::Integral: {
5192    llvm::APSInt Value = ReadAPSInt(Record, Idx);
5193    QualType T = readType(F, Record, Idx);
5194    return TemplateArgument(Value, T);
5195  }
5196  case TemplateArgument::Template:
5197    return TemplateArgument(ReadTemplateName(F, Record, Idx));
5198  case TemplateArgument::TemplateExpansion: {
5199    TemplateName Name = ReadTemplateName(F, Record, Idx);
5200    llvm::Optional<unsigned> NumTemplateExpansions;
5201    if (unsigned NumExpansions = Record[Idx++])
5202      NumTemplateExpansions = NumExpansions - 1;
5203    return TemplateArgument(Name, NumTemplateExpansions);
5204  }
5205  case TemplateArgument::Expression:
5206    return TemplateArgument(ReadExpr(F));
5207  case TemplateArgument::Pack: {
5208    unsigned NumArgs = Record[Idx++];
5209    TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5210    for (unsigned I = 0; I != NumArgs; ++I)
5211      Args[I] = ReadTemplateArgument(F, Record, Idx);
5212    return TemplateArgument(Args, NumArgs);
5213  }
5214  }
5215
5216  assert(0 && "Unhandled template argument kind!");
5217  return TemplateArgument();
5218}
5219
5220TemplateParameterList *
5221ASTReader::ReadTemplateParameterList(Module &F,
5222                                     const RecordData &Record, unsigned &Idx) {
5223  SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5224  SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5225  SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
5226
5227  unsigned NumParams = Record[Idx++];
5228  SmallVector<NamedDecl *, 16> Params;
5229  Params.reserve(NumParams);
5230  while (NumParams--)
5231    Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
5232
5233  TemplateParameterList* TemplateParams =
5234    TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5235                                  Params.data(), Params.size(), RAngleLoc);
5236  return TemplateParams;
5237}
5238
5239void
5240ASTReader::
5241ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
5242                         Module &F, const RecordData &Record,
5243                         unsigned &Idx) {
5244  unsigned NumTemplateArgs = Record[Idx++];
5245  TemplArgs.reserve(NumTemplateArgs);
5246  while (NumTemplateArgs--)
5247    TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
5248}
5249
5250/// \brief Read a UnresolvedSet structure.
5251void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
5252                                  const RecordData &Record, unsigned &Idx) {
5253  unsigned NumDecls = Record[Idx++];
5254  while (NumDecls--) {
5255    NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
5256    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5257    Set.addDecl(D, AS);
5258  }
5259}
5260
5261CXXBaseSpecifier
5262ASTReader::ReadCXXBaseSpecifier(Module &F,
5263                                const RecordData &Record, unsigned &Idx) {
5264  bool isVirtual = static_cast<bool>(Record[Idx++]);
5265  bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5266  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
5267  bool inheritConstructors = static_cast<bool>(Record[Idx++]);
5268  TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5269  SourceRange Range = ReadSourceRange(F, Record, Idx);
5270  SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
5271  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
5272                          EllipsisLoc);
5273  Result.setInheritConstructors(inheritConstructors);
5274  return Result;
5275}
5276
5277std::pair<CXXCtorInitializer **, unsigned>
5278ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
5279                                   unsigned &Idx) {
5280  CXXCtorInitializer **CtorInitializers = 0;
5281  unsigned NumInitializers = Record[Idx++];
5282  if (NumInitializers) {
5283    ASTContext &C = *getContext();
5284
5285    CtorInitializers
5286        = new (C) CXXCtorInitializer*[NumInitializers];
5287    for (unsigned i=0; i != NumInitializers; ++i) {
5288      TypeSourceInfo *BaseClassInfo = 0;
5289      bool IsBaseVirtual = false;
5290      FieldDecl *Member = 0;
5291      IndirectFieldDecl *IndirectMember = 0;
5292      CXXConstructorDecl *Target = 0;
5293
5294      CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5295      switch (Type) {
5296       case CTOR_INITIALIZER_BASE:
5297        BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
5298        IsBaseVirtual = Record[Idx++];
5299        break;
5300
5301       case CTOR_INITIALIZER_DELEGATING:
5302        Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
5303        break;
5304
5305       case CTOR_INITIALIZER_MEMBER:
5306        Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
5307        break;
5308
5309       case CTOR_INITIALIZER_INDIRECT_MEMBER:
5310        IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
5311        break;
5312      }
5313
5314      SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
5315      Expr *Init = ReadExpr(F);
5316      SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5317      SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
5318      bool IsWritten = Record[Idx++];
5319      unsigned SourceOrderOrNumArrayIndices;
5320      SmallVector<VarDecl *, 8> Indices;
5321      if (IsWritten) {
5322        SourceOrderOrNumArrayIndices = Record[Idx++];
5323      } else {
5324        SourceOrderOrNumArrayIndices = Record[Idx++];
5325        Indices.reserve(SourceOrderOrNumArrayIndices);
5326        for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
5327          Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
5328      }
5329
5330      CXXCtorInitializer *BOMInit;
5331      if (Type == CTOR_INITIALIZER_BASE) {
5332        BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5333                                             LParenLoc, Init, RParenLoc,
5334                                             MemberOrEllipsisLoc);
5335      } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5336        BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5337                                             Target, Init, RParenLoc);
5338      } else if (IsWritten) {
5339        if (Member)
5340          BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5341                                               LParenLoc, Init, RParenLoc);
5342        else
5343          BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5344                                               MemberOrEllipsisLoc, LParenLoc,
5345                                               Init, RParenLoc);
5346      } else {
5347        BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5348                                             LParenLoc, Init, RParenLoc,
5349                                             Indices.data(), Indices.size());
5350      }
5351
5352      if (IsWritten)
5353        BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
5354      CtorInitializers[i] = BOMInit;
5355    }
5356  }
5357
5358  return std::make_pair(CtorInitializers, NumInitializers);
5359}
5360
5361NestedNameSpecifier *
5362ASTReader::ReadNestedNameSpecifier(Module &F,
5363                                   const RecordData &Record, unsigned &Idx) {
5364  unsigned N = Record[Idx++];
5365  NestedNameSpecifier *NNS = 0, *Prev = 0;
5366  for (unsigned I = 0; I != N; ++I) {
5367    NestedNameSpecifier::SpecifierKind Kind
5368      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5369    switch (Kind) {
5370    case NestedNameSpecifier::Identifier: {
5371      IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5372      NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5373      break;
5374    }
5375
5376    case NestedNameSpecifier::Namespace: {
5377      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5378      NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5379      break;
5380    }
5381
5382    case NestedNameSpecifier::NamespaceAlias: {
5383      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5384      NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5385      break;
5386    }
5387
5388    case NestedNameSpecifier::TypeSpec:
5389    case NestedNameSpecifier::TypeSpecWithTemplate: {
5390      const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
5391      if (!T)
5392        return 0;
5393
5394      bool Template = Record[Idx++];
5395      NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5396      break;
5397    }
5398
5399    case NestedNameSpecifier::Global: {
5400      NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5401      // No associated value, and there can't be a prefix.
5402      break;
5403    }
5404    }
5405    Prev = NNS;
5406  }
5407  return NNS;
5408}
5409
5410NestedNameSpecifierLoc
5411ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
5412                                      unsigned &Idx) {
5413  unsigned N = Record[Idx++];
5414  NestedNameSpecifierLocBuilder Builder;
5415  for (unsigned I = 0; I != N; ++I) {
5416    NestedNameSpecifier::SpecifierKind Kind
5417      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5418    switch (Kind) {
5419    case NestedNameSpecifier::Identifier: {
5420      IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
5421      SourceRange Range = ReadSourceRange(F, Record, Idx);
5422      Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
5423      break;
5424    }
5425
5426    case NestedNameSpecifier::Namespace: {
5427      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5428      SourceRange Range = ReadSourceRange(F, Record, Idx);
5429      Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
5430      break;
5431    }
5432
5433    case NestedNameSpecifier::NamespaceAlias: {
5434      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5435      SourceRange Range = ReadSourceRange(F, Record, Idx);
5436      Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
5437      break;
5438    }
5439
5440    case NestedNameSpecifier::TypeSpec:
5441    case NestedNameSpecifier::TypeSpecWithTemplate: {
5442      bool Template = Record[Idx++];
5443      TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5444      if (!T)
5445        return NestedNameSpecifierLoc();
5446      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5447
5448      // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5449      Builder.Extend(*Context,
5450                     Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5451                     T->getTypeLoc(), ColonColonLoc);
5452      break;
5453    }
5454
5455    case NestedNameSpecifier::Global: {
5456      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5457      Builder.MakeGlobal(*Context, ColonColonLoc);
5458      break;
5459    }
5460    }
5461  }
5462
5463  return Builder.getWithLocInContext(*Context);
5464}
5465
5466SourceRange
5467ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
5468                           unsigned &Idx) {
5469  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5470  SourceLocation end = ReadSourceLocation(F, Record, Idx);
5471  return SourceRange(beg, end);
5472}
5473
5474/// \brief Read an integral value
5475llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
5476  unsigned BitWidth = Record[Idx++];
5477  unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5478  llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5479  Idx += NumWords;
5480  return Result;
5481}
5482
5483/// \brief Read a signed integral value
5484llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
5485  bool isUnsigned = Record[Idx++];
5486  return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5487}
5488
5489/// \brief Read a floating-point value
5490llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
5491  return llvm::APFloat(ReadAPInt(Record, Idx));
5492}
5493
5494// \brief Read a string
5495std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
5496  unsigned Len = Record[Idx++];
5497  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
5498  Idx += Len;
5499  return Result;
5500}
5501
5502VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5503                                         unsigned &Idx) {
5504  unsigned Major = Record[Idx++];
5505  unsigned Minor = Record[Idx++];
5506  unsigned Subminor = Record[Idx++];
5507  if (Minor == 0)
5508    return VersionTuple(Major);
5509  if (Subminor == 0)
5510    return VersionTuple(Major, Minor - 1);
5511  return VersionTuple(Major, Minor - 1, Subminor - 1);
5512}
5513
5514CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
5515                                          const RecordData &Record,
5516                                          unsigned &Idx) {
5517  CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
5518  return CXXTemporary::Create(*Context, Decl);
5519}
5520
5521DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
5522  return Diag(SourceLocation(), DiagID);
5523}
5524
5525DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
5526  return Diags.Report(Loc, DiagID);
5527}
5528
5529/// \brief Retrieve the identifier table associated with the
5530/// preprocessor.
5531IdentifierTable &ASTReader::getIdentifierTable() {
5532  assert(PP && "Forgot to set Preprocessor ?");
5533  return PP->getIdentifierTable();
5534}
5535
5536/// \brief Record that the given ID maps to the given switch-case
5537/// statement.
5538void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
5539  assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5540  SwitchCaseStmts[ID] = SC;
5541}
5542
5543/// \brief Retrieve the switch-case statement with the given ID.
5544SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
5545  assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5546  return SwitchCaseStmts[ID];
5547}
5548
5549void ASTReader::ClearSwitchCaseIDs() {
5550  SwitchCaseStmts.clear();
5551}
5552
5553void ASTReader::FinishedDeserializing() {
5554  assert(NumCurrentElementsDeserializing &&
5555         "FinishedDeserializing not paired with StartedDeserializing");
5556  if (NumCurrentElementsDeserializing == 1) {
5557    // If any identifiers with corresponding top-level declarations have
5558    // been loaded, load those declarations now.
5559    while (!PendingIdentifierInfos.empty()) {
5560      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5561                              PendingIdentifierInfos.front().DeclIDs, true);
5562      PendingIdentifierInfos.pop_front();
5563    }
5564
5565    // Ready to load previous declarations of Decls that were delayed.
5566    while (!PendingPreviousDecls.empty()) {
5567      loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5568                                PendingPreviousDecls.front().second);
5569      PendingPreviousDecls.pop_front();
5570    }
5571
5572    // We are not in recursive loading, so it's safe to pass the "interesting"
5573    // decls to the consumer.
5574    if (Consumer)
5575      PassInterestingDeclsToConsumer();
5576
5577    assert(PendingForwardRefs.size() == 0 &&
5578           "Some forward refs did not get linked to the definition!");
5579  }
5580  --NumCurrentElementsDeserializing;
5581}
5582
5583ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
5584                     StringRef isysroot, bool DisableValidation,
5585                     bool DisableStatCache)
5586  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5587    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5588    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5589    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5590    RelocatablePCH(false), isysroot(isysroot),
5591    DisableValidation(DisableValidation),
5592    DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5593    NumSLocEntriesRead(0), TotalNumSLocEntries(0),
5594    NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5595    TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5596    NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5597    NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5598    NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5599    TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5600    NumCXXBaseSpecifiersLoaded(0)
5601{
5602  SourceMgr.setExternalSLocEntrySource(this);
5603}
5604
5605ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5606                     Diagnostic &Diags, StringRef isysroot,
5607                     bool DisableValidation, bool DisableStatCache)
5608  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5609    Diags(Diags), SemaObj(0), PP(0), Context(0),
5610    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5611    RelocatablePCH(false), isysroot(isysroot),
5612    DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5613    NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5614    TotalNumSLocEntries(0), NumStatementsRead(0),
5615    TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5616    NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5617    TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5618    TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5619    TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5620    NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
5621{
5622  SourceMgr.setExternalSLocEntrySource(this);
5623}
5624
5625ASTReader::~ASTReader() {
5626  for (DeclContextVisibleUpdatesPending::iterator
5627           I = PendingVisibleUpdates.begin(),
5628           E = PendingVisibleUpdates.end();
5629       I != E; ++I) {
5630    for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5631                                             F = I->second.end();
5632         J != F; ++J)
5633      delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
5634  }
5635}
5636
5637//===----------------------------------------------------------------------===//
5638// Module implementation
5639//===----------------------------------------------------------------------===//
5640Module::Module(ModuleKind Kind)
5641  : Kind(Kind), DirectlyImported(false), SizeInBits(0),
5642    LocalNumSLocEntries(0), SLocEntryBaseID(0),
5643    SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5644    SLocFileOffsets(0), LocalNumIdentifiers(0),
5645    IdentifierOffsets(0), BaseIdentifierID(0), IdentifierTableData(0),
5646    IdentifierLookupTable(0), BasePreprocessedEntityID(0),
5647    LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
5648    BaseMacroDefinitionID(0), LocalNumHeaderFileInfos(0),
5649    HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
5650    HeaderFileFrameworkStrings(0),
5651    LocalNumSelectors(0), SelectorOffsets(0), BaseSelectorID(0),
5652    SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
5653    DeclOffsets(0), BaseDeclID(0),
5654    LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5655    LocalNumTypes(0), TypeOffsets(0), BaseTypeIndex(0), StatCache(0),
5656    NumPreallocatedPreprocessingEntities(0)
5657{}
5658
5659Module::~Module() {
5660  for (DeclContextInfosMap::iterator I = DeclContextInfos.begin(),
5661                                     E = DeclContextInfos.end();
5662       I != E; ++I) {
5663    if (I->second.NameLookupTableData)
5664      delete static_cast<ASTDeclContextNameLookupTable*>(
5665               I->second.NameLookupTableData);
5666  }
5667
5668  delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
5669  delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
5670  delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5671}
5672
5673template<typename Key, typename Offset, unsigned InitialCapacity>
5674static void
5675dumpLocalRemap(StringRef Name,
5676               const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
5677  if (Map.begin() == Map.end())
5678    return;
5679
5680  typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
5681  llvm::errs() << "  " << Name << ":\n";
5682  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5683       I != IEnd; ++I) {
5684    llvm::errs() << "    " << I->first << " -> " << I->second
5685    << "\n";
5686  }
5687}
5688
5689void Module::dump() {
5690  llvm::errs() << "\nModule: " << FileName << "\n";
5691  if (!Imports.empty()) {
5692    llvm::errs() << "  Imports: ";
5693    for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
5694      if (I)
5695        llvm::errs() << ", ";
5696      llvm::errs() << Imports[I]->FileName;
5697    }
5698    llvm::errs() << "\n";
5699  }
5700
5701  // Remapping tables.
5702  llvm::errs() << "  Base source location offset: " << SLocEntryBaseOffset
5703               << '\n';
5704  dumpLocalRemap("Source location offset local -> global map", SLocRemap);
5705
5706  llvm::errs() << "  Base identifier ID: " << BaseIdentifierID << '\n'
5707               << "  Number of identifiers: " << LocalNumIdentifiers << '\n';
5708  dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
5709
5710  llvm::errs() << "  Base selector ID: " << BaseSelectorID << '\n'
5711               << "  Number of selectors: " << LocalNumSelectors << '\n';
5712  dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
5713
5714  llvm::errs() << "  Base preprocessed entity ID: " << BasePreprocessedEntityID
5715               << '\n'
5716               << "  Number of preprocessed entities: "
5717               << NumPreallocatedPreprocessingEntities << '\n';
5718  dumpLocalRemap("Preprocessed entity ID local -> global map",
5719                 PreprocessedEntityRemap);
5720
5721  llvm::errs() << "  Base macro definition ID: " << BaseMacroDefinitionID
5722               << '\n'
5723               << "  Number of macro definitions: " << LocalNumMacroDefinitions
5724               << '\n';
5725  dumpLocalRemap("Macro definition ID local -> global map",
5726                 MacroDefinitionRemap);
5727
5728  llvm::errs() << "  Base type index: " << BaseTypeIndex << '\n'
5729               << "  Number of types: " << LocalNumTypes << '\n';
5730  dumpLocalRemap("Type index local -> global map", TypeRemap);
5731
5732  llvm::errs() << "  Base decl ID: " << BaseDeclID << '\n'
5733               << "  Number of decls: " << LocalNumDecls << '\n';
5734  dumpLocalRemap("Decl ID local -> global map", DeclRemap);
5735}
5736
5737//===----------------------------------------------------------------------===//
5738// Module manager implementation
5739//===----------------------------------------------------------------------===//
5740
5741Module *ModuleManager::lookup(StringRef Name) {
5742  const FileEntry *Entry = FileMgr.getFile(Name);
5743  return Modules[Entry];
5744}
5745
5746llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
5747  const FileEntry *Entry = FileMgr.getFile(Name);
5748  return InMemoryBuffers[Entry];
5749}
5750
5751std::pair<Module *, bool>
5752ModuleManager::addModule(StringRef FileName, ModuleKind Type,
5753                         Module *ImportedBy, std::string &ErrorStr) {
5754  const FileEntry *Entry = FileMgr.getFile(FileName);
5755  if (!Entry && FileName != "-") {
5756    ErrorStr = "file not found";
5757    return std::make_pair(static_cast<Module*>(0), false);
5758  }
5759
5760  // Check whether we already loaded this module, before
5761  Module *&ModuleEntry = Modules[Entry];
5762  bool NewModule = false;
5763  if (!ModuleEntry) {
5764    // Allocate a new module.
5765    Module *New = new Module(Type);
5766    New->FileName = FileName.str();
5767    Chain.push_back(New);
5768    NewModule = true;
5769    ModuleEntry = New;
5770
5771    // Load the contents of the module
5772    if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
5773      // The buffer was already provided for us.
5774      assert(Buffer && "Passed null buffer");
5775      New->Buffer.reset(Buffer);
5776    } else {
5777      // Open the AST file.
5778      llvm::error_code ec;
5779      if (FileName == "-") {
5780        ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
5781        if (ec)
5782          ErrorStr = ec.message();
5783      } else
5784        New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
5785
5786      if (!New->Buffer)
5787        return std::make_pair(static_cast<Module*>(0), false);
5788    }
5789
5790    // Initialize the stream
5791    New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
5792                         (const unsigned char *)New->Buffer->getBufferEnd());     }
5793
5794  if (ImportedBy) {
5795    ModuleEntry->ImportedBy.insert(ImportedBy);
5796    ImportedBy->Imports.insert(ModuleEntry);
5797  } else {
5798    ModuleEntry->DirectlyImported = true;
5799  }
5800
5801  return std::make_pair(ModuleEntry, NewModule);
5802}
5803
5804void ModuleManager::addInMemoryBuffer(StringRef FileName,
5805  llvm::MemoryBuffer *Buffer) {
5806
5807  const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
5808    Buffer->getBufferSize(), 0);
5809  InMemoryBuffers[Entry] = Buffer;
5810}
5811
5812ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
5813
5814ModuleManager::~ModuleManager() {
5815  for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5816    delete Chain[e - i - 1];
5817}
5818
5819void ModuleManager::visit(bool (*Visitor)(Module &M, void *UserData),
5820                          void *UserData) {
5821  unsigned N = size();
5822
5823  // Record the number of incoming edges for each module. When we
5824  // encounter a module with no incoming edges, push it into the queue
5825  // to seed the queue.
5826  SmallVector<Module *, 4> Queue;
5827  Queue.reserve(N);
5828  llvm::DenseMap<Module *, unsigned> UnusedIncomingEdges;
5829  for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
5830    if (unsigned Size = (*M)->ImportedBy.size())
5831      UnusedIncomingEdges[*M] = Size;
5832    else
5833      Queue.push_back(*M);
5834  }
5835
5836  llvm::SmallPtrSet<Module *, 4> Skipped;
5837  unsigned QueueStart = 0;
5838  while (QueueStart < Queue.size()) {
5839    Module *CurrentModule = Queue[QueueStart++];
5840
5841    // Check whether this module should be skipped.
5842    if (Skipped.count(CurrentModule))
5843      continue;
5844
5845    if (Visitor(*CurrentModule, UserData)) {
5846      // The visitor has requested that cut off visitation of any
5847      // module that the current module depends on. To indicate this
5848      // behavior, we mark all of the reachable modules as having N
5849      // incoming edges (which is impossible otherwise).
5850      SmallVector<Module *, 4> Stack;
5851      Stack.push_back(CurrentModule);
5852      Skipped.insert(CurrentModule);
5853      while (!Stack.empty()) {
5854        Module *NextModule = Stack.back();
5855        Stack.pop_back();
5856
5857        // For any module that this module depends on, push it on the
5858        // stack (if it hasn't already been marked as visited).
5859        for (llvm::SetVector<Module *>::iterator
5860                  M = NextModule->Imports.begin(),
5861               MEnd = NextModule->Imports.end();
5862             M != MEnd; ++M) {
5863          if (Skipped.insert(*M))
5864            Stack.push_back(*M);
5865        }
5866      }
5867      continue;
5868    }
5869
5870    // For any module that this module depends on, push it on the
5871    // stack (if it hasn't already been marked as visited).
5872    for (llvm::SetVector<Module *>::iterator M = CurrentModule->Imports.begin(),
5873                                          MEnd = CurrentModule->Imports.end();
5874         M != MEnd; ++M) {
5875
5876      // Remove our current module as an impediment to visiting the
5877      // module we depend on. If we were the last unvisited module
5878      // that depends on this particular module, push it into the
5879      // queue to be visited.
5880      unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
5881      if (NumUnusedEdges && (--NumUnusedEdges == 0))
5882        Queue.push_back(*M);
5883    }
5884  }
5885}
5886
5887/// \brief Perform a depth-first visit of the current module.
5888static bool visitDepthFirst(Module &M,
5889                            bool (*Visitor)(Module &M, bool Preorder,
5890                                            void *UserData),
5891                            void *UserData,
5892                            llvm::SmallPtrSet<Module *, 4> &Visited) {
5893  // Preorder visitation
5894  if (Visitor(M, /*Preorder=*/true, UserData))
5895    return true;
5896
5897  // Visit children
5898  for (llvm::SetVector<Module *>::iterator IM = M.Imports.begin(),
5899                                        IMEnd = M.Imports.end();
5900       IM != IMEnd; ++IM) {
5901    if (!Visited.insert(*IM))
5902      continue;
5903
5904    if (visitDepthFirst(**IM, Visitor, UserData, Visited))
5905      return true;
5906  }
5907
5908  // Postorder visitation
5909  return Visitor(M, /*Preorder=*/false, UserData);
5910}
5911
5912void ModuleManager::visitDepthFirst(bool (*Visitor)(Module &M, bool Preorder,
5913                                                    void *UserData),
5914                                    void *UserData) {
5915  llvm::SmallPtrSet<Module *, 4> Visited;
5916  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
5917    if (!Visited.insert(Chain[I]))
5918      continue;
5919
5920    if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
5921      return;
5922  }
5923}
5924