comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Generic instantiation before actual subprogram body
Date: Thu, 04 Dec 2014 10:34:21 -0500
Date: 2014-12-04T10:34:21-05:00	[thread overview]
Message-ID: <wcciohrwhs2.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: slrnm80cc9.nrc.lithiumcat@nat.rebma.instinctive.eu

Natasha Kerensikova <lithiumcat@instinctive.eu> writes:

> This is the body of the generic unit, while my question was about the
> body of the actual subprograms use in the generic instantiation to match
> for the formal subprogram parameters in the generic.
>
> Since the generic unit is another unit, I don't expect moving the
> instantiation in the source text to have any effect. While the
> Program_Error I mentioned was about the position of the instantiation
> with regard to actual subprogram bodies.
>
> It really looked like that:
>
> with Generic_Procedure;
>
> package body Which_Raises_Program_Error is
>
>    procedure Local_Procedure;
>
>    procedure Instantiated is new Generic_Procedure (Local_Procedure);

Procedures don't do anything during elaboration, other than to take note
of the fact that they've been elaborated (i.e. set a flag).  So it
can't call Local_Procedure during elaboration, so there's no problem
here.

If Generic_Procedure were a generic package, then it might call
Local_Procedure during elaboration, in which case you'd get P_E.  That's
unusual -- in my experience, generic packages don't contain a lot of
elaboration code.

>    procedure Local_Procedure is
>    begin
>       <some code here>
>    end Local_Procedure;
>
>    procedure Public_Procedure is
>    begin
>       Instantiated;
>    end Public_Procedure;
>
> end Which_Raises_Program_Error;

> It was really a problem related to the position of the instantiation
> (Instantiated in the example) relative to the body of an actual
> subprogram (here Local_Procedure).
>
> If I understand correctly, one obvious way to reach this effect is when
> a generic package uses a formal function to initialize a global
> variable. The function call would be elaborated when the generic package
> is instantiated, so that would be a function call whose body has not yet
> been seen, so Program_Error.

Yes, that's one example, but it can happen only for generic packages,
not generic procedures.  Another would be if the generic package body
ends with:

    begin
        Formal_Procedure(...);
    end My_Generic;

> I'm quite sure this wasn't the case I encountered, so I'm looking for
> other ways an actual body can influence a generic instance.
> Are there any?

Any call during elaboration of a generic package body (well, I mean
elaboration of the instance).

> Or maybe my memory is playing tricks on me, confusing actual subprogram
> body and generic unit body? Though I don't remember having ever tried to
> instantiate a generic in the same compilation unit where I define it.

If you use static elaboration (which is the default; the -gnatE switch
uses the inferior standard Ada way), then you are correct that
inter-unit instantiations can never fail due to unelaborated generic
body (but you might get a link-time complaint about elaboration cycles).
Within the same compilation unit, it can fail, and I think GNAT will
always warn about it.

> Would that mean that a subprogram whose body has not yet been seen can
> always be used as an actual for a generic instantiation, except when
> used immediately like in the package example above?

Yes.

There is a long section in the GNAT docs explaining elaboration in great
detail.  It's worth reading, and is a lot easier to understand than the
RM.

- Bob


  parent reply	other threads:[~2014-12-04 15:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-04  8:48 Generic instantiation before actual subprogram body Natasha Kerensikova
2014-12-04  9:31 ` J-P. Rosen
2014-12-04 10:03   ` Natasha Kerensikova
2014-12-04 10:40     ` Georg Bauhaus
2014-12-04 14:12     ` J-P. Rosen
2014-12-04 15:34     ` Robert A Duff [this message]
2014-12-04 15:49 ` Robert A Duff
2014-12-04 22:49   ` Randy Brukardt
replies disabled

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