Browse Source

Merge a5ac3ee003 into e093f713b9

pull/1700/merge
Fabio Anderegg 2 months ago committed by GitHub
parent
commit
0d6834417a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      src/Generator/AST/Utils.cs
  2. 30
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 37
      src/Generator/Passes/CheckAmbiguousFunctions.cs
  4. 4
      src/Generator/Passes/ValidateOperatorsPass.cs

5
src/Generator/AST/Utils.cs

@ -36,10 +36,11 @@ namespace CppSharp.AST @@ -36,10 +36,11 @@ namespace CppSharp.AST
if (method.IsDestructor)
return true;
if (method.OperatorKind == CXXOperatorKind.Equal)
if (method.Access == AccessSpecifier.Private && !method.IsOverride && !method.IsExplicitlyGenerated)
return true;
if (method.Access == AccessSpecifier.Private && !method.IsOverride && !method.IsExplicitlyGenerated)
// operator= does not make sense on static classes, but might be generated anyway, so ignore here
if (method.OperatorKind == CXXOperatorKind.Equal && @class != null && @class.IsStatic)
return true;
// Ignore copy constructor if a base class don't have a copy constructor (or it's private)

30
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1016,8 +1016,33 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat @@ -1016,8 +1016,33 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType)
{
if (field.Type.IsClass() && !field.Type.IsPointer())
{
if (field.Type.TryGetClass(out Class fieldClass) && !(fieldClass is ClassTemplateSpecialization))
{
var caop = fieldClass.Methods.FirstOrDefault(m => m.OperatorKind == CXXOperatorKind.Equal);
if (caop != null && caop.IsGenerated)
{
var fieldName = ((Class)field.Namespace).Layout.Fields.First(
f => f.FieldPtr == field.OriginalPtr).Name;
var typeName = TypePrinter.PrintNative(@class);
WriteLine($"var dest = new __IntPtr(&(({typeName}*)__Instance)->{fieldName});");
WriteLine($"var src = value.{Helpers.InstanceIdentifier};");
if (IsInternalClassNested(fieldClass))
typeName.RemoveNamespace();
WriteLine($"{fieldClass}.__Internal.OperatorEqual(dest, src);");
return;
}
}
}
string returnVar;
Type type = field.Type.Desugar();
var arrayType = type as ArrayType;
if (arrayType != null && @class.IsValueType)
{
@ -1498,6 +1523,11 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat @@ -1498,6 +1523,11 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
continue;
}
// We only use the copy assignment operator internally,
// so do not generate a public method wrapper for it
if (method.OperatorKind == CXXOperatorKind.Equal)
continue;
GenerateMethod(method, @class);
}

37
src/Generator/Passes/CheckAmbiguousFunctions.cs

@ -69,6 +69,43 @@ namespace CppSharp.Passes @@ -69,6 +69,43 @@ namespace CppSharp.Passes
private bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
{
// detect if function and overload are copy assignment or move assignment operators
// if both are either one of those types, ignore move assignment operator
if (function.OperatorKind == CXXOperatorKind.Equal && overload.OperatorKind == CXXOperatorKind.Equal &&
function.Parameters.Count == 1 && overload.Parameters.Count == 1)
{
var functionParamType = function.Parameters[0].Type;
var overloadParamType = overload.Parameters[0].Type;
if (functionParamType is PointerType && overloadParamType is PointerType)
{
var functionParamPointerType = functionParamType as PointerType;
var overloadParamPointerType = overloadParamType as PointerType;
var functionPointee = functionParamPointerType.GetPointee();
var overloadPointee = overloadParamPointerType.GetPointee();
functionPointee.TryGetClass(out Class @functionPointeeClass);
overloadPointee.TryGetClass(out Class @overloadPointeeClass);
if (functionPointeeClass == function.Namespace && @overloadPointeeClass == overload.Namespace)
{
if (functionParamPointerType.Modifier == PointerType.TypeModifier.RVReference &&
overloadParamPointerType.Modifier == PointerType.TypeModifier.LVReference)
{
function.ExplicitlyIgnore();
return true;
}
else if (functionParamPointerType.Modifier == PointerType.TypeModifier.LVReference &&
overloadParamPointerType.Modifier == PointerType.TypeModifier.RVReference)
{
overload.ExplicitlyIgnore();
return true;
}
}
}
}
List<Parameter> functionParams = RemoveOperatorParams(function);
List<Parameter> overloadParams = RemoveOperatorParams(overload);

4
src/Generator/Passes/ValidateOperatorsPass.cs

@ -55,6 +55,9 @@ namespace CppSharp.Passes @@ -55,6 +55,9 @@ namespace CppSharp.Passes
// The conversion operators can be overloaded
case CXXOperatorKind.Conversion:
case CXXOperatorKind.ExplicitConversion:
// Copy assignment operator is used internally
case CXXOperatorKind.Equal:
return true;
// The comparison operators can be overloaded if their return type is bool
@ -128,7 +131,6 @@ namespace CppSharp.Passes @@ -128,7 +131,6 @@ namespace CppSharp.Passes
case CXXOperatorKind.PipePipe:
// These operators cannot be overloaded.
case CXXOperatorKind.Equal:
case CXXOperatorKind.Comma:
case CXXOperatorKind.ArrowStar:
case CXXOperatorKind.Arrow:

Loading…
Cancel
Save