Skip to content

Commit 39d389a

Browse files
Fabio AndereggJordanL8
authored andcommitted
generate and use copy assignment operator when setting struct/class value properties
1 parent 8e22170 commit 39d389a

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/Generator/AST/Utils.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public static bool CheckIgnoreMethod(Method method)
3636
if (method.IsDestructor)
3737
return true;
3838

39-
if (method.OperatorKind == CXXOperatorKind.Equal)
40-
return true;
41-
4239
if (method.Access == AccessSpecifier.Private && !method.IsOverride && !method.IsExplicitlyGenerated)
4340
return true;
4441

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,34 @@ 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())
1013+
{
1014+
if (field.Type.TryGetClass(out Class fieldClass))
1015+
{
1016+
var caop = fieldClass.Methods.FirstOrDefault(m => m.OperatorKind == CXXOperatorKind.Equal);
1017+
if (caop != null)
1018+
{
1019+
var fieldName = ((Class)field.Namespace).Layout.Fields.First(
1020+
f => f.FieldPtr == field.OriginalPtr).Name;
1021+
WriteLine($"var dest = new __IntPtr(&((__Internal*)__Instance)->{fieldName});");
1022+
WriteLine($"var src = value.{Helpers.InstanceIdentifier};");
1023+
1024+
var typeName = TypePrinter.PrintNative(fieldClass);
1025+
if (IsInternalClassNested(fieldClass))
1026+
typeName.RemoveNamespace();
1027+
1028+
WriteLine($"{fieldClass}.__Internal.OperatorEqual(dest, src);");
1029+
//UnindentAndWriteCloseBrace();
1030+
1031+
return;
1032+
}
1033+
}
1034+
}
1035+
1036+
10121037
string returnVar;
10131038
Type type = field.Type.Desugar();
1039+
10141040
var arrayType = type as ArrayType;
10151041
if (arrayType != null && @class.IsValueType)
10161042
{
@@ -1492,6 +1518,11 @@ public void GenerateClassMethods(IList<Method> methods)
14921518
continue;
14931519
}
14941520

1521+
// We only use the copy assignment operator internally,
1522+
// so do not generate a public method wrapper for it
1523+
if (method.OperatorKind == CXXOperatorKind.Equal)
1524+
continue;
1525+
14951526
GenerateMethod(method, @class);
14961527
}
14971528

src/Generator/Passes/ValidateOperatorsPass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ private bool IsValidOperatorOverload(Method @operator)
5555
// The conversion operators can be overloaded
5656
case CXXOperatorKind.Conversion:
5757
case CXXOperatorKind.ExplicitConversion:
58+
59+
// Copy assignment operator is used internally
60+
case CXXOperatorKind.Equal:
5861
return true;
5962

6063
// The comparison operators can be overloaded if their return type is bool
@@ -127,7 +130,6 @@ private bool IsValidOperatorOverload(Method @operator)
127130
case CXXOperatorKind.PipePipe:
128131

129132
// These operators cannot be overloaded.
130-
case CXXOperatorKind.Equal:
131133
case CXXOperatorKind.Comma:
132134
case CXXOperatorKind.ArrowStar:
133135
case CXXOperatorKind.Arrow:

0 commit comments

Comments
 (0)