OSDN Git Service

2007-01-26 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / checks.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               C H E C K S                                --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  Package containing routines used to deal with runtime checks. These
28 --  routines are used both by the semantics and by the expander. In some
29 --  cases, checks are enabled simply by setting flags for gigi, and in
30 --  other cases the code for the check is expanded.
31
32 --  The approach used for range and length checks, in regards to suppressed
33 --  checks, is to attempt to detect at compilation time that a constraint
34 --  error will occur. If this is detected a warning or error is issued and the
35 --  offending expression or statement replaced with a constraint error node.
36 --  This always occurs whether checks are suppressed or not.  Dynamic range
37 --  checks are, of course, not inserted if checks are suppressed.
38
39 with Types; use Types;
40 with Uintp; use Uintp;
41
42 package Checks is
43
44    procedure Initialize;
45    --  Called for each new main source program, to initialize internal
46    --  variables used in the package body of the Checks unit.
47
48    function Access_Checks_Suppressed        (E : Entity_Id) return Boolean;
49    function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean;
50    function Alignment_Checks_Suppressed     (E : Entity_Id) return Boolean;
51    function Discriminant_Checks_Suppressed  (E : Entity_Id) return Boolean;
52    function Division_Checks_Suppressed      (E : Entity_Id) return Boolean;
53    function Elaboration_Checks_Suppressed   (E : Entity_Id) return Boolean;
54    function Index_Checks_Suppressed         (E : Entity_Id) return Boolean;
55    function Length_Checks_Suppressed        (E : Entity_Id) return Boolean;
56    function Overflow_Checks_Suppressed      (E : Entity_Id) return Boolean;
57    function Range_Checks_Suppressed         (E : Entity_Id) return Boolean;
58    function Storage_Checks_Suppressed       (E : Entity_Id) return Boolean;
59    function Tag_Checks_Suppressed           (E : Entity_Id) return Boolean;
60    function Validity_Checks_Suppressed      (E : Entity_Id) return Boolean;
61    --  These functions check to see if the named check is suppressed, either
62    --  by an active scope suppress setting, or because the check has been
63    --  specifically suppressed for the given entity. If no entity is relevant
64    --  for the current check, then Empty is used as an argument. Note: the
65    --  reason we insist on specifying Empty is to force the caller to think
66    --  about whether there is any relevant entity that should be checked.
67
68    --  General note on following checks. These checks are always active if
69    --  Expander_Active and not Inside_A_Generic. They are inactive and have
70    --  no effect Inside_A_Generic. In the case where not Expander_Active
71    --  and not Inside_A_Generic, most of them are inactive, but some of them
72    --  operate anyway since they may generate useful compile time warnings.
73
74    procedure Apply_Access_Check (N : Node_Id);
75    --  Determines whether an expression node requires a runtime access
76    --  check and if so inserts the appropriate run-time check.
77
78    procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id);
79    --  Given a name N denoting an access parameter, emits a run-time
80    --  accessibility check (if necessary), checking that the level of
81    --  the object denoted by the access parameter is not deeper than the
82    --  level of the type Typ. Program_Error is raised if the check fails.
83
84    procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id);
85    --  E is the entity for an object which has an address clause. If checks
86    --  are enabled, then this procedure generates a check that the specified
87    --  address has an alignment consistent with the alignment of the object,
88    --  raising PE if this is not the case. The resulting check (if one is
89    --  generated) is inserted before node N. check is also made for the case of
90    --  a clear overlay situation that the size of the overlaying object is not
91    --  larger than the overlaid object.
92
93    procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id);
94    --  N is the node for an object declaration that declares an object of
95    --  array type Typ. This routine generates, if necessary, a check that
96    --  the size of the array is not too large, raising Storage_Error if so.
97
98    procedure Apply_Arithmetic_Overflow_Check (N : Node_Id);
99    --  Given a binary arithmetic operator (+ - *) expand a software integer
100    --  overflow check using range checks on a larger checking type or a call
101    --  to an appropriate runtime routine. This is used for all three operators
102    --  for the signed integer case, and for +/- in the fixed-point case. The
103    --  check is expanded only if Software_Overflow_Checking is enabled and
104    --  Do_Overflow_Check is set on node N. Note that divide is handled
105    --  separately using Apply_Arithmetic_Divide_Overflow_Check.
106
107    procedure Apply_Constraint_Check
108      (N          : Node_Id;
109       Typ        : Entity_Id;
110       No_Sliding : Boolean := False);
111    --  Top-level procedure, calls all the others depending on the class of Typ.
112    --  Checks that expression N verifies the constraint of type Typ. No_Sliding
113    --  is only relevant for constrained array types, id set to true, it
114    --  checks that indexes are in range.
115
116    procedure Apply_Discriminant_Check
117      (N   : Node_Id;
118       Typ : Entity_Id;
119       Lhs : Node_Id := Empty);
120    --  Given an expression N of a discriminated type, or of an access type
121    --  whose designated type is a discriminanted type, generates a check to
122    --  ensure that the expression can be converted to the subtype given as
123    --  the second parameter. Lhs is empty except in the case of assignments,
124    --  where the target object may be needed to determine the subtype to
125    --  check against (such as the cases of unconstrained formal parameters
126    --  and unconstrained aliased objects). For the case of unconstrained
127    --  formals, the check is peformed only if the corresponding actual is
128    --  constrained, i.e., whether Lhs'Constrained is True.
129
130    function Build_Discriminant_Checks
131      (N     : Node_Id;
132       T_Typ : Entity_Id)
133       return  Node_Id;
134    --  Subsidiary routine for Apply_Discriminant_Check. Builds the expression
135    --  that compares discriminants of the expression with discriminants of the
136    --  type. Also used directly for membership tests (see Exp_Ch4.Expand_N_In).
137
138    procedure Apply_Divide_Check (N : Node_Id);
139    --  The node kind is N_Op_Divide, N_Op_Mod, or N_Op_Rem. An appropriate
140    --  check is generated to ensure that the right operand is non-zero. In
141    --  the divide case, we also check that we do not have the annoying case
142    --  of the largest negative number divided by minus one.
143
144    procedure Apply_Type_Conversion_Checks (N : Node_Id);
145    --  N is an N_Type_Conversion node. A type conversion actually involves
146    --  two sorts of checks. The first check is the checks that ensures that
147    --  the operand in the type conversion fits onto the base type of the
148    --  subtype it is being converted to (see RM 4.6 (28)-(50)). The second
149    --  check is there to ensure that once the operand has been converted to
150    --  a value of the target type, this converted value meets the
151    --  constraints imposed by the target subtype (see RM 4.6 (51)).
152
153    procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id);
154    --  The argument N is an attribute reference node intended for processing
155    --  by gigi. The attribute is one that returns a universal integer, but
156    --  the attribute reference node is currently typed with the expected
157    --  result type. This routine deals with range and overflow checks needed
158    --  to make sure that the universal result is in range.
159
160    procedure Determine_Range
161      (N  : Node_Id;
162       OK : out Boolean;
163       Lo : out Uint;
164       Hi : out Uint);
165    --  N is a node for a subexpression. If N is of a discrete type with no
166    --  error indications, and no other peculiarities (e.g. missing type
167    --  fields), then OK is True on return, and Lo and Hi are set to a
168    --  conservative estimate of the possible range of values of N. Thus if OK
169    --  is True on return, the value of the subexpression N is known to like in
170    --  the range Lo .. Hi (inclusive). If the expression is not of a discrete
171    --  type, or some kind of error condition is detected, then OK is False on
172    --  exit, and Lo/Hi are set to No_Uint. Thus the significance of OK being
173    --  False on return is that no useful information is available on the range
174    --  of the expression.
175
176    procedure Install_Null_Excluding_Check (N : Node_Id);
177    --  Determines whether an access node requires a runtime access check and
178    --  if so inserts the appropriate run-time check.
179
180    -------------------------------------------------------
181    -- Control and Optimization of Range/Overflow Checks --
182    -------------------------------------------------------
183
184    --  Range checks are controlled by the Do_Range_Check flag. The front end
185    --  is responsible for setting this flag in relevant nodes. Originally
186    --  the back end generated all corresponding range checks. But later on
187    --  we decided to generate all range checks in the front end. We are now
188    --  in the transitional phase where some of these checks are still done
189    --  by the back end, but many are done by the front end.
190
191    --  Overflow checks are similarly controlled by the Do_Overflow_Check flag.
192    --  The difference here is that if Backend_Overflow_Checks is is
193    --  (Backend_Overflow_Checks_On_Target set False), then the actual overflow
194    --  checks are generated by the front end, but if back end overflow checks
195    --  are active (Backend_Overflow_Checks_On_Target set True), then the back
196    --  end does generate the checks.
197
198    --  The following two routines are used to set these flags, they allow
199    --  for the possibility of eliminating checks. Checks can be eliminated
200    --  if an identical check has already been performed.
201
202    procedure Enable_Overflow_Check (N : Node_Id);
203    --  First this routine determines if an overflow check is needed by doing
204    --  an appropriate range check. If a check is not needed, then the call
205    --  has no effect. If a check is needed then this routine sets the flag
206    --  Set Do_Overflow_Check in node N to True, unless it can be determined
207    --  that the check is not needed. The only condition under which this is
208    --  the case is if there was an identical check earlier on.
209
210    procedure Enable_Range_Check (N : Node_Id);
211    --  Set Do_Range_Check flag in node N True, unless it can be determined
212    --  that the check is not needed. The only condition under which this is
213    --  the case is if there was an identical check earlier on. This routine
214    --  is not responsible for doing range analysis to determine whether or
215    --  not such a check is needed -- the caller is expected to do this. The
216    --  one other case in which the request to set the flag is ignored is
217    --  when Kill_Range_Check is set in an N_Unchecked_Conversion node.
218
219    --  The following routines are used to keep track of processing sequences
220    --  of statements (e.g. the THEN statements of an IF statement). A check
221    --  that appears within such a sequence can eliminate an identical check
222    --  within this sequence of statements. However, after the end of the
223    --  sequence of statements, such a check is no longer of interest, since
224    --  it may not have been executed.
225
226    procedure Conditional_Statements_Begin;
227    --  This call marks the start of processing of a sequence of statements.
228    --  Every call to this procedure must be followed by a matching call to
229    --  Conditional_Statements_End.
230
231    procedure Conditional_Statements_End;
232    --  This call removes from consideration all saved checks since the
233    --  corresponding call to Conditional_Statements_Begin. These two
234    --  procedures operate in a stack like manner.
235
236    --  The mechanism for optimizing checks works by remembering checks
237    --  that have already been made, but certain conditions, for example
238    --  an assignment to a variable involved in a check, may mean that the
239    --  remembered check is no longer valid, in the sense that if the same
240    --  expression appears again, another check is required because the
241    --  value may have changed.
242
243    --  The following routines are used to note conditions which may render
244    --  some or all of the stored and remembered checks to be invalidated.
245
246    procedure Kill_Checks (V : Entity_Id);
247    --  This procedure records an assignment or other condition that causes
248    --  the value of the variable to be changed, invalidating any stored
249    --  checks that reference the value. Note that all such checks must
250    --  be discarded, even if they are not in the current statement range.
251
252    procedure Kill_All_Checks;
253    --  This procedure kills all remembered checks
254
255    -----------------------------
256    -- Length and Range Checks --
257    -----------------------------
258
259    --  In the following procedures, there are three arguments which have
260    --  a common meaning as follows:
261
262    --    Expr        The expression to be checked. If a check is required,
263    --                the appropriate flag will be placed on this node. Whether
264    --                this node is further examined depends on the setting of
265    --                the parameter Source_Typ, as described below.
266
267    --    Target_Typ  The target type on which the check is to be based. For
268    --                example, if we have a scalar range check, then the check
269    --                is that we are in range of this type.
270
271    --    Source_Typ  Normally Empty, but can be set to a type, in which case
272    --                this type is used for the check, see below.
273
274    --  The checks operate in one of two modes:
275
276    --    If Source_Typ is Empty, then the node Expr is examined, at the very
277    --    least to get the source subtype. In addition for some of the checks,
278    --    the actual form of the node may be examined. For example, a node of
279    --    type Integer whose actual form is an Integer conversion from a type
280    --    with range 0 .. 3 can be determined to have a value in range 0 .. 3.
281
282    --    If Source_Typ is given, then nothing can be assumed about the Expr,
283    --    and indeed its contents are not examined. In this case the check is
284    --    based on the assumption that Expr can be an arbitrary value of the
285    --    given Source_Typ.
286
287    --  Currently, the only case in which a Source_Typ is explicitly supplied
288    --  is for the case of Out and In_Out parameters, where, for the conversion
289    --  on return (the Out direction), the types must be reversed. This is
290    --  handled by the caller.
291
292    procedure Apply_Length_Check
293      (Ck_Node    : Node_Id;
294       Target_Typ : Entity_Id;
295       Source_Typ : Entity_Id := Empty);
296    --  This procedure builds a sequence of declarations to do a length check
297    --  that checks if the lengths of the two arrays Target_Typ and source type
298    --  are the same. The resulting actions are inserted at Node using a call
299    --  to Insert_Actions.
300    --
301    --  For access types, the Directly_Designated_Type is retrieved and
302    --  processing continues as enumerated above, with a guard against null
303    --  values.
304    --
305    --  Note: calls to Apply_Length_Check currently never supply an explicit
306    --  Source_Typ parameter, but Apply_Length_Check takes this parameter and
307    --  processes it as described above for consistency with the other routines
308    --  in this section.
309
310    procedure Apply_Range_Check
311      (Ck_Node    : Node_Id;
312       Target_Typ : Entity_Id;
313       Source_Typ : Entity_Id := Empty);
314    --  For an Node of kind N_Range, constructs a range check action that tests
315    --  first that the range is not null and then that the range is contained in
316    --  the Target_Typ range.
317    --
318    --  For scalar types, constructs a range check action that first tests that
319    --  the expression is contained in the Target_Typ range. The difference
320    --  between this and Apply_Scalar_Range_Check is that the latter generates
321    --  the actual checking code in gigi against the Etype of the expression.
322    --
323    --  For constrained array types, construct series of range check actions
324    --  to check that each Expr range is properly contained in the range of
325    --  Target_Typ.
326    --
327    --  For a type conversion to an unconstrained array type, constructs a range
328    --  check action to check that the bounds of the source type are within the
329    --  constraints imposed by the Target_Typ.
330    --
331    --  For access types, the Directly_Designated_Type is retrieved and
332    --  processing continues as enumerated above, with a guard against null
333    --  values.
334    --
335    --  The source type is used by type conversions to unconstrained array
336    --  types to retrieve the corresponding bounds.
337
338    procedure Apply_Static_Length_Check
339      (Expr       : Node_Id;
340       Target_Typ : Entity_Id;
341       Source_Typ : Entity_Id := Empty);
342    --  Tries to determine statically whether the two array types source type
343    --  and Target_Typ have the same length. If it can be determined at compile
344    --  time that they do not, then an N_Raise_Constraint_Error node replaces
345    --  Expr, and a warning message is issued.
346
347    procedure Apply_Scalar_Range_Check
348      (Expr       : Node_Id;
349       Target_Typ : Entity_Id;
350       Source_Typ : Entity_Id := Empty;
351       Fixed_Int  : Boolean   := False);
352    --  For scalar types, determines whether an expression node should be
353    --  flagged as needing a runtime range check. If the node requires such a
354    --  check, the Do_Range_Check flag is turned on. The Fixed_Int flag if set
355    --  causes any fixed-point values to be treated as though they were discrete
356    --  values (i.e. the underlying integer value is used).
357
358    type Check_Result is private;
359    --  Type used to return result of Range_Check call, for later use in
360    --  call to Insert_Range_Checks procedure.
361
362    procedure Append_Range_Checks
363      (Checks       : Check_Result;
364       Stmts        : List_Id;
365       Suppress_Typ : Entity_Id;
366       Static_Sloc  : Source_Ptr;
367       Flag_Node    : Node_Id);
368    --  Called to append range checks as returned by a call to Range_Check.
369    --  Stmts is a list to which either the dynamic check is appended or the
370    --  raise Constraint_Error statement is appended (for static checks).
371    --  Static_Sloc is the Sloc at which the raise CE node points, Flag_Node is
372    --  used as the node at which to set the Has_Dynamic_Check flag. Checks_On
373    --  is a boolean value that says if range and index checking is on or not.
374
375    procedure Insert_Range_Checks
376      (Checks       : Check_Result;
377       Node         : Node_Id;
378       Suppress_Typ : Entity_Id;
379       Static_Sloc  : Source_Ptr := No_Location;
380       Flag_Node    : Node_Id    := Empty;
381       Do_Before    : Boolean    := False);
382    --  Called to insert range checks as returned by a call to Range_Check.
383    --  Node is the node after which either the dynamic check is inserted or
384    --  the raise Constraint_Error statement is inserted (for static checks).
385    --  Suppress_Typ is the type to check to determine if checks are suppressed.
386    --  Static_Sloc, if passed, is the Sloc at which the raise CE node points,
387    --  otherwise Sloc (Node) is used. The Has_Dynamic_Check flag is normally
388    --  set at Node. If Flag_Node is present, then this is used instead as the
389    --  node at which to set the Has_Dynamic_Check flag. Normally the check is
390    --  inserted after, if Do_Before is True, the check is inserted before
391    --  Node.
392
393    function Range_Check
394      (Ck_Node    : Node_Id;
395       Target_Typ : Entity_Id;
396       Source_Typ : Entity_Id := Empty;
397       Warn_Node  : Node_Id   := Empty)
398       return       Check_Result;
399    --  Like Apply_Range_Check, except it does not modify anything. Instead
400    --  it returns an encapsulated result of the check operations for later
401    --  use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its
402    --  Sloc is used, in the static case, for the generated warning or error.
403    --  Additionally, it is used rather than Expr (or Low/High_Bound of Expr)
404    --  in constructing the check.
405
406    -----------------------
407    -- Expander Routines --
408    -----------------------
409
410    --  Some of the earlier processing for checks results in temporarily setting
411    --  the Do_Range_Check flag rather than actually generating checks. Now we
412    --  are moving the generation of such checks into the front end for reasons
413    --  of efficiency and simplicity (there were difficutlies in handling this
414    --  in the back end when side effects were present in the expressions being
415    --  checked).
416
417    --  Probably we could eliminate the Do_Range_Check flag entirely and
418    --  generate the checks earlier, but this is a delicate area and it
419    --  seemed safer to implement the following routines, which are called
420    --  late on in the expansion process. They check the Do_Range_Check flag
421    --  and if it is set, generate the actual checks and reset the flag.
422
423    procedure Generate_Range_Check
424      (N           : Node_Id;
425       Target_Type : Entity_Id;
426       Reason      : RT_Exception_Code);
427    --  This procedure is called to actually generate and insert a range check.
428    --  A check is generated to ensure that the value of N lies within the range
429    --  of the target type. Note that the base type of N may be different from
430    --  the base type of the target type. This happens in the conversion case.
431    --  The Reason parameter is the exception code to be used for the exception
432    --  if raised.
433    --
434    --  Note on the relation of this routine to the Do_Range_Check flag. Mostly
435    --  for historical reasons, we often set the Do_Range_Check flag and then
436    --  later we call Generate_Range_Check if this flag is set. Most probably we
437    --  could eliminate this intermediate setting of the flag (historically the
438    --  back end dealt with range checks, using this flag to indicate if a check
439    --  was required, then we moved checks into the front end).
440
441    procedure Generate_Index_Checks (N : Node_Id);
442    --  This procedure is called to generate index checks on the subscripts for
443    --  the indexed component node N. Each subscript expression is examined, and
444    --  if the Do_Range_Check flag is set, an appropriate index check is
445    --  generated and the flag is reset.
446
447    --  Similarly, we set the flag Do_Discriminant_Check in the semantic
448    --  analysis to indicate that a discriminant check is required for selected
449    --  component of a discriminated type. The following routine is called from
450    --  the expander to actually generate the call.
451
452    procedure Generate_Discriminant_Check (N : Node_Id);
453    --  N is a selected component for which a discriminant check is required to
454    --  make sure that the discriminants have appropriate values for the
455    --  selection. This is done by calling the appropriate discriminant checking
456    --  routine for the selector.
457
458    -----------------------
459    -- Validity Checking --
460    -----------------------
461
462    --  In (RM 13.9.1(9-11)) we have the following rules on invalid values
463
464    --    If the representation of a scalar object does not represent value of
465    --    the object's subtype (perhaps because the object was not initialized),
466    --    the object is said to have an invalid representation. It is a bounded
467    --    error to evaluate the value of such an object. If the error is
468    --    detected, either Constraint_Error or Program_Error is raised.
469    --    Otherwise, execution continues using the invalid representation. The
470    --    rules of the language outside this subclause assume that all objects
471    --    have valid representations. The semantics of operations on invalid
472    --    representations are as follows:
473    --
474    --       10  If the representation of the object represents a value of the
475    --           object's type, the value of the type is used.
476    --
477    --       11  If the representation of the object does not represent a value
478    --           of the object's type, the semantics of operations on such
479    --           representations is implementation-defined, but does not by
480    --           itself lead to erroneous or unpredictable execution, or to
481    --           other objects becoming abnormal.
482
483    --  We quote the rules in full here since they are quite delicate. Most
484    --  of the time, we can just compute away with wrong values, and get a
485    --  possibly wrong result, which is well within the range of allowed
486    --  implementation defined behavior. The two tricky cases are subscripted
487    --  array assignments, where we don't want to do wild stores, and case
488    --  statements where we don't want to do wild jumps.
489
490    --  In GNAT, we control validity checking with a switch -gnatV that can take
491    --  three parameters, n/d/f for None/Default/Full. These modes have the
492    --  following meanings:
493
494    --    None (no validity checking)
495
496    --      In this mode, there is no specific checking for invalid values
497    --      and the code generator assumes that all stored values are always
498    --      within the bounds of the object subtype. The consequences are as
499    --      follows:
500
501    --        For case statements, an out of range invalid value will cause
502    --        Constraint_Error to be raised, or an arbitrary one of the case
503    --        alternatives will be executed. Wild jumps cannot result even
504    --        in this mode, since we always do a range check
505
506    --        For subscripted array assignments, wild stores will result in
507    --        the expected manner when addresses are calculated using values
508    --        of subscripts that are out of range.
509
510    --      It could perhaps be argued that this mode is still conformant with
511    --      the letter of the RM, since implementation defined is a rather
512    --      broad category, but certainly it is not in the spirit of the
513    --      RM requirement, since wild stores certainly seem to be a case of
514    --      erroneous behavior.
515
516    --    Default (default standard RM-compatible validity checking)
517
518    --      In this mode, which is the default, minimal validity checking is
519    --      performed to ensure no erroneous behavior as follows:
520
521    --        For case statements, an out of range invalid value will cause
522    --        Constraint_Error to be raised.
523
524    --        For subscripted array assignments, invalid out of range
525    --        subscript values will cause Constraint_Error to be raised.
526
527    --    Full (Full validity checking)
528
529    --      In this mode, the protections guaranteed by the standard mode are
530    --      in place, and the following additional checks are made:
531
532    --        For every assignment, the right side is checked for validity
533
534    --        For every call, IN and IN OUT parameters are checked for validity
535
536    --        For every subscripted array reference, both for stores and loads,
537    --        all subscripts are checked for validity.
538
539    --      These checks are not required by the RM, but will in practice
540    --      improve the detection of uninitialized variables, particularly
541    --      if used in conjunction with pragma Normalize_Scalars.
542
543    --  In the above description, we talk about performing validity checks,
544    --  but we don't actually generate a check in a case where the compiler
545    --  can be sure that the value is valid. Note that this assurance must
546    --  be achieved without assuming that any uninitialized value lies within
547    --  the range of its type. The following are cases in which values are
548    --  known to be valid. The flag Is_Known_Valid is used to keep track of
549    --  some of these cases.
550
551    --    If all possible stored values are valid, then any uninitialized
552    --    value must be valid.
553
554    --    Literals, including enumeration literals, are clearly always valid
555
556    --    Constants are always assumed valid, with a validity check being
557    --    performed on the initializing value where necessary to ensure that
558    --    this is the case.
559
560    --    For variables, the status is set to known valid if there is an
561    --    initializing expression. Again a check is made on the initializing
562    --    value if necessary to ensure that this assumption is valid. The
563    --    status can change as a result of local assignments to a variable.
564    --    If a known valid value is unconditionally assigned, then we mark
565    --    the left side as known valid. If a value is assigned that is not
566    --    known to be valid, then we mark the left side as invalid. This
567    --    kind of processing does NOT apply to non-local variables since we
568    --    are not following the flow graph (more properly the flow of actual
569    --    processing only corresponds to the flow graph for local assignments).
570    --    For non-local variables, we preserve the current setting, i.e. a
571    --    validity check is performed when assigning to a knonwn valid global.
572
573    --  Note: no validity checking is required if range checks are suppressed
574    --  regardless of the setting of the validity checking mode.
575
576    --  The following procedures are used in handling validity checking
577
578    procedure Apply_Subscript_Validity_Checks (Expr : Node_Id);
579    --  Expr is the node for an indexed component. If validity checking and
580    --  range checking are enabled, all subscripts for this indexed component
581    --  are checked for validity.
582
583    procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id);
584    --  Expr is a lvalue, i.e. an expression representing the target of an
585    --  assignment. This procedure checks for this expression involving an
586    --  assignment to an array value. We have to be sure that all the subscripts
587    --  in such a case are valid, since according to the rules in (RM
588    --  13.9.1(9-11)) such assignments are not permitted to result in erroneous
589    --  behavior in the case of invalid subscript values.
590
591    procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False);
592    --  Ensure that Expr represents a valid value of its type. If this type
593    --  is not a scalar type, then the call has no effect, since validity
594    --  is only an issue for scalar types. The effect of this call is to
595    --  check if the value is known valid, if so, nothing needs to be done.
596    --  If this is not known, then either Expr is set to be range checked,
597    --  or specific checking code is inserted so that an exception is raised
598    --  if the value is not valid.
599    --
600    --  The optional argument Holes_OK indicates whether it is necessary to
601    --  worry about enumeration types with non-standard representations leading
602    --  to "holes" in the range of possible representations. If Holes_OK is
603    --  True, then such values are assumed valid (this is used when the caller
604    --  will make a separate check for this case anyway). If Holes_OK is False,
605    --  then this case is checked, and code is inserted to ensure that Expr is
606    --  valid, raising Constraint_Error if the value is not valid.
607
608    function Expr_Known_Valid (Expr : Node_Id) return Boolean;
609    --  This function tests it the value of Expr is known to be valid in the
610    --  sense of RM 13.9.1(9-11). In the case of GNAT, it is only discrete types
611    --  which are a concern, since for non-discrete types we simply continue
612    --  computation with invalid values, which does not lead to erroneous
613    --  behavior. Thus Expr_Known_Valid always returns True if the type of Expr
614    --  is non-discrete. For discrete types the value returned is True only if
615    --  it can be determined that the value is Valid. Otherwise False is
616    --  returned.
617
618    procedure Insert_Valid_Check (Expr : Node_Id);
619    --  Inserts code that will check for the value of Expr being valid, in
620    --  the sense of the 'Valid attribute returning True. Constraint_Error
621    --  will be raised if the value is not valid.
622
623    procedure Null_Exclusion_Static_Checks (N : Node_Id);
624    --  Ada 2005 (AI-231): Check bad usages of the null-exclusion issue
625
626    procedure Remove_Checks (Expr : Node_Id);
627    --  Remove all checks from Expr except those that are only executed
628    --  conditionally (on the right side of And Then/Or Else. This call
629    --  removes only embedded checks (Do_Range_Check, Do_Overflow_Check).
630
631    procedure Validity_Check_Range (N : Node_Id);
632    --  If N is an N_Range node, then Ensure_Valid is called on its bounds,
633    --  if validity checking of operands is enabled.
634
635 private
636
637    type Check_Result is array (Positive range 1 .. 2) of Node_Id;
638    --  There are two cases for the result returned by Range_Check:
639    --
640    --    For the static case the result is one or two nodes that should cause
641    --    a Constraint_Error. Typically these will include Expr itself or the
642    --    direct descendents of Expr, such as Low/High_Bound (Expr)). It is the
643    --    responsibility of the caller to rewrite and substitute the nodes with
644    --    N_Raise_Constraint_Error nodes.
645    --
646    --    For the non-static case a single N_Raise_Constraint_Error node with a
647    --    non-empty Condition field is returned.
648    --
649    --  Unused entries in Check_Result, if any, are simply set to Empty For
650    --  external clients, the required processing on this result is achieved
651    --  using the Insert_Range_Checks routine.
652
653    pragma Inline (Apply_Length_Check);
654    pragma Inline (Apply_Range_Check);
655    pragma Inline (Apply_Static_Length_Check);
656 end Checks;