]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codgen: Generalize CCodeBaseModule.get_this_interface_cexpression()
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 4 May 2021 07:21:10 +0000 (09:21 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 4 May 2021 07:21:10 +0000 (09:21 +0200)
codegen/valaccodebasemodule.vala

index 55435ea1cbdcbeddee4ab6f499fa5fbb60a410b2..340245414a750bb5736470929cbfe51df4ec6d82 100644 (file)
@@ -2558,20 +2558,35 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                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)));
+       public CCodeExpression get_this_interface_cexpression (Interface iface, TargetValue? instance = null) {
+               unowned Class? cl = current_class;
+               if (cl != null && cl.implements (iface)) {
+                       return new CCodeIdentifier ("%s_%s_parent_iface".printf (get_ccode_lower_case_name (cl), get_ccode_lower_case_name (iface)));
                }
 
-               if (!current_class.is_a (iface)) {
-                       Report.warning (current_class.source_reference, "internal: `%s' is not a `%s'", current_class.get_full_name (), iface.get_full_name ());
+               if (cl != null && !cl.is_a (iface)) {
+                       Report.warning (cl.source_reference, "internal: `%s' is not a `%s'", cl.get_full_name (), iface.get_full_name ());
                }
 
-               var vcast = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_INSTANCE_GET_INTERFACE"));
-               vcast.add_argument (get_this_cexpression ());
-               vcast.add_argument (new CCodeIdentifier (get_ccode_type_id (iface)));
-               vcast.add_argument (new CCodeIdentifier (get_ccode_type_name (iface)));
-               return vcast;
+               CCodeExpression cast;
+               if (instance != null) {
+                       var call = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_INSTANCE_GET_INTERFACE"));
+                       call.add_argument (get_cvalue_ (instance));
+                       call.add_argument (new CCodeIdentifier (get_ccode_type_id (iface)));
+                       call.add_argument (new CCodeIdentifier (get_ccode_type_name (iface)));
+                       cast = call;
+               } else if (get_this_type () != null) {
+                       var call = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_INSTANCE_GET_INTERFACE"));
+                       call.add_argument (get_this_cexpression ());
+                       call.add_argument (new CCodeIdentifier (get_ccode_type_id (iface)));
+                       call.add_argument (new CCodeIdentifier (get_ccode_type_name (iface)));
+                       cast = call;
+               } else {
+                       Report.error (null, "internal: missing instance");
+                       cast = null;
+                       assert_not_reached ();
+               }
+               return cast;
        }
 
        public CCodeExpression get_inner_error_cexpression () {