[fix] 修复桥接函数计算时未归结函数参数类型,导致出现多个同名签名的bug

main
walon 2023-11-01 20:23:31 +08:00
parent d5b52cc8db
commit 47fbd443c1
2 changed files with 36 additions and 4 deletions

View File

@ -48,7 +48,7 @@ namespace HybridCLR.Editor.ABI
public bool Equals(TypeInfo other) public bool Equals(TypeInfo other)
{ {
return PorType == other.PorType && object.Equals(Klass, other.Klass); return PorType == other.PorType && TypeEqualityComparer.Instance.Equals(Klass, other.Klass);
} }
public override bool Equals(object obj) public override bool Equals(object obj)
@ -58,7 +58,7 @@ namespace HybridCLR.Editor.ABI
public override int GetHashCode() public override int GetHashCode()
{ {
return (int)PorType * 23 + (Klass?.GetHashCode() ?? 0); return (int)PorType * 23 + (Klass != null ? TypeEqualityComparer.Instance.GetHashCode(Klass) : 0);
} }
public bool NeedExpandValue() public bool NeedExpandValue()

View File

@ -52,6 +52,23 @@ namespace HybridCLR.Editor.MethodBridge
_typeCreator = new TypeCreator(); _typeCreator = new TypeCreator();
} }
private readonly Dictionary<string, TypeInfo> _sig2Types = new Dictionary<string, TypeInfo>();
private TypeInfo GetSharedTypeInfo(TypeSig type)
{
var typeInfo = _typeCreator.CreateTypeInfo(type);
if (!typeInfo.IsStruct)
{
return typeInfo;
}
string sigName = ToFullName(typeInfo.Klass);
if (!_sig2Types.TryGetValue(sigName, out var sharedTypeInfo))
{
sharedTypeInfo = typeInfo;
_sig2Types.Add(sigName, sharedTypeInfo);
}
return sharedTypeInfo;
}
private MethodDesc CreateMethodDesc(MethodDef methodDef, bool forceRemoveThis, TypeSig returnType, List<TypeSig> parameters) private MethodDesc CreateMethodDesc(MethodDef methodDef, bool forceRemoveThis, TypeSig returnType, List<TypeSig> parameters)
{ {
@ -70,12 +87,12 @@ namespace HybridCLR.Editor.MethodBridge
{ {
throw new Exception($"[PreservedMethod] method:{methodDef} has generic parameters"); throw new Exception($"[PreservedMethod] method:{methodDef} has generic parameters");
} }
paramInfos.Add(new ParamInfo() { Type = _typeCreator.CreateTypeInfo(paramInfo) }); paramInfos.Add(new ParamInfo() { Type = GetSharedTypeInfo(paramInfo) });
} }
var mbs = new MethodDesc() var mbs = new MethodDesc()
{ {
MethodDef = methodDef, MethodDef = methodDef,
ReturnInfo = new ReturnInfo() { Type = returnType != null ? _typeCreator.CreateTypeInfo(returnType) : TypeInfo.s_void }, ReturnInfo = new ReturnInfo() { Type = returnType != null ? GetSharedTypeInfo(returnType) : TypeInfo.s_void },
ParamInfos = paramInfos, ParamInfos = paramInfos,
}; };
return mbs; return mbs;
@ -158,6 +175,18 @@ namespace HybridCLR.Editor.MethodBridge
} }
} }
static void CheckUnique(IEnumerable<string> names)
{
var set = new HashSet<string>();
foreach (var name in names)
{
if (!set.Add(name))
{
throw new Exception($"[CheckUnique] duplicate name:{name}");
}
}
}
public void Generate() public void Generate()
{ {
var frr = new FileRegionReplace(_templateCode); var frr = new FileRegionReplace(_templateCode);
@ -191,6 +220,9 @@ namespace HybridCLR.Editor.MethodBridge
GenerateClassInfo(type, classTypeSet, classInfos); GenerateClassInfo(type, classTypeSet, classInfos);
} }
CheckUnique(structTypes.Select(t => ToFullName(t.Klass)));
CheckUnique(structTypes.Select(t => t.CreateSigName()));
GenerateStructDefines(classInfos, lines); GenerateStructDefines(classInfos, lines);
GenerateStructureSignatureStub(structTypes, lines); GenerateStructureSignatureStub(structTypes, lines);