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 @@ -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,6 +1991,9 @@ namespace CppSharp.Generators.CSharp @@ -1991,6 +1991,9 @@ namespace CppSharp.Generators.CSharp
WriteLine(")");
WriteStartBraceIndent();
if (function.SynthKind == FunctionSynthKind.DefaultValueOverload)
GenerateOverloadCall(function);
else
GenerateInternalFunctionCall(function);
WriteCloseBraceIndent();
@ -2079,14 +2082,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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;

24
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -15,6 +15,8 @@ namespace CppSharp.Passes @@ -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<DeclarationContext, List<Function>> overloads = new Dictionary<DeclarationContext, List<Function>>();
public HandleDefaultParamValuesPass()
{
Options.VisitFunctionParameters = false;
@ -24,12 +26,17 @@ namespace CppSharp.Passes @@ -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<int>(function.Parameters.Count);
foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null))
@ -64,7 +71,7 @@ namespace CppSharp.Passes @@ -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 @@ -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)
{
@ -247,7 +254,14 @@ namespace CppSharp.Passes @@ -247,7 +254,14 @@ namespace CppSharp.Passes
if (method != null)
((Class) function.Namespace).Methods.Add((Method) overload);
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++)
function.Parameters[i].DefaultArgument = null;

1
tests/CSharpTemp/CSharpTemp.Tests.cs

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

4
tests/CSharpTemp/CSharpTemp.cpp

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

2
tests/CSharpTemp/CSharpTemp.h

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

Loading…
Cancel
Save