Initial support for interop with class/struct/union fields. (#5849)

Add a new type, `custom_layout_type`, representing a struct type whose
size, alignment, and field offsets can be manually controlled. Use this
as the object representation type for imported C++ class types (which
also includes struct and union types), allowing us to model C++ class
type layouts. In passing, also add support for incomplete C++ class
types, mapping them into incomplete Carbon class types.

Map C++ fields into Carbon field declarations, allowing direct access to
C++ fields from Carbon. So far, no support is added for base classes nor
anonymous struct or union declarations; those will be added in
subsequent PRs. Also, we don't map C++ access control into Carbon yet,
so all C++ fields are accessible regardless of their access control.

For now we still use a `struct_type` as the object representation for
empty C++ classes, in order to continue to support our existing tests
that convert `{}` to empty C++ class types. This is temporary and should
be removed once we support interop with C++ class initialization.
This commit is contained in:
Richard Smith
2025-07-25 21:09:24 +00:00
committed by GitHub
parent ef748ab36d
commit 36f0a73092
36 changed files with 1772 additions and 592 deletions
+6
View File
@@ -299,6 +299,12 @@ class Stringifier {
step_stack_->PushInstId(inst.inner_id);
}
auto StringifyInst(InstId /*inst_id*/, CustomLayoutType inst) -> void {
auto layout = sem_ir_->custom_layouts().Get(inst.layout_id);
*out_ << "<size " << layout[CustomLayoutId::SizeIndex] << ", align "
<< layout[CustomLayoutId::AlignIndex] << ">";
}
auto StringifyInst(InstId /*inst_id*/, FacetAccessType inst) -> void {
// Given `T:! I`, print `T as type` as simply `T`.
step_stack_->PushInstId(inst.facet_value_inst_id);