Skip to content

Commit

Permalink
Add support for avoiding pinning when returning a ref to an anonymous…
Browse files Browse the repository at this point in the history
… pointer field
  • Loading branch information
tannergooding committed Nov 2, 2021
1 parent eddd264 commit 72d76cb
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,7 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)

_outputBuilder.WriteRegularField(typeString, escapedName);

generateCompatibleCode |=
((type.CanonicalType is PointerType) || (type.CanonicalType is ReferenceType)) &&
(typeName != "IntPtr") && (typeName != "UIntPtr");
var isIndirectPointerField = ((type.CanonicalType is PointerType) || (type.CanonicalType is ReferenceType)) && (typeName != "IntPtr") && (typeName != "UIntPtr");

_outputBuilder.BeginBody();
_outputBuilder.BeginGetter(_config.GenerateAggressiveInlining);
Expand Down Expand Up @@ -831,9 +829,16 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
code.Write("MemoryMarshal.CreateSpan(ref ");
}

code.Write(contextName);
code.Write('.');
code.Write(escapedName);
if (isIndirectPointerField)
{
code.Write("this");
}
else
{
code.Write(contextName);
code.Write('.');
code.Write(escapedName);
}

if (isFixedSizedBuffer)
{
Expand All @@ -853,6 +858,15 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
}

code.Write(')');

if (isIndirectPointerField)
{
code.Write('.');
code.Write(contextName);
code.Write('.');
code.Write(escapedName);
}

code.WriteSemicolon();
code.WriteNewline();
_outputBuilder.EndCSharpCode(code);
Expand Down

0 comments on commit 72d76cb

Please sign in to comment.