Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added marshalling to struct... #525

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ src/generator/generator
*.csproj
*.ilk
*.manifest
*.tmp
/build/vs2012
/build/vs2013
/build/gmake
Expand Down
8 changes: 8 additions & 0 deletions src/AST/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public Class()
IsUnion = false;
IsOpaque = false;
IsPOD = false;
IsForcedRefType = false;
Type = ClassType.RefType;
Layout = new ClassLayout();
}
Expand All @@ -137,6 +138,13 @@ public Class(Class @class)
HasNonTrivialCopyConstructor = @class.HasNonTrivialCopyConstructor;
HasNonTrivialDestructor = @class.HasNonTrivialDestructor;
IsStatic = @class.IsStatic;
IsForcedRefType = @class.IsForcedRefType;
}

public bool IsForcedRefType
{
get;
set;
}

public bool HasBase
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Generators/CSharp/CSharpTextTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2573,10 +2573,10 @@ private ParamMarshal GenerateFunctionParamMarshal(Parameter param, int paramInde
var paramType = param.Type;

Class @class;
if ( (paramType.GetFinalPointee() ?? paramType).Desugar().TryGetClass(out @class)
&& @class.IsRefType)
if ( (paramType.GetFinalPointee() ?? paramType).Desugar().TryGetClass(out @class))
{
WriteLine("{0} = new {1}();", param.Name, paramType);
if(@class.IsRefType || @class.IsValueType)
WriteLine("{0} = new {1}();", param.Name, paramType);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Generator/Passes/CheckMacrosPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ namespace CppSharp.Passes
/// Used to flag a method as internal to an assembly. So, it is
/// not accessible outside that assembly.
///
/// CS_REF_TYPE (classes and structs)
/// Used to flag a type as ref type. So, it is generated as a C# class
/// even when the CheckValueTypeClassesPass is enabled.
///
/// There isn't a standardized header provided by CppSharp so you will
/// have to define these on your own.
/// </summary>
Expand Down Expand Up @@ -109,6 +113,8 @@ public override bool VisitClassDecl(Class @class)

if (expansions.Any(e => e.Text == Prefix + "_VALUE_TYPE"))
@class.Type = ClassType.ValueType;
else if (expansions.Any(e => e.Text == Prefix + "_REF_TYPE"))
@class.IsForcedRefType = true;

// If the class is a forward declaration, then we process the macro expansions
// of the complete class as if they were specified on the forward declaration.
Expand Down
39 changes: 39 additions & 0 deletions src/Generator/Passes/CheckValueTypeClassesPass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using CppSharp.AST;

namespace CppSharp.Passes
{
public class CheckValueTypeClassesPass : TranslationUnitPass
{
public CheckValueTypeClassesPass()
{
}

public override bool VisitClassDecl(Class @class)
{
@class.Type = CheckClassIsStructible(@class, Driver) ? ClassType.ValueType : @class.Type;
return base.VisitClassDecl(@class);
}

private bool CheckClassIsStructible(Class @class, Driver Driver)
{
if (@class.IsUnion || @class.Namespace.Templates.Any(tmp => tmp.Name.Equals(@class.Name)))
return false;
if (@class.IsInterface || @class.IsStatic || @class.IsAbstract)
return false;
if (@class.Declarations.Any(decl => decl.Access == AccessSpecifier.Protected))
return false;
if (@class.IsDynamic)
return false;
if (@class.HasBaseClass && @class.BaseClass.IsRefType)
return false;

var allTrUnits = Driver.ASTContext.TranslationUnits;
if (allTrUnits.Any(trUnit => trUnit.Classes.Any(
cls => cls.Bases.Any(clss => clss.IsClass && clss.Class == @class))))
return false;

return @class.IsPOD && [email protected];
}
}
}
1 change: 1 addition & 0 deletions tests/Basic/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override void Preprocess(Driver driver, ASTContext ctx)
{
driver.AddTranslationUnitPass(new GetterSetterToPropertyPass());
driver.AddTranslationUnitPass(new CheckMacroPass());
driver.AddTranslationUnitPass(new CheckValueTypeClassesPass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is supposed to be added to Driver, not just to the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to see how to do that. WIll try.

ctx.SetClassAsValueType("Bar");
ctx.SetClassAsValueType("Bar2");
ctx.IgnoreClassWithName("IgnoredType");
Expand Down
5 changes: 5 additions & 0 deletions tests/Basic/Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,8 @@ class DLL_API ReturnsEmpty
public:
Empty getEmpty();
};

DLL_API class TestIsStructFreeClass { };
DLL_API struct TestIsStructFreeStruct { };
DLL_API struct TestIsStructInheritedStruct { };
DLL_API struct TestIsStructInheritingStruct : public TestIsStructInheritedStruct { };
2 changes: 2 additions & 0 deletions tests/CLITemp/CLITemp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Passes;
using CppSharp.Utils;

namespace CppSharp.Tests
Expand All @@ -20,6 +21,7 @@ public override void Setup(Driver driver)

public override void Preprocess(Driver driver, ASTContext ctx)
{
driver.TranslationUnitPasses.AddPass(new CheckValueTypeClassesPass());
}

public static void Main(string[] args)
Expand Down
3 changes: 3 additions & 0 deletions tests/STL/STL.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Passes;
using CppSharp.Utils;

namespace CppSharp.Tests
Expand All @@ -14,6 +15,8 @@ public STL(GeneratorKind kind)
public override void Preprocess(Driver driver, ASTContext ctx)
{
ctx.SetClassAsValueType("IntWrapperValueType");
driver.TranslationUnitPasses.AddPass(new CheckMacroPass());
driver.TranslationUnitPasses.AddPass(new CheckValueTypeClassesPass());
}

public static void Main(string[] args)
Expand Down
3 changes: 2 additions & 1 deletion tests/STL/STL.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <vector>
#include <ostream>

struct DLL_API IntWrapper
#define CS_REF_TYPE
struct DLL_API CS_REF_TYPE IntWrapper
{
int Value;
};
Expand Down