From dff819f93d2e19b2f56378f8e96bd519c87236d9 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Sat, 9 Nov 2019 15:20:11 +0100 Subject: [PATCH] codegen: Commonize the determining of return-type for delegates and methods --- codegen/valaccodebasemodule.vala | 16 ++++++++++++++ codegen/valaccodedelegatemodule.vala | 31 +++++++++------------------- codegen/valaccodemethodmodule.vala | 25 ++-------------------- codegen/valagasyncmodule.vala | 6 +----- codegen/valagtypemodule.vala | 20 +++++------------- 5 files changed, 34 insertions(+), 64 deletions(-) diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 615180dbe..ba3ab2d9c 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -6191,6 +6191,22 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { return generated_external_symbols.add (external_symbol); } + public static DataType get_callable_creturn_type (Callable c) { + assert (c is Method || c is Delegate); + var creturn_type = c.return_type.copy (); + if (c is CreationMethod) { + unowned Class? cl = c.parent_symbol as Class; + if (cl != null) { + // object creation methods return the new object in C + // in Vala they have no return type + creturn_type = new ObjectType (cl); + } + } else if (c.return_type.is_real_non_null_struct_type ()) { + // structs are returned via out parameter + creturn_type = new VoidType (); + } + return creturn_type; + } public CCodeExpression? default_value_for_type (DataType type, bool initializer_expression, bool on_error = false) { unowned Struct? st = type.type_symbol as Struct; diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala index 64a83411f..4f2b87f7f 100644 --- a/codegen/valaccodedelegatemodule.vala +++ b/codegen/valaccodedelegatemodule.vala @@ -39,20 +39,15 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule { generate_type_declaration (new DelegateType (d), decl_space); - string return_type_cname = get_ccode_name (d.return_type); + var creturn_type = get_callable_creturn_type (d); - if (d.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - return_type_cname = "void"; - } - - if (return_type_cname == get_ccode_name (d)) { + if (creturn_type is DelegateType && ((DelegateType) creturn_type).delegate_symbol == d) { // recursive delegate - return_type_cname = "GCallback"; - } else { - generate_type_declaration (d.return_type, decl_space); + creturn_type = new DelegateType ((Delegate) context.root.scope.lookup ("GLib").scope.lookup ("Callback")); } + generate_type_declaration (creturn_type, decl_space); + var cparam_map = new HashMap (direct_hash, direct_equal); var cfundecl = new CCodeFunctionDeclarator (get_ccode_name (d)); @@ -114,7 +109,7 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule { last_pos = min_pos; } - var ctypedef = new CCodeTypeDefinition (return_type_cname, cfundecl); + var ctypedef = new CCodeTypeDefinition (get_ccode_name (creturn_type), cfundecl); ctypedef.modifiers |= (d.version.deprecated ? CCodeModifiers.DEPRECATED : 0); decl_space.add_type_declaration (ctypedef); @@ -193,15 +188,9 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule { } // declaration + var creturn_type = get_callable_creturn_type (d); - string return_type_cname = get_ccode_name (d.return_type); - - if (d.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - return_type_cname = "void"; - } - - var function = new CCodeFunction (wrapper_name, return_type_cname); + var function = new CCodeFunction (wrapper_name, get_ccode_name (creturn_type)); function.modifiers = CCodeModifiers.STATIC; push_function (function); @@ -411,14 +400,14 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule { ccode.add_expression (ccall); if (!(d.return_type is VoidType || d.return_type.is_real_non_null_struct_type ())) { // return a default value - ccode.add_declaration (return_type_cname, new CCodeVariableDeclarator ("result", default_value_for_type (d.return_type, true))); + ccode.add_declaration (get_ccode_name (creturn_type), new CCodeVariableDeclarator ("result", default_value_for_type (d.return_type, true))); } } else { CCodeExpression result = ccall; if (d.return_type is GenericType) { result = convert_to_generic_pointer (result, m.return_type); } - ccode.add_declaration (return_type_cname, new CCodeVariableDeclarator ("result", result)); + ccode.add_declaration (get_ccode_name (creturn_type), new CCodeVariableDeclarator ("result", result)); } if (d.has_target /* TODO: && dt.value_owned */ && dt.is_called_once) { diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala index 860783b89..9eb868015 100644 --- a/codegen/valaccodemethodmodule.vala +++ b/codegen/valaccodemethodmodule.vala @@ -54,18 +54,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { } public virtual void generate_method_result_declaration (Method m, CCodeFile decl_space, CCodeFunction cfunc, Map cparam_map, Map? carg_map) { - var creturn_type = m.return_type; - if (m is CreationMethod) { - var cl = m.parent_symbol as Class; - if (cl != null) { - // object creation methods return the new object in C - // in Vala they have no return type - creturn_type = new ObjectType (cl); - } - } else if (m.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - creturn_type = new VoidType (); - } + var creturn_type = get_callable_creturn_type (m); cfunc.return_type = get_creturn_type (m, get_ccode_name (creturn_type)); generate_type_declaration (m.return_type, decl_space); @@ -342,11 +331,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { } } - var creturn_type = m.return_type; - if (m.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - creturn_type = new VoidType (); - } + var creturn_type = get_callable_creturn_type (m); foreach (Parameter param in m.get_parameters ()) { param.accept (this); @@ -762,12 +747,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { } if (m is CreationMethod) { - if (current_type_symbol is Class) { - creturn_type = new ObjectType (current_class); - } else { - creturn_type = new VoidType (); - } - if (current_type_symbol is Class && !m.coroutine) { CCodeExpression cresult = new CCodeIdentifier ("self"); if (get_ccode_type (m) != null) { diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala index e4e4edbf6..48f53d47e 100644 --- a/codegen/valagasyncmodule.vala +++ b/codegen/valagasyncmodule.vala @@ -671,11 +671,7 @@ public class Vala.GAsyncModule : GtkModule { return; } - var creturn_type = m.return_type; - if (m.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - creturn_type = new VoidType (); - } + var creturn_type = get_callable_creturn_type (m); // add vfunc field to the type struct var vdeclarator = new CCodeFunctionDeclarator (get_ccode_vfunc_name (m)); diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index 67c36a537..f1c775264 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -343,13 +343,10 @@ public class Vala.GTypeModule : GErrorModule { if (prop.get_accessor != null) { var vdeclarator = new CCodeFunctionDeclarator ("get_%s".printf (prop.name)); vdeclarator.add_parameter (cselfparam); - string creturn_type; + var creturn_type = get_callable_creturn_type (prop.get_accessor.get_method ()); if (prop.property_type.is_real_non_null_struct_type ()) { var cvalueparam = new CCodeParameter ("result", "%s *".printf (get_ccode_name (prop.get_accessor.value_type))); vdeclarator.add_parameter (cvalueparam); - creturn_type = "void"; - } else { - creturn_type = get_ccode_name (prop.get_accessor.value_type); } var array_type = prop.property_type as ArrayType; @@ -362,7 +359,7 @@ public class Vala.GTypeModule : GErrorModule { vdeclarator.add_parameter (new CCodeParameter (get_delegate_target_cname ("result"), "gpointer*")); } - var vdecl = new CCodeDeclaration (creturn_type); + var vdecl = new CCodeDeclaration (get_ccode_name (creturn_type)); vdecl.add_declarator (vdeclarator); type_struct.add_declaration (vdecl); @@ -468,11 +465,7 @@ public class Vala.GTypeModule : GErrorModule { return; } - var creturn_type = m.return_type; - if (m.return_type.is_real_non_null_struct_type ()) { - // structs are returned via out parameter - creturn_type = new VoidType (); - } + var creturn_type = get_callable_creturn_type (m); // add vfunc field to the type struct var vdeclarator = new CCodeFunctionDeclarator (get_ccode_vfunc_name (m)); @@ -2168,13 +2161,10 @@ public class Vala.GTypeModule : GErrorModule { if (prop.get_accessor != null) { var vdeclarator = new CCodeFunctionDeclarator ("get_%s".printf (prop.name)); vdeclarator.add_parameter (cselfparam); - string creturn_type; + var creturn_type = get_callable_creturn_type (prop.get_accessor.get_method ()); if (returns_real_struct) { var cvalueparam = new CCodeParameter ("value", "%s *".printf (get_ccode_name (prop.get_accessor.value_type))); vdeclarator.add_parameter (cvalueparam); - creturn_type = "void"; - } else { - creturn_type = get_ccode_name (prop.get_accessor.value_type); } var array_type = prop.property_type as ArrayType; @@ -2185,7 +2175,7 @@ public class Vala.GTypeModule : GErrorModule { } } - var vdecl = new CCodeDeclaration (creturn_type); + var vdecl = new CCodeDeclaration (get_ccode_name (creturn_type)); vdecl.add_declarator (vdeclarator); type_struct.add_declaration (vdecl); } -- 2.47.2