Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to have a child package as a separate compilation unit in Ada

Tags:

ada

ada95

I have a main package with a normal spec and body file. I am trying to create child packages of the parent, but want them in a separate compilation file(s). I can easily get it done if it is just a package body, or if it is a subprogram/proc/func. However, I can't get it to let me make a child spec file.

The reason I am doing this is because I want to have information in a child that is available to other children of the same parent. I know I can do this by just including the spec portion in the parent, but that is making my parent file pretty big.

Is this even possible, or do I have no choice but to make another root unit? Or just leave everything spec wise in the parent?

I tried:

in parent: package Child1 is separate; (also tried Parent.Child1 but that gave compiles errors

in child:

separate(Parent)

package Parent.Child1 is
....
end Parent.Child1;

Ideas? Just not possible?

Update: I am compiling with Green Hills Multi Compiler. Ada95 language version, non-OO project.

like image 461
Awaken Avatar asked May 03 '13 01:05

Awaken


2 Answers

Noting that you're using the separate keyword I'm going to venture that your question is not about child units, but nested units.

Try the following:

Testing.adb

With
Ada.Text_IO,
Parent;

Procedure Testing is
Begin
    Ada.Text_IO.Put_Line("Starting Test:");

    Parent.Nested.Test_Procedure;

    Ada.Text_IO.Put_Line("Testing complete.");
End Test;

Parent.ads

Package Parent is

    Package Nested is
        Procedure Test_Procedure;
    End Nested;

End Parent;

Parent.adb

Package Body Parent is

    Package Body Nested is separate;

End Parent;

Parent-Nested.adb

(Note: you may have to use something slightly different for the file-name, I'm using GNAT with the default settings for "dot replacement".)

with Ada.Text_IO;

separate (Parent)

package body Nested is

    Procedure Test_Procedure is
    Message : Constant string:= ASCII.HT &
      "Hello from the separate, nested test-procedure.";
    begin
        Ada.Text_IO.Put_Line( Message );
    end Test_Procedure;

End Nested;

You should be able to compile and the output should be three lines as follows:

  1. Starting Test:
  2. Hello from the separate, nested test-procedure.
  3. Testing complete.

The problem here stems from a slight misunderstanding regarding the differences between nested and child packages. Both are accessed with the same method of dot-delimited-qualification: Parent.Nested and Parent.Child.

The subtle difference is that child-packages are always a separately compiled unit (in GNAT they are always in a different file, this is an implementation restriction due to how they [don't] implement the Library.. but some Ada compilers can put different compilation_units into the same file) -- but a nested-package must be compiled at the same time that its enclosing unit is compiled unless it is specifically tagged as separate.


In order to preserve the current nested structure and still use separates you can use the following method with a single Auxiliary package holding all the specs for the packages.

Parent.ads

Package Parent is

    -- Here's the magic of renaming. --'
    Package Nested renames Auxiliary.Delegate;

End Parent;

Auxiliary.ads

Package Auxiliary is

    Package Delegate is
        Procedure Test_Procedure;
    End Delegate;

End Auxiliary;

Auxiliary.adb

package body Auxiliary is

    Package Body Delegate is separate;

end Auxiliary;

Auxiliary-Delegate.adb

(Note: you may have to use something slightly different for the file-name, I'm using GNAT with the default settings for "dot replacement".)

with Ada.Text_IO;

separate (Auxiliary)

package body Delegate is

    Procedure Test_Procedure is
    Message : Constant string:= ASCII.HT &
      "Hello from the separate, nested test-procedure.";
    begin
        Ada.Text_IO.Put_Line( Message );
    end Test_Procedure;

End Delegate;
like image 124
Shark8 Avatar answered Dec 09 '22 03:12

Shark8


Yes, this is totally fine. You can have your parent and child packages in separate files:

parent.ads

package Parent is
-- ...
end Parent;

parent-child.ads

package Parent.Child is
-- ...
end Parent.Child;

parent-other.ads:

limited with Parent.Child; --Need Ada 2005
package Parent.Other is
-- ...
end Parent.Other;

The parent.child package and the parent.other package have access to definitions in parent (with some limitations).

Notice how parent.other "withs" parent.child so that it has access to the definitions in parent.child.

I have an example of how it can be done. Also, here is an example from wikibooks.

like image 35
Anthony Avatar answered Dec 09 '22 04:12

Anthony