Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interfacing enums with C

123 views
Skip to first unread message

Victor Porton

unread,
Aug 17, 2014, 3:02:45 PM8/17/14
to
Let in C code are defined:

typedef enum { A=1, B=2 } option_type;

void f(option_type option);

Let we also have

type Option_Type is (A, B);
for Option_Type'Size use Interfaces.C.unsigned'Size;
for Option_Type use (A=>1, B=>2);

X: Option_Type := A;

Which of the following code is correct (accordingly RM)?

-- First code
declare
procedure F (Option: Option_Type)
with Import, Convention=>C, External_Name=>"f";
begin
F(X);
end;

or

-- Second code
declare
procedure F (Option: Interfaces.C.unsigned)
with Import, Convention=>C, External_Name=>"f";
function Conv is new Ada.Unchecked_Conversion(Option_Type, Interfaces.C.unsigned);
begin
F(Conv(X));
end;

I think both first and second Ada fragments are correct but am not sure.

--
Victor Porton - http://portonvictor.org

Simon Wright

unread,
Aug 17, 2014, 3:39:01 PM8/17/14
to
Victor Porton <por...@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);

And your first interfacing convention is much better. IMO.

Victor Porton

unread,
Aug 17, 2014, 3:46:34 PM8/17/14
to
Simon Wright wrote:
> Victor Porton <por...@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;

> And your first interfacing convention is much better. IMO.

Victor Porton

unread,
Aug 17, 2014, 3:48:08 PM8/17/14
to
BTW, I could propose enumeration type to be eligible for Convention C in the
next version of Ada RM!

Peter Chapin

unread,
Aug 17, 2014, 6:48:17 PM8/17/14
to
On 2014-08-17 15:48, Victor Porton wrote:

>> 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;
>>
>>> And your first interfacing convention is much better. IMO.
>
> BTW, I could propose enumeration type to be eligible for Convention C in the
> next version of Ada RM!

FWIW, GNAT does allow 'Convention => C' for enumeration types. So
Simon's suggestion should work with GNAT if you are willing to make use
of a GNAT extension.

Peter

Simon Wright

unread,
Aug 18, 2014, 5:17:06 AM8/18/14
to
Victor Porton <por...@narod.ru> writes:

> Simon Wright wrote:
>> Victor Porton <por...@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

Simon Wright

unread,
Aug 18, 2014, 5:19:40 AM8/18/14
to
Not sure it's precisely an _extension_ so much as a taking-up of an
implementation permission.
0 new messages