Skip to content

Commit cf8f61d

Browse files
authored
Merge pull request #60905 from hyp/eng/generic-struct-props
[interop][SwiftToCxx] correctly invoke property accessors in generic …
2 parents e6063db + d9f4c07 commit cf8f61d

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ class DeclAndTypePrinter::Implementation
901901
if (auto *accessor = dyn_cast<AccessorDecl>(AFD)) {
902902
declPrinter.printCxxPropertyAccessorMethod(
903903
typeDeclContext, accessor, funcABI->getSymbolName(), resultTy,
904-
/*isDefinition=*/false);
904+
/*isDefinition=*/false, funcABI->additionalParams);
905905
} else {
906906
declPrinter.printCxxMethod(
907907
typeDeclContext, AFD, funcABI->getSymbolName(), resultTy,
@@ -917,7 +917,7 @@ class DeclAndTypePrinter::Implementation
917917

918918
defPrinter.printCxxPropertyAccessorMethod(
919919
typeDeclContext, accessor, funcABI->getSymbolName(), resultTy,
920-
/*isDefinition=*/true);
920+
/*isDefinition=*/true, funcABI->additionalParams);
921921
} else {
922922
defPrinter.printCxxMethod(
923923
typeDeclContext, AFD, funcABI->getSymbolName(), resultTy,

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ static std::string remapPropertyName(const AccessorDecl *accessor,
874874

875875
void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
876876
const NominalTypeDecl *typeDeclContext, const AccessorDecl *accessor,
877-
StringRef swiftSymbolName, Type resultTy, bool isDefinition) {
877+
StringRef swiftSymbolName, Type resultTy, bool isDefinition,
878+
ArrayRef<AdditionalParam> additionalParams) {
878879
assert(accessor->isSetter() || accessor->getParameters()->size() == 0);
879880
os << " ";
880881

@@ -894,10 +895,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
894895
os << " {\n";
895896
// FIXME: should it be objTy for resultTy?
896897
printCxxThunkBody(swiftSymbolName, accessor->getModuleContext(), resultTy,
897-
accessor->getParameters(),
898-
{AdditionalParam{AdditionalParam::Role::Self,
899-
typeDeclContext->getDeclaredType(),
900-
/*isIndirect=*/accessor->isSetter()}});
898+
accessor->getParameters(), additionalParams);
901899
os << " }\n";
902900
}
903901

lib/PrintAsClang/PrintClangFunction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ class DeclAndTypeClangFunctionPrinter {
131131
ArrayRef<AdditionalParam> additionalParams);
132132

133133
/// Print the C++ getter/setter method signature.
134-
void printCxxPropertyAccessorMethod(const NominalTypeDecl *typeDeclContext,
135-
const AccessorDecl *accessor,
136-
StringRef swiftSymbolName, Type resultTy,
137-
bool isDefinition);
134+
void printCxxPropertyAccessorMethod(
135+
const NominalTypeDecl *typeDeclContext, const AccessorDecl *accessor,
136+
StringRef swiftSymbolName, Type resultTy, bool isDefinition,
137+
ArrayRef<AdditionalParam> additionalParams);
138138

139139
/// Print Swift type as C/C++ type, as the return type of a C/C++ function.
140140
ClangRepresentation

test/Interop/SwiftToCxx/generics/generic-struct-execution.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ int main() {
3737
takeGenericPair(xprime);
3838
// CHECK-NEXT: GenericPair<Int32, Int32>(x: -995, y: 11)
3939
// CHECK-NEXT: GenericPair<Int32, Int32>(x: 11, y: -995)
40+
assert(x.getY() == 11);
41+
x.setY(561);
42+
takeGenericPair(x);
43+
// CHECK-NEXT: GenericPair<Int32, Int32>(x: -995, y: 561)
44+
assert(x.getComputedProp() == 42);
45+
assert(x.getComputedVar() == -995);
46+
x.setComputedVar(-123456);
47+
assert(x.getComputedVar() == -123456);
48+
assert(x.getY() == 561);
49+
takeGenericPair(x);
50+
// CHECK-NEXT: GenericPair<T, T2>::computeVar::get
51+
// CHECK-NEXT: GenericPair<T, T2>::computeVar::set
52+
// CHECK-NEXT: GenericPair<T, T2>::computeVar::get
53+
// CHECK-NEXT: GenericPair<Int32, Int32>(x: -123456, y: 561)
4054
}
4155

4256
{

test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@frozen
1616
public struct GenericPair<T, T2> {
1717
var x: T
18-
var y: T2
18+
public var y: T2
1919

2020
public func method() {
2121
let copyOfSelf = self
@@ -26,6 +26,20 @@ public struct GenericPair<T, T2> {
2626
x = other.y
2727
y = other.x
2828
}
29+
30+
public var computedProp: Int {
31+
return 42
32+
}
33+
34+
public var computedVar: T {
35+
get {
36+
print("GenericPair<T, T2>::computeVar::get")
37+
return x
38+
} set {
39+
print("GenericPair<T, T2>::computeVar::set")
40+
x = newValue
41+
}
42+
}
2943
}
3044

3145
public func makeGenericPair<T, T1>(_ x: T, _ y: T1) -> GenericPair<T, T1> {
@@ -62,8 +76,13 @@ public func inoutConcretePair(_ x: UInt32, _ y: inout GenericPair<UInt32, UInt32
6276
y.x = x
6377
}
6478

65-
// CHECK: SWIFT_EXTERN void $s8Generics11GenericPairV6methodyyF(void * _Nonnull , SWIFT_CONTEXT const void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // method()
79+
// CHECK: SWIFT_EXTERN void $s8Generics11GenericPairV1yq_vg(SWIFT_INDIRECT_RESULT void * _Nonnull, void * _Nonnull , SWIFT_CONTEXT const void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // _
80+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics11GenericPairV1yq_vs(const void * _Nonnull value, void * _Nonnull , SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // _
81+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics11GenericPairV6methodyyF(void * _Nonnull , SWIFT_CONTEXT const void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // method()
6682
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics11GenericPairV14mutatingMethodyyACyq_xGF(const void * _Nonnull other, void * _Nonnull , SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // mutatingMethod(_:)
83+
// CHECK-NEXT: SWIFT_EXTERN ptrdiff_t $s8Generics11GenericPairV12computedPropSivg(void * _Nonnull , SWIFT_CONTEXT const void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // _
84+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics11GenericPairV11computedVarxvg(SWIFT_INDIRECT_RESULT void * _Nonnull, void * _Nonnull , SWIFT_CONTEXT const void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // _
85+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics11GenericPairV11computedVarxvs(const void * _Nonnull newValue, void * _Nonnull , SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // _
6786
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics17inoutConcretePairyys6UInt32V_AA07GenericD0VyA2DGztF(uint32_t x, char * _Nonnull y) SWIFT_NOEXCEPT SWIFT_CALL; // inoutConcretePair(_:_:)
6887
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics16inoutGenericPairyyAA0cD0Vyxq_Gz_xtr0_lF(void * _Nonnull x, const void * _Nonnull y, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // inoutGenericPair(_:_:)
6988
// CHECK-NEXT: // Stub struct to be used to pass/return values to/from Swift functions.
@@ -183,6 +202,28 @@ public func inoutConcretePair(_ x: UInt32, _ y: inout GenericPair<UInt32, UInt32
183202

184203
// CHECK: template<class T_0_0, class T_0_1>
185204
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
205+
// CHECK-NEXT: inline T_0_1 GenericPair<T_0_0, T_0_1>::getY() const {
206+
// CHECK-NEXT: if constexpr (std::is_base_of<::swift::_impl::RefCountedClass, T_0_1>::value) {
207+
// CHECK-NEXT: void *returnValue;
208+
// CHECK-NEXT: _impl::$s8Generics11GenericPairV1yq_vg(reinterpret_cast<void *>(&returnValue), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
209+
// CHECK-NEXT: return ::swift::_impl::implClassFor<T_0_1>::type::makeRetained(returnValue);
210+
// CHECK-NEXT: } else if constexpr (::swift::_impl::isValueType<T_0_1>) {
211+
// CHECK-NEXT: return ::swift::_impl::implClassFor<T_0_1>::type::returnNewValue([&](void * _Nonnull returnValue) {
212+
// CHECK-NEXT: _impl::$s8Generics11GenericPairV1yq_vg(returnValue, swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
213+
// CHECK-NEXT: });
214+
// CHECK-NEXT: } else {
215+
// CHECK-NEXT: T_0_1 returnValue;
216+
// CHECK-NEXT: _impl::$s8Generics11GenericPairV1yq_vg(reinterpret_cast<void *>(&returnValue), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
217+
// CHECK-NEXT: return returnValue;
218+
// CHECK-NEXT: }
219+
// CHECK-NEXT: }
220+
// CHECK-NEXT: template<class T_0_0, class T_0_1>
221+
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
222+
// CHECK-NEXT: inline void GenericPair<T_0_0, T_0_1>::setY(const T_0_1& value) {
223+
// CHECK-NEXT: return _impl::$s8Generics11GenericPairV1yq_vs(swift::_impl::getOpaquePointer(value), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
224+
// CHECK-NEXT: }
225+
// CHECK-NEXT: template<class T_0_0, class T_0_1>
226+
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
186227
// CHECK-NEXT: inline void GenericPair<T_0_0, T_0_1>::method() const {
187228
// CHECK-NEXT: return _impl::$s8Generics11GenericPairV6methodyyF(swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
188229
// CHECK-NEXT: }
@@ -191,3 +232,13 @@ public func inoutConcretePair(_ x: UInt32, _ y: inout GenericPair<UInt32, UInt32
191232
// CHECK-NEXT: inline void GenericPair<T_0_0, T_0_1>::mutatingMethod(const GenericPair<T_0_1, T_0_0>& other) {
192233
// CHECK-NEXT: return _impl::$s8Generics11GenericPairV14mutatingMethodyyACyq_xGF(_impl::_impl_GenericPair<T_0_1, T_0_0>::getOpaquePointer(other), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
193234
// CHECK-NEXT: }
235+
// CHECK-NEXT: template<class T_0_0, class T_0_1>
236+
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
237+
// CHECK-NEXT: inline swift::Int GenericPair<T_0_0, T_0_1>::getComputedProp() const {
238+
// CHECK-NEXT: return _impl::$s8Generics11GenericPairV12computedPropSivg(swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
239+
// CHECK-NEXT: }
240+
241+
// CHECK: inline T_0_0 GenericPair<T_0_0, T_0_1>::getComputedVar()
242+
// CHECK: _impl::$s8Generics11GenericPairV11computedVarxvg(reinterpret_cast<void *>(&returnValue), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());
243+
// CHECK: inline void GenericPair<T_0_0, T_0_1>::setComputedVar(const T_0_0& newValue) {
244+
// CHECK-NEXT: return _impl::$s8Generics11GenericPairV11computedVarxvs(swift::_impl::getOpaquePointer(newValue), swift::TypeMetadataTrait<GenericPair<T_0_0, T_0_1>>::getTypeMetadata(), _getOpaquePointer());

0 commit comments

Comments
 (0)