comp.lang.ada
 help / color / mirror / Atom feed
* Re: Redefining Integer
  1997-11-08  0:00 Redefining Integer Wayne Magor
@ 1997-11-08  0:00 ` Wayne Magor
  1997-11-16  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Wayne Magor @ 1997-11-08  0:00 UTC (permalink / raw)




Never mind.  I should have known you can't hide anything with use clauses.




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

* Redefining Integer
@ 1997-11-08  0:00 Wayne Magor
  1997-11-08  0:00 ` Wayne Magor
  1997-11-16  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 3+ messages in thread
From: Wayne Magor @ 1997-11-08  0:00 UTC (permalink / raw)




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

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;

  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);

however, if I put this declaration of Integer into a separate package and
put a "with" and "use" on every package that uses Integer, it doesn't work.
I get this type of message:

    7       for packed_boolean_array'Size use 16;
...........1
%ADAC-E-SIZE_NOT_EXACT, (1) The number of bits to represent index constraint
        {type for packed_boolean_array} at line 6 must be exactly 32 [LRM
        13.2(6+), 13.4(5+)]

This tells me that the compiler is using 32 for the value of Integer'Size.
Why is that?  Are the declarations in Standard visible before the use
clause declarations?




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

* Re: Redefining Integer
  1997-11-08  0:00 Redefining Integer Wayne Magor
  1997-11-08  0:00 ` Wayne Magor
@ 1997-11-16  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1997-11-16  0:00 UTC (permalink / raw)



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




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

end of thread, other threads:[~1997-11-16  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-11-08  0:00 Redefining Integer Wayne Magor
1997-11-08  0:00 ` Wayne Magor
1997-11-16  0:00 ` Matthew Heaney

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