mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
[Carbon/C++ interop] Add support for int function params (#5197)
Added support for `int` function parameters. Currently only
pass-by-value is supported.
```c++
// hello_int.h;
auto foo_int(int a, int b) -> int;
```
```c++
// hello_int.cpp
#include "hello_int.h"
#include <cstdio>
auto foo_int(int a, int b) -> int {
printf("a = %i \n", a);
printf("b = %i \n", b);
return a + b;
}
```
```c++
// 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(2, 8));
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
a = 2
b = 8
10
```
Part of #5064
This commit is contained in:
@@ -131,25 +131,27 @@ fn F() {
|
||||
// TODO: Test that template functions are unsupported.
|
||||
// This is not tested because template functions are not considered a single result when doing lookup.
|
||||
|
||||
// --- parameterized_function_decl.h
|
||||
// --- unsupported_param_type_function_decl.h
|
||||
|
||||
void foo(int);
|
||||
struct S {};
|
||||
|
||||
// --- fail_import_parameterized_function_decl.carbon
|
||||
auto foo(S) -> void;
|
||||
|
||||
// --- fail_import_unsupported_param_type_function_decl.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp library "parameterized_function_decl.h";
|
||||
import Cpp library "unsupported_param_type_function_decl.h";
|
||||
|
||||
fn F() {
|
||||
// CHECK:STDERR: fail_import_parameterized_function_decl.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: Function with parameters` [SemanticsTodo]
|
||||
// CHECK:STDERR: Cpp.foo();
|
||||
// CHECK:STDERR: fail_import_unsupported_param_type_function_decl.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: parameter type: struct S` [SemanticsTodo]
|
||||
// CHECK:STDERR: Cpp.foo({});
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR: fail_import_parameterized_function_decl.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `foo` [InCppNameLookup]
|
||||
// CHECK:STDERR: Cpp.foo();
|
||||
// CHECK:STDERR: fail_import_unsupported_param_type_function_decl.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `foo` [InCppNameLookup]
|
||||
// CHECK:STDERR: Cpp.foo({});
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR:
|
||||
Cpp.foo();
|
||||
Cpp.foo({});
|
||||
}
|
||||
|
||||
// --- non_void_return_function_decl.h
|
||||
@@ -411,11 +413,12 @@ fn F() {
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_import_parameterized_function_decl.carbon
|
||||
// CHECK:STDOUT: --- fail_import_unsupported_param_type_function_decl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
|
||||
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
|
||||
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: imports {
|
||||
@@ -431,7 +434,7 @@ fn F() {
|
||||
// CHECK:STDOUT: .F = %F.decl
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
|
||||
// CHECK:STDOUT: import Cpp "parameterized_function_decl.h"
|
||||
// CHECK:STDOUT: import Cpp "unsupported_param_type_function_decl.h"
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
||||
// CHECK:STDOUT: }
|
||||
@@ -440,6 +443,7 @@ fn F() {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
|
||||
// CHECK:STDOUT: %foo.ref: <error> = name_ref foo, <error> [concrete = <error>]
|
||||
// CHECK:STDOUT: %.loc14: %empty_struct_type = struct_literal ()
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
|
||||
Reference in New Issue
Block a user