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