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