Merged into single file, added assertions
[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         [StructLayout (LayoutKind.Sequential)]
48         internal abstract class DerivedType : Type
49         {
50                 internal Type elementType;
51
52                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
53                 internal static extern void create_unmanaged_type (Type type);
54
55                 internal DerivedType (Type elementType)
56                 {
57                         this.elementType = elementType;
58                 }
59
60                 internal abstract String FormatName (string elementName);
61
62                 public override Type GetInterface (string name, bool ignoreCase)
63                 {
64                         throw new NotSupportedException ();
65                 }
66
67                 public override Type[] GetInterfaces ()
68                 {
69                         throw new NotSupportedException ();
70                 }
71
72                 public override Type GetElementType ()
73                 {
74                         return elementType;
75                 }
76
77                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
78                 {
79                         throw new NotSupportedException ();
80                 }
81
82                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
83                 {
84                         throw new NotSupportedException ();
85                 }
86
87                 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
88                 {
89                         throw new NotSupportedException ();
90                 }
91
92                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
93                 {
94                         throw new NotSupportedException ();
95                 }
96
97                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
98                 {
99                         throw new NotSupportedException ();
100                 }
101
102                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
103                                                              CallingConventions callConvention, Type[] types,
104                                                              ParameterModifier[] modifiers)
105                 {
106                         throw new NotSupportedException ();
107                 }
108
109                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
110                 {
111                         throw new NotSupportedException ();
112                 }
113
114                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
115                 {
116                         throw new NotSupportedException ();
117                 }
118
119                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
120                 {
121                         throw new NotSupportedException ();
122                 }
123
124                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
125                 {
126                         throw new NotSupportedException ();
127                 }
128
129                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
130                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers)
131                 {
132                         throw new NotSupportedException ();
133                 }
134
135                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
136                                                                        Binder binder,
137                                                                        CallingConventions callConvention,
138                                                                        Type[] types,
139                                                                        ParameterModifier[] modifiers)
140                 {
141                         throw new NotSupportedException ();
142                 }
143
144
145                 protected override TypeAttributes GetAttributeFlagsImpl ()
146                 {
147                         /*LAMEIMPL MS just return the elementType.Attributes*/
148                         return elementType.Attributes; 
149                 }
150
151                 protected override bool HasElementTypeImpl ()
152                 {
153                         return true;
154                 }
155
156                 protected override bool IsArrayImpl ()
157                 {
158                         return false;
159                 }
160
161                 protected override bool IsByRefImpl ()
162                 {
163                         return false;
164                 }
165
166                 protected override bool IsCOMObjectImpl ()
167                 {
168                         return false;
169                 }
170
171                 protected override bool IsPointerImpl ()
172                 {
173                         return false;
174                 }
175
176                 protected override bool IsPrimitiveImpl ()
177                 {
178                         return false;
179                 }
180
181
182                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
183                 {
184                         throw new NotSupportedException ();
185                 }
186
187                 public override object InvokeMember (string name, BindingFlags invokeAttr,
188                                                      Binder binder, object target, object[] args,
189                                                      ParameterModifier[] modifiers,
190                                                      CultureInfo culture, string[] namedParameters)
191                 {
192                         throw new NotSupportedException ();
193                 }
194
195                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
196                 {
197                         throw new NotSupportedException ();
198                 }
199
200                 public override bool IsInstanceOfType (object o)
201                 {
202                         return false;
203                 }
204
205                 public override bool IsAssignableFrom (Type c)
206                 {
207                         return false;
208                 }
209
210                 public override bool ContainsGenericParameters {
211                         get { return elementType.ContainsGenericParameters; }
212                 }
213
214                 //FIXME this should be handled by System.Type
215                 public override Type MakeGenericType (params Type[] typeArguments)
216                 {
217                         throw new NotSupportedException ();
218                 }
219
220                 public override Type MakeArrayType ()
221                 {
222                         return new ArrayType (this, 0);
223                 }
224
225                 public override Type MakeArrayType (int rank)
226                 {
227                         if (rank < 1)
228                                 throw new IndexOutOfRangeException ();
229                         return new ArrayType (this, rank);
230                 }
231
232                 public override Type MakeByRefType ()
233                 {
234                         return new ByRefType (this);
235                 }
236
237                 public override Type MakePointerType ()
238                 {
239                         return new PointerType (this);
240                 }
241
242                 public override string ToString ()
243                 {
244                         return FormatName (elementType.ToString ());
245                 }
246
247                 public override GenericParameterAttributes GenericParameterAttributes {
248                         get { throw new NotSupportedException (); }
249                 }
250
251                 public override StructLayoutAttribute StructLayoutAttribute {
252                         get { throw new NotSupportedException (); }
253                 }
254
255                 public override Assembly Assembly {
256                         get { return elementType.Assembly; }
257                 }
258
259                 public override string AssemblyQualifiedName {
260                         get {
261                                 string fullName = FormatName (elementType.FullName);
262                                 if (fullName == null)
263                                         return null;
264                                 return fullName + ", " + elementType.Assembly.FullName;
265                         }
266                 }
267
268
269                 public override string FullName {
270                         get {
271                                 return FormatName (elementType.FullName);
272                         }
273                 }
274
275                 public override string Name {
276                         get {
277                                 return FormatName (elementType.Name);
278                         }
279                 }
280
281                 public override Guid GUID {
282                         get { throw new NotSupportedException (); }
283                 }
284
285                 public override Module Module {
286                         get { return elementType.Module; }
287                 }
288         
289                 public override string Namespace {
290                         get { return elementType.Namespace; }
291                 }
292
293                 public override RuntimeTypeHandle TypeHandle {
294                         get { throw new NotSupportedException (); }
295                 }
296
297                 public override Type UnderlyingSystemType {
298                         get {
299                                 create_unmanaged_type (this);
300                                 return this;
301                         }
302                 }
303
304                 //MemberInfo
305                 public override bool IsDefined (Type attributeType, bool inherit)
306                 {
307                         throw new NotSupportedException ();
308                 }
309
310                 public override object [] GetCustomAttributes (bool inherit)
311                 {
312                         throw new NotSupportedException ();
313                 }
314
315                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
316                 {
317                         throw new NotSupportedException ();
318                 }
319
320                 internal override bool IsUserType {
321                         get {
322                                 return elementType.IsUserType;
323                         }
324                 }
325         }
326
327         [StructLayout (LayoutKind.Sequential)]
328         internal class ArrayType : DerivedType
329         {
330                 int rank;
331
332                 internal ArrayType (Type elementType, int rank) : base (elementType)
333                 {
334                         this.rank = rank;
335                 }
336
337                 internal int GetEffectiveRank ()
338                 {
339                         return rank;
340                 }
341
342                 internal override Type InternalResolve ()
343                 {
344                         Type et = elementType.InternalResolve (); 
345                         if (rank == 0)
346                                 return et.MakeArrayType ();                     
347                         return et.MakeArrayType (rank);
348                 }
349
350                 protected override bool IsArrayImpl ()
351                 {
352                         return true;
353                 }
354
355                 public override int GetArrayRank ()
356                 {
357                         return (rank == 0) ? 1 : rank;
358                 }
359
360                 public override Type BaseType {
361                         get { return typeof (System.Array); }
362                 }
363
364                 protected override TypeAttributes GetAttributeFlagsImpl ()
365                 {
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         [StructLayout (LayoutKind.Sequential)]
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         [StructLayout (LayoutKind.Sequential)]
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 }