-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathBindingSyntaxFactory.Runtime.cs
More file actions
304 lines (274 loc) · 12.5 KB
/
BindingSyntaxFactory.Runtime.cs
File metadata and controls
304 lines (274 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Extensions;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Macios.Generator.Emitters;
static partial class BindingSyntaxFactory {
public const string Runtime = "Runtime";
public const string ClassPtr = "class_ptr";
/// <summary>
/// Generates a call to the Runtime.GetNSObject<T> method to create a nsobject from a handle.
/// </summary>
/// <param name="nsObjectType">The type of object to use as T</param>
/// <param name="args">The arguments to pass to the GetNSObject method.</param>
/// <param name="suppressNullableWarning">If we should suppress the nullable warning.</param>
/// <returns>The expression that calls GetNSObject method.</returns>
public static ExpressionSyntax GetNSObject (string nsObjectType, ImmutableArray<ArgumentSyntax> args,
bool suppressNullableWarning = false)
{
var argsList = ArgumentList (SeparatedList<ArgumentSyntax> (args.ToSyntaxNodeOrTokenArray ()));
return StaticInvocationGenericExpression (Runtime, "GetNSObject",
nsObjectType, argsList, suppressNullableWarning);
}
/// <summary>
/// Generates a call to the method CFArray.ArrayFromHandle<T> to create a collection of NSObjects.
/// </summary>
/// <param name="nsObjectType">The type of the object to use as T</param>
/// <param name="args">The arguments to bass to the ArrayFromHandle method.</param>
/// <param name="suppressNullableWarning">If we should suppress the nullable warning.</param>
/// <returns>The expression that calls ArrayFromHandle method.</returns>
public static ExpressionSyntax GetNSArrayFromHandle (string nsObjectType, ImmutableArray<ArgumentSyntax> args,
bool suppressNullableWarning = false)
{
var argsList = ArgumentList (SeparatedList<ArgumentSyntax> (args.ToSyntaxNodeOrTokenArray ()));
return StaticInvocationGenericExpression ("CFArray", "ArrayFromHandle",
nsObjectType, argsList, suppressNullableWarning);
}
/// <summary>
/// Returns the expression to get the handle of a selector.
/// </summary>
/// <param name="selector">The selector whose handle we want to retrieve.</param>
/// <returns>The expression to retrieve a selector handle.</returns>
public static InvocationExpressionSyntax GetHandle (string selector)
{
// (selector)
var args = ArgumentList (SingletonSeparatedList (
Argument (LiteralExpression (
SyntaxKind.StringLiteralExpression,
Literal (selector))))).NormalizeWhitespace ();
// Selector.GetHandle (selector)
return InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("Selector"),
IdentifierName ("GetHandle").WithTrailingTrivia (Space)))
.WithArgumentList (args);
}
/// <summary>
/// Generates the "this.Handle" expression.
/// </summary>
/// <returns></returns>
public static MemberAccessExpressionSyntax ThisHandle ()
{
return MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
ThisExpression (),
IdentifierName ("Handle"));
}
/// <summary>
/// Generates the expression to call the objc_msgSend method.
/// </summary>
/// <param name="objcMsgSendMethod">The name of the method in the messaging namespace.</param>
/// <param name="selector">The selector.</param>
/// <param name="parameters">An optional argument list.</param>
/// <returns>The expression needed to call a specific messaging method.</returns>
public static InvocationExpressionSyntax MessagingInvocation (string objcMsgSendMethod, string selector,
ImmutableArray<ArgumentSyntax> parameters)
{
// the size of the arguments is 2 + the optional arguments
// [0] = the handle
// [1] = the selector
// [2] = the arguments
// but to be able to use the SeparatedList we need to add a comma for each argument
// except for the last one, so we need to add parametersCount - 1 commas
var parametersCount = 2 + parameters.Length;
var args = new SyntaxNodeOrToken [(2 * parametersCount) - 1];
// the first two arguments are the selector and the handle, we add those by hand
args [0] = Argument (ThisHandle ());
args [1] = Token (SyntaxKind.CommaToken).WithTrailingTrivia (Space);
args [2] = Argument (GetHandle (selector));
// we need to add the commas and the arguments provided by the user of the api
if (parameters.Length > 0) {
// add a comma because we know we added the selector
args [3] = Token (SyntaxKind.CommaToken).WithTrailingTrivia (Space);
var argsIndex = 4; // start at 4 because we added the first 2 parameters + 2 separators
var parametersIndex = 0;
while (argsIndex < args.Length) {
var currentParameter = parameters [parametersIndex++];
args [argsIndex++] = currentParameter;
if (argsIndex < args.Length - 1) {
args [argsIndex++] = Token (SyntaxKind.CommaToken).WithTrailingTrivia (Space);
}
}
}
// generates: (this.Handle, Selector.GetHandle (selector), args)
var argumentList = ArgumentList (SeparatedList<ArgumentSyntax> (args));
// generates: ObjCRuntime.Messaging.objc_msgSend (this.Handle, Selector.GetHandle (selector), args)
var invocation = InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
AliasQualifiedName (
IdentifierName (
Token (SyntaxKind.GlobalKeyword)),
IdentifierName ("ObjCRuntime")),
IdentifierName ("Messaging")),
IdentifierName (objcMsgSendMethod).WithTrailingTrivia (Space)))
.WithArgumentList (argumentList);
return invocation;
}
/// <summary>
/// Generates the expression to call the CFArray.StringArrayFromHandle method.
/// </summary>
/// <param name="arguments">The argument list for the invocation.</param>
/// <returns>The expression to call CFArray.StringArrayFromHandle method with the provided args.</returns>
internal static InvocationExpressionSyntax StringArrayFromHandle (ImmutableArray<ArgumentSyntax> arguments)
{
// generate: (arg1, arg2, arg3)
var argumentList = ArgumentList (
SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
// generate: CFArray.StringArrayFromHandle (arg1, arg2, arg3)
return InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("CFArray"),
IdentifierName ("StringArrayFromHandle").WithTrailingTrivia (Space)))
.WithArgumentList (argumentList);
}
/// <summary>
/// Generates the expression to call the CFString.FromHandle method.
/// </summary>
/// <param name="arguments">The argument list for the invocation.</param>
/// <returns>The expression to call the CFString.FromHandle method with the provided args.</returns>
internal static InvocationExpressionSyntax StringFromHandle (ImmutableArray<ArgumentSyntax> arguments)
{
// generate: (arg1, arg2, arg3)
var argumentList = ArgumentList (
SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
// generate: CFString.FromHandle (arg1, arg2, arg3)
return InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("CFString"),
IdentifierName ("FromHandle").WithTrailingTrivia (Space)))
.WithArgumentList (argumentList);
}
/// <summary>
/// Returns the method group needed to get a NSValue from a handle.
/// </summary>
/// <param name="returnType">The type info of the return type.</param>
/// <returns>The member access to the correct NSValue method.</returns>
internal static MemberAccessExpressionSyntax? NSValueFromHandle (TypeInfo returnType)
{
#pragma warning disable format
var memberName = returnType switch {
// CoreAnimation
{ FullyQualifiedName: "CoreAnimation.CATransform3D" } => "ToCATransform3D",
// CoreGraphics
{ FullyQualifiedName: "CoreGraphics.CGAffineTransform" } => "ToCGAffineTransform",
{ FullyQualifiedName: "CoreGraphics.CGPoint" } => "ToCGPoint",
{ FullyQualifiedName: "CoreGraphics.CGRect" } => "ToCGRect",
{ FullyQualifiedName: "CoreGraphics.CGSize" } => "ToCGSize",
{ FullyQualifiedName: "CoreGraphics.CGVector" } => "ToCGVector",
// CoreMedia
{ FullyQualifiedName: "CoreMedia.CMTime" } => "ToCMTime",
{ FullyQualifiedName: "CoreMedia.CMTimeMapping" } => "ToCMTimeMapping",
{ FullyQualifiedName: "CoreMedia.CMTimeRange" } => "ToCMTimeRange",
{ FullyQualifiedName: "CoreMedia.CMVideoDimensions" } => "ToCMVideoDimensions",
// CoreLocation
{ FullyQualifiedName: "CoreLocation.CLLocationCoordinate2D" } => "ToCLLocationCoordinate2D",
// Foundation
{ FullyQualifiedName: "Foundation.NSRange" } => "ToNSRange",
// MapKit
{ FullyQualifiedName: "MapKit.MKCoordinateSpan" } => "ToMKCoordinateSpan",
// SceneKit
{ FullyQualifiedName: "SceneKit.SCNMatrix4" } => "ToSCNMatrix4",
{ FullyQualifiedName: "SceneKit.SCNVector3" } => "ToSCNVector3",
{ FullyQualifiedName: "SceneKit.SCNVector4" } => "ToSCNVector4",
// UIKit
{ FullyQualifiedName: "UIKit.NSDirectionalEdgeInsets" } => "ToNSDirectionalEdgeInsets",
{ FullyQualifiedName: "UIKit.UIEdgeInsets" } => "ToUIEdgeInsets",
{ FullyQualifiedName: "UIKit.UIOffset" } => "ToUIOffset",
_ => null,
};
#pragma warning restore format
if (memberName is null)
return null;
return MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("NSValue"),
IdentifierName (memberName));
}
/// <summary>
/// Returns the method group needed to get a NSNumber from a handle.
/// </summary>
/// <param name="returnType">The type info of the return type.</param>
/// <returns>The member access to the correct NSNumber method.</returns>
internal static MemberAccessExpressionSyntax? NSNumberFromHandle (TypeInfo returnType)
{
// create a tuple to store the name and special type depending if it is an array
// or a non array type
var info = returnType.IsArray
? (Name: returnType.Name, SpecialType: returnType.ArrayElementType)
: (Name: returnType.Name, SpecialType: returnType.SpecialType);
#pragma warning disable format
var memberName = info switch {
// name must be before SpecialType or you'll get them wrong values because
// the type we want by name also have a valid special type, the tests should catch
// mistakes here
{ Name: "NFloat" or "nfloat" } => "ToNFloat",
{ Name: "nint" } => "ToNInt",
{ Name: "nuint" } => "ToNUInt",
{ SpecialType: SpecialType.System_Boolean } => "ToBool",
{ SpecialType: SpecialType.System_Byte } => "ToByte",
{ SpecialType: SpecialType.System_Double } => "ToDouble",
{ SpecialType: SpecialType.System_Single } => "ToFloat",
{ SpecialType: SpecialType.System_Int16 } => "ToInt16",
{ SpecialType: SpecialType.System_Int32 } => "ToInt32",
{ SpecialType: SpecialType.System_Int64 } => "ToInt64",
{ SpecialType: SpecialType.System_SByte } => "ToSByte",
{ SpecialType: SpecialType.System_UInt16 } => "ToUInt16",
{ SpecialType: SpecialType.System_UInt32 } => "ToUInt32",
{ SpecialType: SpecialType.System_UInt64 } => "ToUInt64",
_ => null,
};
#pragma warning restore format
if (memberName is null)
return null;
return MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("NSNumber"),
IdentifierName (memberName));
}
/// <summary>
/// Generates a call to the NSArray.ArrayFromHandleFunc with the given arguments.
/// </summary>
/// <param name="returnType">The generic return type of the call.</param>
/// <param name="arguments">An immutable array of arguments.</param>
/// <returns>The invocation syntax for the method.</returns>
internal static InvocationExpressionSyntax NSArrayFromHandleFunc (string returnType,
ImmutableArray<ArgumentSyntax> arguments)
{
// generate: (arg1, arg2, arg3)
var argumentList = ArgumentList (
SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
// generate <returnType>
var genericsList = TypeArgumentList (
SingletonSeparatedList<TypeSyntax> (IdentifierName (returnType)));
// generate NSArray.ArrayFromHandleFunc<returnType> (arg1, arg2, arg3)
return InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("NSArray"),
GenericName ("ArrayFromHandleFunc")
.WithTypeArgumentList (genericsList)
.WithTrailingTrivia (Space)))
.WithArgumentList (argumentList);
}
}