Skip to content

Commit b7b150f

Browse files
authored
Enable Integration 4 & Fix issues (#1016)
1 parent a5766c0 commit b7b150f

File tree

8 files changed

+55
-18
lines changed

8 files changed

+55
-18
lines changed

src/generators/Silk.NET.SilkTouch.TypeResolution/NameResolverSymbolVisitor.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class NameResolverSymbolVisitor : SymbolVisitor
3939
/// <param name="logger">A logger used for diagnostic purposes</param>
4040
/// <param name="rootScope">The <see cref="TypeResolutionScope"/> used as root</param>
4141
/// <param name="typeStore">The <see cref="TypeStore"/> used</param>
42-
public NameResolverSymbolVisitor(ILogger logger, TypeResolutionScope rootScope, TypeStore typeStore) : base(typeStore)
42+
public NameResolverSymbolVisitor(ILogger<NameResolverSymbolVisitor> logger, TypeResolutionScope rootScope, TypeStore typeStore) : base(typeStore)
4343
{
4444
_logger = logger;
4545
_rootScope = rootScope;
@@ -51,7 +51,7 @@ protected override TypeSymbol VisitType(TypeSymbol typeSymbol)
5151
{
5252
var startLength = _seenScopes.Count;
5353
_seenScopes.Push(_currentScope);
54-
_currentScope = _currentScope.ChildTypeScopes[typeSymbol];
54+
_currentScope = _currentScope.ChildTypeScopes[typeSymbol.Id];
5555
var result = base.VisitType(typeSymbol);
5656
_currentScope = _seenScopes.Pop();
5757
Debug.Assert(_seenScopes.Count == startLength);
@@ -84,11 +84,13 @@ protected override TypeReference VisitTypeReference(TypeReference typeReference)
8484
return new InternalTypeReference(foundChild!.Id);
8585
}
8686
}
87+
88+
return unresolvedTypeReference;
8789
}
8890
return base.VisitTypeReference(typeReference);
8991
}
9092

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

101103
foreach (var v in scope.ChildTypeScopes)
102104
{
103-
var type = v.Key;
105+
var typeId = v.Key;
106+
if (!TypeStore.TryResolve(typeId, out var type))
107+
{
108+
_logger.LogWarning("Failed to resolve Type ID {id}", typeId);
109+
continue;
110+
}
111+
Debug.Assert(type is not null);
104112

105-
var fullName = prefix + type.Identifier.Value;
113+
var fullName = prefix + type!.Identifier.Value;
106114
if (fullName == text)
107115
{
108116
foundChild = type;

src/generators/Silk.NET.SilkTouch.TypeResolution/TypeResolutionScope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ namespace Silk.NET.SilkTouch.TypeResolution;
99
/// Used to track types and child scopes, used to resolve <see cref="UnresolvedTypeReference"/>
1010
/// </summary>
1111
/// <param name="ChildTypeScopes">Mapping of child types to their scopes</param>
12-
public sealed record TypeResolutionScope(Dictionary<TypeSymbol, TypeResolutionScope> ChildTypeScopes)
12+
public sealed record TypeResolutionScope(Dictionary<TypeId, TypeResolutionScope> ChildTypeScopes)
1313
{
1414
}

src/generators/Silk.NET.SilkTouch.TypeResolution/TypeScopeSymbolVisitor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@ protected override TypeSymbol VisitType(TypeSymbol typeSymbol)
3434
var newScope = new TypeResolutionScope(new());
3535
var oldScope = _currentScope;
3636
var oldParent = _currentParent;
37-
_currentScope.ChildTypeScopes[typeSymbol] = newScope;
37+
_currentScope.ChildTypeScopes[typeSymbol.Id] = newScope;
3838
_currentScope = newScope;
3939
_currentParent = typeSymbol;
4040
var result = base.VisitType(typeSymbol);
4141
_currentScope = oldScope;
4242
_currentParent = oldParent;
4343
return result;
4444
}
45+
46+
/// <inheritdoc />
47+
protected override TypeReference VisitTypeReference(TypeReference typeReference)
48+
{
49+
if (typeReference is UnresolvedTypeReference)
50+
return typeReference;
51+
52+
return base.VisitTypeReference(typeReference);
53+
}
4554
}

tests/Silk.NET.SilkTouch.IntegrationTests/Silk.NET.SilkTouch.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Scraper\Silk.NET.SilkTouch.Scraper.csproj" />
2929
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Emitter\Silk.NET.SilkTouch.Emitter.csproj" />
3030
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.Symbols\Silk.NET.SilkTouch.Symbols.csproj" />
31+
<ProjectReference Include="..\..\src\generators\Silk.NET.SilkTouch.TypeResolution\Silk.NET.SilkTouch.TypeResolution.csproj" />
3132
<ProjectReference Include="..\Silk.NET.SilkTouch.TestFramework\Silk.NET.SilkTouch.TestFramework.csproj" />
3233
</ItemGroup>
3334

tests/Silk.NET.SilkTouch.IntegrationTests/StructIntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct { long k, l; } w;
6666
return Verifier.Verify(result);
6767
}
6868

69-
[Fact(Skip = "TODO: Reenable after proper type support"),
69+
[Fact,
7070
Trait("Category", "Integration"),
7171
Trait("Source Language", "C++"),
7272
Trait("Target Language", "C#"),
@@ -156,7 +156,7 @@ struct S
156156
return Verifier.Verify(result);
157157
}
158158

159-
[Fact(Skip = "TODO: Reenable after proper type support"),
159+
[Fact(Skip = "Nested Types?!"),
160160
Trait("Category", "Integration"),
161161
Trait("Source Language", "C++"),
162162
Trait("Target Language", "C#"),

tests/Silk.NET.SilkTouch.IntegrationTests/TestHelper.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Silk.NET.SilkTouch.Emitter;
1212
using Silk.NET.SilkTouch.Scraper;
1313
using Silk.NET.SilkTouch.Symbols;
14+
using Silk.NET.SilkTouch.TypeResolution;
1415
using Xunit;
1516

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

5051
var typeStore = new TypeStore();
51-
var symbols = scraper.ScrapeXML(xml!, typeStore);
52+
var symbols = scraper.ScrapeXML(xml!, typeStore).ToArray();
53+
54+
var typeScopeSymbolVisitor = ActivatorUtilities.CreateInstance<TypeScopeSymbolVisitor>(serviceProvider, typeStore);
55+
var processors = new SymbolVisitor[]
56+
{
57+
ActivatorUtilities.CreateInstance<PointerTypeResolver>(serviceProvider, typeStore),
58+
59+
typeScopeSymbolVisitor,
60+
ActivatorUtilities.CreateInstance<NameResolverSymbolVisitor>
61+
(serviceProvider, typeScopeSymbolVisitor.RootScope, typeStore)
62+
};
63+
64+
foreach (var processor in processors)
65+
{
66+
symbols = symbols.Select(processor.Visit).ToArray();
67+
}
68+
5269
var emitter = new CSharpEmitter();
5370
var outputs = symbols.Select(x => emitter.Transform(x, typeStore));
5471
return outputs.Aggregate

tests/Silk.NET.SilkTouch.TypeResolution.Tests/NameResolverSymbolVisitorTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ public void SelfTypeIsResolvedCorrectly()
2020
{
2121
new(new UnresolvedTypeReference("a"), new IdentifierSymbol("someField"))
2222
}.ToImmutableArray());
23+
var typeStore = new TypeStore();
24+
typeStore.Store(testType);
2325
var resolutionScope = new TypeResolutionScope
2426
(
25-
new Dictionary<TypeSymbol, TypeResolutionScope>
27+
new Dictionary<TypeId, TypeResolutionScope>
2628
{
27-
[testType] = new TypeResolutionScope(new())
29+
[testType.Id] = new TypeResolutionScope(new())
2830
}
2931
);
3032
var serviceProvider = Helpers.CreateServiceProvider();
3133
var visitor = new NameResolverSymbolVisitor
3234
(
3335
serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger<NameResolverSymbolVisitor>(),
3436
resolutionScope,
35-
new TypeStore()
37+
typeStore
3638
);
3739

3840
var resultType = visitor.Visit(testType);

tests/Silk.NET.SilkTouch.TypeResolution.Tests/TypeScopeSymbolVisitorTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void RootScopeContainsSingleRootType()
2020

2121
var rootScope = visitor.RootScope;
2222
var single = Assert.Single(rootScope.ChildTypeScopes);
23-
Assert.StrictEqual(testType, single.Key);
23+
Assert.StrictEqual(testType.Id, single.Key);
2424
var associatedScope = single.Value;
2525
Assert.Empty(associatedScope.ChildTypeScopes);
2626
}
@@ -55,7 +55,7 @@ public void RootScopeContainsSingleNamespacedType()
5555

5656
var rootScope = visitor.RootScope;
5757
var single = Assert.Single(rootScope.ChildTypeScopes);
58-
Assert.StrictEqual(testType, single.Key);
58+
Assert.StrictEqual(testType.Id, single.Key);
5959
var associatedScope = single.Value;
6060
Assert.Empty(associatedScope.ChildTypeScopes);
6161
}
@@ -86,19 +86,19 @@ public void RootScopeContainsMultipleNamespacedTypes()
8686
rootScope.ChildTypeScopes,
8787
(kv) =>
8888
{
89-
Assert.StrictEqual(testType1, kv.Key);
89+
Assert.StrictEqual(testType1.Id, kv.Key);
9090
var associatedScope = kv.Value;
9191
Assert.Empty(associatedScope.ChildTypeScopes);
9292
},
9393
(kv) =>
9494
{
95-
Assert.StrictEqual(testType2, kv.Key);
95+
Assert.StrictEqual(testType2.Id, kv.Key);
9696
var associatedScope = kv.Value;
9797
Assert.Empty(associatedScope.ChildTypeScopes);
9898
},
9999
(kv) =>
100100
{
101-
Assert.StrictEqual(testType3, kv.Key);
101+
Assert.StrictEqual(testType3.Id, kv.Key);
102102
var associatedScope = kv.Value;
103103
Assert.Empty(associatedScope.ChildTypeScopes);
104104
}

0 commit comments

Comments
 (0)