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