Sre cleanup9 (#3661)
[mono.git] / mcs / class / corlib / System.Reflection / MonoGenericClass.cs
1 //
2 // System.Reflection.MonoGenericClass
3 //
4 // Sean MacIsaac (macisaac@ximian.com)
5 // Paolo Molaro (lupus@ximian.com)
6 // Patrik Torstensson (patrik.torstensson@labs2.com)
7 //
8 // (C) 2001 Ximian, Inc.
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if !FULL_AOT_RUNTIME
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Collections;
38 using System.Runtime.CompilerServices;
39 using System.Globalization;
40 using System.Runtime.Serialization;
41 using System.Text;
42 using System.Runtime.InteropServices;
43
44 namespace System.Reflection
45 {
46         /*
47          * MonoGenericClass represents an instantiation of a generic TypeBuilder. MS
48          * calls this class TypeBuilderInstantiation (a much better name). MS returns 
49          * NotImplementedException for many of the methods but we can't do that as gmcs
50          * depends on them.
51          */
52         [StructLayout (LayoutKind.Sequential)]
53         sealed class MonoGenericClass :
54                 TypeInfo
55         {
56                 #region Keep in sync with object-internals.h
57 #pragma warning disable 649
58                 internal Type generic_type;
59                 Type[] type_arguments;
60 #pragma warning restore 649
61                 #endregion
62
63                 Hashtable fields, ctors, methods;
64
65                 internal MonoGenericClass ()
66                 {
67                         // this should not be used
68                         throw new InvalidOperationException ();
69                 }
70
71                 internal MonoGenericClass (Type tb, Type[] args)
72                 {
73                         this.generic_type = tb;
74                         this.type_arguments = args;
75                 }
76
77                 internal override Type InternalResolve ()
78                 {
79                         Type gtd = generic_type.InternalResolve ();
80                         Type[] args = new Type [type_arguments.Length];
81                         for (int i = 0; i < type_arguments.Length; ++i)
82                                 args [i] = type_arguments [i].InternalResolve ();
83                         return gtd.MakeGenericType (args);
84                 }
85
86                 // Called from the runtime to return the corresponding finished Type object
87                 internal override Type RuntimeResolve ()
88                 {
89                         if (generic_type is TypeBuilder && !(generic_type as TypeBuilder).IsCreated ())
90                                 AppDomain.CurrentDomain.DoTypeResolve (generic_type);
91                         for (int i = 0; i < type_arguments.Length; ++i) {
92                                 var t = type_arguments [i];
93                                 if (t is TypeBuilder && !(t as TypeBuilder).IsCreated ())
94                                         AppDomain.CurrentDomain.DoTypeResolve (t);
95                         }
96                         return InternalResolve ();
97                 }
98
99                 internal bool IsCreated {
100                         get {
101                                 TypeBuilder tb = generic_type as TypeBuilder;
102                                 return tb != null ? tb.is_created : true;
103                         }
104                 }
105
106                 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
107                 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
108
109                 Type GetParentType ()
110                 {
111                         return InflateType (generic_type.BaseType);             
112                 }
113
114                 internal Type InflateType (Type type)
115                 {
116                         return InflateType (type, type_arguments, null);
117                 }
118
119                 internal Type InflateType (Type type, Type[] method_args)
120                 {
121                         return InflateType (type, type_arguments, method_args);
122                 }
123
124                 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
125                 {
126                         if (type == null)
127                                 return null;
128                         if (!type.IsGenericParameter && !type.ContainsGenericParameters)
129                                 return type;
130                         if (type.IsGenericParameter) {
131                                 if (type.DeclaringMethod == null)
132                                         return type_args == null ? type : type_args [type.GenericParameterPosition];
133                                 return method_args == null ? type : method_args [type.GenericParameterPosition];
134                         }
135                         if (type.IsPointer)
136                                 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
137                         if (type.IsByRef)
138                                 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
139                         if (type.IsArray) {
140                                 if (type.GetArrayRank () > 1)
141                                         return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
142                                 
143                                 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
144                                         return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
145                                 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
146                         }
147
148                         Type[] args = type.GetGenericArguments ();
149                         for (int i = 0; i < args.Length; ++i)
150                                 args [i] = InflateType (args [i], type_args, method_args);
151
152                         Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
153                         return gtd.MakeGenericType (args);
154                 }
155                 
156                 public override Type BaseType {
157                         get { return generic_type.BaseType; }
158                 }
159
160                 public override Type[] GetInterfaces ()
161                 {
162                         throw new NotSupportedException ();
163                 }
164
165                 protected override bool IsValueTypeImpl ()
166                 {
167                         return generic_type.IsValueType;
168                 }
169
170                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
171                 {
172                         if (methods == null)
173                                 methods = new Hashtable ();
174                         if (!methods.ContainsKey (fromNoninstanciated))
175                                 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
176                         return (MethodInfo)methods [fromNoninstanciated];
177                 }
178
179                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
180                 {
181                         if (ctors == null)
182                                 ctors = new Hashtable ();
183                         if (!ctors.ContainsKey (fromNoninstanciated))
184                                 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
185                         return (ConstructorInfo)ctors [fromNoninstanciated];
186                 }
187
188                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
189                 {
190                         if (fields == null)
191                                 fields = new Hashtable ();
192                         if (!fields.ContainsKey (fromNoninstanciated))
193                                 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
194                         return (FieldInfo)fields [fromNoninstanciated];
195                 }
196                 
197                 public override MethodInfo[] GetMethods (BindingFlags bf)
198                 {
199                         throw new NotSupportedException ();
200                 }
201
202                 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
203                 {
204                         throw new NotSupportedException ();
205                 }
206
207                 public override FieldInfo[] GetFields (BindingFlags bf)
208                 {
209                         throw new NotSupportedException ();
210                 }
211
212                 public override PropertyInfo[] GetProperties (BindingFlags bf)
213                 {
214                         throw new NotSupportedException ();
215                 }
216
217                 public override EventInfo[] GetEvents (BindingFlags bf)
218                 {
219                         throw new NotSupportedException ();
220                 }
221
222                 public override Type[] GetNestedTypes (BindingFlags bf)
223                 {
224                         throw new NotSupportedException ();
225                 }
226
227                 public override bool IsAssignableFrom (Type c)
228                 {
229                         throw new NotSupportedException ();
230                 }
231
232                 public override Type UnderlyingSystemType {
233                         get { return this; }
234                 }
235
236                 public override Assembly Assembly {
237                         get { return generic_type.Assembly; }
238                 }
239
240                 public override Module Module {
241                         get { return generic_type.Module; }
242                 }
243
244                 public override string Name {
245                         get { return generic_type.Name; }
246                 }
247
248                 public override string Namespace {
249                         get { return generic_type.Namespace; }
250                 }
251
252                 public override string FullName {
253                         get { return format_name (true, false); }
254                 }
255
256                 public override string AssemblyQualifiedName {
257                         get { return format_name (true, true); }
258                 }
259
260                 public override Guid GUID {
261                         get { throw new NotSupportedException (); }
262                 }
263
264                 string format_name (bool full_name, bool assembly_qualified)
265                 {
266                         StringBuilder sb = new StringBuilder (generic_type.FullName);
267
268                         sb.Append ("[");
269                         for (int i = 0; i < type_arguments.Length; ++i) {
270                                 if (i > 0)
271                                         sb.Append (",");
272                                 
273                                 string name;
274                                 if (full_name) {
275                                         string assemblyName = type_arguments [i].Assembly.FullName;
276                                         name = type_arguments [i].FullName;
277                                         if (name != null && assemblyName != null)
278                                                 name = name + ", " + assemblyName;
279                                 } else {
280                                         name = type_arguments [i].ToString ();
281                                 }
282                                 if (name == null) {
283                                         return null;
284                                 }
285                                 if (full_name)
286                                         sb.Append ("[");
287                                 sb.Append (name);
288                                 if (full_name)
289                                         sb.Append ("]");
290                         }
291                         sb.Append ("]");
292                         if (assembly_qualified) {
293                                 sb.Append (", ");
294                                 sb.Append (generic_type.Assembly.FullName);
295                         }
296                         return sb.ToString ();
297                 }
298
299                 public override string ToString ()
300                 {
301                         return format_name (false, false);
302                 }
303
304                 public override Type GetGenericTypeDefinition ()
305                 {
306                         return generic_type;
307                 }
308
309                 public override Type[] GetGenericArguments ()
310                 {
311                         Type[] ret = new Type [type_arguments.Length];
312                         type_arguments.CopyTo (ret, 0);
313                         return ret;
314                 }
315
316                 public override bool ContainsGenericParameters {
317                         get {
318                                 foreach (Type t in type_arguments) {
319                                         if (t.ContainsGenericParameters)
320                                                 return true;
321                                 }
322                                 return false;
323                         }
324                 }
325
326                 public override bool IsGenericTypeDefinition {
327                         get { return false; }
328                 }
329
330                 public override bool IsGenericType {
331                         get { return true; }
332                 }
333
334                 public override Type DeclaringType {
335                         get { return generic_type.DeclaringType; }
336                 }
337
338                 public override RuntimeTypeHandle TypeHandle {
339                         get {
340                                 throw new NotSupportedException ();
341                         }
342                 }
343
344                 public override Type MakeArrayType ()
345                 {
346                         return new ArrayType (this, 0);
347                 }
348
349                 public override Type MakeArrayType (int rank)
350                 {
351                         if (rank < 1)
352                                 throw new IndexOutOfRangeException ();
353                         return new ArrayType (this, rank);
354                 }
355
356                 public override Type MakeByRefType ()
357                 {
358                         return new ByRefType (this);
359                 }
360
361                 public override Type MakePointerType ()
362                 {
363                         return new PointerType (this);
364                 }
365
366                 public override Type GetElementType ()
367                 {
368                         throw new NotSupportedException ();
369                 }
370
371                 protected override bool HasElementTypeImpl ()
372                 {
373                         return false;
374                 }
375
376                 protected override bool IsCOMObjectImpl ()
377                 {
378                         return false;
379                 }
380
381                 protected override bool IsPrimitiveImpl ()
382                 {
383                         return false;
384                 }
385
386                 protected override bool IsArrayImpl ()
387                 {
388                         return false;
389                 }
390
391                 protected override bool IsByRefImpl ()
392                 {
393                         return false;
394                 }
395
396                 protected override bool IsPointerImpl ()
397                 {
398                         return false;
399                 }
400
401                 protected override TypeAttributes GetAttributeFlagsImpl ()
402                 {
403                         return generic_type.Attributes; 
404                 }
405
406                 //stuff that throws
407                 public override Type GetInterface (string name, bool ignoreCase)
408                 {
409                         throw new NotSupportedException ();
410                 }
411
412                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
413                 {
414                         throw new NotSupportedException ();
415                 }
416
417                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
418                 {
419                         throw new NotSupportedException ();
420                 }
421
422                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
423                 {
424                         throw new NotSupportedException ();
425                 }
426
427                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
428                 {
429                         throw new NotSupportedException ();
430                 }
431
432                 public override object InvokeMember (string name, BindingFlags invokeAttr,
433                                                      Binder binder, object target, object[] args,
434                                                      ParameterModifier[] modifiers,
435                                                      CultureInfo culture, string[] namedParameters)
436                 {
437                         throw new NotSupportedException ();
438                 }
439
440                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
441                                                              CallingConventions callConvention, Type[] types,
442                                                              ParameterModifier[] modifiers)
443                 {
444                         throw new NotSupportedException ();
445                 }
446
447                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
448                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers)
449                 {
450                         throw new NotSupportedException ();
451                 }
452
453                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
454                                                                        Binder binder,
455                                                                        CallingConventions callConvention,
456                                                                        Type[] types,
457                                                                        ParameterModifier[] modifiers)
458                 {
459                         throw new NotSupportedException ();
460                 }
461
462                 //MemberInfo
463                 public override bool IsDefined (Type attributeType, bool inherit)
464                 {
465                         throw new NotSupportedException ();
466                 }
467
468                 public override object [] GetCustomAttributes (bool inherit)
469                 {
470                         if (IsCreated)
471                                 return generic_type.GetCustomAttributes (inherit);
472                         throw new NotSupportedException ();
473                 }
474
475                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
476                 {
477                         if (IsCreated)
478                                 return generic_type.GetCustomAttributes (attributeType, inherit);
479                         throw new NotSupportedException ();
480                 }
481
482                 internal override bool IsUserType {
483                         get {
484                                 foreach (var t in type_arguments) {
485                                         if (t.IsUserType)
486                                                 return true;
487                                 }
488                                 return false;
489                         }
490                 }
491
492         }
493 }
494
495 #endif