Browse Source

Fix parameterless constructors not being generated for structs (#1783)

* Fix parameterless constructors not being generated for structs

* Fix implicit non-trivial default ctor

* Adjust `Ignore` linked issue
pull/1787/head
Stefan 2 years ago committed by GitHub
parent
commit
a5afda8603
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Generator/AST/Utils.cs
  2. 3
      src/Generator/Generators/CLI/CLIHeaders.cs
  3. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  4. 3
      src/Generator/Types/Std/Stdlib.CSharp.cs
  5. 28
      tests/dotnet/CSharp/CSharp.Tests.cs
  6. 9
      tests/dotnet/CSharp/CSharp.h
  7. 6
      tests/dotnet/Common/Common.CSharp.csproj

2
src/Generator/AST/Utils.cs

@ -30,7 +30,7 @@ namespace CppSharp.AST @@ -30,7 +30,7 @@ namespace CppSharp.AST
var isEmptyCtor = method.IsConstructor && method.Parameters.Count == 0;
var @class = method.Namespace as Class;
if (@class != null && @class.IsValueType && isEmptyCtor)
if (@class != null && @class.IsValueType && isEmptyCtor && !@class.HasNonTrivialDefaultConstructor)
return true;
if (method.IsDestructor)

3
src/Generator/Generators/CLI/CLIHeaders.cs

@ -798,7 +798,8 @@ namespace CppSharp.Generators.CLI @@ -798,7 +798,8 @@ namespace CppSharp.Generators.CLI
public static bool FunctionIgnored(Function function)
{
return TypeIgnored(function.ReturnType.Type) ||
function.Parameters.Any(param => TypeIgnored(param.Type));
function.Parameters.Any(param => TypeIgnored(param.Type)) ||
function is Method { IsConstructor: true, Parameters: { Count: 0 }, Namespace: Class { IsValueType: true } };
}
public static bool TypeIgnored(CppSharp.AST.Type type)

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

@ -2678,7 +2678,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty @@ -2678,7 +2678,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
if (hasBase && !@class.IsValueType)
WriteLineIndent($": this({(method != null ? "(void*) null" : "native")})");
if (@class.IsValueType)
if (@class.IsValueType && method.Parameters.Count > 0)
WriteLineIndent(": this()");
}

3
src/Generator/Types/Std/Stdlib.CSharp.cs

@ -331,9 +331,6 @@ namespace CppSharp.Types.Std @@ -331,9 +331,6 @@ namespace CppSharp.Types.Std
string var;
if (ctx.ReturnVarName.LastIndexOf('.') > ctx.ReturnVarName.LastIndexOf("->"))
{
ctx.Before.WriteLine("throw new NotImplementedException(\"This method cannot currently be called because it would " +
"leave the object in an invalid state. See https://github.com/mono/CppSharp/issues/1777\");");
var = Generator.GeneratedIdentifier(ctx.ArgName);
ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})");
ctx.Before.WriteOpenBraceAndIndent();

28
tests/dotnet/CSharp/CSharp.Tests.cs

@ -2025,8 +2025,7 @@ public unsafe class CSharpTests @@ -2025,8 +2025,7 @@ public unsafe class CSharpTests
}
[Test]
[Ignore("https://github.com/mono/CppSharp/issues/1730")]
public void TestString()
public void TestValueTypeStringMember()
{
var test = new CSharp.ValueType();
Assert.AreEqual(string.Empty, test.StringMember);
@ -2036,4 +2035,29 @@ public unsafe class CSharpTests @@ -2036,4 +2035,29 @@ public unsafe class CSharpTests
Assert.AreEqual("test", test.StringMember);
Assert.AreEqual("test2", test.CharPtrMember);
}
[Test]
[Ignore("https://github.com/mono/CppSharp/issues/1786")]
public void TestValueTypeStringMemberDefaulted()
{
CSharp.ValueType test = default;
Assert.AreEqual(string.Empty, test.StringMember);
Assert.AreEqual(null, test.CharPtrMember);
test.StringMember = "test";
test.CharPtrMember = "test2";
Assert.AreEqual("test", test.StringMember);
Assert.AreEqual("test2", test.CharPtrMember);
}
[Test]
public void TestValueTypeStringMemberDefaultedCtor()
{
var test = new CSharp.ValueTypeNoCtor();
Assert.AreEqual(string.Empty, test.StringMember);
Assert.AreEqual(null, test.CharPtrMember);
test.StringMember = "test";
test.CharPtrMember = "test2";
Assert.AreEqual("test", test.StringMember);
Assert.AreEqual("test2", test.CharPtrMember);
}
}

9
tests/dotnet/CSharp/CSharp.h

@ -1644,9 +1644,14 @@ inline void DLL_API InstantiateOptionalTemplate(Optional<unsigned int>, Optional @@ -1644,9 +1644,14 @@ inline void DLL_API InstantiateOptionalTemplate(Optional<unsigned int>, Optional
CS_VALUE_TYPE class DLL_API ValueType {
public:
// Parameterless ctors are currently not generated for value types.
ValueType(int) { }
ValueType() { }
std::string string_member;
const char* char_ptr_member;
};
CS_VALUE_TYPE class DLL_API ValueTypeNoCtor {
public:
std::string string_member;
const char* char_ptr_member;
};

6
tests/dotnet/Common/Common.CSharp.csproj

@ -1 +1,5 @@ @@ -1 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>11.0</LangVersion>
</PropertyGroup>
</Project>
Loading…
Cancel
Save