2010-06-22 Atsushi Enomoto <atsushi@ximian.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         [ComVisible (true)]
43         [ComDefaultInterface (typeof (_SignatureHelper))]
44         [ClassInterface (ClassInterfaceType.None)]
45         public sealed class SignatureHelper : _SignatureHelper {
46                 internal enum SignatureHelperType {
47                         HELPER_FIELD,
48                         HELPER_LOCAL,
49                         HELPER_METHOD,
50                         HELPER_PROPERTY
51                 }
52
53                 private ModuleBuilder module; // can be null in 2.0
54                 private Type[] arguments;
55                 private SignatureHelperType type;
56                 private Type returnType;
57                 private CallingConventions callConv;
58                 private CallingConvention unmanagedCallConv;
59 #pragma warning disable 649
60                 private Type[][] modreqs;
61                 private Type[][] modopts;
62 #pragma warning restore 649
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                 public static SignatureHelper GetLocalVarSigHelper ()
87                 {
88                         return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
89                 }
90
91                 public static SignatureHelper GetMethodSigHelper (CallingConventions callingConvention, Type returnType)
92                 {
93                         return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
94                 }
95
96                 public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention, Type returnType)
97                 {
98                         return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
99                 }
100
101                 public static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, Type returnType)
102                 {
103                         return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
104                 }
105
106                 public static SignatureHelper GetMethodSigHelper (Module mod, CallingConvention unmanagedCallConv, Type returnType)
107                 {
108                         return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallConv, returnType, null);
109                 }
110
111                 public static SignatureHelper GetMethodSigHelper (Module mod, Type returnType, Type[] parameterTypes)
112                 {
113                         return GetMethodSigHelper (mod, CallingConventions.Standard, (CallingConvention)0, returnType, parameterTypes);
114                 }
115
116                 [MonoTODO("Not implemented")]
117                 public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType, Type[] parameterTypes)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 [MonoTODO("Not implemented")]
123                 public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
124                                                                     Type [] requiredReturnTypeCustomModifiers,
125                                                                     Type [] optionalReturnTypeCustomModifiers,
126                                                                     Type [] parameterTypes,
127                                                                     Type [] [] requiredParameterTypeCustomModifiers,
128                                                                     Type [] [] optionalParameterTypeCustomModifiers)
129                 {
130                         throw new NotImplementedException ();
131                 }
132
133 #if NET_4_0
134                 [MonoTODO("Not implemented")]
135                 public static SignatureHelper GetPropertySigHelper (Module mod,
136                                                                         CallingConventions callingConvention,
137                                                                         Type returnType,
138                                                                     Type [] requiredReturnTypeCustomModifiers,
139                                                                     Type [] optionalReturnTypeCustomModifiers,
140                                                                     Type [] parameterTypes,
141                                                                     Type [] [] requiredParameterTypeCustomModifiers,
142                                                                     Type [] [] optionalParameterTypeCustomModifiers)
143                 {
144                         throw new NotImplementedException ();
145                 }
146 #endif
147
148                 //
149                 // Grows the given array, and returns the index where the element
150                 // was added
151                 //
152                 static int AppendArray (ref Type [] array, Type t)
153                 {
154                         if (array != null) {
155                                 Type[] new_a = new Type [array.Length + 1];
156                                 System.Array.Copy (array, new_a, array.Length);
157                                 new_a [array.Length] = t;
158                                 array = new_a;
159                                 return array.Length-1;
160                         } else {
161                                 array = new Type [1];
162                                 array [0] = t;
163                                 return 0;
164                         }
165                 }
166
167                 //
168                 // Appends the given type array @t into the @array passed at
169                 // position @pos.   If there is no array, it gets created
170                 //
171                 // This allows adding data to a null array at position 5 for
172                 // example, creating 4 empty slots before the slot where @t
173                 // is stored.
174                 //
175                 //
176                 static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
177                 {
178                         int top = Math.Max (pos, array == null ? 0 : array.Length);
179                         Type[][] new_a = new Type [top+1][];
180                         if (array != null)
181                                 System.Array.Copy (array, new_a, top);
182                         new_a [pos] = t;
183                         array = new_a;
184                 }
185                 
186                 static void ValidateParameterModifiers (string name, Type [] parameter_modifiers)
187                 {
188                         foreach (Type modifier in parameter_modifiers){
189                                 if (modifier == null)
190                                         throw new ArgumentNullException (name);
191                                 if (modifier.IsArray)
192                                         throw new ArgumentException (Locale.GetText ("Array type not permitted"), name);
193                                 if (modifier.ContainsGenericParameters)
194                                         throw new ArgumentException (Locale.GetText ("Open Generic Type not permitted"), name);
195                         }
196                 }
197
198                 static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
199                 {
200                         if (custom_modifiers == null)
201                                 return;
202
203                         if (custom_modifiers.Length != n)
204                                 throw new ArgumentException (
205                                      Locale.GetText (
206                                         String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
207                         
208                         foreach (Type [] parameter_modifiers in custom_modifiers){
209                                 if (parameter_modifiers == null)
210                                         continue;
211
212                                 ValidateParameterModifiers (name, parameter_modifiers);
213                         }
214                 }
215
216                 static Exception MissingFeature ()
217                 {
218                         throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
219                 }
220
221                 [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
222                 public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
223                 {
224                         if (arguments == null)
225                                 throw new ArgumentNullException ("arguments");
226
227                         // For now
228                         if (requiredCustomModifiers != null || optionalCustomModifiers != null){
229                                 throw MissingFeature();
230                         }
231                         
232                         ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
233                         ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
234
235                         for (int i = 0; i < arguments.Length; i++){
236                                 AddArgument (arguments [i],
237                                              requiredCustomModifiers != null ? requiredCustomModifiers [i] : null,
238                                              optionalCustomModifiers != null ? optionalCustomModifiers [i] : null);
239                         }
240                 }
241
242                 [MonoTODO ("pinned is ignored")]
243                 public void AddArgument (Type argument, bool pinned)
244                 {
245                         AddArgument (argument);
246                 }
247
248                 public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
249                 {
250                         if (argument == null)
251                                 throw new ArgumentNullException ("argument");
252
253                         if (requiredCustomModifiers != null)
254                                 ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
255                         if (optionalCustomModifiers != null)
256                                 ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
257
258                         int p = AppendArray (ref arguments, argument);
259                         if (requiredCustomModifiers != null)
260                                 AppendArrayAt (ref modreqs, requiredCustomModifiers, p);
261                         if (optionalCustomModifiers != null)
262                                 AppendArrayAt (ref modopts, optionalCustomModifiers, p);
263                 }
264
265                 public void AddArgument (Type clsArgument)
266                 {
267                         if (clsArgument == null)
268                                 throw new ArgumentNullException ("clsArgument");
269
270                         AppendArray (ref arguments, clsArgument);
271                 }
272
273                 [MonoTODO("Not implemented")]
274                 public void AddSentinel ()
275                 {
276                         throw new NotImplementedException ();
277                 }
278
279                 static bool CompareOK (Type [][] one, Type [][] two)
280                 {
281                         if (one == null){
282                                 if (two == null)
283                                         return true;
284                                 return false;
285                         } else if (two == null)
286                                 return false;
287
288                         if (one.Length != two.Length)
289                                 return false;
290
291                         for (int i = 0; i < one.Length; i++){
292                                 Type [] tone = one [i];
293                                 Type [] ttwo = two [i];
294
295                                 if (tone == null){
296                                         if (ttwo == null)
297                                                 continue;
298                                 } else if (ttwo == null)
299                                         return false;
300
301                                 if (tone.Length != ttwo.Length)
302                                         return false;
303
304                                 for (int j = 0; j < tone.Length; j++){
305                                         Type uone = tone [j];
306                                         Type utwo = ttwo [j];
307                                         
308                                         if (uone == null){
309                                                 if (utwo == null)
310                                                         continue;
311                                                 return false;
312                                         } else if (utwo == null)
313                                                 return false;
314
315                                         if (!uone.Equals (utwo))
316                                                 return false;
317                                 }
318                         }
319                         return true;
320                 }
321                 
322                 public override bool Equals (object obj)
323                 {
324                         SignatureHelper other = obj as SignatureHelper;
325                         if (other == null)
326                                 return false;
327
328                         if (other.module != module ||
329                             other.returnType != returnType ||
330                             other.callConv != callConv ||
331                             other.unmanagedCallConv != unmanagedCallConv)
332                                 return false;
333
334                         if (arguments != null){
335                                 if (other.arguments == null)
336                                         return false;
337                                 if (arguments.Length != other.arguments.Length)
338                                         return false;
339
340                                 for (int i = 0; i < arguments.Length; i++)
341                                         if (!other.arguments [i].Equals (arguments [i]))
342                                                 return false;
343                         } else if (other.arguments != null)
344                                 return false;
345
346                         return CompareOK (other.modreqs, modreqs) && CompareOK (other.modopts, modopts);
347                 }
348
349                 public override int GetHashCode ()
350                 {
351                         // Lame, but easy, and will work, and chances are
352                         // you will only need a few of these.
353                         return 0;
354                 }
355
356                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
357                 internal extern byte[] get_signature_local ();
358
359                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
360                 internal extern byte[] get_signature_field ();
361
362                 public byte[] GetSignature ()
363                 {
364                         switch (type) {
365                         case SignatureHelperType.HELPER_LOCAL:
366                                 return get_signature_local ();
367                         case SignatureHelperType.HELPER_FIELD:
368                                 return get_signature_field ();
369                         default:
370                                 throw new NotImplementedException ();
371                         }
372                 }
373
374                 public override string ToString() {
375                         return "SignatureHelper";
376                 }
377
378                 internal static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, CallingConvention unmanagedCallingConvention, Type returnType,
379                                                                                                                    Type [] parameters)
380                 {
381                         if (mod != null && !(mod is ModuleBuilder))
382                                 throw new ArgumentException ("ModuleBuilder is expected");
383
384                         if (returnType == null)
385                                 returnType = typeof (void);
386
387                         if (returnType.IsUserType)
388                                 throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
389                         if (parameters != null) {
390                                 for (int i = 0; i < parameters.Length; ++i)
391                                         if (parameters [i].IsUserType)
392                                                 throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
393
394                         }
395
396                         SignatureHelper helper = 
397                                 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
398                         helper.returnType = returnType;
399                         helper.callConv = callingConvention;
400                         helper.unmanagedCallConv = unmanagedCallingConvention;
401
402                         if (parameters != null) {
403                                 helper.arguments = new Type [parameters.Length];
404                                 for (int i = 0; i < parameters.Length; ++i)
405                                         helper.arguments [i] = parameters [i];
406                         }
407
408                         return helper;
409                 }
410
411                 void _SignatureHelper.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
412                 {
413                         throw new NotImplementedException ();
414                 }
415
416                 void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
417                 {
418                         throw new NotImplementedException ();
419                 }
420
421                 void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
422                 {
423                         throw new NotImplementedException ();
424                 }
425
426                 void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
427                 {
428                         throw new NotImplementedException ();
429                 }
430         }
431 }