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