LLDB mainline
PdbAstBuilderClang.cpp
Go to the documentation of this file.
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
17#include "PdbUtil.h"
22#include "SymbolFileNativePDB.h"
23#include "UdtRecordCompleter.h"
24#include "lldb/Core/Module.h"
28#include <optional>
29#include <string_view>
30
31using namespace lldb_private;
32using namespace lldb_private::npdb;
33using namespace llvm::codeview;
34using namespace llvm::pdb;
35
36namespace {
37struct CreateMethodDecl : public TypeVisitorCallbacks {
38 CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang,
39 TypeIndex func_type_index,
40 clang::FunctionDecl *&function_decl,
42 llvm::StringRef proc_name, ConstString mangled_name,
43 CompilerType func_ct)
44 : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index),
45 function_decl(function_decl), parent_ty(parent_ty),
46 proc_name(proc_name), mangled_name(mangled_name), func_ct(func_ct) {}
47 PdbIndex &m_index;
48 TypeSystemClang &m_clang;
49 TypeIndex func_type_index;
50 clang::FunctionDecl *&function_decl;
52 llvm::StringRef proc_name;
53 ConstString mangled_name;
54 CompilerType func_ct;
55
56 llvm::Error visitKnownMember(CVMemberRecord &cvr,
57 OverloadedMethodRecord &overloaded) override {
58 TypeIndex method_list_idx = overloaded.MethodList;
59
60 CVType method_list_type = m_index.tpi().getType(method_list_idx);
61 assert(method_list_type.kind() == LF_METHODLIST);
62
63 MethodOverloadListRecord method_list;
64 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
65 method_list_type, method_list));
66
67 for (const OneMethodRecord &method : method_list.Methods) {
68 if (method.getType().getIndex() == func_type_index.getIndex())
69 AddMethod(overloaded.Name, method.getOptions(), method.Attrs);
70 }
71
72 return llvm::Error::success();
73 }
74
75 llvm::Error visitKnownMember(CVMemberRecord &cvr,
76 OneMethodRecord &record) override {
77 AddMethod(record.getName(), record.getOptions(), record.Attrs);
78 return llvm::Error::success();
79 }
80
81 void AddMethod(llvm::StringRef name, MethodOptions options,
82 MemberAttributes attrs) {
83 if (name != proc_name || function_decl)
84 return;
85 bool is_virtual = attrs.isVirtual();
86 bool is_static = attrs.isStatic();
87 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
88 MethodOptions::CompilerGenerated;
89 function_decl = m_clang.AddMethodToCXXRecordType(
90 parent_ty, proc_name, mangled_name, func_ct,
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::TagTypeKind::Class;
102 case TypeRecordKind::Struct:
103 return clang::TagTypeKind::Struct;
104 case TypeRecordKind::Union:
105 return clang::TagTypeKind::Union;
106 case TypeRecordKind::Interface:
107 return clang::TagTypeKind::Interface;
108 case TypeRecordKind::Enum:
109 return clang::TagTypeKind::Enum;
110 default:
111 lldbassert(false && "Invalid tag record kind!");
112 return clang::TagTypeKind::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
164
168
169std::pair<clang::DeclContext *, std::string>
171 TypeIndex ti) {
173 m_clang.GetSymbolFile()->GetBackingSymbolFile());
174 // FIXME: Move this to GetDeclContextContainingUID.
175 if (!record.hasUniqueName())
176 return CreateDeclInfoForUndecoratedName(record.Name);
177
178 llvm::ms_demangle::Demangler demangler;
179 std::string_view sv(record.UniqueName.begin(), record.UniqueName.size());
180 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
181 if (demangler.Error)
182 return CreateDeclInfoForUndecoratedName(record.Name);
183
184 llvm::ms_demangle::IdentifierNode *idn =
185 ttn->QualifiedName->getUnqualifiedIdentifier();
186 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
187
188 llvm::ms_demangle::NodeArrayNode *name_components =
189 ttn->QualifiedName->Components;
190 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
191 name_components->Count - 1);
192
193 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
194
195 // If this type doesn't have a parent type in the debug info, then the best we
196 // can do is to say that it's either a series of namespaces (if the scope is
197 // non-empty), or the translation unit (if the scope is empty).
198 std::optional<TypeIndex> parent_index = pdb->GetParentType(ti);
199 if (!parent_index) {
200 if (scopes.empty())
201 return {context, uname};
202
203 // If there is no parent in the debug info, but some of the scopes have
204 // template params, then this is a case of bad debug info. See, for
205 // example, llvm.org/pr39607. We don't want to create an ambiguity between
206 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
207 // global scope with the fully qualified name.
208 if (AnyScopesHaveTemplateParams(scopes))
209 return {context, std::string(record.Name)};
210
211 for (llvm::ms_demangle::Node *scope : scopes) {
212 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
213 std::string str = nii->toString();
214 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
215 }
216 return {context, uname};
217 }
218
219 // Otherwise, all we need to do is get the parent type of this type and
220 // recurse into our lazy type creation / AST reconstruction logic to get an
221 // LLDB TypeSP for the parent. This will cause the AST to automatically get
222 // the right DeclContext created for any parent.
223 clang::QualType parent_qt = GetOrCreateClangType(*parent_index);
224 if (parent_qt.isNull())
225 return {nullptr, ""};
226
227 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
228 return {context, uname};
229}
230
231static bool isLocalVariableType(SymbolKind K) {
232 switch (K) {
233 case S_REGISTER:
234 case S_REGREL32:
235 case S_REGREL32_INDIR:
236 case S_LOCAL:
237 return true;
238 default:
239 break;
240 }
241 return false;
242}
243
246 m_clang.GetSymbolFile()->GetBackingSymbolFile());
247 PdbIndex &index = pdb->GetIndex();
248 CVSymbol cvs = index.ReadSymbolRecord(id);
249
250 if (isLocalVariableType(cvs.kind())) {
251 clang::DeclContext *scope = GetParentClangDeclContext(id);
252 if (!scope)
253 return nullptr;
254 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
255 PdbCompilandSymId scope_id =
256 PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym();
257 return GetOrCreateVariableDecl(scope_id, id);
258 }
259
260 switch (cvs.kind()) {
261 case S_GPROC32:
262 case S_LPROC32:
263 return GetOrCreateFunctionDecl(id);
264 case S_GDATA32:
265 case S_LDATA32:
266 case S_GTHREAD32:
267 case S_CONSTANT:
268 // global variable
269 return nullptr;
270 case S_BLOCK32:
271 return GetOrCreateBlockDecl(id);
272 case S_INLINESITE:
274 default:
275 return nullptr;
276 }
277}
278
280 if (clang::Decl *result = TryGetDecl(uid))
281 return ToCompilerDecl(result);
282
283 clang::Decl *result = nullptr;
284 switch (uid.kind()) {
287 break;
288 case PdbSymUidKind::Type: {
289 clang::QualType qt = GetOrCreateClangType(uid.asTypeSym());
290 if (qt.isNull())
291 return CompilerDecl();
292 if (auto *tag = qt->getAsTagDecl()) {
293 result = tag;
294 break;
295 }
296 return CompilerDecl();
297 }
298 default:
299 return CompilerDecl();
300 }
301
302 if (!result)
303 return CompilerDecl();
304 m_uid_to_decl[toOpaqueUid(uid)] = result;
305 return ToCompilerDecl(result);
306}
307
308clang::DeclContext *
310 if (uid.kind() == PdbSymUidKind::CompilandSym) {
311 if (uid.asCompilandSym().offset == 0)
313 }
314 clang::Decl *decl = FromCompilerDecl(GetOrCreateDeclForUid(uid));
315 if (!decl)
316 return nullptr;
317
318 return clang::Decl::castToDeclContext(decl);
319}
320
325
326std::pair<clang::DeclContext *, std::string>
329 m_clang.GetSymbolFile()->GetBackingSymbolFile());
330 PdbIndex &index = pdb->GetIndex();
331 MSVCUndecoratedNameParser parser(name);
332 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
333
335
336 llvm::StringRef uname = specs.back().GetBaseName();
337 specs = specs.drop_back();
338 if (specs.empty())
339 return {context, std::string(name)};
340
341 llvm::StringRef scope_name = specs.back().GetFullName();
342
343 // It might be a class name, try that first.
344 std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
345 while (!types.empty()) {
346 clang::QualType qt = GetOrCreateClangType(types.back());
347 if (qt.isNull())
348 continue;
349 clang::TagDecl *tag = qt->getAsTagDecl();
350 if (tag)
351 return {clang::TagDecl::castToDeclContext(tag), std::string(uname)};
352 types.pop_back();
353 }
354
355 // If that fails, treat it as a series of namespaces.
356 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
357 std::string ns_name = spec.GetBaseName().str();
358 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
359 }
360 return {context, std::string(uname)};
361}
362
363clang::DeclContext *
365 PdbCompilandSymId uid) {
367 m_clang.GetSymbolFile()->GetBackingSymbolFile());
368 PdbIndex &index = pdb->GetIndex();
369 CVSymbol sym = index.ReadSymbolRecord(uid);
370
371 llvm::StringRef symbol_name = getSymbolName(sym);
372
373 std::optional<PdbTypeSymId> func_id = GetFunctionType(sym);
374 if (!func_id || !symbol_name.contains("::"))
375 return CreateDeclInfoForUndecoratedName(symbol_name).first;
376
377 // Try to get the context from class type of an LF_MFUNCTION.
378 // For some types, we might not find a class type.
379 auto get_member_fn_context = [&]() -> clang::DeclContext * {
380 TypeIndex id = func_id->index;
381
382 if (func_id->is_ipi) {
383 // Type from IPI, for example from S_INLINESITE
384 std::optional<CVType> func_id_type =
385 index.ipi().tryGetType(func_id->index);
386 if (!func_id_type || func_id_type->kind() != LF_MFUNC_ID)
387 return nullptr;
388
389 MemberFuncIdRecord record;
390 llvm::Error err = TypeDeserializer::deserializeAs<MemberFuncIdRecord>(
391 *func_id_type, record);
392 if (err) {
393 llvm::consumeError(std::move(err));
394 return nullptr;
395 }
396
397 id = record.FunctionType;
398 }
399
400 std::optional<CVType> func_type = index.tpi().tryGetType(id);
401 if (!func_type || func_type->kind() != LF_MFUNCTION)
402 return nullptr;
403
404 MemberFunctionRecord mfr(TypeRecordKind::MemberFunction);
405
406 llvm::Error err =
407 TypeDeserializer::deserializeAs<MemberFunctionRecord>(*func_type, mfr);
408 if (err || mfr.ClassType.isNoneType()) {
409 llvm::consumeError(std::move(err));
410 return nullptr;
411 }
412
413 clang::QualType qt = GetOrCreateClangType(mfr.ClassType);
414 if (qt.isNull())
415 return nullptr;
416 clang::TagDecl *tag = qt->getAsTagDecl();
417 if (!tag)
418 return nullptr;
419
420 return clang::TagDecl::castToDeclContext(tag);
421 };
422
423 clang::DeclContext *context = get_member_fn_context();
424 if (!context)
425 return CreateDeclInfoForUndecoratedName(symbol_name).first;
426
427 return context;
428}
429
430clang::DeclContext *
432 // We must do this *without* calling GetOrCreate on the current uid, as
433 // that would be an infinite recursion.
435 m_clang.GetSymbolFile()->GetBackingSymbolFile());
436 PdbIndex &index = pdb->GetIndex();
437 switch (uid.kind()) {
439 std::optional<PdbCompilandSymId> scope =
440 pdb->FindSymbolScope(uid.asCompilandSym());
441 if (scope)
443
445 }
446 case PdbSymUidKind::Type: {
447 // It could be a namespace, class, or global. We don't support nested
448 // functions yet. Anyway, we just need to consult the parent type map.
449 PdbTypeSymId type_id = uid.asTypeSym();
450 std::optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
451 if (!parent_index)
453 return GetOrCreateClangDeclContextForUid(PdbTypeSymId(*parent_index));
454 }
456 // In this case the parent DeclContext is the one for the class that this
457 // member is inside of.
458 break;
460 // If this refers to a compiland symbol, just recurse in with that symbol.
461 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
462 // need to parse the undecorated name to figure out the scope, then look
463 // that up in the TPI stream. If it's found, it's a type, othewrise it's
464 // a series of namespaces.
465 // FIXME: do this.
466 CVSymbol global = index.ReadSymbolRecord(uid.asGlobalSym());
467 switch (global.kind()) {
468 case SymbolKind::S_GDATA32:
469 case SymbolKind::S_LDATA32:
470 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
471 case SymbolKind::S_PROCREF:
472 case SymbolKind::S_LPROCREF: {
473 ProcRefSym ref{global.kind()};
474 llvm::cantFail(
475 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
476 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
477 return GetParentClangDeclContext(cu_sym_id);
478 }
479 case SymbolKind::S_CONSTANT:
480 case SymbolKind::S_UDT:
481 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
482 default:
483 break;
484 }
485 break;
486 }
487 default:
488 break;
489 }
491}
492
496
498 if (GetClangASTImporter().CanImport(ct))
500
501 clang::QualType qt = FromCompilerType(ct);
502 if (qt.isNull())
503 return false;
504 clang::TagDecl *tag = qt->getAsTagDecl();
505 if (qt->isArrayType()) {
506 const clang::Type *element_type = qt->getArrayElementTypeNoTypeQual();
507 tag = element_type->getAsTagDecl();
508 }
509 if (!tag)
510 return false;
511
512 return CompleteTagDecl(*tag);
513}
514
515bool PdbAstBuilderClang::CompleteTagDecl(clang::TagDecl &tag) {
516 // If this is not in our map, it's an error.
517 auto status_iter = m_decl_to_status.find(&tag);
518 lldbassert(status_iter != m_decl_to_status.end());
519
520 // If it's already complete, just return.
521 DeclStatus &status = status_iter->second;
522 if (status.resolved)
523 return true;
524
525 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
526 PdbIndex &index = static_cast<SymbolFileNativePDB *>(
527 m_clang.GetSymbolFile()->GetBackingSymbolFile())
528 ->GetIndex();
529 lldbassert(IsTagRecord(type_id, index.tpi()));
530
531 clang::QualType tag_qt = m_clang.getASTContext().getCanonicalTagType(&tag);
532 TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
533
534 TypeIndex tag_ti = type_id.index;
535 CVType cvt = index.tpi().getType(tag_ti);
536 if (cvt.kind() == LF_MODIFIER)
537 tag_ti = LookThroughModifierRecord(cvt);
538
539 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, index.tpi());
540 cvt = index.tpi().getType(best_ti.index);
542
543 if (IsForwardRefUdt(cvt)) {
544 // If we can't find a full decl for this forward ref anywhere in the debug
545 // info, then we have no way to complete it.
546 return false;
547 }
548
549 TypeIndex field_list_ti = GetFieldListIndex(cvt);
550 CVType field_list_cvt = index.tpi().getType(field_list_ti);
551 if (field_list_cvt.kind() != LF_FIELDLIST)
552 return false;
553 FieldListRecord field_list;
554 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
555 field_list_cvt, field_list))
556 llvm::consumeError(std::move(error));
557
558 // Visit all members of this class, then perform any finalization necessary
559 // to complete the class.
560 CompilerType ct = ToCompilerType(tag_qt);
561 UdtRecordCompleter completer(best_ti, ct, tag, *this, index, m_decl_to_status,
563 llvm::Error error =
564 llvm::codeview::visitMemberRecordStream(field_list.Data, completer);
565 completer.complete();
566
567 m_decl_to_status[&tag].resolved = true;
568 if (error) {
569 llvm::consumeError(std::move(error));
570 return false;
571 }
572 return true;
573}
574
576 if (ti == TypeIndex::NullptrT())
578
579 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
580 clang::QualType direct_type = GetOrCreateClangType(ti.makeDirect());
581 if (direct_type.isNull())
582 return {};
583 return m_clang.getASTContext().getPointerType(direct_type);
584 }
585
586 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
587 return {};
588
589 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
590 if (bt == lldb::eBasicTypeInvalid)
591 return {};
592
593 return GetBasicType(bt);
594}
595
596clang::QualType
597PdbAstBuilderClang::CreatePointerType(const PointerRecord &pointer) {
598 clang::QualType pointee_type = GetOrCreateClangType(pointer.ReferentType);
599
600 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
601 // create in the AST.
602 if (pointee_type.isNull())
603 return {};
604
605 if (pointer.isPointerToMember()) {
606 MemberPointerInfo mpi = pointer.getMemberInfo();
607 clang::QualType class_type = GetOrCreateClangType(mpi.ContainingType);
608 if (class_type.isNull())
609 return {};
610 if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
611 clang::MSInheritanceAttr::Spelling spelling;
612 switch (mpi.Representation) {
613 case llvm::codeview::PointerToMemberRepresentation::SingleInheritanceData:
614 case llvm::codeview::PointerToMemberRepresentation::
615 SingleInheritanceFunction:
616 spelling =
617 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance;
618 break;
619 case llvm::codeview::PointerToMemberRepresentation::
620 MultipleInheritanceData:
621 case llvm::codeview::PointerToMemberRepresentation::
622 MultipleInheritanceFunction:
623 spelling =
624 clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance;
625 break;
626 case llvm::codeview::PointerToMemberRepresentation::
627 VirtualInheritanceData:
628 case llvm::codeview::PointerToMemberRepresentation::
629 VirtualInheritanceFunction:
630 spelling =
631 clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance;
632 break;
633 case llvm::codeview::PointerToMemberRepresentation::Unknown:
634 spelling =
635 clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance;
636 break;
637 default:
638 spelling = clang::MSInheritanceAttr::Spelling::SpellingNotCalculated;
639 break;
640 }
641 tag->addAttr(clang::MSInheritanceAttr::CreateImplicit(
642 m_clang.getASTContext(), spelling));
643 }
644 return m_clang.getASTContext().getMemberPointerType(
645 pointee_type, /*Qualifier=*/std::nullopt,
646 class_type->getAsCXXRecordDecl());
647 }
648
649 clang::QualType pointer_type;
650 if (pointer.getMode() == PointerMode::LValueReference)
651 pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type);
652 else if (pointer.getMode() == PointerMode::RValueReference)
653 pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type);
654 else
655 pointer_type = m_clang.getASTContext().getPointerType(pointee_type);
656
657 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
658 pointer_type.addConst();
659
660 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
661 pointer_type.addVolatile();
662
663 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
664 pointer_type.addRestrict();
665
666 return pointer_type;
667}
668
669clang::QualType
670PdbAstBuilderClang::CreateModifierType(const ModifierRecord &modifier) {
671 clang::QualType unmodified_type = GetOrCreateClangType(modifier.ModifiedType);
672 if (unmodified_type.isNull())
673 return {};
674
675 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
676 unmodified_type.addConst();
677 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
678 unmodified_type.addVolatile();
679
680 return unmodified_type;
681}
682
684 const TagRecord &record) {
685 clang::DeclContext *context = nullptr;
686 std::string uname;
687 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
688 if (!context)
689 return {};
690
691 clang::TagTypeKind ttk = TranslateUdtKind(record);
692 ClangASTMetadata metadata;
693 metadata.SetUserID(toOpaqueUid(id));
694 metadata.SetIsDynamicCXXType(false);
695
696 CompilerType ct = m_clang.CreateRecordType(
697 context, OptionalClangModuleID(), uname, llvm::to_underlying(ttk),
699
700 lldbassert(ct.IsValid());
701
703
704 // Even if it's possible, don't complete it at this point. Just mark it
705 // forward resolved, and if/when LLDB needs the full definition, it can
706 // ask us.
707 clang::QualType result =
708 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
709
710 TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true);
711 return result;
712}
713
715 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
716 if (iter != m_uid_to_decl.end())
717 return iter->second;
718 return nullptr;
719}
720
721clang::NamespaceDecl *
723 clang::DeclContext &context) {
724 clang::NamespaceDecl *ns = m_clang.GetUniqueNamespaceDeclaration(
725 IsAnonymousNamespaceName(name) ? nullptr : name, &context,
727 m_known_namespaces.insert(ns);
728 m_parent_to_namespaces[&context].insert(ns);
729 return ns;
730}
731
732clang::BlockDecl *
734 if (clang::Decl *decl = TryGetDecl(block_id))
735 return llvm::dyn_cast<clang::BlockDecl>(decl);
736
737 clang::DeclContext *scope = GetParentClangDeclContext(block_id);
738
739 clang::BlockDecl *block_decl =
740 m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID());
741 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
742
743 DeclStatus status;
744 status.resolved = true;
745 status.uid = toOpaqueUid(block_id);
746 m_decl_to_status.insert({block_decl, status});
747
748 return block_decl;
749}
750
751clang::VarDecl *
753 clang::DeclContext &scope) {
754 VariableInfo var_info = GetVariableNameInfo(sym);
755 clang::QualType qt = GetOrCreateClangType(var_info.type);
756 if (qt.isNull())
757 return nullptr;
758
759 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
760 &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt);
761
762 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
763 DeclStatus status;
764 status.resolved = true;
765 status.uid = toOpaqueUid(uid);
766 m_decl_to_status.insert({var_decl, status});
767 return var_decl;
768}
769
770clang::VarDecl *
772 PdbCompilandSymId var_id) {
773 if (clang::Decl *decl = TryGetDecl(var_id))
774 return llvm::dyn_cast<clang::VarDecl>(decl);
775
776 clang::DeclContext *scope = GetOrCreateClangDeclContextForUid(scope_id);
777 if (!scope)
778 return nullptr;
779
781 m_clang.GetSymbolFile()->GetBackingSymbolFile());
782 PdbIndex &index = pdb->GetIndex();
783 CVSymbol sym = index.ReadSymbolRecord(var_id);
784 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
785}
786
787clang::VarDecl *
789 if (clang::Decl *decl = TryGetDecl(var_id))
790 return llvm::dyn_cast<clang::VarDecl>(decl);
791
793 m_clang.GetSymbolFile()->GetBackingSymbolFile());
794 PdbIndex &index = pdb->GetIndex();
795 CVSymbol sym = index.ReadSymbolRecord(var_id);
797 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
798}
799
801 if (clang::Decl *decl = TryGetDecl(id)) {
802 if (auto *tnd = llvm::dyn_cast<clang::TypedefNameDecl>(decl))
803 return ToCompilerType(m_clang.getASTContext().getTypeDeclType(tnd));
804 return CompilerType();
805 }
806
808 m_clang.GetSymbolFile()->GetBackingSymbolFile());
809 PdbIndex &index = pdb->GetIndex();
810 CVSymbol sym = index.ReadSymbolRecord(id);
811 lldbassert(sym.kind() == S_UDT);
812 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
813
814 clang::DeclContext *scope = GetParentClangDeclContext(id);
815
816 PdbTypeSymId real_type_id{udt.Type, false};
817 clang::QualType qt = GetOrCreateClangType(real_type_id);
818 if (qt.isNull() || !scope)
819 return CompilerType();
820
821 std::string uname = std::string(DropNameScope(udt.Name));
822
824 uname.c_str(), ToCompilerDeclContext(scope), 0);
825 DeclStatus status;
826 status.resolved = true;
827 status.uid = toOpaqueUid(id);
828 m_decl_to_status.insert({m_clang.GetAsTypedefDecl(ct), status});
829 return ct;
830}
831
833 CompilerType ct = m_clang.GetBasicType(type);
834 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
835}
836
838 if (type.index.isSimple())
839 return CreateSimpleType(type.index);
840
842 m_clang.GetSymbolFile()->GetBackingSymbolFile());
843 PdbIndex &index = pdb->GetIndex();
844 CVType cvt = index.tpi().getType(type.index);
845
846 if (cvt.kind() == LF_MODIFIER) {
847 ModifierRecord modifier;
848 llvm::cantFail(
849 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
850 return CreateModifierType(modifier);
851 }
852
853 if (cvt.kind() == LF_POINTER) {
854 PointerRecord pointer;
855 llvm::cantFail(
856 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
857 return CreatePointerType(pointer);
858 }
859
860 if (IsTagRecord(cvt)) {
862 if (tag.kind() == CVTagRecord::Union)
863 return CreateRecordType(type.index, tag.asUnion());
864 if (tag.kind() == CVTagRecord::Enum)
865 return CreateEnumType(type.index, tag.asEnum());
866 return CreateRecordType(type.index, tag.asClass());
867 }
868
869 if (cvt.kind() == LF_ARRAY) {
870 ArrayRecord ar;
871 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
872 return CreateArrayType(ar);
873 }
874
875 if (cvt.kind() == LF_PROCEDURE) {
876 ProcedureRecord pr;
877 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
878 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv,
879 /*type_quals=*/0);
880 }
881
882 if (cvt.kind() == LF_MFUNCTION) {
883 MemberFunctionRecord mfr;
884 llvm::cantFail(
885 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
886 unsigned int type_quals = 0;
887 if (!mfr.ThisType.isNoneType()) {
888 clang::QualType this_type = GetOrCreateClangType(mfr.getThisType());
889 if (!this_type.isNull())
890 type_quals = this_type->getPointeeType().getLocalFastQualifiers();
891 }
892 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv,
893 type_quals);
894 }
895
896 return {};
897}
898
900 if (type.index.isNoneType())
901 return {};
902
903 lldb::user_id_t uid = toOpaqueUid(type);
904 auto iter = m_uid_to_type.find(uid);
905 if (iter != m_uid_to_type.end())
906 return iter->second;
907
909 m_clang.GetSymbolFile()->GetBackingSymbolFile());
910 PdbIndex &index = pdb->GetIndex();
911 PdbTypeSymId best_type = GetBestPossibleDecl(type, index.tpi());
912
913 clang::QualType qt;
914 if (best_type.index != type.index) {
915 // This is a forward decl. Call GetOrCreate on the full decl, then map the
916 // forward decl id to the full decl QualType.
917 clang::QualType qt = GetOrCreateClangType(best_type);
918 if (qt.isNull())
919 return {};
920 m_uid_to_type[toOpaqueUid(type)] = qt;
921 return qt;
922 }
923
924 // This is either a full decl, or a forward decl with no matching full decl
925 // in the debug info.
926 qt = CreateType(type);
927 if (qt.isNull())
928 return {};
929
930 m_uid_to_type[toOpaqueUid(type)] = qt;
931 if (IsTagRecord(type, index.tpi())) {
932 clang::TagDecl *tag = qt->getAsTagDecl();
933 lldbassert(m_decl_to_status.count(tag) == 0);
934
935 DeclStatus &status = m_decl_to_status[tag];
936 status.uid = uid;
937 status.resolved = false;
938 }
939 return qt;
940}
941
943 clang::QualType qt = GetOrCreateClangType(type);
944 if (qt.isNull())
945 return {};
946 return ToCompilerType(qt);
947}
948
950 PdbCompilandSymId func_id, llvm::StringRef func_name, TypeIndex func_ti,
951 CompilerType func_ct, uint32_t param_count,
952 clang::StorageClass func_storage, bool is_inline,
953 clang::DeclContext *parent) {
954 clang::FunctionDecl *function_decl = nullptr;
955 if (parent->isRecord()) {
957 m_clang.GetSymbolFile()->GetBackingSymbolFile());
958 PdbIndex &index = pdb->GetIndex();
959 clang::CanQualType parent_qt =
960 m_clang.getASTContext().getCanonicalTypeDeclType(
961 llvm::cast<clang::TypeDecl>(parent));
962 lldb::opaque_compiler_type_t parent_opaque_ty =
964 // FIXME: Remove this workaround.
965 auto iter = m_cxx_record_map.find(parent_opaque_ty);
966 if (iter != m_cxx_record_map.end()) {
967 if (iter->getSecond().contains({func_name, func_ct})) {
968 return nullptr;
969 }
970 }
971
972 CVType cvt = index.tpi().getType(func_ti);
973 MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind()));
974 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(
975 cvt, func_record));
976 TypeIndex class_index = func_record.getClassType();
977
978 CVType parent_cvt = index.tpi().getType(class_index);
979 TagRecord tag_record = CVTagRecord::create(parent_cvt).asTag();
980 // If it's a forward reference, try to get the real TypeIndex.
981 if (tag_record.isForwardRef()) {
982 llvm::Expected<TypeIndex> eti =
983 index.tpi().findFullDeclForForwardRef(class_index);
984 if (eti) {
985 tag_record = CVTagRecord::create(index.tpi().getType(*eti)).asTag();
986 } else {
987 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), eti.takeError(),
988 "failed to find full decl for forward ref: {0}");
989 }
990 }
991
992 ConstString mangled_name(
993 pdb->FindMangledFunctionName(func_id).value_or(llvm::StringRef()));
994
995 if (!tag_record.FieldList.isSimple()) {
996 CVType field_list_cvt = index.tpi().getType(tag_record.FieldList);
997 FieldListRecord field_list;
998 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
999 field_list_cvt, field_list))
1000 llvm::consumeError(std::move(error));
1001 CreateMethodDecl process(index, m_clang, func_ti, function_decl,
1002 parent_opaque_ty, func_name, mangled_name,
1003 func_ct);
1004 if (llvm::Error err = visitMemberRecordStream(field_list.Data, process))
1005 llvm::consumeError(std::move(err));
1006 }
1007
1008 if (!function_decl) {
1009 function_decl = m_clang.AddMethodToCXXRecordType(
1010 parent_opaque_ty, func_name, mangled_name, func_ct,
1011 /*is_virtual=*/false, /*is_static=*/false,
1012 /*is_inline=*/false, /*is_explicit=*/false,
1013 /*is_attr_used=*/false, /*is_artificial=*/false);
1014 }
1015 m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct});
1016 } else {
1017 function_decl = m_clang.CreateFunctionDeclaration(
1018 parent, OptionalClangModuleID(), func_name, func_ct, func_storage,
1019 is_inline, /*asm_label=*/{});
1020 CreateFunctionParameters(func_id, *function_decl, param_count);
1021 }
1022 return function_decl;
1023}
1024
1026 PdbCompilandSymId inlinesite_id) {
1028 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1029 PdbIndex &index = pdb->GetIndex();
1030 CompilandIndexItem *cii = index.compilands().GetCompiland(inlinesite_id.modi);
1031 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset);
1032 InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind()));
1033 cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site));
1034
1035 // Inlinee is the id index to the function id record that is inlined.
1036 PdbTypeSymId func_id(inline_site.Inlinee, true);
1037 // Look up the function decl by the id index to see if we have created a
1038 // function decl for a different inlinesite that refers the same function.
1039 if (clang::Decl *decl = TryGetDecl(func_id))
1040 return llvm::dyn_cast<clang::FunctionDecl>(decl);
1041 clang::FunctionDecl *function_decl =
1042 CreateFunctionDeclFromId(func_id, inlinesite_id);
1043 if (function_decl == nullptr)
1044 return nullptr;
1045
1046 // Use inline site id in m_decl_to_status because it's expected to be a
1047 // PdbCompilandSymId so that we can parse local variables info after it.
1048 uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id);
1049 DeclStatus status;
1050 status.resolved = true;
1051 status.uid = inlinesite_uid;
1052 m_decl_to_status.insert({function_decl, status});
1053 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
1054 // stream are unique and there could be multiple inline sites (different ids)
1055 // referring the same inline function. This avoid creating multiple same
1056 // inline function delcs.
1057 uint64_t func_uid = toOpaqueUid(func_id);
1058 lldbassert(m_uid_to_decl.count(func_uid) == 0);
1059 m_uid_to_decl[func_uid] = function_decl;
1060 return function_decl;
1061}
1062
1063clang::FunctionDecl *
1065 PdbCompilandSymId func_sid) {
1066 lldbassert(func_tid.is_ipi);
1068 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1069 PdbIndex &index = pdb->GetIndex();
1070 std::optional<CVType> func_cvt =
1071 index.ipi().typeCollection().tryGetType(func_tid.index);
1072 if (!func_cvt)
1073 return nullptr;
1074 llvm::StringRef func_name;
1075 TypeIndex func_ti;
1076 clang::DeclContext *parent = nullptr;
1077 switch (func_cvt->kind()) {
1078 case LF_MFUNC_ID: {
1079 MemberFuncIdRecord mfr;
1080 cantFail(
1081 TypeDeserializer::deserializeAs<MemberFuncIdRecord>(*func_cvt, mfr));
1082 func_name = mfr.getName();
1083 func_ti = mfr.getFunctionType();
1084 PdbTypeSymId class_type_id(mfr.ClassType, false);
1085 parent = GetOrCreateClangDeclContextForUid(class_type_id);
1086 break;
1087 }
1088 case LF_FUNC_ID: {
1089 FuncIdRecord fir;
1090 cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(*func_cvt, fir));
1091 func_name = fir.getName();
1092 func_ti = fir.getFunctionType();
1094 if (!fir.ParentScope.isNoneType()) {
1095 CVType parent_cvt = index.ipi().getType(fir.ParentScope);
1096 if (parent_cvt.kind() == LF_STRING_ID) {
1097 StringIdRecord sir;
1098 cantFail(
1099 TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir));
1100 parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent);
1101 }
1102 }
1103 break;
1104 }
1105 default:
1106 lldbassert(false && "Invalid function id type!");
1107 }
1108 clang::QualType func_qt = GetOrCreateClangType(func_ti);
1109 if (func_qt.isNull() || !parent)
1110 return nullptr;
1111 CompilerType func_ct = ToCompilerType(func_qt);
1112 uint32_t param_count =
1113 llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams();
1114 return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count,
1115 clang::SC_None, true, parent);
1116}
1117
1118clang::FunctionDecl *
1120 if (clang::Decl *decl = TryGetDecl(func_id))
1121 return llvm::dyn_cast<clang::FunctionDecl>(decl);
1122
1123 clang::DeclContext *parent = GetParentClangDeclContext(PdbSymUid(func_id));
1124 if (!parent)
1125 return nullptr;
1126 std::string context_name;
1127 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
1128 context_name = ns->getQualifiedNameAsString();
1129 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
1130 context_name = tag->getQualifiedNameAsString();
1131 }
1132
1134 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1135 PdbIndex &index = pdb->GetIndex();
1136 CVSymbol cvs = index.ReadSymbolRecord(func_id);
1137 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1138 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1139
1140 PdbTypeSymId type_id(proc.FunctionType);
1141 clang::QualType qt = GetOrCreateClangType(type_id);
1142 if (qt.isNull())
1143 return nullptr;
1144
1145 clang::StorageClass storage = clang::SC_None;
1146 if (proc.Kind == SymbolRecordKind::ProcSym)
1147 storage = clang::SC_Static;
1148
1149 const clang::FunctionProtoType *func_type =
1150 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1151
1152 CompilerType func_ct = ToCompilerType(qt);
1153
1154 llvm::StringRef proc_name = proc.Name;
1155 if (!context_name.empty() && !(proc_name.consume_front(context_name) &&
1156 proc_name.consume_front("::"))) {
1157 // If we have some context, but the function name doesn't start with it, use
1158 // the basename.
1159 MSVCUndecoratedNameParser parser(proc.Name);
1160 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs(parser.GetSpecifiers());
1161 if (!specs.empty())
1162 proc_name = specs.back().GetBaseName();
1163 }
1164 clang::FunctionDecl *function_decl =
1165 CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct,
1166 func_type->getNumParams(), storage, false, parent);
1167 if (function_decl == nullptr)
1168 return nullptr;
1169
1170 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1171 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1172 DeclStatus status;
1173 status.resolved = true;
1174 status.uid = toOpaqueUid(func_id);
1175 m_decl_to_status.insert({function_decl, status});
1176
1177 return function_decl;
1178}
1179
1183
1188
1192
1194 PdbCompilandSymId var_id) {
1195 GetOrCreateVariableDecl(scope_id, var_id);
1196}
1197
1201
1203 PdbCompilandSymId func_id, clang::FunctionDecl &function_decl,
1204 uint32_t param_count) {
1206 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1207 PdbIndex &index = pdb->GetIndex();
1208 CompilandIndexItem *cii = index.compilands().GetCompiland(func_id.modi);
1209 CVSymbolArray scope =
1210 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1211
1212 scope.drop_front();
1213 auto begin = scope.begin();
1214 auto end = scope.end();
1215 std::vector<clang::ParmVarDecl *> params;
1216 for (uint32_t i = 0; i < param_count && begin != end;) {
1217 uint32_t record_offset = begin.offset();
1218 CVSymbol sym = *begin++;
1219
1220 TypeIndex param_type;
1221 llvm::StringRef param_name;
1222 switch (sym.kind()) {
1223 case S_REGREL32: {
1224 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1225 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1226 param_type = reg.Type;
1227 param_name = reg.Name;
1228 break;
1229 }
1230 case S_REGREL32_INDIR: {
1231 RegRelativeIndirSym reg(SymbolRecordKind::RegRelativeIndirSym);
1232 cantFail(
1233 SymbolDeserializer::deserializeAs<RegRelativeIndirSym>(sym, reg));
1234 param_type = reg.Type;
1235 param_name = reg.Name;
1236 break;
1237 }
1238 case S_REGISTER: {
1239 RegisterSym reg(SymbolRecordKind::RegisterSym);
1240 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1241 param_type = reg.Index;
1242 param_name = reg.Name;
1243 break;
1244 }
1245 case S_LOCAL: {
1246 LocalSym local(SymbolRecordKind::LocalSym);
1247 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1248 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1249 continue;
1250 param_type = local.Type;
1251 param_name = local.Name;
1252 break;
1253 }
1254 case S_BLOCK32:
1255 case S_INLINESITE:
1256 case S_INLINESITE2:
1257 // All parameters should come before the first block/inlinesite. If that
1258 // isn't the case, then perhaps this is bad debug info that doesn't
1259 // contain information about all parameters.
1260 return;
1261 default:
1262 continue;
1263 }
1264
1265 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1266 clang::QualType qt = GetOrCreateClangType(param_type);
1267 if (qt.isNull())
1268 return;
1269
1270 CompilerType param_type_ct = m_clang.GetType(qt);
1271 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1272 &function_decl, OptionalClangModuleID(), param_name.str().c_str(),
1273 param_type_ct, clang::SC_None, true);
1274 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1275
1276 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1277 params.push_back(param);
1278 ++i;
1279 }
1280
1281 if (!params.empty() && params.size() == param_count)
1282 function_decl.setParams(params);
1283}
1284
1286 const EnumRecord &er) {
1287 clang::DeclContext *decl_context = nullptr;
1288 std::string uname;
1289 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1290 if (!decl_context)
1291 return {};
1292
1293 clang::QualType underlying_type = GetOrCreateClangType(er.UnderlyingType);
1294 if (underlying_type.isNull())
1295 return {};
1296
1297 Declaration declaration;
1298 CompilerType enum_ct = m_clang.CreateEnumerationType(
1299 uname, decl_context, OptionalClangModuleID(), declaration,
1300 ToCompilerType(underlying_type), er.isScoped());
1301
1304
1305 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1306}
1307
1308clang::QualType PdbAstBuilderClang::CreateArrayType(const ArrayRecord &ar) {
1309 clang::QualType element_type = GetOrCreateClangType(ar.ElementType);
1311
1313 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1314 PdbIndex &index = pdb->GetIndex();
1315 uint64_t element_size = GetSizeOfType({ar.ElementType}, index.tpi());
1316 if (element_type.isNull() || element_size == 0)
1317 return {};
1318 uint64_t element_count = ar.Size / element_size;
1319
1320 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1321 element_count, false);
1322 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1323}
1324
1326 TypeIndex args_type_idx, TypeIndex return_type_idx,
1327 llvm::codeview::CallingConvention calling_convention,
1328 unsigned int type_quals) {
1330 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1331 PdbIndex &index = pdb->GetIndex();
1332 TpiStream &stream = index.tpi();
1333 CVType args_cvt = stream.getType(args_type_idx);
1334 ArgListRecord args;
1335 llvm::cantFail(
1336 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1337
1338 llvm::ArrayRef<TypeIndex> arg_indices = llvm::ArrayRef(args.ArgIndices);
1339 bool is_variadic = IsCVarArgsFunction(arg_indices);
1340 if (is_variadic)
1341 arg_indices = arg_indices.drop_back();
1342
1343 std::vector<CompilerType> arg_types;
1344 arg_types.reserve(arg_indices.size());
1345
1346 for (TypeIndex arg_index : arg_indices) {
1347 clang::QualType arg_type = GetOrCreateClangType(arg_index);
1348 if (arg_type.isNull())
1349 continue;
1350 arg_types.push_back(ToCompilerType(arg_type));
1351 }
1352
1353 clang::QualType return_type = GetOrCreateClangType(return_type_idx);
1354 if (return_type.isNull())
1355 return {};
1356
1357 std::optional<clang::CallingConv> cc =
1358 TranslateCallingConvention(calling_convention);
1359 if (!cc)
1360 return {};
1361
1362 CompilerType return_ct = ToCompilerType(return_type);
1363 CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1364 return_ct, arg_types, is_variadic, type_quals, *cc);
1365
1366 return clang::QualType::getFromOpaquePtr(
1367 func_sig_ast_type.GetOpaqueQualType());
1368}
1369
1370static bool isTagDecl(clang::DeclContext &context) {
1371 return llvm::isa<clang::TagDecl>(&context);
1372}
1373
1374static bool isFunctionDecl(clang::DeclContext &context) {
1375 return llvm::isa<clang::FunctionDecl>(&context);
1376}
1377
1378static bool isBlockDecl(clang::DeclContext &context) {
1379 return llvm::isa<clang::BlockDecl>(&context);
1380}
1381
1382void PdbAstBuilderClang::ParseNamespace(clang::DeclContext &context) {
1383 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(&context);
1384 if (m_parsed_namespaces.contains(ns))
1385 return;
1386 std::string qname = ns->getQualifiedNameAsString();
1388 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1389 PdbIndex &index = pdb->GetIndex();
1390 TypeIndex ti{index.tpi().TypeIndexBegin()};
1391 for (const CVType &cvt : index.tpi().typeArray()) {
1392 PdbTypeSymId tid{ti};
1393 ++ti;
1394
1395 if (!IsTagRecord(cvt))
1396 continue;
1397
1399
1400 // Call CreateDeclInfoForType unconditionally so that the namespace info
1401 // gets created. But only call CreateRecordType if the namespace name
1402 // matches.
1403 clang::DeclContext *context = nullptr;
1404 std::string uname;
1405 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1406 if (!context || !context->isNamespace())
1407 continue;
1408
1409 clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context);
1410 llvm::StringRef ns_name = ns->getName();
1411 if (ns_name.starts_with(qname)) {
1412 ns_name = ns_name.drop_front(qname.size());
1413 if (ns_name.starts_with("::"))
1415 }
1416 }
1418 m_parsed_namespaces.insert(ns);
1419}
1420
1422 llvm::call_once(m_parse_all_types, [this]() {
1424 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1425 PdbIndex &index = pdb->GetIndex();
1426 TypeIndex ti{index.tpi().TypeIndexBegin()};
1427 for (const CVType &cvt : index.tpi().typeArray()) {
1428 PdbTypeSymId tid{ti};
1429 ++ti;
1430
1431 if (!IsTagRecord(cvt))
1432 continue;
1433
1435 }
1436 });
1437}
1438
1440 llvm::call_once(m_parse_functions_and_non_local_vars, [this]() {
1442 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1443 PdbIndex &index = pdb->GetIndex();
1444 uint32_t module_count = index.dbi().modules().getModuleCount();
1445 for (uint16_t modi = 0; modi < module_count; ++modi) {
1447 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1448 auto iter = symbols.begin();
1449 while (iter != symbols.end()) {
1450 PdbCompilandSymId sym_id{modi, iter.offset()};
1451
1452 switch (iter->kind()) {
1453 case S_GPROC32:
1454 case S_LPROC32:
1456 iter = symbols.at(getScopeEndOffset(*iter));
1457 break;
1458 case S_GDATA32:
1459 case S_GTHREAD32:
1460 case S_LDATA32:
1461 case S_LTHREAD32:
1463 ++iter;
1464 break;
1465 default:
1466 ++iter;
1467 continue;
1468 }
1469 }
1470 }
1471 });
1472}
1473
1474static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1475 const CVSymbolArray &symbols) {
1476 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1477 if (!func_decl)
1478 return symbols;
1479 unsigned int params = func_decl->getNumParams();
1480 if (params == 0)
1481 return symbols;
1482
1483 CVSymbolArray result = symbols;
1484
1485 while (!result.empty()) {
1486 if (params == 0)
1487 return result;
1488
1489 CVSymbol sym = *result.begin();
1490 result.drop_front();
1491
1492 if (!isLocalVariableType(sym.kind()))
1493 continue;
1494
1495 --params;
1496 }
1497 return result;
1498}
1499
1502 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1503 PdbIndex &index = pdb->GetIndex();
1504 CVSymbol sym = index.ReadSymbolRecord(block_id);
1505 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1506 sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE);
1507 CompilandIndexItem &cii =
1508 index.compilands().GetOrCreateCompiland(block_id.modi);
1509 CVSymbolArray symbols =
1510 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1511
1512 // Function parameters should already have been created when the function was
1513 // parsed.
1514 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1515 symbols =
1516 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1517
1518 symbols.drop_front();
1519 auto begin = symbols.begin();
1520 while (begin != symbols.end()) {
1521 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1522 GetOrCreateSymbolForId(child_sym_id);
1523 if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) {
1524 ParseBlockChildren(child_sym_id);
1525 begin = symbols.at(getScopeEndOffset(*begin));
1526 }
1527 ++begin;
1528 }
1529}
1530
1532 clang::DeclContext &context) {
1533
1534 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1535 lldbassert(decl);
1536
1537 auto iter = m_decl_to_status.find(decl);
1538 lldbassert(iter != m_decl_to_status.end());
1539
1540 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1541 CompleteTagDecl(*tag);
1542 return;
1543 }
1544
1545 if (isFunctionDecl(context) || isBlockDecl(context)) {
1546 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1547 ParseBlockChildren(block_id);
1548 }
1549}
1550
1552 clang::DeclContext *dc = FromCompilerDeclContext(context);
1553 if (!dc)
1554 return;
1555
1556 // Namespaces aren't explicitly represented in the debug info, and the only
1557 // way to parse them is to parse all type info, demangling every single type
1558 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1559 // is an expensive operation, we have to special case it so that we do other
1560 // work (such as parsing the items that appear within the namespaces) at the
1561 // same time.
1562 if (dc->isTranslationUnit()) {
1563 ParseAllTypes();
1565 return;
1566 }
1567
1568 if (dc->isNamespace()) {
1569 ParseNamespace(*dc);
1570 return;
1571 }
1572
1573 if (isTagDecl(*dc) || isFunctionDecl(*dc) || isBlockDecl(*dc)) {
1575 return;
1576 }
1577}
1578
1580 return m_clang.GetCompilerDecl(decl);
1581}
1582
1584 return m_clang.GetType(qt);
1585}
1586
1590
1592PdbAstBuilderClang::ToCompilerDeclContext(clang::DeclContext *context) {
1593 return m_clang.CreateDeclContext(context);
1594}
1595
1597 if (decl.GetTypeSystem() != nullptr)
1598 return ClangUtil::GetDecl(decl);
1599 return nullptr;
1600}
1601
1602clang::DeclContext *
1604 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1605}
1606
1607void PdbAstBuilderClang::Dump(Stream &stream, llvm::StringRef filter,
1608 bool show_color) {
1609 m_clang.Dump(stream.AsRawOstream(), filter, show_color);
1610}
1611
1614 llvm::StringRef name) {
1615 clang::DeclContext *parent = FromCompilerDeclContext(parent_ctx);
1616 NamespaceSet *set;
1617
1618 if (parent) {
1619 auto it = m_parent_to_namespaces.find(parent);
1620 if (it == m_parent_to_namespaces.end())
1621 return {};
1622
1623 set = &it->second;
1624 } else {
1625 // In this case, search through all known namespaces
1626 set = &m_known_namespaces;
1627 }
1628 assert(set);
1629
1630 for (clang::NamespaceDecl *namespace_decl : *set)
1631 if (namespace_decl->getName() == name)
1632 return ToCompilerDeclContext(namespace_decl);
1633
1634 for (clang::NamespaceDecl *namespace_decl : *set)
1635 if (namespace_decl->isAnonymousNamespace())
1636 return FindNamespaceDecl(ToCompilerDeclContext(namespace_decl), name);
1637
1638 return {};
1639}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
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
bool CompleteType(const CompilerType &compiler_type)
void SetUserID(lldb::user_id_t user_id)
void SetIsDynamicCXXType(std::optional< bool > b)
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
TypeSystem * GetTypeSystem() const
Generic representation of a type in a programming language.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
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 uniqued constant string class.
Definition ConstString.h:40
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:405
A TypeSystem implementation based on Clang.
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static void RequireCompleteType(CompilerType type)
Complete a type from debug info, or mark it as forcefully completed if there is no definition of the ...
CompilandIndexItem & GetOrCreateCompiland(uint16_t modi)
const CompilandIndexItem * GetCompiland(uint16_t modi) const
clang::Decl * GetOrCreateSymbolForId(PdbCompilandSymId id)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForType(const llvm::codeview::TagRecord &record, TypeIndex ti)
void ParseDeclsForSimpleContext(clang::DeclContext &context)
CompilerDeclContext ToCompilerDeclContext(clang::DeclContext *context)
clang::FunctionDecl * GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id)
llvm::DenseMap< clang::DeclContext *, NamespaceSet > m_parent_to_namespaces
void CreateFunctionParameters(PdbCompilandSymId func_id, clang::FunctionDecl &function_decl, uint32_t param_count)
bool CompleteType(CompilerType ct) override
clang::QualType GetBasicType(lldb::BasicType type)
clang::Decl * TryGetDecl(PdbSymUid uid) const
clang::QualType CreateType(PdbTypeSymId type)
clang::QualType CreateArrayType(const llvm::codeview::ArrayRecord &array)
llvm::DenseMap< clang::Decl *, DeclStatus > m_decl_to_status
llvm::DenseMap< lldb::opaque_compiler_type_t, llvm::SmallSet< std::pair< llvm::StringRef, CompilerType >, 8 > > m_cxx_record_map
clang::FunctionDecl * CreateFunctionDeclFromId(PdbTypeSymId func_tid, PdbCompilandSymId func_sid)
CompilerType GetOrCreateType(PdbTypeSymId type) override
clang::QualType CreatePointerType(const llvm::codeview::PointerRecord &pointer)
clang::QualType CreateFunctionType(TypeIndex args_type_idx, TypeIndex return_type_idx, llvm::codeview::CallingConvention calling_convention, unsigned int type_quals)
clang::DeclContext * GetOrCreateDeclContextForCompilandSymbol(PdbCompilandSymId uid)
clang::QualType CreateModifierType(const llvm::codeview::ModifierRecord &modifier)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForUndecoratedName(llvm::StringRef uname)
clang::FunctionDecl * GetOrCreateFunctionDecl(PdbCompilandSymId func_id)
clang::NamespaceDecl * GetOrCreateNamespaceDecl(const char *name, clang::DeclContext &context)
void EnsureVariable(PdbCompilandSymId scope_id, PdbCompilandSymId var_id) override
clang::VarDecl * CreateVariableDecl(PdbSymUid uid, llvm::codeview::CVSymbol sym, clang::DeclContext &scope)
void ParseDeclsForContext(CompilerDeclContext context) override
CompilerDecl ToCompilerDecl(clang::Decl *decl)
void EnsureFunction(PdbCompilandSymId func_id) override
llvm::DenseMap< lldb::user_id_t, clang::QualType > m_uid_to_type
void EnsureBlock(PdbCompilandSymId block_id) override
llvm::DenseMap< lldb::user_id_t, clang::Decl * > m_uid_to_decl
CompilerDecl GetOrCreateDeclForUid(PdbSymUid uid) override
CompilerDeclContext FindNamespaceDecl(CompilerDeclContext parent_ctx, llvm::StringRef name) override
void ParseNamespace(clang::DeclContext &parent)
llvm::DenseSet< clang::NamespaceDecl * > NamespaceSet
clang::VarDecl * GetOrCreateVariableDecl(PdbCompilandSymId scope_id, PdbCompilandSymId var_id)
clang::BlockDecl * GetOrCreateBlockDecl(PdbCompilandSymId block_id)
clang::QualType CreateSimpleType(TypeIndex ti)
clang::DeclContext * GetParentClangDeclContext(PdbSymUid uid)
clang::QualType FromCompilerType(CompilerType ct)
clang::QualType CreateEnumType(PdbTypeSymId id, const llvm::codeview::EnumRecord &record)
CompilerType GetOrCreateTypedefType(PdbGlobalSymId id) override
clang::QualType CreateRecordType(PdbTypeSymId id, const llvm::codeview::TagRecord &record)
CompilerDeclContext GetParentDeclContext(PdbSymUid uid) override
clang::DeclContext * FromCompilerDeclContext(CompilerDeclContext context)
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)
CompilerDeclContext GetOrCreateDeclContextForUid(PdbSymUid uid) override
void EnsureInlinedFunction(PdbCompilandSymId inlinesite_id) override
CompilerType ToCompilerType(clang::QualType qt)
clang::QualType GetOrCreateClangType(PdbTypeSymId type)
void ParseBlockChildren(PdbCompilandSymId block_id)
void Dump(Stream &stream, llvm::StringRef filter, bool show_color) override
clang::Decl * FromCompilerDecl(CompilerDecl decl)
clang::DeclContext * GetOrCreateClangDeclContextForUid(PdbSymUid uid)
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
PdbCompilandSymId asCompilandSym() const
PdbTypeSymId asTypeSym() const
PdbSymUidKind kind() const
std::optional< PdbTypeSymId > GetFunctionType(llvm::codeview::CVSymbol symbol)
Definition PdbUtil.cpp:1199
llvm::StringRef DropNameScope(llvm::StringRef name)
Definition PdbUtil.cpp:600
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:518
llvm::codeview::TypeIndex LookThroughModifierRecord(llvm::codeview::CVType modifier)
bool IsForwardRefUdt(llvm::codeview::CVType cvt)
llvm::codeview::TypeIndex GetFieldListIndex(llvm::codeview::CVType cvt)
size_t GetSizeOfType(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
Definition PdbUtil.cpp:1145
PdbTypeSymId GetBestPossibleDecl(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
void * opaque_compiler_type_t
Definition lldb-types.h:90
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eLanguageTypeC_plus_plus
ISO C++:1998.
uint64_t user_id_t
Definition lldb-types.h:82
static clang::QualType GetQualType(const CompilerType &ct)
Definition ClangUtil.cpp:36
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:114