Skip to content

Commit 617f997

Browse files
committed
Some small targeted fixes around remappings and type lookup
1 parent e0e07dc commit 617f997

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl,
23572357
Debug.Assert(fieldDecl.IsBitField);
23582358

23592359
var type = fieldDecl.Type;
2360-
var typeName = GetRemappedTypeName(fieldDecl, context: null, type, out var nativeTypeName);
2360+
var typeName = GetRemappedTypeName(fieldDecl, context: null, type, out var nativeTypeName, out var wasRemapped);
23612361

23622362
if (string.IsNullOrWhiteSpace(nativeTypeName))
23632363
{

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
886886
{
887887
var outputBuilder = StartCSharpCode();
888888

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

28122812
case CX_UO_AddrOf:
28132813
{
2814-
if ((unaryOperator.SubExpr is DeclRefExpr declRefExpr) && (declRefExpr.Decl.Type is LValueReferenceType))
2814+
if ((unaryOperator.SubExpr is DeclRefExpr declRefExpr) && (declRefExpr.Decl.Type.CanonicalType is LValueReferenceType))
28152815
{
28162816
Visit(unaryOperator.SubExpr);
28172817
}

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,10 @@ private CallingConvention GetCallingConvention(Cursor? cursor, Cursor? context,
21752175
{
21762176
return GetCallingConvention(cursor, context, decltypeType.UnderlyingType, ref wasRemapped);
21772177
}
2178+
else if (type is ElaboratedType elaboratedType)
2179+
{
2180+
return GetCallingConvention(cursor, context, elaboratedType.NamedType, ref wasRemapped);
2181+
}
21782182
else if (type is FunctionType functionType)
21792183
{
21802184
var callingConv = functionType.CallConv;
@@ -2520,7 +2524,7 @@ uint GetOverloadIndex(CXXMethodDecl cxxMethodDeclToMatch, CXXRecordDecl cxxRecor
25202524
}
25212525
}
25222526

2523-
private static CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
2527+
private CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
25242528
{
25252529
var baseType = cxxBaseSpecifier.Type;
25262530

@@ -2532,11 +2536,35 @@ private static CXXRecordDecl GetRecordDecl(CXXBaseSpecifier cxxBaseSpecifier)
25322536
}
25332537
else if (baseType is ElaboratedType elaboratedType)
25342538
{
2535-
baseType = elaboratedType.CanonicalType;
2539+
baseType = elaboratedType.NamedType;
25362540
}
25372541
else if (baseType is TemplateSpecializationType templateSpecializationType)
25382542
{
2539-
baseType = templateSpecializationType.CanonicalType;
2543+
if (templateSpecializationType.IsTypeAlias)
2544+
{
2545+
baseType = templateSpecializationType.AliasedType;
2546+
}
2547+
else if (templateSpecializationType.IsSugared)
2548+
{
2549+
baseType = templateSpecializationType.Desugar;
2550+
}
2551+
else if (templateSpecializationType.TemplateName.AsTemplateDecl is TemplateDecl templateDecl)
2552+
{
2553+
if (templateDecl.TemplatedDecl is TypeDecl typeDecl)
2554+
{
2555+
baseType = typeDecl.TypeForDecl;
2556+
}
2557+
else
2558+
{
2559+
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported template specialization declaration kind: '{templateDecl.TemplatedDecl.DeclKindName}'.", cxxBaseSpecifier);
2560+
baseType = templateSpecializationType.CanonicalType;
2561+
}
2562+
}
2563+
else
2564+
{
2565+
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported template specialization type: '{templateSpecializationType}'.", cxxBaseSpecifier);
2566+
baseType = templateSpecializationType.CanonicalType;
2567+
}
25402568
}
25412569
else if (baseType is TypedefType typedefType)
25422570
{
@@ -2739,11 +2767,16 @@ string AddUsingDirectiveIfNeeded(IOutputBuilder? outputBuilder, string remappedN
27392767
}
27402768

27412769
private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, out string nativeTypeName, bool skipUsing = false, bool ignoreTransparentStructsWhereRequired = false)
2770+
{
2771+
return GetRemappedTypeName(cursor, context, type, out nativeTypeName, out _, skipUsing, ignoreTransparentStructsWhereRequired);
2772+
}
2773+
2774+
private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, out string nativeTypeName, out bool wasRemapped, bool skipUsing = false, bool ignoreTransparentStructsWhereRequired = false)
27422775
{
27432776
var name = GetTypeName(cursor, context, type, ignoreTransparentStructsWhereRequired, out nativeTypeName);
27442777

27452778
var nameToCheck = nativeTypeName;
2746-
var remappedName = GetRemappedName(nameToCheck, cursor, tryRemapOperatorName: false, out var wasRemapped, skipUsing, skipUsingIfNotRemapped: true);
2779+
var remappedName = GetRemappedName(nameToCheck, cursor, tryRemapOperatorName: false, out wasRemapped, skipUsing, skipUsingIfNotRemapped: true);
27472780

27482781
if (wasRemapped)
27492782
{
@@ -3102,7 +3135,7 @@ private string GetTypeName(Cursor? cursor, Cursor? context, Type rootType, Type
31023135
}
31033136
else if (type is DeducedType deducedType)
31043137
{
3105-
result.typeName = GetTypeName(cursor, context, rootType, deducedType.CanonicalType, ignoreTransparentStructsWhereRequired, out _);
3138+
result.typeName = GetTypeName(cursor, context, rootType, deducedType.GetDeducedType, ignoreTransparentStructsWhereRequired, out _);
31063139
}
31073140
else if (type is DependentNameType dependentNameType)
31083141
{
@@ -3862,11 +3895,7 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon
38623895
// platform size, based on whatever parameters were passed into clang.
38633896

38643897
var name = GetTypeName(cursor, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);
3865-
3866-
if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
3867-
{
3868-
remappedName = name;
3869-
}
3898+
var remappedName = GetRemappedTypeName(cursor, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
38703899

38713900
if ((remappedName == name) && _config.WithTransparentStructs.TryGetValue(remappedName, out var transparentStruct) && ((transparentStruct.Name == "long") || (transparentStruct.Name == "ulong")))
38723901
{
@@ -4350,7 +4379,7 @@ bool IsComProxy(FunctionDecl functionDecl, string name)
43504379
parmVarDecl = functionDecl.Parameters.FirstOrDefault();
43514380
}
43524381

4353-
if ((parmVarDecl is not null) && (parmVarDecl.Type is PointerType pointerType))
4382+
if ((parmVarDecl is not null) && (parmVarDecl.Type.Desugar is PointerType pointerType))
43544383
{
43554384
var typeName = GetTypeName(parmVarDecl, context: null, pointerType.PointeeType, ignoreTransparentStructsWhereRequired: false, out var nativeTypeName);
43564385
return name.StartsWith($"{nativeTypeName}_") || name.StartsWith($"{typeName}_") || (typeName == "IRpcStubBuffer");
@@ -4590,11 +4619,7 @@ private bool IsFixedSize(Cursor cursor, Type type)
45904619
else if (type is TypedefType typedefType)
45914620
{
45924621
var name = GetTypeName(cursor, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);
4593-
4594-
if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
4595-
{
4596-
remappedName = name;
4597-
}
4622+
var remappedName = GetRemappedTypeName(cursor, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
45984623

45994624
return (remappedName != "IntPtr")
46004625
&& (remappedName != "nint")
@@ -5335,13 +5360,7 @@ private bool IsUnsafe(FieldDecl fieldDecl)
53355360

53365361
if (type.CanonicalType is ConstantArrayType or IncompleteArrayType)
53375362
{
5338-
var name = GetTypeName(fieldDecl, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);
5339-
5340-
if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
5341-
{
5342-
remappedName = name;
5343-
}
5344-
5363+
var remappedName = GetRemappedTypeName(fieldDecl, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
53455364
return IsSupportedFixedSizedBufferType(remappedName);
53465365
}
53475366

@@ -5417,13 +5436,7 @@ private bool IsUnsafe(TypedefDecl typedefDecl, FunctionProtoType functionProtoTy
54175436

54185437
private bool IsUnsafe(NamedDecl namedDecl, Type type)
54195438
{
5420-
var name = GetTypeName(namedDecl, context: null, type, ignoreTransparentStructsWhereRequired: false, out _);
5421-
5422-
if (!_config.RemappedNames.TryGetValue(name, out var remappedName))
5423-
{
5424-
remappedName = name;
5425-
}
5426-
5439+
var remappedName = GetRemappedTypeName(namedDecl, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
54275440
return remappedName.Contains('*');
54285441
}
54295442

@@ -5973,7 +5986,7 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =
59735986

59745987
if (namedDecl is FieldDecl fieldDecl)
59755988
{
5976-
if (fieldDecl.Type is TypedefType defType && defType.Decl.HasAttrs)
5989+
if (fieldDecl.Type.Desugar is TypedefType defType && defType.Decl.HasAttrs)
59775990
{
59785991
declAttrs = declAttrs.Concat(defType.Decl.Attrs);
59795992
}

0 commit comments

Comments
 (0)