| 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 |
[email protected] |
| 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 |
if (customBuilder.KnownCA == KnownCA.SpecialNameAttribute) |
| 101 |
{ |
| 102 |
attributes |= EventAttributes.SpecialName; |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
if (lazyPseudoToken == 0) |
| 107 |
{ |
| 108 |
lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken(); |
| 109 |
} |
| 110 |
typeBuilder.ModuleBuilder.SetCustomAttribute(lazyPseudoToken, customBuilder); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public override EventAttributes Attributes |
| 115 |
{ |
| 116 |
get { return attributes; } |
| 117 |
} |
| 118 |
|
| 119 |
public override MethodInfo GetAddMethod(bool nonPublic) |
| 120 |
{ |
| 121 |
return nonPublic || (addOnMethod != null && addOnMethod.IsPublic) ? addOnMethod : null; |
| 122 |
} |
| 123 |
|
| 124 |
public override MethodInfo GetRemoveMethod(bool nonPublic) |
| 125 |
{ |
| 126 |
return nonPublic || (removeOnMethod != null && removeOnMethod.IsPublic) ? removeOnMethod : null; |
| 127 |
} |
| 128 |
|
| 129 |
public override MethodInfo GetRaiseMethod(bool nonPublic) |
| 130 |
{ |
| 131 |
return nonPublic || (fireMethod != null && fireMethod.IsPublic) ? fireMethod : null; |
| 132 |
} |
| 133 |
|
| 134 |
public override MethodInfo[] GetOtherMethods(bool nonPublic) |
| 135 |
{ |
| 136 |
List<MethodInfo> list = new List<MethodInfo>(); |
| 137 |
foreach (Accessor acc in accessors) |
| 138 |
{ |
| 139 |
if (acc.Semantics == MethodSemanticsTable.Other && (nonPublic || acc.Method.IsPublic)) |
| 140 |
{ |
| 141 |
list.Add(acc.Method); |
| 142 |
} |
| 143 |
} |
| 144 |
return list.ToArray(); |
| 145 |
} |
| 146 |
|
| 147 |
public override MethodInfo[] __GetMethods() |
| 148 |
{ |
| 149 |
List<MethodInfo> list = new List<MethodInfo>(); |
| 150 |
foreach (Accessor acc in accessors) |
| 151 |
{ |
| 152 |
list.Add(acc.Method); |
| 153 |
} |
| 154 |
return list.ToArray(); |
| 155 |
} |
| 156 |
|
| 157 |
public override Type DeclaringType |
| 158 |
{ |
| 159 |
get { return typeBuilder; } |
| 160 |
} |
| 161 |
|
| 162 |
public override string Name |
| 163 |
{ |
| 164 |
get { return name; } |
| 165 |
} |
| 166 |
|
| 167 |
public override Module Module |
| 168 |
{ |
| 169 |
get { return typeBuilder.ModuleBuilder; } |
| 170 |
} |
| 171 |
|
| 172 |
public EventToken GetEventToken() |
| 173 |
{ |
| 174 |
if (lazyPseudoToken == 0) |
| 175 |
{ |
| 176 |
lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken(); |
| 177 |
} |
| 178 |
return new EventToken(lazyPseudoToken); |
| 179 |
} |
| 180 |
|
| 181 |
public override Type EventHandlerType |
| 182 |
{ |
| 183 |
get { return typeBuilder.ModuleBuilder.ResolveType(eventtype); } |
| 184 |
} |
| 185 |
|
| 186 |
internal void Bake() |
| 187 |
{ |
| 188 |
EventTable.Record rec = new EventTable.Record(); |
| 189 |
rec.EventFlags = (short)attributes; |
| 190 |
rec.Name = typeBuilder.ModuleBuilder.Strings.Add(name); |
| 191 |
rec.EventType = eventtype; |
| 192 |
int token = 0x14000000 | typeBuilder.ModuleBuilder.Event.AddRecord(rec); |
| 193 |
|
| 194 |
if (lazyPseudoToken == 0) |
| 195 |
{ |
| 196 |
lazyPseudoToken = token; |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token); |
| 201 |
} |
| 202 |
|
| 203 |
foreach (Accessor acc in accessors) |
| 204 |
{ |
| 205 |
AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
private void AddMethodSemantics(short semantics, int methodToken, int propertyToken) |
| 210 |
{ |
| 211 |
MethodSemanticsTable.Record rec = new MethodSemanticsTable.Record(); |
| 212 |
rec.Semantics = semantics; |
| 213 |
rec.Method = methodToken; |
| 214 |
rec.Association = propertyToken; |
| 215 |
typeBuilder.ModuleBuilder.MethodSemantics.AddRecord(rec); |
| 216 |
} |
| 217 |
|
| 218 |
internal override bool IsPublic |
| 219 |
{ |
| 220 |
get |
| 221 |
{ |
| 222 |
foreach (Accessor acc in accessors) |
| 223 |
{ |
| 224 |
if (acc.Method.IsPublic) |
| 225 |
{ |
| 226 |
return true; |
| 227 |
} |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
internal override bool IsNonPrivate |
| 234 |
{ |
| 235 |
get |
| 236 |
{ |
| 237 |
foreach (Accessor acc in accessors) |
| 238 |
{ |
| 239 |
if ((acc.Method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private) |
| 240 |
{ |
| 241 |
return true; |
| 242 |
} |
| 243 |
} |
| 244 |
return false; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
internal override bool IsStatic |
| 249 |
{ |
| 250 |
get |
| 251 |
{ |
| 252 |
foreach (Accessor acc in accessors) |
| 253 |
{ |
| 254 |
if (acc.Method.IsStatic) |
| 255 |
{ |
| 256 |
return true; |
| 257 |
} |
| 258 |
} |
| 259 |
return false; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
internal override bool IsBaked |
| 264 |
{ |
| 265 |
get { return typeBuilder.IsBaked; } |
| 266 |
} |
| 267 |
|
| 268 |
internal override int GetCurrentToken() |
| 269 |
{ |
| 270 |
if (typeBuilder.ModuleBuilder.IsSaved && ModuleBuilder.IsPseudoToken(lazyPseudoToken)) |
| 271 |
{ |
| 272 |
return typeBuilder.ModuleBuilder.ResolvePseudoToken(lazyPseudoToken); |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
return lazyPseudoToken; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |