authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-03-06 12:04:14+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-03-06 12:04:14+01:00
log5ab25798e313105bf783934ac3c18b2930c8da5e
tree57b40a4ccf7139a0190f7d6e440a39572baf9580
parentbf47cf418af785550f298a519b0dbfa2efcdd3cb

We now also use trans_to_bool_expr on bool not


2 files changed, 23 insertions(+), 8 deletions(-)

src/translate_c.cpp+7-7
......@@ -118,7 +118,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
118118static TransScope *trans_stmt(Context *c, TransScope *scope, const Stmt *stmt, AstNode **out_node);
119119static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);
120120static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc);
121
121static AstNode *trans_to_bool_expr(Context *c, TransScope *scope, AstNode *expr);
122122
123123ATTRIBUTE_PRINTF(3, 4)
124124static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {
......@@ -632,7 +632,7 @@ static bool c_is_signed_integer(Context *c, QualType qt) {
632632 case BuiltinType::Int128:
633633 case BuiltinType::WChar_S:
634634 return true;
635 default:
635 default:
636636 return false;
637637 }
638638}
......@@ -653,7 +653,7 @@ static bool c_is_unsigned_integer(Context *c, QualType qt) {
653653 case BuiltinType::UInt128:
654654 case BuiltinType::WChar_U:
655655 return true;
656 default:
656 default:
657657 return false;
658658 }
659659}
......@@ -678,7 +678,7 @@ static bool c_is_float(Context *c, QualType qt) {
678678 case BuiltinType::Float128:
679679 case BuiltinType::LongDouble:
680680 return true;
681 default:
681 default:
682682 return false;
683683 }
684684}
......@@ -1389,7 +1389,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
13891389 if (result_used == ResultUsedYes) {
13901390 // break :x *_ref
13911391 child_scope->node->data.block.statements.append(
1392 trans_create_node_break(c, label_name,
1392 trans_create_node_break(c, label_name,
13931393 trans_create_node_prefix_op(c, PrefixOpDereference,
13941394 trans_create_node_symbol(c, tmp_var_name))));
13951395 }
......@@ -1918,7 +1918,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
19181918 switch (stmt->getOpcode()) {
19191919 case UO_LNot:
19201920 // TODO: Handle int, float, pointer negation
1921 return trans_create_node_prefix_op(c, PrefixOpBoolNot, sub_node);
1921 return trans_create_node_prefix_op(c, PrefixOpBoolNot, trans_to_bool_expr(c, scope, sub_node));
19221922 case UO_Not:
19231923 return trans_create_node_prefix_op(c, PrefixOpBinNot, sub_node);
19241924 default:
......@@ -2291,7 +2291,7 @@ static AstNode *trans_while_loop(Context *c, TransScope *scope, const WhileStmt
22912291
22922292 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(),
22932293 &while_scope->node->data.while_expr.body);
2294 if (body_scope == nullptr)
2294 if (body_scope == nullptr)
22952295 return nullptr;
22962296
22972297 return while_scope->node;
test/translate_c.zig+16-1
......@@ -1083,6 +1083,21 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
10831083 \\}
10841084 );
10851085
1086 cases.add("bool not",
1087 \\int foo(int x) {
1088 \\ return !(x == 0);
1089 \\ return !x;
1090 \\}
1091 ,
1092 \\pub fn foo(x: c_int) c_int {
1093 \\ return !(x == 0);
1094 \\ return !__to_bool_expr: {
1095 \\ const _tmp = x;
1096 \\ break :__to_bool_expr @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0;
1097 \\ };
1098 \\}
1099 );
1100
10861101 cases.add("primitive types included in defined symbols",
10871102 \\int foo(int u32) {
10881103 \\ return u32;
......@@ -1110,7 +1125,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
11101125 );
11111126
11121127 cases.add("macro pointer cast",
1113 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
1128 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
11141129 ,
11151130 \\pub const NRF_GPIO = if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Pointer) @ptrCast(&NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Int) @intToPtr(&NRF_GPIO_Type, NRF_GPIO_BASE) else (&NRF_GPIO_Type)(NRF_GPIO_BASE);
11161131 );