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