As I've previously posted here, I'm porting a medium to largish Ada codebase from the DDC-i Ada compiler to GNAT Studio in the hopes of creating a simulator/emulator/trainer/brain-in-a-box for the device this code is the firmware of. The signature software architectural style of the company who wrote this code, across projects and languages, is to make giant hairballs where everything depends on everything. There's no obvious conceptual order to use for an elaboration order, and evidently they had developed around whatever accidental/implicit order DDC-i compiled things in.
- The main event loop calls logging. The logging package sends log messages to the comm package. The comm package deposits "please do I/O" events on the main loop.
- Package A provides types and procedures for serial data format A. Package B provides types and procedures for data format B. Packages A and B 'with' each other and use types and procs from each other.
- The main event loop needs types from data formats A and B to send to subprocedures. Instead of the authors creating spec file for shared types with no dependencies, the main loop must 'with' packages A and B to get those types. A and B are mutually co-dependent; see: problem 2. Also, A and B perform logging; see: problem 1.
It goes on and on like that; GNAT produces >2,000 lines of elaboration order circularities detected. Actually before I cleaned it up some, it was so bad that processing this codebase was causing the compiler to crash with internal errors, sometimes producing an exception message, sometimes just halting with no output. I couldn't get a good minimal example for a bug report because it wasn't deterministic and the last code location it printed before dying wasn't (evidently) where the problem code was (and I'm not at liberty to share the unredacted code).
I got some advice on my last post(s) about this. Advice to mark packages as "with Pure" or "with Preelaborate" didn't have any effect, and I eventually reverted that change. The advice that helped was for each package to have a pragma Elaborate_All listing all depended-upon packages, and progressively relax things from there. That accomplished two things; it made the compiler complain about circularity problems early, and it seems to avoid the compiler crashing later.
Now the problem I have is, once I've identified an elaboration order I think will work, and I take one or more depended-upon packages out of the list, it doesn't change the complained-about circularities at all. Or if I try to follow the compiler's suggestions it will give advice either telling me to do what I've already done, or it will go in circles. I.e. it'll tell me change Elaborate_All to Elaborate for a package; on recompile it says remove Elaborate for the same package; on recompile it says change/remove Elaborate_all for the package that isn't even listed anymore! Or the suggestion tells me to put things back to how I had them in the first place.
Now I realize that there are actual unresolvable circularities in the organization of this code, but it must also be the case (surely?) that it is possible to have two packages A and B whose .ADB (body) files call each other, as long as their .ADS (spec) files don't mutually depend upon each other? Taking the previous example of data formats A and B, I pulled out the shared types into new separate spec files that don't have any dependencies. Now it's just the bodies of packages A and B that call procs or functions each from the other. Yet, I still can't make GNAT happy no matter which way I pull them out of the Elaborate_all pragmas or attempt to influence the elaboration order. Why?
The closest thing I found to answering this is "https://groups.google.com/g/comp.lang.ada/c/aRUD89LJIT0". It starts out asking about Parent.Child packages, but the main loop package in my codebase has a lot of child packages, so that's relevant anyway. What that thread seems to be saying is that pragma Elaborate_all is transitive, whereas pragma Elaborate is not transitive. That would seem to make some sense, and could explain why changing just a few pragmas at a time doesn't change the number and content of warning messages I'm receiving (if other, transitive uses of the package are causing its dependencies to be early elaborated anyway).
Although even there there's room for confusion what transitive means: is it transitive only along unbroken chains of Elaborate_all, or does Elaborate_all override Elaborate, transitively? That is, given file A has pragma Elaborate_all(B), file B has Elaborate(C), does A's use of Elaborate_all transitively transmogrify B's "Elaborate(C)" into an "Elaborate_all(C)", or does B's use of plain Elaborate terminate the chain of transitive Elaborate_all's? To borrow terminology from regular expressions, is Elaborate_all greedy or non-greedy?
The other thing in that thread which could help me understand, but due to insufficient detail leaves me feeling even more confused, is that later in the thread they say Elaborate_all is actually the default for with'ed packages (that GNAT is more strict than standard Ada, in this respect). Okay if that's the case it would certainly help explain why removing individual names from Elaborate_all pragmas didn't change the circularity warnings. On the other hand if it was already the default, why did explicitly adding them fix my compiler crashes and get me further towards a full compilation?
They say in that thread that if you don't do anything you get Elaborate_all for your with'ed packages, so you have to explicitly put the package name into a pragma Elaborate to change GNAT's behavior. But, what if you don't want to Elaborate_all *or* Elaborate? Where's the pragma Elaborate_None? There's a pragma Elaborate_Body, but that's the OPPOSITE of what I need; at most I need "pragma Elaborate_Spec".
I have read, "https://gcc.gnu.org/onlinedocs/gnat_ugn/Controlling-the-Elaboration-Order-in-Ada.html" but it still doesn't answer my basic question: what do you do if you have packages A and B that need to call each other, only in their bodies (but not in the spec)? In C/C++ this is trivial: each .c or .cpp file could include the .h (header) file for the other, which provides the function specifications, but does not require each to be compiled before the other. Are you telling me Ada (as GNAT strictly interprets it) can't/won't do that? I understand elaboration order is different from compilation order, and has to do with initialization of static resources, but the problem to be solved, "how do I make the compiler happy when I have two different compilation units which mutually call into each other?" is still the same.
P.S. Some other advice I received was to try compiling smaller subsets of this codebase, fix problems there and accrete packages as I get them working. That's sensible advice that I'd also give myself. Unfortunately I can't see how to implement it here because everything is so inter-connected. If there were pieces without dependencies I could pull out and compile separately, they wouldn't have elaboration order circularities! Bottom-up the most I can pull out is trivial definitions (specs) files - many of them I created myself by pulling common shared types out of circularly dependent packages - but as soon as you get one level removed from that you get into The Hairball where everything depends on everything. Approaching the pulling out of packages top-down, I would have to stub out so many packages and hundreds of methods that it seems not worth the effort. And right in the middle sits this main loop package that has a dozen child packages that's even worse because I'm not sure how to separate a child package from the rest of it and vice-versa.