[vala] Allow using lambdas within enums.



commit fe9beb82b68090ce8f7557c05029fb50ecccb02b
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Fri Aug 23 22:54:42 2013 +0200

    Allow using lambdas within enums.
    
    Fixes bug 659778

 codegen/valaccodebasemodule.vala   |   12 +++++++-----
 codegen/valaccodemethodmodule.vala |    2 +-
 tests/delegates/bug659778.vala     |   26 +++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 7 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 3af2c2d..b3680d5 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1858,7 +1858,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                data.add_field ("Block%dData *".printf (parent_block_id), "_data%d_".printf 
(parent_block_id));
                        } else {
                                if (get_this_type () != null) {
-                                       data.add_field ("%s *".printf (get_ccode_name (current_type_symbol)), 
"self");
+                                       data.add_field (get_ccode_name (get_data_type_for_symbol 
(current_type_symbol)), "self");
                                }
 
                                if (current_method != null) {
@@ -2021,7 +2021,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                        if (get_this_type () != null) {
                                // assign "self" for type parameters
-                               ccode.add_declaration ("%s *".printf (get_ccode_name (current_type_symbol)), 
new CCodeVariableDeclarator ("self"));
+                               ccode.add_declaration(get_ccode_name (get_data_type_for_symbol 
(current_type_symbol)), new CCodeVariableDeclarator ("self"));
                                ccode.add_assignment (new CCodeIdentifier ("self"), new 
CCodeMemberAccess.pointer (outer_block, "self"));
                        }
 
@@ -2115,9 +2115,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                ccode.add_expression (unref_call);
                                ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier 
("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), new CCodeConstant ("NULL"));
                        } else {
-                               if (get_this_type () != null) {
-                                       // reference count for self is not increased in finalizers
-                                       if (!is_in_destructor ()) {
+                               var this_type = get_this_type ();
+                               if (this_type != null) {
+                                       if (this_type.is_disposable () && !is_in_destructor ()) {
+                                               // reference count for self is not increased in finalizers
                                                var this_value = new GLibValue (get_data_type_for_symbol 
(current_type_symbol), new CCodeIdentifier ("self"), true);
                                                ccode.add_expression (destroy_value (this_value));
                                        }
@@ -5455,6 +5456,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        set_delegate_target (lambda, delegate_target);
                } else if (get_this_type () != null) {
                        CCodeExpression delegate_target = get_result_cexpression ("self");
+                       delegate_target = convert_to_generic_pointer (delegate_target, get_this_type ());
                        if (expr_owned || delegate_type.is_called_once) {
                                if (get_this_type () != null) {
                                        var ref_call = new CCodeFunctionCall (get_dup_func_expression 
(get_this_type (), lambda.source_reference));
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 8a4333c..d48c03e 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -496,7 +496,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                        // as closures have block data parameter
                                        if (m.binding == MemberBinding.INSTANCE) {
                                                var cself = new CCodeMemberAccess.pointer (new 
CCodeIdentifier ("_data%d_".printf (block_id)), "self");
-                                               ccode.add_declaration ("%s *".printf (get_ccode_name 
(current_type_symbol)), new CCodeVariableDeclarator ("self"));
+                                               ccode.add_declaration (get_ccode_name 
(get_data_type_for_symbol (current_type_symbol)), new CCodeVariableDeclarator ("self"));
                                                ccode.add_assignment (new CCodeIdentifier ("self"), cself);
                                        }
 
diff --git a/tests/delegates/bug659778.vala b/tests/delegates/bug659778.vala
index 3e60386..e73e3c1 100644
--- a/tests/delegates/bug659778.vala
+++ b/tests/delegates/bug659778.vala
@@ -3,7 +3,28 @@ delegate G DoSomething<G>(G g);
 void do_something<G> (DoSomething<G> f) {}
 
 enum TE {
-       T
+       T;
+       public void f() {
+               do_something<TE> ((x) => {
+                       switch (this) {
+                       case T:
+                               return T;
+                       default:
+                               assert_not_reached ();
+                       }
+        });
+       }
+       public void g(int i) {
+               do_something<TE> ((x) => {
+                       switch (this) {
+                       case T:
+                               i++;
+                               return T;
+                       default:
+                               assert_not_reached ();
+                       }
+               });
+       }
 }
 
 class Test {
@@ -22,6 +43,9 @@ class Test {
 }
 
 int main() {
+       TE t = TE.T;
+       t.f ();
+       t.g (0);
        Test t2 = new Test ();
        t2.f ();
        return 0;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]