]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use store_local in visit_local_variable
authorJürg Billeter <j@bitron.ch>
Wed, 19 Jan 2011 20:37:47 +0000 (21:37 +0100)
committerJürg Billeter <j@bitron.ch>
Wed, 19 Jan 2011 21:04:23 +0000 (22:04 +0100)
Based on patch by Luca Bruno.

codegen/valaccodeassignmentmodule.vala
codegen/valaccodebasemodule.vala
codegen/valadovaassignmentmodule.vala
vala/valacodegenerator.vala

index 9c53c5ac40672ce4a2ffea976e0b8d9addbd8061..cf8e217db50ec0b8b8188754a475c0247327792d 100644 (file)
@@ -171,8 +171,8 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
                }
        }
 
-       void store_variable (Variable variable, TargetValue lvalue, TargetValue value) {
-               if (requires_destroy (variable.variable_type)) {
+       void store_variable (Variable variable, TargetValue lvalue, TargetValue value, bool initializer) {
+               if (!initializer && requires_destroy (variable.variable_type)) {
                        /* unref old value */
                        ccode.add_expression (destroy_value (lvalue));
                }
@@ -181,17 +181,33 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
 
                var array_type = variable.variable_type as ArrayType;
                if (array_type != null) {
-                       for (int dim = 1; dim <= array_type.rank; dim++) {
-                               if (get_array_length_cvalue (lvalue, dim) != null) {
-                                       ccode.add_assignment (get_array_length_cvalue (lvalue, dim), get_array_length_cvalue (value, dim));
+                       if (array_type.fixed_length) {
+                               cfile.add_include ("string.h");
+
+                               // it is necessary to use memcpy for fixed-length (stack-allocated) arrays
+                               // simple assignments do not work in C
+                               var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+                               sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
+                               var size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call);
+
+                               var ccopy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
+                               ccopy.add_argument (get_cvalue_ (lvalue));
+                               ccopy.add_argument (get_cvalue_ (value));
+                               ccopy.add_argument (size);
+                               ccode.add_expression (ccopy);
+                       } else {
+                               for (int dim = 1; dim <= array_type.rank; dim++) {
+                                       if (get_array_length_cvalue (lvalue, dim) != null) {
+                                               ccode.add_assignment (get_array_length_cvalue (lvalue, dim), get_array_length_cvalue (value, dim));
+                                       }
                                }
-                       }
-                       if (array_type.rank == 1) {
-                               if (get_array_size_cvalue (lvalue) != null) {
-                                       if (get_array_size_cvalue (value) != null) {
-                                               ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_size_cvalue (value));
-                                       } else {
-                                               ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_length_cvalue (value, 1));
+                               if (array_type.rank == 1) {
+                                       if (get_array_size_cvalue (lvalue) != null) {
+                                               if (get_array_size_cvalue (value) != null) {
+                                                       ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_size_cvalue (value));
+                                               } else {
+                                                       ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_length_cvalue (value, 1));
+                                               }
                                        }
                                }
                        }
@@ -208,7 +224,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
                }
        }
 
-       public override void store_local (LocalVariable local, TargetValue value) {
-               store_variable (local, get_local_cvalue (local), value);
+       public override void store_local (LocalVariable local, TargetValue value, bool initializer) {
+               store_variable (local, get_local_cvalue (local), value, initializer);
        }
 }
index 09fc4e58c6c22c86240a6adcdf81711d8f533f8e..c0c249092c91c17e5e9cea8b10ff3f1e5face14e 100644 (file)
@@ -2066,86 +2066,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                }
        
                if (rhs != null) {
-                       var target_value = get_variable_cvalue (local);
-
-                       if (local.variable_type is ArrayType) {
-                               var array_type = (ArrayType) local.variable_type;
-
-                               if (array_type.fixed_length) {
-                                       rhs = null;
-                               } else {
-                                       for (int dim = 1; dim <= array_type.rank; dim++) {
-                                               var lhs_array_len = get_array_length_cvalue (target_value, dim);
-                                               var rhs_array_len = get_array_length_cexpression (local.initializer, dim);
-                                               ccode.add_assignment (lhs_array_len, rhs_array_len);
-                                       }
-                                       if (array_type.rank == 1 && !local.captured) {
-                                               var lhs_array_size = get_array_size_cvalue (target_value);
-                                               var rhs_array_len = get_array_length_cvalue (target_value, 1);
-                                               ccode.add_assignment (lhs_array_size, rhs_array_len);
-                                       }
-                               }
-                       } else if (local.variable_type is DelegateType) {
-                               var deleg_type = (DelegateType) local.variable_type;
-                               var d = deleg_type.delegate_symbol;
-                               if (d.has_target) {
-                                       var lhs_delegate_target = get_delegate_target_cvalue (target_value);
-                                       var lhs_delegate_target_destroy_notify = get_delegate_target_destroy_notify_cvalue (target_value);
-
-                                       CCodeExpression rhs_delegate_target_destroy_notify;
-                                       var rhs_delegate_target = get_delegate_target_cexpression (local.initializer, out rhs_delegate_target_destroy_notify);
-                                       ccode.add_assignment (lhs_delegate_target, rhs_delegate_target);
-
-                                       if (deleg_type.value_owned) {
-                                               ccode.add_assignment (lhs_delegate_target_destroy_notify, rhs_delegate_target_destroy_notify);
-                                       }
-                               }
-                       }
-               }
-
-               if (local.captured) {
-                       if (local.initializer != null) {
-                               if (has_simple_struct_initializer (local)) {
-                                       ccode.add_expression (rhs);
-                               } else {
-                                       ccode.add_assignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id ((Block) local.parent_symbol))), get_variable_cname (local.name)), rhs);
-                               }
-                       }
-               } else if (current_method != null && current_method.coroutine) {
-                       if (local.initializer != null) {
-                               if (has_simple_struct_initializer (local)) {
-                                       ccode.add_expression (rhs);
-                               } else {
-                                       ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (local.name)), rhs);
-                               }
-                       }
-               } else {
-                       if (rhs != null) {
-                               if (has_simple_struct_initializer (local)) {
-                                       ccode.add_expression (rhs);
-                               } else {
-                                       ccode.add_assignment (get_variable_cexpression (local.name), rhs);
-                               }
-                       }
-               }
-
-               if (local.initializer != null && local.variable_type is ArrayType) {
-                       var array_type = (ArrayType) local.variable_type;
-
-                       if (array_type.fixed_length) {
-                               cfile.add_include ("string.h");
-
-                               // it is necessary to use memcpy for fixed-length (stack-allocated) arrays
-                               // simple assignments do not work in C
-                               var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
-                               sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
-                               var size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call);
-
-                               var ccopy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
-                               ccopy.add_argument (get_variable_cexpression (local.name));
-                               ccopy.add_argument (get_cvalue (local.initializer));
-                               ccopy.add_argument (size);
-                               ccode.add_expression (ccopy);
+                       if (has_simple_struct_initializer (local)) {
+                               ccode.add_expression (rhs);
+                       } else {
+                               store_local (local, local.initializer.target_value, true);
                        }
                }
 
index cf94b79d16fb3406d9d54ea60422005029332737..837c873a72aae6d97cca021dd583e6f8a170f419 100644 (file)
@@ -132,8 +132,8 @@ public class Vala.DovaAssignmentModule : DovaMemberAccessModule {
                }
        }
 
-       void store_variable (Variable variable, TargetValue lvalue, TargetValue value) {
-               if (requires_destroy (variable.variable_type)) {
+       void store_variable (Variable variable, TargetValue lvalue, TargetValue value, bool initializer) {
+               if (!initializer && requires_destroy (variable.variable_type)) {
                        /* unref old value */
                        ccode.add_expression (destroy_value (lvalue));
                }
@@ -141,7 +141,7 @@ public class Vala.DovaAssignmentModule : DovaMemberAccessModule {
                ccode.add_assignment (get_cvalue_ (lvalue), get_cvalue_ (value));
        }
 
-       public override void store_local (LocalVariable local, TargetValue value) {
-               store_variable (local, get_local_cvalue (local), value);
+       public override void store_local (LocalVariable local, TargetValue value, bool initializer) {
+               store_variable (local, get_local_cvalue (local), value, initializer);
        }
 }
index 9c33fcd08e408a134409f9a11f2420fdb3157d95..65624f4fbb862e0c61d4108acacaca64e8381788 100644 (file)
@@ -36,5 +36,5 @@ public abstract class Vala.CodeGenerator : CodeVisitor {
 
        public abstract TargetValue load_local (LocalVariable local);
 
-       public abstract void store_local (LocalVariable local, TargetValue value);
+       public abstract void store_local (LocalVariable local, TargetValue value, bool initializer);
 }