Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The embedded copy of Clang is doing some heavy work I think. At no point does any of the C or C++ code get transformed into Swift. Instead, it's used to import the the header directly into Swift (in a manner similar to a pre-compiled header), and then Swift is able to use the platform C calling convention to directly call the C code. Swift is generally really good at code-switching between various calling conventions, including its own, C, Objective-C msg_send, etc.

Many languages have some facility for importing declarations like that and even using C calling convention to invoke them (e.g. Rust + bindgen), but Swift goes much further than just importing declarations; if the header happens to contain C definition code, it actually compiles that code directly and then can then emit a call to it.

In the case of C++, which doesn't have a stable ABI on most platforms, it likely also knows and emits code using Clang's C++ calling convention for the platform (e.g. Clang on Windows uses the MSVC calling convention and so does Swift when calling C++). But Swift's ability to actually compile C++ code is very powerful in dealing with template functions and classes because Swift can instantiate and compile templated code in the C++ headers. AFAIK it's the only major production language I've seen that even attempts to do this.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: