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