Support mangling imported C++ functions using Clang's MangleContext (#5062)

Keep a pointer to the Clang declaration in Carbon's function declaration
and use it in Carbon mangling by calling Clang mangling.
Create Clang's `MangleContext` once on demand.

Part of #4666.

C++ Interop Demo:

```c++
// hello_world.h

void hello_world();
```

```c++
// hello_world.cpp

#include <cstdio>

void hello_world() { printf("Hello World!\n"); }
```

```
// main.carbon

library "Main";

import Cpp library "hello_world.h";

fn Run() -> i32 {
  Cpp.hello_world();
  return 0;
}
```

```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
Hello World!
```
This commit is contained in:
Boaz Brickner
2025-03-06 09:51:01 +00:00
committed by GitHub
parent d25140e8b9
commit 156ab889f8
7 changed files with 86 additions and 1 deletions
+2 -1
View File
@@ -237,7 +237,8 @@ static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
.definition_id = SemIR::InstId::None},
{.return_slot_pattern_id = SemIR::InstId::None,
.virtual_modifier = SemIR::FunctionFields::VirtualModifier::None,
.self_param_id = SemIR::InstId::None}};
.self_param_id = SemIR::InstId::None,
.cpp_decl = clang_decl}};
function_decl.function_id = context.functions().Add(function_info);