From `@_cdecl` to `@cdecl`

@jrose had a good writeup in a previous thread covering most of the to-do items for formalizing _cdecl, most of which still looks relevant.

Swift declarations don't have any inherent convention of their own; the compiler generates an entry point with the right calling convention as needed by the ABI, by dispatch tables, or by uses of the function. Regular Swift functions can be used as @convention(c) functions (assuming their parameters and results are C-compatible, and they don't capture any context), and conversely functions marked @_cdecl can still be used as regular Swift functions, including as closures. That's one reason @convention(c) isn't reused, since @_cdecl only adds the C calling convention entry point to the ABI of the module but doesn't fundamentally affect how the declaration can be used otherwise. (And, yeah, @escaping is irrelevant to @convention(c) since C function pointers never carry any context.)

I think that providing implementations for externally-declared C functions would be an additional feature, akin to @objcImplementation for implementing externally declared ObjC interfaces. Typically Swift-defined interfaces are consumed from C/ObjC via the header Swift generates.

6 Likes