Commit 5ab25798e3

Jimmi Holst Christensen <jhc@liab.dk>
2018-03-06 12:04:14
We now also use trans_to_bool_expr on bool not
1 parent bf47cf4
Changed files (2)
src/translate_c.cpp
@@ -118,7 +118,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
 static TransScope *trans_stmt(Context *c, TransScope *scope, const Stmt *stmt, AstNode **out_node);
 static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);
 static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc);
-
+static AstNode *trans_to_bool_expr(Context *c, TransScope *scope, AstNode *expr);
 
 ATTRIBUTE_PRINTF(3, 4)
 static 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) {
         case BuiltinType::Int128:
         case BuiltinType::WChar_S:
             return true;
-        default: 
+        default:
             return false;
     }
 }
@@ -653,7 +653,7 @@ static bool c_is_unsigned_integer(Context *c, QualType qt) {
         case BuiltinType::UInt128:
         case BuiltinType::WChar_U:
             return true;
-        default: 
+        default:
             return false;
     }
 }
@@ -678,7 +678,7 @@ static bool c_is_float(Context *c, QualType qt) {
         case BuiltinType::Float128:
         case BuiltinType::LongDouble:
             return true;
-        default: 
+        default:
             return false;
     }
 }
@@ -1389,7 +1389,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
         if (result_used == ResultUsedYes) {
             // break :x *_ref
             child_scope->node->data.block.statements.append(
-                trans_create_node_break(c, label_name, 
+                trans_create_node_break(c, label_name,
                     trans_create_node_prefix_op(c, PrefixOpDereference,
                         trans_create_node_symbol(c, tmp_var_name))));
         }
@@ -1918,7 +1918,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
                 switch (stmt->getOpcode()) {
                     case UO_LNot:
                         // TODO: Handle int, float, pointer negation
-                        return trans_create_node_prefix_op(c, PrefixOpBoolNot, sub_node);
+                        return trans_create_node_prefix_op(c, PrefixOpBoolNot, trans_to_bool_expr(c, scope, sub_node));
                     case UO_Not:
                         return trans_create_node_prefix_op(c, PrefixOpBinNot, sub_node);
                     default:
@@ -2291,7 +2291,7 @@ static AstNode *trans_while_loop(Context *c, TransScope *scope, const WhileStmt
 
     TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(),
             &while_scope->node->data.while_expr.body);
-    if (body_scope == nullptr) 
+    if (body_scope == nullptr)
         return nullptr;
 
     return while_scope->node;
test/translate_c.zig
@@ -1083,6 +1083,21 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
         \\}
     );
 
+    cases.add("bool not",
+        \\int foo(int x) {
+        \\    return !(x == 0);
+        \\    return !x;
+        \\}
+    ,
+        \\pub fn foo(x: c_int) c_int {
+        \\    return !(x == 0);
+        \\    return !__to_bool_expr: {
+        \\        const _tmp = x;
+        \\        break :__to_bool_expr @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0;
+        \\    };
+        \\}
+    );
+
     cases.add("primitive types included in defined symbols",
         \\int foo(int u32) {
         \\    return u32;
@@ -1110,7 +1125,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
     );
 
     cases.add("macro pointer cast",
-        \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) 
+        \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
     ,
         \\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);
     );