Skip to content

Commit 0ac3bfd

Browse files
committed
Made copy assignment operator code path only run for Kyhera-specific code
1 parent 48b74a4 commit 0ac3bfd

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

src/Generator/AST/Utils.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,17 @@ public static string GetOperatorIdentifier(CXXOperatorKind kind,
340340

341341
throw new NotSupportedException();
342342
}
343+
344+
public static bool IsInNamespace(this Declaration decl, string name)
345+
{
346+
DeclarationContext @namespace = decl.Namespace;
347+
while (@namespace != null)
348+
{
349+
if (@namespace.Name == name)
350+
return true;
351+
@namespace = @namespace.Namespace;
352+
}
353+
return false;
354+
}
343355
}
344356
}

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,30 +1009,29 @@ private bool GenerateFunctionSetter(Class @class, Property property)
10091009

10101010
private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType)
10111011
{
1012-
if (field.Type.IsClass() && !field.Type.IsPointer())
1012+
if (@class.IsInNamespace("Kyt"))
10131013
{
1014-
if (field.Type.TryGetClass(out Class fieldClass) && !(fieldClass is ClassTemplateSpecialization))
1014+
if (field.Type.IsClass() && !field.Type.IsPointer())
10151015
{
1016-
var caop = fieldClass.Methods.FirstOrDefault(m => m.OperatorKind == CXXOperatorKind.Equal);
1017-
if (caop != null && caop.IsGenerated)
1016+
if (field.Type.TryGetClass(out Class fieldClass) && !(fieldClass is ClassTemplateSpecialization) && !fieldClass.IsValueType)
10181017
{
1019-
var fieldName = ((Class)field.Namespace).Layout.Fields.First(
1020-
f => f.FieldPtr == field.OriginalPtr).Name;
1021-
var typeName = TypePrinter.PrintNative(@class);
1022-
WriteLine($"var dest = new __IntPtr(&(({typeName}*)__Instance)->{fieldName});");
1023-
WriteLine($"var src = value.{Helpers.InstanceIdentifier};");
1024-
1025-
if (IsInternalClassNested(fieldClass))
1026-
typeName.RemoveNamespace();
1027-
1028-
WriteLine($"{fieldClass}.__Internal.OperatorEqual(dest, src);");
1029-
1030-
return;
1018+
var caop = fieldClass.Methods.FirstOrDefault(m => m.OperatorKind == CXXOperatorKind.Equal);
1019+
if (caop != null && caop.IsGenerated)
1020+
{
1021+
var fieldName = ((Class)field.Namespace).Layout.Fields.First(
1022+
f => f.FieldPtr == field.OriginalPtr).Name;
1023+
var typeName = TypePrinter.PrintNative(@class);
1024+
WriteLine($"var dest = new __IntPtr(&(({typeName}*)__Instance)->{fieldName});");
1025+
WriteLine($"var src = value.{Helpers.InstanceIdentifier};");
1026+
if (IsInternalClassNested(fieldClass))
1027+
typeName.RemoveNamespace();
1028+
WriteLine($"{fieldClass}.__Internal.OperatorEqual(dest, src);");
1029+
return;
1030+
}
10311031
}
10321032
}
10331033
}
10341034

1035-
10361035
string returnVar;
10371036
Type type = field.Type.Desugar();
10381037

@@ -1519,8 +1518,11 @@ public void GenerateClassMethods(IList<Method> methods)
15191518

15201519
// We only use the copy assignment operator internally,
15211520
// so do not generate a public method wrapper for it
1522-
if (method.OperatorKind == CXXOperatorKind.Equal)
1523-
continue;
1521+
if (@class.IsInNamespace("Kyt")
1522+
&& method.OperatorKind == CXXOperatorKind.Equal)
1523+
{
1524+
continue;
1525+
}
15241526

15251527
GenerateMethod(method, @class);
15261528
}

src/Generator/Passes/CheckAmbiguousFunctions.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using CppSharp.AST;
56
using CppSharp.AST.Extensions;
@@ -69,38 +70,40 @@ public override bool VisitFunctionDecl(AST.Function function)
6970

7071
private bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
7172
{
72-
// detect if function and overload are copy assignment or move assignment operators
73-
// if both are either one of those types, ignore move assignment operator
74-
if (function.OperatorKind == CXXOperatorKind.Equal && overload.OperatorKind == CXXOperatorKind.Equal &&
75-
function.Parameters.Count == 1 && overload.Parameters.Count == 1)
73+
if (function.IsInNamespace("Kyt"))
7674
{
77-
var functionParamType = function.Parameters[0].Type;
78-
var overloadParamType = overload.Parameters[0].Type;
79-
80-
if (functionParamType is PointerType && overloadParamType is PointerType)
75+
// detect if function and overload are copy assignment or move assignment operators
76+
// if both are either one of those types, ignore move assignment operator
77+
if (function.OperatorKind == CXXOperatorKind.Equal && overload.OperatorKind == CXXOperatorKind.Equal &&
78+
function.Parameters.Count == 1 && overload.Parameters.Count == 1)
8179
{
82-
var functionParamPointerType = functionParamType as PointerType;
83-
var overloadParamPointerType = overloadParamType as PointerType;
80+
var functionParamType = function.Parameters[0].Type;
81+
var overloadParamType = overload.Parameters[0].Type;
8482

85-
var functionPointee = functionParamPointerType.GetPointee();
86-
var overloadPointee = overloadParamPointerType.GetPointee();
83+
if (functionParamType is PointerType && overloadParamType is PointerType)
84+
{
85+
var functionParamPointerType = functionParamType as PointerType;
86+
var overloadParamPointerType = overloadParamType as PointerType;
8787

88-
functionPointee.TryGetClass(out Class @functionPointeeClass);
89-
overloadPointee.TryGetClass(out Class @overloadPointeeClass);
88+
var functionPointee = functionParamPointerType.GetPointee();
89+
var overloadPointee = overloadParamPointerType.GetPointee();
9090

91-
if (functionPointeeClass == function.Namespace && @overloadPointeeClass == overload.Namespace)
92-
{
93-
if (functionParamPointerType.Modifier == PointerType.TypeModifier.RVReference &&
94-
overloadParamPointerType.Modifier == PointerType.TypeModifier.LVReference)
95-
{
96-
function.ExplicitlyIgnore();
97-
return true;
98-
}
99-
else if (functionParamPointerType.Modifier == PointerType.TypeModifier.LVReference &&
100-
overloadParamPointerType.Modifier == PointerType.TypeModifier.RVReference)
91+
functionPointee.TryGetClass(out Class @functionPointeeClass);
92+
overloadPointee.TryGetClass(out Class @overloadPointeeClass);
93+
if (functionPointeeClass == function.Namespace && @overloadPointeeClass == overload.Namespace)
10194
{
102-
overload.ExplicitlyIgnore();
103-
return true;
95+
if (functionParamPointerType.Modifier == PointerType.TypeModifier.RVReference &&
96+
overloadParamPointerType.Modifier == PointerType.TypeModifier.LVReference)
97+
{
98+
function.ExplicitlyIgnore();
99+
return true;
100+
}
101+
else if (functionParamPointerType.Modifier == PointerType.TypeModifier.LVReference &&
102+
overloadParamPointerType.Modifier == PointerType.TypeModifier.RVReference)
103+
{
104+
overload.ExplicitlyIgnore();
105+
return true;
106+
}
104107
}
105108
}
106109
}

src/Generator/Passes/ValidateOperatorsPass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ private bool IsValidOperatorOverload(Method @operator)
5555
// The conversion operators can be overloaded
5656
case CXXOperatorKind.Conversion:
5757
case CXXOperatorKind.ExplicitConversion:
58+
return true;
5859

5960
// Copy assignment operator is used internally
6061
case CXXOperatorKind.Equal:
61-
return true;
62+
return @operator.IsInNamespace("Kyt") ? true : false;
6263

6364
// The comparison operators can be overloaded if their return type is bool
6465
case CXXOperatorKind.EqualEqual:

0 commit comments

Comments
 (0)