PR c++/11509
[official-gcc.git] / gcc / ada / checks.ads
blob8ccafdea7c008e6d28d0045bb094052512f572a1
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-2001 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
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.
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.
39 with Types; use Types;
40 with Uintp; use Uintp;
42 package Checks is
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.
48 function Access_Checks_Suppressed (E : Entity_Id) return Boolean;
49 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean;
50 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean;
51 function Division_Checks_Suppressed (E : Entity_Id) return Boolean;
52 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean;
53 function Index_Checks_Suppressed (E : Entity_Id) return Boolean;
54 function Length_Checks_Suppressed (E : Entity_Id) return Boolean;
55 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean;
56 function Range_Checks_Suppressed (E : Entity_Id) return Boolean;
57 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean;
58 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean;
59 -- These functions check to see if the named check is suppressed,
60 -- either by an active scope suppress setting, or because the check
61 -- has been specifically suppressed for the given entity. If no entity
62 -- is relevant for the current check, then Empty is used as an argument.
63 -- Note: the reason we insist on specifying Empty is to force the
64 -- caller to think about whether there is any relevant entity that
65 -- should be checked.
67 -- General note on following checks. These checks are always active if
68 -- Expander_Active and not Inside_A_Generic. They are inactive and have
69 -- no effect Inside_A_Generic. In the case where not Expander_Active
70 -- and not Inside_A_Generic, most of them are inactive, but some of them
71 -- operate anyway since they may generate useful compile time warnings.
73 procedure Apply_Access_Check (N : Node_Id);
74 -- Determines whether an expression node should be flagged as needing
75 -- a runtime access check. If the node requires such a check, the
76 -- Do_Access_Check flag is turned on.
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.
84 procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id);
85 -- E is the entity for an object. If there is an address clause for
86 -- this entity, and checks are enabled, then this procedure generates
87 -- a check that the specified address has an alignment consistent with
88 -- the alignment of the object, raising PE if this is not the case. The
89 -- resulting check (if one is generated) is inserted before node N.
91 procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id);
92 -- N is the node for an object declaration that declares an object of
93 -- array type Typ. This routine generates, if necessary, a check that
94 -- the size of the array is not too large, raising Storage_Error if so.
96 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id);
97 -- Given a binary arithmetic operator (+ - *) expand a software integer
98 -- overflow check using range checks on a larger checking type or a call
99 -- to an appropriate runtime routine. This is used for all three operators
100 -- for the signed integer case, and for +/- in the fixed-point case. The
101 -- check is expanded only if Software_Overflow_Checking is enabled and
102 -- Do_Overflow_Check is set on node N. Note that divide is handled
103 -- separately using Apply_Arithmetic_Divide_Overflow_Check.
105 procedure Apply_Constraint_Check
106 (N : Node_Id;
107 Typ : Entity_Id;
108 No_Sliding : Boolean := False);
109 -- Top-level procedure, calls all the others depending on the class of Typ.
110 -- Checks that expression N verifies the constraint of type Typ. No_Sliding
111 -- is only relevant for constrained array types, id set to true, it
112 -- checks that indexes are in range.
114 procedure Apply_Discriminant_Check
115 (N : Node_Id;
116 Typ : Entity_Id;
117 Lhs : Node_Id := Empty);
118 -- Given an expression N of a discriminated type, or of an access type
119 -- whose designated type is a discriminanted type, generates a check to
120 -- ensure that the expression can be converted to the subtype given as
121 -- the second parameter. Lhs is empty except in the case of assignments,
122 -- where the target object may be needed to determine the subtype to
123 -- check against (such as the cases of unconstrained formal parameters
124 -- and unconstrained aliased objects). For the case of unconstrained
125 -- formals, the check is peformed only if the corresponding actual is
126 -- constrained, i.e., whether Lhs'Constrained is True.
128 function Build_Discriminant_Checks
129 (N : Node_Id;
130 T_Typ : Entity_Id)
131 return Node_Id;
132 -- Subsidiary routine for Apply_Discriminant_Check. Builds the expression
133 -- that compares discriminants of the expression with discriminants of the
134 -- type. Also used directly for membership tests (see Exp_Ch4.Expand_N_In).
136 procedure Apply_Divide_Check (N : Node_Id);
137 -- The node kind is N_Op_Divide, N_Op_Mod, or N_Op_Rem. An appropriate
138 -- check is generated to ensure that the right operand is non-zero. In
139 -- the divide case, we also check that we do not have the annoying case
140 -- of the largest negative number divided by minus one.
142 procedure Apply_Type_Conversion_Checks (N : Node_Id);
143 -- N is an N_Type_Conversion node. A type conversion actually involves
144 -- two sorts of checks. The first check is the checks that ensures that
145 -- the operand in the type conversion fits onto the base type of the
146 -- subtype it is being converted to (see RM 4.6 (28)-(50)). The second
147 -- check is there to ensure that once the operand has been converted to
148 -- a value of the target type, this converted value meets the
149 -- constraints imposed by the target subtype (see RM 4.6 (51)).
151 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id);
152 -- The argument N is an attribute reference node intended for processing
153 -- by gigi. The attribute is one that returns a universal integer, but
154 -- the attribute reference node is currently typed with the expected
155 -- result type. This routine deals with range and overflow checks needed
156 -- to make sure that the universal result is in range.
158 procedure Determine_Range
159 (N : Node_Id;
160 OK : out Boolean;
161 Lo : out Uint;
162 Hi : out Uint);
163 -- N is a node for a subexpression. If N is of a discrete type with
164 -- no error indications, and no other peculiarities (e.g. missing
165 -- type fields), then OK is True on return, and Lo and Hi are set
166 -- to a conservative estimate of the possible range of values of N.
167 -- Thus if OK is True on return, the value of the subexpression N is
168 -- known to like in the range Lo .. Hi (inclusive). If the expression
169 -- is not of a discrete type, or some kind of error condition is
170 -- detected, then OK is False on exit, and Lo/Hi are set to No_Uint.
171 -- Thus the significance of OK being False on return is that no
172 -- useful information is available on the range of the expression.
174 -----------------------------
175 -- Length and Range Checks --
176 -----------------------------
178 -- In the following procedures, there are three arguments which have
179 -- a common meaning as follows:
181 -- Expr The expression to be checked. If a check is required,
182 -- the appropriate flag will be placed on this node. Whether
183 -- this node is further examined depends on the setting of
184 -- the parameter Source_Typ, as described below.
186 -- Target_Typ The target type on which the check is to be based. For
187 -- example, if we have a scalar range check, then the check
188 -- is that we are in range of this type.
190 -- Source_Typ Normally Empty, but can be set to a type, in which case
191 -- this type is used for the check, see below.
193 -- The checks operate in one of two modes:
195 -- If Source_Typ is Empty, then the node Expr is examined, at the
196 -- very least to get the source subtype. In addition for some of
197 -- the checks, the actual form of the node may be examined. For
198 -- example, a node of type Integer whose actual form is an Integer
199 -- conversion from a type with range 0 .. 3 can be determined to
200 -- have a value in the range 0 .. 3.
202 -- If Source_Typ is given, then nothing can be assumed about the
203 -- Expr, and indeed its contents are not examined. In this case the
204 -- check is based on the assumption that Expr can be an arbitrary
205 -- value of the given Source_Typ.
207 -- Currently, the only case in which a Source_Typ is explicitly supplied
208 -- is for the case of Out and In_Out parameters, where, for the conversion
209 -- on return (the Out direction), the types must be reversed. This is
210 -- handled by the caller.
212 procedure Apply_Length_Check
213 (Ck_Node : Node_Id;
214 Target_Typ : Entity_Id;
215 Source_Typ : Entity_Id := Empty);
216 -- This procedure builds a sequence of declarations to do a length check
217 -- that checks if the lengths of the two arrays Target_Typ and source type
218 -- are the same. The resulting actions are inserted at Node using a call
219 -- to Insert_Actions.
221 -- For access types, the Directly_Designated_Type is retrieved and
222 -- processing continues as enumerated above, with a guard against
223 -- null values.
225 -- Note: calls to Apply_Length_Check currently never supply an explicit
226 -- Source_Typ parameter, but Apply_Length_Check takes this parameter and
227 -- processes it as described above for consistency with the other routines
228 -- in this section.
230 procedure Apply_Range_Check
231 (Ck_Node : Node_Id;
232 Target_Typ : Entity_Id;
233 Source_Typ : Entity_Id := Empty);
234 -- For an Node of kind N_Range, constructs a range check action that
235 -- tests first that the range is not null and then that the range
236 -- is contained in the Target_Typ range.
238 -- For scalar types, constructs a range check action that first tests that
239 -- the expression is contained in the Target_Typ range. The difference
240 -- between this and Apply_Scalar_Range_Check is that the latter generates
241 -- the actual checking code in gigi against the Etype of the expression.
243 -- For constrained array types, construct series of range check actions
244 -- to check that each Expr range is properly contained in the range of
245 -- Target_Typ.
247 -- For a type conversion to an unconstrained array type, constructs
248 -- a range check action to check that the bounds of the source type
249 -- are within the constraints imposed by the Target_Typ.
251 -- For access types, the Directly_Designated_Type is retrieved and
252 -- processing continues as enumerated above, with a guard against
253 -- null values.
255 -- The source type is used by type conversions to unconstrained array
256 -- types to retrieve the corresponding bounds.
258 procedure Apply_Static_Length_Check
259 (Expr : Node_Id;
260 Target_Typ : Entity_Id;
261 Source_Typ : Entity_Id := Empty);
262 -- Tries to determine statically whether the two array types source type
263 -- and Target_Typ have the same length. If it can be determined at compile
264 -- time that they do not, then an N_Raise_Constraint_Error node replaces
265 -- Expr, and a warning message is issued.
267 procedure Apply_Scalar_Range_Check
268 (Expr : Node_Id;
269 Target_Typ : Entity_Id;
270 Source_Typ : Entity_Id := Empty;
271 Fixed_Int : Boolean := False);
272 -- For scalar types, determines whether an expression node should be
273 -- flagged as needing a runtime range check. If the node requires such
274 -- a check, the Do_Range_Check flag is turned on. The Fixed_Int flag
275 -- if set causes any fixed-point values to be treated as though they
276 -- were discrete values (i.e. the underlying integer value is used).
278 type Check_Result is private;
279 -- Type used to return result of Range_Check call, for later use in
280 -- call to Insert_Range_Checks procedure.
282 procedure Append_Range_Checks
283 (Checks : Check_Result;
284 Stmts : List_Id;
285 Suppress_Typ : Entity_Id;
286 Static_Sloc : Source_Ptr;
287 Flag_Node : Node_Id);
288 -- Called to append range checks as returned by a call to Range_Check.
289 -- Stmts is a list to which either the dynamic check is appended or
290 -- the raise Constraint_Error statement is appended (for static checks).
291 -- Static_Sloc is the Sloc at which the raise CE node points,
292 -- Flag_Node is used as the node at which to set the Has_Dynamic_Check
293 -- flag. Checks_On is a boolean value that says if range and index checking
294 -- is on or not.
296 procedure Enable_Range_Check (N : Node_Id);
297 pragma Inline (Enable_Range_Check);
298 -- Set Do_Range_Check flag in node N to True unless Kill_Range_Check flag
299 -- is set in N (the purpose of the latter flag is precisely to prevent
300 -- Do_Range_Check from being set).
302 procedure Insert_Range_Checks
303 (Checks : Check_Result;
304 Node : Node_Id;
305 Suppress_Typ : Entity_Id;
306 Static_Sloc : Source_Ptr := No_Location;
307 Flag_Node : Node_Id := Empty;
308 Do_Before : Boolean := False);
309 -- Called to insert range checks as returned by a call to Range_Check.
310 -- Node is the node after which either the dynamic check is inserted or
311 -- the raise Constraint_Error statement is inserted (for static checks).
312 -- Suppress_Typ is the type to check to determine if checks are suppressed.
313 -- Static_Sloc, if passed, is the Sloc at which the raise CE node points,
314 -- otherwise Sloc (Node) is used. The Has_Dynamic_Check flag is normally
315 -- set at Node. If Flag_Node is present, then this is used instead as the
316 -- node at which to set the Has_Dynamic_Check flag. Normally the check is
317 -- inserted after, if Do_Before is True, the check is inserted before
318 -- Node.
320 function Range_Check
321 (Ck_Node : Node_Id;
322 Target_Typ : Entity_Id;
323 Source_Typ : Entity_Id := Empty;
324 Warn_Node : Node_Id := Empty)
325 return Check_Result;
326 -- Like Apply_Range_Check, except it does not modify anything. Instead
327 -- it returns an encapsulated result of the check operations for later
328 -- use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its
329 -- Sloc is used, in the static case, for the generated warning or error.
330 -- Additionally, it is used rather than Expr (or Low/High_Bound of Expr)
331 -- in constructing the check.
333 -----------------------
334 -- Validity Checking --
335 -----------------------
337 -- In (RM 13.9.1(9-11)) we have the following rules on invalid values
339 -- 9 If the representation of a scalar object does not represent a
340 -- value of the object's subtype (perhaps because the object was not
341 -- initialized), the object is said to have an invalid representation.
342 -- It is a bounded error to evaluate the value of such an object. If
343 -- the error is detected, either Constraint_Error or Program_Error is
344 -- raised. Otherwise, execution continues using the invalid
345 -- representation. The rules of the language outside this subclause
346 -- assume that all objects have valid representations. The semantics
347 -- of operations on invalid representations are as follows:
349 -- 10 If the representation of the object represents a value of the
350 -- object's type, the value of the type is used.
352 -- 11 If the representation of the object does not represent a value
353 -- of the object's type, the semantics of operations on such
354 -- representations is implementation-defined, but does not by
355 -- itself lead to erroneous or unpredictable execution, or to
356 -- other objects becoming abnormal.
358 -- We quote the rules in full here since they are quite delicate. Most
359 -- of the time, we can just compute away with wrong values, and get a
360 -- possibly wrong result, which is well within the range of allowed
361 -- implementation defined behavior. The two tricky cases are subscripted
362 -- array assignments, where we don't want to do wild stores, and case
363 -- statements where we don't want to do wild jumps.
365 -- In GNAT, we control validity checking with a switch -gnatV that
366 -- can take three parameters, n/d/f for None/Default/Full. These
367 -- modes have the following meanings:
369 -- None (no validity checking)
371 -- In this mode, there is no specific checking for invalid values
372 -- and the code generator assumes that all stored values are always
373 -- within the bounds of the object subtype. The consequences are as
374 -- follows:
376 -- For case statements, an out of range invalid value will cause
377 -- Constraint_Error to be raised, or an arbitrary one of the case
378 -- alternatives will be executed. Wild jumps cannot result even
379 -- in this mode, since we always do a range check
381 -- For subscripted array assignments, wild stores will result in
382 -- the expected manner when addresses are calculated using values
383 -- of subscripts that are out of range.
385 -- It could perhaps be argued that this mode is still conformant with
386 -- the letter of the RM, since implementation defined is a rather
387 -- broad category, but certainly it is not in the spirit of the
388 -- RM requirement, since wild stores certainly seem to be a case of
389 -- erroneous behavior.
391 -- Default (default standard RM-compatible validity checking)
393 -- In this mode, which is the default, minimal validity checking is
394 -- performed to ensure no erroneous behavior as follows:
396 -- For case statements, an out of range invalid value will cause
397 -- Constraint_Error to be raised.
399 -- For subscripted array assignments, invalid out of range
400 -- subscript values will cause Constraint_Error to be raised.
402 -- Full (Full validity checking)
404 -- In this mode, the protections guaranteed by the standard mode are
405 -- in place, and the following additional checks are made:
407 -- For every assignment, the right side is checked for validity
409 -- For every call, IN and IN OUT parameters are checked for validity
411 -- For every subscripted array reference, both for stores and loads,
412 -- all subscripts are checked for validity.
414 -- These checks are not required by the RM, but will in practice
415 -- improve the detection of uninitialized variables, particularly
416 -- if used in conjunction with pragma Normalize_Scalars.
418 -- In the above description, we talk about performing validity checks,
419 -- but we don't actually generate a check in a case where the compiler
420 -- can be sure that the value is valid. Note that this assurance must
421 -- be achieved without assuming that any uninitialized value lies within
422 -- the range of its type. The following are cases in which values are
423 -- known to be valid. The flag Is_Known_Valid is used to keep track of
424 -- some of these cases.
426 -- If all possible stored values are valid, then any uninitialized
427 -- value must be valid.
429 -- Literals, including enumeration literals, are clearly always valid.
431 -- Constants are always assumed valid, with a validity check being
432 -- performed on the initializing value where necessary to ensure that
433 -- this is the case.
435 -- For variables, the status is set to known valid if there is an
436 -- initializing expression. Again a check is made on the initializing
437 -- value if necessary to ensure that this assumption is valid. The
438 -- status can change as a result of local assignments to a variable.
439 -- If a known valid value is unconditionally assigned, then we mark
440 -- the left side as known valid. If a value is assigned that is not
441 -- known to be valid, then we mark the left side as invalid. This
442 -- kind of processing does NOT apply to non-local variables since we
443 -- are not following the flow graph (more properly the flow of actual
444 -- processing only corresponds to the flow graph for local assignments).
445 -- For non-local variables, we preserve the current setting, i.e. a
446 -- validity check is performed when assigning to a knonwn valid global.
448 -- Note: no validity checking is required if range checks are suppressed
449 -- regardless of the setting of the validity checking mode.
451 -- The following procedures are used in handling validity checking
453 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id);
454 -- Expr is the node for an indexed component. If validity checking and
455 -- range checking are enabled, all subscripts for this indexed component
456 -- are checked for validity.
458 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id);
459 -- Expr is a lvalue, i.e. an expression representing the target of
460 -- an assignment. This procedure checks for this expression involving
461 -- an assignment to an array value. We have to be sure that all the
462 -- subscripts in such a case are valid, since according to the rules
463 -- in (RM 13.9.1(9-11)) such assignments are not permitted to result
464 -- in erroneous behavior in the case of invalid subscript values.
466 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False);
467 -- Ensure that Expr represents a valid value of its type. If this type
468 -- is not a scalar type, then the call has no effect, since validity
469 -- is only an issue for scalar types. The effect of this call is to
470 -- check if the value is known valid, if so, nothing needs to be done.
471 -- If this is not known, then either Expr is set to be range checked,
472 -- or specific checking code is inserted so that an exception is raised
473 -- if the value is not valid.
475 -- The optional argument Holes_OK indicates whether it is necessary to
476 -- worry about enumeration types with non-standard representations leading
477 -- to "holes" in the range of possible representations. If Holes_OK is
478 -- True, then such values are assumed valid (this is used when the caller
479 -- will make a separate check for this case anyway). If Holes_OK is False,
480 -- then this case is checked, and code is inserted to ensure that Expr is
481 -- valid, raising Constraint_Error if the value is not valid.
483 function Expr_Known_Valid (Expr : Node_Id) return Boolean;
484 -- This function tests it the value of Expr is known to be valid in
485 -- the sense of RM 13.9.1(9-11). In the case of GNAT, it is only
486 -- discrete types which are a concern, since for non-discrete types
487 -- we simply continue computation with invalid values, which does
488 -- not lead to erroneous behavior. Thus Expr_Known_Valid always
489 -- returns True if the type of Expr is non-discrete. For discrete
490 -- types the value returned is True only if it can be determined
491 -- that the value is Valid. Otherwise False is returned.
493 procedure Insert_Valid_Check (Expr : Node_Id);
494 -- Inserts code that will check for the value of Expr being valid, in
495 -- the sense of the 'Valid attribute returning True. Constraint_Error
496 -- will be raised if the value is not valid.
498 procedure Remove_Checks (Expr : Node_Id);
499 -- Remove all checks from Expr except those that are only executed
500 -- conditionally (on the right side of And Then/Or Else. This call
501 -- removes only embedded checks (Do_Range_Check, Do_Overflow_Check).
503 private
505 type Check_Result is array (Positive range 1 .. 2) of Node_Id;
506 -- There are two cases for the result returned by Range_Check:
508 -- For the static case the result is one or two nodes that should cause
509 -- a Constraint_Error. Typically these will include Expr itself or the
510 -- direct descendents of Expr, such as Low/High_Bound (Expr)). It is the
511 -- responsibility of the caller to rewrite and substitute the nodes with
512 -- N_Raise_Constraint_Error nodes.
514 -- For the non-static case a single N_Raise_Constraint_Error node
515 -- with a non-empty Condition field is returned.
517 -- Unused entries in Check_Result, if any, are simply set to Empty
518 -- For external clients, the required processing on this result is
519 -- achieved using the Insert_Range_Checks routine.
521 pragma Inline (Access_Checks_Suppressed);
522 pragma Inline (Accessibility_Checks_Suppressed);
523 pragma Inline (Discriminant_Checks_Suppressed);
524 pragma Inline (Division_Checks_Suppressed);
525 pragma Inline (Elaboration_Checks_Suppressed);
526 pragma Inline (Index_Checks_Suppressed);
527 pragma Inline (Length_Checks_Suppressed);
528 pragma Inline (Overflow_Checks_Suppressed);
529 pragma Inline (Range_Checks_Suppressed);
530 pragma Inline (Storage_Checks_Suppressed);
531 pragma Inline (Tag_Checks_Suppressed);
533 pragma Inline (Apply_Length_Check);
534 pragma Inline (Apply_Range_Check);
535 pragma Inline (Apply_Static_Length_Check);
536 end Checks;