System.Reflection.Emit's turn to be de-ifdefified
[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 using System.Runtime.InteropServices;
40
41 namespace System.Reflection.Emit {
42         [ComVisible (true)]
43         [ComDefaultInterface (typeof (_EnumBuilder))]
44         [ClassInterface (ClassInterfaceType.None)]
45         public sealed class EnumBuilder : Type, _EnumBuilder {
46                 private TypeBuilder _tb;
47                 private FieldBuilder _underlyingField;
48                 private Type _underlyingType;
49
50                 internal EnumBuilder (ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType)
51                 {
52                         _tb = new TypeBuilder (mb, name, (visibility | TypeAttributes.Sealed), 
53                                 typeof(Enum), null, PackingSize.Unspecified, 0, null);
54                         _underlyingType = underlyingType;
55                         _underlyingField = _tb.DefineField ("value__", underlyingType,
56                                 (FieldAttributes.SpecialName | FieldAttributes.Private | FieldAttributes.RTSpecialName));
57                         setup_enum_type (_tb);
58                 }
59
60                 internal TypeBuilder GetTypeBuilder ()
61                 {
62                         return _tb;
63                 }
64
65                 public override Assembly Assembly {
66                         get {
67                                 return _tb.Assembly;
68                         }
69                 }
70
71                 public override string AssemblyQualifiedName {
72                         get {
73                                 return _tb.AssemblyQualifiedName;
74                         }
75                 }
76
77                 public override Type BaseType {
78                         get {
79                                 return _tb.BaseType;
80                         }
81                 }
82
83                 public override Type DeclaringType {
84                         get {
85                                 return _tb.DeclaringType;
86                         }
87                 }
88
89                 public override string FullName {
90                         get {
91                                 return _tb.FullName;
92                         }
93                 }
94
95                 public override Guid GUID {
96                         get {
97                                 return _tb.GUID;
98                         }
99                 }
100
101                 public override Module Module {
102                         get {
103                                 return _tb.Module;
104                         }
105                 }
106
107                 public override string Name {
108                         get {
109                                 return _tb.Name;
110                         }
111                 }
112
113                 public override string Namespace {
114                         get { 
115                                 return _tb.Namespace;
116                         }
117                 }
118
119                 public override Type ReflectedType {
120                         get {
121                                 return _tb.ReflectedType;
122                         }
123                 }
124
125                 public override RuntimeTypeHandle TypeHandle {
126                         get {
127                                 return _tb.TypeHandle;
128                         }
129                 }
130
131                 public TypeToken TypeToken {
132                         get {
133                                 return _tb.TypeToken;
134                         }
135                 }
136
137                 public FieldBuilder UnderlyingField {
138                         get {
139                                 return _underlyingField;
140                         }
141                 }
142
143                 public override Type UnderlyingSystemType {
144                         get {
145                                 return _underlyingType;
146                         }
147                 }
148
149                 public Type CreateType ()
150                 {
151                         Type res = _tb.CreateType ();
152                         return res;
153                 }
154
155                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
156                 private extern void setup_enum_type (Type t);
157
158                 public FieldBuilder DefineLiteral (string literalName, object literalValue)
159                 {
160                         Type fieldType = this;
161                         FieldBuilder fieldBuilder = _tb.DefineField (literalName, 
162                                 fieldType, (FieldAttributes.Literal | 
163                                 (FieldAttributes.Static | FieldAttributes.Public)));
164                         fieldBuilder.SetConstant (literalValue);
165                         return fieldBuilder;
166                 }
167
168                 protected override TypeAttributes GetAttributeFlagsImpl ()
169                 {
170                         return _tb.attrs;
171                 }
172
173                 protected override ConstructorInfo GetConstructorImpl (
174                         BindingFlags bindingAttr, Binder binder, CallingConventions callConvention,
175                         Type[] types, ParameterModifier[] modifiers)
176                 {
177                         return _tb.GetConstructor (bindingAttr, binder, callConvention, types, 
178                                 modifiers);
179                 }
180
181                 [ComVisible (true)]
182                 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
183                 {
184                         return _tb.GetConstructors (bindingAttr);
185                 }
186
187                 public override object[] GetCustomAttributes(bool inherit)
188                 {
189                         return _tb.GetCustomAttributes (inherit);
190                 }
191
192                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
193                 {
194                         return _tb.GetCustomAttributes (attributeType, inherit);
195                 }
196
197                 public override Type GetElementType()
198                 {
199                         return _tb.GetElementType ();
200                 }
201
202                 public override EventInfo GetEvent( string name, BindingFlags bindingAttr)
203                 {
204                         return _tb.GetEvent (name, bindingAttr);
205                 }
206
207                 public override EventInfo[] GetEvents()
208                 {
209                         return _tb.GetEvents ();
210                 }
211
212                 public override EventInfo[] GetEvents( BindingFlags bindingAttr)
213                 {
214                         return _tb.GetEvents (bindingAttr);
215                 }
216
217                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
218                 {
219                         return _tb.GetField (name, bindingAttr);
220                 }
221
222                 public override FieldInfo[] GetFields( BindingFlags bindingAttr)
223                 {
224                         return _tb.GetFields (bindingAttr);
225                 }
226
227                 public override Type GetInterface (string name, bool ignoreCase)
228                 {
229                         return _tb.GetInterface (name, ignoreCase);
230                 }
231
232                 [ComVisible (true)]
233                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
234                 {
235                         return _tb.GetInterfaceMap (interfaceType);
236                 }
237
238                 public override Type[] GetInterfaces()
239                 {
240                         return _tb.GetInterfaces ();
241                 }
242
243                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
244                 {
245                         return _tb.GetMember (name, type, bindingAttr);
246                 }
247
248                 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
249                 {
250                         return _tb.GetMembers (bindingAttr);
251                 }
252
253                 protected override MethodInfo GetMethodImpl (
254                         string name, BindingFlags bindingAttr, Binder binder,
255                         CallingConventions callConvention, Type[] types,
256                         ParameterModifier[] modifiers)
257                 {
258                         if (types == null) {
259                                 return _tb.GetMethod (name, bindingAttr);
260                         }
261
262                         return _tb.GetMethod (name, bindingAttr, binder, 
263                                 callConvention, types, modifiers);
264                 }
265
266                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
267                 {
268                         return _tb.GetMethods (bindingAttr);
269                 }
270
271                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
272                 {
273                         return _tb.GetNestedType (name, bindingAttr);
274                 }
275
276                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
277                 {
278                         return _tb.GetNestedTypes (bindingAttr);
279                 }
280
281                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
282                 {
283                         return _tb.GetProperties (bindingAttr);
284                 }
285
286                 protected override PropertyInfo GetPropertyImpl (
287                         string name, BindingFlags bindingAttr, Binder binder,
288                         Type returnType, Type[] types,
289                         ParameterModifier[] modifiers)
290                 {
291                         throw CreateNotSupportedException ();
292                 }
293
294                 protected override bool HasElementTypeImpl ()
295                 {
296                         return _tb.HasElementType;
297                 }
298
299                 public override object InvokeMember (
300                         string name, BindingFlags invokeAttr, Binder binder,
301                         object target, object[] args,
302                         ParameterModifier[] modifiers, CultureInfo culture,
303                         string[] namedParameters)
304                 {
305                         return _tb.InvokeMember (name, invokeAttr, binder, target, 
306                                 args, modifiers, culture, namedParameters);
307                 }
308
309                 protected override bool IsArrayImpl()
310                 {
311                         return false;
312                 }
313
314                 protected override bool IsByRefImpl()
315                 {
316                         return false;
317                 }
318
319                 protected override bool IsCOMObjectImpl()
320                 {
321                         return false;
322                 }
323
324                 protected override bool IsPointerImpl()
325                 {
326                         return false;
327                 }
328
329                 protected override bool IsPrimitiveImpl()
330                 {
331                         return false;
332                 }
333
334                 protected override bool IsValueTypeImpl()
335                 {
336                         return true;
337                 }
338
339                 public override bool IsDefined (Type attributeType, bool inherit)
340                 {
341                         return _tb.IsDefined (attributeType, inherit);
342                 }
343
344                 public override Type MakeArrayType ()
345                 {
346                         return  new ArrayType (this, 0);
347                 }
348
349                 public override Type MakeArrayType (int rank)
350                 {
351                         if (rank < 1)
352                                 throw new IndexOutOfRangeException ();
353                         return new ArrayType (this, rank);
354                 }
355
356                 public override Type MakeByRefType ()
357                 {
358                         return new ByRefType (this);
359                 }
360
361                 public override Type MakePointerType ()
362                 {
363                         return new PointerType (this);
364                 }
365
366                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
367                 {
368                         _tb.SetCustomAttribute (customBuilder);
369                 }
370
371                 [ComVisible (true)]
372                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
373                 {
374                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
375                 }
376
377                 private Exception CreateNotSupportedException ()
378                 {
379                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
380                 }
381
382                 void _EnumBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
383                 {
384                         throw new NotImplementedException ();
385                 }
386
387                 void _EnumBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
388                 {
389                         throw new NotImplementedException ();
390                 }
391
392                 void _EnumBuilder.GetTypeInfoCount (out uint pcTInfo)
393                 {
394                         throw new NotImplementedException ();
395                 }
396
397                 void _EnumBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
398                 {
399                         throw new NotImplementedException ();
400                 }
401         }
402 }