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"
31
32
#include " lldb/ValueObject/ValueObjectCast.h"
32
33
#include " lldb/ValueObject/ValueObjectMemory.h"
33
34
#include " llvm/ADT/STLExtras.h"
35
+ #include " llvm/Support/Casting.h"
34
36
35
37
#include " swift/AST/ASTContext.h"
36
38
#include " swift/AST/ASTMangler.h"
@@ -607,13 +609,14 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffset(
607
609
namespace {
608
610
609
611
CompilerType GetTypeFromTypeRef (TypeSystemSwiftTypeRef &ts,
610
- const swift::reflection::TypeRef *type_ref) {
612
+ const swift::reflection::TypeRef *type_ref,
613
+ swift::Mangle::ManglingFlavor flavor) {
611
614
if (!type_ref)
612
615
return {};
613
616
swift::Demangle::Demangler dem;
614
617
swift::Demangle::NodePointer node = type_ref->getDemangling (dem);
615
618
// TODO: the mangling flavor should come from the TypeRef.
616
- return ts.RemangleAsType (dem, node, ts. GetManglingFlavor () );
619
+ return ts.RemangleAsType (dem, node, flavor );
617
620
}
618
621
619
622
struct ExistentialSyntheticChild {
@@ -627,7 +630,8 @@ struct ExistentialSyntheticChild {
627
630
llvm::SmallVector<ExistentialSyntheticChild, 4 >
628
631
GetExistentialSyntheticChildren (TypeSystemSwiftTypeRef &ts,
629
632
const swift::reflection::TypeRef *tr,
630
- const swift::reflection::TypeInfo *ti) {
633
+ const swift::reflection::TypeInfo *ti,
634
+ swift::Mangle::ManglingFlavor flavor) {
631
635
llvm::SmallVector<ExistentialSyntheticChild, 4 > children;
632
636
auto *protocol_composition_tr =
633
637
llvm::dyn_cast<swift::reflection::ProtocolCompositionTypeRef>(tr);
@@ -641,7 +645,8 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
641
645
children.push_back ({" object" , [=]() {
642
646
if (auto *super_class_tr =
643
647
protocol_composition_tr->getSuperclass ())
644
- return GetTypeFromTypeRef (*ts_sp, super_class_tr);
648
+ return GetTypeFromTypeRef (*ts_sp, super_class_tr,
649
+ flavor);
645
650
else
646
651
return rti ? ts_sp->GetBuiltinUnknownObjectType ()
647
652
: ts_sp->GetBuiltinRawPointerType ();
@@ -652,9 +657,9 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
652
657
for (unsigned i = 1 ; i < fields.size (); ++i) {
653
658
TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef ();
654
659
auto *type_ref = fields[i].TR ;
655
- children.push_back ({fields[i]. Name , [=]() {
656
- return GetTypeFromTypeRef (*ts_sp, type_ref);
657
- }});
660
+ children.push_back (
661
+ {fields[i]. Name ,
662
+ [=]() { return GetTypeFromTypeRef (*ts_sp, type_ref, flavor); }});
658
663
}
659
664
}
660
665
}
@@ -702,12 +707,20 @@ CompilerType GetTypedefedTypeRecursive(CompilerType type) {
702
707
class SwiftRuntimeTypeVisitor {
703
708
SwiftLanguageRuntime &m_runtime;
704
709
ExecutionContext m_exe_ctx;
710
+ swift::Mangle::ManglingFlavor m_flavor =
711
+ swift::Mangle::ManglingFlavor::Default;
705
712
CompilerType m_type;
706
713
ValueObject *m_valobj = nullptr ;
707
714
bool m_hide_superclass = false ;
708
715
bool m_include_clang_types = false ;
709
716
bool m_visit_superclass = false ;
710
717
718
+ void SetFlavor () {
719
+ if (auto ts_sp =
720
+ m_type.GetTypeSystem ().dyn_cast_or_null <TypeSystemSwiftTypeRef>())
721
+ m_flavor = ts_sp->GetManglingFlavor (&m_exe_ctx);
722
+ }
723
+
711
724
public:
712
725
struct ChildInfo {
713
726
uint32_t byte_size = 0 ;
@@ -732,6 +745,7 @@ class SwiftRuntimeTypeVisitor {
732
745
: m_runtime(runtime), m_type(type), m_valobj(valobj) {
733
746
if (valobj)
734
747
m_exe_ctx = valobj->GetExecutionContextRef ();
748
+ SetFlavor ();
735
749
}
736
750
SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
737
751
ExecutionContextScope *exe_scope,
@@ -740,6 +754,7 @@ class SwiftRuntimeTypeVisitor {
740
754
m_include_clang_types(include_clang_types) {
741
755
if (exe_scope)
742
756
exe_scope->CalculateExecutionContext (m_exe_ctx);
757
+ SetFlavor ();
743
758
}
744
759
SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
745
760
ExecutionContext *exe_ctx, bool hide_superclass,
@@ -749,6 +764,7 @@ class SwiftRuntimeTypeVisitor {
749
764
m_visit_superclass(visit_superclass) {
750
765
if (exe_ctx)
751
766
m_exe_ctx = *exe_ctx;
767
+ SetFlavor ();
752
768
}
753
769
llvm::Error VisitAllChildren (VisitCallback callback) {
754
770
return VisitImpl ({}, callback).takeError ();
@@ -776,11 +792,11 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
776
792
777
793
const unsigned success = 0 ;
778
794
bool count_only = !visit_callback;
779
- auto ts_sp =
780
- m_type. GetTypeSystem (). dyn_cast_or_null <TypeSystemSwiftTypeRef>( );
781
- if (!ts_sp )
782
- return llvm::createStringError ( " no type system " );
783
- auto &ts = *ts_sp ;
795
+ auto ts_or_err =
796
+ SwiftLanguageRuntime::GetReflectionTypeSystem (m_type, m_exe_ctx );
797
+ if (!ts_or_err )
798
+ return ts_or_err. takeError ( );
799
+ auto &ts = *ts_or_err-> get () ;
784
800
785
801
// Deal with the LLDB-only SILPackType variant.
786
802
if (auto pack_type_info = ts.IsSILPackType (m_type)) {
@@ -871,7 +887,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
871
887
field_type = tuple->element_type ;
872
888
else {
873
889
if (!field_type)
874
- field_type = GetTypeFromTypeRef (ts, field.TR );
890
+ field_type = GetTypeFromTypeRef (ts, field.TR , m_flavor );
875
891
}
876
892
auto get_info = [&]() -> llvm::Expected<ChildInfo> {
877
893
ChildInfo child;
@@ -968,7 +984,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
968
984
if (rti->getRecordKind () ==
969
985
swift::reflection::RecordKind::ClassExistential) {
970
986
// Compatibility with SwiftASTContext.
971
- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
987
+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
972
988
if (count_only)
973
989
return children.size ();
974
990
auto visit_existential = [&](ExistentialSyntheticChild c, unsigned idx) {
@@ -1019,7 +1035,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1019
1035
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
1020
1036
// Is this an Existential?
1021
1037
unsigned i = 0 ;
1022
- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
1038
+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
1023
1039
if (children.size ()) {
1024
1040
if (count_only)
1025
1041
return children.size ();
@@ -1109,7 +1125,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1109
1125
if (auto *super_tr = reflection_ctx->LookupSuperclass (
1110
1126
*tr, ts.GetDescriptorFinder ()))
1111
1127
if (auto error = visit_callback (
1112
- GetTypeFromTypeRef (ts, super_tr), depth,
1128
+ GetTypeFromTypeRef (ts, super_tr, m_flavor ), depth,
1113
1129
[]() -> std::string { return " <base class>" ; },
1114
1130
[]() -> llvm::Expected<ChildInfo> {
1115
1131
return ChildInfo ();
@@ -1143,8 +1159,9 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1143
1159
return ChildInfo ();
1144
1160
};
1145
1161
1146
- if (auto error = visit_callback (GetTypeFromTypeRef (ts, super_tr), 0 ,
1147
- get_name, get_info))
1162
+ if (auto error =
1163
+ visit_callback (GetTypeFromTypeRef (ts, super_tr, m_flavor), 0 ,
1164
+ get_name, get_info))
1148
1165
return error;
1149
1166
}
1150
1167
@@ -1263,7 +1280,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
1263
1280
return success;
1264
1281
}
1265
1282
1266
- CompilerType super_type = GetTypeFromTypeRef (ts, type_ref);
1283
+ CompilerType super_type = GetTypeFromTypeRef (ts, type_ref, m_flavor );
1267
1284
auto get_name = [&]() -> std::string {
1268
1285
auto child_name = super_type.GetTypeName ().GetStringRef ().str ();
1269
1286
// FIXME: This should be fixed in GetDisplayTypeName instead!
@@ -1471,6 +1488,8 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1471
1488
if (!ti_or_err)
1472
1489
return ti_or_err.takeError ();
1473
1490
auto *ti = &*ti_or_err;
1491
+ auto flavor =
1492
+ SwiftLanguageRuntime::GetManglingFlavor (enum_type.GetMangledTypeName ());
1474
1493
1475
1494
auto project_indirect_enum =
1476
1495
[&](uint64_t offset, std::string name) -> llvm::Expected<ValueObjectSP> {
@@ -1496,13 +1515,14 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1496
1515
auto &field = rti->getFields ()[0 ];
1497
1516
auto *type_ref = field.TR ;
1498
1517
payload += field.Offset ;
1499
- payload_type = GetTypeFromTypeRef (ts, type_ref);
1518
+ payload_type = GetTypeFromTypeRef (ts, type_ref, flavor );
1500
1519
break ;
1501
1520
}
1502
1521
case swift::reflection::RecordKind::Tuple: {
1503
1522
std::vector<TypeSystemSwift::TupleElement> elts;
1504
1523
for (auto &field : rti->getFields ())
1505
- elts.emplace_back (ConstString (), GetTypeFromTypeRef (ts, field.TR ));
1524
+ elts.emplace_back (ConstString (),
1525
+ GetTypeFromTypeRef (ts, field.TR , flavor));
1506
1526
payload_type = ts.CreateTupleType (elts);
1507
1527
break ;
1508
1528
}
@@ -1570,7 +1590,7 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
1570
1590
if (is_indirect_enum)
1571
1591
return project_indirect_enum (field_info.Offset , field_info.Name );
1572
1592
1573
- CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR );
1593
+ CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR , flavor );
1574
1594
if (field_info.Offset != 0 ) {
1575
1595
assert (false );
1576
1596
return llvm::createStringError (" enum with unexpected offset" );
@@ -1678,7 +1698,9 @@ CompilerType SwiftLanguageRuntime::GetBaseClass(CompilerType class_ty) {
1678
1698
}
1679
1699
auto *super_tr = reflection_ctx->LookupSuperclass (
1680
1700
*type_ref_or_err, tr_ts->GetDescriptorFinder ());
1681
- return GetTypeFromTypeRef (*tr_ts, super_tr);
1701
+ auto flavor =
1702
+ SwiftLanguageRuntime::GetManglingFlavor (class_ty.GetMangledTypeName ());
1703
+ return GetTypeFromTypeRef (*tr_ts, super_tr, flavor);
1682
1704
}
1683
1705
1684
1706
bool SwiftLanguageRuntime::ForEachSuperClassType (
@@ -3362,6 +3384,42 @@ SwiftLanguageRuntime::GetTypeRef(CompilerType type,
3362
3384
return type_ref_or_err;
3363
3385
}
3364
3386
3387
+ llvm::Expected<TypeSystemSwiftTypeRefSP>
3388
+ SwiftLanguageRuntime::GetReflectionTypeSystem (CompilerType for_type,
3389
+ ExecutionContext exe_ctx) {
3390
+ auto ts_sp = for_type.GetTypeSystem ().dyn_cast_or_null <TypeSystemSwift>();
3391
+ if (!ts_sp)
3392
+ return llvm::createStringError (" not a Swift type" );
3393
+
3394
+ // The TypeSystemSwiftTypeRefForExpressions doesn't have a SymbolFile,
3395
+ // so any DWARF lookups for Embedded Swift fail.
3396
+ //
3397
+ // FIXME: It's unclear whether this is safe to do in a non-LTO Swift program.
3398
+ if (auto *tr_ts =
3399
+ llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
3400
+ ts_sp.get ())) {
3401
+ if (tr_ts->GetManglingFlavor (&exe_ctx) ==
3402
+ swift::Mangle::ManglingFlavor::Embedded) {
3403
+ if (auto *frame = exe_ctx.GetFramePtr ()) {
3404
+ auto &sc = frame->GetSymbolContext (eSymbolContextModule);
3405
+ if (sc.module_sp ) {
3406
+ auto ts_or_err =
3407
+ sc.module_sp ->GetTypeSystemForLanguage (eLanguageTypeSwift);
3408
+ if (!ts_or_err)
3409
+ return ts_or_err.takeError ();
3410
+ if (auto *tr_ts =
3411
+ llvm::dyn_cast_or_null<TypeSystemSwift>(ts_or_err->get ()))
3412
+ ts_sp = tr_ts->GetTypeSystemSwiftTypeRef ();
3413
+ }
3414
+ }
3415
+ }
3416
+ }
3417
+ auto tr_ts = ts_sp->GetTypeSystemSwiftTypeRef ();
3418
+ if (!tr_ts)
3419
+ return llvm::createStringError (" no Swift typesystem" );
3420
+ return tr_ts;
3421
+ }
3422
+
3365
3423
llvm::Expected<const swift::reflection::TypeInfo &>
3366
3424
SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo (
3367
3425
CompilerType type, ExecutionContextScope *exe_scope,
@@ -3372,14 +3430,17 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
3372
3430
" [SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo] Getting "
3373
3431
" type info for type: {0}" ,
3374
3432
type.GetMangledTypeName ());
3375
-
3376
- auto ts_sp = type.GetTypeSystem ().dyn_cast_or_null <TypeSystemSwift>();
3377
- if (!ts_sp)
3378
- return llvm::createStringError (" not a Swift type" );
3379
- auto tr_ts = ts_sp->GetTypeSystemSwiftTypeRef ();
3380
- if (!tr_ts)
3381
- return llvm::createStringError (" no Swift typesystem" );
3382
- auto &ts = *tr_ts;
3433
+ StackFrame *frame = nullptr ;
3434
+ ExecutionContext exe_ctx;
3435
+ if (exe_scope) {
3436
+ frame = exe_scope->CalculateStackFrame ().get ();
3437
+ if (frame)
3438
+ frame->CalculateExecutionContext (exe_ctx);
3439
+ }
3440
+ auto ts_or_err = GetReflectionTypeSystem (type, exe_ctx);
3441
+ if (!ts_or_err)
3442
+ return ts_or_err.takeError ();
3443
+ auto &ts = *ts_or_err->get ();
3383
3444
3384
3445
// Resolve all type aliases.
3385
3446
type = type.GetCanonicalType ();
@@ -3393,15 +3454,13 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
3393
3454
// Resolve all generic type parameters in the type for the current
3394
3455
// frame. Generic parameter binding has to happen in the scratch
3395
3456
// context.
3396
- if (exe_scope)
3397
- if (StackFrame *frame = exe_scope->CalculateStackFrame ().get ()) {
3398
- ExecutionContext exe_ctx;
3399
- frame->CalculateExecutionContext (exe_ctx);
3400
- auto bound_type_or_err = BindGenericTypeParameters (*frame, type);
3401
- if (!bound_type_or_err)
3402
- return bound_type_or_err.takeError ();
3403
- type = *bound_type_or_err;
3404
- }
3457
+ if (frame) {
3458
+ frame->CalculateExecutionContext (exe_ctx);
3459
+ auto bound_type_or_err = BindGenericTypeParameters (*frame, type);
3460
+ if (!bound_type_or_err)
3461
+ return bound_type_or_err.takeError ();
3462
+ type = *bound_type_or_err;
3463
+ }
3405
3464
3406
3465
// BindGenericTypeParameters imports the type into the scratch
3407
3466
// context, but we need to resolve (any DWARF links in) the typeref
@@ -3419,7 +3478,7 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
3419
3478
3420
3479
LLDBTypeInfoProvider provider (*this , ts);
3421
3480
return reflection_ctx->GetTypeInfo (*type_ref_or_err, &provider,
3422
- tr_ts-> GetDescriptorFinder ());
3481
+ ts. GetDescriptorFinder ());
3423
3482
}
3424
3483
3425
3484
bool SwiftLanguageRuntime::IsStoredInlineInBuffer (CompilerType type) {
@@ -3555,7 +3614,7 @@ SwiftLanguageRuntime::ResolveTypeAlias(CompilerType alias) {
3555
3614
type_ref = &*type_ref_or_err;
3556
3615
}
3557
3616
3558
- CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref);
3617
+ CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref, flavor );
3559
3618
LLDB_LOG (GetLog (LLDBLog::Types),
3560
3619
" Resolved type alias {0} = {1} using reflection metadata." ,
3561
3620
alias.GetMangledTypeName (), resolved.GetMangledTypeName ());
0 commit comments