Browse Source

Fixed the generated C# for public fields with type a dependent pointer.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1033/head
Dimitar Dobrev 8 years ago
parent
commit
6dec97fd99
  1. 45
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  3. 3
      src/Generator/Passes/MultipleInheritancePass.cs
  4. 8
      tests/CSharp/CSharp.Tests.cs
  5. 18
      tests/CSharp/CSharpTemplates.h

45
src/Generator/Generators/CSharp/CSharpSources.cs

@ -896,7 +896,8 @@ namespace CppSharp.Generators.CSharp @@ -896,7 +896,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.Declaration = field;
var arrayType = field.Type.Desugar() as ArrayType;
Type type = field.Type.Desugar();
var arrayType = type as ArrayType;
if (arrayType != null && @class.IsValueType)
{
@ -923,13 +924,20 @@ namespace CppSharp.Generators.CSharp @@ -923,13 +924,20 @@ namespace CppSharp.Generators.CSharp
if (marshal.Context.Return.StringBuilder.Length > 0)
{
WriteLine("{0} = {1}{2};", ctx.ReturnVarName,
field.Type.IsPointer() && field.Type.GetFinalPointee().IsPrimitiveType() &&
!CSharpTypePrinter.IsConstCharString(field.Type) ?
string.Format("({0}) ", CSharpTypePrinter.IntPtrType) :
string.Empty,
marshal.Context.Return);
Write($"{ctx.ReturnVarName} = ");
if (type.IsPointer())
{
Type pointee = type.GetFinalPointee();
if (pointee.IsPrimitiveType() &&
!CSharpTypePrinter.IsConstCharString(type))
{
Write($"({CSharpTypePrinter.IntPtrType}) ");
var templateSubstitution = pointee.Desugar(false) as TemplateParameterSubstitutionType;
if (templateSubstitution != null)
Write($"(object) ");
}
}
WriteLine($"{marshal.Context.Return};");
}
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
@ -1191,21 +1199,26 @@ namespace CppSharp.Generators.CSharp @@ -1191,21 +1199,26 @@ namespace CppSharp.Generators.CSharp
if (ctx.HasCodeBlock)
PushIndent();
Write("return ");
var @return = marshal.Context.Return.ToString();
if (field.Type.IsPointer())
{
var final = field.Type.GetFinalPointee().Desugar();
if (final.IsPrimitiveType() && !final.IsPrimitiveType(PrimitiveType.Void) &&
var final = field.Type.GetFinalPointee().Desugar(resolveTemplateSubstitution: false);
var templateSubstitution = final as TemplateParameterSubstitutionType;
if (templateSubstitution != null)
Write($"({templateSubstitution.ReplacedParameter.Parameter.Name}) (object) ");
if ((final.IsPrimitiveType() && !final.IsPrimitiveType(PrimitiveType.Void) &&
(!final.IsPrimitiveType(PrimitiveType.Char) &&
!final.IsPrimitiveType(PrimitiveType.WideChar) ||
(!Context.Options.MarshalCharAsManagedChar &&
!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)))
@return = string.Format("({0}*) {1}", field.Type.GetPointee().Desugar(), @return);
if (!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst &&
final.IsPrimitiveType(PrimitiveType.WideChar))
@return = string.Format("({0}*) {1}", field.Type.GetPointee().Desugar(), @return);
!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)) &&
templateSubstitution == null) ||
(!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst &&
final.IsPrimitiveType(PrimitiveType.WideChar)))
Write($"({field.Type.GetPointee().Desugar()}*) ");
}
WriteLine("return {0};", @return);
WriteLine($"{@return};");
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
WriteCloseBraceIndent();

4
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -35,12 +35,12 @@ namespace CppSharp.Generators.CSharp @@ -35,12 +35,12 @@ namespace CppSharp.Generators.CSharp
{
if (@class.IsDependent)
{
if (@class.Fields.Any(f => f.Type.Desugar() is TemplateParameterType))
if (@class.Fields.Any(f => f.Type.IsDependent))
{
foreach (var parameter in @class.TemplateParameters)
gen.WriteLine($"var __{parameter.Name} = typeof({parameter.Name});");
foreach (var specialization in @class.Specializations.Where(s => !s.Ignore))
foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated))
{
WriteTemplateSpecializationCheck(gen, @class, specialization);
gen.WriteStartBraceIndent();

3
src/Generator/Passes/MultipleInheritancePass.cs

@ -135,7 +135,8 @@ namespace CppSharp.Passes @@ -135,7 +135,8 @@ namespace CppSharp.Passes
Namespace = @interface,
Name = "Dispose",
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
SynthKind = FunctionSynthKind.InterfaceDispose
SynthKind = FunctionSynthKind.InterfaceDispose,
Mangled = string.Empty
};
@interface.Methods.Add(dispose);

8
tests/CSharp/CSharp.Tests.cs

@ -904,6 +904,14 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -904,6 +904,14 @@ public unsafe class CSharpTests : GeneratorTestFixture
}
}
[Test]
public void TestFieldWithDependentPointerType()
{
using (var dependentPointerFields = new DependentPointerFields<float>())
{
}
}
[Test]
public void TestAbstractImplementatonsInPrimaryAndSecondaryBases()
{

18
tests/CSharp/CSharpTemplates.h

@ -211,10 +211,22 @@ public: @@ -211,10 +211,22 @@ public:
template <typename T>
class DLL_API DependentPointerFields
{
private:
public:
DependentPointerFields();
~DependentPointerFields();
T* field;
};
template <typename T>
DependentPointerFields<T>::DependentPointerFields()
{
}
template <typename T>
DependentPointerFields<T>::~DependentPointerFields()
{
}
template <typename K, typename V>
class TwoTemplateArgs
{
@ -576,7 +588,8 @@ void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool> @@ -576,7 +588,8 @@ void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool>
TemplateWithIndexer<int> _10, TemplateWithIndexer<T1> _11,
TemplateWithIndexer<T2*> _12, TemplateWithIndexer<UsedInTemplatedIndexer> _13,
TemplateDerivedFromRegularDynamic<RegularDynamic> _14,
IndependentFields<OnlySpecialisedInTypeArg<double>> _15, std::string s);
IndependentFields<OnlySpecialisedInTypeArg<double>> _15,
DependentPointerFields<float> _16, std::string s);
void hasIgnoredParam(DependentValueFields<IndependentFields<Ignored>> ii);
@ -592,6 +605,7 @@ template class DLL_API IndependentFields<std::string>; @@ -592,6 +605,7 @@ template class DLL_API IndependentFields<std::string>;
template class DLL_API Base<int>;
template class DLL_API DependentValueFields<int>;
template class DLL_API DependentValueFields<float>;
template class DLL_API DependentPointerFields<float>;
template class DLL_API VirtualTemplate<int>;
template class DLL_API VirtualTemplate<bool>;
template class DLL_API HasDefaultTemplateArgument<int, int>;

Loading…
Cancel
Save