* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / EnumBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/EnumBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39
40 namespace System.Reflection.Emit {
41         public sealed class EnumBuilder : Type {
42                 private CustomAttributeBuilder[] cattrs;
43                 private TypeBuilder _tb;
44                 private FieldBuilder _underlyingField;
45                 private Type _underlyingType;
46
47                 internal EnumBuilder (ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType)
48                 {
49                         _tb = new TypeBuilder (mb, name, (visibility | TypeAttributes.Sealed), 
50                                 typeof(Enum), null, PackingSize.Unspecified, 0, null);
51                         _underlyingType = underlyingType;
52                         _underlyingField = _tb.DefineField ("value__", underlyingType,
53                                 (FieldAttributes.SpecialName | FieldAttributes.Private));
54                         setup_enum_type (_tb);
55                 }
56
57                 public override Assembly Assembly {
58                         get {
59                                 return _tb.Assembly;
60                         }
61                 }
62
63                 public override string AssemblyQualifiedName {
64                         get {
65                                 return _tb.AssemblyQualifiedName;
66                         }
67                 }
68
69                 public override Type BaseType {
70                         get {
71                                 return _tb.BaseType;
72                         }
73                 }
74
75                 public override Type DeclaringType {
76                         get {
77                                 return _tb.DeclaringType;
78                         }
79                 }
80
81                 public override string FullName {
82                         get {
83                                 return _tb.FullName;
84                         }
85                 }
86
87                 public override Guid GUID {
88                         get {
89                                 return _tb.GUID;
90                         }
91                 }
92
93                 public override Module Module {
94                         get {
95                                 return _tb.Module;
96                         }
97                 }
98
99                 public override string Name {
100                         get {
101                                 return _tb.Name;
102                         }
103                 }
104
105                 public override string Namespace {
106                         get { 
107                                 return _tb.Namespace;
108                         }
109                 }
110
111                 public override Type ReflectedType {
112                         get {
113                                 return _tb.ReflectedType;
114                         }
115                 }
116
117                 public override RuntimeTypeHandle TypeHandle {
118                         get {
119                                 return _tb.TypeHandle;
120                         }
121                 }
122
123                 public TypeToken TypeToken {
124                         get {
125                                 return _tb.TypeToken;
126                         }
127                 }
128
129                 public FieldBuilder UnderlyingField {
130                         get {
131                                 return _underlyingField;
132                         }
133                 }
134
135                 public override Type UnderlyingSystemType {
136                         get {
137                                 return _underlyingType;
138                         }
139                 }
140
141                 public Type CreateType ()
142                 {
143                         Type res = _tb.CreateType ();
144                         return res;
145                 }
146
147                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
148                 private extern void setup_enum_type (Type t);
149
150                 public FieldBuilder DefineLiteral (string literalName, object literalValue)
151                 {
152                         FieldBuilder fieldBuilder = _tb.DefineField (literalName, 
153                                 _underlyingType, (FieldAttributes.Literal | 
154                                 (FieldAttributes.Static | FieldAttributes.Public)));
155                         fieldBuilder.SetConstant (literalValue);
156                         return fieldBuilder;
157                 }
158
159                 protected override TypeAttributes GetAttributeFlagsImpl ()
160                 {
161                         return _tb.attrs;
162                 }
163
164                 protected override ConstructorInfo GetConstructorImpl (
165                         BindingFlags bindingAttr, Binder binder, CallingConventions cc,
166                         Type[] types, ParameterModifier[] modifiers)
167                 {
168                         return _tb.GetConstructor (bindingAttr, binder, cc, types, 
169                                 modifiers);
170                 }
171
172                 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
173                 {
174                         return _tb.GetConstructors (bindingAttr);
175                 }
176
177                 public override object[] GetCustomAttributes(bool inherit)
178                 {
179                         return _tb.GetCustomAttributes (inherit);
180                 }
181
182                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
183                 {
184                         return _tb.GetCustomAttributes (attributeType, inherit);
185                 }
186
187                 public override Type GetElementType()
188                 {
189                         return _tb.GetElementType ();
190                 }
191
192                 public override EventInfo GetEvent( string name, BindingFlags bindingAttr)
193                 {
194                         return _tb.GetEvent (name, bindingAttr);
195                 }
196
197                 public override EventInfo[] GetEvents()
198                 {
199                         return _tb.GetEvents ();
200                 }
201
202                 public override EventInfo[] GetEvents( BindingFlags bindingAttr)
203                 {
204                         return _tb.GetEvents (bindingAttr);
205                 }
206
207                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
208                 {
209                         return _tb.GetField (name, bindingAttr);
210                 }
211
212                 public override FieldInfo[] GetFields( BindingFlags bindingAttr)
213                 {
214                         return _tb.GetFields (bindingAttr);
215                 }
216
217                 public override Type GetInterface (string name, bool ignoreCase)
218                 {
219                         return _tb.GetInterface (name, ignoreCase);
220                 }
221
222                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
223                 {
224                         return _tb.GetInterfaceMap (interfaceType);
225                 }
226
227                 public override Type[] GetInterfaces()
228                 {
229                         return _tb.GetInterfaces ();
230                 }
231
232                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
233                 {
234                         return _tb.GetMember (name, type, bindingAttr);
235                 }
236
237                 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
238                 {
239                         return _tb.GetMembers (bindingAttr);
240                 }
241
242                 protected override MethodInfo GetMethodImpl (
243                         string name, BindingFlags bindingAttr, Binder binder,
244                         CallingConventions callConvention, Type[] types,
245                         ParameterModifier[] modifiers)
246                 {
247                         if (types == null) {
248                                 return _tb.GetMethod (name, bindingAttr);
249                         }
250
251                         return _tb.GetMethod (name, bindingAttr, binder, 
252                                 callConvention, types, modifiers);
253                 }
254
255                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
256                 {
257                         return _tb.GetMethods (bindingAttr);
258                 }
259
260                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
261                 {
262                         return _tb.GetNestedType (name, bindingAttr);
263                 }
264
265                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
266                 {
267                         return _tb.GetNestedTypes (bindingAttr);
268                 }
269
270                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
271                 {
272                         return _tb.GetProperties (bindingAttr);
273                 }
274
275                 protected override PropertyInfo GetPropertyImpl (
276                         string name, BindingFlags bindingAttr, Binder binder,
277                         Type returnType, Type[] types,
278                         ParameterModifier[] modifiers)
279                 {
280                         throw CreateNotSupportedException ();
281                 }
282
283                 protected override bool HasElementTypeImpl ()
284                 {
285                         return _tb.HasElementType;
286                 }
287
288                 public override object InvokeMember (
289                         string name, BindingFlags invokeAttr, Binder binder,
290                         object target, object[] args,
291                         ParameterModifier[] modifiers, CultureInfo culture,
292                         string[] namedParameters)
293                 {
294                         return _tb.InvokeMember (name, invokeAttr, binder, target, 
295                                 args, modifiers, culture, namedParameters);
296                 }
297
298                 protected override bool IsArrayImpl()
299                 {
300                         return false;
301                 }
302
303                 protected override bool IsByRefImpl()
304                 {
305                         return false;
306                 }
307
308                 protected override bool IsCOMObjectImpl()
309                 {
310                         return false;
311                 }
312
313                 protected override bool IsPointerImpl()
314                 {
315                         return false;
316                 }
317
318                 protected override bool IsPrimitiveImpl()
319                 {
320                         return false;
321                 }
322
323                 protected override bool IsValueTypeImpl()
324                 {
325                         return true;
326                 }
327
328                 public override bool IsDefined (Type attributeType, bool inherit)
329                 {
330                         return _tb.IsDefined (attributeType, inherit);
331                 }
332
333                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
334                 {
335                         if (cattrs != null) {
336                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
337                                 cattrs.CopyTo (new_array, 0);
338                                 new_array [cattrs.Length] = customBuilder;
339                                 cattrs = new_array;
340                         } else {
341                                 cattrs = new CustomAttributeBuilder [1];
342                                 cattrs [0] = customBuilder;
343                         }
344                 }
345
346                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
347                 {
348                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
349                 }
350
351 #if NET_2_0 || BOOTSTRAP_NET_2_0
352                 [MonoTODO]
353                 public override Type[] GetGenericArguments ()
354                 {
355                         throw new NotImplementedException ();
356                 }
357
358                 [MonoTODO]
359                 public override bool HasGenericArguments {
360                         get {
361                                 throw new NotImplementedException ();
362                         }
363                 }
364
365                 [MonoTODO]
366                 public override bool ContainsGenericParameters {
367                         get {
368                                 throw new NotImplementedException ();
369                         }
370                 }
371
372                 [MonoTODO]
373                 public override bool IsGenericParameter {
374                         get {
375                                 throw new NotImplementedException ();
376                         }
377                 }
378
379                 [MonoTODO]
380                 public override int GenericParameterPosition {
381                         get {
382                                 throw new NotImplementedException ();
383                         }
384                 }
385
386                 [MonoTODO]
387                 public override MethodInfo DeclaringMethod {
388                         get {
389                                 throw new NotImplementedException ();
390                         }
391                 }
392 #endif
393
394                 private Exception CreateNotSupportedException ()
395                 {
396                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
397                 }
398         }
399 }