From 9224485334ddc1f5ac096537223ef4af953f3486 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 26 Nov 2020 01:06:25 +0100 Subject: [PATCH] Removed Roslyn dependency from main library --- .../ComputeSharp.Shaders.csproj | 4 - .../Extensions/SyntaxNodeExtensions.cs | 271 ------------------ .../Renderer/Models/Functions/FunctionInfo.cs | 75 ----- .../Models/Functions/LocalFunctionInfo.cs | 29 -- .../Models/Functions/ParameterInfo.cs | 52 ---- .../Renderer/Models/ShaderInfo.cs | 7 +- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 4 +- .../Translation/ShaderLoader{T}.cs | 10 +- .../Translation/ShaderSyntaxRewriter.cs | 180 ------------ 9 files changed, 9 insertions(+), 623 deletions(-) delete mode 100644 src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs delete mode 100644 src/ComputeSharp.Shaders/Renderer/Models/Functions/FunctionInfo.cs delete mode 100644 src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs delete mode 100644 src/ComputeSharp.Shaders/Renderer/Models/Functions/ParameterInfo.cs delete mode 100644 src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs diff --git a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj index 3b059c63..6221de2e 100644 --- a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj +++ b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj @@ -6,10 +6,6 @@ true - - - - diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs deleted file mode 100644 index fe175b91..00000000 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ /dev/null @@ -1,271 +0,0 @@ -using System; -using System.Diagnostics.Contracts; -using System.Globalization; -using System.Linq; -using System.Reflection; -using ComputeSharp; -using ComputeSharp.Shaders.Mappings; -using ComputeSharp.Shaders.Translation.Models; -using Microsoft.Toolkit.Diagnostics; - -namespace Microsoft.CodeAnalysis.CSharp.Syntax -{ - /// - /// A with some extension methods for C# syntax nodes - /// - internal 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 - /// The info on parsed static members, if any - /// The info on parsed static methods, if any - /// A instance that is compatible with HLSL - [Pure] - public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, ReadableMember MemberInfo)? variable, out (string Name, MethodInfo MethodInfo)? method) - { - // Set the out parameters to null, replace them later on if needed - variable = null; - method = null; - - SymbolInfo containingMemberSymbolInfo; - ISymbol? memberSymbol; - try - { - containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); - SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); - memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); - } - catch (ArgumentException) - { - // Member access on a captured HLSL-compatible field or property - string name = node.Name.ToFullString(); - if (name == "X" || name == "Y" || name == "Z" || name == "W") return node.WithName(SyntaxFactory.IdentifierName(name.ToLowerInvariant())); - return node; - } - - // If the input member has no symbol, try to load it manually - if (memberSymbol is null) - { - string expression = node.WithoutTrivia().ToFullString(); - int index = expression.LastIndexOf(Type.Delimiter); - string fullname = expression.Substring(0, index); - if (!(AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(fullname)).FirstOrDefault(t => t != null) is Type type)) - { - // The current node can't possibly represent a field or a property, so just return it - return node; - } - - // Try to get the target static field or property, if present - string name = expression.Substring(index + 1, expression.Length - fullname.Length - 1); - MemberInfo[] memberInfos = type.GetMember(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - if (memberInfos.Length == 0) return node; - bool isReadonly; - ReadableMember memberInfo; - switch (memberInfos.First()) - { - case FieldInfo fieldInfo: - isReadonly = fieldInfo.IsInitOnly; - memberInfo = fieldInfo; - break; - case PropertyInfo propertyInfo: - isReadonly = !propertyInfo.CanWrite; - memberInfo = propertyInfo; - break; - case MethodInfo methodInfo: - string methodName = $"{methodInfo.DeclaringType!.Name}_{methodInfo.Name}"; - method = (methodName, methodInfo); - return SyntaxFactory.IdentifierName(methodName).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!; - } - - // Handle the loaded info - return ProcessStaticMember(node, memberInfo, isReadonly, ref variable); - } - - // If the input member is not a field, property or method, just return it - if (memberSymbol.Kind != SymbolKind.Field && - memberSymbol.Kind != SymbolKind.Property && - memberSymbol.Kind != SymbolKind.Method) - { - return node; - } - - // Handle static members as a special case - if (memberSymbol.IsStatic && ( - memberSymbol.Kind == SymbolKind.Field || - memberSymbol.Kind == SymbolKind.Property || - memberSymbol.Kind == SymbolKind.Method)) - { - // Get the containing type - string - typeFullname = memberSymbol.ContainingType.ToString()!, - assemblyFullname = memberSymbol.ContainingAssembly.ToString()!; - Type memberDeclaringType = Type.GetType($"{typeFullname}, {assemblyFullname}")!; - - // Static field or property - if (memberSymbol.Kind == SymbolKind.Field || memberSymbol.Kind == SymbolKind.Property) - { - bool isReadonly; - ReadableMember memberInfo; - switch (memberSymbol.Kind) - { - case SymbolKind.Field: - FieldInfo fieldInfo = memberDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!; - isReadonly = fieldInfo.IsInitOnly; - memberInfo = fieldInfo; - break; - case SymbolKind.Property: - PropertyInfo propertyInfo = memberDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!; - isReadonly = !propertyInfo.CanWrite; - memberInfo = propertyInfo; - break; - default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!; - } - - // Handle the loaded info - return ProcessStaticMember(node, memberInfo, isReadonly, ref variable); - } - - // Static method - MethodInfo methodInfo = memberDeclaringType.GetMethod(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!; - string name = $"{methodInfo.DeclaringType!.Name}_{methodInfo.Name}"; - method = (name, methodInfo); - - return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - return node; - } - - /// - /// 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 - [Pure] - public static InvocationExpressionSyntax ReplaceInvocation(this InvocationExpressionSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable, out (string Name, MethodInfo MethodInfo)? method) - { - // Set the variable and method to null - variable = null; - method = null; - - // Explore the nested classes from bottom to top - if (declaringType.TryFindAncestorMember(node.Expression.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result)) - { - // If a static method or delegate is found, track it - string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}"; - switch (result.Value.MemberInfo) - { - case FieldInfo fieldInfo: - variable = (name, fieldInfo); - break; - case PropertyInfo propertyInfo: - 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: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!; - } - - // Return the mapped expression - return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - return node; - } - - /// - /// 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 - /// A instance that is compatible with HLSL - [Pure] - public static IdentifierNameSyntax ReplaceIdentifierName(this IdentifierNameSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable) - { - // Set the variable to null - variable = null; - - // Explore the nested classes from bottom to top - if (declaringType.TryFindAncestorMember(node.Identifier.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result)) - { - // If a static scalar or vector member is found, track it - string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}"; - switch (result.Value.MemberInfo) - { - case FieldInfo fieldInfo when HlslKnownTypes.IsKnownType(fieldInfo.FieldType): - variable = (name, fieldInfo); - break; - case PropertyInfo propertyInfo when HlslKnownTypes.IsKnownType(propertyInfo.PropertyType): - variable = (name, propertyInfo); - break; - default: return node; - } - - // Return the mapped expression - return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - return node; - } - - /// - /// Processes a loaded static member, either a field or a property - /// - /// The input to check and modify if needed - /// The wrapped member that needs to be processed - /// Indicates whether or not the target member is readonly - /// The info on parsed static members, if any - /// A instance that is compatible with HLSL - private static SyntaxNode ProcessStaticMember(SyntaxNode node, ReadableMember memberInfo, bool isReadonly, ref (string Name, ReadableMember MemberInfo)? variable) - { - // Constant replacement if the value is a readonly scalar value - if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberInfo.MemberType)) - { - LiteralExpressionSyntax expression = memberInfo.GetValue(null) switch - { - true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), - false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)), - Bool b when b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), - Bool b when !b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)), - IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))), - _ => ThrowHelper.ThrowArgumentException("Invalid field type") - }; - return expression.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - - // Captured member, treat it like any other captured variable in the closure - string name = $"{memberInfo.DeclaringType.Name}_{memberInfo.Name}"; - variable = (name, memberInfo); - return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); - } - } -} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Functions/FunctionInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Functions/FunctionInfo.cs deleted file mode 100644 index 9d77fa67..00000000 --- a/src/ComputeSharp.Shaders/Renderer/Models/Functions/FunctionInfo.cs +++ /dev/null @@ -1,75 +0,0 @@ -using ComputeSharp.Graphics.Extensions; -using System; -using System.Collections.Generic; - -namespace ComputeSharp.Shaders.Renderer.Models.Functions -{ - /// - /// A that contains info on a shader function - /// - internal sealed class FunctionInfo - { - /// - /// Gets the return type of the current function in the C# source - /// - public string FunctionCsharpReturnType { get; } - - /// - /// Gets the fullname of the current function in the C# source - /// - public string FunctionFullName { get; } - - /// - /// Gets the parameters of the function in the C# source - /// - public string FunctionCsharpParameters { get; } - - /// - /// Gets the return type of the current function - /// - public string ReturnType { get; } - - /// - /// Gets the name of the current function - /// - public string FunctionName { get; } - - /// - /// Gets the list of parameters for the current function - /// - public IReadOnlyList ParametersList { get; } - - /// - /// Gets the body of the current function - /// - public string FunctionBody { get; } - - /// - /// Creates a new instance with the specified parameters - /// - /// The concrete return type for the function - /// The fullname of the function in the C# source - /// The parameters of the function in the C# source - /// The return type of the current function - /// The name of the current function - /// The function parameters, if any - /// The current function - public FunctionInfo( - Type functionCsharpType, - string functionFullname, - string functionCsharpParameters, - string returnType, - string functionName, - IReadOnlyList parameters, - string functionBody) - { - FunctionCsharpReturnType = functionCsharpType.ToFriendlyString(); - FunctionFullName = functionFullname; - FunctionCsharpParameters = functionCsharpParameters; - ReturnType = returnType; - FunctionName = functionName; - ParametersList = parameters; - FunctionBody = functionBody; - } - } -} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs deleted file mode 100644 index 4dfd45f9..00000000 --- a/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace ComputeSharp.Shaders.Renderer.Models.Functions -{ - /// - /// A that contains info on a local shader function - /// - internal sealed class LocalFunctionInfo - { - /// - /// Gets the prototype for the current function - /// - public string FunctionPrototype => FunctionDeclaration.Split(Environment.NewLine)[0]; - - /// - /// Gets the declaration of the current function - /// - public string FunctionDeclaration { get; } - - /// - /// Creates a new instance with the specified parameters - /// - /// The declaration of the current function - public LocalFunctionInfo(string functionDeclaration) - { - FunctionDeclaration = functionDeclaration; - } - } -} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Functions/ParameterInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Functions/ParameterInfo.cs deleted file mode 100644 index a424aaf2..00000000 --- a/src/ComputeSharp.Shaders/Renderer/Models/Functions/ParameterInfo.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace ComputeSharp.Shaders.Renderer.Models.Functions -{ - /// - /// A that contains info on a function parameter - /// - internal sealed class ParameterInfo - { - /// - /// Gets the modifier to use for the current parameter - /// - public string ParameterModifier { get; } - - /// - /// Gets the type of the current parameter - /// - public string ParameterType { get; } - - /// - /// Gets the name to use for the current parameter - /// - public string ParameterName { get; } - - /// - /// Gets whether or not the current parameter is the last one for the parent function - /// - public bool IsLastParameter { get; } - - /// - /// Creates a new instance with the specified parameters - /// - /// The modifiers used in the current parameter - /// The type of the current parameter - /// The name of the current parameter - /// Indicates whether or not the current parameter is the last one - public ParameterInfo(IReadOnlyList parameterModifiers, string parameterType, string parameterName, bool last) - { - if (parameterModifiers.Count == 0) ParameterModifier = "in"; - else if (parameterModifiers.First().IsKind(SyntaxKind.OutKeyword)) ParameterModifier = "out"; - else if (parameterModifiers.Any(m => m.IsKind(SyntaxKind.RefKeyword)) && !parameterModifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword))) ParameterModifier = "inout"; - else ParameterModifier = "in"; - - ParameterType = parameterType; - ParameterName = parameterName; - IsLastParameter = last; - } - } -} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 31b0acfd..2b5aabad 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using ComputeSharp.Shaders.Renderer.Models.Fields; using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -using ComputeSharp.Shaders.Renderer.Models.Functions; #pragma warning disable CS8618 @@ -50,11 +49,11 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets or sets the list of static functions used by the shader. /// - public IReadOnlyList FunctionsList { get; init; } + public IReadOnlyList FunctionsList { get; init; } // TODO /// - /// Gets or sets the list of local (implicit) functions used by the shader. + /// Gets or sets the list of local functions used by the shader. /// - public IReadOnlyList LocalFunctionsList { get; init; } + public IReadOnlyList LocalFunctionsList { get; init; } } } diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 4bf34567..e24a17be 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -168,7 +168,7 @@ namespace ComputeSharp.Shaders ShaderLoader shaderLoader = ShaderLoader.Load(in shader); // Render the loaded shader - string shaderSource = ShaderRenderer.Instance.Render(new ShaderInfo + using var shaderSource = HlslShaderRenderer.Render(new ShaderInfo { BuffersList = shaderLoader.HslsBuffersInfo, FieldsList = shaderLoader.FieldsInfo, @@ -182,7 +182,7 @@ namespace ComputeSharp.Shaders }); // Compile the loaded shader to HLSL bytecode - IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource)); + IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource.WrittenSpan)); // Get the cached shader data shaderData = new CachedShader(shaderLoader, shaderBytecode); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index a65f7e77..a841ea1c 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -7,9 +7,7 @@ using System.Runtime.InteropServices; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; -using ComputeSharp.Shaders.Renderer.Models.Functions; using ComputeSharp.Shaders.Translation.Models; -using Microsoft.CodeAnalysis; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE; @@ -63,12 +61,12 @@ namespace ComputeSharp.Shaders.Translation /// /// The with the items for the shader functions. /// - private readonly List functionsInfo = new(); + private readonly List functionsInfo = new(); /// /// The with the items for the shader local functions. /// - private readonly List localFunctionsInfo = new(); + private readonly List localFunctionsInfo = new(); /// /// Creates a new instance. @@ -108,12 +106,12 @@ namespace ComputeSharp.Shaders.Translation /// /// Gets the collection of items for the shader. /// - public IReadOnlyList FunctionsInfo => this.functionsInfo; + public IReadOnlyList FunctionsInfo => this.functionsInfo; /// /// Gets the collection of items for the shader. /// - public IReadOnlyList LocalFunctionsInfo => this.localFunctionsInfo; + public IReadOnlyList LocalFunctionsInfo => this.localFunctionsInfo; /// /// Loads and processes an input shadeer diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs deleted file mode 100644 index 4e62fb13..00000000 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using ComputeSharp.Shaders.Translation.Models; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace ComputeSharp.Shaders.Translation -{ - /// - /// A custom that processes C# methods to convert to HLSL - /// - internal class ShaderSyntaxRewriter : CSharpSyntaxRewriter - { - /// - /// The instance to use to rewrite the decompiled code - /// - private readonly SemanticModel SemanticModel; - - /// - /// The declaring type that hosts the root from which the current syntax tree is inspected - /// - private readonly Type DeclaringType; - - /// - /// Creates a new instance with the specified parameters - /// - /// The instance to use to rewrite the decompiled code - /// The declaring type that hosts the root from which the current syntax tree is inspected - public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType) - { - SemanticModel = semanticModel; - DeclaringType = declaringType; - } - - private readonly Dictionary _StaticMembers = new Dictionary(); - - /// - /// Gets the mapping of captured static members used by the target code - /// - public IReadOnlyDictionary StaticMembers => _StaticMembers; - - private readonly Dictionary _StaticMethods = new Dictionary(); - - /// - /// Gets the mapping of captured static methods used by the target code - /// - public IReadOnlyDictionary StaticMethods => _StaticMethods; - - /// - public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node) - { - node = (IdentifierNameSyntax)base.VisitIdentifierName(node)!; - node = node.ReplaceIdentifierName(DeclaringType, out var variable); - - // Register the captured member, if any - if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) - { - _StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo); - } - - return node; - } - - /// - public override SyntaxNode VisitParameter(ParameterSyntax node) - { - node = (ParameterSyntax)base.VisitParameter(node)!; - node = node.WithAttributeLists(default); - node = node.ReplaceType(node.Type!); - - return node; - } - - /// - public override SyntaxNode VisitArgument(ArgumentSyntax node) - { - node = (ArgumentSyntax)base.VisitArgument(node)!; - - if (node.RefKindKeyword.IsKind(SyntaxKind.RefKeyword)) - { - node = node.WithRefKindKeyword(SyntaxFactory.Token(SyntaxKind.None)); - } - - return node; - } - - /// - public override SyntaxNode VisitCastExpression(CastExpressionSyntax node) - { - node = (CastExpressionSyntax)base.VisitCastExpression(node)!; - - return node.ReplaceType(node.Type); - } - - /// - public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) - { - node = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node)!; - - return node.ReplaceType(node.Declaration.Type); - } - - /// - public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) - { - node = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!; - node = node.ReplaceType(node.Type); - - if (node.ArgumentList!.Arguments.Count == 0) - { - return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); - } - - return SyntaxFactory.InvocationExpression(node.Type, node.ArgumentList); - } - - /// - public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node) - { - node = (DefaultExpressionSyntax)base.VisitDefaultExpression(node)!; - node = node.ReplaceType(node.Type); - - return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); - } - - /// - public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) - { - node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!; - SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable, out var method); - - // Register the captured members, if any - if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) - { - _StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo); - } - if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name)) - { - _StaticMethods.Add(method.Value.Name, method.Value.MethodInfo); - } - - return syntaxNode; - } - - /// - public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node) - { - node = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!; - node = node.ReplaceInvocation(DeclaringType, out var variable, out var method); - - // Register the captured members, if any - if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name)) - { - _StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo); - } - if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name)) - { - _StaticMethods.Add(method.Value.Name, method.Value.MethodInfo); - } - - return node; - } - - /// - public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) - { - node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!; - - // Replace the return type node, if needed - if (!node.ReturnType.ToString().Equals("void")) - { - return node.ReplaceType(node.ReturnType); - } - - return node; - } - } -}