Merge pull request #301 from directhex/master
[mono.git] / mcs / class / IKVM.Reflection / Emit / EventBuilder.cs
1 /*
2   Copyright (C) 2009-2011 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Runtime.CompilerServices;
27 using IKVM.Reflection.Metadata;
28 using IKVM.Reflection.Writer;
29
30 namespace IKVM.Reflection.Emit
31 {
32         public sealed class EventBuilder : EventInfo
33         {
34                 private readonly TypeBuilder typeBuilder;
35                 private readonly string name;
36                 private EventAttributes attributes;
37                 private readonly int eventtype;
38                 private MethodBuilder addOnMethod;
39                 private MethodBuilder removeOnMethod;
40                 private MethodBuilder fireMethod;
41                 private readonly List<Accessor> accessors = new List<Accessor>();
42                 private int lazyPseudoToken;
43
44                 private struct Accessor
45                 {
46                         internal short Semantics;
47                         internal MethodBuilder Method;
48                 }
49
50                 internal EventBuilder(TypeBuilder typeBuilder, string name, EventAttributes attributes, Type eventtype)
51                 {
52                         this.typeBuilder = typeBuilder;
53                         this.name = name;
54                         this.attributes = attributes;
55                         this.eventtype = typeBuilder.ModuleBuilder.GetTypeTokenForMemberRef(eventtype);
56                 }
57
58                 public void SetAddOnMethod(MethodBuilder mdBuilder)
59                 {
60                         addOnMethod = mdBuilder;
61                         Accessor acc;
62                         acc.Semantics = MethodSemanticsTable.AddOn;
63                         acc.Method = mdBuilder;
64                         accessors.Add(acc);
65                 }
66
67                 public void SetRemoveOnMethod(MethodBuilder mdBuilder)
68                 {
69                         removeOnMethod = mdBuilder;
70                         Accessor acc;
71                         acc.Semantics = MethodSemanticsTable.RemoveOn;
72                         acc.Method = mdBuilder;
73                         accessors.Add(acc);
74                 }
75
76                 public void SetRaiseMethod(MethodBuilder mdBuilder)
77                 {
78                         fireMethod = mdBuilder;
79                         Accessor acc;
80                         acc.Semantics = MethodSemanticsTable.Fire;
81                         acc.Method = mdBuilder;
82                         accessors.Add(acc);
83                 }
84
85                 public void AddOtherMethod(MethodBuilder mdBuilder)
86                 {
87                         Accessor acc;
88                         acc.Semantics = MethodSemanticsTable.Other;
89                         acc.Method = mdBuilder;
90                         accessors.Add(acc);
91                 }
92
93                 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
94                 {
95                         SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
96                 }
97
98                 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
99                 {
100                         Universe u = typeBuilder.ModuleBuilder.universe;
101                         if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
102                         {
103                                 attributes |= EventAttributes.SpecialName;
104                         }
105                         else
106                         {
107                                 if (lazyPseudoToken == 0)
108                                 {
109                                         lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
110                                 }
111                                 typeBuilder.ModuleBuilder.SetCustomAttribute(lazyPseudoToken, customBuilder);
112                         }
113                 }
114
115                 public override EventAttributes Attributes
116                 {
117                         get { return attributes; }
118                 }
119
120                 public override MethodInfo GetAddMethod(bool nonPublic)
121                 {
122                         return nonPublic || (addOnMethod != null && addOnMethod.IsPublic) ? addOnMethod : null;
123                 }
124
125                 public override MethodInfo GetRemoveMethod(bool nonPublic)
126                 {
127                         return nonPublic || (removeOnMethod != null && removeOnMethod.IsPublic) ? removeOnMethod : null;
128                 }
129
130                 public override MethodInfo GetRaiseMethod(bool nonPublic)
131                 {
132                         return nonPublic || (fireMethod != null && fireMethod.IsPublic) ? fireMethod : null;
133                 }
134
135                 public override MethodInfo[] GetOtherMethods(bool nonPublic)
136                 {
137                         List<MethodInfo> list = new List<MethodInfo>();
138                         foreach (Accessor acc in accessors)
139                         {
140                                 if (acc.Semantics == MethodSemanticsTable.Other && (nonPublic || acc.Method.IsPublic))
141                                 {
142                                         list.Add(acc.Method);
143                                 }
144                         }
145                         return list.ToArray();
146                 }
147
148                 public override MethodInfo[] __GetMethods()
149                 {
150                         List<MethodInfo> list = new List<MethodInfo>();
151                         foreach (Accessor acc in accessors)
152                         {
153                                 list.Add(acc.Method);
154                         }
155                         return list.ToArray();
156                 }
157
158                 public override Type DeclaringType
159                 {
160                         get { return typeBuilder; }
161                 }
162
163                 public override string Name
164                 {
165                         get { return name; }
166                 }
167
168                 public override Module Module
169                 {
170                         get { return typeBuilder.ModuleBuilder; }
171                 }
172
173                 public EventToken GetEventToken()
174                 {
175                         if (lazyPseudoToken == 0)
176                         {
177                                 lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
178                         }
179                         return new EventToken(lazyPseudoToken);
180                 }
181
182                 public override Type EventHandlerType
183                 {
184                         get { return typeBuilder.ModuleBuilder.ResolveType(eventtype); }
185                 }
186
187                 internal void Bake()
188                 {
189                         EventTable.Record rec = new EventTable.Record();
190                         rec.EventFlags = (short)attributes;
191                         rec.Name = typeBuilder.ModuleBuilder.Strings.Add(name);
192                         rec.EventType = eventtype;
193                         int token = 0x14000000 | typeBuilder.ModuleBuilder.Event.AddRecord(rec);
194
195                         if (lazyPseudoToken != 0)
196                         {
197                                 typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token);
198                         }
199
200                         foreach (Accessor acc in accessors)
201                         {
202                                 AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token);
203                         }
204                 }
205
206                 private void AddMethodSemantics(short semantics, int methodToken, int propertyToken)
207                 {
208                         MethodSemanticsTable.Record rec = new MethodSemanticsTable.Record();
209                         rec.Semantics = semantics;
210                         rec.Method = methodToken;
211                         rec.Association = propertyToken;
212                         typeBuilder.ModuleBuilder.MethodSemantics.AddRecord(rec);
213                 }
214
215                 internal override bool IsPublic
216                 {
217                         get
218                         {
219                                 foreach (Accessor acc in accessors)
220                                 {
221                                         if (acc.Method.IsPublic)
222                                         {
223                                                 return true;
224                                         }
225                                 }
226                                 return false;
227                         }
228                 }
229
230                 internal override bool IsNonPrivate
231                 {
232                         get
233                         {
234                                 foreach (Accessor acc in accessors)
235                                 {
236                                         if ((acc.Method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private)
237                                         {
238                                                 return true;
239                                         }
240                                 }
241                                 return false;
242                         }
243                 }
244
245                 internal override bool IsStatic
246                 {
247                         get
248                         {
249                                 foreach (Accessor acc in accessors)
250                                 {
251                                         if (acc.Method.IsStatic)
252                                         {
253                                                 return true;
254                                         }
255                                 }
256                                 return false;
257                         }
258                 }
259         }
260 }