home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 3 / CDASC03.ISO / sorties / 118 / lrm_12 < prev    next >
Text File  |  1993-04-01  |  44KB  |  953 lines

  1. The Ada Joint Program Office does not guarantee the accuracy of this
  2. file, as compared with the contents of ANSI/MIL-STD-1815A-1983,
  3. the Reference Manual for the Ada Programming Language.  If errors or
  4. discrepancies are found in this machine-readable version, please 
  5. forward comments via the Defense Data Network (DDN) to:
  6.  
  7.                      ACTION@AJPO.SEI.CMU.EDU
  8.  
  9. or via conventional mail to
  10.  
  11.                   Ada Information Clearinghouse
  12.                    3D139 (1211 S. Fern, C-107)
  13.                          The Pentagon
  14.                    Washington, D.C.  20301-3081
  15.  
  16. -----------------------------------------------------------------------
  17.  
  18. Copyright 1980, 1982, 1983 owned by the United States Government as
  19. represented by the Under Secretary of Defense, Research and
  20. Engineering.  All rights reserved.  Provided that notice of copyright
  21. is included on the first page, this document may be copied in its
  22. entirety without alteration or as altered by (1) adding text that is
  23. clearly marked as an insertion; (2) shading or highlighting existing
  24. text; (3) deleting examples.  Permission to publish other excerpts
  25. should be obtained from the Ada Joint Program Office, OUSDRE (R&AT),
  26. The Pentagon, Washington, DC 20301-2081, U.S.A.
  27.  
  28.  
  29.  
  30.  
  31.  
  32.                              12. Generic Units   
  33.  
  34. A generic unit is a program unit that is either a generic subprogram  or  a
  35. generic  package.   A generic unit is a template, which is parameterized or
  36. not, and from which corresponding (nongeneric) subprograms or packages  can
  37. be  obtained.   The resulting program units are said to be instances of the
  38. original generic unit. 
  39.  
  40. A generic unit  is  declared  by  a  generic  declaration.   This  form  of
  41. declaration  has  a  generic  formal  part  declaring  any  generic  formal
  42. parameters.  An instance of a generic unit is obtained as the result  of  a
  43. generic  instantiation  with  appropriate generic actual parameters for the
  44. generic formal parameters.  An  instance  of  a  generic  subprogram  is  a
  45. subprogram.  An instance of a generic package is a package. 
  46.  
  47. Generic  units are templates.  As templates they do not have the properties
  48. that are specific to their nongeneric counterparts.  For example, a generic
  49. subprogram can be instantiated but it cannot be called.  In  contrast,  the
  50. instance  of  a generic subprogram is a nongeneric subprogram;  hence, this
  51. instance can be called but it cannot be used to produce further  instances. 
  52.  
  53. References:   declaration  3.1,  generic  actual  parameter  12.3,  generic
  54. declaration 12.1, generic formal parameter 12.1, generic formal part  12.1,
  55. generic  instantiation 12.3, generic package 12.1, generic subprogram 12.1,
  56. instance 12.3, package 7, program unit 6, subprogram 6    
  57.  
  58. 12.1  Generic Declarations  
  59.  
  60. A generic declaration declares a generic unit, which is  either  a  generic
  61. subprogram  or a generic package.  A generic declaration includes a generic
  62. formal part declaring any generic  formal  parameters.   A  generic  formal
  63. parameter  can  be  an  object;   alternatively  (unlike  a  parameter of a
  64. subprogram), it can be a type or a subprogram. 
  65.  
  66.     generic_declaration ::= generic_specification; 
  67.  
  68.     generic_specification ::=
  69.          generic_formal_part subprogram_specification
  70.        | generic_formal_part package_specification 
  71.  
  72.     generic_formal_part ::= generic {generic_parameter_declaration} 
  73.  
  74.     generic_parameter_declaration ::=
  75.          identifier_list : [in [out]] type_mark [:= expression];  
  76.        | type identifier is generic_type_definition;
  77.        | private_type_declaration
  78.        | with subprogram_specification [is name];
  79.        | with subprogram_specification [is <>]; 
  80.  
  81.     generic_type_definition ::=
  82.          (<>) | range <> | digits <> | delta <>
  83.        | array_type_definition | access_type_definition                           
  84.                                
  85.  
  86. The terms generic formal object (or simply, formal object), generic  formal
  87. type  (or  simply,  formal type), and generic formal subprogram (or simply,
  88. formal subprogram) are  used  to  refer  to  corresponding  generic  formal
  89. parameters. 
  90.  
  91. The only form of subtype indication allowed within a generic formal part is
  92. a  type  mark (that is, the subtype indication must not include an explicit
  93. constraint).  The designator of a generic subprogram must be an identifier. 
  94.  
  95. Outside the specification and body of a generic  unit,  the  name  of  this
  96. program unit denotes the generic unit.  In contrast, within the declarative
  97. region  associated with a generic subprogram, the name of this program unit
  98. denotes the subprogram obtained by the current instantiation of the generic
  99. unit.  Similarly, within the declarative region associated with  a  generic
  100. package,  the name of this program unit denotes the package obtained by the
  101. current instantiation. 
  102.  
  103. The elaboration of a generic declaration has no other effect. 
  104.  
  105. Examples of generic formal parts: 
  106.  
  107.     generic     --  parameterless 
  108.  
  109.     generic
  110.        SIZE : NATURAL;  --  formal object 
  111.  
  112.     generic
  113.        LENGTH : INTEGER := 200;          -- formal object with a default
  114.                                             expression
  115.        AREA   : INTEGER := LENGTH*LENGTH; -- formal object with a default
  116.                                              expression
  117.  
  118.     generic
  119.        type ITEM  is private;                       -- formal type
  120.        type INDEX is (<>);                          -- formal type
  121.        type ROW   is array(INDEX range <>) of ITEM; -- formal type
  122.        with function "<"(X, Y : ITEM) return BOOLEAN;    -- formal subprogram 
  123.  
  124. Examples of generic declarations declaring generic subprograms: 
  125.  
  126.     generic
  127.        type ELEM is private;
  128.     procedure EXCHANGE(U, V : in out ELEM); 
  129.  
  130.     generic
  131.        type ITEM is private;
  132.        with function "*"(U, V : ITEM) return ITEM is <>;
  133.     function SQUARING(X : ITEM) return ITEM; 
  134.  
  135. Example of a generic declaration declaring a generic package: 
  136.  
  137.     generic
  138.        type ITEM   is private;
  139.        type VECTOR is array (POSITIVE range <>) of ITEM;
  140.        with function SUM(X, Y : ITEM) return ITEM;
  141.     package ON_VECTORS is
  142.        function SUM  (A, B : VECTOR) return VECTOR;  
  143.        function SIGMA(A    : VECTOR) return ITEM;
  144.        LENGTH_ERROR : exception;
  145.        end;                                                                      
  146.                                                                               
  147.                                                                               
  148. Notes: 
  149.  
  150. Within a generic subprogram, the name of this program unit acts as the name 
  151. of a subprogram.  Hence this name can be overloaded, and it can appear in a
  152. recursive call of the current instantiation.  For  the  same  reason,  this
  153. name  cannot  appear  after  the reserved word new in a (recursive) generic
  154. instantiation. 
  155.  
  156. An expression that occurs in a generic formal part is  either  the  default
  157. expression  for  a generic formal object of mode in, or a constituent of an
  158. entry name given as default name for a formal subprogram,  or  the  default
  159. expression for a parameter of a formal subprogram.  Default expressions for
  160. generic  formal  objects  and default names for formal subprograms are only
  161. evaluated for generic  instantiations  that  use  such  defaults.   Default
  162. expressions  for  parameters  of  formal subprograms are only evaluated for
  163. calls of the  formal  subprograms  that  use  such  defaults.   (The  usual
  164. visibility  rules  apply  to  any  name  used in a default expression:  the
  165. denoted entity must therefore be visible at the place of  the  expression.) 
  166.  
  167. Neither   generic  formal  parameters  nor  their  attributes  are  allowed
  168. constituents of static expressions (see 4.9). 
  169.  
  170. References:   access  type  definition  3.8,  array  type  definition  3.6,
  171. attribute   4.1.4,   constraint   3.3,  declaration  3.1,  designator  6.1,
  172. elaboration has no other effect 3.1, entity 3.1, expression  4.4,  function
  173. 6.5,  generic  instantiation  12.3,  identifier  2.3,  identifier list 3.2,
  174. instance  12.3,  name  4.1,  object  3.2,  overloading  6.6  8.7,   package
  175. specification  7.1,  parameter of a subprogram 6.2, private type definition
  176. 7.4, procedure 6.1, reserved word 2.9, static expression 4.9, subprogram 6,
  177. subprogram specification 6.1, subtype indication 3.3.2, type 3.3, type mark
  178. 3.3.2    
  179.  
  180. 12.1.1  Generic Formal Objects  
  181.  
  182. The first form of generic parameter  declaration  declares  generic  formal
  183. objects.   The type of a generic formal object is the base type of the type  
  184. denoted by the type mark given in the  generic  parameter  declaration.   A
  185. generic  parameter  declaration with several identifiers is equivalent to a
  186. sequence of single generic parameter declarations, as explained in  section
  187. 3.2. 
  188.  
  189. A  generic  formal  object  has a mode that is either in or in out.  In the
  190. absence of an explicit mode indication in a generic parameter  declaration,
  191. the  mode  in  is  assumed;  otherwise the mode is the one indicated.  If a
  192. generic parameter declaration ends with an expression,  the  expression  is
  193. the  default  expression  of  the  generic  formal  parameter.   A  default
  194. expression is only allowed  if  the  mode  is  in  (whether  this  mode  is
  195. indicated explicitly or implicitly).  The type of a default expression must
  196. be that of the corresponding generic formal parameter.                        
  197.  
  198. A  generic  formal object of mode in is a constant whose value is a copy of
  199. the value supplied as the matching generic actual parameter  in  a  generic
  200. instantiation,  as described in section 12.3.  The type of a generic formal
  201. object of mode in must not be a  limited  type;   the  subtype  of  such  a
  202. generic  formal object is the subtype denoted by the type mark given in the
  203. generic parameter declaration. 
  204.  
  205. A generic formal object of mode in out is a variable and denotes the object
  206. supplied  as  the  matching  generic  actual   parameter   in   a   generic
  207. instantiation, as described in section 12.3.  The constraints that apply to
  208. the  generic  formal  object  are those of the corresponding generic actual
  209. parameter.                                                                       
  210.  
  211. Note:
  212.  
  213. The constraints that apply to a generic formal object of mode  in  out  are
  214. those  of  the corresponding generic actual parameter (not those implied by
  215. the type mark that appears in the generic parameter declaration).  Whenever
  216. possible (to avoid confusion) it is recommended that the  name  of  a  base
  217. type be used for the declaration of such a formal object.  If, however, the
  218. base  type is anonymous, it is recommended that the subtype name defined by
  219. the type declaration for the base type be used. 
  220.  
  221. References:  anonymous type 3.3.1, assignment 5.2, base type 3.3,  constant
  222. declaration  3.2, constraint 3.3, declaration 3.1, generic actual parameter
  223. 12.3, generic formal object 12.1, generic formal  parameter  12.1,  generic
  224. instantiation  12.3,  generic  parameter  declaration 12.1, identifier 2.3,
  225. limited type 7.4.4, matching generic actual parameter 12.3, mode 6.1,  name
  226. 4.1,  object  3.2, simple name 4.1, subtype 3.3, type declaration 3.3, type
  227. mark 3.3.2, variable 3.2.1    
  228.  
  229. 12.1.2  Generic Formal Types   
  230.  
  231. A generic parameter declaration that includes a generic type definition  or
  232. a  private  type  declaration  declares  a  generic formal type.  A generic
  233. formal type denotes  the  subtype  supplied  as  the  corresponding  actual
  234. parameter  in  a  generic instantiation, as described in 12.3(d).  However,
  235. within a generic unit,  a  generic  formal  type  is  considered  as  being
  236. distinct  from  all  other  (formal  or  nonformal)  types.   The  form  of
  237. constraint applicable to a formal type in a subtype indication  depends  on
  238. the class of the type as for a nonformal type. 
  239.  
  240. The only form of discrete range that is allowed within the declaration of a
  241. generic formal (constrained) array type is a type mark. 
  242.  
  243. The  discriminant  part of a generic formal private type must not include a
  244. default expression for a discriminant.  (Consequently, a variable  that  is
  245. declared  by  an  object  declaration  must be constrained if its type is a
  246. generic formal type with discriminants.) 
  247.  
  248. Within the declaration and body of a generic unit, the operations available
  249. for values of a generic formal type (apart from  any  additional  operation
  250. specified  by  a  generic  formal subprogram) are determined by the generic
  251. parameter declaration for the formal type: 
  252.  
  253. (a)  For a private type declaration, the  available  operations  are  those
  254.      defined  in  section  7.4.2  (in particular, assignment, equality, and
  255.      inequality are available for a private type unless it is limited).                                                                                                                                                                                                                                                 
  256.  
  257. (b)  For an array type  definition,  the  available  operations  are  those
  258.      defined  in  section 3.6.2 (for example, they include the formation of
  259.      indexed components and slices).    
  260.  
  261. (c)  For an access type definition,  the  available  operations  are  those
  262.      defined in section 3.8.2 (for example, allocators can be used). 
  263.  
  264. The  four forms of generic type definition in which a box appears (that is,
  265. the compound delimiter <>) correspond  to  the  following  major  forms  of
  266. scalar type: 
  267.  
  268. (d)  Discrete types:  (<>) 
  269.  
  270.      The  available operations are the operations common to enumeration and
  271.      integer types; these are defined in section 3.5.5.                                              
  272.  
  273. (e)  Integer types:  range <> 
  274.  
  275.      The available operations are the operations of integer  types  defined
  276.      in section 3.5.5. 
  277.  
  278. (f)  Floating point types:  digits <> 
  279.  
  280.      The available operations are those defined in section 3.5.8. 
  281.  
  282. (g)  Fixed point types:  delta <>  
  283.  
  284.      The available operations are those defined in section 3.5.10.  
  285.  
  286. In  all  of  the  above  cases  (a)  through (f), each operation implicitly
  287. associated with a formal type (that is, other than an  operation  specified
  288. by  a  formal  subprogram)  is  implicitly  declared  at  the  place of the
  289. declaration of the formal type.  The same holds for a  formal  fixed  point
  290. type,  except  for  the  multiplying operators that deliver a result of the
  291. type  universal_fixed  (see  4.5.5),  since  these  special  operators  are
  292. declared in the package STANDARD. 
  293.  
  294. For  an  instantiation of the generic unit, each of these operations is the
  295. corresponding basic operation or predefined operator of the matching actual
  296. type.  For an operator, this rule applies even if  the  operator  has  been
  297. redefined  for  the actual type or for some parent type of the actual type.  
  298.  
  299. Examples of generic formal types: 
  300.  
  301.     type ITEM is private;
  302.     type BUFFER(LENGTH : NATURAL) is limited private; 
  303.  
  304.     type ENUM  is (<>);
  305.     type INT   is range <>;
  306.     type ANGLE is delta <>;
  307.     type MASS  is digits <>; 
  308.  
  309.     type TABLE is array (ENUM) of ITEM;  
  310.  
  311. Example of a generic formal part declaring a formal integer type: 
  312.  
  313.     generic
  314.        type RANK is range <>;
  315.        FIRST  : RANK := RANK'FIRST;
  316.        SECOND : RANK := FIRST + 1;  --  the operator "+" of the type RANK  
  317.  
  318. References:   access  type  definition  3.8,  allocator  4.8,  array   type
  319. definition  3.6, assignment 5.2, body of a generic unit 12.2, class of type
  320. 3.3, constraint 3.3, declaration 3.1, declaration of a generic  unit  12.1,
  321. discrete range 3.6, discrete type 3.5, discriminant part 3.7.1, enumeration
  322. type  3.5.1,  equality  4.5.2,  fixed point type 3.5.9, floating point type
  323. 3.5.7, generic actual type 12.3, generic formal part 12.1,  generic  formal  
  324. subprogram  12.1.3, generic formal type 12.1, generic parameter declaration
  325. 12.1, generic type definition 12.1,  indexed  component  4.1.1,  inequality
  326. 4.5.2,  instantiation 12.3, integer type 3.5.4, limited private type 7.4.4,
  327. matching generic actual  type  12.3.2  12.3.3  12.3.4  12.3.5,  multiplying
  328. operator  4.5  4.5.5, operation 3.3, operator 4.5, parent type 3.4, private
  329. type definition 7.4, scalar type 3.5, slice 4.1.2, standard package 8.6  C,
  330. subtype indication 3.3.2, type mark 3.3.2, universal_fixed 3.5.9                                                   
  331.  
  332. 12.1.3  Generic Formal Subprograms  
  333.  
  334. A  generic  parameter  declaration that includes a subprogram specification
  335. declares a generic formal subprogram. 
  336.  
  337. Two alternative forms of defaults can be specified in the declaration of  a
  338. generic formal subprogram.  In these forms, the subprogram specification is
  339. followed  by  the  reserved  word  is  and  either  a  box or the name of a
  340. subprogram or entry.  The matching rules for these defaults  are  explained
  341. in section 12.3.6. 
  342.  
  343. A generic formal subprogram denotes the subprogram, enumeration literal, or
  344. entry  supplied  as the corresponding generic actual parameter in a generic
  345. instantiation, as described in section 12.3(f). 
  346.  
  347. Examples of generic formal subprograms: 
  348.  
  349.     with function INCREASE(X : INTEGER) return INTEGER;
  350.     with function SUM(X, Y : ITEM) return ITEM; 
  351.  
  352.     with function "+"(X, Y : ITEM) return ITEM is <>;
  353.     with function IMAGE(X : ENUM) return STRING is ENUM'IMAGE; 
  354.  
  355.     with procedure UPDATE is DEFAULT_UPDATE; 
  356.  
  357. Notes: 
  358.  
  359. The constraints that apply to a parameter of a formal subprogram are  those   
  360. of  the corresponding parameter in the specification of the matching actual
  361. subprogram (not those  implied  by  the  corresponding  type  mark  in  the
  362. specification  of  the formal subprogram).  A similar remark applies to the
  363. result of a function.   Whenever  possible  (to  avoid  confusion),  it  is
  364. recommended  that the name of a base type be used rather than the name of a
  365. subtype in any declaration of a formal subprogram.  If, however,  the  base
  366. type  is  anonymous, it is recommended that the subtype name defined by the
  367. type declaration be used. 
  368.  
  369. The type specified for a formal parameter of a  generic  formal  subprogram
  370. can  be  any  visible  type,  including  a  generic formal type of the same
  371. generic formal part. 
  372.  
  373. References:  anonymous type 3.3.1, base type  3.3,  box  delimiter  12.1.2,
  374. constraint  3.3,  designator  6.1,  generic  actual parameter 12.3, generic
  375. formal function 12.1, generic formal subprogram 12.1, generic instantiation
  376. 12.3, generic parameter declaration 12.1, identifier 2.3, matching  generic
  377. actual  subprogram  12.3.6,  operator symbol 6.1, parameter of a subprogram
  378. 6.2, renaming declaration 8.5, reserved word 2.9, scope 8.2, subprogram  6,
  379. subprogram specification 6.1, subtype 3.3.2, type 3.3, type mark 3.3.2    
  380.  
  381. 12.2  Generic Bodies   
  382.  
  383. The  body  of a generic subprogram or generic package is a template for the
  384. bodies of the corresponding subprograms or  packages  obtained  by  generic
  385. instantiations.   The  syntax  of  a generic body is identical to that of a
  386. nongeneric body. 
  387.  
  388. For each declaration of a generic subprogram, there must be a corresponding
  389. body.                                                                                 
  390.  
  391. The elaboration of a generic body has no other  effect  than  to  establish
  392. that  the  body  can from then on be used as the template for obtaining the
  393. corresponding instances.  
  394.  
  395. Example of a generic procedure body: 
  396.  
  397.     procedure EXCHANGE(U, V : in out ELEM) is  --  see example in 12.1
  398.        T : ELEM;  --  the generic formal type
  399.     begin
  400.        T := U;
  401.        U := V;
  402.        V := T;
  403.     end EXCHANGE;  
  404.  
  405. Example of a generic function body: 
  406.  
  407.     function SQUARING(X : ITEM) return ITEM is  --  see example in 12.1
  408.     begin
  409.        return X*X;  --  the formal operator "*"
  410.     end;  
  411.  
  412. Example of a generic package body: 
  413.  
  414.     package body ON_VECTORS is  --  see example in 12.1 
  415.  
  416.        function SUM(A, B : VECTOR) return VECTOR is
  417.           RESULT : VECTOR(A'RANGE);     --  the formal type VECTOR
  418.           BIAS   : constant INTEGER := B'FIRST - A'FIRST;
  419.        begin
  420.           if A'LENGTH /= B'LENGTH then
  421.              raise LENGTH_ERROR;
  422.           end if; 
  423.  
  424.           for N in A'RANGE loop
  425.              RESULT(N) := SUM(A(N), B(N + BIAS));  -- the formal function SUM
  426.           end loop;
  427.           return RESULT;
  428.        end; 
  429.  
  430.        function SIGMA(A : VECTOR) return ITEM is
  431.           TOTAL : ITEM := A(A'FIRST);                --  the formal type ITEM
  432.        begin  
  433.           for N in A'FIRST + 1 .. A'LAST loop
  434.              TOTAL := SUM(TOTAL, A(N));           --  the formal function SUM
  435.           end loop;
  436.           return TOTAL;
  437.        end;
  438.     end;  
  439.  
  440. References:   body  3.9,  elaboration  3.9,  generic  body  12.1,   generic
  441. instantiation 12.3, generic package 12.1, generic subprogram 12.1, instance  
  442. 12.3, package body 7.1, package 7, subprogram 6, subprogram body 6.3                                                        
  443.  
  444. 12.3  Generic Instantiation
  445.   
  446. An instance of a generic unit is declared by a generic instantiation. 
  447.  
  448.     generic_instantiation ::=
  449.          package identifier is
  450.              new generic_package_name [generic_actual_part];
  451.        | procedure identifier is
  452.              new generic_procedure_name [generic_actual_part];
  453.        | function designator is
  454.              new generic_function_name [generic_actual_part]; 
  455.  
  456.     generic_actual_part ::=
  457.        (generic_association {, generic_association}) 
  458.  
  459.     generic_association ::=
  460.        [generic_formal_parameter =>] generic_actual_parameter 
  461.  
  462.     generic_formal_parameter ::= parameter_simple_name | operator_symbol 
  463.  
  464.     generic_actual_parameter ::= expression | variable_name
  465.        | subprogram_name | entry_name | type_mark 
  466.  
  467. An  explicit  generic  actual  parameter  must be supplied for each generic
  468. formal parameter, unless the corresponding  generic  parameter  declaration
  469. specifies  that  a default can be used.  Generic associations can be either
  470. positional or named  in  the  same  manner  as  parameter  associations  of
  471. subprogram  calls  (see  6.4).   If two or more formal subprograms have the
  472. same  designator,  then  named  associations  are  not  allowed   for   the
  473. corresponding generic parameters. 
  474.  
  475. Each  generic  actual parameter must match the corresponding generic formal
  476. parameter.  An expression can match a formal object of mode in;  a variable
  477. name can match a formal object of mode in out;  a  subprogram  name  or  an
  478. entry  name  can match a formal subprogram;  a type mark can match a formal
  479. type.  The detailed  rules  defining  the  allowed  matches  are  given  in
  480. sections 12.3.1 to 12.3.6;  these are the only allowed matches.   
  481.  
  482. The  instance  is a copy of the generic unit, apart from the generic formal
  483. part;  thus the instance of a generic package  is  a  package,  that  of  a
  484. generic  procedure  is  a  procedure,  and  that of a generic function is a
  485. function.  For each  occurrence, within the generic unit, of  a  name  that
  486. denotes  a given entity, the following list defines which entity is denoted
  487. by the corresponding occurrence within the instance. 
  488.  
  489. (a)  For a  name  that  denotes  the  generic  unit:     The  corresponding
  490.      occurrence denotes the instance. 
  491.  
  492. (b)  For a name that denotes a generic formal  object  of  mode  in:    The
  493.      corresponding  name  denotes  a  constant whose value is a copy of the
  494.      value of the associated generic actual parameter. 
  495.  
  496. (c)  For a name that denotes a generic formal object of mode in out:    The
  497.      corresponding  name  denotes  the  variable  named  by  the associated  
  498.      generic actual parameter. 
  499.  
  500. (d)  For a name that denotes a generic  formal  type:    The  corresponding
  501.      name  denotes  the  subtype  named  by  the  associated generic actual
  502.      parameter (the actual subtype). 
  503.  
  504. (e)  For a name that denotes a discriminant of a generic formal type:   The
  505.      corresponding name denotes the corresponding discriminant (there  must
  506.      be one) of the actual type associated with the generic formal type.                                                
  507.  
  508. (f)  For  a  name  that  denotes  a  generic  formal   subprogram:      The
  509.      corresponding  name  denotes  the  subprogram, enumeration literal, or
  510.      entry named by the associated generic  actual  parameter  (the  actual
  511.      subprogram).  
  512.  
  513. (g)  For a name that  denotes  a  formal  parameter  of  a  generic  formal
  514.      subprogram:    The corresponding name denotes the corresponding formal
  515.      parameter  of  the  actual  subprogram  associated  with  the   formal
  516.      subprogram. 
  517.  
  518. (h)  For a name that denotes a local entity  declared  within  the  generic
  519.      unit:    The  corresponding  name  denotes  the entity declared by the
  520.      corresponding local declaration within the instance. 
  521.  
  522. (i)  For a name that denotes  a  global  entity  declared  outside  of  the
  523.      generic unit:   The corresponding name denotes the same global entity. 
  524.  
  525. Similar  rules  apply  to  operators  and basic operations:  in particular,
  526. formal operators follow a rule similar to rule (f), local operations follow
  527. a rule similar to rule (h), and operations for global types follow  a  rule
  528. similar  to rule (i).  In addition, if within the generic unit a predefined
  529. operator or basic operation of a formal  type  is  used,  then  within  the
  530. instance   the   corresponding   occurrence  refers  to  the  corresponding
  531. predefined operation of the actual type associated with the formal type. 
  532.  
  533. The above rules apply also to any type mark or (default)  expression  given
  534. within the generic formal part of the generic unit. 
  535.  
  536. For the elaboration of a generic instantiation, each expression supplied as
  537. an  explicit  generic  actual parameter is first evaluated, as well as each
  538. expression that appears as a constituent of a variable name or  entry  name
  539. supplied  as  an  explicit  generic  actual  parameter;   these evaluations
  540. proceed in some order that is not defined by the language.  Then, for  each
  541. omitted  generic association (if any), the corresponding default expression
  542. or default name is evaluated;  such evaluations are performed in the  order
  543. of  the  generic parameter declarations.  Finally, the implicitly generated  
  544. instance is elaborated.  The elaboration of  a  generic  instantiation  may
  545. also  involve  certain constraint checks as described in later subsections. 
  546.  
  547. Recursive generic instantiation is not allowed in the following sense:   if
  548. a  given  generic  unit includes an instantiation of a second generic unit,
  549. then the instance generated by  this  instantiation  must  not  include  an
  550. instance  of  the  first  generic  unit (whether this instance is generated
  551. directly, or indirectly by intermediate instantiations). 
  552.  
  553. Examples of generic instantiations (see 12.1): 
  554.  
  555.     procedure SWAP is new EXCHANGE(ELEM => INTEGER);
  556.     procedure SWAP is new EXCHANGE(CHARACTER);  --  SWAP is overloaded 
  557.  
  558.     function SQUARE is new SQUARING(INTEGER);  --  "*" of INTEGER used by default
  559.     function SQUARE is new SQUARING(ITEM => MATRIX, "*" => MATRIX_PRODUCT);
  560.     function SQUARE is new SQUARING(MATRIX, MATRIX_PRODUCT); -- same as previous    
  561.  
  562.     package INT_VECTORS is new ON_VECTORS(INTEGER, TABLE, "+"); 
  563.  
  564. Examples of uses of instantiated units: 
  565.  
  566.     SWAP(A, B);
  567.     A := SQUARE(A); 
  568.  
  569.     T : TABLE(1 .. 5) := (10, 20, 30, 40, 50);
  570.     N : INTEGER := INT_VECTORS.SIGMA(T);  --  150 (see 12.2 for the body of
  571.                                               SIGMA)
  572.     use INT_VECTORS;
  573.     M : INTEGER := SIGMA(T);  --  150                                            
  574.  
  575. Notes:
  576.  
  577. Omission of a generic actual parameter is only allowed if  a  corresponding
  578. default exists.  If default expressions or default names (other than simple
  579. names) are used, they are evaluated in the order in which the corresponding
  580. generic formal parameters are declared.  
  581.  
  582. If  two  overloaded subprograms declared in a generic package specification
  583. differ only by the (formal) type of  their  parameters  and  results,  then
  584. there  exist  legal instantiations for which all calls of these subprograms
  585. from outside the instance are ambiguous.  For example: 
  586.  
  587.     generic
  588.        type A is (<>);
  589.        type B is private;
  590.     package G is
  591.        function NEXT(X : A) return A;
  592.        function NEXT(X : B) return B;
  593.     end; 
  594.  
  595.     package P is new G(A => BOOLEAN, B => BOOLEAN);
  596.     -- calls of P.NEXT are ambiguous 
  597.  
  598. References:   declaration  3.1,   designator   6.1,   discriminant   3.7.1,
  599. elaboration 3.1 3.9, entity 3.1, entry name 9.5, evaluation 4.5, expression
  600. 4.4,  generic  formal  object  12.1, generic formal parameter 12.1, generic
  601. formal  subprogram  12.1,  generic  formal  type  12.1,  generic  parameter
  602. declaration   12.1,   global  declaration  8.1,  identifier  2.3,  implicit
  603. declaration 3.1, local declaration 8.1, mode in 12.1.1, mode in out 12.1.1,
  604. name 4.1, operation 3.3, operator symbol 6.1, overloading 6.6 8.7,  package
  605. 7, simple name 4.1, subprogram 6, subprogram call 6.4, subprogram name 6.1,
  606. subtype  declaration 3.3.2, type mark 3.3.2, variable 3.2.1, visibility 8.3          
  607.  
  608. 12.3.1  Matching Rules for Formal Objects  
  609.  
  610. A generic formal parameter of mode in of a given  type  is  matched  by  an
  611. expression of the same type.  If a generic unit has a generic formal object
  612. of  mode  in,  a check is made that the value of the expression  belongs to
  613. the subtype  denoted  by  the  type  mark,  as  for  an  explicit  constant
  614. declaration  (see  3.2.1). The exception CONSTRAINT_ERROR is raised if this
  615. check fails. 
  616.  
  617. A generic formal parameter of mode in out of a given type is matched by the
  618. name of a variable of the same type.  The variable must  not  be  a  formal
  619. parameter  of  mode  out or a subcomponent thereof.  The name must denote a  
  620. variable for which renaming is allowed (see 8.5). 
  621.  
  622. Notes: 
  623.  
  624. The type of a generic actual parameter of mode in must  not  be  a  limited
  625. type.   The constraints that apply to a generic formal parameter of mode in  
  626. out are those of the corresponding generic actual parameter  (see  12.1.1). 
  627.  
  628. References:   constraint  3.3,  constraint_error exception 11.1, expression
  629. 4.4, formal parameter 6.1, generic actual parameter  12.3,  generic  formal
  630. object  12.1.1,  generic formal parameter 12.1, generic instantiation 12.3,
  631. generic unit 12.1, limited type 7.4.4, matching  generic  actual  parameter
  632. 12.3,  mode  in 12.1.1, mode in out 12.1.1, mode out 6.2, name 4.1, raising
  633. of exceptions 11, satisfy 3.3, subcomponent 3.3, type 3.3, type mark 3.3.2,
  634. variable 3.2.1                                                                        
  635.  
  636. 12.3.2  Matching Rules for Formal Private Types
  637.   
  638. A generic formal private type is matched by any type or subtype (the actual
  639. subtype) that satisfies the following conditions: 
  640.  
  641.   -  If the formal type is not limited, the  actual  type  must  not  be  a
  642.      limited  type.  (If, on the other hand, the formal type is limited, no
  643.      such condition is imposed on the corresponding actual type, which  can
  644.      be limited or not limited.) 
  645.  
  646.   -  If the formal type has a discriminant part, the actual type must be  a
  647.      type   with   the  same  number  of  discriminants;   the  type  of  a
  648.      discriminant that appears at a given position in the discriminant part
  649.      of the actual type must be the same as the type  of  the  discriminant
  650.      that  appears  at  the  same  position in the discriminant part of the   
  651.      formal type;  and the actual subtype must be unconstrained.   (If,  on
  652.      the  other hand, the formal type has no discriminants, the actual type
  653.      is allowed to have discriminants.) 
  654.  
  655. Furthermore, consider any occurrence of the name of the formal  type  at  a
  656. place  where this name is used as an unconstrained subtype indication.  The
  657. actual subtype must not be an unconstrained array type or an  unconstrained
  658. type  with  discriminants,  if any of these occurrences is at a place where
  659. either a constraint or default discriminants would be required for an array
  660. type or for a type with discriminants (see  3.6.1  and  3.7.2).   The  same
  661. restriction  applies  to occurrences of the name of a subtype of the formal
  662. type, and to occurrences of the  name  of  any  type  or  subtype  derived,
  663. directly or indirectly, from the formal type. 
  664.  
  665. If  a  generic  unit  has  a  formal  private  type with discriminants, the
  666. elaboration of  a  corresponding  generic  instantiation  checks  that  the
  667. subtype  of each discriminant of the actual type is the same as the subtype
  668. of the corresponding  discriminant  of  the  formal  type.   The  exception
  669. CONSTRAINT_ERROR is raised if this check fails. 
  670.  
  671. References:   array  type  3.6,  constraint 3.3, constraint_error exception
  672. 11.1, default expression  for  a  discriminant  3.7.1,  derived  type  3.4,
  673. discriminant  3.7.1,  discriminant  part  3.7.1,  elaboration  3.9, generic
  674. actual type 12.3, generic body 12.2, generic formal  type  12.1.2,  generic
  675. instantiation   12.3,  generic  specification  12.1,  limited  type  7.4.4,
  676. matching generic actual parameter 12.3, name 4.1, private type 7.4, raising
  677. of exceptions 11, subtype 3.3, subtype indication  3.3.2,  type  3.3,  type
  678. with discriminants 3.3, unconstrained array type 3.6, unconstrained subtype
  679. 3.3
  680.     
  681. 12.3.3  Matching Rules for Formal Scalar Types    
  682.  
  683. A  generic  formal  type defined by (<>) is matched by any discrete subtype
  684. (that is, any enumeration or  integer  subtype).   A  generic  formal  type
  685. defined  by  range <> is matched by any integer subtype.   A generic formal  
  686. type defined by digits <> is matched by  any  floating  point  subtype.   A
  687. generic  formal  type  defined  by  delta  <> is matched by any fixed point
  688. subtype.  No other matches are possible for these generic formal types. 
  689.  
  690. References:  box delimiter 12.1.2,  discrete  type  3.5,  enumeration  type
  691. 3.5.1,  fixed  point  type 3.5.9, floating point type 3.5.7, generic actual
  692. type 12.3, generic  formal  type  12.1.2,  generic  type  definition  12.1,
  693. integer type 3.5.4, matching generic actual parameter 12.3, scalar type 3.5                                                 
  694.  
  695. 12.3.4  Matching Rules for Formal Array Types
  696.   
  697. A  formal  array  type is matched by an actual array subtype that satisfies
  698. the following conditions: 
  699.  
  700.   -  The formal array type and the actual array type  must  have  the  same
  701.      dimensionality;  the formal type and the actual subtype must be either
  702.      both constrained or both unconstrained. 
  703.  
  704.   -  For each index position, the index type  must  be  the  same  for  the
  705.      actual array type as for the formal array type. 
  706.  
  707.   -  The component type must be the same for the actual array type  as  for
  708.      the  formal  array type.  If the component type is other than a scalar
  709.      type, then the component subtypes must be either both  constrained  or
  710.      both unconstrained. 
  711.  
  712. If  a  generic  unit  has  a  formal  array  type,  the  elaboration  of  a  
  713. corresponding instantiation checks that the constraints  (if  any)  on  the
  714. component  type  are  the  same for the actual array type as for the formal
  715. array type, and likewise that  for  any  given  index  position  the  index
  716. subtypes  or  the  discrete  ranges  have  the  same bounds.  The exception
  717. CONSTRAINT_ERROR is raised if this check fails. 
  718.  
  719. Example: 
  720.  
  721.     --  given the generic package 
  722.  
  723.     generic
  724.        type ITEM   is private;
  725.        type INDEX  is (<>);
  726.        type VECTOR is array (INDEX range <>) of ITEM;
  727.        type TABLE  is array (INDEX) of ITEM;
  728.     package P is
  729.        ...
  730.     end; 
  731.  
  732.     --  and the types 
  733.  
  734.     type MIX    is array (COLOR range <>) of BOOLEAN;
  735.     type OPTION is array (COLOR) of BOOLEAN; 
  736.  
  737.     --  then MIX can match VECTOR and OPTION can match TABLE 
  738.  
  739.     package R is new P(ITEM   => BOOLEAN, INDEX => COLOR,
  740.                        VECTOR => MIX,     TABLE => OPTION); 
  741.  
  742.     --  Note that MIX cannot match TABLE and OPTION cannot match VECTOR
  743.  
  744. Note:
  745.  
  746. For the above rules, if any of the index or component types of  the  formal
  747. array  type  is  itself  a  formal  type, then within the instance its name
  748. denotes the corresponding actual subtype (see 12.3(d)).  
  749.  
  750. References:  array type 3.6, array type definition  3.6,  component  of  an
  751. array  3.6,  constrained  array  type 3.6, constraint 3.3, constraint_error
  752. exception 11.1, elaboration 3.9, formal  type  12.1,  generic  formal  type
  753. 12.1.2,  generic  instantiation  12.3,  index  3.6, index constraint 3.6.1,
  754. matching generic actual parameter 12.3, raise statement 11.3, subtype  3.3,
  755. unconstrained array type 3.6                                                     
  756.  
  757. 12.3.5  Matching Rules for Formal Access Types
  758.   
  759. A  formal access type is matched by an actual access subtype if the type of
  760. the designated objects is the same for the actual type as  for  the  formal
  761. type.   If  the  designated  type  is  other  than  a scalar type, then the
  762. designated subtypes must be either both constrained or both  unconstrained. 
  763.  
  764. If  a  generic  unit  has  a  formal  access  type,  the  elaboration  of a
  765. corresponding instantiation checks that any constraints on  the  designated
  766. objects are the same for the actual access subtype as for the formal access
  767. type.  The exception CONSTRAINT_ERROR is raised if this check fails. 
  768.  
  769. Example: 
  770.  
  771.     --  the formal types of the generic package 
  772.  
  773.     generic
  774.        type NODE is private;
  775.        type LINK is access NODE;
  776.     package P is
  777.        ...
  778.     end; 
  779.  
  780.     --  can be matched by the actual types 
  781.  
  782.     type CAR;
  783.     type CAR_NAME is access CAR; 
  784.  
  785.     type CAR is
  786.        record
  787.           PRED, SUCC : CAR_NAME;
  788.           NUMBER     : LICENSE_NUMBER;
  789.           OWNER      : PERSON;
  790.        end record;  
  791.  
  792.     --  in the following generic instantiation 
  793.  
  794.     package R is new P(NODE => CAR, LINK => CAR_NAME);
  795.  
  796. Note: 
  797.  
  798. For  the  above rules, if the designated type is itself a formal type, then
  799. within the instance its name denotes the corresponding actual subtype  (see
  800. 12.3(d)). 
  801.  
  802. References:   access  type 3.8, access type definition 3.8, constraint 3.3,
  803. constraint_error exception 11.1, designate 3.8,  elaboration  3.9,  generic
  804. formal  type  12.1.2,  generic  instantiation 12.3, matching generic actual
  805. parameter 12.3, object 3.2, raise statement 11.3, value of access type  3.8      
  806.  
  807. 12.3.6  Matching Rules for Formal Subprograms
  808.    
  809. A  formal  subprogram  is  matched  by  an  actual  subprogram, enumeration
  810. literal, or entry if both have the same parameter and result  type  profile
  811. (see  6.6);   in  addition,  parameter  modes  must be identical for formal
  812. parameters that are at the same parameter position. 
  813.  
  814. If a generic unit has a default subprogram specified by a name,  this  name
  815. must denote a subprogram, an enumeration literal, or an entry, that matches
  816. the  formal subprogram (in the above sense).  The evaluation of the default
  817. name takes place during the elaboration of each instantiation that uses the
  818. default,  as defined in section 12.3. 
  819.  
  820. If a generic unit has  a  default  subprogram  specified   by  a  box,  the
  821. corresponding  actual parameter can be omitted if a subprogram, enumeration
  822. literal, or entry  matching  the  formal  subprogram,  and  with  the  same
  823. designator  as  the  formal subprogram, is directly visible at the place of
  824. the generic instantiation;  this subprogram, enumeration literal, or  entry
  825. is  then used by default (there must be exactly one subprogram, enumeration
  826. literal, or entry satisfying the previous conditions). 
  827.  
  828. Example: 
  829.  
  830.     --  given the generic function specification 
  831.  
  832.     generic
  833.        type ITEM is private;
  834.        with function "*" (U, V : ITEM) return ITEM is <>;
  835.     function SQUARING(X : ITEM) return ITEM; 
  836.  
  837.     --  and the function 
  838.  
  839.     function MATRIX_PRODUCT(A, B : MATRIX) return MATRIX; 
  840.  
  841.     --  the following instantiation is possible 
  842.  
  843.     function SQUARE is new SQUARING(MATRIX, MATRIX_PRODUCT); 
  844.  
  845.     --  the following instantiations are equivalent 
  846.  
  847.     function SQUARE is new SQUARING(ITEM => INTEGER, "*" => "*");
  848.     function SQUARE is new SQUARING(INTEGER, "*");
  849.     function SQUARE is new SQUARING(INTEGER); 
  850.  
  851. Notes: 
  852.  
  853. The matching rules for  formal  subprograms  state  requirements  that  are   
  854. similar  to  those  applying to subprogram renaming declarations (see 8.5).
  855. In particular, the name of a parameter of the formal subprogram need not be
  856. the same as that of the corresponding parameter of the  actual  subprogram;
  857. similarly,  for  these parameters, default expressions need not correspond. 
  858.  
  859. A formal subprogram is matched by an attribute of a type if  the  attribute
  860. is  a  function with a matching specification.  An enumeration literal of a  
  861. given type matches a parameterless formal function whose result type is the
  862. given type. 
  863.  
  864. References:  attribute 4.1.4, box delimiter 12.1.2, designator  6.1,  entry
  865. 9.5,  function  6.5,  generic  actual  type 12.3, generic formal subprogram
  866. 12.1.3, generic formal type 12.1.2, generic  instantiation  12.3,  matching
  867. generic  actual parameter 12.3, name 4.1, parameter and result type profile
  868. 6.3, subprogram 6, subprogram specification 6.1,  subtype  3.3,  visibility 8.3                                                
  869.  
  870. 12.4  Example of a Generic Package
  871.   
  872. The following example provides a possible formulation of stacks by means of
  873. a  generic  package.   The  size  of  each  stack and the type of the stack
  874. elements are provided as generic parameters. 
  875.  
  876.     generic
  877.        SIZE : POSITIVE;
  878.        type ITEM is private;
  879.     package STACK is
  880.        procedure PUSH(E : in  ITEM);
  881.        procedure POP (E : out ITEM);
  882.        OVERFLOW, UNDERFLOW : exception;
  883.     end STACK; 
  884.  
  885.     package body STACK is 
  886.  
  887.        type TABLE is array (POSITIVE range <>) of ITEM;
  888.        SPACE : TABLE(1 .. SIZE);
  889.        INDEX : NATURAL := 0; 
  890.  
  891.        procedure PUSH(E : in ITEM) is
  892.        begin
  893.           if INDEX >= SIZE then
  894.              raise OVERFLOW;
  895.           end if;
  896.           INDEX := INDEX + 1;
  897.           SPACE(INDEX) := E;
  898.        end PUSH; 
  899.  
  900.        procedure POP(E : out ITEM) is
  901.        begin
  902.           if INDEX = 0 then
  903.              raise UNDERFLOW;
  904.           end if;
  905.           E := SPACE(INDEX);
  906.           INDEX := INDEX - 1;
  907.        end POP; 
  908.  
  909.     end STACK; 
  910.  
  911. Instances of this generic package can be obtained as follows: 
  912.  
  913.     package STACK_INT  is new STACK(SIZE => 200, ITEM => INTEGER);
  914.     package STACK_BOOL is new STACK(100, BOOLEAN);
  915.  
  916. Thereafter, the procedures of the instantiated packages can  be  called  as
  917. follows: 
  918.  
  919.     STACK_INT.PUSH(N);
  920.     STACK_BOOL.PUSH(TRUE);                                                       
  921.  
  922. Alternatively,  a  generic  formulation  of  the type STACK can be given as
  923. follows (package body omitted): 
  924.  
  925.     generic
  926.        type ITEM is private;
  927.     package ON_STACKS is
  928.        type STACK(SIZE : POSITIVE) is limited private;
  929.        procedure PUSH(S : in out STACK; E : in  ITEM);
  930.        procedure POP (S : in out STACK; E : out ITEM);
  931.        OVERFLOW, UNDERFLOW : exception;
  932.     private
  933.        type TABLE is array (POSITIVE range <>) of ITEM;
  934.        type STACK(SIZE : POSITIVE) is
  935.           record
  936.              SPACE : TABLE(1 .. SIZE);
  937.              INDEX : NATURAL := 0;
  938.           end record;
  939.     end; 
  940.  
  941. In order to use such a  package,  an  instantiation  must  be  created  and
  942. thereafter stacks of the corresponding type can be declared: 
  943.  
  944.     declare
  945.        package STACK_REAL is new ON_STACKS(REAL); use STACK_REAL;
  946.        S : STACK(100)
  947.     begin
  948.        ...
  949.        PUSH(S, 2.54);
  950.        ...
  951.     end;                           
  952.  
  953.