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

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/TypeDefinition.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
// TypeDefinition.cs
 
3
//
 
4
// Author:
 
5
//   Jb Evain (jbevain@gmail.com)
 
6
//
 
7
// Copyright (c) 2008 - 2010 Jb Evain
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
 
 
31
using Mono.Collections.Generic;
 
32
 
 
33
namespace Mono.Cecil {
 
34
 
 
35
        public sealed class TypeDefinition : TypeReference, IMemberDefinition, ISecurityDeclarationProvider {
 
36
 
 
37
                uint attributes;
 
38
                TypeReference base_type;
 
39
                internal Range fields_range;
 
40
                internal Range methods_range;
 
41
 
 
42
                short packing_size = Mixin.NotResolvedMarker;
 
43
                int class_size = Mixin.NotResolvedMarker;
 
44
 
 
45
                Collection<TypeReference> interfaces;
 
46
                Collection<TypeDefinition> nested_types;
 
47
                Collection<MethodDefinition> methods;
 
48
                Collection<FieldDefinition> fields;
 
49
                Collection<EventDefinition> events;
 
50
                Collection<PropertyDefinition> properties;
 
51
                Collection<CustomAttribute> custom_attributes;
 
52
                Collection<SecurityDeclaration> security_declarations;
 
53
 
 
54
                public TypeAttributes Attributes {
 
55
                        get { return (TypeAttributes) attributes; }
 
56
                        set { attributes = (uint) value; }
 
57
                }
 
58
 
 
59
                public TypeReference BaseType {
 
60
                        get { return base_type; }
 
61
                        set { base_type = value; }
 
62
                }
 
63
 
 
64
                void ResolveLayout ()
 
65
                {
 
66
                        if (packing_size != Mixin.NotResolvedMarker || class_size != Mixin.NotResolvedMarker)
 
67
                                return;
 
68
 
 
69
                        if (!HasImage) {
 
70
                                packing_size = Mixin.NoDataMarker;
 
71
                                class_size = Mixin.NoDataMarker;
 
72
                                return;
 
73
                        }
 
74
 
 
75
                        var row = Module.Read (this, (type, reader) => reader.ReadTypeLayout (type));
 
76
 
 
77
                        packing_size = row.Col1;
 
78
                        class_size = row.Col2;
 
79
                }
 
80
 
 
81
                public bool HasLayoutInfo {
 
82
                        get {
 
83
                                if (packing_size >= 0 || class_size >= 0)
 
84
                                        return true;
 
85
 
 
86
                                ResolveLayout ();
 
87
 
 
88
                                return packing_size >= 0 || class_size >= 0;
 
89
                        }
 
90
                }
 
91
 
 
92
                public short PackingSize {
 
93
                        get {
 
94
                                if (packing_size >= 0)
 
95
                                        return packing_size;
 
96
 
 
97
                                ResolveLayout ();
 
98
 
 
99
                                return packing_size >= 0 ? packing_size : (short) -1;
 
100
                        }
 
101
                        set { packing_size = value; }
 
102
                }
 
103
 
 
104
                public int ClassSize {
 
105
                        get {
 
106
                                if (class_size >= 0)
 
107
                                        return class_size;
 
108
 
 
109
                                ResolveLayout ();
 
110
 
 
111
                                return class_size >= 0 ? class_size : -1;
 
112
                        }
 
113
                        set { class_size = value; }
 
114
                }
 
115
 
 
116
                public bool HasInterfaces {
 
117
                        get {
 
118
                                if (interfaces != null)
 
119
                                        return interfaces.Count > 0;
 
120
 
 
121
                                if (HasImage)
 
122
                                        return Module.Read (this, (type, reader) => reader.HasInterfaces (type));
 
123
 
 
124
                                return false;
 
125
                        }
 
126
                }
 
127
 
 
128
                public Collection<TypeReference> Interfaces {
 
129
                        get {
 
130
                                if (interfaces != null)
 
131
                                        return interfaces;
 
132
 
 
133
                                if (HasImage)
 
134
                                        return interfaces = Module.Read (this, (type, reader) => reader.ReadInterfaces (type));
 
135
 
 
136
                                return interfaces = new Collection<TypeReference> ();
 
137
                        }
 
138
                }
 
139
 
 
140
                public bool HasNestedTypes {
 
141
                        get {
 
142
                                if (nested_types != null)
 
143
                                        return nested_types.Count > 0;
 
144
 
 
145
                                if (HasImage)
 
146
                                        return Module.Read (this, (type, reader) => reader.HasNestedTypes (type));
 
147
 
 
148
                                return false;
 
149
                        }
 
150
                }
 
151
 
 
152
                public Collection<TypeDefinition> NestedTypes {
 
153
                        get {
 
154
                                if (nested_types != null)
 
155
                                        return nested_types;
 
156
 
 
157
                                if (HasImage)
 
158
                                        return nested_types = Module.Read (this, (type, reader) => reader.ReadNestedTypes (type));
 
159
 
 
160
                                return nested_types = new MemberDefinitionCollection<TypeDefinition> (this);
 
161
                        }
 
162
                }
 
163
 
 
164
                internal new bool HasImage {
 
165
                        get { return Module != null && Module.HasImage; }
 
166
                }
 
167
 
 
168
                public bool HasMethods {
 
169
                        get {
 
170
                                if (methods != null)
 
171
                                        return methods.Count > 0;
 
172
 
 
173
                                if (HasImage)
 
174
                                        return methods_range.Length > 0;
 
175
 
 
176
                                return false;
 
177
                        }
 
178
                }
 
179
 
 
180
                public Collection<MethodDefinition> Methods {
 
181
                        get {
 
182
                                if (methods != null)
 
183
                                        return methods;
 
184
 
 
185
                                if (HasImage)
 
186
                                        return methods = Module.Read (this, (type, reader) => reader.ReadMethods (type));
 
187
 
 
188
                                return methods = new MemberDefinitionCollection<MethodDefinition> (this);
 
189
                        }
 
190
                }
 
191
 
 
192
                public bool HasFields {
 
193
                        get {
 
194
                                if (fields != null)
 
195
                                        return fields.Count > 0;
 
196
 
 
197
                                if (HasImage)
 
198
                                        return fields_range.Length > 0;
 
199
 
 
200
                                return false;
 
201
                        }
 
202
                }
 
203
 
 
204
                public Collection<FieldDefinition> Fields {
 
205
                        get {
 
206
                                if (fields != null)
 
207
                                        return fields;
 
208
 
 
209
                                if (HasImage)
 
210
                                        return fields = Module.Read (this, (type, reader) => reader.ReadFields (type));
 
211
 
 
212
                                return fields = new MemberDefinitionCollection<FieldDefinition> (this);
 
213
                        }
 
214
                }
 
215
 
 
216
                public bool HasEvents {
 
217
                        get {
 
218
                                if (events != null)
 
219
                                        return events.Count > 0;
 
220
 
 
221
                                if (HasImage)
 
222
                                        return Module.Read (this, (type, reader) => reader.HasEvents (type));
 
223
 
 
224
                                return false;
 
225
                        }
 
226
                }
 
227
 
 
228
                public Collection<EventDefinition> Events {
 
229
                        get {
 
230
                                if (events != null)
 
231
                                        return events;
 
232
 
 
233
                                if (HasImage)
 
234
                                        return events = Module.Read (this, (type, reader) => reader.ReadEvents (type));
 
235
 
 
236
                                return events = new MemberDefinitionCollection<EventDefinition> (this);
 
237
                        }
 
238
                }
 
239
 
 
240
                public bool HasProperties {
 
241
                        get {
 
242
                                if (properties != null)
 
243
                                        return properties.Count > 0;
 
244
 
 
245
                                if (HasImage)
 
246
                                        return Module.Read (this, (type, reader) => reader.HasProperties (type));
 
247
 
 
248
                                return false;
 
249
                        }
 
250
                }
 
251
 
 
252
                public Collection<PropertyDefinition> Properties {
 
253
                        get {
 
254
                                if (properties != null)
 
255
                                        return properties;
 
256
 
 
257
                                if (HasImage)
 
258
                                        return properties = Module.Read (this, (type, reader) => reader.ReadProperties (type));
 
259
 
 
260
                                return properties = new MemberDefinitionCollection<PropertyDefinition> (this);
 
261
                        }
 
262
                }
 
263
 
 
264
                public bool HasSecurityDeclarations {
 
265
                        get {
 
266
                                if (security_declarations != null)
 
267
                                        return security_declarations.Count > 0;
 
268
 
 
269
                                return this.GetHasSecurityDeclarations (Module);
 
270
                        }
 
271
                }
 
272
 
 
273
                public Collection<SecurityDeclaration> SecurityDeclarations {
 
274
                        get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (Module)); }
 
275
                }
 
276
 
 
277
                public bool HasCustomAttributes {
 
278
                        get {
 
279
                                if (custom_attributes != null)
 
280
                                        return custom_attributes.Count > 0;
 
281
 
 
282
                                return this.GetHasCustomAttributes (Module);
 
283
                        }
 
284
                }
 
285
 
 
286
                public Collection<CustomAttribute> CustomAttributes {
 
287
                        get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
 
288
                }
 
289
 
 
290
                public override bool HasGenericParameters {
 
291
                        get {
 
292
                                if (generic_parameters != null)
 
293
                                        return generic_parameters.Count > 0;
 
294
 
 
295
                                return this.GetHasGenericParameters (Module);
 
296
                        }
 
297
                }
 
298
 
 
299
                public override Collection<GenericParameter> GenericParameters {
 
300
                        get { return generic_parameters ?? (generic_parameters = this.GetGenericParameters (Module)); }
 
301
                }
 
302
 
 
303
                #region TypeAttributes
 
304
 
 
305
                public bool IsNotPublic {
 
306
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic); }
 
307
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic, value); }
 
308
                }
 
309
 
 
310
                public bool IsPublic {
 
311
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public); }
 
312
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public, value); }
 
313
                }
 
314
 
 
315
                public bool IsNestedPublic {
 
316
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic); }
 
317
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic, value); }
 
318
                }
 
319
 
 
320
                public bool IsNestedPrivate {
 
321
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate); }
 
322
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate, value); }
 
323
                }
 
324
 
 
325
                public bool IsNestedFamily {
 
326
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily); }
 
327
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily, value); }
 
328
                }
 
329
 
 
330
                public bool IsNestedAssembly {
 
331
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly); }
 
332
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly, value); }
 
333
                }
 
334
 
 
335
                public bool IsNestedFamilyAndAssembly {
 
336
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem); }
 
337
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem, value); }
 
338
                }
 
339
 
 
340
                public bool IsNestedFamilyOrAssembly {
 
341
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem); }
 
342
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem, value); }
 
343
                }
 
344
 
 
345
                public bool IsAutoLayout {
 
346
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout); }
 
347
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout, value); }
 
348
                }
 
349
 
 
350
                public bool IsSequentialLayout {
 
351
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout); }
 
352
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout, value); }
 
353
                }
 
354
 
 
355
                public bool IsExplicitLayout {
 
356
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout); }
 
357
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout, value); }
 
358
                }
 
359
 
 
360
                public bool IsClass {
 
361
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class); }
 
362
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class, value); }
 
363
                }
 
364
 
 
365
                public bool IsInterface {
 
366
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface); }
 
367
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface, value); }
 
368
                }
 
369
 
 
370
                public bool IsAbstract {
 
371
                        get { return attributes.GetAttributes ((uint) TypeAttributes.Abstract); }
 
372
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Abstract, value); }
 
373
                }
 
374
 
 
375
                public bool IsSealed {
 
376
                        get { return attributes.GetAttributes ((uint) TypeAttributes.Sealed); }
 
377
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Sealed, value); }
 
378
                }
 
379
 
 
380
                public bool IsSpecialName {
 
381
                        get { return attributes.GetAttributes ((uint) TypeAttributes.SpecialName); }
 
382
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.SpecialName, value); }
 
383
                }
 
384
 
 
385
                public bool IsImport {
 
386
                        get { return attributes.GetAttributes ((uint) TypeAttributes.Import); }
 
387
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Import, value); }
 
388
                }
 
389
 
 
390
                public bool IsSerializable {
 
391
                        get { return attributes.GetAttributes ((uint) TypeAttributes.Serializable); }
 
392
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Serializable, value); }
 
393
                }
 
394
 
 
395
                public bool IsAnsiClass {
 
396
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass); }
 
397
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass, value); }
 
398
                }
 
399
 
 
400
                public bool IsUnicodeClass {
 
401
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass); }
 
402
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass, value); }
 
403
                }
 
404
 
 
405
                public bool IsAutoClass {
 
406
                        get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass); }
 
407
                        set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass, value); }
 
408
                }
 
409
 
 
410
                public bool IsBeforeFieldInit {
 
411
                        get { return attributes.GetAttributes ((uint) TypeAttributes.BeforeFieldInit); }
 
412
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.BeforeFieldInit, value); }
 
413
                }
 
414
 
 
415
                public bool IsRuntimeSpecialName {
 
416
                        get { return attributes.GetAttributes ((uint) TypeAttributes.RTSpecialName); }
 
417
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.RTSpecialName, value); }
 
418
                }
 
419
 
 
420
                public bool HasSecurity {
 
421
                        get { return attributes.GetAttributes ((uint) TypeAttributes.HasSecurity); }
 
422
                        set { attributes = attributes.SetAttributes ((uint) TypeAttributes.HasSecurity, value); }
 
423
                }
 
424
 
 
425
                #endregion
 
426
 
 
427
                public bool IsEnum {
 
428
                        get { return base_type != null && base_type.IsTypeOf ("System", "Enum"); }
 
429
                }
 
430
 
 
431
                public override bool IsValueType {
 
432
                        get {
 
433
                                if (base_type == null)
 
434
                                        return false;
 
435
 
 
436
                                return base_type.IsTypeOf ("System", "Enum") || (base_type.IsTypeOf ("System", "ValueType") && !this.IsTypeOf ("System", "Enum"));
 
437
                        }
 
438
                }
 
439
 
 
440
                public override bool IsDefinition {
 
441
                        get { return true; }
 
442
                }
 
443
 
 
444
                public new TypeDefinition DeclaringType {
 
445
                        get { return (TypeDefinition) base.DeclaringType; }
 
446
                        set { base.DeclaringType = value; }
 
447
                }
 
448
 
 
449
                public TypeDefinition (string @namespace, string name, TypeAttributes attributes)
 
450
                        : base (@namespace, name)
 
451
                {
 
452
                        this.attributes = (uint) attributes;
 
453
                        this.token = new MetadataToken (TokenType.TypeDef);
 
454
                }
 
455
 
 
456
                public TypeDefinition (string @namespace, string name, TypeAttributes attributes, TypeReference baseType) :
 
457
                        this (@namespace, name, attributes)
 
458
                {
 
459
                        this.BaseType = baseType;
 
460
                }
 
461
 
 
462
                public override TypeDefinition Resolve ()
 
463
                {
 
464
                        return this;
 
465
                }
 
466
        }
 
467
 
 
468
        static partial class Mixin {
 
469
 
 
470
                public static TypeReference GetEnumUnderlyingType (this TypeDefinition self)
 
471
                {
 
472
                        var fields = self.Fields;
 
473
 
 
474
                        for (int i = 0; i < fields.Count; i++) {
 
475
                                var field = fields [i];
 
476
                                if (!field.IsStatic)
 
477
                                        return field.FieldType;
 
478
                        }
 
479
 
 
480
                        throw new ArgumentException ();
 
481
                }
 
482
 
 
483
                public static TypeDefinition GetNestedType (this TypeDefinition self, string name)
 
484
                {
 
485
                        if (!self.HasNestedTypes)
 
486
                                return null;
 
487
 
 
488
                        var nested_types = self.NestedTypes;
 
489
 
 
490
                        for (int i = 0; i < nested_types.Count; i++) {
 
491
                                var nested_type = nested_types [i];
 
492
                                if (nested_type.Name == name)
 
493
                                        return nested_type;
 
494
                        }
 
495
 
 
496
                        return null;
 
497
                }
 
498
        }
 
499
}