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 1 commit
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
42 changes: 42 additions & 0 deletions src/Generator/Passes/CheckValueTypeClassesPass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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.Methods.Any(m => m.IsVirtual))
Copy link
Contributor

Choose a reason for hiding this comment

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

That's OK but just so you know, there's a short cut: @class.IsDynamic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh. great! Thanks.

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;

if (@class.IsPOD || @class.IsValueType)
Copy link
Contributor

Choose a reason for hiding this comment

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

What @tritao had in mind was to use IsPOD instead of IsValueType. So you'd best change it to just:

return @class.IsPOD.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

return true;

return false;
}
}
}
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 { };