zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit c1fde0e8c45e4ba20fbc3306238c5e7627c2dfd9 (tree)
parent 6356724057ffc94fec03c697ae99bed32d33d2f7
Author: Josh Wolfe <thejoshwolfe@gmail.com>
Date:   Mon, 13 Nov 2017 20:49:53 -0700

parsec supports bitshift operators

Diffstat:
Msrc/parsec.cpp | 21+++++++++++++++++----
Mtest/parsec.zig | 10++++++++++
2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/parsec.cpp b/src/parsec.cpp @@ -987,6 +987,21 @@ static AstNode *trans_create_assign(Context *c, bool result_used, AstNode *block } } +static AstNode *trans_create_shift_op(Context *c, AstNode *block, QualType result_type, Expr *lhs_expr, BinOpType bin_op, Expr *rhs_expr) { + const SourceLocation &rhs_location = rhs_expr->getLocStart(); + AstNode *rhs_type = qual_type_to_log2_int_ref(c, result_type, rhs_location); + // lhs >> u5(rh) + + AstNode *lhs = trans_expr(c, true, block, lhs_expr, TransLValue); + if (lhs == nullptr) return nullptr; + + AstNode *rhs = trans_expr(c, true, block, rhs_expr, TransRValue); + if (rhs == nullptr) return nullptr; + AstNode *coerced_rhs = trans_create_node_fn_call_1(c, rhs_type, rhs); + + return trans_create_node_bin_op(c, lhs, bin_op, coerced_rhs); +} + static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) { switch (stmt->getOpcode()) { case BO_PtrMemD: @@ -1038,11 +1053,9 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeSubWrap : BinOpTypeSub, stmt->getRHS()); case BO_Shl: - emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shl"); - return nullptr; + return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftLeft, stmt->getRHS()); case BO_Shr: - emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Shr"); - return nullptr; + return trans_create_shift_op(c, block, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftRight, stmt->getRHS()); case BO_LT: return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS()); case BO_GT: diff --git a/test/parsec.zig b/test/parsec.zig @@ -620,6 +620,16 @@ pub fn addCases(cases: &tests.ParseCContext) { \\} ); + cases.addC("bitshift", + \\int foo(void) { + \\ return (1 << 2) >> 1; + \\} + , + \\export fn foo() -> c_int { + \\ return (1 << @import("std").math.Log2Int(c_int)(2)) >> @import("std").math.Log2Int(c_int)(1); + \\} + ); + cases.addC("duplicate typedef", \\typedef long foo; \\typedef int bar;