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`,
7979`ZIG_LIBC_STATIC_LIB_DIR`, and `ZIG_LIBC_INCLUDE_DIR` should be set to
8080(example below).
8181
82For MacOS, `ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused.
83
8284```
8385mkdir build
8486cd build
src/all_types.hpp+1
......@@ -1124,6 +1124,7 @@ enum BuiltinFnId {
11241124 BuiltinFnIdCDefine,
11251125 BuiltinFnIdCUndef,
11261126 BuiltinFnIdCompileVar,
1127 BuiltinFnIdCompileErr,
11271128 BuiltinFnIdConstEval,
11281129 BuiltinFnIdCtz,
11291130 BuiltinFnIdClz,
src/analyze.cpp+22-2
......@@ -4759,6 +4759,20 @@ static TypeTableEntry *analyze_truncate(CodeGen *g, ImportTableEntry *import,
47594759 return dest_type;
47604760}
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
47624776static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
47634777 TypeTableEntry *expected_type, AstNode *node)
47644778{
......@@ -5103,6 +5117,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
51035117 return analyze_div_exact(g, import, context, node);
51045118 case BuiltinFnIdTruncate:
51055119 return analyze_truncate(g, import, context, node);
5120 case BuiltinFnIdCompileErr:
5121 return analyze_compile_err(g, import, context, node);
51065122 }
51075123 zig_unreachable();
51085124}
......@@ -5763,9 +5779,13 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
57635779 BlockContext *child_context = prong_node->data.switch_prong.block_context;
57645780 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);
57685782 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 }
57695789 }
57705790
57715791 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) {
539539 case BuiltinFnIdCUndef:
540540 case BuiltinFnIdImport:
541541 case BuiltinFnIdCImport:
542 case BuiltinFnIdCompileErr:
542543 zig_unreachable();
543544 case BuiltinFnIdCtz:
544545 case BuiltinFnIdClz:
......@@ -4655,6 +4656,7 @@ static void define_builtin_fns(CodeGen *g) {
46554656 create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1);
46564657 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2);
46574658 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);
4659 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1);
46584660}
46594661
46604662static 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_
833833 case BuiltinFnIdInvalid:
834834 case BuiltinFnIdFrameAddress:
835835 case BuiltinFnIdReturnAddress:
836 case BuiltinFnIdCompileErr:
836837 zig_unreachable();
837838 }
838839
std/bootstrap.zig+1-1
......@@ -25,7 +25,7 @@ export fn _start() -> unreachable {
2525 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
2626 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
2727 },
28 else => unreachable{},
28 else => @compile_err("unsupported arch"),
2929 }
3030 call_main_and_exit()
3131}
std/linux.zig+1-1
......@@ -1,7 +1,7 @@
11const arch = switch (@compile_var("arch")) {
22 x86_64 => @import("linux_x86_64.zig"),
33 i386 => @import("linux_i386.zig"),
4 else => unreachable{},
4 else => @compile_err("unsupported arch"),
55};
66const errno = @import("errno.zig");
77
std/os.zig+1-1
......@@ -17,7 +17,7 @@ pub fn get_random_bytes(buf: []u8) -> %void {
1717 }
1818 }
1919 },
20 else => unreachable{},
20 else => @compile_err("unsupported os"),
2121 }
2222}
2323