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