mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
@@ -712,9 +712,9 @@ auto CompilationUnit::RunLower() -> void {
|
||||
if (options_->include_debug_info) {
|
||||
subtrees = cache_->tree_and_subtrees_getters();
|
||||
}
|
||||
module_ =
|
||||
Lower::LowerToLLVM(*llvm_context_, subtrees, input_filename_, *sem_ir_,
|
||||
sem_ir_->cpp_ast(), &inst_namer, vlog_stream_);
|
||||
module_ = Lower::LowerToLLVM(*llvm_context_, driver_env_->fs, subtrees,
|
||||
input_filename_, *sem_ir_, sem_ir_->cpp_ast(),
|
||||
&inst_namer, vlog_stream_);
|
||||
});
|
||||
if (vlog_stream_) {
|
||||
CARBON_VLOG("*** llvm::Module ***\n");
|
||||
@@ -964,6 +964,7 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
|
||||
CARBON_VLOG_TO(driver_env.vlog_stream, "*** Check::CheckParseTrees ***\n");
|
||||
Check::CheckParseTrees(check_units, cache.tree_and_subtrees_getters(),
|
||||
options_.prelude_import, driver_env.fs,
|
||||
options_.codegen_options.target,
|
||||
driver_env.vlog_stream, driver_env.fuzzing);
|
||||
CARBON_VLOG_TO(driver_env.vlog_stream,
|
||||
"*** Check::CheckParseTrees done ***\n");
|
||||
|
||||
Reference in New Issue
Block a user