]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use get_this_type() to simplify several if statements
authorLuca Bruno <lucabru@src.gnome.org>
Sat, 10 Sep 2011 09:00:30 +0000 (11:00 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Sat, 10 Sep 2011 14:17:37 +0000 (16:17 +0200)
codegen/valaccodebasemodule.vala
codegen/valaccodedelegatemodule.vala
codegen/valaccodememberaccessmodule.vala
codegen/valaccodemethodcallmodule.vala
codegen/valaccodemethodmodule.vala
codegen/valagobjectmodule.vala

index 5b66896133e0534348ec7f07ed63310d46f31526..17e1abe8fd2d7c838ae60dfd3bcbfeb19b12c1b4 100644 (file)
@@ -116,6 +116,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                }
        }
 
+       public Constructor? current_constructor {
+               get {
+                       var sym = current_symbol;
+                       while (sym is Block) {
+                               sym = sym.parent_symbol;
+                       }
+                       return sym as Constructor;
+               }
+       }
+
        public DataType? current_return_type {
                get {
                        var m = current_method;
@@ -236,8 +246,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
        public int next_regex_id = 0;
        public bool in_creation_method { get { return current_method is CreationMethod; } }
-       public bool in_constructor = false;
-       public bool in_static_or_class_context = false;
 
        public bool current_method_inner_error {
                get { return emit_context.current_method_inner_error; }
@@ -1743,8 +1751,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                                data.add_field ("Block%dData *".printf (parent_block_id), "_data%d_".printf (parent_block_id));
                        } else {
-                               if (in_constructor || (current_method != null && current_method.binding == MemberBinding.INSTANCE) ||
-                                          (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE)) {
+                               if (get_this_type () != null) {
                                        data.add_field ("%s *".printf (get_ccode_name (current_type_symbol)), "self");
                                }
 
@@ -1806,13 +1813,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                                ccode.add_assignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), ref_call);
                        } else {
-                               bool in_instance_method = (current_method != null && current_method.binding == MemberBinding.INSTANCE);
-                               bool in_instance_property = (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE);
                                // skip self assignment in toplevel block of creation methods with chainup as self is not set at the beginning of the method
                                // the chainup statement takes care of assigning self in the closure struct
                                bool in_creation_method_with_chainup = (current_method is CreationMethod && current_class != null && current_class.base_class != null);
 
-                               if (in_constructor || (in_instance_method && (!in_creation_method_with_chainup || current_method.body != b)) || in_instance_property) {
+                               if (get_this_type () != null && (!in_creation_method_with_chainup || current_method.body != b)) {
                                        var ref_call = new CCodeFunctionCall (get_dup_func_expression (get_data_type_for_symbol (current_type_symbol), b.source_reference));
                                        ref_call.add_argument (get_result_cexpression ("self"));
 
@@ -1896,8 +1901,7 @@ 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 (in_constructor || (current_method != null && current_method.binding == MemberBinding.INSTANCE) ||
-                                          (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE)) {
+                               if (get_this_type () != null) {
                                        var this_value = new GLibValue (get_data_type_for_symbol (current_type_symbol), new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "self"), true);
                                        ccode.add_expression (destroy_value (this_value));
                                }
@@ -3402,9 +3406,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                } else if (member.is_class_member ()) {
                        CCodeExpression klass;
 
-                       if (current_method != null && current_method.binding == MemberBinding.INSTANCE ||
-                           current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE ||
-                           (in_constructor && !in_static_or_class_context)) {
+                       if (get_this_type () != null) {
                                var k = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_GET_CLASS"));
                                k.add_argument (new CCodeIdentifier ("self"));
                                klass = k;
@@ -4995,7 +4997,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                set_delegate_target_destroy_notify (lambda, new CCodeConstant ("NULL"));
                        }
                        set_delegate_target (lambda, delegate_target);
-               } else if (get_this_type () != null || in_constructor) {
+               } else if (get_this_type () != null) {
                        CCodeExpression delegate_target = get_result_cexpression ("self");
                        if (expr_owned || delegate_type.is_called_once) {
                                if (get_this_type () != null) {
@@ -5899,6 +5901,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        return current_method.this_parameter.variable_type;
                } else if (current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE) {
                        return current_property_accessor.prop.this_parameter.variable_type;
+               } else if (current_constructor != null && current_constructor.binding == MemberBinding.INSTANCE) {
+                       return current_constructor.this_parameter.variable_type;
                }
                return null;
        }
index 6975e8ee604b7c508c3de190fed35d2b5d448804..0c2242d6bcf7ef97b5ca8c246d040f214e40642a 100644 (file)
@@ -416,8 +416,6 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
                                destroy_notify = new CCodeIdentifier ("block%d_data_unref".printf (block_id));
                        } else if (get_this_type () != null && m.binding != MemberBinding.STATIC && !m.is_async_callback && is_reference_counting (m.this_parameter.variable_type.data_type)) {
                                destroy_notify = get_destroy_func_expression (m.this_parameter.variable_type);
-                       } else if (in_constructor) {
-                               destroy_notify = new CCodeIdentifier ("g_object_unref");
                        }
 
                        if (destroy_notify != null) {
index 349a96ead1fe4f2e40b2ffdd3416dfba5383f462..c4f75aa88f353e73a933e89f971804574af641ac 100644 (file)
@@ -587,7 +587,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 
                        CCodeExpression klass;
                        if (instance == null) {
-                               if (in_static_or_class_context) {
+                               if (get_this_type () == null) {
                                        // Accessing the field from a static or class constructor
                                        klass = new CCodeIdentifier ("klass");
                                } else {
index d9ae9a626a0cf548d4c5eb612b52c86ac25c89e0..04cecfdcd1dcf365cdd3caef9136c51b5b2d879c 100644 (file)
@@ -207,7 +207,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        
                        CCodeExpression klass;
                        if (ma.inner == null) {
-                               if (in_static_or_class_context) {
+                               if (get_this_type () == null) {
                                        // Accessing the method from a static or class constructor
                                        klass = new CCodeIdentifier ("klass");
                                } else {
index 8550f29f294cf5ba7e72e654eba360fd422665c5..a5be3346544fc7e5fe05a20e7934d7af24b33782 100644 (file)
@@ -283,11 +283,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        creturn_type = new VoidType ();
                }
 
-               if (m.binding == MemberBinding.CLASS || m.binding == MemberBinding.STATIC) {
-                       in_static_or_class_context = true;
-               }
-
-
                foreach (Parameter param in m.get_parameters ()) {
                        param.accept (this);
                }
@@ -660,9 +655,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        cfile.add_function (ccode);
                }
 
-
-               in_static_or_class_context = false;
-
                pop_context ();
 
                if ((m.is_abstract || m.is_virtual) && !m.coroutine &&
index 08f0af979b9c40e7e5d774954f2049642d42d554..a22a8b2c51550e6ca4686166de546ed74e3fb5a9 100644 (file)
@@ -402,12 +402,6 @@ public class Vala.GObjectModule : GTypeModule {
        public override void visit_constructor (Constructor c) {
                push_line (c.source_reference);
 
-               if (c.binding == MemberBinding.CLASS || c.binding == MemberBinding.STATIC) {
-                       in_static_or_class_context = true;
-               } else {
-                       in_constructor = true;
-               }
-
                var cl = (Class) c.parent_symbol;
 
                if (c.binding == MemberBinding.INSTANCE) {
@@ -511,10 +505,6 @@ public class Vala.GObjectModule : GTypeModule {
                        Report.error (c.source_reference, "internal error: constructors must have instance, class, or static binding");
                }
 
-               in_static_or_class_context = false;
-
-               in_constructor = false;
-
                pop_line ();
        }