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
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)

_outputBuilder.BeginFunctionInnerPrototype(in desc);

bool needsThis = isVirtual || (isCxxMethodDecl && !hasBody && cxxMethodDecl.IsInstance);
var needsThis = isVirtual || (isCxxMethodDecl && !hasBody && cxxMethodDecl.IsInstance);

if (needsThis)
{
Expand Down Expand Up @@ -669,7 +669,12 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
outputBuilder.Write("return ");
}

outputBuilder.Write("Base.");
var cxxBaseSpecifier = _cxxRecordDeclContext.Bases.Where((baseSpecifier) => baseSpecifier.Referenced == cxxMethodDecl.Parent).Single();
var baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base");
baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true);

outputBuilder.Write(baseFieldName);
outputBuilder.Write('.');
outputBuilder.Write(name);
outputBuilder.Write('(');

Expand Down Expand Up @@ -1564,8 +1569,9 @@ private void VisitRecordDecl(RecordDecl recordDecl)

if (cxxRecordDecl != null)
{
foreach (var cxxBaseSpecifier in cxxRecordDecl.Bases)
for (var index = 0; index < cxxRecordDecl.Bases.Count; index++)
{
var cxxBaseSpecifier = cxxRecordDecl.Bases[index];
var baseCxxRecordDecl = GetRecordDecl(cxxBaseSpecifier);

if (HasField(baseCxxRecordDecl))
Expand All @@ -1574,11 +1580,6 @@ private void VisitRecordDecl(RecordDecl recordDecl)
var baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base");
baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true);

if (baseFieldName.StartsWith("__AnonymousBase_"))
{
baseFieldName = "Base";
}

var fieldDesc = new FieldDesc {
AccessSpecifier = GetAccessSpecifier(baseCxxRecordDecl, matchStar: true),
NativeTypeName = null,
Expand Down
22 changes: 16 additions & 6 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,27 +1597,37 @@ private void VisitLabelStmt(LabelStmt labelStmt)
private void VisitMemberExpr(MemberExpr memberExpr)
{
var outputBuilder = StartCSharpCode();
var isForDerivedType = false;
var baseFieldName = "";

if ((memberExpr.Base is ImplicitCastExpr implicitCastExpr) && (implicitCastExpr.CastKind is CX_CastKind.CX_CK_DerivedToBase or CX_CastKind.CX_CK_DerivedToBaseMemberPointer or CX_CastKind.CX_CK_UncheckedDerivedToBase))
{
if (memberExpr.MemberDecl is CXXMethodDecl cxxMethodDecl)
{
isForDerivedType = (_cxxRecordDeclContext is not null) && (_cxxRecordDeclContext != cxxMethodDecl.Parent) && HasField(cxxMethodDecl.Parent);
if ((_cxxRecordDeclContext is not null) && (_cxxRecordDeclContext != cxxMethodDecl.Parent) && HasField(cxxMethodDecl.Parent))
{
var cxxBaseSpecifier = _cxxRecordDeclContext.Bases.Where((baseSpecifier) => baseSpecifier.Referenced == cxxMethodDecl.Parent).Single();
baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base");
baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true);
}
}
else if (memberExpr.MemberDecl is FieldDecl fieldDecl)
{
isForDerivedType = (_cxxRecordDeclContext is not null) && (_cxxRecordDeclContext != fieldDecl.Parent);
if ((_cxxRecordDeclContext is not null) && (_cxxRecordDeclContext != fieldDecl.Parent))
{
var cxxBaseSpecifier = _cxxRecordDeclContext.Bases.Where((baseSpecifier) => baseSpecifier.Referenced == fieldDecl.Parent).Single();
baseFieldName = GetAnonymousName(cxxBaseSpecifier, "Base");
baseFieldName = GetRemappedName(baseFieldName, cxxBaseSpecifier, tryRemapOperatorName: true, out var wasRemapped, skipUsing: true);
}
}
}

if (!memberExpr.IsImplicitAccess || isForDerivedType)
if (!memberExpr.IsImplicitAccess || !string.IsNullOrWhiteSpace(baseFieldName))
{
var memberExprBase = memberExpr.Base.IgnoreImplicit;

if (isForDerivedType)
if (!string.IsNullOrWhiteSpace(baseFieldName))
{
outputBuilder.Write("Base");
outputBuilder.Write(baseFieldName);
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -2503,6 +2504,20 @@ private string GetRemappedName(string name, Cursor cursor, bool tryRemapOperator
return AddUsingDirectiveIfNeeded(_outputBuilder, remappedName, skipUsing);
}

if ((cursor is CXXBaseSpecifier cxxBaseSpecifier) && remappedName.StartsWith("__AnonymousBase_"))
{
remappedName = "Base";

if (_cxxRecordDeclContext.Bases.Count > 1)
{
var index = _cxxRecordDeclContext.Bases.IndexOf(cxxBaseSpecifier) + 1;
remappedName += index.ToString();
}

wasRemapped = true;
return AddUsingDirectiveIfNeeded(_outputBuilder, remappedName, skipUsing);
}

wasRemapped = false;
return AddUsingDirectiveIfNeeded(_outputBuilder, remappedName, skipUsingIfNotRemapped);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,9 @@ public partial struct MyStruct1B
[NativeTypeName(""struct MyStruct2 : MyStruct1A, MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down Expand Up @@ -810,9 +810,9 @@ public partial struct MyStruct1B
[NativeInheritance(""MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,9 @@ public partial struct MyStruct1B
[NativeTypeName(""struct MyStruct2 : MyStruct1A, MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down Expand Up @@ -814,9 +814,9 @@ public partial struct MyStruct1B
[NativeInheritance(""MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,9 @@ public partial struct MyStruct1B
[NativeTypeName(""struct MyStruct2 : MyStruct1A, MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down Expand Up @@ -818,9 +818,9 @@ public partial struct MyStruct1B
[NativeInheritance(""MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,9 @@ public partial struct MyStruct1B
[NativeTypeName(""struct MyStruct2 : MyStruct1A, MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down Expand Up @@ -822,9 +822,9 @@ public partial struct MyStruct1B
[NativeInheritance(""MyStruct1B"")]
public partial struct MyStruct2
{
public MyStruct1A Base;
public MyStruct1A Base1;

public MyStruct1B Base;
public MyStruct1B Base2;

public int z;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down Expand Up @@ -859,10 +859,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"" parent=""MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down Expand Up @@ -865,10 +865,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"" parent=""MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down Expand Up @@ -863,10 +863,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"" parent=""MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down Expand Up @@ -869,10 +869,10 @@ struct MyStruct2 : MyStruct1A, MyStruct1B
</field>
</struct>
<struct name=""MyStruct2"" access=""public"" native=""struct MyStruct2 : MyStruct1A, MyStruct1B"" parent=""MyStruct1B"">
<field name=""Base"" access=""public"" inherited=""MyStruct1A"">
<field name=""Base1"" access=""public"" inherited=""MyStruct1A"">
<type>MyStruct1A</type>
</field>
<field name=""Base"" access=""public"" inherited=""MyStruct1B"">
<field name=""Base2"" access=""public"" inherited=""MyStruct1B"">
<type>MyStruct1B</type>
</field>
<field name=""z"" access=""public"">
Expand Down