1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Util; use Exp_Util;
35 with Elists; use Elists;
36 with Freeze; use Freeze;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
40 with Restrict; use Restrict;
41 with Rtsfind; use Rtsfind;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sem_Warn; use Sem_Warn;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
49 with Stand; use Stand;
50 with Targparm; use Targparm;
51 with Tbuild; use Tbuild;
52 with Ttypes; use Ttypes;
53 with Urealp; use Urealp;
54 with Validsw; use Validsw;
56 package body Checks is
58 -- General note: many of these routines are concerned with generating
59 -- checking code to make sure that constraint error is raised at runtime.
60 -- Clearly this code is only needed if the expander is active, since
61 -- otherwise we will not be generating code or going into the runtime
64 -- We therefore disconnect most of these checks if the expander is
65 -- inactive. This has the additional benefit that we do not need to
66 -- worry about the tree being messed up by previous errors (since errors
67 -- turn off expansion anyway).
69 -- There are a few exceptions to the above rule. For instance routines
70 -- such as Apply_Scalar_Range_Check that do not insert any code can be
71 -- safely called even when the Expander is inactive (but Errors_Detected
72 -- is 0). The benefit of executing this code when expansion is off, is
73 -- the ability to emit constraint error warning for static expressions
74 -- even when we are not generating code.
76 ----------------------------
77 -- Local Subprogram Specs --
78 ----------------------------
80 procedure Apply_Selected_Length_Checks
82 Target_Typ : Entity_Id;
83 Source_Typ : Entity_Id;
85 -- This is the subprogram that does all the work for Apply_Length_Check
86 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
87 -- described for the above routines. The Do_Static flag indicates that
88 -- only a static check is to be done.
90 procedure Apply_Selected_Range_Checks
92 Target_Typ : Entity_Id;
93 Source_Typ : Entity_Id;
95 -- This is the subprogram that does all the work for Apply_Range_Check.
96 -- Expr, Target_Typ and Source_Typ are as described for the above
97 -- routine. The Do_Static flag indicates that only a static check is
100 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
101 -- If a discriminal is used in constraining a prival, Return reference
102 -- to the discriminal of the protected body (which renames the parameter
103 -- of the enclosing protected operation). This clumsy transformation is
104 -- needed because privals are created too late and their actual subtypes
105 -- are not available when analysing the bodies of the protected operations.
106 -- To be cleaned up???
108 function Guard_Access
113 -- In the access type case, guard the test with a test to ensure
114 -- that the access value is non-null, since the checks do not
115 -- not apply to null access values.
117 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
118 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
119 -- Constraint_Error node.
121 function Selected_Length_Checks
123 Target_Typ : Entity_Id;
124 Source_Typ : Entity_Id;
127 -- Like Apply_Selected_Length_Checks, except it doesn't modify
128 -- anything, just returns a list of nodes as described in the spec of
129 -- this package for the Range_Check function.
131 function Selected_Range_Checks
133 Target_Typ : Entity_Id;
134 Source_Typ : Entity_Id;
137 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
138 -- just returns a list of nodes as described in the spec of this package
139 -- for the Range_Check function.
141 ------------------------------
142 -- Access_Checks_Suppressed --
143 ------------------------------
145 function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
147 return Scope_Suppress.Access_Checks
148 or else (Present (E) and then Suppress_Access_Checks (E));
149 end Access_Checks_Suppressed;
151 -------------------------------------
152 -- Accessibility_Checks_Suppressed --
153 -------------------------------------
155 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
157 return Scope_Suppress.Accessibility_Checks
158 or else (Present (E) and then Suppress_Accessibility_Checks (E));
159 end Accessibility_Checks_Suppressed;
161 -------------------------
162 -- Append_Range_Checks --
163 -------------------------
165 procedure Append_Range_Checks
166 (Checks : Check_Result;
168 Suppress_Typ : Entity_Id;
169 Static_Sloc : Source_Ptr;
172 Internal_Flag_Node : Node_Id := Flag_Node;
173 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
174 Checks_On : constant Boolean :=
175 (not Index_Checks_Suppressed (Suppress_Typ))
177 (not Range_Checks_Suppressed (Suppress_Typ));
180 -- For now we just return if Checks_On is false, however this should
181 -- be enhanced to check for an always True value in the condition
182 -- and to generate a compilation warning???
184 if not Checks_On then
189 exit when No (Checks (J));
191 if Nkind (Checks (J)) = N_Raise_Constraint_Error
192 and then Present (Condition (Checks (J)))
194 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
195 Append_To (Stmts, Checks (J));
196 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
202 Make_Raise_Constraint_Error (Internal_Static_Sloc,
203 Reason => CE_Range_Check_Failed));
206 end Append_Range_Checks;
208 ------------------------
209 -- Apply_Access_Check --
210 ------------------------
212 procedure Apply_Access_Check (N : Node_Id) is
213 P : constant Node_Id := Prefix (N);
216 if Inside_A_Generic then
220 if Is_Entity_Name (P) then
221 Check_Unset_Reference (P);
224 if Is_Entity_Name (P)
225 and then Access_Checks_Suppressed (Entity (P))
229 elsif Access_Checks_Suppressed (Etype (P)) then
233 Set_Do_Access_Check (N, True);
235 end Apply_Access_Check;
237 -------------------------------
238 -- Apply_Accessibility_Check --
239 -------------------------------
241 procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id) is
242 Loc : constant Source_Ptr := Sloc (N);
243 Param_Ent : constant Entity_Id := Param_Entity (N);
244 Param_Level : Node_Id;
245 Type_Level : Node_Id;
248 if Inside_A_Generic then
251 -- Only apply the run-time check if the access parameter
252 -- has an associated extra access level parameter and
253 -- when the level of the type is less deep than the level
254 -- of the access parameter.
256 elsif Present (Param_Ent)
257 and then Present (Extra_Accessibility (Param_Ent))
258 and then UI_Gt (Object_Access_Level (N),
259 Type_Access_Level (Typ))
260 and then not Accessibility_Checks_Suppressed (Param_Ent)
261 and then not Accessibility_Checks_Suppressed (Typ)
264 New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
267 Make_Integer_Literal (Loc, Type_Access_Level (Typ));
269 -- Raise Program_Error if the accessibility level of the
270 -- the access parameter is deeper than the level of the
271 -- target access type.
274 Make_Raise_Program_Error (Loc,
277 Left_Opnd => Param_Level,
278 Right_Opnd => Type_Level),
279 Reason => PE_Accessibility_Check_Failed));
281 Analyze_And_Resolve (N);
283 end Apply_Accessibility_Check;
285 ---------------------------
286 -- Apply_Alignment_Check --
287 ---------------------------
289 procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id) is
290 AC : constant Node_Id := Address_Clause (E);
295 if No (AC) or else Range_Checks_Suppressed (E) then
300 Expr := Expression (AC);
302 if Nkind (Expr) = N_Unchecked_Type_Conversion then
303 Expr := Expression (Expr);
305 elsif Nkind (Expr) = N_Function_Call
306 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
308 Expr := First (Parameter_Associations (Expr));
310 if Nkind (Expr) = N_Parameter_Association then
311 Expr := Explicit_Actual_Parameter (Expr);
315 -- Here Expr is the address value. See if we know that the
316 -- value is unacceptable at compile time.
318 if Compile_Time_Known_Value (Expr)
319 and then Known_Alignment (E)
321 if Expr_Value (Expr) mod Alignment (E) /= 0 then
323 Make_Raise_Program_Error (Loc,
324 Reason => PE_Misaligned_Address_Value));
326 ("?specified address for& not " &
327 "consistent with alignment", Expr, E);
330 -- Here we do not know if the value is acceptable, generate
331 -- code to raise PE if alignment is inappropriate.
334 -- Skip generation of this code if we don't want elab code
336 if not Restrictions (No_Elaboration_Code) then
337 Insert_After_And_Analyze (N,
338 Make_Raise_Program_Error (Loc,
345 (RTE (RE_Integer_Address),
346 Duplicate_Subexpr (Expr)),
348 Make_Attribute_Reference (Loc,
349 Prefix => New_Occurrence_Of (E, Loc),
350 Attribute_Name => Name_Alignment)),
351 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
352 Reason => PE_Misaligned_Address_Value),
353 Suppress => All_Checks);
358 end Apply_Alignment_Check;
360 -------------------------------------
361 -- Apply_Arithmetic_Overflow_Check --
362 -------------------------------------
364 -- This routine is called only if the type is an integer type, and
365 -- a software arithmetic overflow check must be performed for op
366 -- (add, subtract, multiply). The check is performed only if
367 -- Software_Overflow_Checking is enabled and Do_Overflow_Check
368 -- is set. In this case we expand the operation into a more complex
369 -- sequence of tests that ensures that overflow is properly caught.
371 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
372 Loc : constant Source_Ptr := Sloc (N);
373 Typ : constant Entity_Id := Etype (N);
374 Rtyp : constant Entity_Id := Root_Type (Typ);
375 Siz : constant Int := UI_To_Int (Esize (Rtyp));
376 Dsiz : constant Int := Siz * 2;
386 if Backend_Overflow_Checks_On_Target
387 or not Do_Overflow_Check (N)
388 or not Expander_Active
393 -- Nothing to do if the range of the result is known OK
395 Determine_Range (N, OK, Lo, Hi);
397 -- Note in the test below that we assume that if a bound of the
398 -- range is equal to that of the type. That's not quite accurate
399 -- but we do this for the following reasons:
401 -- a) The way that Determine_Range works, it will typically report
402 -- the bounds of the value are the bounds of the type, because
403 -- it either can't tell anything more precise, or does not think
404 -- it is worth the effort to be more precise.
406 -- b) It is very unusual to have a situation in which this would
407 -- generate an unnecessary overflow check (an example would be
408 -- a subtype with a range 0 .. Integer'Last - 1 to which the
409 -- literal value one is added.
411 -- c) The alternative is a lot of special casing in this routine
412 -- which would partially duplicate the Determine_Range processing.
415 and then Lo > Expr_Value (Type_Low_Bound (Typ))
416 and then Hi < Expr_Value (Type_High_Bound (Typ))
421 -- None of the special case optimizations worked, so there is nothing
422 -- for it but to generate the full general case code:
428 -- Typ (Checktyp (x) op Checktyp (y));
430 -- where Typ is the type of the original expression, and Checktyp is
431 -- an integer type of sufficient length to hold the largest possible
434 -- In the case where check type exceeds the size of Long_Long_Integer,
435 -- we use a different approach, expanding to:
437 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
439 -- where xxx is Add, Multiply or Subtract as appropriate
441 -- Find check type if one exists
443 if Dsiz <= Standard_Integer_Size then
444 Ctyp := Standard_Integer;
446 elsif Dsiz <= Standard_Long_Long_Integer_Size then
447 Ctyp := Standard_Long_Long_Integer;
449 -- No check type exists, use runtime call
452 if Nkind (N) = N_Op_Add then
453 Cent := RE_Add_With_Ovflo_Check;
455 elsif Nkind (N) = N_Op_Multiply then
456 Cent := RE_Multiply_With_Ovflo_Check;
459 pragma Assert (Nkind (N) = N_Op_Subtract);
460 Cent := RE_Subtract_With_Ovflo_Check;
465 Make_Function_Call (Loc,
466 Name => New_Reference_To (RTE (Cent), Loc),
467 Parameter_Associations => New_List (
468 OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
469 OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
471 Analyze_And_Resolve (N, Typ);
475 -- If we fall through, we have the case where we do the arithmetic in
476 -- the next higher type and get the check by conversion. In these cases
477 -- Ctyp is set to the type to be used as the check type.
479 Opnod := Relocate_Node (N);
481 Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
484 Set_Etype (Opnd, Ctyp);
485 Set_Analyzed (Opnd, True);
486 Set_Left_Opnd (Opnod, Opnd);
488 Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
491 Set_Etype (Opnd, Ctyp);
492 Set_Analyzed (Opnd, True);
493 Set_Right_Opnd (Opnod, Opnd);
495 -- The type of the operation changes to the base type of the check
496 -- type, and we reset the overflow check indication, since clearly
497 -- no overflow is possible now that we are using a double length
498 -- type. We also set the Analyzed flag to avoid a recursive attempt
499 -- to expand the node.
501 Set_Etype (Opnod, Base_Type (Ctyp));
502 Set_Do_Overflow_Check (Opnod, False);
503 Set_Analyzed (Opnod, True);
505 -- Now build the outer conversion
507 Opnd := OK_Convert_To (Typ, Opnod);
510 Set_Etype (Opnd, Typ);
511 Set_Analyzed (Opnd, True);
512 Set_Do_Overflow_Check (Opnd, True);
515 end Apply_Arithmetic_Overflow_Check;
517 ----------------------------
518 -- Apply_Array_Size_Check --
519 ----------------------------
521 -- Note: Really of course this entre check should be in the backend,
522 -- and perhaps this is not quite the right value, but it is good
523 -- enough to catch the normal cases (and the relevant ACVC tests!)
525 procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id) is
526 Loc : constant Source_Ptr := Sloc (N);
527 Ctyp : constant Entity_Id := Component_Type (Typ);
528 Ent : constant Entity_Id := Defining_Identifier (N);
540 Static : Boolean := True;
541 -- Set false if any index subtye bound is non-static
543 Umark : constant Uintp.Save_Mark := Uintp.Mark;
544 -- We can throw away all the Uint computations here, since they are
545 -- done only to generate boolean test results.
548 -- Size to check against
550 function Is_Address_Or_Import (Decl : Node_Id) return Boolean;
551 -- Determines if Decl is an address clause or Import/Interface pragma
552 -- that references the defining identifier of the current declaration.
554 --------------------------
555 -- Is_Address_Or_Import --
556 --------------------------
558 function Is_Address_Or_Import (Decl : Node_Id) return Boolean is
560 if Nkind (Decl) = N_At_Clause then
561 return Chars (Identifier (Decl)) = Chars (Ent);
563 elsif Nkind (Decl) = N_Attribute_Definition_Clause then
565 Chars (Decl) = Name_Address
567 Nkind (Name (Decl)) = N_Identifier
569 Chars (Name (Decl)) = Chars (Ent);
571 elsif Nkind (Decl) = N_Pragma then
572 if (Chars (Decl) = Name_Import
574 Chars (Decl) = Name_Interface)
575 and then Present (Pragma_Argument_Associations (Decl))
578 F : constant Node_Id :=
579 First (Pragma_Argument_Associations (Decl));
587 Nkind (Expression (Next (F))) = N_Identifier
589 Chars (Expression (Next (F))) = Chars (Ent);
599 end Is_Address_Or_Import;
601 -- Start of processing for Apply_Array_Size_Check
604 if not Expander_Active
605 or else Storage_Checks_Suppressed (Typ)
610 -- It is pointless to insert this check inside an _init_proc, because
611 -- that's too late, we have already built the object to be the right
612 -- size, and if it's too large, too bad!
614 if Inside_Init_Proc then
618 -- Look head for pragma interface/import or address clause applying
619 -- to this entity. If found, we suppress the check entirely. For now
620 -- we only look ahead 20 declarations to stop this becoming too slow
621 -- Note that eventually this whole routine gets moved to gigi.
624 for Ctr in 1 .. 20 loop
628 if Is_Address_Or_Import (Decl) then
633 -- First step is to calculate the maximum number of elements. For this
634 -- calculation, we use the actual size of the subtype if it is static,
635 -- and if a bound of a subtype is non-static, we go to the bound of the
639 Indx := First_Index (Typ);
640 while Present (Indx) loop
641 Xtyp := Etype (Indx);
642 Lo := Type_Low_Bound (Xtyp);
643 Hi := Type_High_Bound (Xtyp);
645 -- If any bound raises constraint error, we will never get this
646 -- far, so there is no need to generate any kind of check.
648 if Raises_Constraint_Error (Lo)
650 Raises_Constraint_Error (Hi)
652 Uintp.Release (Umark);
656 -- Otherwise get bounds values
658 if Is_Static_Expression (Lo) then
659 Lob := Expr_Value (Lo);
661 Lob := Expr_Value (Type_Low_Bound (Base_Type (Xtyp)));
665 if Is_Static_Expression (Hi) then
666 Hib := Expr_Value (Hi);
668 Hib := Expr_Value (Type_High_Bound (Base_Type (Xtyp)));
672 Siz := Siz * UI_Max (Hib - Lob + 1, Uint_0);
676 -- Compute the limit against which we want to check. For subprograms,
677 -- where the array will go on the stack, we use 8*2**24, which (in
678 -- bits) is the size of a 16 megabyte array.
680 if Is_Subprogram (Scope (Ent)) then
681 Check_Siz := Uint_2 ** 27;
683 Check_Siz := Uint_2 ** 31;
686 -- If we have all static bounds and Siz is too large, then we know we
687 -- know we have a storage error right now, so generate message
689 if Static and then Siz >= Check_Siz then
691 Make_Raise_Storage_Error (Loc,
692 Reason => SE_Object_Too_Large));
693 Warn_On_Instance := True;
694 Error_Msg_N ("?Storage_Error will be raised at run-time", N);
695 Warn_On_Instance := False;
696 Uintp.Release (Umark);
700 -- Case of component size known at compile time. If the array
701 -- size is definitely in range, then we do not need a check.
703 if Known_Esize (Ctyp)
704 and then Siz * Esize (Ctyp) < Check_Siz
706 Uintp.Release (Umark);
710 -- Here if a dynamic check is required
712 -- What we do is to build an expression for the size of the array,
713 -- which is computed as the 'Size of the array component, times
714 -- the size of each dimension.
716 Uintp.Release (Umark);
719 Make_Attribute_Reference (Loc,
720 Prefix => New_Occurrence_Of (Ctyp, Loc),
721 Attribute_Name => Name_Size);
723 Indx := First_Index (Typ);
725 for J in 1 .. Number_Dimensions (Typ) loop
727 if Sloc (Etype (Indx)) = Sloc (N) then
728 Ensure_Defined (Etype (Indx), N);
732 Make_Op_Multiply (Loc,
735 Make_Attribute_Reference (Loc,
736 Prefix => New_Occurrence_Of (Typ, Loc),
737 Attribute_Name => Name_Length,
738 Expressions => New_List (
739 Make_Integer_Literal (Loc, J))));
744 Make_Raise_Storage_Error (Loc,
749 Make_Integer_Literal (Loc, Check_Siz)),
750 Reason => SE_Object_Too_Large);
752 Set_Size_Check_Code (Defining_Identifier (N), Code);
753 Insert_Action (N, Code);
754 end Apply_Array_Size_Check;
756 ----------------------------
757 -- Apply_Constraint_Check --
758 ----------------------------
760 procedure Apply_Constraint_Check
763 No_Sliding : Boolean := False)
765 Desig_Typ : Entity_Id;
768 if Inside_A_Generic then
771 elsif Is_Scalar_Type (Typ) then
772 Apply_Scalar_Range_Check (N, Typ);
774 elsif Is_Array_Type (Typ) then
776 -- A useful optimization: an aggregate with only an Others clause
777 -- always has the right bounds.
779 if Nkind (N) = N_Aggregate
780 and then No (Expressions (N))
782 (First (Choices (First (Component_Associations (N)))))
788 if Is_Constrained (Typ) then
789 Apply_Length_Check (N, Typ);
792 Apply_Range_Check (N, Typ);
795 Apply_Range_Check (N, Typ);
798 elsif (Is_Record_Type (Typ)
799 or else Is_Private_Type (Typ))
800 and then Has_Discriminants (Base_Type (Typ))
801 and then Is_Constrained (Typ)
803 Apply_Discriminant_Check (N, Typ);
805 elsif Is_Access_Type (Typ) then
807 Desig_Typ := Designated_Type (Typ);
809 -- No checks necessary if expression statically null
811 if Nkind (N) = N_Null then
814 -- No sliding possible on access to arrays
816 elsif Is_Array_Type (Desig_Typ) then
817 if Is_Constrained (Desig_Typ) then
818 Apply_Length_Check (N, Typ);
821 Apply_Range_Check (N, Typ);
823 elsif Has_Discriminants (Base_Type (Desig_Typ))
824 and then Is_Constrained (Desig_Typ)
826 Apply_Discriminant_Check (N, Typ);
829 end Apply_Constraint_Check;
831 ------------------------------
832 -- Apply_Discriminant_Check --
833 ------------------------------
835 procedure Apply_Discriminant_Check
838 Lhs : Node_Id := Empty)
840 Loc : constant Source_Ptr := Sloc (N);
841 Do_Access : constant Boolean := Is_Access_Type (Typ);
842 S_Typ : Entity_Id := Etype (N);
846 function Is_Aliased_Unconstrained_Component return Boolean;
847 -- It is possible for an aliased component to have a nominal
848 -- unconstrained subtype (through instantiation). If this is a
849 -- discriminated component assigned in the expansion of an aggregate
850 -- in an initialization, the check must be suppressed. This unusual
851 -- situation requires a predicate of its own (see 7503-008).
853 ----------------------------------------
854 -- Is_Aliased_Unconstrained_Component --
855 ----------------------------------------
857 function Is_Aliased_Unconstrained_Component return Boolean is
862 if Nkind (Lhs) /= N_Selected_Component then
865 Comp := Entity (Selector_Name (Lhs));
866 Pref := Prefix (Lhs);
869 if Ekind (Comp) /= E_Component
870 or else not Is_Aliased (Comp)
875 return not Comes_From_Source (Pref)
877 and then not Is_Constrained (Etype (Comp));
878 end Is_Aliased_Unconstrained_Component;
880 -- Start of processing for Apply_Discriminant_Check
884 T_Typ := Designated_Type (Typ);
889 -- Nothing to do if discriminant checks are suppressed or else no code
890 -- is to be generated
892 if not Expander_Active
893 or else Discriminant_Checks_Suppressed (T_Typ)
898 -- No discriminant checks necessary for access when expression
899 -- is statically Null. This is not only an optimization, this is
900 -- fundamental because otherwise discriminant checks may be generated
901 -- in init procs for types containing an access to a non-frozen yet
902 -- record, causing a deadly forward reference.
904 -- Also, if the expression is of an access type whose designated
905 -- type is incomplete, then the access value must be null and
906 -- we suppress the check.
908 if Nkind (N) = N_Null then
911 elsif Is_Access_Type (S_Typ) then
912 S_Typ := Designated_Type (S_Typ);
914 if Ekind (S_Typ) = E_Incomplete_Type then
919 -- If an assignment target is present, then we need to generate
920 -- the actual subtype if the target is a parameter or aliased
921 -- object with an unconstrained nominal subtype.
924 and then (Present (Param_Entity (Lhs))
925 or else (not Is_Constrained (T_Typ)
926 and then Is_Aliased_View (Lhs)
927 and then not Is_Aliased_Unconstrained_Component))
929 T_Typ := Get_Actual_Subtype (Lhs);
932 -- Nothing to do if the type is unconstrained (this is the case
933 -- where the actual subtype in the RM sense of N is unconstrained
934 -- and no check is required).
936 if not Is_Constrained (T_Typ) then
940 -- Suppress checks if the subtypes are the same.
941 -- the check must be preserved in an assignment to a formal, because
942 -- the constraint is given by the actual.
944 if Nkind (Original_Node (N)) /= N_Allocator
946 or else not Is_Entity_Name (Lhs)
947 or else (Ekind (Entity (Lhs)) /= E_In_Out_Parameter
948 and then Ekind (Entity (Lhs)) /= E_Out_Parameter))
951 or else (Do_Access and then Designated_Type (Typ) = S_Typ))
952 and then not Is_Aliased_View (Lhs)
957 -- We can also eliminate checks on allocators with a subtype mark
958 -- that coincides with the context type. The context type may be a
959 -- subtype without a constraint (common case, a generic actual).
961 elsif Nkind (Original_Node (N)) = N_Allocator
962 and then Is_Entity_Name (Expression (Original_Node (N)))
965 Alloc_Typ : Entity_Id := Entity (Expression (Original_Node (N)));
969 or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
970 and then Is_Entity_Name (
971 Subtype_Indication (Parent (T_Typ)))
972 and then Alloc_Typ = Base_Type (T_Typ))
980 -- See if we have a case where the types are both constrained, and
981 -- all the constraints are constants. In this case, we can do the
982 -- check successfully at compile time.
984 -- we skip this check for the case where the node is a rewritten`
985 -- allocator, because it already carries the context subtype, and
986 -- extracting the discriminants from the aggregate is messy.
988 if Is_Constrained (S_Typ)
989 and then Nkind (Original_Node (N)) /= N_Allocator
999 -- S_Typ may not have discriminants in the case where it is a
1000 -- private type completed by a default discriminated type. In
1001 -- that case, we need to get the constraints from the
1002 -- underlying_type. If the underlying type is unconstrained (i.e.
1003 -- has no default discriminants) no check is needed.
1005 if Has_Discriminants (S_Typ) then
1006 Discr := First_Discriminant (S_Typ);
1007 DconS := First_Elmt (Discriminant_Constraint (S_Typ));
1010 Discr := First_Discriminant (Underlying_Type (S_Typ));
1013 (Discriminant_Constraint (Underlying_Type (S_Typ)));
1020 DconT := First_Elmt (Discriminant_Constraint (T_Typ));
1022 while Present (Discr) loop
1023 ItemS := Node (DconS);
1024 ItemT := Node (DconT);
1027 not Is_OK_Static_Expression (ItemS)
1029 not Is_OK_Static_Expression (ItemT);
1031 if Expr_Value (ItemS) /= Expr_Value (ItemT) then
1032 if Do_Access then -- needs run-time check.
1035 Apply_Compile_Time_Constraint_Error
1036 (N, "incorrect value for discriminant&?",
1037 CE_Discriminant_Check_Failed, Ent => Discr);
1044 Next_Discriminant (Discr);
1053 -- Here we need a discriminant check. First build the expression
1054 -- for the comparisons of the discriminants:
1056 -- (n.disc1 /= typ.disc1) or else
1057 -- (n.disc2 /= typ.disc2) or else
1059 -- (n.discn /= typ.discn)
1061 Cond := Build_Discriminant_Checks (N, T_Typ);
1063 -- If Lhs is set and is a parameter, then the condition is
1064 -- guarded by: lhs'constrained and then (condition built above)
1066 if Present (Param_Entity (Lhs)) then
1070 Make_Attribute_Reference (Loc,
1071 Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
1072 Attribute_Name => Name_Constrained),
1073 Right_Opnd => Cond);
1077 Cond := Guard_Access (Cond, Loc, N);
1081 Make_Raise_Constraint_Error (Loc,
1083 Reason => CE_Discriminant_Check_Failed));
1085 end Apply_Discriminant_Check;
1087 ------------------------
1088 -- Apply_Divide_Check --
1089 ------------------------
1091 procedure Apply_Divide_Check (N : Node_Id) is
1092 Loc : constant Source_Ptr := Sloc (N);
1093 Typ : constant Entity_Id := Etype (N);
1094 Left : constant Node_Id := Left_Opnd (N);
1095 Right : constant Node_Id := Right_Opnd (N);
1107 and not Backend_Divide_Checks_On_Target
1109 Determine_Range (Right, ROK, Rlo, Rhi);
1111 -- See if division by zero possible, and if so generate test. This
1112 -- part of the test is not controlled by the -gnato switch.
1114 if Do_Division_Check (N) then
1116 if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
1118 Make_Raise_Constraint_Error (Loc,
1121 Left_Opnd => Duplicate_Subexpr (Right),
1122 Right_Opnd => Make_Integer_Literal (Loc, 0)),
1123 Reason => CE_Divide_By_Zero));
1127 -- Test for extremely annoying case of xxx'First divided by -1
1129 if Do_Overflow_Check (N) then
1131 if Nkind (N) = N_Op_Divide
1132 and then Is_Signed_Integer_Type (Typ)
1134 Determine_Range (Left, LOK, Llo, Lhi);
1135 LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
1137 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
1139 ((not LOK) or else (Llo = LLB))
1142 Make_Raise_Constraint_Error (Loc,
1147 Left_Opnd => Duplicate_Subexpr (Left),
1148 Right_Opnd => Make_Integer_Literal (Loc, LLB)),
1151 Left_Opnd => Duplicate_Subexpr (Right),
1153 Make_Integer_Literal (Loc, -1))),
1154 Reason => CE_Overflow_Check_Failed));
1159 end Apply_Divide_Check;
1161 ------------------------
1162 -- Apply_Length_Check --
1163 ------------------------
1165 procedure Apply_Length_Check
1167 Target_Typ : Entity_Id;
1168 Source_Typ : Entity_Id := Empty)
1171 Apply_Selected_Length_Checks
1172 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1173 end Apply_Length_Check;
1175 -----------------------
1176 -- Apply_Range_Check --
1177 -----------------------
1179 procedure Apply_Range_Check
1181 Target_Typ : Entity_Id;
1182 Source_Typ : Entity_Id := Empty)
1185 Apply_Selected_Range_Checks
1186 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1187 end Apply_Range_Check;
1189 ------------------------------
1190 -- Apply_Scalar_Range_Check --
1191 ------------------------------
1193 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check
1194 -- flag off if it is already set on.
1196 procedure Apply_Scalar_Range_Check
1198 Target_Typ : Entity_Id;
1199 Source_Typ : Entity_Id := Empty;
1200 Fixed_Int : Boolean := False)
1202 Parnt : constant Node_Id := Parent (Expr);
1204 Arr : Node_Id := Empty; -- initialize to prevent warning
1205 Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning
1208 Is_Subscr_Ref : Boolean;
1209 -- Set true if Expr is a subscript
1211 Is_Unconstrained_Subscr_Ref : Boolean;
1212 -- Set true if Expr is a subscript of an unconstrained array. In this
1213 -- case we do not attempt to do an analysis of the value against the
1214 -- range of the subscript, since we don't know the actual subtype.
1217 -- Set to True if Expr should be regarded as a real value
1218 -- even though the type of Expr might be discrete.
1220 procedure Bad_Value;
1221 -- Procedure called if value is determined to be out of range
1223 procedure Bad_Value is
1225 Apply_Compile_Time_Constraint_Error
1226 (Expr, "value not in range of}?", CE_Range_Check_Failed,
1232 if Inside_A_Generic then
1235 -- Return if check obviously not needed. Note that we do not check
1236 -- for the expander being inactive, since this routine does not
1237 -- insert any code, but it does generate useful warnings sometimes,
1238 -- which we would like even if we are in semantics only mode.
1240 elsif Target_Typ = Any_Type
1241 or else not Is_Scalar_Type (Target_Typ)
1242 or else Raises_Constraint_Error (Expr)
1247 -- Now, see if checks are suppressed
1250 Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
1252 if Is_Subscr_Ref then
1253 Arr := Prefix (Parnt);
1254 Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
1257 if not Do_Range_Check (Expr) then
1259 -- Subscript reference. Check for Index_Checks suppressed
1261 if Is_Subscr_Ref then
1263 -- Check array type and its base type
1265 if Index_Checks_Suppressed (Arr_Typ)
1266 or else Suppress_Index_Checks (Base_Type (Arr_Typ))
1270 -- Check array itself if it is an entity name
1272 elsif Is_Entity_Name (Arr)
1273 and then Suppress_Index_Checks (Entity (Arr))
1277 -- Check expression itself if it is an entity name
1279 elsif Is_Entity_Name (Expr)
1280 and then Suppress_Index_Checks (Entity (Expr))
1285 -- All other cases, check for Range_Checks suppressed
1288 -- Check target type and its base type
1290 if Range_Checks_Suppressed (Target_Typ)
1291 or else Suppress_Range_Checks (Base_Type (Target_Typ))
1295 -- Check expression itself if it is an entity name
1297 elsif Is_Entity_Name (Expr)
1298 and then Suppress_Range_Checks (Entity (Expr))
1302 -- If Expr is part of an assignment statement, then check
1303 -- left side of assignment if it is an entity name.
1305 elsif Nkind (Parnt) = N_Assignment_Statement
1306 and then Is_Entity_Name (Name (Parnt))
1307 and then Suppress_Range_Checks (Entity (Name (Parnt)))
1314 -- Now see if we need a check
1316 if No (Source_Typ) then
1317 S_Typ := Etype (Expr);
1319 S_Typ := Source_Typ;
1322 if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
1326 Is_Unconstrained_Subscr_Ref :=
1327 Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
1329 -- Always do a range check if the source type includes infinities
1330 -- and the target type does not include infinities.
1332 if Is_Floating_Point_Type (S_Typ)
1333 and then Has_Infinities (S_Typ)
1334 and then not Has_Infinities (Target_Typ)
1336 Enable_Range_Check (Expr);
1339 -- Return if we know expression is definitely in the range of
1340 -- the target type as determined by Determine_Range. Right now
1341 -- we only do this for discrete types, and not fixed-point or
1342 -- floating-point types.
1344 -- The additional less-precise tests below catch these cases.
1346 -- Note: skip this if we are given a source_typ, since the point
1347 -- of supplying a Source_Typ is to stop us looking at the expression.
1348 -- could sharpen this test to be out parameters only ???
1350 if Is_Discrete_Type (Target_Typ)
1351 and then Is_Discrete_Type (Etype (Expr))
1352 and then not Is_Unconstrained_Subscr_Ref
1353 and then No (Source_Typ)
1356 Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
1357 Thi : constant Node_Id := Type_High_Bound (Target_Typ);
1362 if Compile_Time_Known_Value (Tlo)
1363 and then Compile_Time_Known_Value (Thi)
1365 Determine_Range (Expr, OK, Lo, Hi);
1369 Lov : constant Uint := Expr_Value (Tlo);
1370 Hiv : constant Uint := Expr_Value (Thi);
1373 if Lo >= Lov and then Hi <= Hiv then
1376 elsif Lov > Hi or else Hiv < Lo then
1387 Is_Floating_Point_Type (S_Typ)
1388 or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
1390 -- Check if we can determine at compile time whether Expr is in the
1391 -- range of the target type. Note that if S_Typ is within the
1392 -- bounds of Target_Typ then this must be the case. This checks is
1393 -- only meaningful if this is not a conversion between integer and
1396 if not Is_Unconstrained_Subscr_Ref
1398 Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
1400 (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
1402 Is_In_Range (Expr, Target_Typ, Fixed_Int, Int_Real))
1406 elsif Is_Out_Of_Range (Expr, Target_Typ, Fixed_Int, Int_Real) then
1410 -- Do not set range checks if they are killed
1412 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
1413 and then Kill_Range_Check (Expr)
1417 -- ??? We only need a runtime check if the target type is constrained
1418 -- (the predefined type Float is not for instance).
1419 -- so the following should really be
1421 -- elsif Is_Constrained (Target_Typ) then
1423 -- but it isn't because certain types do not have the Is_Constrained
1424 -- flag properly set (see 1503-003).
1427 Enable_Range_Check (Expr);
1431 end Apply_Scalar_Range_Check;
1433 ----------------------------------
1434 -- Apply_Selected_Length_Checks --
1435 ----------------------------------
1437 procedure Apply_Selected_Length_Checks
1439 Target_Typ : Entity_Id;
1440 Source_Typ : Entity_Id;
1441 Do_Static : Boolean)
1444 R_Result : Check_Result;
1447 Loc : constant Source_Ptr := Sloc (Ck_Node);
1448 Checks_On : constant Boolean :=
1449 (not Index_Checks_Suppressed (Target_Typ))
1451 (not Length_Checks_Suppressed (Target_Typ));
1454 if not Expander_Active then
1459 Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
1461 for J in 1 .. 2 loop
1463 R_Cno := R_Result (J);
1464 exit when No (R_Cno);
1466 -- A length check may mention an Itype which is attached to a
1467 -- subsequent node. At the top level in a package this can cause
1468 -- an order-of-elaboration problem, so we make sure that the itype
1469 -- is referenced now.
1471 if Ekind (Current_Scope) = E_Package
1472 and then Is_Compilation_Unit (Current_Scope)
1474 Ensure_Defined (Target_Typ, Ck_Node);
1476 if Present (Source_Typ) then
1477 Ensure_Defined (Source_Typ, Ck_Node);
1479 elsif Is_Itype (Etype (Ck_Node)) then
1480 Ensure_Defined (Etype (Ck_Node), Ck_Node);
1484 -- If the item is a conditional raise of constraint error,
1485 -- then have a look at what check is being performed and
1488 if Nkind (R_Cno) = N_Raise_Constraint_Error
1489 and then Present (Condition (R_Cno))
1491 Cond := Condition (R_Cno);
1493 if not Has_Dynamic_Length_Check (Ck_Node)
1496 Insert_Action (Ck_Node, R_Cno);
1498 if not Do_Static then
1499 Set_Has_Dynamic_Length_Check (Ck_Node);
1503 -- Output a warning if the condition is known to be True
1505 if Is_Entity_Name (Cond)
1506 and then Entity (Cond) = Standard_True
1508 Apply_Compile_Time_Constraint_Error
1509 (Ck_Node, "wrong length for array of}?",
1510 CE_Length_Check_Failed,
1514 -- If we were only doing a static check, or if checks are not
1515 -- on, then we want to delete the check, since it is not needed.
1516 -- We do this by replacing the if statement by a null statement
1518 elsif Do_Static or else not Checks_On then
1519 Rewrite (R_Cno, Make_Null_Statement (Loc));
1523 Install_Static_Check (R_Cno, Loc);
1528 end Apply_Selected_Length_Checks;
1530 ---------------------------------
1531 -- Apply_Selected_Range_Checks --
1532 ---------------------------------
1534 procedure Apply_Selected_Range_Checks
1536 Target_Typ : Entity_Id;
1537 Source_Typ : Entity_Id;
1538 Do_Static : Boolean)
1541 R_Result : Check_Result;
1544 Loc : constant Source_Ptr := Sloc (Ck_Node);
1545 Checks_On : constant Boolean :=
1546 (not Index_Checks_Suppressed (Target_Typ))
1548 (not Range_Checks_Suppressed (Target_Typ));
1551 if not Expander_Active or else not Checks_On then
1556 Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
1558 for J in 1 .. 2 loop
1560 R_Cno := R_Result (J);
1561 exit when No (R_Cno);
1563 -- If the item is a conditional raise of constraint error,
1564 -- then have a look at what check is being performed and
1567 if Nkind (R_Cno) = N_Raise_Constraint_Error
1568 and then Present (Condition (R_Cno))
1570 Cond := Condition (R_Cno);
1572 if not Has_Dynamic_Range_Check (Ck_Node) then
1573 Insert_Action (Ck_Node, R_Cno);
1575 if not Do_Static then
1576 Set_Has_Dynamic_Range_Check (Ck_Node);
1580 -- Output a warning if the condition is known to be True
1582 if Is_Entity_Name (Cond)
1583 and then Entity (Cond) = Standard_True
1585 -- Since an N_Range is technically not an expression, we
1586 -- have to set one of the bounds to C_E and then just flag
1587 -- the N_Range. The warning message will point to the
1588 -- lower bound and complain about a range, which seems OK.
1590 if Nkind (Ck_Node) = N_Range then
1591 Apply_Compile_Time_Constraint_Error
1592 (Low_Bound (Ck_Node), "static range out of bounds of}?",
1593 CE_Range_Check_Failed,
1597 Set_Raises_Constraint_Error (Ck_Node);
1600 Apply_Compile_Time_Constraint_Error
1601 (Ck_Node, "static value out of range of}?",
1602 CE_Range_Check_Failed,
1607 -- If we were only doing a static check, or if checks are not
1608 -- on, then we want to delete the check, since it is not needed.
1609 -- We do this by replacing the if statement by a null statement
1611 elsif Do_Static or else not Checks_On then
1612 Rewrite (R_Cno, Make_Null_Statement (Loc));
1616 Install_Static_Check (R_Cno, Loc);
1621 end Apply_Selected_Range_Checks;
1623 -------------------------------
1624 -- Apply_Static_Length_Check --
1625 -------------------------------
1627 procedure Apply_Static_Length_Check
1629 Target_Typ : Entity_Id;
1630 Source_Typ : Entity_Id := Empty)
1633 Apply_Selected_Length_Checks
1634 (Expr, Target_Typ, Source_Typ, Do_Static => True);
1635 end Apply_Static_Length_Check;
1637 -------------------------------------
1638 -- Apply_Subscript_Validity_Checks --
1639 -------------------------------------
1641 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
1645 pragma Assert (Nkind (Expr) = N_Indexed_Component);
1647 -- Loop through subscripts
1649 Sub := First (Expressions (Expr));
1650 while Present (Sub) loop
1652 -- Check one subscript. Note that we do not worry about
1653 -- enumeration type with holes, since we will convert the
1654 -- value to a Pos value for the subscript, and that convert
1655 -- will do the necessary validity check.
1657 Ensure_Valid (Sub, Holes_OK => True);
1659 -- Move to next subscript
1663 end Apply_Subscript_Validity_Checks;
1665 ----------------------------------
1666 -- Apply_Type_Conversion_Checks --
1667 ----------------------------------
1669 procedure Apply_Type_Conversion_Checks (N : Node_Id) is
1670 Target_Type : constant Entity_Id := Etype (N);
1671 Target_Base : constant Entity_Id := Base_Type (Target_Type);
1673 Expr : constant Node_Id := Expression (N);
1674 Expr_Type : constant Entity_Id := Etype (Expr);
1677 if Inside_A_Generic then
1680 -- Skip these checks if serious errors detected, there are some nasty
1681 -- situations of incomplete trees that blow things up.
1683 elsif Serious_Errors_Detected > 0 then
1686 -- Scalar type conversions of the form Target_Type (Expr) require
1689 -- - First there is an overflow check to insure that Expr is
1690 -- in the base type of Target_Typ (4.6 (28)),
1692 -- - After we know Expr fits into the base type, we must perform a
1693 -- range check to ensure that Expr meets the constraints of the
1696 elsif Is_Scalar_Type (Target_Type) then
1698 Conv_OK : constant Boolean := Conversion_OK (N);
1699 -- If the Conversion_OK flag on the type conversion is set
1700 -- and no floating point type is involved in the type conversion
1701 -- then fixed point values must be read as integral values.
1706 if not Overflow_Checks_Suppressed (Target_Base)
1707 and then not In_Subrange_Of (Expr_Type, Target_Base, Conv_OK)
1709 Set_Do_Overflow_Check (N);
1712 if not Range_Checks_Suppressed (Target_Type)
1713 and then not Range_Checks_Suppressed (Expr_Type)
1715 Apply_Scalar_Range_Check
1716 (Expr, Target_Type, Fixed_Int => Conv_OK);
1720 elsif Comes_From_Source (N)
1721 and then Is_Record_Type (Target_Type)
1722 and then Is_Derived_Type (Target_Type)
1723 and then not Is_Tagged_Type (Target_Type)
1724 and then not Is_Constrained (Target_Type)
1725 and then Present (Girder_Constraint (Target_Type))
1727 -- A unconstrained derived type may have inherited discriminants.
1728 -- Build an actual discriminant constraint list using the girder
1729 -- constraint, to verify that the expression of the parent type
1730 -- satisfies the constraints imposed by the (unconstrained!)
1731 -- derived type. This applies to value conversions, not to view
1732 -- conversions of tagged types.
1735 Loc : constant Source_Ptr := Sloc (N);
1737 Constraint : Elmt_Id;
1738 Discr_Value : Node_Id;
1740 New_Constraints : Elist_Id := New_Elmt_List;
1741 Old_Constraints : Elist_Id := Discriminant_Constraint (Expr_Type);
1744 Constraint := First_Elmt (Girder_Constraint (Target_Type));
1746 while Present (Constraint) loop
1747 Discr_Value := Node (Constraint);
1749 if Is_Entity_Name (Discr_Value)
1750 and then Ekind (Entity (Discr_Value)) = E_Discriminant
1752 Discr := Corresponding_Discriminant (Entity (Discr_Value));
1755 and then Scope (Discr) = Base_Type (Expr_Type)
1757 -- Parent is constrained by new discriminant. Obtain
1758 -- Value of original discriminant in expression. If
1759 -- the new discriminant has been used to constrain more
1760 -- than one of the girder ones, this will provide the
1761 -- required consistency check.
1764 Make_Selected_Component (Loc,
1766 Duplicate_Subexpr (Expr, Name_Req => True),
1768 Make_Identifier (Loc, Chars (Discr))),
1772 -- Discriminant of more remote ancestor ???
1777 -- Derived type definition has an explicit value for
1778 -- this girder discriminant.
1782 (Duplicate_Subexpr (Discr_Value), New_Constraints);
1785 Next_Elmt (Constraint);
1788 -- Use the unconstrained expression type to retrieve the
1789 -- discriminants of the parent, and apply momentarily the
1790 -- discriminant constraint synthesized above.
1792 Set_Discriminant_Constraint (Expr_Type, New_Constraints);
1793 Cond := Build_Discriminant_Checks (Expr, Expr_Type);
1794 Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
1797 Make_Raise_Constraint_Error (Loc,
1799 Reason => CE_Discriminant_Check_Failed));
1802 -- should there be other checks here for array types ???
1808 end Apply_Type_Conversion_Checks;
1810 ----------------------------------------------
1811 -- Apply_Universal_Integer_Attribute_Checks --
1812 ----------------------------------------------
1814 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
1815 Loc : constant Source_Ptr := Sloc (N);
1816 Typ : constant Entity_Id := Etype (N);
1819 if Inside_A_Generic then
1822 -- Nothing to do if checks are suppressed
1824 elsif Range_Checks_Suppressed (Typ)
1825 and then Overflow_Checks_Suppressed (Typ)
1829 -- Nothing to do if the attribute does not come from source. The
1830 -- internal attributes we generate of this type do not need checks,
1831 -- and furthermore the attempt to check them causes some circular
1832 -- elaboration orders when dealing with packed types.
1834 elsif not Comes_From_Source (N) then
1837 -- Otherwise, replace the attribute node with a type conversion
1838 -- node whose expression is the attribute, retyped to universal
1839 -- integer, and whose subtype mark is the target type. The call
1840 -- to analyze this conversion will set range and overflow checks
1841 -- as required for proper detection of an out of range value.
1844 Set_Etype (N, Universal_Integer);
1845 Set_Analyzed (N, True);
1848 Make_Type_Conversion (Loc,
1849 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
1850 Expression => Relocate_Node (N)));
1852 Analyze_And_Resolve (N, Typ);
1856 end Apply_Universal_Integer_Attribute_Checks;
1858 -------------------------------
1859 -- Build_Discriminant_Checks --
1860 -------------------------------
1862 function Build_Discriminant_Checks
1867 Loc : constant Source_Ptr := Sloc (N);
1870 Disc_Ent : Entity_Id;
1875 Disc := First_Elmt (Discriminant_Constraint (T_Typ));
1877 -- For a fully private type, use the discriminants of the parent
1880 if Is_Private_Type (T_Typ)
1881 and then No (Full_View (T_Typ))
1883 Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
1885 Disc_Ent := First_Discriminant (T_Typ);
1888 while Present (Disc) loop
1890 Dval := Node (Disc);
1892 if Nkind (Dval) = N_Identifier
1893 and then Ekind (Entity (Dval)) = E_Discriminant
1895 Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
1897 Dval := Duplicate_Subexpr (Dval);
1900 Evolve_Or_Else (Cond,
1903 Make_Selected_Component (Loc,
1905 Duplicate_Subexpr (N, Name_Req => True),
1907 Make_Identifier (Loc, Chars (Disc_Ent))),
1908 Right_Opnd => Dval));
1911 Next_Discriminant (Disc_Ent);
1915 end Build_Discriminant_Checks;
1917 -----------------------------------
1918 -- Check_Valid_Lvalue_Subscripts --
1919 -----------------------------------
1921 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
1923 -- Skip this if range checks are suppressed
1925 if Range_Checks_Suppressed (Etype (Expr)) then
1928 -- Only do this check for expressions that come from source. We
1929 -- assume that expander generated assignments explicitly include
1930 -- any necessary checks. Note that this is not just an optimization,
1931 -- it avoids infinite recursions!
1933 elsif not Comes_From_Source (Expr) then
1936 -- For a selected component, check the prefix
1938 elsif Nkind (Expr) = N_Selected_Component then
1939 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
1942 -- Case of indexed component
1944 elsif Nkind (Expr) = N_Indexed_Component then
1945 Apply_Subscript_Validity_Checks (Expr);
1947 -- Prefix may itself be or contain an indexed component, and
1948 -- these subscripts need checking as well
1950 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
1952 end Check_Valid_Lvalue_Subscripts;
1954 ---------------------
1955 -- Determine_Range --
1956 ---------------------
1958 Cache_Size : constant := 2 ** 10;
1959 type Cache_Index is range 0 .. Cache_Size - 1;
1960 -- Determine size of below cache (power of 2 is more efficient!)
1962 Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
1963 Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
1964 Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
1965 -- The above arrays are used to implement a small direct cache
1966 -- for Determine_Range calls. Because of the way Determine_Range
1967 -- recursively traces subexpressions, and because overflow checking
1968 -- calls the routine on the way up the tree, a quadratic behavior
1969 -- can otherwise be encountered in large expressions. The cache
1970 -- entry for node N is stored in the (N mod Cache_Size) entry, and
1971 -- can be validated by checking the actual node value stored there.
1973 procedure Determine_Range
1979 Typ : constant Entity_Id := Etype (N);
1983 -- Lo and Hi bounds of left operand
1987 -- Lo and Hi bounds of right (or only) operand
1990 -- Temp variable used to hold a bound node
1993 -- High bound of base type of expression
1997 -- Refined values for low and high bounds, after tightening
2000 -- Used in lower level calls to indicate if call succeeded
2002 Cindex : Cache_Index;
2003 -- Used to search cache
2005 function OK_Operands return Boolean;
2006 -- Used for binary operators. Determines the ranges of the left and
2007 -- right operands, and if they are both OK, returns True, and puts
2008 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left
2014 function OK_Operands return Boolean is
2016 Determine_Range (Left_Opnd (N), OK1, Lo_Left, Hi_Left);
2022 Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
2026 -- Start of processing for Determine_Range
2029 -- Prevent junk warnings by initializing range variables
2036 -- If the type is not discrete, or is undefined, then we can't
2037 -- do anything about determining the range.
2039 if No (Typ) or else not Is_Discrete_Type (Typ)
2040 or else Error_Posted (N)
2046 -- For all other cases, we can determine the range
2050 -- If value is compile time known, then the possible range is the
2051 -- one value that we know this expression definitely has!
2053 if Compile_Time_Known_Value (N) then
2054 Lo := Expr_Value (N);
2059 -- Return if already in the cache
2061 Cindex := Cache_Index (N mod Cache_Size);
2063 if Determine_Range_Cache_N (Cindex) = N then
2064 Lo := Determine_Range_Cache_Lo (Cindex);
2065 Hi := Determine_Range_Cache_Hi (Cindex);
2069 -- Otherwise, start by finding the bounds of the type of the
2070 -- expression, the value cannot be outside this range (if it
2071 -- is, then we have an overflow situation, which is a separate
2072 -- check, we are talking here only about the expression value).
2074 -- We use the actual bound unless it is dynamic, in which case
2075 -- use the corresponding base type bound if possible. If we can't
2076 -- get a bound then we figure we can't determine the range (a
2077 -- peculiar case, that perhaps cannot happen, but there is no
2078 -- point in bombing in this optimization circuit.
2080 -- First the low bound
2082 Bound := Type_Low_Bound (Typ);
2084 if Compile_Time_Known_Value (Bound) then
2085 Lo := Expr_Value (Bound);
2087 elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then
2088 Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
2095 -- Now the high bound
2097 Bound := Type_High_Bound (Typ);
2099 -- We need the high bound of the base type later on, and this should
2100 -- always be compile time known. Again, it is not clear that this
2101 -- can ever be false, but no point in bombing.
2103 if Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then
2104 Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ)));
2112 -- If we have a static subtype, then that may have a tighter bound
2113 -- so use the upper bound of the subtype instead in this case.
2115 if Compile_Time_Known_Value (Bound) then
2116 Hi := Expr_Value (Bound);
2119 -- We may be able to refine this value in certain situations. If
2120 -- refinement is possible, then Lor and Hir are set to possibly
2121 -- tighter bounds, and OK1 is set to True.
2125 -- For unary plus, result is limited by range of operand
2128 Determine_Range (Right_Opnd (N), OK1, Lor, Hir);
2130 -- For unary minus, determine range of operand, and negate it
2133 Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
2140 -- For binary addition, get range of each operand and do the
2141 -- addition to get the result range.
2145 Lor := Lo_Left + Lo_Right;
2146 Hir := Hi_Left + Hi_Right;
2149 -- Division is tricky. The only case we consider is where the
2150 -- right operand is a positive constant, and in this case we
2151 -- simply divide the bounds of the left operand
2155 if Lo_Right = Hi_Right
2156 and then Lo_Right > 0
2158 Lor := Lo_Left / Lo_Right;
2159 Hir := Hi_Left / Lo_Right;
2166 -- For binary subtraction, get range of each operand and do
2167 -- the worst case subtraction to get the result range.
2169 when N_Op_Subtract =>
2171 Lor := Lo_Left - Hi_Right;
2172 Hir := Hi_Left - Lo_Right;
2175 -- For MOD, if right operand is a positive constant, then
2176 -- result must be in the allowable range of mod results.
2180 if Lo_Right = Hi_Right then
2181 if Lo_Right > 0 then
2183 Hir := Lo_Right - 1;
2185 elsif Lo_Right < 0 then
2186 Lor := Lo_Right + 1;
2195 -- For REM, if right operand is a positive constant, then
2196 -- result must be in the allowable range of mod results.
2200 if Lo_Right = Hi_Right then
2202 Dval : constant Uint := (abs Lo_Right) - 1;
2205 -- The sign of the result depends on the sign of the
2206 -- dividend (but not on the sign of the divisor, hence
2207 -- the abs operation above).
2227 -- Attribute reference cases
2229 when N_Attribute_Reference =>
2230 case Attribute_Name (N) is
2232 -- For Pos/Val attributes, we can refine the range using the
2233 -- possible range of values of the attribute expression
2235 when Name_Pos | Name_Val =>
2236 Determine_Range (First (Expressions (N)), OK1, Lor, Hir);
2238 -- For Length attribute, use the bounds of the corresponding
2239 -- index type to refine the range.
2243 Atyp : Entity_Id := Etype (Prefix (N));
2251 if Is_Access_Type (Atyp) then
2252 Atyp := Designated_Type (Atyp);
2255 -- For string literal, we know exact value
2257 if Ekind (Atyp) = E_String_Literal_Subtype then
2259 Lo := String_Literal_Length (Atyp);
2260 Hi := String_Literal_Length (Atyp);
2264 -- Otherwise check for expression given
2266 if No (Expressions (N)) then
2270 UI_To_Int (Expr_Value (First (Expressions (N))));
2273 Indx := First_Index (Atyp);
2274 for J in 2 .. Inum loop
2275 Indx := Next_Index (Indx);
2279 (Type_Low_Bound (Etype (Indx)), OK1, LL, LU);
2283 (Type_High_Bound (Etype (Indx)), OK1, UL, UU);
2287 -- The maximum value for Length is the biggest
2288 -- possible gap between the values of the bounds.
2289 -- But of course, this value cannot be negative.
2291 Hir := UI_Max (Uint_0, UU - LL);
2293 -- For constrained arrays, the minimum value for
2294 -- Length is taken from the actual value of the
2295 -- bounds, since the index will be exactly of
2298 if Is_Constrained (Atyp) then
2299 Lor := UI_Max (Uint_0, UL - LU);
2301 -- For an unconstrained array, the minimum value
2302 -- for length is always zero.
2311 -- No special handling for other attributes
2312 -- Probably more opportunities exist here ???
2319 -- For type conversion from one discrete type to another, we
2320 -- can refine the range using the converted value.
2322 when N_Type_Conversion =>
2323 Determine_Range (Expression (N), OK1, Lor, Hir);
2325 -- Nothing special to do for all other expression kinds
2333 -- At this stage, if OK1 is true, then we know that the actual
2334 -- result of the computed expression is in the range Lor .. Hir.
2335 -- We can use this to restrict the possible range of results.
2339 -- If the refined value of the low bound is greater than the
2340 -- type high bound, then reset it to the more restrictive
2341 -- value. However, we do NOT do this for the case of a modular
2342 -- type where the possible upper bound on the value is above the
2343 -- base type high bound, because that means the result could wrap.
2346 and then not (Is_Modular_Integer_Type (Typ)
2347 and then Hir > Hbound)
2352 -- Similarly, if the refined value of the high bound is less
2353 -- than the value so far, then reset it to the more restrictive
2354 -- value. Again, we do not do this if the refined low bound is
2355 -- negative for a modular type, since this would wrap.
2358 and then not (Is_Modular_Integer_Type (Typ)
2359 and then Lor < Uint_0)
2365 -- Set cache entry for future call and we are all done
2367 Determine_Range_Cache_N (Cindex) := N;
2368 Determine_Range_Cache_Lo (Cindex) := Lo;
2369 Determine_Range_Cache_Hi (Cindex) := Hi;
2372 -- If any exception occurs, it means that we have some bug in the compiler
2373 -- possibly triggered by a previous error, or by some unforseen peculiar
2374 -- occurrence. However, this is only an optimization attempt, so there is
2375 -- really no point in crashing the compiler. Instead we just decide, too
2376 -- bad, we can't figure out a range in this case after all.
2381 -- Debug flag K disables this behavior (useful for debugging)
2383 if Debug_Flag_K then
2392 end Determine_Range;
2394 ------------------------------------
2395 -- Discriminant_Checks_Suppressed --
2396 ------------------------------------
2398 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
2400 return Scope_Suppress.Discriminant_Checks
2401 or else (Present (E) and then Suppress_Discriminant_Checks (E));
2402 end Discriminant_Checks_Suppressed;
2404 --------------------------------
2405 -- Division_Checks_Suppressed --
2406 --------------------------------
2408 function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
2410 return Scope_Suppress.Division_Checks
2411 or else (Present (E) and then Suppress_Division_Checks (E));
2412 end Division_Checks_Suppressed;
2414 -----------------------------------
2415 -- Elaboration_Checks_Suppressed --
2416 -----------------------------------
2418 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
2420 return Scope_Suppress.Elaboration_Checks
2421 or else (Present (E) and then Suppress_Elaboration_Checks (E));
2422 end Elaboration_Checks_Suppressed;
2424 ------------------------
2425 -- Enable_Range_Check --
2426 ------------------------
2428 procedure Enable_Range_Check (N : Node_Id) is
2430 if Nkind (N) = N_Unchecked_Type_Conversion
2431 and then Kill_Range_Check (N)
2435 Set_Do_Range_Check (N, True);
2437 end Enable_Range_Check;
2443 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is
2444 Typ : constant Entity_Id := Etype (Expr);
2447 -- Ignore call if we are not doing any validity checking
2449 if not Validity_Checks_On then
2452 -- No check required if expression is from the expander, we assume
2453 -- the expander will generate whatever checks are needed. Note that
2454 -- this is not just an optimization, it avoids infinite recursions!
2456 -- Unchecked conversions must be checked, unless they are initialized
2457 -- scalar values, as in a component assignment in an init_proc.
2459 elsif not Comes_From_Source (Expr)
2460 and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
2461 or else Kill_Range_Check (Expr))
2465 -- No check required if expression is known to have valid value
2467 elsif Expr_Known_Valid (Expr) then
2470 -- No check required if checks off
2472 elsif Range_Checks_Suppressed (Typ) then
2475 -- Ignore case of enumeration with holes where the flag is set not
2476 -- to worry about holes, since no special validity check is needed
2478 elsif Is_Enumeration_Type (Typ)
2479 and then Has_Non_Standard_Rep (Typ)
2484 -- No check required on the left-hand side of an assignment.
2486 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
2487 and then Expr = Name (Parent (Expr))
2491 -- An annoying special case. If this is an out parameter of a scalar
2492 -- type, then the value is not going to be accessed, therefore it is
2493 -- inappropriate to do any validity check at the call site.
2496 -- Only need to worry about scalar types
2498 if Is_Scalar_Type (Typ) then
2508 -- Find actual argument (which may be a parameter association)
2509 -- and the parent of the actual argument (the call statement)
2514 if Nkind (P) = N_Parameter_Association then
2519 -- Only need to worry if we are argument of a procedure
2520 -- call since functions don't have out parameters.
2522 if Nkind (P) = N_Procedure_Call_Statement then
2523 L := Parameter_Associations (P);
2524 E := Entity (Name (P));
2526 -- Only need to worry if there are indeed actuals, and
2527 -- if this could be a procedure call, otherwise we cannot
2528 -- get a match (either we are not an argument, or the
2529 -- mode of the formal is not OUT). This test also filters
2530 -- out the generic case.
2532 if Is_Non_Empty_List (L)
2533 and then Is_Subprogram (E)
2535 -- This is the loop through parameters, looking to
2536 -- see if there is an OUT parameter for which we are
2539 F := First_Formal (E);
2542 while Present (F) loop
2543 if Ekind (F) = E_Out_Parameter and then A = N then
2556 -- If we fall through, a validity check is required. Note that it would
2557 -- not be good to set Do_Range_Check, even in contexts where this is
2558 -- permissible, since this flag causes checking against the target type,
2559 -- not the source type in contexts such as assignments
2561 Insert_Valid_Check (Expr);
2564 ----------------------
2565 -- Expr_Known_Valid --
2566 ----------------------
2568 function Expr_Known_Valid (Expr : Node_Id) return Boolean is
2569 Typ : constant Entity_Id := Etype (Expr);
2572 -- Non-scalar types are always consdered valid, since they never
2573 -- give rise to the issues of erroneous or bounded error behavior
2574 -- that are the concern. In formal reference manual terms the
2575 -- notion of validity only applies to scalar types.
2577 if not Is_Scalar_Type (Typ) then
2580 -- If no validity checking, then everything is considered valid
2582 elsif not Validity_Checks_On then
2585 -- Floating-point types are considered valid unless floating-point
2586 -- validity checks have been specifically turned on.
2588 elsif Is_Floating_Point_Type (Typ)
2589 and then not Validity_Check_Floating_Point
2593 -- If the expression is the value of an object that is known to
2594 -- be valid, then clearly the expression value itself is valid.
2596 elsif Is_Entity_Name (Expr)
2597 and then Is_Known_Valid (Entity (Expr))
2601 -- If the type is one for which all values are known valid, then
2602 -- we are sure that the value is valid except in the slightly odd
2603 -- case where the expression is a reference to a variable whose size
2604 -- has been explicitly set to a value greater than the object size.
2606 elsif Is_Known_Valid (Typ) then
2607 if Is_Entity_Name (Expr)
2608 and then Ekind (Entity (Expr)) = E_Variable
2609 and then Esize (Entity (Expr)) > Esize (Typ)
2616 -- Integer and character literals always have valid values, where
2617 -- appropriate these will be range checked in any case.
2619 elsif Nkind (Expr) = N_Integer_Literal
2621 Nkind (Expr) = N_Character_Literal
2625 -- If we have a type conversion or a qualification of a known valid
2626 -- value, then the result will always be valid.
2628 elsif Nkind (Expr) = N_Type_Conversion
2630 Nkind (Expr) = N_Qualified_Expression
2632 return Expr_Known_Valid (Expression (Expr));
2634 -- The result of any function call or operator is always considered
2635 -- valid, since we assume the necessary checks are done by the call.
2637 elsif Nkind (Expr) in N_Binary_Op
2639 Nkind (Expr) in N_Unary_Op
2641 Nkind (Expr) = N_Function_Call
2645 -- For all other cases, we do not know the expression is valid
2650 end Expr_Known_Valid;
2652 ---------------------
2653 -- Get_Discriminal --
2654 ---------------------
2656 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
2657 Loc : constant Source_Ptr := Sloc (E);
2662 -- The entity E is the type of a private component of the protected
2663 -- type, or the type of a renaming of that component within a protected
2664 -- operation of that type.
2668 if Ekind (Sc) /= E_Protected_Type then
2671 if Ekind (Sc) /= E_Protected_Type then
2676 D := First_Discriminant (Sc);
2679 and then Chars (D) /= Chars (Bound)
2681 Next_Discriminant (D);
2684 return New_Occurrence_Of (Discriminal (D), Loc);
2685 end Get_Discriminal;
2691 function Guard_Access
2698 if Nkind (Cond) = N_Or_Else then
2699 Set_Paren_Count (Cond, 1);
2702 if Nkind (Ck_Node) = N_Allocator then
2709 Left_Opnd => Duplicate_Subexpr (Ck_Node),
2710 Right_Opnd => Make_Null (Loc)),
2711 Right_Opnd => Cond);
2715 -----------------------------
2716 -- Index_Checks_Suppressed --
2717 -----------------------------
2719 function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
2721 return Scope_Suppress.Index_Checks
2722 or else (Present (E) and then Suppress_Index_Checks (E));
2723 end Index_Checks_Suppressed;
2729 procedure Initialize is
2731 for J in Determine_Range_Cache_N'Range loop
2732 Determine_Range_Cache_N (J) := Empty;
2736 -------------------------
2737 -- Insert_Range_Checks --
2738 -------------------------
2740 procedure Insert_Range_Checks
2741 (Checks : Check_Result;
2743 Suppress_Typ : Entity_Id;
2744 Static_Sloc : Source_Ptr := No_Location;
2745 Flag_Node : Node_Id := Empty;
2746 Do_Before : Boolean := False)
2748 Internal_Flag_Node : Node_Id := Flag_Node;
2749 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
2751 Check_Node : Node_Id;
2752 Checks_On : constant Boolean :=
2753 (not Index_Checks_Suppressed (Suppress_Typ))
2755 (not Range_Checks_Suppressed (Suppress_Typ));
2758 -- For now we just return if Checks_On is false, however this should
2759 -- be enhanced to check for an always True value in the condition
2760 -- and to generate a compilation warning???
2762 if not Expander_Active or else not Checks_On then
2766 if Static_Sloc = No_Location then
2767 Internal_Static_Sloc := Sloc (Node);
2770 if No (Flag_Node) then
2771 Internal_Flag_Node := Node;
2774 for J in 1 .. 2 loop
2775 exit when No (Checks (J));
2777 if Nkind (Checks (J)) = N_Raise_Constraint_Error
2778 and then Present (Condition (Checks (J)))
2780 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
2781 Check_Node := Checks (J);
2782 Mark_Rewrite_Insertion (Check_Node);
2785 Insert_Before_And_Analyze (Node, Check_Node);
2787 Insert_After_And_Analyze (Node, Check_Node);
2790 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
2795 Make_Raise_Constraint_Error (Internal_Static_Sloc,
2796 Reason => CE_Range_Check_Failed);
2797 Mark_Rewrite_Insertion (Check_Node);
2800 Insert_Before_And_Analyze (Node, Check_Node);
2802 Insert_After_And_Analyze (Node, Check_Node);
2806 end Insert_Range_Checks;
2808 ------------------------
2809 -- Insert_Valid_Check --
2810 ------------------------
2812 procedure Insert_Valid_Check (Expr : Node_Id) is
2813 Loc : constant Source_Ptr := Sloc (Expr);
2817 -- Do not insert if checks off, or if not checking validity
2819 if Range_Checks_Suppressed (Etype (Expr))
2820 or else (not Validity_Checks_On)
2825 -- If we have a checked conversion, then validity check applies to
2826 -- the expression inside the conversion, not the result, since if
2827 -- the expression inside is valid, then so is the conversion result.
2830 while Nkind (Exp) = N_Type_Conversion loop
2831 Exp := Expression (Exp);
2834 -- Insert the validity check. Note that we do this with validity
2835 -- checks turned off, to avoid recursion, we do not want validity
2836 -- checks on the validity checking code itself!
2838 Validity_Checks_On := False;
2841 Make_Raise_Constraint_Error (Loc,
2845 Make_Attribute_Reference (Loc,
2847 Duplicate_Subexpr (Exp, Name_Req => True),
2848 Attribute_Name => Name_Valid)),
2849 Reason => CE_Invalid_Data),
2850 Suppress => All_Checks);
2851 Validity_Checks_On := True;
2852 end Insert_Valid_Check;
2854 --------------------------
2855 -- Install_Static_Check --
2856 --------------------------
2858 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
2859 Stat : constant Boolean := Is_Static_Expression (R_Cno);
2860 Typ : constant Entity_Id := Etype (R_Cno);
2864 Make_Raise_Constraint_Error (Loc,
2865 Reason => CE_Range_Check_Failed));
2866 Set_Analyzed (R_Cno);
2867 Set_Etype (R_Cno, Typ);
2868 Set_Raises_Constraint_Error (R_Cno);
2869 Set_Is_Static_Expression (R_Cno, Stat);
2870 end Install_Static_Check;
2872 ------------------------------
2873 -- Length_Checks_Suppressed --
2874 ------------------------------
2876 function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
2878 return Scope_Suppress.Length_Checks
2879 or else (Present (E) and then Suppress_Length_Checks (E));
2880 end Length_Checks_Suppressed;
2882 --------------------------------
2883 -- Overflow_Checks_Suppressed --
2884 --------------------------------
2886 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
2888 return Scope_Suppress.Overflow_Checks
2889 or else (Present (E) and then Suppress_Overflow_Checks (E));
2890 end Overflow_Checks_Suppressed;
2896 function Range_Check
2898 Target_Typ : Entity_Id;
2899 Source_Typ : Entity_Id := Empty;
2900 Warn_Node : Node_Id := Empty)
2904 return Selected_Range_Checks
2905 (Ck_Node, Target_Typ, Source_Typ, Warn_Node);
2908 -----------------------------
2909 -- Range_Checks_Suppressed --
2910 -----------------------------
2912 function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
2914 -- Note: for now we always suppress range checks on Vax float types,
2915 -- since Gigi does not know how to generate these checks.
2917 return Scope_Suppress.Range_Checks
2918 or else (Present (E) and then Suppress_Range_Checks (E))
2919 or else Vax_Float (E);
2920 end Range_Checks_Suppressed;
2922 ----------------------------
2923 -- Selected_Length_Checks --
2924 ----------------------------
2926 function Selected_Length_Checks
2928 Target_Typ : Entity_Id;
2929 Source_Typ : Entity_Id;
2930 Warn_Node : Node_Id)
2933 Loc : constant Source_Ptr := Sloc (Ck_Node);
2936 Expr_Actual : Node_Id;
2938 Cond : Node_Id := Empty;
2939 Do_Access : Boolean := False;
2940 Wnode : Node_Id := Warn_Node;
2941 Ret_Result : Check_Result := (Empty, Empty);
2942 Num_Checks : Natural := 0;
2944 procedure Add_Check (N : Node_Id);
2945 -- Adds the action given to Ret_Result if N is non-Empty
2947 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
2948 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
2950 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
2951 -- True for equal literals and for nodes that denote the same constant
2952 -- entity, even if its value is not a static constant. This includes the
2953 -- case of a discriminal reference within an init_proc. Removes some
2954 -- obviously superfluous checks.
2956 function Length_E_Cond
2957 (Exptyp : Entity_Id;
2961 -- Returns expression to compute:
2962 -- Typ'Length /= Exptyp'Length
2964 function Length_N_Cond
2969 -- Returns expression to compute:
2970 -- Typ'Length /= Expr'Length
2976 procedure Add_Check (N : Node_Id) is
2980 -- For now, ignore attempt to place more than 2 checks ???
2982 if Num_Checks = 2 then
2986 pragma Assert (Num_Checks <= 1);
2987 Num_Checks := Num_Checks + 1;
2988 Ret_Result (Num_Checks) := N;
2996 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
2998 E1 : Entity_Id := E;
2999 Pt : Entity_Id := Scope (Scope (E));
3002 if Ekind (Scope (E)) = E_Record_Type
3003 and then Has_Discriminants (Scope (E))
3005 N := Build_Discriminal_Subtype_Of_Component (E);
3008 Insert_Action (Ck_Node, N);
3009 E1 := Defining_Identifier (N);
3013 if Ekind (E1) = E_String_Literal_Subtype then
3015 Make_Integer_Literal (Loc,
3016 Intval => String_Literal_Length (E1));
3018 elsif Ekind (Pt) = E_Protected_Type
3019 and then Has_Discriminants (Pt)
3020 and then Has_Completion (Pt)
3021 and then not Inside_Init_Proc
3024 -- If the type whose length is needed is a private component
3025 -- constrained by a discriminant, we must expand the 'Length
3026 -- attribute into an explicit computation, using the discriminal
3027 -- of the current protected operation. This is because the actual
3028 -- type of the prival is constructed after the protected opera-
3029 -- tion has been fully expanded.
3032 Indx_Type : Node_Id;
3035 Do_Expand : Boolean := False;
3038 Indx_Type := First_Index (E);
3040 for J in 1 .. Indx - 1 loop
3041 Next_Index (Indx_Type);
3044 Get_Index_Bounds (Indx_Type, Lo, Hi);
3046 if Nkind (Lo) = N_Identifier
3047 and then Ekind (Entity (Lo)) = E_In_Parameter
3049 Lo := Get_Discriminal (E, Lo);
3053 if Nkind (Hi) = N_Identifier
3054 and then Ekind (Entity (Hi)) = E_In_Parameter
3056 Hi := Get_Discriminal (E, Hi);
3061 if not Is_Entity_Name (Lo) then
3062 Lo := Duplicate_Subexpr (Lo);
3065 if not Is_Entity_Name (Hi) then
3066 Lo := Duplicate_Subexpr (Hi);
3072 Make_Op_Subtract (Loc,
3076 Right_Opnd => Make_Integer_Literal (Loc, 1));
3081 Make_Attribute_Reference (Loc,
3082 Attribute_Name => Name_Length,
3084 New_Occurrence_Of (E1, Loc));
3087 Set_Expressions (N, New_List (
3088 Make_Integer_Literal (Loc, Indx)));
3097 Make_Attribute_Reference (Loc,
3098 Attribute_Name => Name_Length,
3100 New_Occurrence_Of (E1, Loc));
3103 Set_Expressions (N, New_List (
3104 Make_Integer_Literal (Loc, Indx)));
3116 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
3119 Make_Attribute_Reference (Loc,
3120 Attribute_Name => Name_Length,
3122 Duplicate_Subexpr (N, Name_Req => True),
3123 Expressions => New_List (
3124 Make_Integer_Literal (Loc, Indx)));
3132 function Length_E_Cond
3133 (Exptyp : Entity_Id;
3141 Left_Opnd => Get_E_Length (Typ, Indx),
3142 Right_Opnd => Get_E_Length (Exptyp, Indx));
3150 function Length_N_Cond
3159 Left_Opnd => Get_E_Length (Typ, Indx),
3160 Right_Opnd => Get_N_Length (Expr, Indx));
3164 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
3167 (Nkind (L) = N_Integer_Literal
3168 and then Nkind (R) = N_Integer_Literal
3169 and then Intval (L) = Intval (R))
3173 and then Ekind (Entity (L)) = E_Constant
3174 and then ((Is_Entity_Name (R)
3175 and then Entity (L) = Entity (R))
3177 (Nkind (R) = N_Type_Conversion
3178 and then Is_Entity_Name (Expression (R))
3179 and then Entity (L) = Entity (Expression (R)))))
3183 and then Ekind (Entity (R)) = E_Constant
3184 and then Nkind (L) = N_Type_Conversion
3185 and then Is_Entity_Name (Expression (L))
3186 and then Entity (R) = Entity (Expression (L)))
3190 and then Is_Entity_Name (R)
3191 and then Entity (L) = Entity (R)
3192 and then Ekind (Entity (L)) = E_In_Parameter
3193 and then Inside_Init_Proc);
3196 -- Start of processing for Selected_Length_Checks
3199 if not Expander_Active then
3203 if Target_Typ = Any_Type
3204 or else Target_Typ = Any_Composite
3205 or else Raises_Constraint_Error (Ck_Node)
3214 T_Typ := Target_Typ;
3216 if No (Source_Typ) then
3217 S_Typ := Etype (Ck_Node);
3219 S_Typ := Source_Typ;
3222 if S_Typ = Any_Type or else S_Typ = Any_Composite then
3226 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
3227 S_Typ := Designated_Type (S_Typ);
3228 T_Typ := Designated_Type (T_Typ);
3231 -- A simple optimization
3233 if Nkind (Ck_Node) = N_Null then
3238 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
3239 if Is_Constrained (T_Typ) then
3241 -- The checking code to be generated will freeze the
3242 -- corresponding array type. However, we must freeze the
3243 -- type now, so that the freeze node does not appear within
3244 -- the generated condional expression, but ahead of it.
3246 Freeze_Before (Ck_Node, T_Typ);
3248 Expr_Actual := Get_Referenced_Object (Ck_Node);
3249 Exptyp := Get_Actual_Subtype (Expr_Actual);
3251 if Is_Access_Type (Exptyp) then
3252 Exptyp := Designated_Type (Exptyp);
3255 -- String_Literal case. This needs to be handled specially be-
3256 -- cause no index types are available for string literals. The
3257 -- condition is simply:
3259 -- T_Typ'Length = string-literal-length
3261 if Nkind (Expr_Actual) = N_String_Literal then
3264 Left_Opnd => Get_E_Length (T_Typ, 1),
3266 Make_Integer_Literal (Loc,
3268 String_Literal_Length (Etype (Expr_Actual))));
3270 -- General array case. Here we have a usable actual subtype for
3271 -- the expression, and the condition is built from the two types
3274 -- T_Typ'Length /= Exptyp'Length or else
3275 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
3276 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
3279 elsif Is_Constrained (Exptyp) then
3283 Ndims : Nat := Number_Dimensions (T_Typ);
3294 L_Index := First_Index (T_Typ);
3295 R_Index := First_Index (Exptyp);
3297 for Indx in 1 .. Ndims loop
3298 if not (Nkind (L_Index) = N_Raise_Constraint_Error
3300 Nkind (R_Index) = N_Raise_Constraint_Error)
3302 Get_Index_Bounds (L_Index, L_Low, L_High);
3303 Get_Index_Bounds (R_Index, R_Low, R_High);
3305 -- Deal with compile time length check. Note that we
3306 -- skip this in the access case, because the access
3307 -- value may be null, so we cannot know statically.
3310 and then Compile_Time_Known_Value (L_Low)
3311 and then Compile_Time_Known_Value (L_High)
3312 and then Compile_Time_Known_Value (R_Low)
3313 and then Compile_Time_Known_Value (R_High)
3315 if Expr_Value (L_High) >= Expr_Value (L_Low) then
3316 L_Length := Expr_Value (L_High) -
3317 Expr_Value (L_Low) + 1;
3319 L_Length := UI_From_Int (0);
3322 if Expr_Value (R_High) >= Expr_Value (R_Low) then
3323 R_Length := Expr_Value (R_High) -
3324 Expr_Value (R_Low) + 1;
3326 R_Length := UI_From_Int (0);
3329 if L_Length > R_Length then
3331 (Compile_Time_Constraint_Error
3332 (Wnode, "too few elements for}?", T_Typ));
3334 elsif L_Length < R_Length then
3336 (Compile_Time_Constraint_Error
3337 (Wnode, "too many elements for}?", T_Typ));
3340 -- The comparison for an individual index subtype
3341 -- is omitted if the corresponding index subtypes
3342 -- statically match, since the result is known to
3343 -- be true. Note that this test is worth while even
3344 -- though we do static evaluation, because non-static
3345 -- subtypes can statically match.
3348 Subtypes_Statically_Match
3349 (Etype (L_Index), Etype (R_Index))
3352 (Same_Bounds (L_Low, R_Low)
3353 and then Same_Bounds (L_High, R_High))
3356 (Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
3365 -- Handle cases where we do not get a usable actual subtype that
3366 -- is constrained. This happens for example in the function call
3367 -- and explicit dereference cases. In these cases, we have to get
3368 -- the length or range from the expression itself, making sure we
3369 -- do not evaluate it more than once.
3371 -- Here Ck_Node is the original expression, or more properly the
3372 -- result of applying Duplicate_Expr to the original tree,
3373 -- forcing the result to be a name.
3377 Ndims : Nat := Number_Dimensions (T_Typ);
3380 -- Build the condition for the explicit dereference case
3382 for Indx in 1 .. Ndims loop
3384 (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
3391 -- Construct the test and insert into the tree
3393 if Present (Cond) then
3395 Cond := Guard_Access (Cond, Loc, Ck_Node);
3399 (Make_Raise_Constraint_Error (Loc,
3401 Reason => CE_Length_Check_Failed));
3405 end Selected_Length_Checks;
3407 ---------------------------
3408 -- Selected_Range_Checks --
3409 ---------------------------
3411 function Selected_Range_Checks
3413 Target_Typ : Entity_Id;
3414 Source_Typ : Entity_Id;
3415 Warn_Node : Node_Id)
3418 Loc : constant Source_Ptr := Sloc (Ck_Node);
3421 Expr_Actual : Node_Id;
3423 Cond : Node_Id := Empty;
3424 Do_Access : Boolean := False;
3425 Wnode : Node_Id := Warn_Node;
3426 Ret_Result : Check_Result := (Empty, Empty);
3427 Num_Checks : Integer := 0;
3429 procedure Add_Check (N : Node_Id);
3430 -- Adds the action given to Ret_Result if N is non-Empty
3432 function Discrete_Range_Cond
3436 -- Returns expression to compute:
3437 -- Low_Bound (Expr) < Typ'First
3439 -- High_Bound (Expr) > Typ'Last
3441 function Discrete_Expr_Cond
3445 -- Returns expression to compute:
3450 function Get_E_First_Or_Last
3455 -- Returns expression to compute:
3456 -- E'First or E'Last
3458 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
3459 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
3460 -- Returns expression to compute:
3461 -- N'First or N'Last using Duplicate_Subexpr
3463 function Range_E_Cond
3464 (Exptyp : Entity_Id;
3468 -- Returns expression to compute:
3469 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
3471 function Range_Equal_E_Cond
3472 (Exptyp : Entity_Id;
3476 -- Returns expression to compute:
3477 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
3479 function Range_N_Cond
3484 -- Return expression to compute:
3485 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
3491 procedure Add_Check (N : Node_Id) is
3495 -- For now, ignore attempt to place more than 2 checks ???
3497 if Num_Checks = 2 then
3501 pragma Assert (Num_Checks <= 1);
3502 Num_Checks := Num_Checks + 1;
3503 Ret_Result (Num_Checks) := N;
3507 -------------------------
3508 -- Discrete_Expr_Cond --
3509 -------------------------
3511 function Discrete_Expr_Cond
3522 Convert_To (Base_Type (Typ), Duplicate_Subexpr (Expr)),
3524 Convert_To (Base_Type (Typ),
3525 Get_E_First_Or_Last (Typ, 0, Name_First))),
3530 Convert_To (Base_Type (Typ), Duplicate_Subexpr (Expr)),
3534 Get_E_First_Or_Last (Typ, 0, Name_Last))));
3535 end Discrete_Expr_Cond;
3537 -------------------------
3538 -- Discrete_Range_Cond --
3539 -------------------------
3541 function Discrete_Range_Cond
3546 LB : Node_Id := Low_Bound (Expr);
3547 HB : Node_Id := High_Bound (Expr);
3549 Left_Opnd : Node_Id;
3550 Right_Opnd : Node_Id;
3553 if Nkind (LB) = N_Identifier
3554 and then Ekind (Entity (LB)) = E_Discriminant then
3555 LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
3558 if Nkind (HB) = N_Identifier
3559 and then Ekind (Entity (HB)) = E_Discriminant then
3560 HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
3567 (Base_Type (Typ), Duplicate_Subexpr (LB)),
3571 (Base_Type (Typ), Get_E_First_Or_Last (Typ, 0, Name_First)));
3573 if Base_Type (Typ) = Typ then
3576 elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ)))
3578 Compile_Time_Known_Value (High_Bound (Scalar_Range
3581 if Is_Floating_Point_Type (Typ) then
3582 if Expr_Value_R (High_Bound (Scalar_Range (Typ))) =
3583 Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ))))
3589 if Expr_Value (High_Bound (Scalar_Range (Typ))) =
3590 Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ))))
3601 (Base_Type (Typ), Duplicate_Subexpr (HB)),
3606 Get_E_First_Or_Last (Typ, 0, Name_Last)));
3608 return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
3609 end Discrete_Range_Cond;
3611 -------------------------
3612 -- Get_E_First_Or_Last --
3613 -------------------------
3615 function Get_E_First_Or_Last
3627 if Is_Array_Type (E) then
3628 N := First_Index (E);
3630 for J in 2 .. Indx loop
3635 N := Scalar_Range (E);
3638 if Nkind (N) = N_Subtype_Indication then
3639 LB := Low_Bound (Range_Expression (Constraint (N)));
3640 HB := High_Bound (Range_Expression (Constraint (N)));
3642 elsif Is_Entity_Name (N) then
3643 LB := Type_Low_Bound (Etype (N));
3644 HB := Type_High_Bound (Etype (N));
3647 LB := Low_Bound (N);
3648 HB := High_Bound (N);
3651 if Nam = Name_First then
3657 if Nkind (Bound) = N_Identifier
3658 and then Ekind (Entity (Bound)) = E_Discriminant
3660 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
3662 elsif Nkind (Bound) = N_Identifier
3663 and then Ekind (Entity (Bound)) = E_In_Parameter
3664 and then not Inside_Init_Proc
3666 return Get_Discriminal (E, Bound);
3668 elsif Nkind (Bound) = N_Integer_Literal then
3669 return Make_Integer_Literal (Loc, Intval (Bound));
3672 return Duplicate_Subexpr (Bound);
3674 end Get_E_First_Or_Last;
3680 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
3683 Make_Attribute_Reference (Loc,
3684 Attribute_Name => Name_First,
3686 Duplicate_Subexpr (N, Name_Req => True),
3687 Expressions => New_List (
3688 Make_Integer_Literal (Loc, Indx)));
3696 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
3699 Make_Attribute_Reference (Loc,
3700 Attribute_Name => Name_Last,
3702 Duplicate_Subexpr (N, Name_Req => True),
3703 Expressions => New_List (
3704 Make_Integer_Literal (Loc, Indx)));
3712 function Range_E_Cond
3713 (Exptyp : Entity_Id;
3723 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
3724 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3728 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
3729 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3733 ------------------------
3734 -- Range_Equal_E_Cond --
3735 ------------------------
3737 function Range_Equal_E_Cond
3738 (Exptyp : Entity_Id;
3748 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
3749 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3752 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
3753 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3754 end Range_Equal_E_Cond;
3760 function Range_N_Cond
3771 Left_Opnd => Get_N_First (Expr, Indx),
3772 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3776 Left_Opnd => Get_N_Last (Expr, Indx),
3777 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3780 -- Start of processing for Selected_Range_Checks
3783 if not Expander_Active then
3787 if Target_Typ = Any_Type
3788 or else Target_Typ = Any_Composite
3789 or else Raises_Constraint_Error (Ck_Node)
3798 T_Typ := Target_Typ;
3800 if No (Source_Typ) then
3801 S_Typ := Etype (Ck_Node);
3803 S_Typ := Source_Typ;
3806 if S_Typ = Any_Type or else S_Typ = Any_Composite then
3810 -- The order of evaluating T_Typ before S_Typ seems to be critical
3811 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
3812 -- in, and since Node can be an N_Range node, it might be invalid.
3813 -- Should there be an assert check somewhere for taking the Etype of
3814 -- an N_Range node ???
3816 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
3817 S_Typ := Designated_Type (S_Typ);
3818 T_Typ := Designated_Type (T_Typ);
3821 -- A simple optimization
3823 if Nkind (Ck_Node) = N_Null then
3828 -- For an N_Range Node, check for a null range and then if not
3829 -- null generate a range check action.
3831 if Nkind (Ck_Node) = N_Range then
3833 -- There's no point in checking a range against itself
3835 if Ck_Node = Scalar_Range (T_Typ) then
3840 T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
3841 T_HB : constant Node_Id := Type_High_Bound (T_Typ);
3842 LB : constant Node_Id := Low_Bound (Ck_Node);
3843 HB : constant Node_Id := High_Bound (Ck_Node);
3844 Null_Range : Boolean;
3846 Out_Of_Range_L : Boolean;
3847 Out_Of_Range_H : Boolean;
3850 -- Check for case where everything is static and we can
3851 -- do the check at compile time. This is skipped if we
3852 -- have an access type, since the access value may be null.
3854 -- ??? This code can be improved since you only need to know
3855 -- that the two respective bounds (LB & T_LB or HB & T_HB)
3856 -- are known at compile time to emit pertinent messages.
3858 if Compile_Time_Known_Value (LB)
3859 and then Compile_Time_Known_Value (HB)
3860 and then Compile_Time_Known_Value (T_LB)
3861 and then Compile_Time_Known_Value (T_HB)
3862 and then not Do_Access
3864 -- Floating-point case
3866 if Is_Floating_Point_Type (S_Typ) then
3867 Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
3869 (Expr_Value_R (LB) < Expr_Value_R (T_LB))
3871 (Expr_Value_R (LB) > Expr_Value_R (T_HB));
3874 (Expr_Value_R (HB) > Expr_Value_R (T_HB))
3876 (Expr_Value_R (HB) < Expr_Value_R (T_LB));
3878 -- Fixed or discrete type case
3881 Null_Range := Expr_Value (HB) < Expr_Value (LB);
3883 (Expr_Value (LB) < Expr_Value (T_LB))
3885 (Expr_Value (LB) > Expr_Value (T_HB));
3888 (Expr_Value (HB) > Expr_Value (T_HB))
3890 (Expr_Value (HB) < Expr_Value (T_LB));
3893 if not Null_Range then
3894 if Out_Of_Range_L then
3895 if No (Warn_Node) then
3897 (Compile_Time_Constraint_Error
3898 (Low_Bound (Ck_Node),
3899 "static value out of range of}?", T_Typ));
3903 (Compile_Time_Constraint_Error
3905 "static range out of bounds of}?", T_Typ));
3909 if Out_Of_Range_H then
3910 if No (Warn_Node) then
3912 (Compile_Time_Constraint_Error
3913 (High_Bound (Ck_Node),
3914 "static value out of range of}?", T_Typ));
3918 (Compile_Time_Constraint_Error
3920 "static range out of bounds of}?", T_Typ));
3928 LB : Node_Id := Low_Bound (Ck_Node);
3929 HB : Node_Id := High_Bound (Ck_Node);
3933 -- If either bound is a discriminant and we are within
3934 -- the record declaration, it is a use of the discriminant
3935 -- in a constraint of a component, and nothing can be
3936 -- checked here. The check will be emitted within the
3937 -- init_proc. Before then, the discriminal has no real
3940 if Nkind (LB) = N_Identifier
3941 and then Ekind (Entity (LB)) = E_Discriminant
3943 if Current_Scope = Scope (Entity (LB)) then
3947 New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
3951 if Nkind (HB) = N_Identifier
3952 and then Ekind (Entity (HB)) = E_Discriminant
3954 if Current_Scope = Scope (Entity (HB)) then
3958 New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
3962 Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
3963 Set_Paren_Count (Cond, 1);
3969 Left_Opnd => Duplicate_Subexpr (HB),
3970 Right_Opnd => Duplicate_Subexpr (LB)),
3971 Right_Opnd => Cond);
3977 elsif Is_Scalar_Type (S_Typ) then
3979 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
3980 -- except the above simply sets a flag in the node and lets
3981 -- gigi generate the check base on the Etype of the expression.
3982 -- Sometimes, however we want to do a dynamic check against an
3983 -- arbitrary target type, so we do that here.
3985 if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
3986 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
3988 -- For literals, we can tell if the constraint error will be
3989 -- raised at compile time, so we never need a dynamic check, but
3990 -- if the exception will be raised, then post the usual warning,
3991 -- and replace the literal with a raise constraint error
3992 -- expression. As usual, skip this for access types
3994 elsif Compile_Time_Known_Value (Ck_Node)
3995 and then not Do_Access
3998 LB : constant Node_Id := Type_Low_Bound (T_Typ);
3999 UB : constant Node_Id := Type_High_Bound (T_Typ);
4001 Out_Of_Range : Boolean;
4002 Static_Bounds : constant Boolean :=
4003 Compile_Time_Known_Value (LB)
4004 and Compile_Time_Known_Value (UB);
4007 -- Following range tests should use Sem_Eval routine ???
4009 if Static_Bounds then
4010 if Is_Floating_Point_Type (S_Typ) then
4012 (Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
4014 (Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
4016 else -- fixed or discrete type
4018 Expr_Value (Ck_Node) < Expr_Value (LB)
4020 Expr_Value (Ck_Node) > Expr_Value (UB);
4023 -- Bounds of the type are static and the literal is
4024 -- out of range so make a warning message.
4026 if Out_Of_Range then
4027 if No (Warn_Node) then
4029 (Compile_Time_Constraint_Error
4031 "static value out of range of}?", T_Typ));
4035 (Compile_Time_Constraint_Error
4037 "static value out of range of}?", T_Typ));
4042 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
4046 -- Here for the case of a non-static expression, we need a runtime
4047 -- check unless the source type range is guaranteed to be in the
4048 -- range of the target type.
4051 if not In_Subrange_Of (S_Typ, T_Typ) then
4052 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
4057 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
4058 if Is_Constrained (T_Typ) then
4060 Expr_Actual := Get_Referenced_Object (Ck_Node);
4061 Exptyp := Get_Actual_Subtype (Expr_Actual);
4063 if Is_Access_Type (Exptyp) then
4064 Exptyp := Designated_Type (Exptyp);
4067 -- String_Literal case. This needs to be handled specially be-
4068 -- cause no index types are available for string literals. The
4069 -- condition is simply:
4071 -- T_Typ'Length = string-literal-length
4073 if Nkind (Expr_Actual) = N_String_Literal then
4076 -- General array case. Here we have a usable actual subtype for
4077 -- the expression, and the condition is built from the two types
4079 -- T_Typ'First < Exptyp'First or else
4080 -- T_Typ'Last > Exptyp'Last or else
4081 -- T_Typ'First(1) < Exptyp'First(1) or else
4082 -- T_Typ'Last(1) > Exptyp'Last(1) or else
4085 elsif Is_Constrained (Exptyp) then
4089 Ndims : Nat := Number_Dimensions (T_Typ);
4097 L_Index := First_Index (T_Typ);
4098 R_Index := First_Index (Exptyp);
4100 for Indx in 1 .. Ndims loop
4101 if not (Nkind (L_Index) = N_Raise_Constraint_Error
4103 Nkind (R_Index) = N_Raise_Constraint_Error)
4105 Get_Index_Bounds (L_Index, L_Low, L_High);
4106 Get_Index_Bounds (R_Index, R_Low, R_High);
4108 -- Deal with compile time length check. Note that we
4109 -- skip this in the access case, because the access
4110 -- value may be null, so we cannot know statically.
4113 Subtypes_Statically_Match
4114 (Etype (L_Index), Etype (R_Index))
4116 -- If the target type is constrained then we
4117 -- have to check for exact equality of bounds
4118 -- (required for qualified expressions).
4120 if Is_Constrained (T_Typ) then
4123 Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
4127 (Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
4138 -- Handle cases where we do not get a usable actual subtype that
4139 -- is constrained. This happens for example in the function call
4140 -- and explicit dereference cases. In these cases, we have to get
4141 -- the length or range from the expression itself, making sure we
4142 -- do not evaluate it more than once.
4144 -- Here Ck_Node is the original expression, or more properly the
4145 -- result of applying Duplicate_Expr to the original tree,
4146 -- forcing the result to be a name.
4150 Ndims : Nat := Number_Dimensions (T_Typ);
4153 -- Build the condition for the explicit dereference case
4155 for Indx in 1 .. Ndims loop
4157 (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
4164 -- Generate an Action to check that the bounds of the
4165 -- source value are within the constraints imposed by the
4166 -- target type for a conversion to an unconstrained type.
4169 if Nkind (Parent (Ck_Node)) = N_Type_Conversion then
4171 Opnd_Index : Node_Id;
4172 Targ_Index : Node_Id;
4176 := First_Index (Get_Actual_Subtype (Ck_Node));
4177 Targ_Index := First_Index (T_Typ);
4179 while Opnd_Index /= Empty loop
4180 if Nkind (Opnd_Index) = N_Range then
4182 (Low_Bound (Opnd_Index), Etype (Targ_Index))
4185 (High_Bound (Opnd_Index), Etype (Targ_Index))
4189 elsif Is_Out_Of_Range
4190 (Low_Bound (Opnd_Index), Etype (Targ_Index))
4193 (High_Bound (Opnd_Index), Etype (Targ_Index))
4196 (Compile_Time_Constraint_Error
4197 (Wnode, "value out of range of}?", T_Typ));
4203 (Opnd_Index, Etype (Targ_Index)));
4207 Next_Index (Opnd_Index);
4208 Next_Index (Targ_Index);
4215 -- Construct the test and insert into the tree
4217 if Present (Cond) then
4219 Cond := Guard_Access (Cond, Loc, Ck_Node);
4223 (Make_Raise_Constraint_Error (Loc,
4225 Reason => CE_Range_Check_Failed));
4229 end Selected_Range_Checks;
4231 -------------------------------
4232 -- Storage_Checks_Suppressed --
4233 -------------------------------
4235 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
4237 return Scope_Suppress.Storage_Checks
4238 or else (Present (E) and then Suppress_Storage_Checks (E));
4239 end Storage_Checks_Suppressed;
4241 ---------------------------
4242 -- Tag_Checks_Suppressed --
4243 ---------------------------
4245 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
4247 return Scope_Suppress.Tag_Checks
4248 or else (Present (E) and then Suppress_Tag_Checks (E));
4249 end Tag_Checks_Suppressed;