Merge pull request #252 from Sergio0694/feature/ignore-explicit-interface-implementations

Allow custom base interfaces and explicit interface members
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-04-29 13:33:52 +02:00
コミット 53a9e509d2
13ファイル変更
+195 -32

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -59,7 +59,7 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator
{
return (
from fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>()
where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false }
where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }
let typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
select new ParameterInfo(typeName, fieldSymbol.Name)).ToImmutableArray();
}
+10 -7
ファイルの表示
@@ -413,31 +413,34 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a property declaration.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor PropertyDeclaration = new DiagnosticDescriptor(
public static readonly DiagnosticDescriptor InvalidPropertyDeclaration = new DiagnosticDescriptor(
id: "CMPSD2D0031",
title: "Property declaration",
messageFormat: "Property declarations cannot be used in a compute shader",
title: "Invalid property declaration",
messageFormat: "The D2D1 shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Property declarations cannot be used in a compute shader.",
description: "Property declarations (except for stateless properties explicitly implementing an interface property) cannot be used in a D2D1 shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader with a root signature that is too large.
/// <para>
/// Format: <c>"The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources"</c>.
/// Format: <c>"The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new DiagnosticDescriptor(
id: "CMPSD2D0032",
title: "Shader dispatch data size exceeded",
messageFormat: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources",
messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources.",
description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values and resources.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
@@ -45,7 +45,7 @@ partial class ID2D1ShaderGenerator
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Properties are not supported
DetectAndReportPropertyDeclarations(builder, structDeclarationSymbol);
DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -240,6 +240,12 @@ partial class ID2D1ShaderGenerator
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0;
// Except for the entry point, ignore explicit interface implementations
if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
{
continue;
}
// Create the source rewriter for the current method
ShaderSourceRewriter shaderSourceRewriter = new(
structDeclarationSymbol,
@@ -366,15 +372,25 @@ partial class ID2D1ShaderGenerator
}
/// <summary>
/// Finds and reports all declared properties in a shader.
/// Finds and reports all invalid declared properties in a shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
private static void DetectAndReportPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType<IPropertySymbol>())
foreach (var memberSymbol in structDeclarationSymbol.GetMembers())
{
diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol);
// Detect properties that are not explicit interface implementations
if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true })
{
diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol);
}
// Detect properties causing a field to be generated
if (memberSymbol is IFieldSymbol { AssociatedSymbol: IPropertySymbol associatedProperty })
{
diagnostics.Add(InvalidPropertyDeclaration, associatedProperty, structDeclarationSymbol, associatedProperty);
}
}
}
@@ -43,9 +43,7 @@ partial class ID2D1ShaderGenerator
foreach (
IFieldSymbol fieldSymbol in
from fieldSymbol in currentTypeSymbol.GetMembers().OfType<IFieldSymbol>()
where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } &&
!fieldSymbol.IsConst && !fieldSymbol.IsStatic &&
!fieldSymbol.IsFixedSizeBuffer
where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }
select fieldSymbol)
{
string fieldName = fieldSymbol.Name;
+1 -1
ファイルの表示
@@ -18,7 +18,7 @@ partial class ID2D1ShaderGenerator
/// <returns>Whether or not <paramref name="typeSymbol"/> implements <paramref name="d2D1PixelShaderSymbol"/>.</returns>
private static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol)
{
foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces)
foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces)
{
if (interfaceSymbol.Name == nameof(ID2D1PixelShader) &&
SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol))
+7 -4
ファイルの表示
@@ -542,15 +542,18 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a property declaration.
/// <para>
/// Format: <c>"The compute shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor PropertyDeclaration = new DiagnosticDescriptor(
public static readonly DiagnosticDescriptor InvalidPropertyDeclaration = new DiagnosticDescriptor(
id: "CMPS0040",
title: "Property declaration",
messageFormat: "Property declarations cannot be used in a compute shader",
title: "Invalid property declaration",
messageFormat: "The compute shader of type {0} contains an invalid property \"{1}\" declaration (only stateless properties explicitly implementing an interface property can be used)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Property declarations cannot be used in a compute shader.",
description: "Property declarations (except for stateless properties explicitly implementing an interface property) cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
+22 -6
ファイルの表示
@@ -43,8 +43,8 @@ partial class IShaderGenerator
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Properties are not supported
DetectAndReportPropertyDeclarations(builder, structDeclarationSymbol);
// Detect invalid properties
DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -354,6 +354,12 @@ partial class IShaderGenerator
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0);
// Except for the entry point, ignore explicit interface implementations
if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
{
continue;
}
// Create the source rewriter for the current method
ShaderSourceRewriter shaderSourceRewriter = new(
structDeclarationSymbol,
@@ -487,15 +493,25 @@ partial class IShaderGenerator
}
/// <summary>
/// Finds and reports all declared properties in a shader.
/// Finds and reports all invalid declared properties in a shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
private static void DetectAndReportPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType<IPropertySymbol>())
foreach (var memberSymbol in structDeclarationSymbol.GetMembers())
{
diagnostics.Add(DiagnosticDescriptors.PropertyDeclaration, propertySymbol);
// Detect properties that are not explicit interface implementations
if (memberSymbol is IPropertySymbol { ExplicitInterfaceImplementations.IsEmpty: true })
{
diagnostics.Add(InvalidPropertyDeclaration, memberSymbol, structDeclarationSymbol, memberSymbol);
}
// Detect properties causing a field to be generated
if (memberSymbol is IFieldSymbol { AssociatedSymbol: IPropertySymbol associatedProperty })
{
diagnostics.Add(InvalidPropertyDeclaration, associatedProperty, structDeclarationSymbol, associatedProperty);
}
}
}
+1 -1
ファイルの表示
@@ -23,7 +23,7 @@ partial class IShaderGenerator
structDeclarationSymbol
.GetMembers()
.OfType<IFieldSymbol>()
.Where(static member => member.Type is INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false })
.Where(static member => member is { Type: INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false }, IsImplicitlyDeclared: false })
.Select(static member => member.Name)
.ToImmutableArray();
}
+1 -3
ファイルの表示
@@ -48,9 +48,7 @@ partial class IShaderGenerator
foreach (
IFieldSymbol fieldSymbol in
from fieldSymbol in currentTypeSymbol.GetMembers().OfType<IFieldSymbol>()
where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } &&
!fieldSymbol.IsConst && !fieldSymbol.IsStatic &&
!fieldSymbol.IsFixedSizeBuffer
where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }
select fieldSymbol)
{
string fieldName = fieldSymbol.Name;
+1 -1
ファイルの表示
@@ -23,7 +23,7 @@ partial class IShaderGenerator
INamedTypeSymbol iComputeShaderSymbol,
INamedTypeSymbol iPixelShaderSymbol)
{
foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces)
foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces)
{
if (interfaceSymbol.Name == nameof(IComputeShader) &&
SymbolEqualityComparer.Default.Equals(interfaceSymbol, iComputeShaderSymbol))
+33
ファイルの表示
@@ -1253,6 +1253,39 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0040", "CMPS0047");
}
[TestMethod]
public void PropertyDeclaration_AutoProp_WithExplicitInterfaceImplementation()
{
string source = @"
using ComputeSharp;
namespace ComputeSharp
{
public class ReadWriteBuffer<T> { }
}
namespace MyFancyApp.Sample
{
public interface IFoo
{
float Pi { get; }
}
public struct MyShader : IComputeShader, IFoo
{
public ReadWriteBuffer<float> buffer;
float IFoo.Pi { get; } = 3.14f;
public void Execute()
{
}
}
}";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0040", "CMPS0047");
}
[TestMethod]
public void PropertyDeclaration_GetterBlock()
{
+32
ファイルの表示
@@ -54,4 +54,36 @@ public unsafe partial class AutoConstructorAttributeTests
Assert.AreEqual(instance.a, a);
Assert.AreEqual(instance.b, b);
}
public partial interface IAnotherInterface
{
public int B { get; }
public void Foo();
}
[AutoConstructor]
public partial struct AnotherStruct : IAnotherInterface
{
public int a;
public float b;
int IAnotherInterface.B => 100;
void IAnotherInterface.Foo()
{
}
}
[TestMethod]
public void GenerateConstructorWithExplicitInterfaceMembers()
{
int a = 42;
float b = 3.14f;
AnotherStruct instance = new(a, b);
Assert.AreEqual(instance.a, a);
Assert.AreEqual(instance.b, b);
}
}
+64
ファイルの表示
@@ -272,6 +272,70 @@ namespace ComputeSharp.Tests
this.buffer[ThreadIds.X] *= this.number;
}
}
[TestMethod]
public void ComputeShaderWithInheritedShaderInterface()
{
_ = ReflectionServices.GetShaderInfo<ComputeShaderWithInheritedShaderInterfaceShader>();
}
public interface IMyBaseShader : IComputeShader
{
public int A { get; }
public void B();
}
[AutoConstructor]
internal readonly partial struct ComputeShaderWithInheritedShaderInterfaceShader : IMyBaseShader
{
int IMyBaseShader.A => 42;
void IMyBaseShader.B()
{
}
public readonly ReadWriteBuffer<float> buffer;
public readonly float number;
/// <inheritdoc/>
public void Execute()
{
this.buffer[ThreadIds.X] *= this.number;
}
}
[TestMethod]
public void PixelShaderWithInheritedShaderInterface()
{
_ = ReflectionServices.GetShaderInfo<PixelShaderWithInheritedShaderInterfaceShader, float4>();
}
public interface IMyBaseShader<T> : IPixelShader<T>
where T : unmanaged
{
public int A { get; }
public void B();
}
[AutoConstructor]
internal readonly partial struct PixelShaderWithInheritedShaderInterfaceShader : IMyBaseShader<float4>
{
int IMyBaseShader<float4>.A => 42;
void IMyBaseShader<float4>.B()
{
}
public readonly float number;
/// <inheritdoc/>
public float4 Execute()
{
return default;
}
}
}
}