comp.lang.ada
 help / color / mirror / Atom feed
From: Natasha Kerensikova <lithiumcat@instinctive.eu>
Subject: Re: Generic instantiation before actual subprogram body
Date: Thu, 4 Dec 2014 10:03:46 +0000 (UTC)
Date: 2014-12-04T10:03:46+00:00	[thread overview]
Message-ID: <slrnm80cc9.nrc.lithiumcat@nat.rebma.instinctive.eu> (raw)
In-Reply-To: m5p9la$s0u$1@dont-email.me

On 2014-12-04, J-P. Rosen <rosen@adalog.fr> wrote:
> Le 04/12/2014 09:48, Natasha Kerensikova a écrit :
>> My question is now, in which set of circumstances is it allowed (or
>> forbidden) to instantiate a generic with a subprogram whose body has not
>> yet been seen by the compiler?
> RM2012(11.5(20)):
> "When [...] a generic instantiation is elaborated, check that the body
> of the corresponding unit has already been elaborated."

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);

   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;


package body Which_Does_Not_Raise_Program_Error is

   procedure Local_Procedure;

   procedure Local_Procedure is
   begin
      <some code here>
   end Local_Procedure;

   procedure Instantiated is new Generic_Procedure (Local_Procedure);

   procedure Public_Procedure is
   begin
      Instantiated;
   end Public_Procedure;

end Which_Does_Not_Raise_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.

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?


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.

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?

> FYI: to find it, go to the index, and follow the references for
> "Program_Error".

I tried that, though it was after I lost hope of understanding the
influences between elaboration and instantiation using only the ARM.
I'll try again more thoroughly when I have some free time.


Thanks for your insights,
Natasha

  reply	other threads:[~2014-12-04 10:03 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 [this message]
2014-12-04 10:40     ` Georg Bauhaus
2014-12-04 14:12     ` J-P. Rosen
2014-12-04 15:34     ` Robert A Duff
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