Skip to content

Make @objcImpl work with @_cdecl #69468

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 10 commits into from
Dec 14, 2023
Merged

Conversation

beccadax
Copy link
Contributor

@beccadax beccadax commented Oct 27, 2023

Swift can now emit C functions which match a global function declaration in a C header. That is, you can apply @_objcImplementation to a @_cdecl function and it will match it to a global C function in a header.

This doesn't yet have all the functionality I'd like—I'm hoping to cover computed properties and at least some import-as-member functions too. But it already has enough functionality to be useful.

Fixes rdar://103845088.

@beccadax
Copy link
Contributor Author

beccadax commented Dec 7, 2023

@swift-ci please test

@beccadax beccadax marked this pull request as ready for review December 8, 2023 02:02
@beccadax
Copy link
Contributor Author

beccadax commented Dec 8, 2023

@swift-ci please test

1 similar comment
@beccadax
Copy link
Contributor Author

beccadax commented Dec 8, 2023

@swift-ci please test

@beccadax
Copy link
Contributor Author

beccadax commented Dec 8, 2023

@swift-ci please test windows platform

@beccadax
Copy link
Contributor Author

beccadax commented Dec 8, 2023

Note that I got a pass on Windows yesterday with the exact same code: https://ci-external.swift.org/job/swift-PR-windows/19506/

@beccadax beccadax requested a review from tshortli December 8, 2023 19:28
@beccadax
Copy link
Contributor Author

beccadax commented Dec 9, 2023

@swift-ci please test windows platform

@beccadax
Copy link
Contributor Author

beccadax commented Dec 9, 2023

@swift-ci please test

@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-import-objc-header %S/Inputs/objc_implementation.h -D TOP_LEVEL_CODE -swift-version 5) %s | %FileCheck %s
// RUN: %target-run-simple-swift(-import-objc-header %S/Inputs/objc_implementation.h -D TOP_LEVEL_CODE -swift-version 5) %s -save-temps | %FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is leaving -save-temps intentional here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope—I've now removed it. Thanks!

No real diagnostics yet, but we’re emitting mostly correct code.
…at least for declarations in the current module. We continue to pretend that inaccessible declarations in other modules do not exist.
It also reads the C name out of imported C functions, which will be convenient for @cdecl @implementation.
This commit diagnoses cdecl implementations with no matching imported declaration, and also runs them through the ObjCImplementationChecker. Actually testing that the ObjCImplementationChecker diagnoses various failure conditions correctly will be added in a subsequent commit.
We eventually want to be able to implement functions imported as globals or members in Swift, but we’re not there yet. Add some basic tests to make sure we get reasonable diagnostics for them.
@beccadax
Copy link
Contributor Author

@swift-ci please test and merge

1 similar comment
@beccadax
Copy link
Contributor Author

@swift-ci please test and merge

@beccadax
Copy link
Contributor Author

@swift-ci please smoke test and merge

@beccadax
Copy link
Contributor Author

@swift-ci please smoke test

@beccadax beccadax enabled auto-merge December 14, 2023 05:11
@beccadax beccadax merged commit e967219 into swiftlang:main Dec 14, 2023
return cdeclAttr->Name;
}

return "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also @extern(c, "name"), IIRC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like @_extern requires the function to not have a body, so it wouldn't make sense to use it with @implementation (which provides a body). Is that changing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants