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