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

Some small targeted fixes around remappings and type lookup #437

Merged
merged 1 commit into from
Apr 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
{
var outputBuilder = StartCSharpCode();

if (IsPrevContextDecl<EnumConstantDecl>(out _, out _) && explicitCastExpr.Type is EnumType enumType)
if (IsPrevContextDecl<EnumConstantDecl>(out _, out _) && explicitCastExpr.Type.CanonicalType is EnumType enumType)
{
outputBuilder.Write('(');
var enumUnderlyingTypeName = GetRemappedTypeName(explicitCastExpr, context: null, enumType.Decl.IntegerType, out _);
Expand Down Expand Up @@ -2811,7 +2811,7 @@ private void VisitUnaryOperator(UnaryOperator unaryOperator)

case CX_UO_AddrOf:
{
if ((unaryOperator.SubExpr is DeclRefExpr declRefExpr) && (declRefExpr.Decl.Type is LValueReferenceType))
if ((unaryOperator.SubExpr is DeclRefExpr declRefExpr) && (declRefExpr.Decl.Type.CanonicalType is LValueReferenceType))
{
Visit(unaryOperator.SubExpr);
}
Expand Down
66 changes: 37 additions & 29 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,10 @@ private CallingConvention GetCallingConvention(Cursor? cursor, Cursor? context,
{
return GetCallingConvention(cursor, context, decltypeType.UnderlyingType, ref wasRemapped);
}
else if (type is ElaboratedType elaboratedType)
{
return GetCallingConvention(cursor, context, elaboratedType.NamedType, ref wasRemapped);
}
else if (type is FunctionType functionType)
{
var callingConv = functionType.CallConv;
Expand Down Expand Up @@ -2520,7 +2524,7 @@ uint GetOverloadIndex(CXXMethodDecl cxxMethodDeclToMatch, CXXRecordDecl cxxRecor
}
}

private static CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
private CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
{
var baseType = cxxBaseSpecifier.Type;

Expand All @@ -2532,11 +2536,35 @@ private static CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
}
else if (baseType is ElaboratedType elaboratedType)
{
baseType = elaboratedType.CanonicalType;
baseType = elaboratedType.NamedType;
}
else if (baseType is TemplateSpecializationType templateSpecializationType)
{
baseType = templateSpecializationType.CanonicalType;
if (templateSpecializationType.IsTypeAlias)
{
baseType = templateSpecializationType.AliasedType;
}
else if (templateSpecializationType.IsSugared)
{
baseType = templateSpecializationType.Desugar;
}
else if (templateSpecializationType.TemplateName.AsTemplateDecl is TemplateDecl templateDecl)
{
if (templateDecl.TemplatedDecl is TypeDecl typeDecl)
{
baseType = typeDecl.TypeForDecl;
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported template specialization declaration kind: '{templateDecl.TemplatedDecl.DeclKindName}'.", cxxBaseSpecifier);
baseType = templateSpecializationType.CanonicalType;
}
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported template specialization type: '{templateSpecializationType}'.", cxxBaseSpecifier);
baseType = templateSpecializationType.CanonicalType;
}
}
else if (baseType is TypedefType typedefType)
{
Expand Down Expand Up @@ -3102,7 +3130,7 @@ private string GetTypeName(Cursor? cursor, Cursor? context, Type rootType, Type
}
else if (type is DeducedType deducedType)
{
result.typeName = GetTypeName(cursor, context, rootType, deducedType.CanonicalType, ignoreTransparentStructsWhereRequired, out _);
result.typeName = GetTypeName(cursor, context, rootType, deducedType.GetDeducedType, ignoreTransparentStructsWhereRequired, out _);
}
else if (type is DependentNameType dependentNameType)
{
Expand Down Expand Up @@ -3862,11 +3890,7 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon
// platform size, based on whatever parameters were passed into clang.

var name = GetTypeName(cursor, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);

if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
{
remappedName = name;
}
var remappedName = GetRemappedTypeName(cursor, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);

if ((remappedName == name) && _config.WithTransparentStructs.TryGetValue(remappedName, out var transparentStruct) && ((transparentStruct.Name == "long") || (transparentStruct.Name == "ulong")))
{
Expand Down Expand Up @@ -4350,7 +4374,7 @@ bool IsComProxy(FunctionDecl functionDecl, string name)
parmVarDecl = functionDecl.Parameters.FirstOrDefault();
}

if ((parmVarDecl is not null) && (parmVarDecl.Type is PointerType pointerType))
if ((parmVarDecl is not null) && (parmVarDecl.Type.CanonicalType is PointerType pointerType))
{
var typeName = GetTypeName(parmVarDecl, context: null, pointerType.PointeeType, ignoreTransparentStructsWhereRequired: false, out var nativeTypeName);
return name.StartsWith($"{nativeTypeName}_") || name.StartsWith($"{typeName}_") || (typeName == "IRpcStubBuffer");
Expand Down Expand Up @@ -4590,11 +4614,7 @@ private bool IsFixedSize(Cursor cursor, Type type)
else if (type is TypedefType typedefType)
{
var name = GetTypeName(cursor, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);

if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
{
remappedName = name;
}
var remappedName = GetRemappedTypeName(cursor, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);

return (remappedName != "IntPtr")
&& (remappedName != "nint")
Expand Down Expand Up @@ -5335,13 +5355,7 @@ private bool IsUnsafe(FieldDecl fieldDecl)

if (type.CanonicalType is ConstantArrayType or IncompleteArrayType)
{
var name = GetTypeName(fieldDecl, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);

if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
{
remappedName = name;
}

var remappedName = GetRemappedTypeName(fieldDecl, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
return IsSupportedFixedSizedBufferType(remappedName);
}

Expand Down Expand Up @@ -5417,13 +5431,7 @@ private bool IsUnsafe(TypedefDecl typedefDecl, FunctionProtoType functionProtoTy

private bool IsUnsafe(NamedDecl namedDecl, Type type)
{
var name = GetTypeName(namedDecl, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);

if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
{
remappedName = name;
}

var remappedName = GetRemappedTypeName(namedDecl, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
return remappedName.Contains('*');
}

Expand Down