コード例 #1
0
    /// <inheritdoc/>
    public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
    {
        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))
            {
                // Rewrite static and instance mapped members
                return(operation.Member.IsStatic switch
                {
                    true => ParseExpression(mapping !),
                    false => updatedNode.WithName(IdentifierName(mapping !))
                });
コード例 #2
0
    /// <inheritdoc/>
    public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
    {
        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 !))
                });