summaryrefslogtreecommitdiff
path: root/gcc/ada/gnat_rm.texi
diff options
context:
space:
mode:
authorcharlet <charlet@138bc75d-0d04-0410-961f-82ee72b054a4>2004-08-13 10:24:46 +0000
committercharlet <charlet@138bc75d-0d04-0410-961f-82ee72b054a4>2004-08-13 10:24:46 +0000
commit8cc59fa3df92c095c8fac0ce70bca19902ec3389 (patch)
treec7c2be77cbb0fac9968fa1b4ce500bd59db9d31d /gcc/ada/gnat_rm.texi
parentd7bdd8d321be21812e90e422a014e68937389e9a (diff)
downloadgcc-8cc59fa3df92c095c8fac0ce70bca19902ec3389.tar.gz
2004-08-13 Olivier Hainque <hainque@act-europe.fr>
* decl.c (gnat_to_gnu_entity) <E_Variable>: When building an allocator for a global aliased object with a variable size and an unconstrained nominal subtype, pretend there is no initializer if the one we have is incomplete, and avoid referencing an inexistant component in there. The part we have will be rebuilt anyway and the reference may confuse further operations. 2004-08-13 Thomas Quinot <quinot@act-europe.fr> * einfo.ads: Minor reformatting * lib-writ.adb (Output_Main_Program_Line): Do not set parameter restrictions in the ALI if we only want to warn about violations. 2004-08-13 Vincent Celier <celier@gnat.com> * ali.adb (Scan_ALI): Initialize component Body_Needed_For_SAL to False when creating a new Unit_Record in table Units. * gnatls.adb (Output_Unit): In verbose mode, output the restrictions that are violated, if any. * prj-nmsc.adb (Ada_Check.Get_Path_Names_And_Record_Sources): Do not add directory separator if path already ends with a directory separator. 2004-08-13 Ed Schonberg <schonberg@gnat.com> * rtsfind.adb (Entity_Not_Defined): If the error ocurrs in a predefined unit, this is an attempt to inline a construct that is not available in the current restricted mode, so abort rather than trying to continue. * sem_ch3.adb (Build_Underlying_Full_View): If the new type has discriminants that rename those of the parent, recover names of original discriminants for the constraint on the full view of the parent. (Complete_Private_Subtype): Do not create a subtype declaration if the subtype is an itype. * gnat_rm.texi: Added section on implementation of discriminated records with default values for discriminants. 2004-08-13 Ed Schonberg <schonberg@gnat.com> PR ada/15601 * sem_res.adb (Make_Call_Into_Operator): Handle properly the case where the second operand is overloaded. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@85934 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/ada/gnat_rm.texi')
-rw-r--r--gcc/ada/gnat_rm.texi94
1 files changed, 94 insertions, 0 deletions
diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi
index d3d28367e88..82c390ab34f 100644
--- a/gcc/ada/gnat_rm.texi
+++ b/gcc/ada/gnat_rm.texi
@@ -380,6 +380,7 @@ Implementation of Specific Ada Features
* GNAT Implementation of Tasking::
* GNAT Implementation of Shared Passive Packages::
* Code Generation for Array Aggregates::
+* The Size of Discriminated Records with Default Discriminants::
Project File Reference
@@ -12798,6 +12799,7 @@ facilities.
* GNAT Implementation of Tasking::
* GNAT Implementation of Shared Passive Packages::
* Code Generation for Array Aggregates::
+* The Size of Discriminated Records with Default Discriminants::
@end menu
@node Machine Code Insertions
@@ -13342,6 +13344,98 @@ If any of these conditions are violated, the aggregate will be built in
a temporary (created either by the front-end or the code generator) and then
that temporary will be copied onto the target.
+
+@node The Size of Discriminated Records with Default Discriminants
+@section The Size of Discriminated Records with Default Discriminants
+
+@noindent
+If a discriminated type @code{T} has discriminants with default values, it is
+possible to declare an object of this type without providing an explicit
+constraint:
+
+@smallexample @c ada
+@group
+type Size is range 1..100;
+
+type Rec (D : Size := 15) is record
+ Name : String (1..D);
+end T;
+
+Word : Rec;
+@end group
+@end smallexample
+
+@noindent
+Such an object is said to be @emph{unconstrained}.
+The discriminant of the object
+can be modified by a full assignment to the object, as long as it preserves the
+relation between the value of the discriminant, and the value of the components
+that depend on it:
+
+@smallexample @c ada
+@group
+Word := (3, "yes");
+
+Word := (5, "maybe");
+
+Word := (5, "no"); -- raises Constraint_Error
+@end group
+@end smallexample
+
+@noindent
+In order to support this behavior efficiently, an unconstrained object is
+given the maximum size that any value of the type requires. In the case
+above, @code{Word} has storage for the discriminant and for
+a @code{String} of length 100.
+It is important to note that unconstrained objects do not require dynamic
+allocation. It would be an improper implementation to place on the heap those
+components whose size depends on discriminants. (This improper implementation
+was used by some Ada83 compilers, where the @code{Name} component above
+would have
+been stored as a pointer to a dynamic string). Following the principle that
+dynamic storage management should never be introduced implicitly,
+an Ada95 compiler should reserve the full size for an unconstrained declared
+object, and place it on the stack.
+
+This maximum size approach
+has been a source of surprise to some users, who expect the default
+values of the discriminants to determine the size reserved for an
+unconstrained object: ``If the default is 15, why should the object occupy
+a larger size?''
+The answer, of course, is that the discriminant may be later modified,
+and its full range of values must be taken into account. This is why the
+declaration:
+
+@smallexample
+@group
+type Rec (D : Positive := 15) is record
+ Name : String (1..D);
+end record;
+
+Too_Large : Rec;
+@end group
+@end smallexample
+
+@noindent
+is flagged by the compiler with a warning:
+an attempt to create @code{Too_Large} will raise @code{Storage_Error},
+because the required size includes @code{Positive'Last}
+bytes. As the first example indicates, the proper approach is to declare an
+index type of ``reasonable'' range so that unconstrained objects are not too
+large.
+
+One final wrinkle: if the object is declared to be @code{aliased}, or if it is
+created in the heap by means of an allocator, then it is @emph{not}
+unconstrained:
+it is constrained by the default values of the discriminants, and those values
+cannot be modified by full assignment. This is because in the presence of
+aliasing all views of the object (which may be manipulated by different tasks,
+say) must be consistent, so it is imperative that the object, once created,
+remain invariant.
+
+
+
+
@node Project File Reference
@chapter Project File Reference