/// <inheritdoc/> public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { var updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node) !; if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType : false } type) { Context.ReportDiagnostic(InvalidObjectCreationExpression, node, type); } updatedNode = updatedNode.ReplaceAndTrackType(updatedNode.Type, node, SemanticModel.For(node), DiscoveredTypes); // New objects use the default HLSL cast syntax, eg. (float4)0 if (updatedNode.ArgumentList !.Arguments.Count == 0) { return(CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))); } // Add explicit casts for matrix constructors to help the overload resolution if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol matrixType && HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName())) { 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(updatedNode.Type, updatedNode.ArgumentList !)); }
/// <inheritdoc/> public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node) { var updatedNode = (ImplicitObjectCreationExpressionSyntax)base.VisitImplicitObjectCreationExpression(node) !; if (SemanticModel.For(node).GetTypeInfo(node).Type is ITypeSymbol { IsUnmanagedType : false } type) { Context.ReportDiagnostic(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.GetFullMetadataName())) { 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)); }