~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/reflect/Emit/EventBuilder.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                                lazyPseudoToken = token;
 
198
                        }
 
199
                        else
 
200
                        {
 
201
                                typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token);
 
202
                        }
 
203
 
 
204
                        foreach (Accessor acc in accessors)
 
205
                        {
 
206
                                AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token);
 
207
                        }
 
208
                }
 
209
 
 
210
                private void AddMethodSemantics(short semantics, int methodToken, int propertyToken)
 
211
                {
 
212
                        MethodSemanticsTable.Record rec = new MethodSemanticsTable.Record();
 
213
                        rec.Semantics = semantics;
 
214
                        rec.Method = methodToken;
 
215
                        rec.Association = propertyToken;
 
216
                        typeBuilder.ModuleBuilder.MethodSemantics.AddRecord(rec);
 
217
                }
 
218
 
 
219
                internal override bool IsPublic
 
220
                {
 
221
                        get
 
222
                        {
 
223
                                foreach (Accessor acc in accessors)
 
224
                                {
 
225
                                        if (acc.Method.IsPublic)
 
226
                                        {
 
227
                                                return true;
 
228
                                        }
 
229
                                }
 
230
                                return false;
 
231
                        }
 
232
                }
 
233
 
 
234
                internal override bool IsNonPrivate
 
235
                {
 
236
                        get
 
237
                        {
 
238
                                foreach (Accessor acc in accessors)
 
239
                                {
 
240
                                        if ((acc.Method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private)
 
241
                                        {
 
242
                                                return true;
 
243
                                        }
 
244
                                }
 
245
                                return false;
 
246
                        }
 
247
                }
 
248
 
 
249
                internal override bool IsStatic
 
250
                {
 
251
                        get
 
252
                        {
 
253
                                foreach (Accessor acc in accessors)
 
254
                                {
 
255
                                        if (acc.Method.IsStatic)
 
256
                                        {
 
257
                                                return true;
 
258
                                        }
 
259
                                }
 
260
                                return false;
 
261
                        }
 
262
                }
 
263
 
 
264
                internal override bool IsBaked
 
265
                {
 
266
                        get { return typeBuilder.IsBaked; }
 
267
                }
 
268
 
 
269
                internal override int GetCurrentToken()
 
270
                {
 
271
                        if (typeBuilder.ModuleBuilder.IsSaved && ModuleBuilder.IsPseudoToken(lazyPseudoToken))
 
272
                        {
 
273
                                return typeBuilder.ModuleBuilder.ResolvePseudoToken(lazyPseudoToken);
 
274
                        }
 
275
                        else
 
276
                        {
 
277
                                return lazyPseudoToken;
 
278
                        }
 
279
                }
 
280
        }
 
281
}