]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/checks.adb
2017-09-08 Bob Duff <duff@adacore.com>
[thirdparty/gcc.git] / gcc / ada / checks.adb
CommitLineData
ee6ba406 1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- C H E C K S --
6-- --
7-- B o d y --
8-- --
2b4f2458 9-- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
ee6ba406 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- --
80df182a 13-- ware Foundation; either version 3, or (at your option) any later ver- --
ee6ba406 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 --
80df182a 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. --
ee6ba406 20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
e78e8c8e 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
ee6ba406 23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
29448168 27with Casing; use Casing;
ee6ba406 28with Debug; use Debug;
29with Einfo; use Einfo;
7b8fa048 30with Elists; use Elists;
31with Eval_Fat; use Eval_Fat;
32with Exp_Ch11; use Exp_Ch11;
ee6ba406 33with Exp_Ch2; use Exp_Ch2;
df40eeb0 34with Exp_Ch4; use Exp_Ch4;
05fcfafb 35with Exp_Pakd; use Exp_Pakd;
ee6ba406 36with Exp_Util; use Exp_Util;
4fb5f0a0 37with Expander; use Expander;
ee6ba406 38with Freeze; use Freeze;
9dfe12ae 39with Lib; use Lib;
ee6ba406 40with Nlists; use Nlists;
41with Nmake; use Nmake;
42with Opt; use Opt;
9dfe12ae 43with Output; use Output;
c2b56224 44with Restrict; use Restrict;
1e16c51c 45with Rident; use Rident;
ee6ba406 46with Rtsfind; use Rtsfind;
47with Sem; use Sem;
d60c9ff7 48with Sem_Aux; use Sem_Aux;
00f91aef 49with Sem_Ch3; use Sem_Ch3;
9dfe12ae 50with Sem_Ch8; use Sem_Ch8;
7e933b61 51with Sem_Disp; use Sem_Disp;
7b8fa048 52with Sem_Eval; use Sem_Eval;
ee6ba406 53with Sem_Res; use Sem_Res;
54with Sem_Util; use Sem_Util;
55with Sem_Warn; use Sem_Warn;
56with Sinfo; use Sinfo;
9dfe12ae 57with Sinput; use Sinput;
ee6ba406 58with Snames; use Snames;
9dfe12ae 59with Sprint; use Sprint;
ee6ba406 60with Stand; use Stand;
bb569db0 61with Stringt; use Stringt;
f15731c4 62with Targparm; use Targparm;
ee6ba406 63with Tbuild; use Tbuild;
64with Ttypes; use Ttypes;
ee6ba406 65with Validsw; use Validsw;
66
67package body Checks is
68
69 -- General note: many of these routines are concerned with generating
70 -- checking code to make sure that constraint error is raised at runtime.
71 -- Clearly this code is only needed if the expander is active, since
72 -- otherwise we will not be generating code or going into the runtime
73 -- execution anyway.
74
75 -- We therefore disconnect most of these checks if the expander is
76 -- inactive. This has the additional benefit that we do not need to
77 -- worry about the tree being messed up by previous errors (since errors
78 -- turn off expansion anyway).
79
80 -- There are a few exceptions to the above rule. For instance routines
81 -- such as Apply_Scalar_Range_Check that do not insert any code can be
82 -- safely called even when the Expander is inactive (but Errors_Detected
83 -- is 0). The benefit of executing this code when expansion is off, is
84 -- the ability to emit constraint error warning for static expressions
85 -- even when we are not generating code.
86
20cf157b 87 -- The above is modified in gnatprove mode to ensure that proper check
88 -- flags are always placed, even if expansion is off.
89
9dfe12ae 90 -------------------------------------
91 -- Suppression of Redundant Checks --
92 -------------------------------------
93
94 -- This unit implements a limited circuit for removal of redundant
95 -- checks. The processing is based on a tracing of simple sequential
96 -- flow. For any sequence of statements, we save expressions that are
97 -- marked to be checked, and then if the same expression appears later
98 -- with the same check, then under certain circumstances, the second
99 -- check can be suppressed.
100
101 -- Basically, we can suppress the check if we know for certain that
102 -- the previous expression has been elaborated (together with its
103 -- check), and we know that the exception frame is the same, and that
104 -- nothing has happened to change the result of the exception.
105
106 -- Let us examine each of these three conditions in turn to describe
107 -- how we ensure that this condition is met.
108
109 -- First, we need to know for certain that the previous expression has
6fb3c314 110 -- been executed. This is done principally by the mechanism of calling
9dfe12ae 111 -- Conditional_Statements_Begin at the start of any statement sequence
112 -- and Conditional_Statements_End at the end. The End call causes all
113 -- checks remembered since the Begin call to be discarded. This does
114 -- miss a few cases, notably the case of a nested BEGIN-END block with
115 -- no exception handlers. But the important thing is to be conservative.
116 -- The other protection is that all checks are discarded if a label
117 -- is encountered, since then the assumption of sequential execution
118 -- is violated, and we don't know enough about the flow.
119
120 -- Second, we need to know that the exception frame is the same. We
121 -- do this by killing all remembered checks when we enter a new frame.
122 -- Again, that's over-conservative, but generally the cases we can help
123 -- with are pretty local anyway (like the body of a loop for example).
124
125 -- Third, we must be sure to forget any checks which are no longer valid.
126 -- This is done by two mechanisms, first the Kill_Checks_Variable call is
127 -- used to note any changes to local variables. We only attempt to deal
128 -- with checks involving local variables, so we do not need to worry
129 -- about global variables. Second, a call to any non-global procedure
130 -- causes us to abandon all stored checks, since such a all may affect
131 -- the values of any local variables.
132
133 -- The following define the data structures used to deal with remembering
134 -- checks so that redundant checks can be eliminated as described above.
135
136 -- Right now, the only expressions that we deal with are of the form of
137 -- simple local objects (either declared locally, or IN parameters) or
138 -- such objects plus/minus a compile time known constant. We can do
139 -- more later on if it seems worthwhile, but this catches many simple
140 -- cases in practice.
141
142 -- The following record type reflects a single saved check. An entry
143 -- is made in the stack of saved checks if and only if the expression
144 -- has been elaborated with the indicated checks.
145
146 type Saved_Check is record
147 Killed : Boolean;
148 -- Set True if entry is killed by Kill_Checks
149
150 Entity : Entity_Id;
151 -- The entity involved in the expression that is checked
152
153 Offset : Uint;
154 -- A compile time value indicating the result of adding or
155 -- subtracting a compile time value. This value is to be
156 -- added to the value of the Entity. A value of zero is
157 -- used for the case of a simple entity reference.
158
159 Check_Type : Character;
160 -- This is set to 'R' for a range check (in which case Target_Type
161 -- is set to the target type for the range check) or to 'O' for an
162 -- overflow check (in which case Target_Type is set to Empty).
163
164 Target_Type : Entity_Id;
165 -- Used only if Do_Range_Check is set. Records the target type for
166 -- the check. We need this, because a check is a duplicate only if
6fb3c314 167 -- it has the same target type (or more accurately one with a
9dfe12ae 168 -- range that is smaller or equal to the stored target type of a
169 -- saved check).
170 end record;
171
172 -- The following table keeps track of saved checks. Rather than use an
bbbfe30c 173 -- extensible table, we just use a table of fixed size, and we discard
9dfe12ae 174 -- any saved checks that do not fit. That's very unlikely to happen and
175 -- this is only an optimization in any case.
176
177 Saved_Checks : array (Int range 1 .. 200) of Saved_Check;
178 -- Array of saved checks
179
180 Num_Saved_Checks : Nat := 0;
181 -- Number of saved checks
182
183 -- The following stack keeps track of statement ranges. It is treated
184 -- as a stack. When Conditional_Statements_Begin is called, an entry
185 -- is pushed onto this stack containing the value of Num_Saved_Checks
186 -- at the time of the call. Then when Conditional_Statements_End is
187 -- called, this value is popped off and used to reset Num_Saved_Checks.
188
189 -- Note: again, this is a fixed length stack with a size that should
190 -- always be fine. If the value of the stack pointer goes above the
191 -- limit, then we just forget all saved checks.
192
193 Saved_Checks_Stack : array (Int range 1 .. 100) of Nat;
194 Saved_Checks_TOS : Nat := 0;
195
196 -----------------------
197 -- Local Subprograms --
198 -----------------------
ee6ba406 199
0df9d43f 200 procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id);
3cce7f32 201 -- Used to apply arithmetic overflow checks for all cases except operators
691fe9e0 202 -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we
0df9d43f 203 -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a
204 -- signed integer arithmetic operator (but not an if or case expression).
205 -- It is also called for types other than signed integers.
3cce7f32 206
207 procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id);
208 -- Used to apply arithmetic overflow checks for the case where the overflow
0df9d43f 209 -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer
210 -- arithmetic op (which includes the case of if and case expressions). Note
211 -- that Do_Overflow_Check may or may not be set for node Op. In these modes
212 -- we have work to do even if overflow checking is suppressed.
3cce7f32 213
2fe22c69 214 procedure Apply_Division_Check
215 (N : Node_Id;
216 Rlo : Uint;
217 Rhi : Uint;
218 ROK : Boolean);
219 -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies
220 -- division checks as required if the Do_Division_Check flag is set.
221 -- Rlo and Rhi give the possible range of the right operand, these values
222 -- can be referenced and trusted only if ROK is set True.
223
224 procedure Apply_Float_Conversion_Check
225 (Ck_Node : Node_Id;
226 Target_Typ : Entity_Id);
227 -- The checks on a conversion from a floating-point type to an integer
228 -- type are delicate. They have to be performed before conversion, they
229 -- have to raise an exception when the operand is a NaN, and rounding must
230 -- be taken into account to determine the safe bounds of the operand.
231
ee6ba406 232 procedure Apply_Selected_Length_Checks
233 (Ck_Node : Node_Id;
234 Target_Typ : Entity_Id;
235 Source_Typ : Entity_Id;
236 Do_Static : Boolean);
237 -- This is the subprogram that does all the work for Apply_Length_Check
238 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
239 -- described for the above routines. The Do_Static flag indicates that
240 -- only a static check is to be done.
241
242 procedure Apply_Selected_Range_Checks
243 (Ck_Node : Node_Id;
244 Target_Typ : Entity_Id;
245 Source_Typ : Entity_Id;
246 Do_Static : Boolean);
247 -- This is the subprogram that does all the work for Apply_Range_Check.
248 -- Expr, Target_Typ and Source_Typ are as described for the above
249 -- routine. The Do_Static flag indicates that only a static check is
250 -- to be done.
251
2af58f67 252 type Check_Type is new Check_Id range Access_Check .. Division_Check;
13dbf220 253 function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean;
254 -- This function is used to see if an access or division by zero check is
255 -- needed. The check is to be applied to a single variable appearing in the
256 -- source, and N is the node for the reference. If N is not of this form,
257 -- True is returned with no further processing. If N is of the right form,
258 -- then further processing determines if the given Check is needed.
259 --
260 -- The particular circuit is to see if we have the case of a check that is
261 -- not needed because it appears in the right operand of a short circuited
262 -- conditional where the left operand guards the check. For example:
263 --
264 -- if Var = 0 or else Q / Var > 12 then
265 -- ...
266 -- end if;
267 --
268 -- In this example, the division check is not required. At the same time
269 -- we can issue warnings for suspicious use of non-short-circuited forms,
270 -- such as:
271 --
272 -- if Var = 0 or Q / Var > 12 then
273 -- ...
274 -- end if;
275
9dfe12ae 276 procedure Find_Check
277 (Expr : Node_Id;
278 Check_Type : Character;
279 Target_Type : Entity_Id;
280 Entry_OK : out Boolean;
281 Check_Num : out Nat;
282 Ent : out Entity_Id;
283 Ofs : out Uint);
284 -- This routine is used by Enable_Range_Check and Enable_Overflow_Check
285 -- to see if a check is of the form for optimization, and if so, to see
286 -- if it has already been performed. Expr is the expression to check,
287 -- and Check_Type is 'R' for a range check, 'O' for an overflow check.
288 -- Target_Type is the target type for a range check, and Empty for an
289 -- overflow check. If the entry is not of the form for optimization,
290 -- then Entry_OK is set to False, and the remaining out parameters
291 -- are undefined. If the entry is OK, then Ent/Ofs are set to the
292 -- entity and offset from the expression. Check_Num is the number of
293 -- a matching saved entry in Saved_Checks, or zero if no such entry
294 -- is located.
295
ee6ba406 296 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
297 -- If a discriminal is used in constraining a prival, Return reference
298 -- to the discriminal of the protected body (which renames the parameter
299 -- of the enclosing protected operation). This clumsy transformation is
300 -- needed because privals are created too late and their actual subtypes
301 -- are not available when analysing the bodies of the protected operations.
0577b0b1 302 -- This function is called whenever the bound is an entity and the scope
303 -- indicates a protected operation. If the bound is an in-parameter of
304 -- a protected operation that is not a prival, the function returns the
305 -- bound itself.
ee6ba406 306 -- To be cleaned up???
307
308 function Guard_Access
309 (Cond : Node_Id;
310 Loc : Source_Ptr;
314a23b6 311 Ck_Node : Node_Id) return Node_Id;
ee6ba406 312 -- In the access type case, guard the test with a test to ensure
313 -- that the access value is non-null, since the checks do not
314 -- not apply to null access values.
315
316 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
317 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
318 -- Constraint_Error node.
319
3cce7f32 320 function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean;
321 -- Returns True if node N is for an arithmetic operation with signed
0326b4d4 322 -- integer operands. This includes unary and binary operators, and also
323 -- if and case expression nodes where the dependent expressions are of
324 -- a signed integer type. These are the kinds of nodes for which special
691fe9e0 325 -- handling applies in MINIMIZED or ELIMINATED overflow checking mode.
3cce7f32 326
0577b0b1 327 function Range_Or_Validity_Checks_Suppressed
328 (Expr : Node_Id) return Boolean;
329 -- Returns True if either range or validity checks or both are suppressed
330 -- for the type of the given expression, or, if the expression is the name
331 -- of an entity, if these checks are suppressed for the entity.
332
ee6ba406 333 function Selected_Length_Checks
334 (Ck_Node : Node_Id;
335 Target_Typ : Entity_Id;
336 Source_Typ : Entity_Id;
314a23b6 337 Warn_Node : Node_Id) return Check_Result;
ee6ba406 338 -- Like Apply_Selected_Length_Checks, except it doesn't modify
339 -- anything, just returns a list of nodes as described in the spec of
340 -- this package for the Range_Check function.
18cb6d78 341 -- ??? In fact it does construct the test and insert it into the tree,
342 -- and insert actions in various ways (calling Insert_Action directly
343 -- in particular) so we do not call it in GNATprove mode, contrary to
344 -- Selected_Range_Checks.
ee6ba406 345
346 function Selected_Range_Checks
347 (Ck_Node : Node_Id;
348 Target_Typ : Entity_Id;
349 Source_Typ : Entity_Id;
314a23b6 350 Warn_Node : Node_Id) return Check_Result;
ee6ba406 351 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
352 -- just returns a list of nodes as described in the spec of this package
353 -- for the Range_Check function.
354
355 ------------------------------
356 -- Access_Checks_Suppressed --
357 ------------------------------
358
359 function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
360 begin
9dfe12ae 361 if Present (E) and then Checks_May_Be_Suppressed (E) then
362 return Is_Check_Suppressed (E, Access_Check);
363 else
fafc6b97 364 return Scope_Suppress.Suppress (Access_Check);
9dfe12ae 365 end if;
ee6ba406 366 end Access_Checks_Suppressed;
367
368 -------------------------------------
369 -- Accessibility_Checks_Suppressed --
370 -------------------------------------
371
372 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
373 begin
9dfe12ae 374 if Present (E) and then Checks_May_Be_Suppressed (E) then
375 return Is_Check_Suppressed (E, Accessibility_Check);
376 else
fafc6b97 377 return Scope_Suppress.Suppress (Accessibility_Check);
9dfe12ae 378 end if;
ee6ba406 379 end Accessibility_Checks_Suppressed;
380
00c403ee 381 -----------------------------
382 -- Activate_Division_Check --
383 -----------------------------
384
385 procedure Activate_Division_Check (N : Node_Id) is
386 begin
387 Set_Do_Division_Check (N, True);
388 Possible_Local_Raise (N, Standard_Constraint_Error);
389 end Activate_Division_Check;
390
391 -----------------------------
392 -- Activate_Overflow_Check --
393 -----------------------------
394
395 procedure Activate_Overflow_Check (N : Node_Id) is
c8e92b5f 396 Typ : constant Entity_Id := Etype (N);
397
00c403ee 398 begin
c8e92b5f 399 -- Floating-point case. If Etype is not set (this can happen when we
400 -- activate a check on a node that has not yet been analyzed), then
401 -- we assume we do not have a floating-point type (as per our spec).
402
403 if Present (Typ) and then Is_Floating_Point_Type (Typ) then
404
405 -- Ignore call if we have no automatic overflow checks on the target
406 -- and Check_Float_Overflow mode is not set. These are the cases in
407 -- which we expect to generate infinities and NaN's with no check.
408
409 if not (Machine_Overflows_On_Target or Check_Float_Overflow) then
410 return;
411
412 -- Ignore for unary operations ("+", "-", abs) since these can never
413 -- result in overflow for floating-point cases.
b8446e0d 414
c8e92b5f 415 elsif Nkind (N) in N_Unary_Op then
416 return;
417
418 -- Otherwise we will set the flag
419
420 else
421 null;
422 end if;
423
424 -- Discrete case
425
426 else
427 -- Nothing to do for Rem/Mod/Plus (overflow not possible, the check
428 -- for zero-divide is a divide check, not an overflow check).
b8446e0d 429
c8e92b5f 430 if Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then
431 return;
432 end if;
b8446e0d 433 end if;
434
c8e92b5f 435 -- Fall through for cases where we do set the flag
b8446e0d 436
437 Set_Do_Overflow_Check (N, True);
438 Possible_Local_Raise (N, Standard_Constraint_Error);
00c403ee 439 end Activate_Overflow_Check;
440
441 --------------------------
442 -- Activate_Range_Check --
443 --------------------------
444
445 procedure Activate_Range_Check (N : Node_Id) is
446 begin
447 Set_Do_Range_Check (N, True);
448 Possible_Local_Raise (N, Standard_Constraint_Error);
449 end Activate_Range_Check;
450
0577b0b1 451 ---------------------------------
452 -- Alignment_Checks_Suppressed --
453 ---------------------------------
454
455 function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is
456 begin
457 if Present (E) and then Checks_May_Be_Suppressed (E) then
458 return Is_Check_Suppressed (E, Alignment_Check);
459 else
fafc6b97 460 return Scope_Suppress.Suppress (Alignment_Check);
0577b0b1 461 end if;
462 end Alignment_Checks_Suppressed;
463
2d70530c 464 ----------------------------------
465 -- Allocation_Checks_Suppressed --
466 ----------------------------------
467
fa771c05 468 -- Note: at the current time there are no calls to this function, because
469 -- the relevant check is in the run-time, so it is not a check that the
470 -- compiler can suppress anyway, but we still have to recognize the check
471 -- name Allocation_Check since it is part of the standard.
472
2d70530c 473 function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is
474 begin
475 if Present (E) and then Checks_May_Be_Suppressed (E) then
476 return Is_Check_Suppressed (E, Allocation_Check);
477 else
478 return Scope_Suppress.Suppress (Allocation_Check);
479 end if;
480 end Allocation_Checks_Suppressed;
481
ee6ba406 482 -------------------------
483 -- Append_Range_Checks --
484 -------------------------
485
486 procedure Append_Range_Checks
487 (Checks : Check_Result;
488 Stmts : List_Id;
489 Suppress_Typ : Entity_Id;
490 Static_Sloc : Source_Ptr;
491 Flag_Node : Node_Id)
492 is
2b4f2458 493 Checks_On : constant Boolean :=
494 not Index_Checks_Suppressed (Suppress_Typ)
495 or else
496 not Range_Checks_Suppressed (Suppress_Typ);
497
9dfe12ae 498 Internal_Flag_Node : constant Node_Id := Flag_Node;
499 Internal_Static_Sloc : constant Source_Ptr := Static_Sloc;
500
ee6ba406 501 begin
2b4f2458 502 -- For now we just return if Checks_On is false, however this should be
503 -- enhanced to check for an always True value in the condition and to
504 -- generate a compilation warning???
ee6ba406 505
506 if not Checks_On then
507 return;
508 end if;
509
510 for J in 1 .. 2 loop
511 exit when No (Checks (J));
512
513 if Nkind (Checks (J)) = N_Raise_Constraint_Error
514 and then Present (Condition (Checks (J)))
515 then
516 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
517 Append_To (Stmts, Checks (J));
518 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
519 end if;
520
521 else
522 Append_To
f15731c4 523 (Stmts,
524 Make_Raise_Constraint_Error (Internal_Static_Sloc,
525 Reason => CE_Range_Check_Failed));
ee6ba406 526 end if;
527 end loop;
528 end Append_Range_Checks;
529
530 ------------------------
531 -- Apply_Access_Check --
532 ------------------------
533
534 procedure Apply_Access_Check (N : Node_Id) is
535 P : constant Node_Id := Prefix (N);
536
537 begin
13dbf220 538 -- We do not need checks if we are not generating code (i.e. the
539 -- expander is not active). This is not just an optimization, there
540 -- are cases (e.g. with pragma Debug) where generating the checks
541 -- can cause real trouble).
284faf8b 542
a33565dd 543 if not Expander_Active then
13dbf220 544 return;
9dfe12ae 545 end if;
ee6ba406 546
84d0d4a5 547 -- No check if short circuiting makes check unnecessary
9dfe12ae 548
84d0d4a5 549 if not Check_Needed (P, Access_Check) then
550 return;
ee6ba406 551 end if;
9dfe12ae 552
cc60bd16 553 -- No check if accessing the Offset_To_Top component of a dispatch
554 -- table. They are safe by construction.
555
040277b1 556 if Tagged_Type_Expansion
557 and then Present (Etype (P))
cc60bd16 558 and then RTU_Loaded (Ada_Tags)
559 and then RTE_Available (RE_Offset_To_Top_Ptr)
560 and then Etype (P) = RTE (RE_Offset_To_Top_Ptr)
561 then
562 return;
563 end if;
564
84d0d4a5 565 -- Otherwise go ahead and install the check
9dfe12ae 566
fa7497e8 567 Install_Null_Excluding_Check (P);
ee6ba406 568 end Apply_Access_Check;
569
570 -------------------------------
571 -- Apply_Accessibility_Check --
572 -------------------------------
573
55dc6dc2 574 procedure Apply_Accessibility_Check
575 (N : Node_Id;
576 Typ : Entity_Id;
577 Insert_Node : Node_Id)
578 is
ee6ba406 579 Loc : constant Source_Ptr := Sloc (N);
1a9cc6cd 580 Param_Ent : Entity_Id := Param_Entity (N);
ee6ba406 581 Param_Level : Node_Id;
582 Type_Level : Node_Id;
583
584 begin
47d210a3 585 if Ada_Version >= Ada_2012
586 and then not Present (Param_Ent)
587 and then Is_Entity_Name (N)
588 and then Ekind_In (Entity (N), E_Constant, E_Variable)
589 and then Present (Effective_Extra_Accessibility (Entity (N)))
590 then
591 Param_Ent := Entity (N);
592 while Present (Renamed_Object (Param_Ent)) loop
1a9cc6cd 593
47d210a3 594 -- Renamed_Object must return an Entity_Name here
595 -- because of preceding "Present (E_E_A (...))" test.
596
597 Param_Ent := Entity (Renamed_Object (Param_Ent));
598 end loop;
599 end if;
600
ee6ba406 601 if Inside_A_Generic then
602 return;
603
6ffc64fc 604 -- Only apply the run-time check if the access parameter has an
605 -- associated extra access level parameter and when the level of the
606 -- type is less deep than the level of the access parameter, and
607 -- accessibility checks are not suppressed.
ee6ba406 608
609 elsif Present (Param_Ent)
610 and then Present (Extra_Accessibility (Param_Ent))
47d210a3 611 and then UI_Gt (Object_Access_Level (N),
1a9cc6cd 612 Deepest_Type_Access_Level (Typ))
ee6ba406 613 and then not Accessibility_Checks_Suppressed (Param_Ent)
614 and then not Accessibility_Checks_Suppressed (Typ)
615 then
616 Param_Level :=
617 New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
618
1a9cc6cd 619 Type_Level :=
620 Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ));
ee6ba406 621
bf3e1520 622 -- Raise Program_Error if the accessibility level of the access
84d0d4a5 623 -- parameter is deeper than the level of the target access type.
ee6ba406 624
55dc6dc2 625 Insert_Action (Insert_Node,
ee6ba406 626 Make_Raise_Program_Error (Loc,
627 Condition =>
628 Make_Op_Gt (Loc,
629 Left_Opnd => Param_Level,
f15731c4 630 Right_Opnd => Type_Level),
631 Reason => PE_Accessibility_Check_Failed));
ee6ba406 632
633 Analyze_And_Resolve (N);
634 end if;
635 end Apply_Accessibility_Check;
636
0577b0b1 637 --------------------------------
638 -- Apply_Address_Clause_Check --
639 --------------------------------
640
641 procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is
d950dc79 642 pragma Assert (Nkind (N) = N_Freeze_Entity);
643
8650387e 644 AC : constant Node_Id := Address_Clause (E);
645 Loc : constant Source_Ptr := Sloc (AC);
646 Typ : constant Entity_Id := Etype (E);
c2b56224 647
c2b56224 648 Expr : Node_Id;
0577b0b1 649 -- Address expression (not necessarily the same as Aexp, for example
650 -- when Aexp is a reference to a constant, in which case Expr gets
7b8fa048 651 -- reset to reference the value expression of the constant).
0577b0b1 652
c2b56224 653 begin
d6da7448 654 -- See if alignment check needed. Note that we never need a check if the
655 -- maximum alignment is one, since the check will always succeed.
656
657 -- Note: we do not check for checks suppressed here, since that check
658 -- was done in Sem_Ch13 when the address clause was processed. We are
659 -- only called if checks were not suppressed. The reason for this is
660 -- that we have to delay the call to Apply_Alignment_Check till freeze
661 -- time (so that all types etc are elaborated), but we have to check
662 -- the status of check suppressing at the point of the address clause.
663
664 if No (AC)
665 or else not Check_Address_Alignment (AC)
666 or else Maximum_Alignment = 1
667 then
668 return;
669 end if;
670
671 -- Obtain expression from address clause
9dfe12ae 672
514a5555 673 Expr := Address_Value (Expression (AC));
0577b0b1 674
514a5555 675 -- See if we know that Expr has an acceptable value at compile time. If
676 -- it hasn't or we don't know, we defer issuing the warning until the
677 -- end of the compilation to take into account back end annotations.
c2b56224 678
679 if Compile_Time_Known_Value (Expr)
f2a06be9 680 and then (Known_Alignment (E) or else Known_Alignment (Typ))
c2b56224 681 then
f2a06be9 682 declare
683 AL : Uint := Alignment (Typ);
684
685 begin
8650387e 686 -- The object alignment might be more restrictive than the type
687 -- alignment.
f2a06be9 688
689 if Known_Alignment (E) then
690 AL := Alignment (E);
691 end if;
692
514a5555 693 if Expr_Value (Expr) mod AL = 0 then
0577b0b1 694 return;
f2a06be9 695 end if;
696 end;
c2b56224 697
7161e166 698 -- If the expression has the form X'Address, then we can find out if the
699 -- object X has an alignment that is compatible with the object E. If it
700 -- hasn't or we don't know, we defer issuing the warning until the end
701 -- of the compilation to take into account back end annotations.
c2b56224 702
0577b0b1 703 elsif Nkind (Expr) = N_Attribute_Reference
704 and then Attribute_Name (Expr) = Name_Address
7161e166 705 and then
706 Has_Compatible_Alignment (E, Prefix (Expr), False) = Known_Compatible
0577b0b1 707 then
d6da7448 708 return;
0577b0b1 709 end if;
c2b56224 710
6fb3c314 711 -- Here we do not know if the value is acceptable. Strictly we don't
712 -- have to do anything, since if the alignment is bad, we have an
713 -- erroneous program. However we are allowed to check for erroneous
714 -- conditions and we decide to do this by default if the check is not
715 -- suppressed.
0577b0b1 716
717 -- However, don't do the check if elaboration code is unwanted
718
719 if Restriction_Active (No_Elaboration_Code) then
720 return;
721
722 -- Generate a check to raise PE if alignment may be inappropriate
723
724 else
8650387e 725 -- If the original expression is a non-static constant, use the name
726 -- of the constant itself rather than duplicating its initialization
727 -- expression, which was extracted above.
0577b0b1 728
00c403ee 729 -- Note: Expr is empty if the address-clause is applied to in-mode
730 -- actuals (allowed by 13.1(22)).
731
732 if not Present (Expr)
733 or else
734 (Is_Entity_Name (Expression (AC))
735 and then Ekind (Entity (Expression (AC))) = E_Constant
8650387e 736 and then Nkind (Parent (Entity (Expression (AC)))) =
737 N_Object_Declaration)
0577b0b1 738 then
739 Expr := New_Copy_Tree (Expression (AC));
740 else
741 Remove_Side_Effects (Expr);
c2b56224 742 end if;
c2b56224 743
d950dc79 744 if No (Actions (N)) then
745 Set_Actions (N, New_List);
746 end if;
747
748 Prepend_To (Actions (N),
0577b0b1 749 Make_Raise_Program_Error (Loc,
750 Condition =>
751 Make_Op_Ne (Loc,
8650387e 752 Left_Opnd =>
0577b0b1 753 Make_Op_Mod (Loc,
8650387e 754 Left_Opnd =>
0577b0b1 755 Unchecked_Convert_To
756 (RTE (RE_Integer_Address), Expr),
757 Right_Opnd =>
758 Make_Attribute_Reference (Loc,
d950dc79 759 Prefix => New_Occurrence_Of (E, Loc),
0577b0b1 760 Attribute_Name => Name_Alignment)),
761 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8650387e 762 Reason => PE_Misaligned_Address_Value));
7b8fa048 763
764 Warning_Msg := No_Error_Msg;
d950dc79 765 Analyze (First (Actions (N)), Suppress => All_Checks);
cd309f05 766
514a5555 767 -- If the above raise action generated a warning message (for example
7b8fa048 768 -- from Warn_On_Non_Local_Exception mode with the active restriction
769 -- No_Exception_Propagation).
770
771 if Warning_Msg /= No_Error_Msg then
8650387e 772
7b8fa048 773 -- If the expression has a known at compile time value, then
774 -- once we know the alignment of the type, we can check if the
775 -- exception will be raised or not, and if not, we don't need
776 -- the warning so we will kill the warning later on.
777
778 if Compile_Time_Known_Value (Expr) then
779 Alignment_Warnings.Append
780 ((E => E, A => Expr_Value (Expr), W => Warning_Msg));
cd309f05 781
8650387e 782 -- Add explanation of the warning generated by the check
783
784 else
514a5555 785 Error_Msg_N
8650387e 786 ("\address value may be incompatible with alignment of "
787 & "object?X?", AC);
514a5555 788 end if;
cd309f05 789 end if;
78be29d1 790
0577b0b1 791 return;
792 end if;
9dfe12ae 793
794 exception
8650387e 795
0577b0b1 796 -- If we have some missing run time component in configurable run time
797 -- mode then just skip the check (it is not required in any case).
798
9dfe12ae 799 when RE_Not_Available =>
800 return;
0577b0b1 801 end Apply_Address_Clause_Check;
c2b56224 802
ee6ba406 803 -------------------------------------
804 -- Apply_Arithmetic_Overflow_Check --
805 -------------------------------------
806
3cce7f32 807 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
808 begin
809 -- Use old routine in almost all cases (the only case we are treating
21a55437 810 -- specially is the case of a signed integer arithmetic op with the
0df9d43f 811 -- overflow checking mode set to MINIMIZED or ELIMINATED).
3cce7f32 812
0df9d43f 813 if Overflow_Check_Mode = Strict
3cce7f32 814 or else not Is_Signed_Integer_Arithmetic_Op (N)
815 then
0df9d43f 816 Apply_Arithmetic_Overflow_Strict (N);
3cce7f32 817
21a55437 818 -- Otherwise use the new routine for the case of a signed integer
819 -- arithmetic op, with Do_Overflow_Check set to True, and the checking
820 -- mode is MINIMIZED or ELIMINATED.
3cce7f32 821
822 else
823 Apply_Arithmetic_Overflow_Minimized_Eliminated (N);
824 end if;
825 end Apply_Arithmetic_Overflow_Check;
826
0df9d43f 827 --------------------------------------
828 -- Apply_Arithmetic_Overflow_Strict --
829 --------------------------------------
3cce7f32 830
fc1dbe36 831 -- This routine is called only if the type is an integer type and an
832 -- arithmetic overflow check may be needed for op (add, subtract, or
833 -- multiply). This check is performed if Backend_Overflow_Checks_On_Target
834 -- is not enabled and Do_Overflow_Check is set. In this case we expand the
f40f9731 835 -- operation into a more complex sequence of tests that ensures that
836 -- overflow is properly caught.
ee6ba406 837
0df9d43f 838 -- This is used in CHECKED modes. It is identical to the code for this
839 -- cases before the big overflow earthquake, thus ensuring that in this
840 -- modes we have compatible behavior (and reliability) to what was there
841 -- before. It is also called for types other than signed integers, and if
842 -- the Do_Overflow_Check flag is off.
3cce7f32 843
844 -- Note: we also call this routine if we decide in the MINIMIZED case
845 -- to give up and just generate an overflow check without any fuss.
846
0df9d43f 847 procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is
21a55437 848 Loc : constant Source_Ptr := Sloc (N);
849 Typ : constant Entity_Id := Etype (N);
850 Rtyp : constant Entity_Id := Root_Type (Typ);
ee6ba406 851
852 begin
0df9d43f 853 -- Nothing to do if Do_Overflow_Check not set or overflow checks
854 -- suppressed.
855
856 if not Do_Overflow_Check (N) then
857 return;
858 end if;
859
f40f9731 860 -- An interesting special case. If the arithmetic operation appears as
861 -- the operand of a type conversion:
862
863 -- type1 (x op y)
864
865 -- and all the following conditions apply:
866
867 -- arithmetic operation is for a signed integer type
868 -- target type type1 is a static integer subtype
869 -- range of x and y are both included in the range of type1
870 -- range of x op y is included in the range of type1
871 -- size of type1 is at least twice the result size of op
872
a36d34e4 873 -- then we don't do an overflow check in any case. Instead, we transform
f40f9731 874 -- the operation so that we end up with:
875
876 -- type1 (type1 (x) op type1 (y))
877
878 -- This avoids intermediate overflow before the conversion. It is
879 -- explicitly permitted by RM 3.5.4(24):
880
881 -- For the execution of a predefined operation of a signed integer
882 -- type, the implementation need not raise Constraint_Error if the
883 -- result is outside the base range of the type, so long as the
884 -- correct result is produced.
885
886 -- It's hard to imagine that any programmer counts on the exception
887 -- being raised in this case, and in any case it's wrong coding to
888 -- have this expectation, given the RM permission. Furthermore, other
889 -- Ada compilers do allow such out of range results.
890
891 -- Note that we do this transformation even if overflow checking is
892 -- off, since this is precisely about giving the "right" result and
893 -- avoiding the need for an overflow check.
894
8eb4a5eb 895 -- Note: this circuit is partially redundant with respect to the similar
896 -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals
897 -- with cases that do not come through here. We still need the following
898 -- processing even with the Exp_Ch4 code in place, since we want to be
899 -- sure not to generate the arithmetic overflow check in these cases
900 -- (Exp_Ch4 would have a hard time removing them once generated).
901
f40f9731 902 if Is_Signed_Integer_Type (Typ)
903 and then Nkind (Parent (N)) = N_Type_Conversion
ee6ba406 904 then
f32c377d 905 Conversion_Optimization : declare
f40f9731 906 Target_Type : constant Entity_Id :=
b6341c67 907 Base_Type (Entity (Subtype_Mark (Parent (N))));
f40f9731 908
909 Llo, Lhi : Uint;
910 Rlo, Rhi : Uint;
911 LOK, ROK : Boolean;
912
913 Vlo : Uint;
914 Vhi : Uint;
915 VOK : Boolean;
916
917 Tlo : Uint;
918 Thi : Uint;
919
920 begin
921 if Is_Integer_Type (Target_Type)
922 and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp)
923 then
924 Tlo := Expr_Value (Type_Low_Bound (Target_Type));
925 Thi := Expr_Value (Type_High_Bound (Target_Type));
926
9c486805 927 Determine_Range
928 (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True);
929 Determine_Range
930 (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True);
f40f9731 931
932 if (LOK and ROK)
933 and then Tlo <= Llo and then Lhi <= Thi
934 and then Tlo <= Rlo and then Rhi <= Thi
935 then
9c486805 936 Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True);
f40f9731 937
938 if VOK and then Tlo <= Vlo and then Vhi <= Thi then
939 Rewrite (Left_Opnd (N),
940 Make_Type_Conversion (Loc,
941 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
942 Expression => Relocate_Node (Left_Opnd (N))));
943
944 Rewrite (Right_Opnd (N),
945 Make_Type_Conversion (Loc,
946 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
947 Expression => Relocate_Node (Right_Opnd (N))));
948
780bfb21 949 -- Rewrite the conversion operand so that the original
950 -- node is retained, in order to avoid the warning for
951 -- redundant conversions in Resolve_Type_Conversion.
952
953 Rewrite (N, Relocate_Node (N));
954
f40f9731 955 Set_Etype (N, Target_Type);
780bfb21 956
f40f9731 957 Analyze_And_Resolve (Left_Opnd (N), Target_Type);
958 Analyze_And_Resolve (Right_Opnd (N), Target_Type);
959
960 -- Given that the target type is twice the size of the
961 -- source type, overflow is now impossible, so we can
962 -- safely kill the overflow check and return.
963
964 Set_Do_Overflow_Check (N, False);
965 return;
966 end if;
967 end if;
968 end if;
f32c377d 969 end Conversion_Optimization;
ee6ba406 970 end if;
971
f40f9731 972 -- Now see if an overflow check is required
973
974 declare
975 Siz : constant Int := UI_To_Int (Esize (Rtyp));
976 Dsiz : constant Int := Siz * 2;
977 Opnod : Node_Id;
978 Ctyp : Entity_Id;
979 Opnd : Node_Id;
980 Cent : RE_Id;
ee6ba406 981
f40f9731 982 begin
983 -- Skip check if back end does overflow checks, or the overflow flag
df40eeb0 984 -- is not set anyway, or we are not doing code expansion, or the
985 -- parent node is a type conversion whose operand is an arithmetic
986 -- operation on signed integers on which the expander can promote
bbbed24b 987 -- later the operands to type Integer (see Expand_N_Type_Conversion).
ee6ba406 988
f40f9731 989 if Backend_Overflow_Checks_On_Target
990 or else not Do_Overflow_Check (N)
a33565dd 991 or else not Expander_Active
df40eeb0 992 or else (Present (Parent (N))
993 and then Nkind (Parent (N)) = N_Type_Conversion
994 and then Integer_Promotion_Possible (Parent (N)))
f40f9731 995 then
996 return;
997 end if;
ee6ba406 998
f40f9731 999 -- Otherwise, generate the full general code for front end overflow
1000 -- detection, which works by doing arithmetic in a larger type:
ee6ba406 1001
f40f9731 1002 -- x op y
ee6ba406 1003
f40f9731 1004 -- is expanded into
ee6ba406 1005
f40f9731 1006 -- Typ (Checktyp (x) op Checktyp (y));
ee6ba406 1007
f40f9731 1008 -- where Typ is the type of the original expression, and Checktyp is
1009 -- an integer type of sufficient length to hold the largest possible
1010 -- result.
ee6ba406 1011
f40f9731 1012 -- If the size of check type exceeds the size of Long_Long_Integer,
1013 -- we use a different approach, expanding to:
ee6ba406 1014
f40f9731 1015 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
ee6ba406 1016
f40f9731 1017 -- where xxx is Add, Multiply or Subtract as appropriate
ee6ba406 1018
f40f9731 1019 -- Find check type if one exists
1020
1021 if Dsiz <= Standard_Integer_Size then
1022 Ctyp := Standard_Integer;
ee6ba406 1023
f40f9731 1024 elsif Dsiz <= Standard_Long_Long_Integer_Size then
1025 Ctyp := Standard_Long_Long_Integer;
1026
c9f84db7 1027 -- No check type exists, use runtime call
ee6ba406 1028
1029 else
f40f9731 1030 if Nkind (N) = N_Op_Add then
1031 Cent := RE_Add_With_Ovflo_Check;
ee6ba406 1032
f40f9731 1033 elsif Nkind (N) = N_Op_Multiply then
1034 Cent := RE_Multiply_With_Ovflo_Check;
ee6ba406 1035
f40f9731 1036 else
1037 pragma Assert (Nkind (N) = N_Op_Subtract);
1038 Cent := RE_Subtract_With_Ovflo_Check;
1039 end if;
1040
1041 Rewrite (N,
1042 OK_Convert_To (Typ,
1043 Make_Function_Call (Loc,
83c6c069 1044 Name => New_Occurrence_Of (RTE (Cent), Loc),
f40f9731 1045 Parameter_Associations => New_List (
1046 OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
1047 OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
ee6ba406 1048
f40f9731 1049 Analyze_And_Resolve (N, Typ);
1050 return;
1051 end if;
ee6ba406 1052
f40f9731 1053 -- If we fall through, we have the case where we do the arithmetic
1054 -- in the next higher type and get the check by conversion. In these
1055 -- cases Ctyp is set to the type to be used as the check type.
ee6ba406 1056
f40f9731 1057 Opnod := Relocate_Node (N);
ee6ba406 1058
f40f9731 1059 Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
ee6ba406 1060
f40f9731 1061 Analyze (Opnd);
1062 Set_Etype (Opnd, Ctyp);
1063 Set_Analyzed (Opnd, True);
1064 Set_Left_Opnd (Opnod, Opnd);
ee6ba406 1065
f40f9731 1066 Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
ee6ba406 1067
f40f9731 1068 Analyze (Opnd);
1069 Set_Etype (Opnd, Ctyp);
1070 Set_Analyzed (Opnd, True);
1071 Set_Right_Opnd (Opnod, Opnd);
ee6ba406 1072
f40f9731 1073 -- The type of the operation changes to the base type of the check
1074 -- type, and we reset the overflow check indication, since clearly no
1075 -- overflow is possible now that we are using a double length type.
1076 -- We also set the Analyzed flag to avoid a recursive attempt to
1077 -- expand the node.
ee6ba406 1078
f40f9731 1079 Set_Etype (Opnod, Base_Type (Ctyp));
1080 Set_Do_Overflow_Check (Opnod, False);
1081 Set_Analyzed (Opnod, True);
ee6ba406 1082
f40f9731 1083 -- Now build the outer conversion
ee6ba406 1084
f40f9731 1085 Opnd := OK_Convert_To (Typ, Opnod);
1086 Analyze (Opnd);
1087 Set_Etype (Opnd, Typ);
9dfe12ae 1088
f40f9731 1089 -- In the discrete type case, we directly generate the range check
1090 -- for the outer operand. This range check will implement the
1091 -- required overflow check.
9dfe12ae 1092
f40f9731 1093 if Is_Discrete_Type (Typ) then
1094 Rewrite (N, Opnd);
1095 Generate_Range_Check
1096 (Expression (N), Typ, CE_Overflow_Check_Failed);
9dfe12ae 1097
f40f9731 1098 -- For other types, we enable overflow checking on the conversion,
1099 -- after setting the node as analyzed to prevent recursive attempts
1100 -- to expand the conversion node.
9dfe12ae 1101
f40f9731 1102 else
1103 Set_Analyzed (Opnd, True);
1104 Enable_Overflow_Check (Opnd);
1105 Rewrite (N, Opnd);
1106 end if;
1107
1108 exception
1109 when RE_Not_Available =>
1110 return;
1111 end;
0df9d43f 1112 end Apply_Arithmetic_Overflow_Strict;
3cce7f32 1113
1114 ----------------------------------------------------
1115 -- Apply_Arithmetic_Overflow_Minimized_Eliminated --
1116 ----------------------------------------------------
1117
1118 procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is
1119 pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op));
3cce7f32 1120
1121 Loc : constant Source_Ptr := Sloc (Op);
1122 P : constant Node_Id := Parent (Op);
1123
49b3a812 1124 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
1125 -- Operands and results are of this type when we convert
1126
3cce7f32 1127 Result_Type : constant Entity_Id := Etype (Op);
1128 -- Original result type
1129
db415383 1130 Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode;
3cce7f32 1131 pragma Assert (Check_Mode in Minimized_Or_Eliminated);
1132
1133 Lo, Hi : Uint;
1134 -- Ranges of values for result
1135
1136 begin
1137 -- Nothing to do if our parent is one of the following:
1138
0326b4d4 1139 -- Another signed integer arithmetic op
3cce7f32 1140 -- A membership operation
1141 -- A comparison operation
1142
1143 -- In all these cases, we will process at the higher level (and then
1144 -- this node will be processed during the downwards recursion that
0df9d43f 1145 -- is part of the processing in Minimize_Eliminate_Overflows).
3cce7f32 1146
1147 if Is_Signed_Integer_Arithmetic_Op (P)
b8a17a21 1148 or else Nkind (P) in N_Membership_Test
1149 or else Nkind (P) in N_Op_Compare
aa4b16cb 1150
70a2dff4 1151 -- This is also true for an alternative in a case expression
1152
1153 or else Nkind (P) = N_Case_Expression_Alternative
1154
1155 -- This is also true for a range operand in a membership test
aa4b16cb 1156
b8a17a21 1157 or else (Nkind (P) = N_Range
1158 and then Nkind (Parent (P)) in N_Membership_Test)
3cce7f32 1159 then
af90720d 1160 -- If_Expressions and Case_Expressions are treated as arithmetic
1161 -- ops, but if they appear in an assignment or similar contexts
1162 -- there is no overflow check that starts from that parent node,
1163 -- so apply check now.
1164
1165 if Nkind_In (P, N_If_Expression, N_Case_Expression)
1166 and then not Is_Signed_Integer_Arithmetic_Op (Parent (P))
1167 then
1168 null;
1169 else
1170 return;
1171 end if;
3cce7f32 1172 end if;
1173
0326b4d4 1174 -- Otherwise, we have a top level arithmetic operation node, and this
21a55437 1175 -- is where we commence the special processing for MINIMIZED/ELIMINATED
1176 -- modes. This is the case where we tell the machinery not to move into
1177 -- Bignum mode at this top level (of course the top level operation
1178 -- will still be in Bignum mode if either of its operands are of type
1179 -- Bignum).
3cce7f32 1180
0df9d43f 1181 Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True);
3cce7f32 1182
1183 -- That call may but does not necessarily change the result type of Op.
1184 -- It is the job of this routine to undo such changes, so that at the
1185 -- top level, we have the proper type. This "undoing" is a point at
1186 -- which a final overflow check may be applied.
1187
f32c377d 1188 -- If the result type was not fiddled we are all set. We go to base
1189 -- types here because things may have been rewritten to generate the
1190 -- base type of the operand types.
3cce7f32 1191
f32c377d 1192 if Base_Type (Etype (Op)) = Base_Type (Result_Type) then
3cce7f32 1193 return;
1194
1195 -- Bignum case
1196
49b3a812 1197 elsif Is_RTE (Etype (Op), RE_Bignum) then
3cce7f32 1198
d94b5da2 1199 -- We need a sequence that looks like:
3cce7f32 1200
1201 -- Rnn : Result_Type;
1202
1203 -- declare
d94b5da2 1204 -- M : Mark_Id := SS_Mark;
3cce7f32 1205 -- begin
49b3a812 1206 -- Rnn := Long_Long_Integer'Base (From_Bignum (Op));
3cce7f32 1207 -- SS_Release (M);
1208 -- end;
1209
1210 -- This block is inserted (using Insert_Actions), and then the node
1211 -- is replaced with a reference to Rnn.
1212
75b45a21 1213 -- If our parent is a conversion node then there is no point in
a36d34e4 1214 -- generating a conversion to Result_Type. Instead, we let the parent
75b45a21 1215 -- handle this. Note that this special case is not just about
1216 -- optimization. Consider
3cce7f32 1217
1218 -- A,B,C : Integer;
1219 -- ...
49b3a812 1220 -- X := Long_Long_Integer'Base (A * (B ** C));
3cce7f32 1221
1222 -- Now the product may fit in Long_Long_Integer but not in Integer.
21a55437 1223 -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an
1224 -- overflow exception for this intermediate value.
3cce7f32 1225
1226 declare
49b3a812 1227 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3cce7f32 1228 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op);
1229 RHS : Node_Id;
1230
1231 Rtype : Entity_Id;
1232
1233 begin
1234 RHS := Convert_From_Bignum (Op);
1235
1236 if Nkind (P) /= N_Type_Conversion then
49b3a812 1237 Convert_To_And_Rewrite (Result_Type, RHS);
3cce7f32 1238 Rtype := Result_Type;
1239
1240 -- Interesting question, do we need a check on that conversion
1241 -- operation. Answer, not if we know the result is in range.
1242 -- At the moment we are not taking advantage of this. To be
1243 -- looked at later ???
1244
1245 else
49b3a812 1246 Rtype := LLIB;
3cce7f32 1247 end if;
1248
1249 Insert_Before
1250 (First (Statements (Handled_Statement_Sequence (Blk))),
1251 Make_Assignment_Statement (Loc,
1252 Name => New_Occurrence_Of (Rnn, Loc),
1253 Expression => RHS));
1254
1255 Insert_Actions (Op, New_List (
1256 Make_Object_Declaration (Loc,
1257 Defining_Identifier => Rnn,
1258 Object_Definition => New_Occurrence_Of (Rtype, Loc)),
1259 Blk));
1260
1261 Rewrite (Op, New_Occurrence_Of (Rnn, Loc));
1262 Analyze_And_Resolve (Op);
1263 end;
1264
af90720d 1265 -- Here we know the result is Long_Long_Integer'Base, or that it has
412f75eb 1266 -- been rewritten because the parent operation is a conversion. See
0df9d43f 1267 -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization.
3cce7f32 1268
1269 else
f32c377d 1270 pragma Assert
1271 (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion);
3cce7f32 1272
1273 -- All we need to do here is to convert the result to the proper
1274 -- result type. As explained above for the Bignum case, we can
1275 -- omit this if our parent is a type conversion.
1276
1277 if Nkind (P) /= N_Type_Conversion then
1278 Convert_To_And_Rewrite (Result_Type, Op);
1279 end if;
1280
1281 Analyze_And_Resolve (Op);
1282 end if;
1283 end Apply_Arithmetic_Overflow_Minimized_Eliminated;
ee6ba406 1284
ee6ba406 1285 ----------------------------
1286 -- Apply_Constraint_Check --
1287 ----------------------------
1288
1289 procedure Apply_Constraint_Check
1290 (N : Node_Id;
1291 Typ : Entity_Id;
1292 No_Sliding : Boolean := False)
1293 is
1294 Desig_Typ : Entity_Id;
1295
1296 begin
7aafae1c 1297 -- No checks inside a generic (check the instantiations)
1298
ee6ba406 1299 if Inside_A_Generic then
1300 return;
7aafae1c 1301 end if;
ee6ba406 1302
6fb3c314 1303 -- Apply required constraint checks
7aafae1c 1304
1305 if Is_Scalar_Type (Typ) then
ee6ba406 1306 Apply_Scalar_Range_Check (N, Typ);
1307
1308 elsif Is_Array_Type (Typ) then
1309
05fcfafb 1310 -- A useful optimization: an aggregate with only an others clause
5f260d20 1311 -- always has the right bounds.
1312
1313 if Nkind (N) = N_Aggregate
1314 and then No (Expressions (N))
1315 and then Nkind
1316 (First (Choices (First (Component_Associations (N)))))
1317 = N_Others_Choice
1318 then
1319 return;
1320 end if;
1321
ee6ba406 1322 if Is_Constrained (Typ) then
1323 Apply_Length_Check (N, Typ);
1324
1325 if No_Sliding then
1326 Apply_Range_Check (N, Typ);
1327 end if;
1328 else
1329 Apply_Range_Check (N, Typ);
1330 end if;
1331
4fb5f0a0 1332 elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ))
ee6ba406 1333 and then Has_Discriminants (Base_Type (Typ))
1334 and then Is_Constrained (Typ)
1335 then
1336 Apply_Discriminant_Check (N, Typ);
1337
1338 elsif Is_Access_Type (Typ) then
1339
1340 Desig_Typ := Designated_Type (Typ);
1341
1342 -- No checks necessary if expression statically null
1343
2af58f67 1344 if Known_Null (N) then
00c403ee 1345 if Can_Never_Be_Null (Typ) then
1346 Install_Null_Excluding_Check (N);
1347 end if;
ee6ba406 1348
1349 -- No sliding possible on access to arrays
1350
1351 elsif Is_Array_Type (Desig_Typ) then
1352 if Is_Constrained (Desig_Typ) then
1353 Apply_Length_Check (N, Typ);
1354 end if;
1355
1356 Apply_Range_Check (N, Typ);
1357
a63a0aad 1358 -- Do not install a discriminant check for a constrained subtype
1359 -- created for an unconstrained nominal type because the subtype
1360 -- has the correct constraints by construction.
1361
ee6ba406 1362 elsif Has_Discriminants (Base_Type (Desig_Typ))
a63a0aad 1363 and then Is_Constrained (Desig_Typ)
1364 and then not Is_Constr_Subt_For_U_Nominal (Desig_Typ)
ee6ba406 1365 then
1366 Apply_Discriminant_Check (N, Typ);
1367 end if;
fa7497e8 1368
bf3e1520 1369 -- Apply the 2005 Null_Excluding check. Note that we do not apply
00c403ee 1370 -- this check if the constraint node is illegal, as shown by having
1371 -- an error posted. This additional guard prevents cascaded errors
1372 -- and compiler aborts on illegal programs involving Ada 2005 checks.
1373
fa7497e8 1374 if Can_Never_Be_Null (Typ)
1375 and then not Can_Never_Be_Null (Etype (N))
00c403ee 1376 and then not Error_Posted (N)
fa7497e8 1377 then
1378 Install_Null_Excluding_Check (N);
1379 end if;
ee6ba406 1380 end if;
1381 end Apply_Constraint_Check;
1382
1383 ------------------------------
1384 -- Apply_Discriminant_Check --
1385 ------------------------------
1386
1387 procedure Apply_Discriminant_Check
1388 (N : Node_Id;
1389 Typ : Entity_Id;
1390 Lhs : Node_Id := Empty)
1391 is
1392 Loc : constant Source_Ptr := Sloc (N);
1393 Do_Access : constant Boolean := Is_Access_Type (Typ);
1394 S_Typ : Entity_Id := Etype (N);
1395 Cond : Node_Id;
1396 T_Typ : Entity_Id;
1397
7be5088a 1398 function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean;
1399 -- A heap object with an indefinite subtype is constrained by its
1400 -- initial value, and assigning to it requires a constraint_check.
1401 -- The target may be an explicit dereference, or a renaming of one.
1402
ee6ba406 1403 function Is_Aliased_Unconstrained_Component return Boolean;
1404 -- It is possible for an aliased component to have a nominal
1405 -- unconstrained subtype (through instantiation). If this is a
1406 -- discriminated component assigned in the expansion of an aggregate
1407 -- in an initialization, the check must be suppressed. This unusual
2af58f67 1408 -- situation requires a predicate of its own.
ee6ba406 1409
7be5088a 1410 ----------------------------------
1411 -- Denotes_Explicit_Dereference --
1412 ----------------------------------
1413
1414 function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is
1415 begin
1416 return
1417 Nkind (Obj) = N_Explicit_Dereference
1418 or else
1419 (Is_Entity_Name (Obj)
1420 and then Present (Renamed_Object (Entity (Obj)))
9474aa9c 1421 and then Nkind (Renamed_Object (Entity (Obj))) =
1422 N_Explicit_Dereference);
7be5088a 1423 end Denotes_Explicit_Dereference;
1424
ee6ba406 1425 ----------------------------------------
1426 -- Is_Aliased_Unconstrained_Component --
1427 ----------------------------------------
1428
1429 function Is_Aliased_Unconstrained_Component return Boolean is
1430 Comp : Entity_Id;
1431 Pref : Node_Id;
1432
1433 begin
1434 if Nkind (Lhs) /= N_Selected_Component then
1435 return False;
1436 else
1437 Comp := Entity (Selector_Name (Lhs));
1438 Pref := Prefix (Lhs);
1439 end if;
1440
1441 if Ekind (Comp) /= E_Component
1442 or else not Is_Aliased (Comp)
1443 then
1444 return False;
1445 end if;
1446
1447 return not Comes_From_Source (Pref)
1448 and then In_Instance
1449 and then not Is_Constrained (Etype (Comp));
1450 end Is_Aliased_Unconstrained_Component;
1451
1452 -- Start of processing for Apply_Discriminant_Check
1453
1454 begin
1455 if Do_Access then
1456 T_Typ := Designated_Type (Typ);
1457 else
1458 T_Typ := Typ;
1459 end if;
1460
f0d65dae 1461 -- Only apply checks when generating code and discriminant checks are
1462 -- not suppressed. In GNATprove mode, we do not apply the checks, but we
1463 -- still analyze the expression to possibly issue errors on SPARK code
1464 -- when a run-time error can be detected at compile time.
1465
1466 if not GNATprove_Mode then
1467 if not Expander_Active
1468 or else Discriminant_Checks_Suppressed (T_Typ)
1469 then
1470 return;
1471 end if;
ee6ba406 1472 end if;
1473
feff2f05 1474 -- No discriminant checks necessary for an access when expression is
1475 -- statically Null. This is not only an optimization, it is fundamental
1476 -- because otherwise discriminant checks may be generated in init procs
1477 -- for types containing an access to a not-yet-frozen record, causing a
1478 -- deadly forward reference.
ee6ba406 1479
feff2f05 1480 -- Also, if the expression is of an access type whose designated type is
1481 -- incomplete, then the access value must be null and we suppress the
1482 -- check.
ee6ba406 1483
2af58f67 1484 if Known_Null (N) then
ee6ba406 1485 return;
1486
1487 elsif Is_Access_Type (S_Typ) then
1488 S_Typ := Designated_Type (S_Typ);
1489
1490 if Ekind (S_Typ) = E_Incomplete_Type then
1491 return;
1492 end if;
1493 end if;
1494
0577b0b1 1495 -- If an assignment target is present, then we need to generate the
1496 -- actual subtype if the target is a parameter or aliased object with
1497 -- an unconstrained nominal subtype.
1498
1499 -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual
1500 -- subtype to the parameter and dereference cases, since other aliased
1501 -- objects are unconstrained (unless the nominal subtype is explicitly
7be5088a 1502 -- constrained).
ee6ba406 1503
1504 if Present (Lhs)
1505 and then (Present (Param_Entity (Lhs))
de54c5ab 1506 or else (Ada_Version < Ada_2005
0577b0b1 1507 and then not Is_Constrained (T_Typ)
ee6ba406 1508 and then Is_Aliased_View (Lhs)
0577b0b1 1509 and then not Is_Aliased_Unconstrained_Component)
de54c5ab 1510 or else (Ada_Version >= Ada_2005
0577b0b1 1511 and then not Is_Constrained (T_Typ)
7be5088a 1512 and then Denotes_Explicit_Dereference (Lhs)
0577b0b1 1513 and then Nkind (Original_Node (Lhs)) /=
1514 N_Function_Call))
ee6ba406 1515 then
1516 T_Typ := Get_Actual_Subtype (Lhs);
1517 end if;
1518
feff2f05 1519 -- Nothing to do if the type is unconstrained (this is the case where
1520 -- the actual subtype in the RM sense of N is unconstrained and no check
1521 -- is required).
ee6ba406 1522
1523 if not Is_Constrained (T_Typ) then
1524 return;
05fcfafb 1525
1526 -- Ada 2005: nothing to do if the type is one for which there is a
1527 -- partial view that is constrained.
1528
de54c5ab 1529 elsif Ada_Version >= Ada_2005
0d78d2d4 1530 and then Object_Type_Has_Constrained_Partial_View
d41a3f41 1531 (Typ => Base_Type (T_Typ),
1532 Scop => Current_Scope)
05fcfafb 1533 then
1534 return;
ee6ba406 1535 end if;
1536
00f91aef 1537 -- Nothing to do if the type is an Unchecked_Union
1538
1539 if Is_Unchecked_Union (Base_Type (T_Typ)) then
1540 return;
1541 end if;
1542
8d11916f 1543 -- Suppress checks if the subtypes are the same. The check must be
feff2f05 1544 -- preserved in an assignment to a formal, because the constraint is
1545 -- given by the actual.
ee6ba406 1546
1547 if Nkind (Original_Node (N)) /= N_Allocator
1548 and then (No (Lhs)
8143bf7c 1549 or else not Is_Entity_Name (Lhs)
1550 or else No (Param_Entity (Lhs)))
ee6ba406 1551 then
1552 if (Etype (N) = Typ
1553 or else (Do_Access and then Designated_Type (Typ) = S_Typ))
1554 and then not Is_Aliased_View (Lhs)
1555 then
1556 return;
1557 end if;
1558
feff2f05 1559 -- We can also eliminate checks on allocators with a subtype mark that
1560 -- coincides with the context type. The context type may be a subtype
1561 -- without a constraint (common case, a generic actual).
ee6ba406 1562
1563 elsif Nkind (Original_Node (N)) = N_Allocator
1564 and then Is_Entity_Name (Expression (Original_Node (N)))
1565 then
1566 declare
9dfe12ae 1567 Alloc_Typ : constant Entity_Id :=
b6341c67 1568 Entity (Expression (Original_Node (N)));
ee6ba406 1569
1570 begin
1571 if Alloc_Typ = T_Typ
1572 or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
1573 and then Is_Entity_Name (
1574 Subtype_Indication (Parent (T_Typ)))
1575 and then Alloc_Typ = Base_Type (T_Typ))
1576
1577 then
1578 return;
1579 end if;
1580 end;
1581 end if;
1582
feff2f05 1583 -- See if we have a case where the types are both constrained, and all
1584 -- the constraints are constants. In this case, we can do the check
1585 -- successfully at compile time.
ee6ba406 1586
8d11916f 1587 -- We skip this check for the case where the node is rewritten as
d7ec9a29 1588 -- an allocator, because it already carries the context subtype,
1589 -- and extracting the discriminants from the aggregate is messy.
ee6ba406 1590
1591 if Is_Constrained (S_Typ)
1592 and then Nkind (Original_Node (N)) /= N_Allocator
1593 then
1594 declare
1595 DconT : Elmt_Id;
1596 Discr : Entity_Id;
1597 DconS : Elmt_Id;
1598 ItemS : Node_Id;
1599 ItemT : Node_Id;
1600
1601 begin
1602 -- S_Typ may not have discriminants in the case where it is a
feff2f05 1603 -- private type completed by a default discriminated type. In that
8d11916f 1604 -- case, we need to get the constraints from the underlying type.
feff2f05 1605 -- If the underlying type is unconstrained (i.e. has no default
1606 -- discriminants) no check is needed.
ee6ba406 1607
1608 if Has_Discriminants (S_Typ) then
1609 Discr := First_Discriminant (S_Typ);
1610 DconS := First_Elmt (Discriminant_Constraint (S_Typ));
1611
1612 else
1613 Discr := First_Discriminant (Underlying_Type (S_Typ));
1614 DconS :=
1615 First_Elmt
1616 (Discriminant_Constraint (Underlying_Type (S_Typ)));
1617
1618 if No (DconS) then
1619 return;
1620 end if;
fccb5da7 1621
1622 -- A further optimization: if T_Typ is derived from S_Typ
1623 -- without imposing a constraint, no check is needed.
1624
1625 if Nkind (Original_Node (Parent (T_Typ))) =
1626 N_Full_Type_Declaration
1627 then
1628 declare
5c61a0ff 1629 Type_Def : constant Node_Id :=
b6341c67 1630 Type_Definition (Original_Node (Parent (T_Typ)));
fccb5da7 1631 begin
1632 if Nkind (Type_Def) = N_Derived_Type_Definition
1633 and then Is_Entity_Name (Subtype_Indication (Type_Def))
1634 and then Entity (Subtype_Indication (Type_Def)) = S_Typ
1635 then
1636 return;
1637 end if;
1638 end;
1639 end if;
ee6ba406 1640 end if;
1641
86594966 1642 -- Constraint may appear in full view of type
1643
1644 if Ekind (T_Typ) = E_Private_Subtype
1645 and then Present (Full_View (T_Typ))
1646 then
d7ec9a29 1647 DconT :=
86594966 1648 First_Elmt (Discriminant_Constraint (Full_View (T_Typ)));
86594966 1649 else
d7ec9a29 1650 DconT :=
1651 First_Elmt (Discriminant_Constraint (T_Typ));
86594966 1652 end if;
ee6ba406 1653
1654 while Present (Discr) loop
1655 ItemS := Node (DconS);
1656 ItemT := Node (DconT);
1657
00c403ee 1658 -- For a discriminated component type constrained by the
1659 -- current instance of an enclosing type, there is no
1660 -- applicable discriminant check.
1661
1662 if Nkind (ItemT) = N_Attribute_Reference
1663 and then Is_Access_Type (Etype (ItemT))
1664 and then Is_Entity_Name (Prefix (ItemT))
1665 and then Is_Type (Entity (Prefix (ItemT)))
1666 then
1667 return;
1668 end if;
1669
cc60bd16 1670 -- If the expressions for the discriminants are identical
1671 -- and it is side-effect free (for now just an entity),
1672 -- this may be a shared constraint, e.g. from a subtype
1673 -- without a constraint introduced as a generic actual.
1674 -- Examine other discriminants if any.
1675
1676 if ItemS = ItemT
1677 and then Is_Entity_Name (ItemS)
1678 then
1679 null;
1680
1681 elsif not Is_OK_Static_Expression (ItemS)
1682 or else not Is_OK_Static_Expression (ItemT)
1683 then
1684 exit;
ee6ba406 1685
cc60bd16 1686 elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then
ee6ba406 1687 if Do_Access then -- needs run-time check.
1688 exit;
1689 else
1690 Apply_Compile_Time_Constraint_Error
cb97ae5c 1691 (N, "incorrect value for discriminant&??",
f15731c4 1692 CE_Discriminant_Check_Failed, Ent => Discr);
ee6ba406 1693 return;
1694 end if;
1695 end if;
1696
1697 Next_Elmt (DconS);
1698 Next_Elmt (DconT);
1699 Next_Discriminant (Discr);
1700 end loop;
1701
1702 if No (Discr) then
1703 return;
1704 end if;
1705 end;
1706 end if;
1707
f0d65dae 1708 -- In GNATprove mode, we do not apply the checks
1709
1710 if GNATprove_Mode then
1711 return;
1712 end if;
1713
ee6ba406 1714 -- Here we need a discriminant check. First build the expression
1715 -- for the comparisons of the discriminants:
1716
1717 -- (n.disc1 /= typ.disc1) or else
1718 -- (n.disc2 /= typ.disc2) or else
1719 -- ...
1720 -- (n.discn /= typ.discn)
1721
1722 Cond := Build_Discriminant_Checks (N, T_Typ);
1723
3cce7f32 1724 -- If Lhs is set and is a parameter, then the condition is guarded by:
1725 -- lhs'constrained and then (condition built above)
ee6ba406 1726
1727 if Present (Param_Entity (Lhs)) then
1728 Cond :=
1729 Make_And_Then (Loc,
1730 Left_Opnd =>
1731 Make_Attribute_Reference (Loc,
1732 Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
1733 Attribute_Name => Name_Constrained),
1734 Right_Opnd => Cond);
1735 end if;
1736
1737 if Do_Access then
1738 Cond := Guard_Access (Cond, Loc, N);
1739 end if;
1740
1741 Insert_Action (N,
f15731c4 1742 Make_Raise_Constraint_Error (Loc,
1743 Condition => Cond,
1744 Reason => CE_Discriminant_Check_Failed));
ee6ba406 1745 end Apply_Discriminant_Check;
1746
2fe22c69 1747 -------------------------
1748 -- Apply_Divide_Checks --
1749 -------------------------
ee6ba406 1750
2fe22c69 1751 procedure Apply_Divide_Checks (N : Node_Id) is
ee6ba406 1752 Loc : constant Source_Ptr := Sloc (N);
1753 Typ : constant Entity_Id := Etype (N);
1754 Left : constant Node_Id := Left_Opnd (N);
1755 Right : constant Node_Id := Right_Opnd (N);
1756
db415383 1757 Mode : constant Overflow_Mode_Type := Overflow_Check_Mode;
2fe22c69 1758 -- Current overflow checking mode
1759
ee6ba406 1760 LLB : Uint;
1761 Llo : Uint;
1762 Lhi : Uint;
1763 LOK : Boolean;
1764 Rlo : Uint;
1765 Rhi : Uint;
2fe22c69 1766 ROK : Boolean;
96da3284 1767
1768 pragma Warnings (Off, Lhi);
1769 -- Don't actually use this value
ee6ba406 1770
1771 begin
0df9d43f 1772 -- If we are operating in MINIMIZED or ELIMINATED mode, and we are
1773 -- operating on signed integer types, then the only thing this routine
1774 -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That
1775 -- procedure will (possibly later on during recursive downward calls),
1776 -- ensure that any needed overflow/division checks are properly applied.
2fe22c69 1777
1778 if Mode in Minimized_Or_Eliminated
2fe22c69 1779 and then Is_Signed_Integer_Type (Typ)
1780 then
1781 Apply_Arithmetic_Overflow_Minimized_Eliminated (N);
1782 return;
1783 end if;
1784
1785 -- Proceed here in SUPPRESSED or CHECKED modes
1786
a33565dd 1787 if Expander_Active
13dbf220 1788 and then not Backend_Divide_Checks_On_Target
1789 and then Check_Needed (Right, Division_Check)
ee6ba406 1790 then
9c486805 1791 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
ee6ba406 1792
2fe22c69 1793 -- Deal with division check
ee6ba406 1794
2fe22c69 1795 if Do_Division_Check (N)
1796 and then not Division_Checks_Suppressed (Typ)
1797 then
1798 Apply_Division_Check (N, Rlo, Rhi, ROK);
ee6ba406 1799 end if;
1800
2fe22c69 1801 -- Deal with overflow check
1802
0df9d43f 1803 if Do_Overflow_Check (N)
1804 and then not Overflow_Checks_Suppressed (Etype (N))
1805 then
f3ccbbb3 1806 Set_Do_Overflow_Check (N, False);
1807
2fe22c69 1808 -- Test for extremely annoying case of xxx'First divided by -1
1809 -- for division of signed integer types (only overflow case).
ee6ba406 1810
ee6ba406 1811 if Nkind (N) = N_Op_Divide
1812 and then Is_Signed_Integer_Type (Typ)
1813 then
9c486805 1814 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
ee6ba406 1815 LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
1816
1817 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
2fe22c69 1818 and then
1819 ((not LOK) or else (Llo = LLB))
ee6ba406 1820 then
f72e7b2a 1821 -- Ensure that expressions are not evaluated twice (once
1822 -- for their runtime checks and once for their regular
1823 -- computation).
1824
1825 Force_Evaluation (Left, Mode => Strict);
1826 Force_Evaluation (Right, Mode => Strict);
1827
ee6ba406 1828 Insert_Action (N,
1829 Make_Raise_Constraint_Error (Loc,
1830 Condition =>
1831 Make_And_Then (Loc,
2fe22c69 1832 Left_Opnd =>
1833 Make_Op_Eq (Loc,
1834 Left_Opnd =>
1835 Duplicate_Subexpr_Move_Checks (Left),
1836 Right_Opnd => Make_Integer_Literal (Loc, LLB)),
ee6ba406 1837
2fe22c69 1838 Right_Opnd =>
1839 Make_Op_Eq (Loc,
1840 Left_Opnd => Duplicate_Subexpr (Right),
1841 Right_Opnd => Make_Integer_Literal (Loc, -1))),
ee6ba406 1842
f15731c4 1843 Reason => CE_Overflow_Check_Failed));
ee6ba406 1844 end if;
1845 end if;
1846 end if;
1847 end if;
2fe22c69 1848 end Apply_Divide_Checks;
1849
1850 --------------------------
1851 -- Apply_Division_Check --
1852 --------------------------
1853
1854 procedure Apply_Division_Check
1855 (N : Node_Id;
1856 Rlo : Uint;
1857 Rhi : Uint;
1858 ROK : Boolean)
1859 is
1860 pragma Assert (Do_Division_Check (N));
1861
1862 Loc : constant Source_Ptr := Sloc (N);
1863 Right : constant Node_Id := Right_Opnd (N);
1864
1865 begin
a33565dd 1866 if Expander_Active
2fe22c69 1867 and then not Backend_Divide_Checks_On_Target
1868 and then Check_Needed (Right, Division_Check)
1869 then
1870 -- See if division by zero possible, and if so generate test. This
1871 -- part of the test is not controlled by the -gnato switch, since
1872 -- it is a Division_Check and not an Overflow_Check.
1873
1874 if Do_Division_Check (N) then
f3ccbbb3 1875 Set_Do_Division_Check (N, False);
1876
2fe22c69 1877 if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
1878 Insert_Action (N,
1879 Make_Raise_Constraint_Error (Loc,
1880 Condition =>
1881 Make_Op_Eq (Loc,
1882 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
1883 Right_Opnd => Make_Integer_Literal (Loc, 0)),
1884 Reason => CE_Divide_By_Zero));
1885 end if;
1886 end if;
1887 end if;
1888 end Apply_Division_Check;
ee6ba406 1889
5329ca64 1890 ----------------------------------
1891 -- Apply_Float_Conversion_Check --
1892 ----------------------------------
1893
feff2f05 1894 -- Let F and I be the source and target types of the conversion. The RM
1895 -- specifies that a floating-point value X is rounded to the nearest
1896 -- integer, with halfway cases being rounded away from zero. The rounded
1897 -- value of X is checked against I'Range.
1898
1899 -- The catch in the above paragraph is that there is no good way to know
1900 -- whether the round-to-integer operation resulted in overflow. A remedy is
1901 -- to perform a range check in the floating-point domain instead, however:
5329ca64 1902
5329ca64 1903 -- (1) The bounds may not be known at compile time
2af58f67 1904 -- (2) The check must take into account rounding or truncation.
5329ca64 1905 -- (3) The range of type I may not be exactly representable in F.
2af58f67 1906 -- (4) For the rounding case, The end-points I'First - 0.5 and
1907 -- I'Last + 0.5 may or may not be in range, depending on the
1908 -- sign of I'First and I'Last.
5329ca64 1909 -- (5) X may be a NaN, which will fail any comparison
1910
2af58f67 1911 -- The following steps correctly convert X with rounding:
feff2f05 1912
5329ca64 1913 -- (1) If either I'First or I'Last is not known at compile time, use
1914 -- I'Base instead of I in the next three steps and perform a
1915 -- regular range check against I'Range after conversion.
1916 -- (2) If I'First - 0.5 is representable in F then let Lo be that
1917 -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be
2af58f67 1918 -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First).
1919 -- In other words, take one of the closest floating-point numbers
1920 -- (which is an integer value) to I'First, and see if it is in
1921 -- range or not.
5329ca64 1922 -- (3) If I'Last + 0.5 is representable in F then let Hi be that value
1923 -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be
2af58f67 1924 -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last).
5329ca64 1925 -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo)
1926 -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi)
1927
2af58f67 1928 -- For the truncating case, replace steps (2) and (3) as follows:
1929 -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK
1930 -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let
1931 -- Lo_OK be True.
1932 -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK
1933 -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let
141d591a 1934 -- Hi_OK be True.
2af58f67 1935
5329ca64 1936 procedure Apply_Float_Conversion_Check
1937 (Ck_Node : Node_Id;
1938 Target_Typ : Entity_Id)
1939 is
feff2f05 1940 LB : constant Node_Id := Type_Low_Bound (Target_Typ);
1941 HB : constant Node_Id := Type_High_Bound (Target_Typ);
5329ca64 1942 Loc : constant Source_Ptr := Sloc (Ck_Node);
1943 Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node));
feff2f05 1944 Target_Base : constant Entity_Id :=
b6341c67 1945 Implementation_Base_Type (Target_Typ);
feff2f05 1946
2af58f67 1947 Par : constant Node_Id := Parent (Ck_Node);
1948 pragma Assert (Nkind (Par) = N_Type_Conversion);
1949 -- Parent of check node, must be a type conversion
1950
1951 Truncate : constant Boolean := Float_Truncate (Par);
1952 Max_Bound : constant Uint :=
b6341c67 1953 UI_Expon
1954 (Machine_Radix_Value (Expr_Type),
1955 Machine_Mantissa_Value (Expr_Type) - 1) - 1;
2af58f67 1956
5329ca64 1957 -- Largest bound, so bound plus or minus half is a machine number of F
1958
feff2f05 1959 Ifirst, Ilast : Uint;
1960 -- Bounds of integer type
1961
1962 Lo, Hi : Ureal;
1963 -- Bounds to check in floating-point domain
5329ca64 1964
feff2f05 1965 Lo_OK, Hi_OK : Boolean;
1966 -- True iff Lo resp. Hi belongs to I'Range
5329ca64 1967
feff2f05 1968 Lo_Chk, Hi_Chk : Node_Id;
1969 -- Expressions that are False iff check fails
1970
1971 Reason : RT_Exception_Code;
5329ca64 1972
1973 begin
41f06abf 1974 -- We do not need checks if we are not generating code (i.e. the full
1975 -- expander is not active). In SPARK mode, we specifically don't want
1976 -- the frontend to expand these checks, which are dealt with directly
1977 -- in the formal verification backend.
1978
a33565dd 1979 if not Expander_Active then
41f06abf 1980 return;
1981 end if;
1982
5329ca64 1983 if not Compile_Time_Known_Value (LB)
1984 or not Compile_Time_Known_Value (HB)
1985 then
1986 declare
feff2f05 1987 -- First check that the value falls in the range of the base type,
1988 -- to prevent overflow during conversion and then perform a
1989 -- regular range check against the (dynamic) bounds.
5329ca64 1990
5329ca64 1991 pragma Assert (Target_Base /= Target_Typ);
5329ca64 1992
46eb6933 1993 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par);
5329ca64 1994
1995 begin
1996 Apply_Float_Conversion_Check (Ck_Node, Target_Base);
1997 Set_Etype (Temp, Target_Base);
1998
1999 Insert_Action (Parent (Par),
2000 Make_Object_Declaration (Loc,
2001 Defining_Identifier => Temp,
2002 Object_Definition => New_Occurrence_Of (Target_Typ, Loc),
2003 Expression => New_Copy_Tree (Par)),
2004 Suppress => All_Checks);
2005
2006 Insert_Action (Par,
2007 Make_Raise_Constraint_Error (Loc,
2008 Condition =>
2009 Make_Not_In (Loc,
2010 Left_Opnd => New_Occurrence_Of (Temp, Loc),
2011 Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)),
2012 Reason => CE_Range_Check_Failed));
2013 Rewrite (Par, New_Occurrence_Of (Temp, Loc));
2014
2015 return;
2016 end;
2017 end if;
2018
7d86aa98 2019 -- Get the (static) bounds of the target type
5329ca64 2020
2021 Ifirst := Expr_Value (LB);
2022 Ilast := Expr_Value (HB);
2023
7d86aa98 2024 -- A simple optimization: if the expression is a universal literal,
2025 -- we can do the comparison with the bounds and the conversion to
2026 -- an integer type statically. The range checks are unchanged.
2027
2028 if Nkind (Ck_Node) = N_Real_Literal
2029 and then Etype (Ck_Node) = Universal_Real
2030 and then Is_Integer_Type (Target_Typ)
2031 and then Nkind (Parent (Ck_Node)) = N_Type_Conversion
2032 then
2033 declare
2034 Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node));
2035
2036 begin
2037 if Int_Val <= Ilast and then Int_Val >= Ifirst then
2038
4309515d 2039 -- Conversion is safe
7d86aa98 2040
2041 Rewrite (Parent (Ck_Node),
2042 Make_Integer_Literal (Loc, UI_To_Int (Int_Val)));
2043 Analyze_And_Resolve (Parent (Ck_Node), Target_Typ);
2044 return;
2045 end if;
2046 end;
2047 end if;
2048
5329ca64 2049 -- Check against lower bound
2050
2af58f67 2051 if Truncate and then Ifirst > 0 then
2052 Lo := Pred (Expr_Type, UR_From_Uint (Ifirst));
2053 Lo_OK := False;
2054
2055 elsif Truncate then
2056 Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1));
2057 Lo_OK := True;
2058
2059 elsif abs (Ifirst) < Max_Bound then
5329ca64 2060 Lo := UR_From_Uint (Ifirst) - Ureal_Half;
2061 Lo_OK := (Ifirst > 0);
2af58f67 2062
5329ca64 2063 else
2064 Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node);
2065 Lo_OK := (Lo >= UR_From_Uint (Ifirst));
2066 end if;
2067
2068 if Lo_OK then
2069
2070 -- Lo_Chk := (X >= Lo)
2071
2072 Lo_Chk := Make_Op_Ge (Loc,
2073 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
2074 Right_Opnd => Make_Real_Literal (Loc, Lo));
2075
2076 else
2077 -- Lo_Chk := (X > Lo)
2078
2079 Lo_Chk := Make_Op_Gt (Loc,
2080 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
2081 Right_Opnd => Make_Real_Literal (Loc, Lo));
2082 end if;
2083
2084 -- Check against higher bound
2085
2af58f67 2086 if Truncate and then Ilast < 0 then
2087 Hi := Succ (Expr_Type, UR_From_Uint (Ilast));
b2c42753 2088 Hi_OK := False;
2af58f67 2089
2090 elsif Truncate then
2091 Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1));
2092 Hi_OK := True;
2093
2094 elsif abs (Ilast) < Max_Bound then
5329ca64 2095 Hi := UR_From_Uint (Ilast) + Ureal_Half;
2096 Hi_OK := (Ilast < 0);
2097 else
2098 Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node);
2099 Hi_OK := (Hi <= UR_From_Uint (Ilast));
2100 end if;
2101
2102 if Hi_OK then
2103
2104 -- Hi_Chk := (X <= Hi)
2105
2106 Hi_Chk := Make_Op_Le (Loc,
2107 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
2108 Right_Opnd => Make_Real_Literal (Loc, Hi));
2109
2110 else
2111 -- Hi_Chk := (X < Hi)
2112
2113 Hi_Chk := Make_Op_Lt (Loc,
2114 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
2115 Right_Opnd => Make_Real_Literal (Loc, Hi));
2116 end if;
2117
feff2f05 2118 -- If the bounds of the target type are the same as those of the base
2119 -- type, the check is an overflow check as a range check is not
2120 -- performed in these cases.
5329ca64 2121
2122 if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst
2123 and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast
2124 then
2125 Reason := CE_Overflow_Check_Failed;
2126 else
2127 Reason := CE_Range_Check_Failed;
2128 end if;
2129
2130 -- Raise CE if either conditions does not hold
2131
2132 Insert_Action (Ck_Node,
2133 Make_Raise_Constraint_Error (Loc,
05fcfafb 2134 Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)),
5329ca64 2135 Reason => Reason));
2136 end Apply_Float_Conversion_Check;
2137
ee6ba406 2138 ------------------------
2139 -- Apply_Length_Check --
2140 ------------------------
2141
2142 procedure Apply_Length_Check
2143 (Ck_Node : Node_Id;
2144 Target_Typ : Entity_Id;
2145 Source_Typ : Entity_Id := Empty)
2146 is
2147 begin
2148 Apply_Selected_Length_Checks
2149 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
2150 end Apply_Length_Check;
2151
3b045963 2152 -------------------------------------
2153 -- Apply_Parameter_Aliasing_Checks --
2154 -------------------------------------
b73adb97 2155
3b045963 2156 procedure Apply_Parameter_Aliasing_Checks
2157 (Call : Node_Id;
2158 Subp : Entity_Id)
2159 is
bb569db0 2160 Loc : constant Source_Ptr := Sloc (Call);
2161
3b045963 2162 function May_Cause_Aliasing
2163 (Formal_1 : Entity_Id;
2164 Formal_2 : Entity_Id) return Boolean;
2165 -- Determine whether two formal parameters can alias each other
2166 -- depending on their modes.
2167
2168 function Original_Actual (N : Node_Id) return Node_Id;
2169 -- The expander may replace an actual with a temporary for the sake of
2170 -- side effect removal. The temporary may hide a potential aliasing as
2171 -- it does not share the address of the actual. This routine attempts
2172 -- to retrieve the original actual.
2173
bb569db0 2174 procedure Overlap_Check
2175 (Actual_1 : Node_Id;
2176 Actual_2 : Node_Id;
2177 Formal_1 : Entity_Id;
2178 Formal_2 : Entity_Id;
2179 Check : in out Node_Id);
2180 -- Create a check to determine whether Actual_1 overlaps with Actual_2.
2181 -- If detailed exception messages are enabled, the check is augmented to
2182 -- provide information about the names of the corresponding formals. See
2183 -- the body for details. Actual_1 and Actual_2 denote the two actuals to
2184 -- be tested. Formal_1 and Formal_2 denote the corresponding formals.
2185 -- Check contains all and-ed simple tests generated so far or remains
2186 -- unchanged in the case of detailed exception messaged.
2187
3b045963 2188 ------------------------
2189 -- May_Cause_Aliasing --
2190 ------------------------
b73adb97 2191
3b045963 2192 function May_Cause_Aliasing
4a9e7f0c 2193 (Formal_1 : Entity_Id;
3b045963 2194 Formal_2 : Entity_Id) return Boolean
2195 is
2196 begin
2197 -- The following combination cannot lead to aliasing
2198
2199 -- Formal 1 Formal 2
2200 -- IN IN
2201
2202 if Ekind (Formal_1) = E_In_Parameter
a45d946f 2203 and then
2204 Ekind (Formal_2) = E_In_Parameter
3b045963 2205 then
2206 return False;
2207
2208 -- The following combinations may lead to aliasing
2209
2210 -- Formal 1 Formal 2
2211 -- IN OUT
2212 -- IN IN OUT
2213 -- OUT IN
2214 -- OUT IN OUT
2215 -- OUT OUT
2216
2217 else
2218 return True;
2219 end if;
2220 end May_Cause_Aliasing;
2221
2222 ---------------------
2223 -- Original_Actual --
2224 ---------------------
2225
2226 function Original_Actual (N : Node_Id) return Node_Id is
2227 begin
2228 if Nkind (N) = N_Type_Conversion then
2229 return Expression (N);
2230
2231 -- The expander created a temporary to capture the result of a type
2232 -- conversion where the expression is the real actual.
2233
2234 elsif Nkind (N) = N_Identifier
2235 and then Present (Original_Node (N))
2236 and then Nkind (Original_Node (N)) = N_Type_Conversion
2237 then
2238 return Expression (Original_Node (N));
2239 end if;
2240
2241 return N;
2242 end Original_Actual;
2243
bb569db0 2244 -------------------
2245 -- Overlap_Check --
2246 -------------------
2247
2248 procedure Overlap_Check
2249 (Actual_1 : Node_Id;
2250 Actual_2 : Node_Id;
2251 Formal_1 : Entity_Id;
2252 Formal_2 : Entity_Id;
2253 Check : in out Node_Id)
2254 is
29448168 2255 Cond : Node_Id;
2256 ID_Casing : constant Casing_Type :=
2257 Identifier_Casing (Source_Index (Current_Sem_Unit));
bb569db0 2258
2259 begin
2260 -- Generate:
2261 -- Actual_1'Overlaps_Storage (Actual_2)
2262
2263 Cond :=
2264 Make_Attribute_Reference (Loc,
2265 Prefix => New_Copy_Tree (Original_Actual (Actual_1)),
2266 Attribute_Name => Name_Overlaps_Storage,
2267 Expressions =>
2268 New_List (New_Copy_Tree (Original_Actual (Actual_2))));
2269
2270 -- Generate the following check when detailed exception messages are
2271 -- enabled:
2272
2273 -- if Actual_1'Overlaps_Storage (Actual_2) then
2274 -- raise Program_Error with <detailed message>;
2275 -- end if;
2276
2277 if Exception_Extra_Info then
2278 Start_String;
2279
2280 -- Do not generate location information for internal calls
2281
2282 if Comes_From_Source (Call) then
2283 Store_String_Chars (Build_Location_String (Loc));
2284 Store_String_Char (' ');
2285 end if;
2286
2287 Store_String_Chars ("aliased parameters, actuals for """);
29448168 2288
2289 Get_Name_String (Chars (Formal_1));
2290 Set_Casing (ID_Casing);
2291 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2292
bb569db0 2293 Store_String_Chars (""" and """);
29448168 2294
2295 Get_Name_String (Chars (Formal_2));
2296 Set_Casing (ID_Casing);
2297 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2298
bb569db0 2299 Store_String_Chars (""" overlap");
2300
2301 Insert_Action (Call,
2302 Make_If_Statement (Loc,
2303 Condition => Cond,
2304 Then_Statements => New_List (
2305 Make_Raise_Statement (Loc,
2306 Name =>
83c6c069 2307 New_Occurrence_Of (Standard_Program_Error, Loc),
bb569db0 2308 Expression => Make_String_Literal (Loc, End_String)))));
2309
2310 -- Create a sequence of overlapping checks by and-ing them all
2311 -- together.
2312
2313 else
2314 if No (Check) then
2315 Check := Cond;
2316 else
2317 Check :=
2318 Make_And_Then (Loc,
2319 Left_Opnd => Check,
2320 Right_Opnd => Cond);
2321 end if;
2322 end if;
2323 end Overlap_Check;
2324
3b045963 2325 -- Local variables
2326
15fca308 2327 Actual_1 : Node_Id;
2328 Actual_2 : Node_Id;
2329 Check : Node_Id;
2330 Formal_1 : Entity_Id;
2331 Formal_2 : Entity_Id;
2332 Orig_Act_1 : Node_Id;
2333 Orig_Act_2 : Node_Id;
3b045963 2334
2335 -- Start of processing for Apply_Parameter_Aliasing_Checks
2336
2337 begin
bb569db0 2338 Check := Empty;
3b045963 2339
2340 Actual_1 := First_Actual (Call);
2341 Formal_1 := First_Formal (Subp);
2342 while Present (Actual_1) and then Present (Formal_1) loop
15fca308 2343 Orig_Act_1 := Original_Actual (Actual_1);
3b045963 2344
2345 -- Ensure that the actual is an object that is not passed by value.
2346 -- Elementary types are always passed by value, therefore actuals of
747426db 2347 -- such types cannot lead to aliasing. An aggregate is an object in
2348 -- Ada 2012, but an actual that is an aggregate cannot overlap with
4dc3174c 2349 -- another actual. A type that is By_Reference (such as an array of
2350 -- controlled types) is not subject to the check because any update
2351 -- will be done in place and a subsequent read will always see the
2352 -- correct value, see RM 6.2 (12/3).
3b045963 2353
15fca308 2354 if Nkind (Orig_Act_1) = N_Aggregate
2355 or else (Nkind (Orig_Act_1) = N_Qualified_Expression
2356 and then Nkind (Expression (Orig_Act_1)) = N_Aggregate)
747426db 2357 then
2358 null;
2359
15fca308 2360 elsif Is_Object_Reference (Orig_Act_1)
2361 and then not Is_Elementary_Type (Etype (Orig_Act_1))
2362 and then not Is_By_Reference_Type (Etype (Orig_Act_1))
3b045963 2363 then
2364 Actual_2 := Next_Actual (Actual_1);
2365 Formal_2 := Next_Formal (Formal_1);
2366 while Present (Actual_2) and then Present (Formal_2) loop
15fca308 2367 Orig_Act_2 := Original_Actual (Actual_2);
3b045963 2368
2369 -- The other actual we are testing against must also denote
2370 -- a non pass-by-value object. Generate the check only when
2371 -- the mode of the two formals may lead to aliasing.
2372
15fca308 2373 if Is_Object_Reference (Orig_Act_2)
2374 and then not Is_Elementary_Type (Etype (Orig_Act_2))
3b045963 2375 and then May_Cause_Aliasing (Formal_1, Formal_2)
2376 then
72f889fa 2377 Remove_Side_Effects (Actual_1);
2378 Remove_Side_Effects (Actual_2);
2379
bb569db0 2380 Overlap_Check
2381 (Actual_1 => Actual_1,
2382 Actual_2 => Actual_2,
2383 Formal_1 => Formal_1,
2384 Formal_2 => Formal_2,
2385 Check => Check);
3b045963 2386 end if;
2387
2388 Next_Actual (Actual_2);
2389 Next_Formal (Formal_2);
2390 end loop;
2391 end if;
2392
2393 Next_Actual (Actual_1);
2394 Next_Formal (Formal_1);
2395 end loop;
2396
bb569db0 2397 -- Place a simple check right before the call
3b045963 2398
bb569db0 2399 if Present (Check) and then not Exception_Extra_Info then
3b045963 2400 Insert_Action (Call,
2401 Make_Raise_Program_Error (Loc,
bb569db0 2402 Condition => Check,
2403 Reason => PE_Aliased_Parameters));
3b045963 2404 end if;
2405 end Apply_Parameter_Aliasing_Checks;
2406
2407 -------------------------------------
2408 -- Apply_Parameter_Validity_Checks --
2409 -------------------------------------
2410
2411 procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is
2412 Subp_Decl : Node_Id;
b73adb97 2413
4a9e7f0c 2414 procedure Add_Validity_Check
ed695684 2415 (Formal : Entity_Id;
2416 Prag_Nam : Name_Id;
4a9e7f0c 2417 For_Result : Boolean := False);
2418 -- Add a single 'Valid[_Scalar] check which verifies the initialization
ed695684 2419 -- of Formal. Prag_Nam denotes the pre or post condition pragma name.
4a9e7f0c 2420 -- Set flag For_Result when to verify the result of a function.
b73adb97 2421
b73adb97 2422 ------------------------
2423 -- Add_Validity_Check --
2424 ------------------------
2425
2426 procedure Add_Validity_Check
ed695684 2427 (Formal : Entity_Id;
2428 Prag_Nam : Name_Id;
b73adb97 2429 For_Result : Boolean := False)
2430 is
ed695684 2431 procedure Build_Pre_Post_Condition (Expr : Node_Id);
2432 -- Create a pre/postcondition pragma that tests expression Expr
2433
2434 ------------------------------
2435 -- Build_Pre_Post_Condition --
2436 ------------------------------
2437
2438 procedure Build_Pre_Post_Condition (Expr : Node_Id) is
2439 Loc : constant Source_Ptr := Sloc (Subp);
2440 Decls : List_Id;
2441 Prag : Node_Id;
2442
2443 begin
2444 Prag :=
2445 Make_Pragma (Loc,
c33cb5ff 2446 Chars => Prag_Nam,
ed695684 2447 Pragma_Argument_Associations => New_List (
2448 Make_Pragma_Argument_Association (Loc,
2449 Chars => Name_Check,
2450 Expression => Expr)));
2451
2452 -- Add a message unless exception messages are suppressed
2453
2454 if not Exception_Locations_Suppressed then
2455 Append_To (Pragma_Argument_Associations (Prag),
2456 Make_Pragma_Argument_Association (Loc,
2457 Chars => Name_Message,
2458 Expression =>
2459 Make_String_Literal (Loc,
2460 Strval => "failed "
2461 & Get_Name_String (Prag_Nam)
2462 & " from "
2463 & Build_Location_String (Loc))));
2464 end if;
2465
2466 -- Insert the pragma in the tree
2467
2468 if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then
2469 Add_Global_Declaration (Prag);
2470 Analyze (Prag);
2471
2472 -- PPC pragmas associated with subprogram bodies must be inserted
2473 -- in the declarative part of the body.
2474
2475 elsif Nkind (Subp_Decl) = N_Subprogram_Body then
2476 Decls := Declarations (Subp_Decl);
2477
2478 if No (Decls) then
2479 Decls := New_List;
2480 Set_Declarations (Subp_Decl, Decls);
2481 end if;
2482
2483 Prepend_To (Decls, Prag);
2484 Analyze (Prag);
2485
2486 -- For subprogram declarations insert the PPC pragma right after
2487 -- the declarative node.
2488
2489 else
2490 Insert_After_And_Analyze (Subp_Decl, Prag);
2491 end if;
2492 end Build_Pre_Post_Condition;
2493
2494 -- Local variables
2495
4a9e7f0c 2496 Loc : constant Source_Ptr := Sloc (Subp);
ed695684 2497 Typ : constant Entity_Id := Etype (Formal);
b73adb97 2498 Check : Node_Id;
2499 Nam : Name_Id;
2500
ed695684 2501 -- Start of processing for Add_Validity_Check
2502
b73adb97 2503 begin
9916a361 2504 -- For scalars, generate 'Valid test
b73adb97 2505
2506 if Is_Scalar_Type (Typ) then
2507 Nam := Name_Valid;
9916a361 2508
2509 -- For any non-scalar with scalar parts, generate 'Valid_Scalars test
2510
2511 elsif Scalar_Part_Present (Typ) then
b73adb97 2512 Nam := Name_Valid_Scalars;
9916a361 2513
2514 -- No test needed for other cases (no scalars to test)
2515
b73adb97 2516 else
2517 return;
2518 end if;
2519
2520 -- Step 1: Create the expression to verify the validity of the
2521 -- context.
2522
ed695684 2523 Check := New_Occurrence_Of (Formal, Loc);
b73adb97 2524
2525 -- When processing a function result, use 'Result. Generate
2526 -- Context'Result
2527
2528 if For_Result then
2529 Check :=
2530 Make_Attribute_Reference (Loc,
2531 Prefix => Check,
2532 Attribute_Name => Name_Result);
2533 end if;
2534
2535 -- Generate:
2536 -- Context['Result]'Valid[_Scalars]
2537
2538 Check :=
2539 Make_Attribute_Reference (Loc,
2540 Prefix => Check,
2541 Attribute_Name => Nam);
2542
4a9e7f0c 2543 -- Step 2: Create a pre or post condition pragma
2544
ed695684 2545 Build_Pre_Post_Condition (Check);
4a9e7f0c 2546 end Add_Validity_Check;
2547
4a9e7f0c 2548 -- Local variables
2549
2550 Formal : Entity_Id;
4a9e7f0c 2551 Subp_Spec : Node_Id;
2552
3b045963 2553 -- Start of processing for Apply_Parameter_Validity_Checks
b73adb97 2554
2555 begin
4a9e7f0c 2556 -- Extract the subprogram specification and declaration nodes
b73adb97 2557
4a9e7f0c 2558 Subp_Spec := Parent (Subp);
a45d946f 2559
4a9e7f0c 2560 if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then
2561 Subp_Spec := Parent (Subp_Spec);
2562 end if;
a45d946f 2563
4a9e7f0c 2564 Subp_Decl := Parent (Subp_Spec);
9e58d7ed 2565
b73adb97 2566 if not Comes_From_Source (Subp)
4a9e7f0c 2567
2568 -- Do not process formal subprograms because the corresponding actual
2569 -- will receive the proper checks when the instance is analyzed.
2570
2571 or else Is_Formal_Subprogram (Subp)
2572
95ac2d90 2573 -- Do not process imported subprograms since pre and postconditions
a45d946f 2574 -- are never verified on routines coming from a different language.
4a9e7f0c 2575
b73adb97 2576 or else Is_Imported (Subp)
2577 or else Is_Intrinsic_Subprogram (Subp)
4a9e7f0c 2578
a45d946f 2579 -- The PPC pragmas generated by this routine do not correspond to
2580 -- source aspects, therefore they cannot be applied to abstract
2581 -- subprograms.
4a9e7f0c 2582
7c443ae8 2583 or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration
4a9e7f0c 2584
a45d946f 2585 -- Do not consider subprogram renaminds because the renamed entity
2586 -- already has the proper PPC pragmas.
1bd93de5 2587
2588 or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration
2589
a45d946f 2590 -- Do not process null procedures because there is no benefit of
2591 -- adding the checks to a no action routine.
4a9e7f0c 2592
2593 or else (Nkind (Subp_Spec) = N_Procedure_Specification
a45d946f 2594 and then Null_Present (Subp_Spec))
b73adb97 2595 then
2596 return;
2597 end if;
2598
4a9e7f0c 2599 -- Inspect all the formals applying aliasing and scalar initialization
2600 -- checks where applicable.
b73adb97 2601
2602 Formal := First_Formal (Subp);
2603 while Present (Formal) loop
4a9e7f0c 2604
2605 -- Generate the following scalar initialization checks for each
2606 -- formal parameter:
2607
2608 -- mode IN - Pre => Formal'Valid[_Scalars]
2609 -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars]
2610 -- mode OUT - Post => Formal'Valid[_Scalars]
2611
2612 if Check_Validity_Of_Parameters then
2613 if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then
2614 Add_Validity_Check (Formal, Name_Precondition, False);
2615 end if;
2616
2617 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
2618 Add_Validity_Check (Formal, Name_Postcondition, False);
2619 end if;
b73adb97 2620 end if;
2621
b73adb97 2622 Next_Formal (Formal);
2623 end loop;
2624
a45d946f 2625 -- Generate following scalar initialization check for function result:
4a9e7f0c 2626
2627 -- Post => Subp'Result'Valid[_Scalars]
b73adb97 2628
a45d946f 2629 if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then
4a9e7f0c 2630 Add_Validity_Check (Subp, Name_Postcondition, True);
b73adb97 2631 end if;
3b045963 2632 end Apply_Parameter_Validity_Checks;
b73adb97 2633
7aafae1c 2634 ---------------------------
2635 -- Apply_Predicate_Check --
2636 ---------------------------
2637
e60047e5 2638 procedure Apply_Predicate_Check
2639 (N : Node_Id;
2640 Typ : Entity_Id;
2641 Fun : Entity_Id := Empty)
2642 is
301d5ec3 2643 S : Entity_Id;
9e58d7ed 2644
7aafae1c 2645 begin
da2270e7 2646 if Predicate_Checks_Suppressed (Empty) then
2647 return;
301d5ec3 2648
2c011bc5 2649 elsif Predicates_Ignored (Typ) then
2650 return;
2651
da2270e7 2652 elsif Present (Predicate_Function (Typ)) then
301d5ec3 2653 S := Current_Scope;
9e58d7ed 2654 while Present (S) and then not Is_Subprogram (S) loop
301d5ec3 2655 S := Scope (S);
2656 end loop;
2657
ea822fd4 2658 -- A predicate check does not apply within internally generated
2659 -- subprograms, such as TSS functions.
2660
2661 if Within_Internal_Subprogram then
301d5ec3 2662 return;
22631b41 2663
96a2d100 2664 -- If the check appears within the predicate function itself, it
2665 -- means that the user specified a check whose formal is the
2666 -- predicated subtype itself, rather than some covering type. This
2667 -- is likely to be a common error, and thus deserves a warning.
22631b41 2668
0e9014a7 2669 elsif Present (S) and then S = Predicate_Function (Typ) then
e60047e5 2670 Error_Msg_NE
0c4abd51 2671 ("predicate check includes a call to& that requires a "
2672 & "predicate check??", Parent (N), Fun);
96a2d100 2673 Error_Msg_N
cb97ae5c 2674 ("\this will result in infinite recursion??", Parent (N));
e60047e5 2675
2676 if Is_First_Subtype (Typ) then
2677 Error_Msg_NE
0c4abd51 2678 ("\use an explicit subtype of& to carry the predicate",
e60047e5 2679 Parent (N), Typ);
2680 end if;
2681
96a2d100 2682 Insert_Action (N,
61016a7a 2683 Make_Raise_Storage_Error (Sloc (N),
2684 Reason => SE_Infinite_Recursion));
22631b41 2685
64cc9e5d 2686 -- Here for normal case of predicate active
e6281d47 2687
61016a7a 2688 else
b04165c4 2689 -- If the type has a static predicate and the expression is known
2690 -- at compile time, see if the expression satisfies the predicate.
3a75f20b 2691
2692 Check_Expression_Against_Static_Predicate (N, Typ);
e6281d47 2693
e34cd69d 2694 if not Expander_Active then
2695 return;
2696 end if;
2697
2698 -- For an entity of the type, generate a call to the predicate
2699 -- function, unless its type is an actual subtype, which is not
2700 -- visible outside of the enclosing subprogram.
2701
2702 if Is_Entity_Name (N)
2703 and then not Is_Actual_Subtype (Typ)
2704 then
da2270e7 2705 Insert_Action (N,
2706 Make_Predicate_Check
2707 (Typ, New_Occurrence_Of (Entity (N), Sloc (N))));
2708
f9906591 2709 -- If the expression is not an entity it may have side effects,
bc885df9 2710 -- and the following call will create an object declaration for
2711 -- it. We disable checks during its analysis, to prevent an
2712 -- infinite recursion.
da2270e7 2713
e3b910d8 2714 -- If the prefix is an aggregate in an assignment, apply the
2715 -- check to the LHS after assignment, rather than create a
2716 -- redundant temporary. This is only necessary in rare cases
2717 -- of array types (including strings) initialized with an
2718 -- aggregate with an "others" clause, either coming from source
2719 -- or generated by an Initialize_Scalars pragma.
2720
2721 elsif Nkind (N) = N_Aggregate
2722 and then Nkind (Parent (N)) = N_Assignment_Statement
2723 then
2724 Insert_Action_After (Parent (N),
2725 Make_Predicate_Check
2726 (Typ, Duplicate_Subexpr (Name (Parent (N)))));
2727
da2270e7 2728 else
2729 Insert_Action (N,
bc885df9 2730 Make_Predicate_Check
2731 (Typ, Duplicate_Subexpr (N)), Suppress => All_Checks);
da2270e7 2732 end if;
301d5ec3 2733 end if;
7aafae1c 2734 end if;
2735 end Apply_Predicate_Check;
2736
ee6ba406 2737 -----------------------
2738 -- Apply_Range_Check --
2739 -----------------------
2740
2741 procedure Apply_Range_Check
2742 (Ck_Node : Node_Id;
2743 Target_Typ : Entity_Id;
2744 Source_Typ : Entity_Id := Empty)
2745 is
2746 begin
2747 Apply_Selected_Range_Checks
2748 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
2749 end Apply_Range_Check;
2750
2751 ------------------------------
2752 -- Apply_Scalar_Range_Check --
2753 ------------------------------
2754
feff2f05 2755 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag
2756 -- off if it is already set on.
ee6ba406 2757
2758 procedure Apply_Scalar_Range_Check
2759 (Expr : Node_Id;
2760 Target_Typ : Entity_Id;
2761 Source_Typ : Entity_Id := Empty;
2762 Fixed_Int : Boolean := False)
2763 is
2764 Parnt : constant Node_Id := Parent (Expr);
2765 S_Typ : Entity_Id;
2766 Arr : Node_Id := Empty; -- initialize to prevent warning
2767 Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning
16149377 2768 OK : Boolean := False; -- initialize to prevent warning
ee6ba406 2769
2770 Is_Subscr_Ref : Boolean;
2771 -- Set true if Expr is a subscript
2772
2773 Is_Unconstrained_Subscr_Ref : Boolean;
2774 -- Set true if Expr is a subscript of an unconstrained array. In this
2775 -- case we do not attempt to do an analysis of the value against the
2776 -- range of the subscript, since we don't know the actual subtype.
2777
2778 Int_Real : Boolean;
feff2f05 2779 -- Set to True if Expr should be regarded as a real value even though
2780 -- the type of Expr might be discrete.
ee6ba406 2781
a0a15971 2782 procedure Bad_Value (Warn : Boolean := False);
2783 -- Procedure called if value is determined to be out of range. Warn is
2784 -- True to force a warning instead of an error, even when SPARK_Mode is
2785 -- On.
ee6ba406 2786
9dfe12ae 2787 ---------------
2788 -- Bad_Value --
2789 ---------------
2790
a0a15971 2791 procedure Bad_Value (Warn : Boolean := False) is
ee6ba406 2792 begin
2793 Apply_Compile_Time_Constraint_Error
cb97ae5c 2794 (Expr, "value not in range of}??", CE_Range_Check_Failed,
a0a15971 2795 Ent => Target_Typ,
2796 Typ => Target_Typ,
2797 Warn => Warn);
ee6ba406 2798 end Bad_Value;
2799
9dfe12ae 2800 -- Start of processing for Apply_Scalar_Range_Check
2801
ee6ba406 2802 begin
2af58f67 2803 -- Return if check obviously not needed
ee6ba406 2804
2af58f67 2805 if
2806 -- Not needed inside generic
ee6ba406 2807
2af58f67 2808 Inside_A_Generic
2809
2810 -- Not needed if previous error
2811
2812 or else Target_Typ = Any_Type
2813 or else Nkind (Expr) = N_Error
2814
2815 -- Not needed for non-scalar type
2816
2817 or else not Is_Scalar_Type (Target_Typ)
2818
2819 -- Not needed if we know node raises CE already
2820
2821 or else Raises_Constraint_Error (Expr)
ee6ba406 2822 then
2823 return;
2824 end if;
2825
2826 -- Now, see if checks are suppressed
2827
2828 Is_Subscr_Ref :=
2829 Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
2830
2831 if Is_Subscr_Ref then
2832 Arr := Prefix (Parnt);
2833 Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
cce84b09 2834
a3a76ccc 2835 if Is_Access_Type (Arr_Typ) then
245e87df 2836 Arr_Typ := Designated_Type (Arr_Typ);
a3a76ccc 2837 end if;
ee6ba406 2838 end if;
2839
2840 if not Do_Range_Check (Expr) then
2841
2842 -- Subscript reference. Check for Index_Checks suppressed
2843
2844 if Is_Subscr_Ref then
2845
2846 -- Check array type and its base type
2847
2848 if Index_Checks_Suppressed (Arr_Typ)
9dfe12ae 2849 or else Index_Checks_Suppressed (Base_Type (Arr_Typ))
ee6ba406 2850 then
2851 return;
2852
2853 -- Check array itself if it is an entity name
2854
2855 elsif Is_Entity_Name (Arr)
9dfe12ae 2856 and then Index_Checks_Suppressed (Entity (Arr))
ee6ba406 2857 then
2858 return;
2859
2860 -- Check expression itself if it is an entity name
2861
2862 elsif Is_Entity_Name (Expr)
9dfe12ae 2863 and then Index_Checks_Suppressed (Entity (Expr))
ee6ba406 2864 then
2865 return;
2866 end if;
2867
2868 -- All other cases, check for Range_Checks suppressed
2869
2870 else
2871 -- Check target type and its base type
2872
2873 if Range_Checks_Suppressed (Target_Typ)
9dfe12ae 2874 or else Range_Checks_Suppressed (Base_Type (Target_Typ))
ee6ba406 2875 then
2876 return;
2877
2878 -- Check expression itself if it is an entity name
2879
2880 elsif Is_Entity_Name (Expr)
9dfe12ae 2881 and then Range_Checks_Suppressed (Entity (Expr))
ee6ba406 2882 then
2883 return;
2884
feff2f05 2885 -- If Expr is part of an assignment statement, then check left
2886 -- side of assignment if it is an entity name.
ee6ba406 2887
2888 elsif Nkind (Parnt) = N_Assignment_Statement
2889 and then Is_Entity_Name (Name (Parnt))
9dfe12ae 2890 and then Range_Checks_Suppressed (Entity (Name (Parnt)))
ee6ba406 2891 then
2892 return;
2893 end if;
2894 end if;
2895 end if;
2896
9dfe12ae 2897 -- Do not set range checks if they are killed
2898
2899 if Nkind (Expr) = N_Unchecked_Type_Conversion
2900 and then Kill_Range_Check (Expr)
2901 then
2902 return;
2903 end if;
2904
2905 -- Do not set range checks for any values from System.Scalar_Values
39a0c1d3 2906 -- since the whole idea of such values is to avoid checking them.
9dfe12ae 2907
2908 if Is_Entity_Name (Expr)
2909 and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values)
2910 then
2911 return;
2912 end if;
2913
ee6ba406 2914 -- Now see if we need a check
2915
2916 if No (Source_Typ) then
2917 S_Typ := Etype (Expr);
2918 else
2919 S_Typ := Source_Typ;
2920 end if;
2921
2922 if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
2923 return;
2924 end if;
2925
2926 Is_Unconstrained_Subscr_Ref :=
2927 Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
2928
b40670e1 2929 -- Special checks for floating-point type
ee6ba406 2930
b40670e1 2931 if Is_Floating_Point_Type (S_Typ) then
2932
2933 -- Always do a range check if the source type includes infinities and
2934 -- the target type does not include infinities. We do not do this if
2935 -- range checks are killed.
cb388b10 2936 -- If the expression is a literal and the bounds of the type are
2937 -- static constants it may be possible to optimize the check.
b40670e1 2938
2939 if Has_Infinities (S_Typ)
2940 and then not Has_Infinities (Target_Typ)
2941 then
cb388b10 2942 -- If the expression is a literal and the bounds of the type are
2943 -- static constants it may be possible to optimize the check.
2944
2945 if Nkind (Expr) = N_Real_Literal then
2946 declare
2947 Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
2948 Thi : constant Node_Id := Type_High_Bound (Target_Typ);
2949
2950 begin
2951 if Compile_Time_Known_Value (Tlo)
2952 and then Compile_Time_Known_Value (Thi)
2953 and then Expr_Value_R (Expr) >= Expr_Value_R (Tlo)
2954 and then Expr_Value_R (Expr) <= Expr_Value_R (Thi)
2955 then
2956 return;
2957 else
2958 Enable_Range_Check (Expr);
2959 end if;
2960 end;
2961
2962 else
2963 Enable_Range_Check (Expr);
2964 end if;
b40670e1 2965 end if;
ee6ba406 2966 end if;
2967
feff2f05 2968 -- Return if we know expression is definitely in the range of the target
2969 -- type as determined by Determine_Range. Right now we only do this for
2970 -- discrete types, and not fixed-point or floating-point types.
ee6ba406 2971
f2a06be9 2972 -- The additional less-precise tests below catch these cases
ee6ba406 2973
54022749 2974 -- In GNATprove_Mode, also deal with the case of a conversion from
2975 -- floating-point to integer. It is only possible because analysis
2976 -- in GNATprove rules out the possibility of a NaN or infinite value.
2977
feff2f05 2978 -- Note: skip this if we are given a source_typ, since the point of
2979 -- supplying a Source_Typ is to stop us looking at the expression.
2980 -- We could sharpen this test to be out parameters only ???
ee6ba406 2981
2982 if Is_Discrete_Type (Target_Typ)
54022749 2983 and then (Is_Discrete_Type (Etype (Expr))
2984 or else (GNATprove_Mode
2985 and then Is_Floating_Point_Type (Etype (Expr))))
ee6ba406 2986 and then not Is_Unconstrained_Subscr_Ref
2987 and then No (Source_Typ)
2988 then
2989 declare
ee6ba406 2990 Thi : constant Node_Id := Type_High_Bound (Target_Typ);
552d7cbc 2991 Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
ee6ba406 2992
2993 begin
2994 if Compile_Time_Known_Value (Tlo)
2995 and then Compile_Time_Known_Value (Thi)
2996 then
9dfe12ae 2997 declare
9dfe12ae 2998 Hiv : constant Uint := Expr_Value (Thi);
552d7cbc 2999 Lov : constant Uint := Expr_Value (Tlo);
54022749 3000 Hi : Uint;
552d7cbc 3001 Lo : Uint;
ee6ba406 3002
9dfe12ae 3003 begin
552d7cbc 3004 -- If range is null, we for sure have a constraint error (we
3005 -- don't even need to look at the value involved, since all
3006 -- possible values will raise CE).
9dfe12ae 3007
3008 if Lov > Hiv then
f4f2bf51 3009
a0a15971 3010 -- When SPARK_Mode is On, force a warning instead of
3011 -- an error in that case, as this likely corresponds
3012 -- to deactivated code.
3013
3014 Bad_Value (Warn => SPARK_Mode = On);
3015
3016 -- In GNATprove mode, we enable the range check so that
3017 -- GNATprove will issue a message if it cannot be proved.
f4f2bf51 3018
3019 if GNATprove_Mode then
3020 Enable_Range_Check (Expr);
f4f2bf51 3021 end if;
3022
9dfe12ae 3023 return;
3024 end if;
3025
3026 -- Otherwise determine range of value
3027
54022749 3028 if Is_Discrete_Type (Etype (Expr)) then
552d7cbc 3029 Determine_Range
3030 (Expr, OK, Lo, Hi, Assume_Valid => True);
54022749 3031
3032 -- When converting a float to an integer type, determine the
3033 -- range in real first, and then convert the bounds using
3034 -- UR_To_Uint which correctly rounds away from zero when
3035 -- half way between two integers, as required by normal
3036 -- Ada 95 rounding semantics. It is only possible because
3037 -- analysis in GNATprove rules out the possibility of a NaN
3038 -- or infinite value.
3039
3040 elsif GNATprove_Mode
3041 and then Is_Floating_Point_Type (Etype (Expr))
3042 then
3043 declare
54022749 3044 Hir : Ureal;
552d7cbc 3045 Lor : Ureal;
3046
54022749 3047 begin
552d7cbc 3048 Determine_Range_R
3049 (Expr, OK, Lor, Hir, Assume_Valid => True);
54022749 3050
3051 if OK then
3052 Lo := UR_To_Uint (Lor);
3053 Hi := UR_To_Uint (Hir);
3054 end if;
3055 end;
3056 end if;
9dfe12ae 3057
3058 if OK then
3059
3060 -- If definitely in range, all OK
ee6ba406 3061
ee6ba406 3062 if Lo >= Lov and then Hi <= Hiv then
3063 return;
3064
9dfe12ae 3065 -- If definitely not in range, warn
3066
ee6ba406 3067 elsif Lov > Hi or else Hiv < Lo then
3068 Bad_Value;
3069 return;
9dfe12ae 3070
3071 -- Otherwise we don't know
3072
3073 else
3074 null;
ee6ba406 3075 end if;
9dfe12ae 3076 end if;
3077 end;
ee6ba406 3078 end if;
3079 end;
3080 end if;
3081
3082 Int_Real :=
3083 Is_Floating_Point_Type (S_Typ)
3084 or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
3085
3086 -- Check if we can determine at compile time whether Expr is in the
9dfe12ae 3087 -- range of the target type. Note that if S_Typ is within the bounds
3088 -- of Target_Typ then this must be the case. This check is meaningful
3089 -- only if this is not a conversion between integer and real types.
ee6ba406 3090
3091 if not Is_Unconstrained_Subscr_Ref
b40670e1 3092 and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
ee6ba406 3093 and then
7a1dabb3 3094 (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
ac5f5168 3095
3096 -- Also check if the expression itself is in the range of the
3097 -- target type if it is a known at compile time value. We skip
3098 -- this test if S_Typ is set since for OUT and IN OUT parameters
3099 -- the Expr itself is not relevant to the checking.
3100
ee6ba406 3101 or else
ac5f5168 3102 (No (Source_Typ)
3103 and then Is_In_Range (Expr, Target_Typ,
3104 Assume_Valid => True,
3105 Fixed_Int => Fixed_Int,
3106 Int_Real => Int_Real)))
ee6ba406 3107 then
3108 return;
3109
9c486805 3110 elsif Is_Out_Of_Range (Expr, Target_Typ,
3111 Assume_Valid => True,
3112 Fixed_Int => Fixed_Int,
3113 Int_Real => Int_Real)
3114 then
ee6ba406 3115 Bad_Value;
3116 return;
3117
b40670e1 3118 -- Floating-point case
feff2f05 3119 -- In the floating-point case, we only do range checks if the type is
3120 -- constrained. We definitely do NOT want range checks for unconstrained
29d958a7 3121 -- types, since we want to have infinities, except when
3122 -- Check_Float_Overflow is set.
ee6ba406 3123
9dfe12ae 3124 elsif Is_Floating_Point_Type (S_Typ) then
29d958a7 3125 if Is_Constrained (S_Typ) or else Check_Float_Overflow then
9dfe12ae 3126 Enable_Range_Check (Expr);
3127 end if;
ee6ba406 3128
9dfe12ae 3129 -- For all other cases we enable a range check unconditionally
ee6ba406 3130
3131 else
3132 Enable_Range_Check (Expr);
3133 return;
3134 end if;
ee6ba406 3135 end Apply_Scalar_Range_Check;
3136
3137 ----------------------------------
3138 -- Apply_Selected_Length_Checks --
3139 ----------------------------------
3140
3141 procedure Apply_Selected_Length_Checks
3142 (Ck_Node : Node_Id;
3143 Target_Typ : Entity_Id;
3144 Source_Typ : Entity_Id;
3145 Do_Static : Boolean)
3146 is
2b4f2458 3147 Checks_On : constant Boolean :=
3148 not Index_Checks_Suppressed (Target_Typ)
3149 or else
3150 not Length_Checks_Suppressed (Target_Typ);
3151
3152 Loc : constant Source_Ptr := Sloc (Ck_Node);
3153
ee6ba406 3154 Cond : Node_Id;
ee6ba406 3155 R_Cno : Node_Id;
2b4f2458 3156 R_Result : Check_Result;
ee6ba406 3157
3158 begin
18cb6d78 3159 -- Only apply checks when generating code
f0d65dae 3160
4098232e 3161 -- Note: this means that we lose some useful warnings if the expander
f0d65dae 3162 -- is not active.
4098232e 3163
18cb6d78 3164 if not Expander_Active then
ee6ba406 3165 return;
3166 end if;
3167
3168 R_Result :=
3169 Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
3170
3171 for J in 1 .. 2 loop
ee6ba406 3172 R_Cno := R_Result (J);
3173 exit when No (R_Cno);
3174
3175 -- A length check may mention an Itype which is attached to a
3176 -- subsequent node. At the top level in a package this can cause
3177 -- an order-of-elaboration problem, so we make sure that the itype
3178 -- is referenced now.
3179
3180 if Ekind (Current_Scope) = E_Package
3181 and then Is_Compilation_Unit (Current_Scope)
3182 then
3183 Ensure_Defined (Target_Typ, Ck_Node);
3184
3185 if Present (Source_Typ) then
3186 Ensure_Defined (Source_Typ, Ck_Node);
3187
3188 elsif Is_Itype (Etype (Ck_Node)) then
3189 Ensure_Defined (Etype (Ck_Node), Ck_Node);
3190 end if;
3191 end if;
3192
feff2f05 3193 -- If the item is a conditional raise of constraint error, then have
3194 -- a look at what check is being performed and ???
ee6ba406 3195
3196 if Nkind (R_Cno) = N_Raise_Constraint_Error
3197 and then Present (Condition (R_Cno))
3198 then
3199 Cond := Condition (R_Cno);
3200
0577b0b1 3201 -- Case where node does not now have a dynamic check
ee6ba406 3202
0577b0b1 3203 if not Has_Dynamic_Length_Check (Ck_Node) then
3204
3205 -- If checks are on, just insert the check
3206
3207 if Checks_On then
3208 Insert_Action (Ck_Node, R_Cno);
3209
3210 if not Do_Static then
3211 Set_Has_Dynamic_Length_Check (Ck_Node);
3212 end if;
3213
3214 -- If checks are off, then analyze the length check after
3215 -- temporarily attaching it to the tree in case the relevant
6fb3c314 3216 -- condition can be evaluated at compile time. We still want a
0577b0b1 3217 -- compile time warning in this case.
3218
3219 else
3220 Set_Parent (R_Cno, Ck_Node);
3221 Analyze (R_Cno);
ee6ba406 3222 end if;
ee6ba406 3223 end if;
3224
3225 -- Output a warning if the condition is known to be True
3226
3227 if Is_Entity_Name (Cond)
3228 and then Entity (Cond) = Standard_True
3229 then
3230 Apply_Compile_Time_Constraint_Error
cb97ae5c 3231 (Ck_Node, "wrong length for array of}??",
f15731c4 3232 CE_Length_Check_Failed,
ee6ba406 3233 Ent => Target_Typ,
3234 Typ => Target_Typ);
3235
3236 -- If we were only doing a static check, or if checks are not
3237 -- on, then we want to delete the check, since it is not needed.
3238 -- We do this by replacing the if statement by a null statement
3239
3240 elsif Do_Static or else not Checks_On then
00c403ee 3241 Remove_Warning_Messages (R_Cno);
ee6ba406 3242 Rewrite (R_Cno, Make_Null_Statement (Loc));
3243 end if;
3244
3245 else
3246 Install_Static_Check (R_Cno, Loc);
3247 end if;
ee6ba406 3248 end loop;
ee6ba406 3249 end Apply_Selected_Length_Checks;
3250
3251 ---------------------------------
3252 -- Apply_Selected_Range_Checks --
3253 ---------------------------------
3254
3255 procedure Apply_Selected_Range_Checks
3256 (Ck_Node : Node_Id;
3257 Target_Typ : Entity_Id;
3258 Source_Typ : Entity_Id;
3259 Do_Static : Boolean)
3260 is
ee6ba406 3261 Checks_On : constant Boolean :=
f9bcba0d 3262 not Index_Checks_Suppressed (Target_Typ)
5372d110 3263 or else
3264 not Range_Checks_Suppressed (Target_Typ);
f9bcba0d 3265
2b4f2458 3266 Loc : constant Source_Ptr := Sloc (Ck_Node);
3267
f9bcba0d 3268 Cond : Node_Id;
3269 R_Cno : Node_Id;
3270 R_Result : Check_Result;
ee6ba406 3271
3272 begin
f0d65dae 3273 -- Only apply checks when generating code. In GNATprove mode, we do not
3274 -- apply the checks, but we still call Selected_Range_Checks to possibly
3275 -- issue errors on SPARK code when a run-time error can be detected at
3276 -- compile time.
3277
3278 if not GNATprove_Mode then
3279 if not Expander_Active or not Checks_On then
3280 return;
3281 end if;
ee6ba406 3282 end if;
3283
3284 R_Result :=
3285 Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
3286
f0d65dae 3287 if GNATprove_Mode then
3288 return;
3289 end if;
3290
ee6ba406 3291 for J in 1 .. 2 loop
ee6ba406 3292 R_Cno := R_Result (J);
3293 exit when No (R_Cno);
3294
f9bcba0d 3295 -- The range check requires runtime evaluation. Depending on what its
3296 -- triggering condition is, the check may be converted into a compile
3297 -- time constraint check.
ee6ba406 3298
3299 if Nkind (R_Cno) = N_Raise_Constraint_Error
3300 and then Present (Condition (R_Cno))
3301 then
3302 Cond := Condition (R_Cno);
3303
f9bcba0d 3304 -- Insert the range check before the related context. Note that
3305 -- this action analyses the triggering condition.
ee6ba406 3306
f9bcba0d 3307 Insert_Action (Ck_Node, R_Cno);
3308
3309 -- This old code doesn't make sense, why is the context flagged as
3310 -- requiring dynamic range checks now in the middle of generating
3311 -- them ???
3312
3313 if not Do_Static then
3314 Set_Has_Dynamic_Range_Check (Ck_Node);
ee6ba406 3315 end if;
3316
f9bcba0d 3317 -- The triggering condition evaluates to True, the range check
3318 -- can be converted into a compile time constraint check.
ee6ba406 3319
3320 if Is_Entity_Name (Cond)
3321 and then Entity (Cond) = Standard_True
3322 then
feff2f05 3323 -- Since an N_Range is technically not an expression, we have
3324 -- to set one of the bounds to C_E and then just flag the
3325 -- N_Range. The warning message will point to the lower bound
3326 -- and complain about a range, which seems OK.
ee6ba406 3327
3328 if Nkind (Ck_Node) = N_Range then
3329 Apply_Compile_Time_Constraint_Error
5372d110 3330 (Low_Bound (Ck_Node),
3331 "static range out of bounds of}??",
f15731c4 3332 CE_Range_Check_Failed,
ee6ba406 3333 Ent => Target_Typ,
3334 Typ => Target_Typ);
3335
3336 Set_Raises_Constraint_Error (Ck_Node);
3337
3338 else
3339 Apply_Compile_Time_Constraint_Error
5372d110 3340 (Ck_Node,
1581f2d7 3341 "static value out of range of}??",
f15731c4 3342 CE_Range_Check_Failed,
ee6ba406 3343 Ent => Target_Typ,
3344 Typ => Target_Typ);
3345 end if;
3346
3347 -- If we were only doing a static check, or if checks are not
3348 -- on, then we want to delete the check, since it is not needed.
3349 -- We do this by replacing the if statement by a null statement
3350
3fabf0ca 3351 elsif Do_Static then
00c403ee 3352 Remove_Warning_Messages (R_Cno);
ee6ba406 3353 Rewrite (R_Cno, Make_Null_Statement (Loc));
3354 end if;
3355
4ee78e36 3356 -- The range check raises Constraint_Error explicitly
f9bcba0d 3357
ee6ba406 3358 else
3359 Install_Static_Check (R_Cno, Loc);
3360 end if;
ee6ba406 3361 end loop;
ee6ba406 3362 end Apply_Selected_Range_Checks;
3363
3364 -------------------------------
3365 -- Apply_Static_Length_Check --
3366 -------------------------------
3367
3368 procedure Apply_Static_Length_Check
3369 (Expr : Node_Id;
3370 Target_Typ : Entity_Id;
3371 Source_Typ : Entity_Id := Empty)
3372 is
3373 begin
3374 Apply_Selected_Length_Checks
3375 (Expr, Target_Typ, Source_Typ, Do_Static => True);
3376 end Apply_Static_Length_Check;
3377
3378 -------------------------------------
3379 -- Apply_Subscript_Validity_Checks --
3380 -------------------------------------
3381
3382 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
3383 Sub : Node_Id;
3384
3385 begin
3386 pragma Assert (Nkind (Expr) = N_Indexed_Component);
3387
3388 -- Loop through subscripts
3389
3390 Sub := First (Expressions (Expr));
3391 while Present (Sub) loop
3392
feff2f05 3393 -- Check one subscript. Note that we do not worry about enumeration
3394 -- type with holes, since we will convert the value to a Pos value
3395 -- for the subscript, and that convert will do the necessary validity
3396 -- check.
ee6ba406 3397
3398 Ensure_Valid (Sub, Holes_OK => True);
3399
3400 -- Move to next subscript
3401
3402 Sub := Next (Sub);
3403 end loop;
3404 end Apply_Subscript_Validity_Checks;
3405
3406 ----------------------------------
3407 -- Apply_Type_Conversion_Checks --
3408 ----------------------------------
3409
3410 procedure Apply_Type_Conversion_Checks (N : Node_Id) is
3411 Target_Type : constant Entity_Id := Etype (N);
3412 Target_Base : constant Entity_Id := Base_Type (Target_Type);
9dfe12ae 3413 Expr : constant Node_Id := Expression (N);
f4532fe1 3414
3415 Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr));
141d591a 3416 -- Note: if Etype (Expr) is a private type without discriminants, its
3417 -- full view might have discriminants with defaults, so we need the
3418 -- full view here to retrieve the constraints.
ee6ba406 3419
3420 begin
3421 if Inside_A_Generic then
3422 return;
3423
f15731c4 3424 -- Skip these checks if serious errors detected, there are some nasty
ee6ba406 3425 -- situations of incomplete trees that blow things up.
3426
f15731c4 3427 elsif Serious_Errors_Detected > 0 then
ee6ba406 3428 return;
3429
ea822fd4 3430 -- Never generate discriminant checks for Unchecked_Union types
3431
3432 elsif Present (Expr_Type)
3433 and then Is_Unchecked_Union (Expr_Type)
3434 then
3435 return;
3436
feff2f05 3437 -- Scalar type conversions of the form Target_Type (Expr) require a
3438 -- range check if we cannot be sure that Expr is in the base type of
3439 -- Target_Typ and also that Expr is in the range of Target_Typ. These
3440 -- are not quite the same condition from an implementation point of
3441 -- view, but clearly the second includes the first.
ee6ba406 3442
3443 elsif Is_Scalar_Type (Target_Type) then
3444 declare
3445 Conv_OK : constant Boolean := Conversion_OK (N);
feff2f05 3446 -- If the Conversion_OK flag on the type conversion is set and no
ea822fd4 3447 -- floating-point type is involved in the type conversion then
3448 -- fixed-point values must be read as integral values.
ee6ba406 3449
5329ca64 3450 Float_To_Int : constant Boolean :=
b6341c67 3451 Is_Floating_Point_Type (Expr_Type)
3452 and then Is_Integer_Type (Target_Type);
5329ca64 3453
ee6ba406 3454 begin
ee6ba406 3455 if not Overflow_Checks_Suppressed (Target_Base)
0df9d43f 3456 and then not Overflow_Checks_Suppressed (Target_Type)
e254d721 3457 and then not
7a1dabb3 3458 In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK)
5329ca64 3459 and then not Float_To_Int
ee6ba406 3460 then
4642b679 3461 -- A small optimization: the attribute 'Pos applied to an
c59f2b2d 3462 -- enumeration type has a known range, even though its type is
3463 -- Universal_Integer. So in numeric conversions it is usually
3464 -- within range of the target integer type. Use the static
3465 -- bounds of the base types to check. Disable this optimization
3466 -- in case of a generic formal discrete type, because we don't
3467 -- necessarily know the upper bound yet.
259716a0 3468
3469 if Nkind (Expr) = N_Attribute_Reference
3470 and then Attribute_Name (Expr) = Name_Pos
3471 and then Is_Enumeration_Type (Etype (Prefix (Expr)))
c59f2b2d 3472 and then not Is_Generic_Type (Etype (Prefix (Expr)))
259716a0 3473 and then Is_Integer_Type (Target_Type)
3474 then
3475 declare
4642b679 3476 Enum_T : constant Entity_Id :=
3477 Root_Type (Etype (Prefix (Expr)));
3478 Int_T : constant Entity_Id := Base_Type (Target_Type);
3479 Last_I : constant Uint :=
3480 Intval (High_Bound (Scalar_Range (Int_T)));
3481 Last_E : Uint;
259716a0 3482
3483 begin
4642b679 3484 -- Character types have no explicit literals, so we use
259716a0 3485 -- the known number of characters in the type.
3486
3487 if Root_Type (Enum_T) = Standard_Character then
3488 Last_E := UI_From_Int (255);
3489
3490 elsif Enum_T = Standard_Wide_Character
3491 or else Enum_T = Standard_Wide_Wide_Character
3492 then
3493 Last_E := UI_From_Int (65535);
3494
3495 else
4642b679 3496 Last_E :=
3497 Enumeration_Pos
259716a0 3498 (Entity (High_Bound (Scalar_Range (Enum_T))));
3499 end if;
3500
3501 if Last_E <= Last_I then
3502 null;
3503
3504 else
3505 Activate_Overflow_Check (N);
3506 end if;
3507 end;
3508
3509 else
3510 Activate_Overflow_Check (N);
3511 end if;
ee6ba406 3512 end if;
3513
3514 if not Range_Checks_Suppressed (Target_Type)
3515 and then not Range_Checks_Suppressed (Expr_Type)
3516 then
54022749 3517 if Float_To_Int
3518 and then not GNATprove_Mode
3519 then
5329ca64 3520 Apply_Float_Conversion_Check (Expr, Target_Type);
3521 else
3522 Apply_Scalar_Range_Check
3523 (Expr, Target_Type, Fixed_Int => Conv_OK);
798afddc 3524
3525 -- If the target type has predicates, we need to indicate
ea822fd4 3526 -- the need for a check, even if Determine_Range finds that
3527 -- the value is within bounds. This may be the case e.g for
3528 -- a division with a constant denominator.
798afddc 3529
3530 if Has_Predicates (Target_Type) then
3531 Enable_Range_Check (Expr);
3532 end if;
5329ca64 3533 end if;
ee6ba406 3534 end if;
3535 end;
3536
3537 elsif Comes_From_Source (N)
f40f9731 3538 and then not Discriminant_Checks_Suppressed (Target_Type)
ee6ba406 3539 and then Is_Record_Type (Target_Type)
3540 and then Is_Derived_Type (Target_Type)
3541 and then not Is_Tagged_Type (Target_Type)
3542 and then not Is_Constrained (Target_Type)
9dfe12ae 3543 and then Present (Stored_Constraint (Target_Type))
ee6ba406 3544 then
141d591a 3545 -- An unconstrained derived type may have inherited discriminant.
9dfe12ae 3546 -- Build an actual discriminant constraint list using the stored
ee6ba406 3547 -- constraint, to verify that the expression of the parent type
ea822fd4 3548 -- satisfies the constraints imposed by the (unconstrained) derived
3549 -- type. This applies to value conversions, not to view conversions
3550 -- of tagged types.
ee6ba406 3551
3552 declare
9dfe12ae 3553 Loc : constant Source_Ptr := Sloc (N);
3554 Cond : Node_Id;
3555 Constraint : Elmt_Id;
3556 Discr_Value : Node_Id;
3557 Discr : Entity_Id;
3558
3559 New_Constraints : constant Elist_Id := New_Elmt_List;
3560 Old_Constraints : constant Elist_Id :=
b6341c67 3561 Discriminant_Constraint (Expr_Type);
ee6ba406 3562
3563 begin
9dfe12ae 3564 Constraint := First_Elmt (Stored_Constraint (Target_Type));
ee6ba406 3565 while Present (Constraint) loop
3566 Discr_Value := Node (Constraint);
3567
3568 if Is_Entity_Name (Discr_Value)
3569 and then Ekind (Entity (Discr_Value)) = E_Discriminant
3570 then
3571 Discr := Corresponding_Discriminant (Entity (Discr_Value));
3572
3573 if Present (Discr)
3574 and then Scope (Discr) = Base_Type (Expr_Type)
3575 then
3576 -- Parent is constrained by new discriminant. Obtain
feff2f05 3577 -- Value of original discriminant in expression. If the
3578 -- new discriminant has been used to constrain more than
3579 -- one of the stored discriminants, this will provide the
3580 -- required consistency check.
ee6ba406 3581
55868293 3582 Append_Elmt
3583 (Make_Selected_Component (Loc,
3584 Prefix =>
9dfe12ae 3585 Duplicate_Subexpr_No_Checks
3586 (Expr, Name_Req => True),
ee6ba406 3587 Selector_Name =>
3588 Make_Identifier (Loc, Chars (Discr))),
55868293 3589 New_Constraints);
ee6ba406 3590
3591 else
3592 -- Discriminant of more remote ancestor ???
3593
3594 return;
3595 end if;
3596
feff2f05 3597 -- Derived type definition has an explicit value for this
3598 -- stored discriminant.
ee6ba406 3599
3600 else
3601 Append_Elmt
9dfe12ae 3602 (Duplicate_Subexpr_No_Checks (Discr_Value),
3603 New_Constraints);
ee6ba406 3604 end if;
3605
3606 Next_Elmt (Constraint);
3607 end loop;
3608
3609 -- Use the unconstrained expression type to retrieve the
3610 -- discriminants of the parent, and apply momentarily the
3611 -- discriminant constraint synthesized above.
3612
3613 Set_Discriminant_Constraint (Expr_Type, New_Constraints);
3614 Cond := Build_Discriminant_Checks (Expr, Expr_Type);
3615 Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
3616
3617 Insert_Action (N,
f15731c4 3618 Make_Raise_Constraint_Error (Loc,
3619 Condition => Cond,
3620 Reason => CE_Discriminant_Check_Failed));
ee6ba406 3621 end;
3622
175a6969 3623 -- For arrays, checks are set now, but conversions are applied during
3624 -- expansion, to take into accounts changes of representation. The
3625 -- checks become range checks on the base type or length checks on the
3626 -- subtype, depending on whether the target type is unconstrained or
8e802312 3627 -- constrained. Note that the range check is put on the expression of a
3628 -- type conversion, while the length check is put on the type conversion
3629 -- itself.
175a6969 3630
3631 elsif Is_Array_Type (Target_Type) then
3632 if Is_Constrained (Target_Type) then
3633 Set_Do_Length_Check (N);
3634 else
3635 Set_Do_Range_Check (Expr);
3636 end if;
ee6ba406 3637 end if;
ee6ba406 3638 end Apply_Type_Conversion_Checks;
3639
3640 ----------------------------------------------
3641 -- Apply_Universal_Integer_Attribute_Checks --
3642 ----------------------------------------------
3643
3644 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
3645 Loc : constant Source_Ptr := Sloc (N);
3646 Typ : constant Entity_Id := Etype (N);
3647
3648 begin
3649 if Inside_A_Generic then
3650 return;
3651
3652 -- Nothing to do if checks are suppressed
3653
3654 elsif Range_Checks_Suppressed (Typ)
3655 and then Overflow_Checks_Suppressed (Typ)
3656 then
3657 return;
3658
3659 -- Nothing to do if the attribute does not come from source. The
3660 -- internal attributes we generate of this type do not need checks,
3661 -- and furthermore the attempt to check them causes some circular
3662 -- elaboration orders when dealing with packed types.
3663
3664 elsif not Comes_From_Source (N) then
3665 return;
3666
9dfe12ae 3667 -- If the prefix is a selected component that depends on a discriminant
3668 -- the check may improperly expose a discriminant instead of using
3669 -- the bounds of the object itself. Set the type of the attribute to
3670 -- the base type of the context, so that a check will be imposed when
3671 -- needed (e.g. if the node appears as an index).
3672
3673 elsif Nkind (Prefix (N)) = N_Selected_Component
3674 and then Ekind (Typ) = E_Signed_Integer_Subtype
3675 and then Depends_On_Discriminant (Scalar_Range (Typ))
3676 then
3677 Set_Etype (N, Base_Type (Typ));
3678
feff2f05 3679 -- Otherwise, replace the attribute node with a type conversion node
3680 -- whose expression is the attribute, retyped to universal integer, and
3681 -- whose subtype mark is the target type. The call to analyze this
3682 -- conversion will set range and overflow checks as required for proper
3683 -- detection of an out of range value.
ee6ba406 3684
3685 else
3686 Set_Etype (N, Universal_Integer);
3687 Set_Analyzed (N, True);
3688
3689 Rewrite (N,
3690 Make_Type_Conversion (Loc,
3691 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
3692 Expression => Relocate_Node (N)));
3693
3694 Analyze_And_Resolve (N, Typ);
3695 return;
3696 end if;
ee6ba406 3697 end Apply_Universal_Integer_Attribute_Checks;
3698
07c191b0 3699 -------------------------------------
3700 -- Atomic_Synchronization_Disabled --
3701 -------------------------------------
3702
3703 -- Note: internally Disable/Enable_Atomic_Synchronization is implemented
3704 -- using a bogus check called Atomic_Synchronization. This is to make it
3705 -- more convenient to get exactly the same semantics as [Un]Suppress.
3706
3707 function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is
3708 begin
b444f81d 3709 -- If debug flag d.e is set, always return False, i.e. all atomic sync
3710 -- looks enabled, since it is never disabled.
3711
3712 if Debug_Flag_Dot_E then
3713 return False;
3714
3715 -- If debug flag d.d is set then always return True, i.e. all atomic
3716 -- sync looks disabled, since it always tests True.
3717
3718 elsif Debug_Flag_Dot_D then
3719 return True;
3720
3721 -- If entity present, then check result for that entity
3722
3723 elsif Present (E) and then Checks_May_Be_Suppressed (E) then
07c191b0 3724 return Is_Check_Suppressed (E, Atomic_Synchronization);
b444f81d 3725
3726 -- Otherwise result depends on current scope setting
3727
07c191b0 3728 else
fafc6b97 3729 return Scope_Suppress.Suppress (Atomic_Synchronization);
07c191b0 3730 end if;
3731 end Atomic_Synchronization_Disabled;
3732
ee6ba406 3733 -------------------------------
3734 -- Build_Discriminant_Checks --
3735 -------------------------------
3736
3737 function Build_Discriminant_Checks
3738 (N : Node_Id;
314a23b6 3739 T_Typ : Entity_Id) return Node_Id
ee6ba406 3740 is
3741 Loc : constant Source_Ptr := Sloc (N);
3742 Cond : Node_Id;
3743 Disc : Elmt_Id;
3744 Disc_Ent : Entity_Id;
9dfe12ae 3745 Dref : Node_Id;
ee6ba406 3746 Dval : Node_Id;
3747
84d0d4a5 3748 function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id;
3749
3750 ----------------------------------
3751 -- Aggregate_Discriminant_Value --
3752 ----------------------------------
3753
3754 function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is
3755 Assoc : Node_Id;
3756
3757 begin
feff2f05 3758 -- The aggregate has been normalized with named associations. We use
3759 -- the Chars field to locate the discriminant to take into account
3760 -- discriminants in derived types, which carry the same name as those
3761 -- in the parent.
84d0d4a5 3762
3763 Assoc := First (Component_Associations (N));
3764 while Present (Assoc) loop
3765 if Chars (First (Choices (Assoc))) = Chars (Disc) then
3766 return Expression (Assoc);
3767 else
3768 Next (Assoc);
3769 end if;
3770 end loop;
3771
3772 -- Discriminant must have been found in the loop above
3773
3774 raise Program_Error;
3775 end Aggregate_Discriminant_Val;
3776
3777 -- Start of processing for Build_Discriminant_Checks
3778
ee6ba406 3779 begin
84d0d4a5 3780 -- Loop through discriminants evolving the condition
3781
ee6ba406 3782 Cond := Empty;
3783 Disc := First_Elmt (Discriminant_Constraint (T_Typ));
3784
9dfe12ae 3785 -- For a fully private type, use the discriminants of the parent type
ee6ba406 3786
3787 if Is_Private_Type (T_Typ)
3788 and then No (Full_View (T_Typ))
3789 then
3790 Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
3791 else
3792 Disc_Ent := First_Discriminant (T_Typ);
3793 end if;
3794
3795 while Present (Disc) loop
ee6ba406 3796 Dval := Node (Disc);
3797
3798 if Nkind (Dval) = N_Identifier
3799 and then Ekind (Entity (Dval)) = E_Discriminant
3800 then
3801 Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
3802 else
9dfe12ae 3803 Dval := Duplicate_Subexpr_No_Checks (Dval);
ee6ba406 3804 end if;
3805
00f91aef 3806 -- If we have an Unchecked_Union node, we can infer the discriminants
3807 -- of the node.
9dfe12ae 3808
00f91aef 3809 if Is_Unchecked_Union (Base_Type (T_Typ)) then
3810 Dref := New_Copy (
3811 Get_Discriminant_Value (
3812 First_Discriminant (T_Typ),
3813 T_Typ,
3814 Stored_Constraint (T_Typ)));
3815
84d0d4a5 3816 elsif Nkind (N) = N_Aggregate then
3817 Dref :=
3818 Duplicate_Subexpr_No_Checks
3819 (Aggregate_Discriminant_Val (Disc_Ent));
3820
00f91aef 3821 else
3822 Dref :=
3823 Make_Selected_Component (Loc,
20cf157b 3824 Prefix =>
00f91aef 3825 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
20cf157b 3826 Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent)));
00f91aef 3827
3828 Set_Is_In_Discriminant_Check (Dref);
3829 end if;
9dfe12ae 3830
ee6ba406 3831 Evolve_Or_Else (Cond,
3832 Make_Op_Ne (Loc,
20cf157b 3833 Left_Opnd => Dref,
ee6ba406 3834 Right_Opnd => Dval));
3835
3836 Next_Elmt (Disc);
3837 Next_Discriminant (Disc_Ent);
3838 end loop;
3839
3840 return Cond;
3841 end Build_Discriminant_Checks;
3842
13dbf220 3843 ------------------
3844 -- Check_Needed --
3845 ------------------
3846
3847 function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is
3848 N : Node_Id;
3849 P : Node_Id;
3850 K : Node_Kind;
3851 L : Node_Id;
3852 R : Node_Id;
3853
9b2068d4 3854 function Left_Expression (Op : Node_Id) return Node_Id;
3855 -- Return the relevant expression from the left operand of the given
3856 -- short circuit form: this is LO itself, except if LO is a qualified
3857 -- expression, a type conversion, or an expression with actions, in
3858 -- which case this is Left_Expression (Expression (LO)).
3859
3860 ---------------------
3861 -- Left_Expression --
3862 ---------------------
3863
3864 function Left_Expression (Op : Node_Id) return Node_Id is
3865 LE : Node_Id := Left_Opnd (Op);
3866 begin
20cf157b 3867 while Nkind_In (LE, N_Qualified_Expression,
3868 N_Type_Conversion,
3869 N_Expression_With_Actions)
9b2068d4 3870 loop
3871 LE := Expression (LE);
3872 end loop;
3873
3874 return LE;
3875 end Left_Expression;
3876
3877 -- Start of processing for Check_Needed
3878
13dbf220 3879 begin
3880 -- Always check if not simple entity
3881
3882 if Nkind (Nod) not in N_Has_Entity
3883 or else not Comes_From_Source (Nod)
3884 then
3885 return True;
3886 end if;
3887
3888 -- Look up tree for short circuit
3889
3890 N := Nod;
3891 loop
3892 P := Parent (N);
3893 K := Nkind (P);
3894
7b17e51b 3895 -- Done if out of subexpression (note that we allow generated stuff
3896 -- such as itype declarations in this context, to keep the loop going
3897 -- since we may well have generated such stuff in complex situations.
3898 -- Also done if no parent (probably an error condition, but no point
39a0c1d3 3899 -- in behaving nasty if we find it).
7b17e51b 3900
3901 if No (P)
3902 or else (K not in N_Subexpr and then Comes_From_Source (P))
3903 then
13dbf220 3904 return True;
3905
7b17e51b 3906 -- Or/Or Else case, where test is part of the right operand, or is
3907 -- part of one of the actions associated with the right operand, and
3908 -- the left operand is an equality test.
13dbf220 3909
7b17e51b 3910 elsif K = N_Op_Or then
13dbf220 3911 exit when N = Right_Opnd (P)
9b2068d4 3912 and then Nkind (Left_Expression (P)) = N_Op_Eq;
13dbf220 3913
7b17e51b 3914 elsif K = N_Or_Else then
3915 exit when (N = Right_Opnd (P)
3916 or else
3917 (Is_List_Member (N)
3918 and then List_Containing (N) = Actions (P)))
9b2068d4 3919 and then Nkind (Left_Expression (P)) = N_Op_Eq;
13dbf220 3920
7b17e51b 3921 -- Similar test for the And/And then case, where the left operand
3922 -- is an inequality test.
3923
3924 elsif K = N_Op_And then
13dbf220 3925 exit when N = Right_Opnd (P)
9b2068d4 3926 and then Nkind (Left_Expression (P)) = N_Op_Ne;
7b17e51b 3927
3928 elsif K = N_And_Then then
3929 exit when (N = Right_Opnd (P)
3930 or else
3931 (Is_List_Member (N)
20cf157b 3932 and then List_Containing (N) = Actions (P)))
9b2068d4 3933 and then Nkind (Left_Expression (P)) = N_Op_Ne;
13dbf220 3934 end if;
3935
3936 N := P;
3937 end loop;
3938
3939 -- If we fall through the loop, then we have a conditional with an
9b2068d4 3940 -- appropriate test as its left operand, so look further.
3941
3942 L := Left_Expression (P);
3943
3944 -- L is an "=" or "/=" operator: extract its operands
13dbf220 3945
13dbf220 3946 R := Right_Opnd (L);
3947 L := Left_Opnd (L);
3948
3949 -- Left operand of test must match original variable
3950
20cf157b 3951 if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then
13dbf220 3952 return True;
3953 end if;
3954
2af58f67 3955 -- Right operand of test must be key value (zero or null)
13dbf220 3956
3957 case Check is
3958 when Access_Check =>
2af58f67 3959 if not Known_Null (R) then
13dbf220 3960 return True;
3961 end if;
3962
3963 when Division_Check =>
3964 if not Compile_Time_Known_Value (R)
3965 or else Expr_Value (R) /= Uint_0
3966 then
3967 return True;
3968 end if;
2af58f67 3969
3970 when others =>
3971 raise Program_Error;
13dbf220 3972 end case;
3973
3974 -- Here we have the optimizable case, warn if not short-circuited
3975
3976 if K = N_Op_And or else K = N_Op_Or then
c4968aa2 3977 Error_Msg_Warn := SPARK_Mode /= On;
4098232e 3978
13dbf220 3979 case Check is
3980 when Access_Check =>
4098232e 3981 if GNATprove_Mode then
3982 Error_Msg_N
3983 ("Constraint_Error might have been raised (access check)",
3984 Parent (Nod));
3985 else
3986 Error_Msg_N
3987 ("Constraint_Error may be raised (access check)??",
3988 Parent (Nod));
3989 end if;
3990
13dbf220 3991 when Division_Check =>
4098232e 3992 if GNATprove_Mode then
3993 Error_Msg_N
3994 ("Constraint_Error might have been raised (zero divide)",
3995 Parent (Nod));
3996 else
3997 Error_Msg_N
3998 ("Constraint_Error may be raised (zero divide)??",
3999 Parent (Nod));
4000 end if;
2af58f67 4001
4002 when others =>
4003 raise Program_Error;
13dbf220 4004 end case;
4005
4006 if K = N_Op_And then
e977c0cf 4007 Error_Msg_N -- CODEFIX
cb97ae5c 4008 ("use `AND THEN` instead of AND??", P);
13dbf220 4009 else
e977c0cf 4010 Error_Msg_N -- CODEFIX
cb97ae5c 4011 ("use `OR ELSE` instead of OR??", P);
13dbf220 4012 end if;
4013
6fb3c314 4014 -- If not short-circuited, we need the check
13dbf220 4015
4016 return True;
4017
4018 -- If short-circuited, we can omit the check
4019
4020 else
4021 return False;
4022 end if;
4023 end Check_Needed;
4024
ee6ba406 4025 -----------------------------------
4026 -- Check_Valid_Lvalue_Subscripts --
4027 -----------------------------------
4028
4029 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
4030 begin
4031 -- Skip this if range checks are suppressed
4032
4033 if Range_Checks_Suppressed (Etype (Expr)) then
4034 return;
4035
feff2f05 4036 -- Only do this check for expressions that come from source. We assume
4037 -- that expander generated assignments explicitly include any necessary
4038 -- checks. Note that this is not just an optimization, it avoids
39a0c1d3 4039 -- infinite recursions.
ee6ba406 4040
4041 elsif not Comes_From_Source (Expr) then
4042 return;
4043
4044 -- For a selected component, check the prefix
4045
4046 elsif Nkind (Expr) = N_Selected_Component then
4047 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
4048 return;
4049
4050 -- Case of indexed component
4051
4052 elsif Nkind (Expr) = N_Indexed_Component then
4053 Apply_Subscript_Validity_Checks (Expr);
4054
feff2f05 4055 -- Prefix may itself be or contain an indexed component, and these
4056 -- subscripts need checking as well.
ee6ba406 4057
4058 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
4059 end if;
4060 end Check_Valid_Lvalue_Subscripts;
4061
fa7497e8 4062 ----------------------------------
4063 -- Null_Exclusion_Static_Checks --
4064 ----------------------------------
4065
e9998840 4066 procedure Null_Exclusion_Static_Checks
69733a9a 4067 (N : Node_Id;
4068 Comp : Node_Id := Empty;
4069 Array_Comp : Boolean := False)
e9998840 4070 is
b1e656fb 4071 Has_Null : constant Boolean := Has_Null_Exclusion (N);
4072 Kind : constant Node_Kind := Nkind (N);
4073 Error_Nod : Node_Id;
4074 Expr : Node_Id;
4075 Typ : Entity_Id;
fa7497e8 4076
13dbf220 4077 begin
0577b0b1 4078 pragma Assert
b1e656fb 4079 (Nkind_In (Kind, N_Component_Declaration,
4080 N_Discriminant_Specification,
4081 N_Function_Specification,
4082 N_Object_Declaration,
4083 N_Parameter_Specification));
0577b0b1 4084
b1e656fb 4085 if Kind = N_Function_Specification then
0577b0b1 4086 Typ := Etype (Defining_Entity (N));
4087 else
4088 Typ := Etype (Defining_Identifier (N));
4089 end if;
fa7497e8 4090
b1e656fb 4091 case Kind is
13dbf220 4092 when N_Component_Declaration =>
4093 if Present (Access_Definition (Component_Definition (N))) then
b1e656fb 4094 Error_Nod := Component_Definition (N);
13dbf220 4095 else
b1e656fb 4096 Error_Nod := Subtype_Indication (Component_Definition (N));
13dbf220 4097 end if;
5329ca64 4098
0577b0b1 4099 when N_Discriminant_Specification =>
b1e656fb 4100 Error_Nod := Discriminant_Type (N);
0577b0b1 4101
4102 when N_Function_Specification =>
b1e656fb 4103 Error_Nod := Result_Definition (N);
0577b0b1 4104
4105 when N_Object_Declaration =>
b1e656fb 4106 Error_Nod := Object_Definition (N);
0577b0b1 4107
4108 when N_Parameter_Specification =>
b1e656fb 4109 Error_Nod := Parameter_Type (N);
0577b0b1 4110
13dbf220 4111 when others =>
4112 raise Program_Error;
4113 end case;
5329ca64 4114
0577b0b1 4115 if Has_Null then
5329ca64 4116
0577b0b1 4117 -- Enforce legality rule 3.10 (13): A null exclusion can only be
4118 -- applied to an access [sub]type.
5329ca64 4119
0577b0b1 4120 if not Is_Access_Type (Typ) then
503f7fd3 4121 Error_Msg_N
b1e656fb 4122 ("`NOT NULL` allowed only for an access type", Error_Nod);
5329ca64 4123
feff2f05 4124 -- Enforce legality rule RM 3.10(14/1): A null exclusion can only
0577b0b1 4125 -- be applied to a [sub]type that does not exclude null already.
4126
b1e656fb 4127 elsif Can_Never_Be_Null (Typ) and then Comes_From_Source (Typ) then
503f7fd3 4128 Error_Msg_NE
00c403ee 4129 ("`NOT NULL` not allowed (& already excludes null)",
b1e656fb 4130 Error_Nod, Typ);
0577b0b1 4131 end if;
13dbf220 4132 end if;
5329ca64 4133
cc60bd16 4134 -- Check that null-excluding objects are always initialized, except for
4135 -- deferred constants, for which the expression will appear in the full
4136 -- declaration.
13dbf220 4137
b1e656fb 4138 if Kind = N_Object_Declaration
84d0d4a5 4139 and then No (Expression (N))
cc60bd16 4140 and then not Constant_Present (N)
feff2f05 4141 and then not No_Initialization (N)
13dbf220 4142 then
e9998840 4143 if Present (Comp) then
4144
b1ff36e7 4145 -- Specialize the warning message to indicate that we are dealing
e9998840 4146 -- with an uninitialized composite object that has a defaulted
4147 -- null-excluding component.
4148
4149 Error_Msg_Name_1 := Chars (Defining_Identifier (Comp));
4150 Error_Msg_Name_2 := Chars (Defining_Identifier (N));
4151
69733a9a 4152 Discard_Node
4153 (Compile_Time_Constraint_Error
4154 (N => N,
4155 Msg =>
4156 "(Ada 2005) null-excluding component % of object % must "
4157 & "be initialized??",
4158 Ent => Defining_Identifier (Comp)));
4159
4160 -- This is a case of an array with null-excluding components, so
4161 -- indicate that in the warning.
4162
4163 elsif Array_Comp then
4164 Discard_Node
4165 (Compile_Time_Constraint_Error
4166 (N => N,
4167 Msg =>
4168 "(Ada 2005) null-excluding array components must "
4169 & "be initialized??",
4170 Ent => Defining_Identifier (N)));
4171
4172 -- Normal case of object of a null-excluding access type
b1ff36e7 4173
e9998840 4174 else
69733a9a 4175 -- Add an expression that assigns null. This node is needed by
4176 -- Apply_Compile_Time_Constraint_Error, which will replace this
4177 -- with a Constraint_Error node.
4178
4179 Set_Expression (N, Make_Null (Sloc (N)));
4180 Set_Etype (Expression (N), Etype (Defining_Identifier (N)));
4181
e9998840 4182 Apply_Compile_Time_Constraint_Error
4183 (N => Expression (N),
4184 Msg =>
4185 "(Ada 2005) null-excluding objects must be initialized??",
4186 Reason => CE_Null_Not_Allowed);
4187 end if;
13dbf220 4188 end if;
5329ca64 4189
cc60bd16 4190 -- Check that a null-excluding component, formal or object is not being
4191 -- assigned a null value. Otherwise generate a warning message and
2c145f84 4192 -- replace Expression (N) by an N_Constraint_Error node.
13dbf220 4193
b1e656fb 4194 if Kind /= N_Function_Specification then
0577b0b1 4195 Expr := Expression (N);
5329ca64 4196
2af58f67 4197 if Present (Expr) and then Known_Null (Expr) then
b1e656fb 4198 case Kind is
99378362 4199 when N_Component_Declaration
4200 | N_Discriminant_Specification
4201 =>
7189d17f 4202 Apply_Compile_Time_Constraint_Error
0577b0b1 4203 (N => Expr,
99378362 4204 Msg =>
4205 "(Ada 2005) null not allowed in null-excluding "
4206 & "components??",
0577b0b1 4207 Reason => CE_Null_Not_Allowed);
5329ca64 4208
0577b0b1 4209 when N_Object_Declaration =>
7189d17f 4210 Apply_Compile_Time_Constraint_Error
0577b0b1 4211 (N => Expr,
99378362 4212 Msg =>
4213 "(Ada 2005) null not allowed in null-excluding "
4214 & "objects??",
0577b0b1 4215 Reason => CE_Null_Not_Allowed);
5329ca64 4216
0577b0b1 4217 when N_Parameter_Specification =>
7189d17f 4218 Apply_Compile_Time_Constraint_Error
0577b0b1 4219 (N => Expr,
99378362 4220 Msg =>
4221 "(Ada 2005) null not allowed in null-excluding "
4222 & "formals??",
0577b0b1 4223 Reason => CE_Null_Not_Allowed);
13dbf220 4224
4225 when others =>
4226 null;
5329ca64 4227 end case;
4228 end if;
0577b0b1 4229 end if;
fa7497e8 4230 end Null_Exclusion_Static_Checks;
4231
9dfe12ae 4232 ----------------------------------
4233 -- Conditional_Statements_Begin --
4234 ----------------------------------
4235
4236 procedure Conditional_Statements_Begin is
4237 begin
4238 Saved_Checks_TOS := Saved_Checks_TOS + 1;
4239
feff2f05 4240 -- If stack overflows, kill all checks, that way we know to simply reset
4241 -- the number of saved checks to zero on return. This should never occur
4242 -- in practice.
9dfe12ae 4243
4244 if Saved_Checks_TOS > Saved_Checks_Stack'Last then
4245 Kill_All_Checks;
4246
feff2f05 4247 -- In the normal case, we just make a new stack entry saving the current
4248 -- number of saved checks for a later restore.
9dfe12ae 4249
4250 else
4251 Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks;
4252
4253 if Debug_Flag_CC then
4254 w ("Conditional_Statements_Begin: Num_Saved_Checks = ",
4255 Num_Saved_Checks);
4256 end if;
4257 end if;
4258 end Conditional_Statements_Begin;
4259
4260 --------------------------------
4261 -- Conditional_Statements_End --
4262 --------------------------------
4263
4264 procedure Conditional_Statements_End is
4265 begin
4266 pragma Assert (Saved_Checks_TOS > 0);
4267
feff2f05 4268 -- If the saved checks stack overflowed, then we killed all checks, so
4269 -- setting the number of saved checks back to zero is correct. This
4270 -- should never occur in practice.
9dfe12ae 4271
4272 if Saved_Checks_TOS > Saved_Checks_Stack'Last then
4273 Num_Saved_Checks := 0;
4274
feff2f05 4275 -- In the normal case, restore the number of saved checks from the top
4276 -- stack entry.
9dfe12ae 4277
4278 else
4279 Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS);
20cf157b 4280
9dfe12ae 4281 if Debug_Flag_CC then
4282 w ("Conditional_Statements_End: Num_Saved_Checks = ",
4283 Num_Saved_Checks);
4284 end if;
4285 end if;
4286
4287 Saved_Checks_TOS := Saved_Checks_TOS - 1;
4288 end Conditional_Statements_End;
4289
3cce7f32 4290 -------------------------
4291 -- Convert_From_Bignum --
4292 -------------------------
4293
4294 function Convert_From_Bignum (N : Node_Id) return Node_Id is
4295 Loc : constant Source_Ptr := Sloc (N);
4296
4297 begin
4298 pragma Assert (Is_RTE (Etype (N), RE_Bignum));
4299
4300 -- Construct call From Bignum
4301
4302 return
4303 Make_Function_Call (Loc,
4304 Name =>
4305 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4306 Parameter_Associations => New_List (Relocate_Node (N)));
4307 end Convert_From_Bignum;
4308
4309 -----------------------
4310 -- Convert_To_Bignum --
4311 -----------------------
4312
4313 function Convert_To_Bignum (N : Node_Id) return Node_Id is
4314 Loc : constant Source_Ptr := Sloc (N);
4315
4316 begin
0326b4d4 4317 -- Nothing to do if Bignum already except call Relocate_Node
3cce7f32 4318
4319 if Is_RTE (Etype (N), RE_Bignum) then
4320 return Relocate_Node (N);
4321
21a55437 4322 -- Otherwise construct call to To_Bignum, converting the operand to the
4323 -- required Long_Long_Integer form.
3cce7f32 4324
4325 else
4326 pragma Assert (Is_Signed_Integer_Type (Etype (N)));
4327 return
4328 Make_Function_Call (Loc,
4329 Name =>
4330 New_Occurrence_Of (RTE (RE_To_Bignum), Loc),
4331 Parameter_Associations => New_List (
4332 Convert_To (Standard_Long_Long_Integer, Relocate_Node (N))));
4333 end if;
4334 end Convert_To_Bignum;
4335
ee6ba406 4336 ---------------------
4337 -- Determine_Range --
4338 ---------------------
4339
6af1bdbc 4340 Cache_Size : constant := 2 ** 10;
ee6ba406 4341 type Cache_Index is range 0 .. Cache_Size - 1;
39a0c1d3 4342 -- Determine size of below cache (power of 2 is more efficient)
ee6ba406 4343
7ac8c2b1 4344 Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
4345 Determine_Range_Cache_V : array (Cache_Index) of Boolean;
4346 Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
4347 Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
4348 Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal;
4349 Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal;
feff2f05 4350 -- The above arrays are used to implement a small direct cache for
7ac8c2b1 4351 -- Determine_Range and Determine_Range_R calls. Because of the way these
4352 -- subprograms recursively traces subexpressions, and because overflow
4353 -- checking calls the routine on the way up the tree, a quadratic behavior
4354 -- can otherwise be encountered in large expressions. The cache entry for
4355 -- node N is stored in the (N mod Cache_Size) entry, and can be validated
4356 -- by checking the actual node value stored there. The Range_Cache_V array
4357 -- records the setting of Assume_Valid for the cache entry.
ee6ba406 4358
4359 procedure Determine_Range
9c486805 4360 (N : Node_Id;
4361 OK : out Boolean;
4362 Lo : out Uint;
4363 Hi : out Uint;
4364 Assume_Valid : Boolean := False)
ee6ba406 4365 is
e254d721 4366 Typ : Entity_Id := Etype (N);
4367 -- Type to use, may get reset to base type for possibly invalid entity
8880be85 4368
4369 Lo_Left : Uint;
4370 Hi_Left : Uint;
4371 -- Lo and Hi bounds of left operand
ee6ba406 4372
ee6ba406 4373 Lo_Right : Uint;
ee6ba406 4374 Hi_Right : Uint;
8880be85 4375 -- Lo and Hi bounds of right (or only) operand
4376
4377 Bound : Node_Id;
4378 -- Temp variable used to hold a bound node
4379
4380 Hbound : Uint;
4381 -- High bound of base type of expression
4382
4383 Lor : Uint;
4384 Hir : Uint;
4385 -- Refined values for low and high bounds, after tightening
4386
4387 OK1 : Boolean;
4388 -- Used in lower level calls to indicate if call succeeded
4389
4390 Cindex : Cache_Index;
4391 -- Used to search cache
ee6ba406 4392
094ed68e 4393 Btyp : Entity_Id;
4394 -- Base type
4395
ee6ba406 4396 function OK_Operands return Boolean;
4397 -- Used for binary operators. Determines the ranges of the left and
4398 -- right operands, and if they are both OK, returns True, and puts
341bd953 4399 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left.
ee6ba406 4400
4401 -----------------
4402 -- OK_Operands --
4403 -----------------
4404
4405 function OK_Operands return Boolean is
4406 begin
9c486805 4407 Determine_Range
4408 (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid);
ee6ba406 4409
4410 if not OK1 then
4411 return False;
4412 end if;
4413
9c486805 4414 Determine_Range
4415 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
ee6ba406 4416 return OK1;
4417 end OK_Operands;
4418
4419 -- Start of processing for Determine_Range
4420
4421 begin
e12b2502 4422 -- Prevent junk warnings by initializing range variables
4423
4424 Lo := No_Uint;
4425 Hi := No_Uint;
4426 Lor := No_Uint;
4427 Hir := No_Uint;
4428
87bdc21d 4429 -- For temporary constants internally generated to remove side effects
4430 -- we must use the corresponding expression to determine the range of
e12b2502 4431 -- the expression. But note that the expander can also generate
4432 -- constants in other cases, including deferred constants.
87bdc21d 4433
4434 if Is_Entity_Name (N)
4435 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4436 and then Ekind (Entity (N)) = E_Constant
4437 and then Is_Internal_Name (Chars (Entity (N)))
4438 then
e12b2502 4439 if Present (Expression (Parent (Entity (N)))) then
4440 Determine_Range
4441 (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid);
87bdc21d 4442
e12b2502 4443 elsif Present (Full_View (Entity (N))) then
4444 Determine_Range
4445 (Expression (Parent (Full_View (Entity (N)))),
4446 OK, Lo, Hi, Assume_Valid);
ee6ba406 4447
e12b2502 4448 else
4449 OK := False;
4450 end if;
4451 return;
4452 end if;
ee6ba406 4453
a781c0fc 4454 -- If type is not defined, we can't determine its range
ee6ba406 4455
a781c0fc 4456 if No (Typ)
4457
4458 -- We don't deal with anything except discrete types
4459
4460 or else not Is_Discrete_Type (Typ)
4461
4462 -- Ignore type for which an error has been posted, since range in
4463 -- this case may well be a bogosity deriving from the error. Also
4464 -- ignore if error posted on the reference node.
4465
4466 or else Error_Posted (N) or else Error_Posted (Typ)
ee6ba406 4467 then
4468 OK := False;
4469 return;
4470 end if;
4471
4472 -- For all other cases, we can determine the range
4473
4474 OK := True;
4475
feff2f05 4476 -- If value is compile time known, then the possible range is the one
39a0c1d3 4477 -- value that we know this expression definitely has.
ee6ba406 4478
4479 if Compile_Time_Known_Value (N) then
4480 Lo := Expr_Value (N);
4481 Hi := Lo;
4482 return;
4483 end if;
4484
4485 -- Return if already in the cache
4486
4487 Cindex := Cache_Index (N mod Cache_Size);
4488
9c486805 4489 if Determine_Range_Cache_N (Cindex) = N
4490 and then
4491 Determine_Range_Cache_V (Cindex) = Assume_Valid
4492 then
ee6ba406 4493 Lo := Determine_Range_Cache_Lo (Cindex);
4494 Hi := Determine_Range_Cache_Hi (Cindex);
4495 return;
4496 end if;
4497
feff2f05 4498 -- Otherwise, start by finding the bounds of the type of the expression,
4499 -- the value cannot be outside this range (if it is, then we have an
4500 -- overflow situation, which is a separate check, we are talking here
4501 -- only about the expression value).
ee6ba406 4502
341bd953 4503 -- First a check, never try to find the bounds of a generic type, since
4504 -- these bounds are always junk values, and it is only valid to look at
4505 -- the bounds in an instance.
4506
4507 if Is_Generic_Type (Typ) then
4508 OK := False;
4509 return;
4510 end if;
4511
9c486805 4512 -- First step, change to use base type unless we know the value is valid
e254d721 4513
9c486805 4514 if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N)))
4515 or else Assume_No_Invalid_Values
4516 or else Assume_Valid
e254d721 4517 then
9c486805 4518 null;
4519 else
4520 Typ := Underlying_Type (Base_Type (Typ));
e254d721 4521 end if;
4522
094ed68e 4523 -- Retrieve the base type. Handle the case where the base type is a
4524 -- private enumeration type.
4525
4526 Btyp := Base_Type (Typ);
4527
4528 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
4529 Btyp := Full_View (Btyp);
4530 end if;
4531
feff2f05 4532 -- We use the actual bound unless it is dynamic, in which case use the
4533 -- corresponding base type bound if possible. If we can't get a bound
4534 -- then we figure we can't determine the range (a peculiar case, that
4535 -- perhaps cannot happen, but there is no point in bombing in this
4536 -- optimization circuit.
8880be85 4537
4538 -- First the low bound
ee6ba406 4539
4540 Bound := Type_Low_Bound (Typ);
4541
4542 if Compile_Time_Known_Value (Bound) then
4543 Lo := Expr_Value (Bound);
4544
094ed68e 4545 elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then
4546 Lo := Expr_Value (Type_Low_Bound (Btyp));
ee6ba406 4547
4548 else
4549 OK := False;
4550 return;
4551 end if;
4552
8880be85 4553 -- Now the high bound
4554
ee6ba406 4555 Bound := Type_High_Bound (Typ);
4556
8880be85 4557 -- We need the high bound of the base type later on, and this should
4558 -- always be compile time known. Again, it is not clear that this
4559 -- can ever be false, but no point in bombing.
ee6ba406 4560
094ed68e 4561 if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then
4562 Hbound := Expr_Value (Type_High_Bound (Btyp));
ee6ba406 4563 Hi := Hbound;
4564
4565 else
4566 OK := False;
4567 return;
4568 end if;
4569
feff2f05 4570 -- If we have a static subtype, then that may have a tighter bound so
4571 -- use the upper bound of the subtype instead in this case.
8880be85 4572
4573 if Compile_Time_Known_Value (Bound) then
4574 Hi := Expr_Value (Bound);
4575 end if;
4576
feff2f05 4577 -- We may be able to refine this value in certain situations. If any
4578 -- refinement is possible, then Lor and Hir are set to possibly tighter
4579 -- bounds, and OK1 is set to True.
ee6ba406 4580
4581 case Nkind (N) is
4582
4583 -- For unary plus, result is limited by range of operand
4584
4585 when N_Op_Plus =>
9c486805 4586 Determine_Range
4587 (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid);
ee6ba406 4588
4589 -- For unary minus, determine range of operand, and negate it
4590
4591 when N_Op_Minus =>
9c486805 4592 Determine_Range
4593 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
ee6ba406 4594
4595 if OK1 then
4596 Lor := -Hi_Right;
4597 Hir := -Lo_Right;
4598 end if;
4599
4600 -- For binary addition, get range of each operand and do the
4601 -- addition to get the result range.
4602
4603 when N_Op_Add =>
4604 if OK_Operands then
4605 Lor := Lo_Left + Lo_Right;
4606 Hir := Hi_Left + Hi_Right;
4607 end if;
4608
feff2f05 4609 -- Division is tricky. The only case we consider is where the right
4610 -- operand is a positive constant, and in this case we simply divide
4611 -- the bounds of the left operand
ee6ba406 4612
4613 when N_Op_Divide =>
4614 if OK_Operands then
4615 if Lo_Right = Hi_Right
4616 and then Lo_Right > 0
4617 then
4618 Lor := Lo_Left / Lo_Right;
4619 Hir := Hi_Left / Lo_Right;
ee6ba406 4620 else
4621 OK1 := False;
4622 end if;
4623 end if;
4624
feff2f05 4625 -- For binary subtraction, get range of each operand and do the worst
4626 -- case subtraction to get the result range.
ee6ba406 4627
4628 when N_Op_Subtract =>
4629 if OK_Operands then
4630 Lor := Lo_Left - Hi_Right;
4631 Hir := Hi_Left - Lo_Right;
4632 end if;
4633
feff2f05 4634 -- For MOD, if right operand is a positive constant, then result must
4635 -- be in the allowable range of mod results.
ee6ba406 4636
4637 when N_Op_Mod =>
4638 if OK_Operands then
9dfe12ae 4639 if Lo_Right = Hi_Right
4640 and then Lo_Right /= 0
4641 then
ee6ba406 4642 if Lo_Right > 0 then
4643 Lor := Uint_0;
4644 Hir := Lo_Right - 1;
4645
9dfe12ae 4646 else -- Lo_Right < 0
ee6ba406 4647 Lor := Lo_Right + 1;
4648 Hir := Uint_0;
4649 end if;
4650
4651 else
4652 OK1 := False;
4653 end if;
4654 end if;
4655
feff2f05 4656 -- For REM, if right operand is a positive constant, then result must
4657 -- be in the allowable range of mod results.
ee6ba406 4658
4659 when N_Op_Rem =>
4660 if OK_Operands then
99378362 4661 if Lo_Right = Hi_Right and then Lo_Right /= 0 then
ee6ba406 4662 declare
4663 Dval : constant Uint := (abs Lo_Right) - 1;
4664
4665 begin
4666 -- The sign of the result depends on the sign of the
4667 -- dividend (but not on the sign of the divisor, hence
4668 -- the abs operation above).
4669
4670 if Lo_Left < 0 then
4671 Lor := -Dval;
4672 else
4673 Lor := Uint_0;
4674 end if;
4675
4676 if Hi_Left < 0 then
4677 Hir := Uint_0;
4678 else
4679 Hir := Dval;
4680 end if;
4681 end;
4682
4683 else
4684 OK1 := False;
4685 end if;
4686 end if;
4687
4688 -- Attribute reference cases
4689
4690 when N_Attribute_Reference =>
4691 case Attribute_Name (N) is
4692
4693 -- For Pos/Val attributes, we can refine the range using the
ddbf7f2e 4694 -- possible range of values of the attribute expression.
ee6ba406 4695
99378362 4696 when Name_Pos
4697 | Name_Val
4698 =>
9c486805 4699 Determine_Range
4700 (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid);
ee6ba406 4701
4702 -- For Length attribute, use the bounds of the corresponding
4703 -- index type to refine the range.
4704
4705 when Name_Length =>
4706 declare
4707 Atyp : Entity_Id := Etype (Prefix (N));
4708 Inum : Nat;
4709 Indx : Node_Id;
4710
4711 LL, LU : Uint;
4712 UL, UU : Uint;
4713
4714 begin
4715 if Is_Access_Type (Atyp) then
4716 Atyp := Designated_Type (Atyp);
4717 end if;
4718
4719 -- For string literal, we know exact value
4720
4721 if Ekind (Atyp) = E_String_Literal_Subtype then
4722 OK := True;
4723 Lo := String_Literal_Length (Atyp);
4724 Hi := String_Literal_Length (Atyp);
4725 return;
4726 end if;
4727
4728 -- Otherwise check for expression given
4729
4730 if No (Expressions (N)) then
4731 Inum := 1;
4732 else
4733 Inum :=
4734 UI_To_Int (Expr_Value (First (Expressions (N))));
4735 end if;
4736
4737 Indx := First_Index (Atyp);
4738 for J in 2 .. Inum loop
4739 Indx := Next_Index (Indx);
4740 end loop;
4741
9116df93 4742 -- If the index type is a formal type or derived from
c8da6114 4743 -- one, the bounds are not static.
4744
4745 if Is_Generic_Type (Root_Type (Etype (Indx))) then
4746 OK := False;
4747 return;
4748 end if;
4749
ee6ba406 4750 Determine_Range
9c486805 4751 (Type_Low_Bound (Etype (Indx)), OK1, LL, LU,
4752 Assume_Valid);
ee6ba406 4753
4754 if OK1 then
4755 Determine_Range
9c486805 4756 (Type_High_Bound (Etype (Indx)), OK1, UL, UU,
4757 Assume_Valid);
ee6ba406 4758
4759 if OK1 then
4760
4761 -- The maximum value for Length is the biggest
4762 -- possible gap between the values of the bounds.
4763 -- But of course, this value cannot be negative.
4764
9c486805 4765 Hir := UI_Max (Uint_0, UU - LL + 1);
ee6ba406 4766
4767 -- For constrained arrays, the minimum value for
4768 -- Length is taken from the actual value of the
9116df93 4769 -- bounds, since the index will be exactly of this
4770 -- subtype.
ee6ba406 4771
4772 if Is_Constrained (Atyp) then
9c486805 4773 Lor := UI_Max (Uint_0, UL - LU + 1);
ee6ba406 4774
4775 -- For an unconstrained array, the minimum value
4776 -- for length is always zero.
4777
4778 else
4779 Lor := Uint_0;
4780 end if;
4781 end if;
4782 end if;
4783 end;
4784
4785 -- No special handling for other attributes
9116df93 4786 -- Probably more opportunities exist here???
ee6ba406 4787
4788 when others =>
4789 OK1 := False;
4790
4791 end case;
4792
ee6ba406 4793 when N_Type_Conversion =>
54022749 4794
4795 -- For type conversion from one discrete type to another, we can
4796 -- refine the range using the converted value.
4797
4798 if Is_Discrete_Type (Etype (Expression (N))) then
4799 Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid);
4800
4801 -- When converting a float to an integer type, determine the range
4802 -- in real first, and then convert the bounds using UR_To_Uint
4803 -- which correctly rounds away from zero when half way between two
4804 -- integers, as required by normal Ada 95 rounding semantics. It
4805 -- is only possible because analysis in GNATprove rules out the
4806 -- possibility of a NaN or infinite value.
4807
4808 elsif GNATprove_Mode
4809 and then Is_Floating_Point_Type (Etype (Expression (N)))
4810 then
4811 declare
4812 Lor_Real, Hir_Real : Ureal;
4813 begin
4814 Determine_Range_R (Expression (N), OK1, Lor_Real, Hir_Real,
4815 Assume_Valid);
4816
4817 if OK1 then
4818 Lor := UR_To_Uint (Lor_Real);
4819 Hir := UR_To_Uint (Hir_Real);
4820 end if;
4821 end;
4822
4823 else
4824 OK1 := False;
4825 end if;
ee6ba406 4826
4827 -- Nothing special to do for all other expression kinds
4828
4829 when others =>
4830 OK1 := False;
4831 Lor := No_Uint;
4832 Hir := No_Uint;
4833 end case;
4834
9116df93 4835 -- At this stage, if OK1 is true, then we know that the actual result of
4836 -- the computed expression is in the range Lor .. Hir. We can use this
4837 -- to restrict the possible range of results.
ee6ba406 4838
4839 if OK1 then
4840
9116df93 4841 -- If the refined value of the low bound is greater than the type
7ac8c2b1 4842 -- low bound, then reset it to the more restrictive value. However,
9116df93 4843 -- we do NOT do this for the case of a modular type where the
4844 -- possible upper bound on the value is above the base type high
4845 -- bound, because that means the result could wrap.
ee6ba406 4846
4847 if Lor > Lo
9116df93 4848 and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound)
ee6ba406 4849 then
4850 Lo := Lor;
4851 end if;
4852
9116df93 4853 -- Similarly, if the refined value of the high bound is less than the
4854 -- value so far, then reset it to the more restrictive value. Again,
4855 -- we do not do this if the refined low bound is negative for a
4856 -- modular type, since this would wrap.
ee6ba406 4857
4858 if Hir < Hi
9116df93 4859 and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0)
ee6ba406 4860 then
4861 Hi := Hir;
4862 end if;
4863 end if;
4864
4865 -- Set cache entry for future call and we are all done
4866
4867 Determine_Range_Cache_N (Cindex) := N;
9c486805 4868 Determine_Range_Cache_V (Cindex) := Assume_Valid;
ee6ba406 4869 Determine_Range_Cache_Lo (Cindex) := Lo;
4870 Determine_Range_Cache_Hi (Cindex) := Hi;
4871 return;
4872
9116df93 4873 -- If any exception occurs, it means that we have some bug in the compiler,
4874 -- possibly triggered by a previous error, or by some unforeseen peculiar
ee6ba406 4875 -- occurrence. However, this is only an optimization attempt, so there is
4876 -- really no point in crashing the compiler. Instead we just decide, too
4877 -- bad, we can't figure out a range in this case after all.
4878
4879 exception
4880 when others =>
4881
4882 -- Debug flag K disables this behavior (useful for debugging)
4883
4884 if Debug_Flag_K then
4885 raise;
4886 else
4887 OK := False;
4888 Lo := No_Uint;
4889 Hi := No_Uint;
4890 return;
4891 end if;
ee6ba406 4892 end Determine_Range;
4893
7ac8c2b1 4894 -----------------------
4895 -- Determine_Range_R --
4896 -----------------------
4897
4898 procedure Determine_Range_R
4899 (N : Node_Id;
4900 OK : out Boolean;
4901 Lo : out Ureal;
4902 Hi : out Ureal;
4903 Assume_Valid : Boolean := False)
4904 is
4905 Typ : Entity_Id := Etype (N);
4906 -- Type to use, may get reset to base type for possibly invalid entity
4907
4908 Lo_Left : Ureal;
4909 Hi_Left : Ureal;
4910 -- Lo and Hi bounds of left operand
4911
4912 Lo_Right : Ureal;
4913 Hi_Right : Ureal;
4914 -- Lo and Hi bounds of right (or only) operand
4915
4916 Bound : Node_Id;
4917 -- Temp variable used to hold a bound node
4918
4919 Hbound : Ureal;
4920 -- High bound of base type of expression
4921
4922 Lor : Ureal;
4923 Hir : Ureal;
4924 -- Refined values for low and high bounds, after tightening
4925
4926 OK1 : Boolean;
4927 -- Used in lower level calls to indicate if call succeeded
4928
4929 Cindex : Cache_Index;
4930 -- Used to search cache
4931
4932 Btyp : Entity_Id;
4933 -- Base type
4934
4935 function OK_Operands return Boolean;
4936 -- Used for binary operators. Determines the ranges of the left and
4937 -- right operands, and if they are both OK, returns True, and puts
4938 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left.
4939
4940 function Round_Machine (B : Ureal) return Ureal;
4941 -- B is a real bound. Round it using mode Round_Even.
4942
4943 -----------------
4944 -- OK_Operands --
4945 -----------------
4946
4947 function OK_Operands return Boolean is
4948 begin
4949 Determine_Range_R
4950 (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid);
4951
4952 if not OK1 then
4953 return False;
4954 end if;
4955
4956 Determine_Range_R
4957 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
4958 return OK1;
4959 end OK_Operands;
4960
4961 -------------------
4962 -- Round_Machine --
4963 -------------------
4964
4965 function Round_Machine (B : Ureal) return Ureal is
4966 begin
4967 return Machine (Typ, B, Round_Even, N);
4968 end Round_Machine;
4969
4970 -- Start of processing for Determine_Range_R
4971
4972 begin
4973 -- Prevent junk warnings by initializing range variables
4974
4975 Lo := No_Ureal;
4976 Hi := No_Ureal;
4977 Lor := No_Ureal;
4978 Hir := No_Ureal;
4979
4980 -- For temporary constants internally generated to remove side effects
4981 -- we must use the corresponding expression to determine the range of
4982 -- the expression. But note that the expander can also generate
4983 -- constants in other cases, including deferred constants.
4984
4985 if Is_Entity_Name (N)
4986 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4987 and then Ekind (Entity (N)) = E_Constant
4988 and then Is_Internal_Name (Chars (Entity (N)))
4989 then
4990 if Present (Expression (Parent (Entity (N)))) then
4991 Determine_Range_R
4992 (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid);
4993
4994 elsif Present (Full_View (Entity (N))) then
4995 Determine_Range_R
4996 (Expression (Parent (Full_View (Entity (N)))),
4997 OK, Lo, Hi, Assume_Valid);
4998
4999 else
5000 OK := False;
5001 end if;
4c1c7f3f 5002
7ac8c2b1 5003 return;
5004 end if;
5005
5006 -- If type is not defined, we can't determine its range
5007
5008 if No (Typ)
5009
5010 -- We don't deal with anything except IEEE floating-point types
5011
5012 or else not Is_Floating_Point_Type (Typ)
5013 or else Float_Rep (Typ) /= IEEE_Binary
5014
5015 -- Ignore type for which an error has been posted, since range in
5016 -- this case may well be a bogosity deriving from the error. Also
5017 -- ignore if error posted on the reference node.
5018
5019 or else Error_Posted (N) or else Error_Posted (Typ)
5020 then
5021 OK := False;
5022 return;
5023 end if;
5024
5025 -- For all other cases, we can determine the range
5026
5027 OK := True;
5028
5029 -- If value is compile time known, then the possible range is the one
5030 -- value that we know this expression definitely has.
5031
5032 if Compile_Time_Known_Value (N) then
5033 Lo := Expr_Value_R (N);
5034 Hi := Lo;
5035 return;
5036 end if;
5037
5038 -- Return if already in the cache
5039
5040 Cindex := Cache_Index (N mod Cache_Size);
5041
5042 if Determine_Range_Cache_N (Cindex) = N
5043 and then
5044 Determine_Range_Cache_V (Cindex) = Assume_Valid
5045 then
5046 Lo := Determine_Range_Cache_Lo_R (Cindex);
5047 Hi := Determine_Range_Cache_Hi_R (Cindex);
5048 return;
5049 end if;
5050
5051 -- Otherwise, start by finding the bounds of the type of the expression,
5052 -- the value cannot be outside this range (if it is, then we have an
5053 -- overflow situation, which is a separate check, we are talking here
5054 -- only about the expression value).
5055
5056 -- First a check, never try to find the bounds of a generic type, since
5057 -- these bounds are always junk values, and it is only valid to look at
5058 -- the bounds in an instance.
5059
5060 if Is_Generic_Type (Typ) then
5061 OK := False;
5062 return;
5063 end if;
5064
5065 -- First step, change to use base type unless we know the value is valid
5066
5067 if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N)))
5068 or else Assume_No_Invalid_Values
5069 or else Assume_Valid
5070 then
5071 null;
5072 else
5073 Typ := Underlying_Type (Base_Type (Typ));
5074 end if;
5075
5076 -- Retrieve the base type. Handle the case where the base type is a
5077 -- private type.
5078
5079 Btyp := Base_Type (Typ);
5080
5081 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
5082 Btyp := Full_View (Btyp);
5083 end if;
5084
5085 -- We use the actual bound unless it is dynamic, in which case use the
5086 -- corresponding base type bound if possible. If we can't get a bound
5087 -- then we figure we can't determine the range (a peculiar case, that
5088 -- perhaps cannot happen, but there is no point in bombing in this
5089 -- optimization circuit).
5090
5091 -- First the low bound
5092
5093 Bound := Type_Low_Bound (Typ);
5094
5095 if Compile_Time_Known_Value (Bound) then
5096 Lo := Expr_Value_R (Bound);
5097
5098 elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then
5099 Lo := Expr_Value_R (Type_Low_Bound (Btyp));
5100
5101 else
5102 OK := False;
5103 return;
5104 end if;
5105
5106 -- Now the high bound
5107
5108 Bound := Type_High_Bound (Typ);
5109
5110 -- We need the high bound of the base type later on, and this should
5111 -- always be compile time known. Again, it is not clear that this
5112 -- can ever be false, but no point in bombing.
5113
5114 if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then
5115 Hbound := Expr_Value_R (Type_High_Bound (Btyp));
5116 Hi := Hbound;
5117
5118 else
5119 OK := False;
5120 return;
5121 end if;
5122
5123 -- If we have a static subtype, then that may have a tighter bound so
5124 -- use the upper bound of the subtype instead in this case.
5125
5126 if Compile_Time_Known_Value (Bound) then
5127 Hi := Expr_Value_R (Bound);
5128 end if;
5129
5130 -- We may be able to refine this value in certain situations. If any
5131 -- refinement is possible, then Lor and Hir are set to possibly tighter
5132 -- bounds, and OK1 is set to True.
5133
5134 case Nkind (N) is
5135
5136 -- For unary plus, result is limited by range of operand
5137
5138 when N_Op_Plus =>
5139 Determine_Range_R
5140 (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid);
5141
5142 -- For unary minus, determine range of operand, and negate it
5143
5144 when N_Op_Minus =>
5145 Determine_Range_R
5146 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
5147
5148 if OK1 then
5149 Lor := -Hi_Right;
5150 Hir := -Lo_Right;
5151 end if;
5152
5153 -- For binary addition, get range of each operand and do the
5154 -- addition to get the result range.
5155
5156 when N_Op_Add =>
5157 if OK_Operands then
5158 Lor := Round_Machine (Lo_Left + Lo_Right);
5159 Hir := Round_Machine (Hi_Left + Hi_Right);
5160 end if;
5161
5162 -- For binary subtraction, get range of each operand and do the worst
5163 -- case subtraction to get the result range.
5164
5165 when N_Op_Subtract =>
5166 if OK_Operands then
5167 Lor := Round_Machine (Lo_Left - Hi_Right);
5168 Hir := Round_Machine (Hi_Left - Lo_Right);
5169 end if;
5170
5171 -- For multiplication, get range of each operand and do the
5172 -- four multiplications to get the result range.
5173
5174 when N_Op_Multiply =>
5175 if OK_Operands then
5176 declare
5177 M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right);
5178 M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right);
5179 M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right);
5180 M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right);
552d7cbc 5181
7ac8c2b1 5182 begin
5183 Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4));
5184 Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4));
5185 end;
5186 end if;
5187
5188 -- For division, consider separately the cases where the right
5189 -- operand is positive or negative. Otherwise, the right operand
5190 -- can be arbitrarily close to zero, so the result is likely to
5191 -- be unbounded in one direction, do not attempt to compute it.
5192
5193 when N_Op_Divide =>
5194 if OK_Operands then
5195
5196 -- Right operand is positive
5197
5198 if Lo_Right > Ureal_0 then
5199
5200 -- If the low bound of the left operand is negative, obtain
5201 -- the overall low bound by dividing it by the smallest
5202 -- value of the right operand, and otherwise by the largest
5203 -- value of the right operand.
5204
5205 if Lo_Left < Ureal_0 then
5206 Lor := Round_Machine (Lo_Left / Lo_Right);
5207 else
5208 Lor := Round_Machine (Lo_Left / Hi_Right);
5209 end if;
5210
5211 -- If the high bound of the left operand is negative, obtain
5212 -- the overall high bound by dividing it by the largest
5213 -- value of the right operand, and otherwise by the
5214 -- smallest value of the right operand.
5215
5216 if Hi_Left < Ureal_0 then
5217 Hir := Round_Machine (Hi_Left / Hi_Right);
5218 else
5219 Hir := Round_Machine (Hi_Left / Lo_Right);
5220 end if;
5221
5222 -- Right operand is negative
5223
5224 elsif Hi_Right < Ureal_0 then
5225
5226 -- If the low bound of the left operand is negative, obtain
5227 -- the overall low bound by dividing it by the largest
5228 -- value of the right operand, and otherwise by the smallest
5229 -- value of the right operand.
5230
5231 if Lo_Left < Ureal_0 then
5232 Lor := Round_Machine (Lo_Left / Hi_Right);
5233 else
5234 Lor := Round_Machine (Lo_Left / Lo_Right);
5235 end if;
5236
5237 -- If the high bound of the left operand is negative, obtain
5238 -- the overall high bound by dividing it by the smallest
5239 -- value of the right operand, and otherwise by the
5240 -- largest value of the right operand.
5241
5242 if Hi_Left < Ureal_0 then
5243 Hir := Round_Machine (Hi_Left / Lo_Right);
5244 else
5245 Hir := Round_Machine (Hi_Left / Hi_Right);
5246 end if;
5247
5248 else
5249 OK1 := False;
5250 end if;
5251 end if;
5252
7ac8c2b1 5253 when N_Type_Conversion =>
3c5c732f 5254
5255 -- For type conversion from one floating-point type to another, we
5256 -- can refine the range using the converted value.
5257
5258 if Is_Floating_Point_Type (Etype (Expression (N))) then
5259 Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid);
5260
5261 -- When converting an integer to a floating-point type, determine
5262 -- the range in integer first, and then convert the bounds.
5263
5264 elsif Is_Discrete_Type (Etype (Expression (N))) then
5265 declare
552d7cbc 5266 Hir_Int : Uint;
5267 Lor_Int : Uint;
5268
3c5c732f 5269 begin
552d7cbc 5270 Determine_Range
5271 (Expression (N), OK1, Lor_Int, Hir_Int, Assume_Valid);
3c5c732f 5272
5273 if OK1 then
5274 Lor := Round_Machine (UR_From_Uint (Lor_Int));
5275 Hir := Round_Machine (UR_From_Uint (Hir_Int));
5276 end if;
5277 end;
5278
5279 else
5280 OK1 := False;
5281 end if;
7ac8c2b1 5282
5283 -- Nothing special to do for all other expression kinds
5284
5285 when others =>
5286 OK1 := False;
5287 Lor := No_Ureal;
5288 Hir := No_Ureal;
5289 end case;
5290
5291 -- At this stage, if OK1 is true, then we know that the actual result of
5292 -- the computed expression is in the range Lor .. Hir. We can use this
5293 -- to restrict the possible range of results.
5294
5295 if OK1 then
5296
5297 -- If the refined value of the low bound is greater than the type
5298 -- low bound, then reset it to the more restrictive value.
5299
5300 if Lor > Lo then
5301 Lo := Lor;
5302 end if;
5303
5304 -- Similarly, if the refined value of the high bound is less than the
5305 -- value so far, then reset it to the more restrictive value.
5306
5307 if Hir < Hi then
5308 Hi := Hir;
5309 end if;
5310 end if;
5311
5312 -- Set cache entry for future call and we are all done
5313
5314 Determine_Range_Cache_N (Cindex) := N;
5315 Determine_Range_Cache_V (Cindex) := Assume_Valid;
5316 Determine_Range_Cache_Lo_R (Cindex) := Lo;
5317 Determine_Range_Cache_Hi_R (Cindex) := Hi;
5318 return;
5319
5320 -- If any exception occurs, it means that we have some bug in the compiler,
5321 -- possibly triggered by a previous error, or by some unforeseen peculiar
5322 -- occurrence. However, this is only an optimization attempt, so there is
5323 -- really no point in crashing the compiler. Instead we just decide, too
5324 -- bad, we can't figure out a range in this case after all.
5325
5326 exception
5327 when others =>
5328
5329 -- Debug flag K disables this behavior (useful for debugging)
5330
5331 if Debug_Flag_K then
5332 raise;
5333 else
5334 OK := False;
5335 Lo := No_Ureal;
5336 Hi := No_Ureal;
5337 return;
5338 end if;
5339 end Determine_Range_R;
5340
ee6ba406 5341 ------------------------------------
5342 -- Discriminant_Checks_Suppressed --
5343 ------------------------------------
5344
5345 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
5346 begin
9dfe12ae 5347 if Present (E) then
5348 if Is_Unchecked_Union (E) then
5349 return True;
5350 elsif Checks_May_Be_Suppressed (E) then
5351 return Is_Check_Suppressed (E, Discriminant_Check);
5352 end if;
5353 end if;
5354
fafc6b97 5355 return Scope_Suppress.Suppress (Discriminant_Check);
ee6ba406 5356 end Discriminant_Checks_Suppressed;
5357
5358 --------------------------------
5359 -- Division_Checks_Suppressed --
5360 --------------------------------
5361
5362 function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
5363 begin
9dfe12ae 5364 if Present (E) and then Checks_May_Be_Suppressed (E) then
5365 return Is_Check_Suppressed (E, Division_Check);
5366 else
fafc6b97 5367 return Scope_Suppress.Suppress (Division_Check);
9dfe12ae 5368 end if;
ee6ba406 5369 end Division_Checks_Suppressed;
5370
fa771c05 5371 --------------------------------------
5372 -- Duplicated_Tag_Checks_Suppressed --
5373 --------------------------------------
5374
5375 function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
5376 begin
5377 if Present (E) and then Checks_May_Be_Suppressed (E) then
5378 return Is_Check_Suppressed (E, Duplicated_Tag_Check);
5379 else
5380 return Scope_Suppress.Suppress (Duplicated_Tag_Check);
5381 end if;
5382 end Duplicated_Tag_Checks_Suppressed;
5383
ee6ba406 5384 -----------------------------------
5385 -- Elaboration_Checks_Suppressed --
5386 -----------------------------------
5387
5388 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
5389 begin
38f5559f 5390 -- The complication in this routine is that if we are in the dynamic
5391 -- model of elaboration, we also check All_Checks, since All_Checks
5392 -- does not set Elaboration_Check explicitly.
5393
9dfe12ae 5394 if Present (E) then
5395 if Kill_Elaboration_Checks (E) then
5396 return True;
38f5559f 5397
9dfe12ae 5398 elsif Checks_May_Be_Suppressed (E) then
38f5559f 5399 if Is_Check_Suppressed (E, Elaboration_Check) then
5400 return True;
5401 elsif Dynamic_Elaboration_Checks then
5402 return Is_Check_Suppressed (E, All_Checks);
5403 else
5404 return False;
5405 end if;
9dfe12ae 5406 end if;
5407 end if;
5408
fafc6b97 5409 if Scope_Suppress.Suppress (Elaboration_Check) then
38f5559f 5410 return True;
5411 elsif Dynamic_Elaboration_Checks then
fafc6b97 5412 return Scope_Suppress.Suppress (All_Checks);
38f5559f 5413 else
5414 return False;
5415 end if;
ee6ba406 5416 end Elaboration_Checks_Suppressed;
5417
9dfe12ae 5418 ---------------------------
5419 -- Enable_Overflow_Check --
5420 ---------------------------
5421
5422 procedure Enable_Overflow_Check (N : Node_Id) is
4c1c7f3f 5423 Typ : constant Entity_Id := Base_Type (Etype (N));
db415383 5424 Mode : constant Overflow_Mode_Type := Overflow_Check_Mode;
3cce7f32 5425 Chk : Nat;
5426 OK : Boolean;
5427 Ent : Entity_Id;
5428 Ofs : Uint;
5429 Lo : Uint;
5430 Hi : Uint;
ee6ba406 5431
f3ccbbb3 5432 Do_Ovflow_Check : Boolean;
5433
ee6ba406 5434 begin
9dfe12ae 5435 if Debug_Flag_CC then
5436 w ("Enable_Overflow_Check for node ", Int (N));
5437 Write_Str (" Source location = ");
5438 wl (Sloc (N));
00c403ee 5439 pg (Union_Id (N));
ee6ba406 5440 end if;
ee6ba406 5441
75209ec5 5442 -- No check if overflow checks suppressed for type of node
5443
0df9d43f 5444 if Overflow_Checks_Suppressed (Etype (N)) then
75209ec5 5445 return;
5446
49260fa5 5447 -- Nothing to do for unsigned integer types, which do not overflow
5448
5449 elsif Is_Modular_Integer_Type (Typ) then
5450 return;
3cce7f32 5451 end if;
5452
0df9d43f 5453 -- This is the point at which processing for STRICT mode diverges
21a55437 5454 -- from processing for MINIMIZED/ELIMINATED modes. This divergence is
5455 -- probably more extreme that it needs to be, but what is going on here
5456 -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted
0df9d43f 5457 -- to leave the processing for STRICT mode untouched. There were
21a55437 5458 -- two reasons for this. First it avoided any incompatible change of
0df9d43f 5459 -- behavior. Second, it guaranteed that STRICT mode continued to be
21a55437 5460 -- legacy reliable.
3cce7f32 5461
0df9d43f 5462 -- The big difference is that in STRICT mode there is a fair amount of
3cce7f32 5463 -- circuitry to try to avoid setting the Do_Overflow_Check flag if we
5464 -- know that no check is needed. We skip all that in the two new modes,
5465 -- since really overflow checking happens over a whole subtree, and we
5466 -- do the corresponding optimizations later on when applying the checks.
5467
5468 if Mode in Minimized_Or_Eliminated then
0df9d43f 5469 if not (Overflow_Checks_Suppressed (Etype (N)))
5470 and then not (Is_Entity_Name (N)
5471 and then Overflow_Checks_Suppressed (Entity (N)))
5472 then
5473 Activate_Overflow_Check (N);
5474 end if;
3cce7f32 5475
5476 if Debug_Flag_CC then
5477 w ("Minimized/Eliminated mode");
5478 end if;
5479
5480 return;
5481 end if;
5482
0df9d43f 5483 -- Remainder of processing is for STRICT case, and is unchanged from
691fe9e0 5484 -- earlier versions preceding the addition of MINIMIZED/ELIMINATED.
49260fa5 5485
feff2f05 5486 -- Nothing to do if the range of the result is known OK. We skip this
5487 -- for conversions, since the caller already did the check, and in any
5488 -- case the condition for deleting the check for a type conversion is
cc60bd16 5489 -- different.
ee6ba406 5490
3cce7f32 5491 if Nkind (N) /= N_Type_Conversion then
9c486805 5492 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
ee6ba406 5493
cc60bd16 5494 -- Note in the test below that we assume that the range is not OK
5495 -- if a bound of the range is equal to that of the type. That's not
5496 -- quite accurate but we do this for the following reasons:
ee6ba406 5497
9dfe12ae 5498 -- a) The way that Determine_Range works, it will typically report
5499 -- the bounds of the value as being equal to the bounds of the
5500 -- type, because it either can't tell anything more precise, or
5501 -- does not think it is worth the effort to be more precise.
ee6ba406 5502
9dfe12ae 5503 -- b) It is very unusual to have a situation in which this would
5504 -- generate an unnecessary overflow check (an example would be
5505 -- a subtype with a range 0 .. Integer'Last - 1 to which the
cc60bd16 5506 -- literal value one is added).
ee6ba406 5507
9dfe12ae 5508 -- c) The alternative is a lot of special casing in this routine
5509 -- which would partially duplicate Determine_Range processing.
ee6ba406 5510
f3ccbbb3 5511 if OK then
5512 Do_Ovflow_Check := True;
5513
5514 -- Note that the following checks are quite deliberately > and <
5515 -- rather than >= and <= as explained above.
5516
5517 if Lo > Expr_Value (Type_Low_Bound (Typ))
5518 and then
5519 Hi < Expr_Value (Type_High_Bound (Typ))
5520 then
5521 Do_Ovflow_Check := False;
5522
5523 -- Despite the comments above, it is worth dealing specially with
5524 -- division specially. The only case where integer division can
5525 -- overflow is (largest negative number) / (-1). So we will do
5526 -- an extra range analysis to see if this is possible.
5527
5528 elsif Nkind (N) = N_Op_Divide then
5529 Determine_Range
5530 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5531
5532 if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then
5533 Do_Ovflow_Check := False;
5534
5535 else
5536 Determine_Range
5537 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5538
5539 if OK and then (Lo > Uint_Minus_1
5540 or else
5541 Hi < Uint_Minus_1)
5542 then
5543 Do_Ovflow_Check := False;
5544 end if;
5545 end if;
9dfe12ae 5546 end if;
5547
f3ccbbb3 5548 -- If no overflow check required, we are done
5549
5550 if not Do_Ovflow_Check then
5551 if Debug_Flag_CC then
5552 w ("No overflow check required");
5553 end if;
5554
5555 return;
5556 end if;
9dfe12ae 5557 end if;
5558 end if;
5559
feff2f05 5560 -- If not in optimizing mode, set flag and we are done. We are also done
5561 -- (and just set the flag) if the type is not a discrete type, since it
5562 -- is not worth the effort to eliminate checks for other than discrete
5563 -- types. In addition, we take this same path if we have stored the
5564 -- maximum number of checks possible already (a very unlikely situation,
39a0c1d3 5565 -- but we do not want to blow up).
9dfe12ae 5566
5567 if Optimization_Level = 0
5568 or else not Is_Discrete_Type (Etype (N))
5569 or else Num_Saved_Checks = Saved_Checks'Last
ee6ba406 5570 then
00c403ee 5571 Activate_Overflow_Check (N);
9dfe12ae 5572
5573 if Debug_Flag_CC then
5574 w ("Optimization off");
5575 end if;
5576
ee6ba406 5577 return;
9dfe12ae 5578 end if;
ee6ba406 5579
9dfe12ae 5580 -- Otherwise evaluate and check the expression
5581
5582 Find_Check
5583 (Expr => N,
5584 Check_Type => 'O',
5585 Target_Type => Empty,
5586 Entry_OK => OK,
5587 Check_Num => Chk,
5588 Ent => Ent,
5589 Ofs => Ofs);
5590
5591 if Debug_Flag_CC then
5592 w ("Called Find_Check");
5593 w (" OK = ", OK);
5594
5595 if OK then
5596 w (" Check_Num = ", Chk);
5597 w (" Ent = ", Int (Ent));
5598 Write_Str (" Ofs = ");
5599 pid (Ofs);
5600 end if;
5601 end if;
ee6ba406 5602
9dfe12ae 5603 -- If check is not of form to optimize, then set flag and we are done
5604
5605 if not OK then
00c403ee 5606 Activate_Overflow_Check (N);
ee6ba406 5607 return;
9dfe12ae 5608 end if;
ee6ba406 5609
9dfe12ae 5610 -- If check is already performed, then return without setting flag
5611
5612 if Chk /= 0 then
5613 if Debug_Flag_CC then
5614 w ("Check suppressed!");
5615 end if;
ee6ba406 5616
ee6ba406 5617 return;
9dfe12ae 5618 end if;
ee6ba406 5619
9dfe12ae 5620 -- Here we will make a new entry for the new check
5621
00c403ee 5622 Activate_Overflow_Check (N);
9dfe12ae 5623 Num_Saved_Checks := Num_Saved_Checks + 1;
5624 Saved_Checks (Num_Saved_Checks) :=
5625 (Killed => False,
5626 Entity => Ent,
5627 Offset => Ofs,
5628 Check_Type => 'O',
5629 Target_Type => Empty);
5630
5631 if Debug_Flag_CC then
5632 w ("Make new entry, check number = ", Num_Saved_Checks);
5633 w (" Entity = ", Int (Ent));
5634 Write_Str (" Offset = ");
5635 pid (Ofs);
5636 w (" Check_Type = O");
5637 w (" Target_Type = Empty");
5638 end if;
ee6ba406 5639
feff2f05 5640 -- If we get an exception, then something went wrong, probably because of
20cf157b 5641 -- an error in the structure of the tree due to an incorrect program. Or
5642 -- it may be a bug in the optimization circuit. In either case the safest
feff2f05 5643 -- thing is simply to set the check flag unconditionally.
9dfe12ae 5644
5645 exception
5646 when others =>
00c403ee 5647 Activate_Overflow_Check (N);
9dfe12ae 5648
5649 if Debug_Flag_CC then
5650 w (" exception occurred, overflow flag set");
5651 end if;
5652
5653 return;
5654 end Enable_Overflow_Check;
5655
5656 ------------------------
5657 -- Enable_Range_Check --
5658 ------------------------
5659
5660 procedure Enable_Range_Check (N : Node_Id) is
5661 Chk : Nat;
5662 OK : Boolean;
5663 Ent : Entity_Id;
5664 Ofs : Uint;
5665 Ttyp : Entity_Id;
5666 P : Node_Id;
5667
5668 begin
feff2f05 5669 -- Return if unchecked type conversion with range check killed. In this
39a0c1d3 5670 -- case we never set the flag (that's what Kill_Range_Check is about).
9dfe12ae 5671
5672 if Nkind (N) = N_Unchecked_Type_Conversion
5673 and then Kill_Range_Check (N)
ee6ba406 5674 then
5675 return;
9dfe12ae 5676 end if;
ee6ba406 5677
55e8372b 5678 -- Do not set range check flag if parent is assignment statement or
5679 -- object declaration with Suppress_Assignment_Checks flag set
5680
5681 if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration)
5682 and then Suppress_Assignment_Checks (Parent (N))
5683 then
5684 return;
5685 end if;
5686
0577b0b1 5687 -- Check for various cases where we should suppress the range check
5688
5689 -- No check if range checks suppressed for type of node
5690
20cf157b 5691 if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then
0577b0b1 5692 return;
5693
5694 -- No check if node is an entity name, and range checks are suppressed
5695 -- for this entity, or for the type of this entity.
5696
5697 elsif Is_Entity_Name (N)
5698 and then (Range_Checks_Suppressed (Entity (N))
20cf157b 5699 or else Range_Checks_Suppressed (Etype (Entity (N))))
0577b0b1 5700 then
5701 return;
5702
5703 -- No checks if index of array, and index checks are suppressed for
5704 -- the array object or the type of the array.
5705
5706 elsif Nkind (Parent (N)) = N_Indexed_Component then
5707 declare
5708 Pref : constant Node_Id := Prefix (Parent (N));
5709 begin
5710 if Is_Entity_Name (Pref)
5711 and then Index_Checks_Suppressed (Entity (Pref))
5712 then
5713 return;
5714 elsif Index_Checks_Suppressed (Etype (Pref)) then
5715 return;
5716 end if;
5717 end;
5718 end if;
5719
9dfe12ae 5720 -- Debug trace output
ee6ba406 5721
9dfe12ae 5722 if Debug_Flag_CC then
5723 w ("Enable_Range_Check for node ", Int (N));
5724 Write_Str (" Source location = ");
5725 wl (Sloc (N));
00c403ee 5726 pg (Union_Id (N));
9dfe12ae 5727 end if;
5728
feff2f05 5729 -- If not in optimizing mode, set flag and we are done. We are also done
5730 -- (and just set the flag) if the type is not a discrete type, since it
5731 -- is not worth the effort to eliminate checks for other than discrete
5732 -- types. In addition, we take this same path if we have stored the
5733 -- maximum number of checks possible already (a very unlikely situation,
39a0c1d3 5734 -- but we do not want to blow up).
9dfe12ae 5735
5736 if Optimization_Level = 0
5737 or else No (Etype (N))
5738 or else not Is_Discrete_Type (Etype (N))
5739 or else Num_Saved_Checks = Saved_Checks'Last
ee6ba406 5740 then
00c403ee 5741 Activate_Range_Check (N);
9dfe12ae 5742
5743 if Debug_Flag_CC then
5744 w ("Optimization off");
5745 end if;
5746
ee6ba406 5747 return;
9dfe12ae 5748 end if;
ee6ba406 5749
9dfe12ae 5750 -- Otherwise find out the target type
ee6ba406 5751
9dfe12ae 5752 P := Parent (N);
ee6ba406 5753
9dfe12ae 5754 -- For assignment, use left side subtype
5755
5756 if Nkind (P) = N_Assignment_Statement
5757 and then Expression (P) = N
5758 then
5759 Ttyp := Etype (Name (P));
5760
5761 -- For indexed component, use subscript subtype
5762
5763 elsif Nkind (P) = N_Indexed_Component then
5764 declare
5765 Atyp : Entity_Id;
5766 Indx : Node_Id;
5767 Subs : Node_Id;
5768
5769 begin
5770 Atyp := Etype (Prefix (P));
5771
5772 if Is_Access_Type (Atyp) then
5773 Atyp := Designated_Type (Atyp);
f07ea091 5774
5775 -- If the prefix is an access to an unconstrained array,
feff2f05 5776 -- perform check unconditionally: it depends on the bounds of
5777 -- an object and we cannot currently recognize whether the test
5778 -- may be redundant.
f07ea091 5779
5780 if not Is_Constrained (Atyp) then
00c403ee 5781 Activate_Range_Check (N);
f07ea091 5782 return;
5783 end if;
7189d17f 5784
31831d39 5785 -- Ditto if prefix is simply an unconstrained array. We used
5786 -- to think this case was OK, if the prefix was not an explicit
5787 -- dereference, but we have now seen a case where this is not
5788 -- true, so it is safer to just suppress the optimization in this
5789 -- case. The back end is getting better at eliminating redundant
5790 -- checks in any case, so the loss won't be important.
7189d17f 5791
31831d39 5792 elsif Is_Array_Type (Atyp)
7189d17f 5793 and then not Is_Constrained (Atyp)
5794 then
00c403ee 5795 Activate_Range_Check (N);
7189d17f 5796 return;
9dfe12ae 5797 end if;
5798
5799 Indx := First_Index (Atyp);
5800 Subs := First (Expressions (P));
5801 loop
5802 if Subs = N then
5803 Ttyp := Etype (Indx);
5804 exit;
5805 end if;
5806
5807 Next_Index (Indx);
5808 Next (Subs);
5809 end loop;
5810 end;
5811
5812 -- For now, ignore all other cases, they are not so interesting
5813
5814 else
5815 if Debug_Flag_CC then
5816 w (" target type not found, flag set");
5817 end if;
5818
00c403ee 5819 Activate_Range_Check (N);
9dfe12ae 5820 return;
5821 end if;
5822
5823 -- Evaluate and check the expression
5824
5825 Find_Check
5826 (Expr => N,
5827 Check_Type => 'R',
5828 Target_Type => Ttyp,
5829 Entry_OK => OK,
5830 Check_Num => Chk,
5831 Ent => Ent,
5832 Ofs => Ofs);
5833
5834 if Debug_Flag_CC then
5835 w ("Called Find_Check");
5836 w ("Target_Typ = ", Int (Ttyp));
5837 w (" OK = ", OK);
5838
5839 if OK then
5840 w (" Check_Num = ", Chk);
5841 w (" Ent = ", Int (Ent));
5842 Write_Str (" Ofs = ");
5843 pid (Ofs);
5844 end if;
5845 end if;
5846
5847 -- If check is not of form to optimize, then set flag and we are done
5848
5849 if not OK then
5850 if Debug_Flag_CC then
5851 w (" expression not of optimizable type, flag set");
5852 end if;
5853
00c403ee 5854 Activate_Range_Check (N);
9dfe12ae 5855 return;
5856 end if;
5857
5858 -- If check is already performed, then return without setting flag
5859
5860 if Chk /= 0 then
5861 if Debug_Flag_CC then
5862 w ("Check suppressed!");
5863 end if;
5864
5865 return;
5866 end if;
5867
5868 -- Here we will make a new entry for the new check
5869
00c403ee 5870 Activate_Range_Check (N);
9dfe12ae 5871 Num_Saved_Checks := Num_Saved_Checks + 1;
5872 Saved_Checks (Num_Saved_Checks) :=
5873 (Killed => False,
5874 Entity => Ent,
5875 Offset => Ofs,
5876 Check_Type => 'R',
5877 Target_Type => Ttyp);
5878
5879 if Debug_Flag_CC then
5880 w ("Make new entry, check number = ", Num_Saved_Checks);
5881 w (" Entity = ", Int (Ent));
5882 Write_Str (" Offset = ");
5883 pid (Ofs);
5884 w (" Check_Type = R");
5885 w (" Target_Type = ", Int (Ttyp));
00c403ee 5886 pg (Union_Id (Ttyp));
9dfe12ae 5887 end if;
5888
feff2f05 5889 -- If we get an exception, then something went wrong, probably because of
5890 -- an error in the structure of the tree due to an incorrect program. Or
5891 -- it may be a bug in the optimization circuit. In either case the safest
5892 -- thing is simply to set the check flag unconditionally.
9dfe12ae 5893
5894 exception
5895 when others =>
00c403ee 5896 Activate_Range_Check (N);
9dfe12ae 5897
5898 if Debug_Flag_CC then
5899 w (" exception occurred, range flag set");
5900 end if;
5901
5902 return;
5903 end Enable_Range_Check;
5904
5905 ------------------
5906 -- Ensure_Valid --
5907 ------------------
5908
aaec8d13 5909 procedure Ensure_Valid
5910 (Expr : Node_Id;
5911 Holes_OK : Boolean := False;
5912 Related_Id : Entity_Id := Empty;
5913 Is_Low_Bound : Boolean := False;
5914 Is_High_Bound : Boolean := False)
5915 is
9dfe12ae 5916 Typ : constant Entity_Id := Etype (Expr);
5917
5918 begin
5919 -- Ignore call if we are not doing any validity checking
5920
5921 if not Validity_Checks_On then
5922 return;
5923
0577b0b1 5924 -- Ignore call if range or validity checks suppressed on entity or type
9dfe12ae 5925
0577b0b1 5926 elsif Range_Or_Validity_Checks_Suppressed (Expr) then
9dfe12ae 5927 return;
5928
feff2f05 5929 -- No check required if expression is from the expander, we assume the
5930 -- expander will generate whatever checks are needed. Note that this is
39a0c1d3 5931 -- not just an optimization, it avoids infinite recursions.
9dfe12ae 5932
5933 -- Unchecked conversions must be checked, unless they are initialized
5934 -- scalar values, as in a component assignment in an init proc.
5935
5936 -- In addition, we force a check if Force_Validity_Checks is set
5937
5938 elsif not Comes_From_Source (Expr)
5939 and then not Force_Validity_Checks
5940 and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
5941 or else Kill_Range_Check (Expr))
5942 then
5943 return;
5944
5945 -- No check required if expression is known to have valid value
5946
5947 elsif Expr_Known_Valid (Expr) then
5948 return;
5949
ea1f4b87 5950 -- No check needed within a generated predicate function. Validity
5951 -- of input value will have been checked earlier.
5952
5953 elsif Ekind (Current_Scope) = E_Function
5954 and then Is_Predicate_Function (Current_Scope)
5955 then
5956 return;
5957
feff2f05 5958 -- Ignore case of enumeration with holes where the flag is set not to
5959 -- worry about holes, since no special validity check is needed
9dfe12ae 5960
5961 elsif Is_Enumeration_Type (Typ)
5962 and then Has_Non_Standard_Rep (Typ)
5963 and then Holes_OK
5964 then
5965 return;
5966
f2a06be9 5967 -- No check required on the left-hand side of an assignment
9dfe12ae 5968
5969 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
5970 and then Expr = Name (Parent (Expr))
5971 then
5972 return;
5973
6fb3c314 5974 -- No check on a universal real constant. The context will eventually
38f5559f 5975 -- convert it to a machine number for some target type, or report an
5976 -- illegality.
5977
5978 elsif Nkind (Expr) = N_Real_Literal
5979 and then Etype (Expr) = Universal_Real
5980 then
5981 return;
5982
6fb3c314 5983 -- If the expression denotes a component of a packed boolean array,
0577b0b1 5984 -- no possible check applies. We ignore the old ACATS chestnuts that
5985 -- involve Boolean range True..True.
5986
5987 -- Note: validity checks are generated for expressions that yield a
5988 -- scalar type, when it is possible to create a value that is outside of
5989 -- the type. If this is a one-bit boolean no such value exists. This is
5990 -- an optimization, and it also prevents compiler blowing up during the
5991 -- elaboration of improperly expanded packed array references.
5992
5993 elsif Nkind (Expr) = N_Indexed_Component
5994 and then Is_Bit_Packed_Array (Etype (Prefix (Expr)))
5995 and then Root_Type (Etype (Expr)) = Standard_Boolean
5996 then
5997 return;
5998
737e8460 5999 -- For an expression with actions, we want to insert the validity check
6000 -- on the final Expression.
6001
6002 elsif Nkind (Expr) = N_Expression_With_Actions then
6003 Ensure_Valid (Expression (Expr));
6004 return;
6005
9dfe12ae 6006 -- An annoying special case. If this is an out parameter of a scalar
6007 -- type, then the value is not going to be accessed, therefore it is
6008 -- inappropriate to do any validity check at the call site.
6009
6010 else
6011 -- Only need to worry about scalar types
6012
6013 if Is_Scalar_Type (Typ) then
ee6ba406 6014 declare
6015 P : Node_Id;
6016 N : Node_Id;
6017 E : Entity_Id;
6018 F : Entity_Id;
6019 A : Node_Id;
6020 L : List_Id;
6021
6022 begin
6023 -- Find actual argument (which may be a parameter association)
6024 -- and the parent of the actual argument (the call statement)
6025
6026 N := Expr;
6027 P := Parent (Expr);
6028
6029 if Nkind (P) = N_Parameter_Association then
6030 N := P;
6031 P := Parent (N);
6032 end if;
6033
feff2f05 6034 -- Only need to worry if we are argument of a procedure call
6035 -- since functions don't have out parameters. If this is an
6036 -- indirect or dispatching call, get signature from the
6037 -- subprogram type.
ee6ba406 6038
6039 if Nkind (P) = N_Procedure_Call_Statement then
6040 L := Parameter_Associations (P);
9dfe12ae 6041
6042 if Is_Entity_Name (Name (P)) then
6043 E := Entity (Name (P));
6044 else
6045 pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference);
6046 E := Etype (Name (P));
6047 end if;
ee6ba406 6048
feff2f05 6049 -- Only need to worry if there are indeed actuals, and if
6050 -- this could be a procedure call, otherwise we cannot get a
6051 -- match (either we are not an argument, or the mode of the
6052 -- formal is not OUT). This test also filters out the
6053 -- generic case.
ee6ba406 6054
20cf157b 6055 if Is_Non_Empty_List (L) and then Is_Subprogram (E) then
6056
feff2f05 6057 -- This is the loop through parameters, looking for an
6058 -- OUT parameter for which we are the argument.
ee6ba406 6059
6060 F := First_Formal (E);
6061 A := First (L);
ee6ba406 6062 while Present (F) loop
6063 if Ekind (F) = E_Out_Parameter and then A = N then
6064 return;
6065 end if;
6066
6067 Next_Formal (F);
6068 Next (A);
6069 end loop;
6070 end if;
6071 end if;
6072 end;
6073 end if;
6074 end if;
6075
fa6a6949 6076 -- If this is a boolean expression, only its elementary operands need
90a07d4c 6077 -- checking: if they are valid, a boolean or short-circuit operation
6078 -- with them will be valid as well.
784d4230 6079
6080 if Base_Type (Typ) = Standard_Boolean
7af38999 6081 and then
fa6a6949 6082 (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit)
784d4230 6083 then
6084 return;
6085 end if;
6086
0577b0b1 6087 -- If we fall through, a validity check is required
ee6ba406 6088
aaec8d13 6089 Insert_Valid_Check (Expr, Related_Id, Is_Low_Bound, Is_High_Bound);
ce7498d3 6090
6091 if Is_Entity_Name (Expr)
6092 and then Safe_To_Capture_Value (Expr, Entity (Expr))
6093 then
6094 Set_Is_Known_Valid (Entity (Expr));
6095 end if;
ee6ba406 6096 end Ensure_Valid;
6097
6098 ----------------------
6099 -- Expr_Known_Valid --
6100 ----------------------
6101
6102 function Expr_Known_Valid (Expr : Node_Id) return Boolean is
6103 Typ : constant Entity_Id := Etype (Expr);
6104
6105 begin
feff2f05 6106 -- Non-scalar types are always considered valid, since they never give
6107 -- rise to the issues of erroneous or bounded error behavior that are
6108 -- the concern. In formal reference manual terms the notion of validity
6109 -- only applies to scalar types. Note that even when packed arrays are
6110 -- represented using modular types, they are still arrays semantically,
6111 -- so they are also always valid (in particular, the unused bits can be
6112 -- random rubbish without affecting the validity of the array value).
ee6ba406 6113
a88a5773 6114 if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then
ee6ba406 6115 return True;
6116
6117 -- If no validity checking, then everything is considered valid
6118
6119 elsif not Validity_Checks_On then
6120 return True;
6121
6122 -- Floating-point types are considered valid unless floating-point
6123 -- validity checks have been specifically turned on.
6124
6125 elsif Is_Floating_Point_Type (Typ)
6126 and then not Validity_Check_Floating_Point
6127 then
6128 return True;
6129
feff2f05 6130 -- If the expression is the value of an object that is known to be
6131 -- valid, then clearly the expression value itself is valid.
ee6ba406 6132
6133 elsif Is_Entity_Name (Expr)
6134 and then Is_Known_Valid (Entity (Expr))
985fe5d6 6135
6136 -- Exclude volatile variables
6137
6138 and then not Treat_As_Volatile (Entity (Expr))
ee6ba406 6139 then
6140 return True;
6141
0577b0b1 6142 -- References to discriminants are always considered valid. The value
6143 -- of a discriminant gets checked when the object is built. Within the
6144 -- record, we consider it valid, and it is important to do so, since
6145 -- otherwise we can try to generate bogus validity checks which
feff2f05 6146 -- reference discriminants out of scope. Discriminants of concurrent
6147 -- types are excluded for the same reason.
0577b0b1 6148
6149 elsif Is_Entity_Name (Expr)
feff2f05 6150 and then Denotes_Discriminant (Expr, Check_Concurrent => True)
0577b0b1 6151 then
6152 return True;
6153
feff2f05 6154 -- If the type is one for which all values are known valid, then we are
6155 -- sure that the value is valid except in the slightly odd case where
6156 -- the expression is a reference to a variable whose size has been
6157 -- explicitly set to a value greater than the object size.
ee6ba406 6158
6159 elsif Is_Known_Valid (Typ) then
6160 if Is_Entity_Name (Expr)
6161 and then Ekind (Entity (Expr)) = E_Variable
6162 and then Esize (Entity (Expr)) > Esize (Typ)
6163 then
6164 return False;
6165 else
6166 return True;
6167 end if;
6168
6169 -- Integer and character literals always have valid values, where
6170 -- appropriate these will be range checked in any case.
6171
20cf157b 6172 elsif Nkind_In (Expr, N_Integer_Literal, N_Character_Literal) then
ee6ba406 6173 return True;
91e47010 6174
ee6ba406 6175 -- If we have a type conversion or a qualification of a known valid
6176 -- value, then the result will always be valid.
6177
20cf157b 6178 elsif Nkind_In (Expr, N_Type_Conversion, N_Qualified_Expression) then
ee6ba406 6179 return Expr_Known_Valid (Expression (Expr));
6180
1eb1395f 6181 -- Case of expression is a non-floating-point operator. In this case we
6182 -- can assume the result is valid the generated code for the operator
6183 -- will include whatever checks are needed (e.g. range checks) to ensure
6184 -- validity. This assumption does not hold for the floating-point case,
6185 -- since floating-point operators can generate Infinite or NaN results
6186 -- which are considered invalid.
6187
6188 -- Historical note: in older versions, the exemption of floating-point
6189 -- types from this assumption was done only in cases where the parent
6190 -- was an assignment, function call or parameter association. Presumably
6191 -- the idea was that in other contexts, the result would be checked
6192 -- elsewhere, but this list of cases was missing tests (at least the
6193 -- N_Object_Declaration case, as shown by a reported missing validity
6194 -- check), and it is not clear why function calls but not procedure
6195 -- calls were tested for. It really seems more accurate and much
6196 -- safer to recognize that expressions which are the result of a
6197 -- floating-point operator can never be assumed to be valid.
6198
6199 elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then
6200 return True;
1d90d657 6201
feff2f05 6202 -- The result of a membership test is always valid, since it is true or
6203 -- false, there are no other possibilities.
0577b0b1 6204
6205 elsif Nkind (Expr) in N_Membership_Test then
6206 return True;
6207
ee6ba406 6208 -- For all other cases, we do not know the expression is valid
6209
6210 else
6211 return False;
6212 end if;
6213 end Expr_Known_Valid;
6214
9dfe12ae 6215 ----------------
6216 -- Find_Check --
6217 ----------------
6218
6219 procedure Find_Check
6220 (Expr : Node_Id;
6221 Check_Type : Character;
6222 Target_Type : Entity_Id;
6223 Entry_OK : out Boolean;
6224 Check_Num : out Nat;
6225 Ent : out Entity_Id;
6226 Ofs : out Uint)
6227 is
6228 function Within_Range_Of
6229 (Target_Type : Entity_Id;
314a23b6 6230 Check_Type : Entity_Id) return Boolean;
9dfe12ae 6231 -- Given a requirement for checking a range against Target_Type, and
6232 -- and a range Check_Type against which a check has already been made,
6233 -- determines if the check against check type is sufficient to ensure
6234 -- that no check against Target_Type is required.
6235
6236 ---------------------
6237 -- Within_Range_Of --
6238 ---------------------
6239
6240 function Within_Range_Of
6241 (Target_Type : Entity_Id;
314a23b6 6242 Check_Type : Entity_Id) return Boolean
9dfe12ae 6243 is
6244 begin
6245 if Target_Type = Check_Type then
6246 return True;
6247
6248 else
6249 declare
6250 Tlo : constant Node_Id := Type_Low_Bound (Target_Type);
6251 Thi : constant Node_Id := Type_High_Bound (Target_Type);
6252 Clo : constant Node_Id := Type_Low_Bound (Check_Type);
6253 Chi : constant Node_Id := Type_High_Bound (Check_Type);
6254
6255 begin
6256 if (Tlo = Clo
6257 or else (Compile_Time_Known_Value (Tlo)
6258 and then
6259 Compile_Time_Known_Value (Clo)
6260 and then
6261 Expr_Value (Clo) >= Expr_Value (Tlo)))
6262 and then
6263 (Thi = Chi
6264 or else (Compile_Time_Known_Value (Thi)
6265 and then
6266 Compile_Time_Known_Value (Chi)
6267 and then
6268 Expr_Value (Chi) <= Expr_Value (Clo)))
6269 then
6270 return True;
6271 else
6272 return False;
6273 end if;
6274 end;
6275 end if;
6276 end Within_Range_Of;
6277
6278 -- Start of processing for Find_Check
6279
6280 begin
ed195555 6281 -- Establish default, in case no entry is found
9dfe12ae 6282
6283 Check_Num := 0;
6284
6285 -- Case of expression is simple entity reference
6286
6287 if Is_Entity_Name (Expr) then
6288 Ent := Entity (Expr);
6289 Ofs := Uint_0;
6290
6291 -- Case of expression is entity + known constant
6292
6293 elsif Nkind (Expr) = N_Op_Add
6294 and then Compile_Time_Known_Value (Right_Opnd (Expr))
6295 and then Is_Entity_Name (Left_Opnd (Expr))
6296 then
6297 Ent := Entity (Left_Opnd (Expr));
6298 Ofs := Expr_Value (Right_Opnd (Expr));
6299
6300 -- Case of expression is entity - known constant
6301
6302 elsif Nkind (Expr) = N_Op_Subtract
6303 and then Compile_Time_Known_Value (Right_Opnd (Expr))
6304 and then Is_Entity_Name (Left_Opnd (Expr))
6305 then
6306 Ent := Entity (Left_Opnd (Expr));
6307 Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr)));
6308
6309 -- Any other expression is not of the right form
6310
6311 else
6312 Ent := Empty;
6313 Ofs := Uint_0;
6314 Entry_OK := False;
6315 return;
6316 end if;
6317
feff2f05 6318 -- Come here with expression of appropriate form, check if entity is an
6319 -- appropriate one for our purposes.
9dfe12ae 6320
6321 if (Ekind (Ent) = E_Variable
cc60bd16 6322 or else Is_Constant_Object (Ent))
9dfe12ae 6323 and then not Is_Library_Level_Entity (Ent)
6324 then
6325 Entry_OK := True;
6326 else
6327 Entry_OK := False;
6328 return;
6329 end if;
6330
6331 -- See if there is matching check already
6332
6333 for J in reverse 1 .. Num_Saved_Checks loop
6334 declare
6335 SC : Saved_Check renames Saved_Checks (J);
9dfe12ae 6336 begin
6337 if SC.Killed = False
6338 and then SC.Entity = Ent
6339 and then SC.Offset = Ofs
6340 and then SC.Check_Type = Check_Type
6341 and then Within_Range_Of (Target_Type, SC.Target_Type)
6342 then
6343 Check_Num := J;
6344 return;
6345 end if;
6346 end;
6347 end loop;
6348
6349 -- If we fall through entry was not found
6350
9dfe12ae 6351 return;
6352 end Find_Check;
6353
6354 ---------------------------------
6355 -- Generate_Discriminant_Check --
6356 ---------------------------------
6357
6358 -- Note: the code for this procedure is derived from the
feff2f05 6359 -- Emit_Discriminant_Check Routine in trans.c.
9dfe12ae 6360
6361 procedure Generate_Discriminant_Check (N : Node_Id) is
6362 Loc : constant Source_Ptr := Sloc (N);
6363 Pref : constant Node_Id := Prefix (N);
6364 Sel : constant Node_Id := Selector_Name (N);
6365
6366 Orig_Comp : constant Entity_Id :=
b6341c67 6367 Original_Record_Component (Entity (Sel));
9dfe12ae 6368 -- The original component to be checked
6369
6370 Discr_Fct : constant Entity_Id :=
b6341c67 6371 Discriminant_Checking_Func (Orig_Comp);
9dfe12ae 6372 -- The discriminant checking function
6373
6374 Discr : Entity_Id;
6375 -- One discriminant to be checked in the type
6376
6377 Real_Discr : Entity_Id;
6378 -- Actual discriminant in the call
6379
6380 Pref_Type : Entity_Id;
6381 -- Type of relevant prefix (ignoring private/access stuff)
6382
6383 Args : List_Id;
6384 -- List of arguments for function call
6385
6386 Formal : Entity_Id;
feff2f05 6387 -- Keep track of the formal corresponding to the actual we build for
6388 -- each discriminant, in order to be able to perform the necessary type
6389 -- conversions.
9dfe12ae 6390
6391 Scomp : Node_Id;
6392 -- Selected component reference for checking function argument
6393
6394 begin
6395 Pref_Type := Etype (Pref);
6396
6397 -- Force evaluation of the prefix, so that it does not get evaluated
6398 -- twice (once for the check, once for the actual reference). Such a
20cf157b 6399 -- double evaluation is always a potential source of inefficiency, and
6400 -- is functionally incorrect in the volatile case, or when the prefix
f9906591 6401 -- may have side effects. A nonvolatile entity or a component of a
6402 -- nonvolatile entity requires no evaluation.
9dfe12ae 6403
6404 if Is_Entity_Name (Pref) then
6405 if Treat_As_Volatile (Entity (Pref)) then
6406 Force_Evaluation (Pref, Name_Req => True);
6407 end if;
6408
6409 elsif Treat_As_Volatile (Etype (Pref)) then
20cf157b 6410 Force_Evaluation (Pref, Name_Req => True);
9dfe12ae 6411
6412 elsif Nkind (Pref) = N_Selected_Component
6413 and then Is_Entity_Name (Prefix (Pref))
6414 then
6415 null;
6416
6417 else
6418 Force_Evaluation (Pref, Name_Req => True);
6419 end if;
6420
6421 -- For a tagged type, use the scope of the original component to
6422 -- obtain the type, because ???
6423
6424 if Is_Tagged_Type (Scope (Orig_Comp)) then
6425 Pref_Type := Scope (Orig_Comp);
6426
feff2f05 6427 -- For an untagged derived type, use the discriminants of the parent
6428 -- which have been renamed in the derivation, possibly by a one-to-many
d1a2e31b 6429 -- discriminant constraint. For untagged type, initially get the Etype
feff2f05 6430 -- of the prefix
9dfe12ae 6431
6432 else
6433 if Is_Derived_Type (Pref_Type)
6434 and then Number_Discriminants (Pref_Type) /=
6435 Number_Discriminants (Etype (Base_Type (Pref_Type)))
6436 then
6437 Pref_Type := Etype (Base_Type (Pref_Type));
6438 end if;
6439 end if;
6440
6441 -- We definitely should have a checking function, This routine should
6442 -- not be called if no discriminant checking function is present.
6443
6444 pragma Assert (Present (Discr_Fct));
6445
6446 -- Create the list of the actual parameters for the call. This list
6447 -- is the list of the discriminant fields of the record expression to
6448 -- be discriminant checked.
6449
6450 Args := New_List;
6451 Formal := First_Formal (Discr_Fct);
6452 Discr := First_Discriminant (Pref_Type);
6453 while Present (Discr) loop
6454
6455 -- If we have a corresponding discriminant field, and a parent
6456 -- subtype is present, then we want to use the corresponding
6457 -- discriminant since this is the one with the useful value.
6458
6459 if Present (Corresponding_Discriminant (Discr))
6460 and then Ekind (Pref_Type) = E_Record_Type
6461 and then Present (Parent_Subtype (Pref_Type))
6462 then
6463 Real_Discr := Corresponding_Discriminant (Discr);
6464 else
6465 Real_Discr := Discr;
6466 end if;
6467
6468 -- Construct the reference to the discriminant
6469
6470 Scomp :=
6471 Make_Selected_Component (Loc,
6472 Prefix =>
6473 Unchecked_Convert_To (Pref_Type,
6474 Duplicate_Subexpr (Pref)),
6475 Selector_Name => New_Occurrence_Of (Real_Discr, Loc));
6476
6477 -- Manually analyze and resolve this selected component. We really
6478 -- want it just as it appears above, and do not want the expander
feff2f05 6479 -- playing discriminal games etc with this reference. Then we append
6480 -- the argument to the list we are gathering.
9dfe12ae 6481
6482 Set_Etype (Scomp, Etype (Real_Discr));
6483 Set_Analyzed (Scomp, True);
6484 Append_To (Args, Convert_To (Etype (Formal), Scomp));
6485
6486 Next_Formal_With_Extras (Formal);
6487 Next_Discriminant (Discr);
6488 end loop;
6489
6490 -- Now build and insert the call
6491
6492 Insert_Action (N,
6493 Make_Raise_Constraint_Error (Loc,
6494 Condition =>
6495 Make_Function_Call (Loc,
20cf157b 6496 Name => New_Occurrence_Of (Discr_Fct, Loc),
9dfe12ae 6497 Parameter_Associations => Args),
6498 Reason => CE_Discriminant_Check_Failed));
6499 end Generate_Discriminant_Check;
6500
5c99c290 6501 ---------------------------
6502 -- Generate_Index_Checks --
6503 ---------------------------
9dfe12ae 6504
6505 procedure Generate_Index_Checks (N : Node_Id) is
05f3e139 6506
6507 function Entity_Of_Prefix return Entity_Id;
6508 -- Returns the entity of the prefix of N (or Empty if not found)
6509
3f42e2a7 6510 ----------------------
6511 -- Entity_Of_Prefix --
6512 ----------------------
6513
05f3e139 6514 function Entity_Of_Prefix return Entity_Id is
e5d38095 6515 P : Node_Id;
6516
05f3e139 6517 begin
e5d38095 6518 P := Prefix (N);
05f3e139 6519 while not Is_Entity_Name (P) loop
6520 if not Nkind_In (P, N_Selected_Component,
6521 N_Indexed_Component)
6522 then
6523 return Empty;
6524 end if;
6525
6526 P := Prefix (P);
6527 end loop;
6528
6529 return Entity (P);
6530 end Entity_Of_Prefix;
6531
6532 -- Local variables
6533
6534 Loc : constant Source_Ptr := Sloc (N);
6535 A : constant Node_Id := Prefix (N);
6536 A_Ent : constant Entity_Id := Entity_Of_Prefix;
6537 Sub : Node_Id;
9dfe12ae 6538
3f42e2a7 6539 -- Start of processing for Generate_Index_Checks
6540
9dfe12ae 6541 begin
05f3e139 6542 -- Ignore call if the prefix is not an array since we have a serious
6543 -- error in the sources. Ignore it also if index checks are suppressed
6544 -- for array object or type.
0577b0b1 6545
05f3e139 6546 if not Is_Array_Type (Etype (A))
20cf157b 6547 or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent))
0577b0b1 6548 or else Index_Checks_Suppressed (Etype (A))
6549 then
6550 return;
df9fba45 6551
6552 -- The indexed component we are dealing with contains 'Loop_Entry in its
6553 -- prefix. This case arises when analysis has determined that constructs
6554 -- such as
6555
6556 -- Prefix'Loop_Entry (Expr)
6557 -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN)
6558
6559 -- require rewriting for error detection purposes. A side effect of this
6560 -- action is the generation of index checks that mention 'Loop_Entry.
6561 -- Delay the generation of the check until 'Loop_Entry has been properly
6562 -- expanded. This is done in Expand_Loop_Entry_Attributes.
6563
6564 elsif Nkind (Prefix (N)) = N_Attribute_Reference
6565 and then Attribute_Name (Prefix (N)) = Name_Loop_Entry
6566 then
6567 return;
0577b0b1 6568 end if;
6569
05f3e139 6570 -- Generate a raise of constraint error with the appropriate reason and
6571 -- a condition of the form:
6572
3f42e2a7 6573 -- Base_Type (Sub) not in Array'Range (Subscript)
05f3e139 6574
6575 -- Note that the reason we generate the conversion to the base type here
6576 -- is that we definitely want the range check to take place, even if it
6577 -- looks like the subtype is OK. Optimization considerations that allow
6578 -- us to omit the check have already been taken into account in the
6579 -- setting of the Do_Range_Check flag earlier on.
0577b0b1 6580
9dfe12ae 6581 Sub := First (Expressions (N));
05f3e139 6582
6583 -- Handle string literals
6584
6585 if Ekind (Etype (A)) = E_String_Literal_Subtype then
9dfe12ae 6586 if Do_Range_Check (Sub) then
6587 Set_Do_Range_Check (Sub, False);
6588
05f3e139 6589 -- For string literals we obtain the bounds of the string from the
6590 -- associated subtype.
9dfe12ae 6591
05f3e139 6592 Insert_Action (N,
094ed68e 6593 Make_Raise_Constraint_Error (Loc,
6594 Condition =>
6595 Make_Not_In (Loc,
6596 Left_Opnd =>
6597 Convert_To (Base_Type (Etype (Sub)),
6598 Duplicate_Subexpr_Move_Checks (Sub)),
6599 Right_Opnd =>
6600 Make_Attribute_Reference (Loc,
83c6c069 6601 Prefix => New_Occurrence_Of (Etype (A), Loc),
094ed68e 6602 Attribute_Name => Name_Range)),
6603 Reason => CE_Index_Check_Failed));
05f3e139 6604 end if;
9dfe12ae 6605
05f3e139 6606 -- General case
9dfe12ae 6607
05f3e139 6608 else
6609 declare
6610 A_Idx : Node_Id := Empty;
6611 A_Range : Node_Id;
6612 Ind : Nat;
6613 Num : List_Id;
6614 Range_N : Node_Id;
9dfe12ae 6615
05f3e139 6616 begin
6617 A_Idx := First_Index (Etype (A));
6618 Ind := 1;
6619 while Present (Sub) loop
6620 if Do_Range_Check (Sub) then
6621 Set_Do_Range_Check (Sub, False);
9dfe12ae 6622
05f3e139 6623 -- Force evaluation except for the case of a simple name of
f9906591 6624 -- a nonvolatile entity.
9dfe12ae 6625
05f3e139 6626 if not Is_Entity_Name (Sub)
6627 or else Treat_As_Volatile (Entity (Sub))
6628 then
6629 Force_Evaluation (Sub);
6630 end if;
9dfe12ae 6631
05f3e139 6632 if Nkind (A_Idx) = N_Range then
6633 A_Range := A_Idx;
6634
6635 elsif Nkind (A_Idx) = N_Identifier
6636 or else Nkind (A_Idx) = N_Expanded_Name
6637 then
6638 A_Range := Scalar_Range (Entity (A_Idx));
6639
6640 else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication);
6641 A_Range := Range_Expression (Constraint (A_Idx));
6642 end if;
6643
6644 -- For array objects with constant bounds we can generate
6645 -- the index check using the bounds of the type of the index
6646
6647 if Present (A_Ent)
6648 and then Ekind (A_Ent) = E_Variable
6649 and then Is_Constant_Bound (Low_Bound (A_Range))
6650 and then Is_Constant_Bound (High_Bound (A_Range))
6651 then
6652 Range_N :=
6653 Make_Attribute_Reference (Loc,
3f42e2a7 6654 Prefix =>
83c6c069 6655 New_Occurrence_Of (Etype (A_Idx), Loc),
05f3e139 6656 Attribute_Name => Name_Range);
6657
6658 -- For arrays with non-constant bounds we cannot generate
6659 -- the index check using the bounds of the type of the index
6660 -- since it may reference discriminants of some enclosing
6661 -- type. We obtain the bounds directly from the prefix
6662 -- object.
6663
6664 else
6665 if Ind = 1 then
6666 Num := No_List;
6667 else
6668 Num := New_List (Make_Integer_Literal (Loc, Ind));
6669 end if;
6670
6671 Range_N :=
6672 Make_Attribute_Reference (Loc,
6673 Prefix =>
6674 Duplicate_Subexpr_Move_Checks (A, Name_Req => True),
6675 Attribute_Name => Name_Range,
6676 Expressions => Num);
6677 end if;
6678
6679 Insert_Action (N,
094ed68e 6680 Make_Raise_Constraint_Error (Loc,
6681 Condition =>
6682 Make_Not_In (Loc,
6683 Left_Opnd =>
6684 Convert_To (Base_Type (Etype (Sub)),
6685 Duplicate_Subexpr_Move_Checks (Sub)),
6686 Right_Opnd => Range_N),
6687 Reason => CE_Index_Check_Failed));
05f3e139 6688 end if;
6689
6690 A_Idx := Next_Index (A_Idx);
6691 Ind := Ind + 1;
6692 Next (Sub);
6693 end loop;
6694 end;
6695 end if;
9dfe12ae 6696 end Generate_Index_Checks;
6697
6698 --------------------------
6699 -- Generate_Range_Check --
6700 --------------------------
6701
6702 procedure Generate_Range_Check
6703 (N : Node_Id;
6704 Target_Type : Entity_Id;
6705 Reason : RT_Exception_Code)
6706 is
6707 Loc : constant Source_Ptr := Sloc (N);
6708 Source_Type : constant Entity_Id := Etype (N);
6709 Source_Base_Type : constant Entity_Id := Base_Type (Source_Type);
6710 Target_Base_Type : constant Entity_Id := Base_Type (Target_Type);
6711
1f5d83cf 6712 procedure Convert_And_Check_Range;
6713 -- Convert the conversion operand to the target base type and save in
6714 -- a temporary. Then check the converted value against the range of the
6715 -- target subtype.
6716
124f1911 6717 -----------------------------
6718 -- Convert_And_Check_Range --
6719 -----------------------------
1f5d83cf 6720
124f1911 6721 procedure Convert_And_Check_Range is
1f5d83cf 6722 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
6723
124f1911 6724 begin
6725 -- We make a temporary to hold the value of the converted value
6726 -- (converted to the base type), and then do the test against this
6727 -- temporary. The conversion itself is replaced by an occurrence of
6728 -- Tnn and followed by the explicit range check. Note that checks
6729 -- are suppressed for this code, since we don't want a recursive
1f5d83cf 6730 -- range check popping up.
6731
124f1911 6732 -- Tnn : constant Target_Base_Type := Target_Base_Type (N);
6733 -- [constraint_error when Tnn not in Target_Type]
6734
1f5d83cf 6735 Insert_Actions (N, New_List (
6736 Make_Object_Declaration (Loc,
6737 Defining_Identifier => Tnn,
6738 Object_Definition => New_Occurrence_Of (Target_Base_Type, Loc),
6739 Constant_Present => True,
6740 Expression =>
6741 Make_Type_Conversion (Loc,
6742 Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc),
6743 Expression => Duplicate_Subexpr (N))),
6744
6745 Make_Raise_Constraint_Error (Loc,
6746 Condition =>
6747 Make_Not_In (Loc,
6748 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6749 Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
6750 Reason => Reason)),
6751 Suppress => All_Checks);
6752
6753 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
6754
6755 -- Set the type of N, because the declaration for Tnn might not
6756 -- be analyzed yet, as is the case if N appears within a record
6757 -- declaration, as a discriminant constraint or expression.
6758
6759 Set_Etype (N, Target_Base_Type);
6760 end Convert_And_Check_Range;
6761
6762 -- Start of processing for Generate_Range_Check
6763
9dfe12ae 6764 begin
feff2f05 6765 -- First special case, if the source type is already within the range
6766 -- of the target type, then no check is needed (probably we should have
6767 -- stopped Do_Range_Check from being set in the first place, but better
cda40848 6768 -- late than never in preventing junk code and junk flag settings.
9dfe12ae 6769
7a1dabb3 6770 if In_Subrange_Of (Source_Type, Target_Type)
b40670e1 6771
6772 -- We do NOT apply this if the source node is a literal, since in this
6773 -- case the literal has already been labeled as having the subtype of
6774 -- the target.
6775
9dfe12ae 6776 and then not
b40670e1 6777 (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal)
9dfe12ae 6778 or else
b40670e1 6779 (Is_Entity_Name (N)
6780 and then Ekind (Entity (N)) = E_Enumeration_Literal))
9dfe12ae 6781 then
cda40848 6782 Set_Do_Range_Check (N, False);
9dfe12ae 6783 return;
6784 end if;
6785
cda40848 6786 -- Here a check is needed. If the expander is not active, or if we are
6787 -- in GNATProve mode, then simply set the Do_Range_Check flag and we
6788 -- are done. In both these cases, we just want to see the range check
6789 -- flag set, we do not want to generate the explicit range check code.
6790
6791 if GNATprove_Mode or else not Expander_Active then
6792 Set_Do_Range_Check (N, True);
6793 return;
6794 end if;
6795
6796 -- Here we will generate an explicit range check, so we don't want to
6797 -- set the Do_Range check flag, since the range check is taken care of
6798 -- by the code we will generate.
6799
6800 Set_Do_Range_Check (N, False);
6801
6802 -- Force evaluation of the node, so that it does not get evaluated twice
6803 -- (once for the check, once for the actual reference). Such a double
6804 -- evaluation is always a potential source of inefficiency, and is
6805 -- functionally incorrect in the volatile case.
9dfe12ae 6806
b40670e1 6807 if not Is_Entity_Name (N) or else Treat_As_Volatile (Entity (N)) then
9dfe12ae 6808 Force_Evaluation (N);
6809 end if;
6810
feff2f05 6811 -- The easiest case is when Source_Base_Type and Target_Base_Type are
6812 -- the same since in this case we can simply do a direct check of the
6813 -- value of N against the bounds of Target_Type.
9dfe12ae 6814
6815 -- [constraint_error when N not in Target_Type]
6816
6817 -- Note: this is by far the most common case, for example all cases of
6818 -- checks on the RHS of assignments are in this category, but not all
6819 -- cases are like this. Notably conversions can involve two types.
6820
6821 if Source_Base_Type = Target_Base_Type then
99ed4b4c 6822
6823 -- Insert the explicit range check. Note that we suppress checks for
6824 -- this code, since we don't want a recursive range check popping up.
6825
9dfe12ae 6826 Insert_Action (N,
6827 Make_Raise_Constraint_Error (Loc,
6828 Condition =>
6829 Make_Not_In (Loc,
6830 Left_Opnd => Duplicate_Subexpr (N),
6831 Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
99ed4b4c 6832 Reason => Reason),
6833 Suppress => All_Checks);
9dfe12ae 6834
6835 -- Next test for the case where the target type is within the bounds
6836 -- of the base type of the source type, since in this case we can
6837 -- simply convert these bounds to the base type of T to do the test.
6838
6839 -- [constraint_error when N not in
6840 -- Source_Base_Type (Target_Type'First)
6841 -- ..
6842 -- Source_Base_Type(Target_Type'Last))]
6843
f2a06be9 6844 -- The conversions will always work and need no check
9dfe12ae 6845
a9b57347 6846 -- Unchecked_Convert_To is used instead of Convert_To to handle the case
6847 -- of converting from an enumeration value to an integer type, such as
6848 -- occurs for the case of generating a range check on Enum'Val(Exp)
6849 -- (which used to be handled by gigi). This is OK, since the conversion
6850 -- itself does not require a check.
6851
7a1dabb3 6852 elsif In_Subrange_Of (Target_Type, Source_Base_Type) then
99ed4b4c 6853
6854 -- Insert the explicit range check. Note that we suppress checks for
6855 -- this code, since we don't want a recursive range check popping up.
6856
1f5d83cf 6857 if Is_Discrete_Type (Source_Base_Type)
6858 and then
6859 Is_Discrete_Type (Target_Base_Type)
6860 then
6861 Insert_Action (N,
6862 Make_Raise_Constraint_Error (Loc,
6863 Condition =>
6864 Make_Not_In (Loc,
6865 Left_Opnd => Duplicate_Subexpr (N),
6866
6867 Right_Opnd =>
6868 Make_Range (Loc,
6869 Low_Bound =>
6870 Unchecked_Convert_To (Source_Base_Type,
6871 Make_Attribute_Reference (Loc,
6872 Prefix =>
6873 New_Occurrence_Of (Target_Type, Loc),
6874 Attribute_Name => Name_First)),
6875
6876 High_Bound =>
6877 Unchecked_Convert_To (Source_Base_Type,
6878 Make_Attribute_Reference (Loc,
6879 Prefix =>
6880 New_Occurrence_Of (Target_Type, Loc),
6881 Attribute_Name => Name_Last)))),
6882 Reason => Reason),
6883 Suppress => All_Checks);
9dfe12ae 6884
1f5d83cf 6885 -- For conversions involving at least one type that is not discrete,
6886 -- first convert to target type and then generate the range check.
6887 -- This avoids problems with values that are close to a bound of the
6888 -- target type that would fail a range check when done in a larger
6889 -- source type before converting but would pass if converted with
6890 -- rounding and then checked (such as in float-to-float conversions).
6891
6892 else
6893 Convert_And_Check_Range;
6894 end if;
9dfe12ae 6895
feff2f05 6896 -- Note that at this stage we now that the Target_Base_Type is not in
6897 -- the range of the Source_Base_Type (since even the Target_Type itself
6898 -- is not in this range). It could still be the case that Source_Type is
6899 -- in range of the target base type since we have not checked that case.
9dfe12ae 6900
feff2f05 6901 -- If that is the case, we can freely convert the source to the target,
6902 -- and then test the target result against the bounds.
9dfe12ae 6903
7a1dabb3 6904 elsif In_Subrange_Of (Source_Type, Target_Base_Type) then
1f5d83cf 6905 Convert_And_Check_Range;
9dfe12ae 6906
6907 -- At this stage, we know that we have two scalar types, which are
6908 -- directly convertible, and where neither scalar type has a base
6909 -- range that is in the range of the other scalar type.
6910
6911 -- The only way this can happen is with a signed and unsigned type.
6912 -- So test for these two cases:
6913
6914 else
6915 -- Case of the source is unsigned and the target is signed
6916
6917 if Is_Unsigned_Type (Source_Base_Type)
6918 and then not Is_Unsigned_Type (Target_Base_Type)
6919 then
6920 -- If the source is unsigned and the target is signed, then we
6921 -- know that the source is not shorter than the target (otherwise
6922 -- the source base type would be in the target base type range).
6923
feff2f05 6924 -- In other words, the unsigned type is either the same size as
6925 -- the target, or it is larger. It cannot be smaller.
9dfe12ae 6926
6927 pragma Assert
6928 (Esize (Source_Base_Type) >= Esize (Target_Base_Type));
6929
6930 -- We only need to check the low bound if the low bound of the
6931 -- target type is non-negative. If the low bound of the target
6932 -- type is negative, then we know that we will fit fine.
6933
6934 -- If the high bound of the target type is negative, then we
6935 -- know we have a constraint error, since we can't possibly
6936 -- have a negative source.
6937
6938 -- With these two checks out of the way, we can do the check
6939 -- using the source type safely
6940
39a0c1d3 6941 -- This is definitely the most annoying case.
9dfe12ae 6942
6943 -- [constraint_error
6944 -- when (Target_Type'First >= 0
6945 -- and then
6946 -- N < Source_Base_Type (Target_Type'First))
6947 -- or else Target_Type'Last < 0
6948 -- or else N > Source_Base_Type (Target_Type'Last)];
6949
6950 -- We turn off all checks since we know that the conversions
6951 -- will work fine, given the guards for negative values.
6952
6953 Insert_Action (N,
6954 Make_Raise_Constraint_Error (Loc,
6955 Condition =>
6956 Make_Or_Else (Loc,
6957 Make_Or_Else (Loc,
6958 Left_Opnd =>
6959 Make_And_Then (Loc,
6960 Left_Opnd => Make_Op_Ge (Loc,
6961 Left_Opnd =>
6962 Make_Attribute_Reference (Loc,
6963 Prefix =>
6964 New_Occurrence_Of (Target_Type, Loc),
6965 Attribute_Name => Name_First),
6966 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
6967
6968 Right_Opnd =>
6969 Make_Op_Lt (Loc,
6970 Left_Opnd => Duplicate_Subexpr (N),
6971 Right_Opnd =>
6972 Convert_To (Source_Base_Type,
6973 Make_Attribute_Reference (Loc,
6974 Prefix =>
6975 New_Occurrence_Of (Target_Type, Loc),
6976 Attribute_Name => Name_First)))),
6977
6978 Right_Opnd =>
6979 Make_Op_Lt (Loc,
6980 Left_Opnd =>
6981 Make_Attribute_Reference (Loc,
6982 Prefix => New_Occurrence_Of (Target_Type, Loc),
6983 Attribute_Name => Name_Last),
6984 Right_Opnd => Make_Integer_Literal (Loc, Uint_0))),
6985
6986 Right_Opnd =>
6987 Make_Op_Gt (Loc,
6988 Left_Opnd => Duplicate_Subexpr (N),
6989 Right_Opnd =>
6990 Convert_To (Source_Base_Type,
6991 Make_Attribute_Reference (Loc,
6992 Prefix => New_Occurrence_Of (Target_Type, Loc),
6993 Attribute_Name => Name_Last)))),
6994
6995 Reason => Reason),
6996 Suppress => All_Checks);
6997
6998 -- Only remaining possibility is that the source is signed and
fc75802a 6999 -- the target is unsigned.
9dfe12ae 7000
7001 else
7002 pragma Assert (not Is_Unsigned_Type (Source_Base_Type)
20cf157b 7003 and then Is_Unsigned_Type (Target_Base_Type));
9dfe12ae 7004
feff2f05 7005 -- If the source is signed and the target is unsigned, then we
7006 -- know that the target is not shorter than the source (otherwise
7007 -- the target base type would be in the source base type range).
9dfe12ae 7008
feff2f05 7009 -- In other words, the unsigned type is either the same size as
7010 -- the target, or it is larger. It cannot be smaller.
9dfe12ae 7011
feff2f05 7012 -- Clearly we have an error if the source value is negative since
7013 -- no unsigned type can have negative values. If the source type
7014 -- is non-negative, then the check can be done using the target
7015 -- type.
9dfe12ae 7016
7017 -- Tnn : constant Target_Base_Type (N) := Target_Type;
7018
7019 -- [constraint_error
7020 -- when N < 0 or else Tnn not in Target_Type];
7021
feff2f05 7022 -- We turn off all checks for the conversion of N to the target
7023 -- base type, since we generate the explicit check to ensure that
7024 -- the value is non-negative
9dfe12ae 7025
7026 declare
46eb6933 7027 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9dfe12ae 7028
7029 begin
7030 Insert_Actions (N, New_List (
7031 Make_Object_Declaration (Loc,
7032 Defining_Identifier => Tnn,
7033 Object_Definition =>
7034 New_Occurrence_Of (Target_Base_Type, Loc),
7035 Constant_Present => True,
7036 Expression =>
a9b57347 7037 Make_Unchecked_Type_Conversion (Loc,
9dfe12ae 7038 Subtype_Mark =>
7039 New_Occurrence_Of (Target_Base_Type, Loc),
7040 Expression => Duplicate_Subexpr (N))),
7041
7042 Make_Raise_Constraint_Error (Loc,
7043 Condition =>
7044 Make_Or_Else (Loc,
7045 Left_Opnd =>
7046 Make_Op_Lt (Loc,
7047 Left_Opnd => Duplicate_Subexpr (N),
7048 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
7049
7050 Right_Opnd =>
7051 Make_Not_In (Loc,
7052 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7053 Right_Opnd =>
7054 New_Occurrence_Of (Target_Type, Loc))),
7055
20cf157b 7056 Reason => Reason)),
9dfe12ae 7057 Suppress => All_Checks);
7058
feff2f05 7059 -- Set the Etype explicitly, because Insert_Actions may have
7060 -- placed the declaration in the freeze list for an enclosing
7061 -- construct, and thus it is not analyzed yet.
9dfe12ae 7062
7063 Set_Etype (Tnn, Target_Base_Type);
7064 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7065 end;
7066 end if;
7067 end if;
7068 end Generate_Range_Check;
7069
2af58f67 7070 ------------------
7071 -- Get_Check_Id --
7072 ------------------
7073
7074 function Get_Check_Id (N : Name_Id) return Check_Id is
7075 begin
7076 -- For standard check name, we can do a direct computation
7077
7078 if N in First_Check_Name .. Last_Check_Name then
7079 return Check_Id (N - (First_Check_Name - 1));
7080
7081 -- For non-standard names added by pragma Check_Name, search table
7082
7083 else
7084 for J in All_Checks + 1 .. Check_Names.Last loop
7085 if Check_Names.Table (J) = N then
7086 return J;
7087 end if;
7088 end loop;
7089 end if;
7090
7091 -- No matching name found
7092
7093 return No_Check_Id;
7094 end Get_Check_Id;
7095
ee6ba406 7096 ---------------------
7097 -- Get_Discriminal --
7098 ---------------------
7099
7100 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
7101 Loc : constant Source_Ptr := Sloc (E);
7102 D : Entity_Id;
7103 Sc : Entity_Id;
7104
7105 begin
0577b0b1 7106 -- The bound can be a bona fide parameter of a protected operation,
7107 -- rather than a prival encoded as an in-parameter.
7108
7109 if No (Discriminal_Link (Entity (Bound))) then
7110 return Bound;
7111 end if;
7112
2af58f67 7113 -- Climb the scope stack looking for an enclosing protected type. If
7114 -- we run out of scopes, return the bound itself.
7115
7116 Sc := Scope (E);
7117 while Present (Sc) loop
7118 if Sc = Standard_Standard then
7119 return Bound;
2af58f67 7120 elsif Ekind (Sc) = E_Protected_Type then
7121 exit;
7122 end if;
7123
7124 Sc := Scope (Sc);
7125 end loop;
7126
ee6ba406 7127 D := First_Discriminant (Sc);
2af58f67 7128 while Present (D) loop
7129 if Chars (D) = Chars (Bound) then
7130 return New_Occurrence_Of (Discriminal (D), Loc);
7131 end if;
ee6ba406 7132
ee6ba406 7133 Next_Discriminant (D);
7134 end loop;
7135
2af58f67 7136 return Bound;
ee6ba406 7137 end Get_Discriminal;
7138
2af58f67 7139 ----------------------
7140 -- Get_Range_Checks --
7141 ----------------------
7142
7143 function Get_Range_Checks
7144 (Ck_Node : Node_Id;
7145 Target_Typ : Entity_Id;
7146 Source_Typ : Entity_Id := Empty;
7147 Warn_Node : Node_Id := Empty) return Check_Result
7148 is
7149 begin
20cf157b 7150 return
7151 Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Warn_Node);
2af58f67 7152 end Get_Range_Checks;
7153
ee6ba406 7154 ------------------
7155 -- Guard_Access --
7156 ------------------
7157
7158 function Guard_Access
7159 (Cond : Node_Id;
7160 Loc : Source_Ptr;
314a23b6 7161 Ck_Node : Node_Id) return Node_Id
ee6ba406 7162 is
7163 begin
7164 if Nkind (Cond) = N_Or_Else then
7165 Set_Paren_Count (Cond, 1);
7166 end if;
7167
7168 if Nkind (Ck_Node) = N_Allocator then
7169 return Cond;
20cf157b 7170
ee6ba406 7171 else
7172 return
7173 Make_And_Then (Loc,
7174 Left_Opnd =>
7175 Make_Op_Ne (Loc,
9dfe12ae 7176 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
ee6ba406 7177 Right_Opnd => Make_Null (Loc)),
7178 Right_Opnd => Cond);
7179 end if;
7180 end Guard_Access;
7181
7182 -----------------------------
7183 -- Index_Checks_Suppressed --
7184 -----------------------------
7185
7186 function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
7187 begin
9dfe12ae 7188 if Present (E) and then Checks_May_Be_Suppressed (E) then
7189 return Is_Check_Suppressed (E, Index_Check);
7190 else
fafc6b97 7191 return Scope_Suppress.Suppress (Index_Check);
9dfe12ae 7192 end if;
ee6ba406 7193 end Index_Checks_Suppressed;
7194
7195 ----------------
7196 -- Initialize --
7197 ----------------
7198
7199 procedure Initialize is
7200 begin
7201 for J in Determine_Range_Cache_N'Range loop
7202 Determine_Range_Cache_N (J) := Empty;
7203 end loop;
2af58f67 7204
7205 Check_Names.Init;
7206
7207 for J in Int range 1 .. All_Checks loop
7208 Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1));
7209 end loop;
ee6ba406 7210 end Initialize;
7211
7212 -------------------------
7213 -- Insert_Range_Checks --
7214 -------------------------
7215
7216 procedure Insert_Range_Checks
7217 (Checks : Check_Result;
7218 Node : Node_Id;
7219 Suppress_Typ : Entity_Id;
7220 Static_Sloc : Source_Ptr := No_Location;
7221 Flag_Node : Node_Id := Empty;
7222 Do_Before : Boolean := False)
7223 is
2b4f2458 7224 Checks_On : constant Boolean :=
7225 not Index_Checks_Suppressed (Suppress_Typ)
7226 or else
7227 not Range_Checks_Suppressed (Suppress_Typ);
7228
7229 Check_Node : Node_Id;
ee6ba406 7230 Internal_Flag_Node : Node_Id := Flag_Node;
7231 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
7232
ee6ba406 7233 begin
feff2f05 7234 -- For now we just return if Checks_On is false, however this should be
7235 -- enhanced to check for an always True value in the condition and to
7236 -- generate a compilation warning???
ee6ba406 7237
ac9184ed 7238 if not Expander_Active or not Checks_On then
ee6ba406 7239 return;
7240 end if;
7241
7242 if Static_Sloc = No_Location then
7243 Internal_Static_Sloc := Sloc (Node);
7244 end if;
7245
7246 if No (Flag_Node) then
7247 Internal_Flag_Node := Node;
7248 end if;
7249
7250 for J in 1 .. 2 loop
7251 exit when No (Checks (J));
7252
7253 if Nkind (Checks (J)) = N_Raise_Constraint_Error
7254 and then Present (Condition (Checks (J)))
7255 then
7256 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
7257 Check_Node := Checks (J);
7258 Mark_Rewrite_Insertion (Check_Node);
7259
7260 if Do_Before then
7261 Insert_Before_And_Analyze (Node, Check_Node);
7262 else
7263 Insert_After_And_Analyze (Node, Check_Node);
7264 end if;
7265
7266 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
7267 end if;
7268
7269 else
7270 Check_Node :=
f15731c4 7271 Make_Raise_Constraint_Error (Internal_Static_Sloc,
7272 Reason => CE_Range_Check_Failed);
ee6ba406 7273 Mark_Rewrite_Insertion (Check_Node);
7274
7275 if Do_Before then
7276 Insert_Before_And_Analyze (Node, Check_Node);
7277 else
7278 Insert_After_And_Analyze (Node, Check_Node);
7279 end if;
7280 end if;
7281 end loop;
7282 end Insert_Range_Checks;
7283
7284 ------------------------
7285 -- Insert_Valid_Check --
7286 ------------------------
7287
aaec8d13 7288 procedure Insert_Valid_Check
7289 (Expr : Node_Id;
7290 Related_Id : Entity_Id := Empty;
7291 Is_Low_Bound : Boolean := False;
7292 Is_High_Bound : Boolean := False)
7293 is
ee6ba406 7294 Loc : constant Source_Ptr := Sloc (Expr);
70580828 7295 Typ : constant Entity_Id := Etype (Expr);
8b718dab 7296 Exp : Node_Id;
ee6ba406 7297
7298 begin
aaec8d13 7299 -- Do not insert if checks off, or if not checking validity or if
7300 -- expression is known to be valid.
ee6ba406 7301
0577b0b1 7302 if not Validity_Checks_On
7303 or else Range_Or_Validity_Checks_Suppressed (Expr)
06ad5813 7304 or else Expr_Known_Valid (Expr)
ee6ba406 7305 then
8b718dab 7306 return;
ee6ba406 7307
42c57d55 7308 -- Do not insert checks within a predicate function. This will arise
7309 -- if the current unit and the predicate function are being compiled
7310 -- with validity checks enabled.
70580828 7311
89e864b9 7312 elsif Present (Predicate_Function (Typ))
70580828 7313 and then Current_Scope = Predicate_Function (Typ)
7314 then
7315 return;
70580828 7316
310c1cde 7317 -- If the expression is a packed component of a modular type of the
7318 -- right size, the data is always valid.
7319
89e864b9 7320 elsif Nkind (Expr) = N_Selected_Component
310c1cde 7321 and then Present (Component_Clause (Entity (Selector_Name (Expr))))
7322 and then Is_Modular_Integer_Type (Typ)
7323 and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr)))
7324 then
7325 return;
89e864b9 7326
7327 -- Do not generate a validity check when inside a generic unit as this
7328 -- is an expansion activity.
7329
7330 elsif Inside_A_Generic then
7331 return;
310c1cde 7332 end if;
7333
8b718dab 7334 -- If we have a checked conversion, then validity check applies to
7335 -- the expression inside the conversion, not the result, since if
7336 -- the expression inside is valid, then so is the conversion result.
ee6ba406 7337
8b718dab 7338 Exp := Expr;
7339 while Nkind (Exp) = N_Type_Conversion loop
7340 Exp := Expression (Exp);
7341 end loop;
7342
fdb8488b 7343 -- Do not generate a check for a variable which already validates the
7344 -- value of an assignable object.
7345
7346 if Is_Validation_Variable_Reference (Exp) then
7347 return;
7348 end if;
7349
0577b0b1 7350 declare
49adf385 7351 CE : Node_Id;
49adf385 7352 PV : Node_Id;
7353 Var_Id : Entity_Id;
05fcfafb 7354
0577b0b1 7355 begin
fdb8488b 7356 -- If the expression denotes an assignable object, capture its value
7357 -- in a variable and replace the original expression by the variable.
7358 -- This approach has several effects:
06ad5813 7359
fdb8488b 7360 -- 1) The evaluation of the object results in only one read in the
7361 -- case where the object is atomic or volatile.
fa771c05 7362
49adf385 7363 -- Var ... := Object; -- read
fa771c05 7364
fdb8488b 7365 -- 2) The captured value is the one verified by attribute 'Valid.
7366 -- As a result the object is not evaluated again, which would
7367 -- result in an unwanted read in the case where the object is
7368 -- atomic or volatile.
7369
49adf385 7370 -- if not Var'Valid then -- OK, no read of Object
fdb8488b 7371
7372 -- if not Object'Valid then -- Wrong, extra read of Object
7373
7374 -- 3) The captured value replaces the original object reference.
7375 -- As a result the object is not evaluated again, in the same
7376 -- vein as 2).
7377
49adf385 7378 -- ... Var ... -- OK, no read of Object
fdb8488b 7379
7380 -- ... Object ... -- Wrong, extra read of Object
06ad5813 7381
fdb8488b 7382 -- 4) The use of a variable to capture the value of the object
7383 -- allows the propagation of any changes back to the original
7384 -- object.
7385
7386 -- procedure Call (Val : in out ...);
7387
49adf385 7388 -- Var : ... := Object; -- read Object
7389 -- if not Var'Valid then -- validity check
7390 -- Call (Var); -- modify Var
7391 -- Object := Var; -- update Object
fdb8488b 7392
7393 if Is_Variable (Exp) then
49adf385 7394 Var_Id := Make_Temporary (Loc, 'T', Exp);
fdb8488b 7395
7396 Insert_Action (Exp,
7397 Make_Object_Declaration (Loc,
49adf385 7398 Defining_Identifier => Var_Id,
fdb8488b 7399 Object_Definition => New_Occurrence_Of (Typ, Loc),
bf5f89dc 7400 Expression => New_Copy_Tree (Exp)),
7401 Suppress => Validity_Check);
7402
7403 Set_Validated_Object (Var_Id, New_Copy_Tree (Exp));
fdb8488b 7404
49adf385 7405 Rewrite (Exp, New_Occurrence_Of (Var_Id, Loc));
7406 PV := New_Occurrence_Of (Var_Id, Loc);
fdb8488b 7407
7408 -- Otherwise the expression does not denote a variable. Force its
7409 -- evaluation by capturing its value in a constant. Generate:
7410
7411 -- Temp : constant ... := Exp;
7412
7413 else
7414 Force_Evaluation
7415 (Exp => Exp,
7416 Related_Id => Related_Id,
7417 Is_Low_Bound => Is_Low_Bound,
7418 Is_High_Bound => Is_High_Bound);
7419
7420 PV := New_Copy_Tree (Exp);
7421 end if;
0577b0b1 7422
443bdccb 7423 -- A rather specialized test. If PV is an analyzed expression which
7424 -- is an indexed component of a packed array that has not been
7425 -- properly expanded, turn off its Analyzed flag to make sure it
5f46de53 7426 -- gets properly reexpanded. If the prefix is an access value,
7427 -- the dereference will be added later.
23abd64d 7428
7429 -- The reason this arises is that Duplicate_Subexpr_No_Checks did
7430 -- an analyze with the old parent pointer. This may point e.g. to
7431 -- a subprogram call, which deactivates this expansion.
7432
7433 if Analyzed (PV)
7434 and then Nkind (PV) = N_Indexed_Component
5f46de53 7435 and then Is_Array_Type (Etype (Prefix (PV)))
a88a5773 7436 and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV))))
23abd64d 7437 then
7438 Set_Analyzed (PV, False);
7439 end if;
7440
fa771c05 7441 -- Build the raise CE node to check for validity. We build a type
7442 -- qualification for the prefix, since it may not be of the form of
7443 -- a name, and we don't care in this context!
23abd64d 7444
7445 CE :=
aaec8d13 7446 Make_Raise_Constraint_Error (Loc,
7447 Condition =>
7448 Make_Op_Not (Loc,
7449 Right_Opnd =>
7450 Make_Attribute_Reference (Loc,
7451 Prefix => PV,
7452 Attribute_Name => Name_Valid)),
7453 Reason => CE_Invalid_Data);
23abd64d 7454
7455 -- Insert the validity check. Note that we do this with validity
7456 -- checks turned off, to avoid recursion, we do not want validity
39a0c1d3 7457 -- checks on the validity checking code itself.
23abd64d 7458
7459 Insert_Action (Expr, CE, Suppress => Validity_Check);
0577b0b1 7460
6fb3c314 7461 -- If the expression is a reference to an element of a bit-packed
0577b0b1 7462 -- array, then it is rewritten as a renaming declaration. If the
7463 -- expression is an actual in a call, it has not been expanded,
7464 -- waiting for the proper point at which to do it. The same happens
7465 -- with renamings, so that we have to force the expansion now. This
7466 -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb
7467 -- and exp_ch6.adb.
7468
7469 if Is_Entity_Name (Exp)
7470 and then Nkind (Parent (Entity (Exp))) =
20cf157b 7471 N_Object_Renaming_Declaration
0577b0b1 7472 then
7473 declare
7474 Old_Exp : constant Node_Id := Name (Parent (Entity (Exp)));
7475 begin
7476 if Nkind (Old_Exp) = N_Indexed_Component
7477 and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp)))
7478 then
7479 Expand_Packed_Element_Reference (Old_Exp);
7480 end if;
7481 end;
7482 end if;
0577b0b1 7483 end;
ee6ba406 7484 end Insert_Valid_Check;
7485
3cce7f32 7486 -------------------------------------
7487 -- Is_Signed_Integer_Arithmetic_Op --
7488 -------------------------------------
7489
7490 function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is
7491 begin
7492 case Nkind (N) is
99378362 7493 when N_Op_Abs
7494 | N_Op_Add
7495 | N_Op_Divide
7496 | N_Op_Expon
7497 | N_Op_Minus
7498 | N_Op_Mod
7499 | N_Op_Multiply
7500 | N_Op_Plus
7501 | N_Op_Rem
7502 | N_Op_Subtract
7503 =>
3cce7f32 7504 return Is_Signed_Integer_Type (Etype (N));
7505
99378362 7506 when N_Case_Expression
7507 | N_If_Expression
7508 =>
0326b4d4 7509 return Is_Signed_Integer_Type (Etype (N));
7510
3cce7f32 7511 when others =>
7512 return False;
7513 end case;
7514 end Is_Signed_Integer_Arithmetic_Op;
7515
fa7497e8 7516 ----------------------------------
7517 -- Install_Null_Excluding_Check --
7518 ----------------------------------
7519
7520 procedure Install_Null_Excluding_Check (N : Node_Id) is
9f294c82 7521 Loc : constant Source_Ptr := Sloc (Parent (N));
84d0d4a5 7522 Typ : constant Entity_Id := Etype (N);
7523
7b31b357 7524 function Safe_To_Capture_In_Parameter_Value return Boolean;
7525 -- Determines if it is safe to capture Known_Non_Null status for an
7526 -- the entity referenced by node N. The caller ensures that N is indeed
7527 -- an entity name. It is safe to capture the non-null status for an IN
7528 -- parameter when the reference occurs within a declaration that is sure
7529 -- to be executed as part of the declarative region.
7870823d 7530
84d0d4a5 7531 procedure Mark_Non_Null;
7870823d 7532 -- After installation of check, if the node in question is an entity
7533 -- name, then mark this entity as non-null if possible.
7534
7b31b357 7535 function Safe_To_Capture_In_Parameter_Value return Boolean is
7870823d 7536 E : constant Entity_Id := Entity (N);
7537 S : constant Entity_Id := Current_Scope;
7538 S_Par : Node_Id;
7539
7540 begin
7b31b357 7541 if Ekind (E) /= E_In_Parameter then
7542 return False;
7543 end if;
7870823d 7544
7545 -- Two initial context checks. We must be inside a subprogram body
7546 -- with declarations and reference must not appear in nested scopes.
7547
7b31b357 7548 if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure)
7870823d 7549 or else Scope (E) /= S
7550 then
7551 return False;
7552 end if;
7553
7554 S_Par := Parent (Parent (S));
7555
7556 if Nkind (S_Par) /= N_Subprogram_Body
7557 or else No (Declarations (S_Par))
7558 then
7559 return False;
7560 end if;
7561
7562 declare
7563 N_Decl : Node_Id;
7564 P : Node_Id;
7565
7566 begin
7567 -- Retrieve the declaration node of N (if any). Note that N
7568 -- may be a part of a complex initialization expression.
7569
7570 P := Parent (N);
7571 N_Decl := Empty;
7572 while Present (P) loop
7573
7b31b357 7574 -- If we have a short circuit form, and we are within the right
7575 -- hand expression, we return false, since the right hand side
7576 -- is not guaranteed to be elaborated.
7577
7578 if Nkind (P) in N_Short_Circuit
7579 and then N = Right_Opnd (P)
7580 then
7581 return False;
7582 end if;
7583
92f1631f 7584 -- Similarly, if we are in an if expression and not part of the
7585 -- condition, then we return False, since neither the THEN or
7586 -- ELSE dependent expressions will always be elaborated.
7b31b357 7587
92f1631f 7588 if Nkind (P) = N_If_Expression
7b31b357 7589 and then N /= First (Expressions (P))
7590 then
7591 return False;
e977c0cf 7592 end if;
7593
20cf157b 7594 -- If within a case expression, and not part of the expression,
7595 -- then return False, since a particular dependent expression
7596 -- may not always be elaborated
e977c0cf 7597
7598 if Nkind (P) = N_Case_Expression
7599 and then N /= Expression (P)
7600 then
7601 return False;
7b31b357 7602 end if;
7603
20cf157b 7604 -- While traversing the parent chain, if node N belongs to a
7605 -- statement, then it may never appear in a declarative region.
7870823d 7606
7607 if Nkind (P) in N_Statement_Other_Than_Procedure_Call
7608 or else Nkind (P) = N_Procedure_Call_Statement
7609 then
7610 return False;
7611 end if;
7612
7b31b357 7613 -- If we are at a declaration, record it and exit
7614
7870823d 7615 if Nkind (P) in N_Declaration
7616 and then Nkind (P) not in N_Subprogram_Specification
7617 then
7618 N_Decl := P;
7619 exit;
7620 end if;
7621
7622 P := Parent (P);
7623 end loop;
7624
7625 if No (N_Decl) then
7626 return False;
7627 end if;
7628
7629 return List_Containing (N_Decl) = Declarations (S_Par);
7630 end;
7b31b357 7631 end Safe_To_Capture_In_Parameter_Value;
84d0d4a5 7632
7633 -------------------
7634 -- Mark_Non_Null --
7635 -------------------
7636
7637 procedure Mark_Non_Null is
7638 begin
7870823d 7639 -- Only case of interest is if node N is an entity name
7640
84d0d4a5 7641 if Is_Entity_Name (N) then
7870823d 7642
7643 -- For sure, we want to clear an indication that this is known to
39a0c1d3 7644 -- be null, since if we get past this check, it definitely is not.
7870823d 7645
84d0d4a5 7646 Set_Is_Known_Null (Entity (N), False);
7647
7870823d 7648 -- We can mark the entity as known to be non-null if either it is
7649 -- safe to capture the value, or in the case of an IN parameter,
7650 -- which is a constant, if the check we just installed is in the
7651 -- declarative region of the subprogram body. In this latter case,
7b31b357 7652 -- a check is decisive for the rest of the body if the expression
7653 -- is sure to be elaborated, since we know we have to elaborate
7654 -- all declarations before executing the body.
7655
7656 -- Couldn't this always be part of Safe_To_Capture_Value ???
7870823d 7657
7658 if Safe_To_Capture_Value (N, Entity (N))
7b31b357 7659 or else Safe_To_Capture_In_Parameter_Value
7870823d 7660 then
7661 Set_Is_Known_Non_Null (Entity (N));
84d0d4a5 7662 end if;
7663 end if;
7664 end Mark_Non_Null;
7665
7666 -- Start of processing for Install_Null_Excluding_Check
fa7497e8 7667
7668 begin
84d0d4a5 7669 pragma Assert (Is_Access_Type (Typ));
fa7497e8 7670
46e32b5e 7671 -- No check inside a generic, check will be emitted in instance
fa7497e8 7672
84d0d4a5 7673 if Inside_A_Generic then
fa7497e8 7674 return;
84d0d4a5 7675 end if;
7676
7677 -- No check needed if known to be non-null
7678
7679 if Known_Non_Null (N) then
05fcfafb 7680 return;
84d0d4a5 7681 end if;
fa7497e8 7682
84d0d4a5 7683 -- If known to be null, here is where we generate a compile time check
7684
7685 if Known_Null (N) then
d16989f1 7686
20cf157b 7687 -- Avoid generating warning message inside init procs. In SPARK mode
7688 -- we can go ahead and call Apply_Compile_Time_Constraint_Error
46e32b5e 7689 -- since it will be turned into an error in any case.
d16989f1 7690
46e32b5e 7691 if (not Inside_Init_Proc or else SPARK_Mode = On)
7692
28d5d68f 7693 -- Do not emit the warning within a conditional expression,
7694 -- where the expression might not be evaluated, and the warning
7695 -- appear as extraneous noise.
46e32b5e 7696
7697 and then not Within_Case_Or_If_Expression (N)
7698 then
d16989f1 7699 Apply_Compile_Time_Constraint_Error
4098232e 7700 (N, "null value not allowed here??", CE_Access_Check_Failed);
46e32b5e 7701
7702 -- Remaining cases, where we silently insert the raise
7703
d16989f1 7704 else
7705 Insert_Action (N,
7706 Make_Raise_Constraint_Error (Loc,
7707 Reason => CE_Access_Check_Failed));
7708 end if;
7709
84d0d4a5 7710 Mark_Non_Null;
7711 return;
7712 end if;
7713
7714 -- If entity is never assigned, for sure a warning is appropriate
7715
7716 if Is_Entity_Name (N) then
7717 Check_Unset_Reference (N);
fa7497e8 7718 end if;
84d0d4a5 7719
7720 -- No check needed if checks are suppressed on the range. Note that we
7721 -- don't set Is_Known_Non_Null in this case (we could legitimately do
7722 -- so, since the program is erroneous, but we don't like to casually
7723 -- propagate such conclusions from erroneosity).
7724
7725 if Access_Checks_Suppressed (Typ) then
7726 return;
7727 end if;
7728
2af58f67 7729 -- No check needed for access to concurrent record types generated by
7730 -- the expander. This is not just an optimization (though it does indeed
7731 -- remove junk checks). It also avoids generation of junk warnings.
7732
7733 if Nkind (N) in N_Has_Chars
7734 and then Chars (N) = Name_uObject
7735 and then Is_Concurrent_Record_Type
7736 (Directly_Designated_Type (Etype (N)))
7737 then
7738 return;
7739 end if;
7740
228836e8 7741 -- No check needed in interface thunks since the runtime check is
7742 -- already performed at the caller side.
7743
7744 if Is_Thunk (Current_Scope) then
7745 return;
7746 end if;
7747
472ea160 7748 -- No check needed for the Get_Current_Excep.all.all idiom generated by
7749 -- the expander within exception handlers, since we know that the value
7750 -- can never be null.
7751
7752 -- Is this really the right way to do this? Normally we generate such
7753 -- code in the expander with checks off, and that's how we suppress this
7754 -- kind of junk check ???
7755
7756 if Nkind (N) = N_Function_Call
7757 and then Nkind (Name (N)) = N_Explicit_Dereference
7758 and then Nkind (Prefix (Name (N))) = N_Identifier
7759 and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep)
7760 then
7761 return;
7762 end if;
7763
84d0d4a5 7764 -- Otherwise install access check
7765
7766 Insert_Action (N,
7767 Make_Raise_Constraint_Error (Loc,
7768 Condition =>
7769 Make_Op_Eq (Loc,
7770 Left_Opnd => Duplicate_Subexpr_Move_Checks (N),
7771 Right_Opnd => Make_Null (Loc)),
7772 Reason => CE_Access_Check_Failed));
7773
7774 Mark_Non_Null;
fa7497e8 7775 end Install_Null_Excluding_Check;
7776
7e933b61 7777 -----------------------------------------
7778 -- Install_Primitive_Elaboration_Check --
7779 -----------------------------------------
7780
7781 procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id) is
7e933b61 7782 function Within_Compilation_Unit_Instance
7783 (Subp_Id : Entity_Id) return Boolean;
7784 -- Determine whether subprogram Subp_Id appears within an instance which
7785 -- acts as a compilation unit.
7786
7787 --------------------------------------
7788 -- Within_Compilation_Unit_Instance --
7789 --------------------------------------
7790
7791 function Within_Compilation_Unit_Instance
7792 (Subp_Id : Entity_Id) return Boolean
7793 is
7794 Pack : Entity_Id;
7795
7796 begin
7797 -- Examine the scope chain looking for a compilation-unit-level
7798 -- instance.
7799
7800 Pack := Scope (Subp_Id);
7801 while Present (Pack) and then Pack /= Standard_Standard loop
7802 if Ekind (Pack) = E_Package
7803 and then Is_Generic_Instance (Pack)
7804 and then Nkind (Parent (Unit_Declaration_Node (Pack))) =
7805 N_Compilation_Unit
7806 then
7807 return True;
7808 end if;
7809
7810 Pack := Scope (Pack);
7811 end loop;
7812
7813 return False;
7814 end Within_Compilation_Unit_Instance;
7815
7816 -- Local declarations
7817
7818 Context : constant Node_Id := Parent (Subp_Body);
7819 Loc : constant Source_Ptr := Sloc (Subp_Body);
7820 Subp_Id : constant Entity_Id := Unique_Defining_Entity (Subp_Body);
7821 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
7822
7823 Decls : List_Id;
7824 Flag_Id : Entity_Id;
7825 Set_Ins : Node_Id;
7826 Tag_Typ : Entity_Id;
7827
7828 -- Start of processing for Install_Primitive_Elaboration_Check
7829
7830 begin
7831 -- Do not generate an elaboration check in compilation modes where
7832 -- expansion is not desirable.
7833
7834 if ASIS_Mode or GNATprove_Mode then
7835 return;
7836
f0e731f2 7837 -- Do not generate an elaboration check if all checks have been
7838 -- suppressed.
62c62e4b 7839
f0e731f2 7840 elsif Suppress_Checks then
62c62e4b 7841 return;
7842
7e933b61 7843 -- Do not generate an elaboration check if the related subprogram is
7844 -- not subjected to accessibility checks.
7845
7846 elsif Elaboration_Checks_Suppressed (Subp_Id) then
7847 return;
f0e731f2 7848
7849 -- Do not generate an elaboration check if such code is not desirable
7850
7851 elsif Restriction_Active (No_Elaboration_Code) then
7852 return;
7e933b61 7853
7854 -- Do not consider subprograms which act as compilation units, because
7855 -- they cannot be the target of a dispatching call.
7856
7857 elsif Nkind (Context) = N_Compilation_Unit then
7858 return;
7859
7860 -- Only nonabstract library-level source primitives are considered for
7861 -- this check.
7862
7863 elsif not
7864 (Comes_From_Source (Subp_Id)
7865 and then Is_Library_Level_Entity (Subp_Id)
7866 and then Is_Primitive (Subp_Id)
7867 and then not Is_Abstract_Subprogram (Subp_Id))
7868 then
7869 return;
7870
7871 -- Do not consider inlined primitives, because once the body is inlined
7872 -- the reference to the elaboration flag will be out of place and will
7873 -- result in an undefined symbol.
7874
7875 elsif Is_Inlined (Subp_Id) or else Has_Pragma_Inline (Subp_Id) then
7876 return;
7877
7878 -- Do not generate a duplicate elaboration check. This happens only in
7879 -- the case of primitives completed by an expression function, as the
7880 -- corresponding body is apparently analyzed and expanded twice.
7881
7882 elsif Analyzed (Subp_Body) then
7883 return;
7884
7885 -- Do not consider primitives which occur within an instance that acts
7886 -- as a compilation unit. Such an instance defines its spec and body out
7887 -- of order (body is first) within the tree, which causes the reference
7888 -- to the elaboration flag to appear as an undefined symbol.
7889
7890 elsif Within_Compilation_Unit_Instance (Subp_Id) then
7891 return;
7892 end if;
7893
7894 Tag_Typ := Find_Dispatching_Type (Subp_Id);
7895
7896 -- Only tagged primitives may be the target of a dispatching call
7897
7898 if No (Tag_Typ) then
7899 return;
7900
7901 -- Do not consider finalization-related primitives, because they may
7902 -- need to be called while elaboration is taking place.
7903
7904 elsif Is_Controlled (Tag_Typ)
7905 and then Nam_In (Chars (Subp_Id), Name_Adjust,
7906 Name_Finalize,
7907 Name_Initialize)
7908 then
7909 return;
7910 end if;
7911
7912 -- Create the declaration of the elaboration flag. The name carries a
7913 -- unique counter in case of name overloading.
7914
7915 Flag_Id :=
7916 Make_Defining_Identifier (Loc,
7917 Chars => New_External_Name (Chars (Subp_Id), 'F', -1));
7918 Set_Is_Frozen (Flag_Id);
7919
7920 -- Insert the declaration of the elaboration flag in front of the
7921 -- primitive spec and analyze it in the proper context.
7922
7923 Push_Scope (Scope (Subp_Id));
7924
7925 -- Generate:
7926 -- F : Boolean := False;
7927
7928 Insert_Action (Subp_Decl,
7929 Make_Object_Declaration (Loc,
7930 Defining_Identifier => Flag_Id,
7931 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7932 Expression => New_Occurrence_Of (Standard_False, Loc)));
7933 Pop_Scope;
7934
7935 -- Prevent the compiler from optimizing the elaboration check by killing
7936 -- the current value of the flag and the associated assignment.
7937
7938 Set_Current_Value (Flag_Id, Empty);
7939 Set_Last_Assignment (Flag_Id, Empty);
7940
7941 -- Add a check at the top of the body declarations to ensure that the
7942 -- elaboration flag has been set.
7943
7944 Decls := Declarations (Subp_Body);
7945
7946 if No (Decls) then
7947 Decls := New_List;
7948 Set_Declarations (Subp_Body, Decls);
7949 end if;
7950
7951 -- Generate:
7952 -- if not F then
7953 -- raise Program_Error with "access before elaboration";
7954 -- end if;
7955
7956 Prepend_To (Decls,
7957 Make_Raise_Program_Error (Loc,
7958 Condition =>
7959 Make_Op_Not (Loc,
7960 Right_Opnd => New_Occurrence_Of (Flag_Id, Loc)),
7961 Reason => PE_Access_Before_Elaboration));
7962
7963 Analyze (First (Decls));
7964
7965 -- Set the elaboration flag once the body has been elaborated. Insert
7966 -- the statement after the subprogram stub when the primitive body is
7967 -- a subunit.
7968
7969 if Nkind (Context) = N_Subunit then
7970 Set_Ins := Corresponding_Stub (Context);
7971 else
7972 Set_Ins := Subp_Body;
7973 end if;
7974
7975 -- Generate:
7976 -- F := True;
7977
7978 Insert_After_And_Analyze (Set_Ins,
7979 Make_Assignment_Statement (Loc,
7980 Name => New_Occurrence_Of (Flag_Id, Loc),
7981 Expression => New_Occurrence_Of (Standard_True, Loc)));
7982 end Install_Primitive_Elaboration_Check;
7983
ee6ba406 7984 --------------------------
7985 -- Install_Static_Check --
7986 --------------------------
7987
7988 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
cda40848 7989 Stat : constant Boolean := Is_OK_Static_Expression (R_Cno);
ee6ba406 7990 Typ : constant Entity_Id := Etype (R_Cno);
7991
7992 begin
f15731c4 7993 Rewrite (R_Cno,
7994 Make_Raise_Constraint_Error (Loc,
7995 Reason => CE_Range_Check_Failed));
ee6ba406 7996 Set_Analyzed (R_Cno);
7997 Set_Etype (R_Cno, Typ);
7998 Set_Raises_Constraint_Error (R_Cno);
7999 Set_Is_Static_Expression (R_Cno, Stat);
840ab274 8000
8001 -- Now deal with possible local raise handling
8002
8003 Possible_Local_Raise (R_Cno, Standard_Constraint_Error);
ee6ba406 8004 end Install_Static_Check;
8005
3cce7f32 8006 -------------------------
8007 -- Is_Check_Suppressed --
8008 -------------------------
8009
8010 function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is
8011 Ptr : Suppress_Stack_Entry_Ptr;
8012
8013 begin
8014 -- First search the local entity suppress stack. We search this from the
8015 -- top of the stack down so that we get the innermost entry that applies
8016 -- to this case if there are nested entries.
8017
8018 Ptr := Local_Suppress_Stack_Top;
8019 while Ptr /= null loop
8020 if (Ptr.Entity = Empty or else Ptr.Entity = E)
8021 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
8022 then
8023 return Ptr.Suppress;
8024 end if;
8025
8026 Ptr := Ptr.Prev;
8027 end loop;
8028
8029 -- Now search the global entity suppress table for a matching entry.
8030 -- We also search this from the top down so that if there are multiple
8031 -- pragmas for the same entity, the last one applies (not clear what
8032 -- or whether the RM specifies this handling, but it seems reasonable).
8033
8034 Ptr := Global_Suppress_Stack_Top;
8035 while Ptr /= null loop
8036 if (Ptr.Entity = Empty or else Ptr.Entity = E)
8037 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
8038 then
8039 return Ptr.Suppress;
8040 end if;
8041
8042 Ptr := Ptr.Prev;
8043 end loop;
8044
8045 -- If we did not find a matching entry, then use the normal scope
8046 -- suppress value after all (actually this will be the global setting
8047 -- since it clearly was not overridden at any point). For a predefined
8048 -- check, we test the specific flag. For a user defined check, we check
8049 -- the All_Checks flag. The Overflow flag requires special handling to
8050 -- deal with the General vs Assertion case
8051
8052 if C = Overflow_Check then
8053 return Overflow_Checks_Suppressed (Empty);
8054 elsif C in Predefined_Check_Id then
8055 return Scope_Suppress.Suppress (C);
8056 else
8057 return Scope_Suppress.Suppress (All_Checks);
8058 end if;
8059 end Is_Check_Suppressed;
8060
9dfe12ae 8061 ---------------------
8062 -- Kill_All_Checks --
8063 ---------------------
8064
8065 procedure Kill_All_Checks is
8066 begin
8067 if Debug_Flag_CC then
8068 w ("Kill_All_Checks");
8069 end if;
8070
feff2f05 8071 -- We reset the number of saved checks to zero, and also modify all
8072 -- stack entries for statement ranges to indicate that the number of
8073 -- checks at each level is now zero.
9dfe12ae 8074
8075 Num_Saved_Checks := 0;
8076
96da3284 8077 -- Note: the Int'Min here avoids any possibility of J being out of
8078 -- range when called from e.g. Conditional_Statements_Begin.
8079
8080 for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop
9dfe12ae 8081 Saved_Checks_Stack (J) := 0;
8082 end loop;
8083 end Kill_All_Checks;
8084
8085 -----------------
8086 -- Kill_Checks --
8087 -----------------
8088
8089 procedure Kill_Checks (V : Entity_Id) is
8090 begin
8091 if Debug_Flag_CC then
8092 w ("Kill_Checks for entity", Int (V));
8093 end if;
8094
8095 for J in 1 .. Num_Saved_Checks loop
8096 if Saved_Checks (J).Entity = V then
8097 if Debug_Flag_CC then
8098 w (" Checks killed for saved check ", J);
8099 end if;
8100
8101 Saved_Checks (J).Killed := True;
8102 end if;
8103 end loop;
8104 end Kill_Checks;
8105
ee6ba406 8106 ------------------------------
8107 -- Length_Checks_Suppressed --
8108 ------------------------------
8109
8110 function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
8111 begin
9dfe12ae 8112 if Present (E) and then Checks_May_Be_Suppressed (E) then
8113 return Is_Check_Suppressed (E, Length_Check);
8114 else
fafc6b97 8115 return Scope_Suppress.Suppress (Length_Check);
9dfe12ae 8116 end if;
ee6ba406 8117 end Length_Checks_Suppressed;
8118
3cce7f32 8119 -----------------------
8120 -- Make_Bignum_Block --
8121 -----------------------
8122
8123 function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is
8124 M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM);
3cce7f32 8125 begin
8126 return
8127 Make_Block_Statement (Loc,
97c15ab0 8128 Declarations =>
8129 New_List (Build_SS_Mark_Call (Loc, M)),
3cce7f32 8130 Handled_Statement_Sequence =>
8131 Make_Handled_Sequence_Of_Statements (Loc,
97c15ab0 8132 Statements => New_List (Build_SS_Release_Call (Loc, M))));
3cce7f32 8133 end Make_Bignum_Block;
8134
0df9d43f 8135 ----------------------------------
8136 -- Minimize_Eliminate_Overflows --
8137 ----------------------------------
3cce7f32 8138
f32c377d 8139 -- This is a recursive routine that is called at the top of an expression
8140 -- tree to properly process overflow checking for a whole subtree by making
8141 -- recursive calls to process operands. This processing may involve the use
8142 -- of bignum or long long integer arithmetic, which will change the types
8143 -- of operands and results. That's why we can't do this bottom up (since
21a55437 8144 -- it would interfere with semantic analysis).
f32c377d 8145
21a55437 8146 -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then
0df9d43f 8147 -- the operator expansion routines, as well as the expansion routines for
8148 -- if/case expression, do nothing (for the moment) except call the routine
8149 -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That
8150 -- routine does nothing for non top-level nodes, so at the point where the
8151 -- call is made for the top level node, the entire expression subtree has
8152 -- not been expanded, or processed for overflow. All that has to happen as
8153 -- a result of the top level call to this routine.
f32c377d 8154
8155 -- As noted above, the overflow processing works by making recursive calls
8156 -- for the operands, and figuring out what to do, based on the processing
8157 -- of these operands (e.g. if a bignum operand appears, the parent op has
8158 -- to be done in bignum mode), and the determined ranges of the operands.
8159
8160 -- After possible rewriting of a constituent subexpression node, a call is
4fb5f0a0 8161 -- made to either reexpand the node (if nothing has changed) or reanalyze
21a55437 8162 -- the node (if it has been modified by the overflow check processing). The
8163 -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid
8164 -- a recursive call into the whole overflow apparatus, an important rule
0df9d43f 8165 -- for this call is that the overflow handling mode must be temporarily set
8166 -- to STRICT.
f32c377d 8167
0df9d43f 8168 procedure Minimize_Eliminate_Overflows
61016a7a 8169 (N : Node_Id;
8170 Lo : out Uint;
8171 Hi : out Uint;
8172 Top_Level : Boolean)
3cce7f32 8173 is
0326b4d4 8174 Rtyp : constant Entity_Id := Etype (N);
8175 pragma Assert (Is_Signed_Integer_Type (Rtyp));
8176 -- Result type, must be a signed integer type
3cce7f32 8177
db415383 8178 Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode;
3cce7f32 8179 pragma Assert (Check_Mode in Minimized_Or_Eliminated);
8180
8181 Loc : constant Source_Ptr := Sloc (N);
8182
8183 Rlo, Rhi : Uint;
0326b4d4 8184 -- Ranges of values for right operand (operator case)
3cce7f32 8185
16149377 8186 Llo : Uint := No_Uint; -- initialize to prevent warning
8187 Lhi : Uint := No_Uint; -- initialize to prevent warning
0326b4d4 8188 -- Ranges of values for left operand (operator case)
3cce7f32 8189
49b3a812 8190 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
8191 -- Operands and results are of this type when we convert
8192
0326b4d4 8193 LLLo : constant Uint := Intval (Type_Low_Bound (LLIB));
8194 LLHi : constant Uint := Intval (Type_High_Bound (LLIB));
3cce7f32 8195 -- Bounds of Long_Long_Integer
8196
8197 Binary : constant Boolean := Nkind (N) in N_Binary_Op;
8198 -- Indicates binary operator case
8199
8200 OK : Boolean;
8201 -- Used in call to Determine_Range
8202
61016a7a 8203 Bignum_Operands : Boolean;
8204 -- Set True if one or more operands is already of type Bignum, meaning
8205 -- that for sure (regardless of Top_Level setting) we are committed to
0326b4d4 8206 -- doing the operation in Bignum mode (or in the case of a case or if
21a55437 8207 -- expression, converting all the dependent expressions to Bignum).
0326b4d4 8208
8209 Long_Long_Integer_Operands : Boolean;
21a55437 8210 -- Set True if one or more operands is already of type Long_Long_Integer
0326b4d4 8211 -- which means that if the result is known to be in the result type
8212 -- range, then we must convert such operands back to the result type.
0df9d43f 8213
8214 procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False);
8215 -- This is called when we have modified the node and we therefore need
8216 -- to reanalyze it. It is important that we reset the mode to STRICT for
8217 -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode
39a0c1d3 8218 -- we would reenter this routine recursively which would not be good.
0df9d43f 8219 -- The argument Suppress is set True if we also want to suppress
8220 -- overflow checking for the reexpansion (this is set when we know
8221 -- overflow is not possible). Typ is the type for the reanalysis.
8222
8223 procedure Reexpand (Suppress : Boolean := False);
8224 -- This is like Reanalyze, but does not do the Analyze step, it only
8225 -- does a reexpansion. We do this reexpansion in STRICT mode, so that
8226 -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we
8227 -- follow the normal expansion path (e.g. converting A**4 to A**2**2).
8228 -- Note that skipping reanalysis is not just an optimization, testing
8229 -- has showed up several complex cases in which reanalyzing an already
8230 -- analyzed node causes incorrect behavior.
4fb5f0a0 8231
0326b4d4 8232 function In_Result_Range return Boolean;
8233 -- Returns True iff Lo .. Hi are within range of the result type
61016a7a 8234
2fe22c69 8235 procedure Max (A : in out Uint; B : Uint);
21a55437 8236 -- If A is No_Uint, sets A to B, else to UI_Max (A, B)
2fe22c69 8237
8238 procedure Min (A : in out Uint; B : Uint);
21a55437 8239 -- If A is No_Uint, sets A to B, else to UI_Min (A, B)
2fe22c69 8240
0326b4d4 8241 ---------------------
8242 -- In_Result_Range --
8243 ---------------------
8244
8245 function In_Result_Range return Boolean is
8246 begin
f32c377d 8247 if Lo = No_Uint or else Hi = No_Uint then
8248 return False;
8249
cda40848 8250 elsif Is_OK_Static_Subtype (Etype (N)) then
0326b4d4 8251 return Lo >= Expr_Value (Type_Low_Bound (Rtyp))
8252 and then
8253 Hi <= Expr_Value (Type_High_Bound (Rtyp));
f32c377d 8254
0326b4d4 8255 else
8256 return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp)))
8257 and then
8258 Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp)));
8259 end if;
8260 end In_Result_Range;
8261
2fe22c69 8262 ---------
8263 -- Max --
8264 ---------
8265
8266 procedure Max (A : in out Uint; B : Uint) is
8267 begin
8268 if A = No_Uint or else B > A then
8269 A := B;
8270 end if;
8271 end Max;
8272
8273 ---------
8274 -- Min --
8275 ---------
8276
8277 procedure Min (A : in out Uint; B : Uint) is
8278 begin
8279 if A = No_Uint or else B < A then
8280 A := B;
8281 end if;
8282 end Min;
8283
0df9d43f 8284 ---------------
8285 -- Reanalyze --
8286 ---------------
8287
8288 procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is
db415383 8289 Svg : constant Overflow_Mode_Type :=
8290 Scope_Suppress.Overflow_Mode_General;
8291 Sva : constant Overflow_Mode_Type :=
8292 Scope_Suppress.Overflow_Mode_Assertions;
0df9d43f 8293 Svo : constant Boolean :=
8294 Scope_Suppress.Suppress (Overflow_Check);
8295
8296 begin
db415383 8297 Scope_Suppress.Overflow_Mode_General := Strict;
8298 Scope_Suppress.Overflow_Mode_Assertions := Strict;
0df9d43f 8299
8300 if Suppress then
8301 Scope_Suppress.Suppress (Overflow_Check) := True;
8302 end if;
8303
8304 Analyze_And_Resolve (N, Typ);
8305
2b108e18 8306 Scope_Suppress.Suppress (Overflow_Check) := Svo;
8307 Scope_Suppress.Overflow_Mode_General := Svg;
8308 Scope_Suppress.Overflow_Mode_Assertions := Sva;
0df9d43f 8309 end Reanalyze;
8310
4fb5f0a0 8311 --------------
8312 -- Reexpand --
8313 --------------
8314
0df9d43f 8315 procedure Reexpand (Suppress : Boolean := False) is
db415383 8316 Svg : constant Overflow_Mode_Type :=
8317 Scope_Suppress.Overflow_Mode_General;
8318 Sva : constant Overflow_Mode_Type :=
8319 Scope_Suppress.Overflow_Mode_Assertions;
0df9d43f 8320 Svo : constant Boolean :=
8321 Scope_Suppress.Suppress (Overflow_Check);
8322
4fb5f0a0 8323 begin
db415383 8324 Scope_Suppress.Overflow_Mode_General := Strict;
8325 Scope_Suppress.Overflow_Mode_Assertions := Strict;
4fb5f0a0 8326 Set_Analyzed (N, False);
0df9d43f 8327
8328 if Suppress then
8329 Scope_Suppress.Suppress (Overflow_Check) := True;
8330 end if;
8331
4fb5f0a0 8332 Expand (N);
0df9d43f 8333
2b108e18 8334 Scope_Suppress.Suppress (Overflow_Check) := Svo;
8335 Scope_Suppress.Overflow_Mode_General := Svg;
8336 Scope_Suppress.Overflow_Mode_Assertions := Sva;
4fb5f0a0 8337 end Reexpand;
8338
0df9d43f 8339 -- Start of processing for Minimize_Eliminate_Overflows
2fe22c69 8340
3cce7f32 8341 begin
0326b4d4 8342 -- Case where we do not have a signed integer arithmetic operation
3cce7f32 8343
8344 if not Is_Signed_Integer_Arithmetic_Op (N) then
8345
8346 -- Use the normal Determine_Range routine to get the range. We
8347 -- don't require operands to be valid, invalid values may result in
8348 -- rubbish results where the result has not been properly checked for
39a0c1d3 8349 -- overflow, that's fine.
3cce7f32 8350
8351 Determine_Range (N, OK, Lo, Hi, Assume_Valid => False);
8352
21a55437 8353 -- If Determine_Range did not work (can this in fact happen? Not
3cce7f32 8354 -- clear but might as well protect), use type bounds.
8355
8356 if not OK then
8357 Lo := Intval (Type_Low_Bound (Base_Type (Etype (N))));
8358 Hi := Intval (Type_High_Bound (Base_Type (Etype (N))));
8359 end if;
8360
8361 -- If we don't have a binary operator, all we have to do is to set
20cf157b 8362 -- the Hi/Lo range, so we are done.
3cce7f32 8363
8364 return;
8365
0326b4d4 8366 -- Processing for if expression
8367
92f1631f 8368 elsif Nkind (N) = N_If_Expression then
0326b4d4 8369 declare
8370 Then_DE : constant Node_Id := Next (First (Expressions (N)));
8371 Else_DE : constant Node_Id := Next (Then_DE);
8372
8373 begin
8374 Bignum_Operands := False;
8375
0df9d43f 8376 Minimize_Eliminate_Overflows
0326b4d4 8377 (Then_DE, Lo, Hi, Top_Level => False);
8378
8379 if Lo = No_Uint then
8380 Bignum_Operands := True;
8381 end if;
8382
0df9d43f 8383 Minimize_Eliminate_Overflows
0326b4d4 8384 (Else_DE, Rlo, Rhi, Top_Level => False);
8385
8386 if Rlo = No_Uint then
8387 Bignum_Operands := True;
8388 else
8389 Long_Long_Integer_Operands :=
8390 Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB;
8391
8392 Min (Lo, Rlo);
8393 Max (Hi, Rhi);
8394 end if;
8395
21a55437 8396 -- If at least one of our operands is now Bignum, we must rebuild
8397 -- the if expression to use Bignum operands. We will analyze the
0326b4d4 8398 -- rebuilt if expression with overflow checks off, since once we
39a0c1d3 8399 -- are in bignum mode, we are all done with overflow checks.
0326b4d4 8400
8401 if Bignum_Operands then
8402 Rewrite (N,
92f1631f 8403 Make_If_Expression (Loc,
0326b4d4 8404 Expressions => New_List (
8405 Remove_Head (Expressions (N)),
8406 Convert_To_Bignum (Then_DE),
8407 Convert_To_Bignum (Else_DE)),
8408 Is_Elsif => Is_Elsif (N)));
8409
0df9d43f 8410 Reanalyze (RTE (RE_Bignum), Suppress => True);
0326b4d4 8411
8412 -- If we have no Long_Long_Integer operands, then we are in result
8413 -- range, since it means that none of our operands felt the need
8414 -- to worry about overflow (otherwise it would have already been
4fb5f0a0 8415 -- converted to long long integer or bignum). We reexpand to
8416 -- complete the expansion of the if expression (but we do not
8417 -- need to reanalyze).
0326b4d4 8418
8419 elsif not Long_Long_Integer_Operands then
8420 Set_Do_Overflow_Check (N, False);
0df9d43f 8421 Reexpand;
0326b4d4 8422
8423 -- Otherwise convert us to long long integer mode. Note that we
8424 -- don't need any further overflow checking at this level.
8425
8426 else
8427 Convert_To_And_Rewrite (LLIB, Then_DE);
8428 Convert_To_And_Rewrite (LLIB, Else_DE);
8429 Set_Etype (N, LLIB);
f32c377d 8430
8431 -- Now reanalyze with overflow checks off
8432
0326b4d4 8433 Set_Do_Overflow_Check (N, False);
0df9d43f 8434 Reanalyze (LLIB, Suppress => True);
0326b4d4 8435 end if;
8436 end;
8437
8438 return;
8439
8440 -- Here for case expression
8441
8442 elsif Nkind (N) = N_Case_Expression then
8443 Bignum_Operands := False;
8444 Long_Long_Integer_Operands := False;
0326b4d4 8445
8446 declare
f32c377d 8447 Alt : Node_Id;
0326b4d4 8448
8449 begin
8450 -- Loop through expressions applying recursive call
8451
8452 Alt := First (Alternatives (N));
8453 while Present (Alt) loop
8454 declare
8455 Aexp : constant Node_Id := Expression (Alt);
8456
8457 begin
0df9d43f 8458 Minimize_Eliminate_Overflows
0326b4d4 8459 (Aexp, Lo, Hi, Top_Level => False);
8460
8461 if Lo = No_Uint then
8462 Bignum_Operands := True;
8463 elsif Etype (Aexp) = LLIB then
8464 Long_Long_Integer_Operands := True;
8465 end if;
8466 end;
8467
8468 Next (Alt);
8469 end loop;
8470
8471 -- If we have no bignum or long long integer operands, it means
8472 -- that none of our dependent expressions could raise overflow.
8473 -- In this case, we simply return with no changes except for
8474 -- resetting the overflow flag, since we are done with overflow
4fb5f0a0 8475 -- checks for this node. We will reexpand to get the needed
8476 -- expansion for the case expression, but we do not need to
21a55437 8477 -- reanalyze, since nothing has changed.
0326b4d4 8478
f32c377d 8479 if not (Bignum_Operands or Long_Long_Integer_Operands) then
0326b4d4 8480 Set_Do_Overflow_Check (N, False);
0df9d43f 8481 Reexpand (Suppress => True);
0326b4d4 8482
8483 -- Otherwise we are going to rebuild the case expression using
8484 -- either bignum or long long integer operands throughout.
8485
8486 else
f32c377d 8487 declare
8488 Rtype : Entity_Id;
16149377 8489 pragma Warnings (Off, Rtype);
f32c377d 8490 New_Alts : List_Id;
8491 New_Exp : Node_Id;
8492
8493 begin
8494 New_Alts := New_List;
8495 Alt := First (Alternatives (N));
8496 while Present (Alt) loop
8497 if Bignum_Operands then
8498 New_Exp := Convert_To_Bignum (Expression (Alt));
8499 Rtype := RTE (RE_Bignum);
8500 else
8501 New_Exp := Convert_To (LLIB, Expression (Alt));
8502 Rtype := LLIB;
8503 end if;
0326b4d4 8504
f32c377d 8505 Append_To (New_Alts,
8506 Make_Case_Expression_Alternative (Sloc (Alt),
8507 Actions => No_List,
8508 Discrete_Choices => Discrete_Choices (Alt),
8509 Expression => New_Exp));
0326b4d4 8510
f32c377d 8511 Next (Alt);
8512 end loop;
0326b4d4 8513
f32c377d 8514 Rewrite (N,
8515 Make_Case_Expression (Loc,
8516 Expression => Expression (N),
8517 Alternatives => New_Alts));
0326b4d4 8518
0df9d43f 8519 Reanalyze (Rtype, Suppress => True);
f32c377d 8520 end;
0326b4d4 8521 end if;
8522 end;
8523
8524 return;
8525 end if;
8526
8527 -- If we have an arithmetic operator we make recursive calls on the
3cce7f32 8528 -- operands to get the ranges (and to properly process the subtree
20cf157b 8529 -- that lies below us).
3cce7f32 8530
0df9d43f 8531 Minimize_Eliminate_Overflows
0326b4d4 8532 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
3cce7f32 8533
0326b4d4 8534 if Binary then
0df9d43f 8535 Minimize_Eliminate_Overflows
0326b4d4 8536 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
3cce7f32 8537 end if;
8538
f32c377d 8539 -- Record if we have Long_Long_Integer operands
8540
8541 Long_Long_Integer_Operands :=
8542 Etype (Right_Opnd (N)) = LLIB
8543 or else (Binary and then Etype (Left_Opnd (N)) = LLIB);
8544
8545 -- If either operand is a bignum, then result will be a bignum and we
8546 -- don't need to do any range analysis. As previously discussed we could
8547 -- do range analysis in such cases, but it could mean working with giant
8548 -- numbers at compile time for very little gain (the number of cases
21a55437 8549 -- in which we could slip back from bignum mode is small).
3cce7f32 8550
8551 if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then
8552 Lo := No_Uint;
8553 Hi := No_Uint;
61016a7a 8554 Bignum_Operands := True;
3cce7f32 8555
8556 -- Otherwise compute result range
8557
8558 else
61016a7a 8559 Bignum_Operands := False;
8560
3cce7f32 8561 case Nkind (N) is
8562
8563 -- Absolute value
8564
8565 when N_Op_Abs =>
8566 Lo := Uint_0;
de922300 8567 Hi := UI_Max (abs Rlo, abs Rhi);
3cce7f32 8568
8569 -- Addition
8570
8571 when N_Op_Add =>
8572 Lo := Llo + Rlo;
8573 Hi := Lhi + Rhi;
8574
8575 -- Division
8576
8577 when N_Op_Divide =>
2fe22c69 8578
5f4275e1 8579 -- If the right operand can only be zero, set 0..0
2fe22c69 8580
5f4275e1 8581 if Rlo = 0 and then Rhi = 0 then
8582 Lo := Uint_0;
8583 Hi := Uint_0;
2fe22c69 8584
5f4275e1 8585 -- Possible bounds of division must come from dividing end
8586 -- values of the input ranges (four possibilities), provided
8587 -- zero is not included in the possible values of the right
8588 -- operand.
8589
8590 -- Otherwise, we just consider two intervals of values for
8591 -- the right operand: the interval of negative values (up to
8592 -- -1) and the interval of positive values (starting at 1).
8593 -- Since division by 1 is the identity, and division by -1
8594 -- is negation, we get all possible bounds of division in that
8595 -- case by considering:
8596 -- - all values from the division of end values of input
8597 -- ranges;
8598 -- - the end values of the left operand;
8599 -- - the negation of the end values of the left operand.
2fe22c69 8600
5f4275e1 8601 else
8602 declare
8603 Mrk : constant Uintp.Save_Mark := Mark;
8604 -- Mark so we can release the RR and Ev values
2fe22c69 8605
5f4275e1 8606 Ev1 : Uint;
8607 Ev2 : Uint;
8608 Ev3 : Uint;
8609 Ev4 : Uint;
2fe22c69 8610
5f4275e1 8611 begin
8612 -- Discard extreme values of zero for the divisor, since
8613 -- they will simply result in an exception in any case.
2fe22c69 8614
5f4275e1 8615 if Rlo = 0 then
8616 Rlo := Uint_1;
8617 elsif Rhi = 0 then
8618 Rhi := -Uint_1;
2fe22c69 8619 end if;
2fe22c69 8620
5f4275e1 8621 -- Compute possible bounds coming from dividing end
8622 -- values of the input ranges.
2fe22c69 8623
5f4275e1 8624 Ev1 := Llo / Rlo;
8625 Ev2 := Llo / Rhi;
8626 Ev3 := Lhi / Rlo;
8627 Ev4 := Lhi / Rhi;
2fe22c69 8628
5f4275e1 8629 Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4));
8630 Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4));
2fe22c69 8631
5f4275e1 8632 -- If the right operand can be both negative or positive,
8633 -- include the end values of the left operand in the
8634 -- extreme values, as well as their negation.
2fe22c69 8635
5f4275e1 8636 if Rlo < 0 and then Rhi > 0 then
8637 Ev1 := Llo;
8638 Ev2 := -Llo;
8639 Ev3 := Lhi;
8640 Ev4 := -Lhi;
2fe22c69 8641
5f4275e1 8642 Min (Lo,
8643 UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)));
8644 Max (Hi,
8645 UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)));
2fe22c69 8646 end if;
2fe22c69 8647
5f4275e1 8648 -- Release the RR and Ev values
2fe22c69 8649
5f4275e1 8650 Release_And_Save (Mrk, Lo, Hi);
8651 end;
8652 end if;
3cce7f32 8653
8654 -- Exponentiation
8655
8656 when N_Op_Expon =>
de922300 8657
8658 -- Discard negative values for the exponent, since they will
8659 -- simply result in an exception in any case.
8660
8661 if Rhi < 0 then
8662 Rhi := Uint_0;
8663 elsif Rlo < 0 then
8664 Rlo := Uint_0;
8665 end if;
8666
8667 -- Estimate number of bits in result before we go computing
8668 -- giant useless bounds. Basically the number of bits in the
8669 -- result is the number of bits in the base multiplied by the
8670 -- value of the exponent. If this is big enough that the result
8671 -- definitely won't fit in Long_Long_Integer, switch to bignum
8672 -- mode immediately, and avoid computing giant bounds.
8673
8674 -- The comparison here is approximate, but conservative, it
8675 -- only clicks on cases that are sure to exceed the bounds.
8676
8677 if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then
8678 Lo := No_Uint;
8679 Hi := No_Uint;
8680
8681 -- If right operand is zero then result is 1
8682
8683 elsif Rhi = 0 then
8684 Lo := Uint_1;
8685 Hi := Uint_1;
8686
8687 else
8688 -- High bound comes either from exponentiation of largest
5f4275e1 8689 -- positive value to largest exponent value, or from
8690 -- the exponentiation of most negative value to an
8691 -- even exponent.
de922300 8692
8693 declare
8694 Hi1, Hi2 : Uint;
8695
8696 begin
5f4275e1 8697 if Lhi > 0 then
de922300 8698 Hi1 := Lhi ** Rhi;
8699 else
8700 Hi1 := Uint_0;
8701 end if;
8702
8703 if Llo < 0 then
8704 if Rhi mod 2 = 0 then
de922300 8705 Hi2 := Llo ** Rhi;
5f4275e1 8706 else
8707 Hi2 := Llo ** (Rhi - 1);
de922300 8708 end if;
8709 else
8710 Hi2 := Uint_0;
8711 end if;
8712
8713 Hi := UI_Max (Hi1, Hi2);
8714 end;
8715
8716 -- Result can only be negative if base can be negative
8717
8718 if Llo < 0 then
21a55437 8719 if Rhi mod 2 = 0 then
de922300 8720 Lo := Llo ** (Rhi - 1);
8721 else
8722 Lo := Llo ** Rhi;
8723 end if;
8724
21a55437 8725 -- Otherwise low bound is minimum ** minimum
de922300 8726
8727 else
8728 Lo := Llo ** Rlo;
8729 end if;
8730 end if;
3cce7f32 8731
8732 -- Negation
8733
8734 when N_Op_Minus =>
8735 Lo := -Rhi;
8736 Hi := -Rlo;
8737
8738 -- Mod
8739
8740 when N_Op_Mod =>
2fe22c69 8741 declare
5f4275e1 8742 Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1;
2fe22c69 8743 -- This is the maximum absolute value of the result
8744
8745 begin
8746 Lo := Uint_0;
8747 Hi := Uint_0;
8748
8749 -- The result depends only on the sign and magnitude of
8750 -- the right operand, it does not depend on the sign or
8751 -- magnitude of the left operand.
8752
8753 if Rlo < 0 then
8754 Lo := -Maxabs;
8755 end if;
8756
8757 if Rhi > 0 then
8758 Hi := Maxabs;
8759 end if;
8760 end;
3cce7f32 8761
8762 -- Multiplication
8763
8764 when N_Op_Multiply =>
49b3a812 8765
8766 -- Possible bounds of multiplication must come from multiplying
8767 -- end values of the input ranges (four possibilities).
8768
8769 declare
8770 Mrk : constant Uintp.Save_Mark := Mark;
8771 -- Mark so we can release the Ev values
8772
8773 Ev1 : constant Uint := Llo * Rlo;
8774 Ev2 : constant Uint := Llo * Rhi;
8775 Ev3 : constant Uint := Lhi * Rlo;
8776 Ev4 : constant Uint := Lhi * Rhi;
8777
8778 begin
8779 Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4));
8780 Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4));
8781
8782 -- Release the Ev values
8783
8784 Release_And_Save (Mrk, Lo, Hi);
8785 end;
3cce7f32 8786
8787 -- Plus operator (affirmation)
8788
8789 when N_Op_Plus =>
8790 Lo := Rlo;
8791 Hi := Rhi;
8792
8793 -- Remainder
8794
8795 when N_Op_Rem =>
2fe22c69 8796 declare
5f4275e1 8797 Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1;
2fe22c69 8798 -- This is the maximum absolute value of the result. Note
5f4275e1 8799 -- that the result range does not depend on the sign of the
8800 -- right operand.
2fe22c69 8801
8802 begin
8803 Lo := Uint_0;
8804 Hi := Uint_0;
8805
8806 -- Case of left operand negative, which results in a range
8807 -- of -Maxabs .. 0 for those negative values. If there are
8808 -- no negative values then Lo value of result is always 0.
8809
8810 if Llo < 0 then
8811 Lo := -Maxabs;
8812 end if;
8813
8814 -- Case of left operand positive
8815
8816 if Lhi > 0 then
8817 Hi := Maxabs;
8818 end if;
8819 end;
3cce7f32 8820
8821 -- Subtract
8822
8823 when N_Op_Subtract =>
8824 Lo := Llo - Rhi;
8825 Hi := Lhi - Rlo;
8826
8827 -- Nothing else should be possible
8828
8829 when others =>
8830 raise Program_Error;
3cce7f32 8831 end case;
8832 end if;
8833
4fb5f0a0 8834 -- Here for the case where we have not rewritten anything (no bignum
21a55437 8835 -- operands or long long integer operands), and we know the result.
8836 -- If we know we are in the result range, and we do not have Bignum
8837 -- operands or Long_Long_Integer operands, we can just reexpand with
8838 -- overflow checks turned off (since we know we cannot have overflow).
8839 -- As always the reexpansion is required to complete expansion of the
8840 -- operator, but we do not need to reanalyze, and we prevent recursion
8841 -- by suppressing the check.
f32c377d 8842
8843 if not (Bignum_Operands or Long_Long_Integer_Operands)
8844 and then In_Result_Range
8845 then
8846 Set_Do_Overflow_Check (N, False);
0df9d43f 8847 Reexpand (Suppress => True);
f32c377d 8848 return;
8849
8850 -- Here we know that we are not in the result range, and in the general
21a55437 8851 -- case we will move into either the Bignum or Long_Long_Integer domain
8852 -- to compute the result. However, there is one exception. If we are
8853 -- at the top level, and we do not have Bignum or Long_Long_Integer
8854 -- operands, we will have to immediately convert the result back to
8855 -- the result type, so there is no point in Bignum/Long_Long_Integer
8856 -- fiddling.
f32c377d 8857
8858 elsif Top_Level
8859 and then not (Bignum_Operands or Long_Long_Integer_Operands)
b6a8f264 8860
8861 -- One further refinement. If we are at the top level, but our parent
8862 -- is a type conversion, then go into bignum or long long integer node
8863 -- since the result will be converted to that type directly without
8864 -- going through the result type, and we may avoid an overflow. This
8865 -- is the case for example of Long_Long_Integer (A ** 4), where A is
8866 -- of type Integer, and the result A ** 4 fits in Long_Long_Integer
8867 -- but does not fit in Integer.
8868
8869 and then Nkind (Parent (N)) /= N_Type_Conversion
f32c377d 8870 then
0df9d43f 8871 -- Here keep original types, but we need to complete analysis
f32c377d 8872
8873 -- One subtlety. We can't just go ahead and do an analyze operation
21a55437 8874 -- here because it will cause recursion into the whole MINIMIZED/
8875 -- ELIMINATED overflow processing which is not what we want. Here
f32c377d 8876 -- we are at the top level, and we need a check against the result
39a0c1d3 8877 -- mode (i.e. we want to use STRICT mode). So do exactly that.
4fb5f0a0 8878 -- Also, we have not modified the node, so this is a case where
8879 -- we need to reexpand, but not reanalyze.
f32c377d 8880
0df9d43f 8881 Reexpand;
f32c377d 8882 return;
8883
8884 -- Cases where we do the operation in Bignum mode. This happens either
3cce7f32 8885 -- because one of our operands is in Bignum mode already, or because
de922300 8886 -- the computed bounds are outside the bounds of Long_Long_Integer,
8887 -- which in some cases can be indicated by Hi and Lo being No_Uint.
3cce7f32 8888
8889 -- Note: we could do better here and in some cases switch back from
8890 -- Bignum mode to normal mode, e.g. big mod 2 must be in the range
8891 -- 0 .. 1, but the cases are rare and it is not worth the effort.
8892 -- Failing to do this switching back is only an efficiency issue.
8893
f32c377d 8894 elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then
3cce7f32 8895
61016a7a 8896 -- OK, we are definitely outside the range of Long_Long_Integer. The
f32c377d 8897 -- question is whether to move to Bignum mode, or stay in the domain
61016a7a 8898 -- of Long_Long_Integer, signalling that an overflow check is needed.
8899
8900 -- Obviously in MINIMIZED mode we stay with LLI, since we are not in
8901 -- the Bignum business. In ELIMINATED mode, we will normally move
8902 -- into Bignum mode, but there is an exception if neither of our
8903 -- operands is Bignum now, and we are at the top level (Top_Level
8904 -- set True). In this case, there is no point in moving into Bignum
8905 -- mode to prevent overflow if the caller will immediately convert
8906 -- the Bignum value back to LLI with an overflow check. It's more
0df9d43f 8907 -- efficient to stay in LLI mode with an overflow check (if needed)
61016a7a 8908
8909 if Check_Mode = Minimized
8910 or else (Top_Level and not Bignum_Operands)
8911 then
0df9d43f 8912 if Do_Overflow_Check (N) then
8913 Enable_Overflow_Check (N);
8914 end if;
3cce7f32 8915
0df9d43f 8916 -- The result now has to be in Long_Long_Integer mode, so adjust
8917 -- the possible range to reflect this. Note these calls also
8918 -- change No_Uint values from the top level case to LLI bounds.
61016a7a 8919
8920 Max (Lo, LLLo);
8921 Min (Hi, LLHi);
8922
8923 -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode
3cce7f32 8924
8925 else
8926 pragma Assert (Check_Mode = Eliminated);
8927
8928 declare
8929 Fent : Entity_Id;
8930 Args : List_Id;
8931
8932 begin
8933 case Nkind (N) is
99378362 8934 when N_Op_Abs =>
3cce7f32 8935 Fent := RTE (RE_Big_Abs);
8936
99378362 8937 when N_Op_Add =>
3cce7f32 8938 Fent := RTE (RE_Big_Add);
8939
99378362 8940 when N_Op_Divide =>
3cce7f32 8941 Fent := RTE (RE_Big_Div);
8942
99378362 8943 when N_Op_Expon =>
3cce7f32 8944 Fent := RTE (RE_Big_Exp);
8945
99378362 8946 when N_Op_Minus =>
3cce7f32 8947 Fent := RTE (RE_Big_Neg);
8948
99378362 8949 when N_Op_Mod =>
3cce7f32 8950 Fent := RTE (RE_Big_Mod);
8951
8952 when N_Op_Multiply =>
8953 Fent := RTE (RE_Big_Mul);
8954
99378362 8955 when N_Op_Rem =>
3cce7f32 8956 Fent := RTE (RE_Big_Rem);
8957
8958 when N_Op_Subtract =>
8959 Fent := RTE (RE_Big_Sub);
8960
8961 -- Anything else is an internal error, this includes the
8962 -- N_Op_Plus case, since how can plus cause the result
8963 -- to be out of range if the operand is in range?
8964
8965 when others =>
8966 raise Program_Error;
8967 end case;
8968
8969 -- Construct argument list for Bignum call, converting our
8970 -- operands to Bignum form if they are not already there.
8971
8972 Args := New_List;
8973
8974 if Binary then
8975 Append_To (Args, Convert_To_Bignum (Left_Opnd (N)));
8976 end if;
8977
8978 Append_To (Args, Convert_To_Bignum (Right_Opnd (N)));
8979
8980 -- Now rewrite the arithmetic operator with a call to the
8981 -- corresponding bignum function.
8982
8983 Rewrite (N,
8984 Make_Function_Call (Loc,
8985 Name => New_Occurrence_Of (Fent, Loc),
8986 Parameter_Associations => Args));
0df9d43f 8987 Reanalyze (RTE (RE_Bignum), Suppress => True);
61016a7a 8988
8989 -- Indicate result is Bignum mode
8990
8991 Lo := No_Uint;
8992 Hi := No_Uint;
de922300 8993 return;
3cce7f32 8994 end;
8995 end if;
8996
8997 -- Otherwise we are in range of Long_Long_Integer, so no overflow
de922300 8998 -- check is required, at least not yet.
3cce7f32 8999
9000 else
de922300 9001 Set_Do_Overflow_Check (N, False);
9002 end if;
3cce7f32 9003
f32c377d 9004 -- Here we are not in Bignum territory, but we may have long long
9005 -- integer operands that need special handling. First a special check:
9006 -- If an exponentiation operator exponent is of type Long_Long_Integer,
9007 -- it means we converted it to prevent overflow, but exponentiation
9008 -- requires a Natural right operand, so convert it back to Natural.
9009 -- This conversion may raise an exception which is fine.
0326b4d4 9010
f32c377d 9011 if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then
9012 Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N));
0326b4d4 9013 end if;
9014
de922300 9015 -- Here we will do the operation in Long_Long_Integer. We do this even
9016 -- if we know an overflow check is required, better to do this in long
39a0c1d3 9017 -- long integer mode, since we are less likely to overflow.
3cce7f32 9018
de922300 9019 -- Convert right or only operand to Long_Long_Integer, except that
9020 -- we do not touch the exponentiation right operand.
3cce7f32 9021
de922300 9022 if Nkind (N) /= N_Op_Expon then
9023 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
9024 end if;
3cce7f32 9025
de922300 9026 -- Convert left operand to Long_Long_Integer for binary case
49b3a812 9027
de922300 9028 if Binary then
9029 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
9030 end if;
9031
9032 -- Reset node to unanalyzed
9033
9034 Set_Analyzed (N, False);
9035 Set_Etype (N, Empty);
9036 Set_Entity (N, Empty);
9037
2fe22c69 9038 -- Now analyze this new node. This reanalysis will complete processing
9039 -- for the node. In particular we will complete the expansion of an
9040 -- exponentiation operator (e.g. changing A ** 2 to A * A), and also
9041 -- we will complete any division checks (since we have not changed the
9042 -- setting of the Do_Division_Check flag).
3cce7f32 9043
0df9d43f 9044 -- We do this reanalysis in STRICT mode to avoid recursion into the
39a0c1d3 9045 -- MINIMIZED/ELIMINATED handling, since we are now done with that.
3cce7f32 9046
0df9d43f 9047 declare
db415383 9048 SG : constant Overflow_Mode_Type :=
9049 Scope_Suppress.Overflow_Mode_General;
9050 SA : constant Overflow_Mode_Type :=
9051 Scope_Suppress.Overflow_Mode_Assertions;
de922300 9052
0df9d43f 9053 begin
db415383 9054 Scope_Suppress.Overflow_Mode_General := Strict;
9055 Scope_Suppress.Overflow_Mode_Assertions := Strict;
de922300 9056
0df9d43f 9057 if not Do_Overflow_Check (N) then
9058 Reanalyze (LLIB, Suppress => True);
9059 else
9060 Reanalyze (LLIB);
9061 end if;
9062
db415383 9063 Scope_Suppress.Overflow_Mode_General := SG;
9064 Scope_Suppress.Overflow_Mode_Assertions := SA;
0df9d43f 9065 end;
9066 end Minimize_Eliminate_Overflows;
3cce7f32 9067
9068 -------------------------
9069 -- Overflow_Check_Mode --
9070 -------------------------
9071
db415383 9072 function Overflow_Check_Mode return Overflow_Mode_Type is
ee6ba406 9073 begin
724d2bd8 9074 if In_Assertion_Expr = 0 then
db415383 9075 return Scope_Suppress.Overflow_Mode_General;
9dfe12ae 9076 else
db415383 9077 return Scope_Suppress.Overflow_Mode_Assertions;
9dfe12ae 9078 end if;
3cce7f32 9079 end Overflow_Check_Mode;
9080
9081 --------------------------------
9082 -- Overflow_Checks_Suppressed --
9083 --------------------------------
9084
9085 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
9086 begin
0df9d43f 9087 if Present (E) and then Checks_May_Be_Suppressed (E) then
9088 return Is_Check_Suppressed (E, Overflow_Check);
9089 else
9090 return Scope_Suppress.Suppress (Overflow_Check);
9091 end if;
ee6ba406 9092 end Overflow_Checks_Suppressed;
fc75802a 9093
37baba83 9094 ---------------------------------
9095 -- Predicate_Checks_Suppressed --
9096 ---------------------------------
9097
9098 function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is
9099 begin
9100 if Present (E) and then Checks_May_Be_Suppressed (E) then
9101 return Is_Check_Suppressed (E, Predicate_Check);
9102 else
9103 return Scope_Suppress.Suppress (Predicate_Check);
9104 end if;
9105 end Predicate_Checks_Suppressed;
9106
ee6ba406 9107 -----------------------------
9108 -- Range_Checks_Suppressed --
9109 -----------------------------
9110
9111 function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
9112 begin
9dfe12ae 9113 if Present (E) then
ce4d14c4 9114 if Kill_Range_Checks (E) then
9dfe12ae 9115 return True;
5f46de53 9116
9dfe12ae 9117 elsif Checks_May_Be_Suppressed (E) then
9118 return Is_Check_Suppressed (E, Range_Check);
9119 end if;
9120 end if;
ee6ba406 9121
fafc6b97 9122 return Scope_Suppress.Suppress (Range_Check);
ee6ba406 9123 end Range_Checks_Suppressed;
9124
0577b0b1 9125 -----------------------------------------
9126 -- Range_Or_Validity_Checks_Suppressed --
9127 -----------------------------------------
9128
9129 -- Note: the coding would be simpler here if we simply made appropriate
9130 -- calls to Range/Validity_Checks_Suppressed, but that would result in
9131 -- duplicated checks which we prefer to avoid.
9132
9133 function Range_Or_Validity_Checks_Suppressed
9134 (Expr : Node_Id) return Boolean
9135 is
9136 begin
9137 -- Immediate return if scope checks suppressed for either check
9138
fafc6b97 9139 if Scope_Suppress.Suppress (Range_Check)
9140 or
9141 Scope_Suppress.Suppress (Validity_Check)
9142 then
0577b0b1 9143 return True;
9144 end if;
9145
9146 -- If no expression, that's odd, decide that checks are suppressed,
9147 -- since we don't want anyone trying to do checks in this case, which
9148 -- is most likely the result of some other error.
9149
9150 if No (Expr) then
9151 return True;
9152 end if;
9153
9154 -- Expression is present, so perform suppress checks on type
9155
9156 declare
9157 Typ : constant Entity_Id := Etype (Expr);
9158 begin
ce4d14c4 9159 if Checks_May_Be_Suppressed (Typ)
0577b0b1 9160 and then (Is_Check_Suppressed (Typ, Range_Check)
9161 or else
9162 Is_Check_Suppressed (Typ, Validity_Check))
9163 then
9164 return True;
9165 end if;
9166 end;
9167
9168 -- If expression is an entity name, perform checks on this entity
9169
9170 if Is_Entity_Name (Expr) then
9171 declare
9172 Ent : constant Entity_Id := Entity (Expr);
9173 begin
9174 if Checks_May_Be_Suppressed (Ent) then
9175 return Is_Check_Suppressed (Ent, Range_Check)
9176 or else Is_Check_Suppressed (Ent, Validity_Check);
9177 end if;
9178 end;
9179 end if;
9180
9181 -- If we fall through, no checks suppressed
9182
9183 return False;
9184 end Range_Or_Validity_Checks_Suppressed;
9185
226494a3 9186 -------------------
9187 -- Remove_Checks --
9188 -------------------
9189
9190 procedure Remove_Checks (Expr : Node_Id) is
226494a3 9191 function Process (N : Node_Id) return Traverse_Result;
9192 -- Process a single node during the traversal
9193
8f6e4fd5 9194 procedure Traverse is new Traverse_Proc (Process);
9195 -- The traversal procedure itself
226494a3 9196
9197 -------------
9198 -- Process --
9199 -------------
9200
9201 function Process (N : Node_Id) return Traverse_Result is
9202 begin
9203 if Nkind (N) not in N_Subexpr then
9204 return Skip;
9205 end if;
9206
9207 Set_Do_Range_Check (N, False);
9208
9209 case Nkind (N) is
9210 when N_And_Then =>
8f6e4fd5 9211 Traverse (Left_Opnd (N));
226494a3 9212 return Skip;
9213
9214 when N_Attribute_Reference =>
226494a3 9215 Set_Do_Overflow_Check (N, False);
9216
226494a3 9217 when N_Function_Call =>
9218 Set_Do_Tag_Check (N, False);
9219
226494a3 9220 when N_Op =>
9221 Set_Do_Overflow_Check (N, False);
9222
9223 case Nkind (N) is
9224 when N_Op_Divide =>
9225 Set_Do_Division_Check (N, False);
9226
9227 when N_Op_And =>
9228 Set_Do_Length_Check (N, False);
9229
9230 when N_Op_Mod =>
9231 Set_Do_Division_Check (N, False);
9232
9233 when N_Op_Or =>
9234 Set_Do_Length_Check (N, False);
9235
9236 when N_Op_Rem =>
9237 Set_Do_Division_Check (N, False);
9238
9239 when N_Op_Xor =>
9240 Set_Do_Length_Check (N, False);
9241
9242 when others =>
9243 null;
9244 end case;
9245
9246 when N_Or_Else =>
8f6e4fd5 9247 Traverse (Left_Opnd (N));
226494a3 9248 return Skip;
9249
9250 when N_Selected_Component =>
226494a3 9251 Set_Do_Discriminant_Check (N, False);
9252
226494a3 9253 when N_Type_Conversion =>
9dfe12ae 9254 Set_Do_Length_Check (N, False);
9255 Set_Do_Tag_Check (N, False);
226494a3 9256 Set_Do_Overflow_Check (N, False);
226494a3 9257
9258 when others =>
9259 null;
9260 end case;
9261
9262 return OK;
9263 end Process;
9264
9265 -- Start of processing for Remove_Checks
9266
9267 begin
8f6e4fd5 9268 Traverse (Expr);
226494a3 9269 end Remove_Checks;
9270
ee6ba406 9271 ----------------------------
9272 -- Selected_Length_Checks --
9273 ----------------------------
9274
9275 function Selected_Length_Checks
9276 (Ck_Node : Node_Id;
9277 Target_Typ : Entity_Id;
9278 Source_Typ : Entity_Id;
314a23b6 9279 Warn_Node : Node_Id) return Check_Result
ee6ba406 9280 is
9281 Loc : constant Source_Ptr := Sloc (Ck_Node);
9282 S_Typ : Entity_Id;
9283 T_Typ : Entity_Id;
9284 Expr_Actual : Node_Id;
9285 Exptyp : Entity_Id;
9286 Cond : Node_Id := Empty;
9287 Do_Access : Boolean := False;
9288 Wnode : Node_Id := Warn_Node;
9289 Ret_Result : Check_Result := (Empty, Empty);
9290 Num_Checks : Natural := 0;
9291
9292 procedure Add_Check (N : Node_Id);
9293 -- Adds the action given to Ret_Result if N is non-Empty
9294
9295 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
9296 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
314a23b6 9297 -- Comments required ???
ee6ba406 9298
9299 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
9300 -- True for equal literals and for nodes that denote the same constant
5f260d20 9301 -- entity, even if its value is not a static constant. This includes the
9dfe12ae 9302 -- case of a discriminal reference within an init proc. Removes some
5f260d20 9303 -- obviously superfluous checks.
ee6ba406 9304
9305 function Length_E_Cond
9306 (Exptyp : Entity_Id;
9307 Typ : Entity_Id;
314a23b6 9308 Indx : Nat) return Node_Id;
ee6ba406 9309 -- Returns expression to compute:
9310 -- Typ'Length /= Exptyp'Length
9311
9312 function Length_N_Cond
9313 (Expr : Node_Id;
9314 Typ : Entity_Id;
314a23b6 9315 Indx : Nat) return Node_Id;
ee6ba406 9316 -- Returns expression to compute:
9317 -- Typ'Length /= Expr'Length
9318
9319 ---------------
9320 -- Add_Check --
9321 ---------------
9322
9323 procedure Add_Check (N : Node_Id) is
9324 begin
9325 if Present (N) then
9326
20cf157b 9327 -- For now, ignore attempt to place more than two checks ???
9328 -- This is really worrisome, are we really discarding checks ???
ee6ba406 9329
9330 if Num_Checks = 2 then
9331 return;
9332 end if;
9333
9334 pragma Assert (Num_Checks <= 1);
9335 Num_Checks := Num_Checks + 1;
9336 Ret_Result (Num_Checks) := N;
9337 end if;
9338 end Add_Check;
9339
9340 ------------------
9341 -- Get_E_Length --
9342 ------------------
9343
9344 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
00c403ee 9345 SE : constant Entity_Id := Scope (E);
ee6ba406 9346 N : Node_Id;
9347 E1 : Entity_Id := E;
ee6ba406 9348
9349 begin
9350 if Ekind (Scope (E)) = E_Record_Type
9351 and then Has_Discriminants (Scope (E))
9352 then
9353 N := Build_Discriminal_Subtype_Of_Component (E);
9354
9355 if Present (N) then
9356 Insert_Action (Ck_Node, N);
9357 E1 := Defining_Identifier (N);
9358 end if;
9359 end if;
9360
9361 if Ekind (E1) = E_String_Literal_Subtype then
9362 return
9363 Make_Integer_Literal (Loc,
9364 Intval => String_Literal_Length (E1));
9365
00c403ee 9366 elsif SE /= Standard_Standard
9367 and then Ekind (Scope (SE)) = E_Protected_Type
9368 and then Has_Discriminants (Scope (SE))
9369 and then Has_Completion (Scope (SE))
ee6ba406 9370 and then not Inside_Init_Proc
9371 then
ee6ba406 9372 -- If the type whose length is needed is a private component
9373 -- constrained by a discriminant, we must expand the 'Length
9374 -- attribute into an explicit computation, using the discriminal
9375 -- of the current protected operation. This is because the actual
9376 -- type of the prival is constructed after the protected opera-
9377 -- tion has been fully expanded.
9378
9379 declare
9380 Indx_Type : Node_Id;
9381 Lo : Node_Id;
9382 Hi : Node_Id;
9383 Do_Expand : Boolean := False;
9384
9385 begin
9386 Indx_Type := First_Index (E);
9387
9388 for J in 1 .. Indx - 1 loop
9389 Next_Index (Indx_Type);
9390 end loop;
9391
2af58f67 9392 Get_Index_Bounds (Indx_Type, Lo, Hi);
ee6ba406 9393
9394 if Nkind (Lo) = N_Identifier
9395 and then Ekind (Entity (Lo)) = E_In_Parameter
9396 then
9397 Lo := Get_Discriminal (E, Lo);
9398 Do_Expand := True;
9399 end if;
9400
9401 if Nkind (Hi) = N_Identifier
9402 and then Ekind (Entity (Hi)) = E_In_Parameter
9403 then
9404 Hi := Get_Discriminal (E, Hi);
9405 Do_Expand := True;
9406 end if;
9407
9408 if Do_Expand then
9409 if not Is_Entity_Name (Lo) then
9dfe12ae 9410 Lo := Duplicate_Subexpr_No_Checks (Lo);
ee6ba406 9411 end if;
9412
9413 if not Is_Entity_Name (Hi) then
9dfe12ae 9414 Lo := Duplicate_Subexpr_No_Checks (Hi);
ee6ba406 9415 end if;
9416
9417 N :=
9418 Make_Op_Add (Loc,
9419 Left_Opnd =>
9420 Make_Op_Subtract (Loc,
9421 Left_Opnd => Hi,
9422 Right_Opnd => Lo),
9423
9424 Right_Opnd => Make_Integer_Literal (Loc, 1));
9425 return N;
9426
9427 else
9428 N :=
9429 Make_Attribute_Reference (Loc,
9430 Attribute_Name => Name_Length,
9431 Prefix =>
9432 New_Occurrence_Of (E1, Loc));
9433
9434 if Indx > 1 then
9435 Set_Expressions (N, New_List (
9436 Make_Integer_Literal (Loc, Indx)));
9437 end if;
9438
9439 return N;
9440 end if;
9441 end;
9442
9443 else
9444 N :=
9445 Make_Attribute_Reference (Loc,
9446 Attribute_Name => Name_Length,
9447 Prefix =>
9448 New_Occurrence_Of (E1, Loc));
9449
9450 if Indx > 1 then
9451 Set_Expressions (N, New_List (
9452 Make_Integer_Literal (Loc, Indx)));
9453 end if;
9454
9455 return N;
ee6ba406 9456 end if;
9457 end Get_E_Length;
9458
9459 ------------------
9460 -- Get_N_Length --
9461 ------------------
9462
9463 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
9464 begin
9465 return
9466 Make_Attribute_Reference (Loc,
9467 Attribute_Name => Name_Length,
9468 Prefix =>
9dfe12ae 9469 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 9470 Expressions => New_List (
9471 Make_Integer_Literal (Loc, Indx)));
ee6ba406 9472 end Get_N_Length;
9473
9474 -------------------
9475 -- Length_E_Cond --
9476 -------------------
9477
9478 function Length_E_Cond
9479 (Exptyp : Entity_Id;
9480 Typ : Entity_Id;
314a23b6 9481 Indx : Nat) return Node_Id
ee6ba406 9482 is
9483 begin
9484 return
9485 Make_Op_Ne (Loc,
9486 Left_Opnd => Get_E_Length (Typ, Indx),
9487 Right_Opnd => Get_E_Length (Exptyp, Indx));
ee6ba406 9488 end Length_E_Cond;
9489
9490 -------------------
9491 -- Length_N_Cond --
9492 -------------------
9493
9494 function Length_N_Cond
9495 (Expr : Node_Id;
9496 Typ : Entity_Id;
314a23b6 9497 Indx : Nat) return Node_Id
ee6ba406 9498 is
9499 begin
9500 return
9501 Make_Op_Ne (Loc,
9502 Left_Opnd => Get_E_Length (Typ, Indx),
9503 Right_Opnd => Get_N_Length (Expr, Indx));
ee6ba406 9504 end Length_N_Cond;
9505
feff2f05 9506 -----------------
9507 -- Same_Bounds --
9508 -----------------
9509
ee6ba406 9510 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
9511 begin
9512 return
9513 (Nkind (L) = N_Integer_Literal
9514 and then Nkind (R) = N_Integer_Literal
9515 and then Intval (L) = Intval (R))
9516
9517 or else
9518 (Is_Entity_Name (L)
9519 and then Ekind (Entity (L)) = E_Constant
9520 and then ((Is_Entity_Name (R)
9521 and then Entity (L) = Entity (R))
9522 or else
9523 (Nkind (R) = N_Type_Conversion
9524 and then Is_Entity_Name (Expression (R))
9525 and then Entity (L) = Entity (Expression (R)))))
9526
9527 or else
9528 (Is_Entity_Name (R)
9529 and then Ekind (Entity (R)) = E_Constant
9530 and then Nkind (L) = N_Type_Conversion
9531 and then Is_Entity_Name (Expression (L))
5f260d20 9532 and then Entity (R) = Entity (Expression (L)))
9533
9534 or else
9535 (Is_Entity_Name (L)
9536 and then Is_Entity_Name (R)
9537 and then Entity (L) = Entity (R)
9538 and then Ekind (Entity (L)) = E_In_Parameter
9539 and then Inside_Init_Proc);
ee6ba406 9540 end Same_Bounds;
9541
9542 -- Start of processing for Selected_Length_Checks
9543
9544 begin
18cb6d78 9545 -- Checks will be applied only when generating code
f0d65dae 9546
18cb6d78 9547 if not Expander_Active then
ee6ba406 9548 return Ret_Result;
9549 end if;
9550
9551 if Target_Typ = Any_Type
9552 or else Target_Typ = Any_Composite
9553 or else Raises_Constraint_Error (Ck_Node)
9554 then
9555 return Ret_Result;
9556 end if;
9557
9558 if No (Wnode) then
9559 Wnode := Ck_Node;
9560 end if;
9561
9562 T_Typ := Target_Typ;
9563
9564 if No (Source_Typ) then
9565 S_Typ := Etype (Ck_Node);
9566 else
9567 S_Typ := Source_Typ;
9568 end if;
9569
9570 if S_Typ = Any_Type or else S_Typ = Any_Composite then
9571 return Ret_Result;
9572 end if;
9573
9574 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
9575 S_Typ := Designated_Type (S_Typ);
9576 T_Typ := Designated_Type (T_Typ);
9577 Do_Access := True;
9578
2af58f67 9579 -- A simple optimization for the null case
ee6ba406 9580
2af58f67 9581 if Known_Null (Ck_Node) then
ee6ba406 9582 return Ret_Result;
9583 end if;
9584 end if;
9585
9586 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
9587 if Is_Constrained (T_Typ) then
9588
92f1631f 9589 -- The checking code to be generated will freeze the corresponding
9590 -- array type. However, we must freeze the type now, so that the
9591 -- freeze node does not appear within the generated if expression,
9592 -- but ahead of it.
ee6ba406 9593
9594 Freeze_Before (Ck_Node, T_Typ);
9595
9596 Expr_Actual := Get_Referenced_Object (Ck_Node);
84d0d4a5 9597 Exptyp := Get_Actual_Subtype (Ck_Node);
ee6ba406 9598
9599 if Is_Access_Type (Exptyp) then
9600 Exptyp := Designated_Type (Exptyp);
9601 end if;
9602
9603 -- String_Literal case. This needs to be handled specially be-
9604 -- cause no index types are available for string literals. The
9605 -- condition is simply:
9606
9607 -- T_Typ'Length = string-literal-length
9608
9dfe12ae 9609 if Nkind (Expr_Actual) = N_String_Literal
9610 and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype
9611 then
ee6ba406 9612 Cond :=
9613 Make_Op_Ne (Loc,
9614 Left_Opnd => Get_E_Length (T_Typ, 1),
9615 Right_Opnd =>
9616 Make_Integer_Literal (Loc,
9617 Intval =>
9618 String_Literal_Length (Etype (Expr_Actual))));
9619
9620 -- General array case. Here we have a usable actual subtype for
9621 -- the expression, and the condition is built from the two types
9622 -- (Do_Length):
9623
9624 -- T_Typ'Length /= Exptyp'Length or else
9625 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
9626 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
9627 -- ...
9628
9629 elsif Is_Constrained (Exptyp) then
9630 declare
9dfe12ae 9631 Ndims : constant Nat := Number_Dimensions (T_Typ);
9632
9633 L_Index : Node_Id;
9634 R_Index : Node_Id;
9635 L_Low : Node_Id;
9636 L_High : Node_Id;
9637 R_Low : Node_Id;
9638 R_High : Node_Id;
ee6ba406 9639 L_Length : Uint;
9640 R_Length : Uint;
9dfe12ae 9641 Ref_Node : Node_Id;
ee6ba406 9642
9643 begin
feff2f05 9644 -- At the library level, we need to ensure that the type of
9645 -- the object is elaborated before the check itself is
9646 -- emitted. This is only done if the object is in the
9647 -- current compilation unit, otherwise the type is frozen
9648 -- and elaborated in its unit.
9dfe12ae 9649
9650 if Is_Itype (Exptyp)
9651 and then
9652 Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package
9653 and then
9654 not In_Package_Body (Cunit_Entity (Current_Sem_Unit))
d66aa9f6 9655 and then In_Open_Scopes (Scope (Exptyp))
9dfe12ae 9656 then
9657 Ref_Node := Make_Itype_Reference (Sloc (Ck_Node));
9658 Set_Itype (Ref_Node, Exptyp);
9659 Insert_Action (Ck_Node, Ref_Node);
9660 end if;
9661
ee6ba406 9662 L_Index := First_Index (T_Typ);
9663 R_Index := First_Index (Exptyp);
9664
9665 for Indx in 1 .. Ndims loop
9666 if not (Nkind (L_Index) = N_Raise_Constraint_Error
f15731c4 9667 or else
9668 Nkind (R_Index) = N_Raise_Constraint_Error)
ee6ba406 9669 then
9670 Get_Index_Bounds (L_Index, L_Low, L_High);
9671 Get_Index_Bounds (R_Index, R_Low, R_High);
9672
9673 -- Deal with compile time length check. Note that we
9674 -- skip this in the access case, because the access
9675 -- value may be null, so we cannot know statically.
9676
9677 if not Do_Access
9678 and then Compile_Time_Known_Value (L_Low)
9679 and then Compile_Time_Known_Value (L_High)
9680 and then Compile_Time_Known_Value (R_Low)
9681 and then Compile_Time_Known_Value (R_High)
9682 then
9683 if Expr_Value (L_High) >= Expr_Value (L_Low) then
9684 L_Length := Expr_Value (L_High) -
9685 Expr_Value (L_Low) + 1;
9686 else
9687 L_Length := UI_From_Int (0);
9688 end if;
9689
9690 if Expr_Value (R_High) >= Expr_Value (R_Low) then
9691 R_Length := Expr_Value (R_High) -
9692 Expr_Value (R_Low) + 1;
9693 else
9694 R_Length := UI_From_Int (0);
9695 end if;
9696
9697 if L_Length > R_Length then
9698 Add_Check
9699 (Compile_Time_Constraint_Error
cb97ae5c 9700 (Wnode, "too few elements for}??", T_Typ));
ee6ba406 9701
b8eacb12 9702 elsif L_Length < R_Length then
ee6ba406 9703 Add_Check
9704 (Compile_Time_Constraint_Error
cb97ae5c 9705 (Wnode, "too many elements for}??", T_Typ));
ee6ba406 9706 end if;
9707
9708 -- The comparison for an individual index subtype
9709 -- is omitted if the corresponding index subtypes
9710 -- statically match, since the result is known to
9711 -- be true. Note that this test is worth while even
9712 -- though we do static evaluation, because non-static
9713 -- subtypes can statically match.
9714
9715 elsif not
9716 Subtypes_Statically_Match
9717 (Etype (L_Index), Etype (R_Index))
9718
9719 and then not
9720 (Same_Bounds (L_Low, R_Low)
9721 and then Same_Bounds (L_High, R_High))
9722 then
9723 Evolve_Or_Else
9724 (Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
9725 end if;
9726
9727 Next (L_Index);
9728 Next (R_Index);
9729 end if;
9730 end loop;
9731 end;
9732
9733 -- Handle cases where we do not get a usable actual subtype that
9734 -- is constrained. This happens for example in the function call
9735 -- and explicit dereference cases. In these cases, we have to get
9736 -- the length or range from the expression itself, making sure we
9737 -- do not evaluate it more than once.
9738
9739 -- Here Ck_Node is the original expression, or more properly the
feff2f05 9740 -- result of applying Duplicate_Expr to the original tree, forcing
9741 -- the result to be a name.
ee6ba406 9742
9743 else
9744 declare
9dfe12ae 9745 Ndims : constant Nat := Number_Dimensions (T_Typ);
ee6ba406 9746
9747 begin
9748 -- Build the condition for the explicit dereference case
9749
9750 for Indx in 1 .. Ndims loop
9751 Evolve_Or_Else
9752 (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
9753 end loop;
9754 end;
9755 end if;
9756 end if;
9757 end if;
9758
9759 -- Construct the test and insert into the tree
9760
9761 if Present (Cond) then
9762 if Do_Access then
9763 Cond := Guard_Access (Cond, Loc, Ck_Node);
9764 end if;
9765
f15731c4 9766 Add_Check
9767 (Make_Raise_Constraint_Error (Loc,
9768 Condition => Cond,
9769 Reason => CE_Length_Check_Failed));
ee6ba406 9770 end if;
9771
9772 return Ret_Result;
ee6ba406 9773 end Selected_Length_Checks;
9774
9775 ---------------------------
9776 -- Selected_Range_Checks --
9777 ---------------------------
9778
9779 function Selected_Range_Checks
9780 (Ck_Node : Node_Id;
9781 Target_Typ : Entity_Id;
9782 Source_Typ : Entity_Id;
314a23b6 9783 Warn_Node : Node_Id) return Check_Result
ee6ba406 9784 is
9785 Loc : constant Source_Ptr := Sloc (Ck_Node);
9786 S_Typ : Entity_Id;
9787 T_Typ : Entity_Id;
9788 Expr_Actual : Node_Id;
9789 Exptyp : Entity_Id;
9790 Cond : Node_Id := Empty;
9791 Do_Access : Boolean := False;
9792 Wnode : Node_Id := Warn_Node;
9793 Ret_Result : Check_Result := (Empty, Empty);
9794 Num_Checks : Integer := 0;
9795
9796 procedure Add_Check (N : Node_Id);
9797 -- Adds the action given to Ret_Result if N is non-Empty
9798
9799 function Discrete_Range_Cond
9800 (Expr : Node_Id;
314a23b6 9801 Typ : Entity_Id) return Node_Id;
ee6ba406 9802 -- Returns expression to compute:
9803 -- Low_Bound (Expr) < Typ'First
9804 -- or else
9805 -- High_Bound (Expr) > Typ'Last
9806
9807 function Discrete_Expr_Cond
9808 (Expr : Node_Id;
314a23b6 9809 Typ : Entity_Id) return Node_Id;
ee6ba406 9810 -- Returns expression to compute:
9811 -- Expr < Typ'First
9812 -- or else
9813 -- Expr > Typ'Last
9814
9815 function Get_E_First_Or_Last
3cb12758 9816 (Loc : Source_Ptr;
9817 E : Entity_Id;
ee6ba406 9818 Indx : Nat;
314a23b6 9819 Nam : Name_Id) return Node_Id;
79212397 9820 -- Returns an attribute reference
ee6ba406 9821 -- E'First or E'Last
79212397 9822 -- with a source location of Loc.
f73ee678 9823 --
79212397 9824 -- Nam is Name_First or Name_Last, according to which attribute is
9825 -- desired. If Indx is non-zero, it is passed as a literal in the
9826 -- Expressions of the attribute reference (identifying the desired
9827 -- array dimension).
ee6ba406 9828
9829 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
9830 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
9831 -- Returns expression to compute:
9dfe12ae 9832 -- N'First or N'Last using Duplicate_Subexpr_No_Checks
ee6ba406 9833
9834 function Range_E_Cond
9835 (Exptyp : Entity_Id;
9836 Typ : Entity_Id;
9837 Indx : Nat)
9838 return Node_Id;
9839 -- Returns expression to compute:
9840 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
9841
9842 function Range_Equal_E_Cond
9843 (Exptyp : Entity_Id;
9844 Typ : Entity_Id;
314a23b6 9845 Indx : Nat) return Node_Id;
ee6ba406 9846 -- Returns expression to compute:
9847 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
9848
9849 function Range_N_Cond
9850 (Expr : Node_Id;
9851 Typ : Entity_Id;
314a23b6 9852 Indx : Nat) return Node_Id;
ee6ba406 9853 -- Return expression to compute:
9854 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
9855
9856 ---------------
9857 -- Add_Check --
9858 ---------------
9859
9860 procedure Add_Check (N : Node_Id) is
9861 begin
9862 if Present (N) then
9863
9864 -- For now, ignore attempt to place more than 2 checks ???
9865
9866 if Num_Checks = 2 then
9867 return;
9868 end if;
9869
9870 pragma Assert (Num_Checks <= 1);
9871 Num_Checks := Num_Checks + 1;
9872 Ret_Result (Num_Checks) := N;
9873 end if;
9874 end Add_Check;
9875
9876 -------------------------
9877 -- Discrete_Expr_Cond --
9878 -------------------------
9879
9880 function Discrete_Expr_Cond
9881 (Expr : Node_Id;
314a23b6 9882 Typ : Entity_Id) return Node_Id
ee6ba406 9883 is
9884 begin
9885 return
9886 Make_Or_Else (Loc,
9887 Left_Opnd =>
9888 Make_Op_Lt (Loc,
9889 Left_Opnd =>
9dfe12ae 9890 Convert_To (Base_Type (Typ),
9891 Duplicate_Subexpr_No_Checks (Expr)),
ee6ba406 9892 Right_Opnd =>
9893 Convert_To (Base_Type (Typ),
3cb12758 9894 Get_E_First_Or_Last (Loc, Typ, 0, Name_First))),
ee6ba406 9895
9896 Right_Opnd =>
9897 Make_Op_Gt (Loc,
9898 Left_Opnd =>
9dfe12ae 9899 Convert_To (Base_Type (Typ),
9900 Duplicate_Subexpr_No_Checks (Expr)),
ee6ba406 9901 Right_Opnd =>
9902 Convert_To
9903 (Base_Type (Typ),
3cb12758 9904 Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))));
ee6ba406 9905 end Discrete_Expr_Cond;
9906
9907 -------------------------
9908 -- Discrete_Range_Cond --
9909 -------------------------
9910
9911 function Discrete_Range_Cond
9912 (Expr : Node_Id;
314a23b6 9913 Typ : Entity_Id) return Node_Id
ee6ba406 9914 is
9915 LB : Node_Id := Low_Bound (Expr);
9916 HB : Node_Id := High_Bound (Expr);
9917
9918 Left_Opnd : Node_Id;
9919 Right_Opnd : Node_Id;
9920
9921 begin
9922 if Nkind (LB) = N_Identifier
feff2f05 9923 and then Ekind (Entity (LB)) = E_Discriminant
9924 then
ee6ba406 9925 LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
9926 end if;
9927
ee6ba406 9928 Left_Opnd :=
9929 Make_Op_Lt (Loc,
9930 Left_Opnd =>
9931 Convert_To
9dfe12ae 9932 (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)),
ee6ba406 9933
9934 Right_Opnd =>
9935 Convert_To
3cb12758 9936 (Base_Type (Typ),
9937 Get_E_First_Or_Last (Loc, Typ, 0, Name_First)));
ee6ba406 9938
ba9b1a39 9939 if Nkind (HB) = N_Identifier
9940 and then Ekind (Entity (HB)) = E_Discriminant
ee6ba406 9941 then
ba9b1a39 9942 HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
ee6ba406 9943 end if;
9944
9945 Right_Opnd :=
9946 Make_Op_Gt (Loc,
9947 Left_Opnd =>
9948 Convert_To
9dfe12ae 9949 (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)),
ee6ba406 9950
9951 Right_Opnd =>
9952 Convert_To
9953 (Base_Type (Typ),
3cb12758 9954 Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)));
ee6ba406 9955
9956 return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
9957 end Discrete_Range_Cond;
9958
9959 -------------------------
9960 -- Get_E_First_Or_Last --
9961 -------------------------
9962
9963 function Get_E_First_Or_Last
3cb12758 9964 (Loc : Source_Ptr;
9965 E : Entity_Id;
ee6ba406 9966 Indx : Nat;
314a23b6 9967 Nam : Name_Id) return Node_Id
ee6ba406 9968 is
3cb12758 9969 Exprs : List_Id;
ee6ba406 9970 begin
3cb12758 9971 if Indx > 0 then
9972 Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx)));
ee6ba406 9973 else
3cb12758 9974 Exprs := No_List;
ee6ba406 9975 end if;
9976
3cb12758 9977 return Make_Attribute_Reference (Loc,
9978 Prefix => New_Occurrence_Of (E, Loc),
9979 Attribute_Name => Nam,
9980 Expressions => Exprs);
ee6ba406 9981 end Get_E_First_Or_Last;
9982
9983 -----------------
9984 -- Get_N_First --
9985 -----------------
9986
9987 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
9988 begin
9989 return
9990 Make_Attribute_Reference (Loc,
9991 Attribute_Name => Name_First,
9992 Prefix =>
9dfe12ae 9993 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 9994 Expressions => New_List (
9995 Make_Integer_Literal (Loc, Indx)));
ee6ba406 9996 end Get_N_First;
9997
9998 ----------------
9999 -- Get_N_Last --
10000 ----------------
10001
10002 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
10003 begin
10004 return
10005 Make_Attribute_Reference (Loc,
10006 Attribute_Name => Name_Last,
10007 Prefix =>
9dfe12ae 10008 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 10009 Expressions => New_List (
10010 Make_Integer_Literal (Loc, Indx)));
ee6ba406 10011 end Get_N_Last;
10012
10013 ------------------
10014 -- Range_E_Cond --
10015 ------------------
10016
10017 function Range_E_Cond
10018 (Exptyp : Entity_Id;
10019 Typ : Entity_Id;
314a23b6 10020 Indx : Nat) return Node_Id
ee6ba406 10021 is
10022 begin
10023 return
10024 Make_Or_Else (Loc,
10025 Left_Opnd =>
10026 Make_Op_Lt (Loc,
3cb12758 10027 Left_Opnd =>
10028 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First),
10029 Right_Opnd =>
10030 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
ee6ba406 10031
10032 Right_Opnd =>
10033 Make_Op_Gt (Loc,
3cb12758 10034 Left_Opnd =>
10035 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last),
10036 Right_Opnd =>
10037 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 10038 end Range_E_Cond;
10039
10040 ------------------------
10041 -- Range_Equal_E_Cond --
10042 ------------------------
10043
10044 function Range_Equal_E_Cond
10045 (Exptyp : Entity_Id;
10046 Typ : Entity_Id;
314a23b6 10047 Indx : Nat) return Node_Id
ee6ba406 10048 is
10049 begin
10050 return
10051 Make_Or_Else (Loc,
10052 Left_Opnd =>
10053 Make_Op_Ne (Loc,
3cb12758 10054 Left_Opnd =>
10055 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First),
10056 Right_Opnd =>
10057 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
10058
ee6ba406 10059 Right_Opnd =>
10060 Make_Op_Ne (Loc,
3cb12758 10061 Left_Opnd =>
10062 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last),
10063 Right_Opnd =>
10064 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 10065 end Range_Equal_E_Cond;
10066
10067 ------------------
10068 -- Range_N_Cond --
10069 ------------------
10070
10071 function Range_N_Cond
10072 (Expr : Node_Id;
10073 Typ : Entity_Id;
314a23b6 10074 Indx : Nat) return Node_Id
ee6ba406 10075 is
10076 begin
10077 return
10078 Make_Or_Else (Loc,
10079 Left_Opnd =>
10080 Make_Op_Lt (Loc,
3cb12758 10081 Left_Opnd =>
10082 Get_N_First (Expr, Indx),
10083 Right_Opnd =>
10084 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
ee6ba406 10085
10086 Right_Opnd =>
10087 Make_Op_Gt (Loc,
3cb12758 10088 Left_Opnd =>
10089 Get_N_Last (Expr, Indx),
10090 Right_Opnd =>
10091 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 10092 end Range_N_Cond;
10093
10094 -- Start of processing for Selected_Range_Checks
10095
10096 begin
f0d65dae 10097 -- Checks will be applied only when generating code. In GNATprove mode,
10098 -- we do not apply the checks, but we still call Selected_Range_Checks
10099 -- to possibly issue errors on SPARK code when a run-time error can be
10100 -- detected at compile time.
10101
10102 if not Expander_Active and not GNATprove_Mode then
ee6ba406 10103 return Ret_Result;
10104 end if;
10105
10106 if Target_Typ = Any_Type
10107 or else Target_Typ = Any_Composite
10108 or else Raises_Constraint_Error (Ck_Node)
10109 then
10110 return Ret_Result;
10111 end if;
10112
10113 if No (Wnode) then
10114 Wnode := Ck_Node;
10115 end if;
10116
10117 T_Typ := Target_Typ;
10118
10119 if No (Source_Typ) then
10120 S_Typ := Etype (Ck_Node);
10121 else
10122 S_Typ := Source_Typ;
10123 end if;
10124
10125 if S_Typ = Any_Type or else S_Typ = Any_Composite then
10126 return Ret_Result;
10127 end if;
10128
10129 -- The order of evaluating T_Typ before S_Typ seems to be critical
10130 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
10131 -- in, and since Node can be an N_Range node, it might be invalid.
10132 -- Should there be an assert check somewhere for taking the Etype of
10133 -- an N_Range node ???
10134
10135 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
10136 S_Typ := Designated_Type (S_Typ);
10137 T_Typ := Designated_Type (T_Typ);
10138 Do_Access := True;
10139
2af58f67 10140 -- A simple optimization for the null case
ee6ba406 10141
2af58f67 10142 if Known_Null (Ck_Node) then
ee6ba406 10143 return Ret_Result;
10144 end if;
10145 end if;
10146
10147 -- For an N_Range Node, check for a null range and then if not
10148 -- null generate a range check action.
10149
10150 if Nkind (Ck_Node) = N_Range then
10151
10152 -- There's no point in checking a range against itself
10153
10154 if Ck_Node = Scalar_Range (T_Typ) then
10155 return Ret_Result;
10156 end if;
10157
10158 declare
10159 T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
10160 T_HB : constant Node_Id := Type_High_Bound (T_Typ);
eefa141b 10161 Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB);
10162 Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB);
ee6ba406 10163
eefa141b 10164 LB : Node_Id := Low_Bound (Ck_Node);
10165 HB : Node_Id := High_Bound (Ck_Node);
3fabf0ca 10166 Known_LB : Boolean := False;
10167 Known_HB : Boolean := False;
eefa141b 10168
10169 Null_Range : Boolean;
ee6ba406 10170 Out_Of_Range_L : Boolean;
10171 Out_Of_Range_H : Boolean;
10172
10173 begin
eefa141b 10174 -- Compute what is known at compile time
10175
10176 if Known_T_LB and Known_T_HB then
10177 if Compile_Time_Known_Value (LB) then
10178 Known_LB := True;
10179
10180 -- There's no point in checking that a bound is within its
10181 -- own range so pretend that it is known in this case. First
10182 -- deal with low bound.
10183
10184 elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype
10185 and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ)
10186 then
10187 LB := T_LB;
10188 Known_LB := True;
eefa141b 10189 end if;
10190
10191 -- Likewise for the high bound
10192
10193 if Compile_Time_Known_Value (HB) then
10194 Known_HB := True;
10195
10196 elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype
10197 and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ)
10198 then
10199 HB := T_HB;
10200 Known_HB := True;
eefa141b 10201 end if;
10202 end if;
10203
10204 -- Check for case where everything is static and we can do the
10205 -- check at compile time. This is skipped if we have an access
10206 -- type, since the access value may be null.
10207
10208 -- ??? This code can be improved since you only need to know that
10209 -- the two respective bounds (LB & T_LB or HB & T_HB) are known at
10210 -- compile time to emit pertinent messages.
10211
10212 if Known_T_LB and Known_T_HB and Known_LB and Known_HB
10213 and not Do_Access
ee6ba406 10214 then
10215 -- Floating-point case
10216
10217 if Is_Floating_Point_Type (S_Typ) then
10218 Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
10219 Out_Of_Range_L :=
10220 (Expr_Value_R (LB) < Expr_Value_R (T_LB))
eefa141b 10221 or else
ee6ba406 10222 (Expr_Value_R (LB) > Expr_Value_R (T_HB));
10223
10224 Out_Of_Range_H :=
10225 (Expr_Value_R (HB) > Expr_Value_R (T_HB))
eefa141b 10226 or else
ee6ba406 10227 (Expr_Value_R (HB) < Expr_Value_R (T_LB));
10228
10229 -- Fixed or discrete type case
10230
10231 else
10232 Null_Range := Expr_Value (HB) < Expr_Value (LB);
10233 Out_Of_Range_L :=
10234 (Expr_Value (LB) < Expr_Value (T_LB))
eefa141b 10235 or else
ee6ba406 10236 (Expr_Value (LB) > Expr_Value (T_HB));
10237
10238 Out_Of_Range_H :=
10239 (Expr_Value (HB) > Expr_Value (T_HB))
eefa141b 10240 or else
ee6ba406 10241 (Expr_Value (HB) < Expr_Value (T_LB));
10242 end if;
10243
10244 if not Null_Range then
10245 if Out_Of_Range_L then
10246 if No (Warn_Node) then
10247 Add_Check
10248 (Compile_Time_Constraint_Error
10249 (Low_Bound (Ck_Node),
cb97ae5c 10250 "static value out of range of}??", T_Typ));
ee6ba406 10251
10252 else
10253 Add_Check
10254 (Compile_Time_Constraint_Error
10255 (Wnode,
cb97ae5c 10256 "static range out of bounds of}??", T_Typ));
ee6ba406 10257 end if;
10258 end if;
10259
10260 if Out_Of_Range_H then
10261 if No (Warn_Node) then
10262 Add_Check
10263 (Compile_Time_Constraint_Error
10264 (High_Bound (Ck_Node),
cb97ae5c 10265 "static value out of range of}??", T_Typ));
ee6ba406 10266
10267 else
10268 Add_Check
10269 (Compile_Time_Constraint_Error
10270 (Wnode,
cb97ae5c 10271 "static range out of bounds of}??", T_Typ));
ee6ba406 10272 end if;
10273 end if;
ee6ba406 10274 end if;
10275
10276 else
10277 declare
10278 LB : Node_Id := Low_Bound (Ck_Node);
10279 HB : Node_Id := High_Bound (Ck_Node);
10280
10281 begin
feff2f05 10282 -- If either bound is a discriminant and we are within the
10283 -- record declaration, it is a use of the discriminant in a
10284 -- constraint of a component, and nothing can be checked
10285 -- here. The check will be emitted within the init proc.
10286 -- Before then, the discriminal has no real meaning.
10287 -- Similarly, if the entity is a discriminal, there is no
10288 -- check to perform yet.
10289
10290 -- The same holds within a discriminated synchronized type,
10291 -- where the discriminant may constrain a component or an
10292 -- entry family.
ee6ba406 10293
10294 if Nkind (LB) = N_Identifier
0577b0b1 10295 and then Denotes_Discriminant (LB, True)
ee6ba406 10296 then
0577b0b1 10297 if Current_Scope = Scope (Entity (LB))
10298 or else Is_Concurrent_Type (Current_Scope)
10299 or else Ekind (Entity (LB)) /= E_Discriminant
10300 then
ee6ba406 10301 return Ret_Result;
10302 else
10303 LB :=
10304 New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
10305 end if;
10306 end if;
10307
10308 if Nkind (HB) = N_Identifier
0577b0b1 10309 and then Denotes_Discriminant (HB, True)
ee6ba406 10310 then
0577b0b1 10311 if Current_Scope = Scope (Entity (HB))
10312 or else Is_Concurrent_Type (Current_Scope)
10313 or else Ekind (Entity (HB)) /= E_Discriminant
10314 then
ee6ba406 10315 return Ret_Result;
10316 else
10317 HB :=
10318 New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
10319 end if;
10320 end if;
10321
10322 Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
10323 Set_Paren_Count (Cond, 1);
10324
10325 Cond :=
10326 Make_And_Then (Loc,
10327 Left_Opnd =>
10328 Make_Op_Ge (Loc,
86d32751 10329 Left_Opnd =>
10330 Convert_To (Base_Type (Etype (HB)),
10331 Duplicate_Subexpr_No_Checks (HB)),
10332 Right_Opnd =>
10333 Convert_To (Base_Type (Etype (LB)),
10334 Duplicate_Subexpr_No_Checks (LB))),
ee6ba406 10335 Right_Opnd => Cond);
10336 end;
ee6ba406 10337 end if;
10338 end;
10339
10340 elsif Is_Scalar_Type (S_Typ) then
10341
10342 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
10343 -- except the above simply sets a flag in the node and lets
10344 -- gigi generate the check base on the Etype of the expression.
10345 -- Sometimes, however we want to do a dynamic check against an
10346 -- arbitrary target type, so we do that here.
10347
10348 if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
10349 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
10350
10351 -- For literals, we can tell if the constraint error will be
10352 -- raised at compile time, so we never need a dynamic check, but
10353 -- if the exception will be raised, then post the usual warning,
10354 -- and replace the literal with a raise constraint error
10355 -- expression. As usual, skip this for access types
10356
20cf157b 10357 elsif Compile_Time_Known_Value (Ck_Node) and then not Do_Access then
ee6ba406 10358 declare
10359 LB : constant Node_Id := Type_Low_Bound (T_Typ);
10360 UB : constant Node_Id := Type_High_Bound (T_Typ);
10361
10362 Out_Of_Range : Boolean;
10363 Static_Bounds : constant Boolean :=
b6341c67 10364 Compile_Time_Known_Value (LB)
10365 and Compile_Time_Known_Value (UB);
ee6ba406 10366
10367 begin
10368 -- Following range tests should use Sem_Eval routine ???
10369
10370 if Static_Bounds then
10371 if Is_Floating_Point_Type (S_Typ) then
10372 Out_Of_Range :=
10373 (Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
10374 or else
10375 (Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
10376
eefa141b 10377 -- Fixed or discrete type
10378
10379 else
ee6ba406 10380 Out_Of_Range :=
10381 Expr_Value (Ck_Node) < Expr_Value (LB)
10382 or else
10383 Expr_Value (Ck_Node) > Expr_Value (UB);
10384 end if;
10385
eefa141b 10386 -- Bounds of the type are static and the literal is out of
10387 -- range so output a warning message.
ee6ba406 10388
10389 if Out_Of_Range then
10390 if No (Warn_Node) then
10391 Add_Check
10392 (Compile_Time_Constraint_Error
10393 (Ck_Node,
cb97ae5c 10394 "static value out of range of}??", T_Typ));
ee6ba406 10395
10396 else
10397 Add_Check
10398 (Compile_Time_Constraint_Error
10399 (Wnode,
cb97ae5c 10400 "static value out of range of}??", T_Typ));
ee6ba406 10401 end if;
10402 end if;
10403
10404 else
10405 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
10406 end if;
10407 end;
10408
10409 -- Here for the case of a non-static expression, we need a runtime
10410 -- check unless the source type range is guaranteed to be in the
10411 -- range of the target type.
10412
10413 else
7a1dabb3 10414 if not In_Subrange_Of (S_Typ, T_Typ) then
ee6ba406 10415 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
10416 end if;
10417 end if;
10418 end if;
10419
10420 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
10421 if Is_Constrained (T_Typ) then
10422
10423 Expr_Actual := Get_Referenced_Object (Ck_Node);
10424 Exptyp := Get_Actual_Subtype (Expr_Actual);
10425
10426 if Is_Access_Type (Exptyp) then
10427 Exptyp := Designated_Type (Exptyp);
10428 end if;
10429
10430 -- String_Literal case. This needs to be handled specially be-
10431 -- cause no index types are available for string literals. The
10432 -- condition is simply:
10433
10434 -- T_Typ'Length = string-literal-length
10435
10436 if Nkind (Expr_Actual) = N_String_Literal then
10437 null;
10438
10439 -- General array case. Here we have a usable actual subtype for
10440 -- the expression, and the condition is built from the two types
10441
10442 -- T_Typ'First < Exptyp'First or else
10443 -- T_Typ'Last > Exptyp'Last or else
10444 -- T_Typ'First(1) < Exptyp'First(1) or else
10445 -- T_Typ'Last(1) > Exptyp'Last(1) or else
10446 -- ...
10447
10448 elsif Is_Constrained (Exptyp) then
10449 declare
9dfe12ae 10450 Ndims : constant Nat := Number_Dimensions (T_Typ);
10451
ee6ba406 10452 L_Index : Node_Id;
10453 R_Index : Node_Id;
ee6ba406 10454
10455 begin
10456 L_Index := First_Index (T_Typ);
10457 R_Index := First_Index (Exptyp);
10458
10459 for Indx in 1 .. Ndims loop
10460 if not (Nkind (L_Index) = N_Raise_Constraint_Error
f15731c4 10461 or else
10462 Nkind (R_Index) = N_Raise_Constraint_Error)
ee6ba406 10463 then
ee6ba406 10464 -- Deal with compile time length check. Note that we
10465 -- skip this in the access case, because the access
10466 -- value may be null, so we cannot know statically.
10467
10468 if not
10469 Subtypes_Statically_Match
10470 (Etype (L_Index), Etype (R_Index))
10471 then
10472 -- If the target type is constrained then we
10473 -- have to check for exact equality of bounds
10474 -- (required for qualified expressions).
10475
10476 if Is_Constrained (T_Typ) then
10477 Evolve_Or_Else
10478 (Cond,
10479 Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
ee6ba406 10480 else
10481 Evolve_Or_Else
10482 (Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
10483 end if;
10484 end if;
10485
10486 Next (L_Index);
10487 Next (R_Index);
ee6ba406 10488 end if;
10489 end loop;
10490 end;
10491
10492 -- Handle cases where we do not get a usable actual subtype that
10493 -- is constrained. This happens for example in the function call
10494 -- and explicit dereference cases. In these cases, we have to get
10495 -- the length or range from the expression itself, making sure we
10496 -- do not evaluate it more than once.
10497
10498 -- Here Ck_Node is the original expression, or more properly the
10499 -- result of applying Duplicate_Expr to the original tree,
10500 -- forcing the result to be a name.
10501
10502 else
10503 declare
9dfe12ae 10504 Ndims : constant Nat := Number_Dimensions (T_Typ);
ee6ba406 10505
10506 begin
10507 -- Build the condition for the explicit dereference case
10508
10509 for Indx in 1 .. Ndims loop
10510 Evolve_Or_Else
10511 (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
10512 end loop;
10513 end;
ee6ba406 10514 end if;
10515
10516 else
feff2f05 10517 -- For a conversion to an unconstrained array type, generate an
10518 -- Action to check that the bounds of the source value are within
10519 -- the constraints imposed by the target type (RM 4.6(38)). No
10520 -- check is needed for a conversion to an access to unconstrained
10521 -- array type, as 4.6(24.15/2) requires the designated subtypes
10522 -- of the two access types to statically match.
10523
10524 if Nkind (Parent (Ck_Node)) = N_Type_Conversion
10525 and then not Do_Access
10526 then
ee6ba406 10527 declare
10528 Opnd_Index : Node_Id;
10529 Targ_Index : Node_Id;
00c403ee 10530 Opnd_Range : Node_Id;
ee6ba406 10531
10532 begin
feff2f05 10533 Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node));
ee6ba406 10534 Targ_Index := First_Index (T_Typ);
00c403ee 10535 while Present (Opnd_Index) loop
10536
10537 -- If the index is a range, use its bounds. If it is an
10538 -- entity (as will be the case if it is a named subtype
10539 -- or an itype created for a slice) retrieve its range.
10540
10541 if Is_Entity_Name (Opnd_Index)
10542 and then Is_Type (Entity (Opnd_Index))
10543 then
10544 Opnd_Range := Scalar_Range (Entity (Opnd_Index));
10545 else
10546 Opnd_Range := Opnd_Index;
10547 end if;
10548
10549 if Nkind (Opnd_Range) = N_Range then
9c486805 10550 if Is_In_Range
10551 (Low_Bound (Opnd_Range), Etype (Targ_Index),
10552 Assume_Valid => True)
ee6ba406 10553 and then
10554 Is_In_Range
9c486805 10555 (High_Bound (Opnd_Range), Etype (Targ_Index),
10556 Assume_Valid => True)
ee6ba406 10557 then
10558 null;
10559
feff2f05 10560 -- If null range, no check needed
f2a06be9 10561
9dfe12ae 10562 elsif
00c403ee 10563 Compile_Time_Known_Value (High_Bound (Opnd_Range))
9dfe12ae 10564 and then
00c403ee 10565 Compile_Time_Known_Value (Low_Bound (Opnd_Range))
9dfe12ae 10566 and then
00c403ee 10567 Expr_Value (High_Bound (Opnd_Range)) <
10568 Expr_Value (Low_Bound (Opnd_Range))
9dfe12ae 10569 then
10570 null;
10571
ee6ba406 10572 elsif Is_Out_Of_Range
9c486805 10573 (Low_Bound (Opnd_Range), Etype (Targ_Index),
10574 Assume_Valid => True)
ee6ba406 10575 or else
10576 Is_Out_Of_Range
9c486805 10577 (High_Bound (Opnd_Range), Etype (Targ_Index),
10578 Assume_Valid => True)
ee6ba406 10579 then
10580 Add_Check
10581 (Compile_Time_Constraint_Error
cb97ae5c 10582 (Wnode, "value out of range of}??", T_Typ));
ee6ba406 10583
10584 else
10585 Evolve_Or_Else
10586 (Cond,
10587 Discrete_Range_Cond
00c403ee 10588 (Opnd_Range, Etype (Targ_Index)));
ee6ba406 10589 end if;
10590 end if;
10591
10592 Next_Index (Opnd_Index);
10593 Next_Index (Targ_Index);
10594 end loop;
10595 end;
10596 end if;
10597 end if;
10598 end if;
10599
10600 -- Construct the test and insert into the tree
10601
10602 if Present (Cond) then
10603 if Do_Access then
10604 Cond := Guard_Access (Cond, Loc, Ck_Node);
10605 end if;
10606
f15731c4 10607 Add_Check
10608 (Make_Raise_Constraint_Error (Loc,
eefa141b 10609 Condition => Cond,
22d3a5a3 10610 Reason => CE_Range_Check_Failed));
ee6ba406 10611 end if;
10612
10613 return Ret_Result;
ee6ba406 10614 end Selected_Range_Checks;
10615
10616 -------------------------------
10617 -- Storage_Checks_Suppressed --
10618 -------------------------------
10619
10620 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
10621 begin
9dfe12ae 10622 if Present (E) and then Checks_May_Be_Suppressed (E) then
10623 return Is_Check_Suppressed (E, Storage_Check);
10624 else
fafc6b97 10625 return Scope_Suppress.Suppress (Storage_Check);
9dfe12ae 10626 end if;
ee6ba406 10627 end Storage_Checks_Suppressed;
10628
10629 ---------------------------
10630 -- Tag_Checks_Suppressed --
10631 ---------------------------
10632
10633 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
10634 begin
89f1e35c 10635 if Present (E)
10636 and then Checks_May_Be_Suppressed (E)
10637 then
10638 return Is_Check_Suppressed (E, Tag_Check);
20cf157b 10639 else
10640 return Scope_Suppress.Suppress (Tag_Check);
9dfe12ae 10641 end if;
ee6ba406 10642 end Tag_Checks_Suppressed;
10643
7b8fa048 10644 ---------------------------------------
10645 -- Validate_Alignment_Check_Warnings --
10646 ---------------------------------------
10647
10648 procedure Validate_Alignment_Check_Warnings is
10649 begin
10650 for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop
10651 declare
10652 AWR : Alignment_Warnings_Record
10653 renames Alignment_Warnings.Table (J);
10654 begin
10655 if Known_Alignment (AWR.E)
10656 and then AWR.A mod Alignment (AWR.E) = 0
10657 then
10658 Delete_Warning_And_Continuations (AWR.W);
10659 end if;
10660 end;
10661 end loop;
10662 end Validate_Alignment_Check_Warnings;
10663
0577b0b1 10664 --------------------------
10665 -- Validity_Check_Range --
10666 --------------------------
10667
aaec8d13 10668 procedure Validity_Check_Range
10669 (N : Node_Id;
10670 Related_Id : Entity_Id := Empty)
10671 is
0577b0b1 10672 begin
10673 if Validity_Checks_On and Validity_Check_Operands then
10674 if Nkind (N) = N_Range then
aaec8d13 10675 Ensure_Valid
10676 (Expr => Low_Bound (N),
10677 Related_Id => Related_Id,
10678 Is_Low_Bound => True);
10679
10680 Ensure_Valid
10681 (Expr => High_Bound (N),
10682 Related_Id => Related_Id,
10683 Is_High_Bound => True);
0577b0b1 10684 end if;
10685 end if;
10686 end Validity_Check_Range;
10687
10688 --------------------------------
10689 -- Validity_Checks_Suppressed --
10690 --------------------------------
10691
10692 function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is
10693 begin
10694 if Present (E) and then Checks_May_Be_Suppressed (E) then
10695 return Is_Check_Suppressed (E, Validity_Check);
10696 else
fafc6b97 10697 return Scope_Suppress.Suppress (Validity_Check);
0577b0b1 10698 end if;
10699 end Validity_Checks_Suppressed;
10700
ee6ba406 10701end Checks;