mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:54:41 +01:00
When using a C++ struct as a parameter, map its type to a Carbon class type (#5538)
This doesn't support actually passing the value of the struct, which is planned to be implemented using thunks. `ClangDeclId` value is now `ClangDecl` which includes the mapped Carbon instruction in addition to the Clang declaration. This allows finding the Carbon instruction for a given Clang declaration, which is necessary for mapping a Clang struct parameter type to the Carbon class without doing name lookup. We don't take the instruction as part of the hash key, as discussed in [Discord](https://discord.com/channels/655572317891461132/768530752592805919/1380575881050718469). To map the type, we also need to map namespaces. To avoid recursion for inner namespaces, we use a vector. Note that the first commit just changes the order of functions in the file to make review easier. C++ Interop Demo (that shows missing behavior): ```c++ // hello_world.h struct S { S(const S&) { x = 1; } int x; }; void hello_world(S s); ``` ```c++ // hello_world.cpp #include "hello_world.h" #include <cstdio> void hello_world2(S s) { printf("hello_world2: %d\n", s.x); } void hello_world(S s) { printf("hello_world: %d\n", s.x); hello_world2(s); } ``` ```carbon // main.carbon library "Main"; import Cpp library "hello_world.h"; fn Run() -> i32 { var s : Cpp.S; Cpp.hello_world(s); 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: -1108224096 hello_world2: 1 ``` Part of #5533.
This commit is contained in:
@@ -675,7 +675,7 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
|
||||
CARBON_CHECK(!specific_id.has_value(),
|
||||
"Specific functions cannot have C++ definitions");
|
||||
HandleReferencedCppFunction(clang::dyn_cast<clang::FunctionDecl>(
|
||||
sem_ir().clang_decls().Get(clang_decl_id)));
|
||||
sem_ir().clang_decls().Get(clang_decl_id).decl));
|
||||
// TODO: Check that the signature and mangling generated by Clang and the
|
||||
// one we generated are the same.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user