]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
call visit_end_full_expression for collection expression return array
authorJuerg Billeter <j@bitron.ch>
Wed, 11 Jul 2007 13:30:02 +0000 (13:30 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Wed, 11 Jul 2007 13:30:02 +0000 (13:30 +0000)
2007-07-11  Juerg Billeter  <j@bitron.ch>

* vala/valaforeachstatement.vala: call visit_end_full_expression for
  collection expression
* gobject/valacodegenerator.vala, gobject/valacodegeneratormethod.vala:
  return array length if appropriate
* tests/test-032.vala, tests/test-032.out: test array as return value
* tests/Makefile.am: update

Fixes bug 453676

svn path=/trunk/; revision=344

ChangeLog
gobject/valacodegenerator.vala
gobject/valacodegeneratormethod.vala
tests/Makefile.am
tests/test-032.out [new file with mode: 0644]
tests/test-032.vala [new file with mode: 0644]
vala/valaforeachstatement.vala

index a6250e350e420dbff4f60d04d9ec30ab7010c468..a7d4344b4c53830ae28e2f8323d48ec848aba25f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-07-11  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valaforeachstatement.vala: call visit_end_full_expression for
+         collection expression
+       * gobject/valacodegenerator.vala, gobject/valacodegeneratormethod.vala:
+         return array length if appropriate
+       * tests/test-032.vala, tests/test-032.out: test array as return value
+       * tests/Makefile.am: update
+
+       Fixes bug 453676
+
 2007-07-11  Jürg Billeter  <j@bitron.ch>
 
        * vala/valasemanticanalyzer.vala: any reference or array type or pointer
index 0e26fcbf85309d175fcc9dd49b063bc6a58c6142..70faaaa9b86035f90b2e0a862b91570f51c38b87 100644 (file)
@@ -38,6 +38,7 @@ public class Vala.CodeGenerator : CodeVisitor {
        Symbol current_symbol;
        Symbol current_type_symbol;
        Class current_class;
+       Method current_method;
        TypeReference current_return_type;
 
        CCodeFragment header_begin;
@@ -1398,7 +1399,28 @@ public class Vala.CodeGenerator : CodeVisitor {
                                        return_expression_symbol.active = false;
                                }
                        }
-               
+
+                       // return array length if appropriate
+                       if (current_method != null && !current_method.no_array_length && current_return_type.data_type is Array) {
+                               var return_expr_decl = get_temp_variable_declarator (stmt.return_expression.static_type);
+
+                               var ccomma = new CCodeCommaExpression ();
+                               ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (return_expr_decl.name), (CCodeExpression) stmt.return_expression.ccodenode));
+
+                               var arr = (Array) current_return_type.data_type;
+
+                               for (int dim = 1; dim <= arr.rank; dim++) {
+                                       var len_l = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_array_length_cname ("result", dim)));
+                                       var len_r = get_array_length_cexpression (stmt.return_expression, dim);
+                                       ccomma.append_expression (new CCodeAssignment (len_l, len_r));
+                               }
+
+                               ccomma.append_expression (new CCodeIdentifier (return_expr_decl.name));
+                               
+                               stmt.return_expression.ccodenode = ccomma;
+                               stmt.return_expression.temp_vars.append (return_expr_decl);
+                       }
+
                        create_local_free_expr (stmt.return_expression);
                        
                        if (stmt.return_expression.static_type != null &&
index 211ab3782091cfdc85df42e663861ae35d836f70..62d2880c23e83c9236b836fc9ce311b227ac359b 100644 (file)
@@ -25,7 +25,10 @@ using GLib;
 
 public class Vala.CodeGenerator {
        public override void visit_method (Method! m) {
+               Method old_method = current_method;
+               TypeReference old_return_type = current_return_type;
                current_symbol = m.symbol;
+               current_method = m;
                current_return_type = m.return_type;
 
                if (m is CreationMethod) {
@@ -43,7 +46,8 @@ public class Vala.CodeGenerator {
                }
 
                current_symbol = current_symbol.parent_symbol;
-               current_return_type = null;
+               current_method = current_method;
+               current_return_type = old_return_type;
 
                if (current_type_symbol != null && current_type_symbol.node is Interface) {
                        var iface = (Interface) current_type_symbol.node;
@@ -52,13 +56,6 @@ public class Vala.CodeGenerator {
                        }
                }
 
-               if (current_symbol.parent_symbol != null &&
-                   current_symbol.parent_symbol.node is Method) {
-                       /* lambda expressions produce nested methods */
-                       var up_method = (Method) current_symbol.parent_symbol.node;
-                       current_return_type = up_method.return_type;
-               }
-
                function = new CCodeFunction (m.get_real_cname (), m.return_type.get_cname ());
                CCodeFunctionDeclarator vdeclarator = null;
                
@@ -128,7 +125,20 @@ public class Vala.CodeGenerator {
                                vdeclarator.add_parameter ((CCodeFormalParameter) param.ccodenode);
                        }
                }
-               
+
+               // return array length if appropriate
+               if (!m.no_array_length && m.return_type.data_type is Array) {
+                       var arr = (Array) m.return_type.data_type;
+
+                       for (int dim = 1; dim <= arr.rank; dim++) {
+                               var cparam = new CCodeFormalParameter (get_array_length_cname ("result", dim), "int*");
+                               function.add_parameter (cparam);
+                               if (vdeclarator != null) {
+                                       vdeclarator.add_parameter (cparam);
+                               }
+                       }
+               }
+
                if (m.instance && m.instance_last) {
                        function.add_parameter (instance_param);
                }
index 95c84995d0a846371f45abc8e700b6d2f9853782..2556c089b8c2cabbc8ebee51412b3da675afa0a8 100644 (file)
@@ -34,6 +34,7 @@ TESTS = \
        test-029.vala \
        test-030.vala \
        test-031.vala \
+       test-032.vala \
        $(NULL)
 
 EXTRA_DIST = \
@@ -100,4 +101,5 @@ EXTRA_DIST = \
        test-029.out \
        test-030.out \
        test-031.out \
+       test-032.out \
        $(NULL)
diff --git a/tests/test-032.out b/tests/test-032.out
new file mode 100644 (file)
index 0000000..3c6fbf1
--- /dev/null
@@ -0,0 +1 @@
+Array Test: 1 2 3 4 5
diff --git a/tests/test-032.vala b/tests/test-032.vala
new file mode 100644 (file)
index 0000000..72963a6
--- /dev/null
@@ -0,0 +1,24 @@
+using GLib;
+
+class Maman.Bar {
+       public int[] foo_numbers () {
+               return new int[3] { 2, 3, 4 };
+       }
+
+       public void run () {
+               foreach (int i in foo_numbers ()) {
+                       stdout.printf (" %d", i);
+               }
+       }
+
+       static int main (string[] args) {
+               stdout.printf ("Array Test: 1");
+               
+               var bar = new Bar ();
+               bar.run ();
+
+               stdout.printf (" 5\n");
+               
+               return 0;
+       }
+}
index f4ec9a9627746d6db6a89fc6f5ebb85d39756bc4..7c0055b775f75c506e1df5f7cdf28ddf9dd92dcc 100644 (file)
@@ -1,6 +1,6 @@
 /* valaforeachstatement.vala
  *
- * Copyright (C) 2006  Jürg Billeter
+ * Copyright (C) 2006-2007  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -83,7 +83,10 @@ public class Vala.ForeachStatement : Statement {
                visitor.visit_begin_foreach_statement (this);
 
                type_reference.accept (visitor);
+
                collection.accept (visitor);
+               visitor.visit_end_full_expression (collection);
+
                body.accept (visitor);
 
                visitor.visit_end_foreach_statement (this);