52 this.original = original;
55 public Type GetGenericTypeArgument(
int index)
60 public Type GetGenericMethodArgument(
int index)
70 readonly Type declaringType;
71 readonly Type[] methodArgs;
78 internal Binder(Type declaringType, Type[] methodArgs)
80 this.declaringType = declaringType;
81 this.methodArgs = methodArgs;
84 public Type BindTypeParameter(Type type)
86 return declaringType.GetGenericTypeArgument(type.GenericParameterPosition);
89 public Type BindMethodParameter(Type type)
91 if (methodArgs ==
null)
94 return methodArgs[type.GenericParameterPosition];
101 internal static readonly Unbinder Instance =
new Unbinder();
111 public Type BindTypeParameter(Type type)
116 public Type BindMethodParameter(Type type)
123 readonly Type returnType;
124 readonly Type[] parameterTypes;
126 readonly CallingConventions callingConvention;
127 readonly
int genericParamCount;
139 this.returnType = returnType;
140 this.parameterTypes = parameterTypes;
141 this.modifiers = modifiers;
142 this.callingConvention = callingConvention;
143 this.genericParamCount = genericParamCount;
150 && other.callingConvention == callingConvention
151 && other.genericParamCount == genericParamCount
152 && other.returnType.Equals(returnType)
153 &&
Util.ArrayEquals(other.parameterTypes, parameterTypes)
154 && other.modifiers.Equals(modifiers);
159 return genericParamCount ^ 77 * (int)callingConvention
160 ^ 3 * returnType.GetHashCode()
161 ^
Util.GetHashCode(parameterTypes) * 5
162 ^ modifiers.GetHashCode() * 55;
167 var flags = br.ReadByte();
168 var callingConvention = (flags & 7)
switch
170 DEFAULT => CallingConventions.Standard,
171 VARARG => CallingConventions.VarArgs,
172 _ =>
throw new BadImageFormatException(),
175 if ((flags & HASTHIS) != 0)
176 callingConvention |= CallingConventions.HasThis;
178 if ((flags & EXPLICITTHIS) != 0)
179 callingConvention |= CallingConventions.ExplicitThis;
181 var genericParamCount = 0;
182 if ((flags & GENERIC) != 0)
184 genericParamCount = br.ReadCompressedUInt();
185 context =
new UnboundGenericMethodContext(context);
188 var paramCount = br.ReadCompressedUInt();
189 CustomModifiers[] modifiers =
null;
193 var parameterTypes =
new Type[paramCount];
194 for (
int i = 0; i < parameterTypes.Length; i++)
196 if ((callingConvention & CallingConventions.VarArgs) != 0 && br.PeekByte() == SENTINEL)
198 Array.Resize(
ref parameterTypes, i);
199 if (modifiers !=
null)
200 Array.Resize(
ref modifiers, i + 1);
206 parameterTypes[i] =
ReadParam(module, br, context);
214 CallingConventions callingConvention = 0;
215 System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention = 0;
218 var flags = br.ReadByte();
222 callingConvention = CallingConventions.Standard;
226 unmanagedCallingConvention =
System.
Runtime.InteropServices.CallingConvention.Cdecl;
230 unmanagedCallingConvention =
System.
Runtime.InteropServices.CallingConvention.StdCall;
234 unmanagedCallingConvention =
System.
Runtime.InteropServices.CallingConvention.ThisCall;
238 unmanagedCallingConvention =
System.
Runtime.InteropServices.CallingConvention.FastCall;
242 callingConvention = CallingConventions.VarArgs;
246 throw new BadImageFormatException();
249 if ((flags & HASTHIS) != 0)
250 callingConvention |= CallingConventions.HasThis;
251 if ((flags & EXPLICITTHIS) != 0)
252 callingConvention |= CallingConventions.ExplicitThis;
253 if ((flags & GENERIC) != 0)
254 throw new BadImageFormatException();
256 var paramCount = br.ReadCompressedUInt();
257 CustomModifiers[] customModifiers =
null;
262 var parameterTypes =
new List<Type>();
263 var optionalParameterTypes =
new List<Type>();
264 var curr = parameterTypes;
265 for (
int i = 0; i < paramCount; i++)
267 if (br.PeekByte() == SENTINEL)
270 curr = optionalParameterTypes;
274 curr.Add(
ReadParam(module, br, context));
277 return new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray(),
PackedCustomModifiers.Wrap(customModifiers));
280 internal int GetParameterCount()
282 return parameterTypes.Length;
285 internal Type GetParameterType(
int index)
287 return parameterTypes[index];
292 return returnType.BindTypeParameters(binder);
295 internal CustomModifiers GetReturnTypeCustomModifiers(
IGenericBinder binder)
297 return modifiers.GetReturnTypeCustomModifiers().Bind(binder);
302 return parameterTypes[index].BindTypeParameters(binder);
305 internal CustomModifiers GetParameterCustomModifiers(
IGenericBinder binder,
int index)
307 return modifiers.GetParameterCustomModifiers(index).Bind(binder);
312 get {
return callingConvention; }
315 internal int GenericParameterCount
317 get {
return genericParamCount; }
322 var binder =
new Binder(type, methodArgs);
323 return new MethodSignature(returnType.BindTypeParameters(binder),
BindTypeParameters(binder, parameterTypes), modifiers.Bind(binder), callingConvention, genericParamCount);
328 if (genericParamCount > 0)
330 returnType = returnType.BindTypeParameters(Unbinder.Instance);
332 modifiers = modifiers.Bind(Unbinder.Instance);
335 return new MethodSignature(returnType, parameterTypes, modifiers, callingConvention, genericParamCount);
340 return Util.ArrayEquals(other.parameterTypes, parameterTypes);
343 internal bool MatchParameterTypes(
Type[] types)
345 return Util.ArrayEquals(types, parameterTypes);
348 internal override void Write(ModuleBuilder module,
ByteBuffer bb)
350 WriteImpl(module, bb, parameterTypes.Length);
353 internal void WriteMethodRef(ModuleBuilder module,
ByteBuffer bb,
Type[] optionalParameterTypes, CustomModifiers[] customModifiers)
355 WriteImpl(module, bb, parameterTypes.Length + optionalParameterTypes.Length);
357 if (optionalParameterTypes.Length > 0)
360 for (
int i = 0; i < optionalParameterTypes.Length; i++)
363 WriteType(module, bb, optionalParameterTypes[i]);
368 void WriteImpl(ModuleBuilder module,
ByteBuffer bb,
int parameterCount)
372 if ((callingConvention & CallingConventions.Any) == CallingConventions.VarArgs)
374 Debug.Assert(genericParamCount == 0);
377 else if (genericParamCount > 0)
386 if ((callingConvention & CallingConventions.HasThis) != 0)
389 if ((callingConvention & CallingConventions.ExplicitThis) != 0)
390 first |= EXPLICITTHIS;
394 if (genericParamCount > 0)
395 bb.WriteCompressedUInt(genericParamCount);
397 bb.WriteCompressedUInt(parameterCount);
403 for (
int i = 0; i < parameterTypes.Length; i++)
406 WriteType(module, bb, parameterTypes[i]);