Skip to content

Missing type casts when using bitfields with remapped types #618

@Exanite

Description

@Exanite

This is an edge case that occurs when using remappings with bitfields.

I discovered this issue when attempting to map Vulkan Flags enums to FlagBits in order to have ClangSharpPInvokeGenerator output FlagBits as the property type rather than uint.

One example of this issue happening is when generating the struct, VkAccelerationStructureInstanceKHR, with the mapping, VkGeometryInstanceFlagsKHR -> VkGeometryInstanceFlagBitsKHR. (Search for the struct name in vulkan_core.h)

This is a simplified example that I implemented as a test case:

typedef enum FlagBits {
    Member = 0x7FFFFFFF
} FlagBits;
typedef unsigned int Flags;

typedef struct Bitfield {
    uint bits : 8;
    Flags flags : 8;
} Bitfield;

This outputs the following, without the proper casts. This causes a compile error.
The expected is that the get return value is cast to FlagBits and value is cast to uint.

public uint _bitfield;

[NativeTypeName("Flags : 8")]
public FlagBits flags
{
    readonly get
    {
        return (_bitfield >> 8) & 0xFFu;
    }

    set
    {
        _bitfield = (_bitfield & ~(0xFFu << 8)) | ((value & 0xFFu) << 8);
    }
}

You can find the full test cases here in my fork: https://github.com/Exanite/ClangSharp/blob/d3a1dc1a90b85ba465f15b72f60992cbe4c28be7/tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs#L456

Note that BitfieldEnumPropertyTypeCastTest already passes.
BitfieldEnumPropertyTypeCastWithRemappingTest is the test that fails.

This shows that ClangSharp already handles this case to some extent and that it is the remapping that causes issues.


If this is a case that ClangSharp should handle, I'm interested in opening a PR for fixing this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions