2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / CustomAttributeBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/CustomAttributeBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001 Ximian, Inc.  http://www.ximian.com
32 //
33
34 using System;
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39
40 namespace System.Reflection.Emit {
41         [ComVisible (true)]
42         [ComDefaultInterface (typeof (_CustomAttributeBuilder))]
43         [ClassInterface (ClassInterfaceType.None)]
44         public class CustomAttributeBuilder : _CustomAttributeBuilder {
45                 ConstructorInfo ctor;
46                 byte[] data;
47
48                 internal ConstructorInfo Ctor {
49                         get {return ctor;}
50                 }
51
52                 internal byte[] Data {
53                         get {return data;}
54                 }
55                 
56                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57                 static extern byte[] GetBlob(Assembly asmb, ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues);
58                 
59                 internal CustomAttributeBuilder( ConstructorInfo con, byte[] cdata) {
60                         ctor = con;
61                         data = (byte[])cdata.Clone ();
62                         /* should we check that the user supplied data is correct? */
63                 }
64                 
65                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs)
66                 {
67                         Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
68                                         new FieldInfo [0], new object [0]);
69                 }
70                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
71                                 FieldInfo[] namedFields, object[] fieldValues) 
72                 {
73                         Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
74                                         namedFields, fieldValues);
75                 }
76                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
77                                 PropertyInfo[] namedProperties, object[] propertyValues)
78                 {
79                         Initialize (con, constructorArgs, namedProperties, propertyValues, new FieldInfo [0],
80                                         new object [0]);
81                 }
82                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
83                                 PropertyInfo[] namedProperties, object[] propertyValues,
84                                 FieldInfo[] namedFields, object[] fieldValues)
85                 {
86                         Initialize (con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
87                 }
88
89                 private bool IsValidType (Type t)
90                 {
91                         /* FIXME: Add more checks */
92                         if (t.IsArray && t.GetArrayRank () > 1)
93                                 return false;
94                         return true;
95                 }
96
97                 private void Initialize (ConstructorInfo con, object [] constructorArgs,
98                                 PropertyInfo [] namedProperties, object [] propertyValues,
99                                 FieldInfo [] namedFields, object [] fieldValues)
100                 {
101                         ctor = con;
102                         if (con == null)
103                                 throw new ArgumentNullException ("con");
104                         if (constructorArgs == null)
105                                 throw new ArgumentNullException ("constructorArgs");
106                         if (namedProperties == null)
107                                 throw new ArgumentNullException ("namedProperties");
108                         if (propertyValues == null)
109                                 throw new ArgumentNullException ("propertyValues");
110                         if (namedFields == null)
111                                 throw new ArgumentNullException ("namedFields");
112                         if (fieldValues == null)
113                                 throw new ArgumentNullException ("fieldValues");
114                         if (con.GetParameterCount () != constructorArgs.Length)
115                                 throw new ArgumentException ("Parameter count does not match " +
116                                                 "passed in argument value count.");
117                         if (namedProperties.Length != propertyValues.Length)
118                                 throw new ArgumentException ("Array lengths must be the same.",
119                                                 "namedProperties, propertyValues");
120                         if (namedFields.Length != fieldValues.Length)
121                                 throw new ArgumentException ("Array lengths must be the same.",
122                                                 "namedFields, fieldValues");
123                         if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static ||
124                                         (con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
125                                 throw new ArgumentException ("Cannot have private or static constructor.");
126
127                         Type atype = ctor.DeclaringType;
128                         int i;
129                         i = 0;
130                         foreach (FieldInfo fi in namedFields) {
131                                 Type t = fi.DeclaringType;
132                                 if (!IsValidType (t))
133                                         throw new ArgumentException ("Field '" + fi.Name + "' does not have a valid type.");
134                                 if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
135                                         throw new ArgumentException ("Field '" + fi.Name + "' does not belong to the same class as the constructor");
136                                 // FIXME: Check enums and TypeBuilders as well
137                                 if (fieldValues [i] != null)
138                                         // IsEnum does not seem to work on TypeBuilders
139                                         if (!(fi.FieldType is TypeBuilder) && !fi.FieldType.IsEnum && !fi.FieldType.IsInstanceOfType (fieldValues [i])) {
140                                                 //
141                                                 // mcs allways uses object[] for array types and
142                                                 // MS.NET allows this
143                                                 //
144                                                 if (!fi.FieldType.IsArray)
145                                                         throw new ArgumentException ("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType);
146                                                 }
147                                 i ++;
148                         }
149
150                         i = 0;
151                         foreach (PropertyInfo pi in namedProperties) {
152                                 if (!pi.CanWrite)
153                                         throw new ArgumentException ("Property '" + pi.Name + "' does not have a setter.");
154                                 Type t = pi.DeclaringType;
155                                 if (!IsValidType (t))
156                                         throw new ArgumentException ("Property '" + pi.Name + "' does not have a valid type.");
157                                 if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
158                                         throw new ArgumentException ("Property '" + pi.Name + "' does not belong to the same class as the constructor");
159                                 if (propertyValues [i] != null) {
160                                         if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsInstanceOfType (propertyValues [i]))
161                                                 if (!pi.PropertyType.IsArray)
162                                                         throw new ArgumentException ("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues [i]);
163                                 }
164                                 i ++;
165                         }
166
167                         i = 0;
168                         foreach (ParameterInfo pi in GetParameters (con)) {
169                                 if (pi != null) {
170                                         Type paramType = pi.ParameterType;
171                                         if (!IsValidType (paramType))
172                                                 throw new ArgumentException ("Argument " + i + " does not have a valid type.");
173                                         if (constructorArgs [i] != null)
174                                                 if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsInstanceOfType (constructorArgs [i]))
175                                                         if (!paramType.IsArray)
176                                                                 throw new ArgumentException ("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs [i]);
177                                 }
178                                 i ++;
179                         }
180                                 
181                         data = GetBlob (atype.Assembly, con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
182                 }
183
184                 /* helper methods */
185                 internal static int decode_len (byte[] data, int pos, out int rpos) {
186                         int len = 0;
187                         if ((data [pos] & 0x80) == 0) {
188                                 len = (int)(data [pos++] & 0x7f);
189                         } else if ((data [pos] & 0x40) == 0) {
190                                 len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
191                                 pos += 2;
192                         } else {
193                                 len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
194                                 pos += 4;
195                         }
196                         rpos = pos;
197                         return len;
198                 }
199
200                 internal static string string_from_bytes (byte[] data, int pos, int len) 
201                 {
202                         return System.Text.Encoding.UTF8.GetString(data, pos, len);
203                 }
204
205                 internal string string_arg ()
206                 {
207                         int pos = 2;
208                         int len = decode_len (data, pos, out pos);
209                         return string_from_bytes (data, pos, len);
210                 }                       
211
212                 internal static UnmanagedMarshal get_umarshal (CustomAttributeBuilder customBuilder, bool is_field) {
213                         byte[] data = customBuilder.Data;
214                         UnmanagedType subtype = (UnmanagedType)0x50; /* NATIVE_MAX */
215                         int sizeConst = -1;
216                         int sizeParamIndex = -1;
217                         bool hasSize = false;
218                         int value;
219                         int utype; /* the (stupid) ctor takes a short or an enum ... */
220                         string marshalTypeName = null;
221                         Type marshalTypeRef = null;
222                         string marshalCookie = String.Empty;
223                         utype = (int)data [2];
224                         utype |= ((int)data [3]) << 8;
225
226                         string first_type_name = GetParameters (customBuilder.Ctor) [0].ParameterType.FullName;
227                         int pos = 6;
228                         if (first_type_name == "System.Int16")
229                                 pos = 4;
230                         int nnamed = (int)data [pos++];
231                         nnamed |= ((int)data [pos++]) << 8;
232
233                         for (int i = 0; i < nnamed; ++i) {
234                                 int paramType; // What is this ?
235                                 
236                                 /* Skip field/property signature */
237                                 pos ++;
238                                 /* Read type */
239                                 paramType = ((int)data [pos++]);
240                                 if (paramType == 0x55) {
241                                         /* enums, the value is preceeded by the type */
242                                         int len2 = decode_len (data, pos, out pos);
243                                         string_from_bytes (data, pos, len2);
244                                         pos += len2;
245                                 }
246                                 int len = decode_len (data, pos, out pos);
247                                 string named_name = string_from_bytes (data, pos, len);
248                                 pos += len;
249
250                                 switch (named_name) {
251                                 case "ArraySubType":
252                                         value = (int)data [pos++];
253                                         value |= ((int)data [pos++]) << 8;
254                                         value |= ((int)data [pos++]) << 16;
255                                         value |= ((int)data [pos++]) << 24;
256                                         subtype = (UnmanagedType)value;
257                                         break;
258                                 case "SizeConst":
259                                         value = (int)data [pos++];
260                                         value |= ((int)data [pos++]) << 8;
261                                         value |= ((int)data [pos++]) << 16;
262                                         value |= ((int)data [pos++]) << 24;
263                                         sizeConst = value;
264                                         hasSize = true;
265                                         break;
266                                 case "SafeArraySubType":
267                                         value = (int)data[pos++];
268                                         value |= ((int)data[pos++]) << 8;
269                                         value |= ((int)data[pos++]) << 16;
270                                         value |= ((int)data[pos++]) << 24;
271                                         subtype = (UnmanagedType)value;
272                                         break;
273                                 case "IidParameterIndex":
274                                         pos += 4;
275                                         break;
276                                 case "SafeArrayUserDefinedSubType":
277                                         len = decode_len (data, pos, out pos);
278                                         string_from_bytes (data, pos, len);
279                                         pos += len;
280                                         break;
281                                 case "SizeParamIndex":
282                                         value = (int)data [pos++];
283                                         value |= ((int)data [pos++]) << 8;
284                                         sizeParamIndex = value;
285                                         hasSize = true;
286                                         break;
287                                 case "MarshalType":
288                                         len = decode_len (data, pos, out pos);
289                                         marshalTypeName = string_from_bytes (data, pos, len);
290                                         pos += len;
291                                         break;
292                                 case "MarshalTypeRef":
293                                         len = decode_len (data, pos, out pos);
294                                         marshalTypeName = string_from_bytes (data, pos, len);
295                                         marshalTypeRef = Type.GetType (marshalTypeName);
296                                         pos += len;
297                                         break;
298                                 case "MarshalCookie":
299                                         len = decode_len (data, pos, out pos);
300                                         marshalCookie = string_from_bytes (data, pos, len);
301                                         pos += len;
302                                         break;
303                                 default:
304                                         throw new Exception ("Unknown MarshalAsAttribute field: " + named_name);
305                                 }
306                         }
307
308                         switch ((UnmanagedType)utype) {
309                         case UnmanagedType.LPArray:
310                                 if (hasSize)
311                                         return UnmanagedMarshal.DefineLPArrayInternal (subtype, sizeConst, sizeParamIndex);
312                                 else
313                                         return UnmanagedMarshal.DefineLPArray (subtype);
314                         case UnmanagedType.SafeArray:
315                                 return UnmanagedMarshal.DefineSafeArray (subtype);
316                         case UnmanagedType.ByValArray:
317                                 if (!is_field)
318                                         throw new ArgumentException ("Specified unmanaged type is only valid on fields");
319                         
320                                 return UnmanagedMarshal.DefineByValArray (sizeConst);
321                         case UnmanagedType.ByValTStr:
322                                 return UnmanagedMarshal.DefineByValTStr (sizeConst);
323                         case UnmanagedType.CustomMarshaler:
324                                 return UnmanagedMarshal.DefineCustom (marshalTypeRef, marshalCookie, marshalTypeName, Guid.Empty);
325                         default:
326                                 return UnmanagedMarshal.DefineUnmanagedMarshal ((UnmanagedType)utype);
327                         }
328                 }
329
330                 static Type elementTypeToType (int elementType) {
331                         /* Partition II, section 23.1.16 */
332                         switch (elementType) {
333                         case 0x02:
334                                 return typeof (bool);
335                         case 0x03:
336                                 return typeof (char);
337                         case 0x04:
338                                 return typeof (sbyte);
339                         case 0x05:
340                                 return typeof (byte);
341                         case 0x06:
342                                 return typeof (short);
343                         case 0x07:
344                                 return typeof (ushort);
345                         case 0x08:
346                                 return typeof (int);
347                         case 0x09:
348                                 return typeof (uint);
349                         case 0x0a:
350                                 return typeof (long);
351                         case 0x0b:
352                                 return typeof (ulong);
353                         case 0x0c:
354                                 return typeof (float);
355                         case 0x0d:
356                                 return typeof (double);
357                         case 0x0e:
358                                 return typeof (string);
359                         default:
360                                 throw new Exception ("Unknown element type '" + elementType + "'");
361                         }
362                 }
363
364                 static object decode_cattr_value (Type t, byte[] data, int pos, out int rpos) {
365                         switch (Type.GetTypeCode (t)) {
366                         case TypeCode.String:
367                                 if (data [pos] == 0xff) {
368                                         rpos = pos + 1;
369                                         return null;
370                                 }
371                                 int len = decode_len (data, pos, out pos);
372                                 rpos = pos + len;
373                                 return string_from_bytes (data, pos, len);
374                         case TypeCode.Int32:
375                                 rpos = pos + 4;
376                                 return data [pos] + (data [pos + 1] << 8) + (data [pos + 2] << 16) + (data [pos + 3] << 24);
377                         case TypeCode.Boolean:
378                                 rpos = pos + 1;
379                                 return (data [pos] == 0) ? false : true;
380                         case TypeCode.Object:
381                                 int subtype = data [pos];
382                                 pos += 1;
383
384                                 if (subtype >= 0x02 && subtype <= 0x0e)
385                                         return decode_cattr_value (elementTypeToType (subtype), data, pos, out rpos);
386                                 else
387                                         throw new Exception ("Subtype '" + subtype + "' of type object not yet handled in decode_cattr_value");
388                         default:
389                                 throw new Exception ("FIXME: Type " + t + " not yet handled in decode_cattr_value.");
390                         }
391                 }
392
393                 internal struct CustomAttributeInfo {
394                         public ConstructorInfo ctor;
395                         public object[] ctorArgs;
396                         public string[] namedParamNames;
397                         public object[] namedParamValues;
398                 }
399
400                 internal static CustomAttributeInfo decode_cattr (CustomAttributeBuilder customBuilder) {
401                         byte[] data = customBuilder.Data;
402                         ConstructorInfo ctor = customBuilder.Ctor;
403                         int pos = 0;
404
405                         CustomAttributeInfo info = new CustomAttributeInfo ();
406
407                         // Prolog
408                         if (data.Length < 2)
409                                 throw new Exception ("Custom attr length is only '" + data.Length + "'");
410                         if ((data [0] != 0x1) || (data [1] != 0x00))
411                                 throw new Exception ("Prolog invalid");
412                         pos = 2;
413
414                         ParameterInfo [] pi = GetParameters (ctor);
415                         info.ctor = ctor;
416                         info.ctorArgs = new object [pi.Length];
417                         for (int i = 0; i < pi.Length; ++i)
418                                 info.ctorArgs [i] = decode_cattr_value (pi [i].ParameterType, data, pos, out pos);
419
420                         int num_named = data [pos] + (data [pos + 1] * 256);
421                         pos += 2;
422
423                         info.namedParamNames = new string [num_named];
424                         info.namedParamValues = new object [num_named];
425                         for (int i = 0; i < num_named; ++i) {
426                                 int named_type = data [pos++];
427                                 int data_type = data [pos++];
428                                 string enum_type_name = null;
429
430                                 if (data_type == 0x55) {
431                                         int len2 = decode_len (data, pos, out pos);
432                                         enum_type_name = string_from_bytes (data, pos, len2);
433                                         pos += len2;
434                                 }
435
436                                 int len = decode_len (data, pos, out pos);
437                                 string name = string_from_bytes (data, pos, len);
438                                 info.namedParamNames [i] = name;
439                                 pos += len;
440
441                                 if (named_type == 0x53) {
442                                         /* Field */
443                                         FieldInfo fi = ctor.DeclaringType.GetField (name, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance);
444                                         if (fi == null)
445                                                 throw new Exception ("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'");
446
447                                         object val = decode_cattr_value (fi.FieldType, data, pos, out pos);
448                                         if (enum_type_name != null) {
449                                                 Type enumType = Type.GetType (enum_type_name);
450                                                 val = Enum.ToObject (enumType, val);
451                                         }
452
453                                         info.namedParamValues [i] = val;
454                                 }
455                                 else
456                                         // FIXME:
457                                         throw new Exception ("Unknown named type: " + named_type);
458                         }
459
460                         return info;
461                 }
462
463                 void _CustomAttributeBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
464                 {
465                         throw new NotImplementedException ();
466                 }
467
468                 void _CustomAttributeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
469                 {
470                         throw new NotImplementedException ();
471                 }
472
473                 void _CustomAttributeBuilder.GetTypeInfoCount (out uint pcTInfo)
474                 {
475                         throw new NotImplementedException ();
476                 }
477
478                 void _CustomAttributeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
479                 {
480                         throw new NotImplementedException ();
481                 }
482
483                 static ParameterInfo [] GetParameters (ConstructorInfo ctor)
484                 {
485                         ConstructorBuilder cb = ctor as ConstructorBuilder;
486                         if (cb != null)
487                                 return cb.GetParametersInternal ();
488                         return ctor.GetParameters ();
489                 }
490         }
491 }