1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- Package containing routines used to deal with runtime checks. These
30 -- routines are used both by the semantics and by the expander. In some
31 -- cases, checks are enabled simply by setting flags for gigi, and in
32 -- other cases the code for the check is expanded.
34 -- The approach used for range and length checks, in regards to suppressed
35 -- checks, is to attempt to detect at compilation time that a constraint
36 -- error will occur. If this is detected a warning or error is issued and the
37 -- offending expression or statement replaced with a constraint error node.
38 -- This always occurs whether checks are suppressed or not. Dynamic range
39 -- checks are, of course, not inserted if checks are suppressed.
41 with Types; use Types;
42 with Uintp; use Uintp;
47 -- Called for each new main source program, to initialize internal
48 -- variables used in the package body of the Checks unit.
50 function Access_Checks_Suppressed (E : Entity_Id) return Boolean;
51 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean;
52 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean;
53 function Division_Checks_Suppressed (E : Entity_Id) return Boolean;
54 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean;
55 function Index_Checks_Suppressed (E : Entity_Id) return Boolean;
56 function Length_Checks_Suppressed (E : Entity_Id) return Boolean;
57 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean;
58 function Range_Checks_Suppressed (E : Entity_Id) return Boolean;
59 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean;
60 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean;
61 -- These functions check to see if the named check is suppressed,
62 -- either by an active scope suppress setting, or because the check
63 -- has been specifically suppressed for the given entity. If no entity
64 -- is relevant for the current check, then Empty is used as an argument.
65 -- Note: the reason we insist on specifying Empty is to force the
66 -- caller to think about whether there is any relevant entity that
69 -- General note on following checks. These checks are always active if
70 -- Expander_Active and not Inside_A_Generic. They are inactive and have
71 -- no effect Inside_A_Generic. In the case where not Expander_Active
72 -- and not Inside_A_Generic, most of them are inactive, but some of them
73 -- operate anyway since they may generate useful compile time warnings.
75 procedure Apply_Access_Check (N : Node_Id);
76 -- Determines whether an expression node should be flagged as needing
77 -- a runtime access check. If the node requires such a check, the
78 -- Do_Access_Check flag is turned on.
80 procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id);
81 -- Given a name N denoting an access parameter, emits a run-time
82 -- accessibility check (if necessary), checking that the level of
83 -- the object denoted by the access parameter is not deeper than the
84 -- level of the type Typ. Program_Error is raised if the check fails.
86 procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id);
87 -- E is the entity for an object. If there is an address clause for
88 -- this entity, and checks are enabled, then this procedure generates
89 -- a check that the specified address has an alignment consistent with
90 -- the alignment of the object, raising PE if this is not the case. The
91 -- resulting check (if one is generated) is inserted before node N.
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.
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.
107 procedure Apply_Constraint_Check
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.
116 procedure Apply_Discriminant_Check
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.
130 function Build_Discriminant_Checks
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).
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.
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)).
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.
160 procedure Determine_Range
165 -- N is a node for a subexpression. If N is of a discrete type with
166 -- no error indications, and no other peculiarities (e.g. missing
167 -- type fields), then OK is True on return, and Lo and Hi are set
168 -- to a conservative estimate of the possible range of values of N.
169 -- Thus if OK is True on return, the value of the subexpression N is
170 -- known to like in the range Lo .. Hi (inclusive). If the expression
171 -- is not of a discrete type, or some kind of error condition is
172 -- detected, then OK is False on exit, and Lo/Hi are set to No_Uint.
173 -- Thus the significance of OK being False on return is that no
174 -- useful information is available on the range of the expression.
176 -----------------------------
177 -- Length and Range Checks --
178 -----------------------------
180 -- In the following procedures, there are three arguments which have
181 -- a common meaning as follows:
183 -- Expr The expression to be checked. If a check is required,
184 -- the appropriate flag will be placed on this node. Whether
185 -- this node is further examined depends on the setting of
186 -- the parameter Source_Typ, as described below.
188 -- Target_Typ The target type on which the check is to be based. For
189 -- example, if we have a scalar range check, then the check
190 -- is that we are in range of this type.
192 -- Source_Typ Normally Empty, but can be set to a type, in which case
193 -- this type is used for the check, see below.
195 -- The checks operate in one of two modes:
197 -- If Source_Typ is Empty, then the node Expr is examined, at the
198 -- very least to get the source subtype. In addition for some of
199 -- the checks, the actual form of the node may be examined. For
200 -- example, a node of type Integer whose actual form is an Integer
201 -- conversion from a type with range 0 .. 3 can be determined to
202 -- have a value in the range 0 .. 3.
204 -- If Source_Typ is given, then nothing can be assumed about the
205 -- Expr, and indeed its contents are not examined. In this case the
206 -- check is based on the assumption that Expr can be an arbitrary
207 -- value of the given Source_Typ.
209 -- Currently, the only case in which a Source_Typ is explicitly supplied
210 -- is for the case of Out and In_Out parameters, where, for the conversion
211 -- on return (the Out direction), the types must be reversed. This is
212 -- handled by the caller.
214 procedure Apply_Length_Check
216 Target_Typ : Entity_Id;
217 Source_Typ : Entity_Id := Empty);
218 -- This procedure builds a sequence of declarations to do a length check
219 -- that checks if the lengths of the two arrays Target_Typ and source type
220 -- are the same. The resulting actions are inserted at Node using a call
221 -- to Insert_Actions.
223 -- For access types, the Directly_Designated_Type is retrieved and
224 -- processing continues as enumerated above, with a guard against
227 -- Note: calls to Apply_Length_Check currently never supply an explicit
228 -- Source_Typ parameter, but Apply_Length_Check takes this parameter and
229 -- processes it as described above for consistency with the other routines
232 procedure Apply_Range_Check
234 Target_Typ : Entity_Id;
235 Source_Typ : Entity_Id := Empty);
236 -- For an Node of kind N_Range, constructs a range check action that
237 -- tests first that the range is not null and then that the range
238 -- is contained in the Target_Typ range.
240 -- For scalar types, constructs a range check action that first tests that
241 -- the expression is contained in the Target_Typ range. The difference
242 -- between this and Apply_Scalar_Range_Check is that the latter generates
243 -- the actual checking code in gigi against the Etype of the expression.
245 -- For constrained array types, construct series of range check actions
246 -- to check that each Expr range is properly contained in the range of
249 -- For a type conversion to an unconstrained array type, constructs
250 -- a range check action to check that the bounds of the source type
251 -- are within the constraints imposed by the Target_Typ.
253 -- For access types, the Directly_Designated_Type is retrieved and
254 -- processing continues as enumerated above, with a guard against
257 -- The source type is used by type conversions to unconstrained array
258 -- types to retrieve the corresponding bounds.
260 procedure Apply_Static_Length_Check
262 Target_Typ : Entity_Id;
263 Source_Typ : Entity_Id := Empty);
264 -- Tries to determine statically whether the two array types source type
265 -- and Target_Typ have the same length. If it can be determined at compile
266 -- time that they do not, then an N_Raise_Constraint_Error node replaces
267 -- Expr, and a warning message is issued.
269 procedure Apply_Scalar_Range_Check
271 Target_Typ : Entity_Id;
272 Source_Typ : Entity_Id := Empty;
273 Fixed_Int : Boolean := False);
274 -- For scalar types, determines whether an expression node should be
275 -- flagged as needing a runtime range check. If the node requires such
276 -- a check, the Do_Range_Check flag is turned on. The Fixed_Int flag
277 -- if set causes any fixed-point values to be treated as though they
278 -- were discrete values (i.e. the underlying integer value is used).
280 type Check_Result is private;
281 -- Type used to return result of Range_Check call, for later use in
282 -- call to Insert_Range_Checks procedure.
284 procedure Append_Range_Checks
285 (Checks : Check_Result;
287 Suppress_Typ : Entity_Id;
288 Static_Sloc : Source_Ptr;
289 Flag_Node : Node_Id);
290 -- Called to append range checks as returned by a call to Range_Check.
291 -- Stmts is a list to which either the dynamic check is appended or
292 -- the raise Constraint_Error statement is appended (for static checks).
293 -- Static_Sloc is the Sloc at which the raise CE node points,
294 -- Flag_Node is used as the node at which to set the Has_Dynamic_Check
295 -- flag. Checks_On is a boolean value that says if range and index checking
298 procedure Enable_Range_Check (N : Node_Id);
299 pragma Inline (Enable_Range_Check);
300 -- Set Do_Range_Check flag in node N to True unless Kill_Range_Check flag
301 -- is set in N (the purpose of the latter flag is precisely to prevent
302 -- Do_Range_Check from being set).
304 procedure Insert_Range_Checks
305 (Checks : Check_Result;
307 Suppress_Typ : Entity_Id;
308 Static_Sloc : Source_Ptr := No_Location;
309 Flag_Node : Node_Id := Empty;
310 Do_Before : Boolean := False);
311 -- Called to insert range checks as returned by a call to Range_Check.
312 -- Node is the node after which either the dynamic check is inserted or
313 -- the raise Constraint_Error statement is inserted (for static checks).
314 -- Suppress_Typ is the type to check to determine if checks are suppressed.
315 -- Static_Sloc, if passed, is the Sloc at which the raise CE node points,
316 -- otherwise Sloc (Node) is used. The Has_Dynamic_Check flag is normally
317 -- set at Node. If Flag_Node is present, then this is used instead as the
318 -- node at which to set the Has_Dynamic_Check flag. Normally the check is
319 -- inserted after, if Do_Before is True, the check is inserted before
324 Target_Typ : Entity_Id;
325 Source_Typ : Entity_Id := Empty;
326 Warn_Node : Node_Id := Empty)
328 -- Like Apply_Range_Check, except it does not modify anything. Instead
329 -- it returns an encapsulated result of the check operations for later
330 -- use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its
331 -- Sloc is used, in the static case, for the generated warning or error.
332 -- Additionally, it is used rather than Expr (or Low/High_Bound of Expr)
333 -- in constructing the check.
335 -----------------------
336 -- Validity Checking --
337 -----------------------
339 -- In (RM 13.9.1(9-11)) we have the following rules on invalid values
341 -- 9 If the representation of a scalar object does not represent a
342 -- value of the object's subtype (perhaps because the object was not
343 -- initialized), the object is said to have an invalid representation.
344 -- It is a bounded error to evaluate the value of such an object. If
345 -- the error is detected, either Constraint_Error or Program_Error is
346 -- raised. Otherwise, execution continues using the invalid
347 -- representation. The rules of the language outside this subclause
348 -- assume that all objects have valid representations. The semantics
349 -- of operations on invalid representations are as follows:
351 -- 10 If the representation of the object represents a value of the
352 -- object's type, the value of the type is used.
354 -- 11 If the representation of the object does not represent a value
355 -- of the object's type, the semantics of operations on such
356 -- representations is implementation-defined, but does not by
357 -- itself lead to erroneous or unpredictable execution, or to
358 -- other objects becoming abnormal.
360 -- We quote the rules in full here since they are quite delicate. Most
361 -- of the time, we can just compute away with wrong values, and get a
362 -- possibly wrong result, which is well within the range of allowed
363 -- implementation defined behavior. The two tricky cases are subscripted
364 -- array assignments, where we don't want to do wild stores, and case
365 -- statements where we don't want to do wild jumps.
367 -- In GNAT, we control validity checking with a switch -gnatV that
368 -- can take three parameters, n/d/f for None/Default/Full. These
369 -- modes have the following meanings:
371 -- None (no validity checking)
373 -- In this mode, there is no specific checking for invalid values
374 -- and the code generator assumes that all stored values are always
375 -- within the bounds of the object subtype. The consequences are as
378 -- For case statements, an out of range invalid value will cause
379 -- Constraint_Error to be raised, or an arbitrary one of the case
380 -- alternatives will be executed. Wild jumps cannot result even
381 -- in this mode, since we always do a range check
383 -- For subscripted array assignments, wild stores will result in
384 -- the expected manner when addresses are calculated using values
385 -- of subscripts that are out of range.
387 -- It could perhaps be argued that this mode is still conformant with
388 -- the letter of the RM, since implementation defined is a rather
389 -- broad category, but certainly it is not in the spirit of the
390 -- RM requirement, since wild stores certainly seem to be a case of
391 -- erroneous behavior.
393 -- Default (default standard RM-compatible validity checking)
395 -- In this mode, which is the default, minimal validity checking is
396 -- performed to ensure no erroneous behavior as follows:
398 -- For case statements, an out of range invalid value will cause
399 -- Constraint_Error to be raised.
401 -- For subscripted array assignments, invalid out of range
402 -- subscript values will cause Constraint_Error to be raised.
404 -- Full (Full validity checking)
406 -- In this mode, the protections guaranteed by the standard mode are
407 -- in place, and the following additional checks are made:
409 -- For every assignment, the right side is checked for validity
411 -- For every call, IN and IN OUT parameters are checked for validity
413 -- For every subscripted array reference, both for stores and loads,
414 -- all subscripts are checked for validity.
416 -- These checks are not required by the RM, but will in practice
417 -- improve the detection of uninitialized variables, particularly
418 -- if used in conjunction with pragma Normalize_Scalars.
420 -- In the above description, we talk about performing validity checks,
421 -- but we don't actually generate a check in a case where the compiler
422 -- can be sure that the value is valid. Note that this assurance must
423 -- be achieved without assuming that any uninitialized value lies within
424 -- the range of its type. The following are cases in which values are
425 -- known to be valid. The flag Is_Known_Valid is used to keep track of
426 -- some of these cases.
428 -- If all possible stored values are valid, then any uninitialized
429 -- value must be valid.
431 -- Literals, including enumeration literals, are clearly always valid.
433 -- Constants are always assumed valid, with a validity check being
434 -- performed on the initializing value where necessary to ensure that
437 -- For variables, the status is set to known valid if there is an
438 -- initializing expression. Again a check is made on the initializing
439 -- value if necessary to ensure that this assumption is valid. The
440 -- status can change as a result of local assignments to a variable.
441 -- If a known valid value is unconditionally assigned, then we mark
442 -- the left side as known valid. If a value is assigned that is not
443 -- known to be valid, then we mark the left side as invalid. This
444 -- kind of processing does NOT apply to non-local variables since we
445 -- are not following the flow graph (more properly the flow of actual
446 -- processing only corresponds to the flow graph for local assignments).
447 -- For non-local variables, we preserve the current setting, i.e. a
448 -- validity check is performed when assigning to a knonwn valid global.
450 -- Note: no validity checking is required if range checks are suppressed
451 -- regardless of the setting of the validity checking mode.
453 -- The following procedures are used in handling validity checking
455 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id);
456 -- Expr is the node for an indexed component. If validity checking and
457 -- range checking are enabled, all subscripts for this indexed component
458 -- are checked for validity.
460 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id);
461 -- Expr is a lvalue, i.e. an expression representing the target of
462 -- an assignment. This procedure checks for this expression involving
463 -- an assignment to an array value. We have to be sure that all the
464 -- subscripts in such a case are valid, since according to the rules
465 -- in (RM 13.9.1(9-11)) such assignments are not permitted to result
466 -- in erroneous behavior in the case of invalid subscript values.
468 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False);
469 -- Ensure that Expr represents a valid value of its type. If this type
470 -- is not a scalar type, then the call has no effect, since validity
471 -- is only an issue for scalar types. The effect of this call is to
472 -- check if the value is known valid, if so, nothing needs to be done.
473 -- If this is not known, then either Expr is set to be range checked,
474 -- or specific checking code is inserted so that an exception is raised
475 -- if the value is not valid.
477 -- The optional argument Holes_OK indicates whether it is necessary to
478 -- worry about enumeration types with non-standard representations leading
479 -- to "holes" in the range of possible representations. If Holes_OK is
480 -- True, then such values are assumed valid (this is used when the caller
481 -- will make a separate check for this case anyway). If Holes_OK is False,
482 -- then this case is checked, and code is inserted to ensure that Expr is
483 -- valid, raising Constraint_Error if the value is not valid.
485 function Expr_Known_Valid (Expr : Node_Id) return Boolean;
486 -- This function tests it the value of Expr is known to be valid in
487 -- the sense of RM 13.9.1(9-11). In the case of GNAT, it is only
488 -- discrete types which are a concern, since for non-discrete types
489 -- we simply continue computation with invalid values, which does
490 -- not lead to erroneous behavior. Thus Expr_Known_Valid always
491 -- returns True if the type of Expr is non-discrete. For discrete
492 -- types the value returned is True only if it can be determined
493 -- that the value is Valid. Otherwise False is returned.
495 procedure Insert_Valid_Check (Expr : Node_Id);
496 -- Inserts code that will check for the value of Expr being valid, in
497 -- the sense of the 'Valid attribute returning True. Constraint_Error
498 -- will be raised if the value is not valid.
502 type Check_Result is array (Positive range 1 .. 2) of Node_Id;
503 -- There are two cases for the result returned by Range_Check:
505 -- For the static case the result is one or two nodes that should cause
506 -- a Constraint_Error. Typically these will include Expr itself or the
507 -- direct descendents of Expr, such as Low/High_Bound (Expr)). It is the
508 -- responsibility of the caller to rewrite and substitute the nodes with
509 -- N_Raise_Constraint_Error nodes.
511 -- For the non-static case a single N_Raise_Constraint_Error node
512 -- with a non-empty Condition field is returned.
514 -- Unused entries in Check_Result, if any, are simply set to Empty
515 -- For external clients, the required processing on this result is
516 -- achieved using the Insert_Range_Checks routine.
518 pragma Inline (Access_Checks_Suppressed);
519 pragma Inline (Accessibility_Checks_Suppressed);
520 pragma Inline (Discriminant_Checks_Suppressed);
521 pragma Inline (Division_Checks_Suppressed);
522 pragma Inline (Elaboration_Checks_Suppressed);
523 pragma Inline (Index_Checks_Suppressed);
524 pragma Inline (Length_Checks_Suppressed);
525 pragma Inline (Overflow_Checks_Suppressed);
526 pragma Inline (Range_Checks_Suppressed);
527 pragma Inline (Storage_Checks_Suppressed);
528 pragma Inline (Tag_Checks_Suppressed);
530 pragma Inline (Apply_Length_Check);
531 pragma Inline (Apply_Range_Check);
532 pragma Inline (Apply_Static_Length_Check);