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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,691503f3d2c9213d X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Question about circular elaboration order error (GNAT). Date: Sun, 13 Apr 2008 15:46:29 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <48024d11$0$19786$4d3efbfe@news.sover.net> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1208115989 11273 192.74.137.71 (13 Apr 2008 19:46:29 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 13 Apr 2008 19:46:29 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:oHZGAG+TF3SXlRhJVPk1uUaekkk= Xref: g2news1.google.com comp.lang.ada:20915 Date: 2008-04-13T15:46:29-04:00 List-Id: "Peter C. Chapin" 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