LLDB mainline
PdbAstBuilder.cpp
Go to the documentation of this file.
1#include "PdbAstBuilder.h"
2
3#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
4#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
5#include "llvm/DebugInfo/CodeView/RecordName.h"
6#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
7#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
8#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
9#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
10#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
11#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
12#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
13#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
14#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
15#include "llvm/Demangle/MicrosoftDemangle.h"
16
21#include "lldb/Core/Module.h"
24#include "PdbUtil.h"
25#include "UdtRecordCompleter.h"
26#include "SymbolFileNativePDB.h"
27#include <optional>
28
29using namespace lldb_private;
30using namespace lldb_private::npdb;
31using namespace llvm::codeview;
32using namespace llvm::pdb;
33
34namespace {
35struct CreateMethodDecl : public TypeVisitorCallbacks {
36 CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang,
37 TypeIndex func_type_index,
38 clang::FunctionDecl *&function_decl,
40 llvm::StringRef proc_name, CompilerType func_ct)
41 : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index),
42 function_decl(function_decl), parent_ty(parent_ty),
43 proc_name(proc_name), func_ct(func_ct) {}
44 PdbIndex &m_index;
45 TypeSystemClang &m_clang;
46 TypeIndex func_type_index;
47 clang::FunctionDecl *&function_decl;
49 llvm::StringRef proc_name;
50 CompilerType func_ct;
51
52 llvm::Error visitKnownMember(CVMemberRecord &cvr,
53 OverloadedMethodRecord &overloaded) override {
54 TypeIndex method_list_idx = overloaded.MethodList;
55
56 CVType method_list_type = m_index.tpi().getType(method_list_idx);
57 assert(method_list_type.kind() == LF_METHODLIST);
58
59 MethodOverloadListRecord method_list;
60 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
61 method_list_type, method_list));
62
63 for (const OneMethodRecord &method : method_list.Methods) {
64 if (method.getType().getIndex() == func_type_index.getIndex())
65 AddMethod(overloaded.Name, method.getAccess(), method.getOptions(),
66 method.Attrs);
67 }
68
69 return llvm::Error::success();
70 }
71
72 llvm::Error visitKnownMember(CVMemberRecord &cvr,
73 OneMethodRecord &record) override {
74 AddMethod(record.getName(), record.getAccess(), record.getOptions(),
75 record.Attrs);
76 return llvm::Error::success();
77 }
78
79 void AddMethod(llvm::StringRef name, MemberAccess access,
80 MethodOptions options, MemberAttributes attrs) {
81 if (name != proc_name || function_decl)
82 return;
83 lldb::AccessType access_type = TranslateMemberAccess(access);
84 bool is_virtual = attrs.isVirtual();
85 bool is_static = attrs.isStatic();
86 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
87 MethodOptions::CompilerGenerated;
88 function_decl = m_clang.AddMethodToCXXRecordType(
89 parent_ty, proc_name,
90 /*mangled_name=*/nullptr, func_ct, /*access=*/access_type,
91 /*is_virtual=*/is_virtual, /*is_static=*/is_static,
92 /*is_inline=*/false, /*is_explicit=*/false,
93 /*is_attr_used=*/false, /*is_artificial=*/is_artificial);
94 }
95};
96} // namespace
97
98static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) {
99 switch (cr.Kind) {
100 case TypeRecordKind::Class:
101 return clang::TTK_Class;
102 case TypeRecordKind::Struct:
103 return clang::TTK_Struct;
104 case TypeRecordKind::Union:
105 return clang::TTK_Union;
106 case TypeRecordKind::Interface:
107 return clang::TTK_Interface;
108 case TypeRecordKind::Enum:
109 return clang::TTK_Enum;
110 default:
111 lldbassert(false && "Invalid tag record kind!");
112 return clang::TTK_Struct;
113 }
114}
115
116static bool IsCVarArgsFunction(llvm::ArrayRef<TypeIndex> args) {
117 if (args.empty())
118 return false;
119 return args.back() == TypeIndex::None();
120}
121
122static bool
123AnyScopesHaveTemplateParams(llvm::ArrayRef<llvm::ms_demangle::Node *> scopes) {
124 for (llvm::ms_demangle::Node *n : scopes) {
125 auto *idn = static_cast<llvm::ms_demangle::IdentifierNode *>(n);
126 if (idn->TemplateParams)
127 return true;
128 }
129 return false;
130}
131
132static std::optional<clang::CallingConv>
133TranslateCallingConvention(llvm::codeview::CallingConvention conv) {
134 using CC = llvm::codeview::CallingConvention;
135 switch (conv) {
136
137 case CC::NearC:
138 case CC::FarC:
139 return clang::CallingConv::CC_C;
140 case CC::NearPascal:
141 case CC::FarPascal:
142 return clang::CallingConv::CC_X86Pascal;
143 case CC::NearFast:
144 case CC::FarFast:
145 return clang::CallingConv::CC_X86FastCall;
146 case CC::NearStdCall:
147 case CC::FarStdCall:
148 return clang::CallingConv::CC_X86StdCall;
149 case CC::ThisCall:
150 return clang::CallingConv::CC_X86ThisCall;
151 case CC::NearVector:
152 return clang::CallingConv::CC_X86VectorCall;
153 default:
154 return std::nullopt;
155 }
156}
157
158static bool IsAnonymousNamespaceName(llvm::StringRef name) {
159 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
160}
161
163
166}
167
168std::pair<clang::DeclContext *, std::string>
170 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
172 // FIXME: Move this to GetDeclContextContainingUID.
173 if (!record.hasUniqueName())
174 return CreateDeclInfoForUndecoratedName(record.Name);
175
176 llvm::ms_demangle::Demangler demangler;
177 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
178 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
179 if (demangler.Error)
180 return {m_clang.GetTranslationUnitDecl(), std::string(record.UniqueName)};
181
182 llvm::ms_demangle::IdentifierNode *idn =
183 ttn->QualifiedName->getUnqualifiedIdentifier();
184 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
185
186 llvm::ms_demangle::NodeArrayNode *name_components =
187 ttn->QualifiedName->Components;
188 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
189 name_components->Count - 1);
190
191 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
192
193 // If this type doesn't have a parent type in the debug info, then the best we
194 // can do is to say that it's either a series of namespaces (if the scope is
195 // non-empty), or the translation unit (if the scope is empty).
196 std::optional<TypeIndex> parent_index = pdb->GetParentType(ti);
197 if (!parent_index) {
198 if (scopes.empty())
199 return {context, uname};
200
201 // If there is no parent in the debug info, but some of the scopes have
202 // template params, then this is a case of bad debug info. See, for
203 // example, llvm.org/pr39607. We don't want to create an ambiguity between
204 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
205 // global scope with the fully qualified name.
206 if (AnyScopesHaveTemplateParams(scopes))
207 return {context, std::string(record.Name)};
208
209 for (llvm::ms_demangle::Node *scope : scopes) {
210 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
211 std::string str = nii->toString();
212 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
213 }
214 return {context, uname};
215 }
216
217 // Otherwise, all we need to do is get the parent type of this type and
218 // recurse into our lazy type creation / AST reconstruction logic to get an
219 // LLDB TypeSP for the parent. This will cause the AST to automatically get
220 // the right DeclContext created for any parent.
221 clang::QualType parent_qt = GetOrCreateType(*parent_index);
222 if (parent_qt.isNull())
223 return {nullptr, ""};
224
225 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
226 return {context, uname};
227}
228
229static bool isLocalVariableType(SymbolKind K) {
230 switch (K) {
231 case S_REGISTER:
232 case S_REGREL32:
233 case S_LOCAL:
234 return true;
235 default:
236 break;
237 }
238 return false;
239}
240
242 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
244 PdbIndex &index = pdb->GetIndex();
245 CVSymbol cvs = index.ReadSymbolRecord(id);
246
247 if (isLocalVariableType(cvs.kind())) {
248 clang::DeclContext *scope = GetParentDeclContext(id);
249 if (!scope)
250 return nullptr;
251 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
252 PdbCompilandSymId scope_id =
253 PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym();
254 return GetOrCreateVariableDecl(scope_id, id);
255 }
256
257 switch (cvs.kind()) {
258 case S_GPROC32:
259 case S_LPROC32:
260 return GetOrCreateFunctionDecl(id);
261 case S_GDATA32:
262 case S_LDATA32:
263 case S_GTHREAD32:
264 case S_CONSTANT:
265 // global variable
266 return nullptr;
267 case S_BLOCK32:
268 return GetOrCreateBlockDecl(id);
269 case S_INLINESITE:
271 default:
272 return nullptr;
273 }
274}
275
276std::optional<CompilerDecl>
278 if (clang::Decl *result = TryGetDecl(uid))
279 return ToCompilerDecl(*result);
280
281 clang::Decl *result = nullptr;
282 switch (uid.kind()) {
285 break;
286 case PdbSymUidKind::Type: {
287 clang::QualType qt = GetOrCreateType(uid.asTypeSym());
288 if (qt.isNull())
289 return std::nullopt;
290 if (auto *tag = qt->getAsTagDecl()) {
291 result = tag;
292 break;
293 }
294 return std::nullopt;
295 }
296 default:
297 return std::nullopt;
298 }
299
300 if (!result)
301 return std::nullopt;
302 m_uid_to_decl[toOpaqueUid(uid)] = result;
303 return ToCompilerDecl(*result);
304}
305
307 if (uid.kind() == PdbSymUidKind::CompilandSym) {
308 if (uid.asCompilandSym().offset == 0)
310 }
311 auto option = GetOrCreateDeclForUid(uid);
312 if (!option)
313 return nullptr;
314 clang::Decl *decl = FromCompilerDecl(*option);
315 if (!decl)
316 return nullptr;
317
318 return clang::Decl::castToDeclContext(decl);
319}
320
321std::pair<clang::DeclContext *, std::string>
323 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
325 PdbIndex &index = pdb->GetIndex();
326 MSVCUndecoratedNameParser parser(name);
327 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
328
330
331 llvm::StringRef uname = specs.back().GetBaseName();
332 specs = specs.drop_back();
333 if (specs.empty())
334 return {context, std::string(name)};
335
336 llvm::StringRef scope_name = specs.back().GetFullName();
337
338 // It might be a class name, try that first.
339 std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
340 while (!types.empty()) {
341 clang::QualType qt = GetOrCreateType(types.back());
342 if (qt.isNull())
343 continue;
344 clang::TagDecl *tag = qt->getAsTagDecl();
345 if (tag)
346 return {clang::TagDecl::castToDeclContext(tag), std::string(uname)};
347 types.pop_back();
348 }
349
350 // If that fails, treat it as a series of namespaces.
351 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
352 std::string ns_name = spec.GetBaseName().str();
353 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
354 }
355 return {context, std::string(uname)};
356}
357
359 // We must do this *without* calling GetOrCreate on the current uid, as
360 // that would be an infinite recursion.
361 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
363 PdbIndex& index = pdb->GetIndex();
364 switch (uid.kind()) {
366 std::optional<PdbCompilandSymId> scope =
368 if (scope)
369 return GetOrCreateDeclContextForUid(*scope);
370
371 CVSymbol sym = index.ReadSymbolRecord(uid.asCompilandSym());
372 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
373 }
374 case PdbSymUidKind::Type: {
375 // It could be a namespace, class, or global. We don't support nested
376 // functions yet. Anyway, we just need to consult the parent type map.
377 PdbTypeSymId type_id = uid.asTypeSym();
378 std::optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
379 if (!parent_index)
381 return GetOrCreateDeclContextForUid(PdbTypeSymId(*parent_index));
382 }
384 // In this case the parent DeclContext is the one for the class that this
385 // member is inside of.
386 break;
388 // If this refers to a compiland symbol, just recurse in with that symbol.
389 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
390 // need to parse the undecorated name to figure out the scope, then look
391 // that up in the TPI stream. If it's found, it's a type, othewrise it's
392 // a series of namespaces.
393 // FIXME: do this.
394 CVSymbol global = index.ReadSymbolRecord(uid.asGlobalSym());
395 switch (global.kind()) {
396 case SymbolKind::S_GDATA32:
397 case SymbolKind::S_LDATA32:
398 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;;
399 case SymbolKind::S_PROCREF:
400 case SymbolKind::S_LPROCREF: {
401 ProcRefSym ref{global.kind()};
402 llvm::cantFail(
403 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
404 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
405 return GetParentDeclContext(cu_sym_id);
406 }
407 case SymbolKind::S_CONSTANT:
408 case SymbolKind::S_UDT:
409 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
410 default:
411 break;
412 }
413 break;
414 }
415 default:
416 break;
417 }
419}
420
421bool PdbAstBuilder::CompleteType(clang::QualType qt) {
422 if (qt.isNull())
423 return false;
424 clang::TagDecl *tag = qt->getAsTagDecl();
425 if (qt->isArrayType()) {
426 const clang::Type *element_type = qt->getArrayElementTypeNoTypeQual();
427 tag = element_type->getAsTagDecl();
428 }
429 if (!tag)
430 return false;
431
432 return CompleteTagDecl(*tag);
433}
434
435bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
436 // If this is not in our map, it's an error.
437 auto status_iter = m_decl_to_status.find(&tag);
438 lldbassert(status_iter != m_decl_to_status.end());
439
440 // If it's already complete, just return.
441 DeclStatus &status = status_iter->second;
442 if (status.resolved)
443 return true;
444
445 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
446 PdbIndex &index = static_cast<SymbolFileNativePDB *>(
448 ->GetIndex();
449 lldbassert(IsTagRecord(type_id, index.tpi()));
450
451 clang::QualType tag_qt = m_clang.getASTContext().getTypeDeclType(&tag);
452 TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
453
454 TypeIndex tag_ti = type_id.index;
455 CVType cvt = index.tpi().getType(tag_ti);
456 if (cvt.kind() == LF_MODIFIER)
457 tag_ti = LookThroughModifierRecord(cvt);
458
459 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, index.tpi());
460 cvt = index.tpi().getType(best_ti.index);
462
463 if (IsForwardRefUdt(cvt)) {
464 // If we can't find a full decl for this forward ref anywhere in the debug
465 // info, then we have no way to complete it.
466 return false;
467 }
468
469 TypeIndex field_list_ti = GetFieldListIndex(cvt);
470 CVType field_list_cvt = index.tpi().getType(field_list_ti);
471 if (field_list_cvt.kind() != LF_FIELDLIST)
472 return false;
473 FieldListRecord field_list;
474 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
475 field_list_cvt, field_list))
476 llvm::consumeError(std::move(error));
477
478 // Visit all members of this class, then perform any finalization necessary
479 // to complete the class.
480 CompilerType ct = ToCompilerType(tag_qt);
481 UdtRecordCompleter completer(best_ti, ct, tag, *this, index, m_decl_to_status,
483 llvm::Error error =
484 llvm::codeview::visitMemberRecordStream(field_list.Data, completer);
485 completer.complete();
486
487 m_decl_to_status[&tag].resolved = true;
488 if (error) {
489 llvm::consumeError(std::move(error));
490 return false;
491 }
492 return true;
493}
494
496 if (ti == TypeIndex::NullptrT())
498
499 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
500 clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
501 if (direct_type.isNull())
502 return {};
503 return m_clang.getASTContext().getPointerType(direct_type);
504 }
505
506 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
507 return {};
508
509 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
510 if (bt == lldb::eBasicTypeInvalid)
511 return {};
512
513 return GetBasicType(bt);
514}
515
516clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
517 clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
518
519 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
520 // create in the AST.
521 if (pointee_type.isNull())
522 return {};
523
524 if (pointer.isPointerToMember()) {
525 MemberPointerInfo mpi = pointer.getMemberInfo();
526 clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
527 if (class_type.isNull())
528 return {};
529 if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
530 clang::MSInheritanceAttr::Spelling spelling;
531 switch (mpi.Representation) {
532 case llvm::codeview::PointerToMemberRepresentation::SingleInheritanceData:
533 case llvm::codeview::PointerToMemberRepresentation::
534 SingleInheritanceFunction:
535 spelling =
536 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance;
537 break;
538 case llvm::codeview::PointerToMemberRepresentation::
539 MultipleInheritanceData:
540 case llvm::codeview::PointerToMemberRepresentation::
541 MultipleInheritanceFunction:
542 spelling =
543 clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance;
544 break;
545 case llvm::codeview::PointerToMemberRepresentation::
546 VirtualInheritanceData:
547 case llvm::codeview::PointerToMemberRepresentation::
548 VirtualInheritanceFunction:
549 spelling =
550 clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance;
551 break;
552 case llvm::codeview::PointerToMemberRepresentation::Unknown:
553 spelling =
554 clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance;
555 break;
556 default:
557 spelling = clang::MSInheritanceAttr::Spelling::SpellingNotCalculated;
558 break;
559 }
560 tag->addAttr(clang::MSInheritanceAttr::CreateImplicit(
561 m_clang.getASTContext(), spelling));
562 }
563 return m_clang.getASTContext().getMemberPointerType(
564 pointee_type, class_type.getTypePtr());
565 }
566
567 clang::QualType pointer_type;
568 if (pointer.getMode() == PointerMode::LValueReference)
569 pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type);
570 else if (pointer.getMode() == PointerMode::RValueReference)
571 pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type);
572 else
573 pointer_type = m_clang.getASTContext().getPointerType(pointee_type);
574
575 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
576 pointer_type.addConst();
577
578 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
579 pointer_type.addVolatile();
580
581 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
582 pointer_type.addRestrict();
583
584 return pointer_type;
585}
586
587clang::QualType
588PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
589 clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
590 if (unmodified_type.isNull())
591 return {};
592
593 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
594 unmodified_type.addConst();
595 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
596 unmodified_type.addVolatile();
597
598 return unmodified_type;
599}
600
602 const TagRecord &record) {
603 clang::DeclContext *context = nullptr;
604 std::string uname;
605 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
606 if (!context)
607 return {};
608
609 clang::TagTypeKind ttk = TranslateUdtKind(record);
610 lldb::AccessType access =
611 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic;
612
613 ClangASTMetadata metadata;
614 metadata.SetUserID(toOpaqueUid(id));
615 metadata.SetIsDynamicCXXType(false);
616
617 CompilerType ct =
618 m_clang.CreateRecordType(context, OptionalClangModuleID(), access, uname,
619 ttk, lldb::eLanguageTypeC_plus_plus, &metadata);
620
621 lldbassert(ct.IsValid());
622
624
625 // Even if it's possible, don't complete it at this point. Just mark it
626 // forward resolved, and if/when LLDB needs the full definition, it can
627 // ask us.
628 clang::QualType result =
629 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
630
631 TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true);
632 return result;
633}
634
635clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const {
636 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
637 if (iter != m_uid_to_decl.end())
638 return iter->second;
639 return nullptr;
640}
641
642clang::NamespaceDecl *
644 clang::DeclContext &context) {
646 IsAnonymousNamespaceName(name) ? nullptr : name, &context,
648}
649
650clang::BlockDecl *
652 if (clang::Decl *decl = TryGetDecl(block_id))
653 return llvm::dyn_cast<clang::BlockDecl>(decl);
654
655 clang::DeclContext *scope = GetParentDeclContext(block_id);
656
657 clang::BlockDecl *block_decl =
659 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
660
661 DeclStatus status;
662 status.resolved = true;
663 status.uid = toOpaqueUid(block_id);
664 m_decl_to_status.insert({block_decl, status});
665
666 return block_decl;
667}
668
669clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
670 clang::DeclContext &scope) {
671 VariableInfo var_info = GetVariableNameInfo(sym);
672 clang::QualType qt = GetOrCreateType(var_info.type);
673 if (qt.isNull())
674 return nullptr;
675
676 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
677 &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt);
678
679 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
680 DeclStatus status;
681 status.resolved = true;
682 status.uid = toOpaqueUid(uid);
683 m_decl_to_status.insert({var_decl, status});
684 return var_decl;
685}
686
687clang::VarDecl *
689 PdbCompilandSymId var_id) {
690 if (clang::Decl *decl = TryGetDecl(var_id))
691 return llvm::dyn_cast<clang::VarDecl>(decl);
692
693 clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
694 if (!scope)
695 return nullptr;
696
697 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
699 PdbIndex &index = pdb->GetIndex();
700 CVSymbol sym = index.ReadSymbolRecord(var_id);
701 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
702}
703
705 if (clang::Decl *decl = TryGetDecl(var_id))
706 return llvm::dyn_cast<clang::VarDecl>(decl);
707
708 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
710 PdbIndex &index = pdb->GetIndex();
711 CVSymbol sym = index.ReadSymbolRecord(var_id);
713 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
714}
715
716clang::TypedefNameDecl *
718 if (clang::Decl *decl = TryGetDecl(id))
719 return llvm::dyn_cast<clang::TypedefNameDecl>(decl);
720
721 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
723 PdbIndex &index = pdb->GetIndex();
724 CVSymbol sym = index.ReadSymbolRecord(id);
725 lldbassert(sym.kind() == S_UDT);
726 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
727
728 clang::DeclContext *scope = GetParentDeclContext(id);
729
730 PdbTypeSymId real_type_id{udt.Type, false};
731 clang::QualType qt = GetOrCreateType(real_type_id);
732 if (qt.isNull() || !scope)
733 return nullptr;
734
735 std::string uname = std::string(DropNameScope(udt.Name));
736
738 uname.c_str(), ToCompilerDeclContext(*scope), 0);
739 clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct);
740 DeclStatus status;
741 status.resolved = true;
742 status.uid = toOpaqueUid(id);
743 m_decl_to_status.insert({tnd, status});
744 return tnd;
745}
746
749 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
750}
751
753 if (type.index.isSimple())
754 return CreateSimpleType(type.index);
755
756 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
758 PdbIndex &index = pdb->GetIndex();
759 CVType cvt = index.tpi().getType(type.index);
760
761 if (cvt.kind() == LF_MODIFIER) {
762 ModifierRecord modifier;
763 llvm::cantFail(
764 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
765 return CreateModifierType(modifier);
766 }
767
768 if (cvt.kind() == LF_POINTER) {
769 PointerRecord pointer;
770 llvm::cantFail(
771 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
772 return CreatePointerType(pointer);
773 }
774
775 if (IsTagRecord(cvt)) {
777 if (tag.kind() == CVTagRecord::Union)
778 return CreateRecordType(type.index, tag.asUnion());
779 if (tag.kind() == CVTagRecord::Enum)
780 return CreateEnumType(type.index, tag.asEnum());
781 return CreateRecordType(type.index, tag.asClass());
782 }
783
784 if (cvt.kind() == LF_ARRAY) {
785 ArrayRecord ar;
786 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
787 return CreateArrayType(ar);
788 }
789
790 if (cvt.kind() == LF_PROCEDURE) {
791 ProcedureRecord pr;
792 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
793 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
794 }
795
796 if (cvt.kind() == LF_MFUNCTION) {
797 MemberFunctionRecord mfr;
798 llvm::cantFail(
799 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
800 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
801 }
802
803 return {};
804}
805
807 if (type.index.isNoneType())
808 return {};
809
810 lldb::user_id_t uid = toOpaqueUid(type);
811 auto iter = m_uid_to_type.find(uid);
812 if (iter != m_uid_to_type.end())
813 return iter->second;
814
815 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
817 PdbIndex &index = pdb->GetIndex();
818 PdbTypeSymId best_type = GetBestPossibleDecl(type, index.tpi());
819
820 clang::QualType qt;
821 if (best_type.index != type.index) {
822 // This is a forward decl. Call GetOrCreate on the full decl, then map the
823 // forward decl id to the full decl QualType.
824 clang::QualType qt = GetOrCreateType(best_type);
825 if (qt.isNull())
826 return {};
827 m_uid_to_type[toOpaqueUid(type)] = qt;
828 return qt;
829 }
830
831 // This is either a full decl, or a forward decl with no matching full decl
832 // in the debug info.
833 qt = CreateType(type);
834 if (qt.isNull())
835 return {};
836
837 m_uid_to_type[toOpaqueUid(type)] = qt;
838 if (IsTagRecord(type, index.tpi())) {
839 clang::TagDecl *tag = qt->getAsTagDecl();
840 lldbassert(m_decl_to_status.count(tag) == 0);
841
842 DeclStatus &status = m_decl_to_status[tag];
843 status.uid = uid;
844 status.resolved = false;
845 }
846 return qt;
847}
848
849clang::FunctionDecl *
851 llvm::StringRef func_name, TypeIndex func_ti,
852 CompilerType func_ct, uint32_t param_count,
853 clang::StorageClass func_storage,
854 bool is_inline, clang::DeclContext *parent) {
855 clang::FunctionDecl *function_decl = nullptr;
856 if (parent->isRecord()) {
857 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
859 PdbIndex &index = pdb->GetIndex();
860 clang::QualType parent_qt = llvm::cast<clang::TypeDecl>(parent)
861 ->getTypeForDecl()
862 ->getCanonicalTypeInternal();
863 lldb::opaque_compiler_type_t parent_opaque_ty =
865 // FIXME: Remove this workaround.
866 auto iter = m_cxx_record_map.find(parent_opaque_ty);
867 if (iter != m_cxx_record_map.end()) {
868 if (iter->getSecond().contains({func_name, func_ct})) {
869 return nullptr;
870 }
871 }
872
873 CVType cvt = index.tpi().getType(func_ti);
874 MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind()));
875 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(
876 cvt, func_record));
877 TypeIndex class_index = func_record.getClassType();
878
879 CVType parent_cvt = index.tpi().getType(class_index);
880 TagRecord tag_record = CVTagRecord::create(parent_cvt).asTag();
881 // If it's a forward reference, try to get the real TypeIndex.
882 if (tag_record.isForwardRef()) {
883 llvm::Expected<TypeIndex> eti =
884 index.tpi().findFullDeclForForwardRef(class_index);
885 if (eti) {
886 tag_record = CVTagRecord::create(index.tpi().getType(*eti)).asTag();
887 }
888 }
889 if (!tag_record.FieldList.isSimple()) {
890 CVType field_list_cvt = index.tpi().getType(tag_record.FieldList);
891 FieldListRecord field_list;
892 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
893 field_list_cvt, field_list))
894 llvm::consumeError(std::move(error));
895 CreateMethodDecl process(index, m_clang, func_ti, function_decl,
896 parent_opaque_ty, func_name, func_ct);
897 if (llvm::Error err = visitMemberRecordStream(field_list.Data, process))
898 llvm::consumeError(std::move(err));
899 }
900
901 if (!function_decl) {
902 function_decl = m_clang.AddMethodToCXXRecordType(
903 parent_opaque_ty, func_name,
904 /*mangled_name=*/nullptr, func_ct,
906 /*is_virtual=*/false, /*is_static=*/false,
907 /*is_inline=*/false, /*is_explicit=*/false,
908 /*is_attr_used=*/false, /*is_artificial=*/false);
909 }
910 m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct});
911 } else {
912 function_decl = m_clang.CreateFunctionDeclaration(
913 parent, OptionalClangModuleID(), func_name, func_ct, func_storage,
914 is_inline);
915 CreateFunctionParameters(func_id, *function_decl, param_count);
916 }
917 return function_decl;
918}
919
920clang::FunctionDecl *
922 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
924 PdbIndex &index = pdb->GetIndex();
925 CompilandIndexItem *cii =
926 index.compilands().GetCompiland(inlinesite_id.modi);
927 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset);
928 InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind()));
929 cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site));
930
931 // Inlinee is the id index to the function id record that is inlined.
932 PdbTypeSymId func_id(inline_site.Inlinee, true);
933 // Look up the function decl by the id index to see if we have created a
934 // function decl for a different inlinesite that refers the same function.
935 if (clang::Decl *decl = TryGetDecl(func_id))
936 return llvm::dyn_cast<clang::FunctionDecl>(decl);
937 clang::FunctionDecl *function_decl =
938 CreateFunctionDeclFromId(func_id, inlinesite_id);
939 if (function_decl == nullptr)
940 return nullptr;
941
942 // Use inline site id in m_decl_to_status because it's expected to be a
943 // PdbCompilandSymId so that we can parse local variables info after it.
944 uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id);
945 DeclStatus status;
946 status.resolved = true;
947 status.uid = inlinesite_uid;
948 m_decl_to_status.insert({function_decl, status});
949 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
950 // stream are unique and there could be multiple inline sites (different ids)
951 // referring the same inline function. This avoid creating multiple same
952 // inline function delcs.
953 uint64_t func_uid = toOpaqueUid(func_id);
954 lldbassert(m_uid_to_decl.count(func_uid) == 0);
955 m_uid_to_decl[func_uid] = function_decl;
956 return function_decl;
957}
958
959clang::FunctionDecl *
961 PdbCompilandSymId func_sid) {
962 lldbassert(func_tid.is_ipi);
963 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
965 PdbIndex &index = pdb->GetIndex();
966 CVType func_cvt = index.ipi().getType(func_tid.index);
967 llvm::StringRef func_name;
968 TypeIndex func_ti;
969 clang::DeclContext *parent = nullptr;
970 switch (func_cvt.kind()) {
971 case LF_MFUNC_ID: {
972 MemberFuncIdRecord mfr;
973 cantFail(
974 TypeDeserializer::deserializeAs<MemberFuncIdRecord>(func_cvt, mfr));
975 func_name = mfr.getName();
976 func_ti = mfr.getFunctionType();
977 PdbTypeSymId class_type_id(mfr.ClassType, false);
978 parent = GetOrCreateDeclContextForUid(class_type_id);
979 break;
980 }
981 case LF_FUNC_ID: {
982 FuncIdRecord fir;
983 cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(func_cvt, fir));
984 func_name = fir.getName();
985 func_ti = fir.getFunctionType();
987 if (!fir.ParentScope.isNoneType()) {
988 CVType parent_cvt = index.ipi().getType(fir.ParentScope);
989 if (parent_cvt.kind() == LF_STRING_ID) {
990 StringIdRecord sir;
991 cantFail(
992 TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir));
993 parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent);
994 }
995 }
996 break;
997 }
998 default:
999 lldbassert(false && "Invalid function id type!");
1000 }
1001 clang::QualType func_qt = GetOrCreateType(func_ti);
1002 if (func_qt.isNull() || !parent)
1003 return nullptr;
1004 CompilerType func_ct = ToCompilerType(func_qt);
1005 uint32_t param_count =
1006 llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams();
1007 return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count,
1008 clang::SC_None, true, parent);
1009}
1010
1011clang::FunctionDecl *
1013 if (clang::Decl *decl = TryGetDecl(func_id))
1014 return llvm::dyn_cast<clang::FunctionDecl>(decl);
1015
1016 clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
1017 if (!parent)
1018 return nullptr;
1019 std::string context_name;
1020 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
1021 context_name = ns->getQualifiedNameAsString();
1022 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
1023 context_name = tag->getQualifiedNameAsString();
1024 }
1025
1026 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1028 PdbIndex &index = pdb->GetIndex();
1029 CVSymbol cvs = index.ReadSymbolRecord(func_id);
1030 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1031 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1032
1033 PdbTypeSymId type_id(proc.FunctionType);
1034 clang::QualType qt = GetOrCreateType(type_id);
1035 if (qt.isNull())
1036 return nullptr;
1037
1038 clang::StorageClass storage = clang::SC_None;
1039 if (proc.Kind == SymbolRecordKind::ProcSym)
1040 storage = clang::SC_Static;
1041
1042 const clang::FunctionProtoType *func_type =
1043 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1044
1045 CompilerType func_ct = ToCompilerType(qt);
1046
1047 llvm::StringRef proc_name = proc.Name;
1048 proc_name.consume_front(context_name);
1049 proc_name.consume_front("::");
1050 clang::FunctionDecl *function_decl =
1051 CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct,
1052 func_type->getNumParams(), storage, false, parent);
1053 if (function_decl == nullptr)
1054 return nullptr;
1055
1056 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1057 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1058 DeclStatus status;
1059 status.resolved = true;
1060 status.uid = toOpaqueUid(func_id);
1061 m_decl_to_status.insert({function_decl, status});
1062
1063 return function_decl;
1064}
1065
1067 clang::FunctionDecl &function_decl,
1068 uint32_t param_count) {
1069 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1071 PdbIndex &index = pdb->GetIndex();
1072 CompilandIndexItem *cii = index.compilands().GetCompiland(func_id.modi);
1073 CVSymbolArray scope =
1074 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1075
1076 scope.drop_front();
1077 auto begin = scope.begin();
1078 auto end = scope.end();
1079 std::vector<clang::ParmVarDecl *> params;
1080 for (uint32_t i = 0; i < param_count && begin != end;) {
1081 uint32_t record_offset = begin.offset();
1082 CVSymbol sym = *begin++;
1083
1084 TypeIndex param_type;
1085 llvm::StringRef param_name;
1086 switch (sym.kind()) {
1087 case S_REGREL32: {
1088 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1089 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1090 param_type = reg.Type;
1091 param_name = reg.Name;
1092 break;
1093 }
1094 case S_REGISTER: {
1095 RegisterSym reg(SymbolRecordKind::RegisterSym);
1096 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1097 param_type = reg.Index;
1098 param_name = reg.Name;
1099 break;
1100 }
1101 case S_LOCAL: {
1102 LocalSym local(SymbolRecordKind::LocalSym);
1103 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1104 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1105 continue;
1106 param_type = local.Type;
1107 param_name = local.Name;
1108 break;
1109 }
1110 case S_BLOCK32:
1111 case S_INLINESITE:
1112 case S_INLINESITE2:
1113 // All parameters should come before the first block/inlinesite. If that
1114 // isn't the case, then perhaps this is bad debug info that doesn't
1115 // contain information about all parameters.
1116 return;
1117 default:
1118 continue;
1119 }
1120
1121 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1122 clang::QualType qt = GetOrCreateType(param_type);
1123 if (qt.isNull())
1124 return;
1125
1126 CompilerType param_type_ct = m_clang.GetType(qt);
1127 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1128 &function_decl, OptionalClangModuleID(), param_name.str().c_str(),
1129 param_type_ct, clang::SC_None, true);
1130 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1131
1132 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1133 params.push_back(param);
1134 ++i;
1135 }
1136
1137 if (!params.empty() && params.size() == param_count)
1138 m_clang.SetFunctionParameters(&function_decl, params);
1139}
1140
1142 const EnumRecord &er) {
1143 clang::DeclContext *decl_context = nullptr;
1144 std::string uname;
1145 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1146 if (!decl_context)
1147 return {};
1148
1149 clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1150 if (underlying_type.isNull())
1151 return {};
1152
1153 Declaration declaration;
1155 uname, decl_context, OptionalClangModuleID(), declaration,
1156 ToCompilerType(underlying_type), er.isScoped());
1157
1160
1161 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1162}
1163
1164clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1165 clang::QualType element_type = GetOrCreateType(ar.ElementType);
1166
1167 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1169 PdbIndex &index = pdb->GetIndex();
1170 uint64_t element_size = GetSizeOfType({ar.ElementType}, index.tpi());
1171 if (element_type.isNull() || element_size == 0)
1172 return {};
1173 uint64_t element_count = ar.Size / element_size;
1174
1175 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1176 element_count, false);
1177 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1178}
1179
1181 TypeIndex args_type_idx, TypeIndex return_type_idx,
1182 llvm::codeview::CallingConvention calling_convention) {
1183 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1185 PdbIndex &index = pdb->GetIndex();
1186 TpiStream &stream = index.tpi();
1187 CVType args_cvt = stream.getType(args_type_idx);
1188 ArgListRecord args;
1189 llvm::cantFail(
1190 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1191
1192 llvm::ArrayRef<TypeIndex> arg_indices = llvm::ArrayRef(args.ArgIndices);
1193 bool is_variadic = IsCVarArgsFunction(arg_indices);
1194 if (is_variadic)
1195 arg_indices = arg_indices.drop_back();
1196
1197 std::vector<CompilerType> arg_types;
1198 arg_types.reserve(arg_indices.size());
1199
1200 for (TypeIndex arg_index : arg_indices) {
1201 clang::QualType arg_type = GetOrCreateType(arg_index);
1202 if (arg_type.isNull())
1203 continue;
1204 arg_types.push_back(ToCompilerType(arg_type));
1205 }
1206
1207 clang::QualType return_type = GetOrCreateType(return_type_idx);
1208 if (return_type.isNull())
1209 return {};
1210
1211 std::optional<clang::CallingConv> cc =
1212 TranslateCallingConvention(calling_convention);
1213 if (!cc)
1214 return {};
1215
1216 CompilerType return_ct = ToCompilerType(return_type);
1217 CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1218 return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc);
1219
1220 return clang::QualType::getFromOpaquePtr(
1221 func_sig_ast_type.GetOpaqueQualType());
1222}
1223
1224static bool isTagDecl(clang::DeclContext &context) {
1225 return llvm::isa<clang::TagDecl>(&context);
1226}
1227
1228static bool isFunctionDecl(clang::DeclContext &context) {
1229 return llvm::isa<clang::FunctionDecl>(&context);
1230}
1231
1232static bool isBlockDecl(clang::DeclContext &context) {
1233 return llvm::isa<clang::BlockDecl>(&context);
1234}
1235
1236void PdbAstBuilder::ParseNamespace(clang::DeclContext &context) {
1237 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(&context);
1238 if (m_parsed_namespaces.contains(ns))
1239 return;
1240 std::string qname = ns->getQualifiedNameAsString();
1241 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1243 PdbIndex &index = pdb->GetIndex();
1244 TypeIndex ti{index.tpi().TypeIndexBegin()};
1245 for (const CVType &cvt : index.tpi().typeArray()) {
1246 PdbTypeSymId tid{ti};
1247 ++ti;
1248
1249 if (!IsTagRecord(cvt))
1250 continue;
1251
1253
1254 // Call CreateDeclInfoForType unconditionally so that the namespace info
1255 // gets created. But only call CreateRecordType if the namespace name
1256 // matches.
1257 clang::DeclContext *context = nullptr;
1258 std::string uname;
1259 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1260 if (!context || !context->isNamespace())
1261 continue;
1262
1263 clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context);
1264 llvm::StringRef ns_name = ns->getName();
1265 if (ns_name.startswith(qname)) {
1266 ns_name = ns_name.drop_front(qname.size());
1267 if (ns_name.startswith("::"))
1268 GetOrCreateType(tid);
1269 }
1270 }
1272 m_parsed_namespaces.insert(ns);
1273}
1274
1276 llvm::call_once(m_parse_all_types, [this]() {
1277 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1278 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1279 PdbIndex &index = pdb->GetIndex();
1280 TypeIndex ti{index.tpi().TypeIndexBegin()};
1281 for (const CVType &cvt : index.tpi().typeArray()) {
1282 PdbTypeSymId tid{ti};
1283 ++ti;
1284
1285 if (!IsTagRecord(cvt))
1286 continue;
1287
1288 GetOrCreateType(tid);
1289 }
1290 });
1291}
1292
1294 llvm::call_once(m_parse_functions_and_non_local_vars, [this]() {
1295 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1296 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1297 PdbIndex &index = pdb->GetIndex();
1298 uint32_t module_count = index.dbi().modules().getModuleCount();
1299 for (uint16_t modi = 0; modi < module_count; ++modi) {
1301 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1302 auto iter = symbols.begin();
1303 while (iter != symbols.end()) {
1304 PdbCompilandSymId sym_id{modi, iter.offset()};
1305
1306 switch (iter->kind()) {
1307 case S_GPROC32:
1308 case S_LPROC32:
1310 iter = symbols.at(getScopeEndOffset(*iter));
1311 break;
1312 case S_GDATA32:
1313 case S_GTHREAD32:
1314 case S_LDATA32:
1315 case S_LTHREAD32:
1317 ++iter;
1318 break;
1319 default:
1320 ++iter;
1321 continue;
1322 }
1323 }
1324 }
1325 });
1326}
1327
1328static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1329 const CVSymbolArray &symbols) {
1330 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1331 if (!func_decl)
1332 return symbols;
1333 unsigned int params = func_decl->getNumParams();
1334 if (params == 0)
1335 return symbols;
1336
1337 CVSymbolArray result = symbols;
1338
1339 while (!result.empty()) {
1340 if (params == 0)
1341 return result;
1342
1343 CVSymbol sym = *result.begin();
1344 result.drop_front();
1345
1346 if (!isLocalVariableType(sym.kind()))
1347 continue;
1348
1349 --params;
1350 }
1351 return result;
1352}
1353
1355 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1357 PdbIndex &index = pdb->GetIndex();
1358 CVSymbol sym = index.ReadSymbolRecord(block_id);
1359 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1360 sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE);
1361 CompilandIndexItem &cii =
1362 index.compilands().GetOrCreateCompiland(block_id.modi);
1363 CVSymbolArray symbols =
1364 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1365
1366 // Function parameters should already have been created when the function was
1367 // parsed.
1368 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1369 symbols =
1370 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1371
1372 symbols.drop_front();
1373 auto begin = symbols.begin();
1374 while (begin != symbols.end()) {
1375 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1376 GetOrCreateSymbolForId(child_sym_id);
1377 if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) {
1378 ParseBlockChildren(child_sym_id);
1379 begin = symbols.at(getScopeEndOffset(*begin));
1380 }
1381 ++begin;
1382 }
1383}
1384
1385void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
1386
1387 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1388 lldbassert(decl);
1389
1390 auto iter = m_decl_to_status.find(decl);
1391 lldbassert(iter != m_decl_to_status.end());
1392
1393 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1394 CompleteTagDecl(*tag);
1395 return;
1396 }
1397
1398 if (isFunctionDecl(context) || isBlockDecl(context)) {
1399 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1400 ParseBlockChildren(block_id);
1401 }
1402}
1403
1404void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1405 // Namespaces aren't explicitly represented in the debug info, and the only
1406 // way to parse them is to parse all type info, demangling every single type
1407 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1408 // is an expensive operation, we have to special case it so that we do other
1409 // work (such as parsing the items that appear within the namespaces) at the
1410 // same time.
1411 if (context.isTranslationUnit()) {
1412 ParseAllTypes();
1414 return;
1415 }
1416
1417 if (context.isNamespace()) {
1418 ParseNamespace(context);
1419 return;
1420 }
1421
1422 if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1424 return;
1425 }
1426}
1427
1429 return m_clang.GetCompilerDecl(&decl);
1430}
1431
1433 return {m_clang.weak_from_this(), qt.getAsOpaquePtr()};
1434}
1435
1437PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) {
1438 return m_clang.CreateDeclContext(&context);
1439}
1440
1442 return ClangUtil::GetDecl(decl);
1443}
1444
1445clang::DeclContext *
1447 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1448}
1449
1451 m_clang.Dump(stream.AsRawOstream());
1452}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition: LLDBAssert.h:15
static bool isFunctionDecl(clang::DeclContext &context)
static bool isBlockDecl(clang::DeclContext &context)
static CVSymbolArray skipFunctionParameters(clang::Decl &decl, const CVSymbolArray &symbols)
static std::optional< clang::CallingConv > TranslateCallingConvention(llvm::codeview::CallingConvention conv)
static bool isTagDecl(clang::DeclContext &context)
static bool isLocalVariableType(SymbolKind K)
static bool IsAnonymousNamespaceName(llvm::StringRef name)
static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr)
static bool IsCVarArgsFunction(llvm::ArrayRef< TypeIndex > args)
static bool AnyScopesHaveTemplateParams(llvm::ArrayRef< llvm::ms_demangle::Node * > scopes)
llvm::ArrayRef< MSVCUndecoratedNameSpecifier > GetSpecifiers() const
void SetUserID(lldb::user_id_t user_id)
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
Definition: CompilerDecl.h:28
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:234
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx, uint32_t payload) const
Create a typedef to this type using "name" as the name of the typedef this type is valid and the type...
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
virtual SymbolFile * GetBackingSymbolFile()
SymbolFileOnDemand class overrides this to return the underlying backing SymbolFile implementation th...
Definition: SymbolFile.h:85
A TypeSystem implementation based on Clang.
clang::TranslationUnitDecl * GetTranslationUnitDecl()
static clang::TypedefNameDecl * GetAsTypedefDecl(const CompilerType &type)
CompilerType GetBasicType(lldb::BasicType type)
CompilerType GetType(clang::QualType qt)
Creates a CompilerType from the given QualType with the current TypeSystemClang instance as the Compi...
CompilerDecl GetCompilerDecl(clang::Decl *decl)
Creates a CompilerDecl from the given Decl with the current TypeSystemClang instance as its typesyste...
void Dump(llvm::raw_ostream &output) override
CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx)
Creates a CompilerDeclContext from the given DeclContext with the current TypeSystemClang instance as...
clang::FunctionDecl * CreateFunctionDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, llvm::StringRef name, const CompilerType &function_Type, clang::StorageClass storage, bool is_inline)
clang::CXXMethodDecl * AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, llvm::StringRef name, const char *mangled_name, const CompilerType &method_type, lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, bool is_explicit, bool is_attr_used, bool is_artificial)
clang::NamespaceDecl * GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, bool is_inline=false)
clang::ASTContext & getASTContext()
Returns the clang::ASTContext instance managed by this TypeSystemClang.
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
CompilerType CreateEnumerationType(llvm::StringRef name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const Declaration &decl, const CompilerType &integer_qual_type, bool is_scoped)
clang::ParmVarDecl * CreateParameterDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const char *name, const CompilerType &param_type, int storage, bool add_decl=false)
CompilerType CreateArrayType(const CompilerType &element_type, size_t element_count, bool is_vector)
static bool StartTagDeclarationDefinition(const CompilerType &type)
CompilerType CreateFunctionType(const CompilerType &result_type, const CompilerType *args, unsigned num_args, bool is_variadic, unsigned type_quals, clang::CallingConv cc=clang::CC_C, clang::RefQualifierKind ref_qual=clang::RQ_None)
void SetFunctionParameters(clang::FunctionDecl *function_decl, llvm::ArrayRef< clang::ParmVarDecl * > params)
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, ClangASTMetadata *metadata=nullptr, bool exports_symbols=false)
clang::VarDecl * CreateVariableDeclaration(clang::DeclContext *decl_context, OptionalClangModuleID owning_module, const char *name, clang::QualType type)
clang::BlockDecl * CreateBlockDeclaration(clang::DeclContext *ctx, OptionalClangModuleID owning_module)
virtual SymbolFile * GetSymbolFile() const
Definition: TypeSystem.h:99
CompilandIndexItem & GetOrCreateCompiland(uint16_t modi)
const CompilandIndexItem * GetCompiland(uint16_t modi) const
clang::QualType CreatePointerType(const llvm::codeview::PointerRecord &pointer)
bool CompleteTagDecl(clang::TagDecl &tag)
void ParseBlockChildren(PdbCompilandSymId block_id)
llvm::DenseMap< lldb::opaque_compiler_type_t, llvm::SmallSet< std::pair< llvm::StringRef, CompilerType >, 8 > > m_cxx_record_map
clang::FunctionDecl * CreateFunctionDecl(PdbCompilandSymId func_id, llvm::StringRef func_name, TypeIndex func_ti, CompilerType func_ct, uint32_t param_count, clang::StorageClass func_storage, bool is_inline, clang::DeclContext *parent)
lldb_private::CompilerDeclContext GetTranslationUnitDecl()
clang::QualType CreateModifierType(const llvm::codeview::ModifierRecord &modifier)
void ParseDeclsForSimpleContext(clang::DeclContext &context)
CompilerDecl ToCompilerDecl(clang::Decl &decl)
llvm::DenseMap< lldb::user_id_t, clang::QualType > m_uid_to_type
clang::QualType CreateFunctionType(TypeIndex args_type_idx, TypeIndex return_type_idx, llvm::codeview::CallingConvention calling_convention)
clang::DeclContext * GetParentDeclContext(PdbSymUid uid)
void ParseNamespace(clang::DeclContext &parent)
llvm::codeview::TypeIndex TypeIndex
Definition: PdbAstBuilder.h:95
CompilerDeclContext ToCompilerDeclContext(clang::DeclContext &context)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForUndecoratedName(llvm::StringRef uname)
clang::Decl * TryGetDecl(PdbSymUid uid) const
clang::DeclContext * FromCompilerDeclContext(CompilerDeclContext context)
clang::VarDecl * GetOrCreateVariableDecl(PdbCompilandSymId scope_id, PdbCompilandSymId var_id)
clang::Decl * GetOrCreateSymbolForId(PdbCompilandSymId id)
CompilerType ToCompilerType(clang::QualType qt)
clang::NamespaceDecl * GetOrCreateNamespaceDecl(const char *name, clang::DeclContext &context)
PdbAstBuilder(TypeSystemClang &clang)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForType(const llvm::codeview::TagRecord &record, TypeIndex ti)
clang::QualType GetBasicType(lldb::BasicType type)
bool CompleteType(clang::QualType qt)
llvm::DenseSet< clang::NamespaceDecl * > m_parsed_namespaces
llvm::DenseMap< clang::Decl *, DeclStatus > m_decl_to_status
clang::DeclContext * GetOrCreateDeclContextForUid(PdbSymUid uid)
void ParseDeclsForContext(clang::DeclContext &context)
llvm::DenseMap< lldb::user_id_t, clang::Decl * > m_uid_to_decl
clang::BlockDecl * GetOrCreateBlockDecl(PdbCompilandSymId block_id)
clang::Decl * FromCompilerDecl(CompilerDecl decl)
clang::QualType CreateSimpleType(TypeIndex ti)
void CreateFunctionParameters(PdbCompilandSymId func_id, clang::FunctionDecl &function_decl, uint32_t param_count)
clang::FunctionDecl * GetOrCreateFunctionDecl(PdbCompilandSymId func_id)
clang::QualType CreateArrayType(const llvm::codeview::ArrayRecord &array)
clang::QualType CreateRecordType(PdbTypeSymId id, const llvm::codeview::TagRecord &record)
clang::FunctionDecl * CreateFunctionDeclFromId(PdbTypeSymId func_tid, PdbCompilandSymId func_sid)
llvm::once_flag m_parse_functions_and_non_local_vars
clang::VarDecl * CreateVariableDecl(PdbSymUid uid, llvm::codeview::CVSymbol sym, clang::DeclContext &scope)
clang::QualType GetOrCreateType(PdbTypeSymId type)
clang::QualType CreateEnumType(PdbTypeSymId id, const llvm::codeview::EnumRecord &record)
clang::FunctionDecl * GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id)
std::optional< lldb_private::CompilerDecl > GetOrCreateDeclForUid(PdbSymUid uid)
clang::QualType CreateType(PdbTypeSymId type)
clang::TypedefNameDecl * GetOrCreateTypedefDecl(PdbGlobalSymId id)
PdbIndex - Lazy access to the important parts of a PDB file.
Definition: PdbIndex.h:47
llvm::pdb::TpiStream & ipi()
Definition: PdbIndex.h:127
CompileUnitIndex & compilands()
Definition: PdbIndex.h:142
llvm::pdb::DbiStream & dbi()
Definition: PdbIndex.h:121
llvm::codeview::CVSymbol ReadSymbolRecord(PdbCompilandSymId cu_sym) const
Definition: PdbIndex.cpp:187
llvm::pdb::TpiStream & tpi()
Definition: PdbIndex.h:124
PdbGlobalSymId asGlobalSym() const
Definition: PdbSymUid.cpp:134
PdbCompilandSymId asCompilandSym() const
Definition: PdbSymUid.cpp:125
PdbTypeSymId asTypeSym() const
Definition: PdbSymUid.cpp:144
PdbSymUidKind kind() const
Definition: PdbSymUid.cpp:112
std::optional< llvm::codeview::TypeIndex > GetParentType(llvm::codeview::TypeIndex ti)
std::optional< PdbCompilandSymId > FindSymbolScope(PdbCompilandSymId id)
llvm::StringRef DropNameScope(llvm::StringRef name)
Definition: PdbUtil.cpp:599
uint64_t toOpaqueUid(const T &cid)
Definition: PdbSymUid.h:114
lldb::BasicType GetCompilerTypeForSimpleKind(llvm::codeview::SimpleTypeKind kind)
VariableInfo GetVariableNameInfo(llvm::codeview::CVSymbol symbol)
bool IsTagRecord(llvm::codeview::CVType cvt)
Definition: PdbUtil.cpp:517
llvm::codeview::TypeIndex LookThroughModifierRecord(llvm::codeview::CVType modifier)
bool IsForwardRefUdt(llvm::codeview::CVType cvt)
lldb::AccessType TranslateMemberAccess(llvm::codeview::MemberAccess access)
llvm::codeview::TypeIndex GetFieldListIndex(llvm::codeview::CVType cvt)
size_t GetSizeOfType(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
Definition: PdbUtil.cpp:1069
PdbTypeSymId GetBestPossibleDecl(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
void * opaque_compiler_type_t
Definition: lldb-types.h:86
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eBasicTypeNullPtr
@ eBasicTypeInvalid
@ eLanguageTypeC_plus_plus
ISO C++:1998.
uint64_t user_id_t
Definition: lldb-types.h:80
static clang::Decl * GetDecl(const CompilerDecl &decl)
Returns the clang::Decl of the given CompilerDecl.
Definition: ClangUtil.cpp:31
const llvm::codeview::UnionRecord & asUnion() const
Definition: PdbUtil.h:62
static CVTagRecord create(llvm::codeview::CVType type)
Definition: PdbUtil.cpp:198
const llvm::codeview::ClassRecord & asClass() const
Definition: PdbUtil.h:52
const llvm::codeview::EnumRecord & asEnum() const
Definition: PdbUtil.h:57
const llvm::codeview::TagRecord & asTag() const
Definition: PdbUtil.h:44
Represents a single compile unit.
llvm::pdb::ModuleDebugStreamRef m_debug_stream
llvm::codeview::TypeIndex index
Definition: PdbSymUid.h:76
llvm::codeview::TypeIndex type
Definition: PdbUtil.h:104