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