mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Support calling simple Carbon functions from C++ (#6967)
For now, only functions with no parameters and a `()` return type are supported.
This commit is contained in:
@@ -389,6 +389,48 @@ auto CarbonExternalASTSource::MapInstIdToClangDecl(
|
||||
*ast_context_, clang::TagTypeKind::Class, &decl_context,
|
||||
clang::SourceLocation(), clang::SourceLocation(), identifier_info);
|
||||
}
|
||||
case SemIR::StructValue::Kind: {
|
||||
auto callee = GetCallee(context_->sem_ir(), target_constant);
|
||||
auto* callee_function = std::get_if<SemIR::CalleeFunction>(&callee);
|
||||
if (!callee_function) {
|
||||
return nullptr;
|
||||
}
|
||||
const SemIR::Function& function =
|
||||
context_->functions().Get(callee_function->function_id);
|
||||
auto* identifier_info =
|
||||
GetClangIdentifierInfo(*context_, function.name_id);
|
||||
|
||||
if (function.call_param_ranges.explicit_size() != 0) {
|
||||
context_->TODO(target_inst_id,
|
||||
"unsupported: C++ calling a Carbon function with "
|
||||
"parameters");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (function.return_type_inst_id != SemIR::TypeInstId::None) {
|
||||
context_->TODO(target_inst_id,
|
||||
"unsupported: C++ calling a Carbon function with "
|
||||
"return type other than `()`");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// TODO: support non-empty parameter lists.
|
||||
llvm::SmallVector<clang::QualType> cpp_param_types;
|
||||
|
||||
// TODO: support non-void return types.
|
||||
auto cpp_return_type = ast_context_->VoidTy;
|
||||
|
||||
auto cpp_function_type = ast_context_->getFunctionType(
|
||||
cpp_return_type, cpp_param_types,
|
||||
clang::FunctionProtoType::ExtProtoInfo());
|
||||
|
||||
return clang::FunctionDecl::Create(
|
||||
*ast_context_, &decl_context,
|
||||
/*StartLoc=*/clang::SourceLocation(),
|
||||
/*NLoc=*/clang::SourceLocation(),
|
||||
clang::DeclarationName(identifier_info), cpp_function_type,
|
||||
/*TInfo=*/nullptr, clang::SC_Extern);
|
||||
}
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user