This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
/
generator.cs
4441 lines (3826 loc) · 147 KB
/
generator.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This is the binding generator for the MonoTouch API, it uses the
// contract in API.cs to generate the binding.
//
// Authors:
// Geoff Norton
// Miguel de Icaza
// Marek Safar (marek.safar@gmail.com)
//
// Copyright 2009-2010, Novell, Inc.
// Copyright 2011-2013 Xamarin, Inc.
//
//
// This generator produces various */*.g.cs files based on the
// interface-based type description on this file, see the
// embedded `MonoTouch.UIKit' namespace here for an example
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// TODO:
// * Add support for wrapping "ref" and "out" NSObjects (WrappedTypes)
// Typically this is necessary for things like NSError.
//
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.ComponentModel;
#if MONOMAC
using MonoMac.ObjCRuntime;
using MonoMac.Foundation;
using MonoMac.CoreFoundation;
using MonoMac.CoreGraphics;
using MonoMac.CoreVideo;
using MonoMac.OpenGL;
using MonoMac.CoreMidi;
using MonoMac.CoreMedia;
using DictionaryContainerType = MonoMac.Foundation.DictionaryContainer;
#else
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using MonoTouch.CoreFoundation;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreMedia;
using MonoTouch.CoreVideo;
using MonoTouch.CoreMidi;
using MonoTouch.MediaToolbox;
using DictionaryContainerType = MonoTouch.Foundation.DictionaryContainer;
#endif
public static class ReflectionExtensions {
public static Type GetBaseType (Type type)
{
object [] btype = type.GetCustomAttributes (typeof (BaseTypeAttribute), true);
BaseTypeAttribute bta = btype.Length > 0 ? ((BaseTypeAttribute) btype [0]) : null;
Type base_type = bta != null ? bta.BaseType : typeof (object);
return base_type;
}
public static List <PropertyInfo> GatherProperties (this Type type) {
List <PropertyInfo> properties = new List <PropertyInfo> (type.GetProperties ());
if (Generator.IsBtouch)
return properties;
Type parent_type = GetBaseType (type);
string owrap;
string nwrap;
if (parent_type != typeof (NSObject)) {
if (Attribute.IsDefined (parent_type, typeof (ModelAttribute), false)) {
foreach (PropertyInfo pinfo in parent_type.GetProperties ()) {
bool toadd = true;
var modelea = Generator.GetExportAttribute (pinfo, out nwrap);
if (modelea == null)
continue;
foreach (PropertyInfo exists in properties) {
var origea = Generator.GetExportAttribute (exists, out owrap);
if (origea.Selector == modelea.Selector)
toadd = false;
}
if (toadd)
properties.Add (pinfo);
}
}
parent_type = GetBaseType (parent_type);
}
return properties;
}
public static List <PropertyInfo> GatherProperties (this Type type, BindingFlags flags) {
List <PropertyInfo> properties = new List <PropertyInfo> (type.GetProperties (flags));
if (Generator.IsBtouch)
return properties;
Type parent_type = GetBaseType (type);
string owrap;
string nwrap;
if (parent_type != typeof (NSObject)) {
if (Attribute.IsDefined (parent_type, typeof (ModelAttribute), false)) {
foreach (PropertyInfo pinfo in parent_type.GetProperties (flags)) {
bool toadd = true;
var modelea = Generator.GetExportAttribute (pinfo, out nwrap);
if (modelea == null)
continue;
foreach (PropertyInfo exists in properties) {
var origea = Generator.GetExportAttribute (exists, out owrap);
if (origea.Selector == modelea.Selector)
toadd = false;
}
if (toadd)
properties.Add (pinfo);
}
}
parent_type = GetBaseType (parent_type);
}
return properties;
}
public static List <MethodInfo> GatherMethods (this Type type) {
List <MethodInfo> methods = new List <MethodInfo> (type.GetMethods ());
if (Generator.IsBtouch)
return methods;
Type parent_type = GetBaseType (type);
if (parent_type != typeof (NSObject)) {
if (Attribute.IsDefined (parent_type, typeof (ModelAttribute), false))
foreach (MethodInfo minfo in parent_type.GetMethods ())
if (minfo.GetCustomAttributes (typeof (ExportAttribute), false).Length > 0)
methods.Add (minfo);
parent_type = GetBaseType (parent_type);
}
return methods;
}
public static List <MethodInfo> GatherMethods (this Type type, BindingFlags flags) {
List <MethodInfo> methods = new List <MethodInfo> (type.GetMethods (flags));
if (Generator.IsBtouch)
return methods;
Type parent_type = GetBaseType (type);
if (parent_type != typeof (NSObject)) {
if (Attribute.IsDefined (parent_type, typeof (ModelAttribute), false))
foreach (MethodInfo minfo in parent_type.GetMethods ())
if (minfo.GetCustomAttributes (typeof (ExportAttribute), false).Length > 0)
methods.Add (minfo);
parent_type = GetBaseType (parent_type);
}
return methods;
}
}
// Used to mark if a type is not a wrapper type.
public class SyntheticAttribute : Attribute {
public SyntheticAttribute () { }
}
public class NeedsAuditAttribute : Attribute {
public NeedsAuditAttribute (string reason)
{
Reason = reason;
}
public string Reason { get; set; }
}
public class MarshalNativeExceptionsAttribute : Attribute {
}
public class RetainListAttribute : Attribute {
public RetainListAttribute (bool doadd, string name)
{
Add = doadd;
WrapName = name;
}
public string WrapName { get; set; }
public bool Add { get; set; }
}
public class RetainAttribute : Attribute {
public RetainAttribute ()
{
}
public RetainAttribute (string wrap)
{
WrapName = wrap;
}
public string WrapName { get; set; }
}
public class ReleaseAttribute : Attribute {
}
[AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
public class PostGetAttribute : Attribute {
public PostGetAttribute (string name)
{
MethodName = name;
}
public string MethodName { get; set; }
}
public class BaseTypeAttribute : Attribute {
public BaseTypeAttribute (Type t)
{
BaseType = t;
}
public Type BaseType { get; set; }
public string Name { get; set; }
public Type [] Events { get; set; }
public string [] Delegates { get; set; }
public bool Singleton { get; set; }
// If set, the code will keep a reference in the EnsureXXX method for
// delegates and will clear the reference to the object in the method
// referenced by KeepUntilRef. Currently uses an ArrayList, so this
// is not really designed as a workaround for systems that create
// too many objects, but two cases in particular that users keep
// trampling on: UIAlertView and UIActionSheet
public string KeepRefUntil { get; set; }
}
//
// Used for methods that invoke other targets, not this.Handle
//
public class BindAttribute : Attribute {
public BindAttribute (string sel)
{
Selector = sel;
}
public string Selector { get; set; }
// By default [Bind] makes non-virtual methods
public bool Virtual { get; set; }
}
public class WrapAttribute : Attribute {
public WrapAttribute (string methodname)
{
MethodName = methodname;
}
public string MethodName { get; set; }
}
// When applied instructs the generator to call Release on the returned objects
// this happens when factory methods in Objetive-C return objects with refcount=1
public class FactoryAttribute : Attribute {
public FactoryAttribute () {}
}
// When applied, it instructs the generator to not use NSStrings for marshalling.
public class PlainStringAttribute : Attribute {
public PlainStringAttribute () {}
}
public class AutoreleaseAttribute : Attribute {
public AutoreleaseAttribute () {}
}
// When applied, the generator generates a check for the Handle being valid on the main object, to
// ensure that the user did not Dispose() the object.
//
// This is typically used in scenarios where the user might be tempted to dispose
// the object in a callback:
//
// foo.FinishedDownloading += delegate { foo.Dispose (); }
//
// This would invalidate "foo" and force the code to return to a destroyed/freed
// object
public class CheckDisposedAttribute : Attribute {
public CheckDisposedAttribute () {}
}
//
// When applied, instructs the generator to use this object as the
// target, instead of the implicit Handle Can only be used in methods
// that are [Bind] instead of [Export]
//
public class TargetAttribute : Attribute {
public TargetAttribute () {}
}
public class ProxyAttribute : Attribute {
public ProxyAttribute () {}
}
// When applied to a member, generates the member as static
public class StaticAttribute : Attribute {
public StaticAttribute () {}
}
// flags the backing field for the property to with .NET's [ThreadStatic] property
public class IsThreadStaticAttribute : Attribute {
public IsThreadStaticAttribute () {}
}
// When applied to a member, generates the member as static
// and passes IntPtr.Zero or null if the parameter is null
public class NullAllowedAttribute : Attribute {
public NullAllowedAttribute () {}
}
// When applied to a method or property, flags the resulting generated code as internal
public class InternalAttribute : Attribute {
public InternalAttribute () {}
}
// When applied to a method or property, flags the resulting generated code as internal
public sealed class ProtectedAttribute : Attribute {
}
// When this attribute is applied to the interface definition it will
// flag the default constructor as private. This means that you can
// still instantiate object of this class internally from your
// extension file, but it just wont be accessible to users of your
// class.
public class PrivateDefaultCtorAttribute : DefaultCtorVisibilityAttribute {
public PrivateDefaultCtorAttribute () : base (Visibility.Private) {}
}
public enum Visibility {
Public,
Protected,
Internal,
ProtectedInternal,
Private,
Disabled
}
// When this attribute is applied to the interface definition it will
// flag the default ctor with the corresponding visibility (or disabled
// altogether if Visibility.Disabled is used).
public class DefaultCtorVisibilityAttribute : Attribute {
public DefaultCtorVisibilityAttribute (Visibility visibility)
{
this.Visibility = visibility;
}
public Visibility Visibility { get; set; }
}
// When this attribute is applied to the interface definition it will
// prevent the generator from producing the default constructor.
public class DisableDefaultCtorAttribute : DefaultCtorVisibilityAttribute {
public DisableDefaultCtorAttribute () : base (Visibility.Disabled) {}
}
//
// If this attribute is applied to a property, we do not generate a
// backing field. See bugzilla #3359 and Assistly 7032 for some
// background information
//
public class TransientAttribute : Attribute {
public TransientAttribute () {}
}
// Used for mandatory methods that must be implemented in a [Model].
public class AbstractAttribute : Attribute {
public AbstractAttribute () {}
}
// Used for mandatory methods that must be implemented in a [Model].
public class OverrideAttribute : Attribute {
public OverrideAttribute () {}
}
// Makes the result use the `new' attribtue
public class NewAttribute : Attribute {
public NewAttribute () {}
}
// Makes the result sealed
public class SealedAttribute : Attribute {
public SealedAttribute () {}
}
// Flags the object as being thread safe
public class ThreadSafeAttribute : Attribute {
public ThreadSafeAttribute () {}
}
// Marks a struct parameter/return value as requiring a certain alignment.
public class AlignAttribute : Attribute {
public int Align { get; set; }
public AlignAttribute (int align)
{
Align = align;
}
public int Bits {
get {
int bits = 0;
int tmp = Align;
while (tmp > 1) {
bits++;
tmp /= 2;
}
return bits;
}
}
}
//
// When applied, flags the [Flags] as a notification and generates the
// code to strongly type the notification.
//
// The type has information about the strong type notification, while the
// NotificationCenter if not null, indicates how to get the notification center.
//
// If you do not specify it, it will use NSNotificationCenter.DefaultCenter,
// you would typically use this to specify the code needed to get to it.
//
[AttributeUsage(AttributeTargets.Property, AllowMultiple=true)]
public class NotificationAttribute : Attribute {
public NotificationAttribute (Type t) { Type = t; }
public NotificationAttribute (Type t, string notificationCenter) { Type = t; NotificationCenter = notificationCenter; }
public NotificationAttribute (string notificationCenter) { NotificationCenter = notificationCenter; }
public NotificationAttribute () {}
public Type Type { get; set; }
public string NotificationCenter { get; set; }
}
//
// Applied to attributes in the notification EventArgs
// to generate code that merely probes for the existance of
// the key, instead of extracting a value out of the
// userInfo dictionary
//
[AttributeUsage(AttributeTargets.Property, AllowMultiple=true)]
public class ProbePresenceAttribute : Attribute {
public ProbePresenceAttribute () {}
}
public class EventArgsAttribute : Attribute {
public EventArgsAttribute (string s)
{
ArgName = s;
}
public EventArgsAttribute (string s, bool skip)
{
ArgName = s;
SkipGeneration = skip;
}
public EventArgsAttribute (string s, bool skip, bool fullname)
{
ArgName = s;
SkipGeneration = skip;
FullName = fullname;
}
public string ArgName { get; set; }
public bool SkipGeneration { get; set; }
public bool FullName { get; set; }
}
//
// Used to specify the delegate type that will be created when
// the generator creates the delegate properties on the host
// class that holds events
//
// example:
// interface SomeDelegate {
// [Export ("foo"), DelegateName ("GetBoolean"), DefaultValue (false)]
// bool Confirm (Some source);
//
public class DelegateNameAttribute : Attribute {
public DelegateNameAttribute (string s)
{
Name = s;
}
public string Name { get; set; }
}
public class EventNameAttribute : Attribute {
public EventNameAttribute (string s)
{
EvtName = s;
}
public string EvtName { get; set; }
}
public class DefaultValueAttribute : Attribute {
public DefaultValueAttribute (object o){
Default = o;
}
public object Default { get; set; }
}
public class DefaultValueFromArgumentAttribute : Attribute {
public DefaultValueFromArgumentAttribute (string s){
Argument = s;
}
public string Argument { get; set; }
}
public class NoDefaultValueAttribute : Attribute {
}
// Apply to strings parameters that are merely retained or assigned,
// not copied this is an exception as it is advised in the coding
// standard for Objective-C to avoid this, but a few properties do use
// this. Use this attribtue for properties flagged with `retain' or
// `assign', which look like this:
//
// @property (retain) NSString foo;
// @property (assign) NSString assigned;
//
// This forced the generator to create an NSString before calling the
// API instead of using the fast string marshalling code.
public class DisableZeroCopyAttribute : Attribute {
public DisableZeroCopyAttribute () {}
}
//
// By default, the generator will not do Zero Copying of strings, as most
// third party libraries do not follow Apple's design guidelines of making
// string properties and parameters copy parameters, instead many libraries
// "retain" as a broken optimization [1].
//
// The consumer of the genertor can force this by passing
// --use-zero-copy or setting the [assembly:ZeroCopyStrings] attribute.
// When these are set, the generator assumes the library perform
// copies over any NSStrings it keeps instead of retains/assigns and
// that any property that happens to be a retain/assign has the
// [DisableZeroCopyAttribute] attribute applied.
//
// [1] It is broken becase consumer code can pass an NSMutableString, the
// library retains the value, but does not have a way of noticing changes
// that might happen to the mutable string behind its back.
//
// In the ZeroCopy case it is a problem because we pass handles to stack-allocated
// strings that stop existing after the invocation is over.
//
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Method|AttributeTargets.Interface, AllowMultiple=true)]
public class ZeroCopyStringsAttribute : Attribute {
}
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property, AllowMultiple=true)]
public class SnippetAttribute : Attribute {
public SnippetAttribute (string s)
{
Code = s;
}
public string Code { get; set; }
}
//
// PreSnippet code is inserted after the parameters have been validated/marshalled
//
public class PreSnippetAttribute : SnippetAttribute {
public PreSnippetAttribute (string s) : base (s) {}
}
//
// PrologueSnippet code is inserted before any code is generated
//
public class PrologueSnippetAttribute : SnippetAttribute {
public PrologueSnippetAttribute (string s) : base (s) {}
}
//
// PostSnippet code is inserted before returning, before paramters are disposed/released
//
public class PostSnippetAttribute : SnippetAttribute {
public PostSnippetAttribute (string s) : base (s) {}
}
//
// Code to run from a generated Dispose method
//
[AttributeUsage(AttributeTargets.Interface, AllowMultiple=true)]
public class DisposeAttribute : SnippetAttribute {
public DisposeAttribute (string s) : base (s) {}
}
//
// This attribute is used to flag properties that should be exposed on the strongly typed
// nested Appearance class. It is usually a superset of what Apple has labeled with
// UI_APPEARANCE_SELECTOR because they do support more selectors than those flagged in
// the UIApperance proxies, so we must label all the options. This will be a list that
// is organically grown as we find them
//
[AttributeUsage (AttributeTargets.Property|AttributeTargets.Method, AllowMultiple=false)]
public class AppearanceAttribute : Attribute {
public AppearanceAttribute () {}
}
//
// This is designed to be applied to setter methods in
// a base class `Foo' when a `MutableFoo' exists.
//
// This allows the Foo.set_XXX to exists but throw an exception
// but derived classes would then override the property
//
[AttributeUsage (AttributeTargets.Method, AllowMultiple=false)]
public class NotImplementedAttribute : Attribute {
public NotImplementedAttribute () {}
}
//
// Apply this attribute to a class to add methods that in Objective-c
// are added as categories
//
// Use the BaseType attribute to reference which class this is extending
//
// Like this:
// [Category]
// [BaseType (typeof (UIView))]
// interface UIViewExtensions {
// [Export ("method_in_the_objective_c_category")]
// void ThisWillBecome_a_c_sharp_extension_method_in_class_UIViewExtensions ();
// }
[AttributeUsage (AttributeTargets.Interface, AllowMultiple=false)]
public class CategoryAttribute : Attribute {
public CategoryAttribute () {}
}
//
// Apply this attribute to a method that you want an async version of a callback method.
//
// Use the ResultType or ResultTypeName attribute to describe any composite value to be by the Task object.
// Use MethodName to customize the name of the generated method
//
// Note that this only supports the case where the callback is the last parameter of the method.
//
// Like this:
//[Export ("saveAccount:withCompletionHandler:")] [Async]
//void SaveAccount (ACAccount account, ACAccountStoreSaveCompletionHandler completionHandler);
// }
[AttributeUsage (AttributeTargets.Method, AllowMultiple=false)]
public class AsyncAttribute : Attribute {
//This will automagically generate the async method.
//This works with 4 kinds of callbacks: (), (NSError), (result), (result, NSError)
public AsyncAttribute () {}
//This works with 2 kinds of callbacks: (...) and (..., NSError).
//Parameters are passed in order to a constructor in resultType
public AsyncAttribute (Type resultType) {
ResultType = resultType;
}
//This works with 2 kinds of callbacks: (...) and (..., NSError).
//Parameters are passed in order to a result type that is automatically created if size > 1
//The generated method is named after the @methodName
public AsyncAttribute (string methodName) {
MethodName = methodName;
}
public Type ResultType { get; set; }
public string MethodName { get; set; }
public string ResultTypeName { get; set; }
}
//
// Used to encapsulate flags about types in either the parameter or the return value
// For now, it only supports the [PlainString] attribute on strings.
//
public class MarshalInfo {
public bool PlainString;
public Type Type;
// This is set on a string parameter if the argument parameters are set to
// Copy. This means that we can do fast string passing.
public bool ZeroCopyStringMarshal;
public bool IsAligned;
// Used for parameters
public MarshalInfo (MethodInfo mi, ParameterInfo pi)
{
PlainString = pi.GetCustomAttributes (typeof (PlainStringAttribute), true).Length > 0;
Type = pi.ParameterType;
ZeroCopyStringMarshal = (Type == typeof (string)) && PlainString == false && !Generator.HasAttribute (pi, (typeof (DisableZeroCopyAttribute))) && Generator.SharedGenerator.type_wants_zero_copy;
IsAligned = Generator.HasAttribute (pi, typeof (AlignAttribute));
if (IsAligned)
Type = typeof (IntPtr);
if (ZeroCopyStringMarshal && Generator.HasAttribute (mi, typeof (DisableZeroCopyAttribute)))
ZeroCopyStringMarshal = false;
}
// Used to return values
public MarshalInfo (MethodInfo mi)
{
PlainString = mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (PlainStringAttribute), true).Length > 0;
Type = mi.ReturnType;
IsAligned = Generator.HasAttribute (mi, typeof (AlignAttribute));
if (IsAligned)
Type = typeof (IntPtr);
}
public static bool UseString (MethodInfo mi, ParameterInfo pi)
{
return new MarshalInfo (mi, pi).PlainString;
}
public static implicit operator MarshalInfo (MethodInfo mi)
{
return new MarshalInfo (mi);
}
}
public class Tuple<A,B> {
public Tuple (A a, B b)
{
Item1 = a;
Item2 = b;
}
public A Item1;
public B Item2;
}
//
// Encapsulates the information necessary to create a block delegate
//
// The Name is the internal generated name we use for the delegate
// The Parameters is used for the internal delegate signature
// The Invoke contains the invocation steps necessary to invoke the method
//
public class TrampolineInfo {
public string UserDelegate, DelegateName, TrampolineName, Parameters, Invoke, ReturnType, DelegateReturnType, ReturnFormat, Clear;
public TrampolineInfo (string userDelegate, string delegateName, string trampolineName, string pars, string invoke, string returnType, string delegateReturnType, string returnFormat, string clear)
{
UserDelegate = userDelegate;
DelegateName = delegateName;
Parameters = pars;
TrampolineName = trampolineName;
Invoke = invoke;
ReturnType = returnType;
DelegateReturnType = delegateReturnType;
ReturnFormat = returnFormat;
Clear = clear;
}
public string StaticName {
get {
return "S" + DelegateName;
}
}
}
//
// This class is used to generate a graph of the type hierarchy of the
// generated types and required by the UIApperance support to determine
// which types need to have Appearance methods created
//
public class GeneratedType {
static Dictionary<Type,GeneratedType> knownTypes = new Dictionary<Type,GeneratedType> ();
public static GeneratedType Lookup (Type t)
{
if (knownTypes.ContainsKey (t))
return knownTypes [t];
var n = new GeneratedType (t);
knownTypes [t] = n;
return n;
}
public GeneratedType (Type t)
{
Type = t;
foreach (var iface in Type.GetInterfaces ()){
if (iface.Name == "UIAppearance")
ImplementsAppearance = true;
}
var btype = ReflectionExtensions.GetBaseType (Type);
if (btype != typeof (object)){
Parent = btype;
ParentGenerated = Lookup (Parent);
// If our parent had UIAppearance, we flag this class as well
if (ParentGenerated.ImplementsAppearance)
ImplementsAppearance = true;
ParentGenerated.Children.Add (this);
}
if (t.GetCustomAttributes (typeof (CategoryAttribute), true).Length != 0)
ImplementsAppearance = false;
}
public Type Type;
public List<GeneratedType> Children = new List<GeneratedType> (1);
public Type Parent;
public GeneratedType ParentGenerated;
public bool ImplementsAppearance;
List<MemberInfo> appearance_selectors;
public List<MemberInfo> AppearanceSelectors {
get {
if (appearance_selectors == null)
appearance_selectors = new List<MemberInfo> ();
return appearance_selectors;
}
}
}
public class MemberInformation
{
public readonly bool is_abstract, is_protected, is_internal, is_override, is_new, is_sealed, is_static, is_thread_static, is_autorelease, is_wrapper;
public readonly Generator.ThreadCheck threadCheck;
public bool is_unsafe, is_virtual_method, is_export, is_category_extension, is_variadic;
public string selector, wrap_method;
MemberInformation (MemberInfo mi, Type type)
{
is_abstract = Generator.HasAttribute (mi, typeof (AbstractAttribute)) && mi.DeclaringType == type;
is_protected = Generator.HasAttribute (mi, typeof (ProtectedAttribute));
is_internal = Generator.HasAttribute (mi, typeof (InternalAttribute));
is_override = Generator.HasAttribute (mi, typeof (OverrideAttribute)) || !Generator.MemberBelongsToType (mi.DeclaringType, type);
is_new = Generator.HasAttribute (mi, typeof (NewAttribute));
is_sealed = Generator.HasAttribute (mi, typeof (SealedAttribute));
is_static = Generator.HasAttribute (mi, typeof (StaticAttribute));
is_thread_static = Generator.HasAttribute (mi, typeof (IsThreadStaticAttribute));
is_autorelease = Generator.HasAttribute (mi, typeof (AutoreleaseAttribute));
is_wrapper = !Generator.HasAttribute (mi.DeclaringType, typeof(SyntheticAttribute));
threadCheck = Generator.HasAttribute (mi, typeof (ThreadSafeAttribute)) ? Generator.ThreadCheck.Off : Generator.ThreadCheck.On;
}
public MemberInformation (MethodInfo mi, Type type, Type category_extension_type) : this ((MemberInfo)mi, type)
{
foreach (ParameterInfo pi in mi.GetParameters ())
if (pi.ParameterType.IsSubclassOf (typeof (Delegate)))
is_unsafe = true;
object [] attr = mi.GetCustomAttributes (typeof (ExportAttribute), true);
if (attr.Length != 1){
attr = mi.GetCustomAttributes (typeof (BindAttribute), true);
if (attr.Length != 1) {
attr = mi.GetCustomAttributes (typeof (WrapAttribute), true);
if (attr.Length != 1)
throw new BindingException (1012, true, "No Export or Bind attribute defined on {0}.{1}", type, mi.Name);
wrap_method = ((WrapAttribute) attr [0]).MethodName;
} else {
BindAttribute ba = (BindAttribute) attr [0];
selector = ba.Selector;
is_virtual_method = ba.Virtual;
}
} else {
ExportAttribute ea = (ExportAttribute) attr [0];
selector = ea.Selector;
is_variadic = ea.IsVariadic;
if (!is_sealed || !is_wrapper) {
is_virtual_method = mi.Name != "Constructor";
is_export = true;
}
}
if (category_extension_type != null)
is_category_extension = true;
if (is_static || is_category_extension)
is_virtual_method = false;
}
public MemberInformation (PropertyInfo pi, Type type) : this ((MemberInfo)pi, type)
{
if (pi.PropertyType.IsSubclassOf (typeof (Delegate)))
is_unsafe = true;
var export = Generator.GetExportAttribute (pi, out wrap_method);
if (export != null)
selector = export.Selector;
if (wrap_method != null)
is_virtual_method = false;
else
is_virtual_method = !is_static;
}
public string GetVisibility ()
{
var mod = is_protected ? "protected" : null;
mod += is_internal ? "internal" : null;
if (string.IsNullOrEmpty (mod))
mod = "public";
return mod;
}
public string GetModifiers ()
{
string mods = "";
mods += is_unsafe ? "unsafe " : null;
mods += is_new ? "new " : "";
if (is_sealed) {
mods += "";
} else if (is_static || is_category_extension) {
mods += "static ";
} else if (is_abstract) {
mods += "abstract ";
} else if (is_virtual_method) {
mods += is_override ? "override " : "virtual ";
}
return mods;
}
}
public class Generator {
internal static bool IsBtouch;
Dictionary<Type,IEnumerable<string>> selectors = new Dictionary<Type,IEnumerable<string>> ();
Dictionary<Type,bool> need_static = new Dictionary<Type,bool> ();
Dictionary<Type,bool> need_abstract = new Dictionary<Type,bool> ();
Dictionary<string,int> selector_use = new Dictionary<string, int> ();
Dictionary<string,string> selector_names = new Dictionary<string,string> ();
Dictionary<string,string> send_methods = new Dictionary<string,string> ();
Dictionary<string,string> original_methods = new Dictionary<string,string> ();
List<MarshalType> marshal_types = new List<MarshalType> ();
Dictionary<Type,TrampolineInfo> trampolines = new Dictionary<Type,TrampolineInfo> ();
Dictionary<Type,int> trampolines_generic_versions = new Dictionary<Type,int> ();
Dictionary<Type,Type> delegates_emitted = new Dictionary<Type, Type> ();
Dictionary<Type,Type> notification_event_arg_types = new Dictionary<Type,Type> ();
List <string> libraries = new List <string> ();
List<Tuple<string, ParameterInfo[]>> async_result_types = new List<Tuple <string, ParameterInfo[]>> ();
HashSet<string> async_result_types_emitted = new HashSet<string> ();
//
// This contains delegates that are referenced in the source and need to be generated.
//
Dictionary<string,MethodInfo> delegate_types = new Dictionary<string,MethodInfo> ();
public bool Alpha;
public bool OnlyX86;
Type [] types;
bool debug;
bool external;
StreamWriter sw, m;
int indent;
public class MarshalType {
public Type Type;
public string Encoding;
public string ParameterMarshal;
public string CreateFromRet;
public MarshalType (Type t, string encode, string fetch, string create)
{
Type = t;
Encoding = encode;
ParameterMarshal = fetch;
CreateFromRet = create;
}
}