Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[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                 [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
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                         TypeBuilder.ResolveUserTypes (arguments);
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