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