Browse Source

Cloned types when changing them to interfaces for multiple inheritance.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/523/merge
Dimitar Dobrev 10 years ago
parent
commit
54caddeed1
  1. 7
      src/AST/Function.cs
  2. 154
      src/AST/Type.cs
  3. 7
      src/Generator/Passes/ConstructorToConversionOperatorPass.cs
  4. 21
      src/Generator/Passes/ParamTypeToInterfacePass.cs
  5. 3
      tests/CSharpTemp/CSharpTemp.Tests.cs
  6. 5
      tests/CSharpTemp/CSharpTemp.cpp
  7. 1
      tests/CSharpTemp/CSharpTemp.h

7
src/AST/Function.cs

@ -195,6 +195,13 @@ namespace CppSharp.AST
return hiddenParam.QualifiedType; return hiddenParam.QualifiedType;
} }
set
{
if (HasIndirectReturnTypeParameter)
Parameters.Single(p => p.Kind == ParameterKind.IndirectReturnType).QualifiedType = value;
else
ReturnType = value;
}
} }
public FunctionSynthKind SynthKind { get; set; } public FunctionSynthKind SynthKind { get; set; }

154
src/AST/Type.cs

@ -7,7 +7,7 @@ namespace CppSharp.AST
/// <summary> /// <summary>
/// Represents a C++ type. /// Represents a C++ type.
/// </summary> /// </summary>
public abstract class Type public abstract class Type : ICloneable
{ {
public static Func<Type, string> TypePrinterDelegate; public static Func<Type, string> TypePrinterDelegate;
@ -20,6 +20,8 @@ namespace CppSharp.AST
{ {
return TypePrinterDelegate(this); return TypePrinterDelegate(this);
} }
public abstract object Clone();
} }
/// <summary> /// <summary>
@ -108,6 +110,11 @@ namespace CppSharp.AST
return visitor.VisitTagType(this, quals); return visitor.VisitTagType(this, quals);
} }
public override object Clone()
{
return new TagType(Declaration) { IsDependent = IsDependent };
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TagType; var type = obj as TagType;
@ -154,6 +161,17 @@ namespace CppSharp.AST
return visitor.VisitArrayType(this, quals); return visitor.VisitArrayType(this, quals);
} }
public override object Clone()
{
return new ArrayType
{
IsDependent = IsDependent,
QualifiedType = new QualifiedType((Type) QualifiedType.Type.Clone(), QualifiedType.Qualifiers),
SizeType = SizeType,
Size = Size
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as ArrayType; var type = obj as ArrayType;
@ -195,6 +213,17 @@ namespace CppSharp.AST
return visitor.VisitFunctionType(this, quals); return visitor.VisitFunctionType(this, quals);
} }
public override object Clone()
{
return new FunctionType
{
IsDependent = IsDependent,
ReturnType = new QualifiedType((Type) ReturnType.Type.Clone(), ReturnType.Qualifiers),
Parameters = Parameters.Select(p => new Parameter(p)).ToList(),
CallingConvention = CallingConvention
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as FunctionType; var type = obj as FunctionType;
@ -252,6 +281,15 @@ namespace CppSharp.AST
return visitor.VisitPointerType(this, QualifiedPointee.Qualifiers); return visitor.VisitPointerType(this, QualifiedPointee.Qualifiers);
} }
public override object Clone()
{
return new PointerType(new QualifiedType((Type) QualifiedPointee.Type.Clone(), QualifiedPointee.Qualifiers))
{
IsDependent = IsDependent,
Modifier = Modifier
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as PointerType; var type = obj as PointerType;
@ -284,6 +322,15 @@ namespace CppSharp.AST
return visitor.VisitMemberPointerType(this, quals); return visitor.VisitMemberPointerType(this, quals);
} }
public override object Clone()
{
return new MemberPointerType
{
IsDependent = IsDependent,
QualifiedPointee = new QualifiedType((Type) QualifiedPointee.Type.Clone(), QualifiedPointee.Qualifiers)
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var pointer = obj as MemberPointerType; var pointer = obj as MemberPointerType;
@ -310,6 +357,15 @@ namespace CppSharp.AST
return visitor.VisitTypedefType(this, quals); return visitor.VisitTypedefType(this, quals);
} }
public override object Clone()
{
return new TypedefType
{
IsDependent = IsDependent,
Declaration = Declaration
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var typedef = obj as TypedefType; var typedef = obj as TypedefType;
@ -346,6 +402,16 @@ namespace CppSharp.AST
return visitor.VisitAttributedType(this, quals); return visitor.VisitAttributedType(this, quals);
} }
public override object Clone()
{
return new AttributedType
{
IsDependent = IsDependent,
Modified = new QualifiedType((Type) Modified.Type.Clone(), Modified.Qualifiers),
Equivalent = new QualifiedType((Type) Equivalent.Type.Clone(), Equivalent.Qualifiers)
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var attributed = obj as AttributedType; var attributed = obj as AttributedType;
@ -375,6 +441,17 @@ namespace CppSharp.AST
return visitor.VisitDecayedType(this, quals); return visitor.VisitDecayedType(this, quals);
} }
public override object Clone()
{
return new DecayedType
{
IsDependent = IsDependent,
Decayed = new QualifiedType((Type) Decayed.Type.Clone(), Decayed.Qualifiers),
Original = new QualifiedType((Type) Original.Type.Clone(), Original.Qualifiers),
Pointee = new QualifiedType((Type) Pointee.Type.Clone(), Pointee.Qualifiers),
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var decay = obj as DecayedType; var decay = obj as DecayedType;
@ -483,6 +560,25 @@ namespace CppSharp.AST
return visitor.VisitTemplateSpecializationType(this, quals); return visitor.VisitTemplateSpecializationType(this, quals);
} }
public override object Clone()
{
return new TemplateSpecializationType
{
IsDependent = IsDependent,
Arguments = Arguments.Select(
t =>
new TemplateArgument
{
Declaration = t.Declaration,
Integral = t.Integral,
Kind = t.Kind,
Type = new QualifiedType((Type) t.Type.Type.Clone(), t.Type.Qualifiers)
}).ToList(),
Template = Template,
Desugared = (Type) Desugared.Clone()
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateSpecializationType; var type = obj as TemplateSpecializationType;
@ -514,6 +610,23 @@ namespace CppSharp.AST
return visitor.VisitTemplateParameterType(this, quals); return visitor.VisitTemplateParameterType(this, quals);
} }
public override object Clone()
{
return new TemplateParameterType
{
IsDependent = IsDependent,
Parameter = new TemplateParameter
{
Constraint = Parameter.Constraint,
IsTypeParameter = Parameter.IsTypeParameter,
Name = Parameter.Name
},
Depth = Depth,
Index = Index,
IsParameterPack = IsParameterPack
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateParameterType; var type = obj as TemplateParameterType;
@ -544,6 +657,15 @@ namespace CppSharp.AST
return visitor.VisitTemplateParameterSubstitutionType(this, quals); return visitor.VisitTemplateParameterSubstitutionType(this, quals);
} }
public override object Clone()
{
return new TemplateParameterSubstitutionType
{
IsDependent = IsDependent,
Replacement = new QualifiedType((Type) Replacement.Type.Clone(), Replacement.Qualifiers)
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as TemplateParameterSubstitutionType; var type = obj as TemplateParameterSubstitutionType;
@ -573,6 +695,16 @@ namespace CppSharp.AST
return visitor.VisitInjectedClassNameType(this, quals); return visitor.VisitInjectedClassNameType(this, quals);
} }
public override object Clone()
{
return new InjectedClassNameType
{
IsDependent = IsDependent,
TemplateSpecialization = (TemplateSpecializationType) TemplateSpecialization.Clone(),
Class = Class
};
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as InjectedClassNameType; var type = obj as InjectedClassNameType;
@ -600,6 +732,11 @@ namespace CppSharp.AST
{ {
return visitor.VisitDependentNameType(this, quals); return visitor.VisitDependentNameType(this, quals);
} }
public override object Clone()
{
return new DependentNameType { IsDependent = IsDependent };
}
} }
/// <summary> /// <summary>
@ -620,6 +757,11 @@ namespace CppSharp.AST
return visitor.VisitCILType(this, quals); return visitor.VisitCILType(this, quals);
} }
public override object Clone()
{
return new CILType(Type) { IsDependent = IsDependent };
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as CILType; var type = obj as CILType;
@ -640,6 +782,11 @@ namespace CppSharp.AST
{ {
return visitor.VisitPackExpansionType(this, quals); return visitor.VisitPackExpansionType(this, quals);
} }
public override object Clone()
{
return new PackExpansionType { IsDependent = IsDependent };
}
} }
#region Primitives #region Primitives
@ -711,6 +858,11 @@ namespace CppSharp.AST
return visitor.VisitBuiltinType(this, quals); return visitor.VisitBuiltinType(this, quals);
} }
public override object Clone()
{
return new BuiltinType(Type) { IsDependent = IsDependent };
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var type = obj as BuiltinType; var type = obj as BuiltinType;

7
src/Generator/Passes/ConstructorToConversionOperatorPass.cs

@ -50,12 +50,7 @@ namespace CppSharp.Passes
OperatorKind = operatorKind, OperatorKind = operatorKind,
IsExplicit = method.IsExplicit IsExplicit = method.IsExplicit
}; };
var p = new Parameter(parameter); conversionOperator.Parameters.Add(new Parameter(parameter) { DefaultArgument = null });
Class @class;
if (p.Type.SkipPointerRefs().TryGetClass(out @class))
p.QualifiedType = new QualifiedType(new TagType(@class), parameter.QualifiedType.Qualifiers);
p.DefaultArgument = null;
conversionOperator.Parameters.Add(p);
((Class) method.Namespace).Methods.Add(conversionOperator); ((Class) method.Namespace).Methods.Add(conversionOperator);
return true; return true;
} }

21
src/Generator/Passes/ParamTypeToInterfacePass.cs

@ -9,19 +9,25 @@ namespace CppSharp.Passes
public override bool VisitFunctionDecl(Function function) public override bool VisitFunctionDecl(Function function)
{ {
if (!function.IsOperator || function.Parameters.Count > 1) if (!function.IsOperator || function.Parameters.Count > 1)
ChangeToInterfaceType(function.OriginalReturnType); {
var originalReturnType = function.OriginalReturnType;
ChangeToInterfaceType(ref originalReturnType);
function.OriginalReturnType = originalReturnType;
}
if (function.OperatorKind != CXXOperatorKind.Conversion && if (function.OperatorKind != CXXOperatorKind.Conversion &&
function.OperatorKind != CXXOperatorKind.ExplicitConversion) function.OperatorKind != CXXOperatorKind.ExplicitConversion)
{
foreach (var parameter in function.Parameters.Where(p => p.Kind != ParameterKind.OperatorParameter)) foreach (var parameter in function.Parameters.Where(p => p.Kind != ParameterKind.OperatorParameter))
ChangeToInterfaceType(parameter.QualifiedType); {
} var qualifiedType = parameter.QualifiedType;
ChangeToInterfaceType(ref qualifiedType);
parameter.QualifiedType = qualifiedType;
}
return base.VisitFunctionDecl(function); return base.VisitFunctionDecl(function);
} }
private static void ChangeToInterfaceType(QualifiedType type) private static void ChangeToInterfaceType(ref QualifiedType type)
{ {
var tagType = (type.Type.GetFinalPointee() ?? type.Type) as TagType; var tagType = (type.Type.GetFinalPointee() ?? type.Type) as TagType;
if (tagType != null) if (tagType != null)
@ -31,7 +37,10 @@ namespace CppSharp.Passes
{ {
var @interface = @class.Namespace.Classes.Find(c => c.OriginalClass == @class); var @interface = @class.Namespace.Classes.Find(c => c.OriginalClass == @class);
if (@interface != null) if (@interface != null)
tagType.Declaration = @interface; {
type.Type = (Type) type.Type.Clone();
((TagType) (type.Type.GetFinalPointee() ?? type.Type)).Declaration = @interface;
}
} }
} }
} }

3
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -39,6 +39,9 @@ public class CSharpTempTests : GeneratorTestFixture
new Bar(qux).Dispose(); new Bar(qux).Dispose();
} }
} }
using (ComplexType complexType = TestFlag.Flag1)
{
}
} }
[Test] [Test]

5
tests/CSharpTemp/CSharpTemp.cpp

@ -236,6 +236,11 @@ ComplexType::ComplexType() : qFlags(QFlags<TestFlag>(TestFlag::Flag2))
{ {
} }
ComplexType::ComplexType(const QFlags<TestFlag> f) : qFlags(QFlags<TestFlag>(TestFlag::Flag2))
{
qFlags = f;
}
int ComplexType::check() int ComplexType::check()
{ {
return 5; return 5;

1
tests/CSharpTemp/CSharpTemp.h

@ -167,6 +167,7 @@ class DLL_API ComplexType
{ {
public: public:
ComplexType(); ComplexType();
ComplexType(const QFlags<TestFlag> f);
int check(); int check();
QFlags<TestFlag> returnsQFlags(); QFlags<TestFlag> returnsQFlags();
void takesQFlags(const QFlags<int> f); void takesQFlags(const QFlags<int> f);

Loading…
Cancel
Save