Skip to content

Primitive type resolution #1017

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 9, 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
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Silk.NET.SilkTouch.Symbols;

namespace Silk.NET.SilkTouch.TypeResolution;

/// <summary>
/// A simple type resolver to resolve <see cref="UnresolvedTypeReference"/>s that refer to primitive types.
/// </summary>
public sealed class PrimitiveTypeResolver : SimpleTypeResolverBase
{

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

private static ExternalTypeReference CreateExternalTypeRef(string @namespace, string name)
=> new ExternalTypeReference(new IdentifierSymbol(@namespace), new IdentifierSymbol(name));
private static readonly Dictionary<string, TypeReference> _typeMap = new Dictionary<string, TypeReference>()
{
["bool"] = CreateExternalTypeRef("System", "Boolean"),
["byte"] = CreateExternalTypeRef("System", "Byte"),
["sbyte"] = CreateExternalTypeRef("System", "SByte"),
["char"] = CreateExternalTypeRef("System", "Char"),
["decimal"] = CreateExternalTypeRef("System", "Decimal"),
["double"] = CreateExternalTypeRef("System", "Double"),
["float"] = CreateExternalTypeRef("System", "Single"),
["int"] = CreateExternalTypeRef("System", "Int32"),
["uint"] = CreateExternalTypeRef("System", "UInt32"),
["nint"] = CreateExternalTypeRef("System", "IntPtr"),
["nuint"] = CreateExternalTypeRef("System", "UIntPtr"),
["long"] = CreateExternalTypeRef("System", "Int64"),
["ulong"] = CreateExternalTypeRef("System", "UInt64"),
["short"] = CreateExternalTypeRef("System", "Int16"),
["ushort"] = CreateExternalTypeRef("System", "UInt16"),
};

/// <inheritdoc />
protected override bool TryResolve(UnresolvedTypeReference utr, out TypeReference? resolved)
{
return _typeMap.TryGetValue(utr.Text, out resolved);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LIBRARY_NAMESPACE
{
public struct Test
{
public int f1;
public int f2;
public System.Int32 f1;
public System.Int32 f2;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Silk.NET.SilkTouch.IntegrationTests;
[UsesVerify]
public class StructIntegrationTests
{
[Fact(Skip = "TODO: Reenable after proper type support"),
[Fact,
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down
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<PrimitiveTypeResolver>(serviceProvider, typeStore),

typeScopeSymbolVisitor,
ActivatorUtilities.CreateInstance<NameResolverSymbolVisitor>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.TypeResolution.Tests;

public sealed class PrimitiveTypeResolverTests
{
[Theory, Trait("Category", "Type Resolution"),
InlineData("bool", "System", "Boolean"),
InlineData("byte", "System", "Byte"),
InlineData("sbyte", "System", "SByte"),
InlineData("char", "System", "Char"),
InlineData("decimal", "System", "Decimal"),
InlineData("double", "System", "Double"),
InlineData("float", "System", "Single"),
InlineData("int", "System", "Int32"),
InlineData("uint", "System", "UInt32"),
InlineData("nint", "System", "IntPtr"),
InlineData("nuint", "System", "UIntPtr"),
InlineData("long", "System", "Int64"),
InlineData("ulong", "System", "UInt64"),
InlineData("short", "System", "Int16"),
InlineData("ushort", "System", "UInt16"),
]
public void ShouldResolve(string text, string @namespace, string identifier)
{
var symbol = new UnresolvedTypeReference(text);
var output = new PrimitiveTypeResolver(new TypeStore());

var finalSymbol = output.Visit(symbol);
var @ref = Assert.IsType<ExternalTypeReference>(finalSymbol);
Assert.Equal(@namespace, @ref.Namespace?.Value);
Assert.Equal(identifier, @ref.TypeIdentifier.Value);
}

[Theory, Trait("Category", "Type Resolution"),
InlineData(""),
InlineData("a"),
InlineData("*"),
InlineData("a.b.c"),
InlineData("longType"),
InlineData("using")
]
public void ShouldNotResolve(string text)
{
var symbol = new UnresolvedTypeReference(text);
var output = new PrimitiveTypeResolver(new TypeStore());

var finalSymbol = output.Visit(symbol);
Assert.IsNotType<ExternalTypeReference>(finalSymbol);
}
}