- If not, then how should enum values be declared in the FFI?
What you need to do is run a little autoconf-like program which constructs a program containing a suitable example, runs it through a C compiler and tells you what's going on.
hsc comes very close but I'm not certain if it does exactly what you need. If not, compiling and running this program should tell you:
Hmm, careful. The C compiler is free to be clever, and use a char if there are <=256 elements in the enum, and short or int otherwise. You want to know the size of your particular enum, not any random enum.
Yes. The C99 spec says (sec. 6.7.2.1): [#4] Each enumerated type shall be compatible with an integer type. The choice of type is implementation-defined but shall be capable of representing the values of all the members of the enumeration. [...] If a C compiler can choose any integer type, then it seems to me that mixing code from two C compilers on the same platform might not work. Hmmm. Anway, I think Alastair meant to say hsc2hs (not hsc), but you do actually get the right answer doing it this way. In hsc2hs if you say (#type enum foo) you'll get a Haskell type which corresponds to the actual type used by the C compiler (provided hsc2hs is using the same C compiler as was used to compile the library, presumably). Cheers, Simon
On 17-Oct-2002, Simon Marlow <simonmar@microsoft.com> wrote:
Yes. The C99 spec says (sec. 6.7.2.1):
[#4] Each enumerated type shall be compatible with an integer type. The choice of type is implementation-defined but shall be capable of representing the values of all the members of the enumeration. [...]
If a C compiler can choose any integer type, then it seems to me that mixing code from two C compilers on the same platform might not work. Hmmm.
The choice of representation of enums is part of the C ABI. Two different compilers on the same platform can use different C ABIs, so yes, mixing code from such compilers might not work. However, enums are the least of your problems there. Compilers also need to cooperate about object file format, name mangling, the usage of registers and stack, the sizes and representations of the basic types such as `int', struct padding, alignment, and so on. If two C compilers both support the same C ABI, then it is possible to mix code between them. -- Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit The University of Melbourne | of excellence is a lethal habit" WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
participants (2)
-
Fergus Henderson -
Simon Marlow