]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Move implicit GValue cast for comparison to BinaryExpression
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 25 Mar 2019 11:46:15 +0000 (12:46 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 10 Apr 2019 14:26:19 +0000 (16:26 +0200)
Handle "==" and "!=" only as it was done before.

This generates correct c-code for boxed simple-types and will perform
value-based comparisons.

See https://bugzilla.gnome.org/show_bug.cgi?id=585063

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/gvalue-implicit-comparison.vala [new file with mode: 0644]
vala/valabinaryexpression.vala

index f2c2fc61f62568778a4ae89704dbc56c6bb6081f..114bbaa3f4f3fba3dba7b73c6e35ec48e989f4eb 100644 (file)
@@ -2806,23 +2806,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                var left_type_as_struct = left_type.data_type as Struct;
                var right_type_as_struct = right_type.data_type as Struct;
 
-               // GValue support
-               var valuecast = try_cast_value_to_type (cleft, left_type, right_type);
-               if (valuecast != null) {
-                       cleft = valuecast;
-                       left_type = right_type;
-                       make_comparable_cexpression (ref left_type, ref cleft, ref right_type, ref cright);
-                       return;
-               }
-
-               valuecast = try_cast_value_to_type (cright, right_type, left_type);
-               if (valuecast != null) {
-                       cright = valuecast;
-                       right_type = left_type;
-                       make_comparable_cexpression (ref left_type, ref cleft, ref right_type, ref cright);
-                       return;
-               }
-
                if (left_type.data_type is Class && !((Class) left_type.data_type).is_compact &&
                    right_type.data_type is Class && !((Class) right_type.data_type).is_compact) {
                        var left_cl = (Class) left_type.data_type;
index 48939cfb90c4cccd4d580d0aa8aa93ad2ea3c169..b7bbed0f814e2819c3daf4c92395c90a6810790b 100644 (file)
@@ -206,6 +206,7 @@ TESTS = \
        structs/struct-no-gtype.vala \
        structs/structs.vala \
        structs/gvalue.vala \
+       structs/gvalue-implicit-comparison.vala \
        structs/bug530605.vala \
        structs/bug572091.vala \
        structs/bug583603.vala \
diff --git a/tests/structs/gvalue-implicit-comparison.vala b/tests/structs/gvalue-implicit-comparison.vala
new file mode 100644 (file)
index 0000000..91865fb
--- /dev/null
@@ -0,0 +1,98 @@
+Value get_value (Value v) {
+    return v;
+}
+
+Value? get_nullable_value (Value? v) {
+    return v;
+}
+
+void main () {
+       {
+               Value v = Value (typeof (int));
+               v.set_int (42);
+               if (v == 42) {
+               } else {
+                       assert_not_reached ();
+               }
+               if (42 == v) {
+               } else {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value? v = Value (typeof (int));
+               v.set_int (42);
+               if (v == 42) {
+               } else {
+                       assert_not_reached ();
+               }
+               if (42 == v) {
+               } else {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value v = Value (typeof (string));
+               v.set_string ("foo");
+               if (v == "foo") {
+               } else {
+                       assert_not_reached ();
+               }
+               if ("foo" == v) {
+               } else {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value? v = Value (typeof (string));
+               v.set_string ("foo");
+               if (v == "foo") {
+               } else {
+                       assert_not_reached ();
+               }
+               if ("foo" == v) {
+               } else {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value v = Value (typeof (int));
+               v.set_int (23);
+               if (get_value (v) != 23) {
+                       assert_not_reached ();
+               }
+               if (23 != get_value (v)) {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value? v = Value (typeof (int));
+               v.set_int (23);
+               if (get_nullable_value (v) != 23) {
+                       assert_not_reached ();
+               }
+               if (23 != get_nullable_value (v)) {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value v = Value (typeof (string));
+               v.set_string ("bar");
+               if (get_value (v) != "bar") {
+                       assert_not_reached ();
+               }
+               if ("bar" != get_value (v)) {
+                       assert_not_reached ();
+               }
+       }
+       {
+               Value? v = Value (typeof (string));
+               v.set_string ("bar");
+               if (get_nullable_value (v) != "bar") {
+                       assert_not_reached ();
+               }
+               if ("bar" != get_nullable_value (v)) {
+                       assert_not_reached ();
+               }
+       }
+}
index beebe118dea89a349bee9592662b78d68b98b221..a3241c71c9e73e4cd8b437fec6b6a036923c2907 100644 (file)
@@ -463,6 +463,26 @@ public class Vala.BinaryExpression : Expression {
                           || operator == BinaryOperator.INEQUALITY) {
                        /* relational operation */
 
+                       // Implicit cast for comparsion expression of GValue with other type
+                       var gvalue_type = context.analyzer.gvalue_type.data_type;
+                       if ((left.target_type.data_type == gvalue_type && right.target_type.data_type != gvalue_type)
+                           || (left.target_type.data_type != gvalue_type && right.target_type.data_type == gvalue_type)) {
+                               Expression gvalue_expr;
+                               DataType target_type;
+                               if (left.target_type.data_type == gvalue_type) {
+                                       gvalue_expr = left;
+                                       target_type = right.target_type;
+                               } else {
+                                       gvalue_expr = right;
+                                       target_type = left.target_type;
+                               }
+
+                               var cast_expr = new CastExpression (gvalue_expr, target_type, gvalue_expr.source_reference);
+                               replace_expression (gvalue_expr, cast_expr);
+                               checked = false;
+                               return check (context);
+                       }
+
                        if (!right.value_type.compatible (left.value_type)
                            && !left.value_type.compatible (right.value_type)) {
                                Report.error (source_reference, "Equality operation: `%s' and `%s' are incompatible".printf (right.value_type.to_string (), left.value_type.to_string ()));