New tests.
[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 using System.Reflection;
31 using System.Reflection.Emit;
32 using System.Collections;
33 using System.Runtime.CompilerServices;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37 using System.Text;
38
39
40 namespace System.Reflection.Emit
41 {
42         internal enum TypeKind : int {
43                 SZARRAY = 0x1d,
44                 ARRAY = 0x14
45         }
46
47         internal abstract class DerivedType : Type
48         {
49                 internal Type elementType;
50
51                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52                 internal static extern void create_unmanaged_type (Type type);
53
54                 internal DerivedType (Type elementType)
55                 {
56                         this.elementType = elementType;
57                 }
58
59                 internal abstract String FormatName (string elementName);
60
61                 internal override bool IsCompilerContext {
62                         get {
63                                 return elementType.IsCompilerContext;
64                         }
65                 }
66
67                 public override Type GetInterface (string name, bool ignoreCase)
68                 {
69                         throw new NotSupportedException ();
70                 }
71
72                 public override Type[] GetInterfaces ()
73                 {
74                         throw new NotSupportedException ();
75                 }
76
77                 public override Type GetElementType ()
78                 {
79                         return elementType;
80                 }
81
82                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
83                 {
84                         throw new NotSupportedException ();
85                 }
86
87                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
88                 {
89                         throw new NotSupportedException ();
90                 }
91
92                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
93                 {
94                         throw new NotSupportedException ();
95                 }
96
97                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
98                 {
99                         throw new NotSupportedException ();
100                 }
101
102                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
103                 {
104                         throw new NotSupportedException ();
105                 }
106
107                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
108                                                              CallingConventions callConvention, Type[] types,
109                                                              ParameterModifier[] modifiers)
110                 {
111                         throw new NotSupportedException ();
112                 }
113
114                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
115                 {
116                         throw new NotSupportedException ();
117                 }
118
119                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
120                 {
121                         throw new NotSupportedException ();
122                 }
123
124                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
125                 {
126                         throw new NotSupportedException ();
127                 }
128
129                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
130                 {
131                         throw new NotSupportedException ();
132                 }
133
134                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
135                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers)
136                 {
137                         throw new NotSupportedException ();
138                 }
139
140                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
141                                                                        Binder binder,
142                                                                        CallingConventions callConvention,
143                                                                        Type[] types,
144                                                                        ParameterModifier[] modifiers)
145                 {
146                         throw new NotSupportedException ();
147                 }
148
149
150                 protected override TypeAttributes GetAttributeFlagsImpl ()
151                 {
152                         /*LAMEIMPL MS just return the elementType.Attributes*/
153                         return elementType.Attributes; 
154                 }
155
156                 protected override bool HasElementTypeImpl ()
157                 {
158                         return true;
159                 }
160
161                 protected override bool IsArrayImpl ()
162                 {
163                         return false;
164                 }
165
166                 protected override bool IsByRefImpl ()
167                 {
168                         return false;
169                 }
170
171                 protected override bool IsCOMObjectImpl ()
172                 {
173                         return false;
174                 }
175
176                 protected override bool IsPointerImpl ()
177                 {
178                         return false;
179                 }
180
181                 protected override bool IsPrimitiveImpl ()
182                 {
183                         return false;
184                 }
185
186
187                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
188                 {
189                         throw new NotSupportedException ();
190                 }
191
192                 public override object InvokeMember (string name, BindingFlags invokeAttr,
193                                                      Binder binder, object target, object[] args,
194                                                      ParameterModifier[] modifiers,
195                                                      CultureInfo culture, string[] namedParameters)
196                 {
197                         throw new NotSupportedException ();
198                 }
199
200                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
201                 {
202                         throw new NotSupportedException ();
203                 }
204
205                 public override bool IsInstanceOfType (object o)
206                 {
207                         return false;
208                 }
209
210                 public override bool IsAssignableFrom (Type c)
211                 {
212                         return false;
213                 }
214
215                 public override bool ContainsGenericParameters {
216                         get { return elementType.ContainsGenericParameters; }
217                 }
218
219                 //FIXME this should be handled by System.Type
220                 public override Type MakeGenericType (params Type[] typeArguments)
221                 {
222                         throw new NotSupportedException ();
223                 }
224
225                 public override Type MakeArrayType ()
226                 {
227                         return new ArrayType (this, 0);
228                 }
229
230                 public override Type MakeArrayType (int rank)
231                 {
232                         if (rank < 1)
233                                 throw new IndexOutOfRangeException ();
234                         return new ArrayType (this, rank);
235                 }
236
237                 public override Type MakeByRefType ()
238                 {
239                         return new ByRefType (this);
240                 }
241
242                 public override Type MakePointerType ()
243                 {
244                         return new PointerType (this);
245                 }
246
247                 public override string ToString ()
248                 {
249                         return FormatName (elementType.ToString ());
250                 }
251
252                 public override GenericParameterAttributes GenericParameterAttributes {
253                         get { throw new NotSupportedException (); }
254                 }
255
256                 public override StructLayoutAttribute StructLayoutAttribute {
257                         get { throw new NotSupportedException (); }
258                 }
259
260                 public override Assembly Assembly {
261                         get { return elementType.Assembly; }
262                 }
263
264                 public override string AssemblyQualifiedName {
265                         get {
266                                 string fullName = FormatName (elementType.FullName);
267                                 if (fullName == null)
268                                         return null;
269                                 return fullName + ", " + elementType.Assembly.FullName;
270                         }
271                 }
272
273
274                 public override string FullName {
275                         get {
276                                 return FormatName (elementType.FullName);
277                         }
278                 }
279
280                 public override string Name {
281                         get {
282                                 return FormatName (elementType.Name);
283                         }
284                 }
285
286                 public override Guid GUID {
287                         get { throw new NotSupportedException (); }
288                 }
289
290                 public override Module Module {
291                         get { return elementType.Module; }
292                 }
293         
294                 public override string Namespace {
295                         get { return elementType.Namespace; }
296                 }
297
298                 public override RuntimeTypeHandle TypeHandle {
299                         get { throw new NotSupportedException (); }
300                 }
301
302                 public override Type UnderlyingSystemType {
303                         get {
304                                 create_unmanaged_type (this);
305                                 return this;
306                         }
307                 }
308
309                 //MemberInfo
310                 public override bool IsDefined (Type attributeType, bool inherit)
311                 {
312                         throw new NotSupportedException ();
313                 }
314
315                 public override object [] GetCustomAttributes (bool inherit)
316                 {
317                         throw new NotSupportedException ();
318                 }
319
320                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
321                 {
322                         throw new NotSupportedException ();
323                 }
324         }
325
326         internal class ArrayType : DerivedType
327         {
328                 int rank;
329
330                 internal ArrayType (Type elementType, int rank) : base (elementType)
331                 {
332                         this.rank = rank;
333                 }
334
335                 internal int GetEffectiveRank ()
336                 {
337                         return rank;
338                 }
339
340                 internal override Type InternalResolve ()
341                 {
342                         Type et = elementType.InternalResolve (); 
343                         if (rank == 0)
344                                 return et.MakeArrayType ();                     
345                         return et.MakeArrayType (rank);
346                 }
347
348                 protected override bool IsArrayImpl ()
349                 {
350                         return true;
351                 }
352
353                 public override int GetArrayRank ()
354                 {
355                         return (rank == 0) ? 1 : rank;
356                 }
357
358                 public override Type BaseType {
359                         get { return typeof (System.Array); }
360                 }
361
362                 protected override TypeAttributes GetAttributeFlagsImpl ()
363                 {
364                         if (IsCompilerContext)
365                                 return (elementType.Attributes & TypeAttributes.VisibilityMask) | TypeAttributes.Sealed | TypeAttributes.Serializable;
366                         return elementType.Attributes;
367                 }
368
369                 internal override String FormatName (string elementName)
370                 {
371                         if (elementName == null)
372                                 return null;
373                         StringBuilder sb = new StringBuilder (elementName);
374                         sb.Append ("[");
375                         for (int i = 1; i < rank; ++i)
376                                 sb.Append (",");
377                         if (rank == 1)
378                                 sb.Append ("*");
379                         sb.Append ("]");
380                         return sb.ToString ();
381                 }
382         }
383
384
385         internal class ByRefType : DerivedType
386         {
387                 internal ByRefType (Type elementType) : base (elementType)
388                 {
389                 }
390
391                 internal override Type InternalResolve ()
392                 {
393                         return elementType.InternalResolve ().MakeByRefType (); 
394                 }
395
396                 protected override bool IsByRefImpl ()
397                 {
398                         return true;
399                 }
400
401                 public override Type BaseType {
402                         get { return typeof (Array); }
403                 }
404
405                 internal override String FormatName (string elementName)
406                 {
407                         if (elementName == null)
408                                 return null;
409                         return elementName + "&";
410                 }
411
412                 public override Type MakeArrayType ()
413                 {
414                         throw new ArgumentException ("Cannot create an array type of a byref type");
415                 }
416
417                 public override Type MakeArrayType (int rank)
418                 {
419                         throw new ArgumentException ("Cannot create an array type of a byref type");
420                 }
421
422                 public override Type MakeByRefType ()
423                 {
424                         throw new ArgumentException ("Cannot create a byref type of an already byref type");
425                 }
426
427                 public override Type MakePointerType ()
428                 {
429                         throw new ArgumentException ("Cannot create a pointer type of a byref type");
430                 }
431         }
432
433
434         internal class PointerType : DerivedType
435         {
436                 internal PointerType (Type elementType) : base (elementType)
437                 {
438                 }
439
440                 internal override Type InternalResolve ()
441                 {
442                         return elementType.InternalResolve ().MakePointerType (); 
443                 }
444
445                 protected override bool IsPointerImpl ()
446                 {
447                         return true;
448                 }
449
450                 public override Type BaseType {
451                         get { return typeof(Array); }
452                 }
453
454                 internal override String FormatName (string elementName)
455                 {
456                         if (elementName == null)
457                                 return null;
458                         return elementName + "*";
459                 }
460         }
461
462 }