This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
processedtypes.cs
453 lines (362 loc) · 12.8 KB
/
processedtypes.cs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using System;
using System.Collections.Generic;
using System.Text;
using IKVM.Reflection;
using Type = IKVM.Reflection.Type;
namespace Embeddinator.ObjC {
// While processing user assemblies, we may come across conditions that will affect
// final code generation that we need to pass to the generation pass
public class ProcessedAssembly : IEquatable<Assembly>, IEquatable<ProcessedAssembly> {
public Assembly Assembly { get; private set; }
public string Name { get; private set; }
public string SafeName { get; private set; }
public bool UserCode { get; set; }
public ProcessedAssembly (Assembly assembly)
{
Assembly = assembly;
Name = assembly.GetName ().Name;
SafeName = Name.Sanitize ();
}
public bool Equals (Assembly other)
{
return Assembly == other;
}
public bool Equals (ProcessedAssembly other)
{
return Assembly == other?.Assembly;
}
public override bool Equals (object obj)
{
return Assembly.Equals (obj);
}
public override int GetHashCode ()
{
return Assembly.GetHashCode ();
}
public static bool operator == (ProcessedAssembly a, ProcessedAssembly b)
{
return a?.Assembly == b?.Assembly;
}
public static bool operator != (ProcessedAssembly a, ProcessedAssembly b)
{
return a?.Assembly != b?.Assembly;
}
public override string ToString () => Assembly.ToString ();
}
public class ProcessedType {
public Type Type { get; private set; }
public string TypeName { get; set; }
public string ObjCName { get; private set; }
public ProcessedAssembly Assembly { get; set; }
public List<ProcessedConstructor> Constructors { get; set; }
public List<ProcessedFieldInfo> Fields { get; set; }
public List<ProcessedMethod> Methods { get; set; }
public List<ProcessedProperty> Properties { get; set; }
public bool HasConstructors => Constructors != null && Constructors.Count > 0;
public bool HasFields => Fields != null && Fields.Count > 0;
public bool HasMethods => Methods != null && Methods.Count > 0;
public bool HasProperties => Properties != null && Properties.Count > 0;
public bool IsClass => !IsEnum && !IsProtocol && !IsNativeReference;
public bool IsEnum => Type.IsEnum && !IsNativeReference;
public bool IsProtocol => Type.IsInterface && !IsNativeReference;
// we can track types that we don't need/want to generate (e.g. linker requirements)
public bool IsNativeReference { get; set; }
public bool UserCode => Assembly.UserCode;
public ProcessedType (Type type)
{
Type = type;
TypeName = ObjC.NameGenerator.GetTypeName (Type);
ObjCName = ObjC.NameGenerator.GetObjCName (Type);
}
public bool SignatureExists (ProcessedMemberWithParameters p)
{
foreach (var pc in Constructors) {
if (p.ObjCSignature == pc.ObjCSignature)
return true;
}
foreach (var pm in Methods) {
if (p.ObjCSignature == pm.ObjCSignature)
return true;
}
// FIXME signature clashes can happen on properties, fields (turned into properties) ...
return false;
}
public override string ToString () => Type.ToString ();
}
public abstract class ProcessedMemberBase {
protected Processor Processor;
public bool FallBackToTypeName { get; set; }
public ProcessedType DeclaringType { get; set; }
public ProcessedMemberBase (Processor processor, ProcessedType declaringType)
{
Processor = processor;
DeclaringType = declaringType;
}
public abstract IEnumerable<string> Selectors { get; }
// this format can be consumed by the linker xml files
// adapted from ikvm reflection and cecil source code
// FIXME: double check when we implement generics support
public string ToString (MethodBase m)
{
StringBuilder sb = new StringBuilder ();
var mi = m as MethodInfo;
if (mi != null)
sb.Append (mi.ReturnType.FullName).Append (' ');
else
sb.Append ("System.Void "); // ConstructorInfo
sb.Append (m.Name);
sb.Append ('(');
var sep = String.Empty;
foreach (var p in m.GetParameters ()) {
sb.Append (sep).Append (p.ParameterType);
sep = ",";
}
sb.Append (')');
return sb.ToString();
}
}
public enum MethodType {
Normal,
DefaultValueWrapper,
NSObjectProcotolHash,
NSObjectProcotolIsEqual,
IEquatable,
}
public abstract class ProcessedMemberWithParameters : ProcessedMemberBase {
public ProcessedMemberWithParameters (Processor processor, ProcessedType declaringType) : base (processor, declaringType)
{
objCSignature = new CachedValue<string> (() => GetObjcSignature (true));
objCSelector = new CachedValue<string> (() => GetObjcSignature (false));
monoSignature = new CachedValue<string> (GetMonoSignature);
}
public abstract string BaseName { get; }
public ParameterInfo[] Parameters { get; protected set; }
public int FirstDefaultParameter { get; set; }
protected abstract string GetObjcSignature (bool includeParamNames);
public override IEnumerable<string> Selectors => ObjCSelector.Yield ();
CachedValue<string> objCSignature;
public string ObjCSignature => objCSignature.Value;
CachedValue<string> objCSelector;
public string ObjCSelector => objCSelector.Value;
protected abstract string GetMonoSignature ();
CachedValue<string> monoSignature;
public string MonoSignature => monoSignature.Value;
public void Freeze ()
{
objCSignature.Freeze ();
objCSelector.Freeze ();
monoSignature.Freeze ();
}
}
public class ProcessedMethod : ProcessedMemberWithParameters {
public MethodInfo Method { get; private set; }
public bool IsOperator { get; set; }
public bool IsPropertyImplementation { get; set; }
public string NameOverride { get; set; }
public string ManagedName { get; set; }
public override string BaseName {
get {
if (NameOverride != null)
return NameOverride;
return IsOperator ? Method.Name.Substring (3).CamelCase () : Method.Name.CamelCase ();
}
}
public MethodType MethodType { get; set; }
public bool IsExtension { get; set; }
public ProcessedMethod (MethodInfo method, Processor processor, ProcessedType declaringType) : base (processor, declaringType)
{
Method = method;
MethodType = MethodType.Normal;
Parameters = method.GetParameters ();
FirstDefaultParameter = -1;
}
public override string ToString () => ToString (Method);
protected override string GetMonoSignature ()
{
var mono = new StringBuilder (Method.Name);
mono.Append ('(');
var end = FirstDefaultParameter == -1 ? Parameters.Length : FirstDefaultParameter;
for (int n = 0; n < end; ++n) {
if (n > 0)
mono.Append (',');
mono.Append (NameGenerator.GetMonoName (Parameters[n].ParameterType));
}
mono.Append (')');
return mono.ToString ();
}
protected override string GetObjcSignature (bool includeParamNames)
{
string objName = BaseName;
if (Method.IsSpecialName)
objName = objName.Replace ("_", String.Empty);
var objc = new StringBuilder (objName);
var end = FirstDefaultParameter == -1 ? Parameters.Length : FirstDefaultParameter;
for (int n = 0; n < end; ++n) {
ParameterInfo p = Parameters[n];
if (includeParamNames) {
if (objc.Length > objName.Length)
objc.Append (' ');
}
string paramName = FallBackToTypeName ? NameGenerator.GetParameterTypeName (p.ParameterType) : p.Name;
if (n > 0 || !IsExtension) {
if (n == 0) {
if (FallBackToTypeName || Method.IsConstructor || (!Method.IsSpecialName && !IsOperator))
objc.Append (paramName.PascalCase ());
} else
objc.Append (paramName.CamelCase ());
}
if (includeParamNames) {
if (n > 0 || !IsExtension) {
string ptname = NameGenerator.GetObjCParamTypeName (p, Processor.Types);
objc.Append (":(").Append (ptname).Append (")").Append (NameGenerator.GetExtendedParameterName (p, Parameters));
}
} else {
objc.Append (":");
}
}
return objc.ToString ();
}
}
public class ProcessedProperty: ProcessedMemberBase {
public PropertyInfo Property { get; private set; }
public override IEnumerable<string> Selectors
{
get {
if (HasGetter)
yield return GetterName;
if (HasSetter)
yield return SetterName;
}
}
public ProcessedProperty (PropertyInfo property, Processor processor, ProcessedType declaringType) : base (processor, declaringType)
{
Property = property;
getMethod = new CachedValue<ProcessedMethod> (() => {
var getter = Property.GetGetMethod ();
if (getter != null) {
return new ProcessedMethod (getter, Processor, declaringType) { NameOverride = GetterName, IsPropertyImplementation = true };
}
return null;
});
setMethod = new CachedValue<ProcessedMethod> (() => {
var setter = Property.GetSetMethod ();
if (setter != null) {
return new ProcessedMethod (setter, Processor, declaringType) { NameOverride = SetterName, IsPropertyImplementation = true };
}
return null;
});
}
public override string ToString () => Property.ToString ();
public string Name => NameOverride != null ? NameOverride : Property.Name.CamelCase ();
public string NameOverride { get; set; }
public bool HasGetter => Property.GetGetMethod () != null;
public bool HasSetter => Property.GetSetMethod () != null;
public string GetterName {
get {
if (!HasGetter)
return null;
return (NameOverride ?? Property.Name).CamelCase ();
}
}
public string SetterName {
get {
if (!HasSetter)
return null;
return "set" + (NameOverride ?? Property.Name).PascalCase ();
}
}
CachedValue<ProcessedMethod> getMethod;
public ProcessedMethod GetMethod => getMethod.Value;
CachedValue<ProcessedMethod> setMethod;
public ProcessedMethod SetMethod => setMethod.Value;
public void Freeze ()
{
getMethod.Freeze ();
setMethod.Freeze ();
}
}
public enum ConstructorType {
Normal,
// a `init*` method generated wrapper should not be decorated with NS_DESIGNATED_INITIALIZER
DefaultValueWrapper,
}
public class ProcessedConstructor : ProcessedMemberWithParameters {
public ConstructorInfo Constructor { get; private set; }
public bool Unavailable { get; set; }
public override string BaseName {
get {
if (Parameters.Length == 0 || FirstDefaultParameter == 0)
return "init";
return "initWith";
}
}
public ConstructorType ConstructorType { get; set; }
public ProcessedConstructor (ConstructorInfo constructor, Processor processor, ProcessedType declaringType) : base (processor, declaringType)
{
Constructor = constructor;
Parameters = Constructor.GetParameters ();
FirstDefaultParameter = -1;
}
public override string ToString () => ToString (Constructor);
protected override string GetMonoSignature ()
{
var mono = new StringBuilder (Constructor.Name);
mono.Append ('(');
var end = FirstDefaultParameter == -1 ? Parameters.Length : FirstDefaultParameter;
for (int n = 0; n < end; ++n) {
if (n > 0)
mono.Append (',');
mono.Append (NameGenerator.GetMonoName (Parameters[n].ParameterType));
}
mono.Append (')');
return mono.ToString ();
}
protected override string GetObjcSignature (bool includeParamNames)
{
var objc = new StringBuilder (BaseName);
var end = FirstDefaultParameter == -1 ? Parameters.Length : FirstDefaultParameter;
for (int n = 0; n < end; ++n) {
ParameterInfo p = Parameters[n];
if (includeParamNames) {
if (objc.Length > BaseName.Length)
objc.Append (' ');
}
string paramName = FallBackToTypeName ? NameGenerator.GetParameterTypeName (p.ParameterType) : p.Name;
if (n == 0)
objc.Append (paramName.PascalCase ());
else
objc.Append (paramName.CamelCase ());
if (includeParamNames) {
string ptname = NameGenerator.GetObjCParamTypeName (p, Processor.Types);
objc.Append (":(").Append (ptname).Append (")").Append (NameGenerator.GetExtendedParameterName (p, Parameters));
} else {
objc.Append (":");
}
}
return objc.ToString ();
}
}
public class ProcessedFieldInfo : ProcessedMemberBase {
public FieldInfo Field { get; private set; }
public string TypeName { get; private set; }
public string ObjCName { get; private set; }
public string Name => (NameOverride ?? Field.Name).CamelCase ();
public string NameOverride { get; set; }
public override IEnumerable<string> Selectors {
get {
yield return GetterName;
yield return SetterName;
}
}
public ProcessedFieldInfo (FieldInfo field, Processor processor, ProcessedType declaringType) : base (processor, declaringType)
{
Field = field;
TypeName = ObjC.NameGenerator.GetTypeName (Field.DeclaringType);
ObjCName = ObjC.NameGenerator.GetObjCName (Field.DeclaringType);
}
// linker compatible signature
public override string ToString () => Field.FieldType.FullName + " " + Field.Name;
public string GetterName => (NameOverride ?? Field.Name).CamelCase ();
public string SetterName => "set" + (NameOverride ?? Field.Name).PascalCase ();
}
}