mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
Add basic support for eval fn and musteval fn. (#6694)
Add support for compile-time functions. `eval fn` is analogous to C++ `constexpr`, and is evaluated at compile time when it has compile-time arguments. `musteval fn` is analogous to C++ `consteval`, and requires that its arguments be available at compile time and is always evaluated at compile time. For now we require the modifier to match across redeclarations of the function. The specific modifier syntax here is a placeholder and not yet part of an approved design. Limitations: Only very basic support for evaluation is provided. So far there's no support for mutable state or `if` expressions, but otherwise control flow and passing and returning values should work. Carbon evaluation recursion is modeled by C++ recursion for now, so you can overflow the toolchain stack easily. Functions that use in-place initialization will generally not work yet, as they are modeled as passing a non-compile-time-constant reference to a temporary to the call. Add missing categorization of `name_binding_decl` as `NotExpr` to match other similar declaration instructions like `FunctionDecl`, so that we can uniformly skip over them when they occur within function bodies. Assisted-by: Gemini 3 Pro and Flash via Antigravity
This commit is contained in:
@@ -90,10 +90,11 @@ static auto DiagnoseModifiers(Context& context,
|
||||
std::optional<SemIR::Inst> parent_scope_inst,
|
||||
SemIR::InstId self_param_id) -> void {
|
||||
CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
|
||||
LimitModifiersOnDecl(context, introducer,
|
||||
KeywordModifierSet::Access | KeywordModifierSet::Extern |
|
||||
KeywordModifierSet::Method |
|
||||
KeywordModifierSet::Interface);
|
||||
LimitModifiersOnDecl(
|
||||
context, introducer,
|
||||
KeywordModifierSet::Access | KeywordModifierSet::Extern |
|
||||
KeywordModifierSet::Export | KeywordModifierSet::Method |
|
||||
KeywordModifierSet::Interface | KeywordModifierSet::Evaluation);
|
||||
RestrictExternModifierOnDecl(context, introducer, parent_scope_inst,
|
||||
is_definition);
|
||||
CheckMethodModifiersOnFunction(context, introducer, parent_scope_inst_id,
|
||||
@@ -128,6 +129,16 @@ static auto GetVirtualModifier(const KeywordModifierSet& modifier_set)
|
||||
.Default(SemIR::Function::VirtualModifier::None);
|
||||
}
|
||||
|
||||
// Returns the evaluation modifier as an enum.
|
||||
static auto GetEvaluationMode(const KeywordModifierSet& modifier_set)
|
||||
-> SemIR::Function::EvaluationMode {
|
||||
return modifier_set.ToEnum<SemIR::Function::EvaluationMode>()
|
||||
.Case(KeywordModifierSet::Eval, SemIR::Function::EvaluationMode::Eval)
|
||||
.Case(KeywordModifierSet::MustEval,
|
||||
SemIR::Function::EvaluationMode::MustEval)
|
||||
.Default(SemIR::Function::EvaluationMode::None);
|
||||
}
|
||||
|
||||
// Tries to merge new_function into prev_function_id. Since new_function won't
|
||||
// have a definition even if one is upcoming, set is_definition to indicate the
|
||||
// planned result.
|
||||
@@ -422,6 +433,7 @@ static auto BuildFunctionDecl(Context& context,
|
||||
parent_scope_inst_id, parent_scope_inst, self_param_id);
|
||||
bool is_extern = introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern);
|
||||
auto virtual_modifier = GetVirtualModifier(introducer.modifier_set);
|
||||
auto evaluation_mode = GetEvaluationMode(introducer.modifier_set);
|
||||
|
||||
// Add the function declaration.
|
||||
SemIR::FunctionDecl function_decl = {SemIR::TypeId::None,
|
||||
@@ -440,6 +452,7 @@ static auto BuildFunctionDecl(Context& context,
|
||||
.return_form_inst_id = return_form_inst_id,
|
||||
.return_patterns_id = return_patterns_id,
|
||||
.virtual_modifier = virtual_modifier,
|
||||
.evaluation_mode = evaluation_mode,
|
||||
.self_param_id = self_param_id}};
|
||||
if (is_definition) {
|
||||
function_info.definition_id = decl_id;
|
||||
|
||||
Reference in New Issue
Block a user