Add diagnostic for invalid return type

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-18 14:26:09 +02:00
1 親 7576cc3045
コミット 140aa10a12
7ファイル変更
+113 -27

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

+55 -26
ファイルの表示
@@ -24,31 +24,68 @@ partial class D2DPixelShaderSourceGenerator
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the target method.</returns>
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeMethodInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
{
// If there is no precompiled bytecode, just emit a span with a 0 value
string bytecodeLiterals = bytecodeInfo.Bytecode.IsDefaultOrEmpty
? "0"
: ID2D1ShaderGenerator.LoadBytecode.BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
// Get the TypeSyntax for the return type (either ReadOnlySpan<byte>, or an invalid one).
// This is done so that if the user picks an invalid type, the code is still generated,
// just returning default, so that proper diagnostic can be generated, without also
// having hard to understand compiler errors due to the mismatched return type here.
TypeSyntax returnType = bytecodeInfo.InvalidReturnType is string invalidReturnType
? IdentifierName(invalidReturnType)
: GenericName(Identifier("global::System.ReadOnlySpan"))
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)));
// Prepare the fixup function
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
BlockSyntax methodBlock;
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
{
// This code generates the following block:
//
// {
// return default;
// }
methodBlock =
Block(ReturnStatement(
LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword))));
fixup = tree => tree.GetText(Encoding.UTF8);
}
else
{
// This code generates the following block:
//
// {
// return new byte[] { __EMBEDDED_SHADER_BYTECODE };
// }
//
// As in the ID2D1Shader generator, the __EMBEDDED_SHADER_BYTECODE identifier will be replaced with the bytecode
// after the full string for the syntax tree has been generated, to avoid every literal going through a token.
methodBlock =
Block(
ReturnStatement(
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(
InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE"))))));
string bytecodeLiterals = ID2D1ShaderGenerator.LoadBytecode.BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
// Prepare the fixup function
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
}
// This code produces a branch as follows:
//
// /// <inheritdoc/>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <MODIFIERS> global::System.ReadOnlySpan<byte> <METHOD_NAME>()
// {
// return new byte[] { __EMBEDDED_SHADER_BYTECODE };
// }
//
// As in the ID2D1Shader generator, the __EMBEDDED_SHADER_BYTECODE identifier will be replaced with the bytecode
// after the full string for the syntax tree has been generated, to avoid every literal going through a token.
// <MODIFIERS> <RETURN_TYPE> <METHOD_NAME>()
// <METHOD_BODY>
return
MethodDeclaration(
GenericName(Identifier("global::System.ReadOnlySpan"))
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
Identifier(bytecodeInfo.MethodName))
MethodDeclaration(returnType, Identifier(bytecodeInfo.MethodName))
.AddModifiers(bytecodeInfo.Modifiers.Select(Token).ToArray())
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
@@ -61,15 +98,7 @@ partial class D2DPixelShaderSourceGenerator
SyntaxKind.OpenBracketToken,
TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.WithBody(Block(
ReturnStatement(
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(
InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))));
.WithBody(methodBlock);
}
}
}