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