Skip to content

Commit f477a72

Browse files
author
git apple-llvm automerger
committed
Merge commit '4e25a1b3b69d' from swift/release/6.2 into stable/20240723
2 parents 3afe324 + 4e25a1b commit f477a72

File tree

10 files changed

+123
-40
lines changed

10 files changed

+123
-40
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class SwiftLanguageRuntime : public LanguageRuntime {
298298
llvm::Error GetObjectDescription(Stream &str, ValueObject &object) override;
299299
CompilerType GetConcreteType(ExecutionContextScope *exe_scope,
300300
ConstString abstract_type_name) override;
301+
CompilerType GetBaseClass(CompilerType class_ty);
301302

302303
CompilerType GetTypeFromMetadata(TypeSystemSwift &tss, Address address);
303304
/// Build the artificial type metadata variable name for \p swift_type.

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

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,29 @@ llvm::Expected<CompilerType> SwiftLanguageRuntime::GetChildCompilerTypeAtIndex(
16901690
return child_type;
16911691
}
16921692

1693+
CompilerType SwiftLanguageRuntime::GetBaseClass(CompilerType class_ty) {
1694+
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
1695+
if (!reflection_ctx)
1696+
return {};
1697+
auto ts_sp = class_ty.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
1698+
if (!ts_sp)
1699+
return {};
1700+
auto tr_ts = ts_sp->GetTypeSystemSwiftTypeRef();
1701+
if (!tr_ts)
1702+
return {};
1703+
auto type_ref_or_err =
1704+
reflection_ctx->GetTypeRef(class_ty.GetMangledTypeName().GetStringRef(),
1705+
tr_ts->GetDescriptorFinder());
1706+
if (!type_ref_or_err) {
1707+
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions | LLDBLog::Types),
1708+
type_ref_or_err.takeError(), "{0}");
1709+
return {};
1710+
}
1711+
auto *super_tr = reflection_ctx->LookupSuperclass(
1712+
*type_ref_or_err, tr_ts->GetDescriptorFinder());
1713+
return GetTypeFromTypeRef(*tr_ts, super_tr);
1714+
}
1715+
16931716
bool SwiftLanguageRuntime::ForEachSuperClassType(
16941717
ValueObject &instance, std::function<bool(SuperClassType)> fn) {
16951718
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
@@ -2082,38 +2105,22 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
20822105
instance_ptr = FixupAddress(instance_ptr, class_type, error);
20832106
if (!error.Success())
20842107
return false;
2108+
address.SetRawAddress(instance_ptr);
20852109

2086-
auto tss = class_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
2087-
if (!tss) {
2088-
// This could be an Objective-C type implemented in Swift. Get the
2089-
// Swift typesystem.
2090-
if (auto module_sp = in_value.GetModule()) {
2091-
auto type_system_or_err =
2092-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2093-
if (!type_system_or_err) {
2094-
llvm::consumeError(type_system_or_err.takeError());
2095-
return false;
2096-
}
2097-
auto ts_sp = *type_system_or_err;
2098-
tss =
2099-
llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
2100-
} else if (auto target_sp = in_value.GetTargetSP()) {
2101-
auto type_system_or_err =
2102-
target_sp->GetScratchTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2103-
if (!type_system_or_err) {
2104-
llvm::consumeError(type_system_or_err.takeError());
2105-
return false;
2106-
}
2107-
auto ts_sp = *type_system_or_err;
2108-
tss =
2109-
llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
2110+
// We are going to use process information to resolve the type, so
2111+
// the result needs to be in the target's scratch context, not a
2112+
// long-lived per-module typesystem.
2113+
TypeSystemSwiftTypeRefSP ts;
2114+
if (auto target_sp = in_value.GetTargetSP()) {
2115+
auto type_system_or_err =
2116+
target_sp->GetScratchTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2117+
if (!type_system_or_err) {
2118+
llvm::consumeError(type_system_or_err.takeError());
2119+
return false;
21102120
}
2121+
auto ts_sp = *type_system_or_err;
2122+
ts = llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
21112123
}
2112-
if (!tss)
2113-
return false;
2114-
2115-
address.SetRawAddress(instance_ptr);
2116-
auto ts = tss->GetTypeSystemSwiftTypeRef();
21172124
if (!ts)
21182125
return false;
21192126

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5221,20 +5221,32 @@ TypeSystemSwiftTypeRef::GetFullyUnqualifiedType(opaque_compiler_type_t type) {
52215221
}
52225222
uint32_t
52235223
TypeSystemSwiftTypeRef::GetNumDirectBaseClasses(opaque_compiler_type_t type) {
5224-
// We forward the call to SwiftASTContext because an implementation of
5225-
// this function would require it to have an execution context being passed
5226-
// in. Given the purpose of TypeSystemSwiftTypeRef, it's unlikely this
5227-
// function will be called much.
5228-
FORWARD_TO_EXPRAST_ONLY(GetNumDirectBaseClasses, (ReconstructType(type)), {});
5224+
auto impl = [&]() -> uint32_t {
5225+
CompilerType class_ty(weak_from_this(), type);
5226+
if (auto target_sp = GetTargetWP().lock())
5227+
if (auto *runtime = SwiftLanguageRuntime::Get(target_sp->GetProcessSP()))
5228+
if (runtime->GetBaseClass(class_ty))
5229+
return 1;
5230+
return 0;
5231+
};
5232+
5233+
VALIDATE_AND_RETURN(impl, GetNumDirectBaseClasses, type, g_no_exe_ctx,
5234+
(ReconstructType(type)));
52295235
}
52305236
CompilerType TypeSystemSwiftTypeRef::GetDirectBaseClassAtIndex(
52315237
opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5232-
// We forward the call to SwiftASTContext because an implementation of
5233-
// this function would require it to have an execution context being passed
5234-
// in. Given the purpose of TypeSystemSwiftTypeRef, it's unlikely this
5235-
// function will be called much.
5236-
FORWARD_TO_EXPRAST_ONLY(GetDirectBaseClassAtIndex,
5237-
(ReconstructType(type), idx, bit_offset_ptr), {});
5238+
auto impl = [&]() {
5239+
if (idx != 0)
5240+
return CompilerType();
5241+
CompilerType class_ty(weak_from_this(), type);
5242+
if (auto target_sp = GetTargetWP().lock())
5243+
if (auto *runtime = SwiftLanguageRuntime::Get(target_sp->GetProcessSP()))
5244+
return runtime->GetBaseClass(class_ty);
5245+
return CompilerType();
5246+
};
5247+
5248+
VALIDATE_AND_RETURN(impl, GetDirectBaseClassAtIndex, type, g_no_exe_ctx,
5249+
(ReconstructType(type), idx, nullptr));
52385250
}
52395251
bool TypeSystemSwiftTypeRef::IsReferenceType(opaque_compiler_type_t type,
52405252
CompilerType *pointee_type,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SWIFT_SOURCES := main.swift
2+
include Makefile.rules
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
class TestSwiftNSClassBaseClass(TestBase):
7+
8+
NO_DEBUG_INFO_TESTCASE = True
9+
@swiftTest
10+
@skipUnlessDarwin
11+
def test(self):
12+
self.build()
13+
lldbutil.run_to_source_breakpoint(self, "break here",
14+
lldb.SBFileSpec('main.swift'))
15+
c = self.frame().FindVariable("c")
16+
c_type = c.GetType()
17+
self.assertIn("C", c_type.GetName())
18+
self.assertEqual(c_type.GetNumberOfDirectBaseClasses(), 1)
19+
nsobject = c_type.GetDirectBaseClassAtIndex(0)
20+
self.assertIn("NSObject", nsobject.GetName())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Foundation
2+
3+
@objc class C : NSObject {}
4+
5+
func main() {
6+
var c = C()
7+
print("break here \(c)")
8+
}
9+
10+
main()

lldb/test/API/lang/swift/clangimporter/protocol_composition/TestSwiftClangImporterProtocolComposition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test(self):
1010
"""Test that protocol composition types can be resolved
1111
through the Swift language runtime"""
1212
self.build()
13+
self.expect('settings set symbols.swift-validate-typesystem false')
1314
lldbutil.run_to_source_breakpoint(self, 'break here',
1415
lldb.SBFileSpec('main.swift'))
1516
obj = self.frame().FindVariable("obj", lldb.eDynamicDontRunTarget)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SWIFT_SOURCES := main.swift
2+
include Makefile.rules
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
class TestSwiftClassBaseClass(TestBase):
7+
8+
NO_DEBUG_INFO_TESTCASE = True
9+
@swiftTest
10+
def test(self):
11+
self.build()
12+
lldbutil.run_to_source_breakpoint(self, "break here",
13+
lldb.SBFileSpec('main.swift'))
14+
c = self.frame().FindVariable("c")
15+
c1 = c.GetType()
16+
self.assertIn("C1", c1.GetName())
17+
self.assertEqual(c1.GetNumberOfDirectBaseClasses(), 1)
18+
c0 = c1.GetDirectBaseClassAtIndex(0)
19+
self.assertIn("C0", c0.GetName())
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class C0 {}
2+
class C1 : C0 {}
3+
4+
func main() {
5+
var c = C1()
6+
print("break here \(c)")
7+
}
8+
9+
main()

0 commit comments

Comments
 (0)