forked from xamarin/xamarin-macios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator-attribute-manager.cs
736 lines (670 loc) · 28.2 KB
/
generator-attribute-manager.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
using System;
using System.Collections.Generic;
using System.Linq;
using IKVM.Reflection;
using Type = IKVM.Reflection.Type;
using PlatformName = ObjCRuntime.PlatformName;
public class AttributeManager
{
public BindingTouch BindingTouch;
TypeManager TypeManager { get { return BindingTouch.TypeManager; } }
public AttributeManager (BindingTouch binding_touch)
{
BindingTouch = binding_touch;
}
System.Type LookupReflectionType (string fullname, ICustomAttributeProvider provider)
{
switch (fullname) {
case "AbstractAttribute":
return typeof (AbstractAttribute);
case "AlignAttribute":
return typeof (AlignAttribute);
case "AppearanceAttribute":
return typeof (AppearanceAttribute);
case "AsyncAttribute":
return typeof (AsyncAttribute);
case "AutoreleaseAttribute":
return typeof (AutoreleaseAttribute);
case "BaseTypeAttribute":
return typeof (BaseTypeAttribute);
case "BindAttribute":
return typeof (BindAttribute);
case "CategoryAttribute":
return typeof (CategoryAttribute);
case "CheckDisposedAttribute":
return typeof (CheckDisposedAttribute);
case "CoreImageFilterAttribute":
return typeof (CoreImageFilterAttribute);
case "CoreImageFilterPropertyAttribute":
return typeof (CoreImageFilterPropertyAttribute);
case "DefaultCtorVisibilityAttribute":
return typeof (DefaultCtorVisibilityAttribute);
case "DefaultEnumValueAttribute":
return typeof (DefaultEnumValueAttribute);
case "DefaultValueAttribute":
return typeof (DefaultValueAttribute);
case "DefaultValueFromArgumentAttribute":
return typeof (DefaultValueFromArgumentAttribute);
case "DelegateApiNameAttribute":
return typeof (DelegateApiNameAttribute);
case "DelegateNameAttribute":
return typeof (DelegateNameAttribute);
case "DesignatedInitializerAttribute":
return typeof (DesignatedInitializerAttribute);
case "DisableDefaultCtorAttribute":
return typeof (DisableDefaultCtorAttribute);
case "DisposeAttribute":
return typeof (DisposeAttribute);
case "ErrorDomainAttribute":
return typeof (ErrorDomainAttribute);
case "EventArgsAttribute":
return typeof (EventArgsAttribute);
case "EventNameAttribute":
return typeof (EventNameAttribute);
case "ForcedTypeAttribute":
return typeof (ForcedTypeAttribute);
case "Foundation.AdviceAttribute":
return typeof (Foundation.AdviceAttribute);
case "Foundation.ExportAttribute":
return typeof (Foundation.ExportAttribute);
case "Foundation.FieldAttribute":
return typeof (Foundation.FieldAttribute);
case "Foundation.ModelAttribute":
return typeof (Foundation.ModelAttribute);
case "Foundation.NotImplementedAttribute":
return typeof (Foundation.NotImplementedAttribute);
case "Foundation.PreserveAttribute":
return typeof (Foundation.PreserveAttribute);
case "Foundation.ProtocolAttribute":
return typeof (Foundation.ProtocolAttribute);
case "Foundation.RegisterAttribute":
return typeof (Foundation.RegisterAttribute);
case "IgnoredInDelegateAttribute":
return typeof (IgnoredInDelegateAttribute);
case "InternalAttribute":
return typeof (InternalAttribute);
case "ManualAttribute":
return typeof (ManualAttribute);
case "MarshalDirectiveAttribute":
return typeof (MarshalDirectiveAttribute);
case "MarshalNativeExceptionsAttribute":
return typeof (MarshalNativeExceptionsAttribute);
case "NewAttribute":
return typeof (NewAttribute);
case "NoDefaultValueAttribute":
return typeof (NoDefaultValueAttribute);
case "NoMethodAttribute":
return typeof (NoMethodAttribute);
case "NotificationAttribute":
return typeof (NotificationAttribute);
case "NullAllowedAttribute":
return typeof (NullAllowedAttribute);
case "ObjCRuntime.ArgumentSemantic":
return typeof (ObjCRuntime.ArgumentSemantic);
case "ObjCRuntime.BindAsAttribute":
return typeof (ObjCRuntime.BindAsAttribute);
case "ObjCRuntime.BindingImplAttribute":
return typeof (ObjCRuntime.BindingImplAttribute);
case "ObjCRuntime.BindingImplOptions":
return typeof (ObjCRuntime.BindingImplOptions);
case "ObjCRuntime.DeprecatedAttribute":
return typeof (ObjCRuntime.DeprecatedAttribute);
case "ObjCRuntime.IntroducedAttribute":
return typeof (ObjCRuntime.IntroducedAttribute);
case "ObjCRuntime.NativeAttribute":
return typeof (ObjCRuntime.NativeAttribute);
case "ObjCRuntime.ObsoletedAttribute":
return typeof (ObjCRuntime.ObsoletedAttribute);
case "ObjCRuntime.PlatformArchitecture":
return typeof (ObjCRuntime.PlatformArchitecture);
case "ObjCRuntime.PlatformName":
return typeof (ObjCRuntime.PlatformName);
case "ObjCRuntime.RequiresSuperAttribute":
return typeof (ObjCRuntime.RequiresSuperAttribute);
case "ObjCRuntime.UnavailableAttribute":
return typeof (ObjCRuntime.UnavailableAttribute);
case "OptionalImplementationAttribute":
return typeof (OptionalImplementationAttribute);
case "OverrideAttribute":
return typeof (OverrideAttribute);
case "PostGetAttribute":
return typeof (PostGetAttribute);
case "PostSnippetAttribute":
return typeof (PostSnippetAttribute);
case "PreSnippetAttribute":
return typeof (PreSnippetAttribute);
case "PrivateDefaultCtorAttribute":
return typeof (PrivateDefaultCtorAttribute);
case "ProtectedAttribute":
return typeof (ProtectedAttribute);
case "ProtocolizeAttribute":
return typeof (ProtocolizeAttribute);
case "SealedAttribute":
return typeof (SealedAttribute);
case "StaticAttribute":
return typeof (StaticAttribute);
case "StrongDictionaryAttribute":
return typeof (StrongDictionaryAttribute);
case "System.Boolean":
return typeof (System.Boolean);
case "System.ComponentModel.EditorBrowsableAttribute":
return typeof (System.ComponentModel.EditorBrowsableAttribute);
case "System.ComponentModel.EditorBrowsableState":
return typeof (System.ComponentModel.EditorBrowsableState);
case "System.Diagnostics.DebuggerBrowsableAttribute":
return typeof (System.Diagnostics.DebuggerBrowsableAttribute);
case "System.Diagnostics.DebuggerBrowsableState":
return typeof (System.Diagnostics.DebuggerBrowsableState);
case "System.Int32":
return typeof (System.Int32);
case "System.Object":
return typeof (System.Object);
case "System.ObsoleteAttribute":
return typeof (System.ObsoleteAttribute);
case "System.Runtime.InteropServices.FieldOffsetAttribute":
return typeof (System.Runtime.InteropServices.FieldOffsetAttribute);
case "System.Runtime.InteropServices.MarshalAsAttribute":
return typeof (System.Runtime.InteropServices.MarshalAsAttribute);
case "System.Runtime.InteropServices.UnmanagedType":
return typeof (System.Runtime.InteropServices.UnmanagedType);
case "System.String":
return typeof (System.String);
case "ThreadSafeAttribute":
return typeof (ThreadSafeAttribute);
case "TransientAttribute":
return typeof (TransientAttribute);
case "UnifiedInternalAttribute":
return typeof (UnifiedInternalAttribute);
case "Visibility":
return typeof (Visibility);
case "WrapAttribute":
return typeof (WrapAttribute);
}
switch (fullname) {
case "ObjCRuntime.iOSAttribute":
case "ObjCRuntime.LionAttribute":
case "ObjCRuntime.AvailabilityAttribute":
case "ObjCRuntime.MacAttribute":
case "ObjCRuntime.SinceAttribute":
case "ObjCRuntime.MountainLionAttribute":
case "ObjCRuntime.MavericksAttribute":
throw ErrorHelper.CreateError (1061, fullname, Generator.FormatProvider (provider));
}
return null;
}
// This method gets the System.Type for a IKVM.Reflection.Type to a System.Type.
System.Type ConvertType (Type type, ICustomAttributeProvider provider)
{
var rv = LookupReflectionType (type.FullName, provider);
if (rv == null)
throw ErrorHelper.CreateError (1055, type.AssemblyQualifiedName);
return rv;
}
// This method gets the IKVM.Reflection.Type for a System.Type.
Type ConvertType (System.Type type, ICustomAttributeProvider provider)
{
var ikvm_type_lookup = BindingTouch.IKVMTypeLookup;
if (!ikvm_type_lookup.TryGetValue (type, out var rv)) {
// Brute force: look everywhere.
// Due to how types move around between assemblies in .NET 5 it gets complicated
// to figure out which assembly each type comes from, so just look in every assembly.
// Report a warning if we find the same type in multiple assemblies though.
var assemblies = BindingTouch.universe.GetAssemblies ();
foreach (var asm in assemblies) {
var lookup = asm.FindType (new TypeName (type.Namespace, type.Name));
if (lookup == null)
continue;
if (lookup.Assembly != asm) {
// Apparently looking for type X in assembly A can return type X from assembly B... ignore those.
continue;
}
if (rv != null) {
ErrorHelper.Warning (1119, /*"Internal error: found the same type ({0}) in multiple assemblies ({1} and {2}). Please file a bug report (https://github.com/xamarin/xamarin-macios/issues/new) with a test case.", */type.FullName, rv.AssemblyQualifiedName, lookup.AssemblyQualifiedName);
break; // no need to report this more than once
}
rv = lookup;
}
ikvm_type_lookup [type] = rv;
}
if (rv == null)
throw ErrorHelper.CreateError (1055, type.AssemblyQualifiedName);
return rv;
}
static IEnumerable<System.Attribute> ConvertOldAttributes (CustomAttributeData attribute)
{
switch (attribute.AttributeType.Namespace) {
case null: // Root namespace such as PlatformAvailabilityShadow.cs
case "MonoTouch.ObjCRuntime":
case "ObjCRuntime":
break;
default:
return Enumerable.Empty<System.Attribute> ();
}
switch (attribute.AttributeType.Name) {
case "SinceAttribute":
case "iOSAttribute":
return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.iOS).Yield ();
case "MacAttribute":
return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacOSX).Yield ();
case "WatchAttribute":
return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.WatchOS).Yield ();
case "TVAttribute":
return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.TvOS).Yield ();
case "MacCatalystAttribute":
return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacCatalyst).Yield ();
case "LionAttribute":
return AttributeFactory.CreateNewIntroducedAttribute (PlatformName.MacOSX, 10, 7).Yield ();
case "MountainLionAttribute":
return AttributeFactory.CreateNewIntroducedAttribute (PlatformName.MacOSX, 10, 8).Yield ();
case "MavericksAttribute":
return AttributeFactory.CreateNewIntroducedAttribute (PlatformName.MacOSX, 10, 9).Yield ();
case "NoMacAttribute":
return AttributeFactory.CreateUnavailableAttribute (PlatformName.MacOSX).Yield ();
case "NoiOSAttribute":
return AttributeFactory.CreateUnavailableAttribute (PlatformName.iOS).Yield ();
case "NoWatchAttribute":
return AttributeFactory.CreateUnavailableAttribute (PlatformName.WatchOS).Yield ();
case "NoTVAttribute":
return AttributeFactory.CreateUnavailableAttribute (PlatformName.TvOS).Yield ();
case "NoMacCatalystAttribute":
return AttributeFactory.CreateUnavailableAttribute (PlatformName.MacCatalyst).Yield ();
case "AvailabilityAttribute":
return AttributeConversionManager.ConvertAvailability (attribute);
default:
return Enumerable.Empty<System.Attribute> ();
}
}
IEnumerable<T> CreateAttributeInstance <T> (CustomAttributeData attribute, ICustomAttributeProvider provider) where T : System.Attribute
{
var convertedAttributes = ConvertOldAttributes (attribute);
if (convertedAttributes.Any ())
return convertedAttributes.OfType<T> ();
var expectedType = ConvertType (typeof (T), provider);
if (attribute.AttributeType != expectedType && !IsSubclassOf (expectedType, attribute.AttributeType))
return Enumerable.Empty<T> ();
System.Type attribType = ConvertType (attribute.AttributeType, provider);
var constructorArguments = new object [attribute.ConstructorArguments.Count];
for (int i = 0; i < constructorArguments.Length; i++) {
var value = attribute.ConstructorArguments [i].Value;
switch (attribute.ConstructorArguments [i].ArgumentType.FullName) {
case "System.Type":
if (value != null) {
if (attribType.Assembly == typeof (TypeManager).Assembly) {
constructorArguments [i] = value;
} else {
constructorArguments [i] = System.Type.GetType (((Type) value).FullName);
}
if (constructorArguments [i] == null)
throw ErrorHelper.CreateError (1056, attribType.FullName, i + 1);
}
break;
default:
constructorArguments [i] = value;
break;
}
}
var parameters = attribute.Constructor.GetParameters ();
var ctorTypes = new System.Type [parameters.Length];
for (int i = 0; i < ctorTypes.Length; i++) {
var paramType = parameters [i].ParameterType;
switch (paramType.FullName) {
case "System.Type":
if (attribType.Assembly == typeof (TypeManager).Assembly) {
ctorTypes [i] = typeof (Type);
} else {
ctorTypes [i] = typeof (System.Type);
}
break;
default:
ctorTypes [i] = ConvertType (paramType, provider);
break;
}
if (ctorTypes [i] == null)
throw ErrorHelper.CreateError (1057, attribType.FullName, i, paramType.FullName);
}
var ctor = attribType.GetConstructor (ctorTypes);
if (ctor == null)
throw ErrorHelper.CreateError (1058, attribType.FullName);
var instance = ctor.Invoke (constructorArguments);
for (int i = 0; i < attribute.NamedArguments.Count; i++) {
var arg = attribute.NamedArguments [i];
var value = arg.TypedValue.Value;
if (arg.TypedValue.ArgumentType == TypeManager.System_String_Array) {
var typed_values = (CustomAttributeTypedArgument []) arg.TypedValue.Value;
var arr = new string [typed_values.Length];
for (int a = 0; a < arr.Length; a++)
arr [a] = (string) typed_values [a].Value;
value = arr;
} else if (arg.TypedValue.ArgumentType.FullName == "System.Type[]") {
var typed_values = (CustomAttributeTypedArgument []) arg.TypedValue.Value;
var arr = new Type [typed_values.Length];
for (int a = 0; a < arr.Length; a++)
arr [a] = (Type) typed_values [a].Value;
value = arr;
} else if (arg.TypedValue.ArgumentType.IsArray) {
throw ErrorHelper.CreateError (1073, attribType.FullName, i + 1, arg.MemberName);
}
if (arg.IsField) {
attribType.GetField (arg.MemberName).SetValue (instance, value);
} else {
attribType.GetProperty (arg.MemberName).SetValue (instance, value, new object [0]);
}
}
return ((T) instance).Yield ();
}
T [] FilterAttributes<T> (IList<CustomAttributeData> attributes, ICustomAttributeProvider provider) where T : System.Attribute
{
if (attributes == null || attributes.Count == 0)
return Array.Empty<T> ();
List<T> list = null;
for (int i = 0; i < attributes.Count; i++) {
// special compiler attribtues not usable from C#
switch (attributes [i].AttributeType.FullName) {
case "System.Runtime.CompilerServices.NullableAttribute":
case "System.Runtime.CompilerServices.NullableContextAttribute":
continue;
}
foreach (var attrib in CreateAttributeInstance<T> (attributes [i], provider)) {
if (list == null)
list = new List<T> ();
list.Add (attrib);
}
}
if (list != null)
return list.ToArray ();
return Array.Empty<T> ();
}
public T [] GetCustomAttributes<T> (ICustomAttributeProvider provider) where T : System.Attribute
{
return FilterAttributes<T> (GetIKVMAttributes (provider), provider);
}
static IList<CustomAttributeData> GetIKVMAttributes (ICustomAttributeProvider provider)
{
if (provider == null)
return null;
var member = provider as MemberInfo;
if (member != null)
return CustomAttributeData.GetCustomAttributes (member);
var assembly = provider as Assembly;
if (assembly != null)
return CustomAttributeData.GetCustomAttributes (assembly);
var pinfo = provider as ParameterInfo;
if (pinfo != null)
return CustomAttributeData.GetCustomAttributes (pinfo);
var module = provider as Module;
if (module != null)
return CustomAttributeData.GetCustomAttributes (module);
throw new BindingException (1051, true, provider.GetType ().FullName);
}
public static bool HasAttribute (ICustomAttributeProvider provider, string type_name)
{
var attribs = GetIKVMAttributes (provider);
for (int i = 0; i < attribs.Count; i++)
if (attribs [i].AttributeType.Name == type_name)
return true;
return false;
}
public bool HasAttribute<T> (ICustomAttributeProvider provider) where T : Attribute
{
var attribute_type = ConvertType (typeof (T), provider);
var attribs = GetIKVMAttributes (provider);
if (attribs == null || attribs.Count == 0)
return false;
for (int i = 0; i < attribs.Count; i++) {
var attrib = attribs [i];
if (attrib.AttributeType == attribute_type)
return true;
if (IsSubclassOf (attribute_type, attrib.AttributeType))
return true;
}
return false;
}
public T GetCustomAttribute<T> (ICustomAttributeProvider provider) where T : System.Attribute
{
if (provider is null)
return null;
var rv = GetCustomAttributes<T> (provider);
if (rv == null || rv.Length == 0)
return null;
if (rv.Length == 1)
return rv [0];
string name = (provider as MemberInfo)?.Name;
if (provider is ParameterInfo) {
var pi = (ParameterInfo) provider;
name = $"the method {pi.Member.DeclaringType.FullName}.{pi.Member.Name}'s parameter #{pi.Position} ({pi.Name})";
} else if (provider is Type type) {
name = $"the type {type.FullName}";
} else if (provider is MemberInfo) {
var mi = (MemberInfo) provider;
name = $"the member {mi.DeclaringType.FullName}.{mi.Name}";
} else if (provider is Assembly) {
name = $"the assembly {((Assembly) provider).FullName}";
} else if (provider is Module) {
name = $"the module {((Module) provider).FullyQualifiedName}";
} else {
name = $"the member {provider.ToString ()}";
}
throw ErrorHelper.CreateError (1059, rv.Length, typeof (T).FullName, name);
}
public static ICustomAttributeProvider GetReturnTypeCustomAttributes (MethodInfo method)
{
return method.ReturnParameter;
}
static bool IsSubclassOf (Type base_class, Type derived_class)
{
return derived_class.IsSubclassOf (base_class);
}
}
public static class AttributeConversionManager
{
public static System.Attribute ConvertPlatformAttribute (CustomAttributeData attribute, PlatformName platform)
{
var constructorArguments = new object [attribute.ConstructorArguments.Count];
for (int i = 0; i < attribute.ConstructorArguments.Count; ++i)
constructorArguments [i] = attribute.ConstructorArguments [i].Value;
Func<string> createErrorMessage = () => {
var b = new System.Text.StringBuilder (" Types { ");
for (int i = 0; i < constructorArguments.Length; ++i)
b.Append (constructorArguments[i].GetType ().ToString () + " ");
b.Append ("}");
return b.ToString ();
};
Func<string> unknownFormatError = () => $"Unknown format for old style availability attribute {attribute.AttributeType.FullName} {attribute.ConstructorArguments.Count} {createErrorMessage ()}";
object [] ctorValues;
System.Type [] ctorTypes;
switch (attribute.ConstructorArguments.Count) {
case 2:
if (constructorArguments [0].GetType () == typeof (byte) &&
constructorArguments [1].GetType () == typeof (byte)) {
ctorValues = new object [] { (byte)platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (byte)0xff, null };
ctorTypes = new System.Type [] { AttributeFactory.PlatformEnum, typeof (int), typeof (int), AttributeFactory.PlatformArch, typeof (string) };
break;
}
throw new NotImplementedException (unknownFormatError ());
case 3:
if (constructorArguments [0].GetType () == typeof (byte) &&
constructorArguments [1].GetType () == typeof (byte) &&
constructorArguments [2].GetType () == typeof (byte)) {
ctorValues = new object [] { (byte) platform, (int)(byte)constructorArguments [0], (int)(byte)constructorArguments [1], (int)(byte)constructorArguments [2], (byte) 0xff, null };
ctorTypes = new System.Type [] { AttributeFactory.PlatformEnum, typeof (int), typeof (int), typeof (int), AttributeFactory.PlatformArch, typeof (string) };
break;
}
if (constructorArguments [0].GetType () == typeof (byte) &&
constructorArguments [1].GetType () == typeof (byte) &&
constructorArguments [2].GetType () == typeof (bool)) {
byte arch = (bool) constructorArguments [2] ? (byte) 2 : (byte) 0xff;
ctorValues = new object [] { (byte)platform, (int)(byte) constructorArguments [0], (int)(byte)constructorArguments [1], arch, null };
ctorTypes = new System.Type [] { AttributeFactory.PlatformEnum, typeof (int), typeof (int), AttributeFactory.PlatformArch, typeof (string) };
break;
}
throw new NotImplementedException (unknownFormatError ());
case 4:
if (constructorArguments [0].GetType () == typeof (byte) &&
constructorArguments [1].GetType () == typeof (byte) &&
constructorArguments [2].GetType () == typeof (byte) &&
constructorArguments [3].GetType () == typeof (bool)) {
byte arch = (bool) constructorArguments [3] ? (byte) 2 : (byte) 0xff;
ctorValues = new object [] { (byte) platform, (int) (byte) constructorArguments [0], (int)(byte) constructorArguments [1], (int)(byte) constructorArguments [2], arch, null };
ctorTypes = new System.Type [] { AttributeFactory.PlatformEnum, typeof (int), typeof (int), typeof (int), AttributeFactory.PlatformArch, typeof (string) };
break;
}
if (constructorArguments [0].GetType () == typeof (byte) &&
constructorArguments [1].GetType () == typeof (byte) &&
constructorArguments [2].GetType () == typeof (byte) &&
constructorArguments [3].GetType () == typeof (byte) /* ObjCRuntime.PlatformArchitecture */) {
ctorValues = new object [] { (byte) platform, (int) (byte) constructorArguments [0], (int)(byte) constructorArguments [1], (int)(byte) constructorArguments [2], constructorArguments [3], null };
ctorTypes = new System.Type [] { AttributeFactory.PlatformEnum, typeof (int), typeof (int), typeof (int), AttributeFactory.PlatformArch, typeof (string) };
break;
}
throw new NotImplementedException (unknownFormatError ());
default:
throw new NotImplementedException ($"Unknown count {attribute.ConstructorArguments.Count} {createErrorMessage ()}");
}
return AttributeFactory.CreateNewAttribute (AttributeFactory.IntroducedAttributeType, ctorTypes, ctorValues);
}
struct ParsedAvailabilityInfo
{
public PlatformName Platform;
public int Major;
public int Minor;
public ParsedAvailabilityInfo (PlatformName platform, int major, int minor)
{
Platform = platform;
Major = major;
Minor = minor;
}
public ParsedAvailabilityInfo (PlatformName platform)
{
Platform = platform;
Major = -1;
Minor = -1;
}
}
static PlatformName ParsePlatforName (string s)
{
switch (s) {
case "iOS":
return PlatformName.iOS;
case "Mac":
return PlatformName.MacOSX;
case "Watch":
return PlatformName.WatchOS;
case "TV":
return PlatformName.TvOS;
default:
return PlatformName.None;
}
}
static ParsedAvailabilityInfo DetermineOldAvailabilityVersion (CustomAttributeNamedArgument arg)
{
string enumName = Enum.GetName (typeof (Platform), (ulong) arg.TypedValue.Value);
if (enumName == null)
throw new NotImplementedException ($"Unknown version format \"{enumName}\" in DetermineOldAvailabilityVersion. Are there two values | togeather?");
string [] enumParts = enumName.Split (new char [] { '_' });
switch (enumParts.Count ())
{
case 1:
if (enumName == "None")
return new ParsedAvailabilityInfo (PlatformName.None);
break;
case 2: {
if (enumParts [1] != "Version")
break;
PlatformName platform = ParsePlatforName (enumParts [0]);
if (platform == PlatformName.None)
break;
return new ParsedAvailabilityInfo (platform);
}
case 3: {
PlatformName platform = ParsePlatforName (enumParts [0]);
if (platform == PlatformName.None)
break;
int major = int.Parse (enumParts [1]);
int minor = int.Parse (enumParts [2]);
return new ParsedAvailabilityInfo (platform, major, minor);
}
}
throw new NotImplementedException ($"Unknown version format \"{enumName}\" in DetermineOldAvailabilityVersion");
}
public static IEnumerable<System.Attribute> ConvertAvailability (CustomAttributeData attribute)
{
string message = null;
if (attribute.NamedArguments.Any (x => x.MemberName == "Message"))
message = (string)attribute.NamedArguments.First (x => x.MemberName == "Message").TypedValue.Value;
foreach (var arg in attribute.NamedArguments) {
switch (arg.MemberName) {
case "Introduced": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateNewIntroducedAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Deprecated": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateDeprecatedAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Obsoleted": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateObsoletedAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message);
continue;
}
case "Unavailable": {
ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg);
yield return AttributeFactory.CreateUnavailableAttribute (availInfo.Platform, message: message);
continue;
}
case "Message":
continue;
default:
throw new NotImplementedException ($"ConvertAvailability found unknown named argument {arg.MemberName}");
}
}
}
}
static class AttributeFactory
{
public static System.Type PlatformEnum = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.PlatformName");
public static System.Type PlatformArch = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.PlatformArchitecture");
public static System.Type IntroducedAttributeType = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.IntroducedAttribute");
public static System.Type UnavailableAttributeType = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.UnavailableAttribute");
public static System.Type ObsoletedAttributeType = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.ObsoletedAttribute");
public static System.Type DeprecatedAttributeType = typeof (TypeManager).Assembly.GetType ("ObjCRuntime.DeprecatedAttribute");
public static System.Attribute CreateNewAttribute (System.Type attribType, System.Type [] ctorTypes, object [] ctorValues)
{
var ctor = attribType.GetConstructor (ctorTypes);
if (ctor == null)
throw ErrorHelper.CreateError (1058, attribType.FullName);
return (System.Attribute) ctor.Invoke (ctorValues);
}
static Attribute CreateMajorMinorAttribute (System.Type type, PlatformName platform, int major, int minor, byte arch, string message)
{
var ctorValues = new object [] { (byte)platform, major, minor, arch, message };
var ctorTypes = new System.Type [] { PlatformEnum, typeof (int), typeof (int), PlatformArch, typeof (string) };
return CreateNewAttribute (type, ctorTypes, ctorValues);
}
public static System.Attribute CreateNewIntroducedAttribute (PlatformName platform, int major, int minor, byte arch = 0xff, string message = null)
{
return CreateMajorMinorAttribute (IntroducedAttributeType, platform, major, minor, arch, message);
}
public static System.Attribute CreateObsoletedAttribute (PlatformName platform, int major, int minor, byte arch = 0xff, string message = null)
{
return CreateMajorMinorAttribute (ObsoletedAttributeType, platform, major, minor, arch, message);
}
public static System.Attribute CreateDeprecatedAttribute (PlatformName platform, int major, int minor, byte arch = 0xff, string message = null)
{
return CreateMajorMinorAttribute (DeprecatedAttributeType, platform, major, minor, arch, message);
}
public static System.Attribute CreateUnavailableAttribute (PlatformName platformName, byte arch = 0xff, string message = null)
{
var ctorValues = new object [] { (byte)platformName, arch, message };
var ctorTypes = new System.Type [] { PlatformEnum, PlatformArch, typeof (string) };
return CreateNewAttribute (UnavailableAttributeType, ctorTypes, ctorValues);
}
}
public static class EnumerableExtensions
{
public static IEnumerable<T> Yield<T> (this T item)
{
yield return item;
}
}