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