Skip to content

Commit d5fb352

Browse files
committed
Adding support for specifying a transparent struct kind
1 parent 7dd1e00 commit d5fb352

File tree

7 files changed

+445
-56
lines changed

7 files changed

+445
-56
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,12 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
15891589
var remappedName = FixupNameForMultipleHits(cxxMethodDecl);
15901590
var name = GetRemappedCursorName(cxxMethodDecl);
15911591
var needsReturnFixup = false;
1592+
var needsCastToTransparentStruct = false;
15921593

15931594
if (returnType.Kind != CXTypeKind.CXType_Void)
15941595
{
15951596
needsReturnFixup = NeedsReturnFixup(cxxMethodDecl);
1597+
needsCastToTransparentStruct = _config.WithTransparentStructs.TryGetValue(returnTypeName, out var transparentStruct) && IsTransparentStructHandle(transparentStruct.Kind);
15961598
}
15971599

15981600
var desc = new FunctionOrDelegateDesc<(string Name, PInvokeGenerator This)>
@@ -1652,6 +1654,13 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
16521654
body.Write("return ");
16531655
}
16541656

1657+
if (needsCastToTransparentStruct)
1658+
{
1659+
body.Write("((");
1660+
body.Write(returnTypeName);
1661+
body.Write(")(");
1662+
}
1663+
16551664
if (needsReturnFixup)
16561665
{
16571666
body.Write('*');
@@ -1748,6 +1757,11 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
17481757
body.Write(" != 0");
17491758
}
17501759

1760+
if (needsCastToTransparentStruct)
1761+
{
1762+
body.Write("))");
1763+
}
1764+
17511765
body.WriteSemicolon();
17521766
body.WriteNewline();
17531767

@@ -2743,9 +2757,9 @@ private void VisitVarDecl(VarDecl varDecl)
27432757
{
27442758
flags |= ValueFlags.Copy;
27452759
}
2746-
else if (_config.WithTransparentStructs.TryGetValue(typeName, out var transparentValueTypeName))
2760+
else if (_config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
27472761
{
2748-
typeName = transparentValueTypeName;
2762+
typeName = transparentStruct.Name;
27492763
}
27502764
}
27512765
else if ((varDecl.StorageClass == CX_StorageClass.CX_SC_Static) || openedOutputBuilder)

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,9 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
702702
{
703703
var cursorName = GetCursorName(varDecl);
704704

705-
if (cursorName.StartsWith("ClangSharpMacro_") && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentValueTypeName))
705+
if (cursorName.StartsWith("ClangSharpMacro_") && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
706706
{
707-
typeName = transparentValueTypeName;
707+
typeName = transparentStruct.Name;
708708
}
709709
}
710710

0 commit comments

Comments
 (0)