authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-22 23:20:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-22 23:21:00-05:00
loge5b17580107ad0783b7f07de100aeb9ccf175603
treec91671f234ac448b22286aeb00946d319c8b5c05
parent201a3c121a5c28273138b1160c5aac4e24d619bd

remove staticEval builtin in favor of comptime expression


11 files changed, 16 insertions(+), 100 deletions(-)

doc/langref.md-7
......@@ -551,13 +551,6 @@ Build scripts can set additional compile variables of any name and type.
551551The result of this function is a compile time constant that is marked as
552552depending on a compile variable.
553553
554### @staticEval(expression) -> @typeOf(expression)
555
556This function wraps an expression and generates a compile error if the
557expression is not known at compile time.
558
559The result of the function is the result of the expression.
560
561554### @generatedCode(expression) -> @typeOf(expression)
562555
563556This function wraps an expression and returns the result of the expression
src/all_types.hpp+1-9
......@@ -1058,7 +1058,6 @@ enum BuiltinFnId {
10581058 BuiltinFnIdCUndef,
10591059 BuiltinFnIdCompileVar,
10601060 BuiltinFnIdCompileErr,
1061 BuiltinFnIdStaticEval,
10621061 BuiltinFnIdGeneratedCode,
10631062 BuiltinFnIdCtz,
10641063 BuiltinFnIdClz,
......@@ -1373,7 +1372,7 @@ struct ScopeLoop {
13731372};
13741373
13751374// This scope is created for a comptime expression.
1376// NodeTypeCompTime
1375// NodeTypeCompTime, NodeTypeSwitchExpr
13771376struct ScopeCompTime {
13781377 Scope base;
13791378};
......@@ -1454,7 +1453,6 @@ enum IrInstructionId {
14541453 IrInstructionIdEnumTag,
14551454 IrInstructionIdClz,
14561455 IrInstructionIdCtz,
1457 IrInstructionIdStaticEval,
14581456 IrInstructionIdGeneratedCode,
14591457 IrInstructionIdImport,
14601458 IrInstructionIdCImport,
......@@ -1867,12 +1865,6 @@ struct IrInstructionEnumTag {
18671865 IrInstruction *value;
18681866};
18691867
1870struct IrInstructionStaticEval {
1871 IrInstruction base;
1872
1873 IrInstruction *value;
1874};
1875
18761868struct IrInstructionGeneratedCode {
18771869 IrInstruction base;
18781870
src/analyze.cpp+1-1
......@@ -139,7 +139,7 @@ ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_en
139139}
140140
141141Scope *create_comptime_scope(AstNode *node, Scope *parent) {
142 assert(node->type == NodeTypeCompTime);
142 assert(node->type == NodeTypeCompTime || node->type == NodeTypeSwitchExpr);
143143 ScopeCompTime *scope = allocate<ScopeCompTime>(1);
144144 init_scope(&scope->base, ScopeIdCompTime, node, parent);
145145 return &scope->base;
src/codegen.cpp-2
......@@ -2261,7 +2261,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
22612261 case IrInstructionIdCompileVar:
22622262 case IrInstructionIdSizeOf:
22632263 case IrInstructionIdSwitchTarget:
2264 case IrInstructionIdStaticEval:
22652264 case IrInstructionIdContainerInitFields:
22662265 case IrInstructionIdMinValue:
22672266 case IrInstructionIdMaxValue:
......@@ -3640,7 +3639,6 @@ static void define_builtin_fns(CodeGen *g) {
36403639 create_builtin_fn(g, BuiltinFnIdCDefine, "cDefine", 2);
36413640 create_builtin_fn(g, BuiltinFnIdCUndef, "cUndef", 1);
36423641 create_builtin_fn(g, BuiltinFnIdCompileVar, "compileVar", 1);
3643 create_builtin_fn(g, BuiltinFnIdStaticEval, "staticEval", 1);
36443642 create_builtin_fn(g, BuiltinFnIdGeneratedCode, "generatedCode", 1);
36453643 create_builtin_fn(g, BuiltinFnIdCtz, "ctz", 1);
36463644 create_builtin_fn(g, BuiltinFnIdClz, "clz", 1);
src/ir.cpp+7-59
......@@ -315,10 +315,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionEnumTag *) {
315315 return IrInstructionIdEnumTag;
316316}
317317
318static constexpr IrInstructionId ir_instruction_id(IrInstructionStaticEval *) {
319 return IrInstructionIdStaticEval;
320}
321
322318static constexpr IrInstructionId ir_instruction_id(IrInstructionGeneratedCode *) {
323319 return IrInstructionIdGeneratedCode;
324320}
......@@ -1403,15 +1399,6 @@ static IrInstruction *ir_build_enum_tag_from(IrBuilder *irb, IrInstruction *old_
14031399 return new_instruction;
14041400}
14051401
1406static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1407 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, scope, source_node);
1408 instruction->value = value;
1409
1410 ir_ref_instruction(value, irb->current_basic_block);
1411
1412 return &instruction->base;
1413}
1414
14151402static IrInstruction *ir_build_generated_code(IrBuilder *irb, Scope *scope, AstNode *source_node,
14161403 IrInstruction *value)
14171404{
......@@ -2300,13 +2287,6 @@ static IrInstruction *ir_instruction_ctz_get_dep(IrInstructionCtz *instruction,
23002287 }
23012288}
23022289
2303static IrInstruction *ir_instruction_staticeval_get_dep(IrInstructionStaticEval *instruction, size_t index) {
2304 switch (index) {
2305 case 0: return instruction->value;
2306 default: return nullptr;
2307 }
2308}
2309
23102290static IrInstruction *ir_instruction_generatedcode_get_dep(IrInstructionGeneratedCode *instruction, size_t index) {
23112291 switch (index) {
23122292 case 0: return instruction->value;
......@@ -2711,8 +2691,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
27112691 return ir_instruction_clz_get_dep((IrInstructionClz *) instruction, index);
27122692 case IrInstructionIdCtz:
27132693 return ir_instruction_ctz_get_dep((IrInstructionCtz *) instruction, index);
2714 case IrInstructionIdStaticEval:
2715 return ir_instruction_staticeval_get_dep((IrInstructionStaticEval *) instruction, index);
27162694 case IrInstructionIdGeneratedCode:
27172695 return ir_instruction_generatedcode_get_dep((IrInstructionGeneratedCode *) instruction, index);
27182696 case IrInstructionIdImport:
......@@ -3727,15 +3705,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
37273705
37283706 return ir_build_clz(irb, scope, node, arg0_value);
37293707 }
3730 case BuiltinFnIdStaticEval:
3731 {
3732 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
3733 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
3734 if (arg0_value == irb->codegen->invalid_instruction)
3735 return arg0_value;
3736
3737 return ir_build_static_eval(irb, scope, node, arg0_value);
3738 }
37393708 case BuiltinFnIdGeneratedCode:
37403709 {
37413710 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -4731,6 +4700,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
47314700 ZigList<IrBasicBlock *> incoming_blocks = {0};
47324701 ZigList<IrInstructionCheckSwitchProngsRange> check_ranges = {0};
47334702
4703 Scope *comptime_scope = create_comptime_scope(node, scope);
47344704 AstNode *else_prong = nullptr;
47354705 for (size_t prong_i = 0; prong_i < prong_count; prong_i += 1) {
47364706 AstNode *prong_node = node->data.switch_expr.prongs.at(prong_i);
......@@ -4764,11 +4734,11 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
47644734 AstNode *start_node = item_node->data.switch_range.start;
47654735 AstNode *end_node = item_node->data.switch_range.end;
47664736
4767 IrInstruction *start_value = ir_gen_node(irb, start_node, scope);
4737 IrInstruction *start_value = ir_gen_node(irb, start_node, comptime_scope);
47684738 if (start_value == irb->codegen->invalid_instruction)
47694739 return irb->codegen->invalid_instruction;
47704740
4771 IrInstruction *end_value = ir_gen_node(irb, end_node, scope);
4741 IrInstruction *end_value = ir_gen_node(irb, end_node, comptime_scope);
47724742 if (end_value == irb->codegen->invalid_instruction)
47734743 return irb->codegen->invalid_instruction;
47744744
......@@ -4776,13 +4746,10 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
47764746 check_range->start = start_value;
47774747 check_range->end = end_value;
47784748
4779 IrInstruction *start_value_const = ir_build_static_eval(irb, scope, start_node, start_value);
4780 IrInstruction *end_value_const = ir_build_static_eval(irb, scope, start_node, end_value);
4781
47824749 IrInstruction *lower_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpGreaterOrEq,
4783 target_value, start_value_const, false);
4750 target_value, start_value, false);
47844751 IrInstruction *upper_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpLessOrEq,
4785 target_value, end_value_const, false);
4752 target_value, end_value, false);
47864753 IrInstruction *both_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolAnd,
47874754 lower_range_ok, upper_range_ok, false);
47884755 if (ok_bit) {
......@@ -4791,7 +4758,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
47914758 ok_bit = both_ok;
47924759 }
47934760 } else {
4794 IrInstruction *item_value = ir_gen_node(irb, item_node, scope);
4761 IrInstruction *item_value = ir_gen_node(irb, item_node, comptime_scope);
47954762 if (item_value == irb->codegen->invalid_instruction)
47964763 return irb->codegen->invalid_instruction;
47974764
......@@ -4833,7 +4800,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
48334800 AstNode *item_node = prong_node->data.switch_prong.items.at(item_i);
48344801 assert(item_node->type != NodeTypeSwitchRange);
48354802
4836 IrInstruction *item_value = ir_gen_node(irb, item_node, scope);
4803 IrInstruction *item_value = ir_gen_node(irb, item_node, comptime_scope);
48374804 if (item_value == irb->codegen->invalid_instruction)
48384805 return irb->codegen->invalid_instruction;
48394806
......@@ -9660,22 +9627,6 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstruc
96609627 return new_instruction->value.type;
96619628}
96629629
9663static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
9664 IrInstructionStaticEval *static_eval_instruction)
9665{
9666 IrInstruction *value = static_eval_instruction->value->other;
9667 if (value->value.type->id == TypeTableEntryIdInvalid)
9668 return ira->codegen->builtin_types.entry_invalid;
9669
9670 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
9671 if (!val)
9672 return ira->codegen->builtin_types.entry_invalid;
9673
9674 ConstExprValue *out_val = ir_build_const_from(ira, &static_eval_instruction->base, val->depends_on_compile_var);
9675 *out_val = *val;
9676 return value->value.type;
9677}
9678
96799630static TypeTableEntry *ir_analyze_instruction_generated_code(IrAnalyze *ira, IrInstructionGeneratedCode *instruction) {
96809631 IrInstruction *value = instruction->value->other;
96819632 if (value->value.type->id == TypeTableEntryIdInvalid)
......@@ -11379,8 +11330,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1137911330 return ir_analyze_instruction_switch_var(ira, (IrInstructionSwitchVar *)instruction);
1138011331 case IrInstructionIdEnumTag:
1138111332 return ir_analyze_instruction_enum_tag(ira, (IrInstructionEnumTag *)instruction);
11382 case IrInstructionIdStaticEval:
11383 return ir_analyze_instruction_static_eval(ira, (IrInstructionStaticEval *)instruction);
1138411333 case IrInstructionIdGeneratedCode:
1138511334 return ir_analyze_instruction_generated_code(ira, (IrInstructionGeneratedCode *)instruction);
1138611335 case IrInstructionIdImport:
......@@ -11591,7 +11540,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1159111540 case IrInstructionIdSwitchVar:
1159211541 case IrInstructionIdSwitchTarget:
1159311542 case IrInstructionIdEnumTag:
11594 case IrInstructionIdStaticEval:
1159511543 case IrInstructionIdGeneratedCode:
1159611544 case IrInstructionIdRef:
1159711545 case IrInstructionIdMinValue:
src/ir_print.cpp-9
......@@ -486,12 +486,6 @@ static void ir_print_enum_tag(IrPrint *irp, IrInstructionEnumTag *instruction) {
486486 ir_print_other_instruction(irp, instruction->value);
487487}
488488
489static void ir_print_static_eval(IrPrint *irp, IrInstructionStaticEval *instruction) {
490 fprintf(irp->f, "@staticEval(");
491 ir_print_other_instruction(irp, instruction->value);
492 fprintf(irp->f, ")");
493}
494
495489static void ir_print_generated_code(IrPrint *irp, IrInstructionGeneratedCode *instruction) {
496490 fprintf(irp->f, "@generatedCode(");
497491 ir_print_other_instruction(irp, instruction->value);
......@@ -938,9 +932,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
938932 case IrInstructionIdEnumTag:
939933 ir_print_enum_tag(irp, (IrInstructionEnumTag *)instruction);
940934 break;
941 case IrInstructionIdStaticEval:
942 ir_print_static_eval(irp, (IrInstructionStaticEval *)instruction);
943 break;
944935 case IrInstructionIdGeneratedCode:
945936 ir_print_generated_code(irp, (IrInstructionGeneratedCode *)instruction);
946937 break;
test/cases/array.zig+1-1
......@@ -49,7 +49,7 @@ fn arrayLiteral() {
4949fn arrayDotLenConstExpr() {
5050 @setFnTest(this);
5151
52 assert(@staticEval(some_array.len) == 4);
52 assert(comptime {some_array.len == 4});
5353}
5454
5555const ArrayDotLenConstExpr = struct {
test/cases/generics.zig+1-1
......@@ -13,7 +13,7 @@ fn max(comptime T: type, a: T, b: T) -> T {
1313}
1414
1515fn add(comptime a: i32, b: i32) -> i32 {
16 return @staticEval(a) + b;
16 return comptime {a} + b;
1717}
1818
1919const the_max = max(u32, 1234, 5678);
test/cases/math.zig+2-2
......@@ -172,8 +172,8 @@ const DivResult = struct {
172172fn binaryNot() {
173173 @setFnTest(this);
174174
175 assert(@staticEval(~u16(0b1010101010101010) == 0b0101010101010101));
176 assert(@staticEval(~u64(2147483647) == 18446744071562067968));
175 assert(comptime {~u16(0b1010101010101010) == 0b0101010101010101});
176 assert(comptime {~u64(2147483647) == 18446744071562067968});
177177 testBinaryNot(0b1010101010101010);
178178}
179179
test/cases/misc.zig+3-3
......@@ -174,8 +174,8 @@ fn memcpyAndMemsetIntrinsics() {
174174fn builtinStaticEval() {
175175 @setFnTest(this);
176176
177 const x : i32 = @staticEval(1 + 2 + 3);
178 assert(x == @staticEval(6));
177 const x : i32 = comptime {1 + 2 + 3};
178 assert(x == comptime 6);
179179}
180180
181181fn slicing() {
......@@ -201,7 +201,7 @@ fn constantEqualFunctionPointers() {
201201 @setFnTest(this);
202202
203203 const alias = emptyFn;
204 assert(@staticEval(emptyFn == alias));
204 assert(comptime {emptyFn == alias});
205205}
206206
207207fn emptyFn() {}
test/run_tests.cpp-6
......@@ -1116,12 +1116,6 @@ const x = @compileVar("bogus");
11161116 )SOURCE", 1, ".tmp_source.zig:2:23: error: unrecognized compile variable: 'bogus'");
11171117
11181118
1119 add_compile_fail_case("@staticEval", R"SOURCE(
1120fn a(x: i32) {
1121 const y = @staticEval(x);
1122}
1123 )SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression");
1124
11251119 add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(
11261120const Foo = struct {
11271121 y: [get()]u8,