comp.lang.ada
 help / color / mirror / Atom feed
* Rules for Representation of Subtypes
@ 1996-09-22  0:00 Matthew Heaney
  1996-09-23  0:00 ` David C. Hoos, Sr.
  1996-09-23  0:00 ` Robert A Duff
  0 siblings, 2 replies; 73+ messages in thread
From: Matthew Heaney @ 1996-09-22  0:00 UTC (permalink / raw)



I've always been curious about the rules for representation of subtypes. 
For example, if I make a declaration like this:

   A_Small_Integer : Integer range 0 .. 255;  -- assume type Integer is 4 bytes

then is an implementation allowed to use fewer than 4 bytes to represent
A_Small_Integer?

Suppose I'm on a Unix system and I do this

   read (fd, A_Small_Integer'Address, 4);

If the representation of A_Small_Integer is only 1 byte, then obviously I'm
in trouble.  Am I required to specify the size of objects?

   A_Small_Integer : Integer range 0 .. 255;
   for A_Small_Integer'Size use 32;                     -- required?

matt
mheaney@ni.net

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




^ permalink raw reply	[flat|nested] 73+ messages in thread
* Re: Valid Attribute and Unchecked Conversion
@ 1996-10-03  0:00 Franco Mazzanti
  0 siblings, 0 replies; 73+ messages in thread
From: Franco Mazzanti @ 1996-10-03  0:00 UTC (permalink / raw)



Robert I. Eachus wrote:

>    It is erroneous.  That is the point of this discussion change to:
> 
>      declare
>          type T is range 1 .. 10;
>          for T'SIZE use Integer'SIZE;
>          function To_T is new Unchecked_Conversion (Integer, T'Base);
>          O : constant T := To_T (0);
>       begin
>          if O'Valid then
>             ...
>       end;
> 
>     Now To_T returns a bit pattern that is legal for the type (T'Base),
> and the constraint check occurs on the assignment.  You never get to
> the call to O'Valid.
> 

However, we must be very careful not to generalize this approach.
E.g.  in the following very similar case:

      declare
          type T1 is new Integer range 1 .. 10;
          type T2 is new Integer range 1 .. 10;
          for T2'SIZE use T1'SIZE;
          function To_T is new Unchecked_Conversion (T1, T2'Base);
          S1:T1;  -- maybe initialized with zero
          S2:T2;
       begin
          S2:= 1;
          ...
          S2:= To_T2(S1);
          ...
          if O'Valid then
             ...
       end;

if S1 is not initialized, and has an invalid value, the call of To_T2 is
still directly erroneous (because the unchecked conversion still tries to
return an invalid scalar value!). 

It seems, in fact, that it is a generic principle in the Reference
Manual that invalid objects can be created ONLY by a missing initialization.
Once initialized, an object can no-more become "invalid". It can become,
at best, abnormal, and just storing its value in an object is erroneous.

My impression is that the safest generic approach still requires the use of 
a "Checked_Scalar_Conversion" as shown inside the previous thread
(Re: Rules for Representation of Subtypes).

Franco Mazzanti




^ permalink raw reply	[flat|nested] 73+ messages in thread
* Re: Valid Attribute and Unchecked Conversion
@ 1996-10-04  0:00 Franco Mazzanti
  0 siblings, 0 replies; 73+ messages in thread
From: Franco Mazzanti @ 1996-10-04  0:00 UTC (permalink / raw)



(Keith Thompson) wrote:
> 
> >So, suppose I have a sparse enumeration type:
> >
> >    type Enum is (Ten, Twenty, Thirty);
> >    for Enum use (10, 20, 30);
> >    for Enum'Size use 8;
> >
> >and a corresponding integer type:
> >
> >    type Int_Type is range 0 .. 31;
> >    for Int_Type'Size use 8;
> >
> >Given an arbitrary value of type Int_Type, is there any non-erroneous
> >way to get the corresponding Enum value if there is one, or raise
> >an exception if there isn't? 


Matthew Heaney wrote:
> 
> The answer would appear to be, No, there is no way to automatically get Ada
> to raise an exception. 

Instead I think that there is a non-erroneous way to get corresponding
Enum value, or raise an exception:
(sorry to repost here a fragment of code previously posted in a different
thread)
Just use the following "checked" conversion:

generic
   type Source(<>) is limited private;
   type Target is (<>);
function Checked_Scalar_Conversion (S:Source) return Target;

with Unchecked_Conversion;
function Checked_Scalar_Conversion (S : Source) return Target is
   type My_Rec is record
      Scalar : Target;
   end record;
   Tmp : My_Rec;
   My_Scalar: Target renames Tmp.Scalar;
   -- small trick: even if Tmp as a whole record becomes abnormal
   -- its scalar component can still be safely checked for validity
   -- without "using" the abnormal record object.

   function Unchecked_Cvt is new Unchecked_Conversion (Source, My_Rec);
begin
   if My_Rec'Size /= Source'Size or
        My_rec'Alignment /= Source'Alignment then
      raise Program_Error;
   end if;
   Tmp := Unchecked_Cvt (S);
   if My_Scalar'Valid then
      return My_Scalar;
   else
      raise Program_Error;
   end if;
end Checked_Scalar_Conversion;

Now you can instantiate the Checked conversion with your types:


function Cvt is new Checked_Scalar_Conversion (Int_Type, Enum);

and use it

   N: Enum;
   N := Cvt (10);   -- OK
   N := Cvt (20);   -- OK
   N := Cvt (30);   -- OK
   N := Cvt (25);   -- raises Program_Error


Franco Mazzanti
(mazzanti@iei.pi.cnr.it)




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

end of thread, other threads:[~1996-10-14  0:00 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-22  0:00 Rules for Representation of Subtypes Matthew Heaney
1996-09-23  0:00 ` David C. Hoos, Sr.
1996-09-23  0:00   ` Samuel T. Harris
1996-09-26  0:00     ` David C. Hoos, Sr.
1996-09-23  0:00   ` Robert A Duff
1996-09-24  0:00   ` Robert Dewar
1996-09-24  0:00   ` Robert Dewar
1996-09-26  0:00     ` Keith Thompson
1996-09-26  0:00       ` Matthew Heaney
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Robert Dewar
1996-09-27  0:00         ` Robert Dewar
1996-09-27  0:00       ` Robert A Duff
1996-09-23  0:00 ` Robert A Duff
1996-09-24  0:00   ` Matthew Heaney
1996-09-26  0:00     ` Robert A Duff
1996-09-26  0:00       ` Larry Kilgallen
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Mark A Biggar
1996-09-30  0:00             ` Robert A Duff
1996-10-01  0:00               ` Larry Kilgallen
1996-10-01  0:00                 ` Samuel Tardieu
1996-10-01  0:00                   ` Ken Garlington
1996-10-01  0:00                   ` Valid Attribute and Unchecked Conversion Matthew Heaney
1996-10-02  0:00                     ` Robert A Duff
1996-10-04  0:00                       ` Keith Thompson
1996-10-04  0:00                         ` Robert A Duff
1996-10-04  0:00                           ` Robert Dewar
1996-10-11  0:00                             ` Norman H. Cohen
1996-10-12  0:00                               ` Robert Dewar
1996-10-06  0:00                           ` Keith Thompson
1996-10-07  0:00                             ` Robert Dewar
1996-10-09  0:00                               ` Keith Thompson
1996-10-07  0:00                           ` Ken Garlington
1996-10-08  0:00                           ` Alan Brain
1996-10-04  0:00                         ` Matthew Heaney
1996-10-07  0:00                           ` Robert Dewar
1996-10-09  0:00                             ` Keith Thompson
1996-10-07  0:00                         ` Robert Dewar
1996-10-10  0:00                           ` Ken Garlington
1996-10-11  0:00                             ` Robert Dewar
1996-10-14  0:00                               ` Keith Thompson
1996-10-14  0:00                               ` Ken Garlington
1996-10-07  0:00                         ` Kenneth Almquist
1996-10-01  0:00                 ` Rules for Representation of Subtypes Robert A Duff
1996-09-28  0:00           ` Larry Kilgallen
1996-09-29  0:00             ` Robert A Duff
1996-09-29  0:00               ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-09-30  0:00                 ` Robert Dewar
1996-09-30  0:00                   ` Matthew Heaney
1996-09-29  0:00               ` Larry Kilgallen
1996-09-29  0:00                 ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-10-01  0:00                   ` Ken Garlington
1996-10-02  0:00                     ` Robert A Duff
1996-10-02  0:00                       ` Ken Garlington
1996-10-06  0:00                   ` Robert Dewar
1996-10-03  0:00             ` Robert Dewar
1996-10-02  0:00         ` Valid Attribute and Unchecked Conversion George Haddad
1996-10-03  0:00           ` John Herro
1996-10-04  0:00             ` Karl Cooper {46901}
1996-10-07  0:00               ` Mark A Biggar
1996-10-08  0:00                 ` Robert Dewar
1996-10-05  0:00             ` Robert Dewar
1996-10-06  0:00               ` Keith Thompson
1996-10-14  0:00                 ` Robert A Duff
1996-10-02  0:00         ` Robert I. Eachus
1996-10-02  0:00           ` Matthew Heaney
1996-09-27  0:00       ` Rules for Representation of Subtypes Matthew Heaney
1996-09-27  0:00         ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1996-10-03  0:00 Valid Attribute and Unchecked Conversion Franco Mazzanti
1996-10-04  0:00 Franco Mazzanti

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