From: Rico Tzschichholz Date: Mon, 18 Jan 2021 14:38:51 +0000 (+0100) Subject: girwriter: Write instance-parameter elements X-Git-Tag: 0.51.1~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fb03903a39d7c0d911d88d9baad3d7d9315cc23;p=thirdparty%2Fvala.git girwriter: Write instance-parameter elements Fixes https://gitlab.gnome.org/GNOME/vala/issues/1128 --- diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala index 14f0819a0..0ad27ed24 100644 --- a/codegen/valagirwriter.vala +++ b/codegen/valagirwriter.vala @@ -1069,16 +1069,16 @@ public class Vala.GIRWriter : CodeVisitor { private void write_implicit_params (DataType? type, ref int index, bool has_array_length, string? name, ParameterDirection direction) { if (type is ArrayType && has_array_length) { for (var i = 0; i < ((ArrayType) type).rank; i++) { - write_param_or_return (((ArrayType) type).length_type, true, ref index, has_array_length, "%s_length%i".printf (name, i + 1), null, direction); + write_param_or_return (((ArrayType) type).length_type, "parameter", ref index, has_array_length, "%s_length%i".printf (name, i + 1), null, direction); } } else if (type is DelegateType) { var deleg_type = (DelegateType) type; if (deleg_type.delegate_symbol.has_target) { var data_type = new PointerType (new VoidType ()); - write_param_or_return (data_type, true, ref index, false, "%s_target".printf (name), null, direction); + write_param_or_return (data_type, "parameter", ref index, false, "%s_target".printf (name), null, direction); if (deleg_type.is_disposable ()) { var notify_type = new DelegateType (context.root.scope.lookup ("GLib").scope.lookup ("DestroyNotify") as Delegate); - write_param_or_return (notify_type, true, ref index, false, "%s_target_destroy_notify".printf (name), null, direction); + write_param_or_return (notify_type, "parameter", ref index, false, "%s_target_destroy_notify".printf (name), null, direction); } } } @@ -1135,17 +1135,13 @@ public class Vala.GIRWriter : CodeVisitor { buffer.append_printf ("\n", tag_type); } - private void write_params_and_return (List params, List? type_params, DataType? return_type, bool return_array_length, string? return_comment = null, bool constructor = false, DataType? instance_type = null, bool user_data = false) { + private void write_params_and_return (List params, List? type_params, DataType? return_type, bool return_array_length, string? return_comment = null, bool constructor = false, Parameter? instance_param = null, bool user_data = false) { int last_index = 0; bool ret_is_struct = return_type != null && return_type.is_real_non_null_struct_type (); - if (params.size != 0 || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { + if (params.size != 0 || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { int index = 0; - if (instance_type != null) { - index++; - } - foreach (Parameter param in params) { index++; @@ -1165,19 +1161,25 @@ public class Vala.GIRWriter : CodeVisitor { } if (return_type != null && !ret_is_struct) { - write_param_or_return (return_type, false, ref last_index, return_array_length, null, return_comment, ParameterDirection.IN, constructor); + write_param_or_return (return_type, "return-value", ref last_index, return_array_length, null, return_comment, ParameterDirection.IN, constructor); } else if (ret_is_struct) { - write_param_or_return (new VoidType (), false, ref last_index, false, null, return_comment, ParameterDirection.IN); + write_param_or_return (new VoidType (), "return-value", ref last_index, false, null, return_comment, ParameterDirection.IN); } - if (params.size != 0 || (type_params != null && type_params.size > 0) || instance_type != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { + if (params.size != 0 || (type_params != null && type_params.size > 0) || instance_param != null || (return_type is ArrayType && return_array_length) || (return_type is DelegateType) || ret_is_struct) { write_indent (); buffer.append_printf ("\n"); indent++; int index = 0; - if (instance_type != null) { - write_param_or_return (instance_type, true, ref index, false, "self"); + if (instance_param != null) { + var type = instance_param.variable_type.copy (); + unowned Struct? st = type.type_symbol as Struct; + if (st != null && !st.is_simple_type ()) { + type.nullable = true; + } + int skip = 0; + write_param_or_return (type, "instance-parameter", ref skip, false, "self"); } if (type_params != null) { @@ -1188,14 +1190,14 @@ public class Vala.GIRWriter : CodeVisitor { } foreach (Parameter param in params) { - write_param_or_return (param.variable_type, true, ref index, get_ccode_array_length (param), param.name, get_parameter_comment (param), param.direction, false, false, param.ellipsis || param.params_array); + write_param_or_return (param.variable_type, "parameter", ref index, get_ccode_array_length (param), param.name, get_parameter_comment (param), param.direction, false, false, param.ellipsis || param.params_array); write_implicit_params (param.variable_type, ref index, get_ccode_array_length (param), param.name, param.direction); } if (ret_is_struct) { // struct returns are converted to parameters - write_param_or_return (return_type, true, ref index, false, "result", return_comment, ParameterDirection.OUT, constructor, true); + write_param_or_return (return_type, "parameter", ref index, false, "result", return_comment, ParameterDirection.OUT, constructor, true); } else { write_implicit_params (return_type, ref index, return_array_length, "result", ParameterDirection.OUT); } @@ -1363,12 +1365,7 @@ public class Vala.GIRWriter : CodeVisitor { write_doc (get_method_comment (m)); } - DataType instance_type = null; - if (instance) { - instance_type = SemanticAnalyzer.get_data_type_for_symbol (m.parent_symbol); - } - - write_params_and_return (params, m.get_type_parameters (), return_type, get_ccode_array_length (m), return_comment, false, instance_type); + write_params_and_return (params, m.get_type_parameters (), return_type, get_ccode_array_length (m), return_comment, false, m.this_parameter); indent--; write_indent (); @@ -1513,9 +1510,8 @@ public class Vala.GIRWriter : CodeVisitor { } - private void write_param_or_return (DataType? type, bool is_parameter, ref int index, bool has_array_length, string? name = null, string? comment = null, ParameterDirection direction = ParameterDirection.IN, bool constructor = false, bool caller_allocates = false, bool ellipsis = false) { + private void write_param_or_return (DataType? type, string tag, ref int index, bool has_array_length, string? name = null, string? comment = null, ParameterDirection direction = ParameterDirection.IN, bool constructor = false, bool caller_allocates = false, bool ellipsis = false) { write_indent (); - string tag = is_parameter ? "parameter" : "return-value"; buffer.append_printf ("<%s", tag); if (ellipsis) { name = "..."; @@ -1555,7 +1551,7 @@ public class Vala.GIRWriter : CodeVisitor { } if (delegate_type != null && delegate_type.delegate_symbol.has_target) { - int closure_index = is_parameter ? + int closure_index = tag == "parameter" ? index + 1 : (type.value_owned ? index - 1 : index); buffer.append_printf (" closure=\"%i\"", closure_index); if (delegate_type.is_called_once) { @@ -1580,7 +1576,7 @@ public class Vala.GIRWriter : CodeVisitor { } else if (type != null) { int length_param_index = -1; if (has_array_length) { - length_param_index = is_parameter ? index + 1 : index; + length_param_index = tag == "parameter" ? index + 1 : index; } write_type (type, length_param_index, direction); } diff --git a/tests/girwriter/GirTest-1.0.gir-expected b/tests/girwriter/GirTest-1.0.gir-expected index 06105bbaf..75a6c51d0 100644 --- a/tests/girwriter/GirTest-1.0.gir-expected +++ b/tests/girwriter/GirTest-1.0.gir-expected @@ -55,12 +55,20 @@ + + + + + + + + @@ -241,6 +249,9 @@ + + + @@ -251,6 +262,9 @@ + + + @@ -261,6 +275,9 @@ + + + @@ -271,6 +288,9 @@ + + + @@ -281,6 +301,9 @@ + + + @@ -291,6 +314,9 @@ + + + @@ -300,12 +326,20 @@ + + + + + + + + @@ -319,6 +353,9 @@ + + + @@ -332,6 +369,9 @@ + + + @@ -347,6 +387,9 @@ + + + @@ -362,6 +405,9 @@ + + + @@ -379,6 +425,9 @@ + + + @@ -389,6 +438,9 @@ + + + @@ -412,6 +464,9 @@ + + + @@ -428,6 +483,9 @@ + + + @@ -445,6 +503,9 @@ + + + @@ -454,12 +515,20 @@ + + + + + + + + @@ -473,6 +542,9 @@ + + + @@ -499,6 +571,9 @@ + + + @@ -521,6 +596,9 @@ + + + @@ -530,6 +608,9 @@ + + + @@ -541,12 +622,20 @@ + + + + + + + + @@ -560,6 +649,9 @@ + + + @@ -570,6 +662,9 @@ + + + @@ -583,6 +678,9 @@ + + + @@ -593,6 +691,9 @@ + + + @@ -606,6 +707,9 @@ + + + @@ -616,6 +720,9 @@ + + + @@ -632,6 +739,9 @@ + + + @@ -645,6 +755,9 @@ + + + @@ -661,6 +774,9 @@ + + + @@ -673,22 +789,40 @@ + + + + + + + + + + + + + + + + + + @@ -698,6 +832,11 @@ + + + + + @@ -706,12 +845,20 @@ + + + + + + + + @@ -725,6 +872,9 @@ + + + @@ -740,12 +890,20 @@ + + + + + + + + @@ -792,9 +950,9 @@ - + - + @@ -807,9 +965,9 @@ - + - + @@ -822,10 +980,10 @@ - + - - + + @@ -840,9 +998,9 @@ - + - + @@ -855,13 +1013,13 @@ - + - + - + @@ -876,9 +1034,9 @@ - + - + @@ -894,9 +1052,9 @@ - + - + @@ -906,6 +1064,9 @@ + + + @@ -926,6 +1087,9 @@ + + + @@ -936,6 +1100,9 @@ + + + @@ -946,6 +1113,9 @@ + + + @@ -956,6 +1126,9 @@ + + + @@ -966,6 +1139,9 @@ + + + @@ -976,6 +1152,9 @@ + + + @@ -985,17 +1164,30 @@ + + + + + + + + + + + + + @@ -1009,6 +1201,9 @@ + + + @@ -1022,6 +1217,9 @@ + + + @@ -1038,6 +1236,9 @@ + + + @@ -1048,6 +1249,9 @@ + + + @@ -1064,6 +1268,9 @@ + + + @@ -1074,6 +1281,9 @@ + + + @@ -1084,6 +1294,9 @@ + + + @@ -1100,9 +1313,9 @@ - + - + @@ -1115,9 +1328,9 @@ - + - + @@ -1130,9 +1343,9 @@ - + - + @@ -1145,9 +1358,9 @@ - + - + @@ -1157,9 +1370,9 @@ - + - + @@ -1175,13 +1388,13 @@ - + - + - + @@ -1196,9 +1409,9 @@ - + - + @@ -1211,9 +1424,9 @@ - + - + @@ -1388,6 +1601,12 @@ + + + + + + @@ -1430,6 +1649,12 @@ + + + + + + @@ -1491,6 +1716,9 @@ + + + @@ -1501,6 +1729,9 @@ + + + @@ -1511,6 +1742,9 @@ + + + @@ -1524,6 +1758,9 @@ + + + @@ -1534,6 +1771,9 @@ + + + @@ -1547,6 +1787,9 @@ + + + @@ -1557,6 +1800,9 @@ + + + @@ -1570,6 +1816,9 @@ + + + @@ -1583,6 +1832,9 @@ + + + @@ -1599,6 +1851,9 @@ + + + @@ -1609,6 +1864,9 @@ + + + @@ -1625,6 +1883,9 @@ + + + @@ -1634,11 +1895,21 @@ + + + + + + + + + + @@ -1647,17 +1918,30 @@ + + + + + + + + + + + + + @@ -1668,6 +1952,9 @@ + + + @@ -1684,9 +1971,9 @@ - + - + @@ -1699,10 +1986,10 @@ - + - - + + @@ -1717,9 +2004,9 @@ - + - + @@ -1732,9 +2019,9 @@ - + - + @@ -1750,13 +2037,13 @@ - + - + - + @@ -1771,9 +2058,9 @@ - + - + @@ -1786,9 +2073,9 @@ - + - + @@ -1798,9 +2085,9 @@ - + - + @@ -1810,9 +2097,9 @@ - + - + @@ -1825,9 +2112,9 @@ - + - + @@ -1837,9 +2124,9 @@ - + - + @@ -1883,6 +2170,11 @@ + + + + + @@ -1906,6 +2198,11 @@ + + + + +