diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs
index 52452865..c70804ba 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs
@@ -97,20 +97,40 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
{
ObjectCreationExpressionSyntax updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!;
+ updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, SemanticModel.For(node), DiscoveredTypes);
+
+ return VisitObjectCreationExpression(node, updatedNode, updatedNode.Type);
+ }
+
+ ///
+ public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
+ {
+ ImplicitObjectCreationExpressionSyntax updatedNode = (ImplicitObjectCreationExpressionSyntax)base.VisitImplicitObjectCreationExpression(node)!;
+ TypeSyntax explicitType = IdentifierName("").ReplaceAndTrackType(node, SemanticModel.For(node), DiscoveredTypes);
+
+ return VisitObjectCreationExpression(node, updatedNode, explicitType);
+ }
+
+ ///
+ /// The original input instance.
+ /// The updated instance with tweaked syntax.
+ /// The for the object being created.
+ /// The rewritten for the object creation expression.
+ private SyntaxNode VisitObjectCreationExpression(BaseObjectCreationExpressionSyntax node, BaseObjectCreationExpressionSyntax updatedNode, TypeSyntax targetType)
+ {
+ // Emit a diagnostic if the object being created is not valid (ie. it's a managed type)
if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType: false } type)
{
Diagnostics.Add(InvalidObjectCreationExpression, node, type);
}
- updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, SemanticModel.For(node), DiscoveredTypes);
-
- // New objects use the default HLSL cast syntax, eg. (float4)0
+ // Mutate the syntax like with explicit object creation expressions
if (updatedNode.ArgumentList!.Arguments.Count == 0)
{
- return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
+ return CastExpression(targetType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
}
- // Add explicit casts for matrix constructors to help the overload resolution
+ // Add explicit casts like with the explicit object creation expressions above
if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol matrixType &&
HlslKnownTypes.IsMatrixType(matrixType.GetFullyQualifiedMetadataName()))
{
@@ -125,43 +145,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
}
}
- return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList!);
- }
-
- ///
- public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
- {
- ImplicitObjectCreationExpressionSyntax updatedNode = (ImplicitObjectCreationExpressionSyntax)base.VisitImplicitObjectCreationExpression(node)!;
-
- if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType: false } type)
- {
- Diagnostics.Add(InvalidObjectCreationExpression, node, type);
- }
-
- TypeSyntax explicitType = IdentifierName("").ReplaceAndTrackType(node, SemanticModel.For(node), DiscoveredTypes);
-
- // Mutate the syntax like with explicit object creation expressions
- if (updatedNode.ArgumentList!.Arguments.Count == 0)
- {
- return CastExpression(explicitType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
- }
-
- // Add explicit casts like with the explicit object creation expressions above
- if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol matrixType &&
- HlslKnownTypes.IsMatrixType(matrixType.GetFullyQualifiedMetadataName()))
- {
- for (int i = 0; i < node.ArgumentList.Arguments.Count; i++)
- {
- IArgumentOperation argumentOperation = (IArgumentOperation)SemanticModel.For(node).GetOperation(node.ArgumentList.Arguments[i])!;
- INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter!.Type;
-
- updatedNode = updatedNode.ReplaceNode(
- updatedNode.ArgumentList.Arguments[i].Expression,
- CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression));
- }
- }
-
- return InvocationExpression(explicitType, updatedNode.ArgumentList);
+ return InvocationExpression(targetType, updatedNode.ArgumentList!);
}
///