Implemented working version of metaprogramming

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-01 14:07:31 +01:00
1 親 df02a534ea
コミット c231e8506a
7ファイル変更
+74 -17

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

+1 -1
ファイルの表示
@@ -56,7 +56,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
{
if (includeParameters)
{
var parameters = $"{string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()))}";
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
}
-3
ファイルの表示
@@ -112,9 +112,6 @@ namespace ComputeSharp.SourceGenerators
{
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
// Delegate types are not included and are processed separately
if (typeSymbol.TypeKind == TypeKind.Delegate) continue;
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
+5
ファイルの表示
@@ -73,8 +73,13 @@ namespace ComputeSharp.SourceGenerators.Mappings
/// </summary>
/// <param name="typeSymbol">The input type to map.</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
[Pure]
public static string GetMappedName(INamedTypeSymbol typeSymbol)
{
// Delegate types just return an empty string, as they're not actually
// used in the generated shaders, but just mapped to a function at runtime.
if (typeSymbol.TypeKind == TypeKind.Delegate) return "";
string typeName = typeSymbol.GetFullMetadataName();
// Special case for the structured buffer types
+13 -5
ファイルの表示
@@ -45,7 +45,7 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var processedMethods = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods);
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
// Helper that converts a sequence of strings into an array expression.
@@ -68,6 +68,7 @@ namespace ComputeSharp.SourceGenerators
Attribute(IdentifierName(typeof(ShaderMethodSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
AttributeArgument(ArrayExpression(processedMethods)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -88,7 +89,7 @@ namespace ComputeSharp.SourceGenerators
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/>.</returns>
[Pure]
private static IEnumerable<string> GetProcessedMethods(
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
MethodDeclarationSyntax methodDeclaration,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -97,15 +98,22 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
var processedMethod = shaderSourceRewriter
.Visit(methodDeclaration)!
.WithIdentifier(Identifier("__<NAME>__"))
.WithoutTrivia()
.NormalizeWhitespace()
.ToFullString();
yield return processedMethod.NormalizeWhitespace().ToFullString();
List<string> methods = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
yield return localFunction.Value.NormalizeWhitespace().ToFullString();
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
return (processedMethod, methods);
}
}
}