carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

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

Open bricknerb opened this issue 6 months ago • 0 comments

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 avatar May 27 '25 13:05 bricknerb