ASTReader.cpp revision 8538e8d43a3a9bd439c987c0de37bcbf035dd391
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 "clang/Frontend/FrontendDiagnostic.h"
17#include "clang/Frontend/Utils.h"
18#include "clang/Sema/Sema.h"
19#include "clang/AST/ASTConsumer.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/Type.h"
23#include "clang/AST/TypeLocVisitor.h"
24#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/PreprocessingRecord.h"
26#include "clang/Lex/Preprocessor.h"
27#include "clang/Lex/HeaderSearch.h"
28#include "clang/Basic/OnDiskHashTable.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/SourceManagerInternals.h"
31#include "clang/Basic/FileManager.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Basic/Version.h"
34#include "llvm/ADT/StringExtras.h"
35#include "llvm/Bitcode/BitstreamReader.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/System/Path.h"
39#include <algorithm>
40#include <iterator>
41#include <cstdio>
42#include <sys/stat.h>
43using namespace clang;
44using namespace clang::serialization;
45
46//===----------------------------------------------------------------------===//
47// PCH validator implementation
48//===----------------------------------------------------------------------===//
49
50ASTReaderListener::~ASTReaderListener() {}
51
52bool
53PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
54  const LangOptions &PPLangOpts = PP.getLangOptions();
55#define PARSE_LANGOPT_BENIGN(Option)
56#define PARSE_LANGOPT_IMPORTANT(Option, DiagID)                    \
57  if (PPLangOpts.Option != LangOpts.Option) {                      \
58    Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option;   \
59    return true;                                                   \
60  }
61
62  PARSE_LANGOPT_BENIGN(Trigraphs);
63  PARSE_LANGOPT_BENIGN(BCPLComment);
64  PARSE_LANGOPT_BENIGN(DollarIdents);
65  PARSE_LANGOPT_BENIGN(AsmPreprocessor);
66  PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
67  PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
68  PARSE_LANGOPT_BENIGN(ImplicitInt);
69  PARSE_LANGOPT_BENIGN(Digraphs);
70  PARSE_LANGOPT_BENIGN(HexFloats);
71  PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
72  PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
73  PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
74  PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
75  PARSE_LANGOPT_BENIGN(CXXOperatorName);
76  PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
77  PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
78  PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
79  PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
80  PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
81                          diag::warn_pch_no_constant_cfstrings);
82  PARSE_LANGOPT_BENIGN(PascalStrings);
83  PARSE_LANGOPT_BENIGN(WritableStrings);
84  PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
85                          diag::warn_pch_lax_vector_conversions);
86  PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
87  PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
88  PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
89  PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
90  PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
91  PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
92  PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
93                          diag::warn_pch_thread_safe_statics);
94  PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
95  PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
96  PARSE_LANGOPT_BENIGN(EmitAllDecls);
97  PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
98  PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
99  PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
100                          diag::warn_pch_heinous_extensions);
101  // FIXME: Most of the options below are benign if the macro wasn't
102  // used. Unfortunately, this means that a PCH compiled without
103  // optimization can't be used with optimization turned on, even
104  // though the only thing that changes is whether __OPTIMIZE__ was
105  // defined... but if __OPTIMIZE__ never showed up in the header, it
106  // doesn't matter. We could consider making this some special kind
107  // of check.
108  PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
109  PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
110  PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
111  PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
112  PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
113  PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
114  PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
115  PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
116  PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
117  if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
118    Reader.Diag(diag::warn_pch_gc_mode)
119      << LangOpts.getGCMode() << PPLangOpts.getGCMode();
120    return true;
121  }
122  PARSE_LANGOPT_BENIGN(getVisibilityMode());
123  PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
124                          diag::warn_pch_stack_protector);
125  PARSE_LANGOPT_BENIGN(InstantiationDepth);
126  PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
127  PARSE_LANGOPT_BENIGN(CatchUndefined);
128  PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
129  PARSE_LANGOPT_BENIGN(SpellChecking);
130#undef PARSE_LANGOPT_IMPORTANT
131#undef PARSE_LANGOPT_BENIGN
132
133  return false;
134}
135
136bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
137  if (Triple == PP.getTargetInfo().getTriple().str())
138    return false;
139
140  Reader.Diag(diag::warn_pch_target_triple)
141    << Triple << PP.getTargetInfo().getTriple().str();
142  return true;
143}
144
145struct EmptyStringRef {
146  bool operator ()(llvm::StringRef r) const { return r.empty(); }
147};
148struct EmptyBlock {
149  bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
150};
151
152static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
153                                PCHPredefinesBlocks R) {
154  // First, sum up the lengths.
155  unsigned LL = 0, RL = 0;
156  for (unsigned I = 0, N = L.size(); I != N; ++I) {
157    LL += L[I].size();
158  }
159  for (unsigned I = 0, N = R.size(); I != N; ++I) {
160    RL += R[I].Data.size();
161  }
162  if (LL != RL)
163    return false;
164  if (LL == 0 && RL == 0)
165    return true;
166
167  // Kick out empty parts, they confuse the algorithm below.
168  L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
169  R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
170
171  // Do it the hard way. At this point, both vectors must be non-empty.
172  llvm::StringRef LR = L[0], RR = R[0].Data;
173  unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
174  (void) RN;
175  for (;;) {
176    // Compare the current pieces.
177    if (LR.size() == RR.size()) {
178      // If they're the same length, it's pretty easy.
179      if (LR != RR)
180        return false;
181      // Both pieces are done, advance.
182      ++LI;
183      ++RI;
184      // If either string is done, they're both done, since they're the same
185      // length.
186      if (LI == LN) {
187        assert(RI == RN && "Strings not the same length after all?");
188        return true;
189      }
190      LR = L[LI];
191      RR = R[RI].Data;
192    } else if (LR.size() < RR.size()) {
193      // Right piece is longer.
194      if (!RR.startswith(LR))
195        return false;
196      ++LI;
197      assert(LI != LN && "Strings not the same length after all?");
198      RR = RR.substr(LR.size());
199      LR = L[LI];
200    } else {
201      // Left piece is longer.
202      if (!LR.startswith(RR))
203        return false;
204      ++RI;
205      assert(RI != RN && "Strings not the same length after all?");
206      LR = LR.substr(RR.size());
207      RR = R[RI].Data;
208    }
209  }
210}
211
212static std::pair<FileID, llvm::StringRef::size_type>
213FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
214  std::pair<FileID, llvm::StringRef::size_type> Res;
215  for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
216    Res.second = Buffers[I].Data.find(MacroDef);
217    if (Res.second != llvm::StringRef::npos) {
218      Res.first = Buffers[I].BufferID;
219      break;
220    }
221  }
222  return Res;
223}
224
225bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
226                                        llvm::StringRef OriginalFileName,
227                                        std::string &SuggestedPredefines) {
228  // We are in the context of an implicit include, so the predefines buffer will
229  // have a #include entry for the PCH file itself (as normalized by the
230  // preprocessor initialization). Find it and skip over it in the checking
231  // below.
232  llvm::SmallString<256> PCHInclude;
233  PCHInclude += "#include \"";
234  PCHInclude += NormalizeDashIncludePath(OriginalFileName);
235  PCHInclude += "\"\n";
236  std::pair<llvm::StringRef,llvm::StringRef> Split =
237    llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
238  llvm::StringRef Left =  Split.first, Right = Split.second;
239  if (Left == PP.getPredefines()) {
240    Error("Missing PCH include entry!");
241    return true;
242  }
243
244  // If the concatenation of all the PCH buffers is equal to the adjusted
245  // command line, we're done.
246  llvm::SmallVector<llvm::StringRef, 2> CommandLine;
247  CommandLine.push_back(Left);
248  CommandLine.push_back(Right);
249  if (EqualConcatenations(CommandLine, Buffers))
250    return false;
251
252  SourceManager &SourceMgr = PP.getSourceManager();
253
254  // The predefines buffers are different. Determine what the differences are,
255  // and whether they require us to reject the PCH file.
256  llvm::SmallVector<llvm::StringRef, 8> PCHLines;
257  for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
258    Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
259
260  llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
261  Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
262  Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
263
264  // Sort both sets of predefined buffer lines, since we allow some extra
265  // definitions and they may appear at any point in the output.
266  std::sort(CmdLineLines.begin(), CmdLineLines.end());
267  std::sort(PCHLines.begin(), PCHLines.end());
268
269  // Determine which predefines that were used to build the PCH file are missing
270  // from the command line.
271  std::vector<llvm::StringRef> MissingPredefines;
272  std::set_difference(PCHLines.begin(), PCHLines.end(),
273                      CmdLineLines.begin(), CmdLineLines.end(),
274                      std::back_inserter(MissingPredefines));
275
276  bool MissingDefines = false;
277  bool ConflictingDefines = false;
278  for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
279    llvm::StringRef Missing = MissingPredefines[I];
280    if (!Missing.startswith("#define ")) {
281      Reader.Diag(diag::warn_pch_compiler_options_mismatch);
282      return true;
283    }
284
285    // This is a macro definition. Determine the name of the macro we're
286    // defining.
287    std::string::size_type StartOfMacroName = strlen("#define ");
288    std::string::size_type EndOfMacroName
289      = Missing.find_first_of("( \n\r", StartOfMacroName);
290    assert(EndOfMacroName != std::string::npos &&
291           "Couldn't find the end of the macro name");
292    llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
293
294    // Determine whether this macro was given a different definition on the
295    // command line.
296    std::string MacroDefStart = "#define " + MacroName.str();
297    std::string::size_type MacroDefLen = MacroDefStart.size();
298    llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
299      = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
300                         MacroDefStart);
301    for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
302      if (!ConflictPos->startswith(MacroDefStart)) {
303        // Different macro; we're done.
304        ConflictPos = CmdLineLines.end();
305        break;
306      }
307
308      assert(ConflictPos->size() > MacroDefLen &&
309             "Invalid #define in predefines buffer?");
310      if ((*ConflictPos)[MacroDefLen] != ' ' &&
311          (*ConflictPos)[MacroDefLen] != '(')
312        continue; // Longer macro name; keep trying.
313
314      // We found a conflicting macro definition.
315      break;
316    }
317
318    if (ConflictPos != CmdLineLines.end()) {
319      Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
320          << MacroName;
321
322      // Show the definition of this macro within the PCH file.
323      std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
324          FindMacro(Buffers, Missing);
325      assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
326      SourceLocation PCHMissingLoc =
327          SourceMgr.getLocForStartOfFile(MacroLoc.first)
328            .getFileLocWithOffset(MacroLoc.second);
329      Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
330
331      ConflictingDefines = true;
332      continue;
333    }
334
335    // If the macro doesn't conflict, then we'll just pick up the macro
336    // definition from the PCH file. Warn the user that they made a mistake.
337    if (ConflictingDefines)
338      continue; // Don't complain if there are already conflicting defs
339
340    if (!MissingDefines) {
341      Reader.Diag(diag::warn_cmdline_missing_macro_defs);
342      MissingDefines = true;
343    }
344
345    // Show the definition of this macro within the PCH file.
346    std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
347        FindMacro(Buffers, Missing);
348    assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
349    SourceLocation PCHMissingLoc =
350        SourceMgr.getLocForStartOfFile(MacroLoc.first)
351          .getFileLocWithOffset(MacroLoc.second);
352    Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
353  }
354
355  if (ConflictingDefines)
356    return true;
357
358  // Determine what predefines were introduced based on command-line
359  // parameters that were not present when building the PCH
360  // file. Extra #defines are okay, so long as the identifiers being
361  // defined were not used within the precompiled header.
362  std::vector<llvm::StringRef> ExtraPredefines;
363  std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
364                      PCHLines.begin(), PCHLines.end(),
365                      std::back_inserter(ExtraPredefines));
366  for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
367    llvm::StringRef &Extra = ExtraPredefines[I];
368    if (!Extra.startswith("#define ")) {
369      Reader.Diag(diag::warn_pch_compiler_options_mismatch);
370      return true;
371    }
372
373    // This is an extra macro definition. Determine the name of the
374    // macro we're defining.
375    std::string::size_type StartOfMacroName = strlen("#define ");
376    std::string::size_type EndOfMacroName
377      = Extra.find_first_of("( \n\r", StartOfMacroName);
378    assert(EndOfMacroName != std::string::npos &&
379           "Couldn't find the end of the macro name");
380    llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
381
382    // Check whether this name was used somewhere in the PCH file. If
383    // so, defining it as a macro could change behavior, so we reject
384    // the PCH file.
385    if (IdentifierInfo *II = Reader.get(MacroName)) {
386      Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
387      return true;
388    }
389
390    // Add this definition to the suggested predefines buffer.
391    SuggestedPredefines += Extra;
392    SuggestedPredefines += '\n';
393  }
394
395  // If we get here, it's because the predefines buffer had compatible
396  // contents. Accept the PCH file.
397  return false;
398}
399
400void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
401                                      unsigned ID) {
402  PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
403  ++NumHeaderInfos;
404}
405
406void PCHValidator::ReadCounter(unsigned Value) {
407  PP.setCounterValue(Value);
408}
409
410//===----------------------------------------------------------------------===//
411// AST reader implementation
412//===----------------------------------------------------------------------===//
413
414ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
415                     const char *isysroot, bool DisableValidation)
416  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
417    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
418    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
419    Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
420    NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
421    TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
422    NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
423    NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
424    TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
425    TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
426    TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
427  RelocatablePCH = false;
428}
429
430ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
431                     Diagnostic &Diags, const char *isysroot,
432                     bool DisableValidation)
433  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
434    Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
435    isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
436    NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
437    NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
438    TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
439    NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
440    NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
441    NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
442    NumCurrentElementsDeserializing(0) {
443  RelocatablePCH = false;
444}
445
446ASTReader::~ASTReader() {
447  for (unsigned i = 0, e = Chain.size(); i != e; ++i)
448    delete Chain[e - i - 1];
449}
450
451ASTReader::PerFileData::PerFileData()
452  : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
453    LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
454    IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
455    LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
456    NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
457    SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
458{}
459
460void
461ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
462  DeserializationListener = Listener;
463  if (DeserializationListener)
464    DeserializationListener->SetReader(this);
465}
466
467
468namespace {
469class ASTSelectorLookupTrait {
470  ASTReader &Reader;
471
472public:
473  struct data_type {
474    SelectorID ID;
475    ObjCMethodList Instance, Factory;
476  };
477
478  typedef Selector external_key_type;
479  typedef external_key_type internal_key_type;
480
481  explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
482
483  static bool EqualKey(const internal_key_type& a,
484                       const internal_key_type& b) {
485    return a == b;
486  }
487
488  static unsigned ComputeHash(Selector Sel) {
489    unsigned N = Sel.getNumArgs();
490    if (N == 0)
491      ++N;
492    unsigned R = 5381;
493    for (unsigned I = 0; I != N; ++I)
494      if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
495        R = llvm::HashString(II->getName(), R);
496    return R;
497  }
498
499  // This hopefully will just get inlined and removed by the optimizer.
500  static const internal_key_type&
501  GetInternalKey(const external_key_type& x) { return x; }
502
503  static std::pair<unsigned, unsigned>
504  ReadKeyDataLength(const unsigned char*& d) {
505    using namespace clang::io;
506    unsigned KeyLen = ReadUnalignedLE16(d);
507    unsigned DataLen = ReadUnalignedLE16(d);
508    return std::make_pair(KeyLen, DataLen);
509  }
510
511  internal_key_type ReadKey(const unsigned char* d, unsigned) {
512    using namespace clang::io;
513    SelectorTable &SelTable = Reader.getContext()->Selectors;
514    unsigned N = ReadUnalignedLE16(d);
515    IdentifierInfo *FirstII
516      = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
517    if (N == 0)
518      return SelTable.getNullarySelector(FirstII);
519    else if (N == 1)
520      return SelTable.getUnarySelector(FirstII);
521
522    llvm::SmallVector<IdentifierInfo *, 16> Args;
523    Args.push_back(FirstII);
524    for (unsigned I = 1; I != N; ++I)
525      Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
526
527    return SelTable.getSelector(N, Args.data());
528  }
529
530  data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
531    using namespace clang::io;
532
533    data_type Result;
534
535    Result.ID = ReadUnalignedLE32(d);
536    unsigned NumInstanceMethods = ReadUnalignedLE16(d);
537    unsigned NumFactoryMethods = ReadUnalignedLE16(d);
538
539    // Load instance methods
540    ObjCMethodList *Prev = 0;
541    for (unsigned I = 0; I != NumInstanceMethods; ++I) {
542      ObjCMethodDecl *Method
543        = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
544      if (!Result.Instance.Method) {
545        // This is the first method, which is the easy case.
546        Result.Instance.Method = Method;
547        Prev = &Result.Instance;
548        continue;
549      }
550
551      ObjCMethodList *Mem =
552        Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
553      Prev->Next = new (Mem) ObjCMethodList(Method, 0);
554      Prev = Prev->Next;
555    }
556
557    // Load factory methods
558    Prev = 0;
559    for (unsigned I = 0; I != NumFactoryMethods; ++I) {
560      ObjCMethodDecl *Method
561        = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
562      if (!Result.Factory.Method) {
563        // This is the first method, which is the easy case.
564        Result.Factory.Method = Method;
565        Prev = &Result.Factory;
566        continue;
567      }
568
569      ObjCMethodList *Mem =
570        Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
571      Prev->Next = new (Mem) ObjCMethodList(Method, 0);
572      Prev = Prev->Next;
573    }
574
575    return Result;
576  }
577};
578
579} // end anonymous namespace
580
581/// \brief The on-disk hash table used for the global method pool.
582typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
583  ASTSelectorLookupTable;
584
585namespace {
586class ASTIdentifierLookupTrait {
587  ASTReader &Reader;
588  llvm::BitstreamCursor &Stream;
589
590  // If we know the IdentifierInfo in advance, it is here and we will
591  // not build a new one. Used when deserializing information about an
592  // identifier that was constructed before the AST file was read.
593  IdentifierInfo *KnownII;
594
595public:
596  typedef IdentifierInfo * data_type;
597
598  typedef const std::pair<const char*, unsigned> external_key_type;
599
600  typedef external_key_type internal_key_type;
601
602  ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
603                           IdentifierInfo *II = 0)
604    : Reader(Reader), Stream(Stream), KnownII(II) { }
605
606  static bool EqualKey(const internal_key_type& a,
607                       const internal_key_type& b) {
608    return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
609                                  : false;
610  }
611
612  static unsigned ComputeHash(const internal_key_type& a) {
613    return llvm::HashString(llvm::StringRef(a.first, a.second));
614  }
615
616  // This hopefully will just get inlined and removed by the optimizer.
617  static const internal_key_type&
618  GetInternalKey(const external_key_type& x) { return x; }
619
620  static std::pair<unsigned, unsigned>
621  ReadKeyDataLength(const unsigned char*& d) {
622    using namespace clang::io;
623    unsigned DataLen = ReadUnalignedLE16(d);
624    unsigned KeyLen = ReadUnalignedLE16(d);
625    return std::make_pair(KeyLen, DataLen);
626  }
627
628  static std::pair<const char*, unsigned>
629  ReadKey(const unsigned char* d, unsigned n) {
630    assert(n >= 2 && d[n-1] == '\0');
631    return std::make_pair((const char*) d, n-1);
632  }
633
634  IdentifierInfo *ReadData(const internal_key_type& k,
635                           const unsigned char* d,
636                           unsigned DataLen) {
637    using namespace clang::io;
638    IdentID ID = ReadUnalignedLE32(d);
639    bool IsInteresting = ID & 0x01;
640
641    // Wipe out the "is interesting" bit.
642    ID = ID >> 1;
643
644    if (!IsInteresting) {
645      // For uninteresting identifiers, just build the IdentifierInfo
646      // and associate it with the persistent ID.
647      IdentifierInfo *II = KnownII;
648      if (!II)
649        II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
650      Reader.SetIdentifierInfo(ID, II);
651      II->setIsFromAST();
652      return II;
653    }
654
655    unsigned Bits = ReadUnalignedLE16(d);
656    bool CPlusPlusOperatorKeyword = Bits & 0x01;
657    Bits >>= 1;
658    bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
659    Bits >>= 1;
660    bool Poisoned = Bits & 0x01;
661    Bits >>= 1;
662    bool ExtensionToken = Bits & 0x01;
663    Bits >>= 1;
664    bool hasMacroDefinition = Bits & 0x01;
665    Bits >>= 1;
666    unsigned ObjCOrBuiltinID = Bits & 0x3FF;
667    Bits >>= 10;
668
669    assert(Bits == 0 && "Extra bits in the identifier?");
670    DataLen -= 6;
671
672    // Build the IdentifierInfo itself and link the identifier ID with
673    // the new IdentifierInfo.
674    IdentifierInfo *II = KnownII;
675    if (!II)
676      II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
677    Reader.SetIdentifierInfo(ID, II);
678
679    // Set or check the various bits in the IdentifierInfo structure.
680    // Token IDs are read-only.
681    if (HasRevertedTokenIDToIdentifier)
682      II->RevertTokenIDToIdentifier();
683    II->setObjCOrBuiltinID(ObjCOrBuiltinID);
684    assert(II->isExtensionToken() == ExtensionToken &&
685           "Incorrect extension token flag");
686    (void)ExtensionToken;
687    II->setIsPoisoned(Poisoned);
688    assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
689           "Incorrect C++ operator keyword flag");
690    (void)CPlusPlusOperatorKeyword;
691
692    // If this identifier is a macro, deserialize the macro
693    // definition.
694    if (hasMacroDefinition) {
695      uint32_t Offset = ReadUnalignedLE32(d);
696      Reader.ReadMacroRecord(Stream, Offset);
697      DataLen -= 4;
698    }
699
700    // Read all of the declarations visible at global scope with this
701    // name.
702    if (Reader.getContext() == 0) return II;
703    if (DataLen > 0) {
704      llvm::SmallVector<uint32_t, 4> DeclIDs;
705      for (; DataLen > 0; DataLen -= 4)
706        DeclIDs.push_back(ReadUnalignedLE32(d));
707      Reader.SetGloballyVisibleDecls(II, DeclIDs);
708    }
709
710    II->setIsFromAST();
711    return II;
712  }
713};
714
715} // end anonymous namespace
716
717/// \brief The on-disk hash table used to contain information about
718/// all of the identifiers in the program.
719typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
720  ASTIdentifierLookupTable;
721
722void ASTReader::Error(const char *Msg) {
723  Diag(diag::err_fe_pch_malformed) << Msg;
724}
725
726/// \brief Tell the AST listener about the predefines buffers in the chain.
727bool ASTReader::CheckPredefinesBuffers() {
728  if (Listener)
729    return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
730                                          ActualOriginalFileName,
731                                          SuggestedPredefines);
732  return false;
733}
734
735//===----------------------------------------------------------------------===//
736// Source Manager Deserialization
737//===----------------------------------------------------------------------===//
738
739/// \brief Read the line table in the source manager block.
740/// \returns true if ther was an error.
741bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
742  unsigned Idx = 0;
743  LineTableInfo &LineTable = SourceMgr.getLineTable();
744
745  // Parse the file names
746  std::map<int, int> FileIDs;
747  for (int I = 0, N = Record[Idx++]; I != N; ++I) {
748    // Extract the file name
749    unsigned FilenameLen = Record[Idx++];
750    std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
751    Idx += FilenameLen;
752    MaybeAddSystemRootToFilename(Filename);
753    FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
754                                                  Filename.size());
755  }
756
757  // Parse the line entries
758  std::vector<LineEntry> Entries;
759  while (Idx < Record.size()) {
760    int FID = Record[Idx++];
761
762    // Extract the line entries
763    unsigned NumEntries = Record[Idx++];
764    assert(NumEntries && "Numentries is 00000");
765    Entries.clear();
766    Entries.reserve(NumEntries);
767    for (unsigned I = 0; I != NumEntries; ++I) {
768      unsigned FileOffset = Record[Idx++];
769      unsigned LineNo = Record[Idx++];
770      int FilenameID = FileIDs[Record[Idx++]];
771      SrcMgr::CharacteristicKind FileKind
772        = (SrcMgr::CharacteristicKind)Record[Idx++];
773      unsigned IncludeOffset = Record[Idx++];
774      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
775                                       FileKind, IncludeOffset));
776    }
777    LineTable.AddEntry(FID, Entries);
778  }
779
780  return false;
781}
782
783namespace {
784
785class ASTStatData {
786public:
787  const bool hasStat;
788  const ino_t ino;
789  const dev_t dev;
790  const mode_t mode;
791  const time_t mtime;
792  const off_t size;
793
794  ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
795  : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
796
797  ASTStatData()
798    : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
799};
800
801class ASTStatLookupTrait {
802 public:
803  typedef const char *external_key_type;
804  typedef const char *internal_key_type;
805
806  typedef ASTStatData data_type;
807
808  static unsigned ComputeHash(const char *path) {
809    return llvm::HashString(path);
810  }
811
812  static internal_key_type GetInternalKey(const char *path) { return path; }
813
814  static bool EqualKey(internal_key_type a, internal_key_type b) {
815    return strcmp(a, b) == 0;
816  }
817
818  static std::pair<unsigned, unsigned>
819  ReadKeyDataLength(const unsigned char*& d) {
820    unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
821    unsigned DataLen = (unsigned) *d++;
822    return std::make_pair(KeyLen + 1, DataLen);
823  }
824
825  static internal_key_type ReadKey(const unsigned char *d, unsigned) {
826    return (const char *)d;
827  }
828
829  static data_type ReadData(const internal_key_type, const unsigned char *d,
830                            unsigned /*DataLen*/) {
831    using namespace clang::io;
832
833    if (*d++ == 1)
834      return data_type();
835
836    ino_t ino = (ino_t) ReadUnalignedLE32(d);
837    dev_t dev = (dev_t) ReadUnalignedLE32(d);
838    mode_t mode = (mode_t) ReadUnalignedLE16(d);
839    time_t mtime = (time_t) ReadUnalignedLE64(d);
840    off_t size = (off_t) ReadUnalignedLE64(d);
841    return data_type(ino, dev, mode, mtime, size);
842  }
843};
844
845/// \brief stat() cache for precompiled headers.
846///
847/// This cache is very similar to the stat cache used by pretokenized
848/// headers.
849class ASTStatCache : public StatSysCallCache {
850  typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
851  CacheTy *Cache;
852
853  unsigned &NumStatHits, &NumStatMisses;
854public:
855  ASTStatCache(const unsigned char *Buckets,
856               const unsigned char *Base,
857               unsigned &NumStatHits,
858               unsigned &NumStatMisses)
859    : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
860    Cache = CacheTy::Create(Buckets, Base);
861  }
862
863  ~ASTStatCache() { delete Cache; }
864
865  int stat(const char *path, struct stat *buf) {
866    // Do the lookup for the file's data in the AST file.
867    CacheTy::iterator I = Cache->find(path);
868
869    // If we don't get a hit in the AST file just forward to 'stat'.
870    if (I == Cache->end()) {
871      ++NumStatMisses;
872      return StatSysCallCache::stat(path, buf);
873    }
874
875    ++NumStatHits;
876    ASTStatData Data = *I;
877
878    if (!Data.hasStat)
879      return 1;
880
881    buf->st_ino = Data.ino;
882    buf->st_dev = Data.dev;
883    buf->st_mtime = Data.mtime;
884    buf->st_mode = Data.mode;
885    buf->st_size = Data.size;
886    return 0;
887  }
888};
889} // end anonymous namespace
890
891
892/// \brief Read a source manager block
893ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
894  using namespace SrcMgr;
895
896  llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
897
898  // Set the source-location entry cursor to the current position in
899  // the stream. This cursor will be used to read the contents of the
900  // source manager block initially, and then lazily read
901  // source-location entries as needed.
902  SLocEntryCursor = F.Stream;
903
904  // The stream itself is going to skip over the source manager block.
905  if (F.Stream.SkipBlock()) {
906    Error("malformed block record in AST file");
907    return Failure;
908  }
909
910  // Enter the source manager block.
911  if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
912    Error("malformed source manager block record in AST file");
913    return Failure;
914  }
915
916  RecordData Record;
917  while (true) {
918    unsigned Code = SLocEntryCursor.ReadCode();
919    if (Code == llvm::bitc::END_BLOCK) {
920      if (SLocEntryCursor.ReadBlockEnd()) {
921        Error("error at end of Source Manager block in AST file");
922        return Failure;
923      }
924      return Success;
925    }
926
927    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
928      // No known subblocks, always skip them.
929      SLocEntryCursor.ReadSubBlockID();
930      if (SLocEntryCursor.SkipBlock()) {
931        Error("malformed block record in AST file");
932        return Failure;
933      }
934      continue;
935    }
936
937    if (Code == llvm::bitc::DEFINE_ABBREV) {
938      SLocEntryCursor.ReadAbbrevRecord();
939      continue;
940    }
941
942    // Read a record.
943    const char *BlobStart;
944    unsigned BlobLen;
945    Record.clear();
946    switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
947    default:  // Default behavior: ignore.
948      break;
949
950    case SM_LINE_TABLE:
951      if (ParseLineTable(Record))
952        return Failure;
953      break;
954
955    case SM_SLOC_FILE_ENTRY:
956    case SM_SLOC_BUFFER_ENTRY:
957    case SM_SLOC_INSTANTIATION_ENTRY:
958      // Once we hit one of the source location entries, we're done.
959      return Success;
960    }
961  }
962}
963
964/// \brief Get a cursor that's correctly positioned for reading the source
965/// location entry with the given ID.
966llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
967  assert(ID != 0 && ID <= TotalNumSLocEntries &&
968         "SLocCursorForID should only be called for real IDs.");
969
970  ID -= 1;
971  PerFileData *F = 0;
972  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
973    F = Chain[N - I - 1];
974    if (ID < F->LocalNumSLocEntries)
975      break;
976    ID -= F->LocalNumSLocEntries;
977  }
978  assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
979
980  F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
981  return F->SLocEntryCursor;
982}
983
984/// \brief Read in the source location entry with the given ID.
985ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
986  if (ID == 0)
987    return Success;
988
989  if (ID > TotalNumSLocEntries) {
990    Error("source location entry ID out-of-range for AST file");
991    return Failure;
992  }
993
994  llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
995
996  ++NumSLocEntriesRead;
997  unsigned Code = SLocEntryCursor.ReadCode();
998  if (Code == llvm::bitc::END_BLOCK ||
999      Code == llvm::bitc::ENTER_SUBBLOCK ||
1000      Code == llvm::bitc::DEFINE_ABBREV) {
1001    Error("incorrectly-formatted source location entry in AST file");
1002    return Failure;
1003  }
1004
1005  RecordData Record;
1006  const char *BlobStart;
1007  unsigned BlobLen;
1008  switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1009  default:
1010    Error("incorrectly-formatted source location entry in AST file");
1011    return Failure;
1012
1013  case SM_SLOC_FILE_ENTRY: {
1014    std::string Filename(BlobStart, BlobStart + BlobLen);
1015    MaybeAddSystemRootToFilename(Filename);
1016    const FileEntry *File = FileMgr.getFile(Filename);
1017    if (File == 0) {
1018      std::string ErrorStr = "could not find file '";
1019      ErrorStr += Filename;
1020      ErrorStr += "' referenced by AST file";
1021      Error(ErrorStr.c_str());
1022      return Failure;
1023    }
1024
1025    if (Record.size() < 10) {
1026      Error("source location entry is incorrect");
1027      return Failure;
1028    }
1029
1030    if (!DisableValidation &&
1031        ((off_t)Record[4] != File->getSize()
1032#if !defined(LLVM_ON_WIN32)
1033        // In our regression testing, the Windows file system seems to
1034        // have inconsistent modification times that sometimes
1035        // erroneously trigger this error-handling path.
1036         || (time_t)Record[5] != File->getModificationTime()
1037#endif
1038        )) {
1039      Diag(diag::err_fe_pch_file_modified)
1040        << Filename;
1041      return Failure;
1042    }
1043
1044    FileID FID = SourceMgr.createFileID(File,
1045                                SourceLocation::getFromRawEncoding(Record[1]),
1046                                       (SrcMgr::CharacteristicKind)Record[2],
1047                                        ID, Record[0]);
1048    if (Record[3])
1049      const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1050        .setHasLineDirectives();
1051
1052    // Reconstruct header-search information for this file.
1053    HeaderFileInfo HFI;
1054    HFI.isImport = Record[6];
1055    HFI.DirInfo = Record[7];
1056    HFI.NumIncludes = Record[8];
1057    HFI.ControllingMacroID = Record[9];
1058    if (Listener)
1059      Listener->ReadHeaderFileInfo(HFI, File->getUID());
1060    break;
1061  }
1062
1063  case SM_SLOC_BUFFER_ENTRY: {
1064    const char *Name = BlobStart;
1065    unsigned Offset = Record[0];
1066    unsigned Code = SLocEntryCursor.ReadCode();
1067    Record.clear();
1068    unsigned RecCode
1069      = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1070
1071    if (RecCode != SM_SLOC_BUFFER_BLOB) {
1072      Error("AST record has invalid code");
1073      return Failure;
1074    }
1075
1076    llvm::MemoryBuffer *Buffer
1077    = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1078                                       Name);
1079    FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
1080
1081    if (strcmp(Name, "<built-in>") == 0) {
1082      PCHPredefinesBlock Block = {
1083        BufferID,
1084        llvm::StringRef(BlobStart, BlobLen - 1)
1085      };
1086      PCHPredefinesBuffers.push_back(Block);
1087    }
1088
1089    break;
1090  }
1091
1092  case SM_SLOC_INSTANTIATION_ENTRY: {
1093    SourceLocation SpellingLoc
1094      = SourceLocation::getFromRawEncoding(Record[1]);
1095    SourceMgr.createInstantiationLoc(SpellingLoc,
1096                              SourceLocation::getFromRawEncoding(Record[2]),
1097                              SourceLocation::getFromRawEncoding(Record[3]),
1098                                     Record[4],
1099                                     ID,
1100                                     Record[0]);
1101    break;
1102  }
1103  }
1104
1105  return Success;
1106}
1107
1108/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1109/// specified cursor.  Read the abbreviations that are at the top of the block
1110/// and then leave the cursor pointing into the block.
1111bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1112                                 unsigned BlockID) {
1113  if (Cursor.EnterSubBlock(BlockID)) {
1114    Error("malformed block record in AST file");
1115    return Failure;
1116  }
1117
1118  while (true) {
1119    unsigned Code = Cursor.ReadCode();
1120
1121    // We expect all abbrevs to be at the start of the block.
1122    if (Code != llvm::bitc::DEFINE_ABBREV)
1123      return false;
1124    Cursor.ReadAbbrevRecord();
1125  }
1126}
1127
1128void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
1129  assert(PP && "Forgot to set Preprocessor ?");
1130
1131  // Keep track of where we are in the stream, then jump back there
1132  // after reading this macro.
1133  SavedStreamPosition SavedPosition(Stream);
1134
1135  Stream.JumpToBit(Offset);
1136  RecordData Record;
1137  llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1138  MacroInfo *Macro = 0;
1139
1140  while (true) {
1141    unsigned Code = Stream.ReadCode();
1142    switch (Code) {
1143    case llvm::bitc::END_BLOCK:
1144      return;
1145
1146    case llvm::bitc::ENTER_SUBBLOCK:
1147      // No known subblocks, always skip them.
1148      Stream.ReadSubBlockID();
1149      if (Stream.SkipBlock()) {
1150        Error("malformed block record in AST file");
1151        return;
1152      }
1153      continue;
1154
1155    case llvm::bitc::DEFINE_ABBREV:
1156      Stream.ReadAbbrevRecord();
1157      continue;
1158    default: break;
1159    }
1160
1161    // Read a record.
1162    Record.clear();
1163    PreprocessorRecordTypes RecType =
1164      (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1165    switch (RecType) {
1166    case PP_MACRO_OBJECT_LIKE:
1167    case PP_MACRO_FUNCTION_LIKE: {
1168      // If we already have a macro, that means that we've hit the end
1169      // of the definition of the macro we were looking for. We're
1170      // done.
1171      if (Macro)
1172        return;
1173
1174      IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1175      if (II == 0) {
1176        Error("macro must have a name in AST file");
1177        return;
1178      }
1179      SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1180      bool isUsed = Record[2];
1181
1182      MacroInfo *MI = PP->AllocateMacroInfo(Loc);
1183      MI->setIsUsed(isUsed);
1184      MI->setIsFromAST();
1185
1186      unsigned NextIndex = 3;
1187      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1188        // Decode function-like macro info.
1189        bool isC99VarArgs = Record[3];
1190        bool isGNUVarArgs = Record[4];
1191        MacroArgs.clear();
1192        unsigned NumArgs = Record[5];
1193        NextIndex = 6 + NumArgs;
1194        for (unsigned i = 0; i != NumArgs; ++i)
1195          MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1196
1197        // Install function-like macro info.
1198        MI->setIsFunctionLike();
1199        if (isC99VarArgs) MI->setIsC99Varargs();
1200        if (isGNUVarArgs) MI->setIsGNUVarargs();
1201        MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1202                            PP->getPreprocessorAllocator());
1203      }
1204
1205      // Finally, install the macro.
1206      PP->setMacroInfo(II, MI);
1207
1208      // Remember that we saw this macro last so that we add the tokens that
1209      // form its body to it.
1210      Macro = MI;
1211
1212      if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1213        // We have a macro definition. Load it now.
1214        PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1215                                        getMacroDefinition(Record[NextIndex]));
1216      }
1217
1218      ++NumMacrosRead;
1219      break;
1220    }
1221
1222    case PP_TOKEN: {
1223      // If we see a TOKEN before a PP_MACRO_*, then the file is
1224      // erroneous, just pretend we didn't see this.
1225      if (Macro == 0) break;
1226
1227      Token Tok;
1228      Tok.startToken();
1229      Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1230      Tok.setLength(Record[1]);
1231      if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1232        Tok.setIdentifierInfo(II);
1233      Tok.setKind((tok::TokenKind)Record[3]);
1234      Tok.setFlag((Token::TokenFlags)Record[4]);
1235      Macro->AddTokenToBody(Tok);
1236      break;
1237    }
1238
1239    case PP_MACRO_INSTANTIATION: {
1240      // If we already have a macro, that means that we've hit the end
1241      // of the definition of the macro we were looking for. We're
1242      // done.
1243      if (Macro)
1244        return;
1245
1246      if (!PP->getPreprocessingRecord()) {
1247        Error("missing preprocessing record in AST file");
1248        return;
1249      }
1250
1251      PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1252      if (PPRec.getPreprocessedEntity(Record[0]))
1253        return;
1254
1255      MacroInstantiation *MI
1256        = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1257                               SourceRange(
1258                                 SourceLocation::getFromRawEncoding(Record[1]),
1259                                 SourceLocation::getFromRawEncoding(Record[2])),
1260                                         getMacroDefinition(Record[4]));
1261      PPRec.SetPreallocatedEntity(Record[0], MI);
1262      return;
1263    }
1264
1265    case PP_MACRO_DEFINITION: {
1266      // If we already have a macro, that means that we've hit the end
1267      // of the definition of the macro we were looking for. We're
1268      // done.
1269      if (Macro)
1270        return;
1271
1272      if (!PP->getPreprocessingRecord()) {
1273        Error("missing preprocessing record in AST file");
1274        return;
1275      }
1276
1277      PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1278      if (PPRec.getPreprocessedEntity(Record[0]))
1279        return;
1280
1281      if (Record[1] >= MacroDefinitionsLoaded.size()) {
1282        Error("out-of-bounds macro definition record");
1283        return;
1284      }
1285
1286      MacroDefinition *MD
1287        = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1288                                SourceLocation::getFromRawEncoding(Record[5]),
1289                              SourceRange(
1290                                SourceLocation::getFromRawEncoding(Record[2]),
1291                                SourceLocation::getFromRawEncoding(Record[3])));
1292      PPRec.SetPreallocatedEntity(Record[0], MD);
1293      MacroDefinitionsLoaded[Record[1]] = MD;
1294      return;
1295    }
1296  }
1297  }
1298}
1299
1300void ASTReader::ReadDefinedMacros() {
1301  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1302    llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
1303
1304    // If there was no preprocessor block, skip this file.
1305    if (!MacroCursor.getBitStreamReader())
1306      continue;
1307
1308    llvm::BitstreamCursor Cursor = MacroCursor;
1309    if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
1310      Error("malformed preprocessor block record in AST file");
1311      return;
1312    }
1313
1314    RecordData Record;
1315    while (true) {
1316      unsigned Code = Cursor.ReadCode();
1317      if (Code == llvm::bitc::END_BLOCK) {
1318        if (Cursor.ReadBlockEnd()) {
1319          Error("error at end of preprocessor block in AST file");
1320          return;
1321        }
1322        break;
1323      }
1324
1325      if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1326        // No known subblocks, always skip them.
1327        Cursor.ReadSubBlockID();
1328        if (Cursor.SkipBlock()) {
1329          Error("malformed block record in AST file");
1330          return;
1331        }
1332        continue;
1333      }
1334
1335      if (Code == llvm::bitc::DEFINE_ABBREV) {
1336        Cursor.ReadAbbrevRecord();
1337        continue;
1338      }
1339
1340      // Read a record.
1341      const char *BlobStart;
1342      unsigned BlobLen;
1343      Record.clear();
1344      switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1345      default:  // Default behavior: ignore.
1346        break;
1347
1348      case PP_MACRO_OBJECT_LIKE:
1349      case PP_MACRO_FUNCTION_LIKE:
1350        DecodeIdentifierInfo(Record[0]);
1351        break;
1352
1353      case PP_TOKEN:
1354        // Ignore tokens.
1355        break;
1356
1357      case PP_MACRO_INSTANTIATION:
1358      case PP_MACRO_DEFINITION:
1359        // Read the macro record.
1360        ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1361        break;
1362      }
1363    }
1364  }
1365}
1366
1367MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
1368  if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1369    return 0;
1370
1371  if (!MacroDefinitionsLoaded[ID]) {
1372    unsigned Index = ID;
1373    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1374      PerFileData &F = *Chain[N - I - 1];
1375      if (Index < F.LocalNumMacroDefinitions) {
1376        ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1377        break;
1378      }
1379      Index -= F.LocalNumMacroDefinitions;
1380    }
1381    assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1382  }
1383
1384  return MacroDefinitionsLoaded[ID];
1385}
1386
1387/// \brief If we are loading a relocatable PCH file, and the filename is
1388/// not an absolute path, add the system root to the beginning of the file
1389/// name.
1390void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1391  // If this is not a relocatable PCH file, there's nothing to do.
1392  if (!RelocatablePCH)
1393    return;
1394
1395  if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
1396    return;
1397
1398  if (isysroot == 0) {
1399    // If no system root was given, default to '/'
1400    Filename.insert(Filename.begin(), '/');
1401    return;
1402  }
1403
1404  unsigned Length = strlen(isysroot);
1405  if (isysroot[Length - 1] != '/')
1406    Filename.insert(Filename.begin(), '/');
1407
1408  Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1409}
1410
1411ASTReader::ASTReadResult
1412ASTReader::ReadASTBlock(PerFileData &F) {
1413  llvm::BitstreamCursor &Stream = F.Stream;
1414
1415  if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1416    Error("malformed block record in AST file");
1417    return Failure;
1418  }
1419
1420  // Read all of the records and blocks for the ASt file.
1421  RecordData Record;
1422  bool First = true;
1423  while (!Stream.AtEndOfStream()) {
1424    unsigned Code = Stream.ReadCode();
1425    if (Code == llvm::bitc::END_BLOCK) {
1426      if (Stream.ReadBlockEnd()) {
1427        Error("error at end of module block in AST file");
1428        return Failure;
1429      }
1430
1431      return Success;
1432    }
1433
1434    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1435      switch (Stream.ReadSubBlockID()) {
1436      case DECLTYPES_BLOCK_ID:
1437        // We lazily load the decls block, but we want to set up the
1438        // DeclsCursor cursor to point into it.  Clone our current bitcode
1439        // cursor to it, enter the block and read the abbrevs in that block.
1440        // With the main cursor, we just skip over it.
1441        F.DeclsCursor = Stream;
1442        if (Stream.SkipBlock() ||  // Skip with the main cursor.
1443            // Read the abbrevs.
1444            ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1445          Error("malformed block record in AST file");
1446          return Failure;
1447        }
1448        break;
1449
1450      case PREPROCESSOR_BLOCK_ID:
1451        F.MacroCursor = Stream;
1452        if (PP)
1453          PP->setExternalSource(this);
1454
1455        if (Stream.SkipBlock()) {
1456          Error("malformed block record in AST file");
1457          return Failure;
1458        }
1459        break;
1460
1461      case SOURCE_MANAGER_BLOCK_ID:
1462        switch (ReadSourceManagerBlock(F)) {
1463        case Success:
1464          break;
1465
1466        case Failure:
1467          Error("malformed source manager block in AST file");
1468          return Failure;
1469
1470        case IgnorePCH:
1471          return IgnorePCH;
1472        }
1473        break;
1474      }
1475      First = false;
1476      continue;
1477    }
1478
1479    if (Code == llvm::bitc::DEFINE_ABBREV) {
1480      Stream.ReadAbbrevRecord();
1481      continue;
1482    }
1483
1484    // Read and process a record.
1485    Record.clear();
1486    const char *BlobStart = 0;
1487    unsigned BlobLen = 0;
1488    switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1489                                                   &BlobStart, &BlobLen)) {
1490    default:  // Default behavior: ignore.
1491      break;
1492
1493    case METADATA: {
1494      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1495        Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1496                                           : diag::warn_pch_version_too_new);
1497        return IgnorePCH;
1498      }
1499
1500      RelocatablePCH = Record[4];
1501      if (Listener) {
1502        std::string TargetTriple(BlobStart, BlobLen);
1503        if (Listener->ReadTargetTriple(TargetTriple))
1504          return IgnorePCH;
1505      }
1506      break;
1507    }
1508
1509    case CHAINED_METADATA: {
1510      if (!First) {
1511        Error("CHAINED_METADATA is not first record in block");
1512        return Failure;
1513      }
1514      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1515        Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1516                                           : diag::warn_pch_version_too_new);
1517        return IgnorePCH;
1518      }
1519
1520      // Load the chained file.
1521      switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
1522      case Failure: return Failure;
1523        // If we have to ignore the dependency, we'll have to ignore this too.
1524      case IgnorePCH: return IgnorePCH;
1525      case Success: break;
1526      }
1527      break;
1528    }
1529
1530    case TYPE_OFFSET:
1531      if (F.LocalNumTypes != 0) {
1532        Error("duplicate TYPE_OFFSET record in AST file");
1533        return Failure;
1534      }
1535      F.TypeOffsets = (const uint32_t *)BlobStart;
1536      F.LocalNumTypes = Record[0];
1537      break;
1538
1539    case DECL_OFFSET:
1540      if (F.LocalNumDecls != 0) {
1541        Error("duplicate DECL_OFFSET record in AST file");
1542        return Failure;
1543      }
1544      F.DeclOffsets = (const uint32_t *)BlobStart;
1545      F.LocalNumDecls = Record[0];
1546      break;
1547
1548    case TU_UPDATE_LEXICAL: {
1549      DeclContextInfo Info = {
1550        /* No visible information */ 0, 0,
1551        reinterpret_cast<const DeclID *>(BlobStart),
1552        BlobLen / sizeof(DeclID)
1553      };
1554      DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1555      break;
1556    }
1557
1558    case REDECLS_UPDATE_LATEST: {
1559      assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1560      for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
1561        DeclID First = Record[i], Latest = Record[i+1];
1562        assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1563                Latest > FirstLatestDeclIDs[First]) &&
1564               "The new latest is supposed to come after the previous latest");
1565        FirstLatestDeclIDs[First] = Latest;
1566      }
1567      break;
1568    }
1569
1570    case LANGUAGE_OPTIONS:
1571      if (ParseLanguageOptions(Record) && !DisableValidation)
1572        return IgnorePCH;
1573      break;
1574
1575    case IDENTIFIER_TABLE:
1576      F.IdentifierTableData = BlobStart;
1577      if (Record[0]) {
1578        F.IdentifierLookupTable
1579          = ASTIdentifierLookupTable::Create(
1580                       (const unsigned char *)F.IdentifierTableData + Record[0],
1581                       (const unsigned char *)F.IdentifierTableData,
1582                       ASTIdentifierLookupTrait(*this, F.Stream));
1583        if (PP)
1584          PP->getIdentifierTable().setExternalIdentifierLookup(this);
1585      }
1586      break;
1587
1588    case IDENTIFIER_OFFSET:
1589      if (F.LocalNumIdentifiers != 0) {
1590        Error("duplicate IDENTIFIER_OFFSET record in AST file");
1591        return Failure;
1592      }
1593      F.IdentifierOffsets = (const uint32_t *)BlobStart;
1594      F.LocalNumIdentifiers = Record[0];
1595      break;
1596
1597    case EXTERNAL_DEFINITIONS:
1598      // Optimization for the first block.
1599      if (ExternalDefinitions.empty())
1600        ExternalDefinitions.swap(Record);
1601      else
1602        ExternalDefinitions.insert(ExternalDefinitions.end(),
1603                                   Record.begin(), Record.end());
1604      break;
1605
1606    case SPECIAL_TYPES:
1607      // Optimization for the first block
1608      if (SpecialTypes.empty())
1609        SpecialTypes.swap(Record);
1610      else
1611        SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
1612      break;
1613
1614    case STATISTICS:
1615      TotalNumStatements += Record[0];
1616      TotalNumMacros += Record[1];
1617      TotalLexicalDeclContexts += Record[2];
1618      TotalVisibleDeclContexts += Record[3];
1619      break;
1620
1621    case TENTATIVE_DEFINITIONS:
1622      // Optimization for the first block.
1623      if (TentativeDefinitions.empty())
1624        TentativeDefinitions.swap(Record);
1625      else
1626        TentativeDefinitions.insert(TentativeDefinitions.end(),
1627                                    Record.begin(), Record.end());
1628      break;
1629
1630    case UNUSED_FILESCOPED_DECLS:
1631      // Optimization for the first block.
1632      if (UnusedFileScopedDecls.empty())
1633        UnusedFileScopedDecls.swap(Record);
1634      else
1635        UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1636                                     Record.begin(), Record.end());
1637      break;
1638
1639    case WEAK_UNDECLARED_IDENTIFIERS:
1640      // Later blocks overwrite earlier ones.
1641      WeakUndeclaredIdentifiers.swap(Record);
1642      break;
1643
1644    case LOCALLY_SCOPED_EXTERNAL_DECLS:
1645      // Optimization for the first block.
1646      if (LocallyScopedExternalDecls.empty())
1647        LocallyScopedExternalDecls.swap(Record);
1648      else
1649        LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1650                                          Record.begin(), Record.end());
1651      break;
1652
1653    case SELECTOR_OFFSETS:
1654      F.SelectorOffsets = (const uint32_t *)BlobStart;
1655      F.LocalNumSelectors = Record[0];
1656      break;
1657
1658    case METHOD_POOL:
1659      F.SelectorLookupTableData = (const unsigned char *)BlobStart;
1660      if (Record[0])
1661        F.SelectorLookupTable
1662          = ASTSelectorLookupTable::Create(
1663                        F.SelectorLookupTableData + Record[0],
1664                        F.SelectorLookupTableData,
1665                        ASTSelectorLookupTrait(*this));
1666      TotalNumMethodPoolEntries += Record[1];
1667      break;
1668
1669    case REFERENCED_SELECTOR_POOL: {
1670      ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1671          Record.begin(), Record.end());
1672      break;
1673    }
1674
1675    case PP_COUNTER_VALUE:
1676      if (!Record.empty() && Listener)
1677        Listener->ReadCounter(Record[0]);
1678      break;
1679
1680    case SOURCE_LOCATION_OFFSETS:
1681      F.SLocOffsets = (const uint32_t *)BlobStart;
1682      F.LocalNumSLocEntries = Record[0];
1683      // We cannot delay this until the entire chain is loaded, because then
1684      // source location preloads would also have to be delayed.
1685      // FIXME: Is there a reason not to do that?
1686      TotalNumSLocEntries += F.LocalNumSLocEntries;
1687      SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
1688      break;
1689
1690    case SOURCE_LOCATION_PRELOADS:
1691      for (unsigned I = 0, N = Record.size(); I != N; ++I) {
1692        ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
1693        if (Result != Success)
1694          return Result;
1695      }
1696      break;
1697
1698    case STAT_CACHE: {
1699      ASTStatCache *MyStatCache =
1700        new ASTStatCache((const unsigned char *)BlobStart + Record[0],
1701                         (const unsigned char *)BlobStart,
1702                         NumStatHits, NumStatMisses);
1703      FileMgr.addStatCache(MyStatCache);
1704      F.StatCache = MyStatCache;
1705      break;
1706    }
1707
1708    case EXT_VECTOR_DECLS:
1709      // Optimization for the first block.
1710      if (ExtVectorDecls.empty())
1711        ExtVectorDecls.swap(Record);
1712      else
1713        ExtVectorDecls.insert(ExtVectorDecls.end(),
1714                              Record.begin(), Record.end());
1715      break;
1716
1717    case VTABLE_USES:
1718      // Later tables overwrite earlier ones.
1719      VTableUses.swap(Record);
1720      break;
1721
1722    case DYNAMIC_CLASSES:
1723      // Optimization for the first block.
1724      if (DynamicClasses.empty())
1725        DynamicClasses.swap(Record);
1726      else
1727        DynamicClasses.insert(DynamicClasses.end(),
1728                              Record.begin(), Record.end());
1729      break;
1730
1731    case PENDING_IMPLICIT_INSTANTIATIONS:
1732      // Optimization for the first block.
1733      if (PendingImplicitInstantiations.empty())
1734        PendingImplicitInstantiations.swap(Record);
1735      else
1736        PendingImplicitInstantiations.insert(
1737             PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1738      break;
1739
1740    case SEMA_DECL_REFS:
1741      // Later tables overwrite earlier ones.
1742      SemaDeclRefs.swap(Record);
1743      break;
1744
1745    case ORIGINAL_FILE_NAME:
1746      // The primary AST will be the last to get here, so it will be the one
1747      // that's used.
1748      ActualOriginalFileName.assign(BlobStart, BlobLen);
1749      OriginalFileName = ActualOriginalFileName;
1750      MaybeAddSystemRootToFilename(OriginalFileName);
1751      break;
1752
1753    case VERSION_CONTROL_BRANCH_REVISION: {
1754      const std::string &CurBranch = getClangFullRepositoryVersion();
1755      llvm::StringRef ASTBranch(BlobStart, BlobLen);
1756      if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1757        Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1758        return IgnorePCH;
1759      }
1760      break;
1761    }
1762
1763    case MACRO_DEFINITION_OFFSETS:
1764      F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1765      F.NumPreallocatedPreprocessingEntities = Record[0];
1766      F.LocalNumMacroDefinitions = Record[1];
1767      break;
1768
1769    case DECL_REPLACEMENTS: {
1770      if (Record.size() % 2 != 0) {
1771        Error("invalid DECL_REPLACEMENTS block in AST file");
1772        return Failure;
1773      }
1774      for (unsigned I = 0, N = Record.size(); I != N; I += 2)
1775        ReplacedDecls[static_cast<DeclID>(Record[I])] =
1776            std::make_pair(&F, Record[I+1]);
1777      break;
1778    }
1779    }
1780    First = false;
1781  }
1782  Error("premature end of bitstream in AST file");
1783  return Failure;
1784}
1785
1786ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1787  switch(ReadASTCore(FileName)) {
1788  case Failure: return Failure;
1789  case IgnorePCH: return IgnorePCH;
1790  case Success: break;
1791  }
1792
1793  // Here comes stuff that we only do once the entire chain is loaded.
1794
1795  // Allocate space for loaded identifiers, decls and types.
1796  unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
1797           TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1798           TotalNumSelectors = 0;
1799  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1800    TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
1801    TotalNumTypes += Chain[I]->LocalNumTypes;
1802    TotalNumDecls += Chain[I]->LocalNumDecls;
1803    TotalNumPreallocatedPreprocessingEntities +=
1804        Chain[I]->NumPreallocatedPreprocessingEntities;
1805    TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
1806    TotalNumSelectors += Chain[I]->LocalNumSelectors;
1807  }
1808  IdentifiersLoaded.resize(TotalNumIdentifiers);
1809  TypesLoaded.resize(TotalNumTypes);
1810  DeclsLoaded.resize(TotalNumDecls);
1811  MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1812  if (PP) {
1813    if (TotalNumIdentifiers > 0)
1814      PP->getHeaderSearchInfo().SetExternalLookup(this);
1815    if (TotalNumPreallocatedPreprocessingEntities > 0) {
1816      if (!PP->getPreprocessingRecord())
1817        PP->createPreprocessingRecord();
1818      PP->getPreprocessingRecord()->SetExternalSource(*this,
1819                                     TotalNumPreallocatedPreprocessingEntities);
1820    }
1821  }
1822  SelectorsLoaded.resize(TotalNumSelectors);
1823
1824  // Check the predefines buffers.
1825  if (!DisableValidation && CheckPredefinesBuffers())
1826    return IgnorePCH;
1827
1828  if (PP) {
1829    // Initialization of keywords and pragmas occurs before the
1830    // AST file is read, so there may be some identifiers that were
1831    // loaded into the IdentifierTable before we intercepted the
1832    // creation of identifiers. Iterate through the list of known
1833    // identifiers and determine whether we have to establish
1834    // preprocessor definitions or top-level identifier declaration
1835    // chains for those identifiers.
1836    //
1837    // We copy the IdentifierInfo pointers to a small vector first,
1838    // since de-serializing declarations or macro definitions can add
1839    // new entries into the identifier table, invalidating the
1840    // iterators.
1841    llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1842    for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1843                                IdEnd = PP->getIdentifierTable().end();
1844         Id != IdEnd; ++Id)
1845      Identifiers.push_back(Id->second);
1846    // We need to search the tables in all files.
1847    for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
1848      ASTIdentifierLookupTable *IdTable
1849        = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1850      // Not all AST files necessarily have identifier tables, only the useful
1851      // ones.
1852      if (!IdTable)
1853        continue;
1854      for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1855        IdentifierInfo *II = Identifiers[I];
1856        // Look in the on-disk hash tables for an entry for this identifier
1857        ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
1858        std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
1859        ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1860        if (Pos == IdTable->end())
1861          continue;
1862
1863        // Dereferencing the iterator has the effect of populating the
1864        // IdentifierInfo node with the various declarations it needs.
1865        (void)*Pos;
1866      }
1867    }
1868  }
1869
1870  if (Context)
1871    InitializeContext(*Context);
1872
1873  return Success;
1874}
1875
1876ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
1877  Chain.push_back(new PerFileData());
1878  PerFileData &F = *Chain.back();
1879
1880  // Set the AST file name.
1881  F.FileName = FileName;
1882
1883  // Open the AST file.
1884  //
1885  // FIXME: This shouldn't be here, we should just take a raw_ostream.
1886  std::string ErrStr;
1887  F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1888  if (!F.Buffer) {
1889    Error(ErrStr.c_str());
1890    return IgnorePCH;
1891  }
1892
1893  // Initialize the stream
1894  F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1895                    (const unsigned char *)F.Buffer->getBufferEnd());
1896  llvm::BitstreamCursor &Stream = F.Stream;
1897  Stream.init(F.StreamFile);
1898  F.SizeInBits = F.Buffer->getBufferSize() * 8;
1899
1900  // Sniff for the signature.
1901  if (Stream.Read(8) != 'C' ||
1902      Stream.Read(8) != 'P' ||
1903      Stream.Read(8) != 'C' ||
1904      Stream.Read(8) != 'H') {
1905    Diag(diag::err_not_a_pch_file) << FileName;
1906    return Failure;
1907  }
1908
1909  while (!Stream.AtEndOfStream()) {
1910    unsigned Code = Stream.ReadCode();
1911
1912    if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1913      Error("invalid record at top-level of AST file");
1914      return Failure;
1915    }
1916
1917    unsigned BlockID = Stream.ReadSubBlockID();
1918
1919    // We only know the AST subblock ID.
1920    switch (BlockID) {
1921    case llvm::bitc::BLOCKINFO_BLOCK_ID:
1922      if (Stream.ReadBlockInfoBlock()) {
1923        Error("malformed BlockInfoBlock in AST file");
1924        return Failure;
1925      }
1926      break;
1927    case AST_BLOCK_ID:
1928      switch (ReadASTBlock(F)) {
1929      case Success:
1930        break;
1931
1932      case Failure:
1933        return Failure;
1934
1935      case IgnorePCH:
1936        // FIXME: We could consider reading through to the end of this
1937        // AST block, skipping subblocks, to see if there are other
1938        // AST blocks elsewhere.
1939
1940        // Clear out any preallocated source location entries, so that
1941        // the source manager does not try to resolve them later.
1942        SourceMgr.ClearPreallocatedSLocEntries();
1943
1944        // Remove the stat cache.
1945        if (F.StatCache)
1946          FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
1947
1948        return IgnorePCH;
1949      }
1950      break;
1951    default:
1952      if (Stream.SkipBlock()) {
1953        Error("malformed block record in AST file");
1954        return Failure;
1955      }
1956      break;
1957    }
1958  }
1959
1960  return Success;
1961}
1962
1963void ASTReader::setPreprocessor(Preprocessor &pp) {
1964  PP = &pp;
1965
1966  unsigned TotalNum = 0;
1967  for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1968    TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1969  if (TotalNum) {
1970    if (!PP->getPreprocessingRecord())
1971      PP->createPreprocessingRecord();
1972    PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
1973  }
1974}
1975
1976void ASTReader::InitializeContext(ASTContext &Ctx) {
1977  Context = &Ctx;
1978  assert(Context && "Passed null context!");
1979
1980  assert(PP && "Forgot to set Preprocessor ?");
1981  PP->getIdentifierTable().setExternalIdentifierLookup(this);
1982  PP->getHeaderSearchInfo().SetExternalLookup(this);
1983  PP->setExternalSource(this);
1984
1985  // Load the translation unit declaration
1986  GetTranslationUnitDecl();
1987
1988  // Load the special types.
1989  Context->setBuiltinVaListType(
1990    GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
1991  if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
1992    Context->setObjCIdType(GetType(Id));
1993  if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
1994    Context->setObjCSelType(GetType(Sel));
1995  if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
1996    Context->setObjCProtoType(GetType(Proto));
1997  if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
1998    Context->setObjCClassType(GetType(Class));
1999
2000  if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
2001    Context->setCFConstantStringType(GetType(String));
2002  if (unsigned FastEnum
2003        = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2004    Context->setObjCFastEnumerationStateType(GetType(FastEnum));
2005  if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2006    QualType FileType = GetType(File);
2007    if (FileType.isNull()) {
2008      Error("FILE type is NULL");
2009      return;
2010    }
2011    if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2012      Context->setFILEDecl(Typedef->getDecl());
2013    else {
2014      const TagType *Tag = FileType->getAs<TagType>();
2015      if (!Tag) {
2016        Error("Invalid FILE type in AST file");
2017        return;
2018      }
2019      Context->setFILEDecl(Tag->getDecl());
2020    }
2021  }
2022  if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2023    QualType Jmp_bufType = GetType(Jmp_buf);
2024    if (Jmp_bufType.isNull()) {
2025      Error("jmp_bug type is NULL");
2026      return;
2027    }
2028    if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2029      Context->setjmp_bufDecl(Typedef->getDecl());
2030    else {
2031      const TagType *Tag = Jmp_bufType->getAs<TagType>();
2032      if (!Tag) {
2033        Error("Invalid jmp_buf type in AST file");
2034        return;
2035      }
2036      Context->setjmp_bufDecl(Tag->getDecl());
2037    }
2038  }
2039  if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2040    QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2041    if (Sigjmp_bufType.isNull()) {
2042      Error("sigjmp_buf type is NULL");
2043      return;
2044    }
2045    if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2046      Context->setsigjmp_bufDecl(Typedef->getDecl());
2047    else {
2048      const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2049      assert(Tag && "Invalid sigjmp_buf type in AST file");
2050      Context->setsigjmp_bufDecl(Tag->getDecl());
2051    }
2052  }
2053  if (unsigned ObjCIdRedef
2054        = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2055    Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2056  if (unsigned ObjCClassRedef
2057      = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2058    Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2059  if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2060    Context->setBlockDescriptorType(GetType(String));
2061  if (unsigned String
2062      = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2063    Context->setBlockDescriptorExtendedType(GetType(String));
2064  if (unsigned ObjCSelRedef
2065      = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2066    Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2067  if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
2068    Context->setNSConstantStringType(GetType(String));
2069
2070  if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
2071    Context->setInt128Installed();
2072}
2073
2074/// \brief Retrieve the name of the original source file name
2075/// directly from the AST file, without actually loading the AST
2076/// file.
2077std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2078                                             Diagnostic &Diags) {
2079  // Open the AST file.
2080  std::string ErrStr;
2081  llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2082  Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
2083  if (!Buffer) {
2084    Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2085    return std::string();
2086  }
2087
2088  // Initialize the stream
2089  llvm::BitstreamReader StreamFile;
2090  llvm::BitstreamCursor Stream;
2091  StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2092                  (const unsigned char *)Buffer->getBufferEnd());
2093  Stream.init(StreamFile);
2094
2095  // Sniff for the signature.
2096  if (Stream.Read(8) != 'C' ||
2097      Stream.Read(8) != 'P' ||
2098      Stream.Read(8) != 'C' ||
2099      Stream.Read(8) != 'H') {
2100    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2101    return std::string();
2102  }
2103
2104  RecordData Record;
2105  while (!Stream.AtEndOfStream()) {
2106    unsigned Code = Stream.ReadCode();
2107
2108    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2109      unsigned BlockID = Stream.ReadSubBlockID();
2110
2111      // We only know the AST subblock ID.
2112      switch (BlockID) {
2113      case AST_BLOCK_ID:
2114        if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2115          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2116          return std::string();
2117        }
2118        break;
2119
2120      default:
2121        if (Stream.SkipBlock()) {
2122          Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2123          return std::string();
2124        }
2125        break;
2126      }
2127      continue;
2128    }
2129
2130    if (Code == llvm::bitc::END_BLOCK) {
2131      if (Stream.ReadBlockEnd()) {
2132        Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2133        return std::string();
2134      }
2135      continue;
2136    }
2137
2138    if (Code == llvm::bitc::DEFINE_ABBREV) {
2139      Stream.ReadAbbrevRecord();
2140      continue;
2141    }
2142
2143    Record.clear();
2144    const char *BlobStart = 0;
2145    unsigned BlobLen = 0;
2146    if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2147          == ORIGINAL_FILE_NAME)
2148      return std::string(BlobStart, BlobLen);
2149  }
2150
2151  return std::string();
2152}
2153
2154/// \brief Parse the record that corresponds to a LangOptions data
2155/// structure.
2156///
2157/// This routine parses the language options from the AST file and then gives
2158/// them to the AST listener if one is set.
2159///
2160/// \returns true if the listener deems the file unacceptable, false otherwise.
2161bool ASTReader::ParseLanguageOptions(
2162                             const llvm::SmallVectorImpl<uint64_t> &Record) {
2163  if (Listener) {
2164    LangOptions LangOpts;
2165
2166  #define PARSE_LANGOPT(Option)                  \
2167      LangOpts.Option = Record[Idx];             \
2168      ++Idx
2169
2170    unsigned Idx = 0;
2171    PARSE_LANGOPT(Trigraphs);
2172    PARSE_LANGOPT(BCPLComment);
2173    PARSE_LANGOPT(DollarIdents);
2174    PARSE_LANGOPT(AsmPreprocessor);
2175    PARSE_LANGOPT(GNUMode);
2176    PARSE_LANGOPT(GNUKeywords);
2177    PARSE_LANGOPT(ImplicitInt);
2178    PARSE_LANGOPT(Digraphs);
2179    PARSE_LANGOPT(HexFloats);
2180    PARSE_LANGOPT(C99);
2181    PARSE_LANGOPT(Microsoft);
2182    PARSE_LANGOPT(CPlusPlus);
2183    PARSE_LANGOPT(CPlusPlus0x);
2184    PARSE_LANGOPT(CXXOperatorNames);
2185    PARSE_LANGOPT(ObjC1);
2186    PARSE_LANGOPT(ObjC2);
2187    PARSE_LANGOPT(ObjCNonFragileABI);
2188    PARSE_LANGOPT(ObjCNonFragileABI2);
2189    PARSE_LANGOPT(NoConstantCFStrings);
2190    PARSE_LANGOPT(PascalStrings);
2191    PARSE_LANGOPT(WritableStrings);
2192    PARSE_LANGOPT(LaxVectorConversions);
2193    PARSE_LANGOPT(AltiVec);
2194    PARSE_LANGOPT(Exceptions);
2195    PARSE_LANGOPT(SjLjExceptions);
2196    PARSE_LANGOPT(NeXTRuntime);
2197    PARSE_LANGOPT(Freestanding);
2198    PARSE_LANGOPT(NoBuiltin);
2199    PARSE_LANGOPT(ThreadsafeStatics);
2200    PARSE_LANGOPT(POSIXThreads);
2201    PARSE_LANGOPT(Blocks);
2202    PARSE_LANGOPT(EmitAllDecls);
2203    PARSE_LANGOPT(MathErrno);
2204    LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2205                                       Record[Idx++]);
2206    PARSE_LANGOPT(HeinousExtensions);
2207    PARSE_LANGOPT(Optimize);
2208    PARSE_LANGOPT(OptimizeSize);
2209    PARSE_LANGOPT(Static);
2210    PARSE_LANGOPT(PICLevel);
2211    PARSE_LANGOPT(GNUInline);
2212    PARSE_LANGOPT(NoInline);
2213    PARSE_LANGOPT(AccessControl);
2214    PARSE_LANGOPT(CharIsSigned);
2215    PARSE_LANGOPT(ShortWChar);
2216    LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2217    LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
2218    LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
2219                                   Record[Idx++]);
2220    PARSE_LANGOPT(InstantiationDepth);
2221    PARSE_LANGOPT(OpenCL);
2222    PARSE_LANGOPT(CatchUndefined);
2223    // FIXME: Missing ElideConstructors?!
2224  #undef PARSE_LANGOPT
2225
2226    return Listener->ReadLanguageOptions(LangOpts);
2227  }
2228
2229  return false;
2230}
2231
2232void ASTReader::ReadPreprocessedEntities() {
2233  ReadDefinedMacros();
2234}
2235
2236/// \brief Get the correct cursor and offset for loading a type.
2237ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
2238  PerFileData *F = 0;
2239  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2240    F = Chain[N - I - 1];
2241    if (Index < F->LocalNumTypes)
2242      break;
2243    Index -= F->LocalNumTypes;
2244  }
2245  assert(F && F->LocalNumTypes > Index && "Broken chain");
2246  return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
2247}
2248
2249/// \brief Read and return the type with the given index..
2250///
2251/// The index is the type ID, shifted and minus the number of predefs. This
2252/// routine actually reads the record corresponding to the type at the given
2253/// location. It is a helper routine for GetType, which deals with reading type
2254/// IDs.
2255QualType ASTReader::ReadTypeRecord(unsigned Index) {
2256  RecordLocation Loc = TypeCursorForIndex(Index);
2257  llvm::BitstreamCursor &DeclsCursor = *Loc.first;
2258
2259  // Keep track of where we are in the stream, then jump back there
2260  // after reading this type.
2261  SavedStreamPosition SavedPosition(DeclsCursor);
2262
2263  ReadingKindTracker ReadingKind(Read_Type, *this);
2264
2265  // Note that we are loading a type record.
2266  Deserializing AType(this);
2267
2268  DeclsCursor.JumpToBit(Loc.second);
2269  RecordData Record;
2270  unsigned Code = DeclsCursor.ReadCode();
2271  switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2272  case TYPE_EXT_QUAL: {
2273    if (Record.size() != 2) {
2274      Error("Incorrect encoding of extended qualifier type");
2275      return QualType();
2276    }
2277    QualType Base = GetType(Record[0]);
2278    Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2279    return Context->getQualifiedType(Base, Quals);
2280  }
2281
2282  case TYPE_COMPLEX: {
2283    if (Record.size() != 1) {
2284      Error("Incorrect encoding of complex type");
2285      return QualType();
2286    }
2287    QualType ElemType = GetType(Record[0]);
2288    return Context->getComplexType(ElemType);
2289  }
2290
2291  case TYPE_POINTER: {
2292    if (Record.size() != 1) {
2293      Error("Incorrect encoding of pointer type");
2294      return QualType();
2295    }
2296    QualType PointeeType = GetType(Record[0]);
2297    return Context->getPointerType(PointeeType);
2298  }
2299
2300  case TYPE_BLOCK_POINTER: {
2301    if (Record.size() != 1) {
2302      Error("Incorrect encoding of block pointer type");
2303      return QualType();
2304    }
2305    QualType PointeeType = GetType(Record[0]);
2306    return Context->getBlockPointerType(PointeeType);
2307  }
2308
2309  case TYPE_LVALUE_REFERENCE: {
2310    if (Record.size() != 1) {
2311      Error("Incorrect encoding of lvalue reference type");
2312      return QualType();
2313    }
2314    QualType PointeeType = GetType(Record[0]);
2315    return Context->getLValueReferenceType(PointeeType);
2316  }
2317
2318  case TYPE_RVALUE_REFERENCE: {
2319    if (Record.size() != 1) {
2320      Error("Incorrect encoding of rvalue reference type");
2321      return QualType();
2322    }
2323    QualType PointeeType = GetType(Record[0]);
2324    return Context->getRValueReferenceType(PointeeType);
2325  }
2326
2327  case TYPE_MEMBER_POINTER: {
2328    if (Record.size() != 2) {
2329      Error("Incorrect encoding of member pointer type");
2330      return QualType();
2331    }
2332    QualType PointeeType = GetType(Record[0]);
2333    QualType ClassType = GetType(Record[1]);
2334    return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
2335  }
2336
2337  case TYPE_CONSTANT_ARRAY: {
2338    QualType ElementType = GetType(Record[0]);
2339    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2340    unsigned IndexTypeQuals = Record[2];
2341    unsigned Idx = 3;
2342    llvm::APInt Size = ReadAPInt(Record, Idx);
2343    return Context->getConstantArrayType(ElementType, Size,
2344                                         ASM, IndexTypeQuals);
2345  }
2346
2347  case TYPE_INCOMPLETE_ARRAY: {
2348    QualType ElementType = GetType(Record[0]);
2349    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2350    unsigned IndexTypeQuals = Record[2];
2351    return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2352  }
2353
2354  case TYPE_VARIABLE_ARRAY: {
2355    QualType ElementType = GetType(Record[0]);
2356    ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2357    unsigned IndexTypeQuals = Record[2];
2358    SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2359    SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
2360    return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
2361                                         ASM, IndexTypeQuals,
2362                                         SourceRange(LBLoc, RBLoc));
2363  }
2364
2365  case TYPE_VECTOR: {
2366    if (Record.size() != 3) {
2367      Error("incorrect encoding of vector type in AST file");
2368      return QualType();
2369    }
2370
2371    QualType ElementType = GetType(Record[0]);
2372    unsigned NumElements = Record[1];
2373    unsigned AltiVecSpec = Record[2];
2374    return Context->getVectorType(ElementType, NumElements,
2375                                  (VectorType::AltiVecSpecific)AltiVecSpec);
2376  }
2377
2378  case TYPE_EXT_VECTOR: {
2379    if (Record.size() != 3) {
2380      Error("incorrect encoding of extended vector type in AST file");
2381      return QualType();
2382    }
2383
2384    QualType ElementType = GetType(Record[0]);
2385    unsigned NumElements = Record[1];
2386    return Context->getExtVectorType(ElementType, NumElements);
2387  }
2388
2389  case TYPE_FUNCTION_NO_PROTO: {
2390    if (Record.size() != 4) {
2391      Error("incorrect encoding of no-proto function type");
2392      return QualType();
2393    }
2394    QualType ResultType = GetType(Record[0]);
2395    FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
2396    return Context->getFunctionNoProtoType(ResultType, Info);
2397  }
2398
2399  case TYPE_FUNCTION_PROTO: {
2400    QualType ResultType = GetType(Record[0]);
2401    bool NoReturn = Record[1];
2402    unsigned RegParm = Record[2];
2403    CallingConv CallConv = (CallingConv)Record[3];
2404    unsigned Idx = 4;
2405    unsigned NumParams = Record[Idx++];
2406    llvm::SmallVector<QualType, 16> ParamTypes;
2407    for (unsigned I = 0; I != NumParams; ++I)
2408      ParamTypes.push_back(GetType(Record[Idx++]));
2409    bool isVariadic = Record[Idx++];
2410    unsigned Quals = Record[Idx++];
2411    bool hasExceptionSpec = Record[Idx++];
2412    bool hasAnyExceptionSpec = Record[Idx++];
2413    unsigned NumExceptions = Record[Idx++];
2414    llvm::SmallVector<QualType, 2> Exceptions;
2415    for (unsigned I = 0; I != NumExceptions; ++I)
2416      Exceptions.push_back(GetType(Record[Idx++]));
2417    return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
2418                                    isVariadic, Quals, hasExceptionSpec,
2419                                    hasAnyExceptionSpec, NumExceptions,
2420                                    Exceptions.data(),
2421                                    FunctionType::ExtInfo(NoReturn, RegParm,
2422                                                          CallConv));
2423  }
2424
2425  case TYPE_UNRESOLVED_USING:
2426    return Context->getTypeDeclType(
2427             cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2428
2429  case TYPE_TYPEDEF: {
2430    if (Record.size() != 2) {
2431      Error("incorrect encoding of typedef type");
2432      return QualType();
2433    }
2434    TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2435    QualType Canonical = GetType(Record[1]);
2436    return Context->getTypedefType(Decl, Canonical);
2437  }
2438
2439  case TYPE_TYPEOF_EXPR:
2440    return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
2441
2442  case TYPE_TYPEOF: {
2443    if (Record.size() != 1) {
2444      Error("incorrect encoding of typeof(type) in AST file");
2445      return QualType();
2446    }
2447    QualType UnderlyingType = GetType(Record[0]);
2448    return Context->getTypeOfType(UnderlyingType);
2449  }
2450
2451  case TYPE_DECLTYPE:
2452    return Context->getDecltypeType(ReadExpr(DeclsCursor));
2453
2454  case TYPE_RECORD: {
2455    if (Record.size() != 2) {
2456      Error("incorrect encoding of record type");
2457      return QualType();
2458    }
2459    bool IsDependent = Record[0];
2460    QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2461    T->Dependent = IsDependent;
2462    return T;
2463  }
2464
2465  case TYPE_ENUM: {
2466    if (Record.size() != 2) {
2467      Error("incorrect encoding of enum type");
2468      return QualType();
2469    }
2470    bool IsDependent = Record[0];
2471    QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2472    T->Dependent = IsDependent;
2473    return T;
2474  }
2475
2476  case TYPE_ELABORATED: {
2477    unsigned Idx = 0;
2478    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2479    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2480    QualType NamedType = GetType(Record[Idx++]);
2481    return Context->getElaboratedType(Keyword, NNS, NamedType);
2482  }
2483
2484  case TYPE_OBJC_INTERFACE: {
2485    unsigned Idx = 0;
2486    ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2487    return Context->getObjCInterfaceType(ItfD);
2488  }
2489
2490  case TYPE_OBJC_OBJECT: {
2491    unsigned Idx = 0;
2492    QualType Base = GetType(Record[Idx++]);
2493    unsigned NumProtos = Record[Idx++];
2494    llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2495    for (unsigned I = 0; I != NumProtos; ++I)
2496      Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2497    return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
2498  }
2499
2500  case TYPE_OBJC_OBJECT_POINTER: {
2501    unsigned Idx = 0;
2502    QualType Pointee = GetType(Record[Idx++]);
2503    return Context->getObjCObjectPointerType(Pointee);
2504  }
2505
2506  case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2507    unsigned Idx = 0;
2508    QualType Parm = GetType(Record[Idx++]);
2509    QualType Replacement = GetType(Record[Idx++]);
2510    return
2511      Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2512                                            Replacement);
2513  }
2514
2515  case TYPE_INJECTED_CLASS_NAME: {
2516    CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2517    QualType TST = GetType(Record[1]); // probably derivable
2518    // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
2519    // for AST reading, too much interdependencies.
2520    return
2521      QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
2522  }
2523
2524  case TYPE_TEMPLATE_TYPE_PARM: {
2525    unsigned Idx = 0;
2526    unsigned Depth = Record[Idx++];
2527    unsigned Index = Record[Idx++];
2528    bool Pack = Record[Idx++];
2529    IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2530    return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2531  }
2532
2533  case TYPE_DEPENDENT_NAME: {
2534    unsigned Idx = 0;
2535    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2536    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2537    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2538    QualType Canon = GetType(Record[Idx++]);
2539    return Context->getDependentNameType(Keyword, NNS, Name, Canon);
2540  }
2541
2542  case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
2543    unsigned Idx = 0;
2544    ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2545    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2546    const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2547    unsigned NumArgs = Record[Idx++];
2548    llvm::SmallVector<TemplateArgument, 8> Args;
2549    Args.reserve(NumArgs);
2550    while (NumArgs--)
2551      Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
2552    return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2553                                                      Args.size(), Args.data());
2554  }
2555
2556  case TYPE_DEPENDENT_SIZED_ARRAY: {
2557    unsigned Idx = 0;
2558
2559    // ArrayType
2560    QualType ElementType = GetType(Record[Idx++]);
2561    ArrayType::ArraySizeModifier ASM
2562      = (ArrayType::ArraySizeModifier)Record[Idx++];
2563    unsigned IndexTypeQuals = Record[Idx++];
2564
2565    // DependentSizedArrayType
2566    Expr *NumElts = ReadExpr(DeclsCursor);
2567    SourceRange Brackets = ReadSourceRange(Record, Idx);
2568
2569    return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2570                                               IndexTypeQuals, Brackets);
2571  }
2572
2573  case TYPE_TEMPLATE_SPECIALIZATION: {
2574    unsigned Idx = 0;
2575    bool IsDependent = Record[Idx++];
2576    TemplateName Name = ReadTemplateName(Record, Idx);
2577    llvm::SmallVector<TemplateArgument, 8> Args;
2578    ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
2579    QualType Canon = GetType(Record[Idx++]);
2580    QualType T;
2581    if (Canon.isNull())
2582      T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2583                                                          Args.size());
2584    else
2585      T = Context->getTemplateSpecializationType(Name, Args.data(),
2586                                                 Args.size(), Canon);
2587    T->Dependent = IsDependent;
2588    return T;
2589  }
2590  }
2591  // Suppress a GCC warning
2592  return QualType();
2593}
2594
2595namespace {
2596
2597class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
2598  ASTReader &Reader;
2599  llvm::BitstreamCursor &DeclsCursor;
2600  const ASTReader::RecordData &Record;
2601  unsigned &Idx;
2602
2603public:
2604  TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2605                const ASTReader::RecordData &Record, unsigned &Idx)
2606    : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
2607
2608  // We want compile-time assurance that we've enumerated all of
2609  // these, so unfortunately we have to declare them first, then
2610  // define them out-of-line.
2611#define ABSTRACT_TYPELOC(CLASS, PARENT)
2612#define TYPELOC(CLASS, PARENT) \
2613  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
2614#include "clang/AST/TypeLocNodes.def"
2615
2616  void VisitFunctionTypeLoc(FunctionTypeLoc);
2617  void VisitArrayTypeLoc(ArrayTypeLoc);
2618};
2619
2620}
2621
2622void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
2623  // nothing to do
2624}
2625void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2626  TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2627  if (TL.needsExtraLocalData()) {
2628    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2629    TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2630    TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2631    TL.setModeAttr(Record[Idx++]);
2632  }
2633}
2634void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2635  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2636}
2637void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2638  TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2639}
2640void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2641  TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2642}
2643void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2644  TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2645}
2646void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2647  TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2648}
2649void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2650  TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2651}
2652void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2653  TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2654  TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2655  if (Record[Idx++])
2656    TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
2657  else
2658    TL.setSizeExpr(0);
2659}
2660void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2661  VisitArrayTypeLoc(TL);
2662}
2663void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2664  VisitArrayTypeLoc(TL);
2665}
2666void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2667  VisitArrayTypeLoc(TL);
2668}
2669void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2670                                            DependentSizedArrayTypeLoc TL) {
2671  VisitArrayTypeLoc(TL);
2672}
2673void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2674                                        DependentSizedExtVectorTypeLoc TL) {
2675  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2676}
2677void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2678  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2679}
2680void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2681  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2682}
2683void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2684  TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2685  TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2686  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
2687    TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
2688  }
2689}
2690void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2691  VisitFunctionTypeLoc(TL);
2692}
2693void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2694  VisitFunctionTypeLoc(TL);
2695}
2696void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2697  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2698}
2699void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2700  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2701}
2702void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2703  TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2704  TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2705  TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2706}
2707void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2708  TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2709  TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2710  TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2711  TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
2712}
2713void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2714  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2715}
2716void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2717  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2718}
2719void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2720  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2721}
2722void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2723  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2724}
2725void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2726                                            SubstTemplateTypeParmTypeLoc TL) {
2727  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2728}
2729void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2730                                           TemplateSpecializationTypeLoc TL) {
2731  TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2732  TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2733  TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2734  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2735    TL.setArgLocInfo(i,
2736        Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
2737                                          DeclsCursor, Record, Idx));
2738}
2739void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2740  TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2741  TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2742}
2743void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2744  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2745}
2746void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2747  TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2748  TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2749  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2750}
2751void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2752       DependentTemplateSpecializationTypeLoc TL) {
2753  TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2754  TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2755  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2756  TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2757  TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2758  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2759    TL.setArgLocInfo(I,
2760        Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
2761                                          DeclsCursor, Record, Idx));
2762}
2763void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2764  TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2765}
2766void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2767  TL.setHasBaseTypeAsWritten(Record[Idx++]);
2768  TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2769  TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2770  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2771    TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
2772}
2773void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2774  TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2775}
2776
2777TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
2778                                             const RecordData &Record,
2779                                             unsigned &Idx) {
2780  QualType InfoTy = GetType(Record[Idx++]);
2781  if (InfoTy.isNull())
2782    return 0;
2783
2784  TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
2785  TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
2786  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2787    TLR.Visit(TL);
2788  return TInfo;
2789}
2790
2791QualType ASTReader::GetType(TypeID ID) {
2792  unsigned FastQuals = ID & Qualifiers::FastMask;
2793  unsigned Index = ID >> Qualifiers::FastWidth;
2794
2795  if (Index < NUM_PREDEF_TYPE_IDS) {
2796    QualType T;
2797    switch ((PredefinedTypeIDs)Index) {
2798    case PREDEF_TYPE_NULL_ID: return QualType();
2799    case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2800    case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
2801
2802    case PREDEF_TYPE_CHAR_U_ID:
2803    case PREDEF_TYPE_CHAR_S_ID:
2804      // FIXME: Check that the signedness of CharTy is correct!
2805      T = Context->CharTy;
2806      break;
2807
2808    case PREDEF_TYPE_UCHAR_ID:      T = Context->UnsignedCharTy;     break;
2809    case PREDEF_TYPE_USHORT_ID:     T = Context->UnsignedShortTy;    break;
2810    case PREDEF_TYPE_UINT_ID:       T = Context->UnsignedIntTy;      break;
2811    case PREDEF_TYPE_ULONG_ID:      T = Context->UnsignedLongTy;     break;
2812    case PREDEF_TYPE_ULONGLONG_ID:  T = Context->UnsignedLongLongTy; break;
2813    case PREDEF_TYPE_UINT128_ID:    T = Context->UnsignedInt128Ty;   break;
2814    case PREDEF_TYPE_SCHAR_ID:      T = Context->SignedCharTy;       break;
2815    case PREDEF_TYPE_WCHAR_ID:      T = Context->WCharTy;            break;
2816    case PREDEF_TYPE_SHORT_ID:      T = Context->ShortTy;            break;
2817    case PREDEF_TYPE_INT_ID:        T = Context->IntTy;              break;
2818    case PREDEF_TYPE_LONG_ID:       T = Context->LongTy;             break;
2819    case PREDEF_TYPE_LONGLONG_ID:   T = Context->LongLongTy;         break;
2820    case PREDEF_TYPE_INT128_ID:     T = Context->Int128Ty;           break;
2821    case PREDEF_TYPE_FLOAT_ID:      T = Context->FloatTy;            break;
2822    case PREDEF_TYPE_DOUBLE_ID:     T = Context->DoubleTy;           break;
2823    case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy;       break;
2824    case PREDEF_TYPE_OVERLOAD_ID:   T = Context->OverloadTy;         break;
2825    case PREDEF_TYPE_DEPENDENT_ID:  T = Context->DependentTy;        break;
2826    case PREDEF_TYPE_NULLPTR_ID:    T = Context->NullPtrTy;          break;
2827    case PREDEF_TYPE_CHAR16_ID:     T = Context->Char16Ty;           break;
2828    case PREDEF_TYPE_CHAR32_ID:     T = Context->Char32Ty;           break;
2829    case PREDEF_TYPE_OBJC_ID:       T = Context->ObjCBuiltinIdTy;    break;
2830    case PREDEF_TYPE_OBJC_CLASS:    T = Context->ObjCBuiltinClassTy; break;
2831    case PREDEF_TYPE_OBJC_SEL:      T = Context->ObjCBuiltinSelTy;   break;
2832    }
2833
2834    assert(!T.isNull() && "Unknown predefined type");
2835    return T.withFastQualifiers(FastQuals);
2836  }
2837
2838  Index -= NUM_PREDEF_TYPE_IDS;
2839  assert(Index < TypesLoaded.size() && "Type index out-of-range");
2840  if (TypesLoaded[Index].isNull()) {
2841    TypesLoaded[Index] = ReadTypeRecord(Index);
2842    TypesLoaded[Index]->setFromAST();
2843    if (DeserializationListener)
2844      DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
2845                                        TypesLoaded[Index]);
2846  }
2847
2848  return TypesLoaded[Index].withFastQualifiers(FastQuals);
2849}
2850
2851TemplateArgumentLocInfo
2852ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2853                                      llvm::BitstreamCursor &DeclsCursor,
2854                                      const RecordData &Record,
2855                                      unsigned &Index) {
2856  switch (Kind) {
2857  case TemplateArgument::Expression:
2858    return ReadExpr(DeclsCursor);
2859  case TemplateArgument::Type:
2860    return GetTypeSourceInfo(DeclsCursor, Record, Index);
2861  case TemplateArgument::Template: {
2862    SourceRange QualifierRange = ReadSourceRange(Record, Index);
2863    SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2864    return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
2865  }
2866  case TemplateArgument::Null:
2867  case TemplateArgument::Integral:
2868  case TemplateArgument::Declaration:
2869  case TemplateArgument::Pack:
2870    return TemplateArgumentLocInfo();
2871  }
2872  llvm_unreachable("unexpected template argument loc");
2873  return TemplateArgumentLocInfo();
2874}
2875
2876TemplateArgumentLoc
2877ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
2878                                   const RecordData &Record, unsigned &Index) {
2879  TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
2880
2881  if (Arg.getKind() == TemplateArgument::Expression) {
2882    if (Record[Index++]) // bool InfoHasSameExpr.
2883      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2884  }
2885  return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
2886                                                             DeclsCursor,
2887                                                             Record, Index));
2888}
2889
2890Decl *ASTReader::GetExternalDecl(uint32_t ID) {
2891  return GetDecl(ID);
2892}
2893
2894TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
2895  if (!DeclsLoaded[0]) {
2896    ReadDeclRecord(0, 0);
2897    if (DeserializationListener)
2898      DeserializationListener->DeclRead(1, DeclsLoaded[0]);
2899  }
2900
2901  return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2902}
2903
2904Decl *ASTReader::GetDecl(DeclID ID) {
2905  if (ID == 0)
2906    return 0;
2907
2908  if (ID > DeclsLoaded.size()) {
2909    Error("declaration ID out-of-range for AST file");
2910    return 0;
2911  }
2912
2913  unsigned Index = ID - 1;
2914  if (!DeclsLoaded[Index]) {
2915    ReadDeclRecord(Index, ID);
2916    if (DeserializationListener)
2917      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2918  }
2919
2920  return DeclsLoaded[Index];
2921}
2922
2923/// \brief Resolve the offset of a statement into a statement.
2924///
2925/// This operation will read a new statement from the external
2926/// source each time it is called, and is meant to be used via a
2927/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
2928Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
2929  // Offset here is a global offset across the entire chain.
2930  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2931    PerFileData &F = *Chain[N - I - 1];
2932    if (Offset < F.SizeInBits) {
2933      // Since we know that this statement is part of a decl, make sure to use
2934      // the decl cursor to read it.
2935      F.DeclsCursor.JumpToBit(Offset);
2936      return ReadStmtFromStream(F.DeclsCursor);
2937    }
2938    Offset -= F.SizeInBits;
2939  }
2940  llvm_unreachable("Broken chain");
2941}
2942
2943bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
2944                                         llvm::SmallVectorImpl<Decl*> &Decls) {
2945  assert(DC->hasExternalLexicalStorage() &&
2946         "DeclContext has no lexical decls in storage");
2947
2948  // There might be lexical decls in multiple parts of the chain, for the TU
2949  // at least.
2950  DeclContextInfos &Infos = DeclContextOffsets[DC];
2951  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2952       I != E; ++I) {
2953    // IDs can be 0 if this context doesn't contain declarations.
2954    if (!I->LexicalDecls)
2955      continue;
2956
2957    // Load all of the declaration IDs
2958    for (const DeclID *ID = I->LexicalDecls,
2959                           *IDE = ID + I->NumLexicalDecls;
2960         ID != IDE; ++ID)
2961      Decls.push_back(GetDecl(*ID));
2962  }
2963
2964  ++NumLexicalDeclContextsRead;
2965  return false;
2966}
2967
2968DeclContext::lookup_result
2969ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
2970                                          DeclarationName Name) {
2971  assert(DC->hasExternalVisibleStorage() &&
2972         "DeclContext has no visible decls in storage");
2973
2974  llvm::SmallVector<VisibleDeclaration, 64> Decls;
2975  // There might be lexical decls in multiple parts of the chain, for the TU
2976  // and namespaces.
2977  DeclContextInfos &Infos = DeclContextOffsets[DC];
2978  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2979       I != E; ++I) {
2980    uint64_t Offset = I->OffsetToVisibleDecls;
2981    if (Offset == 0)
2982      continue;
2983
2984    llvm::BitstreamCursor &DeclsCursor = *I->Stream;
2985
2986    // Keep track of where we are in the stream, then jump back there
2987    // after reading this context.
2988    SavedStreamPosition SavedPosition(DeclsCursor);
2989
2990    // Load the record containing all of the declarations visible in
2991    // this context.
2992    DeclsCursor.JumpToBit(Offset);
2993    RecordData Record;
2994    unsigned Code = DeclsCursor.ReadCode();
2995    unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
2996    if (RecCode != DECL_CONTEXT_VISIBLE) {
2997      Error("Expected visible block");
2998      return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2999                                        DeclContext::lookup_iterator());
3000    }
3001
3002    if (Record.empty())
3003      continue;
3004
3005    unsigned Idx = 0;
3006    while (Idx < Record.size()) {
3007      Decls.push_back(VisibleDeclaration());
3008      Decls.back().Name = ReadDeclarationName(Record, Idx);
3009
3010      unsigned Size = Record[Idx++];
3011      llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
3012      LoadedDecls.reserve(Size);
3013      for (unsigned J = 0; J < Size; ++J)
3014        LoadedDecls.push_back(Record[Idx++]);
3015    }
3016  }
3017
3018  ++NumVisibleDeclContextsRead;
3019
3020  SetExternalVisibleDecls(DC, Decls);
3021  return const_cast<DeclContext*>(DC)->lookup(Name);
3022}
3023
3024void ASTReader::PassInterestingDeclsToConsumer() {
3025  assert(Consumer);
3026  while (!InterestingDecls.empty()) {
3027    DeclGroupRef DG(InterestingDecls.front());
3028    InterestingDecls.pop_front();
3029    Consumer->HandleInterestingDecl(DG);
3030  }
3031}
3032
3033void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
3034  this->Consumer = Consumer;
3035
3036  if (!Consumer)
3037    return;
3038
3039  for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
3040    // Force deserialization of this decl, which will cause it to be queued for
3041    // passing to the consumer.
3042    GetDecl(ExternalDefinitions[I]);
3043  }
3044
3045  PassInterestingDeclsToConsumer();
3046}
3047
3048void ASTReader::PrintStats() {
3049  std::fprintf(stderr, "*** AST File Statistics:\n");
3050
3051  unsigned NumTypesLoaded
3052    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
3053                                      QualType());
3054  unsigned NumDeclsLoaded
3055    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3056                                      (Decl *)0);
3057  unsigned NumIdentifiersLoaded
3058    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3059                                            IdentifiersLoaded.end(),
3060                                            (IdentifierInfo *)0);
3061  unsigned NumSelectorsLoaded
3062    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3063                                          SelectorsLoaded.end(),
3064                                          Selector());
3065
3066  std::fprintf(stderr, "  %u stat cache hits\n", NumStatHits);
3067  std::fprintf(stderr, "  %u stat cache misses\n", NumStatMisses);
3068  if (TotalNumSLocEntries)
3069    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
3070                 NumSLocEntriesRead, TotalNumSLocEntries,
3071                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
3072  if (!TypesLoaded.empty())
3073    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
3074                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3075                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3076  if (!DeclsLoaded.empty())
3077    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
3078                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3079                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
3080  if (!IdentifiersLoaded.empty())
3081    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
3082                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3083                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
3084  if (!SelectorsLoaded.empty())
3085    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
3086                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3087                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
3088  if (TotalNumStatements)
3089    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
3090                 NumStatementsRead, TotalNumStatements,
3091                 ((float)NumStatementsRead/TotalNumStatements * 100));
3092  if (TotalNumMacros)
3093    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
3094                 NumMacrosRead, TotalNumMacros,
3095                 ((float)NumMacrosRead/TotalNumMacros * 100));
3096  if (TotalLexicalDeclContexts)
3097    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
3098                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3099                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3100                  * 100));
3101  if (TotalVisibleDeclContexts)
3102    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
3103                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3104                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3105                  * 100));
3106  if (TotalNumMethodPoolEntries) {
3107    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
3108                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3109                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
3110                  * 100));
3111    std::fprintf(stderr, "  %u method pool misses\n", NumMethodPoolMisses);
3112  }
3113  std::fprintf(stderr, "\n");
3114}
3115
3116void ASTReader::InitializeSema(Sema &S) {
3117  SemaObj = &S;
3118  S.ExternalSource = this;
3119
3120  // Makes sure any declarations that were deserialized "too early"
3121  // still get added to the identifier's declaration chains.
3122  if (SemaObj->TUScope) {
3123    for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3124      SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3125      SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3126    }
3127  }
3128  PreloadedDecls.clear();
3129
3130  // If there were any tentative definitions, deserialize them and add
3131  // them to Sema's list of tentative definitions.
3132  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3133    VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
3134    SemaObj->TentativeDefinitions.push_back(Var);
3135  }
3136
3137  // If there were any unused file scoped decls, deserialize them and add to
3138  // Sema's list of unused file scoped decls.
3139  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3140    DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3141    SemaObj->UnusedFileScopedDecls.push_back(D);
3142  }
3143
3144  // If there were any weak undeclared identifiers, deserialize them and add to
3145  // Sema's list of weak undeclared identifiers.
3146  if (!WeakUndeclaredIdentifiers.empty()) {
3147    unsigned Idx = 0;
3148    for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3149      IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3150      IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3151      SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3152      bool Used = WeakUndeclaredIdentifiers[Idx++];
3153      Sema::WeakInfo WI(AliasId, Loc);
3154      WI.setUsed(Used);
3155      SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3156    }
3157  }
3158
3159  // If there were any locally-scoped external declarations,
3160  // deserialize them and add them to Sema's table of locally-scoped
3161  // external declarations.
3162  for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3163    NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3164    SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3165  }
3166
3167  // If there were any ext_vector type declarations, deserialize them
3168  // and add them to Sema's vector of such declarations.
3169  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3170    SemaObj->ExtVectorDecls.push_back(
3171                               cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
3172
3173  // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3174  // Can we cut them down before writing them ?
3175
3176  // If there were any VTable uses, deserialize the information and add it
3177  // to Sema's vector and map of VTable uses.
3178  if (!VTableUses.empty()) {
3179    unsigned Idx = 0;
3180    for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3181      CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3182      SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3183      bool DefinitionRequired = VTableUses[Idx++];
3184      SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3185      SemaObj->VTablesUsed[Class] = DefinitionRequired;
3186    }
3187  }
3188
3189  // If there were any dynamic classes declarations, deserialize them
3190  // and add them to Sema's vector of such declarations.
3191  for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3192    SemaObj->DynamicClasses.push_back(
3193                               cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
3194
3195  // If there were any pending implicit instantiations, deserialize them
3196  // and add them to Sema's queue of such instantiations.
3197  assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3198         "Expected pairs of entries");
3199  for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3200    ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3201    SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3202    SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3203  }
3204
3205  // Load the offsets of the declarations that Sema references.
3206  // They will be lazily deserialized when needed.
3207  if (!SemaDeclRefs.empty()) {
3208    assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3209    SemaObj->StdNamespace = SemaDeclRefs[0];
3210    SemaObj->StdBadAlloc = SemaDeclRefs[1];
3211  }
3212
3213  // If there are @selector references added them to its pool. This is for
3214  // implementation of -Wselector.
3215  if (!ReferencedSelectorsData.empty()) {
3216    unsigned int DataSize = ReferencedSelectorsData.size()-1;
3217    unsigned I = 0;
3218    while (I < DataSize) {
3219      Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
3220      SourceLocation SelLoc =
3221        SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
3222      SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3223    }
3224  }
3225}
3226
3227IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
3228  // Try to find this name within our on-disk hash tables. We start with the
3229  // most recent one, since that one contains the most up-to-date info.
3230  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3231    ASTIdentifierLookupTable *IdTable
3232        = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
3233    if (!IdTable)
3234      continue;
3235    std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
3236    ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
3237    if (Pos == IdTable->end())
3238      continue;
3239
3240    // Dereferencing the iterator has the effect of building the
3241    // IdentifierInfo node and populating it with the various
3242    // declarations it needs.
3243    return *Pos;
3244  }
3245  return 0;
3246}
3247
3248std::pair<ObjCMethodList, ObjCMethodList>
3249ASTReader::ReadMethodPool(Selector Sel) {
3250  // Find this selector in a hash table. We want to find the most recent entry.
3251  for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3252    PerFileData &F = *Chain[I];
3253    if (!F.SelectorLookupTable)
3254      continue;
3255
3256    ASTSelectorLookupTable *PoolTable
3257      = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3258    ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
3259    if (Pos != PoolTable->end()) {
3260      ++NumSelectorsRead;
3261      // FIXME: Not quite happy with the statistics here. We probably should
3262      // disable this tracking when called via LoadSelector.
3263      // Also, should entries without methods count as misses?
3264      ++NumMethodPoolEntriesRead;
3265      ASTSelectorLookupTrait::data_type Data = *Pos;
3266      if (DeserializationListener)
3267        DeserializationListener->SelectorRead(Data.ID, Sel);
3268      return std::make_pair(Data.Instance, Data.Factory);
3269    }
3270  }
3271
3272  ++NumMethodPoolMisses;
3273  return std::pair<ObjCMethodList, ObjCMethodList>();
3274}
3275
3276void ASTReader::LoadSelector(Selector Sel) {
3277  // It would be complicated to avoid reading the methods anyway. So don't.
3278  ReadMethodPool(Sel);
3279}
3280
3281void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
3282  assert(ID && "Non-zero identifier ID required");
3283  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
3284  IdentifiersLoaded[ID - 1] = II;
3285  if (DeserializationListener)
3286    DeserializationListener->IdentifierRead(ID, II);
3287}
3288
3289/// \brief Set the globally-visible declarations associated with the given
3290/// identifier.
3291///
3292/// If the AST reader is currently in a state where the given declaration IDs
3293/// cannot safely be resolved, they are queued until it is safe to resolve
3294/// them.
3295///
3296/// \param II an IdentifierInfo that refers to one or more globally-visible
3297/// declarations.
3298///
3299/// \param DeclIDs the set of declaration IDs with the name @p II that are
3300/// visible at global scope.
3301///
3302/// \param Nonrecursive should be true to indicate that the caller knows that
3303/// this call is non-recursive, and therefore the globally-visible declarations
3304/// will not be placed onto the pending queue.
3305void
3306ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
3307                              const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3308                                   bool Nonrecursive) {
3309  if (NumCurrentElementsDeserializing && !Nonrecursive) {
3310    PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3311    PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3312    PII.II = II;
3313    for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3314      PII.DeclIDs.push_back(DeclIDs[I]);
3315    return;
3316  }
3317
3318  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3319    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3320    if (SemaObj) {
3321      if (SemaObj->TUScope) {
3322        // Introduce this declaration into the translation-unit scope
3323        // and add it to the declaration chain for this identifier, so
3324        // that (unqualified) name lookup will find it.
3325        SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3326        SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3327      }
3328    } else {
3329      // Queue this declaration so that it will be added to the
3330      // translation unit scope and identifier's declaration chain
3331      // once a Sema object is known.
3332      PreloadedDecls.push_back(D);
3333    }
3334  }
3335}
3336
3337IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
3338  if (ID == 0)
3339    return 0;
3340
3341  if (IdentifiersLoaded.empty()) {
3342    Error("no identifier table in AST file");
3343    return 0;
3344  }
3345
3346  assert(PP && "Forgot to set Preprocessor ?");
3347  ID -= 1;
3348  if (!IdentifiersLoaded[ID]) {
3349    unsigned Index = ID;
3350    const char *Str = 0;
3351    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3352      PerFileData *F = Chain[N - I - 1];
3353      if (Index < F->LocalNumIdentifiers) {
3354         uint32_t Offset = F->IdentifierOffsets[Index];
3355         Str = F->IdentifierTableData + Offset;
3356         break;
3357      }
3358      Index -= F->LocalNumIdentifiers;
3359    }
3360    assert(Str && "Broken Chain");
3361
3362    // All of the strings in the AST file are preceded by a 16-bit length.
3363    // Extract that 16-bit length to avoid having to execute strlen().
3364    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3365    //  unsigned integers.  This is important to avoid integer overflow when
3366    //  we cast them to 'unsigned'.
3367    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
3368    unsigned StrLen = (((unsigned) StrLenPtr[0])
3369                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
3370    IdentifiersLoaded[ID]
3371      = &PP->getIdentifierTable().get(Str, StrLen);
3372    if (DeserializationListener)
3373      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
3374  }
3375
3376  return IdentifiersLoaded[ID];
3377}
3378
3379void ASTReader::ReadSLocEntry(unsigned ID) {
3380  ReadSLocEntryRecord(ID);
3381}
3382
3383Selector ASTReader::DecodeSelector(unsigned ID) {
3384  if (ID == 0)
3385    return Selector();
3386
3387  if (ID > SelectorsLoaded.size()) {
3388    Error("selector ID out of range in AST file");
3389    return Selector();
3390  }
3391
3392  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
3393    // Load this selector from the selector table.
3394    unsigned Idx = ID - 1;
3395    for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3396      PerFileData &F = *Chain[N - I - 1];
3397      if (Idx < F.LocalNumSelectors) {
3398        ASTSelectorLookupTrait Trait(*this);
3399        SelectorsLoaded[ID - 1] =
3400           Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3401        if (DeserializationListener)
3402          DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3403        break;
3404      }
3405      Idx -= F.LocalNumSelectors;
3406    }
3407  }
3408
3409  return SelectorsLoaded[ID - 1];
3410}
3411
3412Selector ASTReader::GetExternalSelector(uint32_t ID) {
3413  return DecodeSelector(ID);
3414}
3415
3416uint32_t ASTReader::GetNumExternalSelectors() {
3417  // ID 0 (the null selector) is considered an external selector.
3418  return getTotalNumSelectors() + 1;
3419}
3420
3421DeclarationName
3422ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
3423  DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3424  switch (Kind) {
3425  case DeclarationName::Identifier:
3426    return DeclarationName(GetIdentifierInfo(Record, Idx));
3427
3428  case DeclarationName::ObjCZeroArgSelector:
3429  case DeclarationName::ObjCOneArgSelector:
3430  case DeclarationName::ObjCMultiArgSelector:
3431    return DeclarationName(GetSelector(Record, Idx));
3432
3433  case DeclarationName::CXXConstructorName:
3434    return Context->DeclarationNames.getCXXConstructorName(
3435                          Context->getCanonicalType(GetType(Record[Idx++])));
3436
3437  case DeclarationName::CXXDestructorName:
3438    return Context->DeclarationNames.getCXXDestructorName(
3439                          Context->getCanonicalType(GetType(Record[Idx++])));
3440
3441  case DeclarationName::CXXConversionFunctionName:
3442    return Context->DeclarationNames.getCXXConversionFunctionName(
3443                          Context->getCanonicalType(GetType(Record[Idx++])));
3444
3445  case DeclarationName::CXXOperatorName:
3446    return Context->DeclarationNames.getCXXOperatorName(
3447                                       (OverloadedOperatorKind)Record[Idx++]);
3448
3449  case DeclarationName::CXXLiteralOperatorName:
3450    return Context->DeclarationNames.getCXXLiteralOperatorName(
3451                                       GetIdentifierInfo(Record, Idx));
3452
3453  case DeclarationName::CXXUsingDirective:
3454    return DeclarationName::getUsingDirectiveName();
3455  }
3456
3457  // Required to silence GCC warning
3458  return DeclarationName();
3459}
3460
3461TemplateName
3462ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
3463  TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3464  switch (Kind) {
3465  case TemplateName::Template:
3466    return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3467
3468  case TemplateName::OverloadedTemplate: {
3469    unsigned size = Record[Idx++];
3470    UnresolvedSet<8> Decls;
3471    while (size--)
3472      Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3473
3474    return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3475  }
3476
3477  case TemplateName::QualifiedTemplate: {
3478    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3479    bool hasTemplKeyword = Record[Idx++];
3480    TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3481    return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3482  }
3483
3484  case TemplateName::DependentTemplate: {
3485    NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3486    if (Record[Idx++])  // isIdentifier
3487      return Context->getDependentTemplateName(NNS,
3488                                               GetIdentifierInfo(Record, Idx));
3489    return Context->getDependentTemplateName(NNS,
3490                                         (OverloadedOperatorKind)Record[Idx++]);
3491  }
3492  }
3493
3494  assert(0 && "Unhandled template name kind!");
3495  return TemplateName();
3496}
3497
3498TemplateArgument
3499ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
3500                                const RecordData &Record, unsigned &Idx) {
3501  switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3502  case TemplateArgument::Null:
3503    return TemplateArgument();
3504  case TemplateArgument::Type:
3505    return TemplateArgument(GetType(Record[Idx++]));
3506  case TemplateArgument::Declaration:
3507    return TemplateArgument(GetDecl(Record[Idx++]));
3508  case TemplateArgument::Integral: {
3509    llvm::APSInt Value = ReadAPSInt(Record, Idx);
3510    QualType T = GetType(Record[Idx++]);
3511    return TemplateArgument(Value, T);
3512  }
3513  case TemplateArgument::Template:
3514    return TemplateArgument(ReadTemplateName(Record, Idx));
3515  case TemplateArgument::Expression:
3516    return TemplateArgument(ReadExpr(DeclsCursor));
3517  case TemplateArgument::Pack: {
3518    unsigned NumArgs = Record[Idx++];
3519    llvm::SmallVector<TemplateArgument, 8> Args;
3520    Args.reserve(NumArgs);
3521    while (NumArgs--)
3522      Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
3523    TemplateArgument TemplArg;
3524    TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3525    return TemplArg;
3526  }
3527  }
3528
3529  assert(0 && "Unhandled template argument kind!");
3530  return TemplateArgument();
3531}
3532
3533TemplateParameterList *
3534ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
3535  SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3536  SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3537  SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3538
3539  unsigned NumParams = Record[Idx++];
3540  llvm::SmallVector<NamedDecl *, 16> Params;
3541  Params.reserve(NumParams);
3542  while (NumParams--)
3543    Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3544
3545  TemplateParameterList* TemplateParams =
3546    TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3547                                  Params.data(), Params.size(), RAngleLoc);
3548  return TemplateParams;
3549}
3550
3551void
3552ASTReader::
3553ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
3554                         llvm::BitstreamCursor &DeclsCursor,
3555                         const RecordData &Record, unsigned &Idx) {
3556  unsigned NumTemplateArgs = Record[Idx++];
3557  TemplArgs.reserve(NumTemplateArgs);
3558  while (NumTemplateArgs--)
3559    TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
3560}
3561
3562/// \brief Read a UnresolvedSet structure.
3563void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
3564                                  const RecordData &Record, unsigned &Idx) {
3565  unsigned NumDecls = Record[Idx++];
3566  while (NumDecls--) {
3567    NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3568    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3569    Set.addDecl(D, AS);
3570  }
3571}
3572
3573CXXBaseSpecifier
3574ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
3575                                const RecordData &Record, unsigned &Idx) {
3576  bool isVirtual = static_cast<bool>(Record[Idx++]);
3577  bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3578  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
3579  TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
3580  SourceRange Range = ReadSourceRange(Record, Idx);
3581  return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
3582}
3583
3584std::pair<CXXBaseOrMemberInitializer **, unsigned>
3585ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
3586                                           const RecordData &Record,
3587                                           unsigned &Idx) {
3588  CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3589  unsigned NumInitializers = Record[Idx++];
3590  if (NumInitializers) {
3591    ASTContext &C = *getContext();
3592
3593    BaseOrMemberInitializers
3594        = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3595    for (unsigned i=0; i != NumInitializers; ++i) {
3596      TypeSourceInfo *BaseClassInfo = 0;
3597      bool IsBaseVirtual = false;
3598      FieldDecl *Member = 0;
3599
3600      bool IsBaseInitializer = Record[Idx++];
3601      if (IsBaseInitializer) {
3602        BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3603        IsBaseVirtual = Record[Idx++];
3604      } else {
3605        Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3606      }
3607      SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3608      Expr *Init = ReadExpr(Cursor);
3609      FieldDecl *AnonUnionMember
3610          = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3611      SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3612      SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3613      bool IsWritten = Record[Idx++];
3614      unsigned SourceOrderOrNumArrayIndices;
3615      llvm::SmallVector<VarDecl *, 8> Indices;
3616      if (IsWritten) {
3617        SourceOrderOrNumArrayIndices = Record[Idx++];
3618      } else {
3619        SourceOrderOrNumArrayIndices = Record[Idx++];
3620        Indices.reserve(SourceOrderOrNumArrayIndices);
3621        for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3622          Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3623      }
3624
3625      CXXBaseOrMemberInitializer *BOMInit;
3626      if (IsBaseInitializer) {
3627        BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3628                                                     IsBaseVirtual, LParenLoc,
3629                                                     Init, RParenLoc);
3630      } else if (IsWritten) {
3631        BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3632                                                     LParenLoc, Init, RParenLoc);
3633      } else {
3634        BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3635                                                     LParenLoc, Init, RParenLoc,
3636                                                     Indices.data(),
3637                                                     Indices.size());
3638      }
3639
3640      BOMInit->setAnonUnionMember(AnonUnionMember);
3641      BaseOrMemberInitializers[i] = BOMInit;
3642    }
3643  }
3644
3645  return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3646}
3647
3648NestedNameSpecifier *
3649ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
3650  unsigned N = Record[Idx++];
3651  NestedNameSpecifier *NNS = 0, *Prev = 0;
3652  for (unsigned I = 0; I != N; ++I) {
3653    NestedNameSpecifier::SpecifierKind Kind
3654      = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3655    switch (Kind) {
3656    case NestedNameSpecifier::Identifier: {
3657      IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3658      NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3659      break;
3660    }
3661
3662    case NestedNameSpecifier::Namespace: {
3663      NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3664      NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3665      break;
3666    }
3667
3668    case NestedNameSpecifier::TypeSpec:
3669    case NestedNameSpecifier::TypeSpecWithTemplate: {
3670      Type *T = GetType(Record[Idx++]).getTypePtr();
3671      bool Template = Record[Idx++];
3672      NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3673      break;
3674    }
3675
3676    case NestedNameSpecifier::Global: {
3677      NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3678      // No associated value, and there can't be a prefix.
3679      break;
3680    }
3681    }
3682    Prev = NNS;
3683  }
3684  return NNS;
3685}
3686
3687SourceRange
3688ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
3689  SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3690  SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3691  return SourceRange(beg, end);
3692}
3693
3694/// \brief Read an integral value
3695llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3696  unsigned BitWidth = Record[Idx++];
3697  unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3698  llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3699  Idx += NumWords;
3700  return Result;
3701}
3702
3703/// \brief Read a signed integral value
3704llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3705  bool isUnsigned = Record[Idx++];
3706  return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3707}
3708
3709/// \brief Read a floating-point value
3710llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
3711  return llvm::APFloat(ReadAPInt(Record, Idx));
3712}
3713
3714// \brief Read a string
3715std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
3716  unsigned Len = Record[Idx++];
3717  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
3718  Idx += Len;
3719  return Result;
3720}
3721
3722CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
3723                                          unsigned &Idx) {
3724  CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3725  return CXXTemporary::Create(*Context, Decl);
3726}
3727
3728DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
3729  return Diag(SourceLocation(), DiagID);
3730}
3731
3732DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
3733  return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
3734}
3735
3736/// \brief Retrieve the identifier table associated with the
3737/// preprocessor.
3738IdentifierTable &ASTReader::getIdentifierTable() {
3739  assert(PP && "Forgot to set Preprocessor ?");
3740  return PP->getIdentifierTable();
3741}
3742
3743/// \brief Record that the given ID maps to the given switch-case
3744/// statement.
3745void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3746  assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3747  SwitchCaseStmts[ID] = SC;
3748}
3749
3750/// \brief Retrieve the switch-case statement with the given ID.
3751SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
3752  assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3753  return SwitchCaseStmts[ID];
3754}
3755
3756/// \brief Record that the given label statement has been
3757/// deserialized and has the given ID.
3758void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3759  assert(LabelStmts.find(ID) == LabelStmts.end() &&
3760         "Deserialized label twice");
3761  LabelStmts[ID] = S;
3762
3763  // If we've already seen any goto statements that point to this
3764  // label, resolve them now.
3765  typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3766  std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3767  for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3768    Goto->second->setLabel(S);
3769  UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
3770
3771  // If we've already seen any address-label statements that point to
3772  // this label, resolve them now.
3773  typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3774  std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3775    = UnresolvedAddrLabelExprs.equal_range(ID);
3776  for (AddrLabelIter AddrLabel = AddrLabels.first;
3777       AddrLabel != AddrLabels.second; ++AddrLabel)
3778    AddrLabel->second->setLabel(S);
3779  UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
3780}
3781
3782/// \brief Set the label of the given statement to the label
3783/// identified by ID.
3784///
3785/// Depending on the order in which the label and other statements
3786/// referencing that label occur, this operation may complete
3787/// immediately (updating the statement) or it may queue the
3788/// statement to be back-patched later.
3789void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3790  std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3791  if (Label != LabelStmts.end()) {
3792    // We've already seen this label, so set the label of the goto and
3793    // we're done.
3794    S->setLabel(Label->second);
3795  } else {
3796    // We haven't seen this label yet, so add this goto to the set of
3797    // unresolved goto statements.
3798    UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3799  }
3800}
3801
3802/// \brief Set the label of the given expression to the label
3803/// identified by ID.
3804///
3805/// Depending on the order in which the label and other statements
3806/// referencing that label occur, this operation may complete
3807/// immediately (updating the statement) or it may queue the
3808/// statement to be back-patched later.
3809void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3810  std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3811  if (Label != LabelStmts.end()) {
3812    // We've already seen this label, so set the label of the
3813    // label-address expression and we're done.
3814    S->setLabel(Label->second);
3815  } else {
3816    // We haven't seen this label yet, so add this label-address
3817    // expression to the set of unresolved label-address expressions.
3818    UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3819  }
3820}
3821
3822void ASTReader::FinishedDeserializing() {
3823  assert(NumCurrentElementsDeserializing &&
3824         "FinishedDeserializing not paired with StartedDeserializing");
3825  if (NumCurrentElementsDeserializing == 1) {
3826    // If any identifiers with corresponding top-level declarations have
3827    // been loaded, load those declarations now.
3828    while (!PendingIdentifierInfos.empty()) {
3829      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3830                              PendingIdentifierInfos.front().DeclIDs, true);
3831      PendingIdentifierInfos.pop_front();
3832    }
3833
3834    // We are not in recursive loading, so it's safe to pass the "interesting"
3835    // decls to the consumer.
3836    if (Consumer)
3837      PassInterestingDeclsToConsumer();
3838  }
3839  --NumCurrentElementsDeserializing;
3840}
3841