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