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