Skip to content

Commit 81b4636

Browse files
committed
Core: Factor out largely-incorrect IsPrimitive prop on TypeAnalysisContext
1 parent ff3368f commit 81b4636

File tree

5 files changed

+22
-45
lines changed

5 files changed

+22
-45
lines changed

Cpp2IL.Core/Model/Contexts/SystemTypesContext.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,6 @@ public SystemTypesContext(ApplicationAnalysisContext appContext)
6969
UnmanagedCallersOnlyAttributeType = systemAssembly.GetTypeByFullName("System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute");
7070
}
7171

72-
public bool IsPrimitive(TypeAnalysisContext context)
73-
{
74-
return context == SystemBooleanType ||
75-
context == SystemCharType ||
76-
context == SystemSByteType ||
77-
context == SystemByteType ||
78-
context == SystemInt16Type ||
79-
context == SystemUInt16Type ||
80-
context == SystemInt32Type ||
81-
context == SystemUInt32Type ||
82-
context == SystemInt64Type ||
83-
context == SystemUInt64Type ||
84-
context == SystemSingleType ||
85-
context == SystemDoubleType ||
86-
context == SystemIntPtrType ||
87-
context == SystemUIntPtrType;
88-
}
89-
9072
public bool TryGetIl2CppTypeEnum(TypeAnalysisContext context, out Il2CppTypeEnum value)
9173
{
9274
if (context == SystemBooleanType)

Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,6 @@ public class TypeAnalysisContext : HasCustomAttributesAndName, ITypeInfoProvider
7676
public TypeAnalysisContext? BaseType => OverrideBaseType ?? (Definition == null ? null : DeclaringAssembly.ResolveIl2CppType(Definition.RawBaseType));
7777

7878
public TypeAnalysisContext[] InterfaceContexts => (Definition?.RawInterfaces.Select(DeclaringAssembly.ResolveIl2CppType).ToArray() ?? [])!;
79-
80-
public bool IsPrimitive
81-
{
82-
get
83-
{
84-
if (Definition == null)
85-
return false;
86-
87-
if (Definition.RawType?.Type.IsIl2CppPrimitive() == true)
88-
return true;
89-
90-
//Might still be TYPE_CLASS but yet int or something, so check it directly
91-
return AppContext.SystemTypes.IsPrimitive(this);
92-
}
93-
}
9479

9580
public virtual Il2CppTypeEnum Type
9681
{

Cpp2IL.Core/Model/CustomAttributes/CustomAttributeTypeParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override string ToString()
5454
if (TypeContext == null)
5555
return "(Type) null";
5656

57-
if (TypeContext.IsPrimitive)
57+
if (TypeContext.Type is var typeEnum && typeEnum.IsIl2CppPrimitive())
5858
return $"typeof({LibCpp2ILUtils.GetTypeName(TypeContext.Type)}";
5959

6060
if (TypeContext is ReferencedTypeAnalysisContext)

Cpp2IL.Core/OutputFormats/WasmNameSectionOutputFormat.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public override void DoOutput(ApplicationAnalysisContext context, string outputR
6060
if (!v.method.IsStatic)
6161
parameters.Insert(0, "this");
6262

63-
if (v.method.ReturnTypeContext is
64-
{ IsValueType: true, IsPrimitive: true, Definition: null or { Size: > 8 } })
63+
if (v.method.ReturnTypeContext is { IsValueType: true, Definition: null or { Size: > 8 } } rt && rt.IsWasmPrimitive()) //TODO Check - I think this IsWasmPrimitive check is inverted but I just ported what it was
6564
parameters.Insert(0, "out");
6665

6766
parameters.Add("methodInfo"); // Only for some methods...?

Cpp2IL.Core/Utils/WasmUtils.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.RegularExpressions;
66
using Cpp2IL.Core.Model.Contexts;
77
using LibCpp2IL;
8+
using LibCpp2IL.BinaryStructures;
89
using LibCpp2IL.Metadata;
910
using LibCpp2IL.Reflection;
1011
using LibCpp2IL.Wasm;
@@ -26,18 +27,28 @@ public static string BuildSignature(MethodAnalysisContext definition)
2627
//It feels like it's something to do with when DateTime is considered a struct and when it's considered a class.
2728
//But I can find no rhyme nor reason to it.
2829

29-
var returnTypeSignature = definition.ReturnTypeContext switch
30-
{
31-
{ Namespace: nameof(System), Name: "Void" } => "v",
32-
{ IsValueType: true, IsPrimitive: false, Definition: null or { Size: < 0 or > 8 } } => "vi", //Large or Generic Struct returns have a void return type, but the actual return value is the first parameter.
33-
{ IsValueType: true, IsPrimitive: false, Definition.Size: > 4 } => "j", //Medium structs are returned as longs
34-
{ IsValueType: true, IsPrimitive: false, Definition.Size: <= 4 } => "i", //Small structs are returned as ints
35-
_ => GetSignatureLetter(definition.ReturnTypeContext!)
36-
};
30+
var returnTypeSignature = definition.ReturnTypeContext.IsWasmPrimitive()
31+
? GetSignatureLetter(definition.ReturnTypeContext)
32+
: definition.ReturnTypeContext switch
33+
{
34+
{ Namespace: nameof(System), Name: "Void" } => "v",
35+
{ IsValueType: true, Definition: null or { Size: < 0 or > 8 } } => "vi", //Large or Generic Struct returns have a void return type, but the actual return value is the first parameter.
36+
{ IsValueType: true, Definition.Size: > 4 } => "j", //Medium structs are returned as longs
37+
{ IsValueType: true, Definition.Size: <= 4 } => "i", //Small structs are returned as ints
38+
_ => GetSignatureLetter(definition.ReturnTypeContext!)
39+
};
3740

3841
return $"{returnTypeSignature}{instanceParam}{string.Join("", definition.Parameters!.Select(p => GetSignatureLetter(p.ParameterTypeContext, p.IsRef)))}i"; //Add an extra i on the end for the method info param
3942
}
4043

44+
public static bool IsWasmPrimitive(this TypeAnalysisContext type)
45+
{
46+
var typeEnum = type.Type;
47+
48+
//TODO Validate this, it's only the remnant from some poorly written logic for checking if a TypeAnalysisContext IsPrimitive.
49+
return typeEnum is >= Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN and <= Il2CppTypeEnum.IL2CPP_TYPE_R8;
50+
}
51+
4152
private static string GetSignatureLetter(TypeAnalysisContext type, bool isRefOrOut = false)
4253
{
4354
if (isRefOrOut)
@@ -60,7 +71,7 @@ private static string GetSignatureLetter(TypeAnalysisContext type, bool isRefOrO
6071
"Single" => "f",
6172
"Double" => "d",
6273
"Int32" => "i",
63-
_ when type is { IsValueType: true, IsPrimitive: false, IsEnumType: false, Definition.Size: <= 8 and > 0 } => "j", //TODO check - value types < 16 bytes (including base object header which is irrelevant here) are passed directly as long?
74+
_ when !type.IsWasmPrimitive() && type is { IsValueType: true, IsEnumType: false, Definition.Size: <= 8 and > 0 } => "j", //TODO check - value types < 16 bytes (including base object header which is irrelevant here) are passed directly as long?
6475
_ => "i"
6576
};
6677
}

0 commit comments

Comments
 (0)