Skip to content

Commit 24e0793

Browse files
committed
Struct Emission
1 parent e0ce57b commit 24e0793

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/generators/Silk.NET.SilkTouch.Emitter/Emitter.cs renamed to src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.CodeAnalysis.CSharp;
77
using Microsoft.CodeAnalysis.CSharp.Syntax;
88
using Silk.NET.SilkTouch.Symbols;
9-
using SymbolVisitor = Microsoft.CodeAnalysis.SymbolVisitor;
9+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1010

1111
namespace Silk.NET.SilkTouch.Emitter;
1212

@@ -24,7 +24,7 @@ public CSharpEmitter()
2424
}
2525

2626
/// <summary>
27-
/// Transforms the given <see cref="Symbol"/> into a <see cref="CSharpSyntaxNode"/>
27+
/// Transforms the given <see cref="Silk.NET.SilkTouch.Symbols.Symbol"/> into a <see cref="CSharpSyntaxNode"/>
2828
/// </summary>
2929
/// <param name="symbol">The symbol to transform</param>
3030
/// <returns>A syntax node, containing syntax depending on the symbol. The syntax node should produce valid C# code.</returns>
@@ -50,5 +50,18 @@ private class Visitor : Silk.NET.SilkTouch.Symbols.SymbolVisitor
5050
{
5151
public CSharpSyntaxNode? Syntax => _syntax;
5252
private CSharpSyntaxNode? _syntax = null;
53+
54+
protected override Symbol VisitStruct(StructSymbol structSymbol)
55+
{
56+
var members = List<MemberDeclarationSyntax>();
57+
var modifiers = TokenList(Token(SyntaxTriviaList.Empty, SyntaxKind.PublicKeyword, TriviaList(Space)));
58+
_syntax = StructDeclaration
59+
(
60+
List<AttributeListSyntax>(), modifiers, Identifier(structSymbol.Identifier.Value), null, null,
61+
List<TypeParameterConstraintClauseSyntax>(), members
62+
)
63+
.WithKeyword(Token(SyntaxTriviaList.Empty, SyntaxKind.StructKeyword, TriviaList(Space)));
64+
return base.VisitStruct(structSymbol);
65+
}
5366
}
5467
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 Silk.NET.SilkTouch.Symbols;
5+
using Xunit;
6+
7+
namespace Silk.NET.SilkTouch.Emitter.Tests;
8+
9+
public sealed class EmitterStructIntegrationTests : EmitterTest
10+
{
11+
[Fact]
12+
public void StructHasStructKeyword()
13+
{
14+
var emitter = CreateEmitter();
15+
16+
var symbol = new StructSymbol(new IdentifierSymbol("Test"));
17+
18+
var syntax = emitter.Transform(symbol);
19+
20+
var result = syntax.ToFullString();
21+
Assert.Equal("public struct Test{}", result);
22+
}
23+
}
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.Emitter.Tests;
5+
6+
public abstract class EmitterTest
7+
{
8+
protected CSharpEmitter CreateEmitter()
9+
{
10+
return new CSharpEmitter();
11+
}
12+
}

0 commit comments

Comments
 (0)