Add support for int return type in Carbon/C++ interop (#5114)

Adding support for importing C++ functions with `int` return type in
Carbon.

Part of #5063

Here is a demo of the functionality:

```c++
// hello_int.h;

auto foo_int() -> int;
```

```c++
// hello_int.cpp

#include "hello_int.h"

auto foo_int() -> int {
    return 1;
}
```

```carbon
// main.carbon

library "Main";

import Cpp library "hello_int.h";
import Core library "io";

fn Carbon_foo(b: i32) {
  Core.Print(b);
}

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

```
$ clang -c hello_int.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$  bazel-bin/toolchain/carbon link hello_int.o main.o --output=demo
$ ./demo
1
```
This commit is contained in:
Ivana Ivanovska
2025-03-24 15:18:20 +00:00
committed by GitHub
parent c0ee446cec
commit ce2ff0a35d
4 changed files with 214 additions and 5 deletions
@@ -153,8 +153,9 @@ fn F() {
}
// --- non_void_return_function_decl.h
struct F;
int foo();
F foo();
// --- fail_import_non_void_return_function_decl.carbon
@@ -163,7 +164,7 @@ library "[[@TEST_NAME]]";
import Cpp library "non_void_return_function_decl.h";
fn F() {
// CHECK:STDERR: fail_import_non_void_return_function_decl.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: Function with non-void return type` [SemanticsTodo]
// CHECK:STDERR: fail_import_non_void_return_function_decl.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: return type: struct F` [SemanticsTodo]
// CHECK:STDERR: Cpp.foo();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_import_non_void_return_function_decl.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `foo` [InCppNameLookup]