Browse Source

Fixed the generation of overloads for free functions with default args.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/516/head
Dimitar Dobrev 10 years ago
parent
commit
069b727a23
  1. 26
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 24
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  3. 1
      tests/CSharpTemp/CSharpTemp.Tests.cs
  4. 4
      tests/CSharpTemp/CSharpTemp.cpp
  5. 2
      tests/CSharpTemp/CSharpTemp.h

26
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -250,7 +250,7 @@ namespace CppSharp.Generators.CSharp
// Generate all the internal function declarations. // Generate all the internal function declarations.
foreach (var function in context.Functions) foreach (var function in context.Functions)
{ {
if (!function.IsGenerated && !function.IsInternal ) continue; if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized) continue;
GenerateInternalFunction(function); GenerateInternalFunction(function);
} }
@ -1991,6 +1991,9 @@ namespace CppSharp.Generators.CSharp
WriteLine(")"); WriteLine(")");
WriteStartBraceIndent(); WriteStartBraceIndent();
if (function.SynthKind == FunctionSynthKind.DefaultValueOverload)
GenerateOverloadCall(function);
else
GenerateInternalFunctionCall(function); GenerateInternalFunctionCall(function);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
@ -2079,14 +2082,7 @@ namespace CppSharp.Generators.CSharp
{ {
if (!method.IsConstructor) if (!method.IsConstructor)
{ {
Type type = method.OriginalReturnType.Type; GenerateOverloadCall(method);
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)));
} }
goto SkipImpl; goto SkipImpl;
} }
@ -2138,6 +2134,18 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); 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) private void GenerateEquals(Function method, Class @class)
{ {
Class leftHandSide; Class leftHandSide;

24
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 regexName = new Regex(@"(\w+)", RegexOptions.Compiled);
private static readonly Regex regexCtor = new Regex(@"^([\w<,>:]+)\s*(\([\w, ]*\))$", RegexOptions.Compiled); private static readonly Regex regexCtor = new Regex(@"^([\w<,>:]+)\s*(\([\w, ]*\))$", RegexOptions.Compiled);
private readonly Dictionary<DeclarationContext, List<Function>> overloads = new Dictionary<DeclarationContext, List<Function>>();
public HandleDefaultParamValuesPass() public HandleDefaultParamValuesPass()
{ {
Options.VisitFunctionParameters = false; Options.VisitFunctionParameters = false;
@ -24,12 +26,17 @@ namespace CppSharp.Passes
{ {
if (!unit.IsGenerated) if (!unit.IsGenerated)
return false; 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) public override bool VisitFunctionDecl(Function function)
{ {
bool result = base.VisitFunctionDecl(function); if (!base.VisitFunctionDecl(function))
return false;
var overloadIndices = new List<int>(function.Parameters.Count); var overloadIndices = new List<int>(function.Parameters.Count);
foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null)) foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null))
@ -64,7 +71,7 @@ namespace CppSharp.Passes
GenerateOverloads(function, overloadIndices); GenerateOverloads(function, overloadIndices);
return result; return true;
} }
private void CheckFloatSyntax(Type desugared, Parameter parameter) private void CheckFloatSyntax(Type desugared, Parameter parameter)
@ -228,7 +235,7 @@ namespace CppSharp.Passes
} }
} }
private static void GenerateOverloads(Function function, List<int> overloadIndices) private void GenerateOverloads(Function function, List<int> overloadIndices)
{ {
foreach (var overloadIndex in overloadIndices) foreach (var overloadIndex in overloadIndices)
{ {
@ -247,7 +254,14 @@ namespace CppSharp.Passes
if (method != null) if (method != null)
((Class) function.Namespace).Methods.Add((Method) overload); ((Class) function.Namespace).Methods.Add((Method) overload);
else else
function.Namespace.Functions.Add(overload); {
List<Function> functions;
if (overloads.ContainsKey(function.Namespace))
functions = overloads[function.Namespace];
else
overloads.Add(function.Namespace, functions = new List<Function>());
functions.Add(overload);
}
for (int i = 0; i <= overloadIndex; i++) for (int i = 0; i <= overloadIndex; i++)
function.Parameters[i].DefaultArgument = null; function.Parameters[i].DefaultArgument = null;

1
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -21,6 +21,7 @@ public class CSharpTempTests : GeneratorTestFixture
new InheritsProtectedVirtualFromSecondaryBase().Dispose(); new InheritsProtectedVirtualFromSecondaryBase().Dispose();
new InheritanceBuffer().Dispose(); new InheritanceBuffer().Dispose();
new HasProtectedVirtual().Dispose(); new HasProtectedVirtual().Dispose();
CSharpTemp.CSharpTemp.FreeFunctionWithUnsupportedDefaultArg();
} }
[Test] [Test]

4
tests/CSharpTemp/CSharpTemp.cpp

@ -598,3 +598,7 @@ void HasProtectedVirtual::protectedVirtual()
void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual() void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual()
{ {
} }
void freeFunctionWithUnsupportedDefaultArg(Foo foo)
{
}

2
tests/CSharpTemp/CSharpTemp.h

@ -568,3 +568,5 @@ class DLL_API InheritsProtectedVirtualFromSecondaryBase : public InheritanceBuff
protected: protected:
void protectedVirtual(); void protectedVirtual();
}; };
void DLL_API freeFunctionWithUnsupportedDefaultArg(Foo foo = Foo());

Loading…
Cancel
Save