Skip to content

Commit 9ca5492

Browse files
authored
Merge pull request #61299 from hyp/eng/static-property-bridge
[interop][SwiftToCxx] add support for bridging static properties
2 parents a1fe500 + 6f9b683 commit 9ca5492

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,6 @@ class DeclAndTypePrinter::Implementation
914914
getForeignResultType(AFD, methodTy, asyncConvention, errorConvention);
915915

916916
if (outputLang == OutputLanguageMode::Cxx) {
917-
// FIXME: Support static methods.
918-
if (isClassMethod)
919-
return;
920-
assert(!AFD->isStatic());
921917
auto *typeDeclContext = dyn_cast<NominalTypeDecl>(AFD->getParent());
922918
if (!typeDeclContext) {
923919
typeDeclContext =
@@ -941,11 +937,13 @@ class DeclAndTypePrinter::Implementation
941937
if (SD)
942938
declPrinter.printCxxSubscriptAccessorMethod(
943939
typeDeclContext, accessor, funcABI->getSignature(),
944-
funcABI->getSymbolName(), resultTy, /*isDefinition=*/false);
940+
funcABI->getSymbolName(), resultTy,
941+
/*isDefinition=*/false);
945942
else
946943
declPrinter.printCxxPropertyAccessorMethod(
947944
typeDeclContext, accessor, funcABI->getSignature(),
948945
funcABI->getSymbolName(), resultTy,
946+
/*isStatic=*/isClassMethod,
949947
/*isDefinition=*/false);
950948
} else {
951949
declPrinter.printCxxMethod(typeDeclContext, AFD,
@@ -968,6 +966,7 @@ class DeclAndTypePrinter::Implementation
968966
defPrinter.printCxxPropertyAccessorMethod(
969967
typeDeclContext, accessor, funcABI->getSignature(),
970968
funcABI->getSymbolName(), resultTy,
969+
/*isStatic=*/isClassMethod,
971970
/*isDefinition=*/true);
972971
} else {
973972
defPrinter.printCxxMethod(typeDeclContext, AFD, funcABI->getSignature(),
@@ -1596,6 +1595,9 @@ class DeclAndTypePrinter::Implementation
15961595
return;
15971596
}
15981597

1598+
// FIXME: Support static methods.
1599+
if (FD->getDeclContext()->isTypeContext() && FD->isStatic())
1600+
return;
15991601
if (FD->getDeclContext()->isTypeContext())
16001602
return printAbstractFunctionAsMethod(FD, FD->isStatic());
16011603

@@ -1688,13 +1690,10 @@ class DeclAndTypePrinter::Implementation
16881690
if (outputLang == OutputLanguageMode::Cxx) {
16891691
// FIXME: Documentation.
16901692
// FIXME: availability.
1691-
// FIXME: support static properties.
1692-
if (VD->isStatic())
1693-
return;
16941693
auto *getter = VD->getOpaqueAccessor(AccessorKind::Get);
1695-
printAbstractFunctionAsMethod(getter, /*isStatic=*/false);
1694+
printAbstractFunctionAsMethod(getter, /*isStatic=*/VD->isStatic());
16961695
if (auto *setter = VD->getOpaqueAccessor(AccessorKind::Set))
1697-
printAbstractFunctionAsMethod(setter, /*isStatic=*/false);
1696+
printAbstractFunctionAsMethod(setter, /*isStatic=*/VD->isStatic());
16981697
return;
16991698
}
17001699

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,15 +1291,17 @@ static std::string remapPropertyName(const AccessorDecl *accessor,
12911291
void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
12921292
const NominalTypeDecl *typeDeclContext, const AccessorDecl *accessor,
12931293
const LoweredFunctionSignature &signature, StringRef swiftSymbolName,
1294-
Type resultTy, bool isDefinition) {
1294+
Type resultTy, bool isStatic, bool isDefinition) {
12951295
assert(accessor->isSetter() || accessor->getParameters()->size() == 0);
12961296
os << " ";
12971297

12981298
FunctionSignatureModifiers modifiers;
12991299
if (isDefinition)
13001300
modifiers.qualifierContext = typeDeclContext;
1301+
modifiers.isStatic = isStatic && !isDefinition;
13011302
modifiers.isInline = true;
1302-
modifiers.isConst = accessor->isGetter() && !isa<ClassDecl>(typeDeclContext);
1303+
modifiers.isConst =
1304+
!isStatic && accessor->isGetter() && !isa<ClassDecl>(typeDeclContext);
13031305
auto result = printFunctionSignature(
13041306
accessor, signature, remapPropertyName(accessor, resultTy), resultTy,
13051307
FunctionSignatureKind::CxxInlineThunk, modifiers);

lib/PrintAsClang/PrintClangFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class DeclAndTypeClangFunctionPrinter {
122122
const AccessorDecl *accessor,
123123
const LoweredFunctionSignature &signature,
124124
StringRef swiftSymbolName, Type resultTy,
125-
bool isDefinition);
125+
bool isStatic, bool isDefinition);
126126

127127
/// Print the C++ subscript method.
128128
void printCxxSubscriptAccessorMethod(

test/Interop/SwiftToCxx/properties/getter-in-cxx-execution.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,12 @@ int main() {
5959
assert(propsInClass.getComputedInt() == -1235);
6060
auto smallStructFromClass = propsInClass.getSmallStruct();
6161
assert(smallStructFromClass.getX() == 1234);
62+
63+
{
64+
auto x = LargeStruct::getStaticX();
65+
assert(x == -402);
66+
auto smallStruct = LargeStruct::getStaticSmallStruct();
67+
assert(smallStruct.getX() == 789);
68+
}
6269
return 0;
6370
}

test/Interop/SwiftToCxx/properties/getter-in-cxx.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public struct LargeStruct {
2424
public var firstSmallStruct: FirstSmallStruct {
2525
return FirstSmallStruct(x: 65)
2626
}
27+
28+
static public var staticX: Int {
29+
return -402
30+
}
31+
32+
static public var staticSmallStruct: FirstSmallStruct {
33+
return FirstSmallStruct(x: 789)
34+
}
2735
}
2836

2937
// CHECK: class LargeStruct final {
@@ -37,6 +45,8 @@ public struct LargeStruct {
3745
// CHECK-NEXT: inline swift::Int getX6() const;
3846
// CHECK-NEXT: inline LargeStruct getAnotherLargeStruct() const;
3947
// CHECK-NEXT: inline FirstSmallStruct getFirstSmallStruct() const;
48+
// CHECK-NEXT: static inline swift::Int getStaticX();
49+
// CHECK-NEXT: static inline FirstSmallStruct getStaticSmallStruct();
4050
// CHECK-NEXT: private:
4151

4252
public final class PropertiesInClass {
@@ -153,6 +163,14 @@ public func createStructWithRefCountStoredProp() -> StructWithRefCountStoredProp
153163
// CHECK-NEXT: _impl::swift_interop_returnDirect_Properties_uint32_t_0_4(result, _impl::$s10Properties11LargeStructV010firstSmallC0AA05FirsteC0Vvg(_getOpaquePointer()));
154164
// CHECK-NEXT: });
155165
// CHECK-NEXT: }
166+
// CHECK-NEXT: inline swift::Int LargeStruct::getStaticX() {
167+
// CHECK-NEXT: return _impl::$s10Properties11LargeStructV7staticXSivgZ();
168+
// CHECK-NEXT: }
169+
// CHECK-NEXT: inline FirstSmallStruct LargeStruct::getStaticSmallStruct() {
170+
// CHECK-NEXT: return _impl::_impl_FirstSmallStruct::returnNewValue([&](char * _Nonnull result) {
171+
// CHECK-NEXT: _impl::swift_interop_returnDirect_Properties_uint32_t_0_4(result, _impl::$s10Properties11LargeStructV011staticSmallC0AA05FirsteC0VvgZ());
172+
// CHECK-NEXT: });
173+
// CHECK-NEXT: }
156174

157175
// CHECK: inline int32_t PropertiesInClass::getStoredInt() {
158176
// CHECK-NEXT: return _impl::$s10Properties0A7InClassC9storedInts5Int32Vvg(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));

test/Interop/SwiftToCxx/properties/setter-in-cxx-execution.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,13 @@ int main() {
5555
propsInClass.setComputedInt(-11);
5656
assert(propsInClass.getComputedInt() == -11);
5757
assert(propsInClass.getStoredInt() == -13);
58+
59+
{
60+
auto x = LargeStruct::getStaticX();
61+
assert(x == 0);
62+
LargeStruct::setStaticX(13);
63+
x = LargeStruct::getStaticX();
64+
assert(x == 13);
65+
}
5866
return 0;
5967
}

test/Interop/SwiftToCxx/properties/setter-in-cxx.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public struct FirstSmallStruct {
1717

1818
public struct LargeStruct {
1919
public var x1, x2, x3, x4, x5, x6: Int
20+
21+
private static var _statX: Int = 0
22+
public static var staticX: Int {
23+
get {
24+
return _statX
25+
}
26+
set {
27+
_statX = newValue
28+
}
29+
}
2030
}
2131

2232
// CHECK: class LargeStruct final {
@@ -34,6 +44,8 @@ public struct LargeStruct {
3444
// CHECK-NEXT: inline void setX5(swift::Int value);
3545
// CHECK-NEXT: inline swift::Int getX6() const;
3646
// CHECK-NEXT: inline void setX6(swift::Int value);
47+
// CHECK-NEXT: static inline swift::Int getStaticX();
48+
// CHECK-NEXT: static inline void setStaticX(swift::Int newValue);
3749
// CHECK-NEXT: private:
3850

3951
public struct LargeStructWithProps {
@@ -128,6 +140,13 @@ public func createFirstSmallStruct(_ x: UInt32) -> FirstSmallStruct {
128140
// CHECK-NEXT: return _impl::$s10Properties11LargeStructV2x1Sivs(value, _getOpaquePointer());
129141
// CHECK-NEXT: }
130142

143+
// CHECK: inline swift::Int LargeStruct::getStaticX() {
144+
// CHECK-NEXT: return _impl::$s10Properties11LargeStructV7staticXSivgZ();
145+
// CHECK-NEXT: }
146+
// CHECK-NEXT: inline void LargeStruct::setStaticX(swift::Int newValue) {
147+
// CHECK-NEXT: return _impl::$s10Properties11LargeStructV7staticXSivsZ(newValue);
148+
// CHECK-NEXT: }
149+
131150
// CHECK: inline LargeStruct LargeStructWithProps::getStoredLargeStruct() const {
132151
// CHECK-NEXT: return _impl::_impl_LargeStruct::returnNewValue([&](char * _Nonnull result) {
133152
// CHECK-NEXT: _impl::$s10Properties20LargeStructWithPropsV06storedbC0AA0bC0Vvg(result, _getOpaquePointer());

0 commit comments

Comments
 (0)