Skip to content

Enable Integration 4 & Fix issues #1016

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 1 commit 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
Expand Up @@ -39,7 +39,7 @@ public sealed class NameResolverSymbolVisitor : SymbolVisitor
/// <param name="logger">A logger used for diagnostic purposes</param>
/// <param name="rootScope">The <see cref="TypeResolutionScope"/> used as root</param>
/// <param name="typeStore">The <see cref="TypeStore"/> used</param>
public NameResolverSymbolVisitor(ILogger logger, TypeResolutionScope rootScope, TypeStore typeStore) : base(typeStore)
public NameResolverSymbolVisitor(ILogger<NameResolverSymbolVisitor> logger, TypeResolutionScope rootScope, TypeStore typeStore) : base(typeStore)
{
_logger = logger;
_rootScope = rootScope;
Expand All @@ -51,7 +51,7 @@ protected override TypeSymbol VisitType(TypeSymbol typeSymbol)
{
var startLength = _seenScopes.Count;
_seenScopes.Push(_currentScope);
_currentScope = _currentScope.ChildTypeScopes[typeSymbol];
_currentScope = _currentScope.ChildTypeScopes[typeSymbol.Id];
var result = base.VisitType(typeSymbol);
_currentScope = _seenScopes.Pop();
Debug.Assert(_seenScopes.Count == startLength);
Expand Down Expand Up @@ -84,11 +84,13 @@ protected override TypeReference VisitTypeReference(TypeReference typeReference)
return new InternalTypeReference(foundChild!.Id);
}
}

return unresolvedTypeReference;
}
return base.VisitTypeReference(typeReference);
}

private static bool TryFindMatchingType(TypeResolutionScope rootScope, string text, out TypeSymbol? foundChild)
private bool TryFindMatchingType(TypeResolutionScope rootScope, string text, out TypeSymbol? foundChild)
{
// this method "recursively" builds the candidate type names and see if anything matches.
Stack<(string, TypeResolutionScope)> todo = new Stack<(string, TypeResolutionScope)>();
Expand All @@ -100,9 +102,15 @@ private static bool TryFindMatchingType(TypeResolutionScope rootScope, string te

foreach (var v in scope.ChildTypeScopes)
{
var type = v.Key;
var typeId = v.Key;
if (!TypeStore.TryResolve(typeId, out var type))
{
_logger.LogWarning("Failed to resolve Type ID {id}", typeId);
continue;
}
Debug.Assert(type is not null);

var fullName = prefix + type.Identifier.Value;
var fullName = prefix + type!.Identifier.Value;
if (fullName == text)
{
foundChild = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Silk.NET.SilkTouch.TypeResolution;
/// Used to track types and child scopes, used to resolve <see cref="UnresolvedTypeReference"/>
/// </summary>
/// <param name="ChildTypeScopes">Mapping of child types to their scopes</param>
public sealed record TypeResolutionScope(Dictionary<TypeSymbol, TypeResolutionScope> ChildTypeScopes)
public sealed record TypeResolutionScope(Dictionary<TypeId, TypeResolutionScope> ChildTypeScopes)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ protected override TypeSymbol VisitType(TypeSymbol typeSymbol)
var newScope = new TypeResolutionScope(new());
var oldScope = _currentScope;
var oldParent = _currentParent;
_currentScope.ChildTypeScopes[typeSymbol] = newScope;
_currentScope.ChildTypeScopes[typeSymbol.Id] = newScope;
_currentScope = newScope;
_currentParent = typeSymbol;
var result = base.VisitType(typeSymbol);
_currentScope = oldScope;
_currentParent = oldParent;
return result;
}

/// <inheritdoc />
protected override TypeReference VisitTypeReference(TypeReference typeReference)
{
if (typeReference is UnresolvedTypeReference)
return typeReference;

return base.VisitTypeReference(typeReference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Scraper\Silk.NET.SilkTouch.Scraper.csproj" />
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Emitter\Silk.NET.SilkTouch.Emitter.csproj" />
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Symbols\Silk.NET.SilkTouch.Symbols.csproj" />
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.TypeResolution\Silk.NET.SilkTouch.TypeResolution.csproj" />
<ProjectReference Include="..\Silk.NET.SilkTouch.TestFramework\Silk.NET.SilkTouch.TestFramework.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct { long k, l; } w;
return Verifier.Verify(result);
}

[Fact(Skip = "TODO: Reenable after proper type support"),
[Fact,
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down Expand Up @@ -156,7 +156,7 @@ struct S
return Verifier.Verify(result);
}

[Fact(Skip = "TODO: Reenable after proper type support"),
[Fact(Skip = "Nested Types?!"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down
19 changes: 18 additions & 1 deletion tests/Silk.NET.SilkTouch.IntegrationTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Silk.NET.SilkTouch.Emitter;
using Silk.NET.SilkTouch.Scraper;
using Silk.NET.SilkTouch.Symbols;
using Silk.NET.SilkTouch.TypeResolution;
using Xunit;

namespace Silk.NET.SilkTouch.IntegrationTests;
Expand Down Expand Up @@ -48,7 +49,23 @@ public static string GetCSharpOutputFromCpp(string cpp)
Assert.NotNull(xml);

var typeStore = new TypeStore();
var symbols = scraper.ScrapeXML(xml!, typeStore);
var symbols = scraper.ScrapeXML(xml!, typeStore).ToArray();

var typeScopeSymbolVisitor = ActivatorUtilities.CreateInstance<TypeScopeSymbolVisitor>(serviceProvider, typeStore);
var processors = new SymbolVisitor[]
{
ActivatorUtilities.CreateInstance<PointerTypeResolver>(serviceProvider, typeStore),

typeScopeSymbolVisitor,
ActivatorUtilities.CreateInstance<NameResolverSymbolVisitor>
(serviceProvider, typeScopeSymbolVisitor.RootScope, typeStore)
};

foreach (var processor in processors)
{
symbols = symbols.Select(processor.Visit).ToArray();
}

var emitter = new CSharpEmitter();
var outputs = symbols.Select(x => emitter.Transform(x, typeStore));
return outputs.Aggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ public void SelfTypeIsResolvedCorrectly()
{
new(new UnresolvedTypeReference("a"), new IdentifierSymbol("someField"))
}.ToImmutableArray());
var typeStore = new TypeStore();
typeStore.Store(testType);
var resolutionScope = new TypeResolutionScope
(
new Dictionary<TypeSymbol, TypeResolutionScope>
new Dictionary<TypeId, TypeResolutionScope>
{
[testType] = new TypeResolutionScope(new())
[testType.Id] = new TypeResolutionScope(new())
}
);
var serviceProvider = Helpers.CreateServiceProvider();
var visitor = new NameResolverSymbolVisitor
(
serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger<NameResolverSymbolVisitor>(),
resolutionScope,
new TypeStore()
typeStore
);

var resultType = visitor.Visit(testType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void RootScopeContainsSingleRootType()

var rootScope = visitor.RootScope;
var single = Assert.Single(rootScope.ChildTypeScopes);
Assert.StrictEqual(testType, single.Key);
Assert.StrictEqual(testType.Id, single.Key);
var associatedScope = single.Value;
Assert.Empty(associatedScope.ChildTypeScopes);
}
Expand Down Expand Up @@ -55,7 +55,7 @@ public void RootScopeContainsSingleNamespacedType()

var rootScope = visitor.RootScope;
var single = Assert.Single(rootScope.ChildTypeScopes);
Assert.StrictEqual(testType, single.Key);
Assert.StrictEqual(testType.Id, single.Key);
var associatedScope = single.Value;
Assert.Empty(associatedScope.ChildTypeScopes);
}
Expand Down Expand Up @@ -86,19 +86,19 @@ public void RootScopeContainsMultipleNamespacedTypes()
rootScope.ChildTypeScopes,
(kv) =>
{
Assert.StrictEqual(testType1, kv.Key);
Assert.StrictEqual(testType1.Id, kv.Key);
var associatedScope = kv.Value;
Assert.Empty(associatedScope.ChildTypeScopes);
},
(kv) =>
{
Assert.StrictEqual(testType2, kv.Key);
Assert.StrictEqual(testType2.Id, kv.Key);
var associatedScope = kv.Value;
Assert.Empty(associatedScope.ChildTypeScopes);
},
(kv) =>
{
Assert.StrictEqual(testType3, kv.Key);
Assert.StrictEqual(testType3.Id, kv.Key);
var associatedScope = kv.Value;
Assert.Empty(associatedScope.ChildTypeScopes);
}
Expand Down