* Interfacing enums with C
@ 2014-08-17 19:02 Victor Porton
2014-08-17 19:39 ` Simon Wright
0 siblings, 1 reply; 7+ messages in thread
From: Victor Porton @ 2014-08-17 19:02 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
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
0 siblings, 1 reply; 7+ messages in thread
From: Simon Wright @ 2014-08-17 19:39 UTC (permalink / raw)
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);
And your first interfacing convention is much better. IMO.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
2014-08-17 19:39 ` Simon Wright
@ 2014-08-17 19:46 ` Victor Porton
2014-08-17 19:48 ` Victor Porton
2014-08-18 9:17 ` Simon Wright
0 siblings, 2 replies; 7+ messages in thread
From: Victor Porton @ 2014-08-17 19:46 UTC (permalink / raw)
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;
> And your first interfacing convention is much better. IMO.
--
Victor Porton - http://portonvictor.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
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:17 ` Simon Wright
1 sibling, 1 reply; 7+ messages in thread
From: Victor Porton @ 2014-08-17 19:48 UTC (permalink / raw)
Victor Porton wrote:
> 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;
>
>> 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!
--
Victor Porton - http://portonvictor.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
2014-08-17 19:48 ` Victor Porton
@ 2014-08-17 22:48 ` Peter Chapin
2014-08-18 9:19 ` Simon Wright
0 siblings, 1 reply; 7+ messages in thread
From: Peter Chapin @ 2014-08-17 22:48 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
2014-08-17 19:46 ` Victor Porton
2014-08-17 19:48 ` Victor Porton
@ 2014-08-18 9:17 ` Simon Wright
1 sibling, 0 replies; 7+ messages in thread
From: Simon Wright @ 2014-08-18 9:17 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Interfacing enums with C
2014-08-17 22:48 ` Peter Chapin
@ 2014-08-18 9:19 ` Simon Wright
0 siblings, 0 replies; 7+ messages in thread
From: Simon Wright @ 2014-08-18 9:19 UTC (permalink / raw)
Peter Chapin <PChapin@vtc.vsc.edu> writes:
> 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.
Not sure it's precisely an _extension_ so much as a taking-up of an
implementation permission.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2014-08-18 9:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox