Move StaticFieldRewriter to shared file
このコミットが含まれているのは:
1 親
7e5e695c2a
コミット
3e6aedaed1
5ファイル変更
+169
-232
検索条件に一致するファイルはありません
@@ -1,139 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Globalization;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Mappings;
|
||||
using ComputeSharp.SourceGeneration.SyntaxRewriters;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.SyntaxRewriters;
|
||||
namespace ComputeSharp.SourceGeneration.SyntaxRewriters;
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="CSharpSyntaxRewriter"/> type that processes C# static field to convert to HLSL static fields (possibly constant).
|
||||
/// </summary>
|
||||
internal sealed class StaticFieldRewriter : HlslSourceRewriter
|
||||
/// <inheritdoc/>
|
||||
partial class StaticFieldRewriter
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StaticFieldRewriter"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the target syntax tree.</param>
|
||||
/// <param name="discoveredTypes">The set of discovered custom types.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
public StaticFieldRewriter(
|
||||
SemanticModelProvider semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions,
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics)
|
||||
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics)
|
||||
/// <inheritdoc/>
|
||||
private partial void TrackKnownPropertyAccess(IMemberReferenceOperation operation, MemberAccessExpressionSyntax node)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
|
||||
public ExpressionSyntax? Visit(VariableDeclaratorSyntax? node)
|
||||
{
|
||||
if (node?.Initializer is EqualsValueClauseSyntax fieldInitializer)
|
||||
if (operation.Member.IsStatic)
|
||||
{
|
||||
return ((EqualsValueClauseSyntax)Visit(fieldInitializer))!.Value;
|
||||
}
|
||||
string typeName = operation.Member.ContainingType.GetFullMetadataName();
|
||||
|
||||
return null;
|
||||
// Special dispatch types are not supported from static constants
|
||||
DiagnosticDescriptor? descriptor = typeName switch
|
||||
{
|
||||
_ when typeName == typeof(ThreadIds).FullName || typeName == typeof(ThreadIds.Normalized).FullName => InvalidThreadIdsUsage,
|
||||
_ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage,
|
||||
_ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage,
|
||||
_ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage,
|
||||
_ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage,
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (descriptor is not null)
|
||||
{
|
||||
Diagnostics.Add(descriptor, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
|
||||
private partial void TrackKnownMethodInvocation(string metadataName)
|
||||
{
|
||||
var updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!;
|
||||
|
||||
if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) &&
|
||||
SemanticModel.For(node).GetOperation(node) is IMemberReferenceOperation operation)
|
||||
{
|
||||
// Track and replace constants
|
||||
if (operation is IFieldReferenceOperation fieldOperation &&
|
||||
fieldOperation.Field.IsConst &&
|
||||
fieldOperation.Type!.TypeKind != TypeKind.Enum)
|
||||
{
|
||||
if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral))
|
||||
{
|
||||
return ParseExpression(constantLiteral!);
|
||||
}
|
||||
|
||||
ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture);
|
||||
|
||||
var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName();
|
||||
var constantName = $"__{ownerTypeName}__{fieldOperation.Field.Name}";
|
||||
|
||||
return IdentifierName(constantName);
|
||||
}
|
||||
|
||||
if (HlslKnownProperties.TryGetMappedName(operation.Member.ToDisplayString(), out string? mapping))
|
||||
{
|
||||
if (operation.Member.IsStatic)
|
||||
{
|
||||
string typeName = operation.Member.ContainingType.GetFullMetadataName();
|
||||
|
||||
// Special dispatch types are not supported from static constants
|
||||
DiagnosticDescriptor? descriptor = typeName switch
|
||||
{
|
||||
_ when typeName == typeof(ThreadIds).FullName || typeName == typeof(ThreadIds.Normalized).FullName => InvalidThreadIdsUsage,
|
||||
_ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage,
|
||||
_ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage,
|
||||
_ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage,
|
||||
_ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage,
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (descriptor is not null)
|
||||
{
|
||||
Diagnostics.Add(descriptor, node);
|
||||
}
|
||||
}
|
||||
|
||||
// Rewrite static and instance mapped members
|
||||
return operation.Member.IsStatic switch
|
||||
{
|
||||
true => ParseExpression(mapping!),
|
||||
false => updatedNode.WithName(IdentifierName(mapping!))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return updatedNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
|
||||
{
|
||||
var updatedNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!;
|
||||
|
||||
if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation operation &&
|
||||
operation.TargetMethod is IMethodSymbol method &&
|
||||
method.IsStatic)
|
||||
{
|
||||
// Rewrite HLSL intrinsic methods
|
||||
if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping))
|
||||
{
|
||||
return updatedNode.WithExpression(ParseExpression(mapping!));
|
||||
}
|
||||
}
|
||||
|
||||
return updatedNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode? VisitArgument(ArgumentSyntax node)
|
||||
{
|
||||
var updatedNode = (ArgumentSyntax)base.VisitArgument(node)!;
|
||||
|
||||
updatedNode = updatedNode.WithRefKindKeyword(Token(SyntaxKind.None));
|
||||
|
||||
return updatedNode;
|
||||
// No special tracking is needed for DX12 compute shaders
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする