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