From 6ae6b5f5b546ab63311b6b08281f5962c9d24667 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 14 May 2016 18:54:37 -0700 Subject: [PATCH] add compile_err builtin --- README.md | 2 ++ src/all_types.hpp | 1 + src/analyze.cpp | 24 ++++++++++++++++++++++-- src/codegen.cpp | 2 ++ src/eval.cpp | 1 + std/bootstrap.zig | 2 +- std/linux.zig | 2 +- std/os.zig | 2 +- 8 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 48791aa544746f05f678b05e74c6215d1eebab85..b7e558aa1b399fb1b3689bf055fb1c48cdbd17d7 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR`, `ZIG_LIBC_STATIC_LIB_DIR`, and `ZIG_LIBC_INCLUDE_DIR` should be set to (example below). +For MacOS, `ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused. + ``` mkdir build cd build diff --git a/src/all_types.hpp b/src/all_types.hpp index 5fd365cc4e78860a6ca20ee0bcdf91feba75b657..5f209f61bd7ff8c4f0cab828cd2fa1676d8690e6 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1124,6 +1124,7 @@ enum BuiltinFnId { BuiltinFnIdCDefine, BuiltinFnIdCUndef, BuiltinFnIdCompileVar, + BuiltinFnIdCompileErr, BuiltinFnIdConstEval, BuiltinFnIdCtz, BuiltinFnIdClz, diff --git a/src/analyze.cpp b/src/analyze.cpp index ee3235d342b0007b9ef43b5bd4d2394e845eeeec..6766c6d24716e4d4fd4dc8d180d3c944cd017767 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4759,6 +4759,20 @@ static TypeTableEntry *analyze_truncate(CodeGen *g, ImportTableEntry *import, return dest_type; } +static TypeTableEntry *analyze_compile_err(CodeGen *g, ImportTableEntry *import, + BlockContext *context, AstNode *node) +{ + AstNode *first_param_node = node->data.fn_call_expr.params.at(0); + Buf *err_msg = resolve_const_expr_str(g, import, context, first_param_node->parent_field); + if (!err_msg) { + return g->builtin_types.entry_invalid; + } + + add_node_error(g, node, err_msg); + + return g->builtin_types.entry_invalid; +} + static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context, TypeTableEntry *expected_type, AstNode *node) { @@ -5103,6 +5117,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry return analyze_div_exact(g, import, context, node); case BuiltinFnIdTruncate: return analyze_truncate(g, import, context, node); + case BuiltinFnIdCompileErr: + return analyze_compile_err(g, import, context, node); } zig_unreachable(); } @@ -5763,9 +5779,13 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import, BlockContext *child_context = prong_node->data.switch_prong.block_context; child_context->codegen_excluded = expr_val->ok && (*const_chosen_prong_index != prong_i); - peer_types[prong_i] = analyze_expression(g, import, child_context, expected_type, - prong_node->data.switch_prong.expr); peer_nodes[prong_i] = prong_node->data.switch_prong.expr; + if (child_context->codegen_excluded) { + peer_types[prong_i] = g->builtin_types.entry_unreachable; + } else { + peer_types[prong_i] = analyze_expression(g, import, child_context, expected_type, + prong_node->data.switch_prong.expr); + } } if (expr_type->id == TypeTableEntryIdEnum && !else_prong) { diff --git a/src/codegen.cpp b/src/codegen.cpp index f42cf5a1a0d69cb48fdebbff00497ed288bbda6d..b8ffb1869c64199d47a335b217e5b79217e2dd49 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -539,6 +539,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) { case BuiltinFnIdCUndef: case BuiltinFnIdImport: case BuiltinFnIdCImport: + case BuiltinFnIdCompileErr: zig_unreachable(); case BuiltinFnIdCtz: case BuiltinFnIdClz: @@ -4655,6 +4656,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1); create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2); create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2); + create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1); } static void init(CodeGen *g, Buf *source_path) { diff --git a/src/eval.cpp b/src/eval.cpp index 4499b8529f12932ce38feda9644aa45a29e1f189..baefad0f66b88cbcbf0ab8a564c462797c76386e 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -833,6 +833,7 @@ static bool eval_fn_call_builtin(EvalFn *ef, AstNode *node, ConstExprValue *out_ case BuiltinFnIdInvalid: case BuiltinFnIdFrameAddress: case BuiltinFnIdReturnAddress: + case BuiltinFnIdCompileErr: zig_unreachable(); } diff --git a/std/bootstrap.zig b/std/bootstrap.zig index 7185c09a4c0f694b7d0a602e0c79d201fb0068f2..153c61a868c7f82244642fad4c3c4af060ae70da 100644 --- a/std/bootstrap.zig +++ b/std/bootstrap.zig @@ -25,7 +25,7 @@ export fn _start() -> unreachable { argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize)); argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8)); }, - else => unreachable{}, + else => @compile_err("unsupported arch"), } call_main_and_exit() } diff --git a/std/linux.zig b/std/linux.zig index eceb9a8fa5782700ac933591f753d18b1cf91d9b..9a3d8569cec797365441feacb8522db3c1f8116b 100644 --- a/std/linux.zig +++ b/std/linux.zig @@ -1,7 +1,7 @@ const arch = switch (@compile_var("arch")) { x86_64 => @import("linux_x86_64.zig"), i386 => @import("linux_i386.zig"), - else => unreachable{}, + else => @compile_err("unsupported arch"), }; const errno = @import("errno.zig"); diff --git a/std/os.zig b/std/os.zig index 7255611a32b1f804e3ce91604bcd46437c12f8fa..b9f1d59a187143872c242d2c5a7f6cbc71fe7b9d 100644 --- a/std/os.zig +++ b/std/os.zig @@ -17,7 +17,7 @@ pub fn get_random_bytes(buf: []u8) -> %void { } } }, - else => unreachable{}, + else => @compile_err("unsupported os"), } } -- 2.54.0