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