Commit 6ae6b5f5b5

Andrew Kelley <superjoe30@gmail.com>
2016-05-15 03:54:37
add compile_err builtin
1 parent 76f909e
src/all_types.hpp
@@ -1124,6 +1124,7 @@ enum BuiltinFnId {
     BuiltinFnIdCDefine,
     BuiltinFnIdCUndef,
     BuiltinFnIdCompileVar,
+    BuiltinFnIdCompileErr,
     BuiltinFnIdConstEval,
     BuiltinFnIdCtz,
     BuiltinFnIdClz,
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) {
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) {
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();
     }
 
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()
 }
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");
 
std/os.zig
@@ -17,7 +17,7 @@ pub fn get_random_bytes(buf: []u8) -> %void {
                 }
             }
         },
-        else => unreachable{},
+        else => @compile_err("unsupported os"),
     }
 }
 
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