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
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
41 namespace System.Reflection.Emit {
44 [ComDefaultInterface (typeof (_SignatureHelper))]
46 [ClassInterface (ClassInterfaceType.None)]
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 private Type[][] modreqs;
62 private Type[][] modopts;
64 internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
70 public static SignatureHelper GetFieldSigHelper (Module mod)
72 if (mod != null && !(mod is ModuleBuilder))
73 throw new ArgumentException ("ModuleBuilder is expected");
75 return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
78 public static SignatureHelper GetLocalVarSigHelper (Module mod)
80 if (mod != null && !(mod is ModuleBuilder))
81 throw new ArgumentException ("ModuleBuilder is expected");
83 return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
87 public static SignatureHelper GetLocalVarSigHelper ()
89 return new SignatureHelper (null, SignatureHelperType.HELPER_LOCAL);
92 public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType)
94 return GetMethodSigHelper (null, callingConvention, (CallingConvention)0, returnType, null);
97 public static SignatureHelper GetMethodSigHelper (CallingConvention unmanagedCallingConvention,
100 return GetMethodSigHelper (null, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
104 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
106 return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
109 public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
111 return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
114 public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
116 return GetMethodSigHelper (mod, CallingConventions.Standard,
117 (CallingConvention)0, returnType,
120 [MonoTODO("Not implemented")]
121 public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
123 throw new NotImplementedException ();
127 // Grows the given array, and returns the index where the element
130 static int AppendArray (ref Type [] array, Type t)
133 Type[] new_a = new Type [array.Length + 1];
134 System.Array.Copy (array, new_a, array.Length);
135 new_a [array.Length] = t;
139 array = new Type [1];
147 // Appends the given type array @t into the @array passed at
148 // position @pos. If there is no array, it gets created
150 // This allows adding data to a null array at position 5 for
151 // example, creating 4 empty slots before the slot where @t
155 static void AppendArrayAt (ref Type [][] array, Type [] t, int pos)
157 int top = Math.Max (pos, array == null ? 0 : array.Length);
158 Type[][] new_a = new Type [top+1][];
160 System.Array.Copy (array, new_a, top);
165 static void ValidateParameterModifiers(string name, Type [] parameter_modifiers)
167 foreach (Type modifier in parameter_modifiers){
168 if (modifier == null)
169 throw new ArgumentNullException (name);
170 if (modifier.IsArray)
171 throw new ArgumentException (Locale.GetText ("Array type not permitted"), name);
172 if (modifier.ContainsGenericParameters)
173 throw new ArgumentException (Locale.GetText ("Open Generic Type not permitted"), name);
177 static void ValidateCustomModifier (int n, Type [][] custom_modifiers, string name)
179 if (custom_modifiers == null)
182 if (custom_modifiers.Length != n)
183 throw new ArgumentException (
185 String.Format ("Custom modifiers length `{0}' does not match the size of the arguments")));
187 for (int i = 0; i < n; i++)
188 foreach (Type [] parameter_modifiers in custom_modifiers){
189 if (parameter_modifiers == null)
192 ValidateParameterModifiers (name, parameter_modifiers);
196 static Exception MissingFeature ()
198 throw new NotImplementedException ("Mono does not currently support setting modOpt/modReq through SignatureHelper");
201 [MonoTODO("Currently we ignore requiredCustomModifiers and optionalCustomModifiers")]
202 public void AddArguments (Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
204 if (arguments == null)
205 throw new ArgumentNullException ("arguments");
208 if (requiredCustomModifiers != null || optionalCustomModifiers != null){
209 throw MissingFeature();
212 ValidateCustomModifier (arguments.Length, requiredCustomModifiers, "requiredCustomModifiers");
213 ValidateCustomModifier (arguments.Length, optionalCustomModifiers, "optionalCustomModifiers");
215 for (int i = 0; i < arguments.Length; i++){
216 AddArgument (arguments [i],
217 requiredCustomModifiers != null ? requiredCustomModifiers [i] : null,
218 optionalCustomModifiers != null ? optionalCustomModifiers [i] : null);
222 [MonoTODO ("pinned is ignored")]
223 public void AddArgument (Type argument, bool pinned)
225 AddArgument (argument);
228 [MonoTODO ("not implemented")]
229 public void AddArgument (Type argument, Type [] requiredCustomModifiers, Type [] optionalCustomModifiers)
231 if (argument == null)
232 throw new ArgumentNullException ("argument");
234 ValidateParameterModifiers ("requiredCustomModifiers", requiredCustomModifiers);
235 ValidateParameterModifiers ("optionalCustomModifiers", optionalCustomModifiers);
237 int p = AppendArray (ref arguments, argument);
238 if (requiredCustomModifiers != null)
239 AppendArrayAt (ref modreqs, requiredCustomModifiers, p);
240 if (optionalCustomModifiers != null)
241 AppendArrayAt (ref modopts, optionalCustomModifiers, p);
244 [MonoTODO("Not implemented")]
245 public static SignatureHelper GetPropertySigHelper (Module mod, Type returnType,
246 Type [] requiredReturnTypeCustomModifiers,
247 Type [] optionalReturnTypeCustomModifiers,
248 Type [] parameterTypes,
249 Type [] [] requiredParameterTypeCustomModifiers,
250 Type [] [] optionalParameterTypeCustomModifiers)
252 throw new NotImplementedException ();
256 public void AddArgument (Type clsArgument)
258 if (clsArgument == null)
259 throw new ArgumentNullException ("argument");
261 AppendArray (ref arguments, clsArgument);
264 [MonoTODO("Not implemented")]
265 public void AddSentinel ()
267 throw new NotImplementedException ();
270 [MonoTODO("Not implemented")]
271 public override bool Equals (object obj)
273 throw new NotImplementedException ();
276 [MonoTODO("Not implemented")]
277 public override int GetHashCode ()
279 throw new NotImplementedException ();
282 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283 internal extern byte[] get_signature_local ();
285 [MethodImplAttribute(MethodImplOptions.InternalCall)]
286 internal extern byte[] get_signature_field ();
288 public byte[] GetSignature ()
291 case SignatureHelperType.HELPER_LOCAL:
292 return get_signature_local ();
293 case SignatureHelperType.HELPER_FIELD:
294 return get_signature_field ();
296 throw new NotImplementedException ();
300 public override string ToString() {
301 return "SignatureHelper";
304 internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
307 if (mod != null && !(mod is ModuleBuilder))
308 throw new ArgumentException ("ModuleBuilder is expected");
310 SignatureHelper helper =
311 new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
312 helper.returnType = returnType;
313 helper.callConv = callConv;
314 helper.unmanagedCallConv = unmanagedCallConv;
316 if (parameters != null) {
317 helper.arguments = new Type [parameters.Length];
318 for (int i = 0; i < parameters.Length; ++i)
319 helper.arguments [i] = parameters [i];
325 void _SignatureHelper.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
327 throw new NotImplementedException ();
330 void _SignatureHelper.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
332 throw new NotImplementedException ();
335 void _SignatureHelper.GetTypeInfoCount (out uint pcTInfo)
337 throw new NotImplementedException ();
340 void _SignatureHelper.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
342 throw new NotImplementedException ();