-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CxxInterop] Expose C++ static members as Swift static properties #31070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,13 @@ | ||
int counter = 12; | ||
|
||
int getCounterFromCxx() { | ||
return counter; | ||
} | ||
int getCounterFromCxx() { return counter; } | ||
|
||
void setCounterFromCxx(int c) { | ||
counter = c; | ||
} | ||
void setCounterFromCxx(int c) { counter = c; } | ||
|
||
namespace Namespaced { | ||
int counter = 12; | ||
int counter = 12; | ||
|
||
int getCounterFromCxx() { | ||
return counter; | ||
} | ||
int getCounterFromCxx() { return counter; } | ||
|
||
void setCounterFromCxx(int c) { | ||
counter = c; | ||
} | ||
} | ||
void setCounterFromCxx(int c) { counter = c; } | ||
} // namespace Namespaced |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "inline-static-member-var.h" | ||
|
||
int *WithInlineStaticMember::getStaticMemberAddress() { return &staticMember; } | ||
|
||
int WithInlineStaticMember::getStaticMemberFromCxx() { return staticMember; } | ||
|
||
void WithInlineStaticMember::setStaticMemberFromCxx(int newVal) { | ||
staticMember = newVal; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
inline static int init() { return 42; } | ||
|
||
class WithInlineStaticMember { | ||
public: | ||
inline static int staticMember = 12; | ||
//TODO needs C++ stdlib symbols, fix after apple/swift#30914 is merged. | ||
// inline static int staticMemberInitializedAtRuntime = init(); | ||
|
||
static int getStaticMemberFromCxx(); | ||
static int *getStaticMemberAddress(); | ||
static void setStaticMemberFromCxx(int); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module StaticLocalVar { | ||
header "static-local-var.h" | ||
} | ||
|
||
module StaticMemberVar { | ||
header "static-member-var.h" | ||
} | ||
|
||
module InlineStaticMemberVar { | ||
header "inline-static-member-var.h" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "static-local-var.h" | ||
|
||
int counterWrapper() { | ||
return counter(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
int counterWrapper(); | ||
|
||
inline int counter() { | ||
static int a = 0; | ||
return a++; | ||
} | ||
hlopko marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "static-member-var.h" | ||
|
||
int WithStaticMember::staticMember = 42; | ||
|
||
int *WithStaticMember::getStaticMemberAddress() { return &staticMember; } | ||
|
||
int WithStaticMember::getStaticMemberFromCxx() { return staticMember; } | ||
|
||
void WithStaticMember::setStaticMemberFromCxx(int newVal) { | ||
staticMember = newVal; | ||
} | ||
|
||
int WithIncompleteStaticMember::arrayMember[3] = {18, 19, 20}; | ||
|
||
WithIncompleteStaticMember WithIncompleteStaticMember::selfMember = | ||
WithIncompleteStaticMember(); | ||
|
||
WithIncompleteStaticMember * | ||
WithIncompleteStaticMember::getStaticMemberFromCxx() { | ||
return &selfMember; | ||
} | ||
|
||
void WithIncompleteStaticMember::setStaticMemberFromCxx( | ||
WithIncompleteStaticMember newVal) { | ||
selfMember = newVal; | ||
} | ||
|
||
const int WithConstStaticMember::defined; | ||
const int WithConstStaticMember::definedOutOfLine = 96; | ||
|
||
int ClassA::notUniqueName = 144; | ||
int ClassB::notUniqueName = 169; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class WithStaticMember { | ||
public: | ||
static int staticMember; | ||
static int *getStaticMemberAddress(); | ||
static int getStaticMemberFromCxx(); | ||
static void setStaticMemberFromCxx(int); | ||
}; | ||
|
||
class WithIncompleteStaticMember { | ||
public: | ||
static int arrayMember[]; | ||
static WithIncompleteStaticMember selfMember; | ||
int id = 3; | ||
|
||
static WithIncompleteStaticMember *getStaticMemberFromCxx(); | ||
static void setStaticMemberFromCxx(WithIncompleteStaticMember); | ||
}; | ||
|
||
class WithConstStaticMember { | ||
public: | ||
const static int notDefined = 24; | ||
const static int defined = 48; | ||
const static int definedOutOfLine; | ||
}; | ||
|
||
class WithConstexprStaticMember { | ||
public: | ||
constexpr static int definedInline = 139; | ||
}; | ||
|
||
class ClassA { | ||
public: | ||
static int notUniqueName; | ||
}; | ||
|
||
class ClassB { | ||
public: | ||
static int notUniqueName; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-clang -c %S/Inputs/static-member-var.cc -I %S/Inputs -o %t/static-member-var.o -std=c++17 | ||
// RUN: %target-build-swift %s -I %S/Inputs -o %t/statics %t/static-member-var.o -Xfrontend -enable-cxx-interop -Xcc -std=c++17 | ||
// RUN: %target-codesign %t/statics | ||
// RUN: %target-run %t/statics | ||
// | ||
// REQUIRES: executable_test | ||
|
||
import StaticMemberVar | ||
import StdlibUnittest | ||
|
||
var ConstexprStaticMemberVarTestSuite = TestSuite("ConstexprStaticMemberVarTestSuite") | ||
|
||
ConstexprStaticMemberVarTestSuite.test("constexpr-static-member") { | ||
expectEqual(139, WithConstexprStaticMember.definedInline) | ||
} | ||
|
||
runAllTests() |
31 changes: 31 additions & 0 deletions
31
test/Interop/Cxx/static/inline-static-member-var-irgen.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: %target-swift-emit-ir -I %S/Inputs -enable-cxx-interop %s | %FileCheck %s | ||
|
||
import InlineStaticMemberVar | ||
|
||
public func readStaticMember() -> CInt { | ||
return WithInlineStaticMember.staticMember | ||
} | ||
|
||
// CHECK: @{{_ZN22WithInlineStaticMember12staticMemberE|"\?staticMember@WithInlineStaticMember@@2HA"}} = linkonce_odr {{(dso_local )?}}global i32 12, {{(comdat, )?}}align 4 | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main16readStaticMembers5Int32VyF"() | ||
// CHECK: [[VALUE:%.*]] = load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{_ZN22WithInlineStaticMember12staticMemberE|"\?staticMember@WithInlineStaticMember@@2HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
// CHECK: ret i32 [[VALUE]] | ||
|
||
public func writeStaticMember(_ c: CInt) { | ||
WithInlineStaticMember.staticMember = c | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main17writeStaticMemberyys5Int32VF"(i32 %0) | ||
// CHECK: store i32 %0, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{_ZN22WithInlineStaticMember12staticMemberE|"\?staticMember@WithInlineStaticMember@@2HA"}} to %Ts5Int32V*), i32 0, i32 0), align 4 | ||
|
||
func modifyInout(_ c: inout CInt) { | ||
c = 42 | ||
} | ||
|
||
public func passingVarAsInout() { | ||
modifyInout(&WithInlineStaticMember.staticMember) | ||
} | ||
|
||
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main17passingVarAsInoutyyF"() | ||
// CHECK: call swiftcc void @"$s4main11modifyInoutyys5Int32VzF"(%Ts5Int32V* nocapture dereferenceable(4) bitcast (i32* @{{_ZN22WithInlineStaticMember12staticMemberE|"\?staticMember@WithInlineStaticMember@@2HA"}} to %Ts5Int32V*)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.