home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / lrm / chap10.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  34.4 KB  |  1,454 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. The following document is a draft  of  the  corresponding  chapter  of  the
  7. version  of  the  Ada  Reference  Manual  produced  in response to the Ansi
  8. Canvass.  It is given a limited circulation  to  Ada  implementers  and  to
  9. other groups contributing comments (according to the conventions defined in
  10. RRM.comments).  This draft should not be referred to in any publication.
  11.  
  12.  
  13.  
  14.                        ANSI-RM-10-v23  Draft Chapter
  15.  
  16.                10  Program Structure and Compilation Issues
  17.                                 version 23
  18.  
  19.                                  83-02-11
  20.  
  21. This revision has addressed all comments up to #5795
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.                10. Program Structure and Compilation Issues
  78.  
  79.  
  80.  
  81. The   overall  structure  of  programs  and  the  facilities  for  separate
  82. compilation are described in this chapter.  A program is  a  collection  of
  83. one  or  more  compilation  units  submitted  to  a compiler in one or more
  84. compilations.  Each compilation unit specifies the separate compilation  of
  85. a  construct  which  can  be  a  subprogram  declaration or body, a package
  86. declaration  or  body,  a  generic  declaration  or  body,  or  a   generic
  87. instantiation.   Alternatively  this  construct  can be a subunit, in which
  88. case it includes the body of a subprogram, package, task unit,  or  generic
  89. unit declared within another compilation unit.
  90.  
  91. References:   compilation  10.1,  compilation unit 10.1, generic body 12.2,
  92. generic declaration 12.1, generic instantiation  12.3,  package  body  7.1,
  93. package  declaration  7.1, subprogram body 6.3, subprogram declaration 6.1,
  94. subunit 10.2, task body 9.1, task unit 9
  95.  
  96.  
  97.  
  98. 10.1  Compilation Units - Library Units
  99.  
  100.  
  101. The text of a program can be submitted to  the  compiler  in  one  or  more
  102. compilations.  Each compilation is a succession of compilation units.
  103.  
  104.     compilation ::= {compilation_unit}
  105.  
  106.     compilation_unit ::=
  107.          context_clause library_unit | context_clause secondary_unit
  108.  
  109.     library_unit ::=
  110.          subprogram_declaration | package_declaration
  111.        | generic_declaration    | generic_instantiation
  112.        | subprogram_body
  113.  
  114.     secondary_unit ::= library_unit_body | subunit
  115.  
  116.     library_unit_body ::= subprogram_body | package_body
  117.  
  118. The compilation units of a program are said to belong to a program library.
  119. A  compilation  unit  defines either a library unit or a secondary unit.  A
  120. secondary unit is either the separately compiled proper body of  a  library
  121. unit,  or  a  subunit  of  another  compilation  unit.  The designator of a
  122. separately compiled subprogram (whether a library unit or a  subunit)  must
  123. be an identifier.  Within a program library the simple names of all library
  124. units must be distinct identifiers.
  125.  
  126.  
  127.  
  128.  
  129.                                   10 - 1
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. The effect of compiling a library unit is to define (or redefine) this unit
  139. as one that belongs to the program library.  For the visibility rules, each
  140. library  unit  acts  as  a  declaration  that occurs immediately within the
  141. package STANDARD.
  142.  
  143. The effect of compiling a secondary unit is to define the body of a library
  144. unit, or in the case of a subunit, to define the proper body of  a  program
  145. unit that is declared within another compilation unit.
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                   10 - 2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. A subprogram body given in a compilation unit is interpreted as a secondary
  205. unit  if  the  program  library  already  contains a library unit that is a
  206. subprogram with the same name;  it  is  otherwise  interpreted  both  as  a
  207. library  unit  and  as  the  corresponding library unit body (that is, as a
  208. secondary unit).
  209.  
  210. The compilation units of a compilation are compiled in the given order.   A
  211. pragma  that  applies  to the whole of a compilation must appear before the
  212. first compilation unit of that compilation.
  213.  
  214. A subprogram that is a library unit can be used as a main  program  in  the
  215. usual sense.  Each main program acts as if called by some environment task;
  216. the  means  by  which this execution is initiated are not prescribed by the
  217. language definition.  An implementation may impose certain requirements  on
  218. the  parameters  and  on  the  result,  if  any,  of  a main program (these
  219. requirements  must  be  stated  in  Appendix  F).   In  any   case,   every
  220. implementation  is  required  to  allow,  at  least, main programs that are
  221. parameterless procedures, and every main program must be a subprogram  that
  222. is a library unit.
  223.  
  224. Notes:
  225.  
  226. A  simple  program may consist of a single compilation unit.  A compilation
  227. need not have any compilation units;  for example, its text can consist  of
  228. pragmas.
  229.  
  230. The  designator  of  a library function cannot be an operator symbol, but a
  231. renaming declaration  is  allowed  to  rename  a  library  function  as  an
  232. operator.   Two  library  subprograms  must  have distinct simple names and
  233. hence cannot overload  each  other.   However,  renaming  declarations  are
  234. allowed  to  define  overloaded  names  for such subprograms, and a locally
  235. declared subprogram is allowed  to  overload  a  library  subprogram.   The
  236. expanded  name STANDARD.L can be used for a library unit L (unless the name
  237. STANDARD is hidden) since library units  act  as  declarations  that  occur
  238. immediately within the package STANDARD.
  239.  
  240. References:   allow 1.6, context clause 10.1.1, declaration 3.1, designator
  241. 6.1, environment 10.4,  generic  declaration  12.1,  generic  instantiation
  242. 12.3, hiding 8.3, identifier 2.3, library unit 10.5, local declaration 8.1,
  243. must  1.6,  name  4.1, occur immediately within 8.1, operator 4.5, operator
  244. symbol 6.1, overloading 6.6 8.7, package body 7.1, package declaration 7.1,
  245. parameter of a subprogram 6.2, pragma 2.8, procedure 6.1, program  unit  6,
  246. proper  body  3.9,  renaming  declaration  8.5,  simple  name 4.1, standard
  247. package 8.6, subprogram 6, subprogram body 6.3, subprogram declaration 6.1,
  248. subunit 10.2, task 9, visibility 8.3
  249.  
  250.  
  251.  
  252.  
  253. 10.1.1  Context Clauses - With Clauses
  254.  
  255.  
  256. A context clause is used to specify  the  library  units  whose  names  are
  257. needed within a compilation unit.
  258.  
  259.  
  260.  
  261.                                   10 - 3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.     context_clause ::= {with_clause {use_clause}}
  271.  
  272.     with_clause ::= with unit_simple_name {, unit_simple_name};
  273.  
  274. The  names  that  appear  in  a  context clause must be the simple names of
  275. library units.  The simple name of any library unit  is  allowed  within  a
  276. with  clause.   The  only names allowed in a use clause of a context clause
  277. are the simple names of library packages mentioned by previous with clauses
  278. of the context clause.  A simple name declared by a renaming declaration is
  279. not allowed in a context clause.
  280.  
  281. The with clauses and use clauses of the context clause of  a  library  unit
  282. apply  to this library unit and also to the secondary unit that defines the
  283. corresponding body (whether such a clause  is  repeated  or  not  for  this
  284. unit).   Similarly,  the with clauses and use clauses of the context clause
  285. of a compilation unit apply to this unit and also to its subunits, if  any.
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                   10 - 4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. If  a  library unit is named by a with clause that applies to a compilation
  337. unit, then this library unit is directly  visible  within  the  compilation
  338. unit,  except  where  hidden;   the  library unit is visible as if declared
  339. immediately within the package STANDARD (see 8.6).
  340.  
  341. Dependences among compilation units are defined by with clauses;  that  is,
  342. a  compilation  unit  that mentions other library units in its with clauses
  343. depends on those library units.  These dependences between units are  taken
  344. into account for the determination of the allowed order of compilation (and
  345. recompilation)  of compilation units, as explained in section 10.3, and for
  346. the determination of the allowed order of elaboration of compilation units,
  347. as explained in section 10.5.
  348.  
  349. Notes:
  350.  
  351. A library unit named by a with clause of  a  compilation  unit  is  visible
  352. (except  where hidden) within the compilation unit and hence can be used as
  353. a corresponding program unit.  Thus within the compilation unit,  the  name
  354. of  a  library  package can be given in use clauses and can be used to form
  355. expanded names;  a library subprogram can be called;  and  instances  of  a
  356. library generic unit can be declared.
  357.  
  358. The  rules given for with clauses are such that the same effect is obtained
  359. whether the name of a library unit is mentioned once or more than  once  by
  360. the applicable with clauses, or even within a given with clause.
  361.  
  362. Example 1 : A main program:
  363.  
  364. The  following  is  an  example  of  a  main program consisting of a single
  365. compilation unit:  a procedure for printing the real roots of  a  quadratic
  366. equation.   The  predefined  package  TEXT_IO  and  a  user-defined package
  367. REAL_OPERATIONS (containing the definition of the  type  REAL  and  of  the
  368. packages  REAL_IO  and REAL_FUNCTIONS) are assumed to be already present in
  369. the program library.  Such packages may be used by other main programs.
  370.  
  371.     with TEXT_IO, REAL_OPERATIONS; use REAL_OPERATIONS;
  372.     procedure QUADRATIC_EQUATION is
  373.        A, B, C, D : REAL;
  374.        use REAL_IO,         --  achieves direct visibility of GET and PUT for REAL
  375.            TEXT_IO,         --  achieves direct visibility of PUT for strings and of NEW_LINE
  376.            REAL_FUNCTIONS;  --  achieves direct visibility of SQRT
  377.     begin
  378.        GET(A); GET(B); GET(C);
  379.        D := B**2 - 4.0*A*C;
  380.        if D < 0.0 then
  381.           PUT("Imaginary Roots.");
  382.        else
  383.           PUT("Real Roots : X1 = ");
  384.           PUT((-B - SQRT(D))/(2.0*A)); PUT(" X2 = ");
  385.           PUT((-B + SQRT(D))/(2.0*A));
  386.        end if;
  387.        NEW_LINE;
  388.     end QUADRATIC_EQUATION;
  389.  
  390.  
  391.  
  392.  
  393.                                   10 - 5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. Notes on the example:
  403.  
  404. The with clauses of a compilation unit need only mention the names of those
  405. library subprograms and packages whose  visibility  is  actually  necessary
  406. within  the  unit.   They  need  not (and should not) mention other library
  407. units that are used in turn by some of the units named in the with clauses,
  408. unless these other library units are also  used  directly  by  the  current
  409. compilation unit.  For example, the body of the package REAL_OPERATIONS may
  410. need elementary operations provided by other packages.  The latter packages
  411. should  not  be  named by the with clause of QUADRATIC_EQUATION since these
  412. elementary operations are not directly called within its body.
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                   10 - 6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468. References:  allow 1.6,  compilation  unit  10.1,  direct  visibility  8.3,
  469. elaboration 3.9, generic body 12.2, generic unit 12.1, hiding 8.3, instance
  470. 12.3,  library unit 10.1, main program 10.1, must 1.6, name 4.1, package 7,
  471. package body 7.1, package declaration 7.1, procedure 6.1, program  unit  6,
  472. secondary  unit  10.1,  simple  name  4.1, standard predefined package 8.6,
  473. subprogram body 6.3, subprogram declaration 6.1, subunit  10.2,  type  3.3,
  474. use clause 8.4, visibility 8.3
  475.  
  476.  
  477.  
  478. 10.1.2  Examples of Compilation Units
  479.  
  480.  
  481. A  compilation  unit  can be split into a number of compilation units.  For
  482. example, consider the following program.
  483.  
  484.     procedure PROCESSOR is
  485.  
  486.        SMALL : constant := 20;
  487.        TOTAL : INTEGER  := 0;
  488.  
  489.        package STOCK is
  490.           LIMIT : constant := 1000;
  491.           TABLE : array (1 .. LIMIT) of INTEGER;
  492.           procedure RESTART;
  493.        end STOCK;
  494.  
  495.        package body STOCK is
  496.           procedure RESTART is
  497.           begin
  498.              for N in 1 .. LIMIT loop
  499.                 TABLE(N) := N;
  500.              end loop;
  501.           end;
  502.        begin
  503.           RESTART;
  504.        end STOCK;
  505.  
  506.        procedure UPDATE(X : INTEGER) is
  507.           use STOCK;
  508.        begin
  509.           ...
  510.           TABLE(X) := TABLE(X) + SMALL;
  511.           ...
  512.        end UPDATE;
  513.  
  514.     begin
  515.        ...
  516.        STOCK.RESTART;  -- reinitializes TABLE
  517.        ...
  518.     end PROCESSOR;
  519.  
  520. The following three compilation units  define  a  program  with  an  effect
  521. equivalent to the above example (the broken lines between compilation units
  522. serve  to remind the reader that these units need not be contiguous texts).
  523.  
  524.  
  525.                                   10 - 7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534. Example 2 : Several compilation units:
  535.  
  536.  
  537.     package STOCK is
  538.        LIMIT : constant := 1000;
  539.        TABLE : array (1 .. LIMIT) of INTEGER;
  540.        procedure RESTART;
  541.     end STOCK;
  542.  
  543.     -------------------------------------------------
  544.  
  545.     package body STOCK is
  546.        procedure RESTART is
  547.        begin
  548.           for N in 1 .. LIMIT loop
  549.              TABLE(N) := N;
  550.           end loop;
  551.        end;
  552.     begin
  553.        RESTART;
  554.     end STOCK;
  555.  
  556.     -------------------------------------------------
  557.  
  558.     with STOCK;
  559.     procedure PROCESSOR is
  560.  
  561.        SMALL : constant := 20;
  562.        TOTAL : INTEGER  := 0;
  563.  
  564.        procedure UPDATE(X : INTEGER) is
  565.           use STOCK;
  566.        begin
  567.           ...
  568.           TABLE(X) := TABLE(X) + SMALL;
  569.           ...
  570.        end UPDATE;
  571.     begin
  572.        ...
  573.        STOCK.RESTART;  --  reinitializes TABLE
  574.        ...
  575.     end PROCESSOR;
  576.  
  577. Note that in the latter version, the package STOCK  has  no  visibility  of
  578. outer  identifiers  other  than  the predefined identifiers (of the package
  579. STANDARD).  In particular, STOCK does not use any  identifier  declared  in
  580. PROCESSOR  such  as  SMALL  or  TOTAL;  otherwise STOCK could not have been
  581. extracted from PROCESSOR in the above manner.  The procedure PROCESSOR,  on
  582. the  other  hand,  depends  on  STOCK  and  mentions this package in a with
  583. clause.  This permits the inner occurrences of STOCK in the  expanded  name
  584. STOCK.RESTART and in the use clause.
  585.  
  586. These three compilation units can be submitted in one or more compilations.
  587. For  example,  it  is  possible to submit the package specification and the
  588. package body together and in this order in a single compilation.
  589.  
  590.  
  591.                                   10 - 8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600. References:   compilation  unit  10.1,  declaration  3.1,  identifier  2.3,
  601. package  7,  package  body  7.1,  package  specification  7.1,  program 10,
  602. standard package 8.6, use clause 8.4, visibility 8.3, with clause 10.1.1
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                   10 - 9
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666. 10.2  Subunits of Compilation Units
  667.  
  668.  
  669. A subunit is used for the separate compilation of  the  proper  body  of  a
  670. program  unit  declared  within  another  compilation unit.  This method of
  671. splitting a program permits hierarchical program development.
  672.  
  673.     body_stub ::=
  674.          subprogram_specification is separate;
  675.        | package body package_simple_name is separate;
  676.        | task body task_simple_name is separate;
  677.  
  678.     subunit ::=
  679.          separate (parent_unit_name) proper_body
  680.  
  681. A body stub is only allowed as the body of a program unit (a subprogram,  a
  682. package,  a  task  unit,  or  a  generic  unit)  if  the  body  stub occurs
  683. immediately within either the specification of a  library  package  or  the
  684. declarative part of another compilation unit.
  685.  
  686. If the body of a program unit is a body stub, a separately compiled subunit
  687. containing  the  corresponding  proper  body is required.  In the case of a
  688. subprogram, the subprogram specifications given in the proper body  and  in
  689. the body stub must conform (see 6.3.1).
  690.  
  691. Each subunit mentions the name of its parent unit, that is, the compilation
  692. unit  where  the corresponding body stub is given.  If the parent unit is a
  693. library unit, it is called the ancestor library unit.  If the  parent  unit
  694. is  itself  a  subunit,  the  parent  unit name must be given in full as an
  695. expanded name, starting with the simple name of the ancestor library  unit.
  696. The  simple  names of all subunits that have the same ancestor library unit
  697. must be distinct identifiers.
  698.  
  699. Visibility within the proper body of a subunit is the visibility that would
  700. be obtained at the place of the corresponding body stub (within the  parent
  701. unit)  if  the with clauses and use clauses of the subunit were appended to
  702. the context clause of the parent unit.  If the  parent  unit  is  itself  a
  703. subunit,  then  the  same  rule is used to define the visibility within the
  704. proper body of the parent unit.
  705.  
  706. The effect of the elaboration of a body stub is  to  elaborate  the  proper
  707. body of the subunit.
  708.  
  709. Notes:
  710.  
  711. Two  subunits  of  different library units in the same program library need
  712. not have distinct identifiers.  In any case, their full expanded names  are
  713. distinct,  since  the  simple names of library units are distinct and since
  714. the simple names of all subunits that have a given library unit as ancestor
  715. unit are also distinct.  By  means  of  renaming  declarations,  overloaded
  716. subprogram names that rename (distinct) subunits can be introduced.
  717.  
  718. A  library unit that is named by the with clause of a subunit can be hidden
  719. by a declaration (with the same identifier) given in the proper body of the
  720. subunit.  Moreover, such a library unit can even be hidden by a declaration
  721.  
  722.  
  723.                                   10 - 10
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732. given within a parent unit since a library unit  acts  as  if  declared  in
  733. STANDARD;   this  however  does  not  affect the interpretation of the with
  734. clauses themselves, since only names of library units can  appear  in  with
  735. clauses.
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                   10 - 11
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798. References:   compilation  unit 10.1, conform 6.3.1, context clause 10.1.1,
  799. declaration 3.1, declarative part 3.9, direct visibility  8.3,  elaboration
  800. 3.9,  expanded  name  4.1.3,  generic  body  12.2,  generic unit 12, hidden
  801. declaration 8.3, identifier 2.3, library unit 10.1, local declaration  8.1,
  802. name 4.1, occur immediately within 8.1, overloading 8.3, package 7, package
  803. body  7.1,  package  specification  7.1, program 10, program unit 6, proper
  804. body 3.9, renaming declaration 8.5, separate compilation 10.1, simple  name
  805. 4.1,  subprogram 6, subprogram body 6.3, subprogram specification 6.1, task
  806. 9, task body 9.1, task unit 9.1,  use  clause  8.4,  visibility  8.3,  with
  807. clause 10.1.1
  808.  
  809.  
  810.  
  811. 10.2.1  Examples of Subunits
  812.  
  813.  
  814. The  procedure TOP is first written as a compilation unit without subunits.
  815.  
  816.     with TEXT_IO;
  817.     procedure TOP is
  818.  
  819.        type REAL is digits 10;
  820.        R, S : REAL := 1.0;
  821.  
  822.        package FACILITY is
  823.           PI : constant := 3.14159_26536;
  824.           function  F(X : REAL) return REAL;
  825.           procedure G(Y, Z : REAL);
  826.        end FACILITY;
  827.  
  828.        package body FACILITY is
  829.           --  some local declarations followed by
  830.  
  831.           function F(X : REAL) return REAL is
  832.           begin
  833.              --  sequence of statements of F
  834.              ...
  835.           end F;
  836.  
  837.           procedure G(Y, Z : REAL) is
  838.              --  local procedures using TEXT_IO
  839.              ...
  840.           begin
  841.              --  sequence of statements of G
  842.              ...
  843.           end G;
  844.        end FACILITY;
  845.  
  846.        procedure TRANSFORM(U : in out REAL) is
  847.           use FACILITY;
  848.        begin
  849.           U := F(U);
  850.           ...
  851.        end TRANSFORM;
  852.     begin -- TOP
  853.  
  854.  
  855.                                   10 - 12
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.        TRANSFORM(R);
  865.        ...
  866.        FACILITY.G(R, S);
  867.     end TOP;
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.                                   10 - 13
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930. The body of the package FACILITY and that of the procedure TRANSFORM can be
  931. made into separate subunits of TOP.  Similarly, the body of the procedure G
  932. can be made into a subunit of FACILITY as follows.
  933.  
  934. Example 3:
  935.  
  936.     procedure TOP is
  937.  
  938.        type REAL is digits 10;
  939.        R, S : REAL := 1.0;
  940.  
  941.        package FACILITY is
  942.           PI : constant := 3.14159_26536;
  943.           function  F(X : REAL) return REAL;
  944.           procedure G(Y, Z : REAL);
  945.        end FACILITY;
  946.  
  947.        package body FACILITY is separate;                 --  stub of FACILITY
  948.        procedure TRANSFORM(U : in out REAL) is separate;  --  stub of TRANSFORM
  949.  
  950.     begin  --  TOP
  951.        TRANSFORM(R);
  952.        ...
  953.        FACILITY.G(R, S);
  954.     end TOP;
  955.  
  956.     -------------------------------------------------
  957.  
  958.     separate (TOP)
  959.     procedure TRANSFORM(U : in out REAL) is
  960.        use FACILITY;
  961.     begin
  962.        U := F(U);
  963.        ...
  964.     end TRANSFORM;
  965.  
  966.     -------------------------------------------------
  967.  
  968.     separate (TOP)
  969.     package body FACILITY is
  970.        --  some local declarations followed by
  971.  
  972.        function F(X : REAL) return REAL is
  973.        begin
  974.           --  sequence of statements of F
  975.           ...
  976.        end F;
  977.  
  978.        procedure G(Y, Z : REAL) is separate;              -- stub of G
  979.     end FACILITY;
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                   10 - 14
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.     -------------------------------------------------
  998.  
  999.     with TEXT_IO;
  1000.     separate (TOP.FACILITY)  --  full name of FACILITY
  1001.     procedure G(Y, Z : REAL) is
  1002.        --  local procedures using TEXT_IO
  1003.        ...
  1004.     begin
  1005.        --  sequence of statements of G
  1006.        ...
  1007.     end G;
  1008.  
  1009. In the above example TRANSFORM and FACILITY are subunits of TOP, and G is a
  1010. subunit of FACILITY.  The visibility in the split version is the same as in
  1011. the initial version except for one change:   since  TEXT_IO  is  only  used
  1012. within  G,  the  corresponding  with clause is written for G instead of for
  1013. TOP.   Apart  from  this  change,  the  same  identifiers  are  visible  at
  1014. corresponding  program points in the two versions.  For example, all of the
  1015. following  are (directly) visible within the proper body of the subunit  G:
  1016. the  procedure  TOP,  the  type  REAL,  the  variables R and S, the package
  1017. FACILITY and the contained named number PI and subprograms F and G.
  1018.  
  1019. References:  body stub 10.2, compilation unit 10.1, identifier  2.3,  local
  1020. declaration  8.1,  named number 3.2, package 7, package body 7.1, procedure
  1021. 6, procedure body 6.3, proper body 3.9, subprogram 6,  type  3.3,  variable
  1022. 3.2.1, visibility 8.3, with clause 10.1.1
  1023.  
  1024.  
  1025.  
  1026. 10.3  Order of Compilation
  1027.  
  1028.  
  1029. The  rules  defining  the  order  in which units can be compiled are direct
  1030. consequences of the visibility rules and, in particular, of the  fact  that
  1031. any  library  unit that is mentioned by the context clause of a compilation
  1032. unit is visible in the compilation unit.
  1033.  
  1034. A compilation unit must be compiled after all library units  named  by  its
  1035. context clause.  A secondary unit that is a subprogram or package body must
  1036. be  compiled after the corresponding library unit.  Any subunit of a parent
  1037. compilation unit must be compiled after the parent compilation unit.
  1038.  
  1039. If any error is detected while attempting to compile  a  compilation  unit,
  1040. then  the attempted compilation is rejected and it has no effect whatsoever
  1041. on the program library;  the same holds for recompilations (no  compilation
  1042. unit can become obsolete because of such a recompilation).
  1043.  
  1044. The  order in which the compilation units of a program are compiled must be
  1045. consistent with the partial ordering defined by the above rules.
  1046.  
  1047. Similar rules apply for recompilations.  A compilation unit is  potentially
  1048. affected  by  a  change in any library unit named by its context clause.  A
  1049. secondary unit is potentially affected by a  change  in  the  corresponding
  1050. library  unit.   The  subunits of a parent compilation unit are potentially
  1051.  
  1052.  
  1053.                                   10 - 15
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062. affected by a change of the parent compilation unit.  If a compilation unit
  1063. is successfully recompiled, the compilation units potentially  affected  by
  1064. this  change  are obsolete and must be recompiled unless they are no longer
  1065. needed.  An implementation may be able to reduce the compilation  costs  if
  1066. it  can deduce that some of the potentially affected units are not actually
  1067. affected by the change.
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.                                   10 - 16
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128. The subunits of a unit can be recompiled without affecting the unit itself.
  1129. Similarly, changes in a subprogram or package  body  do  not  affect  other
  1130. compilation  units  (apart  from  the  subunits  of  the  body) since these
  1131. compilation  units  only  have  access  to  the   subprogram   or   package
  1132. specification.  An implementation is only allowed to deviate from this rule
  1133. for  inline inclusions, for certain compiler optimizations, and for certain
  1134. implementations of generic program units, as described below.
  1135.  
  1136.   -  If a pragma INLINE is applied to a subprogram declaration given  in  a
  1137.      package  specification,  inline inclusion will only be achieved if the
  1138.      package body is compiled before units calling the subprogram.  In such
  1139.      a case, inline inclusion creates a dependence of the calling  unit  on
  1140.      the  package  body,  and  the compiler  must recognize this dependence
  1141.      when deciding on the need for recompilation.  If  a  calling  unit  is
  1142.      compiled  before  the  package  body, the pragma may be ignored by the
  1143.      compiler for such calls (a  warning  that  inline  inclusion  was  not
  1144.      achieved may be issued).  Similar considerations apply to a separately
  1145.      compiled subprogram for which an INLINE pragma is specified.
  1146.  
  1147.   -  For optimization purposes, an implementation may compile several units
  1148.      of a given compilation in a way that creates further dependences among
  1149.      these   compilation   units.    The  compiler  must  then  take  these
  1150.      dependences into account when deciding on the need for recompilations.
  1151.  
  1152.   -  An implementation may require  that  a  generic  declaration  and  the
  1153.      corresponding proper body be part of the same compilation, whether the
  1154.      generic  unit  is  itself  separately  compiled or is local to another
  1155.      compilation unit.  An implementation may also require that subunits of
  1156.      a generic unit be part of the same compilation.
  1157.  
  1158. Examples of Compilation Order:
  1159.  
  1160. (a)  In example 1 (see 10.1.1):  The procedure QUADRATIC_EQUATION  must  be
  1161.      compiled after the library packages TEXT_IO  and REAL_OPERATIONS since
  1162.      they appear in its with clause.
  1163.  
  1164. (b)  In example 2 (see 10.1.2):  The package body STOCK  must  be  compiled
  1165.      after the corresponding package specification.
  1166.  
  1167. (c)  In example 2 (see 10.1.2):  The specification  of  the  package  STOCK
  1168.      must  be  compiled before the procedure PROCESSOR.  On the other hand,
  1169.      the procedure PROCESSOR can be compiled either  before  or  after  the
  1170.      package body STOCK.
  1171.  
  1172. (d)  In example 3 (see 10.2.1):  The procedure G must be compiled after the
  1173.      package  TEXT_IO  since this package is named by the with clause of G.
  1174.      On the other hand, TEXT_IO can be compiled either before or after TOP.
  1175.  
  1176. (e)  In example 3 (see 10.2.1):  The subunits TRANSFORM and  FACILITY  must
  1177.      be compiled after the main program TOP.  Similarly, the subunit G must
  1178.      be compiled after its parent unit FACILITY.
  1179.  
  1180. Notes:
  1181.  
  1182.  
  1183.  
  1184.  
  1185.                                   10 - 17
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194. For  library  packages,  it  follows  from  the  recompilation rules that a
  1195. package body is made obsolete by the  recompilation  of  the  corresponding
  1196. specification.   If  the  new  package specification is such that a package
  1197. body is not required (that  is,  if  the  package  specification  does  not
  1198. contain  the  declaration  of  a program unit), then the recompilation of a
  1199. body for this package is not required.  In any case, the  obsolete  package
  1200. body  must  not  be  used  and  can  therefore  be deleted from the program
  1201. library.
  1202.  
  1203. References:   compilation  10.1,  compilation  unit  10.1,  context  clause
  1204. 10.1.1,  elaboration  3.9,  generic  body  12.2,  generic declaration 12.1,
  1205. generic unit 12, library  unit  10.1,  local  declaration  8.1,  name  4.1,
  1206. package  7,  package body 7.1, package specification 7.1, parent unit 10.2,
  1207. pragma inline 6.3.2, procedure 6.1, procedure body 6.3,  proper  body  3.9,
  1208. secondary  unit  10.1,  subprogram  body  6.3,  subprogram declaration 6.1,
  1209. subprogram specification 6.1,  subunit  10.2,  type  3.3,  variable  3.2.1,
  1210. visibility 8.3, with clause 10.1.1
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.                                   10 - 18
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260. 10.4  The Program Library
  1261.  
  1262.  
  1263. Compilers are required to enforce the language rules in the same manner for
  1264. a  program  consisting of several compilation units (and subunits) as for a
  1265. program submitted as a single compilation.  Consequently,  a  library  file
  1266. containing information on the compilation units of the program library must
  1267. be  maintained  by the compiler or compiling environment.  This information
  1268. may include symbol tables and other information  pertaining to the order of
  1269. previous compilations.
  1270.  
  1271. A normal submission to the compiler consists of the compilation unit(s) and
  1272. the library file.  The latter is used for checks and is  updated  for  each
  1273. compilation unit successfully compiled.
  1274.  
  1275. Notes:
  1276.  
  1277. A  single  program  library  is  implied  for  the  compilation  units of a
  1278. compilation.  The possible existence of different program libraries and the
  1279. means by which they are named are not concerns of the language  definition;
  1280. they are concerns of the programming environment.
  1281.  
  1282. There  should  be  commands  for  creating  the  program library of a given
  1283. program or of a given family of programs.  These commands  may  permit  the
  1284. reuse  of  units  of  other  program  libraries.   Finally, there should be
  1285. commands for interrogating the status of the units of  a  program  library.
  1286. The form of these commands is not specified by the language definition.
  1287.  
  1288. References:   compilation  unit  10.1,  context  clause  10.1.1,  order  of
  1289. compilation 10.3, program 10.1, program library  10.1,  subunit  10.2,  use
  1290. clause 8.4, with clause 10.1.1
  1291.  
  1292.  
  1293.  
  1294. 10.5  Elaboration of Library Units
  1295.  
  1296.  
  1297. Before  the  execution  of  a main program, all library units needed by the
  1298. main program are elaborated, as well  as  the  corresponding  library  unit
  1299. bodies,  if  any.  The library units needed by the main program are:  those
  1300. named by with clauses applicable to the main program, to its body,  and  to
  1301. its  subunits;   those  named  by  with clauses applicable to these library
  1302. units themselves, to the corresponding library unit bodies,  and  to  their
  1303. subunits;  and so on, in a transitive manner.
  1304.  
  1305. The  elaboration  of  these  library units and of the corresponding library
  1306. unit bodies is performed in an order consistent with the  partial  ordering
  1307. defined  by  the  with  clauses  (see  10.3).   In addition, a library unit
  1308. mentioned by the context clause of a subunit must be elaborated before  the
  1309. body of the ancestor library unit of the subunit.
  1310.  
  1311. An  order of elaboration that is consistent with this partial ordering does
  1312. not always ensure that each library unit  body  is  elaborated  before  any
  1313. other compilation unit whose elaboration necessitates that the library unit
  1314. body  be  already  elaborated.   If  the  prior elaboration of library unit
  1315.  
  1316.  
  1317.                                   10 - 19
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. bodies is needed, this can be requested by a pragma ELABORATE.  The form of
  1327. this pragma is as follows:
  1328.  
  1329.     pragma ELABORATE (library_unit_simple_name {, library_unit_simple_name});
  1330.  
  1331. These pragmas are only allowed immediately after the context  clause  of  a
  1332. compilation  unit  (before  the subsequent library unit or secondary unit).
  1333. Each argument of such a pragma must be the simple name of  a  library  unit
  1334. mentioned  by the context clause, and this library unit must have a library
  1335. unit body.  Such a pragma specifies that the  library  unit  body  must  be
  1336. elaborated  before  the  given  compilation unit.  If the given compilation
  1337. unit is a subunit, the library unit body must be elaborated before the body
  1338. of the ancestor library unit of the subunit.
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.                                   10 - 20
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392. The program is illegal if no consistent order can be found (that is,  if  a
  1393. circularity  exists).   The  elaboration  of  the  compilation units of the
  1394. program is performed in some order that is otherwise  not  defined  by  the
  1395. language.
  1396.  
  1397. References:   allow  1.6,  argument of a pragma 2.8, compilation unit 10.1,
  1398. context  clause  10.1.1,  dependence  between   compilation   units   10.3,
  1399. elaboration  3.9,  illegal  1.6, in some order 1.6, library unit 10.1, name
  1400. 4.1,  main  program  10.1,  pragma  2.8,  secondary  unit  10.1,   separate
  1401. compilation 10.1, simple name 4.1, subunit 10.2, with clause 10.1.1
  1402.  
  1403.  
  1404.  
  1405.  
  1406. 10.6  Program Optimization
  1407.  
  1408.  
  1409. Optimization  of  the  elaboration  of  declarations  and  the execution of
  1410. statements may be performed by compilers.  In particular, a compiler may be
  1411. able to optimize a program by evaluating certain expressions,  in  addition
  1412. to  those  that  are  static expressions.  Should one of these expressions,
  1413. whether static or not, be such that an exception would  be  raised  by  its
  1414. evaluation,  then  the  code in that path of the program can be replaced by
  1415. code to raise the exception;  the same holds for exceptions raised  by  the
  1416. evaluation of names and simple expressions.  (See also section 11.6.)
  1417.  
  1418. A  compiler  may  find  that  some  statements or subprograms will never be
  1419. executed, for example, if their execution depends on a condition  known  to
  1420. be FALSE.  The corresponding object machine code can then be omitted.  This
  1421. rule permits the effect of conditional compilation within the language.
  1422.  
  1423. Note:
  1424.  
  1425. An  expression  whose  evaluation  is  known to raise an exception need not
  1426. represent an error if it occurs in a statement or subprogram that is  never
  1427. executed.  The compiler may warn the programmer of a potential error.
  1428.  
  1429. References:   condition  5.3,  declaration 3.1, elaboration 3.9, evaluation
  1430. 4.5, exception 11, expression 4.4, false boolean value 3.5.3,  program  10,
  1431. raising  of exceptions 11.3, statement 5, static expression 4.9, subprogram
  1432. 6
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.                                   10 - 21
  1450.  
  1451.  
  1452.  
  1453.  
  1454.