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