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