comp.lang.ada
 help / color / mirror / Atom feed
From: Geert Bosch <geert@gonzo.sun3.iaf.nl>
Subject: Re: array conversion - how to do?
Date: 1997/06/28
Date: 1997-06-28T00:00:00+00:00	[thread overview]
Message-ID: <5p3qk2$8mk$1@gonzo.sun3.iaf.nl> (raw)
In-Reply-To: dewar.867457041@merv


Geert says
   ``I could imagine why it would not be possible to convert String
     to ASCII_String, since that conversion might need time-consuming
     checks. The conversion in the example is completely safe
     though, which is statically checkable for the compiler.  ''

Robert replies:
   ``You worry about writing the loop, but a good compiler should
     be able to generate efficient code for that loop. After all
     the checks will all get eliminated, and then there is no reason
     to think the loop will generate code any different on a typical
     machine from the conversion. ''

No, that is not the point. There is a fundamental problem why this
conversion is really expensive on all Ada implementations while it 
should be free.  

Assume the following declarations:

   subtype ASCII_Character is 
     Character range Character'Val (32) .. Character'Val (127);
   type String is array (Positive range <>) of Character;
   type ASCII_String is array (Positive range <>) of Character;

   procedure Error (Message : String);
   Storage_Msg : ASCII_String := "Out of storage";

The point is that to write the conversion in another way than using
a typecast, I need to copy the entire string:

   --  Code to do Error (String (Storage_Msg));
   declare
      Error_String  : String (Storage_Msg'Range);
   begin
      for I in Error_String'Range loop
	 Error_String (I) := Storage_Msg (I);
      end loop;
      Error (Error_String);
   end;

My objection is not only that I need to write extra code, but
that the net effect of that code is to accomplish nothing. Of
course anybody facing this problem will create a function that
does the conversion, but I really doubt if this function call
will get eliminated by the compiler.

Although this specific example might not be too good, there
are very similar situations when using access-to-class-wide
types in arrays. 

Now I think about it, it is possible to rather closely model
typecasting for arrays by implementing array conversion as
showed below. It is a pity that this generates lots of code
with GNAT although all code could be eliminated. Are there
any compilers that correctly compile this into a null function?

Regards,
   Geert

--  Array_Conversion provides a typecasting function for arrays
generic
   type Source_Element is limited private;
   type Target_Element is new Source_Element;
   type Index is (<>);
   type Source_Array is array (Index range <>) of Source_Element;
   type Target_Array is array (Index range <>) of Target_Element;
function Array_Conversion (S : Source_Array) return Target_Array;
pragma Pure (Array_Conversion);

with Unchecked_Conversion;
function Array_Conversion (S : Source_Array) return Target_Array is
   subtype Fixed_Source is Source_Array (S'Range);
   subtype Fixed_Target is Target_Array (S'Range);
   function Convert is new Unchecked_Conversion (Fixed_Source, Fixed_Target);
begin
   return Convert (Fixed_Source (S));
end Array_Conversion;




  reply	other threads:[~1997-06-28  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-24  0:00 array conversion - how to do? Geert Bosch
1997-06-27  0:00 ` Wes Groleau
1997-06-27  0:00   ` Robert Dewar
1997-06-28  0:00     ` Geert Bosch [this message]
1997-07-03  0:00       ` Robert Dewar
1997-07-04  0:00         ` Geert Bosch
1997-07-04  0:00           ` Robert Dewar
1997-07-04  0:00         ` Geert Bosch
1997-06-28  0:00 ` Robert I. Eachus
1997-06-28  0:00   ` Geert Bosch
1997-06-30  0:00     ` Robert I. Eachus
replies disabled

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