Skip to content

Commit

Permalink
Merge pull request #313 from tannergooding/main
Browse files Browse the repository at this point in the history
Add support for marking methods as manual imports and fixing the PInvokeGeneratorConfiguration to be more forward compatible
  • Loading branch information
tannergooding committed Dec 19, 2021
2 parents 5416016 + 0f22171 commit d104db8
Show file tree
Hide file tree
Showing 21 changed files with 679 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public bool IsDllImport
}
}

public bool IsManualImport
{
get
{
return (Flags & FunctionOrDelegateFlags.IsManualImport) != 0;
}

set
{
Flags = value
? Flags | FunctionOrDelegateFlags.IsManualImport
: Flags & ~FunctionOrDelegateFlags.IsManualImport;
}
}

public bool HasFnPtrCodeGen
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal partial interface IOutputBuilder
void EndField(in FieldDesc desc);

void BeginFunctionOrDelegate(in FunctionOrDelegateDesc info, ref bool isMethodClassUnsafe);
void BeginFunctionInnerPrototype(string escapedName);
void BeginFunctionInnerPrototype(in FunctionOrDelegateDesc info);
void BeginParameter(in ParameterDesc info);
void BeginParameterDefault();
void EndParameterDefault();
void EndParameter(in ParameterDesc info);
void WriteParameterSeparator();
void EndFunctionInnerPrototype();
void EndFunctionInnerPrototype(in FunctionOrDelegateDesc info);
void BeginConstructorInitializer(string memberRefName, string memberInitName);
void EndConstructorInitializer();
void BeginBody(bool isExpressionBody = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ internal struct ParameterDesc
public Action<object> WriteCustomAttrs { get; set; }
public object CustomAttrGeneratorData { get; set; }
public CXSourceLocation? Location { get; set; }
public bool IsForManualImport { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM

WriteLine(']');
}
else if (desc.IsDllImport)
else if (desc.IsDllImport && !desc.IsManualImport)
{
AddUsingDirective("System.Runtime.InteropServices");

Expand Down Expand Up @@ -418,7 +418,6 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM
{
WriteIndentedLine("[SetsLastSystemError]");
}
// GenerateSetsLastSystemErrorAttribute

if (desc.IsAggressivelyInlined)
{
Expand All @@ -433,7 +432,7 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM
AddVtblIndexAttribute(vtblIndex);
}

if (desc.NativeTypeName is not null)
if ((desc.NativeTypeName is not null) && !desc.IsManualImport)
{
AddNativeTypeNameAttribute(desc.NativeTypeName, attributePrefix: "return: ");
}
Expand All @@ -460,7 +459,7 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM
}
Write("delegate ");
}
else if (desc.IsStatic ?? (desc.IsDllImport || !desc.IsCxx))
else if ((desc.IsStatic ?? (desc.IsDllImport || !desc.IsCxx)) && !desc.IsManualImport)
{
Write("static ");

Expand All @@ -473,28 +472,25 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM

if (!desc.IsVirtual)
{
//if (NeedsNewKeyword(escapedName, functionDecl.Parameters))
if (desc.NeedsNewKeyword)
{
Write("new ");
}

if (desc.IsUnsafe)
{
//if (cxxRecordDecl is null)
if (!desc.IsCtxCxxRecord)
{
isMethodClassUnsafe = true;
}
//else if (!IsUnsafe(cxxRecordDecl))
else if (!desc.IsCxxRecordCtxUnsafe)
{
Write("unsafe ");
}
}
}

if (!desc.IsCxxConstructor)
if (!desc.IsCxxConstructor && !desc.IsManualImport)
{
Write(desc.ReturnType);
Write(' ');
Expand Down Expand Up @@ -532,35 +528,59 @@ private void WriteSourceLocation(CXSourceLocation location, bool inline)
}
}

public void BeginFunctionInnerPrototype(string escapedName)
public void BeginFunctionInnerPrototype(in FunctionOrDelegateDesc desc)
{
Write(escapedName);
Write('(');
if (desc.IsManualImport)
{
Write("delegate* unmanaged");

if (desc.CallingConvention != CallingConvention.Winapi)
{
Write('[');
Write(desc.CallingConvention);
Write(']');
}

Write('<');
}
else
{
Write(desc.EscapedName);
Write('(');
}
}

public void BeginParameter(in ParameterDesc info)
{
if (info.NativeTypeName is not null)
if (info.IsForManualImport)
{
AddNativeTypeNameAttribute(info.NativeTypeName, prefix: "", postfix: " ");
Write(info.Type);
}

if (info.CppAttributes is not null)
else
{
AddCppAttributes(info.CppAttributes, prefix: "", postfix: " ");
}
if (info.NativeTypeName is not null)
{
AddNativeTypeNameAttribute(info.NativeTypeName, prefix: "", postfix: " ");
}

if (info.Location is {} location)
{
WriteSourceLocation(location, true);
}
if (info.CppAttributes is not null)
{
AddCppAttributes(info.CppAttributes, prefix: "", postfix: " ");
}

_customAttrIsForParameter = true;
info.WriteCustomAttrs?.Invoke(info.CustomAttrGeneratorData);
_customAttrIsForParameter = false;
Write(info.Type);
Write(' ');
Write(info.Name);
if (info.Location is { } location)
{
WriteSourceLocation(location, true);
}

_customAttrIsForParameter = true;
info.WriteCustomAttrs?.Invoke(info.CustomAttrGeneratorData);
_customAttrIsForParameter = false;

Write(info.Type);
Write(' ');
Write(info.Name);
}
}

public void BeginParameterDefault() => Write(" = ");
Expand All @@ -581,7 +601,18 @@ public void WriteParameterSeparator()
Write(' ');
}

public void EndFunctionInnerPrototype() => Write(')');
public void EndFunctionInnerPrototype(in FunctionOrDelegateDesc desc)
{
if (desc.IsManualImport)
{
Write(desc.ReturnType);
Write('>');
}
else
{
Write(')');
}
}

public void BeginConstructorInitializer(string memberRefName, string memberInitName)
{
Expand Down Expand Up @@ -646,6 +677,12 @@ public void EndBody(bool isExpressionBody = false)

public void EndFunctionOrDelegate(in FunctionOrDelegateDesc desc)
{
if (desc.IsManualImport)
{
Write(' ');
Write(desc.EscapedName);
}

if (!desc.HasBody || desc.IsVirtual)
{
WriteSemicolon();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<RootNamespace>ClangSharp</RootNamespace>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace ClangSharp
{
Expand All @@ -20,5 +20,6 @@ public enum FunctionOrDelegateFlags
IsNotStatic = 1 << 12,
NeedsReturnFixup = 1 << 13,
IsCxxConstructor = 1 << 14,
IsManualImport = 1 << 15,
}
}

0 comments on commit d104db8

Please sign in to comment.