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