diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs index 57855ccb..af8e3dc9 100644 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs @@ -7,6 +7,35 @@ namespace System.Reflection.Emit /// internal static class ILGeneratorExtensions { + /// + /// Puts the appropriate or instruction to unbox/cast a value onto the stream of instructions + /// + /// The input instance to use to emit instructions + /// The type of value to convert + public static void EmitCastOrUnbox(this ILGenerator il, Type type) + { + il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type); + } + + /// + /// Puts the appropriate , , or instruction to read a member onte the stream of instructions + /// + /// The input instance to use to emit instructions + /// The member to read + public static void EmitReadMember(this ILGenerator il, MemberInfo member) + { + switch (member) + { + case FieldInfo field: + il.Emit(field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, field); + break; + case PropertyInfo property when property.CanRead: + il.EmitCall(property.GetMethod.IsStatic ? OpCodes.Call : OpCodes.Callvirt, property.GetMethod, null); + break; + default: throw new ArgumentException($"The input {member.GetType()} instance can't be read"); + } + } + /// /// Puts the appropriate , and instructions to advance a reference onto the stream of instructions /// diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs index db9df14d..a3858ec0 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs @@ -88,18 +88,13 @@ namespace ComputeSharp.Shaders.Translation.Models if (!IsStatic) { il.Emit(OpCodes.Ldarg_0); - - Type unboxType = hierarchy[0].DeclaringType; - il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType); + il.EmitCastOrUnbox(hierarchy[0].DeclaringType); } // Unroll the member access foreach (ReadableMember member in hierarchy) { - // Get the member value with the appropriate method - if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null); - else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field); - else throw new InvalidOperationException("Field and property can't both be null at the same time"); + il.EmitReadMember(member); } // Box the value, if needed diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs index 9883fd52..a18e0d12 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs @@ -14,14 +14,9 @@ namespace ComputeSharp.Shaders.Translation.Models private readonly string Id; /// - /// The object wrapped by the current instance, if present + /// The object wrapped by the current instance /// - private readonly FieldInfo? Field; - - /// - /// The object wrapped by the current instance, if present - /// - private readonly PropertyInfo? Property; + private readonly MemberInfo Member; /// /// Creates a new instance with the given parameters @@ -30,22 +25,23 @@ namespace ComputeSharp.Shaders.Translation.Models private ReadableMember(MemberInfo memberInfo) { // General properties + Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{memberInfo.Name}"; + Member = memberInfo; Name = memberInfo.Name; - Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{Name}"; DeclaringType = memberInfo.DeclaringType; // Type specific properties if (memberInfo is FieldInfo fieldInfo) { - Field = fieldInfo; - IsStatic = Field.IsStatic; - MemberType = Field.FieldType; + Member = fieldInfo; + IsStatic = fieldInfo.IsStatic; + MemberType = fieldInfo.FieldType; } else if (memberInfo is PropertyInfo propertyInfo) { - Property = propertyInfo; - IsStatic = Property.GetMethod.IsStatic; - MemberType = Property.PropertyType; + Member = propertyInfo; + IsStatic = propertyInfo.GetMethod.IsStatic; + MemberType = propertyInfo.PropertyType; } else throw new InvalidOperationException("Field and property can't both be null at the same time"); } @@ -81,5 +77,11 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// The input object to wrap public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(property); + + /// + /// Converts a object into a instance + /// + /// The input object to unwrap + public static implicit operator MemberInfo(ReadableMember member) => member.Member; } }