1 //===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTReader class, which reads AST files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ASTReader.h"
15 #include "clang/Serialization/ASTDeserializationListener.h"
16 #include "ASTCommon.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Sema/Sema.h"
20 #include "clang/Sema/Scope.h"
21 #include "clang/AST/ASTConsumer.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/TypeLocVisitor.h"
29 #include "clang/Lex/MacroInfo.h"
30 #include "clang/Lex/PreprocessingRecord.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "clang/Lex/HeaderSearch.h"
33 #include "clang/Basic/OnDiskHashTable.h"
34 #include "clang/Basic/SourceManager.h"
35 #include "clang/Basic/SourceManagerInternals.h"
36 #include "clang/Basic/FileManager.h"
37 #include "clang/Basic/FileSystemStatCache.h"
38 #include "clang/Basic/TargetInfo.h"
39 #include "clang/Basic/Version.h"
40 #include "clang/Basic/VersionTuple.h"
41 #include "llvm/ADT/StringExtras.h"
42 #include "llvm/Bitcode/BitstreamReader.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/system_error.h"
53 using namespace clang;
54 using namespace clang::serialization;
56 //===----------------------------------------------------------------------===//
57 // PCH validator implementation
58 //===----------------------------------------------------------------------===//
60 ASTReaderListener::~ASTReaderListener() {}
63 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
64 const LangOptions &PPLangOpts = PP.getLangOptions();
65 #define PARSE_LANGOPT_BENIGN(Option)
66 #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
67 if (PPLangOpts.Option != LangOpts.Option) { \
68 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
72 PARSE_LANGOPT_BENIGN(Trigraphs);
73 PARSE_LANGOPT_BENIGN(BCPLComment);
74 PARSE_LANGOPT_BENIGN(DollarIdents);
75 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
76 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
77 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
78 PARSE_LANGOPT_BENIGN(ImplicitInt);
79 PARSE_LANGOPT_BENIGN(Digraphs);
80 PARSE_LANGOPT_BENIGN(HexFloats);
81 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
82 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
83 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
84 PARSE_LANGOPT_BENIGN(MSCVersion);
85 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
86 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
87 PARSE_LANGOPT_BENIGN(CXXOperatorName);
88 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
89 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
90 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
91 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
92 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
93 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
94 diag::warn_pch_objc_auto_properties);
95 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
96 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
97 diag::warn_pch_no_constant_cfstrings);
98 PARSE_LANGOPT_BENIGN(PascalStrings);
99 PARSE_LANGOPT_BENIGN(WritableStrings);
100 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
101 diag::warn_pch_lax_vector_conversions);
102 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
103 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
104 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
105 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
106 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
107 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
108 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
109 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
110 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
111 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
112 diag::warn_pch_thread_safe_statics);
113 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
114 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
115 PARSE_LANGOPT_BENIGN(EmitAllDecls);
116 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
117 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
118 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
119 diag::warn_pch_heinous_extensions);
120 // FIXME: Most of the options below are benign if the macro wasn't
121 // used. Unfortunately, this means that a PCH compiled without
122 // optimization can't be used with optimization turned on, even
123 // though the only thing that changes is whether __OPTIMIZE__ was
124 // defined... but if __OPTIMIZE__ never showed up in the header, it
125 // doesn't matter. We could consider making this some special kind
127 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
128 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
129 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
130 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
131 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
132 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
133 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
134 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
135 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
136 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
137 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
138 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
139 Reader.Diag(diag::warn_pch_gc_mode)
140 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
143 PARSE_LANGOPT_BENIGN(getVisibilityMode());
144 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
145 diag::warn_pch_stack_protector);
146 PARSE_LANGOPT_BENIGN(InstantiationDepth);
147 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
148 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
149 PARSE_LANGOPT_BENIGN(CatchUndefined);
150 PARSE_LANGOPT_BENIGN(DefaultFPContract);
151 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
152 PARSE_LANGOPT_BENIGN(SpellChecking);
153 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
154 PARSE_LANGOPT_BENIGN(ObjCInferRelatedReturnType);
155 #undef PARSE_LANGOPT_IMPORTANT
156 #undef PARSE_LANGOPT_BENIGN
161 bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
162 if (Triple == PP.getTargetInfo().getTriple().str())
165 Reader.Diag(diag::warn_pch_target_triple)
166 << Triple << PP.getTargetInfo().getTriple().str();
171 struct EmptyStringRef {
172 bool operator ()(llvm::StringRef r) const { return r.empty(); }
175 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
179 static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
180 PCHPredefinesBlocks R) {
181 // First, sum up the lengths.
182 unsigned LL = 0, RL = 0;
183 for (unsigned I = 0, N = L.size(); I != N; ++I) {
186 for (unsigned I = 0, N = R.size(); I != N; ++I) {
187 RL += R[I].Data.size();
191 if (LL == 0 && RL == 0)
194 // Kick out empty parts, they confuse the algorithm below.
195 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
196 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
198 // Do it the hard way. At this point, both vectors must be non-empty.
199 llvm::StringRef LR = L[0], RR = R[0].Data;
200 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
203 // Compare the current pieces.
204 if (LR.size() == RR.size()) {
205 // If they're the same length, it's pretty easy.
208 // Both pieces are done, advance.
211 // If either string is done, they're both done, since they're the same
214 assert(RI == RN && "Strings not the same length after all?");
219 } else if (LR.size() < RR.size()) {
220 // Right piece is longer.
221 if (!RR.startswith(LR))
224 assert(LI != LN && "Strings not the same length after all?");
225 RR = RR.substr(LR.size());
228 // Left piece is longer.
229 if (!LR.startswith(RR))
232 assert(RI != RN && "Strings not the same length after all?");
233 LR = LR.substr(RR.size());
239 static std::pair<FileID, llvm::StringRef::size_type>
240 FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
241 std::pair<FileID, llvm::StringRef::size_type> Res;
242 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
243 Res.second = Buffers[I].Data.find(MacroDef);
244 if (Res.second != llvm::StringRef::npos) {
245 Res.first = Buffers[I].BufferID;
252 bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
253 llvm::StringRef OriginalFileName,
254 std::string &SuggestedPredefines,
255 FileManager &FileMgr) {
256 // We are in the context of an implicit include, so the predefines buffer will
257 // have a #include entry for the PCH file itself (as normalized by the
258 // preprocessor initialization). Find it and skip over it in the checking
260 llvm::SmallString<256> PCHInclude;
261 PCHInclude += "#include \"";
262 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
263 PCHInclude += "\"\n";
264 std::pair<llvm::StringRef,llvm::StringRef> Split =
265 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
266 llvm::StringRef Left = Split.first, Right = Split.second;
267 if (Left == PP.getPredefines()) {
268 Error("Missing PCH include entry!");
272 // If the concatenation of all the PCH buffers is equal to the adjusted
273 // command line, we're done.
274 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
275 CommandLine.push_back(Left);
276 CommandLine.push_back(Right);
277 if (EqualConcatenations(CommandLine, Buffers))
280 SourceManager &SourceMgr = PP.getSourceManager();
282 // The predefines buffers are different. Determine what the differences are,
283 // and whether they require us to reject the PCH file.
284 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
285 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
286 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
288 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
289 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
291 // Pick out implicit #includes after the PCH and don't consider them for
292 // validation; we will insert them into SuggestedPredefines so that the
293 // preprocessor includes them.
294 std::string IncludesAfterPCH;
295 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
296 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
297 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
298 if (AfterPCHLines[i].startswith("#include ")) {
299 IncludesAfterPCH += AfterPCHLines[i];
300 IncludesAfterPCH += '\n';
302 CmdLineLines.push_back(AfterPCHLines[i]);
306 // Make sure we add the includes last into SuggestedPredefines before we
307 // exit this function.
308 struct AddIncludesRAII {
309 std::string &SuggestedPredefines;
310 std::string &IncludesAfterPCH;
312 AddIncludesRAII(std::string &SuggestedPredefines,
313 std::string &IncludesAfterPCH)
314 : SuggestedPredefines(SuggestedPredefines),
315 IncludesAfterPCH(IncludesAfterPCH) { }
317 SuggestedPredefines += IncludesAfterPCH;
319 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
321 // Sort both sets of predefined buffer lines, since we allow some extra
322 // definitions and they may appear at any point in the output.
323 std::sort(CmdLineLines.begin(), CmdLineLines.end());
324 std::sort(PCHLines.begin(), PCHLines.end());
326 // Determine which predefines that were used to build the PCH file are missing
327 // from the command line.
328 std::vector<llvm::StringRef> MissingPredefines;
329 std::set_difference(PCHLines.begin(), PCHLines.end(),
330 CmdLineLines.begin(), CmdLineLines.end(),
331 std::back_inserter(MissingPredefines));
333 bool MissingDefines = false;
334 bool ConflictingDefines = false;
335 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
336 llvm::StringRef Missing = MissingPredefines[I];
337 if (Missing.startswith("#include ")) {
338 // An -include was specified when generating the PCH; it is included in
339 // the PCH, just ignore it.
342 if (!Missing.startswith("#define ")) {
343 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
347 // This is a macro definition. Determine the name of the macro we're
349 std::string::size_type StartOfMacroName = strlen("#define ");
350 std::string::size_type EndOfMacroName
351 = Missing.find_first_of("( \n\r", StartOfMacroName);
352 assert(EndOfMacroName != std::string::npos &&
353 "Couldn't find the end of the macro name");
354 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
356 // Determine whether this macro was given a different definition on the
358 std::string MacroDefStart = "#define " + MacroName.str();
359 std::string::size_type MacroDefLen = MacroDefStart.size();
360 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
361 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
363 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
364 if (!ConflictPos->startswith(MacroDefStart)) {
365 // Different macro; we're done.
366 ConflictPos = CmdLineLines.end();
370 assert(ConflictPos->size() > MacroDefLen &&
371 "Invalid #define in predefines buffer?");
372 if ((*ConflictPos)[MacroDefLen] != ' ' &&
373 (*ConflictPos)[MacroDefLen] != '(')
374 continue; // Longer macro name; keep trying.
376 // We found a conflicting macro definition.
380 if (ConflictPos != CmdLineLines.end()) {
381 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
384 // Show the definition of this macro within the PCH file.
385 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
386 FindMacro(Buffers, Missing);
387 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
388 SourceLocation PCHMissingLoc =
389 SourceMgr.getLocForStartOfFile(MacroLoc.first)
390 .getFileLocWithOffset(MacroLoc.second);
391 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
393 ConflictingDefines = true;
397 // If the macro doesn't conflict, then we'll just pick up the macro
398 // definition from the PCH file. Warn the user that they made a mistake.
399 if (ConflictingDefines)
400 continue; // Don't complain if there are already conflicting defs
402 if (!MissingDefines) {
403 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
404 MissingDefines = true;
407 // Show the definition of this macro within the PCH file.
408 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
409 FindMacro(Buffers, Missing);
410 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
411 SourceLocation PCHMissingLoc =
412 SourceMgr.getLocForStartOfFile(MacroLoc.first)
413 .getFileLocWithOffset(MacroLoc.second);
414 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
417 if (ConflictingDefines)
420 // Determine what predefines were introduced based on command-line
421 // parameters that were not present when building the PCH
422 // file. Extra #defines are okay, so long as the identifiers being
423 // defined were not used within the precompiled header.
424 std::vector<llvm::StringRef> ExtraPredefines;
425 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
426 PCHLines.begin(), PCHLines.end(),
427 std::back_inserter(ExtraPredefines));
428 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
429 llvm::StringRef &Extra = ExtraPredefines[I];
430 if (!Extra.startswith("#define ")) {
431 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
435 // This is an extra macro definition. Determine the name of the
436 // macro we're defining.
437 std::string::size_type StartOfMacroName = strlen("#define ");
438 std::string::size_type EndOfMacroName
439 = Extra.find_first_of("( \n\r", StartOfMacroName);
440 assert(EndOfMacroName != std::string::npos &&
441 "Couldn't find the end of the macro name");
442 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
444 // Check whether this name was used somewhere in the PCH file. If
445 // so, defining it as a macro could change behavior, so we reject
447 if (IdentifierInfo *II = Reader.get(MacroName)) {
448 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
452 // Add this definition to the suggested predefines buffer.
453 SuggestedPredefines += Extra;
454 SuggestedPredefines += '\n';
457 // If we get here, it's because the predefines buffer had compatible
458 // contents. Accept the PCH file.
462 void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
464 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
468 void PCHValidator::ReadCounter(unsigned Value) {
469 PP.setCounterValue(Value);
472 //===----------------------------------------------------------------------===//
473 // AST reader implementation
474 //===----------------------------------------------------------------------===//
477 ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
478 DeserializationListener = Listener;
483 class ASTSelectorLookupTrait {
489 ObjCMethodList Instance, Factory;
492 typedef Selector external_key_type;
493 typedef external_key_type internal_key_type;
495 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
497 static bool EqualKey(const internal_key_type& a,
498 const internal_key_type& b) {
502 static unsigned ComputeHash(Selector Sel) {
503 return serialization::ComputeHash(Sel);
506 // This hopefully will just get inlined and removed by the optimizer.
507 static const internal_key_type&
508 GetInternalKey(const external_key_type& x) { return x; }
510 static std::pair<unsigned, unsigned>
511 ReadKeyDataLength(const unsigned char*& d) {
512 using namespace clang::io;
513 unsigned KeyLen = ReadUnalignedLE16(d);
514 unsigned DataLen = ReadUnalignedLE16(d);
515 return std::make_pair(KeyLen, DataLen);
518 internal_key_type ReadKey(const unsigned char* d, unsigned) {
519 using namespace clang::io;
520 SelectorTable &SelTable = Reader.getContext()->Selectors;
521 unsigned N = ReadUnalignedLE16(d);
522 IdentifierInfo *FirstII
523 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
525 return SelTable.getNullarySelector(FirstII);
527 return SelTable.getUnarySelector(FirstII);
529 llvm::SmallVector<IdentifierInfo *, 16> Args;
530 Args.push_back(FirstII);
531 for (unsigned I = 1; I != N; ++I)
532 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
534 return SelTable.getSelector(N, Args.data());
537 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
538 using namespace clang::io;
542 Result.ID = ReadUnalignedLE32(d);
543 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
544 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
546 // Load instance methods
547 ObjCMethodList *Prev = 0;
548 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
549 ObjCMethodDecl *Method
550 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
551 if (!Result.Instance.Method) {
552 // This is the first method, which is the easy case.
553 Result.Instance.Method = Method;
554 Prev = &Result.Instance;
558 ObjCMethodList *Mem =
559 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
560 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
564 // Load factory methods
566 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
567 ObjCMethodDecl *Method
568 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
569 if (!Result.Factory.Method) {
570 // This is the first method, which is the easy case.
571 Result.Factory.Method = Method;
572 Prev = &Result.Factory;
576 ObjCMethodList *Mem =
577 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
578 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
586 } // end anonymous namespace
588 /// \brief The on-disk hash table used for the global method pool.
589 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
590 ASTSelectorLookupTable;
593 class ASTIdentifierLookupTrait {
595 ASTReader::PerFileData &F;
597 // If we know the IdentifierInfo in advance, it is here and we will
598 // not build a new one. Used when deserializing information about an
599 // identifier that was constructed before the AST file was read.
600 IdentifierInfo *KnownII;
603 typedef IdentifierInfo * data_type;
605 typedef const std::pair<const char*, unsigned> external_key_type;
607 typedef external_key_type internal_key_type;
609 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
610 IdentifierInfo *II = 0)
611 : Reader(Reader), F(F), KnownII(II) { }
613 static bool EqualKey(const internal_key_type& a,
614 const internal_key_type& b) {
615 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
619 static unsigned ComputeHash(const internal_key_type& a) {
620 return llvm::HashString(llvm::StringRef(a.first, a.second));
623 // This hopefully will just get inlined and removed by the optimizer.
624 static const internal_key_type&
625 GetInternalKey(const external_key_type& x) { return x; }
627 // This hopefully will just get inlined and removed by the optimizer.
628 static const external_key_type&
629 GetExternalKey(const internal_key_type& x) { return x; }
631 static std::pair<unsigned, unsigned>
632 ReadKeyDataLength(const unsigned char*& d) {
633 using namespace clang::io;
634 unsigned DataLen = ReadUnalignedLE16(d);
635 unsigned KeyLen = ReadUnalignedLE16(d);
636 return std::make_pair(KeyLen, DataLen);
639 static std::pair<const char*, unsigned>
640 ReadKey(const unsigned char* d, unsigned n) {
641 assert(n >= 2 && d[n-1] == '\0');
642 return std::make_pair((const char*) d, n-1);
645 IdentifierInfo *ReadData(const internal_key_type& k,
646 const unsigned char* d,
648 using namespace clang::io;
649 IdentID ID = ReadUnalignedLE32(d);
650 bool IsInteresting = ID & 0x01;
652 // Wipe out the "is interesting" bit.
655 if (!IsInteresting) {
656 // For uninteresting identifiers, just build the IdentifierInfo
657 // and associate it with the persistent ID.
658 IdentifierInfo *II = KnownII;
660 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
662 Reader.SetIdentifierInfo(ID, II);
667 unsigned Bits = ReadUnalignedLE16(d);
668 bool CPlusPlusOperatorKeyword = Bits & 0x01;
670 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
672 bool Poisoned = Bits & 0x01;
674 bool ExtensionToken = Bits & 0x01;
676 bool hasMacroDefinition = Bits & 0x01;
678 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
681 assert(Bits == 0 && "Extra bits in the identifier?");
684 // Build the IdentifierInfo itself and link the identifier ID with
685 // the new IdentifierInfo.
686 IdentifierInfo *II = KnownII;
688 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
690 Reader.SetIdentifierInfo(ID, II);
692 // Set or check the various bits in the IdentifierInfo structure.
693 // Token IDs are read-only.
694 if (HasRevertedTokenIDToIdentifier)
695 II->RevertTokenIDToIdentifier();
696 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
697 assert(II->isExtensionToken() == ExtensionToken &&
698 "Incorrect extension token flag");
699 (void)ExtensionToken;
700 II->setIsPoisoned(Poisoned);
701 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
702 "Incorrect C++ operator keyword flag");
703 (void)CPlusPlusOperatorKeyword;
705 // If this identifier is a macro, deserialize the macro
707 if (hasMacroDefinition) {
708 uint32_t Offset = ReadUnalignedLE32(d);
709 Reader.SetIdentifierIsMacro(II, F, Offset);
713 // Read all of the declarations visible at global scope with this
715 if (Reader.getContext() == 0) return II;
717 llvm::SmallVector<uint32_t, 4> DeclIDs;
718 for (; DataLen > 0; DataLen -= 4)
719 DeclIDs.push_back(ReadUnalignedLE32(d));
720 Reader.SetGloballyVisibleDecls(II, DeclIDs);
728 } // end anonymous namespace
730 /// \brief The on-disk hash table used to contain information about
731 /// all of the identifiers in the program.
732 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
733 ASTIdentifierLookupTable;
736 class ASTDeclContextNameLookupTrait {
740 /// \brief Pair of begin/end iterators for DeclIDs.
741 typedef std::pair<DeclID *, DeclID *> data_type;
743 /// \brief Special internal key for declaration names.
744 /// The hash table creates keys for comparison; we do not create
745 /// a DeclarationName for the internal key to avoid deserializing types.
747 DeclarationName::NameKind Kind;
749 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
752 typedef DeclarationName external_key_type;
753 typedef DeclNameKey internal_key_type;
755 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
757 static bool EqualKey(const internal_key_type& a,
758 const internal_key_type& b) {
759 return a.Kind == b.Kind && a.Data == b.Data;
762 unsigned ComputeHash(const DeclNameKey &Key) const {
763 llvm::FoldingSetNodeID ID;
764 ID.AddInteger(Key.Kind);
767 case DeclarationName::Identifier:
768 case DeclarationName::CXXLiteralOperatorName:
769 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
771 case DeclarationName::ObjCZeroArgSelector:
772 case DeclarationName::ObjCOneArgSelector:
773 case DeclarationName::ObjCMultiArgSelector:
774 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
776 case DeclarationName::CXXConstructorName:
777 case DeclarationName::CXXDestructorName:
778 case DeclarationName::CXXConversionFunctionName:
779 ID.AddInteger((TypeID)Key.Data);
781 case DeclarationName::CXXOperatorName:
782 ID.AddInteger((OverloadedOperatorKind)Key.Data);
784 case DeclarationName::CXXUsingDirective:
788 return ID.ComputeHash();
791 internal_key_type GetInternalKey(const external_key_type& Name) const {
793 Key.Kind = Name.getNameKind();
794 switch (Name.getNameKind()) {
795 case DeclarationName::Identifier:
796 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
798 case DeclarationName::ObjCZeroArgSelector:
799 case DeclarationName::ObjCOneArgSelector:
800 case DeclarationName::ObjCMultiArgSelector:
801 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
803 case DeclarationName::CXXConstructorName:
804 case DeclarationName::CXXDestructorName:
805 case DeclarationName::CXXConversionFunctionName:
806 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
808 case DeclarationName::CXXOperatorName:
809 Key.Data = Name.getCXXOverloadedOperator();
811 case DeclarationName::CXXLiteralOperatorName:
812 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
814 case DeclarationName::CXXUsingDirective:
821 external_key_type GetExternalKey(const internal_key_type& Key) const {
822 ASTContext *Context = Reader.getContext();
824 case DeclarationName::Identifier:
825 return DeclarationName((IdentifierInfo*)Key.Data);
827 case DeclarationName::ObjCZeroArgSelector:
828 case DeclarationName::ObjCOneArgSelector:
829 case DeclarationName::ObjCMultiArgSelector:
830 return DeclarationName(Selector(Key.Data));
832 case DeclarationName::CXXConstructorName:
833 return Context->DeclarationNames.getCXXConstructorName(
834 Context->getCanonicalType(Reader.GetType(Key.Data)));
836 case DeclarationName::CXXDestructorName:
837 return Context->DeclarationNames.getCXXDestructorName(
838 Context->getCanonicalType(Reader.GetType(Key.Data)));
840 case DeclarationName::CXXConversionFunctionName:
841 return Context->DeclarationNames.getCXXConversionFunctionName(
842 Context->getCanonicalType(Reader.GetType(Key.Data)));
844 case DeclarationName::CXXOperatorName:
845 return Context->DeclarationNames.getCXXOperatorName(
846 (OverloadedOperatorKind)Key.Data);
848 case DeclarationName::CXXLiteralOperatorName:
849 return Context->DeclarationNames.getCXXLiteralOperatorName(
850 (IdentifierInfo*)Key.Data);
852 case DeclarationName::CXXUsingDirective:
853 return DeclarationName::getUsingDirectiveName();
856 llvm_unreachable("Invalid Name Kind ?");
859 static std::pair<unsigned, unsigned>
860 ReadKeyDataLength(const unsigned char*& d) {
861 using namespace clang::io;
862 unsigned KeyLen = ReadUnalignedLE16(d);
863 unsigned DataLen = ReadUnalignedLE16(d);
864 return std::make_pair(KeyLen, DataLen);
867 internal_key_type ReadKey(const unsigned char* d, unsigned) {
868 using namespace clang::io;
871 Key.Kind = (DeclarationName::NameKind)*d++;
873 case DeclarationName::Identifier:
874 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
876 case DeclarationName::ObjCZeroArgSelector:
877 case DeclarationName::ObjCOneArgSelector:
878 case DeclarationName::ObjCMultiArgSelector:
880 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
882 case DeclarationName::CXXConstructorName:
883 case DeclarationName::CXXDestructorName:
884 case DeclarationName::CXXConversionFunctionName:
885 Key.Data = ReadUnalignedLE32(d); // TypeID
887 case DeclarationName::CXXOperatorName:
888 Key.Data = *d++; // OverloadedOperatorKind
890 case DeclarationName::CXXLiteralOperatorName:
891 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
893 case DeclarationName::CXXUsingDirective:
900 data_type ReadData(internal_key_type, const unsigned char* d,
902 using namespace clang::io;
903 unsigned NumDecls = ReadUnalignedLE16(d);
904 DeclID *Start = (DeclID *)d;
905 return std::make_pair(Start, Start + NumDecls);
909 } // end anonymous namespace
911 /// \brief The on-disk hash table used for the DeclContext's Name lookup table.
912 typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
913 ASTDeclContextNameLookupTable;
915 bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
916 const std::pair<uint64_t, uint64_t> &Offsets,
917 DeclContextInfo &Info) {
918 SavedStreamPosition SavedPosition(Cursor);
919 // First the lexical decls.
920 if (Offsets.first != 0) {
921 Cursor.JumpToBit(Offsets.first);
926 unsigned Code = Cursor.ReadCode();
927 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
928 if (RecCode != DECL_CONTEXT_LEXICAL) {
929 Error("Expected lexical block");
933 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
934 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
936 Info.LexicalDecls = 0;
937 Info.NumLexicalDecls = 0;
940 // Now the lookup table.
941 if (Offsets.second != 0) {
942 Cursor.JumpToBit(Offsets.second);
947 unsigned Code = Cursor.ReadCode();
948 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
949 if (RecCode != DECL_CONTEXT_VISIBLE) {
950 Error("Expected visible lookup table block");
953 Info.NameLookupTableData
954 = ASTDeclContextNameLookupTable::Create(
955 (const unsigned char *)Blob + Record[0],
956 (const unsigned char *)Blob,
957 ASTDeclContextNameLookupTrait(*this));
959 Info.NameLookupTableData = 0;
965 void ASTReader::Error(llvm::StringRef Msg) {
966 Error(diag::err_fe_pch_malformed, Msg);
969 void ASTReader::Error(unsigned DiagID,
970 llvm::StringRef Arg1, llvm::StringRef Arg2) {
971 if (Diags.isDiagnosticInFlight())
972 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
974 Diag(DiagID) << Arg1 << Arg2;
977 /// \brief Tell the AST listener about the predefines buffers in the chain.
978 bool ASTReader::CheckPredefinesBuffers() {
980 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
981 ActualOriginalFileName,
987 //===----------------------------------------------------------------------===//
988 // Source Manager Deserialization
989 //===----------------------------------------------------------------------===//
991 /// \brief Read the line table in the source manager block.
992 /// \returns true if there was an error.
993 bool ASTReader::ParseLineTable(PerFileData &F,
994 llvm::SmallVectorImpl<uint64_t> &Record) {
996 LineTableInfo &LineTable = SourceMgr.getLineTable();
998 // Parse the file names
999 std::map<int, int> FileIDs;
1000 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1001 // Extract the file name
1002 unsigned FilenameLen = Record[Idx++];
1003 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1005 MaybeAddSystemRootToFilename(Filename);
1006 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1009 // Parse the line entries
1010 std::vector<LineEntry> Entries;
1011 while (Idx < Record.size()) {
1012 int FID = Record[Idx++];
1014 // Extract the line entries
1015 unsigned NumEntries = Record[Idx++];
1016 assert(NumEntries && "Numentries is 00000");
1018 Entries.reserve(NumEntries);
1019 for (unsigned I = 0; I != NumEntries; ++I) {
1020 unsigned FileOffset = Record[Idx++];
1021 unsigned LineNo = Record[Idx++];
1022 int FilenameID = FileIDs[Record[Idx++]];
1023 SrcMgr::CharacteristicKind FileKind
1024 = (SrcMgr::CharacteristicKind)Record[Idx++];
1025 unsigned IncludeOffset = Record[Idx++];
1026 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1027 FileKind, IncludeOffset));
1029 LineTable.AddEntry(FID, Entries);
1045 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
1046 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1049 class ASTStatLookupTrait {
1051 typedef const char *external_key_type;
1052 typedef const char *internal_key_type;
1054 typedef ASTStatData data_type;
1056 static unsigned ComputeHash(const char *path) {
1057 return llvm::HashString(path);
1060 static internal_key_type GetInternalKey(const char *path) { return path; }
1062 static bool EqualKey(internal_key_type a, internal_key_type b) {
1063 return strcmp(a, b) == 0;
1066 static std::pair<unsigned, unsigned>
1067 ReadKeyDataLength(const unsigned char*& d) {
1068 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1069 unsigned DataLen = (unsigned) *d++;
1070 return std::make_pair(KeyLen + 1, DataLen);
1073 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1074 return (const char *)d;
1077 static data_type ReadData(const internal_key_type, const unsigned char *d,
1078 unsigned /*DataLen*/) {
1079 using namespace clang::io;
1081 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1082 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1083 mode_t mode = (mode_t) ReadUnalignedLE16(d);
1084 time_t mtime = (time_t) ReadUnalignedLE64(d);
1085 off_t size = (off_t) ReadUnalignedLE64(d);
1086 return data_type(ino, dev, mode, mtime, size);
1090 /// \brief stat() cache for precompiled headers.
1092 /// This cache is very similar to the stat cache used by pretokenized
1094 class ASTStatCache : public FileSystemStatCache {
1095 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
1098 unsigned &NumStatHits, &NumStatMisses;
1100 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1101 unsigned &NumStatHits, unsigned &NumStatMisses)
1102 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1103 Cache = CacheTy::Create(Buckets, Base);
1106 ~ASTStatCache() { delete Cache; }
1108 LookupResult getStat(const char *Path, struct stat &StatBuf,
1109 int *FileDescriptor) {
1110 // Do the lookup for the file's data in the AST file.
1111 CacheTy::iterator I = Cache->find(Path);
1113 // If we don't get a hit in the AST file just forward to 'stat'.
1114 if (I == Cache->end()) {
1116 return statChained(Path, StatBuf, FileDescriptor);
1120 ASTStatData Data = *I;
1122 StatBuf.st_ino = Data.ino;
1123 StatBuf.st_dev = Data.dev;
1124 StatBuf.st_mtime = Data.mtime;
1125 StatBuf.st_mode = Data.mode;
1126 StatBuf.st_size = Data.size;
1130 } // end anonymous namespace
1133 /// \brief Read a source manager block
1134 ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
1135 using namespace SrcMgr;
1137 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1139 // Set the source-location entry cursor to the current position in
1140 // the stream. This cursor will be used to read the contents of the
1141 // source manager block initially, and then lazily read
1142 // source-location entries as needed.
1143 SLocEntryCursor = F.Stream;
1145 // The stream itself is going to skip over the source manager block.
1146 if (F.Stream.SkipBlock()) {
1147 Error("malformed block record in AST file");
1151 // Enter the source manager block.
1152 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1153 Error("malformed source manager block record in AST file");
1159 unsigned Code = SLocEntryCursor.ReadCode();
1160 if (Code == llvm::bitc::END_BLOCK) {
1161 if (SLocEntryCursor.ReadBlockEnd()) {
1162 Error("error at end of Source Manager block in AST file");
1168 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1169 // No known subblocks, always skip them.
1170 SLocEntryCursor.ReadSubBlockID();
1171 if (SLocEntryCursor.SkipBlock()) {
1172 Error("malformed block record in AST file");
1178 if (Code == llvm::bitc::DEFINE_ABBREV) {
1179 SLocEntryCursor.ReadAbbrevRecord();
1184 const char *BlobStart;
1187 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1188 default: // Default behavior: ignore.
1192 if (ParseLineTable(F, Record))
1196 case SM_SLOC_FILE_ENTRY:
1197 case SM_SLOC_BUFFER_ENTRY:
1198 case SM_SLOC_EXPANSION_ENTRY:
1199 // Once we hit one of the source location entries, we're done.
1205 /// \brief If a header file is not found at the path that we expect it to be
1206 /// and the PCH file was moved from its original location, try to resolve the
1207 /// file by assuming that header+PCH were moved together and the header is in
1208 /// the same place relative to the PCH.
1210 resolveFileRelativeToOriginalDir(const std::string &Filename,
1211 const std::string &OriginalDir,
1212 const std::string &CurrDir) {
1213 assert(OriginalDir != CurrDir &&
1214 "No point trying to resolve the file if the PCH dir didn't change");
1215 using namespace llvm::sys;
1216 llvm::SmallString<128> filePath(Filename);
1217 fs::make_absolute(filePath);
1218 assert(path::is_absolute(OriginalDir));
1219 llvm::SmallString<128> currPCHPath(CurrDir);
1221 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1222 fileDirE = path::end(path::parent_path(filePath));
1223 path::const_iterator origDirI = path::begin(OriginalDir),
1224 origDirE = path::end(OriginalDir);
1225 // Skip the common path components from filePath and OriginalDir.
1226 while (fileDirI != fileDirE && origDirI != origDirE &&
1227 *fileDirI == *origDirI) {
1231 for (; origDirI != origDirE; ++origDirI)
1232 path::append(currPCHPath, "..");
1233 path::append(currPCHPath, fileDirI, fileDirE);
1234 path::append(currPCHPath, path::filename(Filename));
1235 return currPCHPath.str();
1238 /// \brief Get a cursor that's correctly positioned for reading the source
1239 /// location entry with the given ID.
1240 ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
1241 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1242 "SLocCursorForID should only be called for real IDs.");
1246 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1247 F = Chain[N - I - 1];
1248 if (ID < F->LocalNumSLocEntries)
1250 ID -= F->LocalNumSLocEntries;
1252 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1254 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1258 /// \brief Read in the source location entry with the given ID.
1259 ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
1263 if (ID > TotalNumSLocEntries) {
1264 Error("source location entry ID out-of-range for AST file");
1268 PerFileData *F = SLocCursorForID(ID);
1269 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1271 ++NumSLocEntriesRead;
1272 unsigned Code = SLocEntryCursor.ReadCode();
1273 if (Code == llvm::bitc::END_BLOCK ||
1274 Code == llvm::bitc::ENTER_SUBBLOCK ||
1275 Code == llvm::bitc::DEFINE_ABBREV) {
1276 Error("incorrectly-formatted source location entry in AST file");
1281 const char *BlobStart;
1283 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1285 Error("incorrectly-formatted source location entry in AST file");
1288 case SM_SLOC_FILE_ENTRY: {
1289 std::string Filename(BlobStart, BlobStart + BlobLen);
1290 MaybeAddSystemRootToFilename(Filename);
1291 const FileEntry *File = FileMgr.getFile(Filename);
1292 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1293 OriginalDir != CurrentDir) {
1294 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1297 if (!resolved.empty())
1298 File = FileMgr.getFile(resolved);
1301 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1304 std::string ErrorStr = "could not find file '";
1305 ErrorStr += Filename;
1306 ErrorStr += "' referenced by AST file";
1307 Error(ErrorStr.c_str());
1311 if (Record.size() < 6) {
1312 Error("source location entry is incorrect");
1316 if (!DisableValidation &&
1317 ((off_t)Record[4] != File->getSize()
1318 #if !defined(LLVM_ON_WIN32)
1319 // In our regression testing, the Windows file system seems to
1320 // have inconsistent modification times that sometimes
1321 // erroneously trigger this error-handling path.
1322 || (time_t)Record[5] != File->getModificationTime()
1325 Error(diag::err_fe_pch_file_modified, Filename);
1329 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1330 (SrcMgr::CharacteristicKind)Record[2],
1333 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1334 .setHasLineDirectives();
1339 case SM_SLOC_BUFFER_ENTRY: {
1340 const char *Name = BlobStart;
1341 unsigned Offset = Record[0];
1342 unsigned Code = SLocEntryCursor.ReadCode();
1345 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1347 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1348 Error("AST record has invalid code");
1352 llvm::MemoryBuffer *Buffer
1353 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1355 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
1357 if (strcmp(Name, "<built-in>") == 0) {
1358 PCHPredefinesBlock Block = {
1360 llvm::StringRef(BlobStart, BlobLen - 1)
1362 PCHPredefinesBuffers.push_back(Block);
1368 case SM_SLOC_EXPANSION_ENTRY: {
1369 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1370 SourceMgr.createInstantiationLoc(SpellingLoc,
1371 ReadSourceLocation(*F, Record[2]),
1372 ReadSourceLocation(*F, Record[3]),
1383 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1384 /// specified cursor. Read the abbreviations that are at the top of the block
1385 /// and then leave the cursor pointing into the block.
1386 bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1388 if (Cursor.EnterSubBlock(BlockID)) {
1389 Error("malformed block record in AST file");
1394 uint64_t Offset = Cursor.GetCurrentBitNo();
1395 unsigned Code = Cursor.ReadCode();
1397 // We expect all abbrevs to be at the start of the block.
1398 if (Code != llvm::bitc::DEFINE_ABBREV) {
1399 Cursor.JumpToBit(Offset);
1402 Cursor.ReadAbbrevRecord();
1406 PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
1407 assert(PP && "Forgot to set Preprocessor ?");
1408 llvm::BitstreamCursor &Stream = F.MacroCursor;
1410 // Keep track of where we are in the stream, then jump back there
1411 // after reading this macro.
1412 SavedStreamPosition SavedPosition(Stream);
1414 Stream.JumpToBit(Offset);
1416 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1417 MacroInfo *Macro = 0;
1420 unsigned Code = Stream.ReadCode();
1422 case llvm::bitc::END_BLOCK:
1425 case llvm::bitc::ENTER_SUBBLOCK:
1426 // No known subblocks, always skip them.
1427 Stream.ReadSubBlockID();
1428 if (Stream.SkipBlock()) {
1429 Error("malformed block record in AST file");
1434 case llvm::bitc::DEFINE_ABBREV:
1435 Stream.ReadAbbrevRecord();
1441 const char *BlobStart = 0;
1442 unsigned BlobLen = 0;
1444 PreprocessorRecordTypes RecType =
1445 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
1448 case PP_MACRO_OBJECT_LIKE:
1449 case PP_MACRO_FUNCTION_LIKE: {
1450 // If we already have a macro, that means that we've hit the end
1451 // of the definition of the macro we were looking for. We're
1456 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1458 Error("macro must have a name in AST file");
1461 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
1462 bool isUsed = Record[2];
1464 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
1465 MI->setIsUsed(isUsed);
1468 unsigned NextIndex = 3;
1469 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1470 // Decode function-like macro info.
1471 bool isC99VarArgs = Record[3];
1472 bool isGNUVarArgs = Record[4];
1474 unsigned NumArgs = Record[5];
1475 NextIndex = 6 + NumArgs;
1476 for (unsigned i = 0; i != NumArgs; ++i)
1477 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1479 // Install function-like macro info.
1480 MI->setIsFunctionLike();
1481 if (isC99VarArgs) MI->setIsC99Varargs();
1482 if (isGNUVarArgs) MI->setIsGNUVarargs();
1483 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1484 PP->getPreprocessorAllocator());
1487 // Finally, install the macro.
1488 PP->setMacroInfo(II, MI);
1490 // Remember that we saw this macro last so that we add the tokens that
1491 // form its body to it.
1494 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1495 // We have a macro definition. Load it now.
1496 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1497 getMacroDefinition(Record[NextIndex]));
1505 // If we see a TOKEN before a PP_MACRO_*, then the file is
1506 // erroneous, just pretend we didn't see this.
1507 if (Macro == 0) break;
1511 Tok.setLocation(ReadSourceLocation(F, Record[0]));
1512 Tok.setLength(Record[1]);
1513 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1514 Tok.setIdentifierInfo(II);
1515 Tok.setKind((tok::TokenKind)Record[3]);
1516 Tok.setFlag((Token::TokenFlags)Record[4]);
1517 Macro->AddTokenToBody(Tok);
1526 PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1527 assert(PP && "Forgot to set Preprocessor ?");
1528 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1530 case llvm::bitc::END_BLOCK:
1533 case llvm::bitc::ENTER_SUBBLOCK:
1534 Error("unexpected subblock record in preprocessor detail block");
1537 case llvm::bitc::DEFINE_ABBREV:
1538 Error("unexpected abbrevation record in preprocessor detail block");
1545 if (!PP->getPreprocessingRecord()) {
1546 Error("no preprocessing record");
1551 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1552 const char *BlobStart = 0;
1553 unsigned BlobLen = 0;
1555 PreprocessorDetailRecordTypes RecType =
1556 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1557 Code, Record, BlobStart, BlobLen);
1559 case PPD_MACRO_EXPANSION: {
1560 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1563 MacroExpansion *ME =
1564 new (PPRec) MacroExpansion(DecodeIdentifierInfo(Record[3]),
1565 SourceRange(ReadSourceLocation(F, Record[1]),
1566 ReadSourceLocation(F, Record[2])),
1567 getMacroDefinition(Record[4]));
1568 PPRec.SetPreallocatedEntity(Record[0], ME);
1572 case PPD_MACRO_DEFINITION: {
1573 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1576 if (Record[1] > MacroDefinitionsLoaded.size()) {
1577 Error("out-of-bounds macro definition record");
1581 // Decode the identifier info and then check again; if the macro is
1582 // still defined and associated with the identifier,
1583 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1584 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1586 = new (PPRec) MacroDefinition(II,
1587 ReadSourceLocation(F, Record[5]),
1589 ReadSourceLocation(F, Record[2]),
1590 ReadSourceLocation(F, Record[3])));
1592 PPRec.SetPreallocatedEntity(Record[0], MD);
1593 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1595 if (DeserializationListener)
1596 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1599 return MacroDefinitionsLoaded[Record[1] - 1];
1602 case PPD_INCLUSION_DIRECTIVE: {
1603 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1606 const char *FullFileNameStart = BlobStart + Record[3];
1607 const FileEntry *File
1608 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1609 BlobLen - Record[3]));
1611 // FIXME: Stable encoding
1612 InclusionDirective::InclusionKind Kind
1613 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1614 InclusionDirective *ID
1615 = new (PPRec) InclusionDirective(PPRec, Kind,
1616 llvm::StringRef(BlobStart, Record[3]),
1619 SourceRange(ReadSourceLocation(F, Record[1]),
1620 ReadSourceLocation(F, Record[2])));
1621 PPRec.SetPreallocatedEntity(Record[0], ID);
1626 Error("invalid offset in preprocessor detail block");
1631 /// \brief Trait class used to search the on-disk hash table containing all of
1632 /// the header search information.
1634 /// The on-disk hash table contains a mapping from each header path to
1635 /// information about that header (how many times it has been included, its
1636 /// controlling macro, etc.). Note that we actually hash based on the
1637 /// filename, and support "deep" comparisons of file names based on current
1638 /// inode numbers, so that the search can cope with non-normalized path names
1640 class HeaderFileInfoTrait {
1641 const char *SearchPath;
1642 struct stat SearchPathStatBuf;
1643 llvm::Optional<int> SearchPathStatResult;
1645 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1646 if (Path == SearchPath) {
1647 if (!SearchPathStatResult)
1648 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1650 *StatBuf = SearchPathStatBuf;
1651 return *SearchPathStatResult;
1654 return stat(Path, StatBuf);
1658 typedef const char *external_key_type;
1659 typedef const char *internal_key_type;
1661 typedef HeaderFileInfo data_type;
1663 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1665 static unsigned ComputeHash(const char *path) {
1666 return llvm::HashString(llvm::sys::path::filename(path));
1669 static internal_key_type GetInternalKey(const char *path) { return path; }
1671 bool EqualKey(internal_key_type a, internal_key_type b) {
1672 if (strcmp(a, b) == 0)
1675 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1678 // The file names match, but the path names don't. stat() the files to
1679 // see if they are the same.
1680 struct stat StatBufA, StatBufB;
1681 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1684 return StatBufA.st_ino == StatBufB.st_ino;
1687 static std::pair<unsigned, unsigned>
1688 ReadKeyDataLength(const unsigned char*& d) {
1689 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1690 unsigned DataLen = (unsigned) *d++;
1691 return std::make_pair(KeyLen + 1, DataLen);
1694 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1695 return (const char *)d;
1698 static data_type ReadData(const internal_key_type, const unsigned char *d,
1700 const unsigned char *End = d + DataLen;
1701 using namespace clang::io;
1703 unsigned Flags = *d++;
1704 HFI.isImport = (Flags >> 4) & 0x01;
1705 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
1706 HFI.DirInfo = (Flags >> 1) & 0x03;
1707 HFI.Resolved = Flags & 0x01;
1708 HFI.NumIncludes = ReadUnalignedLE16(d);
1709 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1710 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1713 // This HeaderFileInfo was externally loaded.
1714 HFI.External = true;
1720 /// \brief The on-disk hash table used for the global method pool.
1721 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1722 HeaderFileInfoLookupTable;
1724 void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1726 // Note that this identifier has a macro definition.
1727 II->setHasMacroDefinition(true);
1729 // Adjust the offset based on our position in the chain.
1730 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1734 Offset += Chain[I]->SizeInBits;
1737 UnreadMacroRecordOffsets[II] = Offset;
1740 void ASTReader::ReadDefinedMacros() {
1741 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1742 PerFileData &F = *Chain[N - I - 1];
1743 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
1745 // If there was no preprocessor block, skip this file.
1746 if (!MacroCursor.getBitStreamReader())
1749 llvm::BitstreamCursor Cursor = MacroCursor;
1750 Cursor.JumpToBit(F.MacroStartOffset);
1754 unsigned Code = Cursor.ReadCode();
1755 if (Code == llvm::bitc::END_BLOCK)
1758 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1759 // No known subblocks, always skip them.
1760 Cursor.ReadSubBlockID();
1761 if (Cursor.SkipBlock()) {
1762 Error("malformed block record in AST file");
1768 if (Code == llvm::bitc::DEFINE_ABBREV) {
1769 Cursor.ReadAbbrevRecord();
1774 const char *BlobStart;
1777 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1778 default: // Default behavior: ignore.
1781 case PP_MACRO_OBJECT_LIKE:
1782 case PP_MACRO_FUNCTION_LIKE:
1783 DecodeIdentifierInfo(Record[0]);
1793 // Drain the unread macro-record offsets map.
1794 while (!UnreadMacroRecordOffsets.empty())
1795 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1798 void ASTReader::LoadMacroDefinition(
1799 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1800 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1802 uint64_t Offset = Pos->second;
1803 UnreadMacroRecordOffsets.erase(Pos);
1805 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1806 if (Offset < Chain[I]->SizeInBits) {
1811 Offset -= Chain[I]->SizeInBits;
1814 Error("Malformed macro record offset");
1818 ReadMacroRecord(*F, Offset);
1821 void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1822 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1823 = UnreadMacroRecordOffsets.find(II);
1824 LoadMacroDefinition(Pos);
1827 MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
1828 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
1831 if (!MacroDefinitionsLoaded[ID - 1]) {
1832 unsigned Index = ID - 1;
1833 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1834 PerFileData &F = *Chain[N - I - 1];
1835 if (Index < F.LocalNumMacroDefinitions) {
1836 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1837 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1838 LoadPreprocessedEntity(F);
1841 Index -= F.LocalNumMacroDefinitions;
1843 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
1846 return MacroDefinitionsLoaded[ID - 1];
1849 const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1850 std::string Filename = filenameStrRef;
1851 MaybeAddSystemRootToFilename(Filename);
1852 const FileEntry *File = FileMgr.getFile(Filename);
1853 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1854 OriginalDir != CurrentDir) {
1855 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1858 if (!resolved.empty())
1859 File = FileMgr.getFile(resolved);
1865 /// \brief If we are loading a relocatable PCH file, and the filename is
1866 /// not an absolute path, add the system root to the beginning of the file
1868 void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
1869 // If this is not a relocatable PCH file, there's nothing to do.
1870 if (!RelocatablePCH)
1873 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1876 if (isysroot == 0) {
1877 // If no system root was given, default to '/'
1878 Filename.insert(Filename.begin(), '/');
1882 unsigned Length = strlen(isysroot);
1883 if (isysroot[Length - 1] != '/')
1884 Filename.insert(Filename.begin(), '/');
1886 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1889 ASTReader::ASTReadResult
1890 ASTReader::ReadASTBlock(PerFileData &F) {
1891 llvm::BitstreamCursor &Stream = F.Stream;
1893 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1894 Error("malformed block record in AST file");
1898 // Read all of the records and blocks for the ASt file.
1901 while (!Stream.AtEndOfStream()) {
1902 unsigned Code = Stream.ReadCode();
1903 if (Code == llvm::bitc::END_BLOCK) {
1904 if (Stream.ReadBlockEnd()) {
1905 Error("error at end of module block in AST file");
1912 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1913 switch (Stream.ReadSubBlockID()) {
1914 case DECLTYPES_BLOCK_ID:
1915 // We lazily load the decls block, but we want to set up the
1916 // DeclsCursor cursor to point into it. Clone our current bitcode
1917 // cursor to it, enter the block and read the abbrevs in that block.
1918 // With the main cursor, we just skip over it.
1919 F.DeclsCursor = Stream;
1920 if (Stream.SkipBlock() || // Skip with the main cursor.
1921 // Read the abbrevs.
1922 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1923 Error("malformed block record in AST file");
1928 case DECL_UPDATES_BLOCK_ID:
1929 if (Stream.SkipBlock()) {
1930 Error("malformed block record in AST file");
1935 case PREPROCESSOR_BLOCK_ID:
1936 F.MacroCursor = Stream;
1938 PP->setExternalSource(this);
1940 if (Stream.SkipBlock() ||
1941 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1942 Error("malformed block record in AST file");
1945 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1948 case PREPROCESSOR_DETAIL_BLOCK_ID:
1949 F.PreprocessorDetailCursor = Stream;
1950 if (Stream.SkipBlock() ||
1951 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1952 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1953 Error("malformed preprocessor detail record in AST file");
1956 F.PreprocessorDetailStartOffset
1957 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1960 case SOURCE_MANAGER_BLOCK_ID:
1961 switch (ReadSourceManagerBlock(F)) {
1966 Error("malformed source manager block in AST file");
1978 if (Code == llvm::bitc::DEFINE_ABBREV) {
1979 Stream.ReadAbbrevRecord();
1983 // Read and process a record.
1985 const char *BlobStart = 0;
1986 unsigned BlobLen = 0;
1987 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
1988 &BlobStart, &BlobLen)) {
1989 default: // Default behavior: ignore.
1993 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1994 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1995 : diag::warn_pch_version_too_new);
1999 RelocatablePCH = Record[4];
2001 std::string TargetTriple(BlobStart, BlobLen);
2002 if (Listener->ReadTargetTriple(TargetTriple))
2008 case CHAINED_METADATA: {
2010 Error("CHAINED_METADATA is not first record in block");
2013 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2014 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
2015 : diag::warn_pch_version_too_new);
2019 // Load the chained file, which is always a PCH file.
2020 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
2021 case Failure: return Failure;
2022 // If we have to ignore the dependency, we'll have to ignore this too.
2023 case IgnorePCH: return IgnorePCH;
2024 case Success: break;
2030 if (F.LocalNumTypes != 0) {
2031 Error("duplicate TYPE_OFFSET record in AST file");
2034 F.TypeOffsets = (const uint32_t *)BlobStart;
2035 F.LocalNumTypes = Record[0];
2039 if (F.LocalNumDecls != 0) {
2040 Error("duplicate DECL_OFFSET record in AST file");
2043 F.DeclOffsets = (const uint32_t *)BlobStart;
2044 F.LocalNumDecls = Record[0];
2047 case TU_UPDATE_LEXICAL: {
2048 DeclContextInfo Info = {
2049 /* No visible information */ 0,
2050 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2051 BlobLen / sizeof(KindDeclIDPair)
2053 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2058 case UPDATE_VISIBLE: {
2059 serialization::DeclID ID = Record[0];
2060 void *Table = ASTDeclContextNameLookupTable::Create(
2061 (const unsigned char *)BlobStart + Record[1],
2062 (const unsigned char *)BlobStart,
2063 ASTDeclContextNameLookupTrait(*this));
2064 if (ID == 1 && Context) { // Is it the TU?
2065 DeclContextInfo Info = {
2066 Table, /* No lexical inforamtion */ 0, 0
2068 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2070 PendingVisibleUpdates[ID].push_back(Table);
2074 case REDECLS_UPDATE_LATEST: {
2075 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2076 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
2077 DeclID First = Record[i], Latest = Record[i+1];
2078 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2079 Latest > FirstLatestDeclIDs[First]) &&
2080 "The new latest is supposed to come after the previous latest");
2081 FirstLatestDeclIDs[First] = Latest;
2086 case LANGUAGE_OPTIONS:
2087 if (ParseLanguageOptions(Record) && !DisableValidation)
2091 case IDENTIFIER_TABLE:
2092 F.IdentifierTableData = BlobStart;
2094 F.IdentifierLookupTable
2095 = ASTIdentifierLookupTable::Create(
2096 (const unsigned char *)F.IdentifierTableData + Record[0],
2097 (const unsigned char *)F.IdentifierTableData,
2098 ASTIdentifierLookupTrait(*this, F));
2100 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2104 case IDENTIFIER_OFFSET:
2105 if (F.LocalNumIdentifiers != 0) {
2106 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2109 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2110 F.LocalNumIdentifiers = Record[0];
2113 case EXTERNAL_DEFINITIONS:
2114 // Optimization for the first block.
2115 if (ExternalDefinitions.empty())
2116 ExternalDefinitions.swap(Record);
2118 ExternalDefinitions.insert(ExternalDefinitions.end(),
2119 Record.begin(), Record.end());
2123 // Optimization for the first block
2124 if (SpecialTypes.empty())
2125 SpecialTypes.swap(Record);
2127 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
2131 TotalNumStatements += Record[0];
2132 TotalNumMacros += Record[1];
2133 TotalLexicalDeclContexts += Record[2];
2134 TotalVisibleDeclContexts += Record[3];
2137 case UNUSED_FILESCOPED_DECLS:
2138 // Optimization for the first block.
2139 if (UnusedFileScopedDecls.empty())
2140 UnusedFileScopedDecls.swap(Record);
2142 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2143 Record.begin(), Record.end());
2146 case DELEGATING_CTORS:
2147 // Optimization for the first block.
2148 if (DelegatingCtorDecls.empty())
2149 DelegatingCtorDecls.swap(Record);
2151 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2152 Record.begin(), Record.end());
2155 case WEAK_UNDECLARED_IDENTIFIERS:
2156 // Later blocks overwrite earlier ones.
2157 WeakUndeclaredIdentifiers.swap(Record);
2160 case LOCALLY_SCOPED_EXTERNAL_DECLS:
2161 // Optimization for the first block.
2162 if (LocallyScopedExternalDecls.empty())
2163 LocallyScopedExternalDecls.swap(Record);
2165 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2166 Record.begin(), Record.end());
2169 case SELECTOR_OFFSETS:
2170 F.SelectorOffsets = (const uint32_t *)BlobStart;
2171 F.LocalNumSelectors = Record[0];
2175 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
2177 F.SelectorLookupTable
2178 = ASTSelectorLookupTable::Create(
2179 F.SelectorLookupTableData + Record[0],
2180 F.SelectorLookupTableData,
2181 ASTSelectorLookupTrait(*this));
2182 TotalNumMethodPoolEntries += Record[1];
2185 case REFERENCED_SELECTOR_POOL:
2186 F.ReferencedSelectorsData.swap(Record);
2189 case PP_COUNTER_VALUE:
2190 if (!Record.empty() && Listener)
2191 Listener->ReadCounter(Record[0]);
2194 case SOURCE_LOCATION_OFFSETS:
2195 F.SLocOffsets = (const uint32_t *)BlobStart;
2196 F.LocalNumSLocEntries = Record[0];
2197 F.LocalSLocSize = Record[1];
2200 case FILE_SOURCE_LOCATION_OFFSETS:
2201 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2202 F.LocalNumSLocFileEntries = Record[0];
2205 case SOURCE_LOCATION_PRELOADS:
2206 if (PreloadSLocEntries.empty())
2207 PreloadSLocEntries.swap(Record);
2209 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2210 Record.begin(), Record.end());
2214 if (!DisableStatCache) {
2215 ASTStatCache *MyStatCache =
2216 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2217 (const unsigned char *)BlobStart,
2218 NumStatHits, NumStatMisses);
2219 FileMgr.addStatCache(MyStatCache);
2220 F.StatCache = MyStatCache;
2225 case EXT_VECTOR_DECLS:
2226 // Optimization for the first block.
2227 if (ExtVectorDecls.empty())
2228 ExtVectorDecls.swap(Record);
2230 ExtVectorDecls.insert(ExtVectorDecls.end(),
2231 Record.begin(), Record.end());
2235 // Later tables overwrite earlier ones.
2236 VTableUses.swap(Record);
2239 case DYNAMIC_CLASSES:
2240 // Optimization for the first block.
2241 if (DynamicClasses.empty())
2242 DynamicClasses.swap(Record);
2244 DynamicClasses.insert(DynamicClasses.end(),
2245 Record.begin(), Record.end());
2248 case PENDING_IMPLICIT_INSTANTIATIONS:
2249 F.PendingInstantiations.swap(Record);
2252 case SEMA_DECL_REFS:
2253 // Later tables overwrite earlier ones.
2254 SemaDeclRefs.swap(Record);
2257 case ORIGINAL_FILE_NAME:
2258 // The primary AST will be the last to get here, so it will be the one
2260 ActualOriginalFileName.assign(BlobStart, BlobLen);
2261 OriginalFileName = ActualOriginalFileName;
2262 MaybeAddSystemRootToFilename(OriginalFileName);
2265 case ORIGINAL_FILE_ID:
2266 OriginalFileID = FileID::get(Record[0]);
2269 case ORIGINAL_PCH_DIR:
2270 // The primary AST will be the last to get here, so it will be the one
2272 OriginalDir.assign(BlobStart, BlobLen);
2275 case VERSION_CONTROL_BRANCH_REVISION: {
2276 const std::string &CurBranch = getClangFullRepositoryVersion();
2277 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2278 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2279 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
2285 case MACRO_DEFINITION_OFFSETS:
2286 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2287 F.NumPreallocatedPreprocessingEntities = Record[0];
2288 F.LocalNumMacroDefinitions = Record[1];
2291 case DECL_UPDATE_OFFSETS: {
2292 if (Record.size() % 2 != 0) {
2293 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2296 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2297 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2298 .push_back(std::make_pair(&F, Record[I+1]));
2302 case DECL_REPLACEMENTS: {
2303 if (Record.size() % 2 != 0) {
2304 Error("invalid DECL_REPLACEMENTS block in AST file");
2307 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2308 ReplacedDecls[static_cast<DeclID>(Record[I])] =
2309 std::make_pair(&F, Record[I+1]);
2313 case CXX_BASE_SPECIFIER_OFFSETS: {
2314 if (F.LocalNumCXXBaseSpecifiers != 0) {
2315 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2319 F.LocalNumCXXBaseSpecifiers = Record[0];
2320 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2324 case DIAG_PRAGMA_MAPPINGS:
2325 if (Record.size() % 2 != 0) {
2326 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2329 if (PragmaDiagMappings.empty())
2330 PragmaDiagMappings.swap(Record);
2332 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
2333 Record.begin(), Record.end());
2336 case CUDA_SPECIAL_DECL_REFS:
2337 // Later tables overwrite earlier ones.
2338 CUDASpecialDeclRefs.swap(Record);
2341 case HEADER_SEARCH_TABLE:
2342 F.HeaderFileInfoTableData = BlobStart;
2343 F.LocalNumHeaderFileInfos = Record[1];
2345 F.HeaderFileInfoTable
2346 = HeaderFileInfoLookupTable::Create(
2347 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2348 (const unsigned char *)F.HeaderFileInfoTableData);
2350 PP->getHeaderSearchInfo().SetExternalSource(this);
2354 case FP_PRAGMA_OPTIONS:
2355 // Later tables overwrite earlier ones.
2356 FPPragmaOptions.swap(Record);
2359 case OPENCL_EXTENSIONS:
2360 // Later tables overwrite earlier ones.
2361 OpenCLExtensions.swap(Record);
2364 case TENTATIVE_DEFINITIONS:
2365 // Optimization for the first block.
2366 if (TentativeDefinitions.empty())
2367 TentativeDefinitions.swap(Record);
2369 TentativeDefinitions.insert(TentativeDefinitions.end(),
2370 Record.begin(), Record.end());
2373 case KNOWN_NAMESPACES:
2374 // Optimization for the first block.
2375 if (KnownNamespaces.empty())
2376 KnownNamespaces.swap(Record);
2378 KnownNamespaces.insert(KnownNamespaces.end(),
2379 Record.begin(), Record.end());
2384 Error("premature end of bitstream in AST file");
2388 ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2389 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2390 PerFileData *F = Chain[CI];
2391 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2393 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2394 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
2395 unsigned Code = SLocEntryCursor.ReadCode();
2396 if (Code == llvm::bitc::END_BLOCK ||
2397 Code == llvm::bitc::ENTER_SUBBLOCK ||
2398 Code == llvm::bitc::DEFINE_ABBREV) {
2399 Error("incorrectly-formatted source location entry in AST file");
2404 const char *BlobStart;
2406 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2408 Error("incorrectly-formatted source location entry in AST file");
2411 case SM_SLOC_FILE_ENTRY: {
2412 llvm::StringRef Filename(BlobStart, BlobLen);
2413 const FileEntry *File = getFileEntry(Filename);
2416 std::string ErrorStr = "could not find file '";
2417 ErrorStr += Filename;
2418 ErrorStr += "' referenced by AST file";
2419 Error(ErrorStr.c_str());
2423 if (Record.size() < 6) {
2424 Error("source location entry is incorrect");
2428 // The stat info from the FileEntry came from the cached stat
2429 // info of the PCH, so we cannot trust it.
2430 struct stat StatBuf;
2431 if (::stat(File->getName(), &StatBuf) != 0) {
2432 StatBuf.st_size = File->getSize();
2433 StatBuf.st_mtime = File->getModificationTime();
2436 if (((off_t)Record[4] != StatBuf.st_size
2437 #if !defined(LLVM_ON_WIN32)
2438 // In our regression testing, the Windows file system seems to
2439 // have inconsistent modification times that sometimes
2440 // erroneously trigger this error-handling path.
2441 || (time_t)Record[5] != StatBuf.st_mtime
2444 Error(diag::err_fe_pch_file_modified, Filename);
2457 ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2459 switch(ReadASTCore(FileName, Type)) {
2460 case Failure: return Failure;
2461 case IgnorePCH: return IgnorePCH;
2462 case Success: break;
2465 // Here comes stuff that we only do once the entire chain is loaded.
2467 if (!DisableValidation) {
2468 switch(validateFileEntries()) {
2469 case Failure: return Failure;
2470 case IgnorePCH: return IgnorePCH;
2471 case Success: break;
2475 // Allocate space for loaded slocentries, identifiers, decls and types.
2476 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
2477 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2478 TotalNumSelectors = 0;
2479 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2480 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
2481 NextSLocOffset += Chain[I]->LocalSLocSize;
2482 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
2483 TotalNumTypes += Chain[I]->LocalNumTypes;
2484 TotalNumDecls += Chain[I]->LocalNumDecls;
2485 TotalNumPreallocatedPreprocessingEntities +=
2486 Chain[I]->NumPreallocatedPreprocessingEntities;
2487 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
2488 TotalNumSelectors += Chain[I]->LocalNumSelectors;
2490 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
2491 IdentifiersLoaded.resize(TotalNumIdentifiers);
2492 TypesLoaded.resize(TotalNumTypes);
2493 DeclsLoaded.resize(TotalNumDecls);
2494 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2496 if (TotalNumIdentifiers > 0)
2497 PP->getHeaderSearchInfo().SetExternalLookup(this);
2498 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2499 if (!PP->getPreprocessingRecord())
2500 PP->createPreprocessingRecord(true);
2501 PP->getPreprocessingRecord()->SetExternalSource(*this,
2502 TotalNumPreallocatedPreprocessingEntities);
2505 SelectorsLoaded.resize(TotalNumSelectors);
2506 // Preload SLocEntries.
2507 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2508 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2509 if (Result != Success)
2513 // Check the predefines buffers.
2514 if (!DisableValidation && CheckPredefinesBuffers())
2518 // Initialization of keywords and pragmas occurs before the
2519 // AST file is read, so there may be some identifiers that were
2520 // loaded into the IdentifierTable before we intercepted the
2521 // creation of identifiers. Iterate through the list of known
2522 // identifiers and determine whether we have to establish
2523 // preprocessor definitions or top-level identifier declaration
2524 // chains for those identifiers.
2526 // We copy the IdentifierInfo pointers to a small vector first,
2527 // since de-serializing declarations or macro definitions can add
2528 // new entries into the identifier table, invalidating the
2530 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2531 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2532 IdEnd = PP->getIdentifierTable().end();
2534 Identifiers.push_back(Id->second);
2535 // We need to search the tables in all files.
2536 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
2537 ASTIdentifierLookupTable *IdTable
2538 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2539 // Not all AST files necessarily have identifier tables, only the useful
2543 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2544 IdentifierInfo *II = Identifiers[I];
2545 // Look in the on-disk hash tables for an entry for this identifier
2546 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
2547 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
2548 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2549 if (Pos == IdTable->end())
2552 // Dereferencing the iterator has the effect of populating the
2553 // IdentifierInfo node with the various declarations it needs.
2560 InitializeContext(*Context);
2562 if (DeserializationListener)
2563 DeserializationListener->ReaderInitialized(this);
2565 // If this AST file is a precompiled preamble, then set the main file ID of
2566 // the source manager to the file source file from which the preamble was
2567 // built. This is the only valid way to use a precompiled preamble.
2568 if (Type == Preamble) {
2569 if (OriginalFileID.isInvalid()) {
2571 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2573 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
2576 if (!OriginalFileID.isInvalid())
2577 SourceMgr.SetPreambleFileID(OriginalFileID);
2583 ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2585 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
2586 Chain.push_back(new PerFileData(Type));
2587 PerFileData &F = *Chain.back();
2589 Prev->NextInSource = &F;
2592 F.Loaders.push_back(Prev);
2594 // Set the AST file name.
2595 F.FileName = FileName;
2597 if (FileName != "-") {
2598 CurrentDir = llvm::sys::path::parent_path(FileName);
2599 if (CurrentDir.empty()) CurrentDir = ".";
2602 if (!ASTBuffers.empty()) {
2603 F.Buffer.reset(ASTBuffers.back());
2604 ASTBuffers.pop_back();
2605 assert(F.Buffer && "Passed null buffer");
2607 // Open the AST file.
2609 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2611 llvm::error_code ec;
2612 if (FileName == "-") {
2613 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2615 ErrStr = ec.message();
2617 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2619 Error(ErrStr.c_str());
2624 // Initialize the stream
2625 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2626 (const unsigned char *)F.Buffer->getBufferEnd());
2627 llvm::BitstreamCursor &Stream = F.Stream;
2628 Stream.init(F.StreamFile);
2629 F.SizeInBits = F.Buffer->getBufferSize() * 8;
2631 // Sniff for the signature.
2632 if (Stream.Read(8) != 'C' ||
2633 Stream.Read(8) != 'P' ||
2634 Stream.Read(8) != 'C' ||
2635 Stream.Read(8) != 'H') {
2636 Diag(diag::err_not_a_pch_file) << FileName;
2640 while (!Stream.AtEndOfStream()) {
2641 unsigned Code = Stream.ReadCode();
2643 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2644 Error("invalid record at top-level of AST file");
2648 unsigned BlockID = Stream.ReadSubBlockID();
2650 // We only know the AST subblock ID.
2652 case llvm::bitc::BLOCKINFO_BLOCK_ID:
2653 if (Stream.ReadBlockInfoBlock()) {
2654 Error("malformed BlockInfoBlock in AST file");
2659 switch (ReadASTBlock(F)) {
2667 // FIXME: We could consider reading through to the end of this
2668 // AST block, skipping subblocks, to see if there are other
2669 // AST blocks elsewhere.
2671 // Clear out any preallocated source location entries, so that
2672 // the source manager does not try to resolve them later.
2673 SourceMgr.ClearPreallocatedSLocEntries();
2675 // Remove the stat cache.
2677 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
2683 if (Stream.SkipBlock()) {
2684 Error("malformed block record in AST file");
2694 void ASTReader::setPreprocessor(Preprocessor &pp) {
2697 unsigned TotalNum = 0;
2698 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2699 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2701 if (!PP->getPreprocessingRecord())
2702 PP->createPreprocessingRecord(true);
2703 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
2707 void ASTReader::InitializeContext(ASTContext &Ctx) {
2709 assert(Context && "Passed null context!");
2711 assert(PP && "Forgot to set Preprocessor ?");
2712 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2713 PP->getHeaderSearchInfo().SetExternalLookup(this);
2714 PP->setExternalSource(this);
2715 PP->getHeaderSearchInfo().SetExternalSource(this);
2717 // If we have an update block for the TU waiting, we have to add it before
2718 // deserializing the decl.
2719 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2720 if (DCU != DeclContextOffsets.end()) {
2721 // Insertion could invalidate map, so grab vector.
2723 T.swap(DCU->second);
2724 DeclContextOffsets.erase(DCU);
2725 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2728 // Load the translation unit declaration
2729 GetTranslationUnitDecl();
2731 // Load the special types.
2732 Context->setBuiltinVaListType(
2733 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2734 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
2735 Context->setObjCIdType(GetType(Id));
2736 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
2737 Context->setObjCSelType(GetType(Sel));
2738 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
2739 Context->setObjCProtoType(GetType(Proto));
2740 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
2741 Context->setObjCClassType(GetType(Class));
2743 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
2744 Context->setCFConstantStringType(GetType(String));
2745 if (unsigned FastEnum
2746 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2747 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
2748 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2749 QualType FileType = GetType(File);
2750 if (FileType.isNull()) {
2751 Error("FILE type is NULL");
2754 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2755 Context->setFILEDecl(Typedef->getDecl());
2757 const TagType *Tag = FileType->getAs<TagType>();
2759 Error("Invalid FILE type in AST file");
2762 Context->setFILEDecl(Tag->getDecl());
2765 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2766 QualType Jmp_bufType = GetType(Jmp_buf);
2767 if (Jmp_bufType.isNull()) {
2768 Error("jmp_bug type is NULL");
2771 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2772 Context->setjmp_bufDecl(Typedef->getDecl());
2774 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2776 Error("Invalid jmp_buf type in AST file");
2779 Context->setjmp_bufDecl(Tag->getDecl());
2782 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2783 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2784 if (Sigjmp_bufType.isNull()) {
2785 Error("sigjmp_buf type is NULL");
2788 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2789 Context->setsigjmp_bufDecl(Typedef->getDecl());
2791 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2792 assert(Tag && "Invalid sigjmp_buf type in AST file");
2793 Context->setsigjmp_bufDecl(Tag->getDecl());
2796 if (unsigned ObjCIdRedef
2797 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2798 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2799 if (unsigned ObjCClassRedef
2800 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2801 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2802 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2803 Context->setBlockDescriptorType(GetType(String));
2805 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2806 Context->setBlockDescriptorExtendedType(GetType(String));
2807 if (unsigned ObjCSelRedef
2808 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2809 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2810 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
2811 Context->setNSConstantStringType(GetType(String));
2813 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
2814 Context->setInt128Installed();
2816 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2817 Context->AutoDeductTy = GetType(AutoDeduct);
2818 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2819 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2821 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
2823 // If there were any CUDA special declarations, deserialize them.
2824 if (!CUDASpecialDeclRefs.empty()) {
2825 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2826 Context->setcudaConfigureCallDecl(
2827 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2831 /// \brief Retrieve the name of the original source file name
2832 /// directly from the AST file, without actually loading the AST
2834 std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
2835 FileManager &FileMgr,
2836 Diagnostic &Diags) {
2837 // Open the AST file.
2839 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
2840 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
2842 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
2843 return std::string();
2846 // Initialize the stream
2847 llvm::BitstreamReader StreamFile;
2848 llvm::BitstreamCursor Stream;
2849 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2850 (const unsigned char *)Buffer->getBufferEnd());
2851 Stream.init(StreamFile);
2853 // Sniff for the signature.
2854 if (Stream.Read(8) != 'C' ||
2855 Stream.Read(8) != 'P' ||
2856 Stream.Read(8) != 'C' ||
2857 Stream.Read(8) != 'H') {
2858 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
2859 return std::string();
2863 while (!Stream.AtEndOfStream()) {
2864 unsigned Code = Stream.ReadCode();
2866 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2867 unsigned BlockID = Stream.ReadSubBlockID();
2869 // We only know the AST subblock ID.
2872 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2873 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2874 return std::string();
2879 if (Stream.SkipBlock()) {
2880 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
2881 return std::string();
2888 if (Code == llvm::bitc::END_BLOCK) {
2889 if (Stream.ReadBlockEnd()) {
2890 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
2891 return std::string();
2896 if (Code == llvm::bitc::DEFINE_ABBREV) {
2897 Stream.ReadAbbrevRecord();
2902 const char *BlobStart = 0;
2903 unsigned BlobLen = 0;
2904 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
2905 == ORIGINAL_FILE_NAME)
2906 return std::string(BlobStart, BlobLen);
2909 return std::string();
2912 /// \brief Parse the record that corresponds to a LangOptions data
2915 /// This routine parses the language options from the AST file and then gives
2916 /// them to the AST listener if one is set.
2918 /// \returns true if the listener deems the file unacceptable, false otherwise.
2919 bool ASTReader::ParseLanguageOptions(
2920 const llvm::SmallVectorImpl<uint64_t> &Record) {
2922 LangOptions LangOpts;
2924 #define PARSE_LANGOPT(Option) \
2925 LangOpts.Option = Record[Idx]; \
2929 PARSE_LANGOPT(Trigraphs);
2930 PARSE_LANGOPT(BCPLComment);
2931 PARSE_LANGOPT(DollarIdents);
2932 PARSE_LANGOPT(AsmPreprocessor);
2933 PARSE_LANGOPT(GNUMode);
2934 PARSE_LANGOPT(GNUKeywords);
2935 PARSE_LANGOPT(ImplicitInt);
2936 PARSE_LANGOPT(Digraphs);
2937 PARSE_LANGOPT(HexFloats);
2940 PARSE_LANGOPT(Microsoft);
2941 PARSE_LANGOPT(CPlusPlus);
2942 PARSE_LANGOPT(CPlusPlus0x);
2943 PARSE_LANGOPT(CXXOperatorNames);
2944 PARSE_LANGOPT(ObjC1);
2945 PARSE_LANGOPT(ObjC2);
2946 PARSE_LANGOPT(ObjCNonFragileABI);
2947 PARSE_LANGOPT(ObjCNonFragileABI2);
2948 PARSE_LANGOPT(AppleKext);
2949 PARSE_LANGOPT(ObjCDefaultSynthProperties);
2950 PARSE_LANGOPT(ObjCInferRelatedResultType);
2951 PARSE_LANGOPT(NoConstantCFStrings);
2952 PARSE_LANGOPT(PascalStrings);
2953 PARSE_LANGOPT(WritableStrings);
2954 PARSE_LANGOPT(LaxVectorConversions);
2955 PARSE_LANGOPT(AltiVec);
2956 PARSE_LANGOPT(Exceptions);
2957 PARSE_LANGOPT(ObjCExceptions);
2958 PARSE_LANGOPT(CXXExceptions);
2959 PARSE_LANGOPT(SjLjExceptions);
2960 PARSE_LANGOPT(MSBitfields);
2961 PARSE_LANGOPT(NeXTRuntime);
2962 PARSE_LANGOPT(Freestanding);
2963 PARSE_LANGOPT(NoBuiltin);
2964 PARSE_LANGOPT(ThreadsafeStatics);
2965 PARSE_LANGOPT(POSIXThreads);
2966 PARSE_LANGOPT(Blocks);
2967 PARSE_LANGOPT(EmitAllDecls);
2968 PARSE_LANGOPT(MathErrno);
2969 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2971 PARSE_LANGOPT(HeinousExtensions);
2972 PARSE_LANGOPT(Optimize);
2973 PARSE_LANGOPT(OptimizeSize);
2974 PARSE_LANGOPT(Static);
2975 PARSE_LANGOPT(PICLevel);
2976 PARSE_LANGOPT(GNUInline);
2977 PARSE_LANGOPT(NoInline);
2978 PARSE_LANGOPT(Deprecated);
2979 PARSE_LANGOPT(AccessControl);
2980 PARSE_LANGOPT(CharIsSigned);
2981 PARSE_LANGOPT(ShortWChar);
2982 PARSE_LANGOPT(ShortEnums);
2983 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2984 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
2985 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
2987 PARSE_LANGOPT(InstantiationDepth);
2988 PARSE_LANGOPT(OpenCL);
2989 PARSE_LANGOPT(CUDA);
2990 PARSE_LANGOPT(CatchUndefined);
2991 PARSE_LANGOPT(DefaultFPContract);
2992 PARSE_LANGOPT(ElideConstructors);
2993 PARSE_LANGOPT(SpellChecking);
2994 PARSE_LANGOPT(MRTD);
2995 PARSE_LANGOPT(ObjCAutoRefCount);
2996 #undef PARSE_LANGOPT
2998 return Listener->ReadLanguageOptions(LangOpts);
3004 void ASTReader::ReadPreprocessedEntities() {
3005 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3006 PerFileData &F = *Chain[I];
3007 if (!F.PreprocessorDetailCursor.getBitStreamReader())
3010 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
3011 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3012 while (LoadPreprocessedEntity(F)) { }
3016 PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
3018 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3019 if (Offset < Chain[I]->SizeInBits) {
3024 Offset -= Chain[I]->SizeInBits;
3028 Error("Malformed preprocessed entity offset");
3032 // Keep track of where we are in the stream, then jump back there
3033 // after reading this entity.
3034 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3035 F->PreprocessorDetailCursor.JumpToBit(Offset);
3036 return LoadPreprocessedEntity(*F);
3039 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3040 HeaderFileInfoTrait Trait(FE->getName());
3041 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3042 PerFileData &F = *Chain[I];
3043 HeaderFileInfoLookupTable *Table
3044 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3048 // Look in the on-disk hash table for an entry for this file name.
3049 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3051 if (Pos == Table->end())
3054 HeaderFileInfo HFI = *Pos;
3056 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3061 return HeaderFileInfo();
3064 void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
3066 while (Idx < PragmaDiagMappings.size()) {
3068 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
3070 assert(Idx < PragmaDiagMappings.size() &&
3071 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3072 if (Idx >= PragmaDiagMappings.size())
3073 break; // Something is messed up but at least avoid infinite loop in
3075 unsigned DiagID = PragmaDiagMappings[Idx++];
3076 if (DiagID == (unsigned)-1)
3077 break; // no more diag/map pairs for this location.
3078 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
3079 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3084 /// \brief Get the correct cursor and offset for loading a type.
3085 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
3087 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3088 F = Chain[N - I - 1];
3089 if (Index < F->LocalNumTypes)
3091 Index -= F->LocalNumTypes;
3093 assert(F && F->LocalNumTypes > Index && "Broken chain");
3094 return RecordLocation(F, F->TypeOffsets[Index]);
3097 /// \brief Read and return the type with the given index..
3099 /// The index is the type ID, shifted and minus the number of predefs. This
3100 /// routine actually reads the record corresponding to the type at the given
3101 /// location. It is a helper routine for GetType, which deals with reading type
3103 QualType ASTReader::ReadTypeRecord(unsigned Index) {
3104 RecordLocation Loc = TypeCursorForIndex(Index);
3105 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
3107 // Keep track of where we are in the stream, then jump back there
3108 // after reading this type.
3109 SavedStreamPosition SavedPosition(DeclsCursor);
3111 ReadingKindTracker ReadingKind(Read_Type, *this);
3113 // Note that we are loading a type record.
3114 Deserializing AType(this);
3116 DeclsCursor.JumpToBit(Loc.Offset);
3118 unsigned Code = DeclsCursor.ReadCode();
3119 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3120 case TYPE_EXT_QUAL: {
3121 if (Record.size() != 2) {
3122 Error("Incorrect encoding of extended qualifier type");
3125 QualType Base = GetType(Record[0]);
3126 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3127 return Context->getQualifiedType(Base, Quals);
3130 case TYPE_COMPLEX: {
3131 if (Record.size() != 1) {
3132 Error("Incorrect encoding of complex type");
3135 QualType ElemType = GetType(Record[0]);
3136 return Context->getComplexType(ElemType);
3139 case TYPE_POINTER: {
3140 if (Record.size() != 1) {
3141 Error("Incorrect encoding of pointer type");
3144 QualType PointeeType = GetType(Record[0]);
3145 return Context->getPointerType(PointeeType);
3148 case TYPE_BLOCK_POINTER: {
3149 if (Record.size() != 1) {
3150 Error("Incorrect encoding of block pointer type");
3153 QualType PointeeType = GetType(Record[0]);
3154 return Context->getBlockPointerType(PointeeType);
3157 case TYPE_LVALUE_REFERENCE: {
3158 if (Record.size() != 2) {
3159 Error("Incorrect encoding of lvalue reference type");
3162 QualType PointeeType = GetType(Record[0]);
3163 return Context->getLValueReferenceType(PointeeType, Record[1]);
3166 case TYPE_RVALUE_REFERENCE: {
3167 if (Record.size() != 1) {
3168 Error("Incorrect encoding of rvalue reference type");
3171 QualType PointeeType = GetType(Record[0]);
3172 return Context->getRValueReferenceType(PointeeType);
3175 case TYPE_MEMBER_POINTER: {
3176 if (Record.size() != 2) {
3177 Error("Incorrect encoding of member pointer type");
3180 QualType PointeeType = GetType(Record[0]);
3181 QualType ClassType = GetType(Record[1]);
3182 if (PointeeType.isNull() || ClassType.isNull())
3185 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
3188 case TYPE_CONSTANT_ARRAY: {
3189 QualType ElementType = GetType(Record[0]);
3190 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3191 unsigned IndexTypeQuals = Record[2];
3193 llvm::APInt Size = ReadAPInt(Record, Idx);
3194 return Context->getConstantArrayType(ElementType, Size,
3195 ASM, IndexTypeQuals);
3198 case TYPE_INCOMPLETE_ARRAY: {
3199 QualType ElementType = GetType(Record[0]);
3200 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3201 unsigned IndexTypeQuals = Record[2];
3202 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
3205 case TYPE_VARIABLE_ARRAY: {
3206 QualType ElementType = GetType(Record[0]);
3207 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3208 unsigned IndexTypeQuals = Record[2];
3209 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3210 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3211 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
3212 ASM, IndexTypeQuals,
3213 SourceRange(LBLoc, RBLoc));
3217 if (Record.size() != 3) {
3218 Error("incorrect encoding of vector type in AST file");
3222 QualType ElementType = GetType(Record[0]);
3223 unsigned NumElements = Record[1];
3224 unsigned VecKind = Record[2];
3225 return Context->getVectorType(ElementType, NumElements,
3226 (VectorType::VectorKind)VecKind);
3229 case TYPE_EXT_VECTOR: {
3230 if (Record.size() != 3) {
3231 Error("incorrect encoding of extended vector type in AST file");
3235 QualType ElementType = GetType(Record[0]);
3236 unsigned NumElements = Record[1];
3237 return Context->getExtVectorType(ElementType, NumElements);
3240 case TYPE_FUNCTION_NO_PROTO: {
3241 if (Record.size() != 6) {
3242 Error("incorrect encoding of no-proto function type");
3245 QualType ResultType = GetType(Record[0]);
3246 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3247 (CallingConv)Record[4], Record[5]);
3248 return Context->getFunctionNoProtoType(ResultType, Info);
3251 case TYPE_FUNCTION_PROTO: {
3252 QualType ResultType = GetType(Record[0]);
3254 FunctionProtoType::ExtProtoInfo EPI;
3255 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3256 /*hasregparm*/ Record[2],
3257 /*regparm*/ Record[3],
3258 static_cast<CallingConv>(Record[4]),
3259 /*produces*/ Record[5]);
3262 unsigned NumParams = Record[Idx++];
3263 llvm::SmallVector<QualType, 16> ParamTypes;
3264 for (unsigned I = 0; I != NumParams; ++I)
3265 ParamTypes.push_back(GetType(Record[Idx++]));
3267 EPI.Variadic = Record[Idx++];
3268 EPI.TypeQuals = Record[Idx++];
3269 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
3270 ExceptionSpecificationType EST =
3271 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3272 EPI.ExceptionSpecType = EST;
3273 if (EST == EST_Dynamic) {
3274 EPI.NumExceptions = Record[Idx++];
3275 llvm::SmallVector<QualType, 2> Exceptions;
3276 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3277 Exceptions.push_back(GetType(Record[Idx++]));
3278 EPI.Exceptions = Exceptions.data();
3279 } else if (EST == EST_ComputedNoexcept) {
3280 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3282 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
3286 case TYPE_UNRESOLVED_USING:
3287 return Context->getTypeDeclType(
3288 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3290 case TYPE_TYPEDEF: {
3291 if (Record.size() != 2) {
3292 Error("incorrect encoding of typedef type");
3295 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
3296 QualType Canonical = GetType(Record[1]);
3297 if (!Canonical.isNull())
3298 Canonical = Context->getCanonicalType(Canonical);
3299 return Context->getTypedefType(Decl, Canonical);
3302 case TYPE_TYPEOF_EXPR:
3303 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
3306 if (Record.size() != 1) {
3307 Error("incorrect encoding of typeof(type) in AST file");
3310 QualType UnderlyingType = GetType(Record[0]);
3311 return Context->getTypeOfType(UnderlyingType);
3315 return Context->getDecltypeType(ReadExpr(*Loc.F));
3317 case TYPE_UNARY_TRANSFORM: {
3318 QualType BaseType = GetType(Record[0]);
3319 QualType UnderlyingType = GetType(Record[1]);
3320 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3321 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3325 return Context->getAutoType(GetType(Record[0]));
3328 if (Record.size() != 2) {
3329 Error("incorrect encoding of record type");
3332 bool IsDependent = Record[0];
3333 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
3334 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3339 if (Record.size() != 2) {
3340 Error("incorrect encoding of enum type");
3343 bool IsDependent = Record[0];
3344 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
3345 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3349 case TYPE_ATTRIBUTED: {
3350 if (Record.size() != 3) {
3351 Error("incorrect encoding of attributed type");
3354 QualType modifiedType = GetType(Record[0]);
3355 QualType equivalentType = GetType(Record[1]);
3356 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3357 return Context->getAttributedType(kind, modifiedType, equivalentType);
3361 if (Record.size() != 1) {
3362 Error("incorrect encoding of paren type");
3365 QualType InnerType = GetType(Record[0]);
3366 return Context->getParenType(InnerType);
3369 case TYPE_PACK_EXPANSION: {
3370 if (Record.size() != 2) {
3371 Error("incorrect encoding of pack expansion type");
3374 QualType Pattern = GetType(Record[0]);
3375 if (Pattern.isNull())
3377 llvm::Optional<unsigned> NumExpansions;
3379 NumExpansions = Record[1] - 1;
3380 return Context->getPackExpansionType(Pattern, NumExpansions);
3383 case TYPE_ELABORATED: {
3385 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3386 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3387 QualType NamedType = GetType(Record[Idx++]);
3388 return Context->getElaboratedType(Keyword, NNS, NamedType);
3391 case TYPE_OBJC_INTERFACE: {
3393 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
3394 return Context->getObjCInterfaceType(ItfD);
3397 case TYPE_OBJC_OBJECT: {
3399 QualType Base = GetType(Record[Idx++]);
3400 unsigned NumProtos = Record[Idx++];
3401 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3402 for (unsigned I = 0; I != NumProtos; ++I)
3403 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
3404 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
3407 case TYPE_OBJC_OBJECT_POINTER: {
3409 QualType Pointee = GetType(Record[Idx++]);
3410 return Context->getObjCObjectPointerType(Pointee);
3413 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
3415 QualType Parm = GetType(Record[Idx++]);
3416 QualType Replacement = GetType(Record[Idx++]);
3418 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3422 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3424 QualType Parm = GetType(Record[Idx++]);
3425 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3426 return Context->getSubstTemplateTypeParmPackType(
3427 cast<TemplateTypeParmType>(Parm),
3431 case TYPE_INJECTED_CLASS_NAME: {
3432 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3433 QualType TST = GetType(Record[1]); // probably derivable
3434 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
3435 // for AST reading, too much interdependencies.
3437 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
3440 case TYPE_TEMPLATE_TYPE_PARM: {
3442 unsigned Depth = Record[Idx++];
3443 unsigned Index = Record[Idx++];
3444 bool Pack = Record[Idx++];
3445 TemplateTypeParmDecl *D =
3446 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3447 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
3450 case TYPE_DEPENDENT_NAME: {
3452 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3453 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3454 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3455 QualType Canon = GetType(Record[Idx++]);
3456 if (!Canon.isNull())
3457 Canon = Context->getCanonicalType(Canon);
3458 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
3461 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
3463 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3464 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3465 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3466 unsigned NumArgs = Record[Idx++];
3467 llvm::SmallVector<TemplateArgument, 8> Args;
3468 Args.reserve(NumArgs);
3470 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
3471 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3472 Args.size(), Args.data());
3475 case TYPE_DEPENDENT_SIZED_ARRAY: {
3479 QualType ElementType = GetType(Record[Idx++]);
3480 ArrayType::ArraySizeModifier ASM
3481 = (ArrayType::ArraySizeModifier)Record[Idx++];
3482 unsigned IndexTypeQuals = Record[Idx++];
3484 // DependentSizedArrayType
3485 Expr *NumElts = ReadExpr(*Loc.F);
3486 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
3488 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3489 IndexTypeQuals, Brackets);
3492 case TYPE_TEMPLATE_SPECIALIZATION: {
3494 bool IsDependent = Record[Idx++];
3495 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
3496 llvm::SmallVector<TemplateArgument, 8> Args;
3497 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
3498 QualType Underlying = GetType(Record[Idx++]);
3500 if (Underlying.isNull())
3501 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3504 T = Context->getTemplateSpecializationType(Name, Args.data(),
3505 Args.size(), Underlying);
3506 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
3510 // Suppress a GCC warning
3514 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
3516 ASTReader::PerFileData &F;
3517 llvm::BitstreamCursor &DeclsCursor;
3518 const ASTReader::RecordData &Record;
3521 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3523 return Reader.ReadSourceLocation(F, R, I);
3527 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
3528 const ASTReader::RecordData &Record, unsigned &Idx)
3529 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3532 // We want compile-time assurance that we've enumerated all of
3533 // these, so unfortunately we have to declare them first, then
3534 // define them out-of-line.
3535 #define ABSTRACT_TYPELOC(CLASS, PARENT)
3536 #define TYPELOC(CLASS, PARENT) \
3537 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
3538 #include "clang/AST/TypeLocNodes.def"
3540 void VisitFunctionTypeLoc(FunctionTypeLoc);
3541 void VisitArrayTypeLoc(ArrayTypeLoc);
3544 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
3547 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
3548 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
3549 if (TL.needsExtraLocalData()) {
3550 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3551 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3552 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3553 TL.setModeAttr(Record[Idx++]);
3556 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
3557 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3559 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
3560 TL.setStarLoc(ReadSourceLocation(Record, Idx));
3562 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
3563 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
3565 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
3566 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
3568 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
3569 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
3571 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
3572 TL.setStarLoc(ReadSourceLocation(Record, Idx));
3573 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3575 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
3576 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3577 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
3579 TL.setSizeExpr(Reader.ReadExpr(F));
3583 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3584 VisitArrayTypeLoc(TL);
3586 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3587 VisitArrayTypeLoc(TL);
3589 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3590 VisitArrayTypeLoc(TL);
3592 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3593 DependentSizedArrayTypeLoc TL) {
3594 VisitArrayTypeLoc(TL);
3596 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3597 DependentSizedExtVectorTypeLoc TL) {
3598 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3600 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
3601 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3603 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
3604 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3606 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
3607 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3608 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
3609 TL.setTrailingReturn(Record[Idx++]);
3610 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
3611 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
3614 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3615 VisitFunctionTypeLoc(TL);
3617 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3618 VisitFunctionTypeLoc(TL);
3620 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
3621 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3623 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
3624 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3626 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
3627 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3628 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3629 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3631 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
3632 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3633 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3634 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3635 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3637 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
3638 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3640 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3641 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3642 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3643 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3644 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3646 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3647 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3649 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
3650 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3652 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
3653 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3655 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3656 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3657 if (TL.hasAttrOperand()) {
3659 range.setBegin(ReadSourceLocation(Record, Idx));
3660 range.setEnd(ReadSourceLocation(Record, Idx));
3661 TL.setAttrOperandParensRange(range);
3663 if (TL.hasAttrExprOperand()) {
3665 TL.setAttrExprOperand(Reader.ReadExpr(F));
3667 TL.setAttrExprOperand(0);
3668 } else if (TL.hasAttrEnumOperand())
3669 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3671 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
3672 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3674 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3675 SubstTemplateTypeParmTypeLoc TL) {
3676 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3678 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3679 SubstTemplateTypeParmPackTypeLoc TL) {
3680 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3682 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3683 TemplateSpecializationTypeLoc TL) {
3684 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3685 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3686 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3687 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3689 Reader.GetTemplateArgumentLocInfo(F,
3690 TL.getTypePtr()->getArg(i).getKind(),
3693 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3694 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3695 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3697 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
3698 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3699 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3701 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
3702 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3704 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
3705 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3706 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3707 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3709 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3710 DependentTemplateSpecializationTypeLoc TL) {
3711 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3712 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
3713 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3714 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3715 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3716 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3718 Reader.GetTemplateArgumentLocInfo(F,
3719 TL.getTypePtr()->getArg(I).getKind(),
3722 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3723 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3725 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
3726 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3728 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3729 TL.setHasBaseTypeAsWritten(Record[Idx++]);
3730 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3731 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
3732 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
3733 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
3735 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
3736 TL.setStarLoc(ReadSourceLocation(Record, Idx));
3739 TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
3740 const RecordData &Record,
3742 QualType InfoTy = GetType(Record[Idx++]);
3743 if (InfoTy.isNull())
3746 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
3747 TypeLocReader TLR(*this, F, Record, Idx);
3748 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
3753 QualType ASTReader::GetType(TypeID ID) {
3754 unsigned FastQuals = ID & Qualifiers::FastMask;
3755 unsigned Index = ID >> Qualifiers::FastWidth;
3757 if (Index < NUM_PREDEF_TYPE_IDS) {
3759 switch ((PredefinedTypeIDs)Index) {
3760 case PREDEF_TYPE_NULL_ID: return QualType();
3761 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3762 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
3764 case PREDEF_TYPE_CHAR_U_ID:
3765 case PREDEF_TYPE_CHAR_S_ID:
3766 // FIXME: Check that the signedness of CharTy is correct!
3767 T = Context->CharTy;
3770 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3771 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3772 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3773 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3774 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3775 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3776 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3777 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3778 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3779 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3780 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3781 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3782 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3783 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3784 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3785 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3786 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3787 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
3788 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3789 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
3790 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3791 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3792 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3793 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3794 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3795 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
3798 assert(!T.isNull() && "Unknown predefined type");
3799 return T.withFastQualifiers(FastQuals);
3802 Index -= NUM_PREDEF_TYPE_IDS;
3803 assert(Index < TypesLoaded.size() && "Type index out-of-range");
3804 if (TypesLoaded[Index].isNull()) {
3805 TypesLoaded[Index] = ReadTypeRecord(Index);
3806 if (TypesLoaded[Index].isNull())
3809 TypesLoaded[Index]->setFromAST();
3810 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
3811 if (DeserializationListener)
3812 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
3813 TypesLoaded[Index]);
3816 return TypesLoaded[Index].withFastQualifiers(FastQuals);
3819 TypeID ASTReader::GetTypeID(QualType T) const {
3820 return MakeTypeID(T,
3821 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3824 TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3827 assert(!T.getLocalFastQualifiers());
3829 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3830 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3831 // comparing keys of ASTDeclContextNameLookupTable.
3832 // If the type didn't come from the AST file use a specially marked index
3833 // so that any hash/key comparison fail since no such index is stored
3835 if (I == TypeIdxs.end())
3840 unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3841 unsigned Result = 0;
3842 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3843 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3848 TemplateArgumentLocInfo
3849 ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3850 TemplateArgument::ArgKind Kind,
3851 const RecordData &Record,
3854 case TemplateArgument::Expression:
3856 case TemplateArgument::Type:
3857 return GetTypeSourceInfo(F, Record, Index);
3858 case TemplateArgument::Template: {
3859 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3861 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3862 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3865 case TemplateArgument::TemplateExpansion: {
3866 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3868 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
3869 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3870 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
3873 case TemplateArgument::Null:
3874 case TemplateArgument::Integral:
3875 case TemplateArgument::Declaration:
3876 case TemplateArgument::Pack:
3877 return TemplateArgumentLocInfo();
3879 llvm_unreachable("unexpected template argument loc");
3880 return TemplateArgumentLocInfo();
3884 ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
3885 const RecordData &Record, unsigned &Index) {
3886 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
3888 if (Arg.getKind() == TemplateArgument::Expression) {
3889 if (Record[Index++]) // bool InfoHasSameExpr.
3890 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3892 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
3896 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
3901 ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3906 uint64_t Offset = 0;
3907 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3908 PerFileData &F = *Chain[N - I - 1];
3910 if (ID < F.LocalNumCXXBaseSpecifiers)
3911 return Offset + F.CXXBaseSpecifiersOffsets[ID];
3913 ID -= F.LocalNumCXXBaseSpecifiers;
3914 Offset += F.SizeInBits;
3917 assert(false && "CXXBaseSpecifiers not found");
3921 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3922 // Figure out which AST file contains this offset.
3924 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3925 if (Offset < Chain[N - I - 1]->SizeInBits) {
3926 F = Chain[N - I - 1];
3930 Offset -= Chain[N - I - 1]->SizeInBits;
3934 Error("Malformed AST file: C++ base specifiers at impossible offset");
3938 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3939 SavedStreamPosition SavedPosition(Cursor);
3940 Cursor.JumpToBit(Offset);
3941 ReadingKindTracker ReadingKind(Read_Decl, *this);
3943 unsigned Code = Cursor.ReadCode();
3944 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3945 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3946 Error("Malformed AST file: missing C++ base specifiers");
3951 unsigned NumBases = Record[Idx++];
3952 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3953 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3954 for (unsigned I = 0; I != NumBases; ++I)
3955 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3959 TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
3960 if (!DeclsLoaded[0]) {
3961 ReadDeclRecord(0, 1);
3962 if (DeserializationListener)
3963 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
3966 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3969 Decl *ASTReader::GetDecl(DeclID ID) {
3973 if (ID > DeclsLoaded.size()) {
3974 Error("declaration ID out-of-range for AST file");
3978 unsigned Index = ID - 1;
3979 if (!DeclsLoaded[Index]) {
3980 ReadDeclRecord(Index, ID);
3981 if (DeserializationListener)
3982 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3985 return DeclsLoaded[Index];
3988 /// \brief Resolve the offset of a statement into a statement.
3990 /// This operation will read a new statement from the external
3991 /// source each time it is called, and is meant to be used via a
3992 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
3993 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
3994 // Switch case IDs are per Decl.
3995 ClearSwitchCaseIDs();
3997 // Offset here is a global offset across the entire chain.
3998 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3999 PerFileData &F = *Chain[N - I - 1];
4000 if (Offset < F.SizeInBits) {
4001 // Since we know that this statement is part of a decl, make sure to use
4002 // the decl cursor to read it.
4003 F.DeclsCursor.JumpToBit(Offset);
4004 return ReadStmtFromStream(F);
4006 Offset -= F.SizeInBits;
4008 llvm_unreachable("Broken chain");
4011 ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
4012 bool (*isKindWeWant)(Decl::Kind),
4013 llvm::SmallVectorImpl<Decl*> &Decls) {
4014 // There might be lexical decls in multiple parts of the chain, for the TU
4016 // DeclContextOffsets might reallocate as we load additional decls below,
4017 // so make a copy of the vector.
4018 DeclContextInfos Infos = DeclContextOffsets[DC];
4019 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4021 // IDs can be 0 if this context doesn't contain declarations.
4022 if (!I->LexicalDecls)
4025 // Load all of the declaration IDs
4026 for (const KindDeclIDPair *ID = I->LexicalDecls,
4027 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4028 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4031 Decl *D = GetDecl(ID->second);
4032 assert(D && "Null decl in lexical decls");
4037 ++NumLexicalDeclContextsRead;
4041 DeclContext::lookup_result
4042 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
4043 DeclarationName Name) {
4044 assert(DC->hasExternalVisibleStorage() &&
4045 "DeclContext has no visible decls in storage");
4047 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4048 DeclContext::lookup_iterator(0));
4050 llvm::SmallVector<NamedDecl *, 64> Decls;
4051 // There might be visible decls in multiple parts of the chain, for the TU
4052 // and namespaces. For any given name, the last available results replace
4053 // all earlier ones. For this reason, we walk in reverse.
4054 DeclContextInfos &Infos = DeclContextOffsets[DC];
4055 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
4057 if (!I->NameLookupTableData)
4060 ASTDeclContextNameLookupTable *LookupTable =
4061 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4062 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4063 if (Pos == LookupTable->end())
4066 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4067 for (; Data.first != Data.second; ++Data.first)
4068 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4072 ++NumVisibleDeclContextsRead;
4074 SetExternalVisibleDeclsForName(DC, Name, Decls);
4075 return const_cast<DeclContext*>(DC)->lookup(Name);
4078 void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4079 assert(DC->hasExternalVisibleStorage() &&
4080 "DeclContext has no visible decls in storage");
4082 llvm::SmallVector<NamedDecl *, 64> Decls;
4083 // There might be visible decls in multiple parts of the chain, for the TU
4085 DeclContextInfos &Infos = DeclContextOffsets[DC];
4086 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4088 if (!I->NameLookupTableData)
4091 ASTDeclContextNameLookupTable *LookupTable =
4092 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4093 for (ASTDeclContextNameLookupTable::item_iterator
4094 ItemI = LookupTable->item_begin(),
4095 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4096 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4098 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4100 for (; Data.first != Data.second; ++Data.first)
4101 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4102 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4107 void ASTReader::PassInterestingDeclsToConsumer() {
4109 while (!InterestingDecls.empty()) {
4110 DeclGroupRef DG(InterestingDecls.front());
4111 InterestingDecls.pop_front();
4112 Consumer->HandleInterestingDecl(DG);
4116 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
4117 this->Consumer = Consumer;
4122 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
4123 // Force deserialization of this decl, which will cause it to be queued for
4124 // passing to the consumer.
4125 GetDecl(ExternalDefinitions[I]);
4128 PassInterestingDeclsToConsumer();
4131 void ASTReader::PrintStats() {
4132 std::fprintf(stderr, "*** AST File Statistics:\n");
4134 unsigned NumTypesLoaded
4135 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
4137 unsigned NumDeclsLoaded
4138 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4140 unsigned NumIdentifiersLoaded
4141 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4142 IdentifiersLoaded.end(),
4143 (IdentifierInfo *)0);
4144 unsigned NumSelectorsLoaded
4145 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4146 SelectorsLoaded.end(),
4149 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4150 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
4151 if (TotalNumSLocEntries)
4152 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4153 NumSLocEntriesRead, TotalNumSLocEntries,
4154 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
4155 if (!TypesLoaded.empty())
4156 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
4157 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4158 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4159 if (!DeclsLoaded.empty())
4160 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
4161 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4162 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
4163 if (!IdentifiersLoaded.empty())
4164 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
4165 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4166 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
4167 if (!SelectorsLoaded.empty())
4168 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
4169 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4170 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
4171 if (TotalNumStatements)
4172 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4173 NumStatementsRead, TotalNumStatements,
4174 ((float)NumStatementsRead/TotalNumStatements * 100));
4176 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4177 NumMacrosRead, TotalNumMacros,
4178 ((float)NumMacrosRead/TotalNumMacros * 100));
4179 if (TotalLexicalDeclContexts)
4180 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4181 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4182 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4184 if (TotalVisibleDeclContexts)
4185 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4186 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4187 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4189 if (TotalNumMethodPoolEntries) {
4190 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
4191 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4192 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
4194 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
4196 std::fprintf(stderr, "\n");
4199 /// Return the amount of memory used by memory buffers, breaking down
4200 /// by heap-backed versus mmap'ed memory.
4201 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4202 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4203 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4204 size_t bytes = buf->getBufferSize();
4205 switch (buf->getBufferKind()) {
4206 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4207 sizes.malloc_bytes += bytes;
4209 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4210 sizes.mmap_bytes += bytes;
4216 void ASTReader::InitializeSema(Sema &S) {
4218 S.ExternalSource = this;
4220 // Makes sure any declarations that were deserialized "too early"
4221 // still get added to the identifier's declaration chains.
4222 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4223 if (SemaObj->TUScope)
4224 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
4226 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
4228 PreloadedDecls.clear();
4230 // If there were any tentative definitions, deserialize them and add
4231 // them to Sema's list of tentative definitions.
4232 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4233 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
4234 SemaObj->TentativeDefinitions.push_back(Var);
4237 // If there were any unused file scoped decls, deserialize them and add to
4238 // Sema's list of unused file scoped decls.
4239 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4240 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4241 SemaObj->UnusedFileScopedDecls.push_back(D);
4244 // If there were any delegating constructors, add them to Sema's list
4245 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4246 CXXConstructorDecl *D
4247 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4248 SemaObj->DelegatingCtorDecls.push_back(D);
4251 // If there were any locally-scoped external declarations,
4252 // deserialize them and add them to Sema's table of locally-scoped
4253 // external declarations.
4254 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4255 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4256 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4259 // If there were any ext_vector type declarations, deserialize them
4260 // and add them to Sema's vector of such declarations.
4261 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4262 SemaObj->ExtVectorDecls.push_back(
4263 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
4265 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4266 // Can we cut them down before writing them ?
4268 // If there were any dynamic classes declarations, deserialize them
4269 // and add them to Sema's vector of such declarations.
4270 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4271 SemaObj->DynamicClasses.push_back(
4272 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
4274 // Load the offsets of the declarations that Sema references.
4275 // They will be lazily deserialized when needed.
4276 if (!SemaDeclRefs.empty()) {
4277 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4278 SemaObj->StdNamespace = SemaDeclRefs[0];
4279 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4282 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4284 // If there are @selector references added them to its pool. This is for
4285 // implementation of -Wselector.
4286 if (!F->ReferencedSelectorsData.empty()) {
4287 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4289 while (I < DataSize) {
4290 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4291 SourceLocation SelLoc = ReadSourceLocation(
4292 *F, F->ReferencedSelectorsData, I);
4293 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4298 // The special data sets below always come from the most recent PCH,
4299 // which is at the front of the chain.
4300 PerFileData &F = *Chain.front();
4302 // If there were any pending implicit instantiations, deserialize them
4303 // and add them to Sema's queue of such instantiations.
4304 assert(F.PendingInstantiations.size() % 2 == 0 &&
4305 "Expected pairs of entries");
4306 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4307 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4308 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4309 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4312 // If there were any weak undeclared identifiers, deserialize them and add to
4313 // Sema's list of weak undeclared identifiers.
4314 if (!WeakUndeclaredIdentifiers.empty()) {
4316 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4317 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4318 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4319 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4320 bool Used = WeakUndeclaredIdentifiers[Idx++];
4321 Sema::WeakInfo WI(AliasId, Loc);
4323 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4327 // If there were any VTable uses, deserialize the information and add it
4328 // to Sema's vector and map of VTable uses.
4329 if (!VTableUses.empty()) {
4331 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4332 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4333 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4334 bool DefinitionRequired = VTableUses[Idx++];
4335 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4336 SemaObj->VTablesUsed[Class] = DefinitionRequired;
4340 if (!FPPragmaOptions.empty()) {
4341 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4342 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4345 if (!OpenCLExtensions.empty()) {
4347 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4348 #include "clang/Basic/OpenCLExtensions.def"
4350 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4354 IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
4355 // Try to find this name within our on-disk hash tables. We start with the
4356 // most recent one, since that one contains the most up-to-date info.
4357 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4358 ASTIdentifierLookupTable *IdTable
4359 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
4362 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
4363 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4364 if (Pos == IdTable->end())
4367 // Dereferencing the iterator has the effect of building the
4368 // IdentifierInfo node and populating it with the various
4369 // declarations it needs.
4376 /// \brief An identifier-lookup iterator that enumerates all of the
4377 /// identifiers stored within a set of AST files.
4378 class ASTIdentifierIterator : public IdentifierIterator {
4379 /// \brief The AST reader whose identifiers are being enumerated.
4380 const ASTReader &Reader;
4382 /// \brief The current index into the chain of AST files stored in
4386 /// \brief The current position within the identifier lookup table
4387 /// of the current AST file.
4388 ASTIdentifierLookupTable::key_iterator Current;
4390 /// \brief The end position within the identifier lookup table of
4391 /// the current AST file.
4392 ASTIdentifierLookupTable::key_iterator End;
4395 explicit ASTIdentifierIterator(const ASTReader &Reader);
4397 virtual llvm::StringRef Next();
4401 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4402 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4403 ASTIdentifierLookupTable *IdTable
4404 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4405 Current = IdTable->key_begin();
4406 End = IdTable->key_end();
4409 llvm::StringRef ASTIdentifierIterator::Next() {
4410 while (Current == End) {
4411 // If we have exhausted all of our AST files, we're done.
4413 return llvm::StringRef();
4416 ASTIdentifierLookupTable *IdTable
4417 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4418 Current = IdTable->key_begin();
4419 End = IdTable->key_end();
4422 // We have any identifiers remaining in the current AST file; return
4424 std::pair<const char*, unsigned> Key = *Current;
4426 return llvm::StringRef(Key.first, Key.second);
4429 IdentifierIterator *ASTReader::getIdentifiers() const {
4430 return new ASTIdentifierIterator(*this);
4433 std::pair<ObjCMethodList, ObjCMethodList>
4434 ASTReader::ReadMethodPool(Selector Sel) {
4435 // Find this selector in a hash table. We want to find the most recent entry.
4436 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4437 PerFileData &F = *Chain[I];
4438 if (!F.SelectorLookupTable)
4441 ASTSelectorLookupTable *PoolTable
4442 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4443 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
4444 if (Pos != PoolTable->end()) {
4446 // FIXME: Not quite happy with the statistics here. We probably should
4447 // disable this tracking when called via LoadSelector.
4448 // Also, should entries without methods count as misses?
4449 ++NumMethodPoolEntriesRead;
4450 ASTSelectorLookupTrait::data_type Data = *Pos;
4451 if (DeserializationListener)
4452 DeserializationListener->SelectorRead(Data.ID, Sel);
4453 return std::make_pair(Data.Instance, Data.Factory);
4457 ++NumMethodPoolMisses;
4458 return std::pair<ObjCMethodList, ObjCMethodList>();
4461 void ASTReader::ReadKnownNamespaces(
4462 llvm::SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4465 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4466 if (NamespaceDecl *Namespace
4467 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4468 Namespaces.push_back(Namespace);
4472 void ASTReader::LoadSelector(Selector Sel) {
4473 // It would be complicated to avoid reading the methods anyway. So don't.
4474 ReadMethodPool(Sel);
4477 void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
4478 assert(ID && "Non-zero identifier ID required");
4479 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
4480 IdentifiersLoaded[ID - 1] = II;
4481 if (DeserializationListener)
4482 DeserializationListener->IdentifierRead(ID, II);
4485 /// \brief Set the globally-visible declarations associated with the given
4488 /// If the AST reader is currently in a state where the given declaration IDs
4489 /// cannot safely be resolved, they are queued until it is safe to resolve
4492 /// \param II an IdentifierInfo that refers to one or more globally-visible
4495 /// \param DeclIDs the set of declaration IDs with the name @p II that are
4496 /// visible at global scope.
4498 /// \param Nonrecursive should be true to indicate that the caller knows that
4499 /// this call is non-recursive, and therefore the globally-visible declarations
4500 /// will not be placed onto the pending queue.
4502 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
4503 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4504 bool Nonrecursive) {
4505 if (NumCurrentElementsDeserializing && !Nonrecursive) {
4506 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4507 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4509 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
4513 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4514 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4516 if (SemaObj->TUScope) {
4517 // Introduce this declaration into the translation-unit scope
4518 // and add it to the declaration chain for this identifier, so
4519 // that (unqualified) name lookup will find it.
4520 SemaObj->TUScope->AddDecl(D);
4522 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
4524 // Queue this declaration so that it will be added to the
4525 // translation unit scope and identifier's declaration chain
4526 // once a Sema object is known.
4527 PreloadedDecls.push_back(D);
4532 IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
4536 if (IdentifiersLoaded.empty()) {
4537 Error("no identifier table in AST file");
4541 assert(PP && "Forgot to set Preprocessor ?");
4543 if (!IdentifiersLoaded[ID]) {
4544 unsigned Index = ID;
4545 const char *Str = 0;
4546 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4547 PerFileData *F = Chain[N - I - 1];
4548 if (Index < F->LocalNumIdentifiers) {
4549 uint32_t Offset = F->IdentifierOffsets[Index];
4550 Str = F->IdentifierTableData + Offset;
4553 Index -= F->LocalNumIdentifiers;
4555 assert(Str && "Broken Chain");
4557 // All of the strings in the AST file are preceded by a 16-bit length.
4558 // Extract that 16-bit length to avoid having to execute strlen().
4559 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4560 // unsigned integers. This is important to avoid integer overflow when
4561 // we cast them to 'unsigned'.
4562 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
4563 unsigned StrLen = (((unsigned) StrLenPtr[0])
4564 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
4565 IdentifiersLoaded[ID]
4566 = &PP->getIdentifierTable().get(llvm::StringRef(Str, StrLen));
4567 if (DeserializationListener)
4568 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
4571 return IdentifiersLoaded[ID];
4574 bool ASTReader::ReadSLocEntry(unsigned ID) {
4575 return ReadSLocEntryRecord(ID) != Success;
4578 Selector ASTReader::DecodeSelector(unsigned ID) {
4582 if (ID > SelectorsLoaded.size()) {
4583 Error("selector ID out of range in AST file");
4587 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
4588 // Load this selector from the selector table.
4589 unsigned Idx = ID - 1;
4590 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4591 PerFileData &F = *Chain[N - I - 1];
4592 if (Idx < F.LocalNumSelectors) {
4593 ASTSelectorLookupTrait Trait(*this);
4594 SelectorsLoaded[ID - 1] =
4595 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4596 if (DeserializationListener)
4597 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4600 Idx -= F.LocalNumSelectors;
4604 return SelectorsLoaded[ID - 1];
4607 Selector ASTReader::GetExternalSelector(uint32_t ID) {
4608 return DecodeSelector(ID);
4611 uint32_t ASTReader::GetNumExternalSelectors() {
4612 // ID 0 (the null selector) is considered an external selector.
4613 return getTotalNumSelectors() + 1;
4617 ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
4618 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4620 case DeclarationName::Identifier:
4621 return DeclarationName(GetIdentifierInfo(Record, Idx));
4623 case DeclarationName::ObjCZeroArgSelector:
4624 case DeclarationName::ObjCOneArgSelector:
4625 case DeclarationName::ObjCMultiArgSelector:
4626 return DeclarationName(GetSelector(Record, Idx));
4628 case DeclarationName::CXXConstructorName:
4629 return Context->DeclarationNames.getCXXConstructorName(
4630 Context->getCanonicalType(GetType(Record[Idx++])));
4632 case DeclarationName::CXXDestructorName:
4633 return Context->DeclarationNames.getCXXDestructorName(
4634 Context->getCanonicalType(GetType(Record[Idx++])));
4636 case DeclarationName::CXXConversionFunctionName:
4637 return Context->DeclarationNames.getCXXConversionFunctionName(
4638 Context->getCanonicalType(GetType(Record[Idx++])));
4640 case DeclarationName::CXXOperatorName:
4641 return Context->DeclarationNames.getCXXOperatorName(
4642 (OverloadedOperatorKind)Record[Idx++]);
4644 case DeclarationName::CXXLiteralOperatorName:
4645 return Context->DeclarationNames.getCXXLiteralOperatorName(
4646 GetIdentifierInfo(Record, Idx));
4648 case DeclarationName::CXXUsingDirective:
4649 return DeclarationName::getUsingDirectiveName();
4652 // Required to silence GCC warning
4653 return DeclarationName();
4656 void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4657 DeclarationNameLoc &DNLoc,
4658 DeclarationName Name,
4659 const RecordData &Record, unsigned &Idx) {
4660 switch (Name.getNameKind()) {
4661 case DeclarationName::CXXConstructorName:
4662 case DeclarationName::CXXDestructorName:
4663 case DeclarationName::CXXConversionFunctionName:
4664 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4667 case DeclarationName::CXXOperatorName:
4668 DNLoc.CXXOperatorName.BeginOpNameLoc
4669 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4670 DNLoc.CXXOperatorName.EndOpNameLoc
4671 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4674 case DeclarationName::CXXLiteralOperatorName:
4675 DNLoc.CXXLiteralOperatorName.OpNameLoc
4676 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4679 case DeclarationName::Identifier:
4680 case DeclarationName::ObjCZeroArgSelector:
4681 case DeclarationName::ObjCOneArgSelector:
4682 case DeclarationName::ObjCMultiArgSelector:
4683 case DeclarationName::CXXUsingDirective:
4688 void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4689 DeclarationNameInfo &NameInfo,
4690 const RecordData &Record, unsigned &Idx) {
4691 NameInfo.setName(ReadDeclarationName(Record, Idx));
4692 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4693 DeclarationNameLoc DNLoc;
4694 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4695 NameInfo.setInfo(DNLoc);
4698 void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4699 const RecordData &Record, unsigned &Idx) {
4700 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
4701 unsigned NumTPLists = Record[Idx++];
4702 Info.NumTemplParamLists = NumTPLists;
4704 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4705 for (unsigned i=0; i != NumTPLists; ++i)
4706 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4711 ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4713 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
4715 case TemplateName::Template:
4716 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4718 case TemplateName::OverloadedTemplate: {
4719 unsigned size = Record[Idx++];
4720 UnresolvedSet<8> Decls;
4722 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4724 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4727 case TemplateName::QualifiedTemplate: {
4728 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4729 bool hasTemplKeyword = Record[Idx++];
4730 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4731 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4734 case TemplateName::DependentTemplate: {
4735 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4736 if (Record[Idx++]) // isIdentifier
4737 return Context->getDependentTemplateName(NNS,
4738 GetIdentifierInfo(Record, Idx));
4739 return Context->getDependentTemplateName(NNS,
4740 (OverloadedOperatorKind)Record[Idx++]);
4743 case TemplateName::SubstTemplateTemplateParm: {
4744 TemplateTemplateParmDecl *param
4745 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4746 if (!param) return TemplateName();
4747 TemplateName replacement = ReadTemplateName(F, Record, Idx);
4748 return Context->getSubstTemplateTemplateParm(param, replacement);
4751 case TemplateName::SubstTemplateTemplateParmPack: {
4752 TemplateTemplateParmDecl *Param
4753 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4755 return TemplateName();
4757 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4758 if (ArgPack.getKind() != TemplateArgument::Pack)
4759 return TemplateName();
4761 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4765 assert(0 && "Unhandled template name kind!");
4766 return TemplateName();
4770 ASTReader::ReadTemplateArgument(PerFileData &F,
4771 const RecordData &Record, unsigned &Idx) {
4772 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4774 case TemplateArgument::Null:
4775 return TemplateArgument();
4776 case TemplateArgument::Type:
4777 return TemplateArgument(GetType(Record[Idx++]));
4778 case TemplateArgument::Declaration:
4779 return TemplateArgument(GetDecl(Record[Idx++]));
4780 case TemplateArgument::Integral: {
4781 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4782 QualType T = GetType(Record[Idx++]);
4783 return TemplateArgument(Value, T);
4785 case TemplateArgument::Template:
4786 return TemplateArgument(ReadTemplateName(F, Record, Idx));
4787 case TemplateArgument::TemplateExpansion: {
4788 TemplateName Name = ReadTemplateName(F, Record, Idx);
4789 llvm::Optional<unsigned> NumTemplateExpansions;
4790 if (unsigned NumExpansions = Record[Idx++])
4791 NumTemplateExpansions = NumExpansions - 1;
4792 return TemplateArgument(Name, NumTemplateExpansions);
4794 case TemplateArgument::Expression:
4795 return TemplateArgument(ReadExpr(F));
4796 case TemplateArgument::Pack: {
4797 unsigned NumArgs = Record[Idx++];
4798 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4799 for (unsigned I = 0; I != NumArgs; ++I)
4800 Args[I] = ReadTemplateArgument(F, Record, Idx);
4801 return TemplateArgument(Args, NumArgs);
4805 assert(0 && "Unhandled template argument kind!");
4806 return TemplateArgument();
4809 TemplateParameterList *
4810 ASTReader::ReadTemplateParameterList(PerFileData &F,
4811 const RecordData &Record, unsigned &Idx) {
4812 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4813 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4814 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
4816 unsigned NumParams = Record[Idx++];
4817 llvm::SmallVector<NamedDecl *, 16> Params;
4818 Params.reserve(NumParams);
4820 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
4822 TemplateParameterList* TemplateParams =
4823 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4824 Params.data(), Params.size(), RAngleLoc);
4825 return TemplateParams;
4830 ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
4831 PerFileData &F, const RecordData &Record,
4833 unsigned NumTemplateArgs = Record[Idx++];
4834 TemplArgs.reserve(NumTemplateArgs);
4835 while (NumTemplateArgs--)
4836 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
4839 /// \brief Read a UnresolvedSet structure.
4840 void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
4841 const RecordData &Record, unsigned &Idx) {
4842 unsigned NumDecls = Record[Idx++];
4843 while (NumDecls--) {
4844 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4845 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4851 ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
4852 const RecordData &Record, unsigned &Idx) {
4853 bool isVirtual = static_cast<bool>(Record[Idx++]);
4854 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4855 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
4856 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
4857 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4858 SourceRange Range = ReadSourceRange(F, Record, Idx);
4859 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4860 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
4862 Result.setInheritConstructors(inheritConstructors);
4866 std::pair<CXXCtorInitializer **, unsigned>
4867 ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4869 CXXCtorInitializer **CtorInitializers = 0;
4870 unsigned NumInitializers = Record[Idx++];
4871 if (NumInitializers) {
4872 ASTContext &C = *getContext();
4875 = new (C) CXXCtorInitializer*[NumInitializers];
4876 for (unsigned i=0; i != NumInitializers; ++i) {
4877 TypeSourceInfo *BaseClassInfo = 0;
4878 bool IsBaseVirtual = false;
4879 FieldDecl *Member = 0;
4880 IndirectFieldDecl *IndirectMember = 0;
4881 CXXConstructorDecl *Target = 0;
4883 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4885 case CTOR_INITIALIZER_BASE:
4886 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
4887 IsBaseVirtual = Record[Idx++];
4890 case CTOR_INITIALIZER_DELEGATING:
4891 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4894 case CTOR_INITIALIZER_MEMBER:
4895 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4898 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4899 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4903 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
4904 Expr *Init = ReadExpr(F);
4905 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4906 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
4907 bool IsWritten = Record[Idx++];
4908 unsigned SourceOrderOrNumArrayIndices;
4909 llvm::SmallVector<VarDecl *, 8> Indices;
4911 SourceOrderOrNumArrayIndices = Record[Idx++];
4913 SourceOrderOrNumArrayIndices = Record[Idx++];
4914 Indices.reserve(SourceOrderOrNumArrayIndices);
4915 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4916 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4919 CXXCtorInitializer *BOMInit;
4920 if (Type == CTOR_INITIALIZER_BASE) {
4921 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4922 LParenLoc, Init, RParenLoc,
4923 MemberOrEllipsisLoc);
4924 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4925 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4926 Target, Init, RParenLoc);
4927 } else if (IsWritten) {
4929 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4930 LParenLoc, Init, RParenLoc);
4932 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4933 MemberOrEllipsisLoc, LParenLoc,
4936 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4937 LParenLoc, Init, RParenLoc,
4938 Indices.data(), Indices.size());
4942 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
4943 CtorInitializers[i] = BOMInit;
4947 return std::make_pair(CtorInitializers, NumInitializers);
4950 NestedNameSpecifier *
4951 ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
4952 unsigned N = Record[Idx++];
4953 NestedNameSpecifier *NNS = 0, *Prev = 0;
4954 for (unsigned I = 0; I != N; ++I) {
4955 NestedNameSpecifier::SpecifierKind Kind
4956 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4958 case NestedNameSpecifier::Identifier: {
4959 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4960 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4964 case NestedNameSpecifier::Namespace: {
4965 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4966 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4970 case NestedNameSpecifier::NamespaceAlias: {
4971 NamespaceAliasDecl *Alias
4972 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4973 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4977 case NestedNameSpecifier::TypeSpec:
4978 case NestedNameSpecifier::TypeSpecWithTemplate: {
4979 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
4983 bool Template = Record[Idx++];
4984 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4988 case NestedNameSpecifier::Global: {
4989 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4990 // No associated value, and there can't be a prefix.
4999 NestedNameSpecifierLoc
5000 ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
5002 unsigned N = Record[Idx++];
5003 NestedNameSpecifierLocBuilder Builder;
5004 for (unsigned I = 0; I != N; ++I) {
5005 NestedNameSpecifier::SpecifierKind Kind
5006 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5008 case NestedNameSpecifier::Identifier: {
5009 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
5010 SourceRange Range = ReadSourceRange(F, Record, Idx);
5011 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
5015 case NestedNameSpecifier::Namespace: {
5016 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
5017 SourceRange Range = ReadSourceRange(F, Record, Idx);
5018 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
5022 case NestedNameSpecifier::NamespaceAlias: {
5023 NamespaceAliasDecl *Alias
5024 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
5025 SourceRange Range = ReadSourceRange(F, Record, Idx);
5026 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
5030 case NestedNameSpecifier::TypeSpec:
5031 case NestedNameSpecifier::TypeSpecWithTemplate: {
5032 bool Template = Record[Idx++];
5033 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5035 return NestedNameSpecifierLoc();
5036 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5038 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5039 Builder.Extend(*Context,
5040 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5041 T->getTypeLoc(), ColonColonLoc);
5045 case NestedNameSpecifier::Global: {
5046 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
5047 Builder.MakeGlobal(*Context, ColonColonLoc);
5053 return Builder.getWithLocInContext(*Context);
5057 ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5059 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5060 SourceLocation end = ReadSourceLocation(F, Record, Idx);
5061 return SourceRange(beg, end);
5064 /// \brief Read an integral value
5065 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
5066 unsigned BitWidth = Record[Idx++];
5067 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5068 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5073 /// \brief Read a signed integral value
5074 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
5075 bool isUnsigned = Record[Idx++];
5076 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5079 /// \brief Read a floating-point value
5080 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
5081 return llvm::APFloat(ReadAPInt(Record, Idx));
5084 // \brief Read a string
5085 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
5086 unsigned Len = Record[Idx++];
5087 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
5092 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5094 unsigned Major = Record[Idx++];
5095 unsigned Minor = Record[Idx++];
5096 unsigned Subminor = Record[Idx++];
5098 return VersionTuple(Major);
5100 return VersionTuple(Major, Minor - 1);
5101 return VersionTuple(Major, Minor - 1, Subminor - 1);
5104 CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
5106 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5107 return CXXTemporary::Create(*Context, Decl);
5110 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
5111 return Diag(SourceLocation(), DiagID);
5114 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
5115 return Diags.Report(Loc, DiagID);
5118 /// \brief Retrieve the identifier table associated with the
5120 IdentifierTable &ASTReader::getIdentifierTable() {
5121 assert(PP && "Forgot to set Preprocessor ?");
5122 return PP->getIdentifierTable();
5125 /// \brief Record that the given ID maps to the given switch-case
5127 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
5128 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5129 SwitchCaseStmts[ID] = SC;
5132 /// \brief Retrieve the switch-case statement with the given ID.
5133 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
5134 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5135 return SwitchCaseStmts[ID];
5138 void ASTReader::ClearSwitchCaseIDs() {
5139 SwitchCaseStmts.clear();
5142 void ASTReader::FinishedDeserializing() {
5143 assert(NumCurrentElementsDeserializing &&
5144 "FinishedDeserializing not paired with StartedDeserializing");
5145 if (NumCurrentElementsDeserializing == 1) {
5146 // If any identifiers with corresponding top-level declarations have
5147 // been loaded, load those declarations now.
5148 while (!PendingIdentifierInfos.empty()) {
5149 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5150 PendingIdentifierInfos.front().DeclIDs, true);
5151 PendingIdentifierInfos.pop_front();
5154 // Ready to load previous declarations of Decls that were delayed.
5155 while (!PendingPreviousDecls.empty()) {
5156 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5157 PendingPreviousDecls.front().second);
5158 PendingPreviousDecls.pop_front();
5161 // We are not in recursive loading, so it's safe to pass the "interesting"
5162 // decls to the consumer.
5164 PassInterestingDeclsToConsumer();
5166 assert(PendingForwardRefs.size() == 0 &&
5167 "Some forward refs did not get linked to the definition!");
5169 --NumCurrentElementsDeserializing;
5172 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
5173 const char *isysroot, bool DisableValidation,
5174 bool DisableStatCache)
5175 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5176 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5177 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5178 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
5179 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5180 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5181 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5182 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5183 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5184 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5185 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5186 NumCurrentElementsDeserializing(0)
5188 RelocatablePCH = false;
5191 ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5192 Diagnostic &Diags, const char *isysroot,
5193 bool DisableValidation, bool DisableStatCache)
5194 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5195 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
5196 isysroot(isysroot), DisableValidation(DisableValidation),
5197 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5198 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
5199 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5200 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5201 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5202 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5203 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5204 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
5205 RelocatablePCH = false;
5208 ASTReader::~ASTReader() {
5209 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5210 delete Chain[e - i - 1];
5211 // Delete all visible decl lookup tables
5212 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5213 E = DeclContextOffsets.end();
5215 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5217 if (J->NameLookupTableData)
5218 delete static_cast<ASTDeclContextNameLookupTable*>(
5219 J->NameLookupTableData);
5222 for (DeclContextVisibleUpdatesPending::iterator
5223 I = PendingVisibleUpdates.begin(),
5224 E = PendingVisibleUpdates.end();
5226 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5227 F = I->second.end();
5229 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5233 ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5234 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0),
5235 SLocFileOffsets(0), LocalSLocSize(0),
5236 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5237 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
5238 MacroDefinitionOffsets(0),
5239 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5240 HeaderFileInfoTable(0),
5241 LocalNumSelectors(0), SelectorOffsets(0),
5242 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
5243 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5244 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
5245 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
5248 ASTReader::PerFileData::~PerFileData() {
5249 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
5250 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
5251 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);