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