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