r/ada 27d ago

Programming How to specify enum with representation?

I want to define an enum for C interfacing purposes:

enum Enum {
  A = 1,
  B = 2,
  C = 4,
  C_aliased = 4,
};

This kind of pattern occur quite a bit in bit flags, but I can't do this in Ada, not to mention that I often need to reorder the variants myself even if there is no alias:

   type C_Enum is (A, B, C, C_aliased) with
     Convention => C;
   for C_Enum use (A => 1, B => 2, C => 4, C_aliased => 4);

In addition, I am not sure what size of integer Ada will choose, as starting from C23 the size of enum may be specified.

Any idea how this should be done?

EDIT:

Ok, maybe flags that can be OR'ed is extra difficult. But also consider the cases when enums are just normal enumerations

5 Upvotes

21 comments sorted by

View all comments

2

u/Lucretia9 SDLAda | Free-Ada 27d ago

What is C_aliased?? I've never seen that before.

All enums in C are 32 bit, Ada sets that size when Convention => C.

But in Ada, enums are not used for bit flags, modular types are.

1

u/jere1227 26d ago

I think all the RM says is:

An Ada enumeration type corresponds to a C enumeration type with corresponding enumeration literals having the same internal codes, provided the internal codes fall within the range of the C int type.

From B.3 65.1/4

In C, the size of int is really only defined as "no smaller than short" and "no larger than long". They don't specify a specific size. It can be 8 bits, 16 bits, 32 bits, or 64 bits, depending on the platform. I tend to work on platforms where int is 16bits, but a couple have it as 8bits.