11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#include " LLDBMemoryReader.h"
14
+ #include " Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
14
15
#include " ReflectionContextInterface.h"
15
16
#include " SwiftLanguageRuntime.h"
16
17
#include " SwiftMetadataCache.h"
@@ -607,13 +608,14 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffset(
607
608
namespace {
608
609
609
610
CompilerType GetTypeFromTypeRef (TypeSystemSwiftTypeRef &ts,
610
- const swift::reflection::TypeRef *type_ref) {
611
+ const swift::reflection::TypeRef *type_ref,
612
+ swift::Mangle::ManglingFlavor flavor) {
611
613
if (!type_ref)
612
614
return {};
613
615
swift::Demangle::Demangler dem;
614
616
swift::Demangle::NodePointer node = type_ref->getDemangling (dem);
615
617
// TODO: the mangling flavor should come from the TypeRef.
616
- return ts.RemangleAsType (dem, node, ts. GetManglingFlavor () );
618
+ return ts.RemangleAsType (dem, node, flavor );
617
619
}
618
620
619
621
struct ExistentialSyntheticChild {
@@ -627,7 +629,8 @@ struct ExistentialSyntheticChild {
627
629
llvm::SmallVector<ExistentialSyntheticChild, 4 >
628
630
GetExistentialSyntheticChildren (TypeSystemSwiftTypeRef &ts,
629
631
const swift::reflection::TypeRef *tr,
630
- const swift::reflection::TypeInfo *ti) {
632
+ const swift::reflection::TypeInfo *ti,
633
+ swift::Mangle::ManglingFlavor flavor) {
631
634
llvm::SmallVector<ExistentialSyntheticChild, 4 > children;
632
635
auto *protocol_composition_tr =
633
636
llvm::dyn_cast<swift::reflection::ProtocolCompositionTypeRef>(tr);
@@ -641,7 +644,8 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
641
644
children.push_back ({" object" , [=]() {
642
645
if (auto *super_class_tr =
643
646
protocol_composition_tr->getSuperclass ())
644
- return GetTypeFromTypeRef (*ts_sp, super_class_tr);
647
+ return GetTypeFromTypeRef (*ts_sp, super_class_tr,
648
+ flavor);
645
649
else
646
650
return rti ? ts_sp->GetBuiltinUnknownObjectType ()
647
651
: ts_sp->GetBuiltinRawPointerType ();
@@ -652,9 +656,9 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
652
656
for (unsigned i = 1 ; i < fields.size (); ++i) {
653
657
TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef ();
654
658
auto *type_ref = fields[i].TR ;
655
- children.push_back ({fields[i]. Name , [=]() {
656
- return GetTypeFromTypeRef (*ts_sp, type_ref);
657
- }});
659
+ children.push_back (
660
+ {fields[i]. Name ,
661
+ [=]() { return GetTypeFromTypeRef (*ts_sp, type_ref, flavor); }});
658
662
}
659
663
}
660
664
}
@@ -702,12 +706,20 @@ CompilerType GetTypedefedTypeRecursive(CompilerType type) {
702
706
class SwiftRuntimeTypeVisitor {
703
707
SwiftLanguageRuntime &m_runtime;
704
708
ExecutionContext m_exe_ctx;
709
+ swift::Mangle::ManglingFlavor m_flavor =
710
+ swift::Mangle::ManglingFlavor::Default;
705
711
CompilerType m_type;
706
712
ValueObject *m_valobj = nullptr ;
707
713
bool m_hide_superclass = false ;
708
714
bool m_include_clang_types = false ;
709
715
bool m_visit_superclass = false ;
710
716
717
+ void SetFlavor () {
718
+ if (auto ts_sp =
719
+ m_type.GetTypeSystem ().dyn_cast_or_null <TypeSystemSwiftTypeRef>())
720
+ m_flavor = ts_sp->GetManglingFlavor (&m_exe_ctx);
721
+ }
722
+
711
723
public:
712
724
struct ChildInfo {
713
725
uint32_t byte_size = 0 ;
@@ -732,6 +744,7 @@ class SwiftRuntimeTypeVisitor {
732
744
: m_runtime(runtime), m_type(type), m_valobj(valobj) {
733
745
if (valobj)
734
746
m_exe_ctx = valobj->GetExecutionContextRef ();
747
+ SetFlavor ();
735
748
}
736
749
SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
737
750
ExecutionContextScope *exe_scope,
@@ -740,6 +753,7 @@ class SwiftRuntimeTypeVisitor {
740
753
m_include_clang_types(include_clang_types) {
741
754
if (exe_scope)
742
755
exe_scope->CalculateExecutionContext (m_exe_ctx);
756
+ SetFlavor ();
743
757
}
744
758
SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
745
759
ExecutionContext *exe_ctx, bool hide_superclass,
@@ -749,6 +763,7 @@ class SwiftRuntimeTypeVisitor {
749
763
m_visit_superclass(visit_superclass) {
750
764
if (exe_ctx)
751
765
m_exe_ctx = *exe_ctx;
766
+ SetFlavor ();
752
767
}
753
768
llvm::Error VisitAllChildren (VisitCallback callback) {
754
769
return VisitImpl ({}, callback).takeError ();
@@ -871,7 +886,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
871
886
field_type = tuple->element_type ;
872
887
else {
873
888
if (!field_type)
874
- field_type = GetTypeFromTypeRef (ts, field.TR );
889
+ field_type = GetTypeFromTypeRef (ts, field.TR , m_flavor );
875
890
}
876
891
auto get_info = [&]() -> llvm::Expected<ChildInfo> {
877
892
ChildInfo child;
@@ -968,7 +983,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
968
983
if (rti->getRecordKind () ==
969
984
swift::reflection::RecordKind::ClassExistential) {
970
985
// Compatibility with SwiftASTContext.
971
- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
986
+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
972
987
if (count_only)
973
988
return children.size ();
974
989
auto visit_existential = [&](ExistentialSyntheticChild c, unsigned idx) {
@@ -1019,7 +1034,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1019
1034
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
1020
1035
// Is this an Existential?
1021
1036
unsigned i = 0 ;
1022
- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
1037
+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
1023
1038
if (children.size ()) {
1024
1039
if (count_only)
1025
1040
return children.size ();
@@ -1109,7 +1124,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1109
1124
if (auto *super_tr = reflection_ctx->LookupSuperclass (
1110
1125
*tr, ts.GetDescriptorFinder ()))
1111
1126
if (auto error = visit_callback (
1112
- GetTypeFromTypeRef (ts, super_tr), depth,
1127
+ GetTypeFromTypeRef (ts, super_tr, m_flavor ), depth,
1113
1128
[]() -> std::string { return " <base class>" ; },
1114
1129
[]() -> llvm::Expected<ChildInfo> {
1115
1130
return ChildInfo ();
@@ -1143,8 +1158,9 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1143
1158
return ChildInfo ();
1144
1159
};
1145
1160
1146
- if (auto error = visit_callback (GetTypeFromTypeRef (ts, super_tr), 0 ,
1147
- get_name, get_info))
1161
+ if (auto error =
1162
+ visit_callback (GetTypeFromTypeRef (ts, super_tr, m_flavor), 0 ,
1163
+ get_name, get_info))
1148
1164
return error;
1149
1165
}
1150
1166
@@ -1263,7 +1279,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1263
1279
return success;
1264
1280
}
1265
1281
1266
- CompilerType super_type = GetTypeFromTypeRef (ts, type_ref);
1282
+ CompilerType super_type = GetTypeFromTypeRef (ts, type_ref, m_flavor );
1267
1283
auto get_name = [&]() -> std::string {
1268
1284
auto child_name = super_type.GetTypeName ().GetStringRef ().str ();
1269
1285
// FIXME: This should be fixed in GetDisplayTypeName instead!
@@ -1471,6 +1487,8 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1471
1487
if (!ti_or_err)
1472
1488
return ti_or_err.takeError ();
1473
1489
auto *ti = &*ti_or_err;
1490
+ auto flavor =
1491
+ SwiftLanguageRuntime::GetManglingFlavor (enum_type.GetMangledTypeName ());
1474
1492
1475
1493
auto project_indirect_enum =
1476
1494
[&](uint64_t offset, std::string name) -> llvm::Expected<ValueObjectSP> {
@@ -1496,13 +1514,14 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1496
1514
auto &field = rti->getFields ()[0 ];
1497
1515
auto *type_ref = field.TR ;
1498
1516
payload += field.Offset ;
1499
- payload_type = GetTypeFromTypeRef (ts, type_ref);
1517
+ payload_type = GetTypeFromTypeRef (ts, type_ref, flavor );
1500
1518
break ;
1501
1519
}
1502
1520
case swift::reflection::RecordKind::Tuple: {
1503
1521
std::vector<TypeSystemSwift::TupleElement> elts;
1504
1522
for (auto &field : rti->getFields ())
1505
- elts.emplace_back (ConstString (), GetTypeFromTypeRef (ts, field.TR ));
1523
+ elts.emplace_back (ConstString (),
1524
+ GetTypeFromTypeRef (ts, field.TR , flavor));
1506
1525
payload_type = ts.CreateTupleType (elts);
1507
1526
break ;
1508
1527
}
@@ -1571,7 +1590,7 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1571
1590
if (is_indirect_enum)
1572
1591
return project_indirect_enum (field_info.Offset , field_info.Name );
1573
1592
1574
- CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR );
1593
+ CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR , flavor );
1575
1594
if (field_info.Offset != 0 ) {
1576
1595
assert (false );
1577
1596
return llvm::createStringError (" enum with unexpected offset" );
@@ -1679,7 +1698,9 @@ CompilerType SwiftLanguageRuntime::GetBaseClass(CompilerType class_ty) {
1679
1698
}
1680
1699
auto *super_tr = reflection_ctx->LookupSuperclass (
1681
1700
*type_ref_or_err, tr_ts->GetDescriptorFinder ());
1682
- return GetTypeFromTypeRef (*tr_ts, super_tr);
1701
+ auto flavor =
1702
+ SwiftLanguageRuntime::GetManglingFlavor (class_ty.GetMangledTypeName ());
1703
+ return GetTypeFromTypeRef (*tr_ts, super_tr, flavor);
1683
1704
}
1684
1705
1685
1706
bool SwiftLanguageRuntime::ForEachSuperClassType (
@@ -3399,9 +3420,9 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
3399
3420
// Resolve all generic type parameters in the type for the current
3400
3421
// frame. Generic parameter binding has to happen in the scratch
3401
3422
// context.
3423
+ ExecutionContext exe_ctx;
3402
3424
if (exe_scope)
3403
3425
if (StackFrame *frame = exe_scope->CalculateStackFrame ().get ()) {
3404
- ExecutionContext exe_ctx;
3405
3426
frame->CalculateExecutionContext (exe_ctx);
3406
3427
auto bound_type_or_err = BindGenericTypeParameters (*frame, type);
3407
3428
if (!bound_type_or_err)
@@ -3423,6 +3444,30 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
3423
3444
if (!reflection_ctx)
3424
3445
return llvm::createStringError (" no reflection context" );
3425
3446
3447
+ // The TypeSystemSwiftTypeRefForExpressions doesn't ve a SymbolFile,
3448
+ // so any DWARF lookups for Embedded Swift fail.
3449
+ //
3450
+ // FIXME: It's unclear whether this is safe to do in a non-LTO Swift program.
3451
+ if (llvm::isa<TypeSystemSwiftTypeRefForExpressions>(tr_ts.get ()) &&
3452
+ tr_ts->GetManglingFlavor (&exe_ctx) ==
3453
+ swift::Mangle::ManglingFlavor::Embedded) {
3454
+ if (auto frame_sp = exe_ctx.GetFrameSP ()) {
3455
+ auto &sc = frame_sp->GetSymbolContext (eSymbolContextModule);
3456
+ if (sc.module_sp ) {
3457
+ auto ts_or_err =
3458
+ sc.module_sp ->GetTypeSystemForLanguage (eLanguageTypeSwift);
3459
+ if (!ts_or_err)
3460
+ return ts_or_err.takeError ();
3461
+ if (auto *tr_ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
3462
+ ts_or_err->get ())) {
3463
+ LLDBTypeInfoProvider provider (*this , *tr_ts);
3464
+ return reflection_ctx->GetTypeInfo (*type_ref_or_err, &provider,
3465
+ tr_ts->GetDescriptorFinder ());
3466
+ }
3467
+ }
3468
+ }
3469
+ }
3470
+
3426
3471
LLDBTypeInfoProvider provider (*this , ts);
3427
3472
return reflection_ctx->GetTypeInfo (*type_ref_or_err, &provider,
3428
3473
tr_ts->GetDescriptorFinder ());
@@ -3561,7 +3606,7 @@ SwiftLanguageRuntime::ResolveTypeAlias(CompilerType alias) {
3561
3606
type_ref = &*type_ref_or_err;
3562
3607
}
3563
3608
3564
- CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref);
3609
+ CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref, flavor );
3565
3610
LLDB_LOG (GetLog (LLDBLog::Types),
3566
3611
" Resolved type alias {0} = {1} using reflection metadata." ,
3567
3612
alias.GetMangledTypeName (), resolved.GetMangledTypeName ());
0 commit comments