[fpc-devel] C flags, enums, sets, and the "Pascal way"

Alcom alcombiz at gmail.com
Fri Apr 1 22:01:46 CEST 2016


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;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160401/5a6d63df/attachment.html>


More information about the fpc-devel mailing list