Skip to content

Class symbols #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Parent Symbols (Unlisted, abstract):

| Name | Symbol Layer File | Symbol Layer Tests | Emitter Tests |
| -------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| ClassSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/ClassSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/ClassSymbolTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs) |
| ExternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/ExternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/ExternalTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/ExternalTypeReferenceTests.cs) |
| FieldSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/FieldSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/FieldTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterFieldTests.cs) |
| IdentifierSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/IdentifierSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/IdentifierTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/IdentifierSymbolTests.cs) |
Expand Down
37 changes: 37 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ protected override StructSymbol VisitStruct(StructSymbol structSymbol)
return structSymbol;
}

protected override ClassSymbol VisitClass(ClassSymbol classSymbol)
{
AssertClearState();

VisitIdentifier(classSymbol.Identifier);
if (_syntaxToken is not {} identifierSyntaxToken)
throw new InvalidOperationException("Identifier was not visited correctly");
ClearState();

Indent();
var members = List
(
classSymbol.Methods.Select
(
x =>
{
VisitMethod(x);
if (_syntax is not MethodDeclarationSyntax mds)
throw new InvalidOperationException("Method not visited correctly");
ClearState();
return ((MemberDeclarationSyntax) mds).WithLeadingTrivia(NewLine);
}
)
.ToImmutableArray()
);
Outdent();

_syntax = ClassDeclaration(identifierSyntaxToken.WithLeadingTrivia(Space))
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
.WithKeyword(Token(SyntaxKind.ClassKeyword).WithLeadingTrivia(Space))
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken).WithLeadingTrivia(NewLine))
.WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(NewLine))
.WithMembers(members);

return classSymbol;
}

protected override FieldSymbol VisitField(FieldSymbol fieldSymbol)
{
AssertClearState();
Expand Down
12 changes: 12 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/ClassSymbol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A <see cref="TypeReference"/> representing a <c>class</c>
/// </summary>
public sealed record ClassSymbol(TypeId Id, IdentifierSymbol Identifier, ImmutableArray<MethodSymbol> Methods)
: TypeSymbol(Id, Identifier);
20 changes: 20 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/SymbolVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ protected virtual TypeSymbol VisitType(TypeSymbol typeSymbol)
{
TypeSymbol? result = null;
if (typeSymbol is StructSymbol @struct) result = VisitStruct(@struct);
else if (typeSymbol is ClassSymbol @class) result = VisitClass(@class);

if (result is not null)
{
Expand All @@ -198,6 +199,25 @@ protected virtual TypeSymbol VisitType(TypeSymbol typeSymbol)
return ThrowUnknownSymbol<TypeSymbol>(typeSymbol);
}

/// <summary>
/// Visit a <see cref="ClassSymbol"/>. Will call the appropriate methods to visit the different parts of the class.
/// </summary>
/// <param name="classSymbol">The class symbol to visit</param>
/// <returns>The rewritten symbol</returns>
/// <seealso cref="VisitType"/>
/// <remarks>
/// The order in which the parts are visited is kept as an implementation detail. Do not rely on this order.
/// </remarks>
protected virtual ClassSymbol VisitClass(ClassSymbol classSymbol)
{
return new ClassSymbol
(
classSymbol.Id,
VisitIdentifier(classSymbol.Identifier),
classSymbol.Methods.Select(VisitMethod).ToImmutableArray()
);
}

/// <summary>
/// Visit a <see cref="StructSymbol"/>. Will call the appropriate methods to visit the different parts of the struct.
/// </summary>
Expand Down
47 changes: 47 additions & 0 deletions tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public sealed class ClassSymbolTests : EmitterTest
{
[Fact, Trait("Category", "Symbols"), Trait("Target Language", "C#")]
public void StringTestNoMethods()
{
var symbol = new ClassSymbol(TypeId.CreateNew(), new IdentifierSymbol("C"), ImmutableArray<MethodSymbol>.Empty);

var result = Transform(symbol);

Assert.Equal("public class C\n" + "{\n" + "}", result.ToFullString());
}

[Fact, Trait("Category", "Symbols"), Trait("Target Language", "C#")]
public void StringTestWithMethods()
{
var method = new StaticExternalMethodSymbol
(
new ExternalTypeReference(null, new IdentifierSymbol("int")),
ImmutableArray<Parameter>.Empty,
new IdentifierSymbol("M")
);
var symbol = new ClassSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol("C"),
new MethodSymbol[]
{
method
}.ToImmutableArray()
);



var result = Transform(symbol);

Assert.Equal("public class C\n" + "{" + "\n public static extern int M();" + "\n}", result.ToFullString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using Moq;
using Moq.Protected;
using Xunit;

namespace Silk.NET.SilkTouch.Symbols.Tests.SymbolVisitorTests;

public class ClassSymbolTests
{
[Fact, Trait("Category", "Symbols")]
public void IsVisitedAsSelf()
{
var symbol = new ClassSymbol(TypeId.CreateNew(), new IdentifierSymbol(""), ImmutableArray<MethodSymbol>.Empty);

var visitor = new Mock<MockSymbolVisitor> { CallBase = true };
visitor.Object.Visit(symbol);

visitor.Protected()
.Verify<ClassSymbol>("VisitClass", Times.Once(), ItExpr.IsAny<ClassSymbol>());
}

[Fact, Trait("Category", "Symbols")]
public void IsVisitedAsType()
{
var symbol = new ClassSymbol(TypeId.CreateNew(), new IdentifierSymbol(""), ImmutableArray<MethodSymbol>.Empty);

var visitor = new Mock<MockSymbolVisitor> { CallBase = true };
visitor.Object.Visit(symbol);

visitor.Protected()
.Verify<TypeSymbol>("VisitType", Times.Once(), ItExpr.IsAny<TypeSymbol>());
}
}