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