Skip to content

When using a C++ struct as a parameter, map it to a Carbon type #5538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

bricknerb
Copy link
Contributor

This doesn't support actually passing the value of the struct, which is planned to be implemented using thunks.

I plan to add more tests in a separate PR.

Based on #5534 and #5537.

C++ Interop Demo (that shows missing behavior):

// hello_world.h

struct S {
  S(const S&) { x = 1; }
  int x;
};

void hello_world(S s);
// 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);
}
// main.carbon

library "Main";

import Cpp library "hello_world.h";

fn Run() -> i32 {
  var s : Cpp.S;
  Cpp.hello_world(s);
  return 0;
}
$ 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.

bricknerb added 3 commits May 27, 2025 09:59
…pe complete

This is required in order to pass these types as parameters / return values.
This doesn't support actually passing the value of the struct, which is planned to be implemented using thunks.

I plan to add more tests in a separate PR.

Based on carbon-language#5534 and carbon-language#5537.

Part of carbon-language#5533.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant