Skip to content

Commit 993b2f7

Browse files
Merge pull request #7262 from adrian-prantl/113997661-swiftself-5.9
Avoid a roundtrip through SwiftASTContext in findSwiftSelf() (NFC)
2 parents 379927e + 483ae8b commit 993b2f7

File tree

7 files changed

+73
-35
lines changed

7 files changed

+73
-35
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ struct SwiftSelfInfo {
115115

116116
/// Adjusted type of `self`. If we're in a static method, this is an instance
117117
/// type.
118-
CompilerType type = {};
119-
120-
/// Underlying Swift type for the adjusted type of `self`.
121-
swift::TypeBase *swift_type = nullptr;
118+
CompilerType type;
122119

123120
/// Type flags for the adjusted type of `self`.
124121
Flags type_flags = {};
@@ -143,7 +140,7 @@ findSwiftSelf(StackFrame &frame, lldb::VariableSP self_var_sp) {
143140

144141
// 3) If (1) and (2) fail, give up.
145142
if (!info.type.IsValid())
146-
return llvm::None;
143+
return {};
147144

148145
// 4) If `self` is a metatype, get its instance type.
149146
if (Flags(info.type.GetTypeInfo())
@@ -152,20 +149,15 @@ findSwiftSelf(StackFrame &frame, lldb::VariableSP self_var_sp) {
152149
info.is_metatype = true;
153150
}
154151

155-
info.swift_type = GetSwiftType(info.type).getPointer();
156-
if (auto *dyn_self =
157-
llvm::dyn_cast_or_null<swift::DynamicSelfType>(info.swift_type))
158-
info.swift_type = dyn_self->getSelfType().getPointer();
159-
160-
// 5) If the adjusted type isn't equal to the type according to the runtime,
161-
// switch it to the latter type.
162-
if (info.swift_type && (info.swift_type != info.type.GetOpaqueQualType()))
163-
info.type = ToCompilerType(info.swift_type);
152+
auto ts = info.type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
153+
if (!ts)
154+
return {};
155+
info.type = ts->GetStaticSelfType(info.type.GetOpaqueQualType());
164156

165157
info.type_flags = Flags(info.type.GetTypeInfo());
166158

167159
if (!info.type.IsValid())
168-
return llvm::None;
160+
return {};
169161
return info;
170162
}
171163

@@ -255,12 +247,18 @@ void SwiftUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
255247
m_is_class |= info.type_flags.Test(lldb::eTypeIsClass);
256248

257249
// Handle weak self.
258-
auto *ref_type =
259-
llvm::dyn_cast_or_null<swift::ReferenceStorageType>(info.swift_type);
260-
if (ref_type && ref_type->getOwnership() == swift::ReferenceOwnership::Weak) {
261-
m_is_class = true;
262-
m_is_weak_self = true;
263-
}
250+
251+
auto ts = info.type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
252+
if (!ts)
253+
return;
254+
255+
if (auto ownership_kind = ts->GetNonTriviallyManagedReferenceKind(
256+
info.type.GetOpaqueQualType()))
257+
if (*ownership_kind ==
258+
SwiftASTContext::NonTriviallyManagedReferenceKind::eWeak) {
259+
m_is_class = true;
260+
m_is_weak_self = true;
261+
}
264262

265263
m_needs_object_ptr = !m_in_static_method;
266264

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,20 +2442,9 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
24422442
}
24432443

24442444
Demangler dem;
2445-
NodePointer canonical = TypeSystemSwiftTypeRef::Transform(
2446-
dem, dem.demangleSymbol(mangled_name.GetStringRef()),
2447-
[](NodePointer node) {
2448-
if (node->getKind() != Node::Kind::DynamicSelf)
2449-
return node;
2450-
// Substitute the static type for dynamic self.
2451-
assert(node->getNumChildren() == 1);
2452-
if (node->getNumChildren() != 1)
2453-
return node;
2454-
NodePointer type = node->getChild(0);
2455-
if (type->getKind() != Node::Kind::Type || type->getNumChildren() != 1)
2456-
return node;
2457-
return type->getChild(0);
2458-
});
2445+
2446+
NodePointer canonical = TypeSystemSwiftTypeRef::GetStaticSelfType(
2447+
dem, dem.demangleSymbol(mangled_name.GetStringRef()));
24592448

24602449
// Build the list of type substitutions.
24612450
swift::reflection::GenericArgumentMap substitutions;

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5166,6 +5166,17 @@ CompilerType SwiftASTContext::GetReferentType(opaque_compiler_type_t type) {
51665166
return ToCompilerType({ref_type});
51675167
}
51685168

5169+
CompilerType
5170+
SwiftASTContext::GetStaticSelfType(lldb::opaque_compiler_type_t type) {
5171+
VALID_OR_RETURN_CHECK_TYPE(type, CompilerType());
5172+
5173+
swift::Type swift_type = GetSwiftType(type);
5174+
if (auto *dyn_self =
5175+
llvm::dyn_cast_or_null<swift::DynamicSelfType>(swift_type))
5176+
return ToCompilerType({dyn_self->getSelfType().getPointer()});
5177+
return {weak_from_this(), type};
5178+
}
5179+
51695180
bool SwiftASTContext::IsFullyRealized(const CompilerType &compiler_type) {
51705181
if (swift::CanType swift_can_type = ::GetCanonicalSwiftType(compiler_type)) {
51715182
if (swift::isa<swift::MetatypeType>(swift_can_type))

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ class SwiftASTContext : public TypeSystemSwift {
806806
CompilerType *original_type) override;
807807

808808
CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;
809+
CompilerType GetStaticSelfType(lldb::opaque_compiler_type_t type) override;
809810

810811
/// Retrieve/import the modules imported by the compilation
811812
/// unit. Early-exists with false if there was an import failure.

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class TypeSystemSwift : public TypeSystem {
128128
virtual CompilerType GetReferentType(lldb::opaque_compiler_type_t type) = 0;
129129
static CompilerType GetInstanceType(CompilerType ct);
130130
virtual CompilerType GetInstanceType(lldb::opaque_compiler_type_t type) = 0;
131+
/// Return the static type if this is a DynamicSelf type else the input type.
132+
virtual CompilerType GetStaticSelfType(lldb::opaque_compiler_type_t type) = 0;
131133
enum class TypeAllocationStrategy { eInline, ePointer, eDynamic, eUnknown };
132134
struct TupleElement {
133135
ConstString element_name;

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,38 @@ TypeSystemSwiftTypeRef::GetReferentType(opaque_compiler_type_t type) {
34293429
(ReconstructType(type)), (ReconstructType(type)));
34303430
}
34313431

3432+
swift::Demangle::NodePointer
3433+
TypeSystemSwiftTypeRef::GetStaticSelfType(swift::Demangle::Demangler &dem,
3434+
swift::Demangle::NodePointer node) {
3435+
using namespace swift::Demangle;
3436+
return TypeSystemSwiftTypeRef::Transform(dem, node, [](NodePointer node) {
3437+
if (node->getKind() != Node::Kind::DynamicSelf)
3438+
return node;
3439+
// Substitute the static type for dynamic self.
3440+
assert(node->getNumChildren() == 1);
3441+
if (node->getNumChildren() != 1)
3442+
return node;
3443+
NodePointer type = node->getChild(0);
3444+
if (type->getKind() != Node::Kind::Type || type->getNumChildren() != 1)
3445+
return node;
3446+
return type->getChild(0);
3447+
});
3448+
}
3449+
3450+
CompilerType
3451+
TypeSystemSwiftTypeRef::GetStaticSelfType(lldb::opaque_compiler_type_t type) {
3452+
auto impl = [&]() -> CompilerType {
3453+
using namespace swift::Demangle;
3454+
Demangler dem;
3455+
NodePointer node = GetDemangledType(dem, AsMangledName(type));
3456+
auto *type_node = dem.createNode(Node::Kind::Type);
3457+
type_node->addChild(GetStaticSelfType(dem, node), dem);
3458+
return RemangleAsType(dem, type_node);
3459+
};
3460+
VALIDATE_AND_RETURN(impl, GetStaticSelfType, type, g_no_exe_ctx,
3461+
(ReconstructType(type)), (ReconstructType(type)));
3462+
}
3463+
34323464
CompilerType
34333465
TypeSystemSwiftTypeRef::GetInstanceType(opaque_compiler_type_t type) {
34343466
auto impl = [&]() -> CompilerType {

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
271271
CompilerType GetErrorType() override;
272272
CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;
273273
CompilerType GetInstanceType(lldb::opaque_compiler_type_t type) override;
274+
CompilerType GetStaticSelfType(lldb::opaque_compiler_type_t type) override;
275+
static swift::Demangle::NodePointer
276+
GetStaticSelfType(swift::Demangle::Demangler &dem,
277+
swift::Demangle::NodePointer node);
278+
274279
/// Wrap type inside a SILPackType.
275280
CompilerType CreateSILPackType(CompilerType type, bool indirect);
276281
struct PackTypeInfo {

0 commit comments

Comments
 (0)