* config/xtensa/xtensa.h (GO_IF_MODE_DEPENDENT_ADDRESS): Treat
[official-gcc.git] / gcc / ada / checks.ads
blobdf2c4624359d07d5d0d8eacc6ac16cc71de0c519
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- C H E C K S --
6 -- --
7 -- S p e c --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 -- Package containing routines used to deal with runtime checks. These
29 -- routines are used both by the semantics and by the expander. In some
30 -- cases, checks are enabled simply by setting flags for gigi, and in
31 -- other cases the code for the check is expanded.
33 -- The approach used for range and length checks, in regards to suppressed
34 -- checks, is to attempt to detect at compilation time that a constraint
35 -- error will occur. If this is detected a warning or error is issued and the
36 -- offending expression or statement replaced with a constraint error node.
37 -- This always occurs whether checks are suppressed or not. Dynamic range
38 -- checks are, of course, not inserted if checks are suppressed.
40 with Types; use Types;
41 with Uintp; use Uintp;
43 package Checks is
45 procedure Initialize;
46 -- Called for each new main source program, to initialize internal
47 -- variables used in the package body of the Checks unit.
49 function Access_Checks_Suppressed (E : Entity_Id) return Boolean;
50 function Accessibility_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 -- These functions check to see if the named check is suppressed,
61 -- either by an active scope suppress setting, or because the check
62 -- has been specifically suppressed for the given entity. If no entity
63 -- is relevant for the current check, then Empty is used as an argument.
64 -- Note: the reason we insist on specifying Empty is to force the
65 -- caller to think about whether there is any relevant entity that
66 -- should be checked.
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.
74 procedure Apply_Access_Check (N : Node_Id);
75 -- Determines whether an expression node should be flagged as needing
76 -- a runtime access check. If the node requires such a check, the
77 -- Do_Access_Check flag is turned on.
79 procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id);
80 -- Given a name N denoting an access parameter, emits a run-time
81 -- accessibility check (if necessary), checking that the level of
82 -- the object denoted by the access parameter is not deeper than the
83 -- level of the type Typ. Program_Error is raised if the check fails.
85 procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id);
86 -- E is the entity for an object. If there is an address clause for
87 -- this entity, and checks are enabled, then this procedure generates
88 -- a check that the specified address has an alignment consistent with
89 -- the alignment of the object, raising PE if this is not the case. The
90 -- resulting check (if one is generated) is inserted before node N.
92 procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id);
93 -- N is the node for an object declaration that declares an object of
94 -- array type Typ. This routine generates, if necessary, a check that
95 -- the size of the array is not too large, raising Storage_Error if so.
97 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id);
98 -- Given a binary arithmetic operator (+ - *) expand a software integer
99 -- overflow check using range checks on a larger checking type or a call
100 -- to an appropriate runtime routine. This is used for all three operators
101 -- for the signed integer case, and for +/- in the fixed-point case. The
102 -- check is expanded only if Software_Overflow_Checking is enabled and
103 -- Do_Overflow_Check is set on node N. Note that divide is handled
104 -- separately using Apply_Arithmetic_Divide_Overflow_Check.
106 procedure Apply_Constraint_Check
107 (N : Node_Id;
108 Typ : Entity_Id;
109 No_Sliding : Boolean := False);
110 -- Top-level procedure, calls all the others depending on the class of Typ.
111 -- Checks that expression N verifies the constraint of type Typ. No_Sliding
112 -- is only relevant for constrained array types, id set to true, it
113 -- checks that indexes are in range.
115 procedure Apply_Discriminant_Check
116 (N : Node_Id;
117 Typ : Entity_Id;
118 Lhs : Node_Id := Empty);
119 -- Given an expression N of a discriminated type, or of an access type
120 -- whose designated type is a discriminanted type, generates a check to
121 -- ensure that the expression can be converted to the subtype given as
122 -- the second parameter. Lhs is empty except in the case of assignments,
123 -- where the target object may be needed to determine the subtype to
124 -- check against (such as the cases of unconstrained formal parameters
125 -- and unconstrained aliased objects). For the case of unconstrained
126 -- formals, the check is peformed only if the corresponding actual is
127 -- constrained, i.e., whether Lhs'Constrained is True.
129 function Build_Discriminant_Checks
130 (N : Node_Id;
131 T_Typ : Entity_Id)
132 return Node_Id;
133 -- Subsidiary routine for Apply_Discriminant_Check. Builds the expression
134 -- that compares discriminants of the expression with discriminants of the
135 -- type. Also used directly for membership tests (see Exp_Ch4.Expand_N_In).
137 procedure Apply_Divide_Check (N : Node_Id);
138 -- The node kind is N_Op_Divide, N_Op_Mod, or N_Op_Rem. An appropriate
139 -- check is generated to ensure that the right operand is non-zero. In
140 -- the divide case, we also check that we do not have the annoying case
141 -- of the largest negative number divided by minus one.
143 procedure Apply_Type_Conversion_Checks (N : Node_Id);
144 -- N is an N_Type_Conversion node. A type conversion actually involves
145 -- two sorts of checks. The first check is the checks that ensures that
146 -- the operand in the type conversion fits onto the base type of the
147 -- subtype it is being converted to (see RM 4.6 (28)-(50)). The second
148 -- check is there to ensure that once the operand has been converted to
149 -- a value of the target type, this converted value meets the
150 -- constraints imposed by the target subtype (see RM 4.6 (51)).
152 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id);
153 -- The argument N is an attribute reference node intended for processing
154 -- by gigi. The attribute is one that returns a universal integer, but
155 -- the attribute reference node is currently typed with the expected
156 -- result type. This routine deals with range and overflow checks needed
157 -- to make sure that the universal result is in range.
159 procedure Determine_Range
160 (N : Node_Id;
161 OK : out Boolean;
162 Lo : out Uint;
163 Hi : out Uint);
164 -- N is a node for a subexpression. If N is of a discrete type with
165 -- no error indications, and no other peculiarities (e.g. missing
166 -- type fields), then OK is True on return, and Lo and Hi are set
167 -- to a conservative estimate of the possible range of values of N.
168 -- Thus if OK is True on return, the value of the subexpression N is
169 -- known to like in the range Lo .. Hi (inclusive). If the expression
170 -- is not of a discrete type, or some kind of error condition is
171 -- detected, then OK is False on exit, and Lo/Hi are set to No_Uint.
172 -- Thus the significance of OK being False on return is that no
173 -- useful information is available on the range of the expression.
175 -----------------------------
176 -- Length and Range Checks --
177 -----------------------------
179 -- In the following procedures, there are three arguments which have
180 -- a common meaning as follows:
182 -- Expr The expression to be checked. If a check is required,
183 -- the appropriate flag will be placed on this node. Whether
184 -- this node is further examined depends on the setting of
185 -- the parameter Source_Typ, as described below.
187 -- Target_Typ The target type on which the check is to be based. For
188 -- example, if we have a scalar range check, then the check
189 -- is that we are in range of this type.
191 -- Source_Typ Normally Empty, but can be set to a type, in which case
192 -- this type is used for the check, see below.
194 -- The checks operate in one of two modes:
196 -- If Source_Typ is Empty, then the node Expr is examined, at the
197 -- very least to get the source subtype. In addition for some of
198 -- the checks, the actual form of the node may be examined. For
199 -- example, a node of type Integer whose actual form is an Integer
200 -- conversion from a type with range 0 .. 3 can be determined to
201 -- have a value in the range 0 .. 3.
203 -- If Source_Typ is given, then nothing can be assumed about the
204 -- Expr, and indeed its contents are not examined. In this case the
205 -- check is based on the assumption that Expr can be an arbitrary
206 -- value of the given Source_Typ.
208 -- Currently, the only case in which a Source_Typ is explicitly supplied
209 -- is for the case of Out and In_Out parameters, where, for the conversion
210 -- on return (the Out direction), the types must be reversed. This is
211 -- handled by the caller.
213 procedure Apply_Length_Check
214 (Ck_Node : Node_Id;
215 Target_Typ : Entity_Id;
216 Source_Typ : Entity_Id := Empty);
217 -- This procedure builds a sequence of declarations to do a length check
218 -- that checks if the lengths of the two arrays Target_Typ and source type
219 -- are the same. The resulting actions are inserted at Node using a call
220 -- to Insert_Actions.
222 -- For access types, the Directly_Designated_Type is retrieved and
223 -- processing continues as enumerated above, with a guard against
224 -- null values.
226 -- Note: calls to Apply_Length_Check currently never supply an explicit
227 -- Source_Typ parameter, but Apply_Length_Check takes this parameter and
228 -- processes it as described above for consistency with the other routines
229 -- in this section.
231 procedure Apply_Range_Check
232 (Ck_Node : Node_Id;
233 Target_Typ : Entity_Id;
234 Source_Typ : Entity_Id := Empty);
235 -- For an Node of kind N_Range, constructs a range check action that
236 -- tests first that the range is not null and then that the range
237 -- is contained in the Target_Typ range.
239 -- For scalar types, constructs a range check action that first tests that
240 -- the expression is contained in the Target_Typ range. The difference
241 -- between this and Apply_Scalar_Range_Check is that the latter generates
242 -- the actual checking code in gigi against the Etype of the expression.
244 -- For constrained array types, construct series of range check actions
245 -- to check that each Expr range is properly contained in the range of
246 -- Target_Typ.
248 -- For a type conversion to an unconstrained array type, constructs
249 -- a range check action to check that the bounds of the source type
250 -- are within the constraints imposed by the Target_Typ.
252 -- For access types, the Directly_Designated_Type is retrieved and
253 -- processing continues as enumerated above, with a guard against
254 -- null values.
256 -- The source type is used by type conversions to unconstrained array
257 -- types to retrieve the corresponding bounds.
259 procedure Apply_Static_Length_Check
260 (Expr : Node_Id;
261 Target_Typ : Entity_Id;
262 Source_Typ : Entity_Id := Empty);
263 -- Tries to determine statically whether the two array types source type
264 -- and Target_Typ have the same length. If it can be determined at compile
265 -- time that they do not, then an N_Raise_Constraint_Error node replaces
266 -- Expr, and a warning message is issued.
268 procedure Apply_Scalar_Range_Check
269 (Expr : Node_Id;
270 Target_Typ : Entity_Id;
271 Source_Typ : Entity_Id := Empty;
272 Fixed_Int : Boolean := False);
273 -- For scalar types, determines whether an expression node should be
274 -- flagged as needing a runtime range check. If the node requires such
275 -- a check, the Do_Range_Check flag is turned on. The Fixed_Int flag
276 -- if set causes any fixed-point values to be treated as though they
277 -- were discrete values (i.e. the underlying integer value is used).
279 type Check_Result is private;
280 -- Type used to return result of Range_Check call, for later use in
281 -- call to Insert_Range_Checks procedure.
283 procedure Append_Range_Checks
284 (Checks : Check_Result;
285 Stmts : List_Id;
286 Suppress_Typ : Entity_Id;
287 Static_Sloc : Source_Ptr;
288 Flag_Node : Node_Id);
289 -- Called to append range checks as returned by a call to Range_Check.
290 -- Stmts is a list to which either the dynamic check is appended or
291 -- the raise Constraint_Error statement is appended (for static checks).
292 -- Static_Sloc is the Sloc at which the raise CE node points,
293 -- Flag_Node is used as the node at which to set the Has_Dynamic_Check
294 -- flag. Checks_On is a boolean value that says if range and index checking
295 -- is on or not.
297 procedure Enable_Range_Check (N : Node_Id);
298 pragma Inline (Enable_Range_Check);
299 -- Set Do_Range_Check flag in node N to True unless Kill_Range_Check flag
300 -- is set in N (the purpose of the latter flag is precisely to prevent
301 -- Do_Range_Check from being set).
303 procedure Insert_Range_Checks
304 (Checks : Check_Result;
305 Node : Node_Id;
306 Suppress_Typ : Entity_Id;
307 Static_Sloc : Source_Ptr := No_Location;
308 Flag_Node : Node_Id := Empty;
309 Do_Before : Boolean := False);
310 -- Called to insert range checks as returned by a call to Range_Check.
311 -- Node is the node after which either the dynamic check is inserted or
312 -- the raise Constraint_Error statement is inserted (for static checks).
313 -- Suppress_Typ is the type to check to determine if checks are suppressed.
314 -- Static_Sloc, if passed, is the Sloc at which the raise CE node points,
315 -- otherwise Sloc (Node) is used. The Has_Dynamic_Check flag is normally
316 -- set at Node. If Flag_Node is present, then this is used instead as the
317 -- node at which to set the Has_Dynamic_Check flag. Normally the check is
318 -- inserted after, if Do_Before is True, the check is inserted before
319 -- Node.
321 function Range_Check
322 (Ck_Node : Node_Id;
323 Target_Typ : Entity_Id;
324 Source_Typ : Entity_Id := Empty;
325 Warn_Node : Node_Id := Empty)
326 return Check_Result;
327 -- Like Apply_Range_Check, except it does not modify anything. Instead
328 -- it returns an encapsulated result of the check operations for later
329 -- use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its
330 -- Sloc is used, in the static case, for the generated warning or error.
331 -- Additionally, it is used rather than Expr (or Low/High_Bound of Expr)
332 -- in constructing the check.
334 -----------------------
335 -- Validity Checking --
336 -----------------------
338 -- In (RM 13.9.1(9-11)) we have the following rules on invalid values
340 -- 9 If the representation of a scalar object does not represent a
341 -- value of the object's subtype (perhaps because the object was not
342 -- initialized), the object is said to have an invalid representation.
343 -- It is a bounded error to evaluate the value of such an object. If
344 -- the error is detected, either Constraint_Error or Program_Error is
345 -- raised. Otherwise, execution continues using the invalid
346 -- representation. The rules of the language outside this subclause
347 -- assume that all objects have valid representations. The semantics
348 -- of operations on invalid representations are as follows:
350 -- 10 If the representation of the object represents a value of the
351 -- object's type, the value of the type is used.
353 -- 11 If the representation of the object does not represent a value
354 -- of the object's type, the semantics of operations on such
355 -- representations is implementation-defined, but does not by
356 -- itself lead to erroneous or unpredictable execution, or to
357 -- other objects becoming abnormal.
359 -- We quote the rules in full here since they are quite delicate. Most
360 -- of the time, we can just compute away with wrong values, and get a
361 -- possibly wrong result, which is well within the range of allowed
362 -- implementation defined behavior. The two tricky cases are subscripted
363 -- array assignments, where we don't want to do wild stores, and case
364 -- statements where we don't want to do wild jumps.
366 -- In GNAT, we control validity checking with a switch -gnatV that
367 -- can take three parameters, n/d/f for None/Default/Full. These
368 -- modes have the following meanings:
370 -- None (no validity checking)
372 -- In this mode, there is no specific checking for invalid values
373 -- and the code generator assumes that all stored values are always
374 -- within the bounds of the object subtype. The consequences are as
375 -- follows:
377 -- For case statements, an out of range invalid value will cause
378 -- Constraint_Error to be raised, or an arbitrary one of the case
379 -- alternatives will be executed. Wild jumps cannot result even
380 -- in this mode, since we always do a range check
382 -- For subscripted array assignments, wild stores will result in
383 -- the expected manner when addresses are calculated using values
384 -- of subscripts that are out of range.
386 -- It could perhaps be argued that this mode is still conformant with
387 -- the letter of the RM, since implementation defined is a rather
388 -- broad category, but certainly it is not in the spirit of the
389 -- RM requirement, since wild stores certainly seem to be a case of
390 -- erroneous behavior.
392 -- Default (default standard RM-compatible validity checking)
394 -- In this mode, which is the default, minimal validity checking is
395 -- performed to ensure no erroneous behavior as follows:
397 -- For case statements, an out of range invalid value will cause
398 -- Constraint_Error to be raised.
400 -- For subscripted array assignments, invalid out of range
401 -- subscript values will cause Constraint_Error to be raised.
403 -- Full (Full validity checking)
405 -- In this mode, the protections guaranteed by the standard mode are
406 -- in place, and the following additional checks are made:
408 -- For every assignment, the right side is checked for validity
410 -- For every call, IN and IN OUT parameters are checked for validity
412 -- For every subscripted array reference, both for stores and loads,
413 -- all subscripts are checked for validity.
415 -- These checks are not required by the RM, but will in practice
416 -- improve the detection of uninitialized variables, particularly
417 -- if used in conjunction with pragma Normalize_Scalars.
419 -- In the above description, we talk about performing validity checks,
420 -- but we don't actually generate a check in a case where the compiler
421 -- can be sure that the value is valid. Note that this assurance must
422 -- be achieved without assuming that any uninitialized value lies within
423 -- the range of its type. The following are cases in which values are
424 -- known to be valid. The flag Is_Known_Valid is used to keep track of
425 -- some of these cases.
427 -- If all possible stored values are valid, then any uninitialized
428 -- value must be valid.
430 -- Literals, including enumeration literals, are clearly always valid.
432 -- Constants are always assumed valid, with a validity check being
433 -- performed on the initializing value where necessary to ensure that
434 -- this is the case.
436 -- For variables, the status is set to known valid if there is an
437 -- initializing expression. Again a check is made on the initializing
438 -- value if necessary to ensure that this assumption is valid. The
439 -- status can change as a result of local assignments to a variable.
440 -- If a known valid value is unconditionally assigned, then we mark
441 -- the left side as known valid. If a value is assigned that is not
442 -- known to be valid, then we mark the left side as invalid. This
443 -- kind of processing does NOT apply to non-local variables since we
444 -- are not following the flow graph (more properly the flow of actual
445 -- processing only corresponds to the flow graph for local assignments).
446 -- For non-local variables, we preserve the current setting, i.e. a
447 -- validity check is performed when assigning to a knonwn valid global.
449 -- Note: no validity checking is required if range checks are suppressed
450 -- regardless of the setting of the validity checking mode.
452 -- The following procedures are used in handling validity checking
454 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id);
455 -- Expr is the node for an indexed component. If validity checking and
456 -- range checking are enabled, all subscripts for this indexed component
457 -- are checked for validity.
459 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id);
460 -- Expr is a lvalue, i.e. an expression representing the target of
461 -- an assignment. This procedure checks for this expression involving
462 -- an assignment to an array value. We have to be sure that all the
463 -- subscripts in such a case are valid, since according to the rules
464 -- in (RM 13.9.1(9-11)) such assignments are not permitted to result
465 -- in erroneous behavior in the case of invalid subscript values.
467 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False);
468 -- Ensure that Expr represents a valid value of its type. If this type
469 -- is not a scalar type, then the call has no effect, since validity
470 -- is only an issue for scalar types. The effect of this call is to
471 -- check if the value is known valid, if so, nothing needs to be done.
472 -- If this is not known, then either Expr is set to be range checked,
473 -- or specific checking code is inserted so that an exception is raised
474 -- if the value is not valid.
476 -- The optional argument Holes_OK indicates whether it is necessary to
477 -- worry about enumeration types with non-standard representations leading
478 -- to "holes" in the range of possible representations. If Holes_OK is
479 -- True, then such values are assumed valid (this is used when the caller
480 -- will make a separate check for this case anyway). If Holes_OK is False,
481 -- then this case is checked, and code is inserted to ensure that Expr is
482 -- valid, raising Constraint_Error if the value is not valid.
484 function Expr_Known_Valid (Expr : Node_Id) return Boolean;
485 -- This function tests it the value of Expr is known to be valid in
486 -- the sense of RM 13.9.1(9-11). In the case of GNAT, it is only
487 -- discrete types which are a concern, since for non-discrete types
488 -- we simply continue computation with invalid values, which does
489 -- not lead to erroneous behavior. Thus Expr_Known_Valid always
490 -- returns True if the type of Expr is non-discrete. For discrete
491 -- types the value returned is True only if it can be determined
492 -- that the value is Valid. Otherwise False is returned.
494 procedure Insert_Valid_Check (Expr : Node_Id);
495 -- Inserts code that will check for the value of Expr being valid, in
496 -- the sense of the 'Valid attribute returning True. Constraint_Error
497 -- will be raised if the value is not valid.
499 private
501 type Check_Result is array (Positive range 1 .. 2) of Node_Id;
502 -- There are two cases for the result returned by Range_Check:
504 -- For the static case the result is one or two nodes that should cause
505 -- a Constraint_Error. Typically these will include Expr itself or the
506 -- direct descendents of Expr, such as Low/High_Bound (Expr)). It is the
507 -- responsibility of the caller to rewrite and substitute the nodes with
508 -- N_Raise_Constraint_Error nodes.
510 -- For the non-static case a single N_Raise_Constraint_Error node
511 -- with a non-empty Condition field is returned.
513 -- Unused entries in Check_Result, if any, are simply set to Empty
514 -- For external clients, the required processing on this result is
515 -- achieved using the Insert_Range_Checks routine.
517 pragma Inline (Access_Checks_Suppressed);
518 pragma Inline (Accessibility_Checks_Suppressed);
519 pragma Inline (Discriminant_Checks_Suppressed);
520 pragma Inline (Division_Checks_Suppressed);
521 pragma Inline (Elaboration_Checks_Suppressed);
522 pragma Inline (Index_Checks_Suppressed);
523 pragma Inline (Length_Checks_Suppressed);
524 pragma Inline (Overflow_Checks_Suppressed);
525 pragma Inline (Range_Checks_Suppressed);
526 pragma Inline (Storage_Checks_Suppressed);
527 pragma Inline (Tag_Checks_Suppressed);
529 pragma Inline (Apply_Length_Check);
530 pragma Inline (Apply_Range_Check);
531 pragma Inline (Apply_Static_Length_Check);
532 end Checks;