3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
26 // System.Reflection.Emit/SignatureHelper.cs
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001 Ximian, Inc. http://www.ximian.com
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
42 namespace System.Reflection.Emit {
44 [ComDefaultInterface (typeof (_SignatureHelper))]
45 [ClassInterface (ClassInterfaceType.None)]
46 [StructLayout (LayoutKind.Sequential)]
47 public sealed class SignatureHelper : _SignatureHelper {
48 internal enum SignatureHelperType {
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
66 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
72 public static SignatureHelper GetFieldSigHelper (Module mod)
74 if (mod != null && !(mod is ModuleBuilder))
75 throw new ArgumentException ("ModuleBuilder is expected");
77 return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
80 public static SignatureHelper GetLocalVarSigHelper (Module mod)
82 if (mod != null && !(mod is ModuleBuilder))
83 throw new ArgumentException ("ModuleBuilder is expected");
85 return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
88 public static SignatureHelper GetLocalVarSigHelper ()
90 return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
93 public static SignatureHelper GetMethodSigHelper (CallingConventions callingConvention, Type returnType)
95 return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
98 public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention, Type returnType)
100 return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
103 public static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, Type returnType)
105 return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
108 public static SignatureHelper GetMethodSigHelper (Module mod, CallingConvention unmanagedCallConv, Type returnType)
110 return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallConv, returnType, null);
113 public static SignatureHelper GetMethodSigHelper (Module mod, Type returnType, Type[] parameterTypes)
115 return GetMethodSigHelper (mod, CallingConventions.Standard, (CallingConvention)0, returnType, parameterTypes);
118 [MonoTODO("Not implemented")]
119 public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType, Type[] parameterTypes)
121 throw new NotImplementedException ();
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)
132 throw new NotImplementedException ();
136 [MonoTODO("Not implemented")]
137 public static SignatureHelper GetPropertySigHelper (Module mod,
138 CallingConventions callingConvention,
140 Type [] requiredReturnTypeCustomModifiers,
141 Type [] optionalReturnTypeCustomModifiers,
142 Type [] parameterTypes,
143 Type [] [] requiredParameterTypeCustomModifiers,
144 Type [] [] optionalParameterTypeCustomModifiers)
146 throw new NotImplementedException ();
151 // Grows the given array, and returns the index where the element
154 static int AppendArray (ref Type [] array, Type t)
157 Type[] new_a = new Type [array.Length + 1];
158 System.Array.Copy (array, new_a, array.Length);
159 new_a [array.Length] = t;
161 return array.Length-1;
163 array = new Type [1];
170 // Appends the given type array @t into the @array passed at
171 // position @pos. If there is no array, it gets created
173 // This allows adding data to a null array at position 5 for
174 // example, creating 4 empty slots before the slot where @t
178 static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
180 int top = Math.Max (pos, array == null ? 0 : array.Length);
181 Type[][] new_a = new Type [top+1][];
183 System.Array.Copy (array, new_a, top);
188 static void ValidateParameterModifiers (string name, Type [] parameter_modifiers)
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);
200 static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
202 if (custom_modifiers == null)
205 if (custom_modifiers.Length != n)
206 throw new ArgumentException (
208 String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
210 foreach (Type [] parameter_modifiers in custom_modifiers){
211 if (parameter_modifiers == null)
214 ValidateParameterModifiers (name, parameter_modifiers);
218 static Exception MissingFeature ()
220 throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
223 [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
224 public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
226 if (arguments == null)
227 throw new ArgumentNullException ("arguments");
230 if (requiredCustomModifiers != null || optionalCustomModifiers != null){
231 throw MissingFeature();
234 ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
235 ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
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);
244 [MonoTODO ("pinned is ignored")]
245 public void AddArgument (Type argument, bool pinned)
247 AddArgument (argument);
250 public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
252 if (argument == null)
253 throw new ArgumentNullException ("argument");
255 if (requiredCustomModifiers != null)
256 ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
257 if (optionalCustomModifiers != null)
258 ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
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);
267 public void AddArgument (Type clsArgument)
269 if (clsArgument == null)
270 throw new ArgumentNullException ("clsArgument");
272 AppendArray (ref arguments, clsArgument);
275 [MonoTODO("Not implemented")]
276 public void AddSentinel ()
278 throw new NotImplementedException ();
281 static bool CompareOK (Type [][] one, Type [][] two)
287 } else if (two == null)
290 if (one.Length != two.Length)
293 for (int i = 0; i < one.Length; i++){
294 Type [] tone = one [i];
295 Type [] ttwo = two [i];
300 } else if (ttwo == null)
303 if (tone.Length != ttwo.Length)
306 for (int j = 0; j < tone.Length; j++){
307 Type uone = tone [j];
308 Type utwo = ttwo [j];
314 } else if (utwo == null)
317 if (!uone.Equals (utwo))
324 public override bool Equals (object obj)
326 SignatureHelper other = obj as SignatureHelper;
330 if (other.module != module ||
331 other.returnType != returnType ||
332 other.callConv != callConv ||
333 other.unmanagedCallConv != unmanagedCallConv)
336 if (arguments != null){
337 if (other.arguments == null)
339 if (arguments.Length != other.arguments.Length)
342 for (int i = 0; i < arguments.Length; i++)
343 if (!other.arguments [i].Equals (arguments [i]))
345 } else if (other.arguments != null)
348 return CompareOK (other.modreqs, modreqs) && CompareOK (other.modopts, modopts);
351 public override int GetHashCode ()
353 // Lame, but easy, and will work, and chances are
354 // you will only need a few of these.
358 [MethodImplAttribute(MethodImplOptions.InternalCall)]
359 internal extern byte[] get_signature_local ();
361 [MethodImplAttribute(MethodImplOptions.InternalCall)]
362 internal extern byte[] get_signature_field ();
364 public byte[] GetSignature ()
367 case SignatureHelperType.HELPER_LOCAL:
368 return get_signature_local ();
369 case SignatureHelperType.HELPER_FIELD:
370 return get_signature_field ();
372 throw new NotImplementedException ();
376 public override string ToString() {
377 return "SignatureHelper";
380 internal static SignatureHelper GetMethodSigHelper (Module mod, CallingConventions callingConvention, CallingConvention unmanagedCallingConvention, Type returnType,
383 if (mod != null && !(mod is ModuleBuilder))
384 throw new ArgumentException ("ModuleBuilder is expected");
386 if (returnType == null)
387 returnType = typeof (void);
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.");
398 SignatureHelper helper =
399 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
400 helper.returnType = returnType;
401 helper.callConv = callingConvention;
402 helper.unmanagedCallConv = unmanagedCallingConvention;
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];
413 void _SignatureHelper.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
415 throw new NotImplementedException ();
418 void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
420 throw new NotImplementedException ();
423 void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
425 throw new NotImplementedException ();
428 void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
430 throw new NotImplementedException ();