Skip to content

Commit 7f74101

Browse files
committed
Emitter Transformation Skeleton
1 parent f09a0c3 commit 7f74101

File tree

8 files changed

+132
-0
lines changed

8 files changed

+132
-0
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
<PackageVersion Include="Statiq.Razor" Version="1.0.0-beta.48" />
3939
<PackageVersion Include="Statiq.Yaml" Version="1.0.0-beta.48" />
4040
<PackageVersion Include="Humanizer.Core" Version="2.14.1" />
41+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
4142
</ItemGroup>
4243
</Project>

Silk.NET.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.SilkTouch.Emitter.
8787
EndProject
8888
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.SilkTouch.Symbols", "src\generators\Silk.NET.SilkTouch.Symbols\Silk.NET.SilkTouch.Symbols.csproj", "{97AF0383-2665-446E-9FBD-218051422B13}"
8989
EndProject
90+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.SilkTouch.Symbols.Tests", "tests\Silk.NET.SilkTouch.Symbols.Tests\Silk.NET.SilkTouch.Symbols.Tests.csproj", "{795A93A6-9578-439F-BB08-07B148B1D4CE}"
91+
EndProject
9092
Global
9193
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9294
Debug|Any CPU = Debug|Any CPU
@@ -373,6 +375,18 @@ Global
373375
{97AF0383-2665-446E-9FBD-218051422B13}.Release|x64.Build.0 = Release|Any CPU
374376
{97AF0383-2665-446E-9FBD-218051422B13}.Release|x86.ActiveCfg = Release|Any CPU
375377
{97AF0383-2665-446E-9FBD-218051422B13}.Release|x86.Build.0 = Release|Any CPU
378+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
379+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
380+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|x64.ActiveCfg = Debug|Any CPU
381+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|x64.Build.0 = Debug|Any CPU
382+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|x86.ActiveCfg = Debug|Any CPU
383+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Debug|x86.Build.0 = Debug|Any CPU
384+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
385+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|Any CPU.Build.0 = Release|Any CPU
386+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|x64.ActiveCfg = Release|Any CPU
387+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|x64.Build.0 = Release|Any CPU
388+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|x86.ActiveCfg = Release|Any CPU
389+
{795A93A6-9578-439F-BB08-07B148B1D4CE}.Release|x86.Build.0 = Release|Any CPU
376390
EndGlobalSection
377391
GlobalSection(SolutionProperties) = preSolution
378392
HideSolutionNode = FALSE
@@ -407,6 +421,7 @@ Global
407421
{66DC1792-099B-4477-9A98-F4993BD0DDD0} = {F07CABFC-DC6A-4B5B-BC56-B10EEC2C0BFA}
408422
{2B1A2B37-649F-4F6E-90E9-EC3B28D0A3CC} = {94D5D1E1-B998-4CB1-9D04-DA138A2B0F3C}
409423
{97AF0383-2665-446E-9FBD-218051422B13} = {8238D9F3-E158-4633-8017-B29AA3AD61F7}
424+
{795A93A6-9578-439F-BB08-07B148B1D4CE} = {94D5D1E1-B998-4CB1-9D04-DA138A2B0F3C}
410425
EndGlobalSection
411426
GlobalSection(ExtensibilityGlobals) = postSolution
412427
SolutionGuid = {F5273D7F-3334-48DF-94E3-41AE6816CD4D}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
8+
using Silk.NET.SilkTouch.Symbols;
9+
using SymbolVisitor = Microsoft.CodeAnalysis.SymbolVisitor;
10+
11+
namespace Silk.NET.SilkTouch.Emitter;
12+
13+
/// <summary>
14+
/// The primary entrypoint for all C# related emission
15+
/// </summary>
16+
public sealed class CSharpEmitter
17+
{
18+
/// <summary>
19+
/// Create a new <see cref="CSharpEmitter"/> from dependencies. This should generally be called by DI.
20+
/// </summary>
21+
public CSharpEmitter()
22+
{
23+
24+
}
25+
26+
/// <summary>
27+
/// Transforms the given <see cref="Symbol"/> into a <see cref="CSharpSyntaxNode"/>
28+
/// </summary>
29+
/// <param name="symbol">The symbol to transform</param>
30+
/// <returns>A syntax node, containing syntax depending on the symbol. The syntax node should produce valid C# code.</returns>
31+
/// <remarks>
32+
/// The returned syntax node may not be ideal and is not optimized for code size.
33+
/// In general it will contain code that is optimized for performance and clarity, this is not a guarantee though.
34+
/// It may contain comments that may be stripped for better code size.
35+
/// The returned node will never contain line comments, but other C# language feature may still rely on whitespace and/or newlines.
36+
/// Note that (block) comments will never be used to replace such whitespace, even if this is valid to allow a potential comment stripping to be a simple as possible.
37+
/// </remarks>
38+
public CSharpSyntaxNode Transform(Symbol symbol)
39+
{
40+
var visitor = new Visitor();
41+
visitor.Visit(symbol); // the result is ignored. This allows us to optimize the visitor in some cases.
42+
var syntax = visitor.Syntax;
43+
if (syntax is null)
44+
throw new NotImplementedException
45+
("Resulting Syntax was null. This suggests some unimplemented root Symbol in the Visitor.");
46+
return syntax;
47+
}
48+
49+
private class Visitor : Silk.NET.SilkTouch.Symbols.SymbolVisitor
50+
{
51+
public CSharpSyntaxNode? Syntax => _syntax;
52+
private CSharpSyntaxNode? _syntax = null;
53+
}
54+
}

src/generators/Silk.NET.SilkTouch.Emitter/Silk.NET.SilkTouch.Emitter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<ProjectReference Include="..\Silk.NET.SilkTouch.Symbols\Silk.NET.SilkTouch.Symbols.csproj" />
9+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
910
</ItemGroup>
1011

1112
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Silk.NET.SilkTouch.Symbols;
5+
6+
/// <summary>
7+
/// The base Symbol. Represents shared properties of all Symbols. Primarily used with <see cref="SymbolVisitor"/>
8+
/// </summary>
9+
public abstract class Symbol
10+
{
11+
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Silk.NET.SilkTouch.Symbols;
5+
6+
/// <summary>
7+
/// Implements a base visitor to extend when rewriting or otherwise searching through a <see cref="Symbol"/>
8+
/// </summary>
9+
public abstract class SymbolVisitor
10+
{
11+
/// <summary>
12+
/// Visit a Symbol. This will call the appropriate method based on the actual type of the <paramref name="symbol"/>
13+
/// </summary>
14+
/// <param name="symbol">The symbol to visit</param>
15+
/// <returns>The rewritten symbol. May be equal to the original symbol of no rewriting has taken place.</returns>
16+
public virtual Symbol Visit(Symbol symbol)
17+
{
18+
return symbol;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
12+
<PackageReference Include="xunit" />
13+
<PackageReference Include="xunit.runner.visualstudio">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace Silk.NET.SilkTouch.Symbols.Tests;
7+
8+
public sealed class SymbolVisitorTests
9+
{
10+
}

0 commit comments

Comments
 (0)