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