Skip to content

Commit

Permalink
Merge pull request #285 from tannergooding/main
Browse files Browse the repository at this point in the history
Adding support for specifying a transparent struct kind
  • Loading branch information
tannergooding authored Oct 31, 2021
2 parents 7dd1e00 + 6537e4b commit eddd264
Show file tree
Hide file tree
Showing 7 changed files with 455 additions and 56 deletions.
18 changes: 16 additions & 2 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,10 +1589,12 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
var remappedName = FixupNameForMultipleHits(cxxMethodDecl);
var name = GetRemappedCursorName(cxxMethodDecl);
var needsReturnFixup = false;
var needsCastToTransparentStruct = false;

if (returnType.Kind != CXTypeKind.CXType_Void)
{
needsReturnFixup = NeedsReturnFixup(cxxMethodDecl);
needsCastToTransparentStruct = _config.WithTransparentStructs.TryGetValue(returnTypeName, out var transparentStruct) && IsTransparentStructHandle(transparentStruct.Kind);
}

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

if (needsCastToTransparentStruct)
{
body.Write("((");
body.Write(returnTypeName);
body.Write(")(");
}

if (needsReturnFixup)
{
body.Write('*');
Expand Down Expand Up @@ -1748,6 +1757,11 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
body.Write(" != 0");
}

if (needsCastToTransparentStruct)
{
body.Write("))");
}

body.WriteSemicolon();
body.WriteNewline();

Expand Down Expand Up @@ -2743,9 +2757,9 @@ private void VisitVarDecl(VarDecl varDecl)
{
flags |= ValueFlags.Copy;
}
else if (_config.WithTransparentStructs.TryGetValue(typeName, out var transparentValueTypeName))
else if (_config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
{
typeName = transparentValueTypeName;
typeName = transparentStruct.Name;
}
}
else if ((varDecl.StorageClass == CX_StorageClass.CX_SC_Static) || openedOutputBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,9 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
{
var cursorName = GetCursorName(varDecl);

if (cursorName.StartsWith("ClangSharpMacro_") && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentValueTypeName))
if (cursorName.StartsWith("ClangSharpMacro_") && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
{
typeName = transparentValueTypeName;
typeName = transparentStruct.Name;
}
}

Expand Down
Loading

0 comments on commit eddd264

Please sign in to comment.