]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Support ownership transfer of non-null struct types
authorLuca Bruno <lucabru@src.gnome.org>
Sat, 9 Jul 2011 08:48:44 +0000 (10:48 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Sat, 9 Jul 2011 09:06:25 +0000 (11:06 +0200)
Fixes bug 596144.

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/bug596144.vala [new file with mode: 0644]

index 16e3856fdce88ce62c5e129a9641b15aff6ce470..92f0274c96a3ba6a286ced2d5bf4202500a88d03 100644 (file)
@@ -4597,13 +4597,21 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                /* tmp = expr.inner; expr.inner = NULL; expr = tmp; */
                expr.target_value = store_temp_value (expr.inner.target_value, expr);
 
-               if (!(expr.value_type is DelegateType)) {
-                       ccode.add_assignment (get_cvalue (expr.inner), new CCodeConstant ("NULL"));
-               } else {
+               if (expr.inner.value_type is StructValueType && !expr.inner.value_type.nullable) {
+                       // memset needs string.h
+                       cfile.add_include ("string.h");
+                       var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
+                       creation_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_cvalue (expr.inner)));
+                       creation_call.add_argument (new CCodeConstant ("0"));
+                       creation_call.add_argument (new CCodeIdentifier ("sizeof (%s)".printf (expr.inner.value_type.get_cname ())));
+                       ccode.add_expression (creation_call);
+               } else if (expr.value_type is DelegateType) {
                        var target_destroy_notify = get_delegate_target_destroy_notify_cvalue (expr.inner.target_value);
                        if (target_destroy_notify != null) {
                                ccode.add_assignment (target_destroy_notify, new CCodeConstant ("NULL"));
                        }
+               } else {
+                       ccode.add_assignment (get_cvalue (expr.inner), new CCodeConstant ("NULL"));
                }
        }
 
index eca8dd27badff677352fbee740cf4b0f8f703efa..9a2c4e25194f7c8774ea0e125657549d27d355ac 100644 (file)
@@ -58,6 +58,7 @@ TESTS = \
        structs/bug572091.vala \
        structs/bug583603.vala \
        structs/bug595587.vala \
+       structs/bug596144.vala \
        structs/bug606202.vala \
        structs/bug609642.vala \
        structs/bug613513.vala \
diff --git a/tests/structs/bug596144.vala b/tests/structs/bug596144.vala
new file mode 100644 (file)
index 0000000..67468de
--- /dev/null
@@ -0,0 +1,11 @@
+struct Foo {
+       Object o;
+}
+
+void main () {
+       var o = new Object ();
+       var foo = Foo () { o=o };
+       var bar = (owned) foo;
+       assert (foo.o == null);
+       assert (bar.o == o);
+}