cfgloopmanip.c (copy_loop_info): New function.
[official-gcc.git] / gcc / ada / checks.ads
blobf7a4399386d8cb703825553c8b866f09cc5ec752
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-2012, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 -- Package containing routines used to deal with runtime checks. These
27 -- routines are used both by the semantics and by the expander. In some
28 -- cases, checks are enabled simply by setting flags for gigi, and in
29 -- other cases the code for the check is expanded.
31 -- The approach used for range and length checks, in regards to suppressed
32 -- checks, is to attempt to detect at compilation time that a constraint
33 -- error will occur. If this is detected a warning or error is issued and the
34 -- offending expression or statement replaced with a constraint error node.
35 -- This always occurs whether checks are suppressed or not. Dynamic range
36 -- checks are, of course, not inserted if checks are suppressed.
38 with Namet; use Namet;
39 with Table;
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 Alignment_Checks_Suppressed (E : Entity_Id) return Boolean;
52 function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean;
53 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean;
54 function Division_Checks_Suppressed (E : Entity_Id) return Boolean;
55 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean;
56 function Index_Checks_Suppressed (E : Entity_Id) return Boolean;
57 function Length_Checks_Suppressed (E : Entity_Id) return Boolean;
58 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean;
59 function Range_Checks_Suppressed (E : Entity_Id) return Boolean;
60 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean;
61 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean;
62 function Validity_Checks_Suppressed (E : Entity_Id) return Boolean;
63 -- These functions check to see if the named check is suppressed, either
64 -- by an active scope suppress setting, or because the check has been
65 -- specifically suppressed for the given entity. If no entity is relevant
66 -- for the current check, then Empty is used as an argument. Note: the
67 -- reason we insist on specifying Empty is to force the caller to think
68 -- about whether there is any relevant entity that should be checked.
70 function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean;
71 -- This function is called if Checks_May_Be_Suppressed (E) is True to
72 -- determine whether check C is suppressed either on the entity E or
73 -- as the result of a scope suppress pragma. If Checks_May_Be_Suppressed
74 -- is False, then the status of the check can be determined simply by
75 -- examining Scope_Checks (C), so this routine is not called in that case.
77 function Overflow_Check_Mode (E : Entity_Id) return Overflow_Check_Type;
78 -- Returns current overflow checking mode, taking into account whether
79 -- we are inside an assertion expression. Always returns Suppressed if
80 -- overflow checks are suppressed for entity E.
82 -------------------------------------------
83 -- Procedures to Activate Checking Flags --
84 -------------------------------------------
86 procedure Activate_Division_Check (N : Node_Id);
87 pragma Inline (Activate_Division_Check);
88 -- Sets Do_Division_Check flag in node N, and handles possible local raise.
89 -- Always call this routine rather than calling Set_Do_Division_Check to
90 -- set an explicit value of True, to ensure handling the local raise case.
92 procedure Activate_Overflow_Check (N : Node_Id);
93 pragma Inline (Activate_Overflow_Check);
94 -- Sets Do_Overflow_Check flag in node N, and handles possible local raise.
95 -- Always call this routine rather than calling Set_Do_Overflow_Check to
96 -- set an explicit value of True, to ensure handling the local raise case.
98 procedure Activate_Range_Check (N : Node_Id);
99 pragma Inline (Activate_Range_Check);
100 -- Sets Do_Range_Check flag in node N, and handles possible local raise
101 -- Always call this routine rather than calling Set_Do_Range_Check to
102 -- set an explicit value of True, to ensure handling the local raise case.
104 --------------------------------
105 -- Procedures to Apply Checks --
106 --------------------------------
108 -- General note on following checks. These checks are always active if
109 -- Expander_Active and not Inside_A_Generic. They are inactive and have
110 -- no effect Inside_A_Generic. In the case where not Expander_Active
111 -- and not Inside_A_Generic, most of them are inactive, but some of them
112 -- operate anyway since they may generate useful compile time warnings.
114 procedure Apply_Access_Check (N : Node_Id);
115 -- Determines whether an expression node requires a runtime access
116 -- check and if so inserts the appropriate run-time check.
118 procedure Apply_Accessibility_Check
119 (N : Node_Id;
120 Typ : Entity_Id;
121 Insert_Node : Node_Id);
122 -- Given a name N denoting an access parameter, emits a run-time
123 -- accessibility check (if necessary), checking that the level of
124 -- the object denoted by the access parameter is not deeper than the
125 -- level of the type Typ. Program_Error is raised if the check fails.
126 -- Insert_Node indicates the node where the check should be inserted.
128 procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id);
129 -- E is the entity for an object which has an address clause. If checks
130 -- are enabled, then this procedure generates a check that the specified
131 -- address has an alignment consistent with the alignment of the object,
132 -- raising PE if this is not the case. The resulting check (if one is
133 -- generated) is inserted before node N. check is also made for the case of
134 -- a clear overlay situation that the size of the overlaying object is not
135 -- larger than the overlaid object.
137 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id);
138 -- Handle overflow checking for an arithmetic operator. Also handles the
139 -- cases of ELIMINATED and MINIMIZED overflow checking mode. If the mode
140 -- is one of the latter two, then this routine can also be called with
141 -- an if or case expression node to make sure that we properly handle
142 -- overflow checking for dependent expressions. This routine handles
143 -- front end vs back end overflow checks (in the front end case it expands
144 -- the necessary check). Note that divide is handled separately using
145 -- Apply_Divide_Checks.
147 procedure Apply_Constraint_Check
148 (N : Node_Id;
149 Typ : Entity_Id;
150 No_Sliding : Boolean := False);
151 -- Top-level procedure, calls all the others depending on the class of
152 -- Typ. Checks that expression N satisfies the constraint of type Typ.
153 -- No_Sliding is only relevant for constrained array types, if set to
154 -- True, it checks that indexes are in range.
156 procedure Apply_Discriminant_Check
157 (N : Node_Id;
158 Typ : Entity_Id;
159 Lhs : Node_Id := Empty);
160 -- Given an expression N of a discriminated type, or of an access type
161 -- whose designated type is a discriminanted type, generates a check to
162 -- ensure that the expression can be converted to the subtype given as
163 -- the second parameter. Lhs is empty except in the case of assignments,
164 -- where the target object may be needed to determine the subtype to
165 -- check against (such as the cases of unconstrained formal parameters
166 -- and unconstrained aliased objects). For the case of unconstrained
167 -- formals, the check is performed only if the corresponding actual is
168 -- constrained, i.e., whether Lhs'Constrained is True.
170 procedure Apply_Divide_Checks (N : Node_Id);
171 -- The node kind is N_Op_Divide, N_Op_Mod, or N_Op_Rem if either of the
172 -- flags Do_Division_Check or Do_Overflow_Check is set, then this routine
173 -- ensures that the appropriate checks are made. Note that overflow can
174 -- occur in the signed case for the case of the largest negative number
175 -- divided by minus one.
177 procedure Apply_Parameter_Aliasing_Checks
178 (Call : Node_Id;
179 Subp : Entity_Id);
180 -- Given a subprogram call Call, add a check to verify that none of the
181 -- actuals overlap. Subp denotes the subprogram being called.
183 procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id);
184 -- Given a subprogram Subp, add both a pre and post condition pragmas that
185 -- verify the proper initialization of scalars in parameters and function
186 -- results.
188 procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id);
189 -- N is an expression to which a predicate check may need to be applied
190 -- for Typ, if Typ has a predicate function. The check is applied only
191 -- if the type of N does not match Typ.
193 procedure Apply_Type_Conversion_Checks (N : Node_Id);
194 -- N is an N_Type_Conversion node. A type conversion actually involves
195 -- two sorts of checks. The first check is the checks that ensures that
196 -- the operand in the type conversion fits onto the base type of the
197 -- subtype it is being converted to (see RM 4.6 (28)-(50)). The second
198 -- check is there to ensure that once the operand has been converted to
199 -- a value of the target type, this converted value meets the
200 -- constraints imposed by the target subtype (see RM 4.6 (51)).
202 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id);
203 -- The argument N is an attribute reference node intended for processing
204 -- by gigi. The attribute is one that returns a universal integer, but
205 -- the attribute reference node is currently typed with the expected
206 -- result type. This routine deals with range and overflow checks needed
207 -- to make sure that the universal result is in range.
209 function Build_Discriminant_Checks
210 (N : Node_Id;
211 T_Typ : Entity_Id)
212 return Node_Id;
213 -- Subsidiary routine for Apply_Discriminant_Check. Builds the expression
214 -- that compares discriminants of the expression with discriminants of the
215 -- type. Also used directly for membership tests (see Exp_Ch4.Expand_N_In).
217 function Convert_From_Bignum (N : Node_Id) return Node_Id;
218 -- Returns result of converting node N from Bignum. The returned value is
219 -- not analyzed, the caller takes responsibility for this. Node N must be
220 -- a subexpression node of type Bignum. The result is Long_Long_Integer.
222 function Convert_To_Bignum (N : Node_Id) return Node_Id;
223 -- Returns result of converting node N to Bignum. The returned value is not
224 -- analyzed, the caller takes responsibility for this. Node N must be a
225 -- subexpression node of a signed integer type or Bignum type (if it is
226 -- already a Bignum, the returned value is Relocate_Node (N)).
228 procedure Determine_Range
229 (N : Node_Id;
230 OK : out Boolean;
231 Lo : out Uint;
232 Hi : out Uint;
233 Assume_Valid : Boolean := False);
234 -- N is a node for a subexpression. If N is of a discrete type with no
235 -- error indications, and no other peculiarities (e.g. missing Etype),
236 -- then OK is True on return, and Lo and Hi are set to a conservative
237 -- estimate of the possible range of values of N. Thus if OK is True on
238 -- return, the value of the subexpression N is known to lie in the range
239 -- Lo .. Hi (inclusive). If the expression is not of a discrete type, or
240 -- some kind of error condition is detected, then OK is False on exit, and
241 -- Lo/Hi are set to No_Uint. Thus the significance of OK being False on
242 -- return is that no useful information is available on the range of the
243 -- expression. Assume_Valid determines whether the processing is allowed to
244 -- assume that values are in range of their subtypes. If it is set to True,
245 -- then this assumption is valid, if False, then processing is done using
246 -- base types to allow invalid values.
248 procedure Install_Null_Excluding_Check (N : Node_Id);
249 -- Determines whether an access node requires a runtime access check and
250 -- if so inserts the appropriate run-time check.
252 function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id;
253 -- This function is used by top level overflow checking routines to do a
254 -- mark/release operation on the secondary stack around bignum operations.
255 -- The block created looks like:
257 -- declare
258 -- M : Mark_Id := SS_Mark;
259 -- begin
260 -- SS_Release (M);
261 -- end;
263 -- The idea is that the caller will insert any needed extra declarations
264 -- after the declaration of M, and any needed statements (in particular
265 -- the bignum operations) before the call to SS_Release, and then do an
266 -- Insert_Action of the whole block (it is returned unanalyzed). The Loc
267 -- parameter is used to supply Sloc values for the constructed tree.
269 procedure Minimize_Eliminate_Overflow_Checks
270 (N : Node_Id;
271 Lo : out Uint;
272 Hi : out Uint;
273 Top_Level : Boolean);
274 -- This is the main routine for handling MINIMIZED and ELIMINATED overflow
275 -- checks. On entry N is a node whose result is a signed integer subtype.
276 -- If the node is an arithmetic operation, then a range analysis is carried
277 -- out, and there are three possibilities:
279 -- The node is left unchanged (apart from expansion of an exponentiation
280 -- operation). This happens if the routine can determine that the result
281 -- is definitely in range. The Do_Overflow_Check flag is turned off in
282 -- this case.
284 -- The node is transformed into an arithmetic operation with a result
285 -- type of Long_Long_Integer.
287 -- The node is transformed into a function call that calls an appropriate
288 -- function in the System.Bignums package to compute a Bignum result.
290 -- In the first two cases, Lo and Hi are set to the bounds of the possible
291 -- range of results, computed as accurately as possible. In the third case
292 -- Lo and Hi are set to No_Uint (there are some cases where we could get an
293 -- advantage from keeping result ranges for Bignum values, but it could use
294 -- a lot of space and is very unlikely to be valuable).
296 -- If the node is not an arithmetic operation, then it is unchanged but
297 -- Lo and Hi are still set (to the bounds of the result subtype if nothing
298 -- better can be determined).
300 -- Note: this function is recursive, if called with an arithmetic operator,
301 -- recursive calls are made to process the operands using this procedure.
302 -- So we end up doing things top down. Nothing happens to an arithmetic
303 -- expression until this procedure is called on the top level node and
304 -- then the recursive calls process all the children. We have to do it
305 -- this way. If we try to do it bottom up in natural expansion order, then
306 -- there are two problems. First, where do we stash the bounds, and more
307 -- importantly, semantic processing will be messed up. Consider A+B+C where
308 -- A,B,C are all of type integer, if we processed A+B before doing semantic
309 -- analysis of the addition of this result to C, that addition could end up
310 -- with a Long_Long_Integer left operand and an Integer right operand, and
311 -- we would get a semantic error.
313 -- The routine is called in three situations if we are operating in either
314 -- MINIMIZED or ELIMINATED modes.
316 -- Overflow checks applied to the top node of an expression tree when
317 -- that node is an arithmetic operator. In this case the result is
318 -- converted to the appropriate result type (there is special processing
319 -- when the parent is a conversion, see body for details).
321 -- Overflow checks are applied to the operands of a comparison operation.
322 -- In this case, the comparison is done on the result Long_Long_Integer
323 -- or Bignum values, without raising any exceptions.
325 -- Overflow checks are applied to the left operand of a membership test.
326 -- In this case no exception is raised if a Long_Long_Integer or Bignum
327 -- result is outside the range of the type of that left operand (it is
328 -- just that the result of IN is false in that case).
330 -- Note that if Bignum values appear, the caller must take care of doing
331 -- the appropriate mark/release operations on the secondary stack.
333 -- Top_Level is used to avoid inefficient unnecessary transitions into the
334 -- Bignum domain. If Top_Level is True, it means that the caller will have
335 -- to convert any Bignum value back to Long_Long_Integer, checking that the
336 -- value is in range. This is the normal case for a top level operator in
337 -- a subexpression. There is no point in going into Bignum mode to avoid an
338 -- overflow just so we can check for overflow the next moment. For calls
339 -- from comparisons and membership tests, and for all recursive calls, we
340 -- do want to transition into the Bignum domain if necessary. Note that
341 -- this setting is only relevant in ELIMINATED mode.
343 -------------------------------------------------------
344 -- Control and Optimization of Range/Overflow Checks --
345 -------------------------------------------------------
347 -- Range checks are controlled by the Do_Range_Check flag. The front end
348 -- is responsible for setting this flag in relevant nodes. Originally
349 -- the back end generated all corresponding range checks. But later on
350 -- we decided to generate many range checks in the front end. We are now
351 -- in the transitional phase where some of these checks are still done
352 -- by the back end, but many are done by the front end. It is possible
353 -- that in the future we might move all the checks to the front end. The
354 -- main remaining back end checks are for subscript checking.
356 -- Overflow checks are similarly controlled by the Do_Overflow_Check flag.
357 -- The difference here is that if back end overflow checks are inactive
358 -- (Backend_Overflow_Checks_On_Target set False), then the actual overflow
359 -- checks are generated by the front end, but if back end overflow checks
360 -- are active (Backend_Overflow_Checks_On_Target set True), then the back
361 -- end does generate the checks.
363 -- The following two routines are used to set these flags, they allow
364 -- for the possibility of eliminating checks. Checks can be eliminated
365 -- if an identical check has already been performed.
367 procedure Enable_Overflow_Check (N : Node_Id);
368 -- First this routine determines if an overflow check is needed by doing
369 -- an appropriate range check. If a check is not needed, then the call
370 -- has no effect. If a check is needed then this routine sets the flag
371 -- Do_Overflow_Check in node N to True, unless it can be determined that
372 -- the check is not needed. The only condition under which this is the
373 -- case is if there was an identical check earlier on. These optimziations
374 -- apply to CHECKED mode, but not to MINIMIZED/ELIMINATED modes. See the
375 -- body for a full explanation.
377 procedure Enable_Range_Check (N : Node_Id);
378 -- Set Do_Range_Check flag in node N True, unless it can be determined
379 -- that the check is not needed. The only condition under which this is
380 -- the case is if there was an identical check earlier on. This routine
381 -- is not responsible for doing range analysis to determine whether or
382 -- not such a check is needed -- the caller is expected to do this. The
383 -- one other case in which the request to set the flag is ignored is
384 -- when Kill_Range_Check is set in an N_Unchecked_Conversion node.
386 -- The following routines are used to keep track of processing sequences
387 -- of statements (e.g. the THEN statements of an IF statement). A check
388 -- that appears within such a sequence can eliminate an identical check
389 -- within this sequence of statements. However, after the end of the
390 -- sequence of statements, such a check is no longer of interest, since
391 -- it may not have been executed.
393 procedure Conditional_Statements_Begin;
394 -- This call marks the start of processing of a sequence of statements.
395 -- Every call to this procedure must be followed by a matching call to
396 -- Conditional_Statements_End.
398 procedure Conditional_Statements_End;
399 -- This call removes from consideration all saved checks since the
400 -- corresponding call to Conditional_Statements_Begin. These two
401 -- procedures operate in a stack like manner.
403 -- The mechanism for optimizing checks works by remembering checks
404 -- that have already been made, but certain conditions, for example
405 -- an assignment to a variable involved in a check, may mean that the
406 -- remembered check is no longer valid, in the sense that if the same
407 -- expression appears again, another check is required because the
408 -- value may have changed.
410 -- The following routines are used to note conditions which may render
411 -- some or all of the stored and remembered checks to be invalidated.
413 procedure Kill_Checks (V : Entity_Id);
414 -- This procedure records an assignment or other condition that causes
415 -- the value of the variable to be changed, invalidating any stored
416 -- checks that reference the value. Note that all such checks must
417 -- be discarded, even if they are not in the current statement range.
419 procedure Kill_All_Checks;
420 -- This procedure kills all remembered checks
422 -----------------------------
423 -- Length and Range Checks --
424 -----------------------------
426 -- In the following procedures, there are three arguments which have
427 -- a common meaning as follows:
429 -- Expr The expression to be checked. If a check is required,
430 -- the appropriate flag will be placed on this node. Whether
431 -- this node is further examined depends on the setting of
432 -- the parameter Source_Typ, as described below.
434 -- ??? Apply_Length_Check and Apply_Range_Check do not have an Expr
435 -- formal
437 -- ??? Apply_Length_Check and Apply_Range_Check have a Ck_Node formal
438 -- which is undocumented, is it the same as Expr?
440 -- Target_Typ The target type on which the check is to be based. For
441 -- example, if we have a scalar range check, then the check
442 -- is that we are in range of this type.
444 -- Source_Typ Normally Empty, but can be set to a type, in which case
445 -- this type is used for the check, see below.
447 -- The checks operate in one of two modes:
449 -- If Source_Typ is Empty, then the node Expr is examined, at the very
450 -- least to get the source subtype. In addition for some of the checks,
451 -- the actual form of the node may be examined. For example, a node of
452 -- type Integer whose actual form is an Integer conversion from a type
453 -- with range 0 .. 3 can be determined to have a value in range 0 .. 3.
455 -- If Source_Typ is given, then nothing can be assumed about the Expr,
456 -- and indeed its contents are not examined. In this case the check is
457 -- based on the assumption that Expr can be an arbitrary value of the
458 -- given Source_Typ.
460 -- Currently, the only case in which a Source_Typ is explicitly supplied
461 -- is for the case of Out and In_Out parameters, where, for the conversion
462 -- on return (the Out direction), the types must be reversed. This is
463 -- handled by the caller.
465 procedure Apply_Length_Check
466 (Ck_Node : Node_Id;
467 Target_Typ : Entity_Id;
468 Source_Typ : Entity_Id := Empty);
469 -- This procedure builds a sequence of declarations to do a length check
470 -- that checks if the lengths of the two arrays Target_Typ and source type
471 -- are the same. The resulting actions are inserted at Node using a call
472 -- to Insert_Actions.
474 -- For access types, the Directly_Designated_Type is retrieved and
475 -- processing continues as enumerated above, with a guard against null
476 -- values.
478 -- Note: calls to Apply_Length_Check currently never supply an explicit
479 -- Source_Typ parameter, but Apply_Length_Check takes this parameter and
480 -- processes it as described above for consistency with the other routines
481 -- in this section.
483 procedure Apply_Range_Check
484 (Ck_Node : Node_Id;
485 Target_Typ : Entity_Id;
486 Source_Typ : Entity_Id := Empty);
487 -- For a Node of kind N_Range, constructs a range check action that tests
488 -- first that the range is not null and then that the range is contained in
489 -- the Target_Typ range.
491 -- For scalar types, constructs a range check action that first tests that
492 -- the expression is contained in the Target_Typ range. The difference
493 -- between this and Apply_Scalar_Range_Check is that the latter generates
494 -- the actual checking code in gigi against the Etype of the expression.
496 -- For constrained array types, construct series of range check actions
497 -- to check that each Expr range is properly contained in the range of
498 -- Target_Typ.
500 -- For a type conversion to an unconstrained array type, constructs a range
501 -- check action to check that the bounds of the source type are within the
502 -- constraints imposed by the Target_Typ.
504 -- For access types, the Directly_Designated_Type is retrieved and
505 -- processing continues as enumerated above, with a guard against null
506 -- values.
508 -- The source type is used by type conversions to unconstrained array
509 -- types to retrieve the corresponding bounds.
511 procedure Apply_Static_Length_Check
512 (Expr : Node_Id;
513 Target_Typ : Entity_Id;
514 Source_Typ : Entity_Id := Empty);
515 -- Tries to determine statically whether the two array types source type
516 -- and Target_Typ have the same length. If it can be determined at compile
517 -- time that they do not, then an N_Raise_Constraint_Error node replaces
518 -- Expr, and a warning message is issued.
520 procedure Apply_Scalar_Range_Check
521 (Expr : Node_Id;
522 Target_Typ : Entity_Id;
523 Source_Typ : Entity_Id := Empty;
524 Fixed_Int : Boolean := False);
525 -- For scalar types, determines whether an expression node should be
526 -- flagged as needing a runtime range check. If the node requires such a
527 -- check, the Do_Range_Check flag is turned on. The Fixed_Int flag if set
528 -- causes any fixed-point values to be treated as though they were discrete
529 -- values (i.e. the underlying integer value is used).
531 type Check_Result is private;
532 -- Type used to return result of Get_Range_Checks call, for later use in
533 -- call to Insert_Range_Checks procedure.
535 function Get_Range_Checks
536 (Ck_Node : Node_Id;
537 Target_Typ : Entity_Id;
538 Source_Typ : Entity_Id := Empty;
539 Warn_Node : Node_Id := Empty) return Check_Result;
540 -- Like Apply_Range_Check, except it does not modify anything. Instead
541 -- it returns an encapsulated result of the check operations for later
542 -- use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its
543 -- Sloc is used, in the static case, for the generated warning or error.
544 -- Additionally, it is used rather than Expr (or Low/High_Bound of Expr)
545 -- in constructing the check.
547 procedure Append_Range_Checks
548 (Checks : Check_Result;
549 Stmts : List_Id;
550 Suppress_Typ : Entity_Id;
551 Static_Sloc : Source_Ptr;
552 Flag_Node : Node_Id);
553 -- Called to append range checks as returned by a call to Get_Range_Checks.
554 -- Stmts is a list to which either the dynamic check is appended or the
555 -- raise Constraint_Error statement is appended (for static checks).
556 -- Static_Sloc is the Sloc at which the raise CE node points, Flag_Node is
557 -- used as the node at which to set the Has_Dynamic_Check flag. Checks_On
558 -- is a boolean value that says if range and index checking is on or not.
560 procedure Insert_Range_Checks
561 (Checks : Check_Result;
562 Node : Node_Id;
563 Suppress_Typ : Entity_Id;
564 Static_Sloc : Source_Ptr := No_Location;
565 Flag_Node : Node_Id := Empty;
566 Do_Before : Boolean := False);
567 -- Called to insert range checks as returned by a call to Get_Range_Checks.
568 -- Node is the node after which either the dynamic check is inserted or
569 -- the raise Constraint_Error statement is inserted (for static checks).
570 -- Suppress_Typ is the type to check to determine if checks are suppressed.
571 -- Static_Sloc, if passed, is the Sloc at which the raise CE node points,
572 -- otherwise Sloc (Node) is used. The Has_Dynamic_Check flag is normally
573 -- set at Node. If Flag_Node is present, then this is used instead as the
574 -- node at which to set the Has_Dynamic_Check flag. Normally the check is
575 -- inserted after, if Do_Before is True, the check is inserted before
576 -- Node.
578 -----------------------
579 -- Expander Routines --
580 -----------------------
582 -- Some of the earlier processing for checks results in temporarily setting
583 -- the Do_Range_Check flag rather than actually generating checks. Now we
584 -- are moving the generation of such checks into the front end for reasons
585 -- of efficiency and simplicity (there were difficulties in handling this
586 -- in the back end when side effects were present in the expressions being
587 -- checked).
589 -- Probably we could eliminate the Do_Range_Check flag entirely and
590 -- generate the checks earlier, but this is a delicate area and it
591 -- seemed safer to implement the following routines, which are called
592 -- late on in the expansion process. They check the Do_Range_Check flag
593 -- and if it is set, generate the actual checks and reset the flag.
595 procedure Generate_Range_Check
596 (N : Node_Id;
597 Target_Type : Entity_Id;
598 Reason : RT_Exception_Code);
599 -- This procedure is called to actually generate and insert a range check.
600 -- A check is generated to ensure that the value of N lies within the range
601 -- of the target type. Note that the base type of N may be different from
602 -- the base type of the target type. This happens in the conversion case.
603 -- The Reason parameter is the exception code to be used for the exception
604 -- if raised.
606 -- Note on the relation of this routine to the Do_Range_Check flag. Mostly
607 -- for historical reasons, we often set the Do_Range_Check flag and then
608 -- later we call Generate_Range_Check if this flag is set. Most probably we
609 -- could eliminate this intermediate setting of the flag (historically the
610 -- back end dealt with range checks, using this flag to indicate if a check
611 -- was required, then we moved checks into the front end).
613 procedure Generate_Index_Checks (N : Node_Id);
614 -- This procedure is called to generate index checks on the subscripts for
615 -- the indexed component node N. Each subscript expression is examined, and
616 -- if the Do_Range_Check flag is set, an appropriate index check is
617 -- generated and the flag is reset.
619 -- Similarly, we set the flag Do_Discriminant_Check in the semantic
620 -- analysis to indicate that a discriminant check is required for selected
621 -- component of a discriminated type. The following routine is called from
622 -- the expander to actually generate the call.
624 procedure Generate_Discriminant_Check (N : Node_Id);
625 -- N is a selected component for which a discriminant check is required to
626 -- make sure that the discriminants have appropriate values for the
627 -- selection. This is done by calling the appropriate discriminant checking
628 -- routine for the selector.
630 -----------------------
631 -- Validity Checking --
632 -----------------------
634 -- In (RM 13.9.1(9-11)) we have the following rules on invalid values
636 -- If the representation of a scalar object does not represent value of
637 -- the object's subtype (perhaps because the object was not initialized),
638 -- the object is said to have an invalid representation. It is a bounded
639 -- error to evaluate the value of such an object. If the error is
640 -- detected, either Constraint_Error or Program_Error is raised.
641 -- Otherwise, execution continues using the invalid representation. The
642 -- rules of the language outside this subclause assume that all objects
643 -- have valid representations. The semantics of operations on invalid
644 -- representations are as follows:
646 -- 10 If the representation of the object represents a value of the
647 -- object's type, the value of the type is used.
649 -- 11 If the representation of the object does not represent a value
650 -- of the object's type, the semantics of operations on such
651 -- representations is implementation-defined, but does not by
652 -- itself lead to erroneous or unpredictable execution, or to
653 -- other objects becoming abnormal.
655 -- We quote the rules in full here since they are quite delicate. Most
656 -- of the time, we can just compute away with wrong values, and get a
657 -- possibly wrong result, which is well within the range of allowed
658 -- implementation defined behavior. The two tricky cases are subscripted
659 -- array assignments, where we don't want to do wild stores, and case
660 -- statements where we don't want to do wild jumps.
662 -- In GNAT, we control validity checking with a switch -gnatV that can take
663 -- three parameters, n/d/f for None/Default/Full. These modes have the
664 -- following meanings:
666 -- None (no validity checking)
668 -- In this mode, there is no specific checking for invalid values
669 -- and the code generator assumes that all stored values are always
670 -- within the bounds of the object subtype. The consequences are as
671 -- follows:
673 -- For case statements, an out of range invalid value will cause
674 -- Constraint_Error to be raised, or an arbitrary one of the case
675 -- alternatives will be executed. Wild jumps cannot result even
676 -- in this mode, since we always do a range check
678 -- For subscripted array assignments, wild stores will result in
679 -- the expected manner when addresses are calculated using values
680 -- of subscripts that are out of range.
682 -- It could perhaps be argued that this mode is still conformant with
683 -- the letter of the RM, since implementation defined is a rather
684 -- broad category, but certainly it is not in the spirit of the
685 -- RM requirement, since wild stores certainly seem to be a case of
686 -- erroneous behavior.
688 -- Default (default standard RM-compatible validity checking)
690 -- In this mode, which is the default, minimal validity checking is
691 -- performed to ensure no erroneous behavior as follows:
693 -- For case statements, an out of range invalid value will cause
694 -- Constraint_Error to be raised.
696 -- For subscripted array assignments, invalid out of range
697 -- subscript values will cause Constraint_Error to be raised.
699 -- Full (Full validity checking)
701 -- In this mode, the protections guaranteed by the standard mode are
702 -- in place, and the following additional checks are made:
704 -- For every assignment, the right side is checked for validity
706 -- For every call, IN and IN OUT parameters are checked for validity
708 -- For every subscripted array reference, both for stores and loads,
709 -- all subscripts are checked for validity.
711 -- These checks are not required by the RM, but will in practice
712 -- improve the detection of uninitialized variables, particularly
713 -- if used in conjunction with pragma Normalize_Scalars.
715 -- In the above description, we talk about performing validity checks,
716 -- but we don't actually generate a check in a case where the compiler
717 -- can be sure that the value is valid. Note that this assurance must
718 -- be achieved without assuming that any uninitialized value lies within
719 -- the range of its type. The following are cases in which values are
720 -- known to be valid. The flag Is_Known_Valid is used to keep track of
721 -- some of these cases.
723 -- If all possible stored values are valid, then any uninitialized
724 -- value must be valid.
726 -- Literals, including enumeration literals, are clearly always valid
728 -- Constants are always assumed valid, with a validity check being
729 -- performed on the initializing value where necessary to ensure that
730 -- this is the case.
732 -- For variables, the status is set to known valid if there is an
733 -- initializing expression. Again a check is made on the initializing
734 -- value if necessary to ensure that this assumption is valid. The
735 -- status can change as a result of local assignments to a variable.
736 -- If a known valid value is unconditionally assigned, then we mark
737 -- the left side as known valid. If a value is assigned that is not
738 -- known to be valid, then we mark the left side as invalid. This
739 -- kind of processing does NOT apply to non-local variables since we
740 -- are not following the flow graph (more properly the flow of actual
741 -- processing only corresponds to the flow graph for local assignments).
742 -- For non-local variables, we preserve the current setting, i.e. a
743 -- validity check is performed when assigning to a knonwn valid global.
745 -- Note: no validity checking is required if range checks are suppressed
746 -- regardless of the setting of the validity checking mode.
748 -- The following procedures are used in handling validity checking
750 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id);
751 -- Expr is the node for an indexed component. If validity checking and
752 -- range checking are enabled, all subscripts for this indexed component
753 -- are checked for validity.
755 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id);
756 -- Expr is a lvalue, i.e. an expression representing the target of an
757 -- assignment. This procedure checks for this expression involving an
758 -- assignment to an array value. We have to be sure that all the subscripts
759 -- in such a case are valid, since according to the rules in (RM
760 -- 13.9.1(9-11)) such assignments are not permitted to result in erroneous
761 -- behavior in the case of invalid subscript values.
763 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False);
764 -- Ensure that Expr represents a valid value of its type. If this type
765 -- is not a scalar type, then the call has no effect, since validity
766 -- is only an issue for scalar types. The effect of this call is to
767 -- check if the value is known valid, if so, nothing needs to be done.
768 -- If this is not known, then either Expr is set to be range checked,
769 -- or specific checking code is inserted so that an exception is raised
770 -- if the value is not valid.
772 -- The optional argument Holes_OK indicates whether it is necessary to
773 -- worry about enumeration types with non-standard representations leading
774 -- to "holes" in the range of possible representations. If Holes_OK is
775 -- True, then such values are assumed valid (this is used when the caller
776 -- will make a separate check for this case anyway). If Holes_OK is False,
777 -- then this case is checked, and code is inserted to ensure that Expr is
778 -- valid, raising Constraint_Error if the value is not valid.
780 function Expr_Known_Valid (Expr : Node_Id) return Boolean;
781 -- This function tests it the value of Expr is known to be valid in the
782 -- sense of RM 13.9.1(9-11). In the case of GNAT, it is only discrete types
783 -- which are a concern, since for non-discrete types we simply continue
784 -- computation with invalid values, which does not lead to erroneous
785 -- behavior. Thus Expr_Known_Valid always returns True if the type of Expr
786 -- is non-discrete. For discrete types the value returned is True only if
787 -- it can be determined that the value is Valid. Otherwise False is
788 -- returned.
790 procedure Insert_Valid_Check (Expr : Node_Id);
791 -- Inserts code that will check for the value of Expr being valid, in
792 -- the sense of the 'Valid attribute returning True. Constraint_Error
793 -- will be raised if the value is not valid.
795 procedure Null_Exclusion_Static_Checks (N : Node_Id);
796 -- Ada 2005 (AI-231): Check bad usages of the null-exclusion issue
798 procedure Remove_Checks (Expr : Node_Id);
799 -- Remove all checks from Expr except those that are only executed
800 -- conditionally (on the right side of And Then/Or Else. This call
801 -- removes only embedded checks (Do_Range_Check, Do_Overflow_Check).
803 procedure Validity_Check_Range (N : Node_Id);
804 -- If N is an N_Range node, then Ensure_Valid is called on its bounds,
805 -- if validity checking of operands is enabled.
807 -----------------------------
808 -- Handling of Check Names --
809 -----------------------------
811 -- The following table contains Name_Id's for recognized checks. The first
812 -- entries (corresponding to the values of the subtype Predefined_Check_Id)
813 -- contain the Name_Id values for the checks that are predefined, including
814 -- All_Checks (see Types). Remaining entries are those that are introduced
815 -- by pragma Check_Names.
817 package Check_Names is new Table.Table (
818 Table_Component_Type => Name_Id,
819 Table_Index_Type => Check_Id,
820 Table_Low_Bound => 1,
821 Table_Initial => 30,
822 Table_Increment => 200,
823 Table_Name => "Name_Check_Names");
825 function Get_Check_Id (N : Name_Id) return Check_Id;
826 -- Function to search above table for matching name. If found returns the
827 -- corresponding Check_Id value in the range 1 .. Check_Name.Last. If not
828 -- found returns No_Check_Id.
830 private
832 type Check_Result is array (Positive range 1 .. 2) of Node_Id;
833 -- There are two cases for the result returned by Range_Check:
835 -- For the static case the result is one or two nodes that should cause
836 -- a Constraint_Error. Typically these will include Expr itself or the
837 -- direct descendents of Expr, such as Low/High_Bound (Expr)). It is the
838 -- responsibility of the caller to rewrite and substitute the nodes with
839 -- N_Raise_Constraint_Error nodes.
841 -- For the non-static case a single N_Raise_Constraint_Error node with a
842 -- non-empty Condition field is returned.
844 -- Unused entries in Check_Result, if any, are simply set to Empty For
845 -- external clients, the required processing on this result is achieved
846 -- using the Insert_Range_Checks routine.
848 pragma Inline (Apply_Length_Check);
849 pragma Inline (Apply_Range_Check);
850 pragma Inline (Apply_Static_Length_Check);
851 end Checks;