@Part(03, Root="ada.mss") @Comment{$Date: 2005/11/24 02:15:00 $} @Comment{$Source: e:\\cvsroot/ARM/Source/03b.mss,v $} @Comment{$Revision: 1.62 $} @LabeledClause{Array Types} @begin{Intro} @Defn{array} @Defn{array type} An @i(array) object is a composite object consisting of components which all have the same subtype. The name for a component of an array uses one or more index values belonging to specified discrete types. The value of an array object is a composite value consisting of the values of the components. @end{Intro} @begin{Syntax} @Syn{lhs=,rhs=" @Syn2{unconstrained_array_definition} | @Syn2{constrained_array_definition}"} @Syn{lhs=,rhs=" @key{array}(@Syn2{index_subtype_definition} {, @Syn2{index_subtype_definition}}) @key{of} @Syn2{component_definition}"} @Syn{lhs=,rhs="@Syn2{subtype_mark} @key{range} <>"} @Syn{lhs=,rhs=" @key{array} (@Syn2{discrete_subtype_definition} {, @Syn2{discrete_subtype_definition}}) @key{of} @Syn2{component_definition}"} @Syn{lhs=,rhs="@SynI{discrete_}@Syn2{subtype_indication} | @Syn2{range}"} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00406-01]} @Syn{lhs=,rhs="@Chg{Version=[2],New=< >,Old=<>}[@key{aliased}] @Syn2{subtype_indication}@Chg{Version=[2],New=< | [@key{aliased}] @Syn2{access_definition}>,Old=<>}"} @end{Syntax} @begin{Resolution} @PDefn2{Term=[expected type], Sec=(discrete_subtype_definition range)} For a @nt that is a @nt, the @nt shall resolve to be of some specific discrete type@Redundant[; which discrete type shall be determined without using any context other than the bounds of the @nt itself (plus the preference for @i(root_integer) @em see @RefSecNum(The Context of Overload Resolution)).] @end{Resolution} @begin{Legality} @Defn{index subtype} Each @nt{index_subtype_definition} or @nt{discrete_subtype_definition} in an @nt{array_type_definition} defines an @i(index subtype); @Defn{index type} its type (the @i(index type)) shall be discrete. @begin{Discussion} @Defn2{Term=[index], Sec=(of an array)} An @i(index) is a discrete quantity used to select along a given dimension of an array. A component is selected by specifying corresponding values for each of the indices. @end{Discussion} @Defn{component subtype} The subtype defined by the @nt of a @nt (the @i(component subtype)) shall be a definite subtype. @begin{Ramification} This applies to all uses of @nt, including in @nts and @nts.@end{ramification} @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI-00363-01]} @ChgDeleted{Version=[2],Text=[Within the definition of a nonlimited composite type (or a limited composite type that later in its immediate scope becomes nonlimited @em see @RefSecNum{Private Operations} and @RefSecNum{Limited Types}), if a @nt{component_definition} contains the reserved word @key[aliased] and the type of the component is discriminated, then the nominal subtype of the component shall be constrained.]} @begin{Reason} @ChgRef{Version=[2],Kind=[Deleted]} @ChgNote{The rule's gone, we might as well clobber all the notes.} @ChgDeleted{Version=[2],Text=[If we allowed the subtype to be unconstrained, then the discriminants might change because of an assignment to the containing (nonlimited) object, thus causing a potential violation of an access subtype constraint of an access value designating the aliased component.]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[Note that the rule elsewhere defining all aliased discriminated objects to be constrained does not help @em that rule prevents assignments to the component itself from doing any harm, but not assignments to the containing object.]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Type=[Leading],Text=[We allow this for components within limited types since assignment to the enclosing object is not a problem. Furthermore, it is important to be able to use a default expression for a discriminant in arrays of limited components, since that is the only way to give the components different values for their discriminants. For example:]} @begin{Example} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[@key[protected] @key[type] Counter_Type(Initial_Value : Integer := 1) @key[is] @key[procedure] Get_Next(Next_Value : @key[out] Integer); --@RI{ Returns the next value on each call, bumping Count} --@RI{ before returning.} @key[private] Count : Integer := Initial_Value; @key[end] Counter_Type; @key[protected] @key[body] Counter_Type @key[is] ...]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[@key[function] Next_Id(Counter : @key[access] Counter_Type) @key[return] Integer @key[is] Result : Integer; @key[begin] Counter.Get_Next(Result); @key[return] Result; @key[end] Next_Id;]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[C : @key[aliased] Counter_Type; @key[task] @key[type] T(Who_Am_I : Integer := Next_Id(C'Access)); @key[task] @key[body] T @key[is] ...]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[Task_Array : @key[array](1..100) @key[of] @key[aliased] T; --@RI{ Array of task elements, each with its own unique ID.} --@RI{ We specify "aliased" so we can use Task_Array(I)'Access.} --@RI{ This is safe because Task_Array is of a limited type,} --@RI{ so there is no way an assignment to it could change} --@RI{ the discriminants of one of its components.}]} @end{Example} @end{Reason} @begin{Ramification} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[Note that this rule applies to array components and record components, but not to protected type components (since they are always limited).]} @end{Ramification} @end{Legality} @begin{StaticSem} @Defn2{Term=[dimensionality], Sec=(of an array)} @Defn{one-dimensional array} @Defn{multi-dimensional array} An array is characterized by the number of indices (the @i(dimensionality) of the array), the type and position of each index, the lower and upper bounds for each index, and the subtype of the components. The order of the indices is significant. A one-dimensional array has a distinct component for each possible index value. A multidimensional array has a distinct component for each possible sequence of index values that can be formed by selecting one value for each index position (in the given order). The possible values for a given index are all the values between the lower and upper bounds, inclusive; @Defn{index range} this range of values is called the @i(index range). @Defn2{Term=[bounds], Sec=(of an array)} The @i(bounds) of an array are the bounds of its index ranges. @Defn2{Term=[length], Sec=(of a dimension of an array)} The @i(length) of a dimension of an array is the number of values of the index range of the dimension (zero for a null range). @Defn2{Term=[length], Sec=(of a one-dimensional array)} The @i(length) of a one-dimensional array is the length of its only dimension. An @nt defines an array type and its first subtype. For each object of this array type, the number of indices, the type and position of each index, and the subtype of the components are as in the type definition@Redundant[; the values of the lower and upper bounds for each index belong to the corresponding index subtype of its type, except for null arrays (see @RefSecNum(Index Constraints and Discrete Ranges))]. @Defn2{Term=[constrained], Sec=(subtype)} @Defn2{Term=[unconstrained], Sec=(subtype)} An @nt{unconstrained_array_definition} defines an array type with an unconstrained first subtype. Each @nt{index_@!subtype_@!definition} defines the corresponding index subtype to be the subtype denoted by the @nt{subtype_@!mark}. @redundant[@PDefn2{Term=[box], Sec=(compound delimiter)} The compound delimiter <> (called a @i(box)) of an @nt stands for an undefined range (different objects of the type need not have the same bounds).] @Defn2{Term=[constrained], Sec=(subtype)} @Defn2{Term=[unconstrained], Sec=(subtype)} A @nt{constrained_array_definition} defines an array type with a constrained first subtype. Each @nt{discrete_@!subtype_@!definition} defines the corresponding index subtype, as well as the corresponding index range for the constrained first subtype. @PDefn2{Term=[constraint], Sec=(of a first array subtype)} The @i(constraint) of the first subtype consists of the bounds of the index ranges. @begin{Discussion} Although there is no namable unconstrained array subtype in this case, the predefined slicing and concatenation operations can operate on and yield values that do not necessarily belong to the first array subtype. This is also true for Ada 83. @end{Discussion} @Leading@;The discrete subtype defined by a @nt{discrete_@!subtype_@!definition} is either that defined by the @nt{subtype_@!indication}, or a subtype determined by the @nt{range} as follows: @begin(itemize) If the type of the @nt{range} resolves to @i(root_integer), then the @nt{discrete_subtype_definition} defines a subtype of the predefined type Integer with bounds given by a conversion to Integer of the bounds of the @nt; @PDefn2{Term=[implicit subtype conversion],Sec=(bounds of a range)} @begin{Reason} This ensures that indexing over the discrete subtype can be performed with regular Integers, rather than only @i(universal_integer)s. @end{Reason} @begin{Discussion} We considered doing this by simply creating a @lquotes@;preference@rquotes@; for Integer when resolving the @nt. @PDefn{Beaujolais effect} However, this can introduce @i(Beaujolais) effects when the @nts involve calls on functions visible due to @key(use) clauses. @end{Discussion} Otherwise, the @nt{discrete_subtype_definition} defines a subtype of the type of the @nt{range}, with the bounds given by the @nt. @end(itemize) @PDefn2{Term=[nominal subtype], Sec=(of a component)} The @nt{component_definition} of an @nt defines the nominal subtype of the components. If the reserved word @key(aliased) appears in the @nt{component_definition}, then each component of the array is aliased (see @RefSecNum{Access Types}). @begin(Ramification) @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI-00363-01]} @ChgDeleted{Version=[2],Text=[In this case, the nominal subtype cannot be an unconstrained discriminated subtype. See @RefSecNum{Record Types}.]} @end(Ramification) @end{StaticSem} @begin{RunTime} @PDefn2{Term=[elaboration], Sec=(array_type_definition)} The elaboration of an @nt{array_type_definition} creates the array type and its first subtype, and consists of the elaboration of any @nt{discrete_@!subtype_@!definition}s and the @nt{component_@!definition}. @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI-00171-01]} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI-00230-01]} @PDefn2{Term=[elaboration], Sec=(discrete_subtype_definition)} The elaboration of a @nt{discrete_subtype_definition} @Chg{New=[that does not contain any per-object expressions],Old=[]} creates the discrete subtype, and consists of the elaboration of the @nt{subtype_@!indication} or the evaluation of the @nt{range}. @Chg{New=[The elaboration of a @nt{discrete_subtype_definition} that contains one or more per-object expressions is defined in @RefSecNum{Record Types}.],Old=[]} @PDefn2{Term=[elaboration], Sec=(component_definition)} The elaboration of a @nt{component_@!definition} in an @nt{array_@!type_@!definition} consists of the elaboration of the @nt{subtype_@!indication}@Chg{Version=[2],New=[ or @nt{access_definition}],Old=[]}. The elaboration of any @nt{discrete_@!subtype_@!definition}s and the elaboration of the @nt{component_@!definition} are performed in an arbitrary order. @end{RunTime} @begin{Notes} All components of an array have the same subtype. In particular, for an array of components that are one-dimensional arrays, this means that all components have the same bounds and hence the same length. Each elaboration of an @nt creates a distinct array type. A consequence of this is that each object whose @nt contains an @nt is of its own unique type. @end{Notes} @begin{Examples} @Leading@keepnext@i(Examples of type declarations with unconstrained array definitions: ) @begin(Example) @key(type) Vector @key(is) @key(array)(Integer @key(range) <>) @key(of) Real; @key(type) Matrix @key(is) @key(array)(Integer @key(range) <>, Integer @key(range) <>) @key(of) Real; @key(type) Bit_Vector @key(is) @key(array)(Integer @key(range) <>) @key(of) Boolean; @key(type) Roman @key(is) @key(array)(Positive @key(range) <>) @key(of) Roman_Digit; --@RI[ see @RefSecNum(Character Types)] @end(Example) @begin{Wide} @leading@keepnext@i(Examples of type declarations with constrained array definitions: ) @end{Wide} @begin(Example) @key(type) Table @key(is) @key(array)(1 .. 10) @key(of) Integer; @key(type) Schedule @key(is) @key(array)(Day) @key(of) Boolean; @key(type) Line @key(is) @key(array)(1 .. Max_Line_Size) @key(of) Character; @end(Example) @begin{Wide} @leading@keepnext@i(Examples of object declarations with array type definitions: ) @end{Wide} @begin(Example) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI-00433-01]} Grid @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(1 .. 80, 1 .. 100) @key(of) Boolean; Mix @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(Color @key(range) Red .. Green) @key(of) Boolean;@Chg{Version=[2],New=[ Msg_Table : @key(constant array)(Error_Code) @key(of access constant) String := (Too_Big => @key(new) String'("Result too big"), Too_Small => ...);],Old=[]} Page @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(Positive @key(range) <>) @key(of) Line := --@RI[ an array of arrays] (1 | 50 => Line'(1 | Line'Last => '+', @key(others) => '-'), --@RI[ see @RefSecNum(Array Aggregates)] 2 .. 49 => Line'(1 | Line'Last => '|', @key(others) => ' ')); --@RI[ Page is constrained by its initial value to (1..50)] @end(Example) @end{Examples} @begin{Extend83} @Defn{extensions to Ada 83} The syntax rule for @nt{component_definition} is modified to allow the reserved word @key{aliased}. The syntax rules for @nt{unconstrained_array_definition} and @nt{constrained_array_definition} are modified to use @nt{component_definition} (instead of @i(component_)@nt{subtype_indication}). The effect of this change is to allow the reserved word @key{aliased} before the component @nt{subtype_indication}. A @nt{range} in a @nt{discrete_subtype_definition} may use arbitrary universal expressions for each bound (e.g. @en@;1 .. 3+5), rather than strictly "implicitly convertible" operands. The subtype defined will still be a subtype of Integer. @end{Extend83} @begin{DiffWord83} We introduce a new syntactic category, @nt{discrete_subtype_definition}, as distinct from @nt{discrete_range}. These two constructs have the same syntax, but their semantics are quite different (one defines a subtype, with a preference for Integer subtypes, while the other just selects a subrange of an existing subtype). We use this new syntactic category in @key(for) loops and entry families. The syntax for @nt{index_constraint} and @nt{discrete_range} have been moved to their own subclause, since they are no longer used here. The syntax rule for @nt{component_definition} (formerly @nt) is moved here from RM83-3.7. @end{DiffWord83} @begin{Extend95} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00230-01],ARef=[AI95-00406-01]} @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} Array components can have an anonymous access type.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]} @ChgAdded{Version=[2],Text=[The prohibition against unconstrained discriminated aliased components has been lifted. It has been replaced by a prohibition against the actual troublemakers: general access discriminant constraints (see @RefSecNum{Discriminant Constraints}).]} @end{Extend95} @begin{DiffWord95} @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]} @ChgAdded{Version=[2],Text=[@b Added wording to allow the elaboration of per-object constraints for constrained arrays.]} @end{DiffWord95} @LabeledSubClause{Index Constraints and Discrete Ranges} @begin{Intro} An @nt determines the range of possible values for every index of an array subtype, and thereby the corresponding array bounds. @end{Intro} @begin{Syntax} @Syn{lhs=,rhs=" (@Syn2{discrete_range} {, @Syn2{discrete_range}})"} @Syn{lhs=,rhs="@SynI{discrete_}@Syn2{subtype_indication} | @Syn2{range}"} @end{Syntax} @begin{Resolution} @Defn{type of a @nt{discrete_range}} The type of a @nt is the type of the subtype defined by the @nt, or the type of the @nt. @PDefn2{Term=[expected type], Sec=(index_constraint discrete_range)} For an @nt{index_constraint}, each @nt{discrete_range} shall resolve to be of the type of the corresponding index. @begin(Discussion) In Ada 95, @nt{index_constraint}s only appear in a @nt{subtype_indication}; they no longer appear in @nts. @end(Discussion) @end{Resolution} @begin{Legality} An @nt{index_constraint} shall appear only in a @nt{subtype_indication} whose @nt{subtype_mark} denotes either an unconstrained array subtype, or an unconstrained access subtype whose designated subtype is an unconstrained array subtype; in either case, the @nt{index_constraint} shall provide a @nt{discrete_range} for each index of the array type. @end{Legality} @begin{StaticSem} @Defn2{Term=[bounds], Sec=(of a @nt)} A @nt{discrete_range} defines a range whose bounds are given by the @nt{range}, or by the range of the subtype defined by the @nt{subtype_indication}. @end{StaticSem} @begin{RunTime} @PDefn2{Term=[compatibility], Sec=(index constraint with a subtype)} An @nt{index_constraint} is @i(compatible) with an unconstrained array subtype if and only if the index range defined by each @nt{discrete_range} is compatible (see @RefSecNum(Scalar Types)) with the corresponding index subtype. @Defn{null array} If any of the @nts defines a null range, any array thus constrained is a @i(null array), having no components. @PDefn2{Term=[satisfies], Sec=(an index constraint)} An array value @i(satisfies) an @nt{index_constraint} if at each index position the array value and the @nt{index_constraint} have the same index bounds. @begin{Ramification} There is no need to define compatibility with a constrained array subtype, because one is not allowed to constrain it again.@end{ramification} @PDefn2{Term=[elaboration], Sec=(index_constraint)} The elaboration of an @nt{index_constraint} consists of the evaluation of the @nt{discrete_range}(s), in an arbitrary order. @PDefn2{Term=[evaluation], Sec=(discrete_range)} The evaluation of a @nt{discrete_range} consists of the elaboration of the @nt{subtype_indication} or the evaluation of the @nt{range}. @end{RunTime} @begin{Notes} The elaboration of a @nt consisting of a @nt followed by an @nt checks the compatibility of the @nt with the @nt (see @RefSecNum(Subtype Declarations)). Even if an array value does not satisfy the index constraint of an array subtype, Constraint_Error is not raised on conversion to the array subtype, so long as the length of each dimension of the array value and the array subtype match. See @RefSecNum(Type Conversions). @end{Notes} @begin{Examples} @leading@keepnext@i(Examples of array declarations including an index constraint: ) @begin(Example) Board : Matrix(1 .. 8, 1 .. 8); --@RI[ see @RefSecNum(Array Types)] Rectangle : Matrix(1 .. 20, 1 .. 30); Inverse : Matrix(1 .. N, 1 .. N); --@RI[ N need not be static ] Filter : Bit_Vector(0 .. 31); @end(Example) @begin{Wide} @leading@keepnext@i(Example of array declaration with a constrained array subtype: ) @end{Wide} @begin(Example) My_Schedule : Schedule; --@RI[ all arrays of type Schedule have the same bounds] @end(Example) @begin{Wide} @leading@keepnext@i(Example of record type with a component that is an array: ) @end{Wide} @begin(Example) @key(type) Var_Line(Length : Natural) @key(is) @key(record) Image : String(1 .. Length); @key(end) @key(record); Null_Line : Var_Line(0); --@RI[ Null_Line.Image is a null array] @end(Example) @end{Examples} @begin{Extend83} @Defn{extensions to Ada 83} We allow the declaration of a variable with a nominally unconstrained array subtype, so long as it has an initialization expression to determine its bounds. @end{Extend83} @begin{DiffWord83} We have moved the syntax for @nt{index_constraint} and @nt{discrete_range} here since they are no longer used in @nt{constrained_array_definition}s. We therefore also no longer have to describe the (special) semantics of @nt{index_constraint}s and @nt{discrete_range}s that appear in @nt{constrained_array_definition}s. The rules given in RM83-3.6.1(5,7-10), which define the bounds of an array object, are redundant with rules given elsewhere, and so are not repeated here. RM83-3.6.1(6), which requires that the (nominal) subtype of an array variable be constrained, no longer applies, so long as the variable is explicitly initialized. @end{DiffWord83} @LabeledSubClause{Operations of Array Types} @begin{Legality} @Redundant[The argument N used in the @nts for the N-th dimension of an array shall be a static @nt of some integer type.] The value of N shall be positive (nonzero) and no greater than the dimensionality of the array. @end{Legality} @begin{StaticSem} @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0006],ARef=[AI95-00030-01]} The following attributes are defined for @ChgPrefixType{Version=[1],Kind=[Revised],Text=[a @Chg{New=[@nt{prefix}], Old=[prefix]} A that is of an array type @Redundant[(after any implicit dereference)], or denotes a constrained array subtype]}: @begin{Ramification} These attributes are not defined if A is a subtype-mark for an access-to-array subtype. They are defined (by implicit dereference) for access-to-array values.@end{ramification} @begin(description) @Attribute{Prefix=, AttrName=, Text=[A'First denotes the lower bound of the first index range; its type is the corresponding index type.]} @Attribute{Prefix=, AttrName=, Text=[A'First(N) denotes the lower bound of the N-th index range; its type is the corresponding index type.]} @Attribute{Prefix=, AttrName=, Text=[A'Last denotes the upper bound of the first index range; its type is the corresponding index type.]} @Attribute{Prefix=, AttrName=, Text=[A'Last(N) denotes the upper bound of the N-th index range; its type is the corresponding index type.]} @Attribute{Prefix=, AttrName=, Text=[A'Range is equivalent to the range A'First .. A'Last, except that the @nt A is only evaluated once.]} @Attribute{Prefix=, AttrName=, Text=[A'Range(N) is equivalent to the range A'First(N) .. A'Last(N), except that the @nt A is only evaluated once.]} @Attribute{Prefix=, AttrName=, Text=[A'Length denotes the number of values of the first index range (zero for a null range); its type is @i(universal_integer).]} @Attribute{Prefix=, AttrName=, Text=[A'Length(N) denotes the number of values of the N-th index range (zero for a null range); its type is @i(universal_integer).]} @end(description) @EndPrefixType{} @end{StaticSem} @begin{ImplAdvice} An implementation should normally represent multidimensional arrays in row-major order, consistent with the notation used for multidimensional array aggregates (see @RefSecNum(Array Aggregates)). However, if a @key Convention(Fortran, ...) applies to a multidimensional array type, then column-major order should be used instead (see @RefSec{Interfacing with Fortran}). @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2], Text=[Multidimensional arrays should be represented in row-major order, unless the array has convention Fortran.]}]} @end{ImplAdvice} @begin{Notes} @Leading@;The @nts A'First and A'First(1) denote the same value. A similar relation exists for the @nts A'Last, A'Range, and A'Length. The following relation is satisfied (except for a null array) by the above attributes if the index type is an integer type: @begin(example) A'Length(N) = A'Last(N) - A'First(N) + 1 @end(example) An array type is limited if its component type is limited (see @RefSecNum(Limited Types)). @PDefn2{Term=[predefined operations],Sec=(of an array type)} The predefined operations of an array type include the membership tests, qualification, and explicit conversion. If the array type is not limited, they also include assignment and the predefined equality operators. For a one-dimensional array type, they include the predefined concatenation operators (if nonlimited) and, if the component type is discrete, the predefined relational operators; if the component type is boolean, the predefined logical operators are also included. @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]} A component of an array can be named with an @nt. A value of an array type can be specified with an @nt@Chg{Version=[2],New=[],Old=[, unless the array type is limited]}. For a one-dimensional array type, a slice of the array can be named; also, string literals are defined if the component type is a character type. @end{Notes} @begin{Examples} @Leading@keepnext@i{Examples (using arrays declared in the examples of subclause @RefSecNum(Index Constraints and Discrete Ranges)):} @begin(Example) -- Filter'First = 0 Filter'Last = 31 Filter'Length = 32 -- Rectangle'Last(1) = 20 Rectangle'Last(2) = 30 @end(Example) @end{Examples} @LabeledSubClause{String Types} @begin{StaticSem} @Defn{string type} A one-dimensional array type whose component type is a character type is called a @i(string) type. @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00285-01]} @Leading@redundant[There are @Chg{Version=[2],New=[three],Old=[two]} predefined string types, String@Chg{Version=[2],New=[,],Old=[ and]} Wide_String@Chg{Version=[2],New=[, and Wide_Wide_String],Old=[]}, each indexed by values of the predefined subtype Positive; these are declared in the visible part of package Standard:] @begin(example) @redundant[@key(subtype) Positive @key(is) Integer @key(range) 1 .. Integer'Last; @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00285-01]} @key(type) String @key(is) @key(array)(Positive @key(range) <>) @key(of) Character; @key(type) Wide_String @key(is) @key(array)(Positive @key(range) <>) @key(of) Wide_Character; @Chg{Version=[2],New=[@key(type) Wide_Wide_String @key(is) @key(array)(Positive @key(range) <>) @key(of) Wide_Wide_Character;],Old=[]} ]@end(example) @end{StaticSem} @begin{Notes} String literals (see @RefSecNum(String Literals) and @RefSecNum(Literals)) are defined for all string types. The concatenation operator & is predefined for string types, as for all nonlimited one-dimensional array types. The ordering operators <, <=, >, and >= are predefined for string types, as for all one-dimensional discrete array types; these ordering operators correspond to lexicographic order (see @RefSecNum(Relational Operators and Membership Tests)). @end{Notes} @begin{Examples} @Leading@keepnext@i(Examples of string objects:) @begin(Example) @TabClear()@TabSet(P50) Stars : String(1 .. 120) := (1 .. 120 => '*' ); Question : @key(constant) String := "How many characters?"; @\--@RI[ Question'First = 1, Question'Last = 20] @\--@RI[ Question'Length = 20 (the number of characters)] Ask_Twice : String := Question & Question;@\--@RI[ constrained to (1..40)] Ninety_Six : @key(constant) Roman := "XCVI";@\--@RI[ see @RefSecNum(Character Types) and @RefSecNum(Array Types)] @end(Example) @end{Examples} @begin{Inconsistent83} @Defn{inconsistencies with Ada 83} The declaration of Wide_String in Standard hides a use-visible declaration with the same @nt. In rare cases, this might result in an inconsistency between Ada 83 and Ada 95. @end{Inconsistent83} @begin{Incompatible83} @Defn{incompatibilities with Ada 83} Because both String and Wide_String are always directly visible, an expression like @begin(Example) "a" < "bc" @end(Example) is now ambiguous, whereas in Ada 83 both string literals could be resolved to type String. @end{Incompatible83} @begin{Extend83} @Defn{extensions to Ada 83} The type Wide_String is new (though it was approved by ARG for Ada 83 compilers as well). @end{Extend83} @begin{DiffWord83} We define the term @i(string type) as a natural analogy to the term @i(character type). @end{DiffWord83} @begin{Inconsistent95} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00285-01]} @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95} The declaration of Wide_Wide_String in Standard hides a use-visible declaration with the same @nt. In the (very) unlikely event that an Ada 95 program had depended on such a use-visible declaration, and the program remains legal after the substitution of Standard.Wide_Wide_String, the meaning of the program will be different.]} @end{Inconsistent95} @begin{Extend95} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00285-01]} @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} The type Wide_Wide_String is new.]} @end{Extend95} @LabeledClause{Discriminants} @begin{Intro} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01],ARef=[AI95-00326-01]} @redundant[@Defn{discriminant} @IndexSee{Term=[type parameter],See=(discriminant)} @IndexSeeAlso{Term=[parameter],See=(discriminant)} A composite type (other than an array@Chg{Version=[2],New=[ or interface],Old=[]} type) can have discriminants, which parameterize the type. A @nt specifies the discriminants of a composite type. A discriminant of an object is a component of the object, and is either of a discrete type or an access type. An @nt in the declaration of a@Chg{Version=[2],New=[],Old=[ partial]} view of a type specifies that the discriminants of the type are unknown for the given view; all subtypes of such a@Chg{Version=[2],New=[],Old=[ partial]} view are indefinite subtypes.] @ChgRef{Version=[2],Kind=[Revised]} @ChgToGlossary{Version=[2],Kind=[Revised],Term=, Text=} @begin{Discussion} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]} @PDefn{unknown discriminants} @PDefn2{Term=[discriminants], Sec=(unknown)} A @Chg{Version=[2],New=[view of a ],Old=[]}type, and all @Chg{Version=[2],New=[], Old=[of its ]}subtypes@Chg{Version=[2],New=[ of the view],Old=[]}, have @i(unknown discriminants) when the number or names of the discriminants, if any, are unknown at the point of the type declaration@Chg{Version=[2],New=[ for the view],Old=[]}. A @nt of (<>) is used to indicate unknown discriminants. @end{Discussion} @end{Intro} @begin{Metarules} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00402-01]} @ChgAdded{Version=[2],Text=[When an access discriminant is initialized at the time of object creation with an allocator of an anonymous type, the allocated object and the object with the discriminant are tied together for their lifetime. They should be allocated out of the same storage pool, and then at the end of the lifetime of the enclosing object, finalized and reclaimed together. In this case, the allocated object is called a coextension (see @RefSecNum{Operations of Access Types}).]} @begin{Discussion} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[The above principle when applied to a nonlimited type implies that such an object may be copied only to a shorter-lived object, because attempting to assign it to a longer-lived object would fail because the access discriminants would not match. In a copy, the lifetime connection between the enclosing object and the allocated object does not exist. The allocated object is tied in the above sense only to the original object. Other copies have only secondary references to it.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Note that when an @nt{allocator} appears as a constraint on an access discriminant in a @nt{subtype_indication} that is elaborated independently from object creation, no such connection exists. For example, if a named constrained subtype is declared via "@key{subtype} Constr @key{is} Rec(Acc_Discrim => @key{new} T);" or if such an @nt{allocator} appears in the @nt{subtype_indication} for a component, the allocator is evaluated when the @nt{subtype_indication} is elaborated, and hence its lifetime is typically longer than the objects or components that will later be subject to the constraint. In these cases, the allocated object should not be reclaimed until the @nt{subtype_indication} goes out of scope.]} @end{Discussion} @end{Metarules} @begin{Syntax} @Syn{lhs=,rhs="@Syn2{unknown_discriminant_part} | @Syn2{known_discriminant_part}"} @Syn{lhs=,rhs="(<>)"} @Syn{lhs=,rhs=" (@Syn2{discriminant_specification} {; @Syn2{discriminant_specification}})"} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]} @Syn{lhs=,rhs=" @Syn2{defining_identifier_list} : @Chg{Version=[2],New=<[@Syn2{null_exclusion}] >,Old=<>}@Syn2{subtype_mark} [:= @Syn2{default_expression}] | @Syn2{defining_identifier_list} : @Syn2{access_definition} [:= @Syn2{default_expression}]"} @Syn{lhs=,rhs="@Syn2{expression}"} @end{Syntax} @begin{Resolution} @PDefn2{Term=[expected type], Sec=(discriminant default_expression)} The expected type for the @nt{default_expression} of a @nt{discriminant_specification} is that of the corresponding discriminant. @end{Resolution} @begin{Legality} @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0007],ARef=[AI95-00098-01]} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]} A @Chg{New=[@nt{discriminant_part}],Old=[@nt{known_discriminant_part}]} is only permitted in a declaration for a composite type that is not an array@Chg{Version=[2],New=[ or interface],Old=[]} type @Redundant[(this includes generic formal types)]@Chg{New=[. A],Old=[; a]} type declared with a @nt is called a @i(discriminated) type,@Defn{discriminated type} as is a type that inherits (known) discriminants. @begin{ImplNote} Discriminants on array types were considered, but were omitted to ease (existing) implementations. @end{implnote} @begin(Discussion) Note that the above definition for @lquotes@;discriminated type@rquotes@; does not include types declared with an @nt. This seems consistent with Ada 83, where such types (in a generic formal part) would not be considered discriminated types. Furthermore, the full type for a type with unknown discriminants need not even be composite, much less have any discriminants. @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0007],ARef=[AI95-00098-01]} @Chg{New=[On the other hand, @nts cannot be applied to type declarations that cannot have a @nt. There is no point in having unknown discriminants on a type that can never have discriminants (for instance, a formal modular type), even when these are allowed syntactically.],Old=[]} @end(Discussion) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01],ARef=[AI95-00254-01]} The subtype of a discriminant may be defined by@Chg{Version=[2],New=[ an optional @nt{null_exclusion} and],Old=[]} a @nt, in which case the @nt shall denote a discrete or access subtype, or it may be defined by an @nt@Chg{Version=[2],New=[],Old=[ @Redundant[(in which case the @nt of the @nt may denote any kind of subtype)]]}. @Defn{access discriminant} A discriminant that is defined by an @nt is called an @i(access discriminant) and is of an anonymous @Chg{Version=[2],New=[access], Old=[general access-to-variable]} type@Chg{Version=[2],New=[],Old=[ whose designated subtype is denoted by the @nt of the @nt]}. @begin(Reason) @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00230-01]} @ChgNote{It still does cause complexities. :-)}@ChgNote{We don't want to delete "Reason:" here, thus we use @Chg and not @ChgDeleted} @Chg{Version=[2],New=[],Old=[In an early version of Ada 9X, we allowed access discriminants on nonlimited types, but this created unpleasant complexities. It turned out to be simpler and more uniform to allow discriminants of a named access type on any discriminated type, and keep access discriminants just for limited types.]} Note that discriminants of a named access type are not considered @lquotes@;access discriminants.@rquotes@; Similarly, @lquotes@;access parameter@rquotes@; only refers to a formal parameter defined by an @nt. @end(Reason) @ChgNote{This paragraph is just moved up} @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00402-01]} @ChgAdded{Version=[2],Text=[@nt{Default_expression}s shall be provided either for all or for none of the discriminants of a @nt{known_@!discriminant_@!part}. No @nts are permitted in a @nt in a declaration of a tagged type @Redundant[or a generic formal type].]} @begin(Reason) @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[The all-or-none rule is related to the rule that a discriminant constraint shall specify values for all discriminants. One could imagine a different rule that allowed a constraint to specify only some of the discriminants, with the others provided by default. Having defaults for discriminants has a special significance @em it allows objects of the type to be unconstrained, with the discriminants alterable as part of assigning to the object.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Defaults for discriminants of tagged types are disallowed so that every object of a tagged type is constrained, either by an explicit constraint, or by its initial discriminant values. This substantially simplifies the semantic rules and the implementation of inherited dispatching operations. For generic formal types, the restriction simplifies the type matching rules. If one simply wants a "default" value for the discriminants, a constrained subtype can be declared for future use.]} @end(Reason) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00402-01],ARef=[AI95-00419-01]} A @nt for an access discriminant @Chg{Version=[2],New=[may have a @nt{default_expression}],Old=[shall appear]} only in the declaration for a task or protected type, or for a type @Chg{Version=[2],New=[that is a descendant of an explicitly limited record type],Old=[with the reserved word @key[limited] in its @Redundant[(full)] definition or in that of one of its ancestors]}. In addition to the places where @LegalityTitle normally apply (see @RefSecNum{Generic Instantiation}), this rule applies also in the private part of an instance of a generic unit.@PDefn{generic contract issue} @begin{Discussion} @ChgRef{Version=[2],Kind=[Revised]} This rule implies that a type can have @Chg{Version=[2], New=[a default for ],Old=[]}an access discriminant if the type is limited, but not if the only reason it's limited is because of a limited component. Compare with the definition of limited type in @RefSecNum{Limited Types}.@Chg{Version=[2],New=[ Also, recall that a @lquotes@;descendant@rquote includes the type itself, so an explicitly limited record type can have defaults.],Old=[]} @end{Discussion} @begin{Ramification} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[ It is a consequence of this rule that only a return-by-reference type can have an access discriminant (see @RefSecNum{Return Statements}). This is important to avoid dangling references to local variables.]} @end{Ramification} @begin{Reason} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]} @Leading@Keepnext@;We @Chg{Version=[2],New=[],Old=[also ]}considered the following rules@Chg{Version=[2],New=[ for access discriminants],Old=[]}: @begin{Itemize} If a type has an access discriminant, this automatically makes it limited, just like having a limited component automatically makes a type limited. This was rejected because it decreases program readability, and because it seemed error prone (two bugs in a previous version of the RM9X were attributable to this rule). @ChgRef{Version=[2],Kind=[Revised]} A type with an access discriminant shall be limited. This is equivalent to the rule we actually chose@Chg{Version=[2],New=[ for Ada 95],Old=[]}, except that it allows a type to have an access discriminant if it is limited just because of a limited component. For example, any record containing a task would be allowed to have an access discriminant, whereas the actual rule requires @lquotes@key[limited] @key[record]@rquotes@;. This rule was also rejected due to readability concerns, and because would interact badly with the rules for limited types that @lquotes@;become nonlimited@rquotes@;. @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[A type may have an access discriminant if it is a limited partial view, or a task, protected, or explicitly limited record type. This was the rule chosen for Ada 95.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant. For nonlimited type, there is no special accessibility for access discriminants; they're the same as any other anonymous access component. For a limited type, they have the special accessibility of Ada 95. However, this doesn't work because a limited partial view can have a nonlimited full view -- giving the two views different accessibility.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant, as above. However, special accessibility rules only apply to types that are @lquotes@;really@rquotes@; limited (task, protected, and explicitly limited records). However, this breaks privacy; worse, @LegalityTitle depend on the definition of accessibility.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant, as above. Limited types have special accessibility, while nonlimited types have normal accessibility. However, a limited partial view with an access discriminant can only be completed by a task, protected, or explicitly limited record type. That prevents accessibility from changing. A runtime accessibility check is required on generic formal types with access discriminants. However, changing between limited and nonlimited types would have far-reaching consequences for access discriminants @em which is uncomfortable.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant. All types have special accessibility. This was considered early during the Ada 9X process, but was dropped for @lquotes@;unpleasant complexities@rquotes@;, which unfortunately aren't recorded. It does seem that an accessibility check would be needed on assignment of such a type, to avoid copying an object with a discriminant pointing to a local object into a more global object (and thus creating a dangling pointer).]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant, but access discriminants cannot have defaults. All types have special accessibility. This gets rid of the problems on assignment (you couldn't change such a discriminant), but it would be horribly incompatible with Ada 95.]} @ChgRef{Version=[2],Kind=[AddedNormal]} @ChgAdded{Version=[2],Text=[Any type may have an access discriminant, but access discriminants may have defaults only if they are a @lquotes@;really@rquotes@; limited type. This is the rule chosen for Ada 2005, as it is not incompatible, and it doesn't require weird accessibility checks.]} @end{Itemize} @end{Reason} @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00402-01]} @ChgDeleted{Version=[2],Text=[@nt{Default_expression}s shall be provided either for all or for none of the discriminants of a @nt{known_@!discriminant_@!part}. No @nts are permitted in a @nt in a declaration of a tagged type @Redundant[or a generic formal type].]} @begin(Reason) @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[The all-or-none rule is related to the rule that a discriminant constraint shall specify values for all discriminants. One could imagine a different rule that allowed a constraint to specify only some of the discriminants, with the others provided by default. Having defaults for discriminants has a special significance @em it allows objects of the type to be unconstrained, with the discriminants alterable as part of assigning to the object.]} @ChgRef{Version=[2],Kind=[Deleted]} @ChgDeleted{Version=[2],Text=[Defaults for discriminants of tagged types are disallowed so that every object of a tagged type is constrained, either by an explicit constraint, or by its initial discriminant values. This substantially simplifies the semantic rules and the implementation of inherited dispatching operations. For generic formal types, the restriction simplifies the type matching rules. If one simply wants a "default" value for the discriminants, a constrained subtype can be declared for future use.]} @end(Reason) @Leading@;For a type defined by a @nt, if a @nt is provided in its declaration, then: @begin{Itemize} The parent subtype shall be constrained; If the parent type is not a tagged type, then each discriminant of the derived type shall be used in the constraint defining the parent subtype;@begin{ImplNote} This ensures that the new discriminant can share storage with an existing discriminant.@end{implnote} If a discriminant is used in the constraint defining the parent subtype, the subtype of the discriminant shall be statically compatible (see @RefSecNum{Statically Matching Constraints and Subtypes}) with the subtype of the corresponding parent discriminant. @begin(Reason) This ensures that on conversion (or extension via an extension aggregate) to a distantly related type, if the discriminants satisfy the target type's requirements they satisfy all the intermediate types' requirements as well. @end(Reason) @begin(Ramification) There is no requirement that the new discriminant have the same (or any) @nt as the parent's discriminant. @end(Ramification) @end{Itemize} The type of the @nt, if any, for an access discriminant shall be convertible to the anonymous access type of the discriminant (see @RefSecNum{Type Conversions}). @PDefn2{Term=[convertible],Sec=(required)} @begin{Ramification} This requires convertibility of the designated subtypes. @end{ramification} @end{Legality} @begin{StaticSem} A @nt{discriminant_specification} declares a discriminant; the @nt{subtype_mark} denotes its subtype unless it is an access discriminant, in which case the discriminant's subtype is the anonymous access-to-variable subtype defined by the @nt{access_definition}. @Redundant[For a type defined by a @nt, each discriminant of the parent type is either inherited, constrained to equal some new discriminant of the derived type, or constrained to the value of an expression.] @Defn{corresponding discriminants} When inherited or constrained to equal some new discriminant, the parent discriminant and the discriminant of the derived type are said to @i(correspond). Two discriminants also correspond if there is some common discriminant to which they both correspond. A discriminant corresponds to itself as well. @Defn{specified discriminant} If a discriminant of a parent type is constrained to a specific value by a @nt, then that discriminant is said to be @i(specified) by that @nt. @begin{Ramification} The correspondence relationship is transitive, symmetric, and reflexive. That is, if A corresponds to B, and B corresponds to C, then A, B, and C each corresponds to A, B, and C in all combinations.@end{ramification} @Defn2{Term=[depend on a discriminant], Sec=(for a @nt or @nt)} A @nt{constraint} that appears within the definition of a discriminated type @i(depends on a discriminant) of the type if it names the discriminant as a bound or discriminant value. A @nt{component_definition} depends on a discriminant if its @nt{constraint} depends on the discriminant, or on a discriminant that corresponds to it. @begin(Ramification) A @nt{constraint} in a @nt{task_body} is not considered to @i(depend) on a discriminant of the task type, even if it names it. It is only the @nts in the type definition itself that are considered dependents. Similarly for protected types. @end(Ramification) @Leading@keepnext@Defn2{Term=[depend on a discriminant], Sec=(for a component)} A component @i(depends on a discriminant) if: @begin(itemize) Its @nt{component_definition} depends on the discriminant; or @begin{Ramification} A component does @i(not) depend on a discriminant just because its @nt refers to the discriminant.@end{ramification} It is declared in a @nt{variant_part} that is governed by the discriminant; or It is a component inherited as part of a @nt, and the @nt of the @i(parent_)@nt depends on the discriminant; or @begin{Reason} When the parent subtype depends on a discriminant, the parent part of the derived type is treated like a discriminant-dependent component. @end{Reason} @begin{Ramification} Because of this rule, we don't really need to worry about @lquotes@;corresponding@rquotes@; discriminants, since all the inherited components will be discriminant-dependent if there is a new @nt whose discriminants are used to constrain the old discriminants. @end{Ramification} It is a subcomponent of a component that depends on the discriminant. @end(itemize) @begin{Reason} The concept of discriminant-dependent (sub)components is primarily used in various rules that disallow renaming or 'Access, or specify that certain discriminant-changing assignments are erroneous. The goal is to allow implementations to move around or change the size of discriminant-dependent subcomponents upon a discriminant-changing assignment to an enclosing object. The above definition specifies that all subcomponents of a discriminant-dependent component or parent part are themselves discriminant-dependent, even though their presence or size does not in fact depend on a discriminant. This is because it is likely that they will move in a discriminant-changing assignment if they are a component of one of several discriminant-dependent parts of the same record. @end{Reason} Each value of a discriminated type includes a value for each component of the type that does not depend on a discriminant@Redundant[; this includes the discriminants themselves]. The values of discriminants determine which other component values are present in the value of the discriminated type. @begin{Honest} Which values are present might depend on discriminants of some ancestor type that are constrained in an intervening @nt. That's why we say "values of discriminants" instead of "values of @i(the) discriminants" @em a subtle point.@end{honest} @Defn{known discriminants} @Defn2{Term=[discriminants], Sec=(known)} @Defn2{Term=[constrained], Sec=(subtype)} @Defn2{Term=[unconstrained], Sec=(subtype)} A type declared with a @nt is said to have @i(known discriminants); its first subtype is unconstrained. @Defn{unknown discriminants} @Defn2{Term=[discriminants], Sec=(unknown)} A type declared with an @nt is said to have @i(unknown discriminants). A type declared without a @nt has no discriminants, unless it is a derived type; if derived, such a type has the same sort of discriminants (known, unknown, or none) as its parent (or ancestor) type. A tagged class-wide type also has unknown discriminants. @Defn{class-wide type} @Defn{indefinite subtype} @Redundant[Any subtype of a type with unknown discriminants is an unconstrained and indefinite subtype (see @RefSecNum{Types and Subtypes} and @RefSecNum{Objects and Named Numbers}).] @begin(Discussion) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]} An @nt @lquotes@;(<>)@rquotes@; is only permitted in the declaration of a (generic or nongeneric) private type, private extension, @Chg{Version=[2],New=[incomplete type, ],Old=[]}or formal derived type.@Comment{That was always intended, but 8652/0007 was needed to make it true.} Hence, only such types, descendants thereof, and class-wide types can have unknown discriminants. An @nt is used to indicate that the corresponding actual or full type might have discriminants without defaults, or be an unconstrained array subtype. Tagged class-wide types are also considered to have unknown discriminants because discriminants can be added by type extensions, so the total number of discriminants of any given value of a tagged class-wide type is not known at compile time. @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]} A subtype with unknown discriminants is indefinite, and hence an object of such a subtype needs explicit initialization.@Chg{Version=[2],New=[],Old=[ If the subtype is limited, no (stand-alone) objects can be declared since initialization is not permitted (though formal parameters are permitted, and objects of the actual/full type will generally be declarable).]} A limited private type with unknown discriminants is @lquotes@;extremely@rquotes@; limited;@Chg{Version=[2],New=[ objects of],Old=[]} such a type @Chg{Version=[2],New=[ can be initialized only by subprograms (either procedures with a parameter of the type, or a function returning the type) declared in the package. Subprograms declared elsewhere can operate on and even return the type, but they can only initialize the object by calling (ultimately) a subprogram in the package declaring the type. Such a type],Old=[]} is useful for keeping complete control over object creation within the package declaring the type. A partial view of a type might have unknown discriminants, while the full view of the same type might have known, unknown, or no discriminants@Chg{New=[.],Old=[,]} @end(Discussion) @end{StaticSem} @begin{RunTime} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00416-01]} @Chg{Version=[2],New=[For an access discriminant, its], Old=[An]} @nt is elaborated when the value of @Chg{Version=[2],New=[the],Old=[a corresponding]} access discriminant is defined@Chg{Version=[2],New=[:],Old=[, either]} by evaluation of its @nt@Chg{Version=[2],New=[,],Old=[ or]} by elaboration of a @nt@Chg{Version=[2],New=[, or by an assignment that initializes the enclosing object.],Old=[. @Redundant[The elaboration of an @nt creates the anonymous access type. When the expression defining the access discriminant is evaluated, it is converted to this anonymous access type (see @RefSecNum{Type Conversions}).]]} @PDefn2{Term=[implicit subtype conversion],Sec=(access discriminant)} @begin(Ramification) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01],ARef=[AI95-00416-01]} @Chg{Version=[2],New=[The],Old=[This]} conversion@Chg{Version=[2],New=[ of the @nt{expression} defining the access discriminant to the anonymous access type],Old=[]} raises @Chg{Version=[2],New=[Program_Error], Old=[Constraint_Error if the initial value is @key(null), or,]} for an object created by an allocator of an access type T, if the initial value is an access parameter that designates a view whose accessibility level is deeper than that of T. @end(Ramification) @end{RunTime} @begin{Notes} If a discriminated type has @nts for its discriminants, then unconstrained variables of the type are permitted, and the values of the discriminants can be changed by an assignment to such a variable. If defaults are not provided for the discriminants, then all variables of the type are constrained, either by explicit constraint or by their initial value; the values of the discriminants of such a variable cannot be changed after initialization. @begin(Discussion) This connection between discriminant defaults and unconstrained variables can be a source of confusion. For Ada 95, we considered various ways to break the connection between defaults and unconstrainedness, but ultimately gave up for lack of a sufficiently simple and intuitive alternative. @Defn{mutable} An unconstrained discriminated subtype with defaults is called a @i{mutable} subtype, and a variable of such a subtype is called a mutable variable, because the discriminants of such a variable can change. There are no mutable arrays (that is, the bounds of an array object can never change), because there is no way in the language to define default values for the bounds. Similarly, there are no mutable class-wide subtypes, because there is no way to define the default tag, and defaults for discriminants are not allowed in the tagged case. Mutable tags would also require a way for the maximum possible size of such a class-wide subtype to be known. (In some implementations, all mutable variables are allocated with the maximum possible size. This approach is appropriate for real-time applications where implicit use of the heap is inappropriate.) @end(Discussion) The @nt{default_expression} for a discriminant of a type is evaluated when an object of an unconstrained subtype of the type is created. Assignment to a discriminant of an object (after its initialization) is not allowed, since the name of a discriminant is a constant; neither @nt{assignment_statement}s nor assignments inherent in passing as an @key(in out) or @key(out) parameter are allowed. Note however that the value of a discriminant can be changed by assigning to the enclosing object, presuming it is an unconstrained variable. @begin(Discussion) @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]} An @nt{unknown_discriminant_part} is permitted only in the declaration of a private type (including generic formal private), private extension, @Chg{Version=[2],New=[incomplete type, ],Old=[]}or generic formal derived type. These are the things that will have a corresponding completion or generic actual, which will either define the discriminants, or say there are none. The (<>) indicates that the actual/full subtype might be an indefinite subtype. An @nt{unknown_discriminant_part} is not permitted in a normal untagged derived type declaration, because there is no separate full type declaration for such a type. Note that (<>) allows unconstrained array bounds; those are somewhat like undefaulted discriminants. For a derived type, either the discriminants are inherited as is, or completely respecified in a new @nt. In this latter case, each discriminant of the parent type shall be constrained, either to a specific value, or to equal one of the new discriminants. Constraining a parent type's discriminant to equal one of the new discriminants is like a renaming of the discriminant, except that the subtype of the new discriminant can be more restrictive than that of the parent's one. In any case, the new discriminant can share storage with the parent's discriminant. @end(Discussion) A discriminant that is of a named access type is not called an access discriminant; that term is used only for discriminants defined by an @nt. @end{Notes} @begin{Examples} @Leading@keepnext@i(Examples of discriminated types:) @begin(Example) @key(type) Buffer(Size : Buffer_Size := 100) @key(is) --@RI[ see @RefSecNum(Integer Types)] @key(record) Pos : Buffer_Size := 0; Value : String(1 .. Size); @key(end) @key(record); @key(type) Matrix_Rec(Rows, Columns : Integer) @key(is) @key(record) Mat : Matrix(1 .. Rows, 1 .. Columns); --@RI[ see @RefSecNum(Array Types)] @key(end) @key(record); @key(type) Square(Side : Integer) @key(is) @key(new) Matrix_Rec(Rows => Side, Columns => Side); @key(type) Double_Square(Number : Integer) @key(is) @key(record) Left : Square(Number); Right : Square(Number); @key(end) @key(record); @ChgRef{Version=[2],Kind=[Revised],ARef=[AI-00433-01]} @Chg{Version=[2],New=[@key(task type) Worker(Prio : System.Priority; Buf : @key(access) Buffer) @key(is) --@RI[ discriminants used to parameterize the task type (see @RefSecNum{Task Units and Task Objects})] @key(pragma) Priority(Prio); --@RI[ see @RefSecNum{Task Priorities}] @key(entry) Fill; @key(entry) Drain; @key(end) Worker;],Old=[@key(type) Item(Number : Positive) @key(is) @key(record) Content : Integer; --@RI[ no component depends on the discriminant] @key(end) @key(record);]} @end(Example) @end{Examples} @begin{Extend83} @Defn{extensions to Ada 83} The syntax for a @nt{discriminant_specification} is modified to allow an @i{access discriminant}, with a type specified by an @nt{access_definition} (see @RefSecNum{Access Types}). @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]} Discriminants are allowed on all composite types other than array@Chg{Version=[2],New=[ and interface],Old=[]} types. Discriminants may be of an access type. @end{Extend83} @begin{DiffWord83} @nt{Discriminant_part}s are not elaborated, though an @nt is elaborated when the discriminant is initialized. @end{DiffWord83} @begin{Extend95} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00230-01],ARef=[AI95-00402-01],ARef=[AI95-00416-01]} @ChgAdded{Version=[2],Text=[Access discriminants (anonymous access types used as a discriminant) can be used on any type allowing discriminants. Defaults aren't allowed on discriminants of non-limited types, however, so that accessibility problems don't happen on assignment.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]} @ChgAdded{Version=[2],Text=[ @nt{null_exclusion} can be used in the declaration of a discriminant.]} @end{Extend95} @begin{DiffWord95} @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0007],ARef=[AI95-00098-01]} @ChgAdded{Version=[2],Text=[@b The wording was clarified so that types that cannot have discriminants cannot have an @nt{unknown_discriminant_part}.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]} @ChgAdded{Version=[2],Text=[Added wording to prevent interfaces from having discriminants. We don't want interfaces to have any components.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00254-01]} @ChgAdded{Version=[2],Text=[Removed wording which implied or required an access discriminant to have an access-to-object type (anonymous access types can now be access-to-subprogram types as well).]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00326-01]} @ChgAdded{Version=[2],Text=[Fixed the wording of the introduction to this clause to reflect that both incomplete and partial views can have unknown discriminants. That was always true, but for some reason this wording specified partial views.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]} @ChgAdded{Version=[2],Text=[Changed the wording to use the new term @lquotes@;explicitly limited record@rquotes, which makes the intent much clearer (and eliminates confusion with derived types that happen to contain the reserved word @key(limited).]} @end{DiffWord95} @LabeledSubClause{Discriminant Constraints} @begin{Intro} A @nt specifies the values of the discriminants for a given discriminated type. @end{Intro} @begin{MetaRules} The rules in this clause are intentionally parallel to those given in Record Aggregates. @end{MetaRules} @begin{Syntax} @Syn{lhs=,rhs=" (@Syn2{discriminant_association} {, @Syn2{discriminant_association}})"} @Syn{lhs=,rhs=" [@SynI{discriminant_}@Syn2{selector_name} {| @SynI{discriminant_}@Syn2{selector_name}} =>] @Syn2{expression}"} @begin{SyntaxText} @Defn{named discriminant association} A @nt is said to be @i(named) if it has one or more @i(discriminant_)@nts; @Defn{positional discriminant association} it is otherwise said to be @i(positional). In a @nt, any positional associations shall precede any named associations. @end{SyntaxText} @end{Syntax} @begin{Resolution} Each @nt of a named @nt shall resolve to denote a discriminant of the subtype being constrained; @Defn2{Term=[associated discriminants], Sec=(of a named @nt)} the discriminants so named are the @i(associated discriminants) of the named association. @Defn2{Term=[associated discriminants], Sec=(of a positional @nt)} For a positional association, the @i(associated discriminant) is the one whose @nt occurred in the corresponding position in the @nt that defined the discriminants of the subtype being constrained. @PDefn2{Term=[expected type], Sec=(discriminant_association expression)} The expected type for the @nt{expression} in a @nt{discriminant_association} is that of the associated discriminant(s). @end{Resolution} @begin{Legality} @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0008],ARef=[AI95-00168-01]} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00363-01]} A @nt{discriminant_constraint} is only allowed in a @nt{subtype_indication} whose @nt{subtype_mark} denotes either an unconstrained discriminated subtype, or an unconstrained access subtype whose designated subtype is an unconstrained discriminated subtype. @Chg{New=[However, in the case of @Chg{Version=[2],New=[an],Old=[a general]} access subtype, a @nt{discriminant_@!constraint} is illegal if @Chg{Version=[2],New=[the designated type has a partial view that is constrained or, for a general access subtype, has @nt{default_expression}s for its discriminants. In addition to the places where @LegalityTitle@; normally apply (see @RefSecNum{Generic Instantiation}), these rules apply also in the private part of an instance of a generic unit. In a generic body, this rule is checked presuming all formal access types of the generic might be general access types, and all untagged discriminated formal types of the generic might have @nt{default_expression}s for its discriminants. @PDefn{generic contract issue}], Old=[there is a place within the immediate scope of the designated subtype where the designated subtype's view is constrained.]}],Old=[]} @begin{Reason} @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0008],ARef=[AI95-00168-01]} @ChgRef{Version=[2],Kind=[DeletedAdded],ARef=[AI95-00363-01]} @ChgDeleted{Version=[2],Text=[@Chg{New=[The second rule is necessary to prevent assignments that change the discriminant of a constrained object. See the defect report for examples.],Old=[]}]} @end{Reason} @begin{Reason} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]} @ChgAdded{Version=[2],Text=[The second rule is necessary to prevent objects from changing so that they no longer match their constraint. In Ada 95, we attempted to prevent this by banning every case where an aliased object could be unconstrained or be changed by an enclosing assignment. New ways to cause this problem were being discovered frequently, meaning that new rules had to be dreamed up to cover them. Meanwhile, aliased objects and components were getting more and more limited. In Ada 2005, we sweep away all of that cruft and replace it by a simple rule @lquotes@;thou shalt not create an access subtype that can point to an item whose discriminants can be changed by assignment@rquotes@;.]} @end{Reason} A named @nt with more than one @nt is allowed only if the named discriminants are all of the same type. A @nt shall provide exactly one value for each discriminant of the subtype being constrained. The @nt associated with an access discriminant shall be of a type convertible to the anonymous access type. @PDefn2{Term=[convertible],Sec=(required)} @begin(Ramification) This implies both convertibility of designated types, and static accessibility. This implies that if an object of type T with an access discriminant is created by an allocator for an access type A, then it requires that the type of the @nt associated with the access discriminant have an accessibility level that is not statically deeper than that of A. This is to avoid dangling references. @end(Ramification) @end{Legality} @begin{RunTime} @PDefn2{Term=[compatibility], Sec=(discriminant constraint with a subtype)} A @nt{discriminant_constraint} is @i(compatible) with an unconstrained discriminated subtype if each discriminant value belongs to the subtype of the corresponding discriminant. @begin{Ramification} The "dependent compatibility check" has been eliminated in Ada 95. Any checking on subcomponents is performed when (and if) an object is created.@end{ramification} @begin{Discussion} There is no need to define compatibility with a constrained discriminated subtype, because one is not allowed to constrain it again.@end{discussion} @PDefn2{Term=[satisfies], Sec=(a discriminant constraint)} A composite value @i(satisfies) a discriminant constraint if and only if each discriminant of the composite value has the value imposed by the discriminant constraint. @PDefn2{Term=[elaboration], Sec=(discriminant_constraint)} For the elaboration of a @nt{discriminant_constraint}, the @nt{expression}s in the @nt{discriminant_association}s are evaluated in an arbitrary order and converted to the type of the associated discriminant (which might raise Constraint_Error @em see @RefSecNum{Type Conversions}); the @nt{expression} of a named association is evaluated (and converted) once for each associated discriminant. @PDefn2{Term=[implicit subtype conversion],Sec=(discriminant values)} The result of each evaluation and conversion is the value imposed by the constraint for the associated discriminant. @begin{Reason} We convert to the type, not the subtype, so that the definition of compatibility of discriminant constraints is not vacuous.@end{reason} @end{RunTime} @begin{Notes} The rules of the language ensure that a discriminant of an object always has a value, either from explicit or implicit initialization. @begin(Discussion) Although it is illegal to constrain a class-wide tagged subtype, it is possible to have a partially constrained class-wide subtype: If the subtype S is defined by T(A => B), then S'Class is partially constrained in the sense that objects of subtype S'Class have to have discriminants corresponding to A equal to B, but there can be other discriminants defined in extensions that are not constrained to any particular value. @end(Discussion) @end{Notes} @begin{Examples} @Leading@keepnext@i{Examples (using types declared above in clause @RefSecNum(Discriminants)):} @begin(Example) Large : Buffer(200); --@RI[ constrained, always 200 characters] --@RI[ (explicit discriminant value)] Message : Buffer; --@RI[ unconstrained, initially 100 characters] --@RI[ (default discriminant value)] Basis : Square(5); --@RI[ constrained, always 5 by 5] Illegal : Square; --@RI[ illegal, a Square has to be constrained] @end(Example) @end{Examples} @begin{Inconsistent83} @Defn{inconsistencies with Ada 83} Dependent compatibility checks are no longer performed on subtype declaration. Instead they are deferred until object creation (see @RefSecNum(Object Declarations)). This is upward compatible for a program that does not raise Constraint_Error. @end{Inconsistent83} @begin{DiffWord83} Everything in RM83-3.7.2(7-12), which specifies the initial values for discriminants, is now redundant with 3.3.1, 6.4.1, 8.5.1, and 12.4. Therefore, we don't repeat it here. Since the material is largely intuitive, but nevertheless complicated to state formally, it doesn't seem worth putting it in a "NOTE." @end{DiffWord83} @begin{Incompatible95} @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0008],ARef=[AI95-00168-01],ARef=[AI95-00363-01]} @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95} The Corrigendum added a restriction on @nt{discriminant_constraint}s for general access subtypes. Such constraints are prohibited if the designated type can be treated as constrained somewhere in the program. Ada 2005 goes further and prohibits such @nt{discriminant_constraint}s if the designated type has (or might have, in the case of a formal type) defaults for its discriminants. The use of general access subtypes is rare, and this eliminates a boatload of problems that required many restrictions on the use of aliased objects and components (now lifted). Similarly, Ada 2005 prohibits @nt{discriminant_constraint}s on any access type whose designated type has a partial view that is constrained. Such a type will not be constrained in the heap to avoid privacy problems. Again, the use of such subtypes is rare (they can only happen within the package and its child units).]} @end{Incompatible95} @LabeledSubClause{Operations of Discriminated Types} @begin{Intro} @Redundant[If a discriminated type has @nts for its discriminants, then unconstrained variables of the type are permitted, and the discriminants of such a variable can be changed by assignment to the variable. For a formal parameter of such a type, an attribute is provided to determine whether the corresponding actual parameter is constrained or unconstrained.] @end{Intro} @begin{StaticSem} For @PrefixType{a @nt A that is of a discriminated type @Redundant[(after any implicit dereference)]}, the following attribute is defined: @begin(description) @Attribute{Prefix=, AttrName=, Text=[Yields the value True if A denotes a constant, a value, or a constrained variable, and False otherwise.]} @begin(ImplNote) This attribute is primarily used on parameters, to determine whether the discriminants can be changed as part of an assignment. The Constrained attribute is statically True for @key(in) parameters. For @key(in out) and @key(out) parameters of a discriminated type, the value of this attribute needs to be passed as an implicit parameter, in general. However, if the type does not have defaults for its discriminants, the attribute is statically True, so no implicit parameter is needed. Parameters of a limited type with defaulted discriminants need this implicit parameter, unless there are no nonlimited views, because they might be passed to a subprogram whose body has visibility on a nonlimited view of the type, and hence might be able to assign to the object and change its discriminants. @end(ImplNote) @end(description) @EndPrefixType{} @end{StaticSem} @begin{Erron} @PDefn2{Term=(erroneous execution),Sec=(cause)} The execution of a construct is erroneous if the construct has a constituent that is a @nt denoting a subcomponent that depends on discriminants, and the value of any of these discriminants is changed by this execution between evaluating the @nt and the last use (within this execution) of the subcomponent denoted by the @nt. @begin{Ramification} This rule applies to @nts, calls (except when the discriminant-dependent subcomponent is an @key(in) parameter passed by copy), @nts, and @nts. Ada 83 only covered the first two cases. AI83-00585 pointed out the situation with the last two cases. The cases of @nts and generic formal @key(in out) objects are handled differently, by disallowing the situation at compile time. @end{Ramification} @end{Erron} @begin{Extend83} @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{To be consistent with 8652/0006} @Defn{extensions to Ada 83} For consistency with other attributes, we are allowing the @Chg{New=[@nt{prefix}],Old=[prefix]} of Constrained to be a value as well as an object of a discriminated type, and also an implicit dereference. These extensions are not important capabilities, but there seems no reason to make this attribute different from other similar attributes. We are curious what most Ada 83 compilers do with F(1).X'Constrained. We now handle in a general way the cases of erroneousness identified by AI83-00585, where the @nt of an @nt or @nt is discriminant-dependent, and the evaluation of the index or discrete range changes the value of a discriminant. @end{Extend83} @begin{DiffWord83} We have moved all discussion of erroneous use of @nts that denote discriminant-dependent subcomponents to this subclause. In Ada 83, it used to appear separately under @nt{assignment_statement}s and subprogram calls. @end{DiffWord83} @LabeledClause{Record Types} @begin{Intro} @Defn{record} @Defn{record type} A record object is a composite object consisting of named components. The value of a record object is a composite value consisting of the values of the components. @IndexSee{Term=[structure],See=(record type)} @end{Intro} @begin{Syntax} @Syn{lhs=,rhs="[[@key{abstract}] @key{tagged}] [@key{limited}] @Syn2{record_definition}"} @Syn{lhs=,rhs=" @key{record} @Syn2{component_list} @key{end} @key{record} | @key{null record}"} @Syn{lhs=,rhs=" @Syn2{component_item} {@Syn2{component_item}} | {@Syn2{component_item}} @Syn2{variant_part} | @key{null};"} @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]} @Syn{lhs=,rhs="@Syn2{component_declaration} | @Chg{New=[@Syn2{aspect_clause}],Old=[@Syn2{representation_clause}]}"} @Syn{lhs=,rhs=" @Syn2{defining_identifier_list} : @Syn2{component_definition} [:= @Syn2{default_expression}];"} @end{Syntax} @begin{Resolution} @PDefn2{Term=[expected type], Sec=(component_declaration default_expression)} The expected type for the @nt, if any, in a @nt is the type of the component. @end{Resolution} @begin{Legality} @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]} @ChgDeleted{Version=[2],Text=[A @nt is not permitted if the component is of a limited type.]} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00366-01]}@ChgNote{This is not THE definition of component} @PDefn2{Term=[components], Sec=(of a record type)} Each @nt declares a @Chg{Version=[2],New=[component],Old=[@i(component)]} of the record type. Besides components declared by @nts, the components of a record type include any components declared by @nts of the record type declaration. @Redundant[The identifiers of all components of a record type shall be distinct.] @begin{TheProof} The identifiers of all components of a record type have to be distinct because they are all declared immediately within the same declarative region. See Section 8. @end{theproof} Within a @nt{type_declaration}, a @nt{name} that denotes a component, protected subprogram, or entry of the type is allowed only in the following cases: @begin{Itemize} A @nt{name} that denotes any component, protected subprogram, or entry is allowed within a representation item that occurs within the declaration of the composite type. A @nt{name} that denotes a noninherited discriminant is allowed within the declaration of the type, but not within the @nt{discriminant_part}. If the discriminant is used to define the constraint of a component, the bounds of an entry family, or the constraint of the parent subtype in a @nt then its name shall appear alone as a @nt (not as part of a larger expression or expanded name). A discriminant shall not be used to define the constraint of a scalar component.@Defn2{Term=[discriminant],Sec=[use in a record definition]}@ChgNote{New index entry} @begin{Reason} The penultimate restriction simplifies implementation, and allows the outer discriminant and the inner discriminant or bound to possibly share storage. @end{reason} @begin{Ramification} Other rules prevent such a discriminant from being an inherited one. @end{ramification} @begin{Reason} The last restriction is inherited from Ada 83. The restriction is not really necessary from a language design point of view, but we did not remove it, in order to avoid unnecessary changes to existing compilers. @end{Reason} @begin{Discussion} Note that a discriminant can be used to define the constraint for a component that is of an access-to-composite type. @end{Discussion} @begin{Reason} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]} The above rules, and a similar one in @RefSecNum{Subprogram Declarations} for formal parameters, are intended to allow initializations of components or parameters to occur in @Chg{Version=[2], New=[a (nearly)],Old=[an]} arbitrary order @em whatever order is most efficient@Chg{Version=[2],New=[ (subject to the restrictions of @RefSecNum{Object Declarations})],Old=[]}, since one @nt{default_expression} cannot depend on the value of another one. @Chg{Version=[2],New=[They],Old=[It]} also prevent circularities. @end{Reason} @begin{Ramification} Inherited discriminants are not allowed to be denoted, except within representation items. However, the @SynI{discriminant_}@nt{selector_name} of the parent @nt{subtype_indication} is allowed to denote a discriminant of the parent. @end{Ramification} @end{Itemize} If the name of the current instance of a type (see @RefSecNum{The Context of Overload Resolution}) is used to define the constraint of a component, then it shall appear as a @nt{direct_name} that is the @nt of an @nt{attribute_reference} whose result is of an access type, and the @nt{attribute_reference} shall appear alone. @begin{Reason} This rule allows T'Access or T'Unchecked_Access, but disallows, for example, a range constraint (1..T'Size). Allowing things like (1..T'Size) would mean that a per-object constraint could affect the size of the object, which would be bad. @end{Reason} @end{Legality} @begin{StaticSem} @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]} @ChgAdded{Version=[2],Text=[@Defn{explicitly limited record} @Defn2{Term=[record],Sec=(explicitly limited)} If a @nt{record_type_declaration} includes the reserved word @key{limited}, the type is called an @i type.]} @PDefn2{Term=[nominal subtype], Sec=(of a record component)} The @nt{component_definition} of a @nt defines the (nominal) subtype of the component. If the reserved word @key(aliased) appears in the @nt{component_definition}, then the component is aliased (see @RefSecNum{Access Types}). @begin(Ramification) @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00363-01]} @ChgDeleted{Version=[2],Text=[In this case, the nominal subtype cannot be an unconstrained discriminated subtype. See @RefSecNum{Array Types}.]} @end(Ramification) @Defn{null record} If the @nt of a record type is defined by the reserved word @key(null) and there are no discriminants, then the record type has no components and all records of the type are @i(null records). A @nt of @key{null record} is equivalent to @key{record null; end record}. @begin{Ramification} This short-hand is available both for declaring a record type and a record extension @em see @RefSecNum(Type Extensions). @end{ramification} @end{StaticSem} @begin{RunTime} @PDefn2{Term=[elaboration], Sec=(record_type_definition)} The elaboration of a @nt{record_type_definition} creates the record type and its first subtype, and consists of the elaboration of the @nt. @PDefn2{Term=[elaboration], Sec=(record_definition)} The elaboration of a @nt consists of the elaboration of its @nt{component_list}, if any. @PDefn2{Term=[elaboration], Sec=(component_list)} The elaboration of a @nt{component_list} consists of the elaboration of the @nt{component_item}s and @nt{variant_part}, if any, in the order in which they appear. @PDefn2{Term=[elaboration], Sec=(component_declaration)} The elaboration of a @nt{component_declaration} consists of the elaboration of the @nt{component_definition}. @begin(Discussion) If the @nt has more than one @nt, we presume here that the transformation explained in @RefSecNum(Object Declarations) has already taken place. Alternatively, we could say that the @nt is elaborated once for each @nt in the list. @end(Discussion) @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI95-00171-01]} @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]} @Defn{per-object expression} @Defn{per-object constraint} @Defn{entry index subtype} Within the definition of a composite type, if a @nt or @nt (see @RefSecNum{Entries and Accept Statements}) includes a @nt that denotes a discriminant of the type, or that is an @nt whose @nt denotes the current instance of the type, the expression containing the @nt is called a @i(per-object expression), and the @Chg{New=[@nt{constraint} or @nt{range}],Old=[constraint]} being defined is called a @i(per-object constraint). @PDefn2{Term=[elaboration], Sec=(component_definition)} For the elaboration of a @nt{component_definition} of a @nt@Chg{New=[ or the @nt{discrete_@!subtype_@!definition} of an @nt{entry_@!declaration} for an entry family (see @RefSecNum{Entries and Accept Statements})],Old=[]},@Chg{Version=[2], New=[ if the component subtype is defined by an @nt{access_definition} or], Old=[]} if the @nt{constraint} @Chg{New=[or @nt{range}],Old=[]} of the @nt{subtype_indication} @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]} is not a per-object constraint, then the@Chg{Version=[2],New=[ @nt{access_definition},],Old=[]} @nt{subtype_indication}@Chg{Version=[2],New=[,],Old=[]} @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]} is elaborated. On the other hand, if the @nt{constraint} @Chg{New=[or @nt{range}],Old=[]} is a per-object constraint, then the elaboration consists of the evaluation of any included expression that is not part of a per-object expression. @Chg{New=[Each such expression is evaluated once unless it is part of a named association in a discriminant constraint, in which case it is evaluated once for each associated discriminant.],Old=[]} @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0002],ARef=[AI95-00171-01]} @Chg{New=[@PDefn2{Term=[Elaboration],Sec=(per-object constraint)}When a per-object constraint is elaborated @Redundant[(as part of creating an object)], each per-object expression of the constraint is evaluated. For other expressions, the values determined during the elaboration of the @nt{component_@!definition} or @nt{entry_@!declaration} are used. Any checks associated with the enclosing @nt{subtype_indication} or @nt{discrete_subtype_definition} are performed@Redundant[, including the subtype compatibility check (see @RefSecNum{Subtype Declarations}),] and the associated subtype is created.], Old=[]} @begin(Discussion) The evaluation of other expressions that appear in @nts and @nts is performed when the type definition is elaborated. The evaluation of expressions that appear as @nts is postponed until an object is created. Expressions in representation items that appear within a composite type definition are evaluated according to the rules of the particular representation item. @end(Discussion) @end{RunTime} @begin{Notes} A @nt with several identifiers is equivalent to a sequence of single @nts, as explained in @RefSecNum{Object Declarations}. The @nt of a record component is only evaluated upon the creation of a default-initialized object of the record type (presuming the object has the component, if it is in a @nt @em see @RefSecNum{Object Declarations}). The subtype defined by a @nt (see @RefSecNum(Array Types)) has to be a definite subtype. If a record type does not have a @nt, then the same components are present in all values of the type. A record type is limited if it has the reserved word @key[limited] in its definition, or if any of its components are limited (see @RefSecNum{Limited Types}). @PDefn2{Term=[predefined operations],Sec=(of a record type)} The predefined operations of a record type include membership tests, qualification, and explicit conversion. If the record type is nonlimited, they also include assignment and the predefined equality operators. @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]} A component of a record can be named with a @nt. A value of a record can be specified with a @nt@Chg{Version=[2], New=[],Old=[, unless the record type is limited]}. @end{Notes} @begin{Examples} @Leading@keepnext@i(Examples of record type declarations: ) @begin(Example) @key(type) Date @key(is) @key(record) Day : Integer @key(range) 1 .. 31; Month : Month_Name; Year : Integer @key(range) 0 .. 4000; @key(end) @key(record); @key(type) Complex @key(is) @key(record) Re : Real := 0.0; Im : Real := 0.0; @key(end) @key(record); @end(Example) @begin{Wide} @leading@keepnext@i(Examples of record variables: ) @end{Wide} @begin(Example) Tomorrow, Yesterday : Date; A, B, C : Complex; --@RI[ both components of A, B, and C are implicitly initialized to zero ] @end(Example) @end{Examples} @begin{Extend83} @Defn{extensions to Ada 83} The syntax rule for @nt is modified to use @nt (instead of @nt). The effect of this change is to allow the reserved word @key(aliased) before the @nt. A short-hand is provided for defining a null record type (and a null record extension), as these will be more common for abstract root types (and derived types without additional components). The syntax rule for @nt{record_type_definition} is modified to allow the reserved words @key{tagged} and @key{limited}. Tagging is new. Limitedness is now orthogonal to privateness. In Ada 83 the syntax implied that limited private was sort of more private than private. However, limitedness really has nothing to do with privateness; limitedness simply indicates the lack of assignment capabilities, and makes perfect sense for nonprivate types such as record types. @end{Extend83} @begin{DiffWord83} @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]} The syntax rules now allow @Chg{New=[@nt{aspect_clause}s], Old=[@nt{representation_clause}s]} to appear in a @nt{record_definition}. This is not a language extension, because @LegalityName@;s prevent all language-defined representation clauses from appearing there. However, an implementation-defined @nt{attribute_definition_clause} could appear there. The reason for this change is to allow the rules for @Chg{New=[@nt{aspect_clause}s],Old=[@nt{representation_clause}s]} and representation pragmas to be as similar as possible. @end{DiffWord83} @begin{Extend95} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]} @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} Record components can have an anonymous access type.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]} @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} Limited components can be initialized, so long as the expression is one that allows building the object in place (such as an @nt{aggregate} or @nt{function_call}).]} @end{Extend95} @begin{DiffWord95} @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]} @ChgAdded{Version=[2],Text=[@b Improved the description of the elaboration of per-object constraints.]} @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0009],ARef=[AI95-00137-01]} @ChgAdded{Version=[2],Text=[@b Changed representation clauses to aspect clauses to reflect that they are used for more than just representation.]} @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]} @ChgAdded{Version=[2],Text=[Defined @i{explicitly limited record} type to use in other rules.]} @end{DiffWord95} @LabeledSubClause{Variant Parts and Discrete Choices} @begin{Intro} A record type with a @nt specifies alternative lists of components. Each @nt defines the components for the value or values of the discriminant covered by its @nt. @begin{Discussion} @RootDefn{cover a value} @nt{Discrete_choice_list}s and @nt{discrete_choice}s are said to @i(cover) values as defined below; which @nt{discrete_choice_list} covers a value determines which of various alternatives is chosen. These are used in @nt{variant_part}s, @nt{array_aggregate}s, and @nt{case_statement}s. @end{Discussion} @end{Intro} @begin{MetaRules} The definition of @lquotes@;cover@rquotes@; in this subclause and the rules about discrete choices are designed so that they are also appropriate for array aggregates and case statements. The rules of this subclause intentionally parallel those for case statements. @end{MetaRules} @begin{Syntax} @Syn{lhs=,rhs=" @key{case} @SynI{discriminant_}@Syn2{direct_name} @key{is} @Syn2{variant} {@Syn2{variant}} @key{end} @key{case};"} @Syn{lhs=,rhs=" @key{when} @Syn2{discrete_choice_list} => @Syn2{component_list}"} @Syn{lhs=,rhs="@Syn2{discrete_choice} {| @Syn2{discrete_choice}}"} @Syn{lhs=,rhs="@Syn2{expression} | @Syn2{discrete_range} | @key{others}"} @end{Syntax} @begin{Resolution} @Defn2{Term=[discriminant], Sec=(of a @nt)} The @i(discriminant_)@nt{direct_name} shall resolve to denote a discriminant (called the @i(discriminant of the @nt)) specified in the @nt{known_discriminant_part} of the @nt{full_type_declaration} that contains the @nt{variant_part}. @PDefn2{Term=[expected type], Sec=(variant_part discrete_choice)} The expected type for each @nt{discrete_choice} in a @nt is the type of the discriminant of the @nt{variant_part}. @begin{Ramification} A @nt with a @nt has to have a (new) @nt; the discriminant of the @nt cannot be an inherited discriminant. @end{Ramification} @end{Resolution} @begin{Legality} The discriminant of the @nt{variant_part} shall be of a discrete type. @begin{Ramification} It shall not be of an access type, named or anonymous.@end{ramification} The @nt{expression}s and @nt{discrete_range}s given as @nt{discrete_choice}s in a @nt{variant_part} shall be static. The @nt{discrete_choice} @key(others) shall appear alone in a @nt{discrete_choice_list}, and such a @nt{discrete_choice_list}, if it appears, shall be the last one in the enclosing construct. @Leading@PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice})} A @nt is defined to @i(cover a value) in the following cases: @begin(itemize) A @nt{discrete_choice} that is an @nt{expression} covers a value if the value equals the value of the @nt{expression} converted to the expected type. A @nt{discrete_choice} that is a @nt{discrete_range} covers all values (possibly none) that belong to the range. The @nt{discrete_choice} @key{others} covers all values of its expected type that are not covered by previous @nt{discrete_choice_list}s of the same construct. @begin(Ramification) For @nt{case_statement}s, this includes values outside the range of the static subtype (if any) to be covered by the choices. It even includes values outside the base range of the case expression's type, since values of numeric types (and undefined values of any scalar type?) can be outside their base range. @end(Ramification) @end(itemize) @PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice_list})} A @nt{discrete_choice_list} covers a value if one of its @nt{discrete_choice}s covers the value. @Leading@keepnext@;The possible values of the discriminant of a @nt{variant_part} shall be covered as follows: @begin{itemize} If the discriminant is of a static constrained scalar subtype, then each non-@key{others} @nt{discrete_@!choice} shall cover only values in that subtype, and each value of that subtype shall be covered by some @nt{discrete_@!choice} @Redundant[(either explicitly or by @key)]; If the type of the discriminant is a descendant of a generic formal scalar type then the @nt{variant_part} shall have an @key{others} @nt{discrete_choice};@begin{Reason} The base range is not known statically in this case.@end{reason} Otherwise, each value of the base range of the type of the discriminant shall be covered @Redundant[(either explicitly or by @key)]. @end{itemize} Two distinct @nt{discrete_choice}s of a @nt{variant_part} shall not cover the same value. @end{Legality} @begin{StaticSem} If the @nt{component_list} of a @nt{variant} is specified by @key(null), the variant has no components. @Defn{govern a @nt{variant_part}} @Defn{govern a @nt{variant}} The discriminant of a @nt is said to @i(govern) the @nt and its @nts. In addition, the discriminant of a derived type governs a @nt and its @nts if it corresponds (see @RefSecNum(Discriminants)) to the discriminant of the @nt. @end{StaticSem} @begin{RunTime} A record value contains the values of the components of a particular @nt{variant} only if the value of the discriminant governing the @nt is covered by the @nt{discrete_choice_list} of the @nt{variant}. This rule applies in turn to any further @nt{variant} that is, itself, included in the @nt{component_list} of the given @nt{variant}. @PDefn2{Term=[elaboration], Sec=(variant_part)} The elaboration of a @nt{variant_part} consists of the elaboration of the @nt{component_list} of each @nt{variant} in the order in which they appear. @end{RunTime} @begin{Examples} @Leading@keepnext@i(Example of record type with a variant part: ) @begin(Example) @key(type) Device @key(is) (Printer, Disk, Drum); @key(type) State @key(is) (Open, Closed); @key(type) Peripheral(Unit : Device := Disk) @key(is) @key(record) Status : State; @key(case) Unit @key(is) @key(when) Printer => Line_Count : Integer @key(range) 1 .. Page_Size; @key(when) @key(others) => Cylinder : Cylinder_Index; Track : Track_Number; @key(end) @key(case); @key(end) @key(record); @end(Example) @begin{Wide} @leading@keepnext@i(Examples of record subtypes:) @end{Wide} @begin(Example) @key(subtype) Drum_Unit @key(is) Peripheral(Drum); @key(subtype) Disk_Unit @key(is) Peripheral(Disk); @end(Example) @begin{Wide} @leading@keepnext@i(Examples of constrained record variables:) @end{Wide} @begin(Example) Writer : Peripheral(Unit => Printer); Archive : Disk_Unit; @end(Example) @end{Examples} @begin{Extend83} @Defn{extensions to Ada 83} In Ada 83, the discriminant of a @nt{variant_part} is not allowed to be of a generic formal type. This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice} is required in this case. @end{Extend83} @begin{DiffWord83} The syntactic category @nt{choice} is removed. The syntax rules for @nt{variant}, @nt{array_aggregate}, and @nt{case_statement} now use @nt{discrete_choice_list} or @nt{discrete_choice} instead. The syntax rule for @nt{record_aggregate} now defines its own syntax for named associations. We have added the term Discrete Choice to the title since this is where they are talked about. This is analogous to the name of the subclause "Index Constraints and Discrete Ranges" in the clause on Array Types. The rule requiring that the discriminant denote a discriminant of the type being defined seems to have been left implicit in RM83. @end{DiffWord83}