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