2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / GenericTypeParameterBuilder.cs
1 //
2 // System.Reflection.Emit.GenericTypeParameterBuilder
3 //
4 // Martin Baulig (martin@ximian.com)
5 //
6 // (C) 2004 Novell, Inc.
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Collections;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Globalization;
38 using System.Runtime.Serialization;
39
40 namespace System.Reflection.Emit
41 {
42         [ComVisible (true)]
43         public sealed class GenericTypeParameterBuilder : Type
44         {
45         #region Sync with reflection.h
46                 private TypeBuilder tbuilder;
47                 private MethodBuilder mbuilder;
48                 private string name;
49                 private int index;
50                 private Type base_type;
51                 private Type[] iface_constraints;
52                 private CustomAttributeBuilder[] cattrs;
53                 private GenericParameterAttributes attrs;
54         #endregion
55
56                 public void SetBaseTypeConstraint (Type baseTypeConstraint)
57                 {
58                         this.base_type = baseTypeConstraint ?? typeof (object);
59                 }
60
61                 [ComVisible (true)]
62                 public void SetInterfaceConstraints (params Type[] interfaceConstraints)
63                 {
64                         this.iface_constraints = interfaceConstraints;
65                 }
66
67                 public void SetGenericParameterAttributes (GenericParameterAttributes genericParameterAttributes)
68                 {
69                         this.attrs = genericParameterAttributes;
70                 }
71
72                 internal GenericTypeParameterBuilder (TypeBuilder tbuilder,
73                                                       MethodBuilder mbuilder,
74                                                       string name, int index)
75                 {
76                         this.tbuilder = tbuilder;
77                         this.mbuilder = mbuilder;
78                         this.name = name;
79                         this.index = index;
80
81                         initialize ();
82                 }
83
84
85                 internal override bool IsCompilerContext {
86                         get {
87                                 return tbuilder.IsCompilerContext;
88                         }
89                 }
90
91                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
92                 private extern void initialize ();
93
94                 [ComVisible (true)]
95                 public override bool IsSubclassOf (Type c)
96                 {
97                         if (!IsCompilerContext)
98                                 throw not_supported ();
99                         if (BaseType == null)
100                                 return false;
101                         else
102                                 return BaseType == c || BaseType.IsSubclassOf (c);
103                 }
104
105                 protected override TypeAttributes GetAttributeFlagsImpl ()
106                 {
107 #if NET_4_0
108                         return TypeAttributes.Public;
109 #else
110                         if (IsCompilerContext)
111                                 return TypeAttributes.Public;
112                         throw not_supported ();
113 #endif
114                 }
115
116                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
117                                                                        Binder binder,
118                                                                        CallingConventions callConvention,
119                                                                        Type[] types,
120                                                                        ParameterModifier[] modifiers)
121                 {
122                         throw not_supported ();
123                 }
124
125                 [ComVisible (true)]
126                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
127                 {
128                         throw not_supported ();
129                 }
130
131                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
132                 {
133                         throw not_supported ();
134                 }
135
136                 public override EventInfo[] GetEvents ()
137                 {
138                         throw not_supported ();
139                 }
140
141                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
142                 {
143                         throw not_supported ();
144                 }
145
146                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
147                 {
148                         throw not_supported ();
149                 }
150
151                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
152                 {
153                         throw not_supported ();
154                 }
155                 
156                 public override Type GetInterface (string name, bool ignoreCase)
157                 {
158                         throw not_supported ();
159                 }
160
161                 public override Type[] GetInterfaces ()
162                 {
163                         throw not_supported ();
164                 }
165                 
166                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
167                 {
168                         throw not_supported ();
169                 }
170
171                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
172                 {
173                         throw not_supported ();
174                 }
175
176                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
177                 {
178                         throw not_supported ();
179                 }
180
181                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
182                                                              Binder binder,
183                                                              CallingConventions callConvention,
184                                                              Type[] types, ParameterModifier[] modifiers)
185                 {
186                         throw not_supported ();
187                 }
188
189                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
190                 {
191                         throw not_supported ();
192                 }
193
194                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
195                 {
196                         throw not_supported ();
197                 }
198
199                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
200                 {
201                         throw not_supported ();
202                 }
203
204                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
205                                                                  Binder binder, Type returnType,
206                                                                  Type[] types,
207                                                                  ParameterModifier[] modifiers)
208                 {
209                         throw not_supported ();
210                 }
211
212                 protected override bool HasElementTypeImpl ()
213                 {
214                         return false;
215                 }
216
217                 public override bool IsAssignableFrom (Type c)
218                 {
219                         throw not_supported ();
220                 }
221
222                 public override bool IsInstanceOfType (object o)
223                 {
224                         throw not_supported ();
225                 }
226
227                 protected override bool IsArrayImpl ()
228                 {
229                         return false;
230                 }
231
232                 protected override bool IsByRefImpl ()
233                 {
234                         return false;
235                 }
236
237                 protected override bool IsCOMObjectImpl ()
238                 {
239                         return false;
240                 }
241
242                 protected override bool IsPointerImpl ()
243                 {
244                         return false;
245                 }
246
247                 protected override bool IsPrimitiveImpl ()
248                 {
249                         return false;
250                 }
251
252                 protected override bool IsValueTypeImpl ()
253                 {
254                         return base_type != null ? base_type.IsValueType : false;
255                 }
256                 
257                 public override object InvokeMember (string name, BindingFlags invokeAttr,
258                                                      Binder binder, object target, object[] args,
259                                                      ParameterModifier[] modifiers,
260                                                      CultureInfo culture, string[] namedParameters)
261                 {
262                         throw not_supported ();
263                 }
264
265                 public override Type GetElementType ()
266                 {
267                         throw not_supported ();
268                 }
269
270                 public override Type UnderlyingSystemType {
271                         get {
272                                 return this;
273                         }
274                 }
275
276                 public override Assembly Assembly {
277                         get { return tbuilder.Assembly; }
278                 }
279
280                 public override string AssemblyQualifiedName {
281                         get { return null; }
282                 }
283
284                 public override Type BaseType {
285                         get { return base_type; }
286                 }
287
288                 public override string FullName {
289                         get { return null; }
290                 }
291
292                 public override Guid GUID {
293                         get { throw not_supported (); }
294                 }
295
296                 public override bool IsDefined (Type attributeType, bool inherit)
297                 {
298                         throw not_supported ();
299                 }
300
301                 public override object[] GetCustomAttributes (bool inherit)
302                 {
303                         throw not_supported ();
304                 }
305
306                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
307                 {
308                         throw not_supported ();
309                 }
310
311                 [ComVisible (true)]
312                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
313                 {
314                         throw not_supported ();
315                 }
316
317                 public override string Name {
318                         get { return name; }
319                 }
320
321                 public override string Namespace {
322                         get { return null; }
323                 }
324
325                 public override Module Module {
326                         get { return tbuilder.Module; }
327                 }
328
329                 public override Type DeclaringType {
330                         get { return mbuilder != null ? mbuilder.DeclaringType : tbuilder; }
331                 }
332
333                 public override Type ReflectedType {
334                         get { return DeclaringType; }
335                 }
336
337                 public override RuntimeTypeHandle TypeHandle {
338                         get { throw not_supported (); }
339                 }
340
341                 public override Type[] GetGenericArguments ()
342                 {
343                         throw new InvalidOperationException ();
344                 }
345
346                 public override Type GetGenericTypeDefinition ()
347                 {
348                         throw new InvalidOperationException ();
349                 }
350
351                 public override bool ContainsGenericParameters {
352                         get { return true; }
353                 }
354
355                 public override bool IsGenericParameter {
356                         get { return true; }
357                 }
358
359                 public override bool IsGenericType {
360                         get { return false; }
361                 }
362
363                 public override bool IsGenericTypeDefinition {
364                         get { return false; }
365                 }
366
367                 public override GenericParameterAttributes GenericParameterAttributes {
368                         get {
369                                 if (IsCompilerContext)
370                                         return attrs;
371                                 throw new NotSupportedException ();
372                         }
373                 }
374
375                 public override int GenericParameterPosition {
376                         get { return index; }
377                 }
378
379                 public override Type[] GetGenericParameterConstraints ()
380                 {
381                         if (!IsCompilerContext)
382                                 throw new InvalidOperationException ();
383                         if (base_type == null) {
384                                 if (iface_constraints != null)
385                                         return iface_constraints;
386
387                                 return Type.EmptyTypes;
388                         }
389
390                         if (iface_constraints == null)
391                                 return new Type[] { base_type };
392
393                         Type[] ret = new Type [iface_constraints.Length + 1];
394                         ret [0] = base_type;
395                         iface_constraints.CopyTo (ret, 1);
396                         return ret;
397                 }
398
399                 public override MethodBase DeclaringMethod {
400                         get { return mbuilder; }
401                 }
402
403                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
404                 {
405                         if (customBuilder == null)
406                                 throw new ArgumentNullException ("customBuilder");
407
408                         if (cattrs != null) {
409                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
410                                 cattrs.CopyTo (new_array, 0);
411                                 new_array [cattrs.Length] = customBuilder;
412                                 cattrs = new_array;
413                         } else {
414                                 cattrs = new CustomAttributeBuilder [1];
415                                 cattrs [0] = customBuilder;
416                         }
417                 }
418
419                 [MonoTODO ("unverified implementation")]
420                 public void SetCustomAttribute (ConstructorInfo con, byte [] binaryAttribute)
421                 {
422                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
423                 }
424
425                 private Exception not_supported ()
426                 {
427                         return new NotSupportedException ();
428                 }
429
430                 public override string ToString ()
431                 {
432                         return name;
433                 }
434
435                 [MonoTODO]
436                 public override bool Equals (object o)
437                 {
438                         return base.Equals (o);
439                 }
440
441                 [MonoTODO]
442                 public override int GetHashCode ()
443                 {
444                         return base.GetHashCode ();
445                 }
446
447                 public override Type MakeArrayType ()
448                 {
449                         return  new ArrayType (this, 0);
450                 }
451
452                 public override Type MakeArrayType (int rank)
453                 {
454                         if (rank < 1)
455                                 throw new IndexOutOfRangeException ();
456                         return new ArrayType (this, rank);
457                 }
458
459                 public override Type MakeByRefType ()
460                 {
461                         return new ByRefType (this);
462                 }
463
464                 [MonoTODO]
465                 public override Type MakeGenericType (params Type [] typeArguments)
466                 {
467                         return base.MakeGenericType (typeArguments);
468                 }
469
470                 public override Type MakePointerType ()
471                 {
472                         return new PointerType (this);
473                 }
474         }
475 }