comp.lang.ada
 help / color / mirror / Atom feed
* C interface using a record with discriminant and representation clauses
@ 2009-06-30 23:01 Hibou57 (Yannick Duchêne)
  2009-06-30 23:38 ` Hibou57 (Yannick Duchêne)
  0 siblings, 1 reply; 3+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-06-30 23:01 UTC (permalink / raw)


Hello,

I wonder about what to think about it, while I could not imagine a
better solution :

Say a C structure is to be interfaced with Ada. The structure has a
"count" member and a variable length array, commonly defined in C as
something like elements[1].

I.e. a C structure like

typedef struct {
   int count;
   my_element_type elements[1];
} my_struct;

I'm tempted to use something like

type Element_Type is <Something>;
for Element_Type'Size use <Something>;

type Array_Type is array (Index_Type range <>) of aliased
Element_Type;
pragma Convention (C, Entity => Array_Type);

type Record_Type (Length : Index_Type) is record
   Elements : Array_Type (1..Length);
end record;

for Record_Type use record
   Length at 0 range 0 .. Something; -- Tell the Ada compiler where
the C
                                                     -- compiler has
placed the “ discriminant ”
end record;

pragma Convention (C, Entity => Record_Type);

Though the trouble may be that it is not possible to specify the bits
address of Elements, beceause it is not possible to have something
like “ Elements at 0 range <Something> .. <Something> * Length - 1; ”

What to think about it ?

Is it portable through most compilers ?

** Is it trustable ? **




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C interface using a record with discriminant and representation clauses
  2009-06-30 23:01 C interface using a record with discriminant and representation clauses Hibou57 (Yannick Duchêne)
@ 2009-06-30 23:38 ` Hibou57 (Yannick Duchêne)
  2009-07-05  3:17   ` Hibou57 (Yannick Duchêne)
  0 siblings, 1 reply; 3+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-06-30 23:38 UTC (permalink / raw)


A side note : I know it is better, when interfacing with C, to get
ride of Ada constraints which does not exist in C, thus as an exemple,
to declare array types with full range bounds (to not to be
instanciated). But there, there is something which could be used as a
discriminant, and I would like to try to benefit from it.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C interface using a record with discriminant and representation clauses
  2009-06-30 23:38 ` Hibou57 (Yannick Duchêne)
@ 2009-07-05  3:17   ` Hibou57 (Yannick Duchêne)
  0 siblings, 0 replies; 3+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-07-05  3:17 UTC (permalink / raw)


An anwser here :

http://www.adapower.com/index.php?Command=Class&ClassID=Advanced&CID=228
says (at the end):
> You need rep specs to handle tricky and unusual cases such as:
>
> * Mapping Ada record types with a discriminant and a variant part onto
>   C unions.  This can be made to work as long as the C type has a member
>   that can serve as the discriminant; most sensibly designed C code does.
>
> [...]
>
> If you use rep specs, you should not need pragma Convention, and vice versa.

So this suggest that representation specs and pragma convention should
not be used simultaneously, as I've done due to some doubts.

Using representation specs is Ok, but the pragma Convention is to be
removed (will be done in a minute).



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-07-05  3:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-30 23:01 C interface using a record with discriminant and representation clauses Hibou57 (Yannick Duchêne)
2009-06-30 23:38 ` Hibou57 (Yannick Duchêne)
2009-07-05  3:17   ` Hibou57 (Yannick Duchêne)

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