Output: Render finalizers as C# destructors (CS0249)

This commit is contained in:
Katy Coe
2019-11-18 06:24:18 +01:00
parent b141e57598
commit 8f7b461310
2 changed files with 13 additions and 2 deletions

View File

@@ -418,8 +418,16 @@ namespace Il2CppInspector
// IL2CPP doesn't seem to retain return type attributes // IL2CPP doesn't seem to retain return type attributes
//writer.Append(method.ReturnType.CustomAttributes.ToString(prefix + "\t", "return: ", emitPointer: !SuppressMetadata)); //writer.Append(method.ReturnType.CustomAttributes.ToString(prefix + "\t", "return: ", emitPointer: !SuppressMetadata));
writer.Append($"{prefix}\t{method.GetModifierString(scope)}"); writer.Append($"{prefix}\t{method.GetModifierString(scope)}");
if (method.Name != "op_Implicit" && method.Name != "op_Explicit")
// Finalizers become destructors
if (method.Name == "Finalize" && method.IsVirtual && method.ReturnType.FullName == "System.Void" && method.IsFamily)
writer.Append("~" + method.DeclaringType.UnmangledBaseName);
// Regular method or operator overload
else if (method.Name != "op_Implicit" && method.Name != "op_Explicit")
writer.Append($"{method.ReturnParameter.GetReturnParameterString(scope)} {method.CSharpName}{method.GetTypeParametersString(scope)}"); writer.Append($"{method.ReturnParameter.GetReturnParameterString(scope)} {method.CSharpName}{method.GetTypeParametersString(scope)}");
// User-defined conversion operator
else else
writer.Append($"{method.CSharpName}{method.ReturnType.GetScopedCSharpName(scope)}"); writer.Append($"{method.CSharpName}{method.ReturnType.GetScopedCSharpName(scope)}");
writer.Append("(" + method.GetParametersString(scope, !SuppressMetadata) + ")"); writer.Append("(" + method.GetParametersString(scope, !SuppressMetadata) + ")");

View File

@@ -135,6 +135,9 @@ namespace Il2CppInspector.Reflection
// Static constructors can not have an access level modifier // Static constructors can not have an access level modifier
{ IsConstructor: true, IsStatic: true } => "", { IsConstructor: true, IsStatic: true } => "",
// Finalizers can not have an access level modifier
{ Name: "Finalize", IsVirtual: true, IsFamily: true } => "",
// Explicit interface implementations do not have an access level modifier // Explicit interface implementations do not have an access level modifier
{ IsVirtual: true, IsFinal: true, Attributes: var a } when (a & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot && Name.IndexOf('.') != -1 => "", { IsVirtual: true, IsFinal: true, Attributes: var a } when (a & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot && Name.IndexOf('.') != -1 => "",
@@ -162,7 +165,7 @@ namespace Il2CppInspector.Reflection
if (IsFinal && (Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot) if (IsFinal && (Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot)
modifiers.Append("sealed override "); modifiers.Append("sealed override ");
// All abstract, override and sealed methods are also virtual by nature // All abstract, override and sealed methods are also virtual by nature
if (IsVirtual && !IsAbstract && !IsFinal) if (IsVirtual && !IsAbstract && !IsFinal && Name != "Finalize")
modifiers.Append((Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot ? "virtual " : "override "); modifiers.Append((Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot ? "virtual " : "override ");
if (IsStatic) if (IsStatic)
modifiers.Append("static "); modifiers.Append("static ");