mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-14 05:38:31 +00:00
Carbon gives the C++ declaration of an exported Carbon thunk an asm label that includes the target's global symbol prefix (`_` on Mach-O). Clang names a declaration with an asm label `\01<label>` in the IR, so that LLVM uses the label verbatim as the symbol name, while Carbon lowered the thunk definition under its unprefixed name. Both name the same symbol, but they were distinct globals in the IR, so LLVM couldn't see that the C++ calls refer to the Carbon definition. That only works while the definition is never discarded. Mark the Carbon thunk as a `CppThunk` (as described in `toolchain/docs/check/cpp/thunks.md`), record its C++ declaration in `clang_decls`, and when lowering the thunk, ask Clang for its function (creating it if necessary) and define the thunk in that function. This means that the thunk and the C++ calls to it always use the same IR global, with Clang's name and signature. The thunk takes all of its parameters by reference, so Clang lowers its parameters the same way Carbon does, but some C++ ABIs (such as Apple arm64) return `this` from constructors. In that case, return the parameter that Clang marks `returned`. On targets without a global prefix, the IR names already matched, so the only change is that the thunk definitions now carry Clang's parameter and function attributes.