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

Draft
wants to merge 17 commits into
base: trunk
Choose a base branch
from

Conversation

bricknerb
Copy link
Contributor

@bricknerb bricknerb commented May 27, 2025

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 #5468.

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.

@@ -605,6 +661,9 @@ static auto ImportCXXRecordDecl(Context& context, SemIR::LocId loc_id,
// TODO: Set block.
/*body=*/{});

CompleteTypeOrCheckFail(context,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, you added this to prevent a crash that looks like this:

CHECK failure at toolchain/check/pattern_match.cpp:379: static_cast<size_t>(param_pattern.index.index) == results_.size(): Parameters out of order; expecting 0 but got -1

If so, I think that crash indicates violation of an invariant that CalleePatternMatch is supposed to establish, but the import logic isn't calling CalleePatternMatch. Instead MakeParamPatternsBlockId is trying to emulate it, and there are some bugs in the emulation (see also here). I'd strongly encourage you to use CalleePatternMatch whenever you're creating an EntityWithParams. If there are major obstacles to doing that, let me know; I'm happy to help figure out a solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
It seems like CalleePatternMatch() would be called as part of resolving #5449, so I'll hold off this change until we manage to do that. I know @ivanaivanovska has been working on this as her top priority.


struct S {};

auto foo(S) -> void;

// --- fail_todo_import_struct_param_type.carbon
// --- todo_import_struct_definition_param_type.carbon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove todo_?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -347,11 +345,60 @@ static auto MapType(Context& context, clang::QualType type) -> TypeExpr {
.type_id = SemIR::ErrorInst::TypeId};
}

// Maps a C++ record type to a Carbon type.
// TODO: Support more record types.
static auto MapRecordType(Context& context, SemIR::LocId loc_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for the scope? (e.g. the name not found in the scope)

@bricknerb bricknerb marked this pull request as draft June 6, 2025 10:27
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.

3 participants