comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
Subject: Re: Default rep specs for record types - documented??
Date: Fri, 04 Nov 2005 09:39:42 -0500
Date: 2005-11-04T09:39:42-05:00	[thread overview]
Message-ID: <uy844sci9.fsf@acm.org> (raw)
In-Reply-To: uacgktuhd.fsf@acm.org

Stephen Leake <stephen_leake@acm.org> writes:

> Someone mentioned a 'pragma Preserve_Layout', which presumably means
> "assume a representation clause that puts things in declaration order,
> with the base size for each component, and no gaps". That would be
> useful, in situations where the type declarations do in fact match the
> hardware precisely. Hmm. That is true for most of the rep clauses I've
> written recently. I'll have to suggest that to AdaCore as a possible
> enhancement; it would have prevented my bug. 

I've submitted the following to AdaCore:

I had a record:

record_byte_length : constant := 4;

type Record_Type is record
    field_1 : Integer_16;
    field_2 : Integer_16;
end record;

for record_type use record
    Field_1 at 0 range 0 .. 15;
    field_2 at 2 range 0 .. 15;
end record;
for record_type'size use record_byte_length * 8;

Then I needed to add a field, which should go before the others:

record_byte_length : constant := 6;

type Record_Type is record
    field_0 : Integer_16;
    field_1 : Integer_16;
    field_2 : Integer_16;
end record;

for record_type use record
    Field_1 at 0 range 0 .. 15;
    field_2 at 2 range 0 .. 15;
end record;
for record_type'size use record_byte_length * 8;


I forgot to add Field_0 to the rep clause, and the compiler put it at
the end (where it didn't belong). Most importantly, the compiler
didn't warn me.

One solution is to have the compiler write the representation clause
for me; in this case, if we make reasonable assumptions about
'record_type', there is only one reasonable rep clause. So I'd like to
define:

------------
pragma Deduce_Representation (type_local_name);

Specifies that the compiler will assume a record representation clause for
'type_local_name'.

Every component of 'type_local_name' shall be of a type that has an
explicit size attribute.

'type_local_name' shall have an explicit size attribute, which is
equal to the sum of the component sizes.

'type_local_name' shall have an explicit Bit_Order attribute.

The representation clause shall layout the components in declaration
order, using the sizes specified in the corresponding size attribute.
------------


Most of the record representation clauses I write meet these criteria,
and it would prevent maintenance bugs like the above. It would also
save time, and avoid other bugs related to writing rep clauses.

It would be nice if the assumed rep clause could be endianness-independent,
but I suspect that's not possible.

It's important that the typical user be able to predict what rep
clause the compiler will assume, which is why I've required explicit
size attributes on the component types. If we also need to exclude
tagged types or disciminated records, that's ok by me.


-- 
-- Stephe



  parent reply	other threads:[~2005-11-04 14:39 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-04  2:21 Default rep specs for record types - documented?? Anonymous Coward
2005-11-04  2:36 ` Steve
2005-11-04  4:11   ` Anonymous Coward
2005-11-04  5:30     ` Jeffrey R. Carter
2005-11-05  3:13       ` Steve
2005-11-05  4:45         ` Jeffrey R. Carter
2005-11-06 14:05           ` Steve
2005-11-06 16:08             ` Anonymous Coward
2005-11-07  7:25             ` Jeffrey R. Carter
2005-11-08 13:36               ` Steve
2005-11-14  1:12             ` Robert A Duff
2005-11-14  3:03               ` Anonymous Coward
2005-11-14 18:08                 ` Jeffrey R. Carter
2005-11-14 18:49                 ` Robert A Duff
2005-11-15  1:16                   ` ADA compilers can reject types arbitrarily? Anonymous Coward
2005-11-15  2:10                     ` tmoran
2005-11-15  3:12                     ` Robert A Duff
2005-11-15  6:44                     ` Simon Wright
2005-11-16  0:16                       ` Adaists Deny Acronym. (was: ADA compilers can reject types arbitrarily?) Björn Persson
2005-11-16  5:38                         ` Adaists Deny Acronym Simon Wright
2005-11-16  6:16                         ` Adaists Deny Acronym. (was: ADA compilers can reject types arbitrarily?) Samuel Tardieu
2005-11-15 12:43                     ` ADA compilers can reject types arbitrarily? Jeff Creem
2005-11-14 21:14                 ` Default rep specs for record types - documented?? Simon Wright
2005-11-04 13:26     ` Stephen Leake
2005-11-04 14:33       ` Marc A. Criley
2005-11-04 18:35         ` Georg Bauhaus
2005-11-04 20:07           ` Britt Snodgrass
2005-11-04 14:39       ` Stephen Leake [this message]
2005-11-04 15:27         ` Britt Snodgrass
2005-11-04 15:55           ` Lauri Ora
2005-11-04 21:42             ` Larry Kilgallen
2005-11-05  2:26             ` Anonymous Coward
2005-11-05  2:42               ` Frank J. Lhota
2005-11-05  3:27               ` Ed Falis
2005-11-05  3:55                 ` Anonymous Coward
2005-11-05  4:07                 ` Lauri Ora
2005-11-05 13:46                   ` Ed Falis
2005-11-05 10:14                 ` Stephen Leake
2005-11-04 16:52         ` Frank J. Lhota
2005-11-04 16:57           ` Frank J. Lhota
2005-11-04 23:27           ` tmoran
2005-11-05 10:25           ` Stephen Leake
2005-11-14  1:09             ` Robert A Duff
2005-11-05  3:33       ` Anonymous Coward
2005-11-05 10:34         ` Stephen Leake
2005-11-05 16:35           ` ADA/C interfaces: type representations uncontrollable in C Anonymous Coward
2005-11-05 16:49             ` Ed Falis
2005-11-05 18:24             ` tmoran
2005-11-09  2:12           ` 'Size can mean value size OR object size, depending Anonymous Coward
2005-11-09  3:27             ` Jeffrey R. Carter
2005-11-09  4:04               ` Anonymous Coward
2005-11-05 14:27         ` Default rep specs for record types - documented?? Michael Bode
2005-11-05 16:17           ` pragma convention Anonymous Coward
2005-11-06  1:07             ` Jeffrey R. Carter
2005-11-06 22:22               ` Anonymous Coward
2005-11-07  7:34                 ` Jeffrey R. Carter
2005-11-05 14:39         ` Default rep specs for record types - documented?? Martin Krischik
2005-11-04  9:40   ` Martin Dowie
2005-11-04 14:36   ` Marc A. Criley
2005-11-04 17:45 ` Martin Krischik
replies disabled

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