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