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