comp.lang.ada
 help / color / mirror / Atom feed
* Return_By_Reference or Return_By_Copy (GNAT bug?)
@ 2004-12-31 11:07 Duncan Sands
  2004-12-31 21:32 ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Duncan Sands @ 2004-12-31 11:07 UTC (permalink / raw)
  To: comp.lang.ada

Consider the following version of the Rosen trick.  Should
A_Type be returned by reference or by copy?  GNAT 3.15p says:
by reference; more recent versions of GNAT say: by copy.  My
understanding is that it should be by reference, because it
has a component R_Type that is a return_by_reference type
(R_Type is return_by_reference because the full view is limited
private).

GNAT 3.15p gives the expected output:

Mechanism: 2 <= GNAT specific, means return_by_reference

 0
 1

 1
 2


gcc at the pre-ssa tag gives:

Mechanism: 1 <= GNAT specific, means return_by_copy

 0
 0

 1
 1

Before reporting this bug I would like to be sure that it is one.
Language lawyers, please step forwards!

Thanks a lot,

Duncan.


-- B spec --

package B is

   type A_Type is limited private;

   function Get_The_A return A_Type;

   procedure Increment (An_A : A_Type);

   procedure Print (An_A : A_Type);

private

   type R_Type (An_A : access A_Type) is limited null record;

   type A_Type is record
      R : R_Type (A_Type'Access);
      I : Integer := 0;
   end record;

end;


-- B body --

with Ada.Text_IO; use Ada.Text_IO;

package body B is

   The_A : A_Type;

   function Get_The_A return A_Type is
   begin
      return The_A;
   end;

   procedure Increment (An_A : A_Type) is
   begin
      An_A.R.An_A.I := An_A.R.An_A.I + 1;
   end;

   procedure Print (An_A : A_Type) is
   begin
      Put_Line (An_A.I'Img);
   end;

end;


-- X --

with Ada.Text_IO; use Ada.Text_IO;
with B; use B;

procedure X is

   procedure Look is
      The_A_Reference : A_Type renames Get_The_A;
   begin
      Print (The_A_Reference);
      Increment (The_A_Reference);
      Print (The_A_Reference);
   end;

begin
   Put_Line ("Mechanism:" & Integer'Image (Get_The_A'Mechanism_Code)); -- GNAT specific attribute, 1 = by_copy, 2 = by_reference
   New_Line;
   Look;
   New_Line;
   Look;
end;



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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2004-12-31 11:07 Return_By_Reference or Return_By_Copy (GNAT bug?) Duncan Sands
@ 2004-12-31 21:32 ` Randy Brukardt
  2005-01-01 22:00   ` Duncan Sands
  0 siblings, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2004-12-31 21:32 UTC (permalink / raw)


"Duncan Sands" <baldrick@free.fr> wrote in message
news:mailman.19.1104491242.527.comp.lang.ada@ada-france.org...
> Consider the following version of the Rosen trick.  Should
> A_Type be returned by reference or by copy?  GNAT 3.15p says:
> by reference; more recent versions of GNAT say: by copy.  My
> understanding is that it should be by reference, because it
> has a component R_Type that is a return_by_reference type
> (R_Type is return_by_reference because the full view is limited
> private).

A_Type certainly is return-by-reference.

But take care: Ada 2005 flushes the entire return-by-reference garbage in
favor of build-in-place limited functions. Such functions only can return
aggregates or build-in-place functions (or be built component-by-component
in an extended return statement). To do what you want in Ada 2005 would
require using an anonymous access function:

    function Get_an_A return access A_Type;

Note that this makes the reference return explicit (no magical rules to page
through).

This probably is the biggest incompatibility in Ada 2005 (although it often
is just changing a runtime check to compile-time). We tried various halfway
solutions, but eventually decided that this was so screwed up that we were
better off just doing it over correctly. (Build-in-place functions can be
used to initialize objects, so we now can write meaningful limited
constants, and don't have to stand on our heads to initialize things.)

Moral: Avoid functions returning limited types in Ada 95 code, since they'll
likely have to be changed in Ada 2005 anyway.

                           Randy.







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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2004-12-31 21:32 ` Randy Brukardt
@ 2005-01-01 22:00   ` Duncan Sands
  2005-01-03 23:11     ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Duncan Sands @ 2005-01-01 22:00 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Randy Brukardt

Hi Randy,

> A_Type certainly is return-by-reference.

thanks for confirming.

>     function Get_an_A return access A_Type;
> 
> Note that this makes the reference return explicit (no magical rules to page
> through).

I was playing around with return by reference to see if it is possible to
get unserialized access to a protected variable using the Rosen trick (it is,
see example below, especially the protected object P in package C).  Will this
be illegal in Ada 2005?

All the best,

Duncan.


package B is

   type A_Type is limited private;

   function Get_The_A return A_Type;

   procedure Increment (An_A : A_Type);

   procedure Print (An_A : A_Type);

private

   type R_Type (An_A : access A_Type) is limited null record;

   type A_Type is record
      R : R_Type (A_Type'Access);
      I : Integer := 0;
   end record;

end;

with Ada.Text_IO; use Ada.Text_IO;

package body B is

   The_A : A_Type;

   function Get_The_A return A_Type is
   begin
      return The_A;
   end;

   procedure Increment (An_A : A_Type) is
   begin
      An_A.R.An_A.I := An_A.R.An_A.I + 1;
   end;

   procedure Print (An_A : A_Type) is
   begin
      Put_Line (An_A.I'Img);
   end;

end;

with B; use B;

package C is

   protected P is

      function Get_A return A_Type;

   private

      P_A : A_Type;

   end P;

end;

package body C is

   protected body P is

      function Get_A return A_Type is
      begin
         return P_A;
      end Get_A;

   end P;

end;

with Ada.Text_IO; use Ada.Text_IO;
with B; use B;
with C; use C;

procedure Y is

   procedure Look is
      The_A_Reference : A_Type renames P.Get_A;
   begin
      Print (The_A_Reference);
      Increment (The_A_Reference);
      Print (The_A_Reference);
   end;

begin
   Put_Line ("Mechanism:" & Integer'Image (Get_The_A'Mechanism_Code));
   New_Line;
   Look;
   New_Line;
   Look;
end;



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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-01 22:00   ` Duncan Sands
@ 2005-01-03 23:11     ` Randy Brukardt
  2005-01-04  4:20       ` Larry Kilgallen
  2005-01-08 13:06       ` Duncan Sands
  0 siblings, 2 replies; 9+ messages in thread
From: Randy Brukardt @ 2005-01-03 23:11 UTC (permalink / raw)


"Duncan Sands" <baldrick@free.fr> wrote in message
news:mailman.21.1104616882.527.comp.lang.ada@ada-france.org...
> I was playing around with return by reference to see if it is possible to
> get unserialized access to a protected variable using the Rosen trick (it
is,
> see example below, especially the protected object P in package C).  Will
this
> be illegal in Ada 2005?

Yes, it will.

...
...
> package body B is
>
>    The_A : A_Type;
>
>    function Get_The_A return A_Type is
>    begin
>       return The_A;

-- This return will be illegal in Ada 2005 (the return expression is not an
aggregate or function call).
-- Declare the function:
    function Get_The_A return access A_Type is
    begin
       return The_A'Access;
-- to do this in Ada 2005. (Note this makes it explicit what you're doing -
a good thing, IMHO.)

>    end;

...

> package body C is
>
>    protected body P is
>
>       function Get_A return A_Type is
>       begin
>          return P_A;

-- Similarly, this return will be illegal in Ada 2005.
--  (Can't return an object from a limited function, as that would require a
copy into the result object.)
-- Use the same workaround as above.

>       end Get_A;
>
>    end P;
>
> end;

...







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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-03 23:11     ` Randy Brukardt
@ 2005-01-04  4:20       ` Larry Kilgallen
  2005-01-04 23:27         ` Randy Brukardt
  2005-01-08 13:06       ` Duncan Sands
  1 sibling, 1 reply; 9+ messages in thread
From: Larry Kilgallen @ 2005-01-04  4:20 UTC (permalink / raw)


In article <H4ydndUFyuAmTUTcRVn-3A@megapath.net>, "Randy Brukardt" <randy@rrsoftware.com> writes:
> "Duncan Sands" <baldrick@free.fr> wrote in message
> news:mailman.21.1104616882.527.comp.lang.ada@ada-france.org...
>> I was playing around with return by reference to see if it is possible to
>> get unserialized access to a protected variable using the Rosen trick (it
> is,
>> see example below, especially the protected object P in package C).  Will
> this
>> be illegal in Ada 2005?
> 
> Yes, it will.
> 
> ...
> ...
>> package body B is
>>
>>    The_A : A_Type;
>>
>>    function Get_The_A return A_Type is
>>    begin
>>       return The_A;
> 
> -- This return will be illegal in Ada 2005 (the return expression is not an
> aggregate or function call).

Even though nothing in that sample says protected ?



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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-04  4:20       ` Larry Kilgallen
@ 2005-01-04 23:27         ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2005-01-04 23:27 UTC (permalink / raw)


"Larry Kilgallen" <Kilgallen@SpamCop.net> wrote in message
news:$xgF7TJy$z4V@eisner.encompasserve.org...
> >> package body B is
> >>
> >>    The_A : A_Type;
> >>
> >>    function Get_The_A return A_Type is
> >>    begin
> >>       return The_A;
> >
> > -- This return will be illegal in Ada 2005 (the return expression is not
an
> > aggregate or function call).
>
> Even though nothing in that sample says protected ?

It's nothing to do with protected; the rules for all functions returning
*limited* types have changed. (Both to make constructor functions possible,
and to make limited interfaces be near universal.) The incompatibility is
fairly rare (such functions are rarely useful in Ada 95), and the workaround
is easy -- with one possible exception: functions returning limited private
types. (Those seem more common.) Note, however, that such a function, while
legal in Ada 95, probably would raise Program_Error if instantiated with a
really limited type. So we're just making a run-time problem into a
compile-time one: that is, detecting an error earlier.

                            Randy.






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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-03 23:11     ` Randy Brukardt
  2005-01-04  4:20       ` Larry Kilgallen
@ 2005-01-08 13:06       ` Duncan Sands
  2005-01-10 21:05         ` Randy Brukardt
  1 sibling, 1 reply; 9+ messages in thread
From: Duncan Sands @ 2005-01-08 13:06 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Randy Brukardt

Hi Randy,

> > I was playing around with return by reference to see if it is possible to
> > get unserialized access to a protected variable using the Rosen trick (it
> is,
> > see example below, especially the protected object P in package C).  Will
> this
> > be illegal in Ada 2005?
> 
> Yes, it will.

it seems to be legal to have a protected procedure pass out an access to a
protected variable, allowing that variable to be accessed without serialisation.
I somehow expected this to be illegal...  Is there any legitimate use for it?

Thanks,

Duncan.



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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-08 13:06       ` Duncan Sands
@ 2005-01-10 21:05         ` Randy Brukardt
  2005-03-08 16:12           ` Duncan Sands
  0 siblings, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2005-01-10 21:05 UTC (permalink / raw)


"Duncan Sands" <baldrick@free.fr> wrote in message
news:mailman.38.1105189579.527.comp.lang.ada@ada-france.org...
> Hi Randy,
>
> > > I was playing around with return by reference to see if it is possible
to
> > > get unserialized access to a protected variable using the Rosen trick
(it
> > is,
> > > see example below, especially the protected object P in package C).
Will
> > this
> > > be illegal in Ada 2005?
> >
> > Yes, it will.
>
> it seems to be legal to have a protected procedure pass out an access to a
> protected variable, allowing that variable to be accessed without
serialisation.
> I somehow expected this to be illegal...  Is there any legitimate use for
it?

I would have expected that accessibility checks would have made that
illegal. Or that it would have been legal in Ada 95. But I haven't been able
to convince myself that either is the case. I'm pretty sure that no one has
thought about this as a problem.

Since we're still fixing problems in the standard, there is still time to
consider this issue. Therefore, I'd suggest writing an example using Ada
2005 anonymous access functions and sending it to Ada-Comment@ada-auth.org
along with your question. That way, it will get some airing in front of the
ARG.

                       Randy.






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

* Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
  2005-01-10 21:05         ` Randy Brukardt
@ 2005-03-08 16:12           ` Duncan Sands
  0 siblings, 0 replies; 9+ messages in thread
From: Duncan Sands @ 2005-03-08 16:12 UTC (permalink / raw)
  To: Randy Brukardt; +Cc: comp.lang.ada

On Mon, 2005-01-10 at 15:05 -0600, Randy Brukardt wrote:
> "Duncan Sands" <baldrick@free.fr> wrote in message
> news:mailman.38.1105189579.527.comp.lang.ada@ada-france.org...
> > Hi Randy,
> >
> > > > I was playing around with return by reference to see if it is possible
> to
> > > > get unserialized access to a protected variable using the Rosen trick
> (it
> > > is,
> > > > see example below, especially the protected object P in package C).
> Will
> > > this
> > > > be illegal in Ada 2005?
> > >
> > > Yes, it will.
> >
> > it seems to be legal to have a protected procedure pass out an access to a
> > protected variable, allowing that variable to be accessed without
> serialisation.
> > I somehow expected this to be illegal...  Is there any legitimate use for
> it?
> 
> I would have expected that accessibility checks would have made that
> illegal. Or that it would have been legal in Ada 95. But I haven't been able
> to convince myself that either is the case. I'm pretty sure that no one has
> thought about this as a problem.
> 
> Since we're still fixing problems in the standard, there is still time to
> consider this issue. Therefore, I'd suggest writing an example using Ada
> 2005 anonymous access functions and sending it to Ada-Comment@ada-auth.org
> along with your question. That way, it will get some airing in front of the
> ARG.

Hi Randy, in fact there is no need for anonymous access types.  Consider
the following program.  The protected procedure PT.G passes a pointer to
one of its protected variables to an outside procedure DDD, which merrily
modifies the variable with no synchronization at all.  With the latest
GNAT compiler I see:

$ ./ddd
 0
 1

All the best,

Duncan.

-- chop here --

package CCC is

   type IA is access all Integer;

   protected PT is

      procedure G (P : out IA);

      function H return Integer;

   private
      I : aliased Integer;
   end;

end CCC;

package body CCC is

   protected body PT is

      procedure G (P : out IA) is
      begin
         P := I'Access;
      end;

      function H return Integer is
      begin
         return I;
      end;

   end;

end CCC;

with Ada.Text_IO;
use Ada.Text_IO;
with CCC;
use CCC;

procedure DDD is
   X : IA;
begin
   Put_Line (Integer'Image (PT.H));
   PT.G (X);
   X.all := 1;
   Put_Line (Integer'Image (PT.H));
end DDD;





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

end of thread, other threads:[~2005-03-08 16:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-31 11:07 Return_By_Reference or Return_By_Copy (GNAT bug?) Duncan Sands
2004-12-31 21:32 ` Randy Brukardt
2005-01-01 22:00   ` Duncan Sands
2005-01-03 23:11     ` Randy Brukardt
2005-01-04  4:20       ` Larry Kilgallen
2005-01-04 23:27         ` Randy Brukardt
2005-01-08 13:06       ` Duncan Sands
2005-01-10 21:05         ` Randy Brukardt
2005-03-08 16:12           ` Duncan Sands

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