From: Rico Tzschichholz Date: Tue, 4 May 2021 07:21:10 +0000 (+0200) Subject: codgen: Generalize CCodeBaseModule.get_this_interface_cexpression() X-Git-Tag: 0.53.1~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ad53f259ae15435deb1534f2450e1b8949d73dc;p=thirdparty%2Fvala.git codgen: Generalize CCodeBaseModule.get_this_interface_cexpression() --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 55435ea1c..340245414 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -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 () {