-
A value of an access type (an access value) provides indirect access to
the object or subprogram it designates. Depending on its type, an access
value can designate either subprograms, objects created by allocators,
See section 4.8 Allocators, or more generally aliased objects of an appropriate type.
Syntax
-
access_type_definition ::=
access_to_object_definition
| access_to_subprogram_definition
-
access_to_object_definition ::=
access [general_access_modifier] subtype_indication
-
general_access_modifier ::= all | constant
-
access_to_subprogram_definition ::=
access [protected] procedure parameter_profile
| access [protected] function parameter_and_result_profile
-
access_definition ::= access subtype_mark
Static Semantics
-
There are two kinds of access types, access-to-object types, whose
values designate objects, and access-to-subprogram types, whose values
designate subprograms. Associated with an access-to-object type is a
storage pool; several access types may share the same storage pool. A
storage pool is an area of storage used to hold dynamically allocated
objects (called pool elements) created by allocators; storage pools are
described further in See section 13.11 Storage Management.
-
Access-to-object types are further subdivided into pool-specific access
types, whose values can designate only the elements of their associated
storage pool, and general access types, whose values can designate the
elements of any storage pool, as well as aliased objects created by
declarations rather than allocators, and aliased subcomponents of other
objects.
-
A view of an object is defined to be aliased if it is defined by an
object_declaration or component_definition with the reserved word
aliased, or by a renaming of an aliased view. In addition, the
dereference of an access-to-object value denotes an aliased view, as
does a view conversion, See section 4.6 Type Conversions of an aliased view. Finally, the
current instance of a limited type, and a formal parameter or generic
formal object of a tagged type are defined to be aliased. Aliased views
are the ones that can be designated by an access value. If the view
defined by an object_declaration is aliased, and the type of the object
has discriminants, then the object is constrained; if its nominal
subtype is unconstrained, then the object is constrained by its initial
value. Similarly, if the object created by an allocator has
discriminants, the object is constrained, either by the designated
subtype, or by its initial value.
-
An access_to_object_definition defines an access-to-object type and its
first subtype; the subtype_indication defines the designated subtype of
the access type. If a general_access_modifier appears, then the access
type is a general access type. If the modifier is the reserved word
constant, then the type is an access-to-constant type; a designated
object cannot be updated through a value of such a type. If the modifier
is the reserved word all, then the type is an access-to-variable type; a
designated object can be both read and updated through a value of such a
type. If no general_access_modifier appears in the
access_to_object_definition, the access type is a pool-specific
access-to-variable type.
-
An access_to_subprogram_definition defines an access-to-subprogram type
and its first subtype; the parameter_profile or
parameter_and_result_profile defines the designated profile of the
access type. There is a calling convention associated with the
designated profile; only subprograms with this calling convention can be
designated by values of the access type. By default, the calling
convention is "protected" if the reserved word protected appears, and
"Ada" otherwise. See Annex See section B Interface to Other Languages (normative) for how to override this default.
-
An access_definition defines an anonymous general access-to-variable
type; the subtype_mark denotes its designated subtype. An
access_definition is used in the specification of an access
discriminant, See section 3.7 Discriminants, or an access parameter, See section 6.1 Subprogram Declarations.
-
For each (named) access type, there is a literal null which has a null
access value designating no entity at all. The null value of a named
access type is the default initial value of the type. Other values of an
access type are obtained by evaluating an attribute_reference for the
Access or Unchecked_Access attribute of an aliased view of an object or
non-intrinsic subprogram, or, in the case of a named access-to-object
type, an allocator, which returns an access value designating a newly
created object, See section 3.10.2 Operations of Access Types.
-
All subtypes of an access-to-subprogram type are constrained. The first
subtype of a type defined by an access_type_definition or an
access_to_object_definition is unconstrained if the designated subtype
is an unconstrained array or discriminated type; otherwise it is
constrained.
Dynamic Semantics
-
A composite_constraint is compatible with an unconstrained access
subtype if it is compatible with the designated subtype. An access value
satisfies a composite_constraint of an access subtype if it equals the
null value of its type or if it designates an object whose value
satisfies the constraint.
-
The elaboration of an access_type_definition creates the access type and
its first subtype. For an access-to-object type, this elaboration
includes the elaboration of the subtype_indication, which creates the
designated subtype.
-
The elaboration of an access_definition creates an anonymous general
access-to-variable type (this happens as part of the initialization of
an access parameter or access discriminant).
NOTES
-
(77) Access values are called "pointers" or "references" in some
other languages.
-
(78) Each access-to-object type has an associated storage pool; several
access types can share the same pool. An object can be created in the
storage pool of an access type by an allocator, See section 4.8 Allocators, for the
access type. A storage pool (roughly) corresponds to what some other
languages call a "heap." See section 13.11 Storage Management, for a discussion of pools.
-
(79) Only index_constraints and discriminant_constraints can be applied
to access types, See section 3.6.1 Index Constraints and Discrete Ranges, and See section 3.7.1 Discriminant Constraints.
Examples
-
Examples of access-to-object types:
-
type Peripheral_Ref is access Peripheral; -- See section 3.8.1 Variant Parts and Discrete Choices
type Binop_Ptr is access all Binary_Operation'Class;
-- general access-to-class-wide, See section 3.9.1 Type Extensions
-
Example of an access subtype:
-
subtype Drum_Ref is Peripheral_Ref(Drum); -- See section 3.8.1 Variant Parts and Discrete Choices
-
Example of an access-to-subprogram type:
-
type Message_Procedure is access
procedure (M : in String := "Error!");
procedure Default_Message_Procedure(M : in String);
Give_Message : Message_Procedure := Default_Message_Procedure'Access;
...
procedure Other_Procedure(M : in String);
...
Give_Message := Other_Procedure'Access;
...
Give_Message("File not found.");
-- call with parameter (.all is optional)
Give_Message.all;
-- call with no parameters