Model: Give operator overloads and user-defined conversion operators the correct syntax

This commit is contained in:
Katy Coe
2019-10-29 21:44:43 +01:00
parent 26443f594a
commit 035a663484
2 changed files with 58 additions and 2 deletions

View File

@@ -201,8 +201,12 @@ namespace Il2CppInspector
// Don't re-output methods for constructors, properties, events etc.
foreach (var method in type.DeclaredMethods.Except(usedMethods)) {
writer.Write($"\t{method.GetModifierString()}{method.ReturnType.CSharpName} {method.Name}(");
writer.Write(getParametersString(method.DeclaredParameters));
writer.Write($"\t{method.GetModifierString()}");
if (method.Name != "op_Implicit" && method.Name != "op_Explicit")
writer.Write($"{method.ReturnType.CSharpName} {method.CSharpName}");
else
writer.Write($"{method.CSharpName}{method.ReturnType.CSharpName}");
writer.Write("(" + getParametersString(method.DeclaredParameters));
writer.Write(");" + (method.VirtualAddress != 0? $" // {formatAddress(method.VirtualAddress)}" : "") + "\n");
}
writer.Write("}\n");

View File

@@ -46,6 +46,10 @@ namespace Il2CppInspector.Reflection
// TODO: GetMethodBody()
public string CSharpName =>
// Operator overload or user-defined conversion operator
OperatorMethodNames.ContainsKey(Name)? "operator " + OperatorMethodNames[Name] : Name;
protected MethodBase(Il2CppInspector pkg, int methodIndex, TypeInfo declaringType) : base(declaringType) {
Definition = pkg.Methods[methodIndex];
Index = methodIndex;
@@ -124,8 +128,56 @@ namespace Il2CppInspector.Reflection
if ((Attributes & MethodAttributes.PinvokeImpl) != 0)
modifiers.Append("extern ");
if (Name == "op_Implicit")
modifiers.Append("implicit ");
if (Name == "op_Explicit")
modifiers.Append("explicit ");
// Will include a trailing space
return modifiers.ToString();
}
// List of operator overload metadata names
// https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/operator-overloads
public static Dictionary<string, string> OperatorMethodNames = new Dictionary<string, string> {
["op_Implicit"] = "",
["op_Explicit"] = "",
["op_Addition"] = "+",
["op_Subtraction"] = "-",
["op_Multiply"] = "*",
["op_Division"] = "/",
["op_Modulus"] = "%",
["op_ExclusiveOr"] = "^",
["op_BitwiseAnd"] = "&",
["op_BitwiseOr"] = "|",
["op_LogicalAnd"] = "&&",
["op_LogicalOr"] = "||",
["op_Assign"] = "=",
["op_LeftShift"] = "<<",
["op_RightShift"] = ">>",
["op_SignedLeftShift"] = "", // Listed as N/A in the documentation
["op_SignedRightShift"] = "", // Listed as N/A in the documentation
["op_Equality"] = "==",
["op_Inequality"] = "!=",
["op_GreaterThan"] = ">",
["op_LessThan"] = "<",
["op_GreaterThanOrEqual"] = ">=",
["op_LessThanOrEqual"] = "<=",
["op_MultiplicationAssignment"] = "*=",
["op_SubtractionAssignment"] = "-=",
["op_ExclusiveOrAssignment"] = "^=",
["op_LeftShiftAssignment"] = "<<=", // Doesn't seem to be any right shift assignment`in documentation
["op_ModulusAssignment"] = "%=",
["op_AdditionAssignment"] = "+=",
["op_BitwiseAndAssignment"] = "&=",
["op_BitwiseOrAssignment"] = "|=",
["op_Comma"] = ",",
["op_DivisionAssignment"] = "*/=",
["op_Decrement"] = "--",
["op_Increment"] = "++",
["op_UnaryNegation"] = "-",
["op_UnaryPlus"] = "+",
["op_OnesComplement"] = "~"
};
}
}