mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Support for converting a function to a C++ function pointer (#7789)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "toolchain/check/context.h"
|
||||
#include "toolchain/check/control_flow.h"
|
||||
#include "toolchain/check/core_identifier.h"
|
||||
#include "toolchain/check/cpp/export.h"
|
||||
#include "toolchain/check/diagnostic_helpers.h"
|
||||
#include "toolchain/check/eval.h"
|
||||
#include "toolchain/check/impl_lookup.h"
|
||||
@@ -501,6 +502,25 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
|
||||
.dest_id = return_slot_arg_id});
|
||||
}
|
||||
|
||||
// Performs a conversion from a function to a C++ function pointer type.
|
||||
static auto ConvertFunctionToCppPointer(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::FunctionType src_type,
|
||||
SemIR::CppFunctionPointerType dest_type,
|
||||
SemIR::InstId value_id,
|
||||
ConversionTarget target)
|
||||
-> SemIR::InstId {
|
||||
if (!ExportFunctionToCppPointerConversion(context, value_id, src_type,
|
||||
dest_type, target.diagnose)) {
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
|
||||
return AddInst<SemIR::CppAddrOfFunction>(
|
||||
context, loc_id,
|
||||
{.type_id = target.type_id,
|
||||
.function_ref_id = value_id,
|
||||
.function_id = src_type.function_id});
|
||||
}
|
||||
|
||||
// Performs a conversion from a tuple to a tuple type. This function only
|
||||
// converts the type, and does not perform a final conversion to the requested
|
||||
// expression category.
|
||||
@@ -1630,6 +1650,17 @@ static auto PerformBuiltinConversion(Context& context, SemIR::LocId loc_id,
|
||||
}
|
||||
}
|
||||
|
||||
// Function types can convert to C++ function pointer types.
|
||||
if (auto fn_ptr_type =
|
||||
context.types().TryGetAs<SemIR::CppFunctionPointerType>(
|
||||
target.type_id)) {
|
||||
if (auto src_fn_type =
|
||||
context.types().TryGetAs<SemIR::FunctionType>(value_type_id)) {
|
||||
return ConvertFunctionToCppPointer(context, loc_id, *src_fn_type,
|
||||
*fn_ptr_type, value_id, target);
|
||||
}
|
||||
}
|
||||
|
||||
// Split the qualifiers off the target type.
|
||||
// TODO: Most conversions should probably be looking at the unqualified target
|
||||
// type.
|
||||
|
||||
Reference in New Issue
Block a user