Remove IVT from System.ServiceModel on MonoDroid, MonoTouch profiles.
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / TypeDefinition.cs
1 //
2 // TypeDefinition.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 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 namespace Mono.Cecil {
30
31         public sealed class TypeDefinition : TypeReference, IMemberDefinition, IHasSecurity {
32
33                 TypeAttributes m_attributes;
34                 TypeReference m_baseType;
35
36                 bool m_hasInfo;
37                 ushort m_packingSize;
38                 uint m_classSize;
39
40                 InterfaceCollection m_interfaces;
41                 NestedTypeCollection m_nestedTypes;
42                 MethodDefinitionCollection m_methods;
43                 ConstructorCollection m_ctors;
44                 FieldDefinitionCollection m_fields;
45                 EventDefinitionCollection m_events;
46                 PropertyDefinitionCollection m_properties;
47                 SecurityDeclarationCollection m_secDecls;
48
49                 public TypeAttributes Attributes {
50                         get { return m_attributes; }
51                         set { m_attributes = value; }
52                 }
53
54                 public TypeReference BaseType {
55                         get { return m_baseType; }
56                         set { m_baseType = value; }
57                 }
58
59                 public bool HasLayoutInfo {
60                         get { return m_hasInfo; }
61                 }
62
63                 public ushort PackingSize {
64                         get { return m_packingSize; }
65                         set {
66                                 m_hasInfo = true;
67                                 m_packingSize = value;
68                         }
69                 }
70
71                 public uint ClassSize {
72                         get { return m_classSize; }
73                         set {
74                                 m_hasInfo = true;
75                                 m_classSize = value;
76                         }
77                 }
78
79                 public bool HasInterfaces {
80                         get { return (m_interfaces == null) ? false : (m_interfaces.Count > 0); }
81                 }
82
83                 public InterfaceCollection Interfaces {
84                         get {
85                                 if (m_interfaces == null)
86                                         m_interfaces = new InterfaceCollection (this);
87
88                                 return m_interfaces;
89                         }
90                 }
91
92                 public bool HasNestedTypes {
93                         get { return (m_nestedTypes == null) ? false : (m_nestedTypes.Count > 0); }
94                 }
95
96                 public NestedTypeCollection NestedTypes {
97                         get {
98                                 if (m_nestedTypes == null)
99                                         m_nestedTypes = new NestedTypeCollection (this);
100
101                                 return m_nestedTypes;
102                         }
103                 }
104
105                 public bool HasMethods {
106                         get { return (m_methods == null) ? false : (m_methods.Count > 0); }
107                 }
108
109                 public MethodDefinitionCollection Methods {
110                         get {
111                                 if (m_methods == null)
112                                         m_methods = new MethodDefinitionCollection (this);
113
114                                 return m_methods;
115                         }
116                 }
117
118                 public bool HasConstructors {
119                         get { return (m_ctors == null) ? false : (m_ctors.Count > 0); }
120                 }
121
122                 public ConstructorCollection Constructors {
123                         get {
124                                 if (m_ctors == null)
125                                         m_ctors = new ConstructorCollection (this);
126
127                                 return m_ctors;
128                         }
129                 }
130
131                 public bool HasFields {
132                         get { return (m_fields == null) ? false : (m_fields.Count > 0); }
133                 }
134
135                 public FieldDefinitionCollection Fields {
136                         get {
137                                 if (m_fields == null)
138                                         m_fields = new FieldDefinitionCollection (this);
139
140                                 return m_fields;
141                         }
142                 }
143
144                 public bool HasEvents {
145                         get { return (m_events == null) ? false : (m_events.Count > 0); }
146                 }
147
148                 public EventDefinitionCollection Events {
149                         get {
150                                 if (m_events == null)
151                                         m_events = new EventDefinitionCollection (this);
152
153                                 return m_events;
154                         }
155                 }
156
157                 public bool HasProperties {
158                         get { return (m_properties == null) ? false : (m_properties.Count > 0); }
159                 }
160
161                 public PropertyDefinitionCollection Properties {
162                         get {
163                                 if (m_properties == null)
164                                         m_properties = new PropertyDefinitionCollection (this);
165
166                                 return m_properties;
167                         }
168                 }
169
170                 public bool HasSecurityDeclarations {
171                         get { return (m_secDecls == null) ? false : (m_secDecls.Count > 0); }
172                 }
173
174                 public SecurityDeclarationCollection SecurityDeclarations {
175                         get {
176                                 if (m_secDecls == null)
177                                         m_secDecls = new SecurityDeclarationCollection (this);
178
179                                 return m_secDecls;
180                         }
181                 }
182
183                 #region TypeAttributes
184
185                 public bool IsNotPublic {
186                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic; }
187                         set {
188                                 if (value) {
189                                         m_attributes &= ~TypeAttributes.VisibilityMask;
190                                         m_attributes |= TypeAttributes.NotPublic;
191                                 } else
192                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NotPublic);
193                         }
194                 }
195
196                 public bool IsPublic {
197                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public; }
198                         set {
199                                 if (value) {
200                                         m_attributes &= ~TypeAttributes.VisibilityMask;
201                                         m_attributes |= TypeAttributes.Public;
202                                 } else
203                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.Public);
204                         }
205                 }
206
207                 public bool IsNestedPublic {
208                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic; }
209                         set {
210                                 if (value) {
211                                         m_attributes &= ~TypeAttributes.VisibilityMask;
212                                         m_attributes |= TypeAttributes.NestedPublic;
213                                 } else
214                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPublic);
215                         }
216                 }
217
218                 public bool IsNestedPrivate {
219                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate; }
220                         set {
221                                 if (value) {
222                                         m_attributes &= ~TypeAttributes.VisibilityMask;
223                                         m_attributes |= TypeAttributes.NestedPrivate;
224                                 } else
225                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPrivate);
226                         }
227                 }
228
229                 public bool IsNestedFamily {
230                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily; }
231                         set {
232                                 if (value) {
233                                         m_attributes &= ~TypeAttributes.VisibilityMask;
234                                         m_attributes |= TypeAttributes.NestedFamily;
235                                 } else
236                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamily);
237                         }
238                 }
239
240                 public bool IsNestedAssembly {
241                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly; }
242                         set {
243                                 if (value) {
244                                         m_attributes &= ~TypeAttributes.VisibilityMask;
245                                         m_attributes |= TypeAttributes.NestedAssembly;
246                                 } else
247                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedAssembly);
248                         }
249                 }
250
251                 public bool IsNestedFamilyAndAssembly {
252                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem; }
253                         set {
254                                 if (value) {
255                                         m_attributes &= ~TypeAttributes.VisibilityMask;
256                                         m_attributes |= TypeAttributes.NestedFamANDAssem;
257                                 } else
258                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamANDAssem);
259                         }
260                 }
261
262                 public bool IsNestedFamilyOrAssembly {
263                         get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem; }
264                         set {
265                                 if (value) {
266                                         m_attributes &= ~TypeAttributes.VisibilityMask;
267                                         m_attributes |= TypeAttributes.NestedFamORAssem;
268                                 } else
269                                         m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamORAssem);
270                         }
271                 }
272
273                 public bool IsAutoLayout {
274                         get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
275                         set {
276                                 if (value) {
277                                         m_attributes &= ~TypeAttributes.LayoutMask;
278                                         m_attributes |= TypeAttributes.AutoLayout;
279                                 } else
280                                         m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.AutoLayout);
281                         }
282                 }
283
284                 public bool IsSequentialLayout {
285                         get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
286                         set {
287                                 if (value) {
288                                         m_attributes &= ~TypeAttributes.LayoutMask;
289                                         m_attributes |= TypeAttributes.SequentialLayout;
290                                 } else
291                                         m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.SequentialLayout);
292                         }
293                 }
294
295                 public bool IsExplicitLayout {
296                         get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
297                         set {
298                                 if (value) {
299                                         m_attributes &= ~TypeAttributes.LayoutMask;
300                                         m_attributes |= TypeAttributes.ExplicitLayout;
301                                 } else
302                                         m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.ExplicitLayout);
303                         }
304                 }
305
306                 public bool IsClass {
307                         get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Class; }
308                         set {
309                                 if (value) {
310                                         m_attributes &= ~TypeAttributes.ClassSemanticMask;
311                                         m_attributes |= TypeAttributes.Class;
312                                 } else
313                                         m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Class);
314                         }
315                 }
316
317                 public bool IsInterface {
318                         get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Interface; }
319                         set {
320                                 if (value) {
321                                         m_attributes &= ~TypeAttributes.ClassSemanticMask;
322                                         m_attributes |= TypeAttributes.Interface;
323                                 } else
324                                         m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Interface);
325                         }
326                 }
327
328                 public bool IsAbstract {
329                         get { return (m_attributes & TypeAttributes.Abstract) != 0; }
330                         set {
331                                 if (value)
332                                         m_attributes |= TypeAttributes.Abstract;
333                                 else
334                                         m_attributes &= ~TypeAttributes.Abstract;
335                         }
336                 }
337
338                 public bool IsSealed {
339                         get { return (m_attributes & TypeAttributes.Sealed) != 0; }
340                         set {
341                                 if (value)
342                                         m_attributes |= TypeAttributes.Sealed;
343                                 else
344                                         m_attributes &= ~TypeAttributes.Sealed;
345                         }
346                 }
347
348                 public bool IsSpecialName {
349                         get { return (m_attributes & TypeAttributes.SpecialName) != 0; }
350                         set {
351                                 if (value)
352                                         m_attributes |= TypeAttributes.SpecialName;
353                                 else
354                                         m_attributes &= ~TypeAttributes.SpecialName;
355                         }
356                 }
357
358                 public bool IsImport {
359                         get { return (m_attributes & TypeAttributes.Import) != 0; }
360                         set {
361                                 if (value)
362                                         m_attributes |= TypeAttributes.Import;
363                                 else
364                                         m_attributes &= ~TypeAttributes.Import;
365                         }
366                 }
367
368                 public bool IsSerializable {
369                         get { return (m_attributes & TypeAttributes.Serializable) != 0; }
370                         set {
371                                 if (value)
372                                         m_attributes |= TypeAttributes.Serializable;
373                                 else
374                                         m_attributes &= ~TypeAttributes.Serializable;
375                         }
376                 }
377
378                 public bool IsAnsiClass {
379                         get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
380                         set {
381                                 if (value) {
382                                         m_attributes &= ~TypeAttributes.StringFormatMask;
383                                         m_attributes |= TypeAttributes.AnsiClass;
384                                 } else
385                                         m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AnsiClass);
386                         }
387                 }
388
389                 public bool IsUnicodeClass {
390                         get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
391                         set {
392                                 if (value) {
393                                         m_attributes &= ~TypeAttributes.StringFormatMask;
394                                         m_attributes |= TypeAttributes.UnicodeClass;
395                                 } else
396                                         m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.UnicodeClass);
397                         }
398                 }
399
400                 public bool IsAutoClass {
401                         get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
402                         set {
403                                 if (value) {
404                                         m_attributes &= ~TypeAttributes.StringFormatMask;
405                                         m_attributes |= TypeAttributes.AutoClass;
406                                 } else
407                                         m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AutoClass);
408                         }
409                 }
410
411                 public bool IsBeforeFieldInit {
412                         get { return (m_attributes & TypeAttributes.BeforeFieldInit) != 0; }
413                         set {
414                                 if (value)
415                                         m_attributes |= TypeAttributes.BeforeFieldInit;
416                                 else
417                                         m_attributes &= ~TypeAttributes.BeforeFieldInit;
418                         }
419                 }
420
421                 public bool IsRuntimeSpecialName {
422                         get { return (m_attributes & TypeAttributes.RTSpecialName) != 0; }
423                         set {
424                                 if (value)
425                                         m_attributes |= TypeAttributes.RTSpecialName;
426                                 else
427                                         m_attributes &= ~TypeAttributes.RTSpecialName;
428                         }
429                 }
430
431                 public bool HasSecurity {
432                         get { return (m_attributes & TypeAttributes.HasSecurity) != 0; }
433                         set {
434                                 if (value)
435                                         m_attributes |= TypeAttributes.HasSecurity;
436                                 else
437                                         m_attributes &= ~TypeAttributes.HasSecurity;
438                         }
439                 }
440
441                 #endregion
442
443                 public bool IsEnum {
444                         get { return m_baseType != null && m_baseType.FullName == Constants.Enum; }
445                 }
446
447                 public override bool IsValueType {
448                         get {
449                                 return m_baseType != null && ((m_baseType.FullName == Constants.Enum) ||
450                                         (m_baseType.FullName == Constants.ValueType && this.FullName != Constants.Enum));
451                         }
452                 }
453
454                 public new TypeDefinition DeclaringType {
455                         get { return (TypeDefinition) base.DeclaringType; }
456                         set { base.DeclaringType = value; }
457                 }
458
459                 internal TypeDefinition (string name, string ns, TypeAttributes attrs) :
460                         base (name, ns)
461                 {
462                         m_hasInfo = false;
463                         m_attributes = attrs;
464                 }
465
466                 public TypeDefinition (string name, string ns,
467                         TypeAttributes attributes, TypeReference baseType) :
468                         this (name, ns, attributes)
469                 {
470                         this.BaseType = baseType;
471                 }
472
473                 public override TypeDefinition Resolve ()
474                 {
475                         return this;
476                 }
477
478                 public TypeDefinition Clone ()
479                 {
480                         return Clone (this, new ImportContext (NullReferenceImporter.Instance, this));
481                 }
482
483                 internal static TypeDefinition Clone (TypeDefinition type, ImportContext context)
484                 {
485                         TypeDefinition nt = new TypeDefinition (
486                                 type.Name,
487                                 type.Namespace,
488                                 type.Attributes);
489
490                         TypeReference contextType = context.GenericContext.Type;
491
492                         context.GenericContext.Type = nt;
493
494                         GenericParameter.CloneInto (type, nt, context);
495
496                         if (type.BaseType != null)
497                                 nt.BaseType = context.Import (type.BaseType);
498
499                         if (type.HasLayoutInfo) {
500                                 nt.ClassSize = type.ClassSize;
501                                 nt.PackingSize = type.PackingSize;
502                         }
503
504                         if (type.HasFields) {
505                                 foreach (FieldDefinition field in type.Fields)
506                                         nt.Fields.Add (FieldDefinition.Clone (field, context));
507                         }
508                         if (type.HasConstructors) {
509                                 foreach (MethodDefinition ctor in type.Constructors)
510                                         nt.Constructors.Add (MethodDefinition.Clone (ctor, context));
511                         }
512                         if (type.HasMethods) {
513                                 foreach (MethodDefinition meth in type.Methods)
514                                         nt.Methods.Add (MethodDefinition.Clone (meth, context));
515                         }
516                         if (type.HasEvents) {
517                                 foreach (EventDefinition evt in type.Events)
518                                         nt.Events.Add (EventDefinition.Clone (evt, context));
519                         }
520                         if (type.HasProperties) {
521                                 foreach (PropertyDefinition prop in type.Properties)
522                                         nt.Properties.Add (PropertyDefinition.Clone (prop, context));
523                         }
524                         if (type.HasInterfaces) {
525                                 foreach (TypeReference intf in type.Interfaces)
526                                         nt.Interfaces.Add (context.Import (intf));
527                         }
528                         if (type.HasNestedTypes) {
529                                 foreach (TypeDefinition nested in type.NestedTypes)
530                                         nt.NestedTypes.Add (Clone (nested, context));
531                         }
532                         if (type.HasCustomAttributes) {
533                                 foreach (CustomAttribute ca in type.CustomAttributes)
534                                         nt.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
535                         }
536                         if (type.HasSecurityDeclarations) {
537                                 foreach (SecurityDeclaration dec in type.SecurityDeclarations)
538                                         nt.SecurityDeclarations.Add (SecurityDeclaration.Clone (dec));
539                         }
540
541                         context.GenericContext.Type = contextType;
542
543                         return nt;
544                 }
545
546                 public override void Accept (IReflectionVisitor visitor)
547                 {
548                         visitor.VisitTypeDefinition (this);
549
550                         this.GenericParameters.Accept (visitor);
551                         this.Interfaces.Accept (visitor);
552                         this.Constructors.Accept (visitor);
553                         this.Methods.Accept (visitor);
554                         this.Fields.Accept (visitor);
555                         this.Properties.Accept (visitor);
556                         this.Events.Accept (visitor);
557                         this.NestedTypes.Accept (visitor);
558                         this.CustomAttributes.Accept (visitor);
559                         this.SecurityDeclarations.Accept (visitor);
560                 }
561         }
562 }