comp.lang.ada
 help / color / mirror / Atom feed
From: jcallen@Encore.COM (Jerry Callen)
Subject: aliases/function return values
Date: 28 Aug 90 15:14:22 GMT	[thread overview]
Message-ID: <12594@encore.Encore.COM> (raw)

I've run across a curious anomaly with the value returned by a function;
if the function value is returned by reference, an alias can be created.
The following program illustrates the problem. NOTE: I will not attempt
to justify the following code; it's boiled down from some code supplied
by a user. Surely _I_ would never write such bizarre code. :-)

-----------------------------------------------------------------
with report; use report;

procedure alias1 is
    package p1 is
        ELEMENTS: constant := 100;
        subtype range_t is integer range 1..ELEMENTS;
        type array_t is array(range_t) of boolean;

        function make_set (which: range_t) return array_t;
        function transform (a, b: array_t) return array_t;
    end p1;

    use p1;

    v1, v2, v3: array_t;

    package body p1 is
        global: array_t;
        null_array: constant array_t := (others => FALSE);

        function make_set (which: range_t) return array_t is
        begin
            global := null_array;
            global(which) := TRUE;
            return global; -- may create an alias for "global"
        end make_set;

        function transform (a, b: array_t) return array_t is
        begin
            -- if the value returned by make_set is used as a
            -- parameter to this function, AND the compiler
            -- created an alias, the next statement will tromp
            -- on the parameter.
            global := null_array;
            global := (a xor b) and a;
            return global;
        end transform;
    end p1;
begin
    test ("alias1", "see if an alias is created for function return");
    v1 := (others => TRUE);
    v2 := make_set (20);
    v3 := transform (v1, v2);

    if v3(20) then
        failed ("base case failed - compiler is TOTALLY broke");
    end if;

    v3 := transform (v1, make_set (20)); -- interesting call

    if v3(20) then
        failed ("compiler used 'return by value' and created an alias");
    end if;

    result;
end alias1;

-----------------------------------------------------------------------

This is obviously a pretty contrived test case, but it should get the
point across. Is a compiler permitted to return objects "by reference"
(with the possibility of creating aliases) or must it create a copy
of the return value? I can't find anything in the LRM on this point,
although there is some gobble-de-gook in 6.2.13 that might be relevent.

Aside: this program is also dependent upon the mechanism used to
pass parameters; to fail, "return by reference" and "call by reference"
must BOTH be used.

I would LIKE to think that objects may be returned by reference; 
otherwise potentially large objects would have to be copied.
I would call the program above erroneous, since it is dependent upon
the mechanism used for parameter passing and value return.

-- Jerry Callen
   jcallen@encore.com

             reply	other threads:[~1990-08-28 15:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-08-28 15:14 Jerry Callen [this message]
1990-08-29 15:12 ` aliases/function return values davidr
1990-09-07 19:00   ` Robert I. Eachus
1990-09-07 20:42     ` Michael Feldman
1990-08-31  0:47 ` Charles H. Sampson
  -- strict thread matches above, loose matches on Subject: below --
1990-08-30 13:29 "Norman H. Cohen"
replies disabled

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