Files
ZuneUIXTools/libs/UIX.DecompXml/Mock/IrisObject.cs
T

69 lines
2.2 KiB
C#
Raw Normal View History

2025-08-02 21:37:10 -05:00
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;
internal record IrisObject(object Object, TypeSchema Type)
{
2025-07-17 14:30:19 -05:00
public static IrisObject Create(object objIn, TypeSchema type, DecompileContext context)
{
object obj = objIn;
if (objIn is IrisObject irisObj)
{
return irisObj.Type is null
? (irisObj with { Type = type })
: irisObj;
}
2025-07-20 02:03:16 -05:00
else if (objIn is null)
{
type ??= UIXTypes.MapIDToType(UIXTypeID.Null);
}
2025-08-02 21:37:10 -05:00
else if (objIn is ExpressionSyntax expr)
{
type ??= GuessExpressionReturnType(expr, context);
2025-08-04 21:08:02 -05:00
return new(obj, type);
2025-08-02 21:37:10 -05:00
}
if (objIn is Disassembler.RawConstantInfo constantInfo)
{
obj = constantInfo.Value;
type ??= constantInfo.Type;
}
2025-07-17 14:30:19 -05:00
type ??= Disassembler.GuessTypeSchema(obj.GetType(), context.LoadResult);
return new(obj, type);
}
2025-08-02 21:37:10 -05:00
private static TypeSchema GuessExpressionReturnType(ExpressionSyntax expr, DecompileContext ctx)
{
2025-08-04 21:08:02 -05:00
if (expr is ParenthesizedExpressionSyntax parenExpr)
expr = parenExpr.Expression;
2025-08-02 21:37:10 -05:00
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;
}
2025-08-04 21:41:16 -05:00
else if (expr is CastExpressionSyntax castExpr)
{
var castTargetName = QualifiedTypeName.Parse(castExpr.Type.ToString());
return ctx.GetImportedType(castTargetName);
}
2025-08-02 21:37:10 -05:00
return null;
}
}