Browse Source

Fixed default args with template types; added an option for expressions to use in type maps.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/477/head
Dimitar Dobrev 10 years ago
parent
commit
7cff21aa78
  1. 5
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  2. 41
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  3. 4
      tests/CSharpTemp/CSharpTemp.cpp
  4. 37
      tests/CSharpTemp/CSharpTemp.cs
  5. 6
      tests/CSharpTemp/CSharpTemp.h

5
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -13,7 +13,8 @@ namespace CppSharp.Generators.CSharp
Native, Native,
Managed, Managed,
ManagedPointer, ManagedPointer,
GenericDelegate GenericDelegate,
DefaultExpression
} }
public class CSharpTypePrinterContext : TypePrinterContext public class CSharpTypePrinterContext : TypePrinterContext
@ -53,7 +54,7 @@ namespace CppSharp.Generators.CSharp
public class CSharpTypePrinter : ITypePrinter<CSharpTypePrinterResult>, public class CSharpTypePrinter : ITypePrinter<CSharpTypePrinterResult>,
IDeclVisitor<CSharpTypePrinterResult> IDeclVisitor<CSharpTypePrinterResult>
{ {
private Driver driver; private readonly Driver driver;
private readonly Stack<CSharpTypePrinterContextKind> contexts; private readonly Stack<CSharpTypePrinterContextKind> contexts;

41
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -42,7 +42,8 @@ namespace CppSharp.Passes
CheckFloatSyntax(desugared, parameter); CheckFloatSyntax(desugared, parameter);
bool? defaultConstruct = CheckForDefaultConstruct(desugared, parameter.DefaultArgument); bool? defaultConstruct = CheckForDefaultConstruct(desugared, parameter.DefaultArgument,
parameter.QualifiedType.Qualifiers);
if (defaultConstruct == null || if (defaultConstruct == null ||
(!Driver.Options.MarshalCharAsManagedChar && (!Driver.Options.MarshalCharAsManagedChar &&
parameter.Type.Desugar().IsPrimitiveType(PrimitiveType.UChar))) parameter.Type.Desugar().IsPrimitiveType(PrimitiveType.UChar)))
@ -106,12 +107,10 @@ namespace CppSharp.Passes
return false; return false;
} }
private bool? CheckForDefaultConstruct(Type desugared, Expression arg) private bool? CheckForDefaultConstruct(Type desugared, Expression arg, TypeQualifiers qualifiers)
{ {
// Unwrapping the underlying type behind a possible pointer/reference // Unwrapping the underlying type behind a possible pointer/reference
Type type; Type type = desugared.GetFinalPointee() ?? desugared;
desugared.IsPointerTo(out type);
type = type ?? desugared;
Class decl; Class decl;
if (!type.TryGetClass(out decl)) if (!type.TryGetClass(out decl))
@ -120,38 +119,48 @@ namespace CppSharp.Passes
var ctor = arg.Declaration as Method; var ctor = arg.Declaration as Method;
TypeMap typeMap; TypeMap typeMap;
if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
{
var typePrinterContext = new CSharpTypePrinterContext var typePrinterContext = new CSharpTypePrinterContext
{ {
CSharpKind = CSharpTypePrinterContextKind.Managed, CSharpKind = CSharpTypePrinterContextKind.DefaultExpression,
Type = type Type = type
}; };
string typePrinterResult = null;
if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
{
var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs(); var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs();
var mappedTo = typeMap.CSharpSignature(typePrinterContext);
Enumeration @enum; Enumeration @enum;
if (typeInSignature.TryGetEnum(out @enum)) if (typeInSignature.TryGetEnum(out @enum))
{
return false; return false;
}
if (ctor == null || !ctor.IsConstructor) if (ctor == null || !ctor.IsConstructor)
return false; return false;
if (mappedTo == "string" && ctor.Parameters.Count == 0)
typePrinterResult = typeMap.CSharpSignature(typePrinterContext);
if (typePrinterResult == "string" && ctor.Parameters.Count == 0)
{ {
arg.String = "\"\""; arg.String = "\"\"";
return true; return true;
} }
} }
if (regexCtor.IsMatch(arg.String)) var match = regexCtor.Match(arg.String);
if (match.Success)
{ {
arg.String = string.Format("new {0}", arg.String); if (ctor != null)
if (ctor != null && ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress())
{ {
var templateSpecializationType = type as TemplateSpecializationType;
var typePrinter = new CSharpTypePrinter(Driver);
typePrinterResult = typePrinterResult ?? (templateSpecializationType != null
? typePrinter.VisitTemplateSpecializationType(templateSpecializationType, qualifiers)
: typePrinter.VisitClassDecl((Class) ctor.Namespace)).Type;
arg.String = string.Format("new {0}{1}", typePrinterResult, match.Groups[2].Value);
if (ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress())
arg.String = arg.String.Replace("(0)", "()"); arg.String = arg.String.Replace("(0)", "()");
return decl.IsValueType ? true : (bool?) null;
} }
else
arg.String = string.Format("new {0}", arg.String);
} }
else else
{ {

4
tests/CSharpTemp/CSharpTemp.cpp

@ -294,6 +294,10 @@ MethodsWithDefaultValues::MethodsWithDefaultValues(int a)
m_foo.A = a; m_foo.A = a;
} }
MethodsWithDefaultValues::MethodsWithDefaultValues(double d, QList<QColor> list)
{
}
void MethodsWithDefaultValues::defaultPointer(Foo *ptr) void MethodsWithDefaultValues::defaultPointer(Foo *ptr)
{ {
} }

37
tests/CSharpTemp/CSharpTemp.cs

@ -41,6 +41,43 @@ namespace CppSharp.Tests
} }
} }
[TypeMap("QList")]
public class QList : TypeMap
{
public override bool IsIgnored
{
get
{
var type = (TemplateSpecializationType) this.Type;
var pointeeType = type.Arguments[0].Type;
var checker = new TypeIgnoreChecker(TypeMapDatabase);
pointeeType.Visit(checker);
return checker.IsIgnored;
}
}
public override string CSharpSignature(CSharpTypePrinterContext ctx)
{
if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native)
return Type.IsAddress() ? "QList.Internal*" : "QList.Internal";
return string.Format("System.Collections.Generic.{0}<{1}>",
ctx.CSharpKind == CSharpTypePrinterContextKind.DefaultExpression ? "List" : "IList",
ctx.GetTemplateParameterList());
}
public override void CSharpMarshalToNative(MarshalContext ctx)
{
// pointless, put just so that the generated code compiles
ctx.Return.Write("new QList.Internal()");
}
public override void CSharpMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
}
public class TestAttributesPass : TranslationUnitPass public class TestAttributesPass : TranslationUnitPass
{ {
public override bool VisitFunctionDecl(Function function) public override bool VisitFunctionDecl(Function function)

6
tests/CSharpTemp/CSharpTemp.h

@ -251,11 +251,17 @@ public:
QColor(Qt::GlobalColor color); QColor(Qt::GlobalColor color);
}; };
template <typename T>
class QList
{
};
class DLL_API MethodsWithDefaultValues class DLL_API MethodsWithDefaultValues
{ {
public: public:
MethodsWithDefaultValues(Foo foo = Foo()); MethodsWithDefaultValues(Foo foo = Foo());
MethodsWithDefaultValues(int a); MethodsWithDefaultValues(int a);
MethodsWithDefaultValues(double d, QList<QColor> list = QList<QColor>());
void defaultPointer(Foo* ptr = 0); void defaultPointer(Foo* ptr = 0);
void defaultVoidStar(void* ptr = 0); void defaultVoidStar(void* ptr = 0);
void defaultValueType(QGenericArgument valueType = QGenericArgument()); void defaultValueType(QGenericArgument valueType = QGenericArgument());

Loading…
Cancel
Save