Browse Source

Fixed a bug when having a default arg value for a pointer to a C# struct.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/472/head
Dimitar Dobrev 10 years ago
parent
commit
53e75ab19f
  1. 58
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  2. 4
      tests/CSharpTemp/CSharpTemp.cpp
  3. 1
      tests/CSharpTemp/CSharpTemp.h

58
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -3,7 +3,6 @@ using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp; using CppSharp.Generators.CSharp;
using CppSharp.Types; using CppSharp.Types;
using Type = CppSharp.AST.Type; using Type = CppSharp.AST.Type;
@ -15,7 +14,19 @@ namespace CppSharp.Passes
private static readonly Regex regexFunctionParams = new Regex(@"\(?(.+)\)?", RegexOptions.Compiled); private static readonly Regex regexFunctionParams = new Regex(@"\(?(.+)\)?", RegexOptions.Compiled);
private static readonly Regex regexDoubleColon = new Regex(@"\w+::", RegexOptions.Compiled); private static readonly Regex regexDoubleColon = new Regex(@"\w+::", RegexOptions.Compiled);
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);
public HandleDefaultParamValuesPass()
{
Options.VisitFunctionParameters = false;
}
public override bool VisitTranslationUnit(TranslationUnit unit)
{
if (!unit.IsGenerated)
return false;
return base.VisitTranslationUnit(unit);
}
public override bool VisitFunctionDecl(Function function) public override bool VisitFunctionDecl(Function function)
{ {
@ -62,13 +73,24 @@ namespace CppSharp.Passes
} }
} }
private static bool CheckForDefaultPointer(Type desugared, Parameter parameter) private bool CheckForDefaultPointer(Type desugared, Parameter parameter)
{ {
if (desugared.IsPointer()) if (desugared.IsPointer())
{ {
// IntPtr.Zero is not a constant // IntPtr.Zero is not a constant
parameter.DefaultArgument.String = desugared.IsPointerToPrimitiveType(PrimitiveType.Void) ? if (desugared.IsPointerToPrimitiveType(PrimitiveType.Void))
"new global::System.IntPtr()" : "null"; {
parameter.DefaultArgument.String = "new global::System.IntPtr()";
return true;
}
Class @class;
if (desugared.GetFinalPointee().TryGetClass(out @class) && @class.IsValueType)
{
parameter.DefaultArgument.String = string.Format("new {0}()",
new CSharpTypePrinter(Driver).VisitClassDecl(@class));
return true;
}
parameter.DefaultArgument.String = "null";
return true; return true;
} }
return false; return false;
@ -90,27 +112,13 @@ namespace CppSharp.Passes
TypeMap typeMap; TypeMap typeMap;
if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap)) if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
{ {
Type typeInSignature; var typePrinterContext = new CSharpTypePrinterContext
string mappedTo;
if (Driver.Options.IsCSharpGenerator)
{ {
var typePrinterContext = new CSharpTypePrinterContext CSharpKind = CSharpTypePrinterContextKind.Managed,
{ Type = type
CSharpKind = CSharpTypePrinterContextKind.Managed, };
Type = type var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs();
}; var mappedTo = typeMap.CSharpSignature(typePrinterContext);
typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs();
mappedTo = typeMap.CSharpSignature(typePrinterContext);
}
else
{
var typePrinterContext = new CLITypePrinterContext
{
Type = type
};
typeInSignature = typeMap.CLISignatureType(typePrinterContext).SkipPointerRefs();
mappedTo = typeMap.CLISignature(typePrinterContext);
}
Enumeration @enum; Enumeration @enum;
if (typeInSignature.TryGetEnum(out @enum)) if (typeInSignature.TryGetEnum(out @enum))
{ {

4
tests/CSharpTemp/CSharpTemp.cpp

@ -362,6 +362,10 @@ void MethodsWithDefaultValues::rotate4x4Matrix(float angle, float x, float y, fl
{ {
} }
void MethodsWithDefaultValues::defaultPointerToValueType(QGenericArgument* pointer)
{
}
int MethodsWithDefaultValues::getA() int MethodsWithDefaultValues::getA()
{ {
return m_foo.A; return m_foo.A;

1
tests/CSharpTemp/CSharpTemp.h

@ -278,6 +278,7 @@ public:
void defaultIntWithLongExpression(unsigned int i = DEFAULT_INT); void defaultIntWithLongExpression(unsigned int i = DEFAULT_INT);
void defaultRefTypeEnumImplicitCtor(const QColor &fillColor = Qt::white); void defaultRefTypeEnumImplicitCtor(const QColor &fillColor = Qt::white);
void rotate4x4Matrix(float angle, float x, float y, float z = 0.0f); void rotate4x4Matrix(float angle, float x, float y, float z = 0.0f);
void defaultPointerToValueType(QGenericArgument* pointer = 0);
int getA(); int getA();
private: private:
Foo m_foo; Foo m_foo;

Loading…
Cancel
Save