From 36d9a54d1b8665ded9423383097d092d98f615b8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 23:55:01 +0200 Subject: [PATCH] Minor code refactoring --- .../Extensions/SyntaxNodeExtensions.cs | 15 +++++------- .../Extensions/TypeExtensions.cs | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index db3dd4fd..6dd571b7 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -172,6 +172,7 @@ namespace ComputeSharp.Shaders.Extensions /// Checks a instance and replaces it to be HLSL compatible, if needed /// /// The input to check and modify if needed + /// The in which the input syntax tree is declared in /// The info on parsed static members, if any /// The info on parsed static methods, if any /// A instance that is compatible with HLSL @@ -183,15 +184,11 @@ namespace ComputeSharp.Shaders.Extensions method = null; // Explore the nested classes from bottom to top - string cSharpName = node.Expression.ToString(); - while ((declaringType = declaringType.DeclaringType) != null) + if (declaringType.TryFindAncestorMember(node.Expression.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result)) { - MemberInfo[] memberInfos = declaringType.GetMember(cSharpName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - if (!(memberInfos.FirstOrDefault() is MemberInfo first)) continue; - // If a static method or delegate is found, track it - string name = $"{declaringType.Name}_{first.Name}"; - switch (first) + string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}"; + switch (result.Value.MemberInfo) { case FieldInfo fieldInfo: variable = (name, fieldInfo); @@ -200,10 +197,10 @@ namespace ComputeSharp.Shaders.Extensions variable = (name, propertyInfo); break; case MethodInfo methodInfo: - + method = (name, methodInfo); return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - default: throw new InvalidOperationException($"Invalid symbol kind: {memberInfos.First().GetType()}"); + default: throw new InvalidOperationException($"Invalid symbol kind: {result.Value.MemberInfo.GetType()}"); } // Return the mapped expression diff --git a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs index 77f693d5..467ccd8a 100644 --- a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; @@ -29,6 +30,29 @@ namespace ComputeSharp.Shaders.Extensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsStatelessDelegateContainer(this Type type) => type.GetFields(BindingFlags.Static | BindingFlags.Public).Length > 0; + /// + /// Tries to find an ancestor member starting from the input + /// + /// The input type to start the search from + /// The name of the member to retrieve + /// A bitmask comprised of one or more that specify how the search is conducted + /// when a member with the given name is found, otherwise + public static bool TryFindAncestorMember(this Type type, string name, BindingFlags bindingAttr, [NotNullWhen(true)] out (Type DeclaringType, MemberInfo MemberInfo)? result) + { + while ((type = type.DeclaringType) != null) + { + MemberInfo[] memberInfos = type.GetMember(name, bindingAttr); + if (memberInfos.Length > 0) + { + result = (type, memberInfos[0]); + return true; + } + } + + result = null; + return false; + } + /// /// The mapping of supported known types to display string ///