comp.lang.ada
 help / color / mirror / Atom feed
From: Duncan Sands <baldrick@free.fr>
To: comp.lang.ada@ada-france.org
Subject: Return_By_Reference or Return_By_Copy (GNAT bug?)
Date: Fri, 31 Dec 2004 12:07:05 +0100
Date: 2004-12-31T12:07:05+01:00	[thread overview]
Message-ID: <mailman.19.1104491242.527.comp.lang.ada@ada-france.org> (raw)

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;



             reply	other threads:[~2004-12-31 11:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-31 11:07 Duncan Sands [this message]
2004-12-31 21:32 ` Return_By_Reference or Return_By_Copy (GNAT bug?) 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
replies disabled

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