-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Introduce @extern(c)
to declare C function without Clang module
#69207
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
Introduce @extern(c)
to declare C function without Clang module
#69207
Conversation
@swift-ci Please smoke test |
6c57b11
to
4a023b4
Compare
@swift-ci Please smoke test |
@swift-ci Please test Apple Silicon |
ba2c8cc
to
13ba0ee
Compare
@swift-ci Please build toolchain |
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.
The Attr.h is shared with SwiftCompilerSources through C++ interop and C++ interop somehow crashes with libc++'s std::optional. So use legacy llvm::Optional for now.
…ible way To fix windows-x86_64 case, which doesn't have byval.
`@_extern(wasm)` has no limitation on the function interface, so the check should be applied only for `@_extern(c)`.
To avoid ambiguity at SIL level name since both tries to change the SIL level name.
And allow expert users to use potentially invalid symbols by specifying it explicitly.
09a8a9d
to
80bef2b
Compare
@_extern(c)
to declare C function without Clang module@extern(c)
to declare C function without Clang module
@swift-ci Please smoke test |
Now the feature is gated by experimental feature flag. It's not shipped in any language release, so this rename should be fine.
80bef2b
to
6947993
Compare
@swift-ci Please smoke test |
@swift-ci build toolchain |
@swift-ci Please smoke test |
Note that the foreign-to-native thunk is still necessary in general. Currently this crashes the compiler:
It would be cool if we treated these like we do functions imported from Clang and generate the foreign-to-native thunk as a shared thunk on demand. |
Motivation
Currently the only proper way to reference a C function from Swift is by importing it through Clang module.
However there are a number of places using the incorrect
@_silgen_name
way to reference C function. (The attribute expects Swift calling convention and it causes calling convention mismatch.)These incorrect references happened because defining a new Clang module requires non-trivial work mostly around the build system. (Also there is a very complex issue, which is not easy to solve with Clang module approach #67853 (comment))
So this PR adds a new variant of
@extern
for C function declaration. Also this unlocks a way to declare a function with C calling convention imported through Wasm’s import mechanism by mixing with@extern(wasm)
The attribute is now gated by
-enable-experimental-feature Extern
Description
The new attribute unlocks declaring a C function in Swift source code. The declared function interfaces are limited to be C-compatible, so no async, no throws, and no ref counted types are allowed. (We might be able to unlock the use of ref counted type in theory but it introduces a lot of complexities)
Examples:
Resolves rdar://115802180 and unblock #67853
Alternative consideration: Allow @_cdecl without body
We initially planned this approach, but didn’t take it for the following reason
@_cdecl
is “expose the Swift declaration to C/Objective-C world” and it allows having Objective-C types in its interface.@_cdecl
function.@_cdecl
function.