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