Skip to content
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

Function pointer support #1037

Merged
merged 4 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 @@ -19,18 +19,19 @@ Parent Symbols (Unlisted, abstract):
| TypeSymbol | |
| MethodSymbol |

| 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) |
| InternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/InternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/InternalTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/InternalTypeReferenceTests.cs) |
| NamespaceSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/NamespaceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceTests.cs) |
| PointerTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/PointerTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/PointerTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/PointerTypeReferenceTests.cs) |
| StaticExternalMethodSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StaticExternalMethodSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StaticExternalMethodSymbolTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/StaticExternalMethodSymbolTests.cs) |
| StructSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StructSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StructTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructTests.cs) |
| UnresolvedTypeReference | [here](src/generators/Silk.NET.SilkTouch.Symbols/UnresolvedTypeReference.cs) | [here](tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/UnresolvedTypeReferenceTests.cs) | - |
| 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) |
| FunctionPointerTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/FunctionPointerTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/FunctionPointerTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/FunctionPointerTypeReferenceTests.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) |
| InternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/InternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/InternalTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/InternalTypeReferenceTests.cs) |
| NamespaceSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/NamespaceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceTests.cs) |
| PointerTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/PointerTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/PointerTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/PointerTypeReferenceTests.cs) |
| StaticExternalMethodSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StaticExternalMethodSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StaticExternalMethodSymbolTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/StaticExternalMethodSymbolTests.cs) |
| StructSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StructSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StructTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructTests.cs) |
| UnresolvedTypeReference | [here](src/generators/Silk.NET.SilkTouch.Symbols/UnresolvedTypeReference.cs) | [here](tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/UnresolvedTypeReferenceTests.cs) | - |

## How to create a symbol

Expand Down
8 changes: 7 additions & 1 deletion src/generators/Silk.NET.SilkTouch.DotnetTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ private static IEnumerable<Symbol> ProcessSymbols
visitors.Add(ActivatorUtilities.CreateInstance<PointerTypeResolver>(serviceProvider, typeStore));
}

if ((usedSymbolVisitors & AvailableSymbolVisitors.FunctionPointerTypeResolver) != 0)
{
visitors.Add(ActivatorUtilities.CreateInstance<FunctionPointerTypeResolver>(serviceProvider, typeStore));
}

if ((usedSymbolVisitors & AvailableSymbolVisitors.InternalTypeResolver) != 0)
{
var typeScopeSymbolVisitor = ActivatorUtilities.CreateInstance<TypeScopeSymbolVisitor>
Expand Down Expand Up @@ -518,7 +523,8 @@ private enum AvailableSymbolVisitors
PointerTypeResolver = 1 << 1,
InternalTypeResolver = 1 << 2,
PrimitiveTypeResolver = 1 << 3,
AllTypeResolvers = PointerTypeResolver | InternalTypeResolver | PrimitiveTypeResolver,
FunctionPointerTypeResolver = 1 << 4,
AllTypeResolvers = PointerTypeResolver | InternalTypeResolver | PrimitiveTypeResolver | FunctionPointerTypeResolver,
}
}
}
39 changes: 39 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,45 @@ protected override PointerTypeReference VisitPointerTypeReference(PointerTypeRef
return pointerTypeReference;
}

protected override FunctionPointerTypeReference VisitFunctionPointerTypeReference
(FunctionPointerTypeReference functionPointerTypeReference)
{
AssertClearState();

var paramList = functionPointerTypeReference.ParameterTypes
.Append(functionPointerTypeReference.ReturnType)
.Select
(
(x, i) =>
{
VisitTypeReference(x);
if (_syntax is not TypeSyntax typeSyntax)
throw new InvalidOperationException("TypeReference did not return TypeSyntax");
ClearState();

if (i == 0 || i > functionPointerTypeReference.ParameterTypes.Length)
return typeSyntax;
else
return typeSyntax.WithLeadingTrivia(Space); // not ideal, but the easiest way to do this
}
)
.Select(FunctionPointerParameter)
.ToImmutableArray();

_syntax = FunctionPointerType
(
Token(SyntaxKind.DelegateKeyword),
Token(SyntaxKind.AsteriskToken),
FunctionPointerCallingConvention(Token(SyntaxKind.UnmanagedKeyword)).WithLeadingTrivia(Space),
FunctionPointerParameterList
(
SeparatedList(paramList)
)
);

return functionPointerTypeReference;
}

protected override ExternalTypeReference VisitExternalTypeReference(ExternalTypeReference typeReference)
{
AssertClearState();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 function pointer.
/// </summary>
public sealed record FunctionPointerTypeReference(TypeReference ReturnType, ImmutableArray<TypeReference> ParameterTypes) : TypeReference
{
}
20 changes: 19 additions & 1 deletion src/generators/Silk.NET.SilkTouch.Symbols/SymbolVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ protected virtual TypeReference VisitTypeReference(TypeReference typeReference)
if (typeReference is InternalTypeReference itr) return VisitInternalTypeReference(itr);
if (typeReference is UnresolvedTypeReference utr) UnresolvedTypeReference.ThrowInvalidSymbol();
if (typeReference is PointerTypeReference ptr) return VisitPointerTypeReference(ptr);
if (typeReference is FunctionPointerTypeReference fptr) return VisitFunctionPointerTypeReference(fptr);
return ThrowUnknownSymbol<TypeReference>(typeReference);
}

Expand All @@ -135,7 +136,24 @@ protected virtual PointerTypeReference VisitPointerTypeReference(PointerTypeRefe
{
return new PointerTypeReference(VisitTypeReference(pointerTypeReference.Underlying));
}

/// <summary>
/// Visit a <see cref="FunctionPointerTypeReference"/>. Will call the appropriate methods to visit the different parts of the symbol.
/// </summary>
/// <param name="functionPointerTypeReference">The function pointer type reference to visit</param>
/// <returns>The rewritten symbol</returns>
/// <remarks>
/// The order in which the parts are visited is kept as an implementation detail. Do not rely on this order.
/// </remarks>
protected virtual FunctionPointerTypeReference VisitFunctionPointerTypeReference
(FunctionPointerTypeReference functionPointerTypeReference)
{
return new FunctionPointerTypeReference
(
VisitTypeReference(functionPointerTypeReference.ReturnType),
functionPointerTypeReference.ParameterTypes.Select(VisitTypeReference).ToImmutableArray()
);
}

/// <summary>
/// Visit a <see cref="InternalTypeReference"/>. Will call the appropriate methods to visit the different parts of the symbol.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 System.Text.RegularExpressions;
using Silk.NET.SilkTouch.Symbols;

namespace Silk.NET.SilkTouch.TypeResolution;

/// <summary>
/// A <see cref="SimpleTypeResolverBase"/> that resolves strings of standard C# form
/// </summary>
public class FunctionPointerTypeResolver : SimpleTypeResolverBase
{
private static readonly Regex _regex = new
(/*lang=regex*/
@"delegate\*\sunmanaged(\[((?'modifier'.(?=,\s?)?)+)*\])?\<((?'parameter'(.(?=,\s?)+))(,\s?))*(?'return_type'(.)+)\>",
RegexOptions.CultureInvariant
);

/// <inheritdoc />
public FunctionPointerTypeResolver(TypeStore typeStore) : base(typeStore)
{
}

/// <inheritdoc />
protected override bool TryResolve(UnresolvedTypeReference utr, out TypeReference? resolved)
{
if (utr.Text.StartsWith("delegate*"))
{
var match = _regex.Match(utr.Text);
if (match.Success)
{
var parameters = match.Groups["parameter"]
.Captures.OfType<Capture>()
.Select(x => new UnresolvedTypeReference(x.Value))
.Cast<TypeReference>()
.ToImmutableArray();
var returnType = new UnresolvedTypeReference(match.Groups["return_type"].Value);

resolved = new FunctionPointerTypeReference(returnType, parameters);
return true;
}
}
resolved = null;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 class FunctionPointerTypeReferenceTests : EmitterTest
{
[Fact, Trait("Category", "Symbols"),
Trait("Target Language", "C#")]
public void StringTestNoParams()
{
var symbol = new FunctionPointerTypeReference
(new ExternalTypeReference(null, new IdentifierSymbol("Ret")), ImmutableArray<TypeReference>.Empty);

var transformed = Transform(symbol);

Assert.Equal("delegate* unmanaged<Ret>", transformed.ToFullString());
}

[Fact, Trait("Category", "Symbols"),
Trait("Target Language", "C#")]
public void StringTestWithParams()
{
var symbol = new FunctionPointerTypeReference
(new ExternalTypeReference(null, new IdentifierSymbol("Ret")), new TypeReference[]
{
new ExternalTypeReference(null, new IdentifierSymbol("Param1")),
new ExternalTypeReference(null, new IdentifierSymbol("Param2")),
}.ToImmutableArray());

var transformed = Transform(symbol);

Assert.Equal("delegate* unmanaged<Param1, Param2, Ret>", transformed.ToFullString());
}
}
1 change: 1 addition & 0 deletions tests/Silk.NET.SilkTouch.IntegrationTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static string GetCSharpOutputFromCpp(string cpp)
var processors = new SymbolVisitor[]
{
ActivatorUtilities.CreateInstance<PointerTypeResolver>(serviceProvider, typeStore),
ActivatorUtilities.CreateInstance<FunctionPointerTypeResolver>(serviceProvider, typeStore),
ActivatorUtilities.CreateInstance<PrimitiveTypeResolver>(serviceProvider, typeStore),

typeScopeSymbolVisitor,
Expand Down
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 FunctionPointerTypeReferenceTests
{
[Fact, Trait("Category", "Symbols")]
public void VisitedAsSelf()
{
var symbol = new FunctionPointerTypeReference(new InternalTypeReference(TypeId.CreateNew()), ImmutableArray<TypeReference>.Empty);
var visitor = new Mock<MockSymbolVisitor> { CallBase = true };
visitor.Object.Visit(symbol);

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

[Fact, Trait("Category", "Symbols")]
public void VisitedAsRef()
{
var symbol = new FunctionPointerTypeReference(new InternalTypeReference(TypeId.CreateNew()), ImmutableArray<TypeReference>.Empty);
var visitor = new Mock<MockSymbolVisitor> { CallBase = true };
visitor.Object.Visit(symbol);

visitor.Protected()
.Verify<TypeReference>
("VisitTypeReference", Times.Once(), ItExpr.Is<TypeReference>(x => ReferenceEquals(x, symbol)));
}
}
Loading