Skip to content

Commit c6f3ff1

Browse files
committed
Init
1 parent b549af2 commit c6f3ff1

File tree

9 files changed

+151
-48
lines changed

9 files changed

+151
-48
lines changed

src/Files.App.CsWin32/Files.App.CsWin32.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
<PackageReference Include="Dongle.GuidRVAGen" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<ProjectReference Include="..\Files.Shared\Files.Shared.csproj" />
23+
<ProjectReference Include="..\Files.Core.SourceGenerator\Files.Core.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
24+
</ItemGroup>
25+
2126
</Project>

src/Files.App.CsWin32/IStorageProviderQuotaUI.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Files.Shared.Attributes;
45
using System;
5-
using System.Runtime.CompilerServices;
66
using Windows.Win32.Foundation;
77

88
namespace Windows.Win32.System.WinRT
99
{
1010
public unsafe partial struct IStorageProviderQuotaUI : IComIID
1111
{
12-
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
13-
private void** lpVtbl;
14-
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
12+
[GeneratedVTableFunction(Index = 6)]
13+
public partial HRESULT GetQuotaTotalInBytes(ulong* value);
1514

16-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17-
public HRESULT GetQuotaTotalInBytes(ulong* value)
18-
{
19-
return (HRESULT)((delegate* unmanaged[MemberFunction]<IStorageProviderQuotaUI*, ulong*, int>)(lpVtbl[6]))((IStorageProviderQuotaUI*)Unsafe.AsPointer(ref this), value);
20-
}
21-
22-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23-
public HRESULT GetQuotaUsedInBytes(ulong* value)
24-
{
25-
return (HRESULT)((delegate* unmanaged[MemberFunction]<IStorageProviderQuotaUI*, ulong*, int>)(lpVtbl[8]))((IStorageProviderQuotaUI*)Unsafe.AsPointer(ref this), value);
26-
}
15+
[GeneratedVTableFunction(Index = 8)]
16+
public partial HRESULT GetQuotaUsedInBytes(ulong* value);
2717

2818
[GuidRVAGen.Guid("BA6295C3-312E-544F-9FD5-1F81B21F3649")]
2919
public static partial ref readonly Guid Guid { get; }

src/Files.App.CsWin32/IStorageProviderStatusUI.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Files.Shared.Attributes;
45
using System;
5-
using System.Runtime.CompilerServices;
66
using Windows.Win32.Foundation;
77

88
namespace Windows.Win32.System.WinRT
99
{
1010
public unsafe partial struct IStorageProviderStatusUI : IComIID
1111
{
12-
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
13-
private void** lpVtbl;
14-
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
15-
16-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17-
public HRESULT GetQuotaUI(IStorageProviderQuotaUI** result)
18-
{
19-
return (HRESULT)((delegate* unmanaged[MemberFunction]<IStorageProviderStatusUI*, IStorageProviderQuotaUI**, int>)lpVtbl[14])((IStorageProviderStatusUI*)Unsafe.AsPointer(ref this), result);
20-
}
12+
[GeneratedVTableFunction(Index = 14)]
13+
public partial HRESULT GetQuotaUI(IStorageProviderQuotaUI** result);
2114

2215
[GuidRVAGen.Guid("D6B6A758-198D-5B80-977F-5FF73DA33118")]
2316
public static partial ref readonly Guid Guid { get; }

src/Files.App.CsWin32/IStorageProviderStatusUISource.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Files.Shared.Attributes;
45
using System;
5-
using System.Runtime.CompilerServices;
66
using Windows.Win32.Foundation;
77

88
namespace Windows.Win32.System.WinRT
99
{
1010
public unsafe partial struct IStorageProviderStatusUISource : IComIID
1111
{
12-
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
13-
private void** lpVtbl;
14-
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
15-
16-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17-
public HRESULT GetStatusUI(IStorageProviderStatusUI** result)
18-
{
19-
return (HRESULT)((delegate* unmanaged[MemberFunction]<IStorageProviderStatusUISource*, IStorageProviderStatusUI**, int>)lpVtbl[6])((IStorageProviderStatusUISource*)Unsafe.AsPointer(ref this), result);
20-
}
12+
[GeneratedVTableFunction(Index = 6)]
13+
public partial HRESULT GetStatusUI(IStorageProviderStatusUI** result);
2114

2215
[GuidRVAGen.Guid("A306C249-3D66-5E70-9007-E43DF96051FF")]
2316
public static partial ref readonly Guid Guid { get; }

src/Files.App.CsWin32/IStorageProviderStatusUISourceFactory.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Files.Shared.Attributes;
45
using System;
5-
using System.Runtime.CompilerServices;
66
using Windows.Win32.Foundation;
77

88
namespace Windows.Win32.System.WinRT
99
{
1010
public unsafe partial struct IStorageProviderStatusUISourceFactory : IComIID
1111
{
12-
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
13-
private void** lpVtbl;
14-
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
15-
16-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17-
public HRESULT GetStatusUISource(nint syncRootId, IStorageProviderStatusUISource** result)
18-
{
19-
return (HRESULT)((delegate* unmanaged[MemberFunction]<IStorageProviderStatusUISourceFactory*, nint, IStorageProviderStatusUISource**, int>)lpVtbl[6])((IStorageProviderStatusUISourceFactory*)Unsafe.AsPointer(ref this), syncRootId, result);
20-
}
12+
[GeneratedVTableFunction(Index = 6)]
13+
public partial HRESULT GetStatusUISource(nint syncRootId, IStorageProviderStatusUISource** result);
2114

2215
[GuidRVAGen.Guid("12E46B74-4E5A-58D1-A62F-0376E8EE7DD8")]
2316
public static partial ref readonly Guid Guid { get; }

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ WinVerifyTrust
251251
FileTimeToSystemTime
252252
FileTimeToLocalFileTime
253253
SystemTimeToFileTime
254-
CRYPTOAPI_BLOB
255254
CMSG_SIGNER_INFO
256-
SignDataHandle
257255
CRYPT_ATTRIBUTE
258256
FILETIME
259257
CRYPT_BIT_BLOB
@@ -266,6 +264,5 @@ CATALOG_INFO
266264
WINTRUST_FILE_INFO
267265
WINTRUST_DATA
268266
HCERTSTORE
269-
HCRYPTMSG
270267
CERT_QUERY_ENCODING_TYPE
271268
CertGetNameString
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.Core.SourceGenerator.Data
5+
{
6+
internal class VTableFunctionInfo
7+
{
8+
public required string Name { get; init; }
9+
10+
public required string ReturnType { get; init; }
11+
12+
public required Dictionary<string, string> Parameters { get; init; }
13+
}
14+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.CodeAnalysis;
5+
6+
namespace Files.Core.SourceGenerator.Generators
7+
{
8+
[Generator(LanguageNames.CSharp)]
9+
internal class VTableFunctionGenerator : IIncrementalGenerator
10+
{
11+
public void Initialize(IncrementalGeneratorInitializationContext context)
12+
{
13+
var sources = context.SyntaxProvider.ForAttributeWithMetadataName(
14+
"Files.Shared.Attributes.GeneratedVTableFunctionAttribute",
15+
static (node, token) => true,
16+
static (context, token) => context)
17+
.Collect();
18+
19+
context.RegisterSourceOutput(sources, (context, sources) =>
20+
{
21+
var vtableFunctionsGroupedByStructs = sources.GroupBy(source => source.TargetSymbol.ContainingType, SymbolEqualityComparer.Default);
22+
23+
foreach (var vtableFunctions in vtableFunctionsGroupedByStructs)
24+
{
25+
if (vtableFunctions.Key is not INamedTypeSymbol structSymbol || structSymbol.Name is not { } structName)
26+
continue;
27+
28+
string vtableFunctionsCode = GenerateVtableFunctionsForStruct(structSymbol, vtableFunctions);
29+
context.AddSource($"{structName}_VTableFunctions.g.cs", vtableFunctionsCode);
30+
}
31+
});
32+
}
33+
34+
private string GenerateVtableFunctionsForStruct(INamedTypeSymbol structSymbol, IEnumerable<GeneratorAttributeSyntaxContext> sources)
35+
{
36+
StringBuilder builder = new();
37+
38+
builder.AppendLine($"// <auto-generated/>");
39+
builder.AppendLine();
40+
builder.AppendLine($"using global::System.Runtime.CompilerServices;");
41+
builder.AppendLine();
42+
builder.AppendLine($"#pragma warning disable");
43+
builder.AppendLine();
44+
45+
if (structSymbol.ContainingNamespace is { IsGlobalNamespace: false })
46+
{
47+
builder.AppendLine($"namespace {structSymbol.ContainingNamespace};");
48+
builder.AppendLine();
49+
}
50+
51+
builder.AppendLine($"public unsafe partial struct {structSymbol.Name}");
52+
builder.AppendLine($"{{");
53+
54+
builder.AppendLine($" private void** lpVtbl;");
55+
builder.AppendLine();
56+
57+
var sourceIndex = 0;
58+
var sourceCount = sources.Count();
59+
60+
foreach (var source in sources)
61+
{
62+
var vtblIndex = source.Attributes[0].NamedArguments.Where(x => x.Key.Equals("Index")).FirstOrDefault().Value;
63+
var info = GetVTableFunctionInfo((IMethodSymbol)source.TargetSymbol);
64+
65+
builder.AppendLine($" [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]");
66+
67+
builder.AppendLine($" public partial {info.ReturnType} {info.Name}({string.Join(", ", info.Parameters.Select(x => $"{x.Key} {x.Value}"))})");
68+
builder.AppendLine($" {{");
69+
builder.AppendLine($" return ({info.ReturnType})((delegate* unmanaged[MemberFunction]<{structSymbol.Name}*, {string.Join(", ", info.Parameters.Select(x => $"{x.Key}"))}, int>)(lpVtbl[{vtblIndex.Value}]))");
70+
builder.AppendLine($" (({structSymbol.Name}*)global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref this), {string.Join(", ", info.Parameters.Select(x => $"{x.Value}"))});");
71+
builder.AppendLine($" }}");
72+
73+
if (sourceIndex < sourceCount - 1)
74+
builder.AppendLine();
75+
76+
sourceIndex++;
77+
}
78+
79+
builder.AppendLine($"}}");
80+
81+
return builder.ToString();
82+
}
83+
84+
private VTableFunctionInfo GetVTableFunctionInfo(IMethodSymbol methodSymbol)
85+
{
86+
string functionName = methodSymbol.Name;
87+
string returnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
88+
89+
Dictionary<string, string> parameters = [];
90+
foreach (var param in methodSymbol.Parameters)
91+
{
92+
var name = param.Name;
93+
var type = param.Type;
94+
95+
parameters.Add(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), name);
96+
}
97+
98+
return new VTableFunctionInfo()
99+
{
100+
Name = functionName,
101+
ReturnType = returnType,
102+
Parameters = parameters,
103+
};
104+
}
105+
}
106+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Files.Shared.Attributes;
7+
8+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
9+
public sealed class GeneratedVTableFunctionAttribute : Attribute
10+
{
11+
public required int Index { get; init; }
12+
}

0 commit comments

Comments
 (0)