Support for mapping array types to/from C++. (#7285)

For now, disable the use of array types as by-var paramters and by-init
return types when exporting Carbon functions to C++, as C++ does not
support raw arrays being passed or returned by value.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-06-01 21:55:16 +00:00
committed by GitHub
parent d01e1d2d06
commit 2952f61095
12 changed files with 495 additions and 37 deletions
+24
View File
@@ -1334,6 +1334,26 @@ static auto MapReferenceType(Context& context, clang::QualType type,
return TypeExpr::ForUnsugared(context, pointer_type_id);
}
// Maps a C++ array type to a Carbon array type.
static auto MapArrayType(Context& context, const clang::ArrayType* array_type,
TypeExpr element_type_expr) -> TypeExpr {
if (const auto* constant_array_type =
llvm::dyn_cast<clang::ConstantArrayType>(array_type)) {
auto bound_const_id = TryEvalInst(
context,
SemIR::IntValue{.type_id = GetSingletonType(
context, SemIR::IntLiteralType::TypeInstId),
.int_id = context.ints().AddUnsigned(
constant_array_type->getSize())});
auto bound_inst_id = context.constant_values().GetInstId(bound_const_id);
auto array_type_id =
GetArrayType(context, bound_inst_id, element_type_expr.inst_id);
return TypeExpr::ForUnsugared(context, array_type_id);
}
return TypeExpr::None;
}
// Maps a C++ type to a Carbon type. `type` should not be canonicalized because
// we check for pointer nullability and nullability will be lost by
// canonicalization.
@@ -1349,6 +1369,8 @@ static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
type = type->getPointeeType();
} else if (type->isReferenceType()) {
type = type.getNonReferenceType();
} else if (const auto* array_type = type->getAsArrayTypeUnsafe()) {
type = array_type->getElementType();
} else {
break;
}
@@ -1369,6 +1391,8 @@ static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
mapped = MapPointerType(context, loc_id, wrapper, mapped);
} else if (wrapper->isReferenceType()) {
mapped = MapReferenceType(context, wrapper, mapped);
} else if (const auto* array_type = wrapper->getAsArrayTypeUnsafe()) {
mapped = MapArrayType(context, array_type, mapped);
} else {
CARBON_FATAL("Unexpected wrapper type {0}", wrapper.getAsString());
}