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