Add support for importing C++ inline functions (#5427)

This requires:
* Making `FunctionDecl` mutable since generating code
(`HandleTopLevelDecl()`) requires a mutable declaration and since we
manually add `used` attribute to force code generation.
* Passing the file system to `Lower` since it's needed by Clang code
generation.
* Creating an internal Clang LLVM module and link it against the Carbon
LLVM module.

Demo:

```c++
// hello_world.h

extern int puts;
inline void hello_world() {
  ((int (*)(const char*))&puts)("hello world");
}
```

```carbon
// main.carbon

library "Main";

import Cpp library "hello_world.h";

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

```shell
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o --output=demo
$ ./demo
hello world
```

Based on https://github.com/carbon-language/carbon-lang/pull/5406.

Part of #5405.
This commit is contained in:
Boaz Brickner
2025-05-21 07:02:05 +00:00
committed by GitHub
parent 5b884ae14d
commit 852d0191a9
16 changed files with 385 additions and 57 deletions
+3 -1
View File
@@ -10,6 +10,7 @@
#include "common/check.h"
#include "common/raw_string_ostream.h"
#include "llvm/TargetParser/Host.h"
#include "toolchain/base/shared_value_stores.h"
#include "toolchain/check/check.h"
#include "toolchain/diagnostics/diagnostic.h"
@@ -152,7 +153,8 @@ auto Context::File::SetText(Context& context, std::optional<int64_t> version,
// TODO: Include the prelude.
Check::CheckParseTrees(
units, llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>(getter),
/*prelude_import=*/false, fs, context.vlog_stream(), /*fuzzing=*/false);
/*prelude_import=*/false, fs, llvm::sys::getDefaultTargetTriple(),
context.vlog_stream(), /*fuzzing=*/false);
// Note we need to publish diagnostics even when empty.
// TODO: Consider caching previously published diagnostics and only publishing