try to make grouping generator

This commit is contained in:
neuecc 2023-04-15 01:54:15 +09:00
parent a8311b8231
commit d6054fe092
8 changed files with 4382 additions and 9 deletions

4
.gitignore vendored
View File

@ -6,6 +6,8 @@
.vs/
dotnet-sandbox/obj/
dotnet-sandbox/bin/
GroupedNativeMethodsGenerator/obj/
GroupedNativeMethodsGenerator/bin/
unity-sandbox/obj/
unity-sandbox/bin/
unity-sandbox/Temp/
@ -13,6 +15,4 @@ unity-sandbox/*.csproj
unity-sandbox/*.csproj.user
unity-sandbox/*.sln
unity-sandbox/Library
unity-sandbox/Logs/

View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33414.496
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CsbindgenDotnetConsoleApp", "dotnet-sandbox\CsbindgenDotnetConsoleApp.csproj", "{9FCF940C-368E-41C5-B83A-CFDEC01E2B70}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GroupedNativeMethodsGenerator", "GroupedNativeMethodsGenerator\GroupedNativeMethodsGenerator.csproj", "{C63592D1-9C02-43BF-A4E0-C23D4B1C0ACE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9FCF940C-368E-41C5-B83A-CFDEC01E2B70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9FCF940C-368E-41C5-B83A-CFDEC01E2B70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FCF940C-368E-41C5-B83A-CFDEC01E2B70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FCF940C-368E-41C5-B83A-CFDEC01E2B70}.Release|Any CPU.Build.0 = Release|Any CPU
{C63592D1-9C02-43BF-A4E0-C23D4B1C0ACE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C63592D1-9C02-43BF-A4E0-C23D4B1C0ACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C63592D1-9C02-43BF-A4E0-C23D4B1C0ACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C63592D1-9C02-43BF-A4E0-C23D4B1C0ACE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8FAD332E-1845-455E-AB46-3F45699D2335}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,71 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace GroupedNativeMethodsGenerator;
[Generator(LanguageNames.CSharp)]
public partial class GroupedNativeMethodsGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(ctx =>
{
ctx.AddSource("GroupedNativeMethodsGenerator.Attribute.cs", """
namespace GroupedNativeMethodsGenerator
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class GroupedNativeMethodsAttribute : Attribute
{
public string RemovePrefix { get; set; } = "";
public bool RemoveUntilTypeName { get; set; } = true;
/// <summary>Fix method name as C# Style, e.g.: foo_bar_baz() -> FooBarBaz()</summary>
public bool FixMethodName { get; set; } = true;
}
}
""");
});
var source = context.SyntaxProvider.ForAttributeWithMetadataName("GroupedNativeMethodsGenerator.GroupedNativeMethodsAttribute",
(node, token) => node is ClassDeclarationSyntax,
(ctx, token) => ctx);
context.RegisterSourceOutput(source, Emit);
}
static void Emit(SourceProductionContext context, GeneratorAttributeSyntaxContext source)
{
var typeSymbol = (INamedTypeSymbol)source.TargetSymbol;
var typeNode = (TypeDeclarationSyntax)source.TargetNode;
var ns = typeSymbol.ContainingNamespace.IsGlobalNamespace
? ""
: $"namespace {typeSymbol.ContainingNamespace};";
var fullType = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
.Replace("global::", "")
.Replace("<", "_")
.Replace(">", "_");
var methods = typeSymbol.GetMembers()
.OfType<IMethodSymbol>();
// context.AddSource($"{fullType}.SampleGenerator.g.cs", code);
}
}
// TODO:...
public static class DiagnosticDescriptors
{
const string Category = "SampleGenerator";
public static readonly DiagnosticDescriptor ExistsOverrideToString = new(
id: "SAMPLE001",
title: "ToString override",
messageFormat: "The GenerateToString class '{0}' has ToString override but it is not allowed.",
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
<IsRoslynComponent>true</IsRoslynComponent>
<AnalyzerLanguage>cs</AnalyzerLanguage>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,8 @@
{
"profiles": {
"P1": {
"commandName": "DebugRoslynComponent",
"targetProject": "..\\dotnet-sandbox\\CsbindgenDotnetConsoleApp.csproj"
}
}
}

View File

@ -0,0 +1,50 @@
using CsBindgen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CsbindgenDotnetConsoleApp
{
// Concept for grouping extension
internal static unsafe class BindingGroupExtensions
{
// public static extern bool quiche_conn_set_keylog_path(quiche_conn* conn, byte* path);
// public static extern nuint quiche_conn_max_send_udp_payload_size(quiche_conn* conn);
// public static extern int quiche_conn_close(quiche_conn* conn, [MarshalAs(UnmanagedType.U1)] bool app, ulong err, byte* reason, nuint reason_len);
public static bool SetKeylogPath(this ref quiche_conn conn, byte* path)
{
return LibQuiche.quiche_conn_set_keylog_path((quiche_conn*)Unsafe.AsPointer(ref conn), path);
}
public static nuint MaxSendUdpPayloadSize(this ref quiche_conn conn)
{
return LibQuiche.quiche_conn_max_send_udp_payload_size((quiche_conn*)Unsafe.AsPointer(ref conn));
}
public static int MaxSendUdpPayloadSize(this ref quiche_conn conn, bool app, ulong err, byte* reason, nuint reason_len)
{
return LibQuiche.quiche_conn_close((quiche_conn*)Unsafe.AsPointer(ref conn), app, err, reason, reason_len);
}
public static bool Hoge(quiche_conn* conn, byte* path)
{
return conn->SetKeylogPath(path);
}
}
}
namespace Physx
{
[GroupedNativeMethodsGenerator.GroupedNativeMethods]
internal static unsafe partial class LibPhysxd
{
}
}

View File

@ -8,13 +8,6 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<!--<Compile Remove="bullet3_bindgen.cs" />
<Compile Remove="lz4_bindgen.cs" />
<Compile Remove="quiche_bindgen.cs" />
<Compile Remove="zstd_bindgen.cs" />-->
</ItemGroup>
<ItemGroup>
<None Include="../target/debug/csbindgen_tests.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -24,4 +17,11 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GroupedNativeMethodsGenerator\GroupedNativeMethodsGenerator.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
</Project>

4197
dotnet-sandbox/jolt_bindgen.cs vendored Normal file

File diff suppressed because it is too large Load Diff