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