Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for avoiding pinning when returning a ref to an anonymous pointer field #286

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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