Initial draft

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-25 01:16:23 +01:00
1 親 1142d7bbcd
コミット 5354eabbf7
4ファイル変更
+156

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

+10
ファイルの表示
@@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.SourceGenerators", "src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj", "{EB52CB67-4162-4722-874B-44EA8DA5450E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -112,6 +114,14 @@ Global
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|x64.ActiveCfg = Debug|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|x64.Build.0 = Debug|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Release|Any CPU.Build.0 = Release|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Release|x64.ActiveCfg = Release|Any CPU
{EB52CB67-4162-4722-874B-44EA8DA5450E}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+127
ファイルの表示
@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Mustache
{
[Generator]
public class AutoConstructorAttributeGenerator : ISourceGenerator
{
/// <inheritdoc/>
public void Initialize(GeneratorInitializationContext context)
{
//context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}
/// <inheritdoc/>
public void Execute(GeneratorExecutionContext context)
{
string attributeSource = @"
using System;
namespace ComputeSharp
{
/// <summary>
/// A shader that indicates that a target shader type should get an automatic constructor for all fields.
/// </summary>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
internal sealed class AutoConstructorAttribute : Attribute
{
}
}";
// Add the GenerateConstructor attribute
context.AddSource("__ComputeSharp_AutoConstructorAttribute", SourceText.From(attributeSource, Encoding.UTF8));
// Ensure we have access to the local syntax receiver
//if (context.SyntaxReceiver is not SyntaxReceiver syntaxReceiver) return;
// Create a new compilation with the new custom attribute
//CSharpParseOptions options = (CSharpParseOptions)((CSharpCompilation)context.Compilation).SyntaxTrees[0].Options;
//Compilation compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(attributeSource, Encoding.UTF8), options));
// Get the new attribute from the compilation
//INamedTypeSymbol attributeSymbol = compilation.GetTypeByMetadataName("ComputeSharp.AutoConstructorAttribute")!;
// Loop over the candidate struct declarations, and keep the ones that are actually annotated
foreach (var structSymbol in
//from structDeclaration in syntaxReceiver.CandidateTypes
from tree in context.Compilation.SyntaxTrees
from node in tree.GetRoot().DescendantNodes()
where node.IsKind(SyntaxKind.Attribute) &&
node is
let semanticModel = compilation.GetSemanticModel(structDeclaration.SyntaxTree)
let symbol = semanticModel.GetDeclaredSymbol(structDeclaration)
let attributes = symbol.GetAttributes()
where attributes.Any(attribute => attribute.AttributeClass!.Equals(attributeSymbol, SymbolEqualityComparer.Default))
select symbol)
{
string namespaceName = structSymbol.ContainingNamespace.ToDisplayString();
// Begin constructing the type
StringBuilder source = new();
source.AppendLine($"namespace {namespaceName}");
source.AppendLine("{");
// Visibility
source.Append(structSymbol.DeclaredAccessibility switch
{
Accessibility.Private => "private",
Accessibility.ProtectedAndInternal => "protected internal",
Accessibility.Protected => "protected",
Accessibility.Internal => "internal",
Accessibility.ProtectedOrInternal => "private protected",
Accessibility.Public => "public",
_ => throw new ArgumentException(),
});
source.Append(" ");
// Readonly
if (structSymbol.IsReadOnly) source.Append("readonly ");
// End the type declaration
source.Append("partial struct");
source.AppendLine("{");
// Constructor
source.AppendLine($"public {structSymbol.Name}(");
// Parameters
var fieldSymbols = structSymbol.GetMembers().Where(symbol => symbol.Kind == SymbolKind.Field).Cast<IFieldSymbol>().ToImmutableArray();
source.AppendLine(string.Join($",{Environment.NewLine}", fieldSymbols.Select(field => $"{field.Type} {field.Name}")));
source.AppendLine(") { }");
}
}
/// <summary>
/// Created on demand before each generation pass
/// </summary>
private sealed class SyntaxReceiver : ISyntaxReceiver
{
/// <summary>
/// Gets the list of candidate struct declarations.
/// </summary>
public List<StructDeclarationSyntax> CandidateTypes { get; } = new();
/// <inheritdoc/>
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// Track all structs with at least one attribute
if (syntaxNode is StructDeclarationSyntax structDeclarationSyntax &&
structDeclarationSyntax.AttributeLists.Count > 0)
{
CandidateTypes.Add(structDeclarationSyntax);
}
}
}
}
}
+14
ファイルの表示
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-5.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1" PrivateAssets="all" />
</ItemGroup>
</Project>
+5
ファイルの表示
@@ -2,11 +2,16 @@
<PropertyGroup>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ComputeSharp.Shaders\ComputeSharp.Shaders.csproj" />
<ProjectReference
Include="..\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>
<ItemGroup>