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