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