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