Skip to content

Commit

Permalink
Merge pull request #286 from tannergooding/main
Browse files Browse the repository at this point in the history
Add support for avoiding pinning when returning a ref to an anonymous pointer field
  • Loading branch information
tannergooding authored Nov 2, 2021
2 parents eddd264 + f4287cd commit 79fea72
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 339 deletions.
48 changes: 31 additions & 17 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 Expand Up @@ -1818,8 +1832,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,
Type typeBacking;
string typeNameBacking;

if ((!_config.GenerateUnixTypes && (currentSize != previousSize)) ||
(fieldDecl.BitWidthValue > remainingBits))
if ((!_config.GenerateUnixTypes && (currentSize != previousSize)) || (fieldDecl.BitWidthValue > remainingBits))
{
if (index >= 0)
{
Expand Down Expand Up @@ -1935,9 +1948,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,

default:
{
AddDiagnostic(DiagnosticLevel.Warning,
$"Unsupported bitfield type: '{canonicalTypeBacking.TypeClassSpelling}'. Generated bindings may be incomplete.",
fieldDecl);
AddDiagnostic(DiagnosticLevel.Warning, $"Unsupported bitfield type: '{canonicalTypeBacking.TypeClassSpelling}'. Generated bindings may be incomplete.", fieldDecl);
break;
}
}
Expand Down Expand Up @@ -2014,9 +2025,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,

default:
{
AddDiagnostic(DiagnosticLevel.Warning,
$"Unsupported bitfield type: '{canonicalType.TypeClassSpelling}'. Generated bindings may be incomplete.",
fieldDecl);
AddDiagnostic(DiagnosticLevel.Warning, $"Unsupported bitfield type: '{canonicalType.TypeClassSpelling}'. Generated bindings may be incomplete.", fieldDecl);
break;
}
}
Expand Down Expand Up @@ -2046,7 +2055,12 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,

code.WriteIndented("return ");

if ((currentSize < 4) || (canonicalTypeBacking != canonicalType))
var recordDeclName = GetCursorName(recordDecl);

var isRemappedToSelf = _config.RemappedNames.TryGetValue(typeName, out var remappedTypeName) && typeName.Equals(remappedTypeName);
var needsCast = (currentSize < 4) || (canonicalTypeBacking != canonicalType) || isRemappedToSelf;

if (needsCast)
{
code.Write('(');
code.BeginMarker("typeName");
Expand Down Expand Up @@ -2086,7 +2100,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,
code.Write(bitwidthHexStringBacking);
code.EndMarker("bitwidthHexStringBacking");

if ((currentSize < 4) || (canonicalTypeBacking != canonicalType))
if (needsCast)
{
code.Write(')');
}
Expand Down Expand Up @@ -2170,7 +2184,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,
code.Write('(');
}

if (canonicalType is EnumType)
if ((canonicalType is EnumType) || isRemappedToSelf)
{
code.Write('(');
code.Write(typeNameBacking);
Expand Down
Loading

0 comments on commit 79fea72

Please sign in to comment.