Remove code duplication in object creation rewriting
このコミットが含まれているのは:
1 親
c9db4e9256
コミット
3392dbea49
1ファイル変更
+26
-42
@@ -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);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="VisitObjectCreationExpression(ObjectCreationExpressionSyntax)"/>
|
||||
/// <param name="node">The original input <see cref="BaseObjectCreationExpressionSyntax"/> instance.</param>
|
||||
/// <param name="updatedNode">The updated <see cref="BaseObjectCreationExpressionSyntax"/> instance with tweaked syntax.</param>
|
||||
/// <param name="targetType">The <see cref="TypeSyntax"/> for the object being created.</param>
|
||||
/// <returns>The rewritten <see cref="SyntaxNode"/> for the object creation expression.</returns>
|
||||
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!);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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!);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする