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