From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Interfacing enums with C Date: Mon, 18 Aug 2014 10:17:06 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx05.eternal-september.org; posting-host="b814ca014269fd1fa21bc4c05b94dd9e"; logging-data="7693"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Yi6v25t1+fY2kFi3znDhr0XXLH1t1nV4=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (darwin) Cancel-Lock: sha1:2PFLWYIWsxs/4Ws9U0/DBjFWBcg= sha1:hvHSrfBJXn/Ds4ulOpbRHMlHbB4= Xref: news.eternal-september.org comp.lang.ada:21803 Date: 2014-08-18T10:17:06+01:00 List-Id: Victor Porton writes: > Simon Wright wrote: >> Victor Porton writes: >> >>> type Option_Type is (A, B); >>> for Option_Type'Size use Interfaces.C.unsigned'Size; >>> for Option_Type use (A=>1, B=>2); >> >> What's wrong with >> >> type Option_Type is (A, B) >> with Convention => C; >> for Option_Type use (A => 1, B => 2); > > http://www.ada-auth.org/standards/12rm/html/RM-B-1.html 14/3 - 18 > does not say that enumeration types are eligible for convention C. > > So, in my opinion, RM does not require the following code to be compilable: > > type Option_Type is (A, B) > with Convention => C; I don't believe that the ARM requires an implementation to support *any* language conventions other than Ada and Intrinsic. B.1 (20) says that an implementation can permit a type to be compatible with a convention. The GNAT RM chapter on interfacing to C[1] says "Ada enumeration types map to C enumeration types directly if pragma Convention C is specified, which causes them to have int length. Without pragma Convention C, Ada enumeration types map to 8, 16, or 32 bits (i.e. C types signed char, short, int, respectively) depending on the number of values passed. This is the only case in which pragma Convention C affects the representation of an Ada type." This snippet #include typedef enum { A=1, B=2 } option_type; void f(option_type option) { printf("option: %d\n", (int)option); printf("sizeof option_type: %d\n", sizeof(option_type)); printf("sizeof option: %d\n", sizeof(option)); } void main() { f(A); } with GCC 4.9.0 on Mac OS X (x86_64) prints option: 1 sizeof option_type: 4 sizeof option: 4 so the *GCC* Ada compiler matches the *GCC* C compiler, rather as you might expect. The compiler writers aren't stupid. Apple's clang compiler produces 3 warnings for the snippet above, but produces the same results, so - for enums at least - appears to be compatible with GCC. But there is no guarantee whatsoever that GNAT would be compatible with any other C compiler. [1] https://gcc.gnu.org/onlinedocs/gnat_rm/Interfacing-to-C.html