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