From c1cedcd34f243b06ca3f78847d5cd2b5b31158c1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 25 Nov 2020 20:20:06 +0100 Subject: [PATCH] Added support for target-typed new() expressions --- .../Extensions/SyntaxNodeExtensions.cs | 23 +++++++++++++++---- .../SyntaxRewriters/ShaderSourceRewriter.cs | 16 +++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs index e8eaef5a..0b5c68e3 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs @@ -22,14 +22,29 @@ namespace ComputeSharp.SourceGenerators.Extensions /// /// Checks a value and replaces the value type to be HLSL compatible, if needed. /// - /// The type of the input instance. + /// The type of the input instance. /// The input to check and modify if needed. - /// The target node to replace. - /// The source to use to get type into from . + /// The source to use to get type into from . /// The instance with info on the input tree. /// A instance that represents a type compatible with HLSL. [Pure] - public static TRoot ReplaceType(this TRoot node, TypeSyntax targetType, TypeSyntax sourceType, SemanticModel semanticModel) + public static TRoot ReplaceType(this TRoot node, SyntaxNode sourceType, SemanticModel semanticModel) + where TRoot : TypeSyntax + { + return node.ReplaceType(node, sourceType, semanticModel); + } + + /// + /// 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 target node to replace. + /// The source to use to get type into from . + /// The instance with info on the input tree. + /// A instance that represents a type compatible with HLSL. + [Pure] + public static TRoot ReplaceType(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel) where TRoot : SyntaxNode { if (semanticModel.GetTypeInfo(sourceType).Type is ITypeSymbol typeSymbol && diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 80786795..d971ce2c 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -78,6 +78,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList); } + /// + public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node) + { + var updatedNode = (ImplicitObjectCreationExpressionSyntax)base.VisitImplicitObjectCreationExpression(node)!; + + TypeSyntax explicitType = IdentifierName("").ReplaceType(node, this.semanticModel); + + // Mutate the syntax like with explicit object creation expressions + if (updatedNode.ArgumentList!.Arguments.Count == 0) + { + return CastExpression(explicitType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); + } + + return InvocationExpression(explicitType, updatedNode.ArgumentList); + } + /// public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node) {