-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Support for printing C++ foreign references #81832
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
+242
−4
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
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,49 @@ | ||
#ifndef TEST_INTEROP_CXX_METADATA_INPUTS_MIRROR_H | ||
#define TEST_INTEROP_CXX_METADATA_INPUTS_MIRROR_H | ||
|
||
struct EmptyStruct {}; | ||
|
||
struct BaseStruct { | ||
private: | ||
int priv; | ||
|
||
public: | ||
int publ; | ||
|
||
protected: | ||
int prot; | ||
|
||
public: | ||
BaseStruct(int i1, int i2, int i3) : priv(i1), publ(i2), prot(i3) {} | ||
}; | ||
|
||
class EmptyClass {}; | ||
|
||
struct OuterStruct { | ||
private: | ||
BaseStruct privStruct; | ||
|
||
public: | ||
BaseStruct publStruct; | ||
|
||
OuterStruct() : privStruct(1, 2, 3), publStruct(4, 5, 6) {} | ||
}; | ||
|
||
struct FRTStruct { | ||
private: | ||
int priv = 1; | ||
|
||
public: | ||
int publ = 2; | ||
} __attribute__((swift_attr("import_reference"))) | ||
__attribute__((swift_attr("retain:retain"))) | ||
__attribute__((swift_attr("release:release"))); | ||
|
||
void retain(FRTStruct *v) {}; | ||
void release(FRTStruct *v) {}; | ||
|
||
class FRTImmortalClass {} __attribute__((swift_attr("import_reference"))) | ||
__attribute__((swift_attr("retain:immortal"))) | ||
__attribute__((swift_attr("release:immortal"))); | ||
|
||
#endif |
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,97 @@ | ||
// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -I %S/Inputs) | ||
|
||
// REQUIRES: executable_test | ||
// Metadata for foreign reference types is not supported on Windows. | ||
// UNSUPPORTED: OS=windows-msvc | ||
|
||
import StdlibUnittest | ||
import Mirror | ||
|
||
var MirrorTestSuite = TestSuite("Mirrors") | ||
|
||
MirrorTestSuite.test("EmptyCxxStruct") { | ||
let s = EmptyStruct() | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.`struct`, m.displayStyle) | ||
expectTrue(m.subjectType == EmptyStruct.self) | ||
expectEqual(0, m.children.count) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
expectEqual("- __C.EmptyStruct\n", output) | ||
} | ||
|
||
MirrorTestSuite.test("EmptyCxxClass") { | ||
let s = EmptyClass() | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.`struct`, m.displayStyle) | ||
expectTrue(m.subjectType == EmptyClass.self) | ||
expectEqual(0, m.children.count) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
expectEqual("- __C.EmptyClass\n", output) | ||
} | ||
|
||
MirrorTestSuite.test("CxxStructWithFields") { | ||
let s = BaseStruct(1, 2, 3) | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.`struct`, m.displayStyle) | ||
expectTrue(m.subjectType == BaseStruct.self) | ||
expectEqual(1, m.children.count) | ||
|
||
expectEqual("publ", m.children.first!.label) | ||
expectEqual(2, m.children.first!.value as? Int32) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
let expected = | ||
"▿ __C.BaseStruct\n" + | ||
" - publ: 2\n" | ||
expectEqual(expected, output) | ||
} | ||
|
||
MirrorTestSuite.test("CxxStructWithStructsAsFields") { | ||
let s = OuterStruct() | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.`struct`, m.displayStyle) | ||
expectTrue(m.subjectType == OuterStruct.self) | ||
expectEqual(1, m.children.count) | ||
expectEqual("publStruct", m.children.first!.label) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
let expected = | ||
"▿ __C.OuterStruct\n" + | ||
" ▿ publStruct: __C.BaseStruct\n" + | ||
" - publ: 5\n" | ||
expectEqual(expected, output) | ||
} | ||
|
||
if #available(SwiftStdlib 6.2, *) { | ||
MirrorTestSuite.test("CxxFRTStruct") { | ||
let s = FRTStruct() | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.foreignReference, m.displayStyle) | ||
expectTrue(m.subjectType == FRTStruct.self) | ||
expectEqual(0, m.children.count) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
expectEqual("- __C.FRTStruct\n", output) | ||
} | ||
|
||
MirrorTestSuite.test("CxxFRTImmortalClass") { | ||
let s = FRTImmortalClass() | ||
let m = Mirror(reflecting: s) | ||
expectEqual(.foreignReference, m.displayStyle) | ||
expectTrue(m.subjectType == FRTImmortalClass.self) | ||
expectEqual(0, m.children.count) | ||
|
||
var output = "" | ||
dump(s, to: &output) | ||
expectEqual("- __C.FRTImmortalClass\n", output) | ||
} | ||
} | ||
|
||
runAllTests() |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to print the (public) fields similar to structs in the future? I think it is fine to not do it in this PR but we might want to have a rdar for it if this is planned and possible to achieve?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, we don't emit field metadata for FRT, so we don't have any field information at runtime:
swift/lib/IRGen/GenMeta.cpp
Lines 2006 to 2009 in 68fb206
swift/lib/IRGen/GenMeta.cpp
Lines 2120 to 2125 in 68fb206
I'm not sure if this is on purpose or it's simply not yet implemented, but I agree it would be useful to figure this out and, potentially, print more information in the future