comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Redefining Integer
Date: 1997/11/16
Date: 1997-11-16T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001611972202050001@news.ni.net> (raw)
In-Reply-To: 640di0$l432@castor.cca.rockwell.com


In article <640di0$l432@castor.cca.rockwell.com>, No@Junk.Mail wrote:

>I am porting some old code that assumes Integer is 16-bits and does lots
>of unchecked conversions based on this fact.

Well, there is your first problem.  Every single Ada book I've ever read
warns precisely against assuming a specific size for a predefined type, and
strongly encourages defining a user-defined type when low-level programming
is required.  Why didn't the developers heed that advice?

>When "Integer" is redefined in the same declarative region that it is
>used it works fine:
>
>  type Integer is range -2**15 .. 2**15 - 1;
>  for Integer'Size use 16;

You should never do this.  If you want a 16 bit signed integer, then use
the type Integer_16 declared in the predefined Ada package Interfaces.

>  subtype bit_range is integer range 0 .. integer'size - 1;
>  type packed_boolean_array is array (Bit_Range) of Boolean;
>  for packed_boolean_array'Size use 16;
>  pragma Pack (packed_boolean_array);

Don't bother tying the Bit_Range type to the size, just do this

subtype Bit_Range is Integer range 0 .. 15;

You already hard-coded the fact that the type has to be 16 bits in the size
clause, so why not hard-code that info in the index subtype too?  If you
want, you could do this

Packed_Boolean_Array_Size : constant := 16;

subtype Bit_Range is Natural range 0 .. Package_Boolean_Array_Size - 1;

type Packed_Boolean_Array is array (Bit_Range) of Boolean;

pragma Pack (Packed_Boolean_Array);
for Package_Boolean_Array'Size use Packed_Boolean_Array_Size;

I don't know why the compiler didn't flag the fact that the type named
Integer was defined in two directly visible scopes.  But because one should
never, ever use the names in package Standard for user-defined type names,
you don't typically have the problem.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




      parent reply	other threads:[~1997-11-16  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-11-08  0:00 Redefining Integer Wayne Magor
1997-11-08  0:00 ` Wayne Magor
1997-11-16  0:00 ` Matthew Heaney [this message]
replies disabled

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