2007-11-17 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / SignatureHelper.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/SignatureHelper.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.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40
41 namespace System.Reflection.Emit {
42 #if NET_2_0
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_SignatureHelper))]
45 #endif
46         [ClassInterface (ClassInterfaceType.None)]
47         public sealed class SignatureHelper : _SignatureHelper {
48                 internal enum SignatureHelperType {
49                         HELPER_FIELD,
50                         HELPER_LOCAL,
51                         HELPER_METHOD,
52                         HELPER_PROPERTY
53                 }
54
55                 private ModuleBuilder module; // can be null in 2.0
56                 private Type[] arguments;
57                 private SignatureHelperType type;
58                 private Type returnType;
59                 private CallingConventions callConv;
60                 private CallingConvention unmanagedCallConv;
61                 private Type[][] modreqs;
62                 private Type[][] modopts;
63
64                 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
65                 {
66                         this.type = type;
67                         this.module = module;
68                 }
69
70                 public static SignatureHelper GetFieldSigHelper (Module mod)
71                 {
72                         if (mod != null && !(mod is ModuleBuilder))
73                                 throw new ArgumentException ("ModuleBuilder is expected");
74
75                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
76                 }
77
78                 public static SignatureHelper GetLocalVarSigHelper (Module mod)
79                 {
80                         if (mod != null && !(mod is ModuleBuilder))
81                                 throw new ArgumentException ("ModuleBuilder is expected");
82
83                         return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
84                 }
85
86 #if NET_2_0
87                 public static SignatureHelper GetLocalVarSigHelper ()
88                 {
89                         return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
90                 }
91
92                 public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType)
93                 {
94                         return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
95                 }
96
97                 public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention,
98                                                                   Type returnType)
99                 {
100                         return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
101                 }
102 #endif
103
104                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
105                 {
106                         return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
107                 }
108
109                 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
110                 {
111                         return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
112                 }
113
114                 public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
115                 {
116                         return GetMethodSigHelper (mod, CallingConventions.Standard, 
117                                                                            (CallingConvention)0, returnType, 
118                                                                            parameterTypes);
119                 }
120                 [MonoTODO("Not implemented")]
121                 public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
122                 {
123                         throw new NotImplementedException ();
124                 }
125
126                 //
127                 // Grows the given array, and returns the index where the element
128                 // was added
129                 //
130                 static int AppendArray (ref Type [] array, Type t)
131                 {
132                         if (array != null) {
133                                 Type[] new_a = new Type [array.Length + 1];
134                                 System.Array.Copy (array, new_a, array.Length);
135                                 new_a [array.Length] = t;
136                                 array = new_a;
137                                 return array.Length;
138                         } else {
139                                 array = new Type [1];
140                                 array [0] = t;
141                                 return 0;
142                         }
143                 }
144
145 #if NET_2_0
146                 //
147                 // Appends the given type array @t into the @array passed at
148                 // position @pos.   If there is no array, it gets created
149                 //
150                 // This allows adding data to a null array at position 5 for
151                 // example, creating 4 empty slots before the slot where @t
152                 // is stored.
153                 //
154                 //
155                 static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
156                 {
157                         int top = Math.Max (pos, array == null ? 0 : array.Length);
158                         Type[][] new_a = new Type [top+1][];
159                         if (array != null)
160                                 System.Array.Copy (array, new_a, top);
161                         new_a [pos] = t;
162                         array = new_a;
163                 }
164                 
165                 static void ValidateParameterModifiers(string name, Type [] parameter_modifiers)
166                 {
167                         foreach (Type modifier in parameter_modifiers){
168                                 if (modifier == null)
169                                         throw new ArgumentNullException (name);
170                                 if (modifier.IsArray)
171                                         throw new ArgumentException (Locale.GetText ("Array type not permitted"), name);
172                                 if (modifier.ContainsGenericParameters)
173                                         throw new ArgumentException (Locale.GetText ("Open Generic Type not permitted"), name);
174                         }
175                 }
176
177                 static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
178                 {
179                         if (custom_modifiers == null)
180                                 return;
181
182                         if (custom_modifiers.Length != n)
183                                 throw new ArgumentException (
184                                      Locale.GetText (
185                                         String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
186                         
187                         for (int i = 0; i < n; i++)
188                         foreach (Type [] parameter_modifiers in custom_modifiers){
189                                 if (parameter_modifiers == null)
190                                         continue;
191
192                                 ValidateParameterModifiers (name, parameter_modifiers);
193                         }
194                 }
195
196                 static Exception MissingFeature ()
197                 {
198                         throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
199                 }
200
201                 [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
202                 public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
203                 {
204                         if (arguments == null)
205                                 throw new ArgumentNullException ("arguments");
206
207                         // For now
208                         if (requiredCustomModifiers != null || optionalCustomModifiers != null){
209                                 throw MissingFeature();
210                         }
211                         
212                         ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
213                         ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
214
215                         for (int i = 0; i < arguments.Length; i++){
216                                 AddArgument (arguments [i],
217                                              requiredCustomModifiers != null ? requiredCustomModifiers [i] : null,
218                                              optionalCustomModifiers != null ? optionalCustomModifiers [i] : null);
219                         }
220                 }
221
222                 [MonoTODO ("pinned is ignored")]
223                 public void AddArgument (Type argument, bool pinned)
224                 {
225                         AddArgument (argument);
226                 }
227
228                 [MonoTODO ("not implemented")]
229                 public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
230                 {
231                         if (argument == null)
232                                 throw new ArgumentNullException ("argument");
233
234                         ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
235                         ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
236
237                         int p = AppendArray (ref arguments, argument);
238                         if (requiredCustomModifiers != null)
239                                 AppendArrayAt (ref modreqs, requiredCustomModifiers, p);
240                         if (optionalCustomModifiers != null)
241                                 AppendArrayAt (ref modopts, optionalCustomModifiers, p);
242                 }
243
244                 [MonoTODO("Not implemented")]
245                 public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
246                                                                     Type [] requiredReturnTypeCustomModifiers,
247                                                                     Type [] optionalReturnTypeCustomModifiers,
248                                                                     Type [] parameterTypes,
249                                                                     Type [] [] requiredParameterTypeCustomModifiers,
250                                                                     Type [] [] optionalParameterTypeCustomModifiers)
251                 {
252                         throw new NotImplementedException ();
253                 }
254 #endif
255
256                 public void AddArgument (Type clsArgument)
257                 {
258                         if (clsArgument == null)
259                                 throw new ArgumentNullException ("argument");
260
261                         AppendArray (ref arguments, clsArgument);
262                 }
263
264                 [MonoTODO("Not implemented")]
265                 public void AddSentinel ()
266                 {
267                         throw new NotImplementedException ();
268                 }
269
270                 [MonoTODO("Not implemented")]
271                 public override bool Equals (object obj)
272                 {
273                         throw new NotImplementedException ();
274                 }
275
276                 [MonoTODO("Not implemented")]
277                 public override int GetHashCode ()
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283                 internal extern byte[] get_signature_local ();
284
285                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
286                 internal extern byte[] get_signature_field ();
287
288                 public byte[] GetSignature ()
289                 {
290                         switch (type) {
291                         case SignatureHelperType.HELPER_LOCAL:
292                                 return get_signature_local ();
293                         case SignatureHelperType.HELPER_FIELD:
294                                 return get_signature_field ();
295                         default:
296                                 throw new NotImplementedException ();
297                         }
298                 }
299
300                 public override string ToString() {
301                         return "SignatureHelper";
302                 }
303
304                 internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
305                                                                                                                    Type [] parameters)
306                 {
307                         if (mod != null && !(mod is ModuleBuilder))
308                                 throw new ArgumentException ("ModuleBuilder is expected");
309
310                         SignatureHelper helper = 
311                                 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
312                         helper.returnType = returnType;
313                         helper.callConv = callConv;
314                         helper.unmanagedCallConv = unmanagedCallConv;
315
316                         if (parameters != null) {
317                                 helper.arguments = new Type [parameters.Length];
318                                 for (int i = 0; i < parameters.Length; ++i)
319                                         helper.arguments [i] = parameters [i];
320                         }
321
322                         return helper;
323                 }
324
325                 void _SignatureHelper.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
326                 {
327                         throw new NotImplementedException ();
328                 }
329
330                 void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
331                 {
332                         throw new NotImplementedException ();
333                 }
334
335                 void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
336                 {
337                         throw new NotImplementedException ();
338                 }
339
340                 void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
341                 {
342                         throw new NotImplementedException ();
343                 }
344         }
345 }
346