~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Mono.Cecil/Mono.Cecil/Mono.Cecil/MethodDefinition.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using Mono.Cecil.Cil;
 
30
using Mono.Collections.Generic;
 
31
 
 
32
using RVA = System.UInt32;
 
33
 
29
34
namespace Mono.Cecil {
30
35
 
31
 
        using Mono.Cecil.Binary;
32
 
        using Mono.Cecil.Cil;
33
 
 
34
 
        public sealed class MethodDefinition : MethodReference, IMemberDefinition,
35
 
                IHasSecurity, ICustomAttributeProvider {
36
 
 
37
 
                public const string Cctor = ".cctor";
38
 
                public const string Ctor = ".ctor";
39
 
 
40
 
                MethodAttributes m_attributes;
41
 
                MethodImplAttributes m_implAttrs;
42
 
                MethodSemanticsAttributes m_semAttrs;
43
 
                SecurityDeclarationCollection m_secDecls;
44
 
                CustomAttributeCollection m_customAttrs;
45
 
 
46
 
                MethodBody m_body;
47
 
                RVA m_rva;
48
 
                OverrideCollection m_overrides;
49
 
                PInvokeInfo m_pinvoke;
50
 
                readonly ParameterDefinition m_this;
 
36
        public sealed class MethodDefinition : MethodReference, IMemberDefinition, ISecurityDeclarationProvider {
 
37
 
 
38
                ushort attributes;
 
39
                ushort impl_attributes;
 
40
                internal MethodSemanticsAttributes? sem_attrs;
 
41
                Collection<CustomAttribute> custom_attributes;
 
42
                Collection<SecurityDeclaration> security_declarations;
 
43
 
 
44
                internal RVA rva;
 
45
                internal PInvokeInfo pinvoke;
 
46
                Collection<MethodReference> overrides;
 
47
 
 
48
                internal MethodBody body;
51
49
 
52
50
                public MethodAttributes Attributes {
53
 
                        get { return m_attributes; }
54
 
                        set { m_attributes = value; }
 
51
                        get { return (MethodAttributes) attributes; }
 
52
                        set { attributes = (ushort) value; }
55
53
                }
56
54
 
57
55
                public MethodImplAttributes ImplAttributes {
58
 
                        get { return m_implAttrs; }
59
 
                        set { m_implAttrs = value; }
 
56
                        get { return (MethodImplAttributes) impl_attributes; }
 
57
                        set { impl_attributes = (ushort) value; }
60
58
                }
61
59
 
62
60
                public MethodSemanticsAttributes SemanticsAttributes {
63
 
                        get { return m_semAttrs; }
64
 
                        set { m_semAttrs = value; }
 
61
                        get {
 
62
                                if (sem_attrs.HasValue)
 
63
                                        return sem_attrs.Value;
 
64
 
 
65
                                if (HasImage) {
 
66
                                        ReadSemantics ();
 
67
                                        return sem_attrs.Value;
 
68
                                }
 
69
 
 
70
                                sem_attrs = MethodSemanticsAttributes.None;
 
71
                                return sem_attrs.Value;
 
72
                        }
 
73
                        set { sem_attrs = value; }
 
74
                }
 
75
 
 
76
                internal void ReadSemantics ()
 
77
                {
 
78
                        if (sem_attrs.HasValue)
 
79
                                return;
 
80
 
 
81
                        var module = this.Module;
 
82
                        if (module == null)
 
83
                                return;
 
84
 
 
85
                        if (!module.HasImage)
 
86
                                return;
 
87
 
 
88
                        module.Read (this, (method, reader) => reader.ReadAllSemantics (method));
65
89
                }
66
90
 
67
91
                public bool HasSecurityDeclarations {
68
 
                        get { return (m_secDecls == null) ? false : (m_secDecls.Count > 0); }
69
 
                }
70
 
 
71
 
                public SecurityDeclarationCollection SecurityDeclarations {
72
92
                        get {
73
 
                                if (m_secDecls == null)
74
 
                                        m_secDecls = new SecurityDeclarationCollection (this);
 
93
                                if (security_declarations != null)
 
94
                                        return security_declarations.Count > 0;
75
95
 
76
 
                                return m_secDecls;
 
96
                                return this.GetHasSecurityDeclarations (Module);
77
97
                        }
78
98
                }
79
99
 
 
100
                public Collection<SecurityDeclaration> SecurityDeclarations {
 
101
                        get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (Module)); }
 
102
                }
 
103
 
80
104
                public bool HasCustomAttributes {
81
 
                        get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
82
 
                }
83
 
 
84
 
                public CustomAttributeCollection CustomAttributes {
85
 
                        get {
86
 
                                if (m_customAttrs == null)
87
 
                                        m_customAttrs = new CustomAttributeCollection (this);
88
 
 
89
 
                                return m_customAttrs;
90
 
                        }
91
 
                }
92
 
 
93
 
                public RVA RVA {
94
 
                        get { return m_rva; }
95
 
                        set { m_rva = value; }
 
105
                        get {
 
106
                                if (custom_attributes != null)
 
107
                                        return custom_attributes.Count > 0;
 
108
 
 
109
                                return this.GetHasCustomAttributes (Module);
 
110
                        }
 
111
                }
 
112
 
 
113
                public Collection<CustomAttribute> CustomAttributes {
 
114
                        get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
 
115
                }
 
116
 
 
117
                public int RVA {
 
118
                        get { return (int) rva; }
 
119
                }
 
120
 
 
121
                public bool HasBody {
 
122
                        get {
 
123
                                return (attributes & (ushort) MethodAttributes.Abstract) == 0 &&
 
124
                                        (attributes & (ushort) MethodAttributes.PInvokeImpl) == 0 &&
 
125
                                        (impl_attributes & (ushort) MethodImplAttributes.InternalCall) == 0 &&
 
126
                                        (impl_attributes & (ushort) MethodImplAttributes.Native) == 0 &&
 
127
                                        (impl_attributes & (ushort) MethodImplAttributes.Unmanaged) == 0 &&
 
128
                                        (impl_attributes & (ushort) MethodImplAttributes.Runtime) == 0;
 
129
                        }
96
130
                }
97
131
 
98
132
                public MethodBody Body {
99
133
                        get {
100
 
                                LoadBody ();
101
 
                                return m_body;
102
 
                        }
103
 
                        set { m_body = value; }
 
134
                                if (body != null)
 
135
                                        return body;
 
136
 
 
137
                                if (!HasBody)
 
138
                                        return null;
 
139
 
 
140
                                if (HasImage && rva != 0)
 
141
                                        return body = Module.Read (this, (method, reader) => reader.ReadMethodBody (method));
 
142
 
 
143
                                return body = new MethodBody (this);
 
144
                        }
 
145
                        set { body = value; }
 
146
                }
 
147
 
 
148
                public bool HasPInvokeInfo {
 
149
                        get {
 
150
                                if (pinvoke != null)
 
151
                                        return true;
 
152
 
 
153
                                return IsPInvokeImpl;
 
154
                        }
104
155
                }
105
156
 
106
157
                public PInvokeInfo PInvokeInfo {
107
 
                        get { return m_pinvoke; }
108
 
                        set { m_pinvoke = value; }
 
158
                        get {
 
159
                                if (pinvoke != null)
 
160
                                        return pinvoke;
 
161
 
 
162
                                if (HasImage && IsPInvokeImpl)
 
163
                                        return pinvoke = Module.Read (this, (method, reader) => reader.ReadPInvokeInfo (method));
 
164
 
 
165
                                return null;
 
166
                        }
 
167
                        set {
 
168
                                IsPInvokeImpl = true;
 
169
                                pinvoke = value;
 
170
                        }
109
171
                }
110
172
 
111
173
                public bool HasOverrides {
112
 
                        get { return (m_overrides == null) ? false : (m_overrides.Count > 0); }
113
 
                }
114
 
 
115
 
                public OverrideCollection Overrides {
116
 
                        get {
117
 
                                if (m_overrides == null)
118
 
                                        m_overrides = new OverrideCollection (this);
119
 
 
120
 
                                return m_overrides;
121
 
                        }
122
 
                }
123
 
 
124
 
                public ParameterDefinition This {
125
 
                        get { return m_this; }
 
174
                        get {
 
175
                                if (overrides != null)
 
176
                                        return overrides.Count > 0;
 
177
 
 
178
                                if (HasImage)
 
179
                                        return Module.Read (this, (method, reader) => reader.HasOverrides (method));
 
180
 
 
181
                                return false;
 
182
                        }
 
183
                }
 
184
 
 
185
                public Collection<MethodReference> Overrides {
 
186
                        get {
 
187
                                if (overrides != null)
 
188
                                        return overrides;
 
189
 
 
190
                                if (HasImage)
 
191
                                        return overrides = Module.Read (this, (method, reader) => reader.ReadOverrides (method));
 
192
 
 
193
                                return overrides = new Collection<MethodReference> ();
 
194
                        }
 
195
                }
 
196
 
 
197
                public override bool HasGenericParameters {
 
198
                        get {
 
199
                                if (generic_parameters != null)
 
200
                                        return generic_parameters.Count > 0;
 
201
 
 
202
                                return this.GetHasGenericParameters (Module);
 
203
                        }
 
204
                }
 
205
 
 
206
                public override Collection<GenericParameter> GenericParameters {
 
207
                        get { return generic_parameters ?? (generic_parameters = this.GetGenericParameters (Module)); }
126
208
                }
127
209
 
128
210
                #region MethodAttributes
129
211
 
130
212
                public bool IsCompilerControlled {
131
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Compilercontrolled; }
132
 
                        set {
133
 
                                if (value) {
134
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
135
 
                                        m_attributes |= MethodAttributes.Compilercontrolled;
136
 
                                } else
137
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.Compilercontrolled);
138
 
                        }
 
213
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.CompilerControlled); }
 
214
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.CompilerControlled, value); }
139
215
                }
140
216
 
141
217
                public bool IsPrivate {
142
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private; }
143
 
                        set {
144
 
                                if (value) {
145
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
146
 
                                        m_attributes |= MethodAttributes.Private;
147
 
                                } else
148
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.Private);
149
 
                        }
 
218
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Private); }
 
219
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Private, value); }
150
220
                }
151
221
 
152
222
                public bool IsFamilyAndAssembly {
153
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem; }
154
 
                        set {
155
 
                                if (value) {
156
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
157
 
                                        m_attributes |= MethodAttributes.FamANDAssem;
158
 
                                } else
159
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.FamANDAssem);
160
 
                        }
 
223
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamANDAssem); }
 
224
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamANDAssem, value); }
161
225
                }
162
226
 
163
227
                public bool IsAssembly {
164
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assem; }
165
 
                        set {
166
 
                                if (value) {
167
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
168
 
                                        m_attributes |= MethodAttributes.Assem;
169
 
                                } else
170
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.Assem);
171
 
                        }
 
228
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Assembly); }
 
229
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Assembly, value); }
172
230
                }
173
231
 
174
232
                public bool IsFamily {
175
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family; }
176
 
                        set {
177
 
                                if (value) {
178
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
179
 
                                        m_attributes |= MethodAttributes.Family;
180
 
                                } else
181
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.Family);
182
 
                        }
 
233
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Family); }
 
234
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Family, value); }
183
235
                }
184
236
 
185
237
                public bool IsFamilyOrAssembly {
186
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem; }
187
 
                        set {
188
 
                                if (value) {
189
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
190
 
                                        m_attributes |= MethodAttributes.FamORAssem;
191
 
                                } else
192
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.FamORAssem);
193
 
                        }
 
238
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamORAssem); }
 
239
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamORAssem, value); }
194
240
                }
195
241
 
196
242
                public bool IsPublic {
197
 
                        get { return (m_attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public; }
198
 
                        set {
199
 
                                if (value) {
200
 
                                        m_attributes &= ~MethodAttributes.MemberAccessMask;
201
 
                                        m_attributes |= MethodAttributes.Public;
202
 
                                } else
203
 
                                        m_attributes &= ~(MethodAttributes.MemberAccessMask & MethodAttributes.Public);
204
 
                        }
 
243
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Public); }
 
244
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Public, value); }
205
245
                }
206
246
 
207
247
                public bool IsStatic {
208
 
                        get { return (m_attributes & MethodAttributes.Static) != 0; }
209
 
                        set {
210
 
                                if (value)
211
 
                                        m_attributes |= MethodAttributes.Static;
212
 
                                else
213
 
                                        m_attributes &= ~MethodAttributes.Static;
214
 
                        }
 
248
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.Static); }
 
249
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Static, value); }
215
250
                }
216
251
 
217
252
                public bool IsFinal {
218
 
                        get { return (m_attributes & MethodAttributes.Final) != 0; }
219
 
                        set {
220
 
                                if (value)
221
 
                                        m_attributes |= MethodAttributes.Final;
222
 
                                else
223
 
                                        m_attributes &= ~MethodAttributes.Final;
224
 
                        }
 
253
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.Final); }
 
254
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Final, value); }
225
255
                }
226
256
 
227
257
                public bool IsVirtual {
228
 
                        get { return (m_attributes & MethodAttributes.Virtual) != 0; }
229
 
                        set {
230
 
                                if (value)
231
 
                                        m_attributes |= MethodAttributes.Virtual;
232
 
                                else
233
 
                                        m_attributes &= ~MethodAttributes.Virtual;
234
 
                        }
 
258
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.Virtual); }
 
259
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Virtual, value); }
235
260
                }
236
261
 
237
262
                public bool IsHideBySig {
238
 
                        get { return (m_attributes & MethodAttributes.HideBySig) != 0; }
239
 
                        set {
240
 
                                if (value)
241
 
                                        m_attributes |= MethodAttributes.HideBySig;
242
 
                                else
243
 
                                        m_attributes &= ~MethodAttributes.HideBySig;
244
 
                        }
 
263
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.HideBySig); }
 
264
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.HideBySig, value); }
245
265
                }
246
266
 
247
267
                public bool IsReuseSlot {
248
 
                        get { return (m_attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot; }
249
 
                        set {
250
 
                                if (value) {
251
 
                                        m_attributes &= ~MethodAttributes.VtableLayoutMask;
252
 
                                        m_attributes |= MethodAttributes.ReuseSlot;
253
 
                                } else
254
 
                                        m_attributes &= ~(MethodAttributes.VtableLayoutMask & MethodAttributes.ReuseSlot);
255
 
                        }
 
268
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.ReuseSlot); }
 
269
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.ReuseSlot, value); }
256
270
                }
257
271
 
258
272
                public bool IsNewSlot {
259
 
                        get { return (m_attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot; }
260
 
                        set {
261
 
                                if (value) {
262
 
                                        m_attributes &= ~MethodAttributes.VtableLayoutMask;
263
 
                                        m_attributes |= MethodAttributes.NewSlot;
264
 
                                } else
265
 
                                        m_attributes &= ~(MethodAttributes.VtableLayoutMask & MethodAttributes.NewSlot);
266
 
                        }
 
273
                        get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.NewSlot); }
 
274
                        set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.NewSlot, value); }
267
275
                }
268
276
 
269
 
                public bool IsStrict {
270
 
                        get { return (m_attributes & MethodAttributes.Strict) != 0; }
271
 
                        set {
272
 
                                if (value)
273
 
                                        m_attributes |= MethodAttributes.Strict;
274
 
                                else
275
 
                                        m_attributes &= ~MethodAttributes.Strict;
276
 
                        }
 
277
                public bool IsCheckAccessOnOverride {
 
278
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.CheckAccessOnOverride); }
 
279
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.CheckAccessOnOverride, value); }
277
280
                }
278
281
 
279
282
                public bool IsAbstract {
280
 
                        get { return (m_attributes & MethodAttributes.Abstract) != 0; }
281
 
                        set {
282
 
                                if (value)
283
 
                                        m_attributes |= MethodAttributes.Abstract;
284
 
                                else
285
 
                                        m_attributes &= ~MethodAttributes.Abstract;
286
 
                        }
 
283
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.Abstract); }
 
284
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Abstract, value); }
287
285
                }
288
286
 
289
287
                public bool IsSpecialName {
290
 
                        get { return (m_attributes & MethodAttributes.SpecialName) != 0; }
291
 
                        set {
292
 
                                if (value)
293
 
                                        m_attributes |= MethodAttributes.SpecialName;
294
 
                                else
295
 
                                        m_attributes &= ~MethodAttributes.SpecialName;
296
 
                        }
 
288
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.SpecialName); }
 
289
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.SpecialName, value); }
297
290
                }
298
291
 
299
292
                public bool IsPInvokeImpl {
300
 
                        get { return (m_attributes & MethodAttributes.PInvokeImpl) != 0; }
301
 
                        set {
302
 
                                if (value)
303
 
                                        m_attributes |= MethodAttributes.PInvokeImpl;
304
 
                                else
305
 
                                        m_attributes &= ~MethodAttributes.PInvokeImpl;
306
 
                        }
 
293
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.PInvokeImpl); }
 
294
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.PInvokeImpl, value); }
307
295
                }
308
296
 
309
297
                public bool IsUnmanagedExport {
310
 
                        get { return (m_attributes & MethodAttributes.UnmanagedExport) != 0; }
311
 
                        set {
312
 
                                if (value)
313
 
                                        m_attributes |= MethodAttributes.UnmanagedExport;
314
 
                                else
315
 
                                        m_attributes &= ~MethodAttributes.UnmanagedExport;
316
 
                        }
 
298
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.UnmanagedExport); }
 
299
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.UnmanagedExport, value); }
317
300
                }
318
301
 
319
302
                public bool IsRuntimeSpecialName {
320
 
                        get { return (m_attributes & MethodAttributes.RTSpecialName) != 0; }
321
 
                        set {
322
 
                                if (value)
323
 
                                        m_attributes |= MethodAttributes.RTSpecialName;
324
 
                                else
325
 
                                        m_attributes &= ~MethodAttributes.RTSpecialName;
326
 
                        }
 
303
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.RTSpecialName); }
 
304
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.RTSpecialName, value); }
327
305
                }
328
306
 
329
307
                public bool HasSecurity {
330
 
                        get { return (m_attributes & MethodAttributes.HasSecurity) != 0; }
331
 
                        set {
332
 
                                if (value)
333
 
                                        m_attributes |= MethodAttributes.HasSecurity;
334
 
                                else
335
 
                                        m_attributes &= ~MethodAttributes.HasSecurity;
336
 
                        }
 
308
                        get { return attributes.GetAttributes ((ushort) MethodAttributes.HasSecurity); }
 
309
                        set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.HasSecurity, value); }
337
310
                }
338
311
 
339
312
                #endregion
341
314
                #region MethodImplAttributes
342
315
 
343
316
                public bool IsIL {
344
 
                        get { return (m_implAttrs & MethodImplAttributes.CodeTypeMask) == MethodImplAttributes.IL; }
345
 
                        set {
346
 
                                if (value) {
347
 
                                        m_implAttrs &= ~MethodImplAttributes.CodeTypeMask;
348
 
                                        m_implAttrs |= MethodImplAttributes.IL;
349
 
                                } else
350
 
                                        m_implAttrs &= ~(MethodImplAttributes.CodeTypeMask & MethodImplAttributes.IL);
351
 
                        }
 
317
                        get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.IL); }
 
318
                        set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.IL, value); }
352
319
                }
353
320
 
354
321
                public bool IsNative {
355
 
                        get { return (m_implAttrs & MethodImplAttributes.CodeTypeMask) == MethodImplAttributes.Native; }
356
 
                        set {
357
 
                                if (value) {
358
 
                                        m_implAttrs &= ~MethodImplAttributes.CodeTypeMask;
359
 
                                        m_implAttrs |= MethodImplAttributes.Native;
360
 
                                } else
361
 
                                        m_implAttrs &= ~(MethodImplAttributes.CodeTypeMask & MethodImplAttributes.Native);
362
 
                        }
 
322
                        get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Native); }
 
323
                        set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Native, value); }
363
324
                }
364
325
 
365
326
                public bool IsRuntime {
366
 
                        get { return (m_implAttrs & MethodImplAttributes.CodeTypeMask) == MethodImplAttributes.Runtime; }
367
 
                        set {
368
 
                                if (value) {
369
 
                                        m_implAttrs &= ~MethodImplAttributes.CodeTypeMask;
370
 
                                        m_implAttrs |= MethodImplAttributes.Runtime;
371
 
                                } else
372
 
                                        m_implAttrs &= ~(MethodImplAttributes.CodeTypeMask & MethodImplAttributes.Runtime);
373
 
                        }
 
327
                        get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Runtime); }
 
328
                        set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Runtime, value); }
374
329
                }
375
330
 
376
331
                public bool IsUnmanaged {
377
 
                        get { return (m_implAttrs & MethodImplAttributes.ManagedMask) == MethodImplAttributes.Unmanaged; }
378
 
                        set {
379
 
                                if (value) {
380
 
                                        m_implAttrs &= ~MethodImplAttributes.ManagedMask;
381
 
                                        m_implAttrs |= MethodImplAttributes.Unmanaged;
382
 
                                } else
383
 
                                        m_implAttrs &= ~(MethodImplAttributes.ManagedMask & MethodImplAttributes.Unmanaged);
384
 
                        }
 
332
                        get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Unmanaged); }
 
333
                        set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Unmanaged, value); }
385
334
                }
386
335
 
387
336
                public bool IsManaged {
388
 
                        get { return (m_implAttrs & MethodImplAttributes.ManagedMask) == MethodImplAttributes.Managed; }
389
 
                        set {
390
 
                                if (value) {
391
 
                                        m_implAttrs &= ~MethodImplAttributes.ManagedMask;
392
 
                                        m_implAttrs |= MethodImplAttributes.Managed;
393
 
                                } else
394
 
                                        m_implAttrs &= ~(MethodImplAttributes.ManagedMask & MethodImplAttributes.Managed);
395
 
                        }
 
337
                        get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Managed); }
 
338
                        set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Managed, value); }
396
339
                }
397
340
 
398
341
                public bool IsForwardRef {
399
 
                        get { return (m_implAttrs & MethodImplAttributes.ForwardRef) != 0; }
400
 
                        set {
401
 
                                if (value)
402
 
                                        m_implAttrs |= MethodImplAttributes.ForwardRef;
403
 
                                else
404
 
                                        m_implAttrs &= ~MethodImplAttributes.ForwardRef;
405
 
                        }
 
342
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.ForwardRef); }
 
343
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.ForwardRef, value); }
406
344
                }
407
345
 
408
346
                public bool IsPreserveSig {
409
 
                        get { return (m_implAttrs & MethodImplAttributes.PreserveSig) != 0; }
410
 
                        set {
411
 
                                if (value)
412
 
                                        m_implAttrs |= MethodImplAttributes.PreserveSig;
413
 
                                else
414
 
                                        m_implAttrs &= ~MethodImplAttributes.PreserveSig;
415
 
                        }
 
347
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.PreserveSig); }
 
348
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.PreserveSig, value); }
416
349
                }
417
350
 
418
351
                public bool IsInternalCall {
419
 
                        get { return (m_implAttrs & MethodImplAttributes.InternalCall) != 0; }
420
 
                        set {
421
 
                                if (value)
422
 
                                        m_implAttrs |= MethodImplAttributes.InternalCall;
423
 
                                else
424
 
                                        m_implAttrs &= ~MethodImplAttributes.InternalCall;
425
 
                        }
 
352
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.InternalCall); }
 
353
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.InternalCall, value); }
426
354
                }
427
355
 
428
356
                public bool IsSynchronized {
429
 
                        get { return (m_implAttrs & MethodImplAttributes.Synchronized) != 0; }
430
 
                        set {
431
 
                                if (value)
432
 
                                        m_implAttrs |= MethodImplAttributes.Synchronized;
433
 
                                else
434
 
                                        m_implAttrs &= ~MethodImplAttributes.Synchronized;
435
 
                        }
 
357
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.Synchronized); }
 
358
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.Synchronized, value); }
436
359
                }
437
360
 
438
361
                public bool NoInlining {
439
 
                        get { return (m_implAttrs & MethodImplAttributes.NoInlining) != 0; }
440
 
                        set {
441
 
                                if (value)
442
 
                                        m_implAttrs |= MethodImplAttributes.NoInlining;
443
 
                                else
444
 
                                        m_implAttrs &= ~MethodImplAttributes.NoInlining;
445
 
                        }
 
362
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.NoInlining); }
 
363
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.NoInlining, value); }
 
364
                }
 
365
 
 
366
                public bool NoOptimization {
 
367
                        get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.NoOptimization); }
 
368
                        set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.NoOptimization, value); }
446
369
                }
447
370
 
448
371
                #endregion
449
372
 
450
373
                #region MethodSemanticsAttributes
 
374
 
451
375
                public bool IsSetter {
452
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.Setter) != 0; }
453
 
                        set {
454
 
                                if (value)
455
 
                                        m_semAttrs |= MethodSemanticsAttributes.Setter;
456
 
                                else
457
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.Setter;
458
 
                        }
 
376
                        get { return this.GetSemantics (MethodSemanticsAttributes.Setter); }
 
377
                        set { this.SetSemantics (MethodSemanticsAttributes.Setter, value); }
459
378
                }
460
379
 
461
380
                public bool IsGetter {
462
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.Getter) != 0; }
463
 
                        set {
464
 
                                if (value)
465
 
                                        m_semAttrs |= MethodSemanticsAttributes.Getter;
466
 
                                else
467
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.Getter;
468
 
                        }
 
381
                        get { return this.GetSemantics (MethodSemanticsAttributes.Getter); }
 
382
                        set { this.SetSemantics (MethodSemanticsAttributes.Getter, value); }
469
383
                }
470
384
 
471
385
                public bool IsOther {
472
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.Other) != 0; }
473
 
                        set {
474
 
                                if (value)
475
 
                                        m_semAttrs |= MethodSemanticsAttributes.Other;
476
 
                                else
477
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.Other;
478
 
                        }
 
386
                        get { return this.GetSemantics (MethodSemanticsAttributes.Other); }
 
387
                        set { this.SetSemantics (MethodSemanticsAttributes.Other, value); }
479
388
                }
480
389
 
481
390
                public bool IsAddOn {
482
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.AddOn) != 0; }
483
 
                        set {
484
 
                                if (value)
485
 
                                        m_semAttrs |= MethodSemanticsAttributes.AddOn;
486
 
                                else
487
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.AddOn;
488
 
                        }
 
391
                        get { return this.GetSemantics (MethodSemanticsAttributes.AddOn); }
 
392
                        set { this.SetSemantics (MethodSemanticsAttributes.AddOn, value); }
489
393
                }
490
394
 
491
395
                public bool IsRemoveOn {
492
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.RemoveOn) != 0; }
493
 
                        set {
494
 
                                if (value)
495
 
                                        m_semAttrs |= MethodSemanticsAttributes.RemoveOn;
496
 
                                else
497
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.RemoveOn;
498
 
                        }
 
396
                        get { return this.GetSemantics (MethodSemanticsAttributes.RemoveOn); }
 
397
                        set { this.SetSemantics (MethodSemanticsAttributes.RemoveOn, value); }
499
398
                }
500
399
 
501
400
                public bool IsFire {
502
 
                        get { return (m_semAttrs & MethodSemanticsAttributes.Fire) != 0; }
503
 
                        set {
504
 
                                if (value)
505
 
                                        m_semAttrs |= MethodSemanticsAttributes.Fire;
506
 
                                else
507
 
                                        m_semAttrs &= ~MethodSemanticsAttributes.Fire;
508
 
                        }
 
401
                        get { return this.GetSemantics (MethodSemanticsAttributes.Fire); }
 
402
                        set { this.SetSemantics (MethodSemanticsAttributes.Fire, value); }
509
403
                }
510
404
 
511
405
                #endregion
512
406
 
513
 
                public bool IsConstructor {
514
 
                        get {
515
 
                                return this.IsRuntimeSpecialName && this.IsSpecialName &&
516
 
                                        (this.Name == Cctor || this.Name == Ctor);
517
 
                        }
518
 
                }
519
 
 
520
 
                public bool HasBody {
521
 
                        get {
522
 
                                return (m_attributes & MethodAttributes.Abstract) == 0 &&
523
 
                                        (m_attributes & MethodAttributes.PInvokeImpl) == 0 &&
524
 
                                        (m_implAttrs & MethodImplAttributes.InternalCall) == 0 &&
525
 
                                        (m_implAttrs & MethodImplAttributes.Native) == 0 &&
526
 
                                        (m_implAttrs & MethodImplAttributes.Unmanaged) == 0 &&
527
 
                                        (m_implAttrs & MethodImplAttributes.Runtime) == 0;
528
 
                        }
529
 
                }
530
 
 
531
407
                public new TypeDefinition DeclaringType {
532
408
                        get { return (TypeDefinition) base.DeclaringType; }
533
409
                        set { base.DeclaringType = value; }
534
410
                }
535
411
 
536
 
                public MethodDefinition (string name, RVA rva,
537
 
                        MethodAttributes attrs, MethodImplAttributes implAttrs,
538
 
                        bool hasThis, bool explicitThis, MethodCallingConvention callConv) :
539
 
                        base (name, hasThis, explicitThis, callConv)
540
 
                {
541
 
                        m_rva = rva;
542
 
                        m_attributes = attrs;
543
 
                        m_implAttrs = implAttrs;
544
 
 
545
 
                        if (!IsStatic)
546
 
                                m_this = new ParameterDefinition ("this", 0, (ParameterAttributes) 0, null);
547
 
                }
548
 
 
549
 
                internal MethodDefinition (string name, MethodAttributes attrs) : base (name)
550
 
                {
551
 
                        m_attributes = attrs;
552
 
 
 
412
                public bool IsConstructor {
 
413
                        get {
 
414
                                return this.IsRuntimeSpecialName
 
415
                                        && this.IsSpecialName
 
416
                                        && (this.Name == ".cctor" || this.Name == ".ctor");
 
417
                        }
 
418
                }
 
419
 
 
420
                public override bool IsDefinition {
 
421
                        get { return true; }
 
422
                }
 
423
 
 
424
                internal MethodDefinition ()
 
425
                {
 
426
                        this.token = new MetadataToken (TokenType.Method);
 
427
                }
 
428
 
 
429
                public MethodDefinition (string name, MethodAttributes attributes, TypeReference returnType)
 
430
                        : base (name, returnType)
 
431
                {
 
432
                        this.attributes = (ushort) attributes;
553
433
                        this.HasThis = !this.IsStatic;
554
 
                        if (!IsStatic)
555
 
                                m_this = new ParameterDefinition ("this", 0, (ParameterAttributes) 0, null);
556
 
                }
557
 
 
558
 
                public MethodDefinition (string name, MethodAttributes attrs, TypeReference returnType) :
559
 
                        this (name, attrs)
560
 
                {
561
 
                        this.ReturnType.ReturnType = returnType;
562
 
                }
563
 
 
564
 
                internal void LoadBody ()
565
 
                {
566
 
                        if (m_body == null && this.HasBody) {
567
 
                                m_body = new MethodBody (this);
568
 
 
569
 
                                ModuleDefinition module = DeclaringType != null ? DeclaringType.Module : null;
570
 
 
571
 
                                if (module != null && m_rva != RVA.Zero)
572
 
                                        module.Controller.Reader.Code.VisitMethodBody (m_body);
573
 
                        }
 
434
                        this.token = new MetadataToken (TokenType.Method);
574
435
                }
575
436
 
576
437
                public override MethodDefinition Resolve ()
577
438
                {
578
439
                        return this;
579
440
                }
580
 
 
581
 
                public MethodDefinition Clone ()
582
 
                {
583
 
                        return Clone (this, new ImportContext (NullReferenceImporter.Instance, this));
584
 
                }
585
 
 
586
 
                internal static MethodDefinition Clone (MethodDefinition meth, ImportContext context)
587
 
                {
588
 
                        MethodDefinition nm = new MethodDefinition (
589
 
                                meth.Name,
590
 
                                RVA.Zero,
591
 
                                meth.Attributes,
592
 
                                meth.ImplAttributes,
593
 
                                meth.HasThis,
594
 
                                meth.ExplicitThis,
595
 
                                meth.CallingConvention);
596
 
 
597
 
                        MethodReference contextMethod = context.GenericContext.Method;
598
 
 
599
 
                        context.GenericContext.Method = nm;
600
 
 
601
 
                        GenericParameter.CloneInto (meth, nm, context);
602
 
 
603
 
                        nm.ReturnType.ReturnType = context.Import (meth.ReturnType.ReturnType);
604
 
 
605
 
                        if (meth.ReturnType.Parameter != null) {
606
 
                                nm.ReturnType.Parameter = ParameterDefinition.Clone (meth.ReturnType.Parameter, context);
607
 
                                nm.ReturnType.Parameter.Method = nm;
608
 
                        }
609
 
 
610
 
                        if (meth.PInvokeInfo != null)
611
 
                                nm.PInvokeInfo = meth.PInvokeInfo; // TODO: import module ?
612
 
 
613
 
                        if (meth.HasParameters) {
614
 
                                foreach (ParameterDefinition param in meth.Parameters)
615
 
                                        nm.Parameters.Add (ParameterDefinition.Clone (param, context));
616
 
                        }
617
 
                        if (meth.HasOverrides) {
618
 
                                foreach (MethodReference ov in meth.Overrides)
619
 
                                        nm.Overrides.Add (context.Import (ov));
620
 
                        }
621
 
                        if (meth.HasCustomAttributes) {
622
 
                                foreach (CustomAttribute ca in meth.CustomAttributes)
623
 
                                        nm.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
624
 
                        }
625
 
                        if (meth.HasSecurityDeclarations) {
626
 
                                foreach (SecurityDeclaration sec in meth.SecurityDeclarations)
627
 
                                        nm.SecurityDeclarations.Add (SecurityDeclaration.Clone (sec));
628
 
                        }
629
 
 
630
 
                        if (meth.Body != null)
631
 
                                nm.Body = MethodBody.Clone (meth.Body, nm, context);
632
 
 
633
 
                        context.GenericContext.Method = contextMethod;
634
 
 
635
 
                        return nm;
636
 
                }
637
 
 
638
 
                public override void Accept (IReflectionVisitor visitor)
639
 
                {
640
 
                        visitor.VisitMethodDefinition (this);
641
 
 
642
 
                        this.GenericParameters.Accept (visitor);
643
 
                        this.Parameters.Accept (visitor);
644
 
 
645
 
                        if (this.PInvokeInfo != null)
646
 
                                this.PInvokeInfo.Accept (visitor);
647
 
 
648
 
                        this.SecurityDeclarations.Accept (visitor);
649
 
                        this.Overrides.Accept (visitor);
650
 
                        this.CustomAttributes.Accept (visitor);
 
441
        }
 
442
 
 
443
        static partial class Mixin {
 
444
 
 
445
                public static ParameterDefinition GetParameter (this MethodBody self, int index)
 
446
                {
 
447
                        var method = self.method;
 
448
 
 
449
                        if (method.HasThis) {
 
450
                                if (index == 0)
 
451
                                        return self.ThisParameter;
 
452
 
 
453
                                index--;
 
454
                        }
 
455
 
 
456
                        var parameters = method.Parameters;
 
457
 
 
458
                        if (index < 0 || index >= parameters.size)
 
459
                                return null;
 
460
 
 
461
                        return parameters [index];
 
462
                }
 
463
 
 
464
                public static VariableDefinition GetVariable (this MethodBody self, int index)
 
465
                {
 
466
                        var variables = self.Variables;
 
467
 
 
468
                        if (index < 0 || index >= variables.size)
 
469
                                return null;
 
470
 
 
471
                        return variables [index];
 
472
                }
 
473
 
 
474
                public static bool GetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics)
 
475
                {
 
476
                        return (self.SemanticsAttributes & semantics) != 0;
 
477
                }
 
478
 
 
479
                public static void SetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics, bool value)
 
480
                {
 
481
                        if (value)
 
482
                                self.SemanticsAttributes |= semantics;
 
483
                        else
 
484
                                self.SemanticsAttributes &= ~semantics;
651
485
                }
652
486
        }
653
487
}