Guess expression return types

This commit is contained in:
Yoshi Askharoun
2025-08-02 21:37:15 -05:00
parent 3a6dfd9f49
commit f82630c6c1
3 changed files with 49 additions and 1 deletions
+8
View File
@@ -20,6 +20,14 @@ public record QualifiedTypeName(string NamespacePrefix, string TypeName) : AsmIt
else
return $"{NamespacePrefix}:{TypeName}";
}
public static QualifiedTypeName Parse(string str)
{
var separatorIndex = str.IndexOf(':');
return separatorIndex < 0
? new(null, str)
: new(str[0..separatorIndex], str[(separatorIndex + 1)..]);
}
}
public record Program
+14
View File
@@ -74,6 +74,15 @@ internal class DecompileContext
public TypeSchema GetImportedType(Operand op) => ImportTables.TypeImports[(ushort)op.Value];
public TypeSchema GetImportedType(QualifiedTypeName typeName)
{
var uri = MapPrefixToNamespace(typeName.NamespacePrefix);
return ImportTables.TypeImports
.Where(t => t.Name == typeName.TypeName || t.AlternateName == typeName.TypeName)
.OrderBy(t => t.Owner.Uri == uri ? 0 : 1)
.First();
}
public MethodSchema GetImportedMethod(Operand op) => ImportTables.MethodImports[(ushort)op.Value];
public PropertySchema GetImportedProperty(Operand op) => ImportTables.PropertyImports[(ushort)op.Value];
@@ -103,6 +112,11 @@ internal class DecompileContext
return prefix;
}
public string MapPrefixToNamespace(string prefix)
{
return _uriAliasMap.First(kvp => kvp.Value == prefix).Key;
}
public QualifiedTypeName GetQualifiedName(TypeSchema schema)
{
var prefix = MapNamespaceToPrefix(schema.Owner.Uri);
+27 -1
View File
@@ -1,4 +1,7 @@
using Microsoft.Iris.Asm;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Iris.Asm;
using Microsoft.Iris.Asm.Models;
using Microsoft.Iris.Markup;
namespace Microsoft.Iris.DecompXml.Mock;
@@ -19,6 +22,10 @@ internal record IrisObject(object Object, TypeSchema Type)
{
type ??= UIXTypes.MapIDToType(UIXTypeID.Null);
}
else if (objIn is ExpressionSyntax expr)
{
type ??= GuessExpressionReturnType(expr, context);
}
if (objIn is Disassembler.RawConstantInfo constantInfo)
{
@@ -30,4 +37,23 @@ internal record IrisObject(object Object, TypeSchema Type)
return new(obj, type);
}
private static TypeSchema GuessExpressionReturnType(ExpressionSyntax expr, DecompileContext ctx)
{
if (expr is MemberAccessExpressionSyntax memberAccessExpression)
{
// TODO: Handle member access on instance methods
var sourceExpr = (IdentifierNameSyntax)memberAccessExpression.Expression;
var sourceTypeName = QualifiedTypeName.Parse(sourceExpr.ToString());
var sourceType = ctx.GetImportedType(sourceTypeName);
var memberName = memberAccessExpression.TryGetInferredMemberName();
var property = sourceType.FindPropertyDeep(memberName);
if (property is not null)
return property.PropertyType;
}
return null;
}
}