home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / lrm / chap12.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  44.3 KB  |  1,982 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-12-v23 - Draft Chapter
  15.  
  16.                              12  Generic Units
  17.                                 version 23
  18.                                  83-02-11
  19.  
  20. This revision has addressed all comments up to number #5795
  21.  
  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.                              12. Generic Units
  78.  
  79.  
  80.  
  81. A generic unit is a program unit that is either a generic subprogram  or  a
  82. generic  package.   A generic unit is a template, which is parameterized or
  83. not, and from which corresponding (nongeneric) subprograms or packages  can
  84. be  obtained.   The resulting program units are said to be instances of the
  85. original generic unit.
  86.  
  87. A generic unit  is  declared  by  a  generic  declaration.   This  form  of
  88. declaration  has  a  generic  formal  part  declaring  any  generic  formal
  89. parameters.  An instance of a generic unit is obtained as the result  of  a
  90. generic  instantiation  with  appropriate generic actual parameters for the
  91. generic formal parameters.  An  instance  of  a  generic  subprogram  is  a
  92. subprogram.  An instance of a generic package is a package.
  93.  
  94. Generic  units are templates.  As templates they do not have the properties
  95. that are specific to their nongeneric counterparts.  For example, a generic
  96. subprogram can be instantiated but it cannot be called.  In  contrast,  the
  97. instance  of  a generic subprogram is a nongeneric subprogram;  hence, this
  98. instance can be called but it cannot be used to produce further  instances.
  99.  
  100. References:   declaration  3.1,  generic  actual  parameter  12.3,  generic
  101. declaration 12.1, generic formal parameter 12.1, generic formal part  12.1,
  102. generic  instantiation 12.3, generic package 12.1, generic subprogram 12.1,
  103. instance 12.3, package 7, program unit 6, subprogram 6
  104.  
  105.  
  106.  
  107.  
  108. 12.1  Generic Declarations
  109.  
  110.  
  111. A generic declaration declares a generic unit, which is  either  a  generic
  112. subprogram  or a generic package.  A generic declaration includes a generic
  113. formal part declaring any generic  formal  parameters.   A  generic  formal
  114. parameter  can  be  an  object;   alternatively  (unlike  a  parameter of a
  115. subprogram), it can be a type or a subprogram.
  116.  
  117.     generic_declaration ::= generic_specification;
  118.  
  119.     generic_specification ::=
  120.          generic_formal_part subprogram_specification
  121.        | generic_formal_part package_specification
  122.  
  123.     generic_formal_part ::= generic {generic_parameter_declaration}
  124.  
  125.     generic_parameter_declaration ::=
  126.          identifier_list : [in [out]] type_mark [:= expression];
  127.  
  128.  
  129.                                   12 - 1
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.        | type identifier is generic_type_definition;
  139.        | private_type_declaration
  140.        | with subprogram_specification [is name];
  141.        | with subprogram_specification [is <>];
  142.  
  143.     generic_type_definition ::=
  144.          (<>) | range <> | digits <> | delta <>
  145.        | array_type_definition | access_type_definition
  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.                                   12 - 2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. The terms generic formal object (or simply, formal object), generic  formal
  205. type  (or  simply,  formal type), and generic formal subprogram (or simply,
  206. formal subprogram) are  used  to  refer  to  corresponding  generic  formal
  207. parameters.
  208.  
  209. The only form of subtype indication allowed within a generic formal part is
  210. a  type  mark (that is, the subtype indication must not include an explicit
  211. constraint).  The designator of a generic subprogram must be an identifier.
  212.  
  213. Outside the specification and body of a generic  unit,  the  name  of  this
  214. program unit denotes the generic unit.  In contrast, within the declarative
  215. region  associated with a generic subprogram, the name of this program unit
  216. denotes the subprogram obtained by the current instantiation of the generic
  217. unit.  Similarly, within the declarative region associated with  a  generic
  218. package,  the name of this program unit denotes the package obtained by the
  219. current instantiation.
  220.  
  221. The elaboration of a generic declaration has no other effect.
  222.  
  223. Examples of generic formal parts:
  224.  
  225.     generic     --  parameterless
  226.  
  227.     generic
  228.        SIZE : NATURAL;  --  formal object
  229.  
  230.     generic
  231.        LENGTH : INTEGER := 200;           -- formal object with a default expression
  232.        AREA   : INTEGER := LENGTH*LENGTH; -- formal object with a default expression
  233.  
  234.     generic
  235.        type ITEM  is private;                       -- formal type
  236.        type INDEX is (<>);                          -- formal type
  237.        type ROW   is array(INDEX range <>) of ITEM; -- formal type
  238.        with function "<"(X, Y : ITEM) return BOOLEAN;    -- formal subprogram
  239.  
  240. Examples of generic declarations declaring generic subprograms:
  241.  
  242.     generic
  243.        type ELEM is private;
  244.     procedure EXCHANGE(U, V : in out ELEM);
  245.  
  246.     generic
  247.        type ITEM is private;
  248.        with function "*"(U, V : ITEM) return ITEM is <>;
  249.     function SQUARING(X : ITEM) return ITEM;
  250.  
  251. Example of a generic declaration declaring a generic package:
  252.  
  253.     generic
  254.        type ITEM   is private;
  255.        type VECTOR is array (POSITIVE range <>) of ITEM;
  256.        with function SUM(X, Y : ITEM) return ITEM;
  257.     package ON_VECTORS is
  258.        function SUM  (A, B : VECTOR) return VECTOR;
  259.  
  260.  
  261.                                   12 - 3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.        function SIGMA(A    : VECTOR) return ITEM;
  271.        LENGTH_ERROR : exception;
  272.     end;
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  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.                                   12 - 4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. Notes:
  337.  
  338. Within a generic subprogram, the name of this program unit acts as the name
  339. of a subprogram.  Hence this name can be overloaded, and it can appear in a
  340. recursive call of the current instantiation.  For  the  same  reason,  this
  341. name  cannot  appear  after  the reserved word new in a (recursive) generic
  342. instantiation.
  343.  
  344. An expression that occurs in a generic formal part is  either  the  default
  345. expression  for  a generic formal object of mode in, or a constituent of an
  346. entry name given as default name for a formal subprogram,  or  the  default
  347. expression for a parameter of a formal subprogram.  Default expressions for
  348. generic  formal  objects  and default names for formal subprograms are only
  349. evaluated for generic  instantiations  that  use  such  defaults.   Default
  350. expressions  for  parameters  of  formal subprograms are only evaluated for
  351. calls of the  formal  subprograms  that  use  such  defaults.   (The  usual
  352. visibility  rules  apply  to  any  name  used in a default expression:  the
  353. denoted entity must therefore be visible at the place of  the  expression.)
  354.  
  355. Neither   generic  formal  parameters  nor  their  attributes  are  allowed
  356. constituents of static expressions (see 4.9).
  357.  
  358. References:   access  type  definition  3.8,  array  type  definition  3.6,
  359. attribute   4.1.4,   constraint   3.3,  declaration  3.1,  designator  6.1,
  360. elaboration has no other effect 3.1, entity 3.1, expression  4.4,  function
  361. 6.5,  generic  instantiation  12.3,  identifier  2.3,  identifier list 3.2,
  362. instance  12.3,  name  4.1,  object  3.2,  overloading  6.6  8.7,   package
  363. specification  7.1,  parameter of a subprogram 6.2, private type definition
  364. 7.4, procedure 6.1, reserved word 2.9, static expression 4.9, subprogram 6,
  365. subprogram specification 6.1, subtype indication 3.3.2, type 3.3, type mark
  366. 3.3.2
  367.  
  368.  
  369.  
  370.  
  371. 12.1.1  Generic Formal Objects
  372.  
  373.  
  374. The first form of generic parameter  declaration  declares  generic  formal
  375. objects.   The type of a generic formal object is the base type of the type
  376. denoted by the type mark given in the  generic  parameter  declaration.   A
  377. generic  parameter  declaration with several identifiers is equivalent to a
  378. sequence of single generic parameter declarations, as explained in  section
  379. 3.2.
  380.  
  381. A  generic  formal  object  has a mode that is either in or in out.  In the
  382. absence of an explicit mode indication in a generic parameter  declaration,
  383. the  mode  in  is  assumed;  otherwise the mode is the one indicated.  If a
  384. generic parameter declaration ends with an expression,  the  expression  is
  385. the  default  expression  of  the  generic  formal  parameter.   A  default
  386. expression is only allowed  if  the  mode  is  in  (whether  this  mode  is
  387. indicated explicitly or implicitly).  The type of a default expression must
  388. be that of the corresponding generic formal parameter.
  389.  
  390.  
  391.  
  392.  
  393.                                   12 - 5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. A  generic  formal object of mode in is a constant whose value is a copy of
  403. the value supplied as the matching generic actual parameter  in  a  generic
  404. instantiation,  as described in section 12.3.  The type of a generic formal
  405. object of mode in must not be a  limited  type;   the  subtype  of  such  a
  406. generic  formal object is the subtype denoted by the type mark given in the
  407. generic parameter declaration.
  408.  
  409. A generic formal object of mode in out is a variable and denotes the object
  410. supplied  as  the  matching  generic  actual   parameter   in   a   generic
  411. instantiation, as described in section 12.3.  The constraints that apply to
  412. the  generic  formal  object  are those of the corresponding generic actual
  413. parameter.
  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.                                   12 - 6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468. Note:
  469.  
  470. The constraints that apply to a generic formal object of mode  in  out  are
  471. those  of  the corresponding generic actual parameter (not those implied by
  472. the type mark that appears in the generic parameter declaration).  Whenever
  473. possible (to avoid confusion) it is recommended that the  name  of  a  base
  474. type be used for the declaration of such a formal object.  If, however, the
  475. base  type is anonymous, it is recommended that the subtype name defined by
  476. the type declaration for the base type be used.
  477.  
  478. References:  anonymous type 3.3.1, assignment 5.2, base type 3.3,  constant
  479. declaration  3.2, constraint 3.3, declaration 3.1, generic actual parameter
  480. 12.3, generic formal object 12.1, generic formal  parameter  12.1,  generic
  481. instantiation  12.3,  generic  parameter  declaration 12.1, identifier 2.3,
  482. limited type 7.4.4, matching generic actual parameter 12.3, mode 6.1,  name
  483. 4.1,  object  3.2, simple name 4.1, subtype 3.3, type declaration 3.3, type
  484. mark 3.3.2, variable 3.2.1
  485.  
  486.  
  487.  
  488.  
  489. 12.1.2  Generic Formal Types
  490.  
  491.  
  492. A generic parameter declaration that includes a generic type definition  or
  493. a  private  type  declaration  declares  a  generic formal type.  A generic
  494. formal type denotes  the  subtype  supplied  as  the  corresponding  actual
  495. parameter  in  a  generic instantiation, as described in 12.3(d).  However,
  496. within a generic unit,  a  generic  formal  type  is  considered  as  being
  497. distinct  from  all  other  (formal  or  nonformal)  types.   The  form  of
  498. constraint applicable to a formal type in a subtype indication  depends  on
  499. the class of the type as for a nonformal type.
  500.  
  501. The only form of discrete range that is allowed within the declaration of a
  502. generic formal (constrained) array type is a type mark.
  503.  
  504. The  discriminant  part of a generic formal private type must not include a
  505. default expression for a discriminant.  (Consequently, a variable  that  is
  506. declared  by  an  object  declaration  must be constrained if its type is a
  507. generic formal type with discriminants.)
  508.  
  509. Within the declaration and body of a generic unit, the operations available
  510. for values of a generic formal type (apart from  any  additional  operation
  511. specified  by  a  generic  formal subprogram) are determined by the generic
  512. parameter declaration for the formal type:
  513.  
  514. (a)  For a private type declaration, the  available  operations  are  those
  515.      defined  in  section  7.4.2  (in particular, assignment, equality, and
  516.      inequality are available for a private type unless it is limited).
  517.  
  518. (b)  For an array type  definition,  the  available  operations  are  those
  519.      defined  in  section 3.6.2 (for example, they include the formation of
  520.      indexed components and slices).
  521.  
  522.  
  523.  
  524.  
  525.                                   12 - 7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534. (c)  For an access type definition,  the  available  operations  are  those
  535.      defined in section 3.8.2 (for example, allocators can be used).
  536.  
  537. The  four forms of generic type definition in which a box appears (that is,
  538. the compound delimiter <>) correspond  to  the  following  major  forms  of
  539. scalar type:
  540.  
  541. (d)  Discrete types:  (<>)
  542.  
  543.      The  available operations are the operations common to enumeration and
  544.      integer types; these are defined in section 3.5.5.
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                   12 - 8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600. (e)  Integer types:  range <>
  601.  
  602.      The available operations are the operations of integer  types  defined
  603.      in section 3.5.5.
  604.  
  605. (f)  Floating point types:  digits <>
  606.  
  607.      The available operations are those defined in section 3.5.8.
  608.  
  609. (g)  Fixed point types:  delta <>
  610.  
  611.      The available operations are those defined in section 3.5.10.
  612.  
  613.  
  614. In  all  of  the  above  cases  (a)  through (f), each operation implicitly
  615. associated with a formal type (that is, other than an  operation  specified
  616. by  a  formal  subprogram)  is  implicitly  declared  at  the  place of the
  617. declaration of the formal type.  The same holds for a  formal  fixed  point
  618. type,  except  for  the  multiplying operators that deliver a result of the
  619. type  universal_fixed  (see  4.5.5),  since  these  special  operators  are
  620. declared in the package STANDARD.
  621.  
  622. For  an  instantiation of the generic unit, each of these operations is the
  623. corresponding basic operation or predefined operator of the matching actual
  624. type.  For an operator, this rule applies even if  the  operator  has  been
  625. redefined  for  the actual type or for some parent type of the actual type.
  626.  
  627.  
  628. Examples of generic formal types:
  629.  
  630.     type ITEM is private;
  631.     type BUFFER(LENGTH : NATURAL) is limited private;
  632.  
  633.     type ENUM  is (<>);
  634.     type INT   is range <>;
  635.     type ANGLE is delta <>;
  636.     type MASS  is digits <>;
  637.  
  638.     type TABLE is array (ENUM) of ITEM;
  639.  
  640.  
  641. Example of a generic formal part declaring a formal integer type:
  642.  
  643.     generic
  644.        type RANK is range <>;
  645.        FIRST  : RANK := RANK'FIRST;
  646.        SECOND : RANK := FIRST + 1;  --  the operator "+" of the type RANK
  647.  
  648.  
  649. References:   access  type  definition  3.8,  allocator  4.8,  array   type
  650. definition  3.6, assignment 5.2, body of a generic unit 12.2, class of type
  651. 3.3, constraint 3.3, declaration 3.1, declaration of a generic  unit  12.1,
  652. discrete range 3.6, discrete type 3.5, discriminant part 3.7.1, enumeration
  653. type  3.5.1,  equality  4.5.2,  fixed point type 3.5.9, floating point type
  654. 3.5.7, generic actual type 12.3, generic formal part 12.1,  generic  formal
  655.  
  656.  
  657.                                   12 - 9
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666. subprogram  12.1.3, generic formal type 12.1, generic parameter declaration
  667. 12.1, generic type definition 12.1,  indexed  component  4.1.1,  inequality
  668. 4.5.2,  instantiation 12.3, integer type 3.5.4, limited private type 7.4.4,
  669. matching generic actual  type  12.3.2  12.3.3  12.3.4  12.3.5,  multiplying
  670. operator  4.5  4.5.5, operation 3.3, operator 4.5, parent type 3.4, private
  671. type definition 7.4, scalar type 3.5, slice 4.1.2, standard package 8.6  C,
  672. subtype indication 3.3.2, type mark 3.3.2, universal_fixed 3.5.9
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                   12 - 10
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732. 12.1.3  Generic Formal Subprograms
  733.  
  734.  
  735. A  generic  parameter  declaration that includes a subprogram specification
  736. declares a generic formal subprogram.
  737.  
  738. Two alternative forms of defaults can be specified in the declaration of  a
  739. generic formal subprogram.  In these forms, the subprogram specification is
  740. followed  by  the  reserved  word  is  and  either  a  box or the name of a
  741. subprogram or entry.  The matching rules for these defaults  are  explained
  742. in section 12.3.6.
  743.  
  744. A generic formal subprogram denotes the subprogram, enumeration literal, or
  745. entry  supplied  as the corresponding generic actual parameter in a generic
  746. instantiation, as described in section 12.3(f).
  747.  
  748. Examples of generic formal subprograms:
  749.  
  750.     with function INCREASE(X : INTEGER) return INTEGER;
  751.     with function SUM(X, Y : ITEM) return ITEM;
  752.  
  753.     with function "+"(X, Y : ITEM) return ITEM is <>;
  754.     with function IMAGE(X : ENUM) return STRING is ENUM'IMAGE;
  755.  
  756.     with procedure UPDATE is DEFAULT_UPDATE;
  757.  
  758. Notes:
  759.  
  760. The constraints that apply to a parameter of a formal subprogram are  those
  761. of  the corresponding parameter in the specification of the matching actual
  762. subprogram (not those  implied  by  the  corresponding  type  mark  in  the
  763. specification  of  the formal subprogram).  A similar remark applies to the
  764. result of a function.   Whenever  possible  (to  avoid  confusion),  it  is
  765. recommended  that the name of a base type be used rather than the name of a
  766. subtype in any declaration of a formal subprogram.  If, however,  the  base
  767. type  is  anonymous, it is recommended that the subtype name defined by the
  768. type declaration be used.
  769.  
  770. The type specified for a formal parameter of a  generic  formal  subprogram
  771. can  be  any  visible  type,  including  a  generic formal type of the same
  772. generic formal part.
  773.  
  774. References:  anonymous type 3.3.1, base type  3.3,  box  delimiter  12.1.2,
  775. constraint  3.3,  designator  6.1,  generic  actual parameter 12.3, generic
  776. formal function 12.1, generic formal subprogram 12.1, generic instantiation
  777. 12.3, generic parameter declaration 12.1, identifier 2.3, matching  generic
  778. actual  subprogram  12.3.6,  operator symbol 6.1, parameter of a subprogram
  779. 6.2, renaming declaration 8.5, reserved word 2.9, scope 8.2, subprogram  6,
  780. subprogram specification 6.1, subtype 3.3.2, type 3.3, type mark 3.3.2
  781.  
  782.  
  783.  
  784.  
  785. 12.2  Generic Bodies
  786.  
  787.  
  788.  
  789.                                   12 - 11
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798. The  body  of a generic subprogram or generic package is a template for the
  799. bodies of the corresponding subprograms or  packages  obtained  by  generic
  800. instantiations.   The  syntax  of  a generic body is identical to that of a
  801. nongeneric body.
  802.  
  803. For each declaration of a generic subprogram, there must be a corresponding
  804. body.
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.                                   12 - 12
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864. The elaboration of a generic body has no other  effect  than  to  establish
  865. that  the  body  can from then on be used as the template for obtaining the
  866. corresponding instances.
  867.  
  868.  
  869. Example of a generic procedure body:
  870.  
  871.     procedure EXCHANGE(U, V : in out ELEM) is  --  see example in 12.1
  872.        T : ELEM;  --  the generic formal type
  873.     begin
  874.        T := U;
  875.        U := V;
  876.        V := T;
  877.     end EXCHANGE;
  878.  
  879.  
  880. Example of a generic function body:
  881.  
  882.     function SQUARING(X : ITEM) return ITEM is  --  see example in 12.1
  883.     begin
  884.        return X*X;  --  the formal operator "*"
  885.     end;
  886.  
  887.  
  888. Example of a generic package body:
  889.  
  890.     package body ON_VECTORS is  --  see example in 12.1
  891.  
  892.        function SUM(A, B : VECTOR) return VECTOR is
  893.           RESULT : VECTOR(A'RANGE);     --  the formal type VECTOR
  894.           BIAS   : constant INTEGER := B'FIRST - A'FIRST;
  895.        begin
  896.           if A'LENGTH /= B'LENGTH then
  897.              raise LENGTH_ERROR;
  898.           end if;
  899.  
  900.           for N in A'RANGE loop
  901.              RESULT(N) := SUM(A(N), B(N + BIAS));    --  the formal function SUM
  902.           end loop;
  903.           return RESULT;
  904.        end;
  905.  
  906.        function SIGMA(A : VECTOR) return ITEM is
  907.           TOTAL : ITEM := A(A'FIRST);                --  the formal type ITEM
  908.        begin
  909.           for N in A'FIRST + 1 .. A'LAST loop
  910.              TOTAL := SUM(TOTAL, A(N));              --  the formal function SUM
  911.           end loop;
  912.           return TOTAL;
  913.        end;
  914.     end;
  915.  
  916.  
  917. References:   body  3.9,  elaboration  3.9,  generic  body  12.1,   generic
  918. instantiation 12.3, generic package 12.1, generic subprogram 12.1, instance
  919.  
  920.  
  921.                                   12 - 13
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930. 12.3, package body 7.1, package 7, subprogram 6, subprogram body 6.3
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                   12 - 14
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996. 12.3  Generic Instantiation
  997.  
  998.  
  999. An instance of a generic unit is declared by a generic instantiation.
  1000.  
  1001.     generic_instantiation ::=
  1002.          package identifier is
  1003.              new generic_package_name [generic_actual_part];
  1004.        | procedure identifier is
  1005.              new generic_procedure_name [generic_actual_part];
  1006.        | function designator is
  1007.              new generic_function_name [generic_actual_part];
  1008.  
  1009.     generic_actual_part ::=
  1010.        (generic_association {, generic_association})
  1011.  
  1012.     generic_association ::=
  1013.        [generic_formal_parameter =>] generic_actual_parameter
  1014.  
  1015.     generic_formal_parameter ::= parameter_simple_name | operator_symbol
  1016.  
  1017.     generic_actual_parameter ::= expression | variable_name
  1018.        | subprogram_name | entry_name | type_mark
  1019.  
  1020. An  explicit  generic  actual  parameter  must be supplied for each generic
  1021. formal parameter, unless the corresponding  generic  parameter  declaration
  1022. specifies  that  a default can be used.  Generic associations can be either
  1023. positional or named  in  the  same  manner  as  parameter  associations  of
  1024. subprogram  calls  (see  6.4).   If two or more formal subprograms have the
  1025. same  designator,  then  named  associations  are  not  allowed   for   the
  1026. corresponding generic parameters.
  1027.  
  1028. Each  generic  actual parameter must match the corresponding generic formal
  1029. parameter.  An expression can match a formal object of mode in;  a variable
  1030. name can match a formal object of mode in out;  a  subprogram  name  or  an
  1031. entry  name  can match a formal subprogram;  a type mark can match a formal
  1032. type.  The detailed  rules  defining  the  allowed  matches  are  given  in
  1033. sections 12.3.1 to 12.3.6;  these are the only allowed matches.
  1034.  
  1035. The  instance  is a copy of the generic unit, apart from the generic formal
  1036. part;  thus the instance of a generic package  is  a  package,  that  of  a
  1037. generic  procedure  is  a  procedure,  and  that of a generic function is a
  1038. function.  For each  occurrence, within the generic unit, of  a  name  that
  1039. denotes  a given entity, the following list defines which entity is denoted
  1040. by the corresponding occurrence within the instance.
  1041.  
  1042. (a)  For a  name  that  denotes  the  generic  unit:     The  corresponding
  1043.      occurrence denotes the instance.
  1044.  
  1045. (b)  For a name that denotes a generic formal  object  of  mode  in:    The
  1046.      corresponding  name  denotes  a  constant whose value is a copy of the
  1047.      value of the associated generic actual parameter.
  1048.  
  1049. (c)  For a name that denotes a generic formal object of mode in out:    The
  1050.      corresponding  name  denotes  the  variable  named  by  the associated
  1051.  
  1052.  
  1053.                                   12 - 15
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.      generic actual parameter.
  1063.  
  1064. (d)  For a name that denotes a generic  formal  type:    The  corresponding
  1065.      name  denotes  the  subtype  named  by  the  associated generic actual
  1066.      parameter (the actual subtype).
  1067.  
  1068. (e)  For a name that denotes a discriminant of a generic formal type:   The
  1069.      corresponding name denotes the corresponding discriminant (there  must
  1070.      be one) of the actual type associated with the generic formal type.
  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.                                   12 - 16
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128. (f)  For  a  name  that  denotes  a  generic  formal   subprogram:      The
  1129.      corresponding  name  denotes  the  subprogram, enumeration literal, or
  1130.      entry named by the associated generic  actual  parameter  (the  actual
  1131.      subprogram).
  1132.  
  1133. (g)  For a name that  denotes  a  formal  parameter  of  a  generic  formal
  1134.      subprogram:    The corresponding name denotes the corresponding formal
  1135.      parameter  of  the  actual  subprogram  associated  with  the   formal
  1136.      subprogram.
  1137.  
  1138. (h)  For a name that denotes a local entity  declared  within  the  generic
  1139.      unit:    The  corresponding  name  denotes  the entity declared by the
  1140.      corresponding local declaration within the instance.
  1141.  
  1142. (i)  For a name that denotes  a  global  entity  declared  outside  of  the
  1143.      generic unit:   The corresponding name denotes the same global entity.
  1144.  
  1145. Similar  rules  apply  to  operators  and basic operations:  in particular,
  1146. formal operators follow a rule similar to rule (f), local operations follow
  1147. a rule similar to rule (h), and operations for global types follow  a  rule
  1148. similar  to rule (i).  In addition, if within the generic unit a predefined
  1149. operator or basic operation of a formal  type  is  used,  then  within  the
  1150. instance   the   corresponding   occurrence  refers  to  the  corresponding
  1151. predefined operation of the actual type associated with the formal type.
  1152.  
  1153. The above rules apply also to any type mark or (default)  expression  given
  1154. within the generic formal part of the generic unit.
  1155.  
  1156. For the elaboration of a generic instantiation, each expression supplied as
  1157. an  explicit  generic  actual parameter is first evaluated, as well as each
  1158. expression that appears as a constituent of a variable name or  entry  name
  1159. supplied  as  an  explicit  generic  actual  parameter;   these evaluations
  1160. proceed in some order that is not defined by the language.  Then, for  each
  1161. omitted  generic association (if any), the corresponding default expression
  1162. or default name is evaluated;  such evaluations are performed in the  order
  1163. of  the  generic parameter declarations.  Finally, the implicitly generated
  1164. instance is elaborated.  The elaboration of  a  generic  instantiation  may
  1165. also  involve  certain constraint checks as described in later subsections.
  1166.  
  1167. Recursive generic instantiation is not allowed in the following sense:   if
  1168. a  given  generic  unit includes an instantiation of a second generic unit,
  1169. then the instance generated by  this  instantiation  must  not  include  an
  1170. instance  of  the  first  generic  unit (whether this instance is generated
  1171. directly, or indirectly by intermediate instantiations).
  1172.  
  1173. Examples of generic instantiations (see 12.1):
  1174.  
  1175.     procedure SWAP is new EXCHANGE(ELEM => INTEGER);
  1176.     procedure SWAP is new EXCHANGE(CHARACTER);  --  SWAP is overloaded
  1177.  
  1178.     function SQUARE is new SQUARING(INTEGER);  --  "*" of INTEGER used by default
  1179.     function SQUARE is new SQUARING(ITEM => MATRIX, "*" => MATRIX_PRODUCT);
  1180.     function SQUARE is new SQUARING(MATRIX, MATRIX_PRODUCT); -- same as previous
  1181.  
  1182.  
  1183.  
  1184.  
  1185.                                   12 - 17
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.     package INT_VECTORS is new ON_VECTORS(INTEGER, TABLE, "+");
  1195.  
  1196. Examples of uses of instantiated units:
  1197.  
  1198.     SWAP(A, B);
  1199.     A := SQUARE(A);
  1200.  
  1201.     T : TABLE(1 .. 5) := (10, 20, 30, 40, 50);
  1202.     N : INTEGER := INT_VECTORS.SIGMA(T);  --  150 (see 12.2 for the body of SIGMA)
  1203.  
  1204.     use INT_VECTORS;
  1205.     M : INTEGER := SIGMA(T);  --  150
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  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.                                   12 - 18
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260. Notes:
  1261.  
  1262. Omission of a generic actual parameter is only allowed if  a  corresponding
  1263. default exists.  If default expressions or default names (other than simple
  1264. names) are used, they are evaluated in the order in which the corresponding
  1265. generic formal parameters are declared.
  1266.  
  1267. If  two  overloaded subprograms declared in a generic package specification
  1268. differ only by the (formal) type of  their  parameters  and  results,  then
  1269. there  exist  legal instantiations for which all calls of these subprograms
  1270. from outside the instance are ambiguous.  For example:
  1271.  
  1272.     generic
  1273.        type A is (<>);
  1274.        type B is private;
  1275.     package G is
  1276.        function NEXT(X : A) return A;
  1277.        function NEXT(X : B) return B;
  1278.     end;
  1279.  
  1280.     package P is new G(A => BOOLEAN, B => BOOLEAN);
  1281.     -- calls of P.NEXT are ambiguous
  1282.  
  1283. References:   declaration  3.1,   designator   6.1,   discriminant   3.7.1,
  1284. elaboration 3.1 3.9, entity 3.1, entry name 9.5, evaluation 4.5, expression
  1285. 4.4,  generic  formal  object  12.1, generic formal parameter 12.1, generic
  1286. formal  subprogram  12.1,  generic  formal  type  12.1,  generic  parameter
  1287. declaration   12.1,   global  declaration  8.1,  identifier  2.3,  implicit
  1288. declaration 3.1, local declaration 8.1, mode in 12.1.1, mode in out 12.1.1,
  1289. name 4.1, operation 3.3, operator symbol 6.1, overloading 6.6 8.7,  package
  1290. 7, simple name 4.1, subprogram 6, subprogram call 6.4, subprogram name 6.1,
  1291. subtype  declaration 3.3.2, type mark 3.3.2, variable 3.2.1, visibility 8.3
  1292.  
  1293.  
  1294.  
  1295.  
  1296. 12.3.1  Matching Rules for Formal Objects
  1297.  
  1298.  
  1299. A generic formal parameter of mode in of a given  type  is  matched  by  an
  1300. expression of the same type.  If a generic unit has a generic formal object
  1301. of  mode  in,  a check is made that the value of the expression  belongs to
  1302. the subtype  denoted  by  the  type  mark,  as  for  an  explicit  constant
  1303. declaration  (see  3.2.1). The exception CONSTRAINT_ERROR is raised if this
  1304. check fails.
  1305.  
  1306. A generic formal parameter of mode in out of a given type is matched by the
  1307. name of a variable of the same type.  The variable must  not  be  a  formal
  1308. parameter  of  mode  out or a subcomponent thereof.  The name must denote a
  1309. variable for which renaming is allowed (see 8.5).
  1310.  
  1311. Notes:
  1312.  
  1313. The type of a generic actual parameter of mode in must  not  be  a  limited
  1314. type.   The constraints that apply to a generic formal parameter of mode in
  1315.  
  1316.  
  1317.                                   12 - 19
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. out are those of the corresponding generic actual parameter  (see  12.1.1).
  1327.  
  1328. References:   constraint  3.3,  constraint_error exception 11.1, expression
  1329. 4.4, formal parameter 6.1, generic actual parameter  12.3,  generic  formal
  1330. object  12.1.1,  generic formal parameter 12.1, generic instantiation 12.3,
  1331. generic unit 12.1, limited type 7.4.4, matching  generic  actual  parameter
  1332. 12.3,  mode  in 12.1.1, mode in out 12.1.1, mode out 6.2, name 4.1, raising
  1333. of exceptions 11, satisfy 3.3, subcomponent 3.3, type 3.3, type mark 3.3.2,
  1334. variable 3.2.1
  1335.  
  1336.  
  1337.  
  1338.  
  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.                                   12 - 20
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392. 12.3.2  Matching Rules for Formal Private Types
  1393.  
  1394.  
  1395. A generic formal private type is matched by any type or subtype (the actual
  1396. subtype) that satisfies the following conditions:
  1397.  
  1398.   -  If the formal type is not limited, the  actual  type  must  not  be  a
  1399.      limited  type.  (If, on the other hand, the formal type is limited, no
  1400.      such condition is imposed on the corresponding actual type, which  can
  1401.      be limited or not limited.)
  1402.  
  1403.   -  If the formal type has a discriminant part, the actual type must be  a
  1404.      type   with   the  same  number  of  discriminants;   the  type  of  a
  1405.      discriminant that appears at a given position in the discriminant part
  1406.      of the actual type must be the same as the type  of  the  discriminant
  1407.      that  appears  at  the  same  position in the discriminant part of the
  1408.      formal type;  and the actual subtype must be unconstrained.   (If,  on
  1409.      the  other hand, the formal type has no discriminants, the actual type
  1410.      is allowed to have discriminants.)
  1411.  
  1412. Furthermore, consider any occurrence of the name of the formal  type  at  a
  1413. place  where this name is used as an unconstrained subtype indication.  The
  1414. actual subtype must not be an unconstrained array type or an  unconstrained
  1415. type  with  discriminants,  if any of these occurrences is at a place where
  1416. either a constraint or default discriminants would be required for an array
  1417. type or for a type with discriminants (see  3.6.1  and  3.7.2).   The  same
  1418. restriction  applies  to occurrences of the name of a subtype of the formal
  1419. type, and to occurrences of the  name  of  any  type  or  subtype  derived,
  1420. directly or indirectly, from the formal type.
  1421.  
  1422. If  a  generic  unit  has  a  formal  private  type with discriminants, the
  1423. elaboration of  a  corresponding  generic  instantiation  checks  that  the
  1424. subtype  of each discriminant of the actual type is the same as the subtype
  1425. of the corresponding  discriminant  of  the  formal  type.   The  exception
  1426. CONSTRAINT_ERROR is raised if this check fails.
  1427.  
  1428. References:   array  type  3.6,  constraint 3.3, constraint_error exception
  1429. 11.1, default expression  for  a  discriminant  3.7.1,  derived  type  3.4,
  1430. discriminant  3.7.1,  discriminant  part  3.7.1,  elaboration  3.9, generic
  1431. actual type 12.3, generic body 12.2, generic formal  type  12.1.2,  generic
  1432. instantiation   12.3,  generic  specification  12.1,  limited  type  7.4.4,
  1433. matching generic actual parameter 12.3, name 4.1, private type 7.4, raising
  1434. of exceptions 11, subtype 3.3, subtype indication  3.3.2,  type  3.3,  type
  1435. with discriminants 3.3, unconstrained array type 3.6, unconstrained subtype
  1436. 3.3
  1437.  
  1438.  
  1439.  
  1440.  
  1441. 12.3.3  Matching Rules for Formal Scalar Types
  1442.  
  1443.  
  1444. A  generic  formal  type defined by (<>) is matched by any discrete subtype
  1445. (that is, any enumeration or  integer  subtype).   A  generic  formal  type
  1446. defined  by  range <> is matched by any integer subtype.   A generic formal
  1447.  
  1448.  
  1449.                                   12 - 21
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. type defined by digits <> is matched by  any  floating  point  subtype.   A
  1459. generic  formal  type  defined  by  delta  <> is matched by any fixed point
  1460. subtype.  No other matches are possible for these generic formal types.
  1461.  
  1462. References:  box delimiter 12.1.2,  discrete  type  3.5,  enumeration  type
  1463. 3.5.1,  fixed  point  type 3.5.9, floating point type 3.5.7, generic actual
  1464. type 12.3, generic  formal  type  12.1.2,  generic  type  definition  12.1,
  1465. integer type 3.5.4, matching generic actual parameter 12.3, scalar type 3.5
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.                                   12 - 22
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524. 12.3.4  Matching Rules for Formal Array Types
  1525.  
  1526.  
  1527. A  formal  array  type is matched by an actual array subtype that satisfies
  1528. the following conditions:
  1529.  
  1530.   -  The formal array type and the actual array type  must  have  the  same
  1531.      dimensionality;  the formal type and the actual subtype must be either
  1532.      both constrained or both unconstrained.
  1533.  
  1534.   -  For each index position, the index type  must  be  the  same  for  the
  1535.      actual array type as for the formal array type.
  1536.  
  1537.   -  The component type must be the same for the actual array type  as  for
  1538.      the  formal  array type.  If the component type is other than a scalar
  1539.      type, then the component subtypes must be either both  constrained  or
  1540.      both unconstrained.
  1541.  
  1542. If  a  generic  unit  has  a  formal  array  type,  the  elaboration  of  a
  1543. corresponding instantiation checks that the constraints  (if  any)  on  the
  1544. component  type  are  the  same for the actual array type as for the formal
  1545. array type, and likewise that  for  any  given  index  position  the  index
  1546. subtypes  or  the  discrete  ranges  have  the  same bounds.  The exception
  1547. CONSTRAINT_ERROR is raised if this check fails.
  1548.  
  1549. Example:
  1550.  
  1551.     --  given the generic package
  1552.  
  1553.     generic
  1554.        type ITEM   is private;
  1555.        type INDEX  is (<>);
  1556.        type VECTOR is array (INDEX range <>) of ITEM;
  1557.        type TABLE  is array (INDEX) of ITEM;
  1558.     package P is
  1559.        ...
  1560.     end;
  1561.  
  1562.     --  and the types
  1563.  
  1564.     type MIX    is array (COLOR range <>) of BOOLEAN;
  1565.     type OPTION is array (COLOR) of BOOLEAN;
  1566.  
  1567.     --  then MIX can match VECTOR and OPTION can match TABLE
  1568.  
  1569.     package R is new P(ITEM   => BOOLEAN, INDEX => COLOR,
  1570.                        VECTOR => MIX,     TABLE => OPTION);
  1571.  
  1572.     --  Note that MIX cannot match TABLE and OPTION cannot match VECTOR
  1573.  
  1574. Note:
  1575.  
  1576. For the above rules, if any of the index or component types of  the  formal
  1577. array  type  is  itself  a  formal  type, then within the instance its name
  1578. denotes the corresponding actual subtype (see 12.3(d)).
  1579.  
  1580.  
  1581.                                   12 - 23
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590. References:  array type 3.6, array type definition  3.6,  component  of  an
  1591. array  3.6,  constrained  array  type 3.6, constraint 3.3, constraint_error
  1592. exception 11.1, elaboration 3.9, formal  type  12.1,  generic  formal  type
  1593. 12.1.2,  generic  instantiation  12.3,  index  3.6, index constraint 3.6.1,
  1594. matching generic actual parameter 12.3, raise statement 11.3, subtype  3.3,
  1595. unconstrained array type 3.6
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.                                   12 - 24
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656. 12.3.5  Matching Rules for Formal Access Types
  1657.  
  1658.  
  1659. A  formal access type is matched by an actual access subtype if the type of
  1660. the designated objects is the same for the actual type as  for  the  formal
  1661. type.   If  the  designated  type  is  other  than  a scalar type, then the
  1662. designated subtypes must be either both constrained or both  unconstrained.
  1663.  
  1664. If  a  generic  unit  has  a  formal  access  type,  the  elaboration  of a
  1665. corresponding instantiation checks that any constraints on  the  designated
  1666. objects are the same for the actual access subtype as for the formal access
  1667. type.  The exception CONSTRAINT_ERROR is raised if this check fails.
  1668.  
  1669. Example:
  1670.  
  1671.     --  the formal types of the generic package
  1672.  
  1673.     generic
  1674.        type NODE is private;
  1675.        type LINK is access NODE;
  1676.     package P is
  1677.        ...
  1678.     end;
  1679.  
  1680.     --  can be matched by the actual types
  1681.  
  1682.     type CAR;
  1683.     type CAR_NAME is access CAR;
  1684.  
  1685.     type CAR is
  1686.        record
  1687.           PRED, SUCC : CAR_NAME;
  1688.           NUMBER     : LICENSE_NUMBER;
  1689.           OWNER      : PERSON;
  1690.        end record;
  1691.  
  1692.  
  1693.     --  in the following generic instantiation
  1694.  
  1695.     package R is new P(NODE => CAR, LINK => CAR_NAME);
  1696.  
  1697. Note:
  1698.  
  1699. For  the  above rules, if the designated type is itself a formal type, then
  1700. within the instance its name denotes the corresponding actual subtype  (see
  1701. 12.3(d)).
  1702.  
  1703. References:   access  type 3.8, access type definition 3.8, constraint 3.3,
  1704. constraint_error exception 11.1, designate 3.8,  elaboration  3.9,  generic
  1705. formal  type  12.1.2,  generic  instantiation 12.3, matching generic actual
  1706. parameter 12.3, object 3.2, raise statement 11.3, value of access type  3.8
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.                                   12 - 25
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722. 12.3.6  Matching Rules for Formal Subprograms
  1723.  
  1724.  
  1725. A  formal  subprogram  is  matched  by  an  actual  subprogram, enumeration
  1726. literal, or entry if both have the same parameter and result  type  profile
  1727. (see  6.6);   in  addition,  parameter  modes  must be identical for formal
  1728. parameters that are at the same parameter position.
  1729.  
  1730. If a generic unit has a default subprogram specified by a name,  this  name
  1731. must denote a subprogram, an enumeration literal, or an entry, that matches
  1732. the  formal subprogram (in the above sense).  The evaluation of the default
  1733. name takes place during the elaboration of each instantiation that uses the
  1734. default,  as defined in section 12.3.
  1735.  
  1736. If a generic unit has  a  default  subprogram  specified   by  a  box,  the
  1737. corresponding  actual parameter can be omitted if a subprogram, enumeration
  1738. literal, or entry  matching  the  formal  subprogram,  and  with  the  same
  1739. designator  as  the  formal subprogram, is directly visible at the place of
  1740. the generic instantiation;  this subprogram, enumeration literal, or  entry
  1741. is  then used by default (there must be exactly one subprogram, enumeration
  1742. literal, or entry satisfying the previous conditions).
  1743.  
  1744. Example:
  1745.  
  1746.     --  given the generic function specification
  1747.  
  1748.     generic
  1749.        type ITEM is private;
  1750.        with function "*" (U, V : ITEM) return ITEM is <>;
  1751.     function SQUARING(X : ITEM) return ITEM;
  1752.  
  1753.     --  and the function
  1754.  
  1755.     function MATRIX_PRODUCT(A, B : MATRIX) return MATRIX;
  1756.  
  1757.     --  the following instantiation is possible
  1758.  
  1759.     function SQUARE is new SQUARING(MATRIX, MATRIX_PRODUCT);
  1760.  
  1761.     --  the following instantiations are equivalent
  1762.  
  1763.     function SQUARE is new SQUARING(ITEM => INTEGER, "*" => "*");
  1764.     function SQUARE is new SQUARING(INTEGER, "*");
  1765.     function SQUARE is new SQUARING(INTEGER);
  1766.  
  1767. Notes:
  1768.  
  1769. The matching rules for  formal  subprograms  state  requirements  that  are
  1770. similar  to  those  applying to subprogram renaming declarations (see 8.5).
  1771. In particular, the name of a parameter of the formal subprogram need not be
  1772. the same as that of the corresponding parameter of the  actual  subprogram;
  1773. similarly,  for  these parameters, default expressions need not correspond.
  1774.  
  1775. A formal subprogram is matched by an attribute of a type if  the  attribute
  1776. is  a  function with a matching specification.  An enumeration literal of a
  1777.  
  1778.  
  1779.                                   12 - 26
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788. given type matches a parameterless formal function whose result type is the
  1789. given type.
  1790.  
  1791. References:  attribute 4.1.4, box delimiter 12.1.2, designator  6.1,  entry
  1792. 9.5,  function  6.5,  generic  actual  type 12.3, generic formal subprogram
  1793. 12.1.3, generic formal type 12.1.2, generic  instantiation  12.3,  matching
  1794. generic  actual parameter 12.3, name 4.1, parameter and result type profile
  1795. 6.3, subprogram 6, subprogram specification 6.1,  subtype  3.3,  visibility
  1796. 8.3
  1797.  
  1798.  
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.                                   12 - 27
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854. 12.4  Example of a Generic Package
  1855.  
  1856.  
  1857. The following example provides a possible formulation of stacks by means of
  1858. a  generic  package.   The  size  of  each  stack and the type of the stack
  1859. elements are provided as generic parameters.
  1860.  
  1861.     generic
  1862.        SIZE : POSITIVE;
  1863.        type ITEM is private;
  1864.     package STACK is
  1865.        procedure PUSH(E : in  ITEM);
  1866.        procedure POP (E : out ITEM);
  1867.        OVERFLOW, UNDERFLOW : exception;
  1868.     end STACK;
  1869.  
  1870.     package body STACK is
  1871.  
  1872.        type TABLE is array (POSITIVE range <>) of ITEM;
  1873.        SPACE : TABLE(1 .. SIZE);
  1874.        INDEX : NATURAL := 0;
  1875.  
  1876.        procedure PUSH(E : in ITEM) is
  1877.        begin
  1878.           if INDEX >= SIZE then
  1879.              raise OVERFLOW;
  1880.           end if;
  1881.           INDEX := INDEX + 1;
  1882.           SPACE(INDEX) := E;
  1883.        end PUSH;
  1884.  
  1885.        procedure POP(E : out ITEM) is
  1886.        begin
  1887.           if INDEX = 0 then
  1888.              raise UNDERFLOW;
  1889.           end if;
  1890.           E := SPACE(INDEX);
  1891.           INDEX := INDEX - 1;
  1892.        end POP;
  1893.  
  1894.     end STACK;
  1895.  
  1896. Instances of this generic package can be obtained as follows:
  1897.  
  1898.     package STACK_INT  is new STACK(SIZE => 200, ITEM => INTEGER);
  1899.     package STACK_BOOL is new STACK(100, BOOLEAN);
  1900.  
  1901. Thereafter, the procedures of the instantiated packages can  be  called  as
  1902. follows:
  1903.  
  1904.     STACK_INT.PUSH(N);
  1905.     STACK_BOOL.PUSH(TRUE);
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.                                   12 - 28
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920. Alternatively,  a  generic  formulation  of  the type STACK can be given as
  1921. follows (package body omitted):
  1922.  
  1923.     generic
  1924.        type ITEM is private;
  1925.     package ON_STACKS is
  1926.        type STACK(SIZE : POSITIVE) is limited private;
  1927.        procedure PUSH(S : in out STACK; E : in  ITEM);
  1928.        procedure POP (S : in out STACK; E : out ITEM);
  1929.        OVERFLOW, UNDERFLOW : exception;
  1930.     private
  1931.        type TABLE is array (POSITIVE range <>) of ITEM;
  1932.        type STACK(SIZE : POSITIVE) is
  1933.           record
  1934.              SPACE : TABLE(1 .. SIZE);
  1935.              INDEX : NATURAL := 0;
  1936.           end record;
  1937.     end;
  1938.  
  1939. In order to use such a  package,  an  instantiation  must  be  created  and
  1940. thereafter stacks of the corresponding type can be declared:
  1941.  
  1942.     declare
  1943.        package STACK_REAL is new ON_STACKS(REAL); use STACK_REAL;
  1944.        S : STACK(100);
  1945.     begin
  1946.        ...
  1947.        PUSH(S, 2.54);
  1948.        ...
  1949.     end;
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.                                   12 - 29
  1978.  
  1979.  
  1980.  
  1981.  
  1982.