Skip to content

Commit

Permalink
Further improve the handling for various transparent structs
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Nov 2, 2021
1 parent 72d76cb commit f4287cd
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 333 deletions.
22 changes: 11 additions & 11 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,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 @@ -1949,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 @@ -2028,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 @@ -2060,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 @@ -2100,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 @@ -2184,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 f4287cd

Please sign in to comment.