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