Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2016-04

C flags, enums, sets, and the "Pascal way"

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alcom

I'm working on some translated C headers for Vulkan for use in FPC.

In the vulkan.h header file, we have the following:

    typedef uint32_t VkFlags;
    typedef enum VkSparseImageFormatFlagBits {
        VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = 0x00000001,
        VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = 0x00000002,
        VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = 0x00000004,
    } VkSparseImageFormatFlagBits;
    typedef VkFlags VkSparseImageFormatFlags;

For code maintenance purposes, it is desirable to have a header translation that is as close as possible to the source. A reasonable translation to Pascal of this might be:

    TVkFlags = Cardinal;
    TVkSparseImageFormatFlagBits =
    (
        VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001,
        VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002,
        VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004
    );
    TVkSparseImageFormatFlags = TVkFlags;

However, this solution has only the *illusion* of being strongly typed -- nothing about which the compiler is aware links TVkImageCreateFlags with the TVkImageCreateFlagBits.

A seemingly better solution (albeit incorrect) would be the following:

    {$packset 4}
    TVkSparseImageFormatFlagBits =
    (
        VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001,
        VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002,
        VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004
    );
    TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits;

This will not give the expected results, because FPC interprets the enum constant as an exponent (or bit position, if you prefer).

My question is this: Is there currently a modifier for the set declaration that instructs the compiler to accept the enum values as literal constants, instead of interpreting them as exponents?

e.g. something like this:
    {$EnumConstantsUseLiteral}
    --or--
    TVkSparseImageFormatFlags = literalset of TVkSparseImageFormatFlagBits;
    --or--
    TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits;
literal;
    --or--
    TVkSparseImageFormatFlags = set of literal TVkSparseImageFormatFlagBits;

Reply by email

Unknown author
to Alcom

Well,

h2pas gives you:
{$packrecords c}
   type
     VkFlags = uint32_t;
     VkSparseImageFormatFlagBits =
(VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT := $00000001,
       VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT := $00000002,
       VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT := $00000004
       );
     VkSparseImageFormatFlags = VkFlags;
Alcom wrote 68 lines
I'm working on some translated C headers for Vulkan for use in FPC. In the vulkan.h header file, we have the following: typedef uint32_t VkFlags; typedef enum VkSparseImageFormatFlagBits { VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = 0x00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = 0x00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = 0x00000004, } VkSparseImageFormatFlagBits; typedef VkFlags VkSparseImageFormatFlags; For code maintenance purposes, it is desirable to have a header translation that is as close as possible to the source. A reasonable translation to Pascal of this might be: TVkFlags = Cardinal; TVkSparseImageFormatFlagBits = ( VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004 ); TVkSparseImageFormatFlags = TVkFlags; However, this solution has only the *illusion* of being strongly typed -- nothing about which the compiler is aware links TVkImageCreateFlags with the TVkImageCreateFlagBits. A seemingly better solution (albeit incorrect) would be the following: {$packset 4} TVkSparseImageFormatFlagBits = ( VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004 ); TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits; This will not give the expected results, because FPC interprets the enum constant as an exponent (or bit position, if you prefer). My question is this: Is there currently a modifier for the set declaration that instructs the compiler to accept the enum values as literal constants, instead of interpreting them as exponents? e.g. something like this: {$EnumConstantsUseLiteral} --or-- TVkSparseImageFormatFlags = literalset of TVkSparseImageFormatFlagBits; --or-- TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits; literal; --or-- TVkSparseImageFormatFlags = set of literal TVkSparseImageFormatFlagBits; _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Alcom
to Unknown author

Yes, h2pas essentially generates my second example.

My question has more to do with using predefined enum constants as bitmasks in sets, instead of the default behaviour (which is to use the enum constant as the bit position instead of a bitmask).

My previous suggestion {$EnumConstantsUseLiteral} makes no sense as a compiler mode flag; a better choice would be something like {$UseEnumConstantsAsBitmasksInSets}, or something even longer if possible. : -)

Show quoted text 94 lines
Well, h2pas gives you: {$packrecords c} type VkFlags = uint32_t; VkSparseImageFormatFlagBits = (VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT := $00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT := $00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT := $00000004 ); VkSparseImageFormatFlags = VkFlags; On 01-Apr-16 10:01 PM, Alcom wrote: I'm working on some translated C headers for Vulkan for use in FPC. In the vulkan.h header file, we have the following: typedef uint32_t VkFlags; typedef enum VkSparseImageFormatFlagBits { VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = 0x00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = 0x00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = 0x00000004, } VkSparseImageFormatFlagBits; typedef VkFlags VkSparseImageFormatFlags; For code maintenance purposes, it is desirable to have a header translation that is as close as possible to the source. A reasonable translation to Pascal of this might be: TVkFlags = Cardinal; TVkSparseImageFormatFlagBits = ( VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004 ); TVkSparseImageFormatFlags = TVkFlags; However, this solution has only the *illusion* of being strongly typed -- nothing about which the compiler is aware links TVkImageCreateFlags with the TVkImageCreateFlagBits. A seemingly better solution (albeit incorrect) would be the following: {$packset 4} TVkSparseImageFormatFlagBits = ( VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001, VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002, VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004 ); TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits; This will not give the expected results, because FPC interprets the enum constant as an exponent (or bit position, if you prefer). My question is this: Is there currently a modifier for the set declaration that instructs the compiler to accept the enum values as literal constants, instead of interpreting them as exponents? e.g. something like this: {$EnumConstantsUseLiteral} --or-- TVkSparseImageFormatFlags = literalset of TVkSparseImageFormatFlagBits; --or-- TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits; literal; --or-- TVkSparseImageFormatFlags = set of literal TVkSparseImageFormatFlagBits; _______________________________________________ fpc-devel maillist - _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email