Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilderInstantiation.cs
1 //
2 // System.Reflection.Emit.TypeBuilderInstantiation
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.Emit
45 {
46         /*
47          * TypeBuilderInstantiation represents an instantiation of a generic TypeBuilder.
48          */
49         [StructLayout (LayoutKind.Sequential)]
50         sealed class TypeBuilderInstantiation :
51                 TypeInfo
52         {
53                 #region Keep in sync with object-internals.h
54 #pragma warning disable 649
55                 internal Type generic_type;
56                 Type[] type_arguments;
57 #pragma warning restore 649
58                 #endregion
59
60                 Hashtable fields, ctors, methods;
61
62                 internal TypeBuilderInstantiation ()
63                 {
64                         // this should not be used
65                         throw new InvalidOperationException ();
66                 }
67
68                 internal TypeBuilderInstantiation (Type tb, Type[] args)
69                 {
70                         this.generic_type = tb;
71                         this.type_arguments = args;
72                 }
73
74                 internal override Type InternalResolve ()
75                 {
76                         Type gtd = generic_type.InternalResolve ();
77                         Type[] args = new Type [type_arguments.Length];
78                         for (int i = 0; i < type_arguments.Length; ++i)
79                                 args [i] = type_arguments [i].InternalResolve ();
80                         return gtd.MakeGenericType (args);
81                 }
82
83                 // Called from the runtime to return the corresponding finished Type object
84                 internal override Type RuntimeResolve ()
85                 {
86                         if (generic_type is TypeBuilder && !(generic_type as TypeBuilder).IsCreated ())
87                                 AppDomain.CurrentDomain.DoTypeResolve (generic_type);
88                         for (int i = 0; i < type_arguments.Length; ++i) {
89                                 var t = type_arguments [i];
90                                 if (t is TypeBuilder && !(t as TypeBuilder).IsCreated ())
91                                         AppDomain.CurrentDomain.DoTypeResolve (t);
92                         }
93                         return InternalResolve ();
94                 }
95
96                 internal bool IsCreated {
97                         get {
98                                 TypeBuilder tb = generic_type as TypeBuilder;
99                                 return tb != null ? tb.is_created : true;
100                         }
101                 }
102
103                 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
104                 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
105
106                 Type GetParentType ()
107                 {
108                         return InflateType (generic_type.BaseType);             
109                 }
110
111                 internal Type InflateType (Type type)
112                 {
113                         return InflateType (type, type_arguments, null);
114                 }
115
116                 internal Type InflateType (Type type, Type[] method_args)
117                 {
118                         return InflateType (type, type_arguments, method_args);
119                 }
120
121                 internal static Type InflateType (Type type, Type[] type_args, Type[] method_args)
122                 {
123                         if (type == null)
124                                 return null;
125                         if (!type.IsGenericParameter && !type.ContainsGenericParameters)
126                                 return type;
127                         if (type.IsGenericParameter) {
128                                 if (type.DeclaringMethod == null)
129                                         return type_args == null ? type : type_args [type.GenericParameterPosition];
130                                 return method_args == null ? type : method_args [type.GenericParameterPosition];
131                         }
132                         if (type.IsPointer)
133                                 return InflateType (type.GetElementType (), type_args, method_args).MakePointerType ();
134                         if (type.IsByRef)
135                                 return InflateType (type.GetElementType (), type_args, method_args).MakeByRefType ();
136                         if (type.IsArray) {
137                                 if (type.GetArrayRank () > 1)
138                                         return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (type.GetArrayRank ());
139                                 
140                                 if (type.ToString ().EndsWith ("[*]", StringComparison.Ordinal)) /*FIXME, the reflection API doesn't offer a way around this*/
141                                         return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType (1);
142                                 return InflateType (type.GetElementType (), type_args, method_args).MakeArrayType ();
143                         }
144
145                         Type[] args = type.GetGenericArguments ();
146                         for (int i = 0; i < args.Length; ++i)
147                                 args [i] = InflateType (args [i], type_args, method_args);
148
149                         Type gtd = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition ();
150                         return gtd.MakeGenericType (args);
151                 }
152                 
153                 public override Type BaseType {
154                         get { return generic_type.BaseType; }
155                 }
156
157                 public override Type[] GetInterfaces ()
158                 {
159                         throw new NotSupportedException ();
160                 }
161
162                 protected override bool IsValueTypeImpl ()
163                 {
164                         return generic_type.IsValueType;
165                 }
166
167                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
168                 {
169                         if (methods == null)
170                                 methods = new Hashtable ();
171                         if (!methods.ContainsKey (fromNoninstanciated))
172                                 methods [fromNoninstanciated] = new MethodOnTypeBuilderInst (this, fromNoninstanciated);
173                         return (MethodInfo)methods [fromNoninstanciated];
174                 }
175
176                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
177                 {
178                         if (ctors == null)
179                                 ctors = new Hashtable ();
180                         if (!ctors.ContainsKey (fromNoninstanciated))
181                                 ctors [fromNoninstanciated] = new ConstructorOnTypeBuilderInst (this, fromNoninstanciated);
182                         return (ConstructorInfo)ctors [fromNoninstanciated];
183                 }
184
185                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
186                 {
187                         if (fields == null)
188                                 fields = new Hashtable ();
189                         if (!fields.ContainsKey (fromNoninstanciated))
190                                 fields [fromNoninstanciated] = new FieldOnTypeBuilderInst (this, fromNoninstanciated);
191                         return (FieldInfo)fields [fromNoninstanciated];
192                 }
193                 
194                 public override MethodInfo[] GetMethods (BindingFlags bf)
195                 {
196                         throw new NotSupportedException ();
197                 }
198
199                 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
200                 {
201                         throw new NotSupportedException ();
202                 }
203
204                 public override FieldInfo[] GetFields (BindingFlags bf)
205                 {
206                         throw new NotSupportedException ();
207                 }
208
209                 public override PropertyInfo[] GetProperties (BindingFlags bf)
210                 {
211                         throw new NotSupportedException ();
212                 }
213
214                 public override EventInfo[] GetEvents (BindingFlags bf)
215                 {
216                         throw new NotSupportedException ();
217                 }
218
219                 public override Type[] GetNestedTypes (BindingFlags bf)
220                 {
221                         throw new NotSupportedException ();
222                 }
223
224                 public override bool IsAssignableFrom (Type c)
225                 {
226                         throw new NotSupportedException ();
227                 }
228
229                 public override Type UnderlyingSystemType {
230                         get { return this; }
231                 }
232
233                 public override Assembly Assembly {
234                         get { return generic_type.Assembly; }
235                 }
236
237                 public override Module Module {
238                         get { return generic_type.Module; }
239                 }
240
241                 public override string Name {
242                         get { return generic_type.Name; }
243                 }
244
245                 public override string Namespace {
246                         get { return generic_type.Namespace; }
247                 }
248
249                 public override string FullName {
250                         get { return format_name (true, false); }
251                 }
252
253                 public override string AssemblyQualifiedName {
254                         get { return format_name (true, true); }
255                 }
256
257                 public override Guid GUID {
258                         get { throw new NotSupportedException (); }
259                 }
260
261                 string format_name (bool full_name, bool assembly_qualified)
262                 {
263                         StringBuilder sb = new StringBuilder (generic_type.FullName);
264
265                         sb.Append ("[");
266                         for (int i = 0; i < type_arguments.Length; ++i) {
267                                 if (i > 0)
268                                         sb.Append (",");
269                                 
270                                 string name;
271                                 if (full_name) {
272                                         string assemblyName = type_arguments [i].Assembly.FullName;
273                                         name = type_arguments [i].FullName;
274                                         if (name != null && assemblyName != null)
275                                                 name = name + ", " + assemblyName;
276                                 } else {
277                                         name = type_arguments [i].ToString ();
278                                 }
279                                 if (name == null) {
280                                         return null;
281                                 }
282                                 if (full_name)
283                                         sb.Append ("[");
284                                 sb.Append (name);
285                                 if (full_name)
286                                         sb.Append ("]");
287                         }
288                         sb.Append ("]");
289                         if (assembly_qualified) {
290                                 sb.Append (", ");
291                                 sb.Append (generic_type.Assembly.FullName);
292                         }
293                         return sb.ToString ();
294                 }
295
296                 public override string ToString ()
297                 {
298                         return format_name (false, false);
299                 }
300
301                 public override Type GetGenericTypeDefinition ()
302                 {
303                         return generic_type;
304                 }
305
306                 public override Type[] GetGenericArguments ()
307                 {
308                         Type[] ret = new Type [type_arguments.Length];
309                         type_arguments.CopyTo (ret, 0);
310                         return ret;
311                 }
312
313                 public override bool ContainsGenericParameters {
314                         get {
315                                 foreach (Type t in type_arguments) {
316                                         if (t.ContainsGenericParameters)
317                                                 return true;
318                                 }
319                                 return false;
320                         }
321                 }
322
323                 public override bool IsGenericTypeDefinition {
324                         get { return false; }
325                 }
326
327                 public override bool IsGenericType {
328                         get { return true; }
329                 }
330
331                 public override Type DeclaringType {
332                         get { return generic_type.DeclaringType; }
333                 }
334
335                 public override RuntimeTypeHandle TypeHandle {
336                         get {
337                                 throw new NotSupportedException ();
338                         }
339                 }
340
341                 public override Type MakeArrayType ()
342                 {
343                         return new ArrayType (this, 0);
344                 }
345
346                 public override Type MakeArrayType (int rank)
347                 {
348                         if (rank < 1)
349                                 throw new IndexOutOfRangeException ();
350                         return new ArrayType (this, rank);
351                 }
352
353                 public override Type MakeByRefType ()
354                 {
355                         return new ByRefType (this);
356                 }
357
358                 public override Type MakePointerType ()
359                 {
360                         return new PointerType (this);
361                 }
362
363                 public override Type GetElementType ()
364                 {
365                         throw new NotSupportedException ();
366                 }
367
368                 protected override bool HasElementTypeImpl ()
369                 {
370                         return false;
371                 }
372
373                 protected override bool IsCOMObjectImpl ()
374                 {
375                         return false;
376                 }
377
378                 protected override bool IsPrimitiveImpl ()
379                 {
380                         return false;
381                 }
382
383                 protected override bool IsArrayImpl ()
384                 {
385                         return false;
386                 }
387
388                 protected override bool IsByRefImpl ()
389                 {
390                         return false;
391                 }
392
393                 protected override bool IsPointerImpl ()
394                 {
395                         return false;
396                 }
397
398                 protected override TypeAttributes GetAttributeFlagsImpl ()
399                 {
400                         return generic_type.Attributes; 
401                 }
402
403                 //stuff that throws
404                 public override Type GetInterface (string name, bool ignoreCase)
405                 {
406                         throw new NotSupportedException ();
407                 }
408
409                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
410                 {
411                         throw new NotSupportedException ();
412                 }
413
414                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
415                 {
416                         throw new NotSupportedException ();
417                 }
418
419                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
420                 {
421                         throw new NotSupportedException ();
422                 }
423
424                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
425                 {
426                         throw new NotSupportedException ();
427                 }
428
429                 public override object InvokeMember (string name, BindingFlags invokeAttr,
430                                                      Binder binder, object target, object[] args,
431                                                      ParameterModifier[] modifiers,
432                                                      CultureInfo culture, string[] namedParameters)
433                 {
434                         throw new NotSupportedException ();
435                 }
436
437                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
438                                                              CallingConventions callConvention, Type[] types,
439                                                              ParameterModifier[] modifiers)
440                 {
441                         throw new NotSupportedException ();
442                 }
443
444                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
445                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers)
446                 {
447                         throw new NotSupportedException ();
448                 }
449
450                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
451                                                                        Binder binder,
452                                                                        CallingConventions callConvention,
453                                                                        Type[] types,
454                                                                        ParameterModifier[] modifiers)
455                 {
456                         throw new NotSupportedException ();
457                 }
458
459                 //MemberInfo
460                 public override bool IsDefined (Type attributeType, bool inherit)
461                 {
462                         throw new NotSupportedException ();
463                 }
464
465                 public override object [] GetCustomAttributes (bool inherit)
466                 {
467                         if (IsCreated)
468                                 return generic_type.GetCustomAttributes (inherit);
469                         throw new NotSupportedException ();
470                 }
471
472                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
473                 {
474                         if (IsCreated)
475                                 return generic_type.GetCustomAttributes (attributeType, inherit);
476                         throw new NotSupportedException ();
477                 }
478
479                 internal override bool IsUserType {
480                         get {
481                                 foreach (var t in type_arguments) {
482                                         if (t.IsUserType)
483                                                 return true;
484                                 }
485                                 return false;
486                         }
487                 }
488
489                 internal static Type MakeGenericType (Type type, Type[] typeArguments)
490                 {
491                         return new TypeBuilderInstantiation (type, typeArguments);
492                 }
493         }
494 }
495 #else
496 namespace System.Reflection.Emit
497 {
498         abstract class TypeBuilderInstantiation : TypeInfo
499         {
500                 internal static Type MakeGenericType (Type type, Type[] typeArguments)
501                 {
502                         throw new NotSupportedException ("User types are not supported under full aot");
503                 }
504         }
505 }
506 #endif