Return_By_Reference or Return_By_Copy (GNAT bug?)

From: Duncan Sands (baldrick_at_free.fr)
Date: 12/31/04


Date: Fri, 31 Dec 2004 12:07:05 +0100
To: 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).

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;



Relevant Pages

  • Re: Efficiency of returning big objects
    ... since I like to store them in indefinite collections ... passed by reference, they are always efficiently passed around. ... Same with arrays - when returning a slice of a non-local string, GNAT ... without efficiency penalties. ...
    (comp.lang.ada)
  • Re: Efficiency of returning big objects
    ... reference, they are always efficiently passed around. ... Same with arrays - when returning a slice of a non-local string, GNAT ... without efficiency penalties. ...
    (comp.lang.ada)
  • Re: Defect with formal package parameters
    ... I had the identical problem ... with GNAT 2007. ... You don't mention the Bugzilla reference you reported this ...
    (comp.lang.ada)
  • Re: Simple library functions
    ... > GNAT has now an equivalent package named GNAT.String_Split. ... But I've got gcc 3.3.3 with GNAT, ... Duh! ...
    (comp.lang.ada)
  • Re: Need Help, Trying to learn WCF
    ... private to the class, which worked great. ... reference the new Function. ... the folder where i was saving the application once this was brought to my ... itself, subclasses, and all classes in the same package or directory. ...
    (microsoft.public.dotnet.framework.webservices)