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,...@@ -118,7 +118,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
118static TransScope *trans_stmt(Context *c, TransScope *scope, const Stmt *stmt, AstNode **out_node);118static TransScope *trans_stmt(Context *c, TransScope *scope, const Stmt *stmt, AstNode **out_node);
119static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);119static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);
120static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc);120static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc);
121121static AstNode *trans_to_bool_expr(Context *c, TransScope *scope, AstNode *expr);
122122
123ATTRIBUTE_PRINTF(3, 4)123ATTRIBUTE_PRINTF(3, 4)
124static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {124static 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) {...@@ -632,7 +632,7 @@ static bool c_is_signed_integer(Context *c, QualType qt) {
632 case BuiltinType::Int128:632 case BuiltinType::Int128:
633 case BuiltinType::WChar_S:633 case BuiltinType::WChar_S:
634 return true;634 return true;
635 default: 635 default:
636 return false;636 return false;
637 }637 }
638}638}
...@@ -653,7 +653,7 @@ static bool c_is_unsigned_integer(Context *c, QualType qt) {...@@ -653,7 +653,7 @@ static bool c_is_unsigned_integer(Context *c, QualType qt) {
653 case BuiltinType::UInt128:653 case BuiltinType::UInt128:
654 case BuiltinType::WChar_U:654 case BuiltinType::WChar_U:
655 return true;655 return true;
656 default: 656 default:
657 return false;657 return false;
658 }658 }
659}659}
...@@ -678,7 +678,7 @@ static bool c_is_float(Context *c, QualType qt) {...@@ -678,7 +678,7 @@ static bool c_is_float(Context *c, QualType qt) {
678 case BuiltinType::Float128:678 case BuiltinType::Float128:
679 case BuiltinType::LongDouble:679 case BuiltinType::LongDouble:
680 return true;680 return true;
681 default: 681 default:
682 return false;682 return false;
683 }683 }
684}684}
...@@ -1389,7 +1389,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result...@@ -1389,7 +1389,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
1389 if (result_used == ResultUsedYes) {1389 if (result_used == ResultUsedYes) {
1390 // break :x *_ref1390 // break :x *_ref
1391 child_scope->node->data.block.statements.append(1391 child_scope->node->data.block.statements.append(
1392 trans_create_node_break(c, label_name, 1392 trans_create_node_break(c, label_name,
1393 trans_create_node_prefix_op(c, PrefixOpDereference,1393 trans_create_node_prefix_op(c, PrefixOpDereference,
1394 trans_create_node_symbol(c, tmp_var_name))));1394 trans_create_node_symbol(c, tmp_var_name))));
1395 }1395 }
...@@ -1918,7 +1918,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc...@@ -1918,7 +1918,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
1918 switch (stmt->getOpcode()) {1918 switch (stmt->getOpcode()) {
1919 case UO_LNot:1919 case UO_LNot:
1920 // TODO: Handle int, float, pointer negation1920 // 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));
1922 case UO_Not:1922 case UO_Not:
1923 return trans_create_node_prefix_op(c, PrefixOpBinNot, sub_node);1923 return trans_create_node_prefix_op(c, PrefixOpBinNot, sub_node);
1924 default:1924 default:
...@@ -2291,7 +2291,7 @@ static AstNode *trans_while_loop(Context *c, TransScope *scope, const WhileStmt...@@ -2291,7 +2291,7 @@ static AstNode *trans_while_loop(Context *c, TransScope *scope, const WhileStmt
22912291
2292 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(),2292 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(),
2293 &while_scope->node->data.while_expr.body);2293 &while_scope->node->data.while_expr.body);
2294 if (body_scope == nullptr) 2294 if (body_scope == nullptr)
2295 return nullptr;2295 return nullptr;
22962296
2297 return while_scope->node;2297 return while_scope->node;
test/translate_c.zig+16-1
...@@ -1083,6 +1083,21 @@ pub fn addCases(cases: &tests.TranslateCContext) void {...@@ -1083,6 +1083,21 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
1083 \\}1083 \\}
1084 );1084 );
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
1086 cases.add("primitive types included in defined symbols",1101 cases.add("primitive types included in defined symbols",
1087 \\int foo(int u32) {1102 \\int foo(int u32) {
1088 \\ return u32;1103 \\ return u32;
...@@ -1110,7 +1125,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {...@@ -1110,7 +1125,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
1110 );1125 );
11111126
1112 cases.add("macro pointer cast",1127 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)
1114 ,1129 ,
1115 \\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);1130 \\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);
1116 );1131 );