Add CppCompat.ULong32, CppCompat.LongLong64 and CppCompat.ULongLong64 (#6386)

This extends #6364 to allow having:
* `Cpp.unsigned_long` as a distinct type when `unsigned long` is 32
bits.
* `Cpp.long_long` and `Cpp.unsigned_long_long` as distinct types when
`long` and `unsigned long` are 64 bits.

Similarly to #6364, we only support implicit conversions from the
matching literal type (`u32`, `i64` and `u64`).

See #6275 for rationale.

Part of #5263.
This commit is contained in:
Boaz Brickner
2025-11-26 16:51:22 +00:00
committed by GitHub
parent 05fd656bdb
commit bef92cf881
10 changed files with 1313 additions and 445 deletions
+25 -3
View File
@@ -1088,6 +1088,19 @@ static auto MakeCppCompatType(Context& context, SemIR::LocId loc_id,
LookupNameInCore(context, loc_id, {"CppCompat", name}));
}
// Maps a C++ builtin integer type to a Carbon `Core.CppCompat` type.
static auto MapBuiltinCppCompatIntegerType(Context& context,
unsigned int cpp_width,
unsigned int carbon_width,
llvm::StringRef cpp_compat_name)
-> TypeExpr {
if (cpp_width != carbon_width) {
return TypeExpr::None;
}
return MakeCppCompatType(context, Parse::NodeId::None, cpp_compat_name);
}
// Maps a C++ builtin integer type to a Carbon type.
// TODO: Handle integer types that map to named aliases.
static auto MapBuiltinIntegerType(Context& context, SemIR::LocId loc_id,
@@ -1114,9 +1127,18 @@ static auto MapBuiltinIntegerType(Context& context, SemIR::LocId loc_id,
return ExprAsType(context, Parse::NodeId::None,
MakeCharTypeLiteral(context, Parse::NodeId::None));
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.LongTy) &&
width == 32) {
return MakeCppCompatType(context, loc_id, "Long32");
if (clang::ASTContext::hasSameType(qual_type, ast_context.LongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 32, "Long32");
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.UnsignedLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 32, "ULong32");
}
if (clang::ASTContext::hasSameType(qual_type, ast_context.LongLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 64, "LongLong64");
}
if (clang::ASTContext::hasSameType(qual_type,
ast_context.UnsignedLongLongTy)) {
return MapBuiltinCppCompatIntegerType(context, width, 64, "ULongLong64");
}
return TypeExpr::None;
}