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