Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ internal void WriteCustomAttributes(List<CustomAttributeWrapper>? customAttribut

private EntityHandle GetTypeReferenceOrSpecificationHandle(Type type)
{
type = type.UnderlyingSystemType;

if (!_typeReferences.TryGetValue(type, out var typeHandle))
{
if (type.HasElementType || type.IsGenericParameter ||
Expand Down Expand Up @@ -740,7 +742,12 @@ private EntityHandle GetMemberReferenceHandle(MemberInfo memberInfo)
declaringType = declaringType.MakeGenericType(declaringType.GetGenericArguments());
}

Type fieldType = ((FieldInfo)GetOriginalMemberIfConstructedType(field)).FieldType;
FieldInfo originalField = (FieldInfo)GetOriginalMemberIfConstructedType(field);
Type fieldType = originalField.FieldType;

if (fieldType.IsUnmanagedFunctionPointer)
fieldType = originalField.GetModifiedFieldType();

memberHandle = AddMemberReference(field.Name, GetTypeHandle(declaringType),
MetadataSignatureHelper.GetFieldSignature(fieldType, field.GetRequiredCustomModifiers(), field.GetOptionalCustomModifiers(), this));

Expand Down Expand Up @@ -790,9 +797,15 @@ private EntityHandle GetMethodReference(MethodInfo methodInfo, Type[] optionalPa
return memberHandle;
}

private BlobBuilder GetMethodSignature(MethodInfo method, Type[]? optionalParameterTypes) =>
MetadataSignatureHelper.GetMethodSignature(this, ParameterTypes(method.GetParameters()), method.ReturnType,
private BlobBuilder GetMethodSignature(MethodInfo method, Type[]? optionalParameterTypes)
{
Type returnType = method.ReturnType;
if (returnType.IsUnmanagedFunctionPointer)
returnType = method.ReturnParameter.GetModifiedParameterType();

return MetadataSignatureHelper.GetMethodSignature(this, ParameterTypes(method.GetParameters()), returnType,
GetSignatureConvention(method.CallingConvention), method.GetGenericArguments().Length, !method.IsStatic, optionalParameterTypes);
}

private BlobBuilder GetMethodArrayMethodSignature(ArrayMethod method) => MetadataSignatureHelper.GetMethodSignature(
this, method.ParameterTypes, method.ReturnType, GetSignatureConvention(method.CallingConvention), isInstance: IsInstance(method.CallingConvention));
Expand Down Expand Up @@ -840,7 +853,11 @@ private static Type[] ParameterTypes(ParameterInfo[] parameterInfos)

for (int i = 0; i < parameterInfos.Length; i++)
{
parameterTypes[i] = parameterInfos[i].ParameterType;
Type paramType = parameterInfos[i].ParameterType;
if (paramType.IsUnmanagedFunctionPointer)
paramType = parameterInfos[i].GetModifiedParameterType();

parameterTypes[i] = paramType;
}

return parameterTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,16 @@ private static void WriteSignatureForFunctionPointerType(SignatureTypeEncoder si
CustomModifiersEncoder retModifiersEncoder = retTypeEncoder.CustomModifiers();

if (returnType.GetOptionalCustomModifiers() is Type[] retModOpts)
{
Array.Reverse(retModOpts);
WriteCustomModifiers(retModifiersEncoder, retModOpts, isOptional: true, module);
}

if (returnType.GetRequiredCustomModifiers() is Type[] retModReqs)
{
Array.Reverse(retModReqs);
WriteCustomModifiers(retModifiersEncoder, retModReqs, isOptional: false, module);
}

WriteSignatureForType(retTypeEncoder.Type(), returnType, module);

Expand All @@ -284,17 +290,24 @@ private static void WriteSignatureForFunctionPointerType(SignatureTypeEncoder si
CustomModifiersEncoder paramModifiersEncoder = paramEncoder.CustomModifiers();

if (paramType.GetOptionalCustomModifiers() is Type[] paramModOpts)
{
Array.Reverse(paramModOpts);
WriteCustomModifiers(paramModifiersEncoder, paramModOpts, isOptional: true, module);
}

if (paramType.GetRequiredCustomModifiers() is Type[] paramModReqs)
{
Array.Reverse(paramModReqs);
WriteCustomModifiers(paramModifiersEncoder, paramModReqs, isOptional: false, module);
}

WriteSignatureForType(paramEncoder.Type(), paramType, module);
}
}

private static void WriteSimpleSignature(SignatureTypeEncoder signature, Type type, ModuleBuilderImpl module)
{
type = type.UnderlyingSystemType;
CoreTypeId? typeId = module.GetTypeIdFromCoreTypes(type);

switch (typeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public void SaveFunctionPointerFields()
}

[Fact]
public void ConsumeFunctionPointerFields()
public void ConsumeFunctionPointerMembers()
{
// public unsafe class Container
// {
Expand Down Expand Up @@ -915,16 +915,23 @@ public void ConsumeFunctionPointerFields()
MethodBuilder mainMethod = programType.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);
mainMethod.SetReturnType(typeof(int));
ILGenerator il = mainMethod.GetILGenerator();
il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerFields).GetField("field1"));
il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerMembers).GetField("field1"));
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerMembers).GetField("field2"));
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerMembers).GetField("field3"));
il.Emit(OpCodes.Pop);
//il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerMembers).GetField("field4"));
//il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Call, typeof(ClassWithFunctionPointerMembers).GetMethod("Method1"));
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Call, typeof(ClassWithFunctionPointerMembers).GetMethod("Method2"));
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Call, typeof(ClassWithFunctionPointerMembers).GetMethod("Method3"));
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Call, typeof(ClassWithFunctionPointerMembers).GetMethod("Method4"));
il.Emit(OpCodes.Call, typeof(ClassWithFunctionPointerMembers).GetMethod("Method5"));
il.Emit(OpCodes.Pop);
// References to fields with unmanaged calling convention are broken
// [ActiveIssue("https://github.com/dotnet/runtime/issues/120909")]
// il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerFields).GetField("field2"));
// il.Emit(OpCodes.Pop);
// il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerFields).GetField("field3"));
// il.Emit(OpCodes.Pop);
// il.Emit(OpCodes.Ldsfld, typeof(ClassWithFunctionPointerFields).GetField("field4"));
// il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Call, assembly1FromDisk.GetType("Container").GetMethod("Init"));
il.Emit(OpCodes.Ldc_I4_2);
il.Emit(OpCodes.Ldc_I4_3);
Expand Down Expand Up @@ -989,11 +996,17 @@ public class ClassWithFields : EmptyTestClass
public byte field2;
}

public unsafe class ClassWithFunctionPointerFields
public unsafe class ClassWithFunctionPointerMembers
{
public static delegate*<ClassWithFunctionPointerFields> field1;
public static delegate*<ClassWithFunctionPointerMembers> field1;
public static delegate* unmanaged<int> field2;
public static delegate* unmanaged[Cdecl]<Guid> field3;
public static delegate* unmanaged[Cdecl, SuppressGCTransition]<Vector<int>, Vector<int>> field4;

public static delegate*<int> Method1() => null;
public static delegate* unmanaged<string> Method2() => null;
public static delegate* unmanaged[Fastcall]<double> Method3() => null;
public static delegate* unmanaged[Cdecl]<delegate* unmanaged<int>, Guid> Method4() => null;
public static delegate* unmanaged[Cdecl]<int> Method5(delegate* unmanaged[Cdecl]<delegate* unmanaged<int>, Guid> funcPtr) => null;
}
}
Loading