Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit b9f1d58

Browse files
committed
Introduce accessor concept to InterfaceVMs
This fixes inconsistent method generation in GIntrface interfaces, adapters and implementor interfaces. For example, in Atk.IText the method GetRangeExtents existed with two different signatures. In Atk.IText and Atk.TextAdapter we had: (1) Atk.TextRectangle GetRangeExtents(int start_offset, int end_offset, Atk.CoordType coord_type); whereas the signature in Atk.ITextImplementor was: (2) void GetRangeExtents (int start_offset, int end_offset, Atk.CoordType coord_type, out Atk.TextRectangle rect); This fix ensures that all interface methods have the form as shown in (1). This has been implemented by applying the same rules for "Accessor"-methods in InterfaceVM as are already in place in class Method. Essentially these rules state that all methods with one out-parameter are Accessor-methods.
1 parent 3065303 commit b9f1d58

File tree

4 files changed

+111
-14
lines changed

4 files changed

+111
-14
lines changed

generator/InterfaceVM.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public void GenerateDeclaration (StreamWriter sw, InterfaceVM complement, string
8686
}
8787
} else if (IsSetter)
8888
sw.WriteLine ("\t\t" + declPrefix + parms[0].CSType + " " + Name.Substring (3) + " { set; }");
89-
else
89+
else if (IsAccessor) {
90+
sw.WriteLine ("\t\t" + declPrefix + Signature.AccessorType + " " + Name + " (" + Signature.AsAccessor + ");");
91+
} else
9092
sw.WriteLine ("\t\t" + declPrefix + retval.CSType + " " + Name + " (" + Signature + ");");
9193
}
9294

generator/ManagedCallString.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace GtkSharp.Generation {
2424
using System;
2525
using System.Collections.Generic;
2626
using System.IO;
27+
using System.Linq;
2728

2829
public class ManagedCallString {
2930

@@ -34,9 +35,18 @@ public class ManagedCallString {
3435
string destroy_param = null;
3536

3637
public ManagedCallString (Parameters parms)
38+
: this (parms, false)
39+
{
40+
}
41+
42+
private ManagedCallString (Parameters parms, bool stripAccessorParameter)
3743
{
3844
for (int i = 0; i < parms.Count; i ++) {
3945
Parameter p = parms [i];
46+
if (stripAccessorParameter && p.PassAs == "out") {
47+
continue;
48+
}
49+
4050
if (p.IsLength && i > 0 && parms [i-1].IsString)
4151
continue;
4252
else if (p.Scope == "notified") {
@@ -90,6 +100,14 @@ public string Unconditional (string indent) {
90100
return ret;
91101
}
92102

103+
public static ManagedCallString CreateWithoutAccessorParameter (Parameters parms,
104+
out Parameter accessorParam)
105+
{
106+
var call = new ManagedCallString (parms, true);
107+
accessorParam = parms.FirstOrDefault (p => p.PassAs == "out");
108+
return call;
109+
}
110+
93111
public string Setup (string indent)
94112
{
95113
string ret = "";
@@ -158,21 +176,29 @@ public string Finish (string indent)
158176
continue;
159177
}
160178

161-
IGeneratable igen = p.Generatable;
162-
163-
if (igen is CallbackGen)
164-
continue;
165-
else if (igen is StructBase || igen is ByRefGen)
166-
ret += indent + String.Format ("if ({0} != IntPtr.Zero) System.Runtime.InteropServices.Marshal.StructureToPtr (my{0}, {0}, false);\n", p.Name);
167-
else if (igen is IManualMarshaler)
168-
ret += String.Format ("{0}{1} = {2};", indent, p.Name, (igen as IManualMarshaler).AllocNative ("my" + p.Name));
169-
else
170-
ret += indent + p.Name + " = " + igen.CallByName ("my" + p.Name) + ";\n";
179+
ret += GetParamAssignmentStatement ("my" + p.Name, p, indent);
171180
}
172181

173182
return ret;
174183
}
175184

185+
public static string GetParamAssignmentStatement (string srcParameterName, Parameter p, string indent)
186+
{
187+
var ret = string.Empty;
188+
IGeneratable igen = p.Generatable;
189+
190+
if (igen is CallbackGen)
191+
return string.Empty;
192+
else if (igen is StructBase || igen is ByRefGen)
193+
ret += indent + String.Format ("if ({0} != IntPtr.Zero) System.Runtime.InteropServices.Marshal.StructureToPtr ({1}, {0}, false);\n", p.Name, srcParameterName);
194+
else if (igen is IManualMarshaler)
195+
ret += String.Format ("{0}{1} = {2};", indent, p.Name, (igen as IManualMarshaler).AllocNative (srcParameterName));
196+
else
197+
ret += indent + p.Name + " = " + igen.CallByName (srcParameterName) + ";\n";
198+
199+
return ret;
200+
}
201+
176202
public string DisposeParams (string indent)
177203
{
178204
string ret = "";

generator/VMSignature.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,57 @@ public VMSignature (Parameters parms)
5555
}
5656
}
5757

58+
public bool IsAccessor {
59+
get {
60+
int count = 0;
61+
foreach (Parameter p in parms) {
62+
if (p.PassAs == "out")
63+
count++;
64+
65+
if (count > 1)
66+
return false;
67+
}
68+
return count == 1;
69+
}
70+
}
71+
72+
public string AccessorType {
73+
get {
74+
foreach (Parameter p in parms)
75+
if (p.PassAs == "out")
76+
return p.CSType;
77+
78+
return null;
79+
}
80+
}
81+
82+
public string AccessorName {
83+
get {
84+
foreach (Parameter p in parms)
85+
if (p.PassAs == "out")
86+
return p.Name;
87+
88+
return null;
89+
}
90+
}
91+
92+
public string AsAccessor {
93+
get {
94+
string[] result = new string [parms.Count - 1];
95+
int i = 0;
96+
97+
foreach (Parameter p in parms) {
98+
if (p.PassAs == "out")
99+
continue;
100+
101+
result [i] = p.PassAs != "" ? p.PassAs + " " : "";
102+
result [i++] += p.CSType + " " + p.Name;
103+
}
104+
105+
return String.Join (", ", result);
106+
}
107+
}
108+
58109
public string GetCallString (bool use_place_holders)
59110
{
60111
if (parms.Count == 0)

generator/VirtualMethod.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace GtkSharp.Generation {
3030
public abstract class VirtualMethod : MethodBase {
3131
protected ReturnValue retval;
3232
protected ManagedCallString call;
33+
private Parameter accessorParam;
3334

3435
protected string modifiers = "";
3536

@@ -56,6 +57,12 @@ protected abstract string CallString {
5657
}
5758
}
5859

60+
protected bool IsAccessor {
61+
get {
62+
return retval.IsVoid && Signature.IsAccessor;
63+
}
64+
}
65+
5966
/* Creates a callback method which invokes the corresponding virtual method
6067
* @implementor is the class that implements the virtual method(e.g. the class that derives from an interface) or NULL if containing and declaring type are equal
6168
*/
@@ -99,16 +106,22 @@ public void GenerateCallback (StreamWriter sw, ClassBase implementor)
99106
}
100107

101108
string indent = "\t\t\t\t";
102-
if (!retval.IsVoid)
109+
if (IsAccessor) {
110+
sw.WriteLine (indent + Signature.AccessorType + " __result;");
111+
} else if (!retval.IsVoid)
103112
sw.WriteLine (indent + retval.CSType + " __result;");
104113
sw.Write (call.Setup (indent));
105114
sw.Write (indent);
106-
if (!retval.IsVoid)
115+
if (!retval.IsVoid || IsAccessor)
107116
sw.Write ("__result = ");
108117
if (!this.IsStatic)
109118
sw.Write ("__obj.");
110119
sw.WriteLine (this.CallString + ";");
111120
sw.Write (call.Finish (indent));
121+
if (IsAccessor) {
122+
sw.Write (ManagedCallString.GetParamAssignmentStatement ("__result", accessorParam, indent));
123+
}
124+
112125
if (!retval.IsVoid)
113126
sw.WriteLine ("\t\t\t\treturn " + retval.ToNative ("__result") + ";");
114127

@@ -149,7 +162,12 @@ public override bool Validate (LogWriter log)
149162
return false;
150163
}
151164

152-
call = new ManagedCallString (parms);
165+
if (IsAccessor) {
166+
call = ManagedCallString.CreateWithoutAccessorParameter (parms, out accessorParam);
167+
} else {
168+
call = new ManagedCallString (parms);
169+
}
170+
153171
return true;
154172
}
155173
}

0 commit comments

Comments
 (0)