comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Interfacing enums with C
Date: Mon, 18 Aug 2014 10:17:06 +0100
Date: 2014-08-18T10:17:06+01:00	[thread overview]
Message-ID: <lyha1ajgx9.fsf@pushface.org> (raw)
In-Reply-To: lsr0qm$559$1@speranza.aioe.org

Victor Porton <porton@narod.ru> writes:

> Simon Wright wrote:
>> Victor Porton <porton@narod.ru> 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 <stdio.h>

   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


      parent reply	other threads:[~2014-08-18  9:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-17 19:02 Interfacing enums with C Victor Porton
2014-08-17 19:39 ` Simon Wright
2014-08-17 19:46   ` Victor Porton
2014-08-17 19:48     ` Victor Porton
2014-08-17 22:48       ` Peter Chapin
2014-08-18  9:19         ` Simon Wright
2014-08-18  9:17     ` Simon Wright [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox