Skip to content

Commit 0c506f0

Browse files
committed
Add docs for ExtractHandles and add ExtractHandlesTests matching the example given in the docs
1 parent 1a10b65 commit 0c506f0

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

docs/for-contributors/Generator/generator-mods.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,47 @@ This mod should be positioned at the start of the mod order, after `AddIncludes`
156156

157157
Mod categories: Creation
158158

159+
This mod adds empty structs for missing types that are identified to be used as handle types. To be a handle type, the
160+
type must only be ever referenced through a pointer. After the empty struct representing the handle type is extracted,
161+
`TransformHandles` can then be used to transform the pointer to be wrapped within the handle struct.
162+
163+
Side note: This mod is similar to `AddOpaqueStructs` in that it adds empty structs, but `ExtractHandles` has a much more
164+
automated approach since it deals specifically with handle types that are referenced using pointers.
165+
166+
This code has been manually trimmed for the sake of example and comes from the state of the Vulkan bindings before
167+
`ExtractHandles` executes. In this case, `VkInstance_T` will be identified as a missing handle type and an empty struct
168+
will be added for it. On the other hand, `VkInstanceCreateInfo` and `VkAllocationCallbacks` will not be affected since
169+
they already exist. Similarly, `VkResult` is not affected because it is not referenced through a pointer.
170+
```cs
171+
public struct VkAllocationCallbacks;
172+
public struct VkInstanceCreateInfo;
173+
174+
public class Vk
175+
{
176+
public static extern VkResult vkCreateInstance(
177+
VkInstanceCreateInfo* pCreateInfo,
178+
VkAllocationCallbacks* pAllocator,
179+
VkInstance_T** pInstance
180+
);
181+
}
182+
```
183+
184+
The result of running `ExtractHandles` on the above code will lead to the creation of a new type:
185+
```cs
186+
public unsafe partial struct VkInstance_T
187+
{
188+
}
189+
```
190+
159191
Usage recommendations:
160192

193+
This mod should be used if a set of bindings contains types referenced only through pointers and those pointers are
194+
missing from the final set of generated bindings.
195+
196+
Furthermore, this mod should be used alongside `TransformHandles` so that the handles are transformed into a more
197+
user-friendly version. `ExtractHandles` should be positioned before `TransformHandles` and any other mods that might use
198+
its results in the mod order.
199+
161200
### ExtractNestedTyping
162201

163202
Mod categories: Creation
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public unsafe partial struct VkInstance_T
2+
{
3+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.Extensions.Logging.Abstractions;
6+
using Silk.NET.SilkTouch.Mods;
7+
8+
namespace Silk.NET.SilkTouch.UnitTests;
9+
10+
public class ExtractHandlesTests
11+
{
12+
static ExtractHandlesTests()
13+
{
14+
if (!VerifyDiffPlex.Initialized)
15+
{
16+
VerifyDiffPlex.Initialize();
17+
}
18+
}
19+
20+
[Test]
21+
public async Task SuccessfullyExtractsHandleType()
22+
{
23+
var inputDocName = "Vk.gen.cs";
24+
var project = TestUtils
25+
.CreateTestProject()
26+
.AddDocument(
27+
inputDocName,
28+
"""
29+
public struct VkAllocationCallbacks;
30+
public struct VkInstanceCreateInfo;
31+
32+
public class Vk
33+
{
34+
public static extern VkResult vkCreateInstance(
35+
VkInstanceCreateInfo* pCreateInfo,
36+
VkAllocationCallbacks* pAllocator,
37+
VkInstance_T** pInstance
38+
);
39+
}
40+
"""
41+
)
42+
.Project;
43+
44+
var context = new DummyModContext() { SourceProject = project };
45+
46+
var extractHandles = new ExtractHandles(NullLogger<ExtractHandles>.Instance);
47+
48+
await extractHandles.ExecuteAsync(context);
49+
50+
// There should be an empty struct named VkInstance_T in a new file
51+
var result = await context
52+
.SourceProject.Documents.First(x => x.Name != inputDocName)
53+
.GetSyntaxRootAsync();
54+
await Verify(result!.NormalizeWhitespace().ToString());
55+
}
56+
}

0 commit comments

Comments
 (0)