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