comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Question about library unit elaboration order.
Date: Tue, 07 Aug 2007 20:00:37 -0400
Date: 2007-08-07T20:00:37-04:00	[thread overview]
Message-ID: <wccvebqao9m.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 46b902fd$0$25585$4d3efbfe@news.sover.net

"Peter C. Chapin" <pchapin@sover.net> writes:

> Hi! Consider the following package body:
>
> with Other;
> package body Example is
>   X : Integer;
> begin
>   X := Other.Some_Function("Argument string");
> end Example;
>
> Clear if this is going to work right package Other's body needs to be
> elaborated before package Example's body (both packages are library
> units, by the way). My question is basically: does the Ada language
> guarantee that this will happen without me doing any further work?

No.  It might work on some implementations, but raise Program_Error on
others.

However, if you are using GNAT, the default rules ensure that it will
work.  You have to use a switch to get the standard rules.

> Looking at the Ada 2005 reference manual, section 10.2, "Program
> Execution", paragraph 9, I see:
>
> "The order of elaboration of library units is determined primarily by
> the elaboration dependences. There is an elaboration dependence of a
> given library_item upon another if the given library_item or any of its
> subunits depends semantically on the other library_item."
>
> I'm not sure exactly what is meant by "depends semantically."

Look up "dependence" in the index.  Mostly, it's the with clauses.

>... However,
> it sounds like this is saying that my package body Example has an
> elaboration dependency on package Other's body.

No.  The "with Other" creates a semantic dependence on the spec of
other, not on the body.

Note that the body of Other might say "with Example", so you don't want
semantic dependences on bodies all the time.

> Later in section 10.2 there is discussion about the way the environment
> task elaborates the library units. Paragraph 14 says, "The order of all
> included library_items is such that there are no forward elaboration
> dependences." So this seems to say (I think) that the language will
> ensure that my package Other gets elaborated before package Example.

No.

> However, if this is all true, then what is the point of pragma
> Elaborate?

The point is to create an elaboration dependence on the body of Other,
to make sure that body is elaborated first.

Note that you normally want Elaborate_All, not Elaborate,
because Other's body might call something else, unknown
to Example.

>... I understand that I can use pragam Elaborate to force the
> relative order of elaboration of two library units. However, if those
> units already have a "semantic dependency," then pragma Elaborate should
> be unnecessary, right?

> I'm confused by this.

There's a section in the GNAT User's Guide called "Elaboration Order
Handling in GNAT", which explains all this stuff in great detail.
Both the language rules, and the way GNAT deals with it.

- Bob



  reply	other threads:[~2007-08-08  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-07 23:40 Question about library unit elaboration order Peter C. Chapin
2007-08-08  0:00 ` Robert A Duff [this message]
2007-08-08  0:32   ` Peter C. Chapin
2007-08-10 19:32 ` Adam Beneschan
replies disabled

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