From: bywater!arnor!watson!prener.watson.ibm.com!ncohen@uunet.uu.net (Norman H . Cohen)
Subject: Re: Untyped "constant"?
Date: 24 Sep 91 21:22:10 GMT [thread overview]
Message-ID: <1991Sep24.212210.58452@watson.ibm.com> (raw)
In article <1991Sep23.164446.13412@aplcen.apl.jhu.edu>,
gralia@ddsdx2.jhuapl.edu (Mars J. Gralia) writes:
|> a) Suppose I have a compiler/RTE in which integer'last is 32_767.
|> b) And suppose the system has a long_integer type in which
|> long_integer'last = 2_147_483_647.
|> c) And suppose I have the program:
|> procedure Named1 is
|> X : constant := integer'last+5;
|> begin
|> for j in 1..X loop
|> null;
|> end loop;
|> end Named1;
|>
|> d) Finally, suppose the "loop" variables are really used. E.g., by
|> disabling optimization when I invoke the compiler.
|>
|>
|> Then: is this a legal program or not?
As Dave Collard points out, Integer'Last+5 is of type Integer, and the
initial-value expression for a named number must be of type
universal_integer, so this is not legal. However, we can get around this
technicality by rewriting the initial-value expression as
Integer'Pos(Integer'Last)+5
since the 'Pos attribute yields a universal_integer result.
However, RM 3.6.1(2), when deciphered, states that a discrete range in
which each bound is either a named number, an integer literal, or an
attribute with a universal_integer value has each bound implicitly
converted to type Integer. Thus 1 .. X is equivalent to
Integer(1) .. Integer(X). Since X has a value greater than Integer'Last,
the conversion in the upper bound raises Constraint_Error.
|> Some compilers, admittedly archaic or for very small computers,
|> will raise a Constraint_Error, thereby forcing me to say:
|> X : constant long_integer := integer'last+5;
|>
|> I would prefer the program given above, because it would appear to be
|> more portable.
Well, it's portable in the sense that it is guaranteed to raise
Constraint_Error (except when the loop is optimized away, in which case
the implicit conversion that would have raised Constraint_Error can be
eliminated pursuant to RM 11.6(7)).
A more useful form of portability would come from defining your own
integer type with the required range:
procedure Named1 is
type Loop_Index_Type is range 1 .. Integer'Last+5;
begin
for J in Loop_Index_Type loop
null;
end loop;
end Named1;
Implementations for which Integer'Last = System.Max_Int will reject this
at compile time. Implementations for which System.Max_Int >=
Integer'Last+5 are guaranteed to execute without an exception.
next reply other threads:[~1991-09-24 21:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-09-24 21:22 Norman H . Cohen [this message]
-- strict thread matches above, loose matches on Subject: below --
1991-09-27 14:13 Untyped "constant"? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt
1991-09-24 15:08 John Goodenough
1991-09-23 17:32 agate!spool.mu.edu!caen!uakari.primate.wisc.edu!aplcen!ddsdx2.jhuapl.edu!
1991-09-23 16:44 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplc
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox