diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/all_types.hpp | 1 | ||||
| -rw-r--r-- | src/analyze.cpp | 20 | ||||
| -rw-r--r-- | src/codegen.cpp | 2 |
3 files changed, 22 insertions, 1 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp index 7378e4d6e4..e7e925f42f 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1033,6 +1033,7 @@ enum BuiltinFnId { BuiltinFnIdCDefine, BuiltinFnIdCUndef, BuiltinFnIdCompileVar, + BuiltinFnIdConstEval, }; struct BuiltinFnEntry { diff --git a/src/analyze.cpp b/src/analyze.cpp index d6485d0312..2b8119ed48 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -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; } } diff --git a/src/codegen.cpp b/src/codegen.cpp index 6ef2a92158..43e78ce194 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -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); } |
