authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-14 18:54:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-14 18:54:37-07:00
log6ae6b5f5b546ab63311b6b08281f5962c9d24667
treed88b267fdfe0428bad29a599e69f80c8baa2dbba
parent76f909edebf86a5d46d24a9dcf897f2cc3ded86d

add compile_err builtin


8 files changed, 31 insertions(+), 5 deletions(-)

README.md+2
...@@ -79,6 +79,8 @@ If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR`,...@@ -79,6 +79,8 @@ If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR`,
79`ZIG_LIBC_STATIC_LIB_DIR`, and `ZIG_LIBC_INCLUDE_DIR` should be set to79`ZIG_LIBC_STATIC_LIB_DIR`, and `ZIG_LIBC_INCLUDE_DIR` should be set to
80(example below).80(example below).
8181
82For MacOS, `ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused.
83
82```84```
83mkdir build85mkdir build
84cd build86cd build
src/all_types.hpp+1
...@@ -1124,6 +1124,7 @@ enum BuiltinFnId {...@@ -1124,6 +1124,7 @@ enum BuiltinFnId {
1124 BuiltinFnIdCDefine,1124 BuiltinFnIdCDefine,
1125 BuiltinFnIdCUndef,1125 BuiltinFnIdCUndef,
1126 BuiltinFnIdCompileVar,1126 BuiltinFnIdCompileVar,
1127 BuiltinFnIdCompileErr,
1127 BuiltinFnIdConstEval,1128 BuiltinFnIdConstEval,
1128 BuiltinFnIdCtz,1129 BuiltinFnIdCtz,
1129 BuiltinFnIdClz,1130 BuiltinFnIdClz,
src/analyze.cpp+22-2
...@@ -4759,6 +4759,20 @@ static TypeTableEntry *analyze_truncate(CodeGen *g, ImportTableEntry *import,...@@ -4759,6 +4759,20 @@ static TypeTableEntry *analyze_truncate(CodeGen *g, ImportTableEntry *import,
4759 return dest_type;4759 return dest_type;
4760}4760}
47614761
4762static TypeTableEntry *analyze_compile_err(CodeGen *g, ImportTableEntry *import,
4763 BlockContext *context, AstNode *node)
4764{
4765 AstNode *first_param_node = node->data.fn_call_expr.params.at(0);
4766 Buf *err_msg = resolve_const_expr_str(g, import, context, first_param_node->parent_field);
4767 if (!err_msg) {
4768 return g->builtin_types.entry_invalid;
4769 }
4770
4771 add_node_error(g, node, err_msg);
4772
4773 return g->builtin_types.entry_invalid;
4774}
4775
4762static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,4776static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
4763 TypeTableEntry *expected_type, AstNode *node)4777 TypeTableEntry *expected_type, AstNode *node)
4764{4778{
...@@ -5103,6 +5117,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry...@@ -5103,6 +5117,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
5103 return analyze_div_exact(g, import, context, node);5117 return analyze_div_exact(g, import, context, node);
5104 case BuiltinFnIdTruncate:5118 case BuiltinFnIdTruncate:
5105 return analyze_truncate(g, import, context, node);5119 return analyze_truncate(g, import, context, node);
5120 case BuiltinFnIdCompileErr:
5121 return analyze_compile_err(g, import, context, node);
5106 }5122 }
5107 zig_unreachable();5123 zig_unreachable();
5108}5124}
...@@ -5763,9 +5779,13 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,...@@ -5763,9 +5779,13 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
5763 BlockContext *child_context = prong_node->data.switch_prong.block_context;5779 BlockContext *child_context = prong_node->data.switch_prong.block_context;
5764 child_context->codegen_excluded = expr_val->ok && (*const_chosen_prong_index != prong_i);5780 child_context->codegen_excluded = expr_val->ok && (*const_chosen_prong_index != prong_i);
57655781
5766 peer_types[prong_i] = analyze_expression(g, import, child_context, expected_type,
5767 prong_node->data.switch_prong.expr);
5768 peer_nodes[prong_i] = prong_node->data.switch_prong.expr;5782 peer_nodes[prong_i] = prong_node->data.switch_prong.expr;
5783 if (child_context->codegen_excluded) {
5784 peer_types[prong_i] = g->builtin_types.entry_unreachable;
5785 } else {
5786 peer_types[prong_i] = analyze_expression(g, import, child_context, expected_type,
5787 prong_node->data.switch_prong.expr);
5788 }
5769 }5789 }
57705790
5771 if (expr_type->id == TypeTableEntryIdEnum && !else_prong) {5791 if (expr_type->id == TypeTableEntryIdEnum && !else_prong) {
src/codegen.cpp+2
...@@ -539,6 +539,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {...@@ -539,6 +539,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {
539 case BuiltinFnIdCUndef:539 case BuiltinFnIdCUndef:
540 case BuiltinFnIdImport:540 case BuiltinFnIdImport:
541 case BuiltinFnIdCImport:541 case BuiltinFnIdCImport:
542 case BuiltinFnIdCompileErr:
542 zig_unreachable();543 zig_unreachable();
543 case BuiltinFnIdCtz:544 case BuiltinFnIdCtz:
544 case BuiltinFnIdClz:545 case BuiltinFnIdClz:
...@@ -4655,6 +4656,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -4655,6 +4656,7 @@ static void define_builtin_fns(CodeGen *g) {
4655 create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1);4656 create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1);
4656 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2);4657 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2);
4657 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);4658 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);
4659 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1);
4658}4660}
46594661
4660static void init(CodeGen *g, Buf *source_path) {4662static void init(CodeGen *g, Buf *source_path) {
src/eval.cpp+1
...@@ -833,6 +833,7 @@ static bool eval_fn_call_builtin(EvalFn *ef, AstNode *node, ConstExprValue *out_...@@ -833,6 +833,7 @@ static bool eval_fn_call_builtin(EvalFn *ef, AstNode *node, ConstExprValue *out_
833 case BuiltinFnIdInvalid:833 case BuiltinFnIdInvalid:
834 case BuiltinFnIdFrameAddress:834 case BuiltinFnIdFrameAddress:
835 case BuiltinFnIdReturnAddress:835 case BuiltinFnIdReturnAddress:
836 case BuiltinFnIdCompileErr:
836 zig_unreachable();837 zig_unreachable();
837 }838 }
838839
std/bootstrap.zig+1-1
...@@ -25,7 +25,7 @@ export fn _start() -> unreachable {...@@ -25,7 +25,7 @@ export fn _start() -> unreachable {
25 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));25 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
26 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));26 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
27 },27 },
28 else => unreachable{},28 else => @compile_err("unsupported arch"),
29 }29 }
30 call_main_and_exit()30 call_main_and_exit()
31}31}
std/linux.zig+1-1
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const arch = switch (@compile_var("arch")) {1const arch = switch (@compile_var("arch")) {
2 x86_64 => @import("linux_x86_64.zig"),2 x86_64 => @import("linux_x86_64.zig"),
3 i386 => @import("linux_i386.zig"),3 i386 => @import("linux_i386.zig"),
4 else => unreachable{},4 else => @compile_err("unsupported arch"),
5};5};
6const errno = @import("errno.zig");6const errno = @import("errno.zig");
77
std/os.zig+1-1
...@@ -17,7 +17,7 @@ pub fn get_random_bytes(buf: []u8) -> %void {...@@ -17,7 +17,7 @@ pub fn get_random_bytes(buf: []u8) -> %void {
17 }17 }
18 }18 }
19 },19 },
20 else => unreachable{},20 else => @compile_err("unsupported os"),
21 }21 }
22}22}
2323