mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
1 changed files with 768360 additions and 0 deletions
|
|
@ -0,0 +1,58 @@
|
|||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace Brutal.Interop.SourceGenerators;
|
||||
|
||||
[Generator]
|
||||
public class InteropHandleGenerator : IIncrementalGenerator
|
||||
{
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
var pipeline = context.SyntaxProvider.ForAttributeWithMetadataName(
|
||||
fullyQualifiedMetadataName: "Brutal.Interop.InteropHandleAttribute",
|
||||
predicate: static (node, _) => node is StructDeclarationSyntax,
|
||||
transform: static (ctx, _) => new Model(ctx)
|
||||
);
|
||||
|
||||
context.RegisterSourceOutput(pipeline, static (ctx, model) => model.Generate(ctx));
|
||||
}
|
||||
|
||||
private readonly record struct Model
|
||||
{
|
||||
private readonly string? _namespace;
|
||||
private readonly string _typeName;
|
||||
|
||||
public Model(GeneratorAttributeSyntaxContext context)
|
||||
{
|
||||
var symbol = (INamedTypeSymbol)context.TargetSymbol;
|
||||
_namespace = symbol.ContainingNamespace is { IsGlobalNamespace: false } ns
|
||||
? ns.ToDisplayString()
|
||||
: null;
|
||||
_typeName = symbol.Name;
|
||||
}
|
||||
|
||||
public void Generate(SourceProductionContext context)
|
||||
{
|
||||
var nsDecl = _namespace is not null ? $"namespace {_namespace};\n\n" : "";
|
||||
|
||||
var source = $$"""
|
||||
#nullable enable
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
using Brutal.Interop;
|
||||
|
||||
{{nsDecl}}[NativeMarshalling(typeof(InteropHandleMarshaller<{{_typeName}}, {{_typeName}}.Handle>))]
|
||||
public readonly unsafe partial struct {{_typeName}} : IInteropHandle<{{_typeName}}.Handle, {{_typeName}}>
|
||||
{
|
||||
public record struct Handle(nint Value);
|
||||
public required Handle Pointer { get; init; }
|
||||
public bool IsNull() => Pointer.Value == 0;
|
||||
public static implicit operator {{_typeName}}(void* ptr) => new() { Pointer = new Handle((nint)ptr) };
|
||||
}
|
||||
""";
|
||||
|
||||
context.AddSource($"{_typeName}.g.cs", SourceText.From(source, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue