From 39c4e28d5b21524efd5f0a844ecf2223d2d3da46 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 14:33:33 +0200 Subject: [PATCH] More code refactoring --- .../Extensions/SyntaxNodeExtensions.cs | 67 +++++++++++++++++++ .../ShaderSyntaxRewriter.cs | 3 +- .../SyntaxNodeExtensions.cs | 45 ------------- 3 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs delete mode 100644 src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs new file mode 100644 index 00000000..d7739008 --- /dev/null +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -0,0 +1,67 @@ +using System.Diagnostics.Contracts; +using System.Linq; +using ComputeSharp.Shaders.Mappings; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace ComputeSharp.Shaders.Extensions +{ + /// + /// A with some extension methods for C# syntax nodes + /// + public static class SyntaxNodeExtensions + { + /// + /// Checks a value and replaces the value type to be HLSL compatible, if needed + /// + /// The type of the input instance + /// The input to check and modify if needed + /// The to use for the input node + /// A instance that represents a type compatible with HLSL + [Pure] + public static TRoot ReplaceType(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode + { + string value = HlslKnownTypes.GetMappedName(type.ToString()); + + // If the HLSL mapped full type name equals the original type, just return the input node + if (value == type.ToString()) return node; + + // Process and return the type name + TypeSyntax newType = SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia()); + return node.ReplaceNode(type, newType); + } + + /// + /// Checks a instance and replaces it to be HLSL compatible, if needed + /// + /// The input to check and modify if needed + /// The to use to load symbols for the input node + /// A instance that is compatible with HLSL + [Pure] + public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel) + { + SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); + SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); + + ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); + + // If the input member has no symbol or is not a field, property or method, just return it + if (memberSymbol is null || + memberSymbol.Kind != SymbolKind.Field && + memberSymbol.Kind != SymbolKind.Property && + memberSymbol.Kind != SymbolKind.Method) + { + return node; + } + + // Process the input node if it's a known method invocation + if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string value) + { + return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); + } + + return node; + } + } +} diff --git a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs index 1f81ab64..17419fc0 100644 --- a/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/ShaderSyntaxRewriter.cs @@ -1,4 +1,5 @@ -using Microsoft.CodeAnalysis; +using ComputeSharp.Shaders.Extensions; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs deleted file mode 100644 index 7660be33..00000000 --- a/src/ComputeSharp.Shaders/SyntaxNodeExtensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Linq; -using ComputeSharp.Shaders.Mappings; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace ComputeSharp.Shaders -{ - public static class SyntaxNodeExtensions - { - public static TRoot ReplaceType(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode - { - string value = HlslKnownTypes.GetMappedName(type.ToString()); - - if (value == type.ToString()) - { - return node; - } - else - { - return node.ReplaceNode(type, SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia())); - } - } - - public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel) - { - SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); - SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); - - ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); - - if (memberSymbol is null || (memberSymbol.Kind != SymbolKind.Field && memberSymbol.Kind != SymbolKind.Property && memberSymbol.Kind != SymbolKind.Method)) - { - return node; - } - - if (HlslKnownMethods.TryGetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol) is string value) - { - return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - return node; - } - } -}