[vala/staging] codegen: Add CCodeBaseModule.get_this_class_cexpression() and use where possible



commit 9cbd53b8d79aa4c394ddf49e766f2c15fc476906
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon May 3 21:12:38 2021 +0200

    codegen: Add CCodeBaseModule.get_this_class_cexpression() and use where possible

 codegen/valaccodebasemodule.vala         | 35 +++++++++++++++++++++-----------
 codegen/valaccodememberaccessmodule.vala | 26 +++---------------------
 codegen/valaccodemethodcallmodule.vala   | 24 ++--------------------
 3 files changed, 28 insertions(+), 57 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index d9eb216c7..55435ea1c 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2539,6 +2539,25 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                return get_cexpression ("self");
        }
 
+       public CCodeExpression get_this_class_cexpression (Class cl, TargetValue? instance = null) {
+               CCodeExpression cast;
+               if (instance != null) {
+                       // Accessing the member of an instance
+                       var call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_type_get_function 
(cl)));
+                       call.add_argument (get_cvalue_ (instance));
+                       cast = call;
+               } else if (get_this_type () != null) {
+                       // Accessing the member from within an instance method
+                       var call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_type_get_function 
(cl)));
+                       call.add_argument (get_this_cexpression ());
+                       cast = call;
+               } else {
+                       // Accessing the member from a static or class constructor
+                       cast = new CCodeIdentifier ("klass");
+               }
+               return cast;
+       }
+
        public CCodeExpression get_this_interface_cexpression (Interface iface) {
                if (current_class.implements (iface)) {
                        return new CCodeIdentifier ("%s_%s_parent_iface".printf (get_ccode_lower_case_name 
(current_class), get_ccode_lower_case_name (iface)));
@@ -4207,18 +4226,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        l = get_cvalue (((MemberAccess) resource).inner);
                        l = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (l, "priv"), 
get_symbol_lock_name (get_ccode_name (member)));
                } else if (member.is_class_member ()) {
-                       CCodeExpression klass;
-
-                       if (get_this_type () != null) {
-                               var k = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_type_get_function ((Class) parent))));
-                               k.add_argument (new CCodeIdentifier ("self"));
-                               klass = k;
-                       } else {
-                               klass = new CCodeIdentifier ("klass");
-                       }
-
-                       var get_class_private_call = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_class_get_private_function ((Class) parent)));
-                       get_class_private_call.add_argument (klass);
+                       unowned Class cl = (Class) parent;
+                       var cast = get_this_class_cexpression (cl);
+                       var get_class_private_call = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_class_get_private_function (cl)));
+                       get_class_private_call.add_argument (cast);
                        l = new CCodeMemberAccess.pointer (get_class_private_call, get_symbol_lock_name 
(get_ccode_name (member)));
                } else {
                        string lock_name = "%s_%s".printf (get_ccode_lower_case_name (parent), get_ccode_name 
(member));
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index b1f130eb9..944aa3ea8 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -702,31 +702,11 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                }
                        }
                } else if (field.binding == MemberBinding.CLASS) {
-                       var cl = (Class) field.parent_symbol;
-                       var cast = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_class_type_function 
(cl)));
-
-                       CCodeExpression klass;
-                       if (instance == null) {
-                               if (get_this_type () == null) {
-                                       // Accessing the field from a static or class constructor
-                                       klass = new CCodeIdentifier ("klass");
-                               } else {
-                                       // Accessing the field from within an instance method
-                                       var k = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_type_get_function (cl)));
-                                       k.add_argument (new CCodeIdentifier ("self"));
-                                       klass = k;
-                               }
-                       } else {
-                               // Accessing the field of an instance
-                               var k = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_type_get_function (cl)));
-                               k.add_argument (get_cvalue_ (instance));
-                               klass = k;
-                       }
-                       cast.add_argument (klass);
-
+                       unowned Class cl = (Class) field.parent_symbol;
+                       var cast = get_this_class_cexpression (cl, instance);
                        if (field.access == SymbolAccessibility.PRIVATE) {
                                var ccall = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_class_get_private_function (cl)));
-                               ccall.add_argument (klass);
+                               ccall.add_argument (cast);
                                result.cvalue = new CCodeMemberAccess.pointer (ccall, get_ccode_name (field));
                        } else {
                                result.cvalue = new CCodeMemberAccess.pointer (cast, get_ccode_name (field));
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index 8f9a197b9..399a05721 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -289,28 +289,8 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                                in_arg_map.set (get_param_pos (get_ccode_instance_pos (m)), instance);
                        }
                } else if (m != null && m.binding == MemberBinding.CLASS) {
-                       var cl = (Class) m.parent_symbol;
-                       var cast = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_class_type_function 
(cl)));
-
-                       CCodeExpression klass;
-                       if (ma.inner == null) {
-                               if (get_this_type () == null) {
-                                       // Accessing the method from a static or class constructor
-                                       klass = new CCodeIdentifier ("klass");
-                               } else {
-                                       // Accessing the method from within an instance method
-                                       var k = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_type_get_function (cl));
-                                       k.add_argument (get_this_cexpression ());
-                                       klass = k;
-                               }
-                       } else {
-                               // Accessing the method of an instance
-                               var k = new CCodeFunctionCall (new CCodeIdentifier 
(get_ccode_type_get_function (cl)));
-                               k.add_argument (get_cvalue (ma.inner));
-                               klass = k;
-                       }
-
-                       cast.add_argument (klass);
+                       unowned Class cl = (Class) m.parent_symbol;
+                       var cast = get_this_class_cexpression (cl, ma.inner != null ? ma.inner.target_value : 
null);
                        in_arg_map.set (get_param_pos (get_ccode_instance_pos (m)), cast);
                        out_arg_map.set (get_param_pos (get_ccode_instance_pos (m)), cast);
                }


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