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