From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!feeder.erje.net!eu.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Generic instantiation before actual subprogram body Date: Thu, 04 Dec 2014 10:34:21 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls7.std.com 1417707232 9205 192.74.137.71 (4 Dec 2014 15:33:52 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 4 Dec 2014 15:33:52 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:lo1IhUv2K05LKAZxciDltsDS2gs= Xref: news.eternal-september.org comp.lang.ada:23862 Date: 2014-12-04T10:34:21-05:00 List-Id: Natasha Kerensikova 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 > > 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