C++ Interop: Add Core.CppCompat.Long32 as a distinct type for Cpp.long when long is 32 bits (#6364)

For now, only support implicit conversions from and to `i32`.

See #6275 for rationale.

Part of #5263.
This commit is contained in:
Boaz Brickner
2025-11-14 23:26:43 +00:00
committed by GitHub
parent e62678e682
commit bc734bb768
7 changed files with 368 additions and 123 deletions
+5
View File
@@ -1112,6 +1112,11 @@ static auto MapBuiltinIntegerType(Context& context, SemIR::LocId loc_id,
return ExprAsType(context, Parse::NodeId::None,
MakeCharTypeLiteral(context, Parse::NodeId::None));
}
if (ast_context.hasSameType(qual_type, ast_context.LongTy) && width == 32) {
return ExprAsType(context, Parse::NodeId::None,
LookupNameInCore(context, Parse::NodeId::None,
{"CppCompat", "Long32"}));
}
return TypeExpr::None;
}
+14 -6
View File
@@ -104,6 +104,8 @@ static auto LookupCppType(
// type is not supported.
static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
-> clang::QualType {
clang::ASTContext& ast_context = context.ast_context();
// If the class was imported from C++, return the original C++ type.
auto clang_decl_id =
context.name_scopes()
@@ -112,7 +114,7 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
if (clang_decl_id.has_value()) {
clang::Decl* clang_decl = context.clang_decls().Get(clang_decl_id).key.decl;
auto* tag_type_decl = clang::cast<clang::TagDecl>(clang_decl);
return context.ast_context().getCanonicalTagType(tag_type_decl);
return ast_context.getCanonicalTagType(tag_type_decl);
}
// If the class represents a Carbon type literal, map it to the corresponding
@@ -135,22 +137,28 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
CARBON_FATAL("Unexpected invalid numeric type literal");
}
case SemIR::NumericTypeLiteralInfo::Float: {
return context.ast_context().getRealTypeForBitwidth(
return ast_context.getRealTypeForBitwidth(
bit_width, clang::FloatModeKind::NoFloat);
}
case SemIR::NumericTypeLiteralInfo::Int: {
return context.ast_context().getIntTypeForBitwidth(bit_width, true);
return ast_context.getIntTypeForBitwidth(bit_width, true);
}
case SemIR::NumericTypeLiteralInfo::UInt: {
return context.ast_context().getIntTypeForBitwidth(bit_width, false);
return ast_context.getIntTypeForBitwidth(bit_width, false);
}
}
}
case SemIR::TypeLiteralInfo::Char: {
return context.ast_context().CharTy;
return ast_context.CharTy;
}
case SemIR::TypeLiteralInfo::CppLong32: {
if (ast_context.getIntWidth(ast_context.LongTy) == 32) {
return ast_context.LongTy;
}
break;
}
case SemIR::TypeLiteralInfo::CppNullptrT: {
return context.ast_context().NullPtrTy;
return ast_context.NullPtrTy;
}
case SemIR::TypeLiteralInfo::Str: {
return LookupCppType(context, {"std", "string_view"});