Skip to content
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
44 changes: 20 additions & 24 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ private void VisitDecl(Decl decl)
{
if (IsExcluded(decl))
{
if (decl.Kind == CX_DeclKind.CX_DeclKind_Typedef)
{
VisitTypedefDecl((TypedefDecl)decl, onlyHandleRemappings: true);
}
return;
}

Expand Down Expand Up @@ -138,7 +134,7 @@ private void VisitDecl(Decl decl)

case CX_DeclKind.CX_DeclKind_Typedef:
{
VisitTypedefDecl((TypedefDecl)decl, onlyHandleRemappings: false);
VisitTypedefDecl((TypedefDecl)decl);
break;
}

Expand Down Expand Up @@ -2830,13 +2826,13 @@ private void VisitTypeAliasDecl(TypeAliasDecl typeAliasDecl)
// Nothing to generate for type alias declarations
}

private void VisitTypedefDecl(TypedefDecl typedefDecl, bool onlyHandleRemappings)
private void VisitTypedefDecl(TypedefDecl typedefDecl)
{
ForUnderlyingType(typedefDecl, typedefDecl.UnderlyingType, onlyHandleRemappings);
ForUnderlyingType(typedefDecl, typedefDecl.UnderlyingType);

void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionProtoType, Type parentType, bool onlyHandleRemappings)
void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionProtoType, Type parentType)
{
if (!_config.ExcludeFnptrCodegen || onlyHandleRemappings)
if (!_config.ExcludeFnptrCodegen)
{
return;
}
Expand Down Expand Up @@ -2883,43 +2879,43 @@ void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionPro
StopUsingOutputBuilder();
}

void ForPointeeType(TypedefDecl typedefDecl, Type parentType, Type pointeeType, bool onlyHandleRemappings)
void ForPointeeType(TypedefDecl typedefDecl, Type parentType, Type pointeeType)
{
if (pointeeType is AttributedType attributedType)
{
ForPointeeType(typedefDecl, attributedType, attributedType.ModifiedType, onlyHandleRemappings);
ForPointeeType(typedefDecl, attributedType, attributedType.ModifiedType);
}
else if (pointeeType is ElaboratedType elaboratedType)
{
ForPointeeType(typedefDecl, elaboratedType, elaboratedType.NamedType, onlyHandleRemappings);
ForPointeeType(typedefDecl, elaboratedType, elaboratedType.NamedType);
}
else if (pointeeType is FunctionProtoType functionProtoType)
{
ForFunctionProtoType(typedefDecl, functionProtoType, parentType, onlyHandleRemappings);
ForFunctionProtoType(typedefDecl, functionProtoType, parentType);
}
else if (pointeeType is PointerType pointerType)
{
ForPointeeType(typedefDecl, pointerType, pointerType.PointeeType, onlyHandleRemappings);
ForPointeeType(typedefDecl, pointerType, pointerType.PointeeType);
}
else if (pointeeType is TypedefType typedefType)
{
ForPointeeType(typedefDecl, typedefType, typedefType.Decl.UnderlyingType, onlyHandleRemappings);
ForPointeeType(typedefDecl, typedefType, typedefType.Decl.UnderlyingType);
}
else if (pointeeType is not ConstantArrayType and not BuiltinType and not TagType and not TemplateTypeParmType)
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported pointee type: '{pointeeType.TypeClassSpelling}'. Generating bindings may be incomplete.", typedefDecl);
}
}

void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHandleRemappings)
void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType)
{
if (underlyingType is ArrayType arrayType)
{
// Nothing to do for array types
}
else if (underlyingType is AttributedType attributedType)
{
ForUnderlyingType(typedefDecl, attributedType.ModifiedType, onlyHandleRemappings);
ForUnderlyingType(typedefDecl, attributedType.ModifiedType);
}
else if (underlyingType is BuiltinType builtinType)
{
Expand All @@ -2931,19 +2927,19 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
}
else if (underlyingType is ElaboratedType elaboratedType)
{
ForUnderlyingType(typedefDecl, elaboratedType.NamedType, onlyHandleRemappings);
ForUnderlyingType(typedefDecl, elaboratedType.NamedType);
}
else if (underlyingType is FunctionProtoType functionProtoType)
{
ForFunctionProtoType(typedefDecl, functionProtoType, parentType: null, onlyHandleRemappings);
ForFunctionProtoType(typedefDecl, functionProtoType, parentType: null);
}
else if (underlyingType is PointerType pointerType)
{
ForPointeeType(typedefDecl, parentType: null, pointerType.PointeeType, onlyHandleRemappings);
ForPointeeType(typedefDecl, parentType: null, pointeeType: pointerType.PointeeType);
}
else if (underlyingType is ReferenceType referenceType)
{
ForPointeeType(typedefDecl, parentType: null, referenceType.PointeeType, onlyHandleRemappings);
ForPointeeType(typedefDecl, parentType: null, pointeeType: referenceType.PointeeType);
}
else if (underlyingType is TagType underlyingTagType)
{
Expand All @@ -2965,7 +2961,7 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
{
if (templateSpecializationType.IsTypeAlias)
{
ForUnderlyingType(typedefDecl, templateSpecializationType.AliasedType, onlyHandleRemappings);
ForUnderlyingType(typedefDecl, templateSpecializationType.AliasedType);
}
else
{
Expand All @@ -2978,7 +2974,7 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
}
else if (underlyingType is TypedefType typedefType)
{
ForUnderlyingType(typedefDecl, typedefType.Decl.UnderlyingType, onlyHandleRemappings);
ForUnderlyingType(typedefDecl, typedefType.Decl.UnderlyingType);
}
else
{
Expand Down Expand Up @@ -3636,7 +3632,7 @@ private bool IsPrimitiveValue(Type type)
{
return IsPrimitiveValue(autoType.CanonicalType);
}
else if (type is BuiltinType builtinType)
else if (type is BuiltinType)
{
switch (type.Kind)
{
Expand Down
18 changes: 18 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,24 @@ private string GetTypeName(Cursor cursor, Cursor context, Type rootType, Type ty
// can be treated correctly. Otherwise, they will resolve to a particular
// platform size, based on whatever parameters were passed into clang.

var underlyingType = typedefType.Decl.UnderlyingType;

if (underlyingType.AsTagDecl is TagDecl underlyingTagDecl)
{
var underlyingName = GetCursorName(underlyingTagDecl);

if (underlyingName != result.typeName)
{
if (!_validNameRemappings.TryGetValue(underlyingName, out var remappings))
{
remappings = new HashSet<string>();
_validNameRemappings[underlyingName] = remappings;
}

_ = remappings.Add(result.typeName);
}
}

var remappedName = GetRemappedName(result.typeName, cursor, tryRemapOperatorName: false, out var wasRemapped, skipUsing: true);
result.typeName = wasRemapped ? remappedName : GetTypeName(cursor, context, rootType, typedefType.Decl.UnderlyingType, ignoreTransparentStructsWhereRequired, out _);
}
Expand Down