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