diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index b1472b70..93933007 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -250,7 +250,7 @@ namespace CppSharp.Generators.CSharp // Generate all the internal function declarations. foreach (var function in context.Functions) { - if (!function.IsGenerated && !function.IsInternal ) continue; + if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized) continue; GenerateInternalFunction(function); } @@ -1991,7 +1991,10 @@ namespace CppSharp.Generators.CSharp WriteLine(")"); WriteStartBraceIndent(); - GenerateInternalFunctionCall(function); + if (function.SynthKind == FunctionSynthKind.DefaultValueOverload) + GenerateOverloadCall(function); + else + GenerateInternalFunctionCall(function); WriteCloseBraceIndent(); PopBlock(NewLineKind.BeforeNextBlock); @@ -2079,14 +2082,7 @@ namespace CppSharp.Generators.CSharp { if (!method.IsConstructor) { - Type type = method.OriginalReturnType.Type; - WriteLine("{0}{1}({2});", - type.IsPrimitiveType(PrimitiveType.Void) ? string.Empty : "return ", - method.Name, - string.Join(", ", - method.Parameters.Where( - p => p.Kind == ParameterKind.Regular).Select( - p => p.Ignore ? p.DefaultArgument.String : p.Name))); + GenerateOverloadCall(method); } goto SkipImpl; } @@ -2138,6 +2134,18 @@ namespace CppSharp.Generators.CSharp PopBlock(NewLineKind.BeforeNextBlock); } + private void GenerateOverloadCall(Function function) + { + Type type = function.OriginalReturnType.Type; + WriteLine("{0}{1}({2});", + type.IsPrimitiveType(PrimitiveType.Void) ? string.Empty : "return ", + function.Name, + string.Join(", ", + function.Parameters.Where( + p => p.Kind == ParameterKind.Regular).Select( + p => p.Ignore ? p.DefaultArgument.String : p.Name))); + } + private void GenerateEquals(Function method, Class @class) { Class leftHandSide; diff --git a/src/Generator/Passes/HandleDefaultParamValuesPass.cs b/src/Generator/Passes/HandleDefaultParamValuesPass.cs index 5e42aa6e..83eb02b6 100644 --- a/src/Generator/Passes/HandleDefaultParamValuesPass.cs +++ b/src/Generator/Passes/HandleDefaultParamValuesPass.cs @@ -15,6 +15,8 @@ namespace CppSharp.Passes private static readonly Regex regexName = new Regex(@"(\w+)", RegexOptions.Compiled); private static readonly Regex regexCtor = new Regex(@"^([\w<,>:]+)\s*(\([\w, ]*\))$", RegexOptions.Compiled); + private readonly Dictionary> overloads = new Dictionary>(); + public HandleDefaultParamValuesPass() { Options.VisitFunctionParameters = false; @@ -24,12 +26,17 @@ namespace CppSharp.Passes { if (!unit.IsGenerated) return false; - return base.VisitTranslationUnit(unit); + var result = base.VisitTranslationUnit(unit); + foreach (var overload in overloads) + overload.Key.Functions.AddRange(overload.Value); + overloads.Clear(); + return result; } public override bool VisitFunctionDecl(Function function) { - bool result = base.VisitFunctionDecl(function); + if (!base.VisitFunctionDecl(function)) + return false; var overloadIndices = new List(function.Parameters.Count); foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null)) @@ -64,7 +71,7 @@ namespace CppSharp.Passes GenerateOverloads(function, overloadIndices); - return result; + return true; } private void CheckFloatSyntax(Type desugared, Parameter parameter) @@ -228,7 +235,7 @@ namespace CppSharp.Passes } } - private static void GenerateOverloads(Function function, List overloadIndices) + private void GenerateOverloads(Function function, List overloadIndices) { foreach (var overloadIndex in overloadIndices) { @@ -247,7 +254,14 @@ namespace CppSharp.Passes if (method != null) ((Class) function.Namespace).Methods.Add((Method) overload); else - function.Namespace.Functions.Add(overload); + { + List functions; + if (overloads.ContainsKey(function.Namespace)) + functions = overloads[function.Namespace]; + else + overloads.Add(function.Namespace, functions = new List()); + functions.Add(overload); + } for (int i = 0; i <= overloadIndex; i++) function.Parameters[i].DefaultArgument = null; diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index c40b5f2f..8eeece86 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -21,6 +21,7 @@ public class CSharpTempTests : GeneratorTestFixture new InheritsProtectedVirtualFromSecondaryBase().Dispose(); new InheritanceBuffer().Dispose(); new HasProtectedVirtual().Dispose(); + CSharpTemp.CSharpTemp.FreeFunctionWithUnsupportedDefaultArg(); } [Test] diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index 3cfa9751..876a6572 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -598,3 +598,7 @@ void HasProtectedVirtual::protectedVirtual() void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual() { } + +void freeFunctionWithUnsupportedDefaultArg(Foo foo) +{ +} diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 91ff35f4..127865e9 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -568,3 +568,5 @@ class DLL_API InheritsProtectedVirtualFromSecondaryBase : public InheritanceBuff protected: void protectedVirtual(); }; + +void DLL_API freeFunctionWithUnsupportedDefaultArg(Foo foo = Foo());