2004-09-04 Sebastien Pouliot <sebastien@ximian.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         public class CustomAttributeBuilder {
42                 ConstructorInfo ctor;
43                 byte[] data;
44
45                 internal ConstructorInfo Ctor {
46                         get {return ctor;}
47                 }
48
49                 internal byte[] Data {
50                         get {return data;}
51                 }
52                 
53                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
54                 static extern byte[] GetBlob(Assembly asmb, ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues);
55                 
56                 internal CustomAttributeBuilder( ConstructorInfo con, byte[] cdata) {
57                         ctor = con;
58                         data = (byte[])cdata.Clone ();
59                         /* should we check that the user supplied data is correct? */
60                 }
61                 
62                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs)
63                 {
64                         Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
65                                         new FieldInfo [0], new object [0]);
66                 }
67                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
68                                 FieldInfo[] namedFields, object[] fieldValues) 
69                 {
70                         Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
71                                         namedFields, fieldValues);
72                 }
73                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
74                                 PropertyInfo[] namedProperties, object[] propertyValues)
75                 {
76                         Initialize (con, constructorArgs, namedProperties, propertyValues, new FieldInfo [0],
77                                         new object [0]);
78                 }
79                 public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
80                                 PropertyInfo[] namedProperties, object[] propertyValues,
81                                 FieldInfo[] namedFields, object[] fieldValues)
82                 {
83                         Initialize (con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
84                 }
85
86                 private void Initialize (ConstructorInfo con, object [] constructorArgs,
87                                 PropertyInfo [] namedProperties, object [] propertyValues,
88                                 FieldInfo [] namedFields, object [] fieldValues)
89                 {
90                         ctor = con;
91                         if (con == null)
92                                 throw new ArgumentNullException ("con");
93                         if (constructorArgs == null)
94                                 throw new ArgumentNullException ("constructorArgs");
95                         if (namedProperties == null)
96                                 throw new ArgumentNullException ("namedProperties");
97                         if (propertyValues == null)
98                                 throw new ArgumentNullException ("propertyValues");
99                         if (namedFields == null)
100                                 throw new ArgumentNullException ("namedFields");
101                         if (fieldValues == null)
102                                 throw new ArgumentNullException ("fieldValues");
103                         if (con.GetParameterCount () != constructorArgs.Length)
104                                 throw new ArgumentException ("Parameter count does not match " +
105                                                 "passed in argument value count.");
106                         if (namedProperties.Length != propertyValues.Length)
107                                 throw new ArgumentException ("Array lengths must be the same.",
108                                                 "namedProperties, propertyValues");
109                         if (namedFields.Length != fieldValues.Length)
110                                 throw new ArgumentException ("Array lengths must be the same.",
111                                                 "namedFields, fieldValues");
112                         if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static ||
113                                         (con.Attributes & MethodAttributes.Private) == MethodAttributes.Private)
114                                 throw new ArgumentException ("Cannot have private or static constructor.");
115
116                         Type atype = ctor.DeclaringType;
117                         int i;
118                         i = 0;
119                         foreach (FieldInfo fi in namedFields) {
120                                 Type t = fi.DeclaringType;
121                                 if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
122                                         throw new ArgumentException ("Field '" + fi.Name + "' does not belong to the same class as the constructor");
123                                 // FIXME: Check enums and TypeBuilders as well
124                                 if (fieldValues [i] != null)
125                                         // IsEnum does not seem to work on TypeBuilders
126                                         if (!(fi.FieldType is TypeBuilder) && !fi.FieldType.IsEnum && !fi.FieldType.IsAssignableFrom (fieldValues [i].GetType ())) {
127                                                 //
128                                                 // mcs allways uses object[] for array types and
129                                                 // MS.NET allows this
130                                                 //
131                                                 if (!fi.FieldType.IsArray)
132                                                         throw new ArgumentException ("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType);
133                                                 }
134                                 i ++;
135                         }
136
137                         i = 0;
138                         foreach (PropertyInfo pi in namedProperties) {
139                                 if (!pi.CanWrite)
140                                         throw new ArgumentException ("Property '" + pi.Name + "' does not have a setter.");
141                                 Type t = pi.DeclaringType;
142                                 if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
143                                         throw new ArgumentException ("Property '" + pi.Name + "' does not belong to the same class as the constructor");
144                                 if (propertyValues [i] != null) {
145                                         if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsAssignableFrom (propertyValues [i].GetType ()))
146                                                 if (!pi.PropertyType.IsArray)
147                                                         throw new ArgumentException ("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues [i]);
148                                 }
149                                 i ++;
150                         }
151
152                         i = 0;
153                         foreach (ParameterInfo pi in con.GetParameters ()) {
154                                 if (pi != null) {
155                                         Type paramType = pi.ParameterType;
156                                         if (constructorArgs [i] != null)
157                                                 if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsAssignableFrom (constructorArgs [i].GetType ()))
158                                                         if (!paramType.IsArray)
159                                                                 throw new ArgumentException ("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs [i]);
160                                 }
161                                 i ++;
162                         }
163                                 
164                         data = GetBlob (atype.Assembly, con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
165                 }
166
167                 /* helper methods */
168                 internal static int decode_len (byte[] data, int pos, out int rpos) {
169                         int len = 0;
170                         if ((data [pos] & 0x80) == 0) {
171                                 len = (int)(data [pos++] & 0x7f);
172                         } else if ((data [pos] & 0x40) == 0) {
173                                 len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
174                                 pos += 2;
175                         } else {
176                                 len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
177                                 pos += 4;
178                         }
179                         rpos = pos;
180                         return len;
181                 }
182
183                 internal static string string_from_bytes (byte[] data, int pos, int len) 
184                 {
185                         return System.Text.Encoding.UTF8.GetString(data, pos, len);
186                 }
187
188                 internal string string_arg ()
189                 {
190                         int pos = 2;
191                         int len = decode_len (data, pos, out pos);
192                         return string_from_bytes (data, pos, len);
193                 }                       
194
195                 internal static UnmanagedMarshal get_umarshal (CustomAttributeBuilder customBuilder, bool is_field) {
196                         byte[] data = customBuilder.Data;
197                         UnmanagedType subtype = UnmanagedType.I4;
198                         int sizeConst = 0;
199                         int value;
200                         int utype; /* the (stupid) ctor takes a short or an enum ... */
201                         utype = (int)data [2];
202                         utype |= ((int)data [3]) << 8;
203
204                         string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
205                         int pos = 6;
206                         if (first_type_name == "System.Int16")
207                                 pos = 4;
208                         int nnamed = (int)data [pos++];
209                         nnamed |= ((int)data [pos++]) << 8;
210                         
211                         for (int i = 0; i < nnamed; ++i) {
212                                 int paramType; // What is this ?
213                                 paramType = (int)data [pos++];
214                                 paramType |= ((int)data [pos++]) << 8;
215                                 int len = decode_len (data, pos, out pos);
216                                 string named_name = string_from_bytes (data, pos, len);
217                                 pos += len;
218
219                                 switch (named_name) {
220                                 case "ArraySubType":
221                                         value = (int)data [pos++];
222                                         value |= ((int)data [pos++]) << 8;
223                                         value |= ((int)data [pos++]) << 16;
224                                         value |= ((int)data [pos++]) << 24;
225                                         subtype = (UnmanagedType)value;
226                                         break;
227                                 case "SizeConst":
228                                         value = (int)data [pos++];
229                                         value |= ((int)data [pos++]) << 8;
230                                         value |= ((int)data [pos++]) << 16;
231                                         value |= ((int)data [pos++]) << 24;
232                                         sizeConst = value;
233                                         break;
234                                 default:
235                                         break;
236                                 }
237                         }
238
239                         switch ((UnmanagedType)utype) {
240                         case UnmanagedType.LPArray:
241                                 return UnmanagedMarshal.DefineLPArray (subtype);
242                         case UnmanagedType.SafeArray:
243                                 return UnmanagedMarshal.DefineSafeArray (subtype);
244                         case UnmanagedType.ByValArray:
245                                 return UnmanagedMarshal.DefineByValArray (sizeConst);
246                         case UnmanagedType.ByValTStr:
247                                 return UnmanagedMarshal.DefineByValTStr (sizeConst);
248                         default:
249                                 return UnmanagedMarshal.DefineUnmanagedMarshal ((UnmanagedType)utype);
250                         }
251                 }
252
253         }
254 }