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