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

Construct high-level datatypes for C enums #40

Open
edsko opened this issue Aug 2, 2024 · 2 comments
Open

Construct high-level datatypes for C enums #40

edsko opened this issue Aug 2, 2024 · 2 comments
Labels
confirmed-use-case Features for which there are client-confirmed use cases

Comments

@edsko
Copy link
Collaborator

edsko commented Aug 2, 2024

This is the high-level counterpart to #15. This is probably relatively simple, turning the C enum into a Haskell ADT. We do need to think about error handling here though: C enums are just ints; what if a function returns something about of the declared range of the enum? This is uncommon; Wikipedia says

Some compilers warn if an object with enumerated type is assigned a value that is not one of its constants. However, such an object can be assigned any values in the range of their compatible type, and enum constants can be used anywhere an integer is expected. For this reason, enum values are often used in place of preprocessor #define directives to create named constants. Such constants are generally safer to use than macros, since they reside within a specific identifier namespace.

(Emphasis not in original.) Thus, turning an enum into an ADT with a strictly limited range must be opt-in.

Moreover, not all enums correspond to ADTs at all; tracking "bitmap enums" separately at #65.

@edsko edsko added this to the 3: High-level API milestone Aug 2, 2024
@edsko edsko added the confirmed-use-case Features for which there are client-confirmed use cases label Aug 3, 2024
@edsko
Copy link
Collaborator Author

edsko commented Aug 3, 2024

As an example, given

typedef enum
{
    AS_X,
    AS_Y,
    AS_Z
} acme_sum;

we might want to generate

data AcmeSum = ASX | ASY | ASZ

@phadej
Copy link
Collaborator

phadej commented Sep 3, 2024

Also enums can contain duplicate values:

enum foo { fail = 1, bad = 1, good = 0 };

@edsko edsko mentioned this issue Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-use-case Features for which there are client-confirmed use cases
Projects
None yet
Development

No branches or pull requests

2 participants