Fixed processing of discovered methods for [SourceMethod]

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-14 17:26:05 +02:00
1 親 4db77f72b0
コミット 8e0db58a19
3ファイル変更
+30 -43

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

-22
ファイルの表示
@@ -12,28 +12,6 @@ namespace ComputeSharp.SourceGenerators.Helpers
/// </summary>
internal static class SyntaxFactoryHelper
{
/// <summary>
/// Creates a <see cref="string"/>[] array expression from the given sequence of <see cref="string"/> instances.
/// <para>
/// That it, it applies the following transformation:
/// <code>
/// { "S1", "S2" } => new string[] { "S1", "S2" }
/// </code>
/// </para>
/// </summary>
/// <param name="values">The input sequence of <see cref="string"/> instances.</param>
/// <returns>An <see cref="ArrayCreationExpressionSyntax"/> instance with the described contents.</returns>
[Pure]
public static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable<string> values)
{
return
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
}
/// <summary>
/// Creates an <see cref="object"/>[] array expression with the nested groups in the input sequence.
/// <para>
+20 -11
ファイルの表示
@@ -66,11 +66,10 @@ namespace ComputeSharp.SourceGenerators
{
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var (invokeMethod, localFunctions) = GetProcessedMethods(context, methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var (invokeMethod, processedMethods) = GetProcessedMethods(context, methodDeclaration, semanticModel, discoveredTypes, constantDefinitions);
var processedTypes = IShaderGenerator.GetProcessedTypes(discoveredTypes).ToArray();
var processedConstants = IShaderGenerator.GetProcessedConstants(constantDefinitions);
@@ -80,9 +79,9 @@ namespace ComputeSharp.SourceGenerators
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName(typeof(ShaderMethodSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
AttributeArgument(NestedArrayExpression(processedTypes)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
AttributeArgument(ArrayExpression(localFunctions)),
AttributeArgument(NestedArrayExpression(processedTypes)),
AttributeArgument(NestedArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -100,18 +99,18 @@ namespace ComputeSharp.SourceGenerators
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> instance for the current method.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the method to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and local functions).</returns>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and all captured methods).</returns>
[Pure]
private static (string TargetMethod, IEnumerable<string> LocalFunctions) GetProcessedMethods(
private static (string TargetMethod, IEnumerable<(string Signature, string Definition)> ProcessedMethods) GetProcessedMethods(
GeneratorExecutionContext context,
MethodDeclarationSyntax methodDeclaration,
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
IDictionary<IFieldSymbol, string> constantDefinitions)
{
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context);
// Rewrite the method syntax tree
@@ -122,15 +121,25 @@ namespace ComputeSharp.SourceGenerators
.NormalizeWhitespace(eol: "\n")
.ToFullString();
List<string> localFunctions = new(shaderSourceRewriter.LocalFunctions.Count);
List<(string, string)> processedMethods = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
localFunctions.Add(localFunction.Value.NormalizeWhitespace(eol: "\n").ToFullString());
processedMethods.Add((
localFunction.Value.AsDefinition().NormalizeWhitespace(eol: "\n").ToFullString(),
localFunction.Value.NormalizeWhitespace(eol: "\n").ToFullString()));
}
return (targetMethod, localFunctions);
// Emit the discovered static methods
foreach (var staticMethod in staticMethods.Values)
{
processedMethods.Add((
staticMethod.AsDefinition().NormalizeWhitespace(eol: "\n").ToFullString(),
staticMethod.NormalizeWhitespace(eol: "\n").ToFullString()));
}
return (targetMethod, processedMethods);
}
}
}
+10 -10
ファイルの表示
@@ -23,16 +23,16 @@ namespace ComputeSharp.__Internals
/// </summary>
internal const string InvokeMethodIdentifier = "__<NAME>__";
/// <summary>
/// The source code for the target entry point method.
/// </summary>
private readonly string invokeMethod;
/// <summary>
/// The fully qualified name of the current method.
/// </summary>
private readonly string methodName;
/// <summary>
/// The source code for the target entry point method.
/// </summary>
private readonly string invokeMethod;
/// <summary>
/// The collection of custom types.
/// </summary>
@@ -41,7 +41,7 @@ namespace ComputeSharp.__Internals
/// <summary>
/// The collection of processed methods.
/// </summary>
private readonly string[] methods;
private readonly Dictionary<string, string> methods;
/// <summary>
/// The collection of discovered constants.
@@ -52,16 +52,16 @@ namespace ComputeSharp.__Internals
/// Creates a new <see cref="ShaderMethodSourceAttribute"/> instance with the specified parameters.
/// </summary>
/// <param name="methodName">The fully qualified name of the current method.</param>
/// <param name="types">The collection of custom types.</param>
/// <param name="invokeMethod">The source code for the target entry point method.</param>
/// <param name="types">The collection of custom types.</param>
/// <param name="methods">The collection of processed methods.</param>
/// <param name="constants">The collection of discovered constants.</param>
public ShaderMethodSourceAttribute(string methodName, object[] types, string invokeMethod, string[] methods, object[] constants)
public ShaderMethodSourceAttribute(string methodName, string invokeMethod, object[] types, object[] methods, object[] constants)
{
this.invokeMethod = invokeMethod;
this.methodName = methodName;
this.invokeMethod = invokeMethod;
this.types = types.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
this.methods = methods;
this.methods = methods.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
this.constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
}