Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit fa4eee8

Browse files
committed
[Gen] Fix up array parameters
1 parent 4d9c17a commit fa4eee8

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

src/Gir.Tests/ClassTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public AboutDialog () : base ()
112112
113113
static extern void gtk_about_dialog_add_credit_section (AboutDialog about, string section_name, string people);
114114
115-
public void AddCreditSection (string section_name, string people);
115+
public void AddCreditSection (string section_name, string[] people);
116116
117117
static extern string gtk_about_dialog_get_artists (AboutDialog about);
118118
@@ -176,11 +176,11 @@ public AboutDialog () : base ()
176176
177177
static extern void gtk_about_dialog_set_artists (AboutDialog about, string artists);
178178
179-
public void SetArtists (string artists);
179+
public void SetArtists (string[] artists);
180180
181181
static extern void gtk_about_dialog_set_authors (AboutDialog about, string authors);
182182
183-
public void SetAuthors (string authors);
183+
public void SetAuthors (string[] authors);
184184
185185
static extern void gtk_about_dialog_set_comments (AboutDialog about, string comments);
186186
@@ -192,7 +192,7 @@ public AboutDialog () : base ()
192192
193193
static extern void gtk_about_dialog_set_documenters (AboutDialog about, string documenters);
194194
195-
public void SetDocumenters (string documenters);
195+
public void SetDocumenters (string[] documenters);
196196
197197
static extern void gtk_about_dialog_set_license (AboutDialog about, string license);
198198

src/Gir.Tests/ImportantGeneratables/GObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Object (UIntPtr object_type, string first_property_name, ... var_args) :
2525
2626
static extern Object g_object_newv (UIntPtr object_type, uint n_parameters, Parameter parameters);
2727
28-
public Object (UIntPtr object_type, uint n_parameters, Parameter parameters) : base (object_type, n_parameters, parameters)
28+
public Object (UIntPtr object_type, uint n_parameters, Parameter[] parameters) : base (object_type, n_parameters, parameters)
2929
{
3030
}
3131
@@ -262,7 +262,7 @@ internal struct ObjectClass
262262
263263
static extern void g_object_class_install_properties (ObjectClass oclass, uint n_pspecs, ParamSpec pspecs);
264264
265-
public void InstallProperties (uint n_pspecs, ParamSpec pspecs);
265+
public void InstallProperties (uint n_pspecs, ParamSpec[] pspecs);
266266
267267
static extern void g_object_class_install_property (ObjectClass oclass, uint property_id, ParamSpec pspec);
268268

src/Gir.Tests/RecordTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct ByteArray
8383
/// and will be freed with g_free(), i.e. it could be allocated using g_strdup().
8484
///</summary>
8585
///<returns>a new #GByteArray</returns>
86-
public static ByteArray NewTake (byte data, UIntPtr len);
86+
public static ByteArray NewTake (byte[] data, UIntPtr len);
8787
8888
static extern ByteArray g_byte_array_prepend (ByteArray array, byte data, uint len);
8989

src/Gir/Generation/Callback.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ public void Generate (GenerationOptions opts)
1111
// Otherwise, we need to generate code which uses a non-static callback.
1212

1313
var returnType = this.GetReturnCSharpType (writer);
14-
var (typesAndNames, names) = this.BuildParameters (opts, true);
14+
var parameters = this.BuildParameters (opts, true);
1515

1616
// Public API delegate which uses managed types.
1717
writer.WriteLine ("[UnmanagedFunctionPointer (CallingConvention.Cdecl)]");
18-
writer.WriteLine ($"public delegate {returnType} {Name} ({typesAndNames})");
18+
writer.WriteLine ($"public delegate {returnType} {Name} ({parameters.TypesAndNames})");
1919
writer.WriteLine ();
2020

2121
// Internal API delegate which uses unmanaged types.
2222
writer.WriteLine ("[UnmanagedFunctionPointer (CallingConvention.Cdecl)]");
2323
// TODO: Use native marshal types.
24-
writer.WriteLine ($"internal delegate {returnType} {Name}Native ({typesAndNames})");
24+
writer.WriteLine ($"internal delegate {returnType} {Name}Native ({parameters.TypesAndNames})");
2525
writer.WriteLine ();
2626

2727
// Generate wrapper class - static if we can use gchandle, otherwise instance
2828
// Check callback convention - async, notify, call
2929
writer.WriteLine ($"internal static class {Name}Wrapper");
3030
writer.WriteLine ("{");
3131
using (writer.Indent ()) {
32-
writer.WriteLine ($"public static void NativeCallback ({typesAndNames})");
32+
writer.WriteLine ($"public static void NativeCallback ({parameters.TypesAndNames})");
3333
writer.WriteLine ("{");
3434
// TODO: marshal params, call, handle exceptions
3535
writer.WriteLine ("}");

src/Gir/Generation/IGeneratableExtensions.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void GenerateImport (this INativeCallable callable, IGeneratable parent,
4949
{
5050
var retType = GetReturnCSharpType (callable, writer);
5151

52-
var (typesAndNames, names) = BuildParameters (callable, writer.Options, appendInstanceParameters: true);
52+
var parameters = BuildParameters (callable, writer.Options, appendInstanceParameters: true);
5353

5454
// TODO: Better than using the constant string, insert a custom generatable which contains the import string as a constant.
5555
/* i.e.
@@ -59,7 +59,7 @@ static class <LibraryName>Constants
5959
}
6060
*/
6161
//writer.WriteLine ($"[DllImport (\"{writer.Options.LibraryName}\", CallingConvention=CallingConvention.Cdecl)]");
62-
writer.WriteLine ($"static extern {retType} {callable.CIdentifier} ({typesAndNames});");
62+
writer.WriteLine ($"static extern {retType} {callable.CIdentifier} ({parameters.MarshalTypesAndNames});");
6363
writer.WriteLine ();
6464
}
6565

@@ -87,8 +87,8 @@ public static void GenerateCallableDefinition (this INativeCallable callable, IG
8787
var returnType = callable.GetReturnCSharpType (writer);
8888

8989
// generate ReturnValue then Parameters
90-
var (typesAndNames, names) = BuildParameters (callable, writer.Options, !callable.IsInstanceCallable (gen, writer.Options));
91-
writer.Write (string.Format ("{0} {1} ({2});", returnType, callable.Name.ToCSharp (), typesAndNames));
90+
var result = BuildParameters (callable, writer.Options, !callable.IsInstanceCallable (gen, writer.Options));
91+
writer.Write (string.Format ("{0} {1} ({2});", returnType, callable.Name.ToCSharp (), result.TypesAndNames));
9292
writer.WriteLine ();
9393
}
9494

@@ -98,17 +98,18 @@ public static void GenerateConstructor (this INativeCallable callable, IGenerata
9898

9999
var modifier = callable.GetModifiers (parent, writer.Options);
100100

101-
var (typesAndNames, names) = BuildParameters (callable, writer.Options, !callable.IsInstanceCallable (parent, writer.Options));
101+
var result = BuildParameters (callable, writer.Options, !callable.IsInstanceCallable (parent, writer.Options));
102102

103103
// FIXME, should check to see if it is deprecated
104-
writer.WriteLine ($"{modifier} {parent.Name} ({typesAndNames}) : base ({names})");
104+
writer.WriteLine ($"{modifier} {parent.Name} ({result.TypesAndNames}) : base ({result.Names})");
105105
writer.WriteLine ("{");
106106
writer.WriteLine ("}");
107107
}
108108

109-
public static (string both, string names) BuildParameters (this IMethodLike callable, GenerationOptions opts, bool appendInstanceParameters)
109+
public static BuildParametersResult BuildParameters(this IMethodLike callable, GenerationOptions opts, bool appendInstanceParameters)
110110
{
111111
var parameters = callable.Parameters;
112+
var marshalTypeAndName = new List<string>(parameters.Count);
112113
var typeAndName = new List<string> (parameters.Count);
113114
var parameterNames = new List<string> (parameters.Count);
114115

@@ -124,20 +125,36 @@ public static (string both, string names) BuildParameters (this IMethodLike call
124125
}
125126

126127
var symbol = parameter.Resolve(opts);
127-
typeAndName.Add(symbol.CSharpType + " " + parameter.Name);
128+
marshalTypeAndName.Add(symbol.CSharpType + " " + parameter.Name);
129+
typeAndName.Add(symbol.CSharpType + (parameter.Array != null ? "[]" : "") + " " + parameter.Name);
128130
parameterNames.Add(parameter.Name);
129131
}
130132

131133
// PERF: Use an array as the string[] overload of Join is way more efficient than the IEnumerable<string> one.
134+
string marshalParameterString = string.Join(", ", marshalTypeAndName.ToArray());
132135
string parameterString = string.Join (", ", typeAndName.ToArray ());
133136
string baseParams = string.Join (", ", parameterNames.ToArray ());
134137

135-
return (parameterString, baseParams);
138+
return new BuildParametersResult(marshalParameterString, parameterString, baseParams);
136139
}
137140

138141
static IEnumerable<IMemberGeneratable> GetMemberGeneratables (this IGeneratable gen)
139142
{
140143
return Utils.GetAllCollectionMembers<IMemberGeneratable> (gen);
141144
}
142145
}
146+
147+
public class BuildParametersResult
148+
{
149+
public string MarshalTypesAndNames { get; }
150+
public string TypesAndNames { get; }
151+
public string Names { get; }
152+
153+
public BuildParametersResult(string marshalTypesAndNames, string typesAndNames, string names)
154+
{
155+
MarshalTypesAndNames = marshalTypesAndNames;
156+
TypesAndNames = typesAndNames;
157+
Names = names;
158+
}
159+
}
143160
}

0 commit comments

Comments
 (0)