comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Interfacing C type, unconstrained array with record
Date: Sun, 17 Oct 2010 08:30:39 -0400
Date: 2010-10-17T08:30:39-04:00	[thread overview]
Message-ID: <wcczkud57dc.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 18tt8tf57xd5m.10yh2wugb4eeo.dlg@40tude.net

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On Sat, 16 Oct 2010 12:19:18 -0700 (PDT), Ron Wills wrote:
>
>> The "pragma Convention" did the trick! I must say, Ada is the one
>> language I've encountered that seems to have the largest learning
>> curve because of the most cryptic references I ever seen ;)
>
> What you are doing is interfacing to another language. That is not Ada, but
> system programming. If you think pragma Convention cryptic, well, what
> about reading the compiler manual for call stack frame format details and
> implementing that using Assembler code insertions? (:-))

Good point.  And what about reading the C standard to find all
the good features for interfacing to Ada (or to any other
language)?  That's easy -- there are no such features!

> P.S. to dear ARG members. Why discrimnants cannot be mapped onto components
> of a C structure. I mean making OK this:
>
>    type Vector is array (size_t range <>) of Whatever;
> � �pragma Convention (C, Vector);
> � �type Constrained (Size : size_t) is record
>    � � �List� : Vector (1..Size);
> � �end record;
> � �pragma Convention (C, Constrained);

Well, in fact I think that will work in GNAT, although it
will warn about the fact that C doesn't have discriminants.

But this is tricky.  The C code depends on the fact that the
List component comes last.  There is no such requirement
in Ada that discriminant-dependent fields come last
(either in the declaration, or in the memory layout).

What if there were two discriminants, and two discriminant-dependent
arrays?  That doesn't match anything on the C side.

Many compilers use extra dope when there are discriminants
or unconstrained arrays.  Ada 83 didn't have very good
support for interfacing -- most of this stuff was invented
for Ada 95.  But by that time, compilers already existed,
and the compiler writers didn't want to change the "dope".
For example, I know of one compiler that would store
the size (in bytes) of the above record (in addition to
the Size field, which is the length of that array).

Whether that's a good idea is arguable, but it has the
advantage that block "=" and ":=" don't have to
calculate the size in bytes.  And it's just a special
case of storing the offset of each discriminant-dependent
component (to avoid recalculating those) -- the size is
just the offset of the end.

(By the way, I prefer to use "size" to refer to memory
sizes, measured in bits, bytes, words, or whatever,
and use "length" to refer to array lengths, measured
in number of components.  The latter is a higher-level
concept.)

Anyway, I guess the real answer to your question is,
C doesn't have discriminants, so nobody thought
it would be a good idea to require interfacing
discriminants to something in C.  It could make
sense, but only in very limited cases.  Compilers
are allowed to support it, but there's no portable
(language defined) support.

- Bob



  reply	other threads:[~2010-10-17 12:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-16 17:35 Interfacing C type, unconstrained array with record Ron Wills
2010-10-16 18:36 ` Dmitry A. Kazakov
2010-10-16 19:19   ` Ron Wills
2010-10-16 22:15     ` Jeffrey Carter
2010-10-17 10:20       ` Simon Wright
2010-10-17 12:38         ` Robert A Duff
2010-10-17  8:35     ` Dmitry A. Kazakov
2010-10-17 12:30       ` Robert A Duff [this message]
2010-10-16 21:41   ` Robert A Duff
2010-10-16 23:34     ` tmoran
2010-10-17  6:59     ` J-P. Rosen
2010-10-17 12:34       ` Robert A Duff
2010-10-17  7:45     ` Dmitry A. Kazakov
replies disabled

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