1111// ===----------------------------------------------------------------------===//
1212
1313#include " LLDBMemoryReader.h"
14+ #include " Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
1415#include " ReflectionContextInterface.h"
1516#include " SwiftLanguageRuntime.h"
1617#include " SwiftMetadataCache.h"
3132#include " lldb/ValueObject/ValueObjectCast.h"
3233#include " lldb/ValueObject/ValueObjectMemory.h"
3334#include " llvm/ADT/STLExtras.h"
35+ #include " llvm/Support/Casting.h"
3436
3537#include " swift/AST/ASTContext.h"
3638#include " swift/AST/ASTMangler.h"
@@ -607,13 +609,14 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffset(
607609namespace {
608610
609611CompilerType GetTypeFromTypeRef (TypeSystemSwiftTypeRef &ts,
610- const swift::reflection::TypeRef *type_ref) {
612+ const swift::reflection::TypeRef *type_ref,
613+ swift::Mangle::ManglingFlavor flavor) {
611614 if (!type_ref)
612615 return {};
613616 swift::Demangle::Demangler dem;
614617 swift::Demangle::NodePointer node = type_ref->getDemangling (dem);
615618 // TODO: the mangling flavor should come from the TypeRef.
616- return ts.RemangleAsType (dem, node, ts. GetManglingFlavor () );
619+ return ts.RemangleAsType (dem, node, flavor );
617620}
618621
619622struct ExistentialSyntheticChild {
@@ -627,7 +630,8 @@ struct ExistentialSyntheticChild {
627630llvm::SmallVector<ExistentialSyntheticChild, 4 >
628631GetExistentialSyntheticChildren (TypeSystemSwiftTypeRef &ts,
629632 const swift::reflection::TypeRef *tr,
630- const swift::reflection::TypeInfo *ti) {
633+ const swift::reflection::TypeInfo *ti,
634+ swift::Mangle::ManglingFlavor flavor) {
631635 llvm::SmallVector<ExistentialSyntheticChild, 4 > children;
632636 auto *protocol_composition_tr =
633637 llvm::dyn_cast<swift::reflection::ProtocolCompositionTypeRef>(tr);
@@ -641,7 +645,8 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
641645 children.push_back ({" object" , [=]() {
642646 if (auto *super_class_tr =
643647 protocol_composition_tr->getSuperclass ())
644- return GetTypeFromTypeRef (*ts_sp, super_class_tr);
648+ return GetTypeFromTypeRef (*ts_sp, super_class_tr,
649+ flavor);
645650 else
646651 return rti ? ts_sp->GetBuiltinUnknownObjectType ()
647652 : ts_sp->GetBuiltinRawPointerType ();
@@ -652,9 +657,9 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
652657 for (unsigned i = 1 ; i < fields.size (); ++i) {
653658 TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef ();
654659 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); }});
658663 }
659664 }
660665 }
@@ -702,12 +707,20 @@ CompilerType GetTypedefedTypeRecursive(CompilerType type) {
702707class SwiftRuntimeTypeVisitor {
703708 SwiftLanguageRuntime &m_runtime;
704709 ExecutionContext m_exe_ctx;
710+ swift::Mangle::ManglingFlavor m_flavor =
711+ swift::Mangle::ManglingFlavor::Default;
705712 CompilerType m_type;
706713 ValueObject *m_valobj = nullptr ;
707714 bool m_hide_superclass = false ;
708715 bool m_include_clang_types = false ;
709716 bool m_visit_superclass = false ;
710717
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+
711724public:
712725 struct ChildInfo {
713726 uint32_t byte_size = 0 ;
@@ -732,6 +745,7 @@ class SwiftRuntimeTypeVisitor {
732745 : m_runtime(runtime), m_type(type), m_valobj(valobj) {
733746 if (valobj)
734747 m_exe_ctx = valobj->GetExecutionContextRef ();
748+ SetFlavor ();
735749 }
736750 SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
737751 ExecutionContextScope *exe_scope,
@@ -740,6 +754,7 @@ class SwiftRuntimeTypeVisitor {
740754 m_include_clang_types(include_clang_types) {
741755 if (exe_scope)
742756 exe_scope->CalculateExecutionContext (m_exe_ctx);
757+ SetFlavor ();
743758 }
744759 SwiftRuntimeTypeVisitor (SwiftLanguageRuntime &runtime, CompilerType type,
745760 ExecutionContext *exe_ctx, bool hide_superclass,
@@ -749,6 +764,7 @@ class SwiftRuntimeTypeVisitor {
749764 m_visit_superclass(visit_superclass) {
750765 if (exe_ctx)
751766 m_exe_ctx = *exe_ctx;
767+ SetFlavor ();
752768 }
753769 llvm::Error VisitAllChildren (VisitCallback callback) {
754770 return VisitImpl ({}, callback).takeError ();
@@ -776,11 +792,11 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
776792
777793 const unsigned success = 0 ;
778794 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 () ;
784800
785801 // Deal with the LLDB-only SILPackType variant.
786802 if (auto pack_type_info = ts.IsSILPackType (m_type)) {
@@ -871,7 +887,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
871887 field_type = tuple->element_type ;
872888 else {
873889 if (!field_type)
874- field_type = GetTypeFromTypeRef (ts, field.TR );
890+ field_type = GetTypeFromTypeRef (ts, field.TR , m_flavor );
875891 }
876892 auto get_info = [&]() -> llvm::Expected<ChildInfo> {
877893 ChildInfo child;
@@ -968,7 +984,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
968984 if (rti->getRecordKind () ==
969985 swift::reflection::RecordKind::ClassExistential) {
970986 // Compatibility with SwiftASTContext.
971- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
987+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
972988 if (count_only)
973989 return children.size ();
974990 auto visit_existential = [&](ExistentialSyntheticChild c, unsigned idx) {
@@ -1019,7 +1035,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
10191035 llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
10201036 // Is this an Existential?
10211037 unsigned i = 0 ;
1022- auto children = GetExistentialSyntheticChildren (ts, tr, ti);
1038+ auto children = GetExistentialSyntheticChildren (ts, tr, ti, m_flavor );
10231039 if (children.size ()) {
10241040 if (count_only)
10251041 return children.size ();
@@ -1109,7 +1125,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
11091125 if (auto *super_tr = reflection_ctx->LookupSuperclass (
11101126 *tr, ts.GetDescriptorFinder ()))
11111127 if (auto error = visit_callback (
1112- GetTypeFromTypeRef (ts, super_tr), depth,
1128+ GetTypeFromTypeRef (ts, super_tr, m_flavor ), depth,
11131129 []() -> std::string { return " <base class>" ; },
11141130 []() -> llvm::Expected<ChildInfo> {
11151131 return ChildInfo ();
@@ -1143,8 +1159,9 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
11431159 return ChildInfo ();
11441160 };
11451161
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))
11481165 return error;
11491166 }
11501167
@@ -1263,7 +1280,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
12631280 return success;
12641281 }
12651282
1266- CompilerType super_type = GetTypeFromTypeRef (ts, type_ref);
1283+ CompilerType super_type = GetTypeFromTypeRef (ts, type_ref, m_flavor );
12671284 auto get_name = [&]() -> std::string {
12681285 auto child_name = super_type.GetTypeName ().GetStringRef ().str ();
12691286 // FIXME: This should be fixed in GetDisplayTypeName instead!
@@ -1471,6 +1488,8 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
14711488 if (!ti_or_err)
14721489 return ti_or_err.takeError ();
14731490 auto *ti = &*ti_or_err;
1491+ auto flavor =
1492+ SwiftLanguageRuntime::GetManglingFlavor (enum_type.GetMangledTypeName ());
14741493
14751494 auto project_indirect_enum =
14761495 [&](uint64_t offset, std::string name) -> llvm::Expected<ValueObjectSP> {
@@ -1496,13 +1515,14 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
14961515 auto &field = rti->getFields ()[0 ];
14971516 auto *type_ref = field.TR ;
14981517 payload += field.Offset ;
1499- payload_type = GetTypeFromTypeRef (ts, type_ref);
1518+ payload_type = GetTypeFromTypeRef (ts, type_ref, flavor );
15001519 break ;
15011520 }
15021521 case swift::reflection::RecordKind::Tuple: {
15031522 std::vector<TypeSystemSwift::TupleElement> elts;
15041523 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));
15061526 payload_type = ts.CreateTupleType (elts);
15071527 break ;
15081528 }
@@ -1570,7 +1590,7 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
15701590 if (is_indirect_enum)
15711591 return project_indirect_enum (field_info.Offset , field_info.Name );
15721592
1573- CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR );
1593+ CompilerType projected_type = GetTypeFromTypeRef (ts, field_info.TR , flavor );
15741594 if (field_info.Offset != 0 ) {
15751595 assert (false );
15761596 return llvm::createStringError (" enum with unexpected offset" );
@@ -1678,7 +1698,9 @@ CompilerType SwiftLanguageRuntime::GetBaseClass(CompilerType class_ty) {
16781698 }
16791699 auto *super_tr = reflection_ctx->LookupSuperclass (
16801700 *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);
16821704}
16831705
16841706bool SwiftLanguageRuntime::ForEachSuperClassType (
@@ -3362,6 +3384,42 @@ SwiftLanguageRuntime::GetTypeRef(CompilerType type,
33623384 return type_ref_or_err;
33633385}
33643386
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+
33653423llvm::Expected<const swift::reflection::TypeInfo &>
33663424SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo (
33673425 CompilerType type, ExecutionContextScope *exe_scope,
@@ -3372,14 +3430,17 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
33723430 " [SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo] Getting "
33733431 " type info for type: {0}" ,
33743432 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 ();
33833444
33843445 // Resolve all type aliases.
33853446 type = type.GetCanonicalType ();
@@ -3393,15 +3454,13 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
33933454 // Resolve all generic type parameters in the type for the current
33943455 // frame. Generic parameter binding has to happen in the scratch
33953456 // 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+ }
34053464
34063465 // BindGenericTypeParameters imports the type into the scratch
34073466 // context, but we need to resolve (any DWARF links in) the typeref
@@ -3419,7 +3478,7 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
34193478
34203479 LLDBTypeInfoProvider provider (*this , ts);
34213480 return reflection_ctx->GetTypeInfo (*type_ref_or_err, &provider,
3422- tr_ts-> GetDescriptorFinder ());
3481+ ts. GetDescriptorFinder ());
34233482}
34243483
34253484bool SwiftLanguageRuntime::IsStoredInlineInBuffer (CompilerType type) {
@@ -3555,7 +3614,7 @@ SwiftLanguageRuntime::ResolveTypeAlias(CompilerType alias) {
35553614 type_ref = &*type_ref_or_err;
35563615 }
35573616
3558- CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref);
3617+ CompilerType resolved = GetTypeFromTypeRef (*tr_ts, type_ref, flavor );
35593618 LLDB_LOG (GetLog (LLDBLog::Types),
35603619 " Resolved type alias {0} = {1} using reflection metadata." ,
35613620 alias.GetMangledTypeName (), resolved.GetMangledTypeName ());
0 commit comments