ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Incorrect enum base type

Open nitroxis opened this issue 2 years ago • 2 comments

The PInvoke generator seems to use the wrong enum base type (or else, it generates values that are not always signed ints). One example is the FT_Glyph_Format enum from freetype defined here.

The tool generates:

public enum FT_Glyph_Format_
{
    FT_GLYPH_FORMAT_NONE = unchecked(((uint)((byte)(0)) << 24) | ((uint)((byte)(0)) << 16) | ((uint)((byte)(0)) << 8) | (uint)((byte)(0))),
    FT_GLYPH_FORMAT_COMPOSITE = unchecked(((uint)((byte)('c')) << 24) | ((uint)((byte)('o')) << 16) | ((uint)((byte)('m')) << 8) | (uint)((byte)('p'))),
    FT_GLYPH_FORMAT_BITMAP = unchecked(((uint)((byte)('b')) << 24) | ((uint)((byte)('i')) << 16) | ((uint)((byte)('t')) << 8) | (uint)((byte)('s'))),
    FT_GLYPH_FORMAT_OUTLINE = unchecked(((uint)((byte)('o')) << 24) | ((uint)((byte)('u')) << 16) | ((uint)((byte)('t')) << 8) | (uint)((byte)('l'))),
    FT_GLYPH_FORMAT_PLOTTER = unchecked(((uint)((byte)('p')) << 24) | ((uint)((byte)('l')) << 16) | ((uint)((byte)('o')) << 8) | (uint)((byte)('t'))),
    FT_GLYPH_FORMAT_SVG = unchecked(((uint)((byte)('S')) << 24) | ((uint)((byte)('V')) << 16) | ((uint)((byte)('G')) << 8) | (uint)((byte)(' '))),
}

It seems like the macro is expanded/converted correctly, but it produces uint. Either, the enum should use uint as base type, or the individual values need to be cast to int using unchecked((int) expr). I think both would yield correct code/bindings.

nitroxis avatar Dec 23 '23 02:12 nitroxis

Still a problem 20.1.2.1

public enum shaderc_env_version
{
    vulkan_1_0 = ((1U << 22)),
    vulkan_1_1 = ((1U << 22) | (1 << 12)),
    vulkan_1_2 = ((1U << 22) | (2 << 12)),
    vulkan_1_3 = ((1U << 22) | (3 << 12)),
    vulkan_1_4 = ((1U << 22) | (4 << 12)),
    opengl_4_5 = 450,
    webgpu,
}

Source C code: https://github.com/google/shaderc/blob/a5a8caa1951b3f893a08f63cab2dc877087dc05b/libshaderc/include/shaderc/env.h#L37-L51

NiTiSon avatar Jun 02 '25 08:06 NiTiSon

The generator needs to retype to int to correctly match the sign of the enum values as defined in C/C++.

You have to explicitly type the enum for it to be a type other than int, it isn't determined based on the values the enum contains.

tannergooding avatar Jul 17 '25 17:07 tannergooding