implement @const_eval

closes #73
master
Andrew Kelley 2016-02-07 15:11:20 -07:00
parent 049e9e5819
commit 26ea20d88f
5 changed files with 36 additions and 1 deletions

View File

@ -1033,6 +1033,7 @@ enum BuiltinFnId {
BuiltinFnIdCDefine,
BuiltinFnIdCUndef,
BuiltinFnIdCompileVar,
BuiltinFnIdConstEval,
};
struct BuiltinFnEntry {

View File

@ -4127,8 +4127,26 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
buf_sprintf("unrecognized compile variable: '%s'", buf_ptr(&var_name)));
return g->builtin_types.entry_invalid;
}
}
case BuiltinFnIdConstEval:
{
AstNode **expr_node = node->data.fn_call_expr.params.at(0)->parent_field;
TypeTableEntry *resolved_type = analyze_expression(g, import, context, expected_type, *expr_node);
if (resolved_type->id == TypeTableEntryIdInvalid) {
return resolved_type;
}
break;
ConstExprValue *const_expr_val = &get_resolved_expr(*expr_node)->const_val;
if (!const_expr_val->ok) {
add_node_error(g, *expr_node, buf_sprintf("unable to evaluate constant expression"));
return resolved_type;
}
ConstExprValue *const_val = &get_resolved_expr(node)->const_val;
*const_val = *const_expr_val;
return resolved_type;
}
}

View File

@ -310,6 +310,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {
case BuiltinFnIdMaxValue:
case BuiltinFnIdMemberCount:
case BuiltinFnIdCompileVar:
case BuiltinFnIdConstEval:
// caught by constant expression eval codegen
zig_unreachable();
}
@ -3513,6 +3514,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "c_define", 2);
create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "c_undef", 1);
create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compile_var", 1);
create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "const_eval", 1);
}

View File

@ -2020,6 +2020,13 @@ fn func() -> bogus {}
add_compile_fail_case("bogus compile var", R"SOURCE(
const x = @compile_var("bogus");
)SOURCE", 1, ".tmp_source.zig:2:24: error: unrecognized compile variable: 'bogus'");
add_compile_fail_case("@const_eval", R"SOURCE(
fn a(x: i32) {
const y = @const_eval(x);
}
)SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression");
}
//////////////////////////////////////////////////////////////////////////////

View File

@ -233,3 +233,10 @@ fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 {
return result;
}
#attribute("test")
fn builtin_const_eval() {
const x : i32 = @const_eval(1 + 2 + 3);
if (x != @const_eval(6)) unreachable{};
}