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