comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst@cts.com>
Subject: Re: Enumeration representation
Date: 1999/09/12
Date: 1999-09-12T00:00:00+00:00	[thread overview]
Message-ID: <yechfkz8k1t.fsf@king.cts.com> (raw)
In-Reply-To: 7rhkte$na7$1@nnrp1.deja.com

Robert Dewar <robert_dewar@my-deja.com> writes:
> > Even an attribute that gives an integer type guaranteed to be
> > Unchecked_Conversion-compatible with a given enumeration type would
> > have helped, in a ugly sort of way. (I'm assuming such a type has to
> > be of the same size and signedness as the (internal representation
> > of the) enumeration type.
> 
> It's easy enough to define the type you need ...
> 
> > Unless the semantics of Unchecked_Conversion between discrete types
> > of different sizes are well-defined -- I've never been quite clear
> > on that point.)
> 
> No, the sizes should be the same, but this is easy enough to
> arrange. In GNAT everything is fine if sizes are different,
> you just get the equivalent of a type conversion without
> any checks, but not all compilers (and certainly not the RM)
> guarantee this.

Hmm.  Ok, suppose you have an enumeration type Foo.  It may or may not
have an enumeration representation clause; if it does, it may or may
not assign negative values to some of the literals.  How do you
declare an integer type that's guaranteed to be the same size and
signedness?

You could do something like this:

    type Equivalent_Type is range -2**Foo'Size .. 2**Foo'Size - 1;
    for Equivalent_Type'Size use Foo'Size;

but I'm not sure that's enough.  (I'm assuming 2's-complement; there
are ugly but portable ways to avoid that assumption.)  For example, it
doesn't distinguish between the following two cases:

    type Unsigned_Byte is (B0, B1, ..., B255);
    for Unsigned_Byte use (0, 1, ..., 255); -- a confirming rep clause
    for Unsigned_Byte'Size use 8;

and

    type Signed_Byte is (BM128, BM127, ..., BM1, B0, B1, ..., B127);
    for Signed_Byte use (-128, -127, ..., -1, 0, 1, ..., 127);
    for Signed_Byte'Size use 8;

If you could get at the internal representation, you could use

    type Equivalent_Type is range Foo'Enum_Rep(Foo'First) ..
                                  Foo'Enum_Rep(Foo'Last);
    for Equivalent_Type'Size use Foo'Size;

but of course the language doesn't provide that.

In real life, the programmer can usually make use of detailed
knowledge about any enumeration representation clause, but it would be
nice not to have to.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
"Oh my gosh!  You are SO ahead of your time!" -- anon.




  reply	other threads:[~1999-09-12  0:00 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-10  0:00 Enumeration representation Alex
1999-09-10  0:00 ` Ted Dennison
1999-09-10  0:00   ` Robert Dewar
1999-09-13  0:00     ` Ted Dennison
1999-09-10  0:00 ` David C. Hoos, Sr.
1999-09-10  0:00   ` Robert Dewar
1999-09-10  0:00 ` Matthew Heaney
1999-09-10  0:00   ` Robert Dewar
1999-09-10  0:00 ` Keith Thompson
1999-09-11  0:00   ` Robert Dewar
1999-09-11  0:00     ` Keith Thompson
1999-09-12  0:00       ` Bryce Bardin
1999-09-13  0:00         ` Robert Dewar
1999-09-13  0:00           ` Keith Thompson
1999-09-13  0:00       ` Robert Dewar
1999-09-12  0:00         ` Keith Thompson [this message]
1999-09-13  0:00           ` Robert Dewar
1999-09-13  0:00             ` Keith Thompson
1999-09-14  0:00               ` Robert Dewar
1999-09-13  0:00             ` Ted Dennison
1999-09-13  0:00         ` Robert A Duff
1999-09-13  0:00           ` Robert Dewar
1999-09-13  0:00 ` Alex
  -- strict thread matches above, loose matches on Subject: below --
2004-01-01 20:44 Luke A. Guest
2004-01-01 21:45 ` Stephen Leake
2004-01-01 22:01   ` Luke A. Guest
2004-01-02  1:17     ` tmoran
2004-01-02  1:29     ` Stephen Leake
2004-01-02  3:10       ` Luke A. Guest
2004-01-02  2:46 ` Robert A Duff
2004-01-02  3:12   ` Luke A. Guest
2004-01-02 13:58   ` Marin David Condic
2004-01-02 21:39     ` Pat Rogers
2004-01-03 13:42       ` Marin David Condic
2004-01-03  1:53     ` Robert A Duff
2004-01-02 20:52   ` Randy Brukardt
2004-01-02 21:05     ` Luke A. Guest
replies disabled

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