]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
use weak local variables where appropriate default local variables to
authorJürg Billeter <j@bitron.ch>
Tue, 20 Mar 2007 15:44:43 +0000 (15:44 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Tue, 20 Mar 2007 15:44:43 +0000 (15:44 +0000)
2007-03-20  Jürg Billeter  <j@bitron.ch>

* vala/valaparser.vala, vala/valasemanticanalyzer.vala,
  vala/valamemorymanager.vala, vala/valacodegenerator.vala,
  vala/valacallback.vala, vala/valainvocationexpression.vala,
  vala/valamethod.vala, vala/valaobjectcreationexpression.vala: use weak
  local variables where appropriate
* vala/parser.y, vala/valasemanticanalyzer.vala: default local variables
  to strong reference
* vala/valasemanticanalyzer.vala: don't promote local variables from
  weak to strong reference
* vala/valacodegenerator.vala: warn when duplicating non-reference
  counted structs implicitly

svn path=/trunk/; revision=247

vala/ChangeLog
vala/vala/parser.y
vala/vala/valacallback.vala
vala/vala/valacodegenerator.vala
vala/vala/valainvocationexpression.vala
vala/vala/valamemorymanager.vala
vala/vala/valamethod.vala
vala/vala/valaobjectcreationexpression.vala
vala/vala/valaparser.vala
vala/vala/valasemanticanalyzer.vala

index 87b6ed72a955a02b9ab9da3de9b131f451a3d4b9..aaf99a885a593e4a5ef8caabfb69c0278584b64a 100644 (file)
@@ -1,3 +1,17 @@
+2007-03-20  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valaparser.vala, vala/valasemanticanalyzer.vala,
+         vala/valamemorymanager.vala, vala/valacodegenerator.vala,
+         vala/valacallback.vala, vala/valainvocationexpression.vala,
+         vala/valamethod.vala, vala/valaobjectcreationexpression.vala: use weak
+         local variables where appropriate
+       * vala/parser.y, vala/valasemanticanalyzer.vala: default local variables
+         to strong reference
+       * vala/valasemanticanalyzer.vala: don't promote local variables from
+         weak to strong reference
+       * vala/valacodegenerator.vala: warn when duplicating non-reference
+         counted structs implicitly
+
 2007-03-20  Jürg Billeter  <j@bitron.ch>
 
        * tests/testrunner.sh: run with /bin/bash due to bashism, patch by
index c4dca5d43add1c7fd8d17473fc7658d87e4dbf19..99be10c8f42008408a23a54e20f0790adb7e41e9 100644 (file)
@@ -1464,6 +1464,7 @@ local_variable_type
                $$ = vala_type_reference_new_from_expression ($1);
                g_object_unref ($1);
                g_object_unref (src);
+               vala_type_reference_set_takes_ownership ($$, TRUE);
                if ($2) {
                        vala_type_reference_set_non_null ($$, TRUE);
                }
index 2242e83ee18dccdcdd41e8dc494426081926b78f..b9023abe8bdf769fb424825685a5bbc48a7ea9ba 100644 (file)
@@ -98,7 +98,7 @@ public class Vala.Callback : DataType {
                }
                
                var method_params = m.get_parameters ();
-               var method_params_it = method_params;
+               weak List<weak FormalParameter> method_params_it = method_params;
                bool first = true;
                foreach (FormalParameter param in parameters) {
                        /* use first callback parameter as instance parameter if
index 5f22c6783a2f895cc45470d82fcfbda60425970f..e569abd8ce304a1198b61297e48f2a62d9e6543c 100644 (file)
@@ -3157,13 +3157,14 @@ public class Vala.CodeGenerator : CodeVisitor {
                bool ellipsis = false;
                
                var i = 1;
+               weak List<weak FormalParameter> params_it = params;
                foreach (Expression arg in expr.get_argument_list ()) {
                        /* explicitly use strong reference as ccall gets
                         * unrefed at end of inner block
                         */
                        ref CCodeExpression cexpr = (CCodeExpression) arg.ccodenode;
-                       if (params != null) {
-                               var param = (FormalParameter) params.data;
+                       if (params_it != null) {
+                               var param = (FormalParameter) params_it.data;
                                ellipsis = param.ellipsis;
                                if (!ellipsis) {
                                        if (param.type_reference.data_type != null
@@ -3199,12 +3200,12 @@ public class Vala.CodeGenerator : CodeVisitor {
                        ccall.add_argument (cexpr);
                        i++;
                        
-                       if (params != null) {
-                               params = params.next;
+                       if (params_it != null) {
+                               params_it = params_it.next;
                        }
                }
-               while (params != null) {
-                       var param = (FormalParameter) params.data;
+               while (params_it != null) {
+                       var param = (FormalParameter) params_it.data;
                        
                        if (param.ellipsis) {
                                ellipsis = true;
@@ -3232,7 +3233,7 @@ public class Vala.CodeGenerator : CodeVisitor {
                        ccall.add_argument ((CCodeExpression) param.default_expression.ccodenode);
                        i++;
                
-                       params = params.next;
+                       params_it = params_it.next;
                }
                
                if (m != null && m.instance && m.instance_last) {
@@ -3366,6 +3367,10 @@ public class Vala.CodeGenerator : CodeVisitor {
                if (expr.static_type.data_type.is_reference_counting ()) {
                        ref_function = expr.static_type.data_type.get_ref_function ();
                } else {
+                       if (expr.static_type.data_type != string_type.data_type) {
+                               // duplicating non-reference counted structs may cause side-effects (and performance issues)
+                               Report.warning (expr.source_reference, "duplicating %s instance, use weak variable or explicitly invoke copy method".printf (expr.static_type.data_type.name));
+                       }
                        ref_function = expr.static_type.data_type.get_dup_function ();
                }
        
@@ -3418,7 +3423,7 @@ public class Vala.CodeGenerator : CodeVisitor {
 
        public override void visit_end_object_creation_expression (ObjectCreationExpression! expr) {
                if (expr.symbol_reference == null) {
-                       // no construction method
+                       // no creation method
                        if (expr.type_reference.data_type is Class) {
                                var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_new"));
                                
@@ -3441,22 +3446,23 @@ public class Vala.CodeGenerator : CodeVisitor {
                                expr.ccodenode = ccall;
                        }
                } else {
-                       // use construction method
+                       // use creation method
                        var m = (Method) expr.symbol_reference.node;
                        var params = m.get_parameters ();
-       
+
                        var ccall = new CCodeFunctionCall (new CCodeIdentifier (m.get_cname ()));
-                       
+
                        bool ellipsis = false;
 
                        int i = 1;
+                       weak List<weak FormalParameter> params_it = params;
                        foreach (Expression arg in expr.get_argument_list ()) {
                                /* explicitly use strong reference as ccall gets
                                 * unrefed at end of inner block
                                 */
                                ref CCodeExpression cexpr = (CCodeExpression) arg.ccodenode;
-                               if (params != null) {
-                                       var param = (FormalParameter) params.data;
+                               if (params_it != null) {
+                                       var param = (FormalParameter) params_it.data;
                                        ellipsis = param.ellipsis;
                                        if (!param.ellipsis
                                            && param.type_reference.data_type != null
@@ -3473,12 +3479,12 @@ public class Vala.CodeGenerator : CodeVisitor {
                                ccall.add_argument (cexpr);
                                i++;
                                
-                               if (params != null) {
-                                       params = params.next;
+                               if (params_it != null) {
+                                       params_it = params_it.next;
                                }
                        }
-                       while (params != null) {
-                               var param = (FormalParameter) params.data;
+                       while (params_it != null) {
+                               var param = (FormalParameter) params_it.data;
                                
                                if (param.ellipsis) {
                                        ellipsis = true;
@@ -3498,7 +3504,7 @@ public class Vala.CodeGenerator : CodeVisitor {
                                ccall.add_argument ((CCodeExpression) param.default_expression.ccodenode);
                                i++;
                        
-                               params = params.next;
+                               params_it = params_it.next;
                        }
 
                        if (ellipsis) {
index fafa375f43f5a0b8bd1232a0482d30bfbd3f00d1..8255d76e338438de00a30a2642e725a15ca6ee0f 100644 (file)
@@ -92,7 +92,7 @@ public class Vala.InvocationExpression : Expression {
                        call = (Expression) new_node;
                }
                
-               List l = argument_list.find (old_node);
+               weak List<Expression> l = argument_list.find (old_node);
                if (l != null) {
                        if (new_node.parent_node != null) {
                                return;
index ac6103b2824227d7a258f07f299bc2bcbb43f9b5..acab38c5502d03617d04a3a26e060e50afb85373 100644 (file)
@@ -147,9 +147,10 @@ public class Vala.MemoryManager : CodeVisitor {
                        var sig = (Signal) msym.node;
                        params = sig.get_parameters ();
                }
+               weak List<weak FormalParameter> params_it = params;
                foreach (Expression arg in expr.get_argument_list ()) {
-                       if (params != null) {
-                               var param = (FormalParameter) params.data;
+                       if (params_it != null) {
+                               var param = (FormalParameter) params_it.data;
                                if (!param.ellipsis
                                    && ((param.type_reference.data_type != null
                                    && param.type_reference.data_type.is_reference_type ())
@@ -226,7 +227,7 @@ public class Vala.MemoryManager : CodeVisitor {
                                        visit_possibly_leaked_expression (arg);
                                }
 
-                               params = params.next;
+                               params_it = params_it.next;
                        } else {
                                visit_possibly_leaked_expression (arg);
                        }
index ee302139410b1ecced1171ce8ab0ea858bcd4dfc..91c88081a3ecae8bd63344118b506c2a455e9946 100644 (file)
@@ -293,7 +293,7 @@ public class Vala.Method : Member, Invokable {
                }
                
                var method_params = m2.get_parameters ();
-               var method_params_it = method_params;
+               weak List<weak FormalParameter> method_params_it = method_params;
                foreach (FormalParameter param in parameters) {
                        /* method may not expect less arguments */
                        if (method_params_it == null) {
index 589ec8facc46cf274d68b25c45ddced238a3d36f..fa81d93a9ee6528185a11f5eca0d91ba085e8f51 100644 (file)
@@ -96,7 +96,7 @@ public class Vala.ObjectCreationExpression : Expression {
        }
 
        public override void replace (CodeNode! old_node, CodeNode! new_node) {
-               List l = argument_list.find (old_node);
+               weak List<Expression> l = argument_list.find (old_node);
                if (l != null) {
                        if (new_node.parent_node != null) {
                                return;
index c8749fd6b7386a9c0448fb09c523d5e67fa5293e..16144680b35e89902c0ccb8bf44e7af26b4a606f 100644 (file)
@@ -81,7 +81,7 @@ public class Vala.Parser : CodeVisitor {
                String result = new String (comment);
                comment = null;
                
-               string index;
+               weak string index;
                while ((index = result.str.chr (-1, '\t')) != null) {
                        result.erase (result.str.pointer_to_offset (index), 1);
                }
index 766802c51f857440c29d8bc1968e292344d44a28..7e5b537201e26f4abdc38a4130ca1e4dbea214b7 100644 (file)
@@ -119,7 +119,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        }
        
        private ref List<DataType> get_all_prerequisites (Interface! iface) {
-               List<DataType> ret = null;
+               weak List<DataType> ret = null;
                
                foreach (TypeReference prereq in iface.get_prerequisites ()) {
                        DataType type = prereq.data_type;
@@ -593,7 +593,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                        }
                        
                        decl.type_reference = decl.initializer.static_type.copy ();
-                       decl.type_reference.takes_ownership = decl.type_reference.transfers_ownership;
+                       decl.type_reference.takes_ownership = (decl.type_reference.data_type == null || decl.type_reference.data_type.is_reference_type ());
                        decl.type_reference.transfers_ownership = false;
                }
                
@@ -1182,7 +1182,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                }
        
                var args = expr.get_argument_list ();
-               List arg_it = args;
+               weak List<weak Expression> arg_it = args;
                foreach (FormalParameter param in params) {
                        if (param.ellipsis) {
                                break;
@@ -1200,8 +1200,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        }
        
        private bool check_arguments (Expression! expr, Symbol! msym, List<FormalParameter> params, List<Expression> args) {
-               List prev_arg_it = null;
-               List arg_it = args;
+               weak List<weak Expression> prev_arg_it = null;
+               weak List<weak Expression> arg_it = args;
                
                bool diag = (msym.node.get_attribute ("Diagnostics") != null);
                
@@ -1910,7 +1910,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                l.method.symbol.parent_symbol = current_symbol;
                
                var lambda_params = l.get_parameters ();
-               var lambda_param_it = lambda_params;
+               weak List<weak FormalParameter> lambda_param_it = lambda_params;
                foreach (FormalParameter cb_param in cb.get_parameters ()) {
                        if (lambda_param_it == null) {
                                /* lambda expressions are allowed to have less parameters */
@@ -2125,14 +2125,9 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                                        if (a.right.static_type.transfers_ownership) {
                                                /* rhs transfers ownership of the expression */
                                                if (!a.left.static_type.takes_ownership) {
-                                                       /* lhs doesn't own the value
-                                                        * promote lhs type if it is a local variable
-                                                        * error if it's not a local variable */
-                                                       if (!(ma.symbol_reference.node is VariableDeclarator)) {
-                                                               Report.error (a.source_reference, "Invalid assignment from owned expression to unowned variable");
-                                                       }
-                                                       
-                                                       a.left.static_type.takes_ownership = true;
+                                                       /* lhs doesn't own the value */
+                                                       a.error = true;
+                                                       Report.error (a.source_reference, "Invalid assignment from owned expression to unowned variable");
                                                }
                                        } else if (a.left.static_type.takes_ownership) {
                                                /* lhs wants to own the value