Skip to content

[Carbon/C++ interop] Add support for C++ type short #5393

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

Merged
merged 6 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions toolchain/check/import_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,28 @@ static auto MakeIntType(Context& context, IntId size_id) -> TypeExpr {
return ExprAsType(context, Parse::NodeId::None, type_inst_id);
}

// Maps a C++ type to a Carbon type. Currently only 32-bit `int` is supported.
// Maps a C++ type to a Carbon type.
// TODO: Support more types.
static auto MapType(Context& context, clang::QualType type) -> TypeExpr {
const auto* builtin_type = dyn_cast<clang::BuiltinType>(type);
if (builtin_type && builtin_type->getKind() == clang::BuiltinType::Int &&
context.ast_context().getTypeSize(type) == 32) {
return MakeIntType(context, context.ints().Add(32));
if (!builtin_type) {
return {.inst_id = SemIR::ErrorInst::TypeInstId,
.type_id = SemIR::ErrorInst::TypeId};
}
// TODO: Refactor to avoid duplication.
switch (builtin_type->getKind()) {
case clang::BuiltinType::Short:
if (context.ast_context().getTypeSize(type) == 16) {
return MakeIntType(context, context.ints().Add(16));
}
break;
case clang::BuiltinType::Int:
if (context.ast_context().getTypeSize(type) == 32) {
return MakeIntType(context, context.ints().Add(32));
}
break;
default:
break;
}
return {.inst_id = SemIR::ErrorInst::TypeInstId,
.type_id = SemIR::ErrorInst::TypeId};
Expand Down
Loading
Loading