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