25using System.Collections.Generic;
26using System.Runtime.InteropServices;
33 internal abstract class SignatureHelper
36 protected readonly
byte type;
37 protected ushort argumentCount;
39 sealed class Lazy : SignatureHelper
42 readonly List<Type> args =
new List<Type>();
48 internal Lazy(
byte type) :
54 internal override Type ReturnType
56 get {
return args[0]; }
59 public override byte[] GetSignature()
61 throw new NotSupportedException();
64 internal override ByteBuffer GetSignature(ModuleBuilder module)
67 Signature.WriteSignatureHelper(module, bb, type, argumentCount, args);
71 public override void AddSentinel()
76 public override void __AddArgument(
Type argument,
bool pinned, CustomModifiers customModifiers)
81 foreach (var mod
in customModifiers)
93 sealed class Eager : SignatureHelper
96 readonly ModuleBuilder module;
98 readonly
Type returnType;
106 internal Eager(ModuleBuilder module,
byte type,
Type returnType) :
109 this.module = module;
110 this.returnType = returnType;
117 internal override Type ReturnType
119 get {
return returnType; }
122 public override byte[] GetSignature()
124 return GetSignature(
null).ToArray();
127 internal override ByteBuffer GetSignature(ModuleBuilder module)
132 bb.Insert(
MetadataWriter.GetCompressedUIntLength(argumentCount) - bb.GetCompressedUIntLength());
133 bb.WriteCompressedUInt(argumentCount);
139 public override void AddSentinel()
144 public override void __AddArgument(
Type argument,
bool pinned, CustomModifiers customModifiers)
149 foreach (var mod
in customModifiers)
151 bb.Write(mod.IsRequired ?
Signature.ELEMENT_TYPE_CMOD_REQD :
Signature.ELEMENT_TYPE_CMOD_OPT);
152 Signature.WriteTypeSpec(module, bb, mod.Type);
155 Signature.WriteTypeSpec(module, bb, argument ?? module.Universe.System_Void);
164 SignatureHelper(
byte type)
169 internal bool HasThis
171 get {
return (type &
Signature.HASTHIS) != 0; }
174 internal abstract Type ReturnType
179 internal int ArgumentCount
181 get {
return argumentCount; }
184 private static SignatureHelper Create(
Module mod,
byte type,
Type returnType)
186 return mod is not ModuleBuilder mb ?
new Lazy(type) : new Eager(mb, type, returnType);
189 public static SignatureHelper GetFieldSigHelper(
Module mod)
191 return Create(mod,
Signature.FIELD,
null);
194 public static SignatureHelper GetLocalVarSigHelper()
199 public static SignatureHelper GetLocalVarSigHelper(
Module mod)
201 return Create(mod,
Signature.LOCAL_SIG,
null);
204 public static SignatureHelper GetPropertySigHelper(
Module mod,
Type returnType,
Type[] parameterTypes)
206 var sig = Create(mod,
Signature.PROPERTY, returnType);
207 sig.AddArgument(returnType);
208 sig.argumentCount = 0;
209 sig.AddArguments(parameterTypes,
null,
null);
213 public static SignatureHelper GetPropertySigHelper(
Module mod,
Type returnType,
Type[] requiredReturnTypeCustomModifiers,
Type[] optionalReturnTypeCustomModifiers,
Type[] parameterTypes,
Type[][] requiredParameterTypeCustomModifiers,
Type[][] optionalParameterTypeCustomModifiers)
215 return GetPropertySigHelper(mod, CallingConventions.Standard, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
218 public static SignatureHelper GetPropertySigHelper(
Module mod, CallingConventions callingConvention,
Type returnType,
Type[] requiredReturnTypeCustomModifiers,
Type[] optionalReturnTypeCustomModifiers,
Type[] parameterTypes,
Type[][] requiredParameterTypeCustomModifiers,
Type[][] optionalParameterTypeCustomModifiers)
221 if ((callingConvention & CallingConventions.HasThis) != 0)
224 var sig = Create(mod, type, returnType);
225 sig.AddArgument(returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers);
226 sig.argumentCount = 0;
227 sig.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
231 public static SignatureHelper GetMethodSigHelper(
CallingConvention unmanagedCallingConvention,
Type returnType)
233 return GetMethodSigHelper(
null, unmanagedCallingConvention, returnType);
236 public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention,
Type returnType)
238 return GetMethodSigHelper(
null, callingConvention, returnType);
243 var type = unmanagedCallConv
switch
245 CallingConvention.Cdecl => (byte)0x01,
247 CallingConvention.ThisCall => (byte)0x03,
249 _ =>
throw new ArgumentOutOfRangeException(
"unmanagedCallConv"),
252 var sig = Create(mod, type, returnType);
253 sig.AddArgument(returnType);
254 sig.argumentCount = 0;
258 public static SignatureHelper GetMethodSigHelper(
Module mod, CallingConventions callingConvention,
Type returnType)
262 if ((callingConvention & CallingConventions.HasThis) != 0)
265 if ((callingConvention & CallingConventions.ExplicitThis) != 0)
268 if ((callingConvention & CallingConventions.VarArgs) != 0)
271 var sig = Create(mod, type, returnType);
272 sig.AddArgument(returnType);
273 sig.argumentCount = 0;
277 public static SignatureHelper GetMethodSigHelper(
Module mod,
Type returnType,
Type[] parameterTypes)
279 var sig = Create(mod, 0, returnType);
280 sig.AddArgument(returnType);
281 sig.argumentCount = 0;
282 sig.AddArguments(parameterTypes,
null,
null);
286 public abstract byte[] GetSignature();
288 internal abstract ByteBuffer GetSignature(ModuleBuilder module);
290 public abstract void AddSentinel();
292 public void AddArgument(
Type clsArgument)
294 AddArgument(clsArgument,
false);
297 public void AddArgument(
Type argument,
bool pinned)
299 __AddArgument(argument, pinned,
new CustomModifiers());
302 public void AddArgument(
Type argument,
Type[] requiredCustomModifiers,
Type[] optionalCustomModifiers)
304 __AddArgument(argument,
false, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
307 public abstract void __AddArgument(
Type argument,
bool pinned, CustomModifiers customModifiers);
309 public void AddArguments(
Type[] arguments,
Type[][] requiredCustomModifiers,
Type[][] optionalCustomModifiers)
311 if (arguments !=
null)
312 for (
int i = 0; i < arguments.Length; i++)
313 __AddArgument(arguments[i],
false, CustomModifiers.FromReqOpt(
Util.NullSafeElementAt(requiredCustomModifiers, i),
Util.NullSafeElementAt(optionalCustomModifiers, i)));
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
System.Runtime.InteropServices.CallingConvention CallingConvention