From 404defd99b76ebf2cfe46ea26248a8813e40136f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 May 2016 20:53:16 -0700 Subject: add div_exact builtin fn closes #149 --- src/analyze.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 5832998900..4bc3022765 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4657,6 +4657,37 @@ static TypeTableEntry *analyze_fence(CodeGen *g, ImportTableEntry *import, return g->builtin_types.entry_void; } +static TypeTableEntry *analyze_div_exact(CodeGen *g, ImportTableEntry *import, + BlockContext *context, AstNode *node) +{ + assert(node->type == NodeTypeFnCallExpr); + + AstNode **op1 = &node->data.fn_call_expr.params.at(0); + AstNode **op2 = &node->data.fn_call_expr.params.at(1); + + TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, *op1); + TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, *op2); + + AstNode *op_nodes[] = {*op1, *op2}; + TypeTableEntry *op_types[] = {op1_type, op2_type}; + TypeTableEntry *result_type = resolve_peer_type_compatibility(g, import, context, node, + op_nodes, op_types, 2); + + if (result_type->id == TypeTableEntryIdInvalid) { + return g->builtin_types.entry_invalid; + } else if (result_type->id == TypeTableEntryIdInt) { + return result_type; + } else if (result_type->id == TypeTableEntryIdNumLitInt) { + // check for division by zero + // check for non exact division + zig_panic("TODO"); + } else { + add_node_error(g, node, + buf_sprintf("expected integer type, got '%s'", buf_ptr(&result_type->name))); + return g->builtin_types.entry_invalid; + } +} + static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context, TypeTableEntry *expected_type, AstNode *node) { @@ -4997,6 +5028,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry return analyze_cmpxchg(g, import, context, node); case BuiltinFnIdFence: return analyze_fence(g, import, context, node); + case BuiltinFnIdDivExact: + return analyze_div_exact(g, import, context, node); } zig_unreachable(); } -- cgit v1.2.3