diff --git a/src/AST/Class.cs b/src/AST/Class.cs index b075ef58..592397c1 100644 --- a/src/AST/Class.cs +++ b/src/AST/Class.cs @@ -199,6 +199,15 @@ namespace CppSharp.AST } } + public override IEnumerable GetOverloads(Function function) + { + var methods = Methods.Where(m => m.Name == function.Name); + if (methods.ToList().Count != 0) + return methods; + + return base.GetOverloads(function); + } + public override T Visit(IDeclVisitor visitor) { return visitor.VisitClassDecl(this); diff --git a/src/AST/Function.cs b/src/AST/Function.cs index 5951bee4..a3c795f8 100644 --- a/src/AST/Function.cs +++ b/src/AST/Function.cs @@ -88,6 +88,7 @@ namespace CppSharp.AST IsVariadic = false; IsInline = false; Signature = string.Empty; + Index = null; } public Function(Function function) @@ -105,6 +106,7 @@ namespace CppSharp.AST SynthKind = function.SynthKind; OriginalFunction = function.OriginalFunction; Mangled = function.Mangled; + Index = function.Index; } public QualifiedType ReturnType { get; set; } @@ -173,6 +175,11 @@ namespace CppSharp.AST public string Signature { get; set; } + /// + /// Keeps an index that de-duplicates native names in the C# backend. + /// + public uint? Index { get; set; } + public override T Visit(IDeclVisitor visitor) { return visitor.VisitFunctionDecl(this); diff --git a/src/AST/Namespace.cs b/src/AST/Namespace.cs index 9ed1f7e2..3c0605bc 100644 --- a/src/AST/Namespace.cs +++ b/src/AST/Namespace.cs @@ -278,8 +278,8 @@ namespace CppSharp.AST return Functions.Where(fn => fn.OperatorKind == kind); } - public virtual IEnumerable GetFunctionOverloads(Function function) - { + public virtual IEnumerable GetOverloads(Function function) + { if (function.IsOperator) return FindOperator(function.OperatorKind); return Functions.Where(fn => fn.Name == function.Name); diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index c6564434..7d4f5ad8 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -2608,10 +2608,10 @@ namespace CppSharp.Generators.CSharp var method = function as Method; if (method != null) { - if (method.IsConstructor) + if (method.IsConstructor && !method.IsCopyConstructor) functionName = "ctor"; else if (method.IsCopyConstructor) - functionName = "cctor"; + functionName = "cctor"; else if (method.IsDestructor) functionName = "dtor"; else @@ -2623,12 +2623,14 @@ namespace CppSharp.Generators.CSharp if (function.IsOperator) identifier = "Operator" + function.OperatorKind; - var overloads = function.Namespace.GetFunctionOverloads(function) + var overloads = function.Namespace.GetOverloads(function) .ToList(); var index = overloads.IndexOf(function); if (index >= 0) identifier += "_" + index.ToString(CultureInfo.InvariantCulture); + else if (function.Index.HasValue) + identifier += "_" + function.Index.Value; return identifier; } diff --git a/src/Generator/Passes/CheckAmbiguousFunctions.cs b/src/Generator/Passes/CheckAmbiguousFunctions.cs index 0c44baf5..e3726767 100644 --- a/src/Generator/Passes/CheckAmbiguousFunctions.cs +++ b/src/Generator/Passes/CheckAmbiguousFunctions.cs @@ -31,7 +31,7 @@ namespace CppSharp.Passes if (function.IsAmbiguous) return false; - var overloads = function.Namespace.GetFunctionOverloads(function); + var overloads = function.Namespace.GetOverloads(function); foreach (var overload in overloads) { diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index aa756fbd..c8ee5c3c 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -25,10 +25,10 @@ namespace CppSharp.Passes if (decl.Name != Name) throw new Exception("Invalid name"); - var method = decl as Method; - if (method != null) + var function = decl as Function; + if (function != null) { - return UpdateName(method); + return UpdateName(function); } var property = decl as Property; @@ -46,9 +46,9 @@ namespace CppSharp.Passes return true; } - private bool UpdateName(Method method) + private bool UpdateName(Function function) { - var @params = method.Parameters.Where(p => p.Kind != ParameterKind.IndirectReturnType) + var @params = function.Parameters.Where(p => p.Kind != ParameterKind.IndirectReturnType) .Select(p => p.QualifiedType.ToString()); var signature = string.Format("{0}({1})", Name,string.Join( ", ", @params)); @@ -66,19 +66,21 @@ namespace CppSharp.Passes if (Count < methodCount+1) Count = methodCount+1; - if (method.IsOperator) + var method = function as Method; + + if (function.IsOperator) { // TODO: turn into a method; append the original type (say, "signed long") of the last parameter to the type so that the user knows which overload is called - Driver.Diagnostics.EmitWarning("Duplicate operator {0} ignored", method.Name); - method.ExplicityIgnored = true; + Driver.Diagnostics.EmitWarning("Duplicate operator {0} ignored", function.Name); + function.ExplicityIgnored = true; } - else if (method.IsConstructor) + else if (method != null && method.IsConstructor) { - Driver.Diagnostics.EmitWarning("Duplicate constructor {0} ignored", method.Name); - method.ExplicityIgnored = true; + Driver.Diagnostics.EmitWarning("Duplicate constructor {0} ignored", function.Name); + function.ExplicityIgnored = true; } else - method.Name += methodCount.ToString(CultureInfo.InvariantCulture); + function.Name += methodCount.ToString(CultureInfo.InvariantCulture); return true; } } @@ -149,6 +151,15 @@ namespace CppSharp.Passes foreach (var property in @class.Properties) VisitProperty(property); + var total = (uint)0; + foreach (var method in @class.Methods.Where(m => m.IsConstructor && + !m.IsCopyConstructor && !m.IsMoveConstructor)) + method.Index = total++; + + total = 0; + foreach (var method in @class.Methods.Where(m => m.IsCopyConstructor)) + method.Index = total++; + return false; }