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

75 lines
2.9 KiB
C#
Raw Normal View History

2025-07-18 00:23:23 -05:00
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Iris.Asm;
using Microsoft.Iris.Markup;
using System;
2025-07-21 01:46:38 -05:00
using System.Collections.Generic;
2025-07-18 00:23:23 -05:00
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
2025-07-17 00:46:05 -05:00
namespace Microsoft.Iris.DecompXml.Mock;
2025-07-20 10:24:49 -05:00
internal static class IrisExpression
2025-07-17 00:46:05 -05:00
{
2025-07-21 01:46:38 -05:00
private static Dictionary<ulong, SyntaxKind> _predefinedTypeMap = null;
2025-07-18 00:23:23 -05:00
public static ExpressionSyntax ToSyntax(object obj, DecompileContext context)
{
return obj switch
{
null => LiteralExpression(SyntaxKind.NullLiteralExpression),
int intValue => LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(intValue)),
2025-07-20 10:24:35 -05:00
bool boolValue => LiteralExpression(boolValue ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression),
2025-07-18 00:23:23 -05:00
string strValue => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(strValue)),
IStringEncodable strEnc => ParseExpression(strEnc.EncodeString()),
2025-07-20 10:24:49 -05:00
Disassembler.RawConstantInfo constantInfo => ToSyntax(constantInfo.Value, constantInfo.Type, context),
IrisObject irisObj => ToSyntax(irisObj.Object, irisObj.Type, context),
2025-07-18 00:43:22 -05:00
SymbolReference symbolRef => ToSyntax(symbolRef),
TypeSchema typeSchema => ToSyntax(typeSchema, context),
2025-07-18 00:23:23 -05:00
ExpressionSyntax exprSyn => exprSyn,
_ => IdentifierName(obj.ToString())
};
}
2025-07-18 00:43:22 -05:00
2025-07-21 01:46:38 -05:00
public static TypeSyntax ToSyntax(TypeSchema type, DecompileContext context)
{
if (TryMapPredefinedType(type, out var kind))
return PredefinedType(Token(kind));
return IdentifierName(context.GetQualifiedName(type).ToString());
}
2025-07-18 00:43:22 -05:00
public static IdentifierNameSyntax ToSyntax(SymbolReference symbolRef) =>
IdentifierName(symbolRef.Symbol);
2025-07-20 10:24:49 -05:00
public static ExpressionSyntax ToSyntax(object obj, TypeSchema type, DecompileContext context)
{
return obj switch
{
Enum _ => MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(context.GetQualifiedName(type).ToString()),
IdentifierName(obj.ToString())
),
_ => ToSyntax(obj, context),
};
}
2025-07-21 01:46:38 -05:00
private static bool TryMapPredefinedType(TypeSchema type, out SyntaxKind kind)
{
_predefinedTypeMap ??= new()
{
[UIXTypes.MapIDToType(UIXTypeID.Void).UniqueId] = SyntaxKind.VoidKeyword,
[UIXTypes.MapIDToType(UIXTypeID.Int32).UniqueId] = SyntaxKind.IntKeyword,
[UIXTypes.MapIDToType(UIXTypeID.Int64).UniqueId] = SyntaxKind.LongKeyword,
[UIXTypes.MapIDToType(UIXTypeID.String).UniqueId] = SyntaxKind.StringKeyword,
[UIXTypes.MapIDToType(UIXTypeID.Boolean).UniqueId] = SyntaxKind.BoolKeyword,
};
return _predefinedTypeMap.TryGetValue(type.UniqueId, out kind);
}
2025-07-17 00:46:05 -05:00
}