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