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