ASTReader.cpp revision 1afb661bc5444462a246cefa0effa61ef25fab29
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.DecodeIdentifierInfo(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.DecodeIdentifierInfo(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 = 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 = 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  typedef std::pair<DeclID *, DeclID *> data_type;
746
747  /// \brief Special internal key for declaration names.
748  /// The hash table creates keys for comparison; we do not create
749  /// a DeclarationName for the internal key to avoid deserializing types.
750  struct DeclNameKey {
751    DeclarationName::NameKind Kind;
752    uint64_t Data;
753    DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
754  };
755
756  typedef DeclarationName external_key_type;
757  typedef DeclNameKey internal_key_type;
758
759  explicit ASTDeclContextNameLookupTrait(ASTReader &Reader,
760                                         Module &F)
761    : Reader(Reader), F(F) { }
762
763  static bool EqualKey(const internal_key_type& a,
764                       const internal_key_type& b) {
765    return a.Kind == b.Kind && a.Data == b.Data;
766  }
767
768  unsigned ComputeHash(const DeclNameKey &Key) const {
769    llvm::FoldingSetNodeID ID;
770    ID.AddInteger(Key.Kind);
771
772    switch (Key.Kind) {
773    case DeclarationName::Identifier:
774    case DeclarationName::CXXLiteralOperatorName:
775      ID.AddString(((IdentifierInfo*)Key.Data)->getName());
776      break;
777    case DeclarationName::ObjCZeroArgSelector:
778    case DeclarationName::ObjCOneArgSelector:
779    case DeclarationName::ObjCMultiArgSelector:
780      ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
781      break;
782    case DeclarationName::CXXConstructorName:
783    case DeclarationName::CXXDestructorName:
784    case DeclarationName::CXXConversionFunctionName:
785      ID.AddInteger((TypeID)Key.Data);
786      break;
787    case DeclarationName::CXXOperatorName:
788      ID.AddInteger((OverloadedOperatorKind)Key.Data);
789      break;
790    case DeclarationName::CXXUsingDirective:
791      break;
792    }
793
794    return ID.ComputeHash();
795  }
796
797  internal_key_type GetInternalKey(const external_key_type& Name) const {
798    DeclNameKey Key;
799    Key.Kind = Name.getNameKind();
800    switch (Name.getNameKind()) {
801    case DeclarationName::Identifier:
802      Key.Data = (uint64_t)Name.getAsIdentifierInfo();
803      break;
804    case DeclarationName::ObjCZeroArgSelector:
805    case DeclarationName::ObjCOneArgSelector:
806    case DeclarationName::ObjCMultiArgSelector:
807      Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
808      break;
809    case DeclarationName::CXXConstructorName:
810    case DeclarationName::CXXDestructorName:
811    case DeclarationName::CXXConversionFunctionName:
812      Key.Data = Reader.GetTypeID(Name.getCXXNameType());
813      break;
814    case DeclarationName::CXXOperatorName:
815      Key.Data = Name.getCXXOverloadedOperator();
816      break;
817    case DeclarationName::CXXLiteralOperatorName:
818      Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
819      break;
820    case DeclarationName::CXXUsingDirective:
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.DecodeIdentifierInfo(ReadUnalignedLE32(d));
881      break;
882    case DeclarationName::ObjCZeroArgSelector:
883    case DeclarationName::ObjCOneArgSelector:
884    case DeclarationName::ObjCMultiArgSelector:
885      Key.Data =
886         (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
887      break;
888    case DeclarationName::CXXConstructorName:
889    case DeclarationName::CXXDestructorName:
890    case DeclarationName::CXXConversionFunctionName:
891      Key.Data = ReadUnalignedLE32(d); // TypeID
892      break;
893    case DeclarationName::CXXOperatorName:
894      Key.Data = *d++; // OverloadedOperatorKind
895      break;
896    case DeclarationName::CXXLiteralOperatorName:
897      Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
898      break;
899    case DeclarationName::CXXUsingDirective:
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  // Otherwise we have a PCH. It's considered to be "imported" at the first
1380  // location of its includer.
1381  if (F->Loaders.empty() || !F->Loaders[0]) {
1382    // Main file is the importer. We assume that it is the first entry in the
1383    // entry table. We can't ask the manager, because at the time of PCH loading
1384    // the main file entry doesn't exist yet.
1385    // The very first entry is the invalid instantiation loc, which takes up
1386    // offsets 0 and 1.
1387    return SourceLocation::getFromRawEncoding(2U);
1388  }
1389  return F->Loaders[0]->FirstLoc;
1390}
1391
1392/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1393/// specified cursor.  Read the abbreviations that are at the top of the block
1394/// and then leave the cursor pointing into the block.
1395bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1396                                 unsigned BlockID) {
1397  if (Cursor.EnterSubBlock(BlockID)) {
1398    Error("malformed block record in AST file");
1399    return Failure;
1400  }
1401
1402  while (true) {
1403    uint64_t Offset = Cursor.GetCurrentBitNo();
1404    unsigned Code = Cursor.ReadCode();
1405
1406    // We expect all abbrevs to be at the start of the block.
1407    if (Code != llvm::bitc::DEFINE_ABBREV) {
1408      Cursor.JumpToBit(Offset);
1409      return false;
1410    }
1411    Cursor.ReadAbbrevRecord();
1412  }
1413}
1414
1415PreprocessedEntity *ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
1416  assert(PP && "Forgot to set Preprocessor ?");
1417  llvm::BitstreamCursor &Stream = F.MacroCursor;
1418
1419  // Keep track of where we are in the stream, then jump back there
1420  // after reading this macro.
1421  SavedStreamPosition SavedPosition(Stream);
1422
1423  Stream.JumpToBit(Offset);
1424  RecordData Record;
1425  SmallVector<IdentifierInfo*, 16> MacroArgs;
1426  MacroInfo *Macro = 0;
1427
1428  while (true) {
1429    unsigned Code = Stream.ReadCode();
1430    switch (Code) {
1431    case llvm::bitc::END_BLOCK:
1432      return 0;
1433
1434    case llvm::bitc::ENTER_SUBBLOCK:
1435      // No known subblocks, always skip them.
1436      Stream.ReadSubBlockID();
1437      if (Stream.SkipBlock()) {
1438        Error("malformed block record in AST file");
1439        return 0;
1440      }
1441      continue;
1442
1443    case llvm::bitc::DEFINE_ABBREV:
1444      Stream.ReadAbbrevRecord();
1445      continue;
1446    default: break;
1447    }
1448
1449    // Read a record.
1450    const char *BlobStart = 0;
1451    unsigned BlobLen = 0;
1452    Record.clear();
1453    PreprocessorRecordTypes RecType =
1454      (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
1455                                                 BlobLen);
1456    switch (RecType) {
1457    case PP_MACRO_OBJECT_LIKE:
1458    case PP_MACRO_FUNCTION_LIKE: {
1459      // If we already have a macro, that means that we've hit the end
1460      // of the definition of the macro we were looking for. We're
1461      // done.
1462      if (Macro)
1463        return 0;
1464
1465      IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1466      if (II == 0) {
1467        Error("macro must have a name in AST file");
1468        return 0;
1469      }
1470      SourceLocation Loc = ReadSourceLocation(F, Record[1]);
1471      bool isUsed = Record[2];
1472
1473      MacroInfo *MI = PP->AllocateMacroInfo(Loc);
1474      MI->setIsUsed(isUsed);
1475      MI->setIsFromAST();
1476
1477      unsigned NextIndex = 3;
1478      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1479        // Decode function-like macro info.
1480        bool isC99VarArgs = Record[3];
1481        bool isGNUVarArgs = Record[4];
1482        MacroArgs.clear();
1483        unsigned NumArgs = Record[5];
1484        NextIndex = 6 + NumArgs;
1485        for (unsigned i = 0; i != NumArgs; ++i)
1486          MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1487
1488        // Install function-like macro info.
1489        MI->setIsFunctionLike();
1490        if (isC99VarArgs) MI->setIsC99Varargs();
1491        if (isGNUVarArgs) MI->setIsGNUVarargs();
1492        MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1493                            PP->getPreprocessorAllocator());
1494      }
1495
1496      // Finally, install the macro.
1497      PP->setMacroInfo(II, MI);
1498
1499      // Remember that we saw this macro last so that we add the tokens that
1500      // form its body to it.
1501      Macro = MI;
1502
1503      if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1504        // We have a macro definition. Load it now.
1505        PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1506                                        getMacroDefinition(Record[NextIndex]));
1507      }
1508
1509      ++NumMacrosRead;
1510      break;
1511    }
1512
1513    case PP_TOKEN: {
1514      // If we see a TOKEN before a PP_MACRO_*, then the file is
1515      // erroneous, just pretend we didn't see this.
1516      if (Macro == 0) break;
1517
1518      Token Tok;
1519      Tok.startToken();
1520      Tok.setLocation(ReadSourceLocation(F, Record[0]));
1521      Tok.setLength(Record[1]);
1522      if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1523        Tok.setIdentifierInfo(II);
1524      Tok.setKind((tok::TokenKind)Record[3]);
1525      Tok.setFlag((Token::TokenFlags)Record[4]);
1526      Macro->AddTokenToBody(Tok);
1527      break;
1528    }
1529  }
1530  }
1531
1532  return 0;
1533}
1534
1535PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
1536  assert(PP && "Forgot to set Preprocessor ?");
1537  unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1538  switch (Code) {
1539  case llvm::bitc::END_BLOCK:
1540    return 0;
1541
1542  case llvm::bitc::ENTER_SUBBLOCK:
1543    Error("unexpected subblock record in preprocessor detail block");
1544    return 0;
1545
1546  case llvm::bitc::DEFINE_ABBREV:
1547    Error("unexpected abbrevation record in preprocessor detail block");
1548    return 0;
1549
1550  default:
1551    break;
1552  }
1553
1554  if (!PP->getPreprocessingRecord()) {
1555    Error("no preprocessing record");
1556    return 0;
1557  }
1558
1559  // Read the record.
1560  PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1561  const char *BlobStart = 0;
1562  unsigned BlobLen = 0;
1563  RecordData Record;
1564  PreprocessorDetailRecordTypes RecType =
1565    (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1566                                             Code, Record, BlobStart, BlobLen);
1567  switch (RecType) {
1568  case PPD_MACRO_EXPANSION: {
1569    if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(Record[0]))
1570      return PE;
1571
1572    MacroExpansion *ME =
1573      new (PPRec) MacroExpansion(DecodeIdentifierInfo(Record[3]),
1574                                 SourceRange(ReadSourceLocation(F, Record[1]),
1575                                             ReadSourceLocation(F, Record[2])),
1576                                 getMacroDefinition(Record[4]));
1577    PPRec.setLoadedPreallocatedEntity(Record[0], ME);
1578    return ME;
1579  }
1580
1581  case PPD_MACRO_DEFINITION: {
1582    if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(Record[0]))
1583      return PE;
1584
1585    if (Record[1] > MacroDefinitionsLoaded.size()) {
1586      Error("out-of-bounds macro definition record");
1587      return 0;
1588    }
1589
1590    // Decode the identifier info and then check again; if the macro is
1591    // still defined and associated with the identifier,
1592    IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1593    if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1594      MacroDefinition *MD
1595        = new (PPRec) MacroDefinition(II,
1596                                      ReadSourceLocation(F, Record[5]),
1597                                      SourceRange(
1598                                            ReadSourceLocation(F, Record[2]),
1599                                            ReadSourceLocation(F, Record[3])));
1600
1601      PPRec.setLoadedPreallocatedEntity(Record[0], MD);
1602      MacroDefinitionsLoaded[Record[1] - 1] = MD;
1603
1604      if (DeserializationListener)
1605        DeserializationListener->MacroDefinitionRead(Record[1], MD);
1606    }
1607
1608    return MacroDefinitionsLoaded[Record[1] - 1];
1609  }
1610
1611  case PPD_INCLUSION_DIRECTIVE: {
1612    if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(Record[0]))
1613      return PE;
1614
1615    const char *FullFileNameStart = BlobStart + Record[3];
1616    const FileEntry *File
1617      = PP->getFileManager().getFile(StringRef(FullFileNameStart,
1618                                                     BlobLen - Record[3]));
1619
1620    // FIXME: Stable encoding
1621    InclusionDirective::InclusionKind Kind
1622      = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1623    InclusionDirective *ID
1624      = new (PPRec) InclusionDirective(PPRec, Kind,
1625                                       StringRef(BlobStart, Record[3]),
1626                                       Record[4],
1627                                       File,
1628                                 SourceRange(ReadSourceLocation(F, Record[1]),
1629                                             ReadSourceLocation(F, Record[2])));
1630    PPRec.setLoadedPreallocatedEntity(Record[0], ID);
1631    return ID;
1632  }
1633  }
1634
1635  Error("invalid offset in preprocessor detail block");
1636  return 0;
1637}
1638
1639namespace {
1640  /// \brief Trait class used to search the on-disk hash table containing all of
1641  /// the header search information.
1642  ///
1643  /// The on-disk hash table contains a mapping from each header path to
1644  /// information about that header (how many times it has been included, its
1645  /// controlling macro, etc.). Note that we actually hash based on the
1646  /// filename, and support "deep" comparisons of file names based on current
1647  /// inode numbers, so that the search can cope with non-normalized path names
1648  /// and symlinks.
1649  class HeaderFileInfoTrait {
1650    HeaderSearch *HS;
1651    const char *FrameworkStrings;
1652    const char *SearchPath;
1653    struct stat SearchPathStatBuf;
1654    llvm::Optional<int> SearchPathStatResult;
1655
1656    int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1657      if (Path == SearchPath) {
1658        if (!SearchPathStatResult)
1659          SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1660
1661        *StatBuf = SearchPathStatBuf;
1662        return *SearchPathStatResult;
1663      }
1664
1665      return stat(Path, StatBuf);
1666    }
1667
1668  public:
1669    typedef const char *external_key_type;
1670    typedef const char *internal_key_type;
1671
1672    typedef HeaderFileInfo data_type;
1673
1674    HeaderFileInfoTrait(HeaderSearch *HS,
1675                        const char *FrameworkStrings,
1676                        const char *SearchPath = 0)
1677      : HS(HS), FrameworkStrings(FrameworkStrings), SearchPath(SearchPath) { }
1678
1679    static unsigned ComputeHash(const char *path) {
1680      return llvm::HashString(llvm::sys::path::filename(path));
1681    }
1682
1683    static internal_key_type GetInternalKey(const char *path) { return path; }
1684
1685    bool EqualKey(internal_key_type a, internal_key_type b) {
1686      if (strcmp(a, b) == 0)
1687        return true;
1688
1689      if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1690        return false;
1691
1692      // The file names match, but the path names don't. stat() the files to
1693      // see if they are the same.
1694      struct stat StatBufA, StatBufB;
1695      if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1696        return false;
1697
1698      return StatBufA.st_ino == StatBufB.st_ino;
1699    }
1700
1701    static std::pair<unsigned, unsigned>
1702    ReadKeyDataLength(const unsigned char*& d) {
1703      unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1704      unsigned DataLen = (unsigned) *d++;
1705      return std::make_pair(KeyLen + 1, DataLen);
1706    }
1707
1708    static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1709      return (const char *)d;
1710    }
1711
1712    data_type ReadData(const internal_key_type, const unsigned char *d,
1713                       unsigned DataLen) {
1714      const unsigned char *End = d + DataLen;
1715      using namespace clang::io;
1716      HeaderFileInfo HFI;
1717      unsigned Flags = *d++;
1718      HFI.isImport = (Flags >> 5) & 0x01;
1719      HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1720      HFI.DirInfo = (Flags >> 2) & 0x03;
1721      HFI.Resolved = (Flags >> 1) & 0x01;
1722      HFI.IndexHeaderMapHeader = Flags & 0x01;
1723      HFI.NumIncludes = ReadUnalignedLE16(d);
1724      HFI.ControllingMacroID = ReadUnalignedLE32(d);
1725      if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1726        // The framework offset is 1 greater than the actual offset,
1727        // since 0 is used as an indicator for "no framework name".
1728        StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1729        HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1730      }
1731
1732      assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1733      (void)End;
1734
1735      // This HeaderFileInfo was externally loaded.
1736      HFI.External = true;
1737      return HFI;
1738    }
1739  };
1740}
1741
1742/// \brief The on-disk hash table used for the global method pool.
1743typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1744  HeaderFileInfoLookupTable;
1745
1746void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
1747                                     uint64_t Offset) {
1748  // Note that this identifier has a macro definition.
1749  II->setHasMacroDefinition(true);
1750
1751  // Adjust the offset to a global offset.
1752  UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + Offset;
1753}
1754
1755void ASTReader::ReadDefinedMacros() {
1756  for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1757      E = ModuleMgr.rend(); I != E; ++I) {
1758    llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
1759
1760    // If there was no preprocessor block, skip this file.
1761    if (!MacroCursor.getBitStreamReader())
1762      continue;
1763
1764    llvm::BitstreamCursor Cursor = MacroCursor;
1765    Cursor.JumpToBit((*I)->MacroStartOffset);
1766
1767    RecordData Record;
1768    while (true) {
1769      unsigned Code = Cursor.ReadCode();
1770      if (Code == llvm::bitc::END_BLOCK)
1771        break;
1772
1773      if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1774        // No known subblocks, always skip them.
1775        Cursor.ReadSubBlockID();
1776        if (Cursor.SkipBlock()) {
1777          Error("malformed block record in AST file");
1778          return;
1779        }
1780        continue;
1781      }
1782
1783      if (Code == llvm::bitc::DEFINE_ABBREV) {
1784        Cursor.ReadAbbrevRecord();
1785        continue;
1786      }
1787
1788      // Read a record.
1789      const char *BlobStart;
1790      unsigned BlobLen;
1791      Record.clear();
1792      switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1793      default:  // Default behavior: ignore.
1794        break;
1795
1796      case PP_MACRO_OBJECT_LIKE:
1797      case PP_MACRO_FUNCTION_LIKE:
1798        DecodeIdentifierInfo(Record[0]);
1799        break;
1800
1801      case PP_TOKEN:
1802        // Ignore tokens.
1803        break;
1804      }
1805    }
1806  }
1807
1808  // Drain the unread macro-record offsets map.
1809  while (!UnreadMacroRecordOffsets.empty())
1810    LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1811}
1812
1813void ASTReader::LoadMacroDefinition(
1814                     llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1815  assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1816  uint64_t Offset = Pos->second;
1817  UnreadMacroRecordOffsets.erase(Pos);
1818
1819  RecordLocation Loc = getLocalBitOffset(Offset);
1820  ReadMacroRecord(*Loc.F, Loc.Offset);
1821}
1822
1823void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1824  llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1825    = UnreadMacroRecordOffsets.find(II);
1826  LoadMacroDefinition(Pos);
1827}
1828
1829MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
1830  if (ID == 0 || ID > MacroDefinitionsLoaded.size())
1831    return 0;
1832
1833  if (!MacroDefinitionsLoaded[ID - 1]) {
1834    GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1835    assert(I != GlobalMacroDefinitionMap.end() &&
1836           "Corrupted global macro definition map");
1837    Module &F = *I->second.first;
1838    unsigned Index = ID - 1 + I->second.second;
1839    SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1840    F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1841    LoadPreprocessedEntity(F);
1842  }
1843
1844  return MacroDefinitionsLoaded[ID - 1];
1845}
1846
1847const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1848  std::string Filename = filenameStrRef;
1849  MaybeAddSystemRootToFilename(Filename);
1850  const FileEntry *File = FileMgr.getFile(Filename);
1851  if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1852      OriginalDir != CurrentDir) {
1853    std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1854                                                            OriginalDir,
1855                                                            CurrentDir);
1856    if (!resolved.empty())
1857      File = FileMgr.getFile(resolved);
1858  }
1859
1860  return File;
1861}
1862
1863/// \brief If we are loading a relocatable PCH file, and the filename is
1864/// not an absolute path, add the system root to the beginning of the file
1865/// name.
1866void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1867  // If this is not a relocatable PCH file, there's nothing to do.
1868  if (!RelocatablePCH)
1869    return;
1870
1871  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1872    return;
1873
1874  if (isysroot.empty()) {
1875    // If no system root was given, default to '/'
1876    Filename.insert(Filename.begin(), '/');
1877    return;
1878  }
1879
1880  unsigned Length = isysroot.size();
1881  if (isysroot[Length - 1] != '/')
1882    Filename.insert(Filename.begin(), '/');
1883
1884  Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1885}
1886
1887ASTReader::ASTReadResult
1888ASTReader::ReadASTBlock(Module &F) {
1889  llvm::BitstreamCursor &Stream = F.Stream;
1890
1891  if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1892    Error("malformed block record in AST file");
1893    return Failure;
1894  }
1895
1896  // Read all of the records and blocks for the ASt file.
1897  RecordData Record;
1898  bool First = true;
1899  while (!Stream.AtEndOfStream()) {
1900    unsigned Code = Stream.ReadCode();
1901    if (Code == llvm::bitc::END_BLOCK) {
1902      if (Stream.ReadBlockEnd()) {
1903        Error("error at end of module block in AST file");
1904        return Failure;
1905      }
1906
1907      return Success;
1908    }
1909
1910    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1911      switch (Stream.ReadSubBlockID()) {
1912      case DECLTYPES_BLOCK_ID:
1913        // We lazily load the decls block, but we want to set up the
1914        // DeclsCursor cursor to point into it.  Clone our current bitcode
1915        // cursor to it, enter the block and read the abbrevs in that block.
1916        // With the main cursor, we just skip over it.
1917        F.DeclsCursor = Stream;
1918        if (Stream.SkipBlock() ||  // Skip with the main cursor.
1919            // Read the abbrevs.
1920            ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1921          Error("malformed block record in AST file");
1922          return Failure;
1923        }
1924        break;
1925
1926      case DECL_UPDATES_BLOCK_ID:
1927        if (Stream.SkipBlock()) {
1928          Error("malformed block record in AST file");
1929          return Failure;
1930        }
1931        break;
1932
1933      case PREPROCESSOR_BLOCK_ID:
1934        F.MacroCursor = Stream;
1935        if (PP)
1936          PP->setExternalSource(this);
1937
1938        if (Stream.SkipBlock() ||
1939            ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1940          Error("malformed block record in AST file");
1941          return Failure;
1942        }
1943        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1944        break;
1945
1946      case PREPROCESSOR_DETAIL_BLOCK_ID:
1947        F.PreprocessorDetailCursor = Stream;
1948        if (Stream.SkipBlock() ||
1949            ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1950                             PREPROCESSOR_DETAIL_BLOCK_ID)) {
1951          Error("malformed preprocessor detail record in AST file");
1952          return Failure;
1953        }
1954        F.PreprocessorDetailStartOffset
1955          = F.PreprocessorDetailCursor.GetCurrentBitNo();
1956        break;
1957
1958      case SOURCE_MANAGER_BLOCK_ID:
1959        switch (ReadSourceManagerBlock(F)) {
1960        case Success:
1961          break;
1962
1963        case Failure:
1964          Error("malformed source manager block in AST file");
1965          return Failure;
1966
1967        case IgnorePCH:
1968          return IgnorePCH;
1969        }
1970        break;
1971      }
1972      First = false;
1973      continue;
1974    }
1975
1976    if (Code == llvm::bitc::DEFINE_ABBREV) {
1977      Stream.ReadAbbrevRecord();
1978      continue;
1979    }
1980
1981    // Read and process a record.
1982    Record.clear();
1983    const char *BlobStart = 0;
1984    unsigned BlobLen = 0;
1985    switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1986                                              &BlobStart, &BlobLen)) {
1987    default:  // Default behavior: ignore.
1988      break;
1989
1990    case METADATA: {
1991      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1992        Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1993                                           : diag::warn_pch_version_too_new);
1994        return IgnorePCH;
1995      }
1996
1997      RelocatablePCH = Record[4];
1998      if (Listener) {
1999        std::string TargetTriple(BlobStart, BlobLen);
2000        if (Listener->ReadTargetTriple(TargetTriple))
2001          return IgnorePCH;
2002      }
2003      break;
2004    }
2005
2006    case CHAINED_METADATA: {
2007      if (!First) {
2008        Error("CHAINED_METADATA is not first record in block");
2009        return Failure;
2010      }
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      // Load the chained file, which is always a PCH file.
2018      // FIXME: This could end up being a module.
2019      switch(ReadASTCore(StringRef(BlobStart, BlobLen), MK_PCH)) {
2020      case Failure: return Failure;
2021        // If we have to ignore the dependency, we'll have to ignore this too.
2022      case IgnorePCH: return IgnorePCH;
2023      case Success: break;
2024      }
2025      break;
2026    }
2027
2028    case TYPE_OFFSET:
2029      if (F.LocalNumTypes != 0) {
2030        Error("duplicate TYPE_OFFSET record in AST file");
2031        return Failure;
2032      }
2033      F.TypeOffsets = (const uint32_t *)BlobStart;
2034      F.LocalNumTypes = Record[0];
2035
2036      // Introduce the global -> local mapping for types within this
2037      // AST file.
2038      GlobalTypeMap.insert(std::make_pair(getTotalNumTypes() + 1,
2039                                          std::make_pair(&F,
2040                                                         -getTotalNumTypes())));
2041      TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2042      break;
2043
2044    case DECL_OFFSET:
2045      if (F.LocalNumDecls != 0) {
2046        Error("duplicate DECL_OFFSET record in AST file");
2047        return Failure;
2048      }
2049      F.DeclOffsets = (const uint32_t *)BlobStart;
2050      F.LocalNumDecls = Record[0];
2051
2052      // Introduce the global -> local mapping for declarations within this
2053      // AST file.
2054      GlobalDeclMap.insert(std::make_pair(getTotalNumDecls() + 1,
2055                                          std::make_pair(&F,
2056                                                         -getTotalNumDecls())));
2057      DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2058      break;
2059
2060    case TU_UPDATE_LEXICAL: {
2061      DeclContextInfo Info = {
2062        &F,
2063        /* No visible information */ 0,
2064        reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2065        static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair))
2066      };
2067      DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2068        .push_back(Info);
2069      break;
2070    }
2071
2072    case UPDATE_VISIBLE: {
2073      serialization::DeclID ID = Record[0];
2074      void *Table = ASTDeclContextNameLookupTable::Create(
2075                        (const unsigned char *)BlobStart + Record[1],
2076                        (const unsigned char *)BlobStart,
2077                        ASTDeclContextNameLookupTrait(*this, F));
2078      if (ID == 1 && Context) { // Is it the TU?
2079        DeclContextInfo Info = {
2080          &F, Table, /* No lexical inforamtion */ 0, 0
2081        };
2082        DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2083      } else
2084        PendingVisibleUpdates[ID].push_back(Table);
2085      break;
2086    }
2087
2088    case REDECLS_UPDATE_LATEST: {
2089      assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2090      for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
2091        DeclID First = Record[i], Latest = Record[i+1];
2092        assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2093                Latest > FirstLatestDeclIDs[First]) &&
2094               "The new latest is supposed to come after the previous latest");
2095        FirstLatestDeclIDs[First] = Latest;
2096      }
2097      break;
2098    }
2099
2100    case LANGUAGE_OPTIONS:
2101      if (ParseLanguageOptions(Record) && !DisableValidation)
2102        return IgnorePCH;
2103      break;
2104
2105    case IDENTIFIER_TABLE:
2106      F.IdentifierTableData = BlobStart;
2107      if (Record[0]) {
2108        F.IdentifierLookupTable
2109          = ASTIdentifierLookupTable::Create(
2110                       (const unsigned char *)F.IdentifierTableData + Record[0],
2111                       (const unsigned char *)F.IdentifierTableData,
2112                       ASTIdentifierLookupTrait(*this, F));
2113        if (PP) {
2114          PP->getIdentifierTable().setExternalIdentifierLookup(this);
2115          PP->getHeaderSearchInfo().SetExternalLookup(this);
2116        }
2117      }
2118      break;
2119
2120    case IDENTIFIER_OFFSET:
2121      if (F.LocalNumIdentifiers != 0) {
2122        Error("duplicate IDENTIFIER_OFFSET record in AST file");
2123        return Failure;
2124      }
2125      F.IdentifierOffsets = (const uint32_t *)BlobStart;
2126      F.LocalNumIdentifiers = Record[0];
2127
2128      // Introduce the global -> local mapping for identifiers within this AST
2129      // file
2130      GlobalIdentifierMap.insert(
2131                     std::make_pair(getTotalNumIdentifiers() + 1,
2132                                    std::make_pair(&F,
2133                                                   -getTotalNumIdentifiers())));
2134      IdentifiersLoaded.resize(IdentifiersLoaded.size() +F.LocalNumIdentifiers);
2135      break;
2136
2137    case EXTERNAL_DEFINITIONS:
2138      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2139        ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2140      break;
2141
2142    case SPECIAL_TYPES:
2143      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2144        SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2145      break;
2146
2147    case STATISTICS:
2148      TotalNumStatements += Record[0];
2149      TotalNumMacros += Record[1];
2150      TotalLexicalDeclContexts += Record[2];
2151      TotalVisibleDeclContexts += Record[3];
2152      break;
2153
2154    case UNUSED_FILESCOPED_DECLS:
2155      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2156        UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2157      break;
2158
2159    case DELEGATING_CTORS:
2160      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2161        DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2162      break;
2163
2164    case WEAK_UNDECLARED_IDENTIFIERS:
2165      // Later blocks overwrite earlier ones.
2166      WeakUndeclaredIdentifiers.swap(Record);
2167      break;
2168
2169    case LOCALLY_SCOPED_EXTERNAL_DECLS:
2170      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2171        LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
2172      break;
2173
2174    case SELECTOR_OFFSETS:
2175      F.SelectorOffsets = (const uint32_t *)BlobStart;
2176      F.LocalNumSelectors = Record[0];
2177
2178      // Introduce the global -> local mapping for identifiers within this AST
2179      // file
2180      GlobalSelectorMap.insert(
2181                     std::make_pair(getTotalNumSelectors() + 1,
2182                                    std::make_pair(&F,
2183                                                   -getTotalNumSelectors())));
2184      SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2185      break;
2186
2187    case METHOD_POOL:
2188      F.SelectorLookupTableData = (const unsigned char *)BlobStart;
2189      if (Record[0])
2190        F.SelectorLookupTable
2191          = ASTSelectorLookupTable::Create(
2192                        F.SelectorLookupTableData + Record[0],
2193                        F.SelectorLookupTableData,
2194                        ASTSelectorLookupTrait(*this, F));
2195      TotalNumMethodPoolEntries += Record[1];
2196      break;
2197
2198    case REFERENCED_SELECTOR_POOL:
2199      if (!Record.empty()) {
2200        for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2201          ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2202                                                                Record[Idx++]));
2203          ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2204                                              getRawEncoding());
2205        }
2206      }
2207      break;
2208
2209    case PP_COUNTER_VALUE:
2210      if (!Record.empty() && Listener)
2211        Listener->ReadCounter(Record[0]);
2212      break;
2213
2214    case SOURCE_LOCATION_OFFSETS: {
2215      F.SLocEntryOffsets = (const uint32_t *)BlobStart;
2216      F.LocalNumSLocEntries = Record[0];
2217      llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2218          SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2219      // Make our entry in the range map. BaseID is negative and growing, so
2220      // we invert it. Because we invert it, though, we need the other end of
2221      // the range.
2222      unsigned RangeStart =
2223          unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2224      GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2225      F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2226
2227      // Initialize the remapping table.
2228      // Invalid stays invalid.
2229      F.SLocRemap.insert(std::make_pair(0U, 0));
2230      // This module. Base was 2 when being compiled.
2231      F.SLocRemap.insert(std::make_pair(2U,
2232                                  static_cast<int>(F.SLocEntryBaseOffset - 2)));
2233
2234      TotalNumSLocEntries += F.LocalNumSLocEntries;
2235      break;
2236    }
2237
2238    case SOURCE_LOCATION_MAP: {
2239      // Additional remapping information.
2240      const unsigned char *Data = (const unsigned char*)BlobStart;
2241      const unsigned char *DataEnd = Data + BlobLen;
2242      while(Data < DataEnd) {
2243        uint32_t Offset = io::ReadUnalignedLE32(Data);
2244        uint16_t Len = io::ReadUnalignedLE16(Data);
2245        StringRef Name = StringRef((const char*)Data, Len);
2246        Module *OM = ModuleMgr.lookup(Name);
2247        if (!OM) {
2248          Error("SourceLocation remap refers to unknown module");
2249          return Failure;
2250        }
2251        // My Offset is mapped to OM->SLocEntryBaseOffset.
2252        F.SLocRemap.insert(std::make_pair(Offset,
2253                        static_cast<int>(OM->SLocEntryBaseOffset - Offset)));
2254        Data += Len;
2255      }
2256      break;
2257    }
2258
2259    case SOURCE_MANAGER_LINE_TABLE:
2260      if (ParseLineTable(F, Record))
2261        return Failure;
2262      break;
2263
2264    case FILE_SOURCE_LOCATION_OFFSETS:
2265      F.SLocFileOffsets = (const uint32_t *)BlobStart;
2266      F.LocalNumSLocFileEntries = Record[0];
2267      break;
2268
2269    case SOURCE_LOCATION_PRELOADS: {
2270      // Need to transform from the local view (1-based IDs) to the global view,
2271      // which is based off F.SLocEntryBaseID.
2272      PreloadSLocEntries.reserve(PreloadSLocEntries.size() + Record.size());
2273      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2274        PreloadSLocEntries.push_back(int(Record[I] - 1) + F.SLocEntryBaseID);
2275      break;
2276    }
2277
2278    case STAT_CACHE: {
2279      if (!DisableStatCache) {
2280        ASTStatCache *MyStatCache =
2281          new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2282                           (const unsigned char *)BlobStart,
2283                           NumStatHits, NumStatMisses);
2284        FileMgr.addStatCache(MyStatCache);
2285        F.StatCache = MyStatCache;
2286      }
2287      break;
2288    }
2289
2290    case EXT_VECTOR_DECLS:
2291      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2292        ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2293      break;
2294
2295    case VTABLE_USES:
2296      // Later tables overwrite earlier ones.
2297      // FIXME: Modules will have some trouble with this.
2298      VTableUses.clear();
2299      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2300        VTableUses.push_back(getGlobalDeclID(F, Record[I]));
2301      break;
2302
2303    case DYNAMIC_CLASSES:
2304      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2305        DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2306      break;
2307
2308    case PENDING_IMPLICIT_INSTANTIATIONS:
2309      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2310        F.PendingInstantiations.push_back(getGlobalDeclID(F, Record[I]));
2311      break;
2312
2313    case SEMA_DECL_REFS:
2314      // Later tables overwrite earlier ones.
2315      // FIXME: Modules will have some trouble with this.
2316      SemaDeclRefs.clear();
2317      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2318        SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2319      break;
2320
2321    case ORIGINAL_FILE_NAME:
2322      // The primary AST will be the last to get here, so it will be the one
2323      // that's used.
2324      ActualOriginalFileName.assign(BlobStart, BlobLen);
2325      OriginalFileName = ActualOriginalFileName;
2326      MaybeAddSystemRootToFilename(OriginalFileName);
2327      break;
2328
2329    case ORIGINAL_FILE_ID:
2330      OriginalFileID = FileID::get(Record[0]);
2331      break;
2332
2333    case ORIGINAL_PCH_DIR:
2334      // The primary AST will be the last to get here, so it will be the one
2335      // that's used.
2336      OriginalDir.assign(BlobStart, BlobLen);
2337      break;
2338
2339    case VERSION_CONTROL_BRANCH_REVISION: {
2340      const std::string &CurBranch = getClangFullRepositoryVersion();
2341      StringRef ASTBranch(BlobStart, BlobLen);
2342      if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2343        Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
2344        return IgnorePCH;
2345      }
2346      break;
2347    }
2348
2349    case MACRO_DEFINITION_OFFSETS: {
2350      F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2351      F.NumPreallocatedPreprocessingEntities = Record[0];
2352      F.LocalNumMacroDefinitions = Record[1];
2353
2354      // Introduce the global -> local mapping for preprocessed entities within
2355      // this AST file.
2356      unsigned StartingID;
2357      if (PP) {
2358        if (!PP->getPreprocessingRecord())
2359          PP->createPreprocessingRecord(true);
2360        if (!PP->getPreprocessingRecord()->getExternalSource())
2361          PP->getPreprocessingRecord()->SetExternalSource(*this);
2362        StartingID
2363          = PP->getPreprocessingRecord()
2364              ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2365      } else {
2366        // FIXME: We'll eventually want to kill this path, since it assumes
2367        // a particular allocation strategy in the preprocessing record.
2368        StartingID = getTotalNumPreprocessedEntities();
2369      }
2370
2371      GlobalPreprocessedEntityMap.insert(
2372                        std::make_pair(StartingID,
2373                                         std::make_pair(&F, -(int)StartingID)));
2374
2375      // Introduce the global -> local mapping for macro definitions within
2376      // this AST file.
2377      GlobalMacroDefinitionMap.insert(
2378               std::make_pair(getTotalNumMacroDefinitions() + 1,
2379                              std::make_pair(&F,
2380                                             -getTotalNumMacroDefinitions())));
2381      MacroDefinitionsLoaded.resize(
2382                    MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
2383      break;
2384    }
2385
2386    case DECL_UPDATE_OFFSETS: {
2387      if (Record.size() % 2 != 0) {
2388        Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2389        return Failure;
2390      }
2391      for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2392        DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2393            .push_back(std::make_pair(&F, Record[I+1]));
2394      break;
2395    }
2396
2397    case DECL_REPLACEMENTS: {
2398      if (Record.size() % 2 != 0) {
2399        Error("invalid DECL_REPLACEMENTS block in AST file");
2400        return Failure;
2401      }
2402      for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2403        ReplacedDecls[static_cast<DeclID>(Record[I])] =
2404            std::make_pair(&F, Record[I+1]);
2405      break;
2406    }
2407
2408    case CXX_BASE_SPECIFIER_OFFSETS: {
2409      if (F.LocalNumCXXBaseSpecifiers != 0) {
2410        Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2411        return Failure;
2412      }
2413
2414      F.LocalNumCXXBaseSpecifiers = Record[0];
2415      F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2416
2417      GlobalCXXBaseSpecifiersMap.insert(std::make_pair(
2418                                        getTotalNumCXXBaseSpecifiers() + 1,
2419                                        std::make_pair(&F,
2420                                            -getTotalNumCXXBaseSpecifiers())));
2421
2422      NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2423      break;
2424    }
2425
2426    case DIAG_PRAGMA_MAPPINGS:
2427      if (Record.size() % 2 != 0) {
2428        Error("invalid DIAG_USER_MAPPINGS block in AST file");
2429        return Failure;
2430      }
2431
2432      if (F.PragmaDiagMappings.empty())
2433        F.PragmaDiagMappings.swap(Record);
2434      else
2435        F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2436                                    Record.begin(), Record.end());
2437      break;
2438
2439    case CUDA_SPECIAL_DECL_REFS:
2440      // Later tables overwrite earlier ones.
2441      // FIXME: Modules will have trouble with this.
2442      CUDASpecialDeclRefs.clear();
2443      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2444        CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2445      break;
2446
2447    case HEADER_SEARCH_TABLE: {
2448      F.HeaderFileInfoTableData = BlobStart;
2449      F.LocalNumHeaderFileInfos = Record[1];
2450      F.HeaderFileFrameworkStrings = BlobStart + Record[2];
2451      if (Record[0]) {
2452        F.HeaderFileInfoTable
2453          = HeaderFileInfoLookupTable::Create(
2454                   (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2455                   (const unsigned char *)F.HeaderFileInfoTableData,
2456                   HeaderFileInfoTrait(PP? &PP->getHeaderSearchInfo() : 0,
2457                                       BlobStart + Record[2]));
2458        if (PP)
2459          PP->getHeaderSearchInfo().SetExternalSource(this);
2460      }
2461      break;
2462    }
2463
2464    case FP_PRAGMA_OPTIONS:
2465      // Later tables overwrite earlier ones.
2466      FPPragmaOptions.swap(Record);
2467      break;
2468
2469    case OPENCL_EXTENSIONS:
2470      // Later tables overwrite earlier ones.
2471      OpenCLExtensions.swap(Record);
2472      break;
2473
2474    case TENTATIVE_DEFINITIONS:
2475      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2476        TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2477      break;
2478
2479    case KNOWN_NAMESPACES:
2480      for (unsigned I = 0, N = Record.size(); I != N; ++I)
2481        KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2482      break;
2483    }
2484    First = false;
2485  }
2486  Error("premature end of bitstream in AST file");
2487  return Failure;
2488}
2489
2490ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2491  for (ModuleIterator I = ModuleMgr.begin(),
2492      E = ModuleMgr.end(); I != E; ++I) {
2493    Module *F = *I;
2494    llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2495
2496    for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2497      SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
2498      unsigned Code = SLocEntryCursor.ReadCode();
2499      if (Code == llvm::bitc::END_BLOCK ||
2500          Code == llvm::bitc::ENTER_SUBBLOCK ||
2501          Code == llvm::bitc::DEFINE_ABBREV) {
2502        Error("incorrectly-formatted source location entry in AST file");
2503        return Failure;
2504      }
2505
2506      RecordData Record;
2507      const char *BlobStart;
2508      unsigned BlobLen;
2509      switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2510      default:
2511        Error("incorrectly-formatted source location entry in AST file");
2512        return Failure;
2513
2514      case SM_SLOC_FILE_ENTRY: {
2515        StringRef Filename(BlobStart, BlobLen);
2516        const FileEntry *File = getFileEntry(Filename);
2517
2518        if (File == 0) {
2519          std::string ErrorStr = "could not find file '";
2520          ErrorStr += Filename;
2521          ErrorStr += "' referenced by AST file";
2522          Error(ErrorStr.c_str());
2523          return IgnorePCH;
2524        }
2525
2526        if (Record.size() < 6) {
2527          Error("source location entry is incorrect");
2528          return Failure;
2529        }
2530
2531        // The stat info from the FileEntry came from the cached stat
2532        // info of the PCH, so we cannot trust it.
2533        struct stat StatBuf;
2534        if (::stat(File->getName(), &StatBuf) != 0) {
2535          StatBuf.st_size = File->getSize();
2536          StatBuf.st_mtime = File->getModificationTime();
2537        }
2538
2539        if (((off_t)Record[4] != StatBuf.st_size
2540#if !defined(LLVM_ON_WIN32)
2541            // In our regression testing, the Windows file system seems to
2542            // have inconsistent modification times that sometimes
2543            // erroneously trigger this error-handling path.
2544             || (time_t)Record[5] != StatBuf.st_mtime
2545#endif
2546            )) {
2547          Error(diag::err_fe_pch_file_modified, Filename);
2548          return IgnorePCH;
2549        }
2550
2551        break;
2552      }
2553      }
2554    }
2555  }
2556
2557  return Success;
2558}
2559
2560ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2561                                            ModuleKind Type) {
2562  switch(ReadASTCore(FileName, Type)) {
2563  case Failure: return Failure;
2564  case IgnorePCH: return IgnorePCH;
2565  case Success: break;
2566  }
2567
2568  // Here comes stuff that we only do once the entire chain is loaded.
2569
2570  if (!DisableValidation) {
2571    switch(validateFileEntries()) {
2572    case Failure: return Failure;
2573    case IgnorePCH: return IgnorePCH;
2574    case Success: break;
2575    }
2576  }
2577
2578  // Preload SLocEntries.
2579  for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2580    ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2581    if (Result != Success)
2582      return Failure;
2583  }
2584  PreloadSLocEntries.clear();
2585
2586  // Check the predefines buffers.
2587  if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
2588    return IgnorePCH;
2589
2590  if (PP) {
2591    // Initialization of keywords and pragmas occurs before the
2592    // AST file is read, so there may be some identifiers that were
2593    // loaded into the IdentifierTable before we intercepted the
2594    // creation of identifiers. Iterate through the list of known
2595    // identifiers and determine whether we have to establish
2596    // preprocessor definitions or top-level identifier declaration
2597    // chains for those identifiers.
2598    //
2599    // We copy the IdentifierInfo pointers to a small vector first,
2600    // since de-serializing declarations or macro definitions can add
2601    // new entries into the identifier table, invalidating the
2602    // iterators.
2603    //
2604    // FIXME: We need a lazier way to load this information, e.g., by marking
2605    // the identifier data as 'dirty', so that it will be looked up in the
2606    // AST file(s) if it is uttered in the source. This could save us some
2607    // module load time.
2608    SmallVector<IdentifierInfo *, 128> Identifiers;
2609    for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2610                                IdEnd = PP->getIdentifierTable().end();
2611         Id != IdEnd; ++Id)
2612      Identifiers.push_back(Id->second);
2613    // We need to search the tables in all files.
2614    for (ModuleIterator J = ModuleMgr.begin(),
2615        M = ModuleMgr.end(); J != M; ++J) {
2616      ASTIdentifierLookupTable *IdTable
2617        = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable;
2618      // Not all AST files necessarily have identifier tables, only the useful
2619      // ones.
2620      if (!IdTable)
2621        continue;
2622      for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2623        IdentifierInfo *II = Identifiers[I];
2624        // Look in the on-disk hash tables for an entry for this identifier
2625        ASTIdentifierLookupTrait Info(*this, *(*J), II);
2626        std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
2627        ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2628        if (Pos == IdTable->end())
2629          continue;
2630
2631        // Dereferencing the iterator has the effect of populating the
2632        // IdentifierInfo node with the various declarations it needs.
2633        (void)*Pos;
2634      }
2635    }
2636  }
2637
2638  if (Context)
2639    InitializeContext(*Context);
2640
2641  if (DeserializationListener)
2642    DeserializationListener->ReaderInitialized(this);
2643
2644  // If this AST file is a precompiled preamble, then set the main file ID of
2645  // the source manager to the file source file from which the preamble was
2646  // built. This is the only valid way to use a precompiled preamble.
2647  if (Type == MK_Preamble) {
2648    if (OriginalFileID.isInvalid()) {
2649      SourceLocation Loc
2650        = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2651      if (Loc.isValid())
2652        OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
2653    }
2654    else {
2655      OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2656                                        + OriginalFileID.getOpaqueValue() - 1);
2657    }
2658
2659    if (!OriginalFileID.isInvalid())
2660      SourceMgr.SetPreambleFileID(OriginalFileID);
2661  }
2662
2663  return Success;
2664}
2665
2666ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
2667                                                ModuleKind Type) {
2668  Module &F = ModuleMgr.addModule(FileName, Type);
2669
2670  if (FileName != "-") {
2671    CurrentDir = llvm::sys::path::parent_path(FileName);
2672    if (CurrentDir.empty()) CurrentDir = ".";
2673  }
2674
2675  if (!ASTBuffers.empty()) {
2676    F.Buffer.reset(ASTBuffers.back());
2677    ASTBuffers.pop_back();
2678    assert(F.Buffer && "Passed null buffer");
2679  } else {
2680    // Open the AST file.
2681    //
2682    // FIXME: This shouldn't be here, we should just take a raw_ostream.
2683    std::string ErrStr;
2684    llvm::error_code ec;
2685    if (FileName == "-") {
2686      ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2687      if (ec)
2688        ErrStr = ec.message();
2689    } else
2690      F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2691    if (!F.Buffer) {
2692      Error(ErrStr.c_str());
2693      return IgnorePCH;
2694    }
2695  }
2696
2697  // Initialize the stream
2698  F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2699                    (const unsigned char *)F.Buffer->getBufferEnd());
2700  llvm::BitstreamCursor &Stream = F.Stream;
2701  Stream.init(F.StreamFile);
2702  F.SizeInBits = F.Buffer->getBufferSize() * 8;
2703
2704  // Sniff for the signature.
2705  if (Stream.Read(8) != 'C' ||
2706      Stream.Read(8) != 'P' ||
2707      Stream.Read(8) != 'C' ||
2708      Stream.Read(8) != 'H') {
2709    Diag(diag::err_not_a_pch_file) << FileName;
2710    return Failure;
2711  }
2712
2713  while (!Stream.AtEndOfStream()) {
2714    unsigned Code = Stream.ReadCode();
2715
2716    if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2717      Error("invalid record at top-level of AST file");
2718      return Failure;
2719    }
2720
2721    unsigned BlockID = Stream.ReadSubBlockID();
2722
2723    // We only know the AST subblock ID.
2724    switch (BlockID) {
2725    case llvm::bitc::BLOCKINFO_BLOCK_ID:
2726      if (Stream.ReadBlockInfoBlock()) {
2727        Error("malformed BlockInfoBlock in AST file");
2728        return Failure;
2729      }
2730      break;
2731    case AST_BLOCK_ID:
2732      switch (ReadASTBlock(F)) {
2733      case Success:
2734        break;
2735
2736      case Failure:
2737        return Failure;
2738
2739      case IgnorePCH:
2740        // FIXME: We could consider reading through to the end of this
2741        // AST block, skipping subblocks, to see if there are other
2742        // AST blocks elsewhere.
2743
2744        // FIXME: We can't clear loaded slocentries anymore.
2745        //SourceMgr.ClearPreallocatedSLocEntries();
2746
2747        // Remove the stat cache.
2748        if (F.StatCache)
2749          FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
2750
2751        return IgnorePCH;
2752      }
2753      break;
2754    default:
2755      if (Stream.SkipBlock()) {
2756        Error("malformed block record in AST file");
2757        return Failure;
2758      }
2759      break;
2760    }
2761  }
2762
2763  // Once read, set the Module bit base offset and update the size in
2764  // bits of all files we've seen.
2765  F.GlobalBitOffset = TotalModulesSizeInBits;
2766  TotalModulesSizeInBits += F.SizeInBits;
2767  GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2768  return Success;
2769}
2770
2771void ASTReader::setPreprocessor(Preprocessor &pp) {
2772  PP = &pp;
2773
2774  if (unsigned N = getTotalNumPreprocessedEntities()) {
2775    if (!PP->getPreprocessingRecord())
2776      PP->createPreprocessingRecord(true);
2777    PP->getPreprocessingRecord()->SetExternalSource(*this);
2778    PP->getPreprocessingRecord()->allocateLoadedEntities(N);
2779  }
2780
2781  PP->getHeaderSearchInfo().SetExternalLookup(this);
2782  PP->getHeaderSearchInfo().SetExternalSource(this);
2783}
2784
2785void ASTReader::InitializeContext(ASTContext &Ctx) {
2786  Context = &Ctx;
2787  assert(Context && "Passed null context!");
2788
2789  assert(PP && "Forgot to set Preprocessor ?");
2790  PP->getIdentifierTable().setExternalIdentifierLookup(this);
2791  PP->setExternalSource(this);
2792
2793  // If we have an update block for the TU waiting, we have to add it before
2794  // deserializing the decl.
2795  DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2796  if (DCU != DeclContextOffsets.end()) {
2797    // Insertion could invalidate map, so grab vector.
2798    DeclContextInfos T;
2799    T.swap(DCU->second);
2800    DeclContextOffsets.erase(DCU);
2801    DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2802  }
2803
2804  // Load the translation unit declaration
2805  GetTranslationUnitDecl();
2806
2807  // Load the special types.
2808  Context->setBuiltinVaListType(
2809    GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2810  if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
2811    Context->setObjCIdType(GetType(Id));
2812  if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
2813    Context->setObjCSelType(GetType(Sel));
2814  if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
2815    Context->setObjCProtoType(GetType(Proto));
2816  if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
2817    Context->setObjCClassType(GetType(Class));
2818
2819  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
2820    Context->setCFConstantStringType(GetType(String));
2821  if (unsigned FastEnum
2822        = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2823    Context->setObjCFastEnumerationStateType(GetType(FastEnum));
2824  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2825    QualType FileType = GetType(File);
2826    if (FileType.isNull()) {
2827      Error("FILE type is NULL");
2828      return;
2829    }
2830    if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2831      Context->setFILEDecl(Typedef->getDecl());
2832    else {
2833      const TagType *Tag = FileType->getAs<TagType>();
2834      if (!Tag) {
2835        Error("Invalid FILE type in AST file");
2836        return;
2837      }
2838      Context->setFILEDecl(Tag->getDecl());
2839    }
2840  }
2841  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2842    QualType Jmp_bufType = GetType(Jmp_buf);
2843    if (Jmp_bufType.isNull()) {
2844      Error("jmp_bug type is NULL");
2845      return;
2846    }
2847    if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2848      Context->setjmp_bufDecl(Typedef->getDecl());
2849    else {
2850      const TagType *Tag = Jmp_bufType->getAs<TagType>();
2851      if (!Tag) {
2852        Error("Invalid jmp_buf type in AST file");
2853        return;
2854      }
2855      Context->setjmp_bufDecl(Tag->getDecl());
2856    }
2857  }
2858  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2859    QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2860    if (Sigjmp_bufType.isNull()) {
2861      Error("sigjmp_buf type is NULL");
2862      return;
2863    }
2864    if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2865      Context->setsigjmp_bufDecl(Typedef->getDecl());
2866    else {
2867      const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2868      assert(Tag && "Invalid sigjmp_buf type in AST file");
2869      Context->setsigjmp_bufDecl(Tag->getDecl());
2870    }
2871  }
2872  if (unsigned ObjCIdRedef
2873        = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2874    Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2875  if (unsigned ObjCClassRedef
2876      = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2877    Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2878  if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2879    Context->setBlockDescriptorType(GetType(String));
2880  if (unsigned String
2881      = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2882    Context->setBlockDescriptorExtendedType(GetType(String));
2883  if (unsigned ObjCSelRedef
2884      = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2885    Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2886  if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
2887    Context->setNSConstantStringType(GetType(String));
2888
2889  if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
2890    Context->setInt128Installed();
2891
2892  if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2893    Context->AutoDeductTy = GetType(AutoDeduct);
2894  if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2895    Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2896
2897  ReadPragmaDiagnosticMappings(Context->getDiagnostics());
2898
2899  // If there were any CUDA special declarations, deserialize them.
2900  if (!CUDASpecialDeclRefs.empty()) {
2901    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2902    Context->setcudaConfigureCallDecl(
2903                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2904  }
2905}
2906
2907/// \brief Retrieve the name of the original source file name
2908/// directly from the AST file, without actually loading the AST
2909/// file.
2910std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2911                                             FileManager &FileMgr,
2912                                             Diagnostic &Diags) {
2913  // Open the AST file.
2914  std::string ErrStr;
2915  llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2916  Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
2917  if (!Buffer) {
2918    Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2919    return std::string();
2920  }
2921
2922  // Initialize the stream
2923  llvm::BitstreamReader StreamFile;
2924  llvm::BitstreamCursor Stream;
2925  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2926                  (const unsigned char *)Buffer->getBufferEnd());
2927  Stream.init(StreamFile);
2928
2929  // Sniff for the signature.
2930  if (Stream.Read(8) != 'C' ||
2931      Stream.Read(8) != 'P' ||
2932      Stream.Read(8) != 'C' ||
2933      Stream.Read(8) != 'H') {
2934    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2935    return std::string();
2936  }
2937
2938  RecordData Record;
2939  while (!Stream.AtEndOfStream()) {
2940    unsigned Code = Stream.ReadCode();
2941
2942    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2943      unsigned BlockID = Stream.ReadSubBlockID();
2944
2945      // We only know the AST subblock ID.
2946      switch (BlockID) {
2947      case AST_BLOCK_ID:
2948        if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2949          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2950          return std::string();
2951        }
2952        break;
2953
2954      default:
2955        if (Stream.SkipBlock()) {
2956          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2957          return std::string();
2958        }
2959        break;
2960      }
2961      continue;
2962    }
2963
2964    if (Code == llvm::bitc::END_BLOCK) {
2965      if (Stream.ReadBlockEnd()) {
2966        Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2967        return std::string();
2968      }
2969      continue;
2970    }
2971
2972    if (Code == llvm::bitc::DEFINE_ABBREV) {
2973      Stream.ReadAbbrevRecord();
2974      continue;
2975    }
2976
2977    Record.clear();
2978    const char *BlobStart = 0;
2979    unsigned BlobLen = 0;
2980    if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2981          == ORIGINAL_FILE_NAME)
2982      return std::string(BlobStart, BlobLen);
2983  }
2984
2985  return std::string();
2986}
2987
2988/// \brief Parse the record that corresponds to a LangOptions data
2989/// structure.
2990///
2991/// This routine parses the language options from the AST file and then gives
2992/// them to the AST listener if one is set.
2993///
2994/// \returns true if the listener deems the file unacceptable, false otherwise.
2995bool ASTReader::ParseLanguageOptions(
2996                             const SmallVectorImpl<uint64_t> &Record) {
2997  if (Listener) {
2998    LangOptions LangOpts;
2999
3000  #define PARSE_LANGOPT(Option)                  \
3001      LangOpts.Option = Record[Idx];             \
3002      ++Idx
3003
3004    unsigned Idx = 0;
3005    PARSE_LANGOPT(Trigraphs);
3006    PARSE_LANGOPT(BCPLComment);
3007    PARSE_LANGOPT(DollarIdents);
3008    PARSE_LANGOPT(AsmPreprocessor);
3009    PARSE_LANGOPT(GNUMode);
3010    PARSE_LANGOPT(GNUKeywords);
3011    PARSE_LANGOPT(ImplicitInt);
3012    PARSE_LANGOPT(Digraphs);
3013    PARSE_LANGOPT(HexFloats);
3014    PARSE_LANGOPT(C99);
3015    PARSE_LANGOPT(C1X);
3016    PARSE_LANGOPT(Microsoft);
3017    PARSE_LANGOPT(CPlusPlus);
3018    PARSE_LANGOPT(CPlusPlus0x);
3019    PARSE_LANGOPT(CXXOperatorNames);
3020    PARSE_LANGOPT(ObjC1);
3021    PARSE_LANGOPT(ObjC2);
3022    PARSE_LANGOPT(ObjCNonFragileABI);
3023    PARSE_LANGOPT(ObjCNonFragileABI2);
3024    PARSE_LANGOPT(AppleKext);
3025    PARSE_LANGOPT(ObjCDefaultSynthProperties);
3026    PARSE_LANGOPT(ObjCInferRelatedResultType);
3027    PARSE_LANGOPT(NoConstantCFStrings);
3028    PARSE_LANGOPT(PascalStrings);
3029    PARSE_LANGOPT(WritableStrings);
3030    PARSE_LANGOPT(LaxVectorConversions);
3031    PARSE_LANGOPT(AltiVec);
3032    PARSE_LANGOPT(Exceptions);
3033    PARSE_LANGOPT(ObjCExceptions);
3034    PARSE_LANGOPT(CXXExceptions);
3035    PARSE_LANGOPT(SjLjExceptions);
3036    PARSE_LANGOPT(MSBitfields);
3037    PARSE_LANGOPT(NeXTRuntime);
3038    PARSE_LANGOPT(Freestanding);
3039    PARSE_LANGOPT(NoBuiltin);
3040    PARSE_LANGOPT(ThreadsafeStatics);
3041    PARSE_LANGOPT(POSIXThreads);
3042    PARSE_LANGOPT(Blocks);
3043    PARSE_LANGOPT(EmitAllDecls);
3044    PARSE_LANGOPT(MathErrno);
3045    LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3046                                       Record[Idx++]);
3047    PARSE_LANGOPT(HeinousExtensions);
3048    PARSE_LANGOPT(Optimize);
3049    PARSE_LANGOPT(OptimizeSize);
3050    PARSE_LANGOPT(Static);
3051    PARSE_LANGOPT(PICLevel);
3052    PARSE_LANGOPT(GNUInline);
3053    PARSE_LANGOPT(NoInline);
3054    PARSE_LANGOPT(Deprecated);
3055    PARSE_LANGOPT(AccessControl);
3056    PARSE_LANGOPT(CharIsSigned);
3057    PARSE_LANGOPT(ShortWChar);
3058    PARSE_LANGOPT(ShortEnums);
3059    LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
3060    LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
3061    LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
3062                                   Record[Idx++]);
3063    PARSE_LANGOPT(InstantiationDepth);
3064    PARSE_LANGOPT(OpenCL);
3065    PARSE_LANGOPT(CUDA);
3066    PARSE_LANGOPT(CatchUndefined);
3067    PARSE_LANGOPT(DefaultFPContract);
3068    PARSE_LANGOPT(ElideConstructors);
3069    PARSE_LANGOPT(SpellChecking);
3070    PARSE_LANGOPT(MRTD);
3071    PARSE_LANGOPT(ObjCAutoRefCount);
3072  #undef PARSE_LANGOPT
3073
3074    return Listener->ReadLanguageOptions(LangOpts);
3075  }
3076
3077  return false;
3078}
3079
3080void ASTReader::ReadPreprocessedEntities() {
3081  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3082    Module &F = *(*I);
3083    if (!F.PreprocessorDetailCursor.getBitStreamReader())
3084      continue;
3085
3086    SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
3087    F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3088    while (LoadPreprocessedEntity(F)) { }
3089  }
3090}
3091
3092PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
3093  RecordLocation Loc = getLocalBitOffset(Offset);
3094
3095  // Keep track of where we are in the stream, then jump back there
3096  // after reading this entity.
3097  SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3098  Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3099  return LoadPreprocessedEntity(*Loc.F);
3100}
3101
3102HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3103  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3104    Module &F = *(*I);
3105
3106    HeaderFileInfoTrait Trait(&PP->getHeaderSearchInfo(),
3107                              F.HeaderFileFrameworkStrings,
3108                              FE->getName());
3109
3110    HeaderFileInfoLookupTable *Table
3111      = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3112    if (!Table)
3113      continue;
3114
3115    // Look in the on-disk hash table for an entry for this file name.
3116    HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3117                                                          &Trait);
3118    if (Pos == Table->end())
3119      continue;
3120
3121    HeaderFileInfo HFI = *Pos;
3122    if (Listener)
3123      Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3124
3125    return HFI;
3126  }
3127
3128  return HeaderFileInfo();
3129}
3130
3131void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
3132  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3133    Module &F = *(*I);
3134    unsigned Idx = 0;
3135    while (Idx < F.PragmaDiagMappings.size()) {
3136      SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3137      while (1) {
3138        assert(Idx < F.PragmaDiagMappings.size() &&
3139               "Invalid data, didn't find '-1' marking end of diag/map pairs");
3140        if (Idx >= F.PragmaDiagMappings.size()) {
3141          break; // Something is messed up but at least avoid infinite loop in
3142                 // release build.
3143        }
3144        unsigned DiagID = F.PragmaDiagMappings[Idx++];
3145        if (DiagID == (unsigned)-1) {
3146          break; // no more diag/map pairs for this location.
3147        }
3148        diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3149        Diag.setDiagnosticMapping(DiagID, Map, Loc);
3150      }
3151    }
3152  }
3153}
3154
3155/// \brief Get the correct cursor and offset for loading a type.
3156ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
3157  GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index+1);
3158  assert(I != GlobalTypeMap.end() && "Corrupted global type map");
3159  return RecordLocation(I->second.first,
3160      I->second.first->TypeOffsets[Index + I->second.second]);
3161}
3162
3163/// \brief Read and return the type with the given index..
3164///
3165/// The index is the type ID, shifted and minus the number of predefs. This
3166/// routine actually reads the record corresponding to the type at the given
3167/// location. It is a helper routine for GetType, which deals with reading type
3168/// IDs.
3169QualType ASTReader::readTypeRecord(unsigned Index) {
3170  RecordLocation Loc = TypeCursorForIndex(Index);
3171  llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
3172
3173  // Keep track of where we are in the stream, then jump back there
3174  // after reading this type.
3175  SavedStreamPosition SavedPosition(DeclsCursor);
3176
3177  ReadingKindTracker ReadingKind(Read_Type, *this);
3178
3179  // Note that we are loading a type record.
3180  Deserializing AType(this);
3181
3182  unsigned Idx = 0;
3183  DeclsCursor.JumpToBit(Loc.Offset);
3184  RecordData Record;
3185  unsigned Code = DeclsCursor.ReadCode();
3186  switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3187  case TYPE_EXT_QUAL: {
3188    if (Record.size() != 2) {
3189      Error("Incorrect encoding of extended qualifier type");
3190      return QualType();
3191    }
3192    QualType Base = readType(*Loc.F, Record, Idx);
3193    Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
3194    return Context->getQualifiedType(Base, Quals);
3195  }
3196
3197  case TYPE_COMPLEX: {
3198    if (Record.size() != 1) {
3199      Error("Incorrect encoding of complex type");
3200      return QualType();
3201    }
3202    QualType ElemType = readType(*Loc.F, Record, Idx);
3203    return Context->getComplexType(ElemType);
3204  }
3205
3206  case TYPE_POINTER: {
3207    if (Record.size() != 1) {
3208      Error("Incorrect encoding of pointer type");
3209      return QualType();
3210    }
3211    QualType PointeeType = readType(*Loc.F, Record, Idx);
3212    return Context->getPointerType(PointeeType);
3213  }
3214
3215  case TYPE_BLOCK_POINTER: {
3216    if (Record.size() != 1) {
3217      Error("Incorrect encoding of block pointer type");
3218      return QualType();
3219    }
3220    QualType PointeeType = readType(*Loc.F, Record, Idx);
3221    return Context->getBlockPointerType(PointeeType);
3222  }
3223
3224  case TYPE_LVALUE_REFERENCE: {
3225    if (Record.size() != 2) {
3226      Error("Incorrect encoding of lvalue reference type");
3227      return QualType();
3228    }
3229    QualType PointeeType = readType(*Loc.F, Record, Idx);
3230    return Context->getLValueReferenceType(PointeeType, Record[1]);
3231  }
3232
3233  case TYPE_RVALUE_REFERENCE: {
3234    if (Record.size() != 1) {
3235      Error("Incorrect encoding of rvalue reference type");
3236      return QualType();
3237    }
3238    QualType PointeeType = readType(*Loc.F, Record, Idx);
3239    return Context->getRValueReferenceType(PointeeType);
3240  }
3241
3242  case TYPE_MEMBER_POINTER: {
3243    if (Record.size() != 2) {
3244      Error("Incorrect encoding of member pointer type");
3245      return QualType();
3246    }
3247    QualType PointeeType = readType(*Loc.F, Record, Idx);
3248    QualType ClassType = readType(*Loc.F, Record, Idx);
3249    if (PointeeType.isNull() || ClassType.isNull())
3250      return QualType();
3251
3252    return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
3253  }
3254
3255  case TYPE_CONSTANT_ARRAY: {
3256    QualType ElementType = readType(*Loc.F, Record, Idx);
3257    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3258    unsigned IndexTypeQuals = Record[2];
3259    unsigned Idx = 3;
3260    llvm::APInt Size = ReadAPInt(Record, Idx);
3261    return Context->getConstantArrayType(ElementType, Size,
3262                                         ASM, IndexTypeQuals);
3263  }
3264
3265  case TYPE_INCOMPLETE_ARRAY: {
3266    QualType ElementType = readType(*Loc.F, Record, Idx);
3267    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3268    unsigned IndexTypeQuals = Record[2];
3269    return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
3270  }
3271
3272  case TYPE_VARIABLE_ARRAY: {
3273    QualType ElementType = readType(*Loc.F, Record, Idx);
3274    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3275    unsigned IndexTypeQuals = Record[2];
3276    SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3277    SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3278    return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
3279                                         ASM, IndexTypeQuals,
3280                                         SourceRange(LBLoc, RBLoc));
3281  }
3282
3283  case TYPE_VECTOR: {
3284    if (Record.size() != 3) {
3285      Error("incorrect encoding of vector type in AST file");
3286      return QualType();
3287    }
3288
3289    QualType ElementType = readType(*Loc.F, Record, Idx);
3290    unsigned NumElements = Record[1];
3291    unsigned VecKind = Record[2];
3292    return Context->getVectorType(ElementType, NumElements,
3293                                  (VectorType::VectorKind)VecKind);
3294  }
3295
3296  case TYPE_EXT_VECTOR: {
3297    if (Record.size() != 3) {
3298      Error("incorrect encoding of extended vector type in AST file");
3299      return QualType();
3300    }
3301
3302    QualType ElementType = readType(*Loc.F, Record, Idx);
3303    unsigned NumElements = Record[1];
3304    return Context->getExtVectorType(ElementType, NumElements);
3305  }
3306
3307  case TYPE_FUNCTION_NO_PROTO: {
3308    if (Record.size() != 6) {
3309      Error("incorrect encoding of no-proto function type");
3310      return QualType();
3311    }
3312    QualType ResultType = readType(*Loc.F, Record, Idx);
3313    FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3314                               (CallingConv)Record[4], Record[5]);
3315    return Context->getFunctionNoProtoType(ResultType, Info);
3316  }
3317
3318  case TYPE_FUNCTION_PROTO: {
3319    QualType ResultType = readType(*Loc.F, Record, Idx);
3320
3321    FunctionProtoType::ExtProtoInfo EPI;
3322    EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3323                                        /*hasregparm*/ Record[2],
3324                                        /*regparm*/ Record[3],
3325                                        static_cast<CallingConv>(Record[4]),
3326                                        /*produces*/ Record[5]);
3327
3328    unsigned Idx = 6;
3329    unsigned NumParams = Record[Idx++];
3330    SmallVector<QualType, 16> ParamTypes;
3331    for (unsigned I = 0; I != NumParams; ++I)
3332      ParamTypes.push_back(readType(*Loc.F, Record, Idx));
3333
3334    EPI.Variadic = Record[Idx++];
3335    EPI.TypeQuals = Record[Idx++];
3336    EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
3337    ExceptionSpecificationType EST =
3338        static_cast<ExceptionSpecificationType>(Record[Idx++]);
3339    EPI.ExceptionSpecType = EST;
3340    if (EST == EST_Dynamic) {
3341      EPI.NumExceptions = Record[Idx++];
3342      SmallVector<QualType, 2> Exceptions;
3343      for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3344        Exceptions.push_back(readType(*Loc.F, Record, Idx));
3345      EPI.Exceptions = Exceptions.data();
3346    } else if (EST == EST_ComputedNoexcept) {
3347      EPI.NoexceptExpr = ReadExpr(*Loc.F);
3348    }
3349    return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
3350                                    EPI);
3351  }
3352
3353  case TYPE_UNRESOLVED_USING: {
3354    unsigned Idx = 0;
3355    return Context->getTypeDeclType(
3356                  ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3357  }
3358
3359  case TYPE_TYPEDEF: {
3360    if (Record.size() != 2) {
3361      Error("incorrect encoding of typedef type");
3362      return QualType();
3363    }
3364    unsigned Idx = 0;
3365    TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
3366    QualType Canonical = readType(*Loc.F, Record, Idx);
3367    if (!Canonical.isNull())
3368      Canonical = Context->getCanonicalType(Canonical);
3369    return Context->getTypedefType(Decl, Canonical);
3370  }
3371
3372  case TYPE_TYPEOF_EXPR:
3373    return Context->getTypeOfExprType(ReadExpr(*Loc.F));
3374
3375  case TYPE_TYPEOF: {
3376    if (Record.size() != 1) {
3377      Error("incorrect encoding of typeof(type) in AST file");
3378      return QualType();
3379    }
3380    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3381    return Context->getTypeOfType(UnderlyingType);
3382  }
3383
3384  case TYPE_DECLTYPE:
3385    return Context->getDecltypeType(ReadExpr(*Loc.F));
3386
3387  case TYPE_UNARY_TRANSFORM: {
3388    QualType BaseType = readType(*Loc.F, Record, Idx);
3389    QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3390    UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3391    return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3392  }
3393
3394  case TYPE_AUTO:
3395    return Context->getAutoType(readType(*Loc.F, Record, Idx));
3396
3397  case TYPE_RECORD: {
3398    if (Record.size() != 2) {
3399      Error("incorrect encoding of record type");
3400      return QualType();
3401    }
3402    unsigned Idx = 0;
3403    bool IsDependent = Record[Idx++];
3404    QualType T
3405      = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
3406    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3407    return T;
3408  }
3409
3410  case TYPE_ENUM: {
3411    if (Record.size() != 2) {
3412      Error("incorrect encoding of enum type");
3413      return QualType();
3414    }
3415    unsigned Idx = 0;
3416    bool IsDependent = Record[Idx++];
3417    QualType T
3418      = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
3419    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3420    return T;
3421  }
3422
3423  case TYPE_ATTRIBUTED: {
3424    if (Record.size() != 3) {
3425      Error("incorrect encoding of attributed type");
3426      return QualType();
3427    }
3428    QualType modifiedType = readType(*Loc.F, Record, Idx);
3429    QualType equivalentType = readType(*Loc.F, Record, Idx);
3430    AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3431    return Context->getAttributedType(kind, modifiedType, equivalentType);
3432  }
3433
3434  case TYPE_PAREN: {
3435    if (Record.size() != 1) {
3436      Error("incorrect encoding of paren type");
3437      return QualType();
3438    }
3439    QualType InnerType = readType(*Loc.F, Record, Idx);
3440    return Context->getParenType(InnerType);
3441  }
3442
3443  case TYPE_PACK_EXPANSION: {
3444    if (Record.size() != 2) {
3445      Error("incorrect encoding of pack expansion type");
3446      return QualType();
3447    }
3448    QualType Pattern = readType(*Loc.F, Record, Idx);
3449    if (Pattern.isNull())
3450      return QualType();
3451    llvm::Optional<unsigned> NumExpansions;
3452    if (Record[1])
3453      NumExpansions = Record[1] - 1;
3454    return Context->getPackExpansionType(Pattern, NumExpansions);
3455  }
3456
3457  case TYPE_ELABORATED: {
3458    unsigned Idx = 0;
3459    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3460    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3461    QualType NamedType = readType(*Loc.F, Record, Idx);
3462    return Context->getElaboratedType(Keyword, NNS, NamedType);
3463  }
3464
3465  case TYPE_OBJC_INTERFACE: {
3466    unsigned Idx = 0;
3467    ObjCInterfaceDecl *ItfD
3468      = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
3469    return Context->getObjCInterfaceType(ItfD);
3470  }
3471
3472  case TYPE_OBJC_OBJECT: {
3473    unsigned Idx = 0;
3474    QualType Base = readType(*Loc.F, Record, Idx);
3475    unsigned NumProtos = Record[Idx++];
3476    SmallVector<ObjCProtocolDecl*, 4> Protos;
3477    for (unsigned I = 0; I != NumProtos; ++I)
3478      Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
3479    return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
3480  }
3481
3482  case TYPE_OBJC_OBJECT_POINTER: {
3483    unsigned Idx = 0;
3484    QualType Pointee = readType(*Loc.F, Record, Idx);
3485    return Context->getObjCObjectPointerType(Pointee);
3486  }
3487
3488  case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
3489    unsigned Idx = 0;
3490    QualType Parm = readType(*Loc.F, Record, Idx);
3491    QualType Replacement = readType(*Loc.F, Record, Idx);
3492    return
3493      Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3494                                            Replacement);
3495  }
3496
3497  case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3498    unsigned Idx = 0;
3499    QualType Parm = readType(*Loc.F, Record, Idx);
3500    TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3501    return Context->getSubstTemplateTypeParmPackType(
3502                                               cast<TemplateTypeParmType>(Parm),
3503                                                     ArgPack);
3504  }
3505
3506  case TYPE_INJECTED_CLASS_NAME: {
3507    CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
3508    QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
3509    // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
3510    // for AST reading, too much interdependencies.
3511    return
3512      QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
3513  }
3514
3515  case TYPE_TEMPLATE_TYPE_PARM: {
3516    unsigned Idx = 0;
3517    unsigned Depth = Record[Idx++];
3518    unsigned Index = Record[Idx++];
3519    bool Pack = Record[Idx++];
3520    TemplateTypeParmDecl *D
3521      = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
3522    return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
3523  }
3524
3525  case TYPE_DEPENDENT_NAME: {
3526    unsigned Idx = 0;
3527    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3528    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3529    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3530    QualType Canon = readType(*Loc.F, Record, Idx);
3531    if (!Canon.isNull())
3532      Canon = Context->getCanonicalType(Canon);
3533    return Context->getDependentNameType(Keyword, NNS, Name, Canon);
3534  }
3535
3536  case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
3537    unsigned Idx = 0;
3538    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3539    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
3540    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3541    unsigned NumArgs = Record[Idx++];
3542    SmallVector<TemplateArgument, 8> Args;
3543    Args.reserve(NumArgs);
3544    while (NumArgs--)
3545      Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
3546    return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3547                                                      Args.size(), Args.data());
3548  }
3549
3550  case TYPE_DEPENDENT_SIZED_ARRAY: {
3551    unsigned Idx = 0;
3552
3553    // ArrayType
3554    QualType ElementType = readType(*Loc.F, Record, Idx);
3555    ArrayType::ArraySizeModifier ASM
3556      = (ArrayType::ArraySizeModifier)Record[Idx++];
3557    unsigned IndexTypeQuals = Record[Idx++];
3558
3559    // DependentSizedArrayType
3560    Expr *NumElts = ReadExpr(*Loc.F);
3561    SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
3562
3563    return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3564                                               IndexTypeQuals, Brackets);
3565  }
3566
3567  case TYPE_TEMPLATE_SPECIALIZATION: {
3568    unsigned Idx = 0;
3569    bool IsDependent = Record[Idx++];
3570    TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
3571    SmallVector<TemplateArgument, 8> Args;
3572    ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
3573    QualType Underlying = readType(*Loc.F, Record, Idx);
3574    QualType T;
3575    if (Underlying.isNull())
3576      T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3577                                                          Args.size());
3578    else
3579      T = Context->getTemplateSpecializationType(Name, Args.data(),
3580                                                 Args.size(), Underlying);
3581    const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3582    return T;
3583  }
3584  }
3585  // Suppress a GCC warning
3586  return QualType();
3587}
3588
3589class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
3590  ASTReader &Reader;
3591  Module &F;
3592  llvm::BitstreamCursor &DeclsCursor;
3593  const ASTReader::RecordData &Record;
3594  unsigned &Idx;
3595
3596  SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3597                                    unsigned &I) {
3598    return Reader.ReadSourceLocation(F, R, I);
3599  }
3600
3601  template<typename T>
3602  T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3603    return Reader.ReadDeclAs<T>(F, Record, Idx);
3604  }
3605
3606public:
3607  TypeLocReader(ASTReader &Reader, Module &F,
3608                const ASTReader::RecordData &Record, unsigned &Idx)
3609    : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3610  { }
3611
3612  // We want compile-time assurance that we've enumerated all of
3613  // these, so unfortunately we have to declare them first, then
3614  // define them out-of-line.
3615#define ABSTRACT_TYPELOC(CLASS, PARENT)
3616#define TYPELOC(CLASS, PARENT) \
3617  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
3618#include "clang/AST/TypeLocNodes.def"
3619
3620  void VisitFunctionTypeLoc(FunctionTypeLoc);
3621  void VisitArrayTypeLoc(ArrayTypeLoc);
3622};
3623
3624void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3625  // nothing to do
3626}
3627void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3628  TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
3629  if (TL.needsExtraLocalData()) {
3630    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3631    TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3632    TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3633    TL.setModeAttr(Record[Idx++]);
3634  }
3635}
3636void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
3637  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3638}
3639void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
3640  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3641}
3642void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3643  TL.setCaretLoc(ReadSourceLocation(Record, Idx));
3644}
3645void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3646  TL.setAmpLoc(ReadSourceLocation(Record, Idx));
3647}
3648void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3649  TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
3650}
3651void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3652  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3653  TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3654}
3655void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
3656  TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3657  TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
3658  if (Record[Idx++])
3659    TL.setSizeExpr(Reader.ReadExpr(F));
3660  else
3661    TL.setSizeExpr(0);
3662}
3663void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3664  VisitArrayTypeLoc(TL);
3665}
3666void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3667  VisitArrayTypeLoc(TL);
3668}
3669void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3670  VisitArrayTypeLoc(TL);
3671}
3672void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3673                                            DependentSizedArrayTypeLoc TL) {
3674  VisitArrayTypeLoc(TL);
3675}
3676void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3677                                        DependentSizedExtVectorTypeLoc TL) {
3678  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3679}
3680void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
3681  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3682}
3683void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
3684  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3685}
3686void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3687  TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3688  TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
3689  TL.setTrailingReturn(Record[Idx++]);
3690  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3691    TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
3692  }
3693}
3694void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3695  VisitFunctionTypeLoc(TL);
3696}
3697void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3698  VisitFunctionTypeLoc(TL);
3699}
3700void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
3701  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3702}
3703void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3704  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3705}
3706void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3707  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3708  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3709  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3710}
3711void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3712  TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3713  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3714  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3715  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3716}
3717void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
3718  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3719}
3720void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3721  TL.setKWLoc(ReadSourceLocation(Record, Idx));
3722  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3723  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3724  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3725}
3726void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3727  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3728}
3729void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
3730  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3731}
3732void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
3733  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3734}
3735void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3736  TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3737  if (TL.hasAttrOperand()) {
3738    SourceRange range;
3739    range.setBegin(ReadSourceLocation(Record, Idx));
3740    range.setEnd(ReadSourceLocation(Record, Idx));
3741    TL.setAttrOperandParensRange(range);
3742  }
3743  if (TL.hasAttrExprOperand()) {
3744    if (Record[Idx++])
3745      TL.setAttrExprOperand(Reader.ReadExpr(F));
3746    else
3747      TL.setAttrExprOperand(0);
3748  } else if (TL.hasAttrEnumOperand())
3749    TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3750}
3751void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
3752  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3753}
3754void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3755                                            SubstTemplateTypeParmTypeLoc TL) {
3756  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3757}
3758void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3759                                          SubstTemplateTypeParmPackTypeLoc TL) {
3760  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3761}
3762void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3763                                           TemplateSpecializationTypeLoc TL) {
3764  TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3765  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3766  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3767  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3768    TL.setArgLocInfo(i,
3769        Reader.GetTemplateArgumentLocInfo(F,
3770                                          TL.getTypePtr()->getArg(i).getKind(),
3771                                          Record, Idx));
3772}
3773void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3774  TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3775  TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3776}
3777void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3778  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3779  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3780}
3781void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3782  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3783}
3784void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
3785  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3786  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3787  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3788}
3789void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3790       DependentTemplateSpecializationTypeLoc TL) {
3791  TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3792  TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3793  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3794  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3795  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3796  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3797    TL.setArgLocInfo(I,
3798        Reader.GetTemplateArgumentLocInfo(F,
3799                                          TL.getTypePtr()->getArg(I).getKind(),
3800                                          Record, Idx));
3801}
3802void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3803  TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3804}
3805void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3806  TL.setNameLoc(ReadSourceLocation(Record, Idx));
3807}
3808void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3809  TL.setHasBaseTypeAsWritten(Record[Idx++]);
3810  TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3811  TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3812  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3813    TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
3814}
3815void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3816  TL.setStarLoc(ReadSourceLocation(Record, Idx));
3817}
3818
3819TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
3820                                             const RecordData &Record,
3821                                             unsigned &Idx) {
3822  QualType InfoTy = readType(F, Record, Idx);
3823  if (InfoTy.isNull())
3824    return 0;
3825
3826  TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
3827  TypeLocReader TLR(*this, F, Record, Idx);
3828  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
3829    TLR.Visit(TL);
3830  return TInfo;
3831}
3832
3833QualType ASTReader::GetType(TypeID ID) {
3834  unsigned FastQuals = ID & Qualifiers::FastMask;
3835  unsigned Index = ID >> Qualifiers::FastWidth;
3836
3837  if (Index < NUM_PREDEF_TYPE_IDS) {
3838    QualType T;
3839    switch ((PredefinedTypeIDs)Index) {
3840    case PREDEF_TYPE_NULL_ID: return QualType();
3841    case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3842    case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
3843
3844    case PREDEF_TYPE_CHAR_U_ID:
3845    case PREDEF_TYPE_CHAR_S_ID:
3846      // FIXME: Check that the signedness of CharTy is correct!
3847      T = Context->CharTy;
3848      break;
3849
3850    case PREDEF_TYPE_UCHAR_ID:      T = Context->UnsignedCharTy;     break;
3851    case PREDEF_TYPE_USHORT_ID:     T = Context->UnsignedShortTy;    break;
3852    case PREDEF_TYPE_UINT_ID:       T = Context->UnsignedIntTy;      break;
3853    case PREDEF_TYPE_ULONG_ID:      T = Context->UnsignedLongTy;     break;
3854    case PREDEF_TYPE_ULONGLONG_ID:  T = Context->UnsignedLongLongTy; break;
3855    case PREDEF_TYPE_UINT128_ID:    T = Context->UnsignedInt128Ty;   break;
3856    case PREDEF_TYPE_SCHAR_ID:      T = Context->SignedCharTy;       break;
3857    case PREDEF_TYPE_WCHAR_ID:      T = Context->WCharTy;            break;
3858    case PREDEF_TYPE_SHORT_ID:      T = Context->ShortTy;            break;
3859    case PREDEF_TYPE_INT_ID:        T = Context->IntTy;              break;
3860    case PREDEF_TYPE_LONG_ID:       T = Context->LongTy;             break;
3861    case PREDEF_TYPE_LONGLONG_ID:   T = Context->LongLongTy;         break;
3862    case PREDEF_TYPE_INT128_ID:     T = Context->Int128Ty;           break;
3863    case PREDEF_TYPE_FLOAT_ID:      T = Context->FloatTy;            break;
3864    case PREDEF_TYPE_DOUBLE_ID:     T = Context->DoubleTy;           break;
3865    case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy;       break;
3866    case PREDEF_TYPE_OVERLOAD_ID:   T = Context->OverloadTy;         break;
3867    case PREDEF_TYPE_BOUND_MEMBER:  T = Context->BoundMemberTy;      break;
3868    case PREDEF_TYPE_DEPENDENT_ID:  T = Context->DependentTy;        break;
3869    case PREDEF_TYPE_UNKNOWN_ANY:   T = Context->UnknownAnyTy;       break;
3870    case PREDEF_TYPE_NULLPTR_ID:    T = Context->NullPtrTy;          break;
3871    case PREDEF_TYPE_CHAR16_ID:     T = Context->Char16Ty;           break;
3872    case PREDEF_TYPE_CHAR32_ID:     T = Context->Char32Ty;           break;
3873    case PREDEF_TYPE_OBJC_ID:       T = Context->ObjCBuiltinIdTy;    break;
3874    case PREDEF_TYPE_OBJC_CLASS:    T = Context->ObjCBuiltinClassTy; break;
3875    case PREDEF_TYPE_OBJC_SEL:      T = Context->ObjCBuiltinSelTy;   break;
3876    }
3877
3878    assert(!T.isNull() && "Unknown predefined type");
3879    return T.withFastQualifiers(FastQuals);
3880  }
3881
3882  Index -= NUM_PREDEF_TYPE_IDS;
3883  assert(Index < TypesLoaded.size() && "Type index out-of-range");
3884  if (TypesLoaded[Index].isNull()) {
3885    TypesLoaded[Index] = readTypeRecord(Index);
3886    if (TypesLoaded[Index].isNull())
3887      return QualType();
3888
3889    TypesLoaded[Index]->setFromAST();
3890    TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
3891    if (DeserializationListener)
3892      DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
3893                                        TypesLoaded[Index]);
3894  }
3895
3896  return TypesLoaded[Index].withFastQualifiers(FastQuals);
3897}
3898
3899QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
3900  return GetType(getGlobalTypeID(F, LocalID));
3901}
3902
3903serialization::TypeID
3904ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
3905  // FIXME: Map from local type ID to global type ID.
3906  return LocalID;
3907}
3908
3909TypeID ASTReader::GetTypeID(QualType T) const {
3910  return MakeTypeID(T,
3911              std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3912}
3913
3914TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3915  if (T.isNull())
3916    return TypeIdx();
3917  assert(!T.getLocalFastQualifiers());
3918
3919  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3920  // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3921  // comparing keys of ASTDeclContextNameLookupTable.
3922  // If the type didn't come from the AST file use a specially marked index
3923  // so that any hash/key comparison fail since no such index is stored
3924  // in a AST file.
3925  if (I == TypeIdxs.end())
3926    return TypeIdx(-1);
3927  return I->second;
3928}
3929
3930TemplateArgumentLocInfo
3931ASTReader::GetTemplateArgumentLocInfo(Module &F,
3932                                      TemplateArgument::ArgKind Kind,
3933                                      const RecordData &Record,
3934                                      unsigned &Index) {
3935  switch (Kind) {
3936  case TemplateArgument::Expression:
3937    return ReadExpr(F);
3938  case TemplateArgument::Type:
3939    return GetTypeSourceInfo(F, Record, Index);
3940  case TemplateArgument::Template: {
3941    NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3942                                                                     Index);
3943    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3944    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3945                                   SourceLocation());
3946  }
3947  case TemplateArgument::TemplateExpansion: {
3948    NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3949                                                                     Index);
3950    SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3951    SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3952    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3953                                   EllipsisLoc);
3954  }
3955  case TemplateArgument::Null:
3956  case TemplateArgument::Integral:
3957  case TemplateArgument::Declaration:
3958  case TemplateArgument::Pack:
3959    return TemplateArgumentLocInfo();
3960  }
3961  llvm_unreachable("unexpected template argument loc");
3962  return TemplateArgumentLocInfo();
3963}
3964
3965TemplateArgumentLoc
3966ASTReader::ReadTemplateArgumentLoc(Module &F,
3967                                   const RecordData &Record, unsigned &Index) {
3968  TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
3969
3970  if (Arg.getKind() == TemplateArgument::Expression) {
3971    if (Record[Index++]) // bool InfoHasSameExpr.
3972      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3973  }
3974  return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
3975                                                             Record, Index));
3976}
3977
3978Decl *ASTReader::GetExternalDecl(uint32_t ID) {
3979  return GetDecl(ID);
3980}
3981
3982uint64_t
3983ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3984  if (ID == 0)
3985    return 0;
3986
3987  GlobalCXXBaseSpecifiersMapType::iterator I =
3988      GlobalCXXBaseSpecifiersMap.find(ID);
3989
3990  assert (I != GlobalCXXBaseSpecifiersMap.end() &&
3991                                    "Corrupted global CXX base specifiers map");
3992
3993  return I->second.first->CXXBaseSpecifiersOffsets[ID - 1 +
3994                                               I->second.second] +
3995                                               I->second.first->GlobalBitOffset;
3996
3997}
3998
3999CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
4000  RecordLocation Loc = getLocalBitOffset(Offset);
4001  llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
4002  SavedStreamPosition SavedPosition(Cursor);
4003  Cursor.JumpToBit(Loc.Offset);
4004  ReadingKindTracker ReadingKind(Read_Decl, *this);
4005  RecordData Record;
4006  unsigned Code = Cursor.ReadCode();
4007  unsigned RecCode = Cursor.ReadRecord(Code, Record);
4008  if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4009    Error("Malformed AST file: missing C++ base specifiers");
4010    return 0;
4011  }
4012
4013  unsigned Idx = 0;
4014  unsigned NumBases = Record[Idx++];
4015  void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4016  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4017  for (unsigned I = 0; I != NumBases; ++I)
4018    Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
4019  return Bases;
4020}
4021
4022TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
4023  if (!DeclsLoaded[0]) {
4024    ReadDeclRecord(0, 1);
4025    if (DeserializationListener)
4026      DeserializationListener->DeclRead(1, DeclsLoaded[0]);
4027  }
4028
4029  return cast<TranslationUnitDecl>(DeclsLoaded[0]);
4030}
4031
4032serialization::DeclID
4033ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
4034  // FIXME: Perform local -> global remapping for declarations.
4035  return LocalID;
4036}
4037
4038Decl *ASTReader::GetDecl(DeclID ID) {
4039  if (ID == 0)
4040    return 0;
4041
4042  if (ID > DeclsLoaded.size()) {
4043    Error("declaration ID out-of-range for AST file");
4044    return 0;
4045  }
4046
4047  unsigned Index = ID - 1;
4048  if (!DeclsLoaded[Index]) {
4049    ReadDeclRecord(Index, ID);
4050    if (DeserializationListener)
4051      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4052  }
4053
4054  return DeclsLoaded[Index];
4055}
4056
4057serialization::DeclID ASTReader::ReadDeclID(Module &F,
4058                                            const RecordData &Record,
4059                                            unsigned &Idx) {
4060  if (Idx >= Record.size()) {
4061    Error("Corrupted AST file");
4062    return 0;
4063  }
4064
4065  return getGlobalDeclID(F, Record[Idx++]);
4066}
4067
4068/// \brief Resolve the offset of a statement into a statement.
4069///
4070/// This operation will read a new statement from the external
4071/// source each time it is called, and is meant to be used via a
4072/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
4073Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
4074  // Switch case IDs are per Decl.
4075  ClearSwitchCaseIDs();
4076
4077  // Offset here is a global offset across the entire chain.
4078  RecordLocation Loc = getLocalBitOffset(Offset);
4079  Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4080  return ReadStmtFromStream(*Loc.F);
4081}
4082
4083ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
4084                                         bool (*isKindWeWant)(Decl::Kind),
4085                                         SmallVectorImpl<Decl*> &Decls) {
4086  // There might be lexical decls in multiple parts of the chain, for the TU
4087  // at least.
4088  // DeclContextOffsets might reallocate as we load additional decls below,
4089  // so make a copy of the vector.
4090  DeclContextInfos Infos = DeclContextOffsets[DC];
4091  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4092       I != E; ++I) {
4093    // IDs can be 0 if this context doesn't contain declarations.
4094    if (!I->LexicalDecls)
4095      continue;
4096
4097    // Load all of the declaration IDs
4098    for (const KindDeclIDPair *ID = I->LexicalDecls,
4099                              *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4100      if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4101        continue;
4102
4103      Decl *D = GetLocalDecl(*I->F, ID->second);
4104      assert(D && "Null decl in lexical decls");
4105      Decls.push_back(D);
4106    }
4107  }
4108
4109  ++NumLexicalDeclContextsRead;
4110  return ELR_Success;
4111}
4112
4113DeclContext::lookup_result
4114ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
4115                                          DeclarationName Name) {
4116  assert(DC->hasExternalVisibleStorage() &&
4117         "DeclContext has no visible decls in storage");
4118  if (!Name)
4119    return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4120                                      DeclContext::lookup_iterator(0));
4121
4122  SmallVector<NamedDecl *, 64> Decls;
4123  // There might be visible decls in multiple parts of the chain, for the TU
4124  // and namespaces. For any given name, the last available results replace
4125  // all earlier ones. For this reason, we walk in reverse.
4126  DeclContextInfos &Infos = DeclContextOffsets[DC];
4127  for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
4128       I != E; ++I) {
4129    if (!I->NameLookupTableData)
4130      continue;
4131
4132    ASTDeclContextNameLookupTable *LookupTable =
4133        (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4134    ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4135    if (Pos == LookupTable->end())
4136      continue;
4137
4138    ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4139    for (; Data.first != Data.second; ++Data.first)
4140      Decls.push_back(GetLocalDeclAs<NamedDecl>(*I->F, *Data.first));
4141    break;
4142  }
4143
4144  ++NumVisibleDeclContextsRead;
4145
4146  SetExternalVisibleDeclsForName(DC, Name, Decls);
4147  return const_cast<DeclContext*>(DC)->lookup(Name);
4148}
4149
4150void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4151  assert(DC->hasExternalVisibleStorage() &&
4152         "DeclContext has no visible decls in storage");
4153
4154  SmallVector<NamedDecl *, 64> Decls;
4155  // There might be visible decls in multiple parts of the chain, for the TU
4156  // and namespaces.
4157  DeclContextInfos &Infos = DeclContextOffsets[DC];
4158  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4159       I != E; ++I) {
4160    if (!I->NameLookupTableData)
4161      continue;
4162
4163    ASTDeclContextNameLookupTable *LookupTable =
4164        (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4165    for (ASTDeclContextNameLookupTable::item_iterator
4166           ItemI = LookupTable->item_begin(),
4167           ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4168      ASTDeclContextNameLookupTable::item_iterator::value_type Val
4169          = *ItemI;
4170      ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4171      Decls.clear();
4172      for (; Data.first != Data.second; ++Data.first)
4173        Decls.push_back(GetLocalDeclAs<NamedDecl>(*I->F, *Data.first));
4174      MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4175    }
4176  }
4177}
4178
4179void ASTReader::PassInterestingDeclsToConsumer() {
4180  assert(Consumer);
4181  while (!InterestingDecls.empty()) {
4182    DeclGroupRef DG(InterestingDecls.front());
4183    InterestingDecls.pop_front();
4184    Consumer->HandleInterestingDecl(DG);
4185  }
4186}
4187
4188void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
4189  this->Consumer = Consumer;
4190
4191  if (!Consumer)
4192    return;
4193
4194  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
4195    // Force deserialization of this decl, which will cause it to be queued for
4196    // passing to the consumer.
4197    GetDecl(ExternalDefinitions[I]);
4198  }
4199
4200  PassInterestingDeclsToConsumer();
4201}
4202
4203void ASTReader::PrintStats() {
4204  std::fprintf(stderr, "*** AST File Statistics:\n");
4205
4206  unsigned NumTypesLoaded
4207    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
4208                                      QualType());
4209  unsigned NumDeclsLoaded
4210    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4211                                      (Decl *)0);
4212  unsigned NumIdentifiersLoaded
4213    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4214                                            IdentifiersLoaded.end(),
4215                                            (IdentifierInfo *)0);
4216  unsigned NumSelectorsLoaded
4217    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4218                                          SelectorsLoaded.end(),
4219                                          Selector());
4220
4221  std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
4222  std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
4223  if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
4224    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
4225                 NumSLocEntriesRead, TotalNumSLocEntries,
4226                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
4227  if (!TypesLoaded.empty())
4228    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
4229                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4230                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4231  if (!DeclsLoaded.empty())
4232    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
4233                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4234                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
4235  if (!IdentifiersLoaded.empty())
4236    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
4237                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4238                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
4239  if (!SelectorsLoaded.empty())
4240    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
4241                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4242                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
4243  if (TotalNumStatements)
4244    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
4245                 NumStatementsRead, TotalNumStatements,
4246                 ((float)NumStatementsRead/TotalNumStatements * 100));
4247  if (TotalNumMacros)
4248    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
4249                 NumMacrosRead, TotalNumMacros,
4250                 ((float)NumMacrosRead/TotalNumMacros * 100));
4251  if (TotalLexicalDeclContexts)
4252    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
4253                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4254                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4255                  * 100));
4256  if (TotalVisibleDeclContexts)
4257    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
4258                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4259                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4260                  * 100));
4261  if (TotalNumMethodPoolEntries) {
4262    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
4263                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4264                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4265                  * 100));
4266    std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
4267  }
4268  std::fprintf(stderr, "\n");
4269  dump();
4270  std::fprintf(stderr, "\n");
4271}
4272
4273template<typename Key, typename Module, unsigned InitialCapacity>
4274static void
4275dumpModuleIDMap(StringRef Name,
4276                const ContinuousRangeMap<Key, Module *,
4277                                         InitialCapacity> &Map) {
4278  if (Map.begin() == Map.end())
4279    return;
4280
4281  typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
4282  llvm::errs() << Name << ":\n";
4283  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4284       I != IEnd; ++I) {
4285    llvm::errs() << "  " << I->first << " -> " << I->second->FileName
4286      << "\n";
4287  }
4288}
4289
4290template<typename Key, typename Module, typename Adjustment,
4291         unsigned InitialCapacity>
4292static void
4293dumpModuleIDOffsetMap(StringRef Name,
4294                      const ContinuousRangeMap<Key,
4295                                               std::pair<Module *,
4296                                                         Adjustment>,
4297                                               InitialCapacity> &Map) {
4298  if (Map.begin() == Map.end())
4299    return;
4300
4301  typedef ContinuousRangeMap<Key, std::pair<Module *, Adjustment>,
4302                             InitialCapacity> MapType;
4303  llvm::errs() << Name << ":\n";
4304  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4305       I != IEnd; ++I) {
4306    llvm::errs() << "  " << I->first << " -> (" << I->second.first->FileName
4307                 << ", " << I->second.second << ")\n";
4308  }
4309}
4310
4311void ASTReader::dump() {
4312  llvm::errs() << "*** AST File Remapping:\n";
4313  dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
4314  dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
4315  dumpModuleIDOffsetMap("Global type map", GlobalTypeMap);
4316  dumpModuleIDOffsetMap("Global declaration map", GlobalDeclMap);
4317  dumpModuleIDOffsetMap("Global identifier map", GlobalIdentifierMap);
4318  dumpModuleIDOffsetMap("Global selector map", GlobalSelectorMap);
4319  dumpModuleIDOffsetMap("Global macro definition map",
4320                        GlobalMacroDefinitionMap);
4321  dumpModuleIDOffsetMap("Global preprocessed entity map",
4322                        GlobalPreprocessedEntityMap);
4323
4324}
4325
4326/// Return the amount of memory used by memory buffers, breaking down
4327/// by heap-backed versus mmap'ed memory.
4328void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4329  for (ModuleConstIterator I = ModuleMgr.begin(),
4330      E = ModuleMgr.end(); I != E; ++I) {
4331    if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
4332      size_t bytes = buf->getBufferSize();
4333      switch (buf->getBufferKind()) {
4334        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4335          sizes.malloc_bytes += bytes;
4336          break;
4337        case llvm::MemoryBuffer::MemoryBuffer_MMap:
4338          sizes.mmap_bytes += bytes;
4339          break;
4340      }
4341    }
4342  }
4343}
4344
4345void ASTReader::InitializeSema(Sema &S) {
4346  SemaObj = &S;
4347  S.ExternalSource = this;
4348
4349  // Makes sure any declarations that were deserialized "too early"
4350  // still get added to the identifier's declaration chains.
4351  for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4352    if (SemaObj->TUScope)
4353      SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4354
4355    SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4356  }
4357  PreloadedDecls.clear();
4358
4359  // Load the offsets of the declarations that Sema references.
4360  // They will be lazily deserialized when needed.
4361  if (!SemaDeclRefs.empty()) {
4362    assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4363    if (!SemaObj->StdNamespace)
4364      SemaObj->StdNamespace = SemaDeclRefs[0];
4365    if (!SemaObj->StdBadAlloc)
4366      SemaObj->StdBadAlloc = SemaDeclRefs[1];
4367  }
4368
4369  // The special data sets below always come from the most recent PCH,
4370  // which is at the front of the chain.
4371  Module &F = ModuleMgr.getPrimaryModule();
4372
4373  // If there were any pending implicit instantiations, deserialize them
4374  // and add them to Sema's queue of such instantiations.
4375  assert(F.PendingInstantiations.size() % 2 == 0 &&
4376         "Expected pairs of entries");
4377  for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4378    ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4379    SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4380    SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4381  }
4382
4383  // If there were any weak undeclared identifiers, deserialize them and add to
4384  // Sema's list of weak undeclared identifiers.
4385  if (!WeakUndeclaredIdentifiers.empty()) {
4386    unsigned Idx = 0;
4387    for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4388      IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4389      IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4390      SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4391      bool Used = WeakUndeclaredIdentifiers[Idx++];
4392      Sema::WeakInfo WI(AliasId, Loc);
4393      WI.setUsed(Used);
4394      SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4395    }
4396  }
4397
4398  // If there were any VTable uses, deserialize the information and add it
4399  // to Sema's vector and map of VTable uses.
4400  if (!VTableUses.empty()) {
4401    unsigned Idx = 0;
4402    for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4403      CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4404      SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4405      bool DefinitionRequired = VTableUses[Idx++];
4406      SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4407      SemaObj->VTablesUsed[Class] = DefinitionRequired;
4408    }
4409  }
4410
4411  if (!FPPragmaOptions.empty()) {
4412    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4413    SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4414  }
4415
4416  if (!OpenCLExtensions.empty()) {
4417    unsigned I = 0;
4418#define OPENCLEXT(nm)  SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4419#include "clang/Basic/OpenCLExtensions.def"
4420
4421    assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4422  }
4423}
4424
4425IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4426  // Try to find this name within our on-disk hash tables. We start with the
4427  // most recent one, since that one contains the most up-to-date info.
4428  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4429    ASTIdentifierLookupTable *IdTable
4430        = (ASTIdentifierLookupTable *)(*I)->IdentifierLookupTable;
4431    if (!IdTable)
4432      continue;
4433    std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
4434    ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4435    if (Pos == IdTable->end())
4436      continue;
4437
4438    // Dereferencing the iterator has the effect of building the
4439    // IdentifierInfo node and populating it with the various
4440    // declarations it needs.
4441    return *Pos;
4442  }
4443  return 0;
4444}
4445
4446namespace clang {
4447  /// \brief An identifier-lookup iterator that enumerates all of the
4448  /// identifiers stored within a set of AST files.
4449  class ASTIdentifierIterator : public IdentifierIterator {
4450    /// \brief The AST reader whose identifiers are being enumerated.
4451    const ASTReader &Reader;
4452
4453    /// \brief The current index into the chain of AST files stored in
4454    /// the AST reader.
4455    unsigned Index;
4456
4457    /// \brief The current position within the identifier lookup table
4458    /// of the current AST file.
4459    ASTIdentifierLookupTable::key_iterator Current;
4460
4461    /// \brief The end position within the identifier lookup table of
4462    /// the current AST file.
4463    ASTIdentifierLookupTable::key_iterator End;
4464
4465  public:
4466    explicit ASTIdentifierIterator(const ASTReader &Reader);
4467
4468    virtual StringRef Next();
4469  };
4470}
4471
4472ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4473  : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
4474  ASTIdentifierLookupTable *IdTable
4475    = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
4476  Current = IdTable->key_begin();
4477  End = IdTable->key_end();
4478}
4479
4480StringRef ASTIdentifierIterator::Next() {
4481  while (Current == End) {
4482    // If we have exhausted all of our AST files, we're done.
4483    if (Index == 0)
4484      return StringRef();
4485
4486    --Index;
4487    ASTIdentifierLookupTable *IdTable
4488      = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4489        IdentifierLookupTable;
4490    Current = IdTable->key_begin();
4491    End = IdTable->key_end();
4492  }
4493
4494  // We have any identifiers remaining in the current AST file; return
4495  // the next one.
4496  std::pair<const char*, unsigned> Key = *Current;
4497  ++Current;
4498  return StringRef(Key.first, Key.second);
4499}
4500
4501IdentifierIterator *ASTReader::getIdentifiers() const {
4502  return new ASTIdentifierIterator(*this);
4503}
4504
4505std::pair<ObjCMethodList, ObjCMethodList>
4506ASTReader::ReadMethodPool(Selector Sel) {
4507  // Find this selector in a hash table. We want to find the most recent entry.
4508  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4509    Module &F = *(*I);
4510    if (!F.SelectorLookupTable)
4511      continue;
4512
4513    ASTSelectorLookupTable *PoolTable
4514      = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4515    ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
4516    if (Pos != PoolTable->end()) {
4517      ++NumSelectorsRead;
4518      // FIXME: Not quite happy with the statistics here. We probably should
4519      // disable this tracking when called via LoadSelector.
4520      // Also, should entries without methods count as misses?
4521      ++NumMethodPoolEntriesRead;
4522      ASTSelectorLookupTrait::data_type Data = *Pos;
4523      if (DeserializationListener)
4524        DeserializationListener->SelectorRead(Data.ID, Sel);
4525      return std::make_pair(Data.Instance, Data.Factory);
4526    }
4527  }
4528
4529  ++NumMethodPoolMisses;
4530  return std::pair<ObjCMethodList, ObjCMethodList>();
4531}
4532
4533void ASTReader::ReadKnownNamespaces(
4534                          SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4535  Namespaces.clear();
4536
4537  for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4538    if (NamespaceDecl *Namespace
4539                = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4540      Namespaces.push_back(Namespace);
4541  }
4542}
4543
4544void ASTReader::ReadTentativeDefinitions(
4545                  SmallVectorImpl<VarDecl *> &TentativeDefs) {
4546  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4547    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4548    if (Var)
4549      TentativeDefs.push_back(Var);
4550  }
4551  TentativeDefinitions.clear();
4552}
4553
4554void ASTReader::ReadUnusedFileScopedDecls(
4555                               SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4556  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4557    DeclaratorDecl *D
4558      = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4559    if (D)
4560      Decls.push_back(D);
4561  }
4562  UnusedFileScopedDecls.clear();
4563}
4564
4565void ASTReader::ReadDelegatingConstructors(
4566                                 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4567  for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4568    CXXConstructorDecl *D
4569      = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4570    if (D)
4571      Decls.push_back(D);
4572  }
4573  DelegatingCtorDecls.clear();
4574}
4575
4576void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4577  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4578    TypedefNameDecl *D
4579      = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4580    if (D)
4581      Decls.push_back(D);
4582  }
4583  ExtVectorDecls.clear();
4584}
4585
4586void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4587  for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4588    CXXRecordDecl *D
4589      = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4590    if (D)
4591      Decls.push_back(D);
4592  }
4593  DynamicClasses.clear();
4594}
4595
4596void
4597ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4598  for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4599    NamedDecl *D
4600      = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4601    if (D)
4602      Decls.push_back(D);
4603  }
4604  LocallyScopedExternalDecls.clear();
4605}
4606
4607void ASTReader::ReadReferencedSelectors(
4608       SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4609  if (ReferencedSelectorsData.empty())
4610    return;
4611
4612  // If there are @selector references added them to its pool. This is for
4613  // implementation of -Wselector.
4614  unsigned int DataSize = ReferencedSelectorsData.size()-1;
4615  unsigned I = 0;
4616  while (I < DataSize) {
4617    Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4618    SourceLocation SelLoc
4619      = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4620    Sels.push_back(std::make_pair(Sel, SelLoc));
4621  }
4622  ReferencedSelectorsData.clear();
4623}
4624
4625void ASTReader::LoadSelector(Selector Sel) {
4626  // It would be complicated to avoid reading the methods anyway. So don't.
4627  ReadMethodPool(Sel);
4628}
4629
4630void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
4631  assert(ID && "Non-zero identifier ID required");
4632  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4633  IdentifiersLoaded[ID - 1] = II;
4634  if (DeserializationListener)
4635    DeserializationListener->IdentifierRead(ID, II);
4636}
4637
4638/// \brief Set the globally-visible declarations associated with the given
4639/// identifier.
4640///
4641/// If the AST reader is currently in a state where the given declaration IDs
4642/// cannot safely be resolved, they are queued until it is safe to resolve
4643/// them.
4644///
4645/// \param II an IdentifierInfo that refers to one or more globally-visible
4646/// declarations.
4647///
4648/// \param DeclIDs the set of declaration IDs with the name @p II that are
4649/// visible at global scope.
4650///
4651/// \param Nonrecursive should be true to indicate that the caller knows that
4652/// this call is non-recursive, and therefore the globally-visible declarations
4653/// will not be placed onto the pending queue.
4654void
4655ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4656                              const SmallVectorImpl<uint32_t> &DeclIDs,
4657                                   bool Nonrecursive) {
4658  if (NumCurrentElementsDeserializing && !Nonrecursive) {
4659    PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4660    PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4661    PII.II = II;
4662    PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4663    return;
4664  }
4665
4666  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4667    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4668    if (SemaObj) {
4669      if (SemaObj->TUScope) {
4670        // Introduce this declaration into the translation-unit scope
4671        // and add it to the declaration chain for this identifier, so
4672        // that (unqualified) name lookup will find it.
4673        SemaObj->TUScope->AddDecl(D);
4674      }
4675      SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4676    } else {
4677      // Queue this declaration so that it will be added to the
4678      // translation unit scope and identifier's declaration chain
4679      // once a Sema object is known.
4680      PreloadedDecls.push_back(D);
4681    }
4682  }
4683}
4684
4685IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
4686  if (ID == 0)
4687    return 0;
4688
4689  if (IdentifiersLoaded.empty()) {
4690    Error("no identifier table in AST file");
4691    return 0;
4692  }
4693
4694  assert(PP && "Forgot to set Preprocessor ?");
4695  ID -= 1;
4696  if (!IdentifiersLoaded[ID]) {
4697    GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4698    assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
4699    unsigned Index = ID + I->second.second;
4700    const char *Str = I->second.first->IdentifierTableData
4701                    + I->second.first->IdentifierOffsets[Index];
4702
4703    // All of the strings in the AST file are preceded by a 16-bit length.
4704    // Extract that 16-bit length to avoid having to execute strlen().
4705    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4706    //  unsigned integers.  This is important to avoid integer overflow when
4707    //  we cast them to 'unsigned'.
4708    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4709    unsigned StrLen = (((unsigned) StrLenPtr[0])
4710                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4711    IdentifiersLoaded[ID]
4712      = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
4713    if (DeserializationListener)
4714      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4715  }
4716
4717  return IdentifiersLoaded[ID];
4718}
4719
4720bool ASTReader::ReadSLocEntry(int ID) {
4721  return ReadSLocEntryRecord(ID) != Success;
4722}
4723
4724Selector ASTReader::DecodeSelector(unsigned ID) {
4725  if (ID == 0)
4726    return Selector();
4727
4728  if (ID > SelectorsLoaded.size()) {
4729    Error("selector ID out of range in AST file");
4730    return Selector();
4731  }
4732
4733  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4734    // Load this selector from the selector table.
4735    GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4736    assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
4737    Module &F = *I->second.first;
4738    ASTSelectorLookupTrait Trait(*this, F);
4739    unsigned Idx = ID - 1 + I->second.second;
4740    SelectorsLoaded[ID - 1] =
4741      Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4742    if (DeserializationListener)
4743      DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4744  }
4745
4746  return SelectorsLoaded[ID - 1];
4747}
4748
4749Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
4750  return DecodeSelector(ID);
4751}
4752
4753uint32_t ASTReader::GetNumExternalSelectors() {
4754  // ID 0 (the null selector) is considered an external selector.
4755  return getTotalNumSelectors() + 1;
4756}
4757
4758serialization::SelectorID
4759ASTReader::getGlobalSelectorID(Module &F, unsigned LocalID) const {
4760  // FIXME: Perform local -> global remapping
4761  return LocalID;
4762}
4763
4764DeclarationName
4765ASTReader::ReadDeclarationName(Module &F,
4766                               const RecordData &Record, unsigned &Idx) {
4767  DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4768  switch (Kind) {
4769  case DeclarationName::Identifier:
4770    return DeclarationName(GetIdentifierInfo(Record, Idx));
4771
4772  case DeclarationName::ObjCZeroArgSelector:
4773  case DeclarationName::ObjCOneArgSelector:
4774  case DeclarationName::ObjCMultiArgSelector:
4775    return DeclarationName(GetSelector(Record, Idx));
4776
4777  case DeclarationName::CXXConstructorName:
4778    return Context->DeclarationNames.getCXXConstructorName(
4779                          Context->getCanonicalType(readType(F, Record, Idx)));
4780
4781  case DeclarationName::CXXDestructorName:
4782    return Context->DeclarationNames.getCXXDestructorName(
4783                          Context->getCanonicalType(readType(F, Record, Idx)));
4784
4785  case DeclarationName::CXXConversionFunctionName:
4786    return Context->DeclarationNames.getCXXConversionFunctionName(
4787                          Context->getCanonicalType(readType(F, Record, Idx)));
4788
4789  case DeclarationName::CXXOperatorName:
4790    return Context->DeclarationNames.getCXXOperatorName(
4791                                       (OverloadedOperatorKind)Record[Idx++]);
4792
4793  case DeclarationName::CXXLiteralOperatorName:
4794    return Context->DeclarationNames.getCXXLiteralOperatorName(
4795                                       GetIdentifierInfo(Record, Idx));
4796
4797  case DeclarationName::CXXUsingDirective:
4798    return DeclarationName::getUsingDirectiveName();
4799  }
4800
4801  // Required to silence GCC warning
4802  return DeclarationName();
4803}
4804
4805void ASTReader::ReadDeclarationNameLoc(Module &F,
4806                                       DeclarationNameLoc &DNLoc,
4807                                       DeclarationName Name,
4808                                      const RecordData &Record, unsigned &Idx) {
4809  switch (Name.getNameKind()) {
4810  case DeclarationName::CXXConstructorName:
4811  case DeclarationName::CXXDestructorName:
4812  case DeclarationName::CXXConversionFunctionName:
4813    DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4814    break;
4815
4816  case DeclarationName::CXXOperatorName:
4817    DNLoc.CXXOperatorName.BeginOpNameLoc
4818        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4819    DNLoc.CXXOperatorName.EndOpNameLoc
4820        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4821    break;
4822
4823  case DeclarationName::CXXLiteralOperatorName:
4824    DNLoc.CXXLiteralOperatorName.OpNameLoc
4825        = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4826    break;
4827
4828  case DeclarationName::Identifier:
4829  case DeclarationName::ObjCZeroArgSelector:
4830  case DeclarationName::ObjCOneArgSelector:
4831  case DeclarationName::ObjCMultiArgSelector:
4832  case DeclarationName::CXXUsingDirective:
4833    break;
4834  }
4835}
4836
4837void ASTReader::ReadDeclarationNameInfo(Module &F,
4838                                        DeclarationNameInfo &NameInfo,
4839                                      const RecordData &Record, unsigned &Idx) {
4840  NameInfo.setName(ReadDeclarationName(F, Record, Idx));
4841  NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4842  DeclarationNameLoc DNLoc;
4843  ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4844  NameInfo.setInfo(DNLoc);
4845}
4846
4847void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
4848                                  const RecordData &Record, unsigned &Idx) {
4849  Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
4850  unsigned NumTPLists = Record[Idx++];
4851  Info.NumTemplParamLists = NumTPLists;
4852  if (NumTPLists) {
4853    Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4854    for (unsigned i=0; i != NumTPLists; ++i)
4855      Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4856  }
4857}
4858
4859TemplateName
4860ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
4861                            unsigned &Idx) {
4862  TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
4863  switch (Kind) {
4864  case TemplateName::Template:
4865      return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
4866
4867  case TemplateName::OverloadedTemplate: {
4868    unsigned size = Record[Idx++];
4869    UnresolvedSet<8> Decls;
4870    while (size--)
4871      Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
4872
4873    return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4874  }
4875
4876  case TemplateName::QualifiedTemplate: {
4877    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
4878    bool hasTemplKeyword = Record[Idx++];
4879    TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
4880    return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4881  }
4882
4883  case TemplateName::DependentTemplate: {
4884    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
4885    if (Record[Idx++])  // isIdentifier
4886      return Context->getDependentTemplateName(NNS,
4887                                               GetIdentifierInfo(Record, Idx));
4888    return Context->getDependentTemplateName(NNS,
4889                                         (OverloadedOperatorKind)Record[Idx++]);
4890  }
4891
4892  case TemplateName::SubstTemplateTemplateParm: {
4893    TemplateTemplateParmDecl *param
4894      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
4895    if (!param) return TemplateName();
4896    TemplateName replacement = ReadTemplateName(F, Record, Idx);
4897    return Context->getSubstTemplateTemplateParm(param, replacement);
4898  }
4899
4900  case TemplateName::SubstTemplateTemplateParmPack: {
4901    TemplateTemplateParmDecl *Param
4902      = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
4903    if (!Param)
4904      return TemplateName();
4905
4906    TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4907    if (ArgPack.getKind() != TemplateArgument::Pack)
4908      return TemplateName();
4909
4910    return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4911  }
4912  }
4913
4914  assert(0 && "Unhandled template name kind!");
4915  return TemplateName();
4916}
4917
4918TemplateArgument
4919ASTReader::ReadTemplateArgument(Module &F,
4920                                const RecordData &Record, unsigned &Idx) {
4921  TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4922  switch (Kind) {
4923  case TemplateArgument::Null:
4924    return TemplateArgument();
4925  case TemplateArgument::Type:
4926    return TemplateArgument(readType(F, Record, Idx));
4927  case TemplateArgument::Declaration:
4928    return TemplateArgument(ReadDecl(F, Record, Idx));
4929  case TemplateArgument::Integral: {
4930    llvm::APSInt Value = ReadAPSInt(Record, Idx);
4931    QualType T = readType(F, Record, Idx);
4932    return TemplateArgument(Value, T);
4933  }
4934  case TemplateArgument::Template:
4935    return TemplateArgument(ReadTemplateName(F, Record, Idx));
4936  case TemplateArgument::TemplateExpansion: {
4937    TemplateName Name = ReadTemplateName(F, Record, Idx);
4938    llvm::Optional<unsigned> NumTemplateExpansions;
4939    if (unsigned NumExpansions = Record[Idx++])
4940      NumTemplateExpansions = NumExpansions - 1;
4941    return TemplateArgument(Name, NumTemplateExpansions);
4942  }
4943  case TemplateArgument::Expression:
4944    return TemplateArgument(ReadExpr(F));
4945  case TemplateArgument::Pack: {
4946    unsigned NumArgs = Record[Idx++];
4947    TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4948    for (unsigned I = 0; I != NumArgs; ++I)
4949      Args[I] = ReadTemplateArgument(F, Record, Idx);
4950    return TemplateArgument(Args, NumArgs);
4951  }
4952  }
4953
4954  assert(0 && "Unhandled template argument kind!");
4955  return TemplateArgument();
4956}
4957
4958TemplateParameterList *
4959ASTReader::ReadTemplateParameterList(Module &F,
4960                                     const RecordData &Record, unsigned &Idx) {
4961  SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4962  SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4963  SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
4964
4965  unsigned NumParams = Record[Idx++];
4966  SmallVector<NamedDecl *, 16> Params;
4967  Params.reserve(NumParams);
4968  while (NumParams--)
4969    Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
4970
4971  TemplateParameterList* TemplateParams =
4972    TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4973                                  Params.data(), Params.size(), RAngleLoc);
4974  return TemplateParams;
4975}
4976
4977void
4978ASTReader::
4979ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
4980                         Module &F, const RecordData &Record,
4981                         unsigned &Idx) {
4982  unsigned NumTemplateArgs = Record[Idx++];
4983  TemplArgs.reserve(NumTemplateArgs);
4984  while (NumTemplateArgs--)
4985    TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
4986}
4987
4988/// \brief Read a UnresolvedSet structure.
4989void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
4990                                  const RecordData &Record, unsigned &Idx) {
4991  unsigned NumDecls = Record[Idx++];
4992  while (NumDecls--) {
4993    NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
4994    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4995    Set.addDecl(D, AS);
4996  }
4997}
4998
4999CXXBaseSpecifier
5000ASTReader::ReadCXXBaseSpecifier(Module &F,
5001                                const RecordData &Record, unsigned &Idx) {
5002  bool isVirtual = static_cast<bool>(Record[Idx++]);
5003  bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5004  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
5005  bool inheritConstructors = static_cast<bool>(Record[Idx++]);
5006  TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5007  SourceRange Range = ReadSourceRange(F, Record, Idx);
5008  SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
5009  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
5010                          EllipsisLoc);
5011  Result.setInheritConstructors(inheritConstructors);
5012  return Result;
5013}
5014
5015std::pair<CXXCtorInitializer **, unsigned>
5016ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
5017                                   unsigned &Idx) {
5018  CXXCtorInitializer **CtorInitializers = 0;
5019  unsigned NumInitializers = Record[Idx++];
5020  if (NumInitializers) {
5021    ASTContext &C = *getContext();
5022
5023    CtorInitializers
5024        = new (C) CXXCtorInitializer*[NumInitializers];
5025    for (unsigned i=0; i != NumInitializers; ++i) {
5026      TypeSourceInfo *BaseClassInfo = 0;
5027      bool IsBaseVirtual = false;
5028      FieldDecl *Member = 0;
5029      IndirectFieldDecl *IndirectMember = 0;
5030      CXXConstructorDecl *Target = 0;
5031
5032      CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5033      switch (Type) {
5034       case CTOR_INITIALIZER_BASE:
5035        BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
5036        IsBaseVirtual = Record[Idx++];
5037        break;
5038
5039       case CTOR_INITIALIZER_DELEGATING:
5040        Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
5041        break;
5042
5043       case CTOR_INITIALIZER_MEMBER:
5044        Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
5045        break;
5046
5047       case CTOR_INITIALIZER_INDIRECT_MEMBER:
5048        IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
5049        break;
5050      }
5051
5052      SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
5053      Expr *Init = ReadExpr(F);
5054      SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5055      SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
5056      bool IsWritten = Record[Idx++];
5057      unsigned SourceOrderOrNumArrayIndices;
5058      SmallVector<VarDecl *, 8> Indices;
5059      if (IsWritten) {
5060        SourceOrderOrNumArrayIndices = Record[Idx++];
5061      } else {
5062        SourceOrderOrNumArrayIndices = Record[Idx++];
5063        Indices.reserve(SourceOrderOrNumArrayIndices);
5064        for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
5065          Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
5066      }
5067
5068      CXXCtorInitializer *BOMInit;
5069      if (Type == CTOR_INITIALIZER_BASE) {
5070        BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5071                                             LParenLoc, Init, RParenLoc,
5072                                             MemberOrEllipsisLoc);
5073      } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5074        BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5075                                             Target, Init, RParenLoc);
5076      } else if (IsWritten) {
5077        if (Member)
5078          BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5079                                               LParenLoc, Init, RParenLoc);
5080        else
5081          BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5082                                               MemberOrEllipsisLoc, LParenLoc,
5083                                               Init, RParenLoc);
5084      } else {
5085        BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5086                                             LParenLoc, Init, RParenLoc,
5087                                             Indices.data(), Indices.size());
5088      }
5089
5090      if (IsWritten)
5091        BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
5092      CtorInitializers[i] = BOMInit;
5093    }
5094  }
5095
5096  return std::make_pair(CtorInitializers, NumInitializers);
5097}
5098
5099NestedNameSpecifier *
5100ASTReader::ReadNestedNameSpecifier(Module &F,
5101                                   const RecordData &Record, unsigned &Idx) {
5102  unsigned N = Record[Idx++];
5103  NestedNameSpecifier *NNS = 0, *Prev = 0;
5104  for (unsigned I = 0; I != N; ++I) {
5105    NestedNameSpecifier::SpecifierKind Kind
5106      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5107    switch (Kind) {
5108    case NestedNameSpecifier::Identifier: {
5109      IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
5110      NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5111      break;
5112    }
5113
5114    case NestedNameSpecifier::Namespace: {
5115      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5116      NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5117      break;
5118    }
5119
5120    case NestedNameSpecifier::NamespaceAlias: {
5121      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5122      NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5123      break;
5124    }
5125
5126    case NestedNameSpecifier::TypeSpec:
5127    case NestedNameSpecifier::TypeSpecWithTemplate: {
5128      const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
5129      if (!T)
5130        return 0;
5131
5132      bool Template = Record[Idx++];
5133      NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5134      break;
5135    }
5136
5137    case NestedNameSpecifier::Global: {
5138      NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5139      // No associated value, and there can't be a prefix.
5140      break;
5141    }
5142    }
5143    Prev = NNS;
5144  }
5145  return NNS;
5146}
5147
5148NestedNameSpecifierLoc
5149ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
5150                                      unsigned &Idx) {
5151  unsigned N = Record[Idx++];
5152  NestedNameSpecifierLocBuilder Builder;
5153  for (unsigned I = 0; I != N; ++I) {
5154    NestedNameSpecifier::SpecifierKind Kind
5155      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5156    switch (Kind) {
5157    case NestedNameSpecifier::Identifier: {
5158      IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
5159      SourceRange Range = ReadSourceRange(F, Record, Idx);
5160      Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
5161      break;
5162    }
5163
5164    case NestedNameSpecifier::Namespace: {
5165      NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
5166      SourceRange Range = ReadSourceRange(F, Record, Idx);
5167      Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
5168      break;
5169    }
5170
5171    case NestedNameSpecifier::NamespaceAlias: {
5172      NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
5173      SourceRange Range = ReadSourceRange(F, Record, Idx);
5174      Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
5175      break;
5176    }
5177
5178    case NestedNameSpecifier::TypeSpec:
5179    case NestedNameSpecifier::TypeSpecWithTemplate: {
5180      bool Template = Record[Idx++];
5181      TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5182      if (!T)
5183        return NestedNameSpecifierLoc();
5184      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5185
5186      // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5187      Builder.Extend(*Context,
5188                     Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5189                     T->getTypeLoc(), ColonColonLoc);
5190      break;
5191    }
5192
5193    case NestedNameSpecifier::Global: {
5194      SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5195      Builder.MakeGlobal(*Context, ColonColonLoc);
5196      break;
5197    }
5198    }
5199  }
5200
5201  return Builder.getWithLocInContext(*Context);
5202}
5203
5204SourceRange
5205ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
5206                           unsigned &Idx) {
5207  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5208  SourceLocation end = ReadSourceLocation(F, Record, Idx);
5209  return SourceRange(beg, end);
5210}
5211
5212/// \brief Read an integral value
5213llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
5214  unsigned BitWidth = Record[Idx++];
5215  unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5216  llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5217  Idx += NumWords;
5218  return Result;
5219}
5220
5221/// \brief Read a signed integral value
5222llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
5223  bool isUnsigned = Record[Idx++];
5224  return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5225}
5226
5227/// \brief Read a floating-point value
5228llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
5229  return llvm::APFloat(ReadAPInt(Record, Idx));
5230}
5231
5232// \brief Read a string
5233std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
5234  unsigned Len = Record[Idx++];
5235  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
5236  Idx += Len;
5237  return Result;
5238}
5239
5240VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5241                                         unsigned &Idx) {
5242  unsigned Major = Record[Idx++];
5243  unsigned Minor = Record[Idx++];
5244  unsigned Subminor = Record[Idx++];
5245  if (Minor == 0)
5246    return VersionTuple(Major);
5247  if (Subminor == 0)
5248    return VersionTuple(Major, Minor - 1);
5249  return VersionTuple(Major, Minor - 1, Subminor - 1);
5250}
5251
5252CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
5253                                          const RecordData &Record,
5254                                          unsigned &Idx) {
5255  CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
5256  return CXXTemporary::Create(*Context, Decl);
5257}
5258
5259DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
5260  return Diag(SourceLocation(), DiagID);
5261}
5262
5263DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
5264  return Diags.Report(Loc, DiagID);
5265}
5266
5267/// \brief Retrieve the identifier table associated with the
5268/// preprocessor.
5269IdentifierTable &ASTReader::getIdentifierTable() {
5270  assert(PP && "Forgot to set Preprocessor ?");
5271  return PP->getIdentifierTable();
5272}
5273
5274/// \brief Record that the given ID maps to the given switch-case
5275/// statement.
5276void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
5277  assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5278  SwitchCaseStmts[ID] = SC;
5279}
5280
5281/// \brief Retrieve the switch-case statement with the given ID.
5282SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
5283  assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5284  return SwitchCaseStmts[ID];
5285}
5286
5287void ASTReader::ClearSwitchCaseIDs() {
5288  SwitchCaseStmts.clear();
5289}
5290
5291void ASTReader::FinishedDeserializing() {
5292  assert(NumCurrentElementsDeserializing &&
5293         "FinishedDeserializing not paired with StartedDeserializing");
5294  if (NumCurrentElementsDeserializing == 1) {
5295    // If any identifiers with corresponding top-level declarations have
5296    // been loaded, load those declarations now.
5297    while (!PendingIdentifierInfos.empty()) {
5298      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5299                              PendingIdentifierInfos.front().DeclIDs, true);
5300      PendingIdentifierInfos.pop_front();
5301    }
5302
5303    // Ready to load previous declarations of Decls that were delayed.
5304    while (!PendingPreviousDecls.empty()) {
5305      loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5306                                PendingPreviousDecls.front().second);
5307      PendingPreviousDecls.pop_front();
5308    }
5309
5310    // We are not in recursive loading, so it's safe to pass the "interesting"
5311    // decls to the consumer.
5312    if (Consumer)
5313      PassInterestingDeclsToConsumer();
5314
5315    assert(PendingForwardRefs.size() == 0 &&
5316           "Some forward refs did not get linked to the definition!");
5317  }
5318  --NumCurrentElementsDeserializing;
5319}
5320
5321ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
5322                     StringRef isysroot, bool DisableValidation,
5323                     bool DisableStatCache)
5324  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5325    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5326    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5327    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5328    RelocatablePCH(false), isysroot(isysroot),
5329    DisableValidation(DisableValidation),
5330    DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5331    NumSLocEntriesRead(0), TotalNumSLocEntries(0),
5332    NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5333    TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5334    NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5335    NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5336    NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5337    TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5338    NumCXXBaseSpecifiersLoaded(0)
5339{
5340  SourceMgr.setExternalSLocEntrySource(this);
5341}
5342
5343ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5344                     Diagnostic &Diags, StringRef isysroot,
5345                     bool DisableValidation, bool DisableStatCache)
5346  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5347    Diags(Diags), SemaObj(0), PP(0), Context(0),
5348    Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5349    RelocatablePCH(false), isysroot(isysroot),
5350    DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5351    NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5352    TotalNumSLocEntries(0), NumStatementsRead(0),
5353    TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5354    NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5355    TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5356    TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5357    TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5358    NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
5359{
5360  SourceMgr.setExternalSLocEntrySource(this);
5361}
5362
5363ASTReader::~ASTReader() {
5364  // Delete all visible decl lookup tables
5365  for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5366                                       E = DeclContextOffsets.end();
5367       I != E; ++I) {
5368    for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5369         J != F; ++J) {
5370      if (J->NameLookupTableData)
5371        delete static_cast<ASTDeclContextNameLookupTable*>(
5372            J->NameLookupTableData);
5373    }
5374  }
5375  for (DeclContextVisibleUpdatesPending::iterator
5376           I = PendingVisibleUpdates.begin(),
5377           E = PendingVisibleUpdates.end();
5378       I != E; ++I) {
5379    for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5380                                             F = I->second.end();
5381         J != F; ++J)
5382      delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5383  }
5384}
5385
5386Module::Module(ModuleKind Kind)
5387  : Kind(Kind), SizeInBits(0), LocalNumSLocEntries(0), SLocEntryBaseID(0),
5388    SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5389    SLocFileOffsets(0), LocalNumIdentifiers(0),
5390    IdentifierOffsets(0), IdentifierTableData(0),
5391    IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
5392    MacroDefinitionOffsets(0), LocalNumHeaderFileInfos(0),
5393    HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
5394    HeaderFileFrameworkStrings(0),
5395    LocalNumSelectors(0), SelectorOffsets(0),
5396    SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
5397    DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5398    LocalNumTypes(0), TypeOffsets(0), StatCache(0),
5399    NumPreallocatedPreprocessingEntities(0), NextInSource(0)
5400{}
5401
5402Module::~Module() {
5403  delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
5404  delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
5405  delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5406}
5407
5408Module *ModuleManager::lookup(StringRef Name) {
5409  const FileEntry *Entry = FileMgr.getFile(Name);
5410  return Modules[Entry];
5411}
5412
5413/// \brief Creates a new module and adds it to the list of known modules
5414Module &ModuleManager::addModule(StringRef FileName, ModuleKind Type) {
5415  Module *Prev = !size() ? 0 : &getLastModule();
5416  Module *Current = new Module(Type);
5417
5418  Current->FileName = FileName.str();
5419
5420  Chain.push_back(Current);
5421  const FileEntry *Entry = FileMgr.getFile(FileName);
5422  Modules[Entry] = Current;
5423
5424  if (Prev)
5425    Prev->NextInSource = Current;
5426  Current->Loaders.push_back(Prev);
5427
5428  return *Current;
5429}
5430
5431/// \brief Exports the list of loaded modules with their corresponding names
5432void ModuleManager::exportLookup(SmallVector<ModuleOffset, 16> &Target) {
5433  Target.reserve(size());
5434  for (ModuleConstIterator I = Chain.begin(), E = Chain.end();
5435       I != E; ++I) {
5436    Target.push_back(ModuleOffset((*I)->SLocEntryBaseOffset,
5437                                  (*I)->FileName));
5438  }
5439  std::sort(Target.begin(), Target.end());
5440}
5441
5442ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
5443
5444ModuleManager::~ModuleManager() {
5445  for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5446    delete Chain[e - i - 1];
5447}
5448