[net.lang.ada] elaboration of package specs

Bryan@SU-SIERRA.ARPA (Doug Bryan) (01/27/86)

consider...
	
	function Initial_Value return Integer;
	function Initial_Value return Integer is
        begin
	   return 42;
	end Initial_Value;

	package X is
	   I : Integer;
	end X;

	with Initial_Value;
	package body X is
	begin
	   I := Initial_Value;
	end X;

Suppose that the above four compilation units are compiled in the order
presented.  Then the function is rewritten as follows:

	function Initial_Value (I : Integer) return Integer;
	function Initial_Value (I : Integer) return Integer is
	begin
	   return I + 42;
	end Initial_Value;

and both the spec and body are recompiled.  May the package X be elaborated
without the recompilation if its body?  It is clear that the body of X
becomes obsolete if the spec of Initial_Value is recompiled.  I heard
that the language experts panel in Boston decided that in such a case
the spec of X may be elaborated and that the body of X would be ignored.
Does "obsolete" mean "removed from the library".  I always had the
understanding that a compilation unit, within a library, could be in one
of three states:

	1- ready for elaboration
	2- obsolete
	3- non-existent

If it was obsolete, it could not be elaborated.  If a compilation unit
was dependant on an obsolete unit, it too could not be elaborated.  It
seems that the new view is that the spec of X is not really dependant
on the body of X.

??

doug
-------

info-ada@ucbvax.UUCP (02/05/86)

In <8601272025.AA19624@ucbvax.berkeley.edu>, Doug Bryan asks:

>consider...
>        
>        function Initial_Value return Integer;
>        function Initial_Value return Integer is
>        begin
>           return 42;
>        end Initial_Value;
>
>        package X is
>           I : Integer;
>        end X;
>
>        with Initial_Value;
>        package body X is
>        begin
>           I := Initial_Value;
>        end X;
>
>Suppose that the above four compilation units are compiled in the order
>presented.  

(First, note that you should write 

        pragma ELABORATE (Initial_Value);

just after the with clause to ensure the body of Initial_Value is elaborated
before package body X is elaborated (and Initial_Value is called).  Otherwise
you will get PROGRAM_ERROR if your implementation does not elaborate
Initial_Value's body before elaborating X's body.  The absence of the pragma,
however, is incidental to Doug's main question, which continues:)

>Then the function is rewritten as follows:
>
>        function Initial_Value (I : Integer) return Integer;
>        function Initial_Value (I : Integer) return Integer is
>        begin
>           return I + 42;
>        end Initial_Value;
>
>and both the spec and body are recompiled.  May the package X be elaborated
>without the recompilation [of] its body?  It is clear that the body of X
>becomes obsolete if the spec of Initial_Value is recompiled.  I heard
>that the language experts panel in Boston decided that in such a case
>the spec of X may be elaborated and that the body of X would be ignored.

As Doug points out, the recompilation of Initial_Value makes the body of X
obsolete, and therefore, it is not available to be elaborated.  If an attempt
is now made to execute a main program that WITHs X, the body of X will not be
elaborated because there is no non-obsolete body of X.  Moreover, since no
body is needed for X (i.e., since the package specification does not require a
body), execution of the main program can occur and no body for X will be
elaborated.  Doug continues:

>Does "obsolete" mean "removed from the library".  I always had the
>understanding that a compilation unit, within a library, could be in one
>of three states:
>
>        1- ready for elaboration
>        2- obsolete
>        3- non-existent
>
>If it was obsolete, it could not be elaborated.  If a compilation unit
>was dependant on an obsolete unit, it too could not be elaborated.  It
>seems that the new view is that the spec of X is not really dependant
>on the body of X.

There is no new view here.  The specification of X is not (and never was)
dependent on the body of X.  The use of variable I within the body of X does
not create a dependence of the specification on the body.  In particular, if
the body becomes obsolete, the specification cannot be considered obsolete as
well.  10.3(5) says obsolete units need not be recompiled "if they are no
longer needed."  In Ada terms, package body X wasn't needed in the first
place, so it certainly isn't needed after it becomes obsolete!  In particular,
execution of the main program can proceed in the absence of X's body.

Whether an obsolete unit is actually removed from a library is up to an
implementation.  From a semantic viewpoint, however, an obsolete body has the
same effect as if it were absent.

If you want to make sure that an "unneeded" package body is never accidentally
made obsolete, and therefore left unelaborated without warning, you should
write a package specification that requires a body.  Perhaps the cheapest way
to do so is with an incomplete type:

        package X is
            I : Integer;
        private
            type Body_Required;  -- incomplete type requiring a body
        end X;

        with Initial_Value;
        pragma Elaborate (Initial_Value);
        package body X is
            type Body_Required is (Fer_Sure); -- dummy type
        begin
            I := Initial_Value;
        end X;

Now recompilation of Initial_Value's specification still makes X's body
obsolete, but any attempt to execute a main program will fail because X needs
a package body, and there is no non-obsolete version in the library.

John B. Goodenough                           goodenou@wanginst        (CSNET)
Wang Institute of Graduate Studies           decvax!wanginst!goodenou (UUCP)
Tyng Road, Tyngsboro, MA 01879               Goodenough@ISI           (ARPA)
(At Wang Institute until 6/1/86)             617-649-9731