Skip to content

Commit

Permalink
Some small targeted fixes around remappings and type lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Apr 2, 2023
1 parent e0e07dc commit 617f997
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,
Debug.Assert(fieldDecl.IsBitField);

var type = fieldDecl.Type;
var typeName = GetRemappedTypeName(fieldDecl, context: null, type, out var nativeTypeName);
var typeName = GetRemappedTypeName(fieldDecl, context: null, type, out var nativeTypeName, out var wasRemapped);

if (string.IsNullOrWhiteSpace(nativeTypeName))
{
Expand Down
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
75 changes: 44 additions & 31 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 @@ -2739,11 +2767,16 @@ string AddUsingDirectiveIfNeeded(IOutputBuilder? outputBuilder, string remappedN
}

private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, out string nativeTypeName, bool skipUsing = false, bool ignoreTransparentStructsWhereRequired = false)
{
return GetRemappedTypeName(cursor, context, type, out nativeTypeName, out _, skipUsing, ignoreTransparentStructsWhereRequired);
}

private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, out string nativeTypeName, out bool wasRemapped, bool skipUsing = false, bool ignoreTransparentStructsWhereRequired = false)
{
var name = GetTypeName(cursor, context, type, ignoreTransparentStructsWhereRequired, out nativeTypeName);

var nameToCheck = nativeTypeName;
var remappedName = GetRemappedName(nameToCheck, cursor, tryRemapOperatorName: false, out var wasRemapped, skipUsing, skipUsingIfNotRemapped: true);
var remappedName = GetRemappedName(nameToCheck, cursor, tryRemapOperatorName: false, out wasRemapped, skipUsing, skipUsingIfNotRemapped: true);

if (wasRemapped)
{
Expand Down Expand Up @@ -3102,7 +3135,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 +3895,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 +4379,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.Desugar 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 +4619,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 +5360,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 +5436,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 Expand Up @@ -5973,7 +5986,7 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =

if (namedDecl is FieldDecl fieldDecl)
{
if (fieldDecl.Type is TypedefType defType && defType.Decl.HasAttrs)
if (fieldDecl.Type.Desugar is TypedefType defType && defType.Decl.HasAttrs)
{
declAttrs = declAttrs.Concat(defType.Decl.Attrs);
}
Expand Down

0 comments on commit 617f997

Please sign in to comment.