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