comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Question about circular elaboration order error (GNAT).
Date: Sun, 13 Apr 2008 15:46:29 -0400
Date: 2008-04-13T15:46:29-04:00	[thread overview]
Message-ID: <wccve2l4jxm.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 48024d11$0$19786$4d3efbfe@news.sover.net

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

> I'm using GNAT GPL 2007. Consider the following packages. The
> specifications and bodies are each in their own files, as usual.
>
> ----> parent.ads <----
>
> package Parent is
>    -- Needed so this package requires/allows a body.
>    procedure Dummy;
> end Parent;
>
> ----> parent-child.ads <----
>
> package Parent.Child is
>    procedure Print_Stuff;
> end Parent.Child;
>
> ----> parent.adb <----
>
> with Parent.Child;
> package body Parent is
>    procedure Dummy is
>    begin
>       null;
>    end;
> begin
>    Parent.Child.Print_Stuff;   -- Note: invoking child here.
> end Parent;
>
> ----> parent-child.adb <----
>
> with Ada.Text_IO;
> package body Parent.Child is
>    procedure Print_Stuff is
>    begin
>       Ada.Text_IO.Put_Line("Printing stuff in package Parent.Child");
>    end Print_Stuff;
> end Parent.Child;
>
> My main procedure just does a "with Parent" but otherwise does nothing
> (it contains only a null statement). Note that the elaboration code in
> package Parent is calling a subprogram in package Parent.Child. With the
> -gnatwl option, GNAT tells me
>
>    warning: implicit pragma Elaborate_All for "Parent.Child" generated

This pragma (on Parent body) means Parent.Child, and it's body, and
everything they depend on, and their bodies, and so on, must all be
elaborated before Parent body.  One of those is Parent body --
hence the cycle.

If you write pragma Elaborate, I think GNAT will not generate the
implicit pragma Elab_All.  Pragma Elaborate(Parent.Child) on Parent body
means elaborate the body of Parent.Child before Parent body, but it's
not transitive.

Note that the default GNAT rules are stricter than standard Ada.
To get the standard Ada rules, use -gnatE.  But it's not a good
idea -- the stricter rules are beneficial.  For your program,
if you use the standard rules, it is implementation dependent
whether or not you get Program_Error.  That's bad language design!

The stricter rules are conservative, and modular -- when compiling
Parent, it sees that you're calling Parent.Child, and assumes the worst
WITHOUT looking at Parent.Child body.  For example, it assumes that
Parent.Child might call Dummy, causing a real cycle.

Pragma Elaborate is somewhat evil, since it breaks this modularity
(it requires one package body to "know" what's in another package
body).

I suggest you read the section in the GNAT docs about elaboration.
It explains all this stuff in great detail.

>... I'm left with the impression that all
> my attempts to control the elaboration order are fruitless.

Well, they're not fruitless, but elab cycles are indeed frustrating.
I find the error messages (from all compilers I've tried, not just
GNAT) to be confusing.  And every time you add or delete one
of those pragmas you have to recompile a whole bunch of stuff.

> Note my actual program involves a task in the parent that is trying to
> use subprograms in the child. However, the difficulties I'm having
> appear to be unrelated to tasking.

Don't be too sure.  Tasks get activated "early", and can easily cause
elab cycles.  Look at the docs for details.

- Bob



  parent reply	other threads:[~2008-04-13 19:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-13 18:12 Question about circular elaboration order error (GNAT) Peter C. Chapin
2008-04-13 19:43 ` Samuel Tardieu
2008-04-13 20:20   ` Robert A Duff
2008-04-13 21:20     ` Samuel Tardieu
2008-04-14 20:21       ` Robert A Duff
2008-04-14 23:36         ` Adam Beneschan
2008-04-15  7:13           ` Georg Bauhaus
2008-04-13 19:46 ` Robert A Duff [this message]
2008-04-13 22:49   ` Peter C. Chapin
2008-04-14 13:56     ` Robert A Duff
2008-04-14 17:33 ` Jeffrey R. Carter
2008-04-14 17:52   ` Robert A Duff
replies disabled

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