comp.lang.ada
 help / color / mirror / Atom feed
* Question about library unit elaboration order.
@ 2007-08-07 23:40 Peter C. Chapin
  2007-08-08  0:00 ` Robert A Duff
  2007-08-10 19:32 ` Adam Beneschan
  0 siblings, 2 replies; 4+ messages in thread
From: Peter C. Chapin @ 2007-08-07 23:40 UTC (permalink / raw)


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?

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." However,
it sounds like this is saying that my package body Example has an
elaboration dependency on package Other's body.

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.

However, if this is all true, then what is the point of pragma
Elaborate? 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?

Now, let's turn to John Barnes' "Programming in Ada 2005." In section
12.8 of that book on page 277, he shows an example that is very similar
to mine.

with P;
package Q is
 I : Integer = Q.A;
end Q;

At the bottom of the page he says, "The key point is that the dependency
rules only partially constrain the order in which the units are
elaborated and so we need some way to impose order at the library level
much as the linear text imposes elaboration order within a unit." He
then shows the solution as

with P;
pragma Elaborate(P);
package Q is
  I : Integer = P.A;
end Q;

I realize this example uses a package specification rather than a
package body as in my example. Does that matter? It seems like package Q
 "depends semantically" on package P's body and so doesn't the reference
manual assure us that the package P's body will get elaborated first
even without the pragma Elaborate?

I'm confused by this.

Peter



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about library unit elaboration order.
  2007-08-07 23:40 Question about library unit elaboration order Peter C. Chapin
@ 2007-08-08  0:00 ` Robert A Duff
  2007-08-08  0:32   ` Peter C. Chapin
  2007-08-10 19:32 ` Adam Beneschan
  1 sibling, 1 reply; 4+ messages in thread
From: Robert A Duff @ 2007-08-08  0:00 UTC (permalink / raw)


"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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about library unit elaboration order.
  2007-08-08  0:00 ` Robert A Duff
@ 2007-08-08  0:32   ` Peter C. Chapin
  0 siblings, 0 replies; 4+ messages in thread
From: Peter C. Chapin @ 2007-08-08  0:32 UTC (permalink / raw)


Robert A Duff wrote:

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

Okay, I'll check it out. Thanks for your note, it helped a lot.

Peter



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about library unit elaboration order.
  2007-08-07 23:40 Question about library unit elaboration order Peter C. Chapin
  2007-08-08  0:00 ` Robert A Duff
@ 2007-08-10 19:32 ` Adam Beneschan
  1 sibling, 0 replies; 4+ messages in thread
From: Adam Beneschan @ 2007-08-10 19:32 UTC (permalink / raw)


On Aug 7, 4:40 pm, "Peter C. Chapin" <pcha...@sover.net> wrote:
> 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?
>
> 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."

It's explained in 10.1.1(26).  If a library unit A has a "with B;"
clause on it, then A depends semantically on B---but it only depends
semantically on B's specification, not on B's body.  I think that's
the key thing you need to understand in order to understand the
relevant language rules.  (If you really want to dig deeper,
10.1.1(26) uses the term "mention", which is defined in 10.1.2(6),
which depends on the term "denote", which is defined in 8.6(16), which
says that an occurrence of a usage name denotes a declaration---and
the "declaration" of a package is its specification, not its body.)

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

No, unless you add an Elaborate or Elaborate_All pragma.  As I
mentioned above, Example's body does *not* semantically depend on
Other's body (it depends semantically on Other's specification).  So
by 10.2(9), there is no elaboration dependency unless there's an
Elaborate or Elaborate_All pragma.  That should also explain what the
purpose of those pragmas is.

                           -- Adam






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-10 19:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-07 23:40 Question about library unit elaboration order Peter C. Chapin
2007-08-08  0:00 ` Robert A Duff
2007-08-08  0:32   ` Peter C. Chapin
2007-08-10 19:32 ` Adam Beneschan

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