Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent fnptr structs #521

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

namespace ClangSharp.Abstractions;

internal struct TransparentStructDesc
{
public string ParentName { get; set; }
public string Name { get; set; }
public string? NativeName { get; set; }
public string Type { get; set; }
public string? NativeType { get; set; }
public PInvokeGeneratorTransparentStructKind Kind { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private void AddVtblIndexAttribute(long vtblIndex, string? prefix = null, string
}
}

private void AddNativeTypeNameAttribute(string nativeTypeName, string? prefix = null, string? postfix = null, string? attributePrefix = null)
public void AddNativeTypeNameAttribute(string nativeTypeName, string? prefix = null, string? postfix = null, string? attributePrefix = null)
{
foreach (var entry in _generator.Config.NativeTypeNamesToStrip)
{
Expand Down
36 changes: 29 additions & 7 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
{
outputBuilder.Write("Base");
}

outputBuilder.Write('.');
outputBuilder.Write(name);
outputBuilder.Write('(');
Expand Down Expand Up @@ -3194,22 +3194,44 @@ private void VisitTypedefDecl(TypedefDecl typedefDecl, bool onlyHandleRemappings

void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionProtoType, Type? parentType, bool onlyHandleRemappings)
{
if (!_config.ExcludeFnptrCodegen || onlyHandleRemappings)
var hasOutput = _config.GenerateFnPtrWrapper || _config.ExcludeFnptrCodegen;
if (!hasOutput || onlyHandleRemappings)
{
return;
}

var name = GetRemappedCursorName(typedefDecl);
var escapedName = EscapeName(name);

var callingConventionName = GetCallingConvention(typedefDecl, context: null, typedefDecl.TypeForDecl);
StartUsingOutputBuilder(name);
Debug.Assert(_outputBuilder is not null);

var returnType = functionProtoType.ReturnType;
var returnTypeName = GetRemappedTypeName(typedefDecl, context: null, returnType, out var nativeTypeName);
if (_config.GenerateFnPtrWrapper)
{
var type = GetTypeName(typedefDecl, null, typedefDecl.TypeForDecl, true, false, out var nativeName);
_ = GetTypeName(typedefDecl, null, typedefDecl.UnderlyingType, true, false, out var nativeType);

StartUsingOutputBuilder(name);
if (IsNativeTypeNameEquivalent(nativeName, name))
{
nativeName = null;
}

var desc = new TransparentStructDesc() {
ParentName = name,
Name = escapedName,
NativeName = nativeName,
Type = type,
NativeType = nativeType,
Kind = PInvokeGeneratorTransparentStructKind.FnPtr
};
GenerateTransparentStruct(desc);
}
else
{
Debug.Assert(_outputBuilder is not null);
var callingConventionName = GetCallingConvention(typedefDecl, context: null, typedefDecl.TypeForDecl);

var returnType = functionProtoType.ReturnType;
var returnTypeName = GetRemappedTypeName(typedefDecl, context: null, returnType, out var nativeTypeName);

var desc = new FunctionOrDelegateDesc {
AccessSpecifier = GetAccessSpecifier(typedefDecl, matchStar: true),
Expand Down
Loading